Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| Share_Status | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
9 | |
0.00% |
0 / 1 |
| get_post_share_status | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
9 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Publicize Share Status class. |
| 4 | * |
| 5 | * @package automattic/jetpack-publicize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Publicize; |
| 9 | |
| 10 | /** |
| 11 | * Publicize Services class. |
| 12 | */ |
| 13 | class Share_Status { |
| 14 | const SHARES_META_KEY = '_publicize_shares'; |
| 15 | |
| 16 | /** |
| 17 | * Gets the share status for a post. |
| 18 | * |
| 19 | * @param int $post_id The post ID. |
| 20 | * @param bool $filter_by_access Whether to filter the shares by access. |
| 21 | */ |
| 22 | public static function get_post_share_status( $post_id, $filter_by_access = true ) { |
| 23 | |
| 24 | $post = get_post( $post_id ); |
| 25 | |
| 26 | if ( empty( $post ) || ! metadata_exists( 'post', $post_id, self::SHARES_META_KEY ) ) { |
| 27 | return array( |
| 28 | 'done' => false, |
| 29 | 'shares' => array(), |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | $shares = get_post_meta( $post_id, self::SHARES_META_KEY, true ); |
| 34 | |
| 35 | // If the data is in an associative array format, we fetch it without true to get all the shares. |
| 36 | // This is needed to support the old WPCOM format. |
| 37 | if ( isset( $shares ) && is_array( $shares ) && ! array_is_list( $shares ) ) { |
| 38 | $shares = get_post_meta( $post_id, self::SHARES_META_KEY, false ); |
| 39 | } |
| 40 | |
| 41 | // Better safe than sorry. |
| 42 | if ( empty( $shares ) ) { |
| 43 | $shares = array(); |
| 44 | } |
| 45 | |
| 46 | if ( $filter_by_access ) { |
| 47 | // The site could have multiple admins, editors and authors connected. Load shares information that only the current user has access to. |
| 48 | $connection_ids = wp_list_pluck( Connections::get_all_for_user(), 'connection_id', 'connection_id' ); |
| 49 | |
| 50 | $shares = array_filter( |
| 51 | $shares, |
| 52 | function ( $share ) use ( $connection_ids ) { |
| 53 | return isset( $share['connection_id'] ) && isset( $connection_ids[ $share['connection_id'] ] ); |
| 54 | } |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | usort( |
| 59 | $shares, |
| 60 | function ( $a, $b ) { |
| 61 | return $b['timestamp'] - $a['timestamp']; |
| 62 | } |
| 63 | ); |
| 64 | |
| 65 | $shares = array_values( $shares ); |
| 66 | |
| 67 | return array( |
| 68 | 'shares' => $shares, |
| 69 | 'done' => true, |
| 70 | ); |
| 71 | } |
| 72 | } |