clubdeuce /
wp-share-this
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Clubdeuce\WPShareThis; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class WP_Share_This |
||
| 7 | * @package Clubdeuce\WPShareThis |
||
| 8 | * |
||
| 9 | * @link http://www.sharethis.com/support/customization/how-to-set-custom-buttons/ |
||
| 10 | */ |
||
| 11 | class WP_Share_This { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var bool |
||
| 15 | */ |
||
| 16 | private static $_facebook_og = true; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private static $_id = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | private static $_services = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * |
||
| 30 | */ |
||
| 31 | public static function initialize() { |
||
| 32 | |||
| 33 | add_action( 'wp_enqueue_scripts', array( __CLASS__, '_wp_enqueue_scripts' ) ); |
||
| 34 | add_action( 'wp_head', array( __CLASS__, '_wp_head' ) ); |
||
| 35 | |||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * |
||
| 40 | */ |
||
| 41 | public static function _wp_enqueue_scripts() { |
||
| 42 | |||
| 43 | $id = self::$_id; |
||
| 44 | |||
| 45 | wp_enqueue_script('sharethis', "//platform-api.sharethis.com/js/sharethis.js#property={$id}&product=unknown", null, false, true ); |
||
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * |
||
| 51 | */ |
||
| 52 | public static function _wp_head() { |
||
| 53 | |||
| 54 | if ( self::$_facebook_og ) { |
||
| 55 | self::_facebook_og(); |
||
| 56 | } |
||
| 57 | |||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The ShareThis account id. |
||
| 62 | * |
||
| 63 | * @param string $id |
||
| 64 | */ |
||
| 65 | public static function register_id( $id ) { |
||
| 66 | |||
| 67 | self::$_id = $id; |
||
| 68 | |||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $service |
||
| 73 | * @param array $params |
||
| 74 | */ |
||
| 75 | public static function register_service( $service, $params = array() ) { |
||
| 76 | |||
| 77 | self::$_services[ $service ] = $params; |
||
| 78 | |||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param \WP_Post|null $post |
||
| 83 | * @param array $args |
||
| 84 | */ |
||
| 85 | public static function the_sharing_links( $post = null, $args = array() ) { |
||
| 86 | |||
| 87 | foreach ( self::$_services as $service => $params ) { |
||
| 88 | $args = array_merge( $params, $args ); |
||
| 89 | self::_render_sharing_link( $args, $service, $post ); |
||
| 90 | } |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param array $params |
||
| 96 | * @param string $service |
||
| 97 | * @param \WP_Post $post |
||
| 98 | */ |
||
| 99 | public static function _render_sharing_link( $params, $service, $post ) { |
||
| 100 | |||
| 101 | $classes = apply_filters( "wpst_link_classes_{$service}", array() ); |
||
| 102 | |||
| 103 | // set some defaults |
||
| 104 | $args = wp_parse_args( $params, array( |
||
| 105 | 'url' => null, |
||
| 106 | 'short_url' => null, |
||
| 107 | 'title' => null, |
||
| 108 | 'image' => null, |
||
| 109 | 'description' => null, |
||
| 110 | 'username' => null, |
||
| 111 | 'message' => null, |
||
| 112 | 'share_count' => true, |
||
| 113 | ) ); |
||
| 114 | |||
| 115 | // if we have a post, we will use post values for the defaults |
||
| 116 | if ( $post instanceof \WP_Post ) { |
||
| 117 | $args = wp_parse_args( $params, array( |
||
| 118 | 'url' => get_permalink( $post ), |
||
| 119 | 'short_url' => null, |
||
| 120 | 'title' => $post->post_title, |
||
| 121 | 'image' => null, |
||
| 122 | 'description' => get_the_excerpt( $post ), |
||
| 123 | 'username' => null, |
||
| 124 | 'message' => null, |
||
| 125 | 'share_count' => true, |
||
| 126 | ) ); |
||
| 127 | |||
| 128 | if ( has_post_thumbnail( $post ) ) { |
||
| 129 | $args['image'] = get_the_post_thumbnail_url( $post ); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | printf( |
||
| 134 | '<div data-network="%1$s" class="st-custom-button %2$s"%3$s%4$s%5$s%6$s%7$s%8$s%9$s>%10$s%11$s</div>', |
||
| 135 | $service, |
||
| 136 | implode( ' ', apply_filters( 'wpst_link_classes', $classes, $service ) ), |
||
| 137 | self::_item_sharing_property( 'url', $args['url'] ), |
||
| 138 | self::_item_sharing_property( 'short_url', $args['short_url'] ), |
||
| 139 | self::_item_sharing_property( 'title', $args['title'] ), |
||
| 140 | self::_item_sharing_property( 'image', $args['image'] ), |
||
| 141 | self::_item_sharing_property( 'description', $args['description'] ), |
||
| 142 | self::_item_sharing_property( 'username', $args['username'] ), |
||
| 143 | self::_item_sharing_property( 'message', $args['message'] ), |
||
| 144 | apply_filters( 'wpst_link_text', ucfirst( $service ) ), |
||
| 145 | self::_item_sharing_count( $args['share_count'] ) |
||
| 146 | ); |
||
| 147 | |||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @link https://developers.facebook.com/docs/sharing/webmasters#basic |
||
| 152 | */ |
||
| 153 | private static function _facebook_og() { |
||
| 154 | |||
| 155 | printf( '<meta property="og:url" content="%1$s" />' . PHP_EOL, self::_og_url() ); |
||
| 156 | print '<meta property="og:type" content="article" />' . PHP_EOL; |
||
| 157 | printf( '<meta property="og:title" content="%1$s" />' . PHP_EOL, self::_og_title() ); |
||
| 158 | |||
| 159 | if ( ! ( empty( $description = self::_og_description() ) ) ) { |
||
| 160 | printf( '<meta property="og:description" content="%1$s" />' . PHP_EOL, $description ); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ( $image_url = self::_og_image() ) { |
||
| 164 | printf( '<meta property="og:image" content="%1$s" />' . PHP_EOL, $image_url ); |
||
| 165 | } |
||
| 166 | |||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return mixed|void |
||
| 171 | */ |
||
| 172 | private static function _og_url() { |
||
| 173 | |||
| 174 | $url = get_permalink(); |
||
| 175 | |||
| 176 | if ( is_home() || is_front_page() ) { |
||
| 177 | $url = home_url(); |
||
| 178 | } |
||
| 179 | |||
| 180 | return apply_filters( 'wpst_og_url', esc_url( $url ) ); |
||
| 181 | |||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return mixed|void |
||
| 186 | */ |
||
| 187 | private static function _og_title() { |
||
| 188 | |||
| 189 | $title = get_the_title(); |
||
| 190 | |||
| 191 | if ( is_home() || is_front_page() ) { |
||
| 192 | $title = get_bloginfo( 'name' ); |
||
| 193 | } |
||
| 194 | |||
| 195 | return apply_filters( 'wpst_og_title', wp_kses_post( $title ) ); |
||
| 196 | |||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return mixed|void |
||
| 201 | */ |
||
| 202 | private static function _og_description() { |
||
| 203 | |||
| 204 | $description = apply_filters( 'the_excerpt', get_the_excerpt() ); |
||
| 205 | |||
| 206 | if ( is_home() || is_front_page() ) { |
||
| 207 | $description = get_bloginfo( 'description' ); |
||
| 208 | } |
||
| 209 | |||
| 210 | return apply_filters( 'wpst_og_description', esc_html( $description ) ); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 211 | |||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return mixed|void |
||
| 216 | */ |
||
| 217 | private static function _og_image() { |
||
| 218 | |||
| 219 | $image_url = get_the_post_thumbnail_url(); |
||
| 220 | |||
| 221 | if ( is_home() || is_front_page() ) { |
||
| 222 | $image_url = ''; |
||
| 223 | } |
||
| 224 | |||
| 225 | return apply_filters( 'wpst_og_image', esc_url( $image_url ) ); |
||
| 226 | |||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $property |
||
| 231 | * @param string $value |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | private static function _item_sharing_property( $property, $value ) { |
||
| 236 | |||
| 237 | $text = ''; |
||
| 238 | $maybe = apply_filters( "wpst_item_{$property}", $value ); |
||
| 239 | |||
| 240 | if ( ! empty( $maybe ) ) { |
||
| 241 | $text = sprintf( ' data-%1$s="%2$s" ', str_replace('_', '-', $property ), esc_attr( $maybe ) ); |
||
| 242 | } |
||
| 243 | |||
| 244 | return $text; |
||
| 245 | |||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param bool $show |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | private static function _item_sharing_count( $show ) { |
||
| 254 | |||
| 255 | $text = ''; |
||
| 256 | |||
| 257 | if ( $show ) { |
||
| 258 | $text = '<span class="count"></span>'; |
||
| 259 | } |
||
| 260 | |||
| 261 | return $text; |
||
| 262 | |||
| 263 | } |
||
| 264 | |||
| 265 | } |
||
| 266 |