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 = '20160429'; |
||
| 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() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set the social_notifications_like option to `on` when the Likes module is activated. |
||
| 91 | * |
||
| 92 | * @since 3.7.0 |
||
| 93 | * |
||
| 94 | * @return null |
||
| 95 | */ |
||
| 96 | function set_social_notifications_like() { |
||
| 97 | update_option( 'social_notifications_like', 'on' ); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Delete the social_notifications_like option that was set to `on` on module activation. |
||
| 102 | * |
||
| 103 | * @since 3.7.0 |
||
| 104 | * |
||
| 105 | * @return null |
||
| 106 | */ |
||
| 107 | function delete_social_notifications_like() { |
||
| 108 | delete_option( 'social_notifications_like' ); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Redirects to the likes section of the sharing page. |
||
| 113 | */ |
||
| 114 | function configuration_redirect() { |
||
| 115 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) ); |
||
| 116 | die(); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable |
||
| 121 | */ |
||
| 122 | function load_jp_css() { |
||
| 123 | // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. |
||
| 124 | // Jetpack::init()->admin_styles(); |
||
| 125 | } |
||
| 126 | /** |
||
| 127 | * Load style on the front end. |
||
| 128 | * @return null |
||
| 129 | */ |
||
| 130 | function load_styles_register_scripts() { |
||
| 131 | |||
| 132 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 133 | if( $this->in_jetpack ) { |
||
| 134 | $this->register_scripts(); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box |
||
| 140 | * @param string $html row heading for the sharedaddy "which page" setting |
||
| 141 | * @return string html with the jetpack-targetable class and likes id. tbody gets closed after the like settings |
||
| 142 | */ |
||
| 143 | function configuration_target_area( $html = '' ) { |
||
| 144 | $html = "<tbody id='likes' class='jetpack-targetable'>" . $html; |
||
| 145 | return $html; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares" |
||
| 150 | * @param string $title The current title of the metabox, not needed/used. |
||
| 151 | */ |
||
| 152 | function add_likes_to_sharing_meta_box_title( $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 | //$g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=switched_post_like_status' ); @todo stat |
||
| 220 | } else { |
||
| 221 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $post_id; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Shows the likes option in the post screen metabox. |
||
| 229 | */ |
||
| 230 | function meta_box_content( $post ) { |
||
| 231 | $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); |
||
| 232 | $checked = true; |
||
| 233 | $disabled = ! $this->is_enabled_sitewide(); |
||
| 234 | $switched_status = get_post_meta( $post_id, 'switch_like_status', true ); |
||
| 235 | |||
| 236 | if ( $disabled && empty( $switched_status ) || false == $disabled && !empty( $switched_status ) ) |
||
| 237 | $checked = false; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Fires before the Likes meta box content in the post editor. |
||
| 241 | * |
||
| 242 | * @module likes |
||
| 243 | * |
||
| 244 | * @since 2.2.0 |
||
| 245 | * |
||
| 246 | * @param WP_Post|array|null $post Post data. |
||
| 247 | */ |
||
| 248 | do_action( 'start_likes_meta_box_content', $post ); |
||
| 249 | ?> |
||
| 250 | |||
| 251 | <p> |
||
| 252 | <label for="wpl_enable_post_likes"> |
||
| 253 | <input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>> |
||
| 254 | <?php esc_html_e( 'Show likes.', 'jetpack' ); ?> |
||
| 255 | </label> |
||
| 256 | <input type="hidden" name="wpl_like_status_hidden" value="1" /> |
||
| 257 | </p> <?php |
||
| 258 | /** |
||
| 259 | * Fires after the Likes meta box content in the post editor. |
||
| 260 | * |
||
| 261 | * @module likes |
||
| 262 | * |
||
| 263 | * @since 2.2.0 |
||
| 264 | * |
||
| 265 | * @param WP_Post|array|null $post Post data. |
||
| 266 | */ |
||
| 267 | do_action( 'end_likes_meta_box_content', $post ); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog) |
||
| 272 | */ |
||
| 273 | function sharing_meta_box_content( $post ) { |
||
| 274 | $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); |
||
| 275 | $disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?> |
||
| 276 | <p> |
||
| 277 | <label for="wpl_enable_post_sharing"> |
||
| 278 | <input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( !$disabled ); ?>> |
||
| 279 | <?php _e( 'Show sharing buttons.', 'jetpack' ); ?> |
||
| 280 | </label> |
||
| 281 | <input type="hidden" name="wpl_sharing_status_hidden" value="1" /> |
||
| 282 | </p> <?php |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page) |
||
| 287 | */ |
||
| 288 | |||
| 289 | View Code Duplication | function admin_discussion_likes_settings_init() { |
|
| 290 | // Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings |
||
| 291 | add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' ); |
||
| 292 | add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' ); |
||
| 293 | // Register the setting |
||
| 294 | register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) ); |
||
| 295 | } |
||
| 296 | |||
| 297 | function admin_discussion_likes_settings_section() { |
||
| 298 | // Atypical usage here. We emit jquery to move likes notification checkbox to be with the rest of the email notification settings |
||
| 299 | ?> |
||
| 300 | <script type="text/javascript"> |
||
| 301 | jQuery( function( $ ) { |
||
| 302 | var table = $( '#social_notifications_like' ).parents( 'table:first' ), |
||
| 303 | header = table.prevAll( 'h3:first' ), |
||
| 304 | newParent = $( '#moderation_notify' ).parent( 'label' ).parent(); |
||
| 305 | |||
| 306 | if ( !table.length || !header.length || !newParent.length ) { |
||
| 307 | return; |
||
| 308 | } |
||
| 309 | |||
| 310 | newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() ); |
||
| 311 | header.remove(); |
||
| 312 | table.remove(); |
||
| 313 | } ); |
||
| 314 | </script> |
||
| 315 | <?php |
||
| 316 | } |
||
| 317 | |||
| 318 | function admin_likes_get_option( $option ) { |
||
| 319 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 320 | $option_setting = get_blog_option( get_current_blog_id(), $option, 'on' ); |
||
| 321 | } else { |
||
| 322 | $option_setting = get_option( $option, 'on' ); |
||
| 323 | } |
||
| 324 | |||
| 325 | return intval( 'on' == $option_setting ); |
||
| 326 | } |
||
| 327 | |||
| 328 | function admin_discussion_likes_settings_field() { |
||
| 329 | $like = $this->admin_likes_get_option( 'social_notifications_like' ); |
||
| 330 | ?> |
||
| 331 | <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> |
||
| 332 | <?php |
||
| 333 | } |
||
| 334 | |||
| 335 | function admin_discussion_likes_settings_validate( $input ) { |
||
| 336 | // If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'. |
||
| 337 | if ( !$input || 'off' == $input ) |
||
| 338 | return 'off'; |
||
| 339 | |||
| 340 | // Otherwise, return 'on'. |
||
| 341 | return 'on'; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * The actual options block to be inserted into the sharing page. |
||
| 346 | */ |
||
| 347 | function admin_settings_init() { ?> |
||
| 348 | <tr> |
||
| 349 | <th scope="row"> |
||
| 350 | <label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label> |
||
| 351 | </th> |
||
| 352 | <td> |
||
| 353 | <div> |
||
| 354 | <label> |
||
| 355 | <input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> /> |
||
| 356 | <?php esc_html_e( 'On for all posts', 'jetpack' ); ?> |
||
| 357 | </label> |
||
| 358 | </div> |
||
| 359 | <div> |
||
| 360 | <label> |
||
| 361 | <input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> /> |
||
| 362 | <?php esc_html_e( 'Turned on per post', 'jetpack' ); ?> |
||
| 363 | </label> |
||
| 364 | <div> |
||
| 365 | </td> |
||
| 366 | </tr> |
||
| 367 | <?php if ( ! $this->in_jetpack ) : ?> |
||
| 368 | <tr> |
||
| 369 | <th scope="row"> |
||
| 370 | <label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label> |
||
| 371 | </th> |
||
| 372 | <td> |
||
| 373 | <div> |
||
| 374 | <label> |
||
| 375 | <input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> /> |
||
| 376 | <?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?> |
||
| 377 | </label> |
||
| 378 | </div> |
||
| 379 | <div> |
||
| 380 | <label> |
||
| 381 | <input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> /> |
||
| 382 | <?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?> |
||
| 383 | </label> |
||
| 384 | <div> |
||
| 385 | </td> |
||
| 386 | </tr> |
||
| 387 | <tr> |
||
| 388 | <th scope="row"> |
||
| 389 | <label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label> |
||
| 390 | </th> |
||
| 391 | <td> |
||
| 392 | <div> |
||
| 393 | <label> |
||
| 394 | <input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> /> |
||
| 395 | <?php esc_html_e( 'On for all comments', 'jetpack' ); ?> |
||
| 396 | </label> |
||
| 397 | </div> |
||
| 398 | </td> |
||
| 399 | </tr> |
||
| 400 | <?php endif; ?> |
||
| 401 | </tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?> |
||
| 402 | <?php } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * 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. |
||
| 406 | */ |
||
| 407 | function admin_settings_showbuttonon_init() { ?> |
||
| 408 | <?php |
||
| 409 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 410 | echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' ); |
||
| 411 | ?> |
||
| 412 | <th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th> |
||
| 413 | <td> |
||
| 414 | <?php |
||
| 415 | $br = false; |
||
| 416 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
||
| 417 | array_unshift( $shows, 'index' ); |
||
| 418 | $global = $this->get_options(); |
||
| 419 | View Code Duplication | foreach ( $shows as $show ) : |
|
| 420 | if ( 'index' == $show ) { |
||
| 421 | $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' ); |
||
| 422 | } else { |
||
| 423 | $post_type_object = get_post_type_object( $show ); |
||
| 424 | $label = $post_type_object->labels->name; |
||
| 425 | } |
||
| 426 | ?> |
||
| 427 | <?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> |
||
| 428 | <?php $br = true; endforeach; ?> |
||
| 429 | </td> |
||
| 430 | <?php |
||
| 431 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 432 | echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' ); |
||
| 433 | ?> |
||
| 434 | <?php } |
||
| 435 | |||
| 436 | |||
| 437 | /** |
||
| 438 | * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option. |
||
| 439 | */ |
||
| 440 | function admin_settings_showbuttonon_callback() { |
||
| 441 | $options = get_option( 'sharing-options' ); |
||
| 442 | if ( !is_array( $options ) ) |
||
| 443 | $options = array(); |
||
| 444 | |||
| 445 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
||
| 446 | $shows[] = 'index'; |
||
| 447 | $data = $_POST; |
||
| 448 | |||
| 449 | if ( isset( $data['show'] ) ) { |
||
| 450 | View Code Duplication | if ( is_scalar( $data['show'] ) ) { |
|
| 451 | switch ( $data['show'] ) { |
||
| 452 | case 'posts' : |
||
| 453 | $data['show'] = array( 'post', 'page' ); |
||
| 454 | break; |
||
| 455 | case 'index' : |
||
| 456 | $data['show'] = array( 'index' ); |
||
| 457 | break; |
||
| 458 | case 'posts-index' : |
||
| 459 | $data['show'] = array( 'post', 'page', 'index' ); |
||
| 460 | break; |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | View Code Duplication | if ( $data['show'] = array_intersect( $data['show'], $shows ) ) { |
|
| 465 | $options['global']['show'] = $data['show']; |
||
| 466 | } |
||
| 467 | } else { |
||
| 468 | $options['global']['show'] = array(); |
||
| 469 | } |
||
| 470 | |||
| 471 | update_option( 'sharing-options', $options ); |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled. |
||
| 476 | */ |
||
| 477 | function process_update_requests_if_sharedaddy_not_loaded() { |
||
| 478 | if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) { |
||
| 479 | if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) { |
||
| 480 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 481 | do_action( 'sharing_admin_update' ); |
||
| 482 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) ); |
||
| 483 | die(); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Saves the setting in the database, bumps a stat on WordPress.com |
||
| 490 | */ |
||
| 491 | function admin_settings_callback() { |
||
| 492 | // We're looking for these, and doing a dance to set some stats and save |
||
| 493 | // them together in array option. |
||
| 494 | $new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on'; |
||
| 495 | $db_state = $this->is_enabled_sitewide(); |
||
| 496 | |||
| 497 | $reblogs_new_state = !empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on'; |
||
| 498 | $reblogs_db_state = $this->reblogs_enabled_sitewide(); |
||
| 499 | /** Default State *********************************************************/ |
||
| 500 | |||
| 501 | // Checked (enabled) |
||
| 502 | View Code Duplication | switch( $new_state ) { |
|
| 503 | case 'off' : |
||
| 504 | if ( true == $db_state && ! $this->in_jetpack ) { |
||
| 505 | $g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=disabled_likes' ); |
||
| 506 | } |
||
| 507 | update_option( 'disabled_likes', 1 ); |
||
| 508 | break; |
||
| 509 | case 'on' : |
||
| 510 | default: |
||
| 511 | if ( false == $db_state && ! $this->in_jetpack ) { |
||
| 512 | $g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=reenabled_likes' ); |
||
| 513 | } |
||
| 514 | delete_option( 'disabled_likes' ); |
||
| 515 | break; |
||
| 516 | } |
||
| 517 | |||
| 518 | View Code Duplication | switch( $reblogs_new_state ) { |
|
| 519 | case 'off' : |
||
| 520 | if ( true == $reblogs_db_state && ! $this->in_jetpack ) { |
||
| 521 | $g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=disabled_reblogs' ); |
||
| 522 | } |
||
| 523 | update_option( 'disabled_reblogs', 1 ); |
||
| 524 | break; |
||
| 525 | case 'on' : |
||
| 526 | default: |
||
| 527 | if ( false == $reblogs_db_state && ! $this->in_jetpack ) { |
||
| 528 | $g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=reenabled_reblogs' ); |
||
| 529 | } |
||
| 530 | delete_option( 'disabled_reblogs' ); |
||
| 531 | break; |
||
| 532 | } |
||
| 533 | |||
| 534 | // comment setting |
||
| 535 | $new_comments_state = !empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false; |
||
| 536 | switch( (bool) $new_comments_state ) { |
||
| 537 | case true: |
||
| 538 | update_option( 'jetpack_comment_likes_enabled', 1 ); |
||
| 539 | break; |
||
| 540 | case false: |
||
| 541 | default: |
||
| 542 | update_option( 'jetpack_comment_likes_enabled', 0 ); |
||
| 543 | break; |
||
| 544 | } |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Force comment likes on for a blog |
||
| 549 | * Used when a new blog is created |
||
| 550 | */ |
||
| 551 | function enable_comment_likes( $blog_id ) { |
||
| 552 | switch_to_blog( $blog_id ); |
||
| 553 | update_option( 'jetpack_comment_likes_enabled', 1 ); |
||
| 554 | restore_current_blog(); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Adds the 'sharing' menu to the settings menu. |
||
| 559 | * Only ran if sharedaddy and publicize are not already active. |
||
| 560 | */ |
||
| 561 | function sharing_menu() { |
||
| 562 | add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) ); |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Provides a sharing page with the sharing_global_options hook |
||
| 567 | * so we can display the setting. |
||
| 568 | * Only ran if sharedaddy and publicize are not already active. |
||
| 569 | */ |
||
| 570 | function sharing_page() { |
||
| 571 | $this->updated_message(); ?> |
||
| 572 | <div class="wrap"> |
||
| 573 | <div class="icon32" id="icon-options-general"><br /></div> |
||
| 574 | <h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1> |
||
| 575 | <?php |
||
| 576 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 577 | do_action( 'pre_admin_screen_sharing' ); |
||
| 578 | ?> |
||
| 579 | <?php $this->sharing_block(); ?> |
||
| 580 | </div> <?php |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Returns the settings have been saved message. |
||
| 585 | */ |
||
| 586 | function updated_message() { |
||
| 587 | if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ) |
||
| 588 | echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>'; |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts |
||
| 593 | */ |
||
| 594 | function sharing_block() { ?> |
||
| 595 | <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2> |
||
| 596 | <form method="post" action=""> |
||
| 597 | <table class="form-table"> |
||
| 598 | <tbody> |
||
| 599 | <?php |
||
| 600 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 601 | do_action( 'sharing_global_options' ); |
||
| 602 | ?> |
||
| 603 | </tbody> |
||
| 604 | </table> |
||
| 605 | |||
| 606 | <p class="submit"> |
||
| 607 | <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" /> |
||
| 608 | </p> |
||
| 609 | |||
| 610 | <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" /> |
||
| 611 | </form> <?php |
||
| 612 | } |
||
| 613 | |||
| 614 | function admin_init() { |
||
| 615 | add_filter( 'manage_posts_columns', array( $this, 'add_like_count_column' ) ); |
||
| 616 | add_filter( 'manage_pages_columns', array( $this, 'add_like_count_column' ) ); |
||
| 617 | add_action( 'manage_posts_custom_column', array( $this, 'likes_edit_column' ), 10, 2 ); |
||
| 618 | add_action( 'manage_pages_custom_column', array( $this, 'likes_edit_column' ), 10, 2 ); |
||
| 619 | add_action( 'admin_print_styles-edit.php', array( $this, 'load_admin_css' ) ); |
||
| 620 | add_action( "admin_print_scripts-edit.php", array( $this, 'enqueue_admin_scripts' ) ); |
||
| 621 | } |
||
| 622 | |||
| 623 | function action_init() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Register scripts |
||
| 656 | */ |
||
| 657 | function register_scripts() { |
||
| 658 | // Lets register all the sciprts |
||
| 659 | wp_register_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 660 | wp_register_script( 'jquery_inview', plugins_url( '_inc/jquery.inview.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 661 | wp_register_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 662 | wp_register_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize', 'jquery_inview' ), JETPACK__VERSION, true ); |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Load the CSS needed for the wp-admin area. |
||
| 667 | */ |
||
| 668 | function load_admin_css() { |
||
| 669 | ?> |
||
| 670 | <?php if ( version_compare( $GLOBALS['wp_version'], '4.3-alpha', '>=' ) ) : ?> |
||
| 671 | <style type="text/css"> |
||
| 672 | .vers img { display: none; } |
||
| 673 | .metabox-prefs .vers img { display: inline; } |
||
| 674 | .fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; } |
||
| 675 | .fixed .column-stats { width: 5em; } |
||
| 676 | .fixed .column-likes .post-com-count { |
||
| 677 | -webkit-box-sizing: border-box; |
||
| 678 | -moz-box-sizing: border-box; |
||
| 679 | box-sizing: border-box; |
||
| 680 | display: inline-block; |
||
| 681 | padding: 0 8px; |
||
| 682 | height: 2em; |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Load the JS required for loading the like counts. |
||
| 734 | */ |
||
| 735 | function enqueue_admin_scripts() { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Add "Likes" column data to the post edit table in wp-admin. |
||
| 750 | * |
||
| 751 | * @param string $column_name |
||
| 752 | * @param int $post_id |
||
| 753 | */ |
||
| 754 | function likes_edit_column( $column_name, $post_id ) { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Add a "Likes" column header to the post edit table in wp-admin. |
||
| 773 | * |
||
| 774 | * @param array $columns |
||
| 775 | * @return array |
||
| 776 | */ |
||
| 777 | function add_like_count_column( $columns ) { |
||
| 786 | |||
| 787 | function post_likes( $content ) { |
||
| 826 | |||
| 827 | function comment_likes( $content, $comment = null ) { |
||
| 860 | |||
| 861 | function post_flair_service_enabled_like( $classes ) { |
||
| 865 | |||
| 866 | function admin_bar_likes() { |
||
| 903 | |||
| 904 | /** |
||
| 905 | * This function needs to get loaded after the scripts get added to the page. |
||
| 906 | * |
||
| 907 | */ |
||
| 908 | function likes_master() { |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Get the 'disabled_likes' option from the DB of the current blog. |
||
| 945 | * |
||
| 946 | * @return array |
||
| 947 | */ |
||
| 948 | function get_options() { |
||
| 986 | |||
| 987 | /** _is_ functions ************************************************************/ |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Are likes visible in this context? |
||
| 991 | * |
||
| 992 | * Some of this code was taken and modified from sharing_display() to ensure |
||
| 993 | * similar logic and filters apply here, too. |
||
| 994 | */ |
||
| 995 | function is_likes_visible() { |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Returns the current state of the "WordPress.com Likes are" option. |
||
| 1085 | * @return boolean true if enabled sitewide, false if not |
||
| 1086 | */ |
||
| 1087 | function is_enabled_sitewide() { |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Returns the current state of the "WordPress.com Reblogs are" option. |
||
| 1103 | * @return boolean true if enabled sitewide, false if not |
||
| 1104 | */ |
||
| 1105 | function reblogs_enabled_sitewide() { |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Returns if comment likes are enabled. Defaults to 'off' |
||
| 1121 | * @todo decide what the default should be |
||
| 1122 | * @return boolean true if we should show comment likes, false if not |
||
| 1123 | */ |
||
| 1124 | function is_comments_enabled() { |
||
| 1137 | |||
| 1138 | function is_admin_bar_button_visible() { |
||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Are likes enabled for this post? |
||
| 1167 | * |
||
| 1168 | * @param int $post_id |
||
| 1169 | * @retun bool |
||
| 1170 | */ |
||
| 1171 | function is_post_likeable( $post_id = 0 ) { |
||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Are Post Likes enabled on archive/front/search pages? |
||
| 1190 | * |
||
| 1191 | * @return bool |
||
| 1192 | */ |
||
| 1193 | function is_index_enabled() { |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Are Post Likes enabled on single posts? |
||
| 1209 | * |
||
| 1210 | * @param String $post_type custom post type identifier |
||
| 1211 | * @return bool |
||
| 1212 | */ |
||
| 1213 | View Code Duplication | function is_single_post_enabled( $post_type = 'post' ) { |
|
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Are Post Likes enabled on single pages? |
||
| 1234 | * |
||
| 1235 | * @return bool |
||
| 1236 | */ |
||
| 1237 | View Code Duplication | function is_single_page_enabled() { |
|
| 1250 | |||
| 1251 | /** |
||
| 1252 | * Are Media Likes enabled on single pages? |
||
| 1253 | * |
||
| 1254 | * @return bool |
||
| 1255 | */ |
||
| 1256 | function is_attachment_enabled() { |
||
| 1269 | } |
||
| 1270 | |||
| 1272 |
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: