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