Skip to content

Commit

Permalink
Allow PCH performance panel to show stats for trackable statuses (#2893)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaurdan authored Oct 30, 2024
1 parent d449778 commit 601414a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url', 'wp-wordcount'), 'version' => 'd6febeb9486e0cff901d');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url', 'wp-wordcount'), 'version' => 'e582fe60e9900e132791');
4 changes: 2 additions & 2 deletions build/content-helper/editor-sidebar.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/@types/assets/window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare global {
wpParselyPostUrl: string;
wpParselySiteId: string,
wpParselySmartLinkingAllowedBlocks: string[];
wpParselyTrackableStatuses: string[];

/**
* Jetpack Editor Initial State.
Expand Down
39 changes: 27 additions & 12 deletions src/class-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,32 @@ public function __construct() {
$this->allow_parsely_remote_requests();
}

/**
* Gets the allowed post statuses for tracking.
*
* Uses the `wp_parsely_trackable_statuses` filter to determine which post statuses are allowed to be tracked.
*
* @since 3.17.0
*
* @param WP_Post|int|null $post The post object.
* @return array<string> The allowed post statuses.
*/
public static function get_trackable_statuses( $post = null ): array {
/**
* Filters the statuses that are permitted to be tracked.
*
* By default, the only status tracked is 'publish'. Use this filter if
* you have other published content that has a different (custom) status.
*
* @since 2.5.0
* @since 3.17.0 Filter extracted to a separate method.
*
* @param string[] $trackable_statuses The list of post statuses that are allowed to be tracked.
* @param WP_Post|int|null $post Which post object or ID is being checked.
*/
return apply_filters( 'wp_parsely_trackable_statuses', array( 'publish' ), $post );
}

/**
* Registers action and filter hook callbacks, and immediately upgrades
* options if needed.
Expand Down Expand Up @@ -394,18 +420,7 @@ public static function post_has_trackable_status( $post ): bool {
return false;
}

/**
* Filters the statuses that are permitted to be tracked.
*
* By default, the only status tracked is 'publish'. Use this filter if
* you have other published content that has a different (custom) status.
*
* @since 2.5.0
*
* @param string[] $trackable_statuses The list of post statuses that are allowed to be tracked.
* @param int|WP_Post $post Which post object or ID is being checked.
*/
$statuses = apply_filters( 'wp_parsely_trackable_statuses', array( 'publish' ), $post );
$statuses = self::get_trackable_statuses( $post );
$cache[ $post_id ] = in_array( get_post_status( $post ), $statuses, true );
return $cache[ $post_id ];
}
Expand Down
10 changes: 9 additions & 1 deletion src/content-helper/editor-sidebar/class-editor-sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,19 @@ public function run(): void {
if ( null !== $parsely_post_url ) {
wp_add_inline_script(
static::get_script_id(),
'wpParselyPostUrl = ' . wp_json_encode( $parsely_post_url ) . ';',
'window.wpParselyPostUrl = ' . wp_json_encode( $parsely_post_url ) . ';',
'before'
);
}

// Inject the trackable statuses.
$trackable_statuses = Parsely::get_trackable_statuses();
wp_add_inline_script(
static::get_script_id(),
'window.wpParselyTrackableStatuses = ' . wp_json_encode( $trackable_statuses ) . ';',
'before'
);

wp_enqueue_style(
static::get_style_id(),
$built_assets_url . 'editor-sidebar.css',
Expand Down
15 changes: 10 additions & 5 deletions src/content-helper/editor-sidebar/performance-stats/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ export class PerformanceStatsProvider extends BaseProvider {
public async getPerformanceStats( period: Period ): Promise<PerformanceData> {
const editor = select( 'core/editor' );

// We cannot show data for non-published posts.
if ( false === editor.isCurrentPostPublished() ) {
// Get the current post's status.
const currentPostStatus = editor.getEditedPostAttribute( 'status' ) ?? 'draft';
const trackableStatuses = window.wpParselyTrackableStatuses ?? [ 'publish' ];

// We cannot show data for non-published posts that are not in the trackable statuses.
if ( ! editor.isCurrentPostPublished() && ! trackableStatuses.includes( currentPostStatus ) ) {
return Promise.reject(
new ContentHelperError( __(
'This post is not published, so its details are unavailable.',
Expand Down Expand Up @@ -118,11 +122,12 @@ export class PerformanceStatsProvider extends BaseProvider {

// No data was returned.
if ( response.length === 0 ) {
const postTitle = select( 'core/editor' ).getEditedPostAttribute( 'title' ) ?? '';
return Promise.reject( new ContentHelperError(
sprintf(
/* translators: URL of the published post */
__( 'The post %d has 0 views, or the Parse.ly API returned no data.',
'wp-parsely' ), postId
/* translators: Title of the published post */
__( '<strong>%s</strong> has 0 views, or the Parse.ly API returned no data.',
'wp-parsely' ), postTitle
), ContentHelperErrorCode.ParselyApiReturnedNoData, ''
) );
}
Expand Down

0 comments on commit 601414a

Please sign in to comment.