Skip to content

Commit 21d49f5

Browse files
authored
Merge pull request #3092 from Parsely/fix/update-metadata-calling-issues
2 parents 5dd93a1 + fc5c365 commit 21d49f5

File tree

3 files changed

+387
-26
lines changed

3 files changed

+387
-26
lines changed

src/UI/class-settings-page.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,6 @@ function (): void {
543543
'title' => __( 'Track Post Types as', 'wp-parsely' ),
544544
'option_key' => $field_id,
545545
'help_text' => $field_help,
546-
'filter' => 'wp_parsely_trackable_statuses',
547546
)
548547
);
549548

src/class-parsely.php

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ public function run(): void {
282282
update_option( self::OPTIONS_KEY, $options );
283283
}
284284

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

288289
/**
@@ -397,12 +398,6 @@ public function insert_page_header_metadata(): void {
397398
* By default,only 'publish' is allowed.
398399
*/
399400
public static function post_has_trackable_status( $post ): bool {
400-
static $cache = array();
401-
$post_id = is_int( $post ) ? $post : $post->ID;
402-
if ( isset( $cache[ $post_id ] ) ) {
403-
return $cache[ $post_id ];
404-
}
405-
406401
/**
407402
* Filters whether the post password check should be skipped when getting
408403
* the post trackable status.
@@ -416,13 +411,11 @@ public static function post_has_trackable_status( $post ): bool {
416411
*/
417412
$skip_password_check = apply_filters( 'wp_parsely_skip_post_password_check', false, $post );
418413
if ( ! $skip_password_check && post_password_required( $post ) ) {
419-
$cache[ $post_id ] = false;
420414
return false;
421415
}
422416

423-
$statuses = self::get_trackable_statuses( $post );
424-
$cache[ $post_id ] = in_array( get_post_status( $post ), $statuses, true );
425-
return $cache[ $post_id ];
417+
$statuses = self::get_trackable_statuses( $post );
418+
return in_array( get_post_status( $post ), $statuses, true );
426419
}
427420

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

446439
/**
447-
* Updates the Parsely metadata endpoint with the new metadata of the post.
440+
* Calls Parse.ly's update metadata endpoint, sending the post's updated
441+
* metadata.
448442
*
449-
* @param int $post_id id of the post to update.
443+
* @param int $post_id The ID of the post to update.
444+
* @return bool True if the metadata endpoint was called, false otherwise.
450445
*/
451-
public function update_metadata_endpoint( int $post_id ): void {
452-
$parsely_options = $this->get_options();
453-
if ( $this->site_id_is_missing() || '' === $parsely_options['metadata_secret'] ) {
454-
return;
446+
public function call_update_metadata_endpoint( int $post_id ): bool {
447+
$options = $this->get_options();
448+
449+
if ( $this->site_id_is_missing() || '' === $options['metadata_secret'] ) {
450+
return false;
451+
}
452+
453+
$current_post_type = get_post_type( $post_id );
454+
if ( false === $current_post_type ) {
455+
return false;
456+
}
457+
458+
$tracked_post_types = array_merge(
459+
$options['track_post_types'],
460+
$options['track_page_types']
461+
);
462+
463+
// Check that the post's type is trackable.
464+
if ( ! in_array( $current_post_type, $tracked_post_types, true ) ) {
465+
return false;
466+
}
467+
468+
// Check that the post's status is trackable.
469+
if ( ! self::post_has_trackable_status( $post_id ) ) {
470+
return false;
455471
}
456472

457473
$post = get_post( $post_id );
458474
if ( null === $post ) {
459-
return;
475+
return false;
476+
}
477+
478+
// Don't call the endpoint when integration tests are running, but
479+
// signal that the above checks have passed.
480+
if ( defined( 'INTEGRATION_TESTS_RUNNING' ) ) {
481+
return true;
460482
}
461483

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

475497
$parsely_api_base_url = Content_API_Service::get_base_url();
476498
$parsely_api_endpoint = $parsely_api_base_url . '/metadata/posts';
477-
$parsely_metadata_secret = $parsely_options['metadata_secret'];
499+
$parsely_metadata_secret = $options['metadata_secret'];
478500

479501
$headers = array( 'Content-Type' => 'application/json' );
480502
$body = wp_json_encode(
@@ -488,22 +510,25 @@ public function update_metadata_endpoint( int $post_id ): void {
488510
/**
489511
* POST request options.
490512
*
491-
* @var WP_HTTP_Request_Args $options
513+
* @var WP_HTTP_Request_Args $request_options
492514
*/
493-
$options = array(
515+
$request_options = array(
494516
'method' => 'POST',
495517
'headers' => $headers,
496518
'blocking' => false,
497519
'body' => $body,
498520
'data_format' => 'body',
499521
);
500522

501-
$response = wp_remote_post( $parsely_api_endpoint, $options );
523+
$response = wp_remote_post( $parsely_api_endpoint, $request_options );
502524

503-
if ( ! is_wp_error( $response ) ) {
504-
$current_timestamp = time();
505-
update_post_meta( $post_id, 'parsely_metadata_last_updated', $current_timestamp );
525+
if ( is_wp_error( $response ) ) {
526+
return false;
506527
}
528+
529+
update_post_meta( $post_id, 'parsely_metadata_last_updated', time() );
530+
531+
return true;
507532
}
508533

509534
/**

0 commit comments

Comments
 (0)