Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Social_Shares
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
342
0.00% covered (danger)
0.00%
0 / 1
 get_social_shares
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
56
 get_the_social_shares
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 get_service_display_name
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Class for the Social Shares.
4 *
5 * @package automattic/jetpack-social-plugin
6 */
7
8use Automattic\Jetpack\Publicize\Share_Status;
9
10/**
11 * Register the Jetpack Social Shares Class.
12 */
13class Social_Shares {
14
15    /**
16     * Return a list of of social shares.
17     *
18     * @param int $post_id The Post ID.
19     *
20     * @return array
21     */
22    public static function get_social_shares( $post_id = null ) {
23
24        $post = get_post( $post_id );
25
26        if ( empty( $post ) ) {
27            return array();
28        }
29
30        $shares = Share_Status::get_post_share_status( $post->ID, false );
31
32        if ( empty( $shares['shares'] ) ) {
33            return array();
34        }
35
36        $succesful_shares = array_filter(
37            $shares['shares'],
38            function ( $share ) {
39                return isset( $share['status'] ) && 'success' === $share['status'];
40            }
41        );
42
43        $shares_by_service = array();
44
45        foreach ( $succesful_shares as $share ) {
46            $service   = $share['service'];
47            $timestamp = $share['timestamp'];
48
49            if ( ! isset( $shares_by_service[ $service ] ) || $timestamp > $shares_by_service[ $service ]['timestamp'] ) {
50                $shares_by_service[ $service ] = $share;
51            }
52        }
53        return $shares_by_service;
54    }
55
56    /**
57     * Return a html to display the social shares.
58     *
59     * @param int $post_id The Post ID.
60     * @return string Markup representing the social share links.
61     */
62    public static function get_the_social_shares( $post_id = 0 ) {
63        $shares = self::get_social_shares( $post_id );
64
65        $html = '<div class="jp_social_shares">';
66
67        if ( ! empty( $shares ) ) {
68            $html .= '<h5>' . __( 'Also on:', 'jetpack-social' ) . '</h5><ul>';
69            foreach ( $shares as $service => $item ) {
70                $message = esc_url( $item['message'] );
71                $html   .= '<li><a href="' . $message . '">' . self::get_service_display_name( $service ) . '</a></li>';
72            }
73            $html .= '</ul>';
74        }
75
76        $html .= '</div>';
77        /**
78         * Apply filters to the social shares data.
79         *
80         * @param string $html The html markup to display the shares.
81         * @param array  $shares  The social shares data.
82         * @param int    $post_id The ID of the post being shared.
83         * @return array The modified $html markup.
84        */
85        return apply_filters(
86            'jp_social_shares',
87            $html,
88            $shares,
89            $post_id
90        );
91    }
92
93    /**
94     * Given a service identify, this returns the name suitable for display.
95     *
96     * @param string $service The name of the social connection provider in small case.
97     * @return string The display name of the social connection provider such as LinkedIn for linkein.
98     */
99    public static function get_service_display_name( $service ) {
100        switch ( $service ) {
101            case 'facebook':
102                return 'Facebook';
103            case 'linkedin':
104                return 'LinkedIn ';
105            case 'instagram-business':
106                return 'Instagram ';
107            case 'nextdoor':
108                return 'Nextdoor';
109            case 'mastodon':
110                return 'Mastodon';
111            case 'tumblr':
112                return 'Tumblr';
113            default:
114                return $service;
115        }
116    }
117}