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 = '20160301'; |
||
| 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' );?>" /> |
||
| 630 | |||
| 631 | function admin_init() { |
||
| 649 | |||
| 650 | function action_init() { |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Register scripts |
||
| 683 | */ |
||
| 684 | function register_scripts() { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Load the CSS needed for the wp-admin area. |
||
| 694 | */ |
||
| 695 | function load_admin_css() { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Load the JS required for loading the like counts. |
||
| 724 | */ |
||
| 725 | function enqueue_admin_scripts() { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Add "Likes" column data to the post edit table in wp-admin. |
||
| 740 | * |
||
| 741 | * @param string $column_name |
||
| 742 | * @param int $post_id |
||
| 743 | */ |
||
| 744 | function likes_edit_column( $column_name, $post_id ) { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Add a "Likes" column header to the post edit table in wp-admin. |
||
| 763 | * |
||
| 764 | * @param array $columns |
||
| 765 | * @return array |
||
| 766 | */ |
||
| 767 | function add_like_count_column( $columns ) { |
||
| 776 | |||
| 777 | function post_likes( $content ) { |
||
| 816 | |||
| 817 | function comment_likes( $content, $comment = null ) { |
||
| 850 | |||
| 851 | function post_flair_service_enabled_like( $classes ) { |
||
| 855 | |||
| 856 | function admin_bar_likes() { |
||
| 893 | |||
| 894 | /** |
||
| 895 | * This function needs to get loaded after the scripts get added to the page. |
||
| 896 | * |
||
| 897 | */ |
||
| 898 | function likes_master() { |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Get the 'disabled_likes' option from the DB of the current blog. |
||
| 935 | * |
||
| 936 | * @return array |
||
| 937 | */ |
||
| 938 | function get_options() { |
||
| 976 | |||
| 977 | /** _is_ functions ************************************************************/ |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Are likes visible in this context? |
||
| 981 | * |
||
| 982 | * Some of this code was taken and modified from sharing_display() to ensure |
||
| 983 | * similar logic and filters apply here, too. |
||
| 984 | */ |
||
| 985 | function is_likes_visible() { |
||
| 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() { |
||
| 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() { |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Returns if comment likes are enabled. Defaults to 'off' |
||
| 1111 | * @todo decide what the default should be |
||
| 1112 | * @return boolean true if we should show comment likes, false if not |
||
| 1113 | */ |
||
| 1114 | function is_comments_enabled() { |
||
| 1127 | |||
| 1128 | function is_admin_bar_button_visible() { |
||
| 1154 | |||
| 1155 | /** |
||
| 1156 | * Are likes enabled for this post? |
||
| 1157 | * |
||
| 1158 | * @param int $post_id |
||
| 1159 | * @retun bool |
||
| 1160 | */ |
||
| 1161 | function is_post_likeable( $post_id = 0 ) { |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Are Post Likes enabled on archive/front/search pages? |
||
| 1180 | * |
||
| 1181 | * @return bool |
||
| 1182 | */ |
||
| 1183 | function is_index_enabled() { |
||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Are Post Likes enabled on single posts? |
||
| 1199 | * |
||
| 1200 | * @param String $post_type custom post type identifier |
||
| 1201 | * @return bool |
||
| 1202 | */ |
||
| 1203 | View Code Duplication | function is_single_post_enabled( $post_type = 'post' ) { |
|
| 1221 | |||
| 1222 | /** |
||
| 1223 | * Are Post Likes enabled on single pages? |
||
| 1224 | * |
||
| 1225 | * @return bool |
||
| 1226 | */ |
||
| 1227 | View Code Duplication | function is_single_page_enabled() { |
|
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Are Media Likes enabled on single pages? |
||
| 1243 | * |
||
| 1244 | * @return bool |
||
| 1245 | */ |
||
| 1246 | function is_attachment_enabled() { |
||
| 1259 | } |
||
| 1260 | |||
| 1262 |
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: