Skip to content
Open
4 changes: 4 additions & 0 deletions projects/packages/post-list/changelog/remove-share-action
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Moved Share post action to the Publicize package for better discoverability. The `jetpack_post_list_display_share_action` filter is now handled by Publicize.
54 changes: 0 additions & 54 deletions projects/packages/post-list/src/class-post-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,6 @@ public function maybe_customize_columns( $post_type ) {
}
}

/**
* Checks the current post type and adds the Share post
* action if it is appropriate to do so.
*
* @param string $post_type The post type associated with the current request.
*/
public function maybe_add_share_action( $post_type ) {
// Add Share action if post type supports 'publicize', uses the 'block editor', and the feature flag is true.
if (
post_type_supports( $post_type, 'publicize' ) &&
use_block_editor_for_post_type( $post_type ) &&
/**
* Determine whether we should show the share action for this post type.
* The default is false.
*
* @since 0.2.0
*
* @param boolean Whether we should show the share action for this post type.
* @param string The current post type.
*/
apply_filters( 'jetpack_post_list_display_share_action', false, $post_type )
) {
// Add Share post action.
add_filter( 'post_row_actions', array( $this, 'add_share_action' ), 20, 2 );
add_filter( 'page_row_actions', array( $this, 'add_share_action' ), 20, 2 );
}
}

/**
* If the current_screen has 'edit' as the base, add filters and actions to change the post list tables.
*
Expand All @@ -163,7 +135,6 @@ public function add_filters_and_actions_for_screen( $current_screen ) {
}

$this->maybe_customize_columns( $current_screen->post_type );
$this->maybe_add_share_action( $current_screen->post_type );
$this->maybe_add_copy_link_action( $current_screen->post_type );
}

Expand Down Expand Up @@ -215,35 +186,10 @@ public function add_filters_and_actions_quick_edit() {
if ( ! empty( $_POST['screen'] ) && str_starts_with( sanitize_key( $_POST['screen'] ), 'edit-' ) && ! empty( $_POST['post_type'] ) ) {
$type = sanitize_key( $_POST['post_type'] );
$this->maybe_customize_columns( $type );
$this->maybe_add_share_action( $type );
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
}

/**
* Adds the Share post action which links to the editor with the sidebar open.
*
* @param array $post_actions The current array of post actions.
* @param WP_Post $post The current post in the post list table.
*
* @return array The modified post actions array.
*/
public function add_share_action( $post_actions, $post ) {
$edit_url = get_edit_post_link( $post->ID, 'raw' );
if ( ! $edit_url || 'publish' !== $post->post_status ) {
// Do nothing since we do not have an edit URL to work with.
return $post_actions;
}

$url = add_query_arg( 'jetpack-editor-action', 'share_post', $edit_url );
$text = _x( 'Share', 'Share the post on social networks', 'jetpack-post-list' );
$title = _draft_or_post_title( $post );
/* translators: post title */
$label = sprintf( __( 'Share “%s” via Jetpack Social', 'jetpack-post-list' ), $title );
$post_actions['share'] = sprintf( '<a href="%s" aria-label="%s">%s</a>', esc_url( $url ), esc_attr( $label ), esc_html( $text ) );
return $post_actions;
}

/**
* Adds a new column header for displaying the thumbnail of a post.
*
Expand Down
63 changes: 11 additions & 52 deletions projects/packages/post-list/tests/php/Post_List_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function confirm_add_filters_and_actions_for_screen_starts_clean() {

/**
* Test add_filters_and_actions_for_screen() with "Pages".
* Thumbnail should show up on "Pages", but Share should not, because 'publicize' is not supported on "Pages".
* Thumbnail and Copy link should show up on "Pages".
*/
public function test_add_filters_and_actions_for_screen_thumbnail() {
$this->confirm_add_filters_and_actions_for_screen_starts_clean();
Expand All @@ -94,28 +94,22 @@ public function test_add_filters_and_actions_for_screen_thumbnail() {
'post_type' => 'page',
);

// Turn on the flag that allows the Share action if applicable.
add_filter( 'jetpack_post_list_display_share_action', '__return_true' );

$post_list->add_filters_and_actions_for_screen( $current_screen );

// Assert that our style, filter, and action has been added.
// Assert that thumbnail columns and copy link actions have been added.
$this->assertTrue( has_filter( 'manage_posts_columns' ) );
$this->assertTrue( has_action( 'manage_posts_custom_column' ) );
$this->assertTrue( has_filter( 'manage_pages_columns' ) );
$this->assertTrue( has_action( 'manage_pages_custom_column' ) );
$this->assertFalse( has_action( 'post_row_actions', array( $post_list, 'add_share_action' ) ) );
$this->assertFalse( has_action( 'page_row_actions', array( $post_list, 'add_share_action' ) ) );
$this->assertNotFalse( has_action( 'post_row_actions', array( $post_list, 'add_copy_link_action' ) ) );
$this->assertNotFalse( has_action( 'page_row_actions', array( $post_list, 'add_copy_link_action' ) ) );
}

