Skip to content

Commit

Permalink
Fix update metadata endpoint calling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
acicovic committed Jan 21, 2025
1 parent 5dd93a1 commit 3eb17dd
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 26 deletions.
1 change: 0 additions & 1 deletion src/UI/class-settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ function (): void {
'title' => __( 'Track Post Types as', 'wp-parsely' ),
'option_key' => $field_id,
'help_text' => $field_help,
'filter' => 'wp_parsely_trackable_statuses',
)
);

Expand Down
75 changes: 50 additions & 25 deletions src/class-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ public function run(): void {
update_option( self::OPTIONS_KEY, $options );
}

add_action( 'save_post', array( $this, 'update_metadata_endpoint' ) );
// @phpstan-ignore return.void
add_action( 'save_post', array( $this, 'call_update_metadata_endpoint' ) );
}

/**
Expand Down Expand Up @@ -397,12 +398,6 @@ public function insert_page_header_metadata(): void {
* By default,only 'publish' is allowed.
*/
public static function post_has_trackable_status( $post ): bool {
static $cache = array();
$post_id = is_int( $post ) ? $post : $post->ID;
if ( isset( $cache[ $post_id ] ) ) {
return $cache[ $post_id ];
}

/**
* Filters whether the post password check should be skipped when getting
* the post trackable status.
Expand All @@ -416,13 +411,11 @@ public static function post_has_trackable_status( $post ): bool {
*/
$skip_password_check = apply_filters( 'wp_parsely_skip_post_password_check', false, $post );
if ( ! $skip_password_check && post_password_required( $post ) ) {
$cache[ $post_id ] = false;
return false;
}

$statuses = self::get_trackable_statuses( $post );
$cache[ $post_id ] = in_array( get_post_status( $post ), $statuses, true );
return $cache[ $post_id ];
$statuses = self::get_trackable_statuses( $post );
return in_array( get_post_status( $post ), $statuses, true );
}

/**
Expand All @@ -444,19 +437,48 @@ public function construct_parsely_metadata( array $parsely_options, WP_Post $pos
}

/**
* Updates the Parsely metadata endpoint with the new metadata of the post.
* Calls Parse.ly's update metadata endpoint, sending the post's updated
* metadata.
*
* @param int $post_id id of the post to update.
* @param int $post_id The ID of the post to update.
* @return bool True if the metadata endpoint was called, false otherwise.
*/
public function update_metadata_endpoint( int $post_id ): void {
$parsely_options = $this->get_options();
if ( $this->site_id_is_missing() || '' === $parsely_options['metadata_secret'] ) {
return;
public function call_update_metadata_endpoint( int $post_id ): bool {
$options = $this->get_options();

if ( $this->site_id_is_missing() || '' === $options['metadata_secret'] ) {
return false;
}

$current_post_type = get_post_type( $post_id );
if ( false === $current_post_type ) {
return false;
}

$tracked_post_types = array_merge(
$options['track_post_types'],
$options['track_page_types']
);

// Check that the post's type is trackable.
if ( ! in_array( $current_post_type, $tracked_post_types, true ) ) {
return false;
}

// Check that the post's status is trackable.
if ( ! self::post_has_trackable_status( $post_id ) ) {
return false;
}

$post = get_post( $post_id );
if ( null === $post ) {
return;
return false;
}

// Don't call the endpoint when integration tests are running, but
// signal that the above checks have passed.
if ( defined( 'INTEGRATION_TESTS_RUNNING' ) ) {
return true;
}

$metadata = ( new Metadata( $this ) )->construct_metadata( $post );
Expand All @@ -474,7 +496,7 @@ public function update_metadata_endpoint( int $post_id ): void {

$parsely_api_base_url = Content_API_Service::get_base_url();
$parsely_api_endpoint = $parsely_api_base_url . '/metadata/posts';
$parsely_metadata_secret = $parsely_options['metadata_secret'];
$parsely_metadata_secret = $options['metadata_secret'];

$headers = array( 'Content-Type' => 'application/json' );
$body = wp_json_encode(
Expand All @@ -488,22 +510,25 @@ public function update_metadata_endpoint( int $post_id ): void {
/**
* POST request options.
*
* @var WP_HTTP_Request_Args $options
* @var WP_HTTP_Request_Args $request_options
*/
$options = array(
$request_options = array(
'method' => 'POST',
'headers' => $headers,
'blocking' => false,
'body' => $body,
'data_format' => 'body',
);

$response = wp_remote_post( $parsely_api_endpoint, $options );
$response = wp_remote_post( $parsely_api_endpoint, $request_options );

if ( ! is_wp_error( $response ) ) {
$current_timestamp = time();
update_post_meta( $post_id, 'parsely_metadata_last_updated', $current_timestamp );
if ( is_wp_error( $response ) ) {
return false;
}

update_post_meta( $post_id, 'parsely_metadata_last_updated', time() );

return true;
}

/**
Expand Down
Loading

0 comments on commit 3eb17dd

Please sign in to comment.