Skip to content

Commit

Permalink
Social | Make post share status immediately available on page load (#…
Browse files Browse the repository at this point in the history
…40301)

* Social | load post share status from initial state

* Phan: No need of array_values

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/12007023901

Upstream-Ref: Automattic/jetpack@7917f09
  • Loading branch information
manzoorwanijk authored and matticbot committed Nov 25, 2024
1 parent e5e994b commit 96897dd
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 154 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is an alpha version! The changes listed here are not final.

### Enhancements
- Google Photos Picker: Adding Google Photos Picker flow backend API endpoints support
- Social | Post share status in the editor is now immediately available on page load
- Subject: update Google Photos external media to support Google Photos Picker API

### Bug fixes
Expand Down
3 changes: 3 additions & 0 deletions jetpack_vendor/automattic/jetpack-publicize/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

This is an alpha version! The changes listed here are not final.

### Added
- Social | Added initial post share status to the initial state

### Changed
- Updated dependencies.
- Updated package dependencies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,20 @@ public static function get_store_initial_state() {

$is_wpcom = ( new Host() )->is_wpcom_platform();

$post = get_post();

$share_status = array();

if ( Utils::should_block_editor_have_social() && $post ) {
$share_status[ $post->ID ] = self::publicize()->get_post_share_status( $post->ID );
}

return array(
'connectionData' => array(
// We do not have this method on WPCOM Publicize class yet.
'connections' => ! $is_wpcom ? self::publicize()->get_all_connections_for_user() : array(),
),
'shareStatus' => array(
// Here goes the share status data for posts with key as post ID.
),
'shareStatus' => $share_status,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,4 +751,48 @@ public function set_post_flags( $flags, $post ) {

return $flags;
}

/**
* Gets the share status for a post.
*
* @param int $post_id The post ID.
*/
public function get_post_share_status( $post_id ) {
$shares = get_post_meta( $post_id, REST_Controller::SOCIAL_SHARES_POST_META_KEY, true );

// If the data is not an array, it means that sharing is not done yet.
$done = is_array( $shares );

if ( $done ) {
// The site could have multiple admins, editors and authors connected. Load shares information that only the current user has access to.
$connection_ids = array_map(
function ( $connection ) {
if ( isset( $connection['connection_id'] ) ) {
return (int) $connection['connection_id'];
}
return 0;
},
$this->get_all_connections_for_user()
);

$shares = array_filter(
$shares,
function ( $share ) use ( $connection_ids ) {
return in_array( (int) $share['connection_id'], $connection_ids, true );
}
);

usort(
$shares,
function ( $a, $b ) {
return $b['timestamp'] - $a['timestamp'];
}
);
}

return array(
'shares' => $done ? $shares : array(),
'done' => $done,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -692,40 +692,10 @@ public function update_post_shares( $request ) {
* @param WP_REST_Request $request The request object.
*/
public function get_post_share_status( WP_REST_Request $request ) {
$post_id = $request->get_param( 'post_id' );

$shares = get_post_meta( $post_id, self::SOCIAL_SHARES_POST_META_KEY, true );

// If the data is not an array, it means that sharing is not done yet.
$done = is_array( $shares );
global $publicize;

if ( $done ) {
// The site could have multiple admins, editors and authors connected. Load shares information that only the current user has access to.
global $publicize;
$connection_ids = array_map(
function ( $connection ) {
if ( isset( $connection['connection_id'] ) ) {
return (int) $connection['connection_id'];
}
return 0;
},
$publicize->get_all_connections_for_user()
);
$shares = array_values(
array_filter(
$shares,
function ( $share ) use ( $connection_ids ) {
return in_array( (int) $share['connection_id'], $connection_ids, true );
}
)
);
}
$post_id = $request->get_param( 'post_id' );

return rest_ensure_response(
array(
'shares' => $done ? $shares : array(),
'done' => $done,
)
);
return rest_ensure_response( $publicize->get_post_share_status( $post_id ) );
}
}
2 changes: 1 addition & 1 deletion jetpack_vendor/i18n-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
),
'jetpack-publicize-pkg' => array(
'path' => 'jetpack_vendor/automattic/jetpack-publicize',
'ver' => '0.56.1-alpha1732342399',
'ver' => '0.56.1-alpha1732526837',
),
'jetpack-search-pkg' => array(
'path' => 'jetpack_vendor/automattic/jetpack-search',
Expand Down
Loading

0 comments on commit 96897dd

Please sign in to comment.