/**
* Test add_filters_and_actions_for_screen() with a custom post type.
* Thumbnail should ONLY show up on "Posts" and "Pages". However, the Share action should show up on custom post
* types that support publicize and the block editor.
* Thumbnail should ONLY show up on "Posts" and "Pages", but copy link should show up.
*/
public function test_add_filters_and_actions_for_screen_share() {
public function test_add_filters_and_actions_for_screen_custom_post_type() {
$this->confirm_add_filters_and_actions_for_screen_starts_clean();
$post_list = Post_List::configure();

Expand All @@ -124,7 +118,8 @@ public function test_add_filters_and_actions_for_screen_share() {
'post_type_we_made_up',
array(
'show_in_rest' => true,
'supports' => array( 'editor', 'publicize' ),
'public' => true,
'supports' => array( 'editor' ),
)
);

Expand All @@ -134,12 +129,9 @@ public function test_add_filters_and_actions_for_screen_share() {
'post_type' => 'post_type_we_made_up',
);

// Turn on the flag that allows the Share action if applicable.
add_filter( 'jetpack_post_list_display_share_action', '__return_true' );

$post_list->add_filters_and_actions_for_screen( $current_screen );

// Assert that only the Share action was enabled.
// Assert that only copy link action was enabled (no thumbnail columns for custom post types).
$this->assertFalse( has_filter( 'manage_posts_columns' ) );
$this->assertFalse( has_action( 'manage_posts_custom_column' ) );
$this->assertFalse( has_filter( 'manage_pages_columns' ) );
Expand All @@ -150,63 +142,30 @@ public function test_add_filters_and_actions_for_screen_share() {

/**
* Test the add_filters_and_actions_for_screen() with "Posts".
* The thumbnail and Share action should be available on "Posts".
*/
public function test_add_filters_and_actions_for_screen_thumbnail_and_share() {
$this->confirm_add_filters_and_actions_for_screen_starts_clean();
$post_list = Post_List::configure();

$current_screen = (object) array(
'base' => 'edit',
'post_type' => 'post',
);
add_post_type_support( 'post', 'publicize' );

// Turn on the flag that allows the Share action if applicable.
add_filter( 'jetpack_post_list_display_share_action', '__return_true' );

$post_list->add_filters_and_actions_for_screen( $current_screen );

// Assert that our style, filter, and action has been added.
$this->assertTrue( has_filter( 'manage_posts_columns' ) );
$this->assertTrue( has_action( 'manage_posts_custom_column' ) );
$this->assertTrue( has_filter( 'manage_pages_columns' ) );
$this->assertTrue( has_action( 'manage_pages_custom_column' ) );
$this->assertTrue( has_action( 'post_row_actions' ) );
$this->assertTrue( has_action( 'page_row_actions' ) );
}

/**
* Test the add_filters_and_actions_for_screen() with "Posts".
* The thumbnail and Share action should be available on "Posts", but we don't set the share flag to true, so only
* thumbnails show up.
* Thumbnail and copy link should be available on "Posts".
*/
public function test_add_filters_and_actions_for_screen_share_flag_disabled() {

public function test_add_filters_and_actions_for_screen_posts() {
$this->confirm_add_filters_and_actions_for_screen_starts_clean();
$post_list = Post_List::configure();

$current_screen = (object) array(
'base' => 'edit',
'post_type' => 'post',
);
add_post_type_support( 'post', 'publicize' );

$post_list->add_filters_and_actions_for_screen( $current_screen );

// Assert that our style, filter, and action has been added.
// Assert that thumbnail columns and copy link actions have been added.
$this->assertTrue( has_filter( 'manage_posts_columns' ) );
$this->assertTrue( has_action( 'manage_posts_custom_column' ) );
$this->assertTrue( has_filter( 'manage_pages_columns' ) );
$this->assertTrue( has_action( 'manage_pages_custom_column' ) );
$this->assertFalse( has_action( 'post_row_actions', array( $post_list, 'add_share_action' ) ) );
$this->assertFalse( has_action( 'page_row_actions', array( $post_list, 'add_share_action' ) ) );
$this->assertNotFalse( has_action( 'post_row_actions', array( $post_list, 'add_copy_link_action' ) ) );
$this->assertNotFalse( has_action( 'page_row_actions', array( $post_list, 'add_copy_link_action' ) ) );
}

/**
* Test the add_filters_and_actions_for_screen() method doesn't add thumbnails or Share if screen not 'edit' base.
* Test the add_filters_and_actions_for_screen() method doesn't add thumbnails or copy link if screen not 'edit' base.
*/
public function test_add_filters_and_actions_for_screen_wrong_screen() {
$post_list = Post_List::configure();
Expand Down
4 changes: 4 additions & 0 deletions projects/packages/publicize/changelog/add-share-post-action
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added Share post action to the post list screen. Shows automatically for plans with republicize support, and supports the `jetpack_post_list_display_share_action` filter for custom overrides.
58 changes: 56 additions & 2 deletions projects/packages/publicize/src/class-publicize-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,63 @@ public static function add_filters_and_actions_for_screen( $current_screen ) {
return;
}

if ( Current_Plan::supports( 'republicize' ) ) {
add_filter( 'jetpack_post_list_display_share_action', '__return_true' );
/**
* Filter to enable/disable the Share action on the post list screen.
*
* The Share action allows users to reshare published posts via Jetpack Social.
* It is automatically enabled for plans that support the 'republicize' feature,
* but can be disabled via this filter.
*
* @since 0.2.0 Originally in jetpack-post-list package.
* @since $$NEXT_VERSION$$ Moved to jetpack-publicize package.
*
* @param bool $show_share Whether to show the share action. Default true.
* @param string $post_type The current post type.
*/
$show_share_action = Current_Plan::supports( 'republicize' )
&& apply_filters( 'jetpack_post_list_display_share_action', true, $current_screen->post_type );

if ( $show_share_action ) {
self::maybe_add_share_action( $current_screen->post_type );
}
}

/**
* Add the Share action for post types that support publicize.
*
* @param string $post_type The post type.
*/
public static function maybe_add_share_action( $post_type ) {
if (
post_type_supports( $post_type, 'publicize' ) &&
use_block_editor_for_post_type( $post_type )
) {
add_filter( 'post_row_actions', array( self::class, 'add_share_action' ), 20, 2 );
add_filter( 'page_row_actions', array( self::class, 'add_share_action' ), 20, 2 );
}
}

/**
* Add the Share action link to the post row actions.
*
* @param array $post_actions The current post actions.
* @param \WP_Post $post The post object.
* @return array Modified post actions.
*/
public static function add_share_action( $post_actions, $post ) {
$edit_url = get_edit_post_link( $post->ID, 'raw' );
if ( ! $edit_url || 'publish' !== $post->post_status ) {
return $post_actions;
}

$url = add_query_arg( 'jetpack-editor-action', 'share_post', $edit_url );
$text = _x( 'Share', 'Share the post on social networks', 'jetpack-publicize-pkg' );
$title = _draft_or_post_title( $post );
/* translators: post title */
$label = sprintf( __( 'Share "%s" via Jetpack Social', 'jetpack-publicize-pkg' ), $title );
$post_actions['share'] = sprintf( '<a href="%s" aria-label="%s">%s</a>', esc_url( $url ), esc_attr( $label ), esc_html( $text ) );

return $post_actions;
}

/**
Expand Down
Loading
Loading