Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Module Name: Likes |
||
| 4 | * Module Description: Give visitors an easy way to show they appreciate your content. |
||
| 5 | * First Introduced: 2.2 |
||
| 6 | * Sort Order: 23 |
||
| 7 | * Requires Connection: Yes |
||
| 8 | * Auto Activate: No |
||
| 9 | * Module Tags: Social |
||
| 10 | * Feature: Engagement |
||
| 11 | * Additional Search Queries: like, likes, wordpress.com |
||
| 12 | */ |
||
| 13 | |||
| 14 | Jetpack::dns_prefetch( array( |
||
| 15 | '//widgets.wp.com', |
||
| 16 | '//s0.wp.com', |
||
| 17 | '//0.gravatar.com', |
||
| 18 | '//1.gravatar.com', |
||
| 19 | '//2.gravatar.com', |
||
| 20 | ) ); |
||
| 21 | |||
| 22 | class Jetpack_Likes { |
||
| 23 | public $version = '20160429'; |
||
| 24 | |||
| 25 | public static function init() { |
||
| 26 | static $instance = NULL; |
||
| 27 | |||
| 28 | if ( ! $instance ) { |
||
| 29 | $instance = new Jetpack_Likes; |
||
| 30 | } |
||
| 31 | |||
| 32 | return $instance; |
||
| 33 | } |
||
| 34 | |||
| 35 | function __construct() { |
||
| 36 | $this->in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true; |
||
| 37 | |||
| 38 | add_action( 'init', array( &$this, 'action_init' ) ); |
||
| 39 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
| 40 | |||
| 41 | if ( $this->in_jetpack ) { |
||
| 42 | add_action( 'jetpack_activate_module_likes', array( $this, 'set_social_notifications_like' ) ); |
||
| 43 | add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) ); |
||
| 44 | |||
| 45 | Jetpack::enable_module_configurable( __FILE__ ); |
||
| 46 | Jetpack::module_configuration_load( __FILE__, array( $this, 'configuration_redirect' ) ); |
||
| 47 | |||
| 48 | add_action('admin_print_scripts-settings_page_sharing', array( &$this, 'load_jp_css' ) ); |
||
| 49 | add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) ); |
||
| 50 | |||
| 51 | $active = Jetpack::get_active_modules(); |
||
| 52 | |||
| 53 | View Code Duplication | if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) { |
|
| 54 | add_action( 'admin_menu', array( $this, 'sharing_menu' ) ); // we don't have a sharing page yet |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) { |
||
| 58 | add_action( 'pre_admin_screen_sharing', array( $this, 'sharing_block' ), 20 ); // we have a sharing page but not the global options area |
||
| 59 | add_action( 'pre_admin_screen_sharing', array( $this, 'updated_message' ), -10 ); |
||
| 60 | } |
||
| 61 | |||
| 62 | if( ! in_array( 'sharedaddy', $active ) ) { |
||
| 63 | add_action( 'admin_init', array( $this, 'process_update_requests_if_sharedaddy_not_loaded' ) ); |
||
| 64 | add_action( 'sharing_global_options', array( $this, 'admin_settings_showbuttonon_init' ), 19 ); |
||
| 65 | add_action( 'sharing_admin_update', array( $this, 'admin_settings_showbuttonon_callback' ), 19 ); |
||
| 66 | add_action( 'admin_init', array( $this, 'add_meta_box' ) ); |
||
| 67 | } else { |
||
| 68 | add_filter( 'sharing_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) ); |
||
| 69 | add_action( 'start_sharing_meta_box_content', array( $this, 'meta_box_content' ) ); |
||
| 70 | } |
||
| 71 | } else { // wpcom |
||
| 72 | add_action( 'wpmu_new_blog', array( $this, 'enable_comment_likes' ), 10, 1 ); |
||
| 73 | add_action( 'admin_init', array( $this, 'add_meta_box' ) ); |
||
| 74 | add_action( 'end_likes_meta_box_content', array( $this, 'sharing_meta_box_content' ) ); |
||
| 75 | add_filter( 'likes_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) ); |
||
| 76 | } |
||
| 77 | |||
| 78 | add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications |
||
| 79 | |||
| 80 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 ); |
||
| 81 | |||
| 82 | add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) ); |
||
| 83 | |||
| 84 | add_action( 'save_post', array( $this, 'meta_box_save' ) ); |
||
| 85 | add_action( 'edit_attachment', array( $this, 'meta_box_save' ) ); |
||
| 86 | add_action( 'sharing_global_options', array( $this, 'admin_settings_init' ), 20 ); |
||
| 87 | add_action( 'sharing_admin_update', array( $this, 'admin_settings_callback' ), 20 ); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set the social_notifications_like option to `on` when the Likes module is activated. |
||
| 92 | * |
||
| 93 | * @since 3.7.0 |
||
| 94 | * |
||
| 95 | * @return null |
||
| 96 | */ |
||
| 97 | function set_social_notifications_like() { |
||
| 98 | update_option( 'social_notifications_like', 'on' ); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Delete the social_notifications_like option that was set to `on` on module activation. |
||
| 103 | * |
||
| 104 | * @since 3.7.0 |
||
| 105 | * |
||
| 106 | * @return null |
||
| 107 | */ |
||
| 108 | function delete_social_notifications_like() { |
||
| 109 | delete_option( 'social_notifications_like' ); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Redirects to the likes section of the sharing page. |
||
| 114 | */ |
||
| 115 | function configuration_redirect() { |
||
| 116 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) ); |
||
| 117 | die(); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable |
||
| 122 | */ |
||
| 123 | function load_jp_css() { |
||
| 124 | // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. |
||
| 125 | // Jetpack::init()->admin_styles(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Load scripts and styles for front end. |
||
| 130 | * @return null |
||
| 131 | */ |
||
| 132 | function load_styles_register_scripts() { |
||
| 133 | if ( $this->in_jetpack ) { |
||
| 134 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 135 | $this->register_scripts(); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box |
||
| 141 | * @param string $html row heading for the sharedaddy "which page" setting |
||
| 142 | * @return string html with the jetpack-targetable class and likes id. tbody gets closed after the like settings |
||
| 143 | */ |
||
| 144 | function configuration_target_area( $html = '' ) { |
||
| 145 | $html = "<tbody id='likes' class='jetpack-targetable'>" . $html; |
||
| 146 | return $html; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares" |
||
| 151 | */ |
||
| 152 | function add_likes_to_sharing_meta_box_title() { |
||
| 153 | return __( 'Likes and Shares', 'jetpack' ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Adds a metabox to the post screen if the sharing one doesn't currently exist. |
||
| 158 | */ |
||
| 159 | function add_meta_box() { |
||
| 160 | if ( |
||
| 161 | /** |
||
| 162 | * Allow disabling of the Likes metabox on the post editor screen. |
||
| 163 | * |
||
| 164 | * @module likes |
||
| 165 | * |
||
| 166 | * @since 2.2.0 |
||
| 167 | * |
||
| 168 | * @param bool false Should the Likes metabox be disabled? Default to false. |
||
| 169 | */ |
||
| 170 | apply_filters( 'post_flair_disable', false ) |
||
| 171 | ) { |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | |||
| 175 | $post_types = get_post_types( array( 'public' => true ) ); |
||
| 176 | /** |
||
| 177 | * Filters the Likes metabox title. |
||
| 178 | * |
||
| 179 | * @module likes |
||
| 180 | * |
||
| 181 | * @since 2.2.0 |
||
| 182 | * |
||
| 183 | * @param string Likes metabox title. Default to "Likes". |
||
| 184 | */ |
||
| 185 | $title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) ); |
||
| 186 | foreach( $post_types as $post_type ) { |
||
| 187 | add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'advanced', 'high' ); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | function meta_box_save( $post_id ) { |
||
| 192 | if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) |
||
| 193 | return $post_id; |
||
| 194 | |||
| 195 | if ( empty( $_POST['wpl_like_status_hidden'] ) ) |
||
| 196 | return $post_id; |
||
| 197 | |||
| 198 | // Record sharing disable. Only needs to be done for WPCOM |
||
| 199 | if ( ! $this->in_jetpack ) { |
||
| 200 | if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ) ) ) { |
||
| 201 | View Code Duplication | if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) { |
|
| 202 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
||
| 203 | } else { |
||
| 204 | delete_post_meta( $post_id, 'sharing_disabled' ); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | if ( 'post' == $_POST['post_type'] ) { |
||
| 210 | if ( !current_user_can( 'edit_post', $post_id ) ) { |
||
| 211 | return $post_id; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | // Record a change in like status for this post - only if it contradicts the |
||
| 216 | // site like setting. |
||
| 217 | if ( ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) || ( ! $this->is_enabled_sitewide() && !empty( $_POST['wpl_enable_post_likes'] ) ) ) { |
||
| 218 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 219 | } else { |
||
| 220 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 221 | } |
||
| 222 | |||
| 223 | return $post_id; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Shows the likes option in the post screen metabox. |
||
| 228 | */ |
||
| 229 | function meta_box_content( $post ) { |
||
| 230 | $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); |
||
| 231 | $checked = true; |
||
| 232 | $disabled = ! $this->is_enabled_sitewide(); |
||
| 233 | $switched_status = get_post_meta( $post_id, 'switch_like_status', true ); |
||
| 234 | |||
| 235 | if ( $disabled && empty( $switched_status ) || false == $disabled && !empty( $switched_status ) ) |
||
| 236 | $checked = false; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Fires before the Likes meta box content in the post editor. |
||
| 240 | * |
||
| 241 | * @module likes |
||
| 242 | * |
||
| 243 | * @since 2.2.0 |
||
| 244 | * |
||
| 245 | * @param WP_Post|array|null $post Post data. |
||
| 246 | */ |
||
| 247 | do_action( 'start_likes_meta_box_content', $post ); |
||
| 248 | ?> |
||
| 249 | |||
| 250 | <p> |
||
| 251 | <label for="wpl_enable_post_likes"> |
||
| 252 | <input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>> |
||
| 253 | <?php esc_html_e( 'Show likes.', 'jetpack' ); ?> |
||
| 254 | </label> |
||
| 255 | <input type="hidden" name="wpl_like_status_hidden" value="1" /> |
||
| 256 | </p> <?php |
||
| 257 | /** |
||
| 258 | * Fires after the Likes meta box content in the post editor. |
||
| 259 | * |
||
| 260 | * @module likes |
||
| 261 | * |
||
| 262 | * @since 2.2.0 |
||
| 263 | * |
||
| 264 | * @param WP_Post|array|null $post Post data. |
||
| 265 | */ |
||
| 266 | do_action( 'end_likes_meta_box_content', $post ); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog) |
||
| 271 | */ |
||
| 272 | function sharing_meta_box_content( $post ) { |
||
| 273 | $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); |
||
| 274 | $disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?> |
||
| 275 | <p> |
||
| 276 | <label for="wpl_enable_post_sharing"> |
||
| 277 | <input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( !$disabled ); ?>> |
||
| 278 | <?php _e( 'Show sharing buttons.', 'jetpack' ); ?> |
||
| 279 | </label> |
||
| 280 | <input type="hidden" name="wpl_sharing_status_hidden" value="1" /> |
||
| 281 | </p> <?php |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page) |
||
| 286 | */ |
||
| 287 | |||
| 288 | View Code Duplication | function admin_discussion_likes_settings_init() { |
|
| 289 | // Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings |
||
| 290 | add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' ); |
||
| 291 | add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' ); |
||
| 292 | // Register the setting |
||
| 293 | register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) ); |
||
| 294 | } |
||
| 295 | |||
| 296 | function admin_discussion_likes_settings_section() { |
||
| 297 | // Atypical usage here. We emit jquery to move likes notification checkbox to be with the rest of the email notification settings |
||
| 298 | ?> |
||
| 299 | <script type="text/javascript"> |
||
| 300 | jQuery( function( $ ) { |
||
| 301 | var table = $( '#social_notifications_like' ).parents( 'table:first' ), |
||
| 302 | header = table.prevAll( 'h2:first' ), |
||
| 303 | newParent = $( '#moderation_notify' ).parent( 'label' ).parent(); |
||
| 304 | |||
| 305 | if ( !table.length || !header.length || !newParent.length ) { |
||
| 306 | return; |
||
| 307 | } |
||
| 308 | |||
| 309 | newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() ); |
||
| 310 | header.remove(); |
||
| 311 | table.remove(); |
||
| 312 | } ); |
||
| 313 | </script> |
||
| 314 | <?php |
||
| 315 | } |
||
| 316 | |||
| 317 | function admin_likes_get_option( $option ) { |
||
| 318 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 319 | $option_setting = get_blog_option( get_current_blog_id(), $option, 'on' ); |
||
| 320 | } else { |
||
| 321 | $option_setting = get_option( $option, 'on' ); |
||
| 322 | } |
||
| 323 | |||
| 324 | return intval( 'on' == $option_setting ); |
||
| 325 | } |
||
| 326 | |||
| 327 | function admin_discussion_likes_settings_field() { |
||
| 328 | $like = $this->admin_likes_get_option( 'social_notifications_like' ); |
||
| 329 | ?> |
||
| 330 | <label><input type="checkbox" id="social_notifications_like" name="social_notifications_like" value="1" <?php checked( $like ); ?> /> <?php esc_html_e( 'Someone likes one of my posts', 'jetpack' ); ?></label> |
||
| 331 | <?php |
||
| 332 | } |
||
| 333 | |||
| 334 | function admin_discussion_likes_settings_validate( $input ) { |
||
| 335 | // If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'. |
||
| 336 | if ( !$input || 'off' == $input ) |
||
| 337 | return 'off'; |
||
| 338 | |||
| 339 | // Otherwise, return 'on'. |
||
| 340 | return 'on'; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * The actual options block to be inserted into the sharing page. |
||
| 345 | */ |
||
| 346 | function admin_settings_init() { ?> |
||
| 347 | <tr> |
||
| 348 | <th scope="row"> |
||
| 349 | <label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label> |
||
| 350 | </th> |
||
| 351 | <td> |
||
| 352 | <div> |
||
| 353 | <label> |
||
| 354 | <input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> /> |
||
| 355 | <?php esc_html_e( 'On for all posts', 'jetpack' ); ?> |
||
| 356 | </label> |
||
| 357 | </div> |
||
| 358 | <div> |
||
| 359 | <label> |
||
| 360 | <input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> /> |
||
| 361 | <?php esc_html_e( 'Turned on per post', 'jetpack' ); ?> |
||
| 362 | </label> |
||
| 363 | <div> |
||
| 364 | </td> |
||
| 365 | </tr> |
||
| 366 | <?php if ( ! $this->in_jetpack ) : ?> |
||
| 367 | <tr> |
||
| 368 | <th scope="row"> |
||
| 369 | <label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label> |
||
| 370 | </th> |
||
| 371 | <td> |
||
| 372 | <div> |
||
| 373 | <label> |
||
| 374 | <input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> /> |
||
| 375 | <?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?> |
||
| 376 | </label> |
||
| 377 | </div> |
||
| 378 | <div> |
||
| 379 | <label> |
||
| 380 | <input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> /> |
||
| 381 | <?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?> |
||
| 382 | </label> |
||
| 383 | <div> |
||
| 384 | </td> |
||
| 385 | </tr> |
||
| 386 | <tr> |
||
| 387 | <th scope="row"> |
||
| 388 | <label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label> |
||
| 389 | </th> |
||
| 390 | <td> |
||
| 391 | <div> |
||
| 392 | <label> |
||
| 393 | <input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> /> |
||
| 394 | <?php esc_html_e( 'On for all comments', 'jetpack' ); ?> |
||
| 395 | </label> |
||
| 396 | </div> |
||
| 397 | </td> |
||
| 398 | </tr> |
||
| 399 | <?php endif; ?> |
||
| 400 | </tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?> |
||
| 401 | <?php } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * If sharedaddy is not loaded, we don't have the "Show buttons on" yet, so we need to add that since it affects likes too. |
||
| 405 | */ |
||
| 406 | function admin_settings_showbuttonon_init() { ?> |
||
| 407 | <?php |
||
| 408 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 409 | echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' ); |
||
| 410 | ?> |
||
| 411 | <th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th> |
||
| 412 | <td> |
||
| 413 | <?php |
||
| 414 | $br = false; |
||
| 415 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
||
| 416 | array_unshift( $shows, 'index' ); |
||
| 417 | $global = $this->get_options(); |
||
| 418 | View Code Duplication | foreach ( $shows as $show ) : |
|
| 419 | if ( 'index' == $show ) { |
||
| 420 | $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' ); |
||
| 421 | } else { |
||
| 422 | $post_type_object = get_post_type_object( $show ); |
||
| 423 | $label = $post_type_object->labels->name; |
||
| 424 | } |
||
| 425 | ?> |
||
| 426 | <?php if ( $br ) echo '<br />'; ?><label><input type="checkbox"<?php checked( in_array( $show, $global['show'] ) ); ?> name="show[]" value="<?php echo esc_attr( $show ); ?>" /> <?php echo esc_html( $label ); ?></label> |
||
| 427 | <?php $br = true; endforeach; ?> |
||
| 428 | </td> |
||
| 429 | <?php |
||
| 430 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 431 | echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' ); |
||
| 432 | ?> |
||
| 433 | <?php } |
||
| 434 | |||
| 435 | |||
| 436 | /** |
||
| 437 | * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option. |
||
| 438 | */ |
||
| 439 | function admin_settings_showbuttonon_callback() { |
||
| 440 | $options = get_option( 'sharing-options' ); |
||
| 441 | if ( !is_array( $options ) ) |
||
| 442 | $options = array(); |
||
| 443 | |||
| 444 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
||
| 445 | $shows[] = 'index'; |
||
| 446 | $data = $_POST; |
||
| 447 | |||
| 448 | if ( isset( $data['show'] ) ) { |
||
| 449 | View Code Duplication | if ( is_scalar( $data['show'] ) ) { |
|
| 450 | switch ( $data['show'] ) { |
||
| 451 | case 'posts' : |
||
| 452 | $data['show'] = array( 'post', 'page' ); |
||
| 453 | break; |
||
| 454 | case 'index' : |
||
| 455 | $data['show'] = array( 'index' ); |
||
| 456 | break; |
||
| 457 | case 'posts-index' : |
||
| 458 | $data['show'] = array( 'post', 'page', 'index' ); |
||
| 459 | break; |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | View Code Duplication | if ( $data['show'] = array_intersect( $data['show'], $shows ) ) { |
|
| 464 | $options['global']['show'] = $data['show']; |
||
| 465 | } |
||
| 466 | } else { |
||
| 467 | $options['global']['show'] = array(); |
||
| 468 | } |
||
| 469 | |||
| 470 | update_option( 'sharing-options', $options ); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled. |
||
| 475 | */ |
||
| 476 | function process_update_requests_if_sharedaddy_not_loaded() { |
||
| 477 | if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) { |
||
| 478 | if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) { |
||
| 479 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 480 | do_action( 'sharing_admin_update' ); |
||
| 481 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) ); |
||
| 482 | die(); |
||
| 483 | } |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Saves the setting in the database, bumps a stat on WordPress.com |
||
| 489 | */ |
||
| 490 | function admin_settings_callback() { |
||
| 491 | // We're looking for these, and doing a dance to set some stats and save |
||
| 492 | // them together in array option. |
||
| 493 | $new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on'; |
||
| 494 | $db_state = $this->is_enabled_sitewide(); |
||
| 495 | |||
| 496 | $reblogs_new_state = !empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on'; |
||
| 497 | $reblogs_db_state = $this->reblogs_enabled_sitewide(); |
||
| 498 | /** Default State *********************************************************/ |
||
| 499 | |||
| 500 | // Checked (enabled) |
||
| 501 | View Code Duplication | switch( $new_state ) { |
|
| 502 | case 'off' : |
||
| 503 | if ( true == $db_state && ! $this->in_jetpack ) { |
||
| 504 | $g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=disabled_likes' ); |
||
| 505 | } |
||
| 506 | update_option( 'disabled_likes', 1 ); |
||
| 507 | break; |
||
| 508 | case 'on' : |
||
| 509 | default: |
||
| 510 | if ( false == $db_state && ! $this->in_jetpack ) { |
||
| 511 | $g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=reenabled_likes' ); |
||
| 512 | } |
||
| 513 | delete_option( 'disabled_likes' ); |
||
| 514 | break; |
||
| 515 | } |
||
| 516 | |||
| 517 | View Code Duplication | switch( $reblogs_new_state ) { |
|
| 518 | case 'off' : |
||
| 519 | if ( true == $reblogs_db_state && ! $this->in_jetpack ) { |
||
| 520 | $g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=disabled_reblogs' ); |
||
| 521 | } |
||
| 522 | update_option( 'disabled_reblogs', 1 ); |
||
| 523 | break; |
||
| 524 | case 'on' : |
||
| 525 | default: |
||
| 526 | if ( false == $reblogs_db_state && ! $this->in_jetpack ) { |
||
| 527 | $g_gif = file_get_contents( 'https://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=reenabled_reblogs' ); |
||
| 528 | } |
||
| 529 | delete_option( 'disabled_reblogs' ); |
||
| 530 | break; |
||
| 531 | } |
||
| 532 | |||
| 533 | // comment setting |
||
| 534 | $new_comments_state = !empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false; |
||
| 535 | switch( (bool) $new_comments_state ) { |
||
| 536 | case true: |
||
| 537 | update_option( 'jetpack_comment_likes_enabled', 1 ); |
||
| 538 | break; |
||
| 539 | case false: |
||
| 540 | default: |
||
| 541 | update_option( 'jetpack_comment_likes_enabled', 0 ); |
||
| 542 | break; |
||
| 543 | } |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Force comment likes on for a blog |
||
| 548 | * Used when a new blog is created |
||
| 549 | */ |
||
| 550 | function enable_comment_likes( $blog_id ) { |
||
| 551 | switch_to_blog( $blog_id ); |
||
| 552 | update_option( 'jetpack_comment_likes_enabled', 1 ); |
||
| 553 | restore_current_blog(); |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Adds the 'sharing' menu to the settings menu. |
||
| 558 | * Only ran if sharedaddy and publicize are not already active. |
||
| 559 | */ |
||
| 560 | function sharing_menu() { |
||
| 561 | add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) ); |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Provides a sharing page with the sharing_global_options hook |
||
| 566 | * so we can display the setting. |
||
| 567 | * Only ran if sharedaddy and publicize are not already active. |
||
| 568 | */ |
||
| 569 | function sharing_page() { |
||
| 570 | $this->updated_message(); ?> |
||
| 571 | <div class="wrap"> |
||
| 572 | <div class="icon32" id="icon-options-general"><br /></div> |
||
| 573 | <h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1> |
||
| 574 | <?php |
||
| 575 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 576 | do_action( 'pre_admin_screen_sharing' ); |
||
| 577 | ?> |
||
| 578 | <?php $this->sharing_block(); ?> |
||
| 579 | </div> <?php |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Returns the settings have been saved message. |
||
| 584 | */ |
||
| 585 | function updated_message() { |
||
| 586 | if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ) |
||
| 587 | echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>'; |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts |
||
| 592 | */ |
||
| 593 | function sharing_block() { ?> |
||
| 594 | <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2> |
||
| 595 | <form method="post" action=""> |
||
| 596 | <table class="form-table"> |
||
| 597 | <tbody> |
||
| 598 | <?php |
||
| 599 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 600 | do_action( 'sharing_global_options' ); |
||
| 601 | ?> |
||
| 602 | </tbody> |
||
| 603 | </table> |
||
| 604 | |||
| 605 | <p class="submit"> |
||
| 606 | <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" /> |
||
| 607 | </p> |
||
| 608 | |||
| 609 | <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" /> |
||
| 610 | </form> <?php |
||
| 611 | } |
||
| 612 | |||
| 613 | function admin_init() { |
||
| 614 | add_filter( 'manage_posts_columns', array( $this, 'add_like_count_column' ) ); |
||
| 615 | add_filter( 'manage_pages_columns', array( $this, 'add_like_count_column' ) ); |
||
| 616 | add_action( 'manage_posts_custom_column', array( $this, 'likes_edit_column' ), 10, 2 ); |
||
| 617 | add_action( 'manage_pages_custom_column', array( $this, 'likes_edit_column' ), 10, 2 ); |
||
| 618 | add_action( 'admin_print_styles-edit.php', array( $this, 'load_admin_css' ) ); |
||
| 619 | add_action( "admin_print_scripts-edit.php", array( $this, 'enqueue_admin_scripts' ) ); |
||
| 620 | } |
||
| 621 | |||
| 622 | function action_init() { |
||
| 623 | if ( is_admin() ) { |
||
| 624 | return; |
||
| 625 | } |
||
| 626 | |||
| 627 | if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || |
||
| 628 | ( defined( 'APP_REQUEST' ) && APP_REQUEST ) || |
||
| 629 | ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) || |
||
| 630 | ( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) || |
||
| 631 | ( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) { |
||
| 632 | return; |
||
| 633 | } |
||
| 634 | |||
| 635 | // Comment Likes widget has been disabled, pending performance improvements. |
||
| 636 | // add_filter( 'comment_text', array( &$this, 'comment_likes' ), 10, 2 ); |
||
| 637 | |||
| 638 | if ( $this->in_jetpack ) { |
||
| 639 | add_filter( 'the_content', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 640 | add_filter( 'the_excerpt', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 641 | |||
| 642 | } else { |
||
| 643 | add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 644 | add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) ); |
||
| 645 | |||
| 646 | wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 647 | wp_enqueue_script( 'jquery_inview', '/wp-content/js/jquery/jquery.inview.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 648 | wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 649 | wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize', 'jquery_inview' ), JETPACK__VERSION, true ); |
||
| 650 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Register scripts |
||
| 656 | */ |
||
| 657 | function register_scripts() { |
||
| 658 | wp_register_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 659 | wp_register_script( 'jquery_inview', plugins_url( '_inc/jquery.inview.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 660 | wp_register_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 661 | wp_register_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize', 'jquery_inview' ), JETPACK__VERSION, true ); |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Load the CSS needed for the wp-admin area. |
||
| 666 | */ |
||
| 667 | function load_admin_css() { |
||
| 668 | ?> |
||
| 669 | <style type="text/css"> |
||
| 670 | .vers img { display: none; } |
||
| 671 | .metabox-prefs .vers img { display: inline; } |
||
| 672 | .fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; } |
||
| 673 | .fixed .column-stats { width: 5em; } |
||
| 674 | .fixed .column-likes .post-com-count { |
||
| 675 | -webkit-box-sizing: border-box; |
||
| 676 | -moz-box-sizing: border-box; |
||
| 677 | box-sizing: border-box; |
||
| 678 | display: inline-block; |
||
| 679 | padding: 0 8px; |
||
| 680 | height: 2em; |
||
| 681 | margin-top: 5px; |
||
| 682 | -webkit-border-radius: 5px; |
||
| 683 | border-radius: 5px; |
||
| 684 | background-color: #72777C; |
||
| 685 | color: #FFF; |
||
| 686 | font-size: 11px; |
||
| 687 | line-height: 21px; |
||
| 688 | } |
||
| 689 | .fixed .column-likes .post-com-count::after { border: none !important; } |
||
| 690 | .fixed .column-likes .post-com-count:hover { background-color: #0073AA; } |
||
| 691 | .fixed .column-likes .vers:before { |
||
| 692 | font: normal 20px/1 dashicons; |
||
| 693 | content: '\f155'; |
||
| 694 | speak: none; |
||
| 695 | -webkit-font-smoothing: antialiased; |
||
| 696 | -moz-osx-font-smoothing: grayscale; |
||
| 697 | } |
||
| 698 | @media screen and (max-width: 782px) { |
||
| 699 | .fixed .column-likes { |
||
| 700 | display: none; |
||
| 701 | } |
||
| 702 | } |
||
| 703 | </style> |
||
| 704 | <?php |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Load the JS required for loading the like counts. |
||
| 709 | */ |
||
| 710 | function enqueue_admin_scripts() { |
||
| 711 | if ( empty( $_GET['post_type'] ) || 'post' == $_GET['post_type'] || 'page' == $_GET['post_type'] ) { |
||
| 712 | if ( $this->in_jetpack ) { |
||
| 713 | wp_enqueue_script( 'likes-post-count', plugins_url( 'modules/likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 714 | wp_enqueue_script( 'likes-post-count-jetpack', plugins_url( 'modules/likes/post-count-jetpack.js', dirname( __FILE__ ) ), array( 'likes-post-count' ), JETPACK__VERSION ); |
||
| 715 | } else { |
||
| 716 | wp_enqueue_script( 'jquery.wpcom-proxy-request', "/wp-content/js/jquery/jquery.wpcom-proxy-request.js", array('jquery'), NULL, true ); |
||
| 717 | wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 718 | wp_enqueue_script( 'likes-post-count-wpcom', plugins_url( 'likes/post-count-wpcom.js', dirname( __FILE__ ) ), array( 'likes-post-count', 'jquery.wpcom-proxy-request' ), JETPACK__VERSION ); |
||
| 719 | } |
||
| 720 | } |
||
| 721 | } |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Add "Likes" column data to the post edit table in wp-admin. |
||
| 725 | * |
||
| 726 | * @param string $column_name |
||
| 727 | * @param int $post_id |
||
| 728 | */ |
||
| 729 | function likes_edit_column( $column_name, $post_id ) { |
||
| 730 | if ( 'likes' == $column_name ) { |
||
| 731 | |||
| 732 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 733 | $blog_id = get_current_blog_id(); |
||
| 734 | } else { |
||
| 735 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 736 | } |
||
| 737 | |||
| 738 | $permalink = get_permalink( get_the_ID() ); ?> |
||
| 739 | <a title="" data-post-id="<?php echo (int) $post_id; ?>" class="post-com-count post-like-count" id="post-like-count-<?php echo (int) $post_id; ?>" data-blog-id="<?php echo (int) $blog_id; ?>" href="<?php echo esc_url( $permalink ); ?>#like-<?php echo (int) $post_id; ?>"> |
||
| 740 | <span class="comment-count">0</span> |
||
| 741 | </a> |
||
| 742 | <?php |
||
| 743 | } |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Add a "Likes" column header to the post edit table in wp-admin. |
||
| 748 | * |
||
| 749 | * @param array $columns |
||
| 750 | * @return array |
||
| 751 | */ |
||
| 752 | function add_like_count_column( $columns ) { |
||
| 753 | $date = $columns['date']; |
||
| 754 | unset( $columns['date'] ); |
||
| 755 | |||
| 756 | $columns['likes'] = '<span class="vers"><img title="' . esc_attr__( 'Likes', 'jetpack' ) . '" alt="' . esc_attr__( 'Likes', 'jetpack' ) . '" src="//s0.wordpress.com/i/like-grey-icon.png" /></span>'; |
||
| 757 | $columns['date'] = $date; |
||
| 758 | |||
| 759 | return $columns; |
||
| 760 | } |
||
| 761 | |||
| 762 | function post_likes( $content ) { |
||
| 763 | $post_id = get_the_ID(); |
||
| 764 | |||
| 765 | if ( ! is_numeric( $post_id ) || ! $this->is_likes_visible() ) |
||
| 766 | return $content; |
||
| 767 | |||
| 768 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 769 | $blog_id = get_current_blog_id(); |
||
| 770 | $bloginfo = get_blog_details( (int) $blog_id ); |
||
| 771 | $domain = $bloginfo->domain; |
||
| 772 | } else { |
||
| 773 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 774 | $url = home_url(); |
||
| 775 | $url_parts = parse_url( $url ); |
||
| 776 | $domain = $url_parts['host']; |
||
| 777 | } |
||
| 778 | // make sure to include the scripts before the iframe otherwise weird things happen |
||
| 779 | add_action( 'wp_footer', array( $this, 'likes_master' ), 21 ); |
||
| 780 | |||
| 781 | /** |
||
| 782 | * if the same post appears more then once on a page the page goes crazy |
||
| 783 | * we need a slightly more unique id / name for the widget wrapper. |
||
| 784 | */ |
||
| 785 | $uniqid = uniqid(); |
||
| 786 | |||
| 787 | $src = sprintf( '//widgets.wp.com/likes/#blog_id=%1$d&post_id=%2$d&origin=%3$s&obj_id=%1$d-%2$d-%4$s', $blog_id, $post_id, $domain, $uniqid ); |
||
| 788 | $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
||
| 789 | $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post_id, $uniqid ); |
||
| 790 | $headline = sprintf( |
||
| 791 | /** This filter is already documented in modules/sharedaddy/sharing-service.php */ |
||
| 792 | apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', esc_html__( 'Like this:', 'jetpack' ), 'likes' ), |
||
| 793 | esc_html__( 'Like this:', 'jetpack' ) |
||
| 794 | ); |
||
| 795 | |||
| 796 | $html = "<div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'>"; |
||
| 797 | $html .= $headline; |
||
| 798 | $html .= "<div class='likes-widget-placeholder post-likes-widget-placeholder' style='height: 55px;'><span class='button'><span>" . esc_html__( 'Like', 'jetpack' ) . '</span></span> <span class="loading">' . esc_html__( 'Loading...', 'jetpack' ) . '</span></div>'; |
||
| 799 | $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>"; |
||
| 800 | $html .= '</div>'; |
||
| 801 | |||
| 802 | // Let's make sure that the script is enqueued |
||
| 803 | wp_enqueue_script( 'jetpack_likes_queuehandler' ); |
||
| 804 | |||
| 805 | return $content . $html; |
||
| 806 | } |
||
| 807 | |||
| 808 | function comment_likes( $content, $comment = null ) { |
||
| 809 | if ( empty( $comment ) ) |
||
| 810 | return $content; |
||
| 811 | |||
| 812 | if ( ! $this->is_comments_enabled() ) |
||
| 813 | return $content; |
||
| 814 | |||
| 815 | $protocol = 'http'; |
||
| 816 | if ( is_ssl() ) |
||
| 817 | $protocol = 'https'; |
||
| 818 | |||
| 819 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 820 | $blog_id = get_current_blog_id(); |
||
| 821 | $bloginfo = get_blog_details( (int) $blog_id ); |
||
| 822 | $domain = $bloginfo->domain; |
||
| 823 | } else { |
||
| 824 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 825 | $url = home_url(); |
||
| 826 | $url_parts = parse_url( $url ); |
||
| 827 | $domain = $url_parts['host']; |
||
| 828 | } |
||
| 829 | // make sure to include the scripts before the iframe otherwise weird things happen |
||
| 830 | add_action( 'wp_footer', array( $this, 'likes_master' ), 21 ); |
||
| 831 | |||
| 832 | $src = sprintf( '%1$s://widgets.wp.com/likes/#blog_id=%2$d&comment_id=%3$d&origin=%1$s://%4$s', $protocol, $blog_id, $comment->comment_ID, $domain ); |
||
| 833 | $name = sprintf( 'like-comment-frame-%1$d-%2$d', $blog_id, $comment->comment_ID ); |
||
| 834 | $wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d', $blog_id, $comment->comment_ID ); |
||
| 835 | |||
| 836 | $html = "<div><div class='jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper'>"; |
||
| 837 | $html .= "<iframe class='comment-likes-widget jetpack-likes-widget' name='$name' height='16px' width='100%' data='$src'></iframe>"; |
||
| 838 | $html .= '</div></div>'; |
||
| 839 | return $content . $html; |
||
| 840 | } |
||
| 841 | |||
| 842 | function post_flair_service_enabled_like( $classes ) { |
||
| 843 | $classes[] = 'sd-like-enabled'; |
||
| 844 | return $classes; |
||
| 845 | } |
||
| 846 | |||
| 847 | function admin_bar_likes() { |
||
| 848 | global $wp_admin_bar; |
||
| 849 | |||
| 850 | $post_id = get_the_ID(); |
||
| 851 | |||
| 852 | if ( ! is_numeric( $post_id ) || ! $this->is_admin_bar_button_visible() ) { |
||
| 853 | return; |
||
| 854 | } |
||
| 855 | |||
| 856 | $protocol = 'http'; |
||
| 857 | if ( is_ssl() ) |
||
| 858 | $protocol = 'https'; |
||
| 859 | |||
| 860 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 861 | $blog_id = get_current_blog_id(); |
||
| 862 | $bloginfo = get_blog_details( (int) $blog_id ); |
||
| 863 | $domain = $bloginfo->domain; |
||
| 864 | } else { |
||
| 865 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 866 | $url = home_url(); |
||
| 867 | $url_parts = parse_url( $url ); |
||
| 868 | $domain = $url_parts['host']; |
||
| 869 | } |
||
| 870 | // make sure to include the scripts before the iframe otherwise weird things happen |
||
| 871 | add_action( 'wp_footer', array( $this, 'likes_master' ), 21 ); |
||
| 872 | |||
| 873 | $src = sprintf( '%1$s://widgets.wp.com/likes/#blog_id=%2$d&post_id=%3$d&origin=%1$s://%4$s', $protocol, $blog_id, $post_id, $domain ); |
||
| 874 | |||
| 875 | $html = "<iframe class='admin-bar-likes-widget jetpack-likes-widget' scrolling='no' frameBorder='0' name='admin-bar-likes-widget' src='$src'></iframe>"; |
||
| 876 | |||
| 877 | $node = array( |
||
| 878 | 'id' => 'admin-bar-likes-widget', |
||
| 879 | 'meta' => array( |
||
| 880 | 'html' => $html |
||
| 881 | ) |
||
| 882 | ); |
||
| 883 | |||
| 884 | $wp_admin_bar->add_node( $node ); |
||
| 885 | } |
||
| 886 | |||
| 887 | /** |
||
| 888 | * This function needs to get loaded after the scripts get added to the page. |
||
| 889 | * |
||
| 890 | */ |
||
| 891 | function likes_master() { |
||
| 892 | $protocol = 'http'; |
||
| 893 | if ( is_ssl() ) |
||
| 894 | $protocol = 'https'; |
||
| 895 | |||
| 896 | $_locale = get_locale(); |
||
| 897 | |||
| 898 | // We have to account for w.org vs WP.com locale divergence |
||
| 899 | if ( $this->in_jetpack ) { |
||
| 900 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
| 901 | return false; |
||
| 902 | } |
||
| 903 | |||
| 904 | require_once JETPACK__GLOTPRESS_LOCALES_PATH; |
||
| 905 | |||
| 906 | $gp_locale = GP_Locales::by_field( 'wp_locale', $_locale ); |
||
| 907 | $_locale = isset( $gp_locale->slug ) ? $gp_locale->slug : ''; |
||
| 908 | } |
||
| 909 | |||
| 910 | $likes_locale = ( '' == $_locale || 'en' == $_locale ) ? '' : '&lang=' . strtolower( $_locale ); |
||
| 911 | |||
| 912 | $src = sprintf( |
||
| 913 | '%1$s://widgets.wp.com/likes/master.html?ver=%2$s#ver=%2$s%3$s', |
||
| 914 | $protocol, |
||
| 915 | $this->version, |
||
| 916 | $likes_locale |
||
| 917 | ); |
||
| 918 | |||
| 919 | $likersText = wp_kses( __( '<span>%d</span> bloggers like this:', 'jetpack' ), array( 'span' => array() ) ); |
||
| 920 | ?> |
||
| 921 | <iframe src='<?php echo $src; ?>' scrolling='no' id='likes-master' name='likes-master' style='display:none;'></iframe> |
||
| 922 | <div id='likes-other-gravatars'><div class="likes-text"><?php echo $likersText; ?></div><ul class="wpl-avatars sd-like-gravatars"></ul></div> |
||
| 923 | <?php |
||
| 924 | } |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Get the 'disabled_likes' option from the DB of the current blog. |
||
| 928 | * |
||
| 929 | * @return array |
||
| 930 | */ |
||
| 931 | function get_options() { |
||
| 932 | $setting = array(); |
||
| 933 | $setting['disabled'] = get_option( 'disabled_likes' ); |
||
| 934 | $sharing = get_option( 'sharing-options' ); |
||
| 935 | |||
| 936 | // Default visibility settings |
||
| 937 | if ( ! isset( $sharing['global']['show'] ) ) { |
||
| 938 | $sharing['global']['show'] = array( 'post', 'page' ); |
||
| 939 | |||
| 940 | // Scalar check |
||
| 941 | } elseif ( is_scalar( $sharing['global']['show'] ) ) { |
||
| 942 | switch ( $sharing['global']['show'] ) { |
||
| 943 | case 'posts' : |
||
| 944 | $sharing['global']['show'] = array( 'post', 'page' ); |
||
| 945 | break; |
||
| 946 | case 'index' : |
||
| 947 | $sharing['global']['show'] = array( 'index' ); |
||
| 948 | break; |
||
| 949 | case 'posts-index' : |
||
| 950 | $sharing['global']['show'] = array( 'post', 'page', 'index' ); |
||
| 951 | break; |
||
| 952 | } |
||
| 953 | } |
||
| 954 | |||
| 955 | // Ensure it's always an array (even if not previously empty or scalar) |
||
| 956 | $setting['show'] = !empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array(); |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Filters where the Likes are displayed. |
||
| 960 | * |
||
| 961 | * @module likes |
||
| 962 | * |
||
| 963 | * @since 2.2.0 |
||
| 964 | * |
||
| 965 | * @param array $setting Array of Likes display settings. |
||
| 966 | */ |
||
| 967 | return apply_filters( 'wpl_get_options', $setting ); |
||
| 968 | } |
||
| 969 | |||
| 970 | /** _is_ functions ************************************************************/ |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Are likes visible in this context? |
||
| 974 | * |
||
| 975 | * Some of this code was taken and modified from sharing_display() to ensure |
||
| 976 | * similar logic and filters apply here, too. |
||
| 977 | */ |
||
| 978 | function is_likes_visible() { |
||
| 979 | require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php'; |
||
| 980 | if ( Jetpack_Sync_Settings::is_syncing() ) { |
||
| 981 | return false; |
||
| 982 | } |
||
| 983 | |||
| 984 | global $wp_current_filter; // Used to apply 'sharing_show' filter |
||
| 985 | |||
| 986 | $post = get_post(); |
||
| 987 | |||
| 988 | // @todo: Remove this block when 4.5 is the minimum |
||
| 989 | global $wp_version; |
||
| 990 | $comment_popup = false; |
||
| 991 | if ( version_compare( $wp_version, '4.5-alpha', '<=' ) ) { |
||
| 992 | $comment_popup = is_comments_popup(); |
||
| 993 | } |
||
| 994 | // End 4.5 conditional block. |
||
| 995 | |||
| 996 | // Never show on feeds or previews |
||
| 997 | if ( is_feed() || is_preview() || $comment_popup ) { // @todo: Remove $comment_popup when 4.5 is minimum. |
||
| 998 | $enabled = false; |
||
| 999 | |||
| 1000 | // Not a feed or preview, so what is it? |
||
| 1001 | } else { |
||
| 1002 | |||
| 1003 | if ( in_the_loop() ) { |
||
| 1004 | // If in the loop, check if the current post is likeable |
||
| 1005 | $enabled = $this->is_post_likeable(); |
||
| 1006 | } else { |
||
| 1007 | // Otherwise, check and see if likes are enabled sitewide |
||
| 1008 | $enabled = $this->is_enabled_sitewide(); |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | if ( post_password_required() ) |
||
| 1012 | $enabled = false; |
||
| 1013 | |||
| 1014 | if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) { |
||
| 1015 | $enabled = false; |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | // Sharing Setting Overrides **************************************** |
||
| 1019 | |||
| 1020 | // Single post including custom post types |
||
| 1021 | if ( is_single() ) { |
||
| 1022 | if ( ! $this->is_single_post_enabled( $post->post_type ) ) { |
||
| 1023 | $enabled = false; |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | // Single page |
||
| 1027 | } elseif ( is_page() && ! is_front_page() ) { |
||
| 1028 | if ( ! $this->is_single_page_enabled() ) { |
||
| 1029 | $enabled = false; |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | // Attachment |
||
| 1033 | } elseif ( is_attachment() ) { |
||
| 1034 | if ( ! $this->is_attachment_enabled() ) { |
||
| 1035 | $enabled = false; |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | // All other loops |
||
| 1039 | } elseif ( ! $this->is_index_enabled() ) { |
||
| 1040 | $enabled = false; |
||
| 1041 | } |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | if ( $post instanceof WP_Post ) { |
||
|
0 ignored issues
–
show
|
|||
| 1045 | // Check that the post is a public, published post. |
||
| 1046 | View Code Duplication | if ( 'attachment' == $post->post_type ) { |
|
| 1047 | $post_status = get_post_status( $post->post_parent ); |
||
| 1048 | } else { |
||
| 1049 | $post_status = $post->post_status; |
||
| 1050 | } |
||
| 1051 | if ( 'publish' != $post_status ) { |
||
| 1052 | $enabled = false; |
||
| 1053 | } |
||
| 1054 | } |
||
| 1055 | |||
| 1056 | // Run through the sharing filters |
||
| 1057 | /** This filter is documented in modules/sharedaddy/sharing-service.php */ |
||
| 1058 | $enabled = apply_filters( 'sharing_show', $enabled, $post ); |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Filters whether the Likes should be visible or not. |
||
| 1062 | * Allows overwriting the options set in Settings > Sharing. |
||
| 1063 | * |
||
| 1064 | * @module likes |
||
| 1065 | * |
||
| 1066 | * @since 2.2.0 |
||
| 1067 | * |
||
| 1068 | * @param bool $enabled Should the Likes be visible? |
||
| 1069 | */ |
||
| 1070 | return (bool) apply_filters( 'wpl_is_likes_visible', $enabled ); |
||
| 1071 | } |
||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Returns the current state of the "WordPress.com Likes are" option. |
||
| 1075 | * @return boolean true if enabled sitewide, false if not |
||
| 1076 | */ |
||
| 1077 | function is_enabled_sitewide() { |
||
| 1078 | /** |
||
| 1079 | * Filters whether Likes are enabled by default on all posts. |
||
| 1080 | * true if enabled sitewide, false if not. |
||
| 1081 | * |
||
| 1082 | * @module likes |
||
| 1083 | * |
||
| 1084 | * @since 2.2.0 |
||
| 1085 | * |
||
| 1086 | * @param bool $option Are Likes enabled sitewide. |
||
| 1087 | */ |
||
| 1088 | return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! Jetpack_Options::get_option_and_ensure_autoload( 'disabled_likes', 0 ) ); |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Returns the current state of the "WordPress.com Reblogs are" option. |
||
| 1093 | * @return boolean true if enabled sitewide, false if not |
||
| 1094 | */ |
||
| 1095 | function reblogs_enabled_sitewide() { |
||
| 1096 | /** |
||
| 1097 | * Filters whether Reblogs are enabled by default on all posts. |
||
| 1098 | * true if enabled sitewide, false if not. |
||
| 1099 | * |
||
| 1100 | * @module likes |
||
| 1101 | * |
||
| 1102 | * @since 3.0.0 |
||
| 1103 | * |
||
| 1104 | * @param bool $option Are Reblogs enabled sitewide. |
||
| 1105 | */ |
||
| 1106 | return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) ); |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Returns if comment likes are enabled. Defaults to 'off' |
||
| 1111 | * @return boolean true if we should show comment likes, false if not |
||
| 1112 | */ |
||
| 1113 | function is_comments_enabled() { |
||
| 1114 | /** |
||
| 1115 | * Filters whether Comment Likes are enabled. |
||
| 1116 | * true if enabled, false if not. |
||
| 1117 | * |
||
| 1118 | * @module likes |
||
| 1119 | * |
||
| 1120 | * @since 2.2.0 |
||
| 1121 | * |
||
| 1122 | * @param bool $option Are Comment Likes enabled sitewide. |
||
| 1123 | */ |
||
| 1124 | return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) ); |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | function is_admin_bar_button_visible() { |
||
| 1128 | global $wp_admin_bar; |
||
| 1129 | |||
| 1130 | if ( ! is_object( $wp_admin_bar ) ) |
||
| 1131 | return false; |
||
| 1132 | |||
| 1133 | if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) ) |
||
| 1134 | return false; |
||
| 1135 | |||
| 1136 | if ( ! $this->is_likes_visible() ) |
||
| 1137 | return false; |
||
| 1138 | |||
| 1139 | if ( ! $this->is_post_likeable() ) |
||
| 1140 | return false; |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Filters whether the Like button is enabled in the admin bar. |
||
| 1144 | * |
||
| 1145 | * @module likes |
||
| 1146 | * |
||
| 1147 | * @since 2.2.0 |
||
| 1148 | * |
||
| 1149 | * @param bool true Should the Like button be visible in the Admin bar. Default to true. |
||
| 1150 | */ |
||
| 1151 | return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true ); |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * Are likes enabled for this post? |
||
| 1156 | * |
||
| 1157 | * @param int $post_id |
||
| 1158 | * @retun bool |
||
| 1159 | */ |
||
| 1160 | function is_post_likeable( $post_id = 0 ) { |
||
| 1161 | $post = get_post( $post_id ); |
||
| 1162 | if ( !$post || is_wp_error( $post ) ) { |
||
| 1163 | return false; |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | $sitewide_likes_enabled = (bool) Jetpack_Likes::is_enabled_sitewide(); |
||
| 1167 | $post_likes_switched = (bool) get_post_meta( $post->ID, 'switch_like_status', true ); |
||
| 1168 | |||
| 1169 | $post_likes_enabled = $sitewide_likes_enabled; |
||
| 1170 | if ( $post_likes_switched ) { |
||
| 1171 | $post_likes_enabled = ! $post_likes_enabled; |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | return $post_likes_enabled; |
||
| 1175 | } |
||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * Are Post Likes enabled on archive/front/search pages? |
||
| 1179 | * |
||
| 1180 | * @return bool |
||
| 1181 | */ |
||
| 1182 | function is_index_enabled() { |
||
| 1183 | $options = $this->get_options(); |
||
| 1184 | /** |
||
| 1185 | * Filters whether Likes should be enabled on archive/front/search pages. |
||
| 1186 | * |
||
| 1187 | * @module likes |
||
| 1188 | * |
||
| 1189 | * @since 2.2.0 |
||
| 1190 | * |
||
| 1191 | * @param bool $enabled Are Post Likes enabled on archive/front/search pages? |
||
| 1192 | */ |
||
| 1193 | return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'] ) ); |
||
| 1194 | } |
||
| 1195 | |||
| 1196 | /** |
||
| 1197 | * Are Post Likes enabled on single posts? |
||
| 1198 | * |
||
| 1199 | * @param String $post_type custom post type identifier |
||
| 1200 | * @return bool |
||
| 1201 | */ |
||
| 1202 | View Code Duplication | function is_single_post_enabled( $post_type = 'post' ) { |
|
| 1203 | $options = $this->get_options(); |
||
| 1204 | return (bool) apply_filters( |
||
| 1205 | /** |
||
| 1206 | * Filters whether Likes should be enabled on single posts. |
||
| 1207 | * |
||
| 1208 | * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled. |
||
| 1209 | * |
||
| 1210 | * @module likes |
||
| 1211 | * |
||
| 1212 | * @since 2.2.0 |
||
| 1213 | * |
||
| 1214 | * @param bool $enabled Are Post Likes enabled on single posts? |
||
| 1215 | */ |
||
| 1216 | "wpl_is_single_{$post_type}_disabled", |
||
| 1217 | (bool) in_array( $post_type, $options['show'] ) |
||
| 1218 | ); |
||
| 1219 | } |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Are Post Likes enabled on single pages? |
||
| 1223 | * |
||
| 1224 | * @return bool |
||
| 1225 | */ |
||
| 1226 | View Code Duplication | function is_single_page_enabled() { |
|
| 1227 | $options = $this->get_options(); |
||
| 1228 | /** |
||
| 1229 | * Filters whether Likes should be enabled on single pages. |
||
| 1230 | * |
||
| 1231 | * @module likes |
||
| 1232 | * |
||
| 1233 | * @since 2.2.0 |
||
| 1234 | * |
||
| 1235 | * @param bool $enabled Are Post Likes enabled on single pages? |
||
| 1236 | */ |
||
| 1237 | return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'] ) ); |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | /** |
||
| 1241 | * Are Media Likes enabled on single pages? |
||
| 1242 | * |
||
| 1243 | * @return bool |
||
| 1244 | */ |
||
| 1245 | function is_attachment_enabled() { |
||
| 1246 | $options = $this->get_options(); |
||
| 1247 | /** |
||
| 1248 | * Filters whether Likes should be enabled on attachment pages. |
||
| 1249 | * |
||
| 1250 | * @module likes |
||
| 1251 | * |
||
| 1252 | * @since 2.2.0 |
||
| 1253 | * |
||
| 1254 | * @param bool $enabled Are Post Likes enabled on attachment pages? |
||
| 1255 | */ |
||
| 1256 | return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'] ) ); |
||
| 1257 | } |
||
| 1258 | } |
||
| 1259 | |||
| 1260 | Jetpack_Likes::init(); |
||
| 1261 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.