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 |
||
| 22 | class Jetpack_Likes { |
||
| 23 | public $version = '20151215'; |
||
| 24 | |||
| 25 | public static function init() { |
||
| 26 | static $instance = NULL; |
||
| 27 | |||
| 28 | if ( ! $instance ) { |
||
| 29 | $instance = new Jetpack_Likes; |
||
| 30 | } |
||
| 31 | |||
| 32 | return $instance; |
||
| 33 | } |
||
| 34 | |||
| 35 | function __construct() { |
||
| 36 | $this->in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true; |
||
|
|
|||
| 37 | |||
| 38 | add_action( 'init', array( &$this, 'action_init' ) ); |
||
| 39 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
| 40 | |||
| 41 | if ( $this->in_jetpack ) { |
||
| 42 | add_action( 'jetpack_activate_module_likes', array( $this, 'set_social_notifications_like' ) ); |
||
| 43 | add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) ); |
||
| 44 | |||
| 45 | Jetpack::enable_module_configurable( __FILE__ ); |
||
| 46 | Jetpack::module_configuration_load( __FILE__, array( $this, 'configuration_redirect' ) ); |
||
| 47 | |||
| 48 | add_action('admin_print_scripts-settings_page_sharing', array( &$this, 'load_jp_css' ) ); |
||
| 49 | add_filter( 'sharing_show_buttons_on_row_start', array( $this, 'configuration_target_area' ) ); |
||
| 50 | |||
| 51 | $active = Jetpack::get_active_modules(); |
||
| 52 | |||
| 53 | View Code Duplication | if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) { |
|
| 54 | add_action( 'admin_menu', array( $this, 'sharing_menu' ) ); // we don't have a sharing page yet |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) { |
||
| 58 | add_action( 'pre_admin_screen_sharing', array( $this, 'sharing_block' ), 20 ); // we have a sharing page but not the global options area |
||
| 59 | add_action( 'pre_admin_screen_sharing', array( $this, 'updated_message' ), -10 ); |
||
| 60 | } |
||
| 61 | |||
| 62 | if( ! in_array( 'sharedaddy', $active ) ) { |
||
| 63 | add_action( 'admin_init', array( $this, 'process_update_requests_if_sharedaddy_not_loaded' ) ); |
||
| 64 | add_action( 'sharing_global_options', array( $this, 'admin_settings_showbuttonon_init' ), 19 ); |
||
| 65 | add_action( 'sharing_admin_update', array( $this, 'admin_settings_showbuttonon_callback' ), 19 ); |
||
| 66 | add_action( 'admin_init', array( $this, 'add_meta_box' ) ); |
||
| 67 | } else { |
||
| 68 | add_filter( 'sharing_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) ); |
||
| 69 | add_action( 'start_sharing_meta_box_content', array( $this, 'meta_box_content' ) ); |
||
| 70 | } |
||
| 71 | } else { // wpcom |
||
| 72 | add_action( 'wpmu_new_blog', array( $this, 'enable_comment_likes' ), 10, 1 ); |
||
| 73 | add_action( 'admin_init', array( $this, 'add_meta_box' ) ); |
||
| 74 | add_action( 'end_likes_meta_box_content', array( $this, 'sharing_meta_box_content' ) ); |
||
| 75 | add_filter( 'likes_meta_box_title', array( $this, 'add_likes_to_sharing_meta_box_title' ) ); |
||
| 76 | } |
||
| 77 | |||
| 78 | add_action( 'admin_init', array( $this, 'admin_discussion_likes_settings_init' ) ); // Likes notifications |
||
| 79 | |||
| 80 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_likes' ), 60 ); |
||
| 81 | |||
| 82 | add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) ); |
||
| 83 | |||
| 84 | add_action( 'save_post', array( $this, 'meta_box_save' ) ); |
||
| 85 | add_action( 'edit_attachment', array( $this, 'meta_box_save' ) ); |
||
| 86 | add_action( 'sharing_global_options', array( $this, 'admin_settings_init' ), 20 ); |
||
| 87 | add_action( 'sharing_admin_update', array( $this, 'admin_settings_callback' ), 20 ); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set the social_notifications_like option to `on` when the Likes module is activated. |
||
| 92 | * |
||
| 93 | * @since 3.7.0 |
||
| 94 | * |
||
| 95 | * @return null |
||
| 96 | */ |
||
| 97 | function set_social_notifications_like() { |
||
| 98 | update_option( 'social_notifications_like', 'on' ); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Delete the social_notifications_like option that was set to `on` on module activation. |
||
| 103 | * |
||
| 104 | * @since 3.7.0 |
||
| 105 | * |
||
| 106 | * @return null |
||
| 107 | */ |
||
| 108 | function delete_social_notifications_like() { |
||
| 109 | delete_option( 'social_notifications_like' ); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Redirects to the likes section of the sharing page. |
||
| 114 | */ |
||
| 115 | function configuration_redirect() { |
||
| 116 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing#likes' ) ); |
||
| 117 | die(); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable |
||
| 122 | */ |
||
| 123 | function load_jp_css() { |
||
| 124 | // Do we really need `admin_styles`? With the new admin UI, it's breaking some bits. |
||
| 125 | // Jetpack::init()->admin_styles(); |
||
| 126 | } |
||
| 127 | /** |
||
| 128 | * Load style on the front end. |
||
| 129 | * @return null |
||
| 130 | */ |
||
| 131 | function load_styles_register_scripts() { |
||
| 132 | |||
| 133 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 134 | if( $this->in_jetpack ) { |
||
| 135 | $this->register_scripts(); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box |
||
| 141 | * @param string $html row heading for the sharedaddy "which page" setting |
||
| 142 | * @return string html with the jetpack-targetable class and likes id. tbody gets closed after the like settings |
||
| 143 | */ |
||
| 144 | function configuration_target_area( $html = '' ) { |
||
| 145 | $html = "<tbody id='likes' class='jetpack-targetable'>" . $html; |
||
| 146 | return $html; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares" |
||
| 151 | * @param string $title The current title of the metabox, not needed/used. |
||
| 152 | */ |
||
| 153 | function add_likes_to_sharing_meta_box_title( $title ) { |
||
| 154 | return __( 'Likes and Shares', 'jetpack' ); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Adds a metabox to the post screen if the sharing one doesn't currently exist. |
||
| 159 | */ |
||
| 160 | function add_meta_box() { |
||
| 161 | if ( |
||
| 162 | /** |
||
| 163 | * Allow disabling of the Likes metabox on the post editor screen. |
||
| 164 | * |
||
| 165 | * @module likes |
||
| 166 | * |
||
| 167 | * @since 2.2.0 |
||
| 168 | * |
||
| 169 | * @param bool false Should the Likes metabox be disabled? Default to false. |
||
| 170 | */ |
||
| 171 | apply_filters( 'post_flair_disable', false ) |
||
| 172 | ) { |
||
| 173 | return; |
||
| 174 | } |
||
| 175 | |||
| 176 | $post_types = get_post_types( array( 'public' => true ) ); |
||
| 177 | /** |
||
| 178 | * Filters the Likes metabox title. |
||
| 179 | * |
||
| 180 | * @module likes |
||
| 181 | * |
||
| 182 | * @since 2.2.0 |
||
| 183 | * |
||
| 184 | * @param string Likes metabox title. Default to "Likes". |
||
| 185 | */ |
||
| 186 | $title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) ); |
||
| 187 | foreach( $post_types as $post_type ) { |
||
| 188 | add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'advanced', 'high' ); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | function meta_box_save( $post_id ) { |
||
| 193 | if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) |
||
| 194 | return $post_id; |
||
| 195 | |||
| 196 | if ( empty( $_POST['wpl_like_status_hidden'] ) ) |
||
| 197 | return $post_id; |
||
| 198 | |||
| 199 | // Record sharing disable. Only needs to be done for WPCOM |
||
| 200 | if ( ! $this->in_jetpack ) { |
||
| 201 | if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ) ) ) { |
||
| 202 | View Code Duplication | if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) { |
|
| 203 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
||
| 204 | } else { |
||
| 205 | delete_post_meta( $post_id, 'sharing_disabled' ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | if ( 'post' == $_POST['post_type'] ) { |
||
| 211 | if ( !current_user_can( 'edit_post', $post_id ) ) { |
||
| 212 | return $post_id; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | // Record a change in like status for this post - only if it contradicts the |
||
| 217 | // site like setting. |
||
| 218 | if ( ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) || ( ! $this->is_enabled_sitewide() && !empty( $_POST['wpl_enable_post_likes'] ) ) ) { |
||
| 219 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 220 | //$g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=switched_post_like_status' ); @todo stat |
||
| 221 | } else { |
||
| 222 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 223 | } |
||
| 224 | |||
| 225 | return $post_id; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Shows the likes option in the post screen metabox. |
||
| 230 | */ |
||
| 231 | function meta_box_content( $post ) { |
||
| 232 | $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); |
||
| 233 | $checked = true; |
||
| 234 | $disabled = ! $this->is_enabled_sitewide(); |
||
| 235 | $switched_status = get_post_meta( $post_id, 'switch_like_status', true ); |
||
| 236 | |||
| 237 | if ( $disabled && empty( $switched_status ) || false == $disabled && !empty( $switched_status ) ) |
||
| 238 | $checked = false; |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Fires before the Likes meta box content in the post editor. |
||
| 242 | * |
||
| 243 | * @module likes |
||
| 244 | * |
||
| 245 | * @since 2.2.0 |
||
| 246 | * |
||
| 247 | * @param WP_Post|array|null $post Post data. |
||
| 248 | */ |
||
| 249 | do_action( 'start_likes_meta_box_content', $post ); |
||
| 250 | ?> |
||
| 251 | |||
| 252 | <p> |
||
| 253 | <label for="wpl_enable_post_likes"> |
||
| 254 | <input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>> |
||
| 255 | <?php esc_html_e( 'Show likes.', 'jetpack' ); ?> |
||
| 256 | </label> |
||
| 257 | <input type="hidden" name="wpl_like_status_hidden" value="1" /> |
||
| 258 | </p> <?php |
||
| 259 | /** |
||
| 260 | * Fires after the Likes meta box content in the post editor. |
||
| 261 | * |
||
| 262 | * @module likes |
||
| 263 | * |
||
| 264 | * @since 2.2.0 |
||
| 265 | * |
||
| 266 | * @param WP_Post|array|null $post Post data. |
||
| 267 | */ |
||
| 268 | do_action( 'end_likes_meta_box_content', $post ); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog) |
||
| 273 | */ |
||
| 274 | function sharing_meta_box_content( $post ) { |
||
| 275 | $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID(); |
||
| 276 | $disabled = get_post_meta( $post_id, 'sharing_disabled', true ); ?> |
||
| 277 | <p> |
||
| 278 | <label for="wpl_enable_post_sharing"> |
||
| 279 | <input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( !$disabled ); ?>> |
||
| 280 | <?php _e( 'Show sharing buttons.', 'jetpack' ); ?> |
||
| 281 | </label> |
||
| 282 | <input type="hidden" name="wpl_sharing_status_hidden" value="1" /> |
||
| 283 | </p> <?php |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page) |
||
| 288 | */ |
||
| 289 | |||
| 290 | View Code Duplication | function admin_discussion_likes_settings_init() { |
|
| 291 | // Add a temporary section, until we can move the setting out of there and with the rest of the email notification settings |
||
| 292 | add_settings_section( 'likes-notifications', __( 'Likes Notifications', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_section' ), 'discussion' ); |
||
| 293 | add_settings_field( 'social-notifications', __( 'Email me whenever', 'jetpack' ), array( $this, 'admin_discussion_likes_settings_field' ), 'discussion', 'likes-notifications' ); |
||
| 294 | // Register the setting |
||
| 295 | register_setting( 'discussion', 'social_notifications_like', array( $this, 'admin_discussion_likes_settings_validate' ) ); |
||
| 296 | } |
||
| 297 | |||
| 298 | function admin_discussion_likes_settings_section() { |
||
| 299 | // Atypical usage here. We emit jquery to move likes notification checkbox to be with the rest of the email notification settings |
||
| 300 | ?> |
||
| 301 | <script type="text/javascript"> |
||
| 302 | jQuery( function( $ ) { |
||
| 303 | var table = $( '#social_notifications_like' ).parents( 'table:first' ), |
||
| 304 | header = table.prevAll( 'h3:first' ), |
||
| 305 | newParent = $( '#moderation_notify' ).parent( 'label' ).parent(); |
||
| 306 | |||
| 307 | if ( !table.size() || !header.size() || !newParent.size() ) { |
||
| 308 | return; |
||
| 309 | } |
||
| 310 | |||
| 311 | newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() ); |
||
| 312 | header.remove(); |
||
| 313 | table.remove(); |
||
| 314 | } ); |
||
| 315 | </script> |
||
| 316 | <?php |
||
| 317 | } |
||
| 318 | |||
| 319 | function admin_likes_get_option( $option ) { |
||
| 320 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 321 | $option_setting = get_blog_option( get_current_blog_id(), $option, 'on' ); |
||
| 322 | } else { |
||
| 323 | $option_setting = get_option( $option, 'on' ); |
||
| 324 | } |
||
| 325 | |||
| 326 | return intval( 'on' == $option_setting ); |
||
| 327 | } |
||
| 328 | |||
| 329 | function admin_discussion_likes_settings_field() { |
||
| 330 | $like = $this->admin_likes_get_option( 'social_notifications_like' ); |
||
| 331 | ?> |
||
| 332 | <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> |
||
| 333 | <?php |
||
| 334 | } |
||
| 335 | |||
| 336 | function admin_discussion_likes_settings_validate( $input ) { |
||
| 337 | // If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'. |
||
| 338 | if ( !$input || 'off' == $input ) |
||
| 339 | return 'off'; |
||
| 340 | |||
| 341 | // Otherwise, return 'on'. |
||
| 342 | return 'on'; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * The actual options block to be inserted into the sharing page. |
||
| 347 | */ |
||
| 348 | function admin_settings_init() { ?> |
||
| 349 | <tr> |
||
| 350 | <th scope="row"> |
||
| 351 | <label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label> |
||
| 352 | </th> |
||
| 353 | <td> |
||
| 354 | <div> |
||
| 355 | <label> |
||
| 356 | <input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> /> |
||
| 357 | <?php esc_html_e( 'On for all posts', 'jetpack' ); ?> |
||
| 358 | </label> |
||
| 359 | </div> |
||
| 360 | <div> |
||
| 361 | <label> |
||
| 362 | <input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> /> |
||
| 363 | <?php esc_html_e( 'Turned on per post', 'jetpack' ); ?> |
||
| 364 | </label> |
||
| 365 | <div> |
||
| 366 | </td> |
||
| 367 | </tr> |
||
| 368 | <?php if ( ! $this->in_jetpack ) : ?> |
||
| 369 | <tr> |
||
| 370 | <th scope="row"> |
||
| 371 | <label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label> |
||
| 372 | </th> |
||
| 373 | <td> |
||
| 374 | <div> |
||
| 375 | <label> |
||
| 376 | <input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> /> |
||
| 377 | <?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?> |
||
| 378 | </label> |
||
| 379 | </div> |
||
| 380 | <div> |
||
| 381 | <label> |
||
| 382 | <input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> /> |
||
| 383 | <?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?> |
||
| 384 | </label> |
||
| 385 | <div> |
||
| 386 | </td> |
||
| 387 | </tr> |
||
| 388 | <tr> |
||
| 389 | <th scope="row"> |
||
| 390 | <label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label> |
||
| 391 | </th> |
||
| 392 | <td> |
||
| 393 | <div> |
||
| 394 | <label> |
||
| 395 | <input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> /> |
||
| 396 | <?php esc_html_e( 'On for all comments', 'jetpack' ); ?> |
||
| 397 | </label> |
||
| 398 | </div> |
||
| 399 | </td> |
||
| 400 | </tr> |
||
| 401 | <?php endif; ?> |
||
| 402 | </tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?> |
||
| 403 | <?php } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * 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. |
||
| 407 | */ |
||
| 408 | function admin_settings_showbuttonon_init() { ?> |
||
| 409 | <?php |
||
| 410 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 411 | echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' ); |
||
| 412 | ?> |
||
| 413 | <th scope="row"><label><?php _e( 'Show buttons on', 'jetpack' ); ?></label></th> |
||
| 414 | <td> |
||
| 415 | <?php |
||
| 416 | $br = false; |
||
| 417 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
||
| 418 | array_unshift( $shows, 'index' ); |
||
| 419 | $global = $this->get_options(); |
||
| 420 | View Code Duplication | foreach ( $shows as $show ) : |
|
| 421 | if ( 'index' == $show ) { |
||
| 422 | $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' ); |
||
| 423 | } else { |
||
| 424 | $post_type_object = get_post_type_object( $show ); |
||
| 425 | $label = $post_type_object->labels->name; |
||
| 426 | } |
||
| 427 | ?> |
||
| 428 | <?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> |
||
| 429 | <?php $br = true; endforeach; ?> |
||
| 430 | </td> |
||
| 431 | <?php |
||
| 432 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 433 | echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' ); |
||
| 434 | ?> |
||
| 435 | <?php } |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option. |
||
| 440 | */ |
||
| 441 | function admin_settings_showbuttonon_callback() { |
||
| 442 | $options = get_option( 'sharing-options' ); |
||
| 443 | if ( !is_array( $options ) ) |
||
| 444 | $options = array(); |
||
| 445 | |||
| 446 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
||
| 447 | $shows[] = 'index'; |
||
| 448 | $data = $_POST; |
||
| 449 | |||
| 450 | if ( isset( $data['show'] ) ) { |
||
| 451 | View Code Duplication | if ( is_scalar( $data['show'] ) ) { |
|
| 452 | switch ( $data['show'] ) { |
||
| 453 | case 'posts' : |
||
| 454 | $data['show'] = array( 'post', 'page' ); |
||
| 455 | break; |
||
| 456 | case 'index' : |
||
| 457 | $data['show'] = array( 'index' ); |
||
| 458 | break; |
||
| 459 | case 'posts-index' : |
||
| 460 | $data['show'] = array( 'post', 'page', 'index' ); |
||
| 461 | break; |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | View Code Duplication | if ( $data['show'] = array_intersect( $data['show'], $shows ) ) { |
|
| 466 | $options['global']['show'] = $data['show']; |
||
| 467 | } |
||
| 468 | } else { |
||
| 469 | $options['global']['show'] = array(); |
||
| 470 | } |
||
| 471 | |||
| 472 | update_option( 'sharing-options', $options ); |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled. |
||
| 477 | */ |
||
| 478 | function process_update_requests_if_sharedaddy_not_loaded() { |
||
| 479 | if ( isset( $_GET['page'] ) && ( $_GET['page'] == 'sharing.php' || $_GET['page'] == 'sharing' ) ) { |
||
| 480 | if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) { |
||
| 481 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 482 | do_action( 'sharing_admin_update' ); |
||
| 483 | wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) ); |
||
| 484 | die(); |
||
| 485 | } |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Saves the setting in the database, bumps a stat on WordPress.com |
||
| 491 | */ |
||
| 492 | function admin_settings_callback() { |
||
| 493 | // We're looking for these, and doing a dance to set some stats and save |
||
| 494 | // them together in array option. |
||
| 495 | $new_state = !empty( $_POST['wpl_default'] ) ? $_POST['wpl_default'] : 'on'; |
||
| 496 | $db_state = $this->is_enabled_sitewide(); |
||
| 497 | |||
| 498 | $reblogs_new_state = !empty( $_POST['jetpack_reblogs_enabled'] ) ? $_POST['jetpack_reblogs_enabled'] : 'on'; |
||
| 499 | $reblogs_db_state = $this->reblogs_enabled_sitewide(); |
||
| 500 | /** Default State *********************************************************/ |
||
| 501 | |||
| 502 | // Checked (enabled) |
||
| 503 | View Code Duplication | switch( $new_state ) { |
|
| 504 | case 'off' : |
||
| 505 | if ( true == $db_state && ! $this->in_jetpack ) { |
||
| 506 | $g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=disabled_likes' ); |
||
| 507 | } |
||
| 508 | update_option( 'disabled_likes', 1 ); |
||
| 509 | break; |
||
| 510 | case 'on' : |
||
| 511 | default: |
||
| 512 | if ( false == $db_state && ! $this->in_jetpack ) { |
||
| 513 | $g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_likes=reenabled_likes' ); |
||
| 514 | } |
||
| 515 | delete_option( 'disabled_likes' ); |
||
| 516 | break; |
||
| 517 | } |
||
| 518 | |||
| 519 | View Code Duplication | switch( $reblogs_new_state ) { |
|
| 520 | case 'off' : |
||
| 521 | if ( true == $reblogs_db_state && ! $this->in_jetpack ) { |
||
| 522 | $g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=disabled_reblogs' ); |
||
| 523 | } |
||
| 524 | update_option( 'disabled_reblogs', 1 ); |
||
| 525 | break; |
||
| 526 | case 'on' : |
||
| 527 | default: |
||
| 528 | if ( false == $reblogs_db_state && ! $this->in_jetpack ) { |
||
| 529 | $g_gif = file_get_contents( 'http://pixel.wp.com/g.gif?v=wpcom-no-pv&x_reblogs=reenabled_reblogs' ); |
||
| 530 | } |
||
| 531 | delete_option( 'disabled_reblogs' ); |
||
| 532 | break; |
||
| 533 | } |
||
| 534 | |||
| 535 | // comment setting |
||
| 536 | $new_comments_state = !empty( $_POST['jetpack_comment_likes_enabled'] ) ? $_POST['jetpack_comment_likes_enabled'] : false; |
||
| 537 | switch( (bool) $new_comments_state ) { |
||
| 538 | case true: |
||
| 539 | update_option( 'jetpack_comment_likes_enabled', 1 ); |
||
| 540 | break; |
||
| 541 | case false: |
||
| 542 | default: |
||
| 543 | update_option( 'jetpack_comment_likes_enabled', 0 ); |
||
| 544 | break; |
||
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Force comment likes on for a blog |
||
| 550 | * Used when a new blog is created |
||
| 551 | */ |
||
| 552 | function enable_comment_likes( $blog_id ) { |
||
| 553 | switch_to_blog( $blog_id ); |
||
| 554 | update_option( 'jetpack_comment_likes_enabled', 1 ); |
||
| 555 | restore_current_blog(); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Adds the 'sharing' menu to the settings menu. |
||
| 560 | * Only ran if sharedaddy and publicize are not already active. |
||
| 561 | */ |
||
| 562 | function sharing_menu() { |
||
| 563 | add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) ); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Provides a sharing page with the sharing_global_options hook |
||
| 568 | * so we can display the setting. |
||
| 569 | * Only ran if sharedaddy and publicize are not already active. |
||
| 570 | */ |
||
| 571 | function sharing_page() { |
||
| 572 | $this->updated_message(); ?> |
||
| 573 | <div class="wrap"> |
||
| 574 | <div class="icon32" id="icon-options-general"><br /></div> |
||
| 575 | <h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1> |
||
| 576 | <?php |
||
| 577 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 578 | do_action( 'pre_admin_screen_sharing' ); |
||
| 579 | ?> |
||
| 580 | <?php $this->sharing_block(); ?> |
||
| 581 | </div> <?php |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Returns the settings have been saved message. |
||
| 586 | */ |
||
| 587 | function updated_message() { |
||
| 588 | if ( isset( $_GET['update'] ) && $_GET['update'] == 'saved' ) |
||
| 589 | echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>'; |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts |
||
| 594 | */ |
||
| 595 | function sharing_block() { ?> |
||
| 596 | <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2> |
||
| 597 | <form method="post" action=""> |
||
| 598 | <table class="form-table"> |
||
| 599 | <tbody> |
||
| 600 | <?php |
||
| 601 | /** This action is documented in modules/sharedaddy/sharing.php */ |
||
| 602 | do_action( 'sharing_global_options' ); |
||
| 603 | ?> |
||
| 604 | </tbody> |
||
| 605 | </table> |
||
| 606 | |||
| 607 | <p class="submit"> |
||
| 608 | <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" /> |
||
| 609 | </p> |
||
| 610 | |||
| 611 | <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'sharing-options' );?>" /> |
||
| 612 | </form> <?php |
||
| 613 | } |
||
| 614 | |||
| 615 | function admin_init() { |
||
| 616 | add_filter( 'manage_posts_columns', array( $this, 'add_like_count_column' ) ); |
||
| 617 | add_filter( 'manage_pages_columns', array( $this, 'add_like_count_column' ) ); |
||
| 618 | add_action( 'manage_posts_custom_column', array( $this, 'likes_edit_column' ), 10, 2 ); |
||
| 619 | add_action( 'manage_pages_custom_column', array( $this, 'likes_edit_column' ), 10, 2 ); |
||
| 620 | add_action( 'admin_print_styles-edit.php', array( $this, 'load_admin_css' ) ); |
||
| 621 | add_action( "admin_print_scripts-edit.php", array( $this, 'enqueue_admin_scripts' ) ); |
||
| 622 | } |
||
| 623 | |||
| 624 | function action_init() { |
||
| 625 | if ( is_admin() ) { |
||
| 626 | return; |
||
| 627 | } |
||
| 628 | |||
| 629 | if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || |
||
| 630 | ( defined( 'APP_REQUEST' ) && APP_REQUEST ) || |
||
| 631 | ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) || |
||
| 632 | ( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) || |
||
| 633 | ( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) { |
||
| 634 | return; |
||
| 635 | } |
||
| 636 | |||
| 637 | // Comment Likes widget has been disabled, pending performance improvements. |
||
| 638 | // add_filter( 'comment_text', array( &$this, 'comment_likes' ), 10, 2 ); |
||
| 639 | |||
| 640 | if ( $this->in_jetpack ) { |
||
| 641 | add_filter( 'the_content', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 642 | add_filter( 'the_excerpt', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 643 | |||
| 644 | } else { |
||
| 645 | add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 646 | add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) ); |
||
| 647 | |||
| 648 | wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 649 | wp_enqueue_script( 'jquery_inview', '/wp-content/js/jquery/jquery.inview.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 650 | wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 651 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 652 | } |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Register scripts |
||
| 657 | */ |
||
| 658 | function register_scripts() { |
||
| 659 | // Lets register all the sciprts |
||
| 660 | wp_register_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 661 | wp_register_script( 'jquery_inview', plugins_url( '_inc/jquery.inview.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 662 | wp_register_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 663 | wp_register_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize', 'jquery_inview' ), JETPACK__VERSION, true ); |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Load the CSS needed for the wp-admin area. |
||
| 668 | */ |
||
| 669 | function load_admin_css() { |
||
| 670 | ?> |
||
| 671 | <?php if ( version_compare( $GLOBALS['wp_version'], '4.3-alpha', '>=' ) ) : ?> |
||
| 672 | <style type="text/css"> |
||
| 673 | .vers img { display: none; } |
||
| 674 | .metabox-prefs .vers img { display: inline; } |
||
| 675 | .fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; } |
||
| 676 | .fixed .column-stats { width: 5em; } |
||
| 677 | .fixed .column-likes .post-com-count { |
||
| 678 | -webkit-box-sizing: border-box; |
||
| 679 | -moz-box-sizing: border-box; |
||
| 680 | box-sizing: border-box; |
||
| 681 | display: inline-block; |
||
| 682 | padding: 0 8px; |
||
| 683 | height: 2em; |
||
| 684 | margin-top: 5px; |
||
| 685 | -webkit-border-radius: 5px; |
||
| 686 | border-radius: 5px; |
||
| 687 | background-color: #72777C; |
||
| 688 | color: #FFF; |
||
| 689 | font-size: 11px; |
||
| 690 | line-height: 21px; |
||
| 691 | } |
||
| 692 | .fixed .column-likes .post-com-count::after { border: none !important; } |
||
| 693 | .fixed .column-likes .post-com-count:hover { background-color: #0073AA; } |
||
| 694 | .fixed .column-likes .vers:before { |
||
| 695 | font: normal 20px/1 dashicons; |
||
| 696 | content: '\f155'; |
||
| 697 | speak: none; |
||
| 698 | -webkit-font-smoothing: antialiased; |
||
| 699 | -moz-osx-font-smoothing: grayscale; |
||
| 700 | } |
||
| 701 | @media screen and (max-width: 782px) { |
||
| 702 | .fixed .column-likes { |
||
| 703 | display: none; |
||
| 704 | } |
||
| 705 | } |
||
| 706 | </style> |
||
| 707 | <?php else : // @todo Remove when 4.3 is minimum ?> |
||
| 708 | <style type="text/css"> |
||
| 709 | .fixed .column-likes { width: 5em; padding-top: 8px; text-align: center !important; } |
||
| 710 | .fixed .column-stats { width: 5em; } |
||
| 711 | .fixed .column-likes .post-com-count { background-image: none; } |
||
| 712 | .fixed .column-likes .post-com-count::after { border: none !important; } |
||
| 713 | .fixed .column-likes .comment-count { background-color: #bbb; } |
||
| 714 | .fixed .column-likes .comment-count:hover { background-color: #2ea2cc; } |
||
| 715 | .fixed .column-likes .vers img { display: none; } |
||
| 716 | .fixed .column-likes .vers:before { |
||
| 717 | font: normal 20px/1 dashicons; |
||
| 718 | content: '\f155'; |
||
| 719 | speak: none; |
||
| 720 | -webkit-font-smoothing: antialiased; |
||
| 721 | -moz-osx-font-smoothing: grayscale; |
||
| 722 | } |
||
| 723 | @media screen and (max-width: 782px) { |
||
| 724 | .fixed .column-likes { |
||
| 725 | display: none; |
||
| 726 | } |
||
| 727 | } |
||
| 728 | </style> |
||
| 729 | <?php endif; ?> |
||
| 730 | <?php |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Load the JS required for loading the like counts. |
||
| 735 | */ |
||
| 736 | function enqueue_admin_scripts() { |
||
| 737 | if ( empty( $_GET['post_type'] ) || 'post' == $_GET['post_type'] || 'page' == $_GET['post_type'] ) { |
||
| 738 | if ( $this->in_jetpack ) { |
||
| 739 | wp_enqueue_script( 'likes-post-count', plugins_url( 'modules/likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 740 | wp_enqueue_script( 'likes-post-count-jetpack', plugins_url( 'modules/likes/post-count-jetpack.js', dirname( __FILE__ ) ), array( 'likes-post-count' ), JETPACK__VERSION ); |
||
| 741 | } else { |
||
| 742 | wp_enqueue_script( 'jquery.wpcom-proxy-request', "/wp-content/js/jquery/jquery.wpcom-proxy-request.js", array('jquery'), NULL, true ); |
||
| 743 | wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 744 | 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 ); |
||
| 745 | } |
||
| 746 | } |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Add "Likes" column data to the post edit table in wp-admin. |
||
| 751 | * |
||
| 752 | * @param string $column_name |
||
| 753 | * @param int $post_id |
||
| 754 | */ |
||
| 755 | function likes_edit_column( $column_name, $post_id ) { |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Add a "Likes" column header to the post edit table in wp-admin. |
||
| 774 | * |
||
| 775 | * @param array $columns |
||
| 776 | * @return array |
||
| 777 | */ |
||
| 778 | function add_like_count_column( $columns ) { |
||
| 779 | $date = $columns['date']; |
||
| 780 | unset( $columns['date'] ); |
||
| 781 | |||
| 782 | $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>'; |
||
| 783 | $columns['date'] = $date; |
||
| 784 | |||
| 785 | return $columns; |
||
| 786 | } |
||
| 787 | |||
| 788 | function post_likes( $content ) { |
||
| 789 | global $post; |
||
| 790 | |||
| 791 | if ( ! $this->is_likes_visible() ) |
||
| 792 | return $content; |
||
| 793 | |||
| 794 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 795 | $blog_id = get_current_blog_id(); |
||
| 796 | $bloginfo = get_blog_details( (int) $blog_id ); |
||
| 797 | $domain = $bloginfo->domain; |
||
| 798 | } else { |
||
| 799 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 800 | $url = home_url(); |
||
| 801 | $url_parts = parse_url( $url ); |
||
| 802 | $domain = $url_parts['host']; |
||
| 803 | } |
||
| 804 | // make sure to include the scripts before the iframe otherwise weird things happen |
||
| 805 | add_action( 'wp_footer', array( $this, 'likes_master' ), 21 ); |
||
| 806 | |||
| 807 | /** |
||
| 808 | * if the same post appears more then once on a page the page goes crazy |
||
| 809 | * we need a slightly more unique id / name for the widget wrapper. |
||
| 810 | */ |
||
| 811 | $uniqid = uniqid(); |
||
| 812 | |||
| 813 | $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 ); |
||
| 814 | $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid ); |
||
| 815 | $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid ); |
||
| 816 | |||
| 817 | $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>'; |
||
| 818 | $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>'; |
||
| 819 | $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>"; |
||
| 820 | $html .= '</div>'; |
||
| 821 | |||
| 822 | // Lets make sure that the script is enqued |
||
| 823 | wp_enqueue_script( 'jetpack_likes_queuehandler' ); |
||
| 824 | |||
| 825 | return $content . $html; |
||
| 826 | } |
||
| 827 | |||
| 828 | function comment_likes( $content, $comment = null ) { |
||
| 829 | if ( empty( $comment ) ) |
||
| 830 | return $content; |
||
| 831 | |||
| 832 | if ( ! $this->is_comments_enabled() ) |
||
| 833 | return $content; |
||
| 834 | |||
| 835 | $protocol = 'http'; |
||
| 836 | if ( is_ssl() ) |
||
| 837 | $protocol = 'https'; |
||
| 838 | |||
| 839 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 840 | $blog_id = get_current_blog_id(); |
||
| 841 | $bloginfo = get_blog_details( (int) $blog_id ); |
||
| 842 | $domain = $bloginfo->domain; |
||
| 843 | } else { |
||
| 844 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 845 | $url = home_url(); |
||
| 846 | $url_parts = parse_url( $url ); |
||
| 847 | $domain = $url_parts['host']; |
||
| 848 | } |
||
| 849 | // make sure to include the scripts before the iframe otherwise weird things happen |
||
| 850 | add_action( 'wp_footer', array( $this, 'likes_master' ), 21 ); |
||
| 851 | |||
| 852 | $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 ); |
||
| 853 | $name = sprintf( 'like-comment-frame-%1$d-%2$d', $blog_id, $comment->comment_ID ); |
||
| 854 | $wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d', $blog_id, $comment->comment_ID ); |
||
| 855 | |||
| 856 | $html = "<div><div class='jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper'>"; |
||
| 857 | $html .= "<iframe class='comment-likes-widget jetpack-likes-widget' name='$name' height='16px' width='100%' data='$src'></iframe>"; |
||
| 858 | $html .= '</div></div>'; |
||
| 859 | return $content . $html; |
||
| 860 | } |
||
| 861 | |||
| 862 | function post_flair_service_enabled_like( $classes ) { |
||
| 866 | |||
| 867 | function admin_bar_likes() { |
||
| 868 | global $wp_admin_bar, $post; |
||
| 869 | |||
| 904 | |||
| 905 | /** |
||
| 906 | * This function needs to get loaded after the scripts get added to the page. |
||
| 907 | * |
||
| 908 | */ |
||
| 909 | function likes_master() { |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Get the 'disabled_likes' option from the DB of the current blog. |
||
| 946 | * |
||
| 947 | * @return array |
||
| 948 | */ |
||
| 949 | function get_options() { |
||
| 987 | |||
| 988 | /** _is_ functions ************************************************************/ |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Are likes visible in this context? |
||
| 992 | * |
||
| 993 | * Some of this code was taken and modified from sharing_display() to ensure |
||
| 994 | * similar logic and filters apply here, too. |
||
| 995 | */ |
||
| 996 | function is_likes_visible() { |
||
| 1083 | |||
| 1084 | /** |
||
| 1085 | * Returns the current state of the "WordPress.com Likes are" option. |
||
| 1086 | * @return boolean true if enabled sitewide, false if not |
||
| 1087 | */ |
||
| 1088 | function is_enabled_sitewide() { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Returns the current state of the "WordPress.com Reblogs are" option. |
||
| 1104 | * @return boolean true if enabled sitewide, false if not |
||
| 1105 | */ |
||
| 1106 | function reblogs_enabled_sitewide() { |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Returns if comment likes are enabled. Defaults to 'off' |
||
| 1122 | * @todo decide what the default should be |
||
| 1123 | * @return boolean true if we should show comment likes, false if not |
||
| 1124 | */ |
||
| 1125 | function is_comments_enabled() { |
||
| 1126 | /** |
||
| 1127 | * Filters whether Comment Likes are enabled. |
||
| 1128 | * true if enabled, false if not. |
||
| 1129 | * |
||
| 1130 | * @module likes |
||
| 1131 | * |
||
| 1132 | * @since 2.2.0 |
||
| 1133 | * |
||
| 1134 | * @param bool $option Are Comment Likes enabled sitewide. |
||
| 1135 | */ |
||
| 1136 | return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) ); |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | function is_admin_bar_button_visible() { |
||
| 1140 | global $wp_admin_bar; |
||
| 1141 | |||
| 1142 | if ( ! is_object( $wp_admin_bar ) ) |
||
| 1143 | return false; |
||
| 1144 | |||
| 1145 | if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) ) |
||
| 1146 | return false; |
||
| 1147 | |||
| 1148 | if ( ! $this->is_likes_visible() ) |
||
| 1149 | return false; |
||
| 1150 | |||
| 1151 | if ( ! $this->is_post_likeable() ) |
||
| 1152 | return false; |
||
| 1153 | |||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Are likes enabled for this post? |
||
| 1168 | * |
||
| 1169 | * @param int $post_id |
||
| 1170 | * @retun bool |
||
| 1171 | */ |
||
| 1172 | function is_post_likeable( $post_id = 0 ) { |
||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Are Post Likes enabled on archive/front/search pages? |
||
| 1191 | * |
||
| 1192 | * @return bool |
||
| 1193 | */ |
||
| 1194 | function is_index_enabled() { |
||
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Are Post Likes enabled on single posts? |
||
| 1210 | * |
||
| 1211 | * @param String $post_type custom post type identifier |
||
| 1212 | * @return bool |
||
| 1213 | */ |
||
| 1214 | View Code Duplication | function is_single_post_enabled( $post_type = 'post' ) { |
|
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Are Post Likes enabled on single pages? |
||
| 1235 | * |
||
| 1236 | * @return bool |
||
| 1237 | */ |
||
| 1238 | View Code Duplication | function is_single_page_enabled() { |
|
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Are Media Likes enabled on single pages? |
||
| 1254 | * |
||
| 1255 | * @return bool |
||
| 1256 | */ |
||
| 1257 | function is_attachment_enabled() { |
||
| 1270 | } |
||
| 1271 | |||
| 1273 |
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: