Skip to content

Commit

Permalink
Social: adds hook for social shares URLs upon save (#39398)
Browse files Browse the repository at this point in the history
* Social: adds hook for social shares URLs upon save
  • Loading branch information
kraftbj authored Sep 16, 2024
1 parent 6504c21 commit 5d6173e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions projects/packages/publicize/changelog/add-share-save-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Social: adds hook for plugin developers to be able to pull social share URLs on save.
19 changes: 19 additions & 0 deletions projects/packages/publicize/src/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,25 @@ public function update_post_shares( $request ) {

if ( $post && 'publish' === $post->post_status && isset( $post_meta[ self::SOCIAL_SHARES_POST_META_KEY ] ) ) {
update_post_meta( $post_id, self::SOCIAL_SHARES_POST_META_KEY, $post_meta[ self::SOCIAL_SHARES_POST_META_KEY ] );
$urls = array();
foreach ( $post_meta[ self::SOCIAL_SHARES_POST_META_KEY ] as $share ) {
if ( isset( $share['status'] ) && 'success' === $share['status'] ) {
$urls[] = array(
'url' => $share['message'],
'service' => $share['service'],
);
}
}
/**
* Fires after Publicize Shares post meta has been saved.
*
* @param array $urls {
* An array of social media shares.
* @type array $url URL to the social media post.
* @type string $service Social media service shared to.
* }
*/
do_action( 'jetpack_publicize_share_urls_saved', $urls );
return rest_ensure_response( new WP_REST_Response() );
}

Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/social/changelog/add-share-save-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Social: adds hook for plugin developers to be able to pull social share URLs on save.
11 changes: 11 additions & 0 deletions projects/plugins/social/src/class-rest-social-note-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public function update_post_shares( $request ) {

if ( $post && $post->post_type === Note::JETPACK_SOCIAL_NOTE_CPT && $post->post_status === 'publish' && isset( $post_meta[ self::SOCIAL_SHARES_POST_META_KEY ] ) ) {
update_post_meta( $post_id, self::SOCIAL_SHARES_POST_META_KEY, $post_meta[ self::SOCIAL_SHARES_POST_META_KEY ] );
$urls = array();
foreach ( $post_meta[ self::SOCIAL_SHARES_POST_META_KEY ] as $share ) {
if ( isset( $share['status'] ) && 'success' === $share['status'] ) {
$urls[] = array(
'url' => $share['message'],
'service' => $share['service'],
);
}
}
/** This action is documented in src/class-rest-controller.php */
do_action( 'jetpack_publicize_share_urls_saved', $urls );
return rest_ensure_response( new WP_REST_Response() );
}

Expand Down

0 comments on commit 5d6173e

Please sign in to comment.