Skip to content

Commit

Permalink
Social | load post share status from initial state
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk committed Nov 22, 2024
1 parent f1ad00d commit 6a274c7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Social | Added initial post share status to the initial state
12 changes: 9 additions & 3 deletions projects/packages/publicize/src/class-publicize-script-data.php
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
46 changes: 46 additions & 0 deletions projects/packages/publicize/src/class-publicize.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,4 +751,50 @@ 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'];
}
);

$shares = array_values( $shares );

Check failure on line 792 in projects/packages/publicize/src/class-publicize.php

View workflow job for this annotation

GitHub Actions / Static analysis

NOOPError PhanRedundantArrayValuesCall Attempting to convert list<mixed> to a list using \array_values() (it is already a list)
}

return array(
'shares' => $done ? $shares : array(),
'done' => $done,
);
}
}
36 changes: 3 additions & 33 deletions projects/packages/publicize/src/class-rest-controller.php
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 ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Social | Post share status in the editor is now immediately available on page load
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Post share status in the editor is now immediately available on page load

0 comments on commit 6a274c7

Please sign in to comment.