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 = '20160429'; |
||
| 24 | |||
| 25 | public static function init() { |
||
| 34 | |||
| 35 | function __construct() { |
||
| 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() { |
||
| 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() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Redirects to the likes section of the sharing page. |
||
| 114 | */ |
||
| 115 | function configuration_redirect() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Loads Jetpack's CSS on the sharing page so we can use .jetpack-targetable |
||
| 122 | */ |
||
| 123 | function load_jp_css() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Load scripts and styles for front end. |
||
| 130 | * @return null |
||
| 131 | */ |
||
| 132 | function load_styles_register_scripts() { |
||
| 133 | if ( $this->in_jetpack ) { |
||
| 134 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 135 | $this->register_scripts(); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Adds in the jetpack-targetable class so when we visit sharing#likes our like settings get highlighted by a yellow box |
||
| 141 | * @param string $html row heading for the sharedaddy "which page" setting |
||
| 142 | * @return string html with the jetpack-targetable class and likes id. tbody gets closed after the like settings |
||
| 143 | */ |
||
| 144 | function configuration_target_area( $html = '' ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares" |
||
| 151 | */ |
||
| 152 | function add_likes_to_sharing_meta_box_title() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Adds a metabox to the post screen if the sharing one doesn't currently exist. |
||
| 158 | */ |
||
| 159 | function add_meta_box() { |
||
| 190 | |||
| 191 | function meta_box_save( $post_id ) { |
||
| 192 | if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) |
||
| 193 | return $post_id; |
||
| 194 | |||
| 195 | if ( empty( $_POST['wpl_like_status_hidden'] ) ) |
||
| 196 | return $post_id; |
||
| 197 | |||
| 198 | // Record sharing disable. Only needs to be done for WPCOM |
||
| 199 | if ( ! $this->in_jetpack ) { |
||
| 200 | if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ) ) ) { |
||
| 201 | View Code Duplication | if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) { |
|
| 202 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
||
| 203 | } else { |
||
| 204 | delete_post_meta( $post_id, 'sharing_disabled' ); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | if ( 'post' == $_POST['post_type'] ) { |
||
| 210 | if ( !current_user_can( 'edit_post', $post_id ) ) { |
||
| 211 | return $post_id; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | // Record a change in like status for this post - only if it contradicts the |
||
| 216 | // site like setting. |
||
| 217 | if ( ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) || ( ! $this->is_enabled_sitewide() && !empty( $_POST['wpl_enable_post_likes'] ) ) ) { |
||
| 218 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 219 | } else { |
||
| 220 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 221 | } |
||
| 222 | |||
| 223 | return $post_id; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Shows the likes option in the post screen metabox. |
||
| 228 | */ |
||
| 229 | function meta_box_content( $post ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog) |
||
| 271 | */ |
||
| 272 | function sharing_meta_box_content( $post ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Options to be added to the discussion page (see also admin_settings_init, etc below for Sharing settings page) |
||
| 286 | */ |
||
| 287 | |||
| 288 | View Code Duplication | function admin_discussion_likes_settings_init() { |
|
| 295 | |||
| 296 | function admin_discussion_likes_settings_section() { |
||
| 316 | |||
| 317 | function admin_likes_get_option( $option ) { |
||
| 326 | |||
| 327 | function admin_discussion_likes_settings_field() { |
||
| 333 | |||
| 334 | function admin_discussion_likes_settings_validate( $input ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * The actual options block to be inserted into the sharing page. |
||
| 345 | */ |
||
| 346 | function admin_settings_init() { ?> |
||
| 402 | |||
| 403 | /** |
||
| 404 | * If sharedaddy is not loaded, we don't have the "Show buttons on" yet, so we need to add that since it affects likes too. |
||
| 405 | */ |
||
| 406 | function admin_settings_showbuttonon_init() { ?> |
||
| 434 | |||
| 435 | |||
| 436 | /** |
||
| 437 | * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option. |
||
| 438 | */ |
||
| 439 | function admin_settings_showbuttonon_callback() { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled. |
||
| 475 | */ |
||
| 476 | function process_update_requests_if_sharedaddy_not_loaded() { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Saves the setting in the database, bumps a stat on WordPress.com |
||
| 489 | */ |
||
| 490 | function admin_settings_callback() { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Force comment likes on for a blog |
||
| 548 | * Used when a new blog is created |
||
| 549 | */ |
||
| 550 | function enable_comment_likes( $blog_id ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Adds the 'sharing' menu to the settings menu. |
||
| 558 | * Only ran if sharedaddy and publicize are not already active. |
||
| 559 | */ |
||
| 560 | function sharing_menu() { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Provides a sharing page with the sharing_global_options hook |
||
| 566 | * so we can display the setting. |
||
| 567 | * Only ran if sharedaddy and publicize are not already active. |
||
| 568 | */ |
||
| 569 | function sharing_page() { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Returns the settings have been saved message. |
||
| 584 | */ |
||
| 585 | function updated_message() { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts |
||
| 592 | */ |
||
| 593 | function sharing_block() { ?> |
||
| 612 | |||
| 613 | function admin_init() { |
||
| 621 | |||
| 622 | function action_init() { |
||
| 623 | if ( is_admin() ) { |
||
| 624 | return; |
||
| 625 | } |
||
| 626 | |||
| 627 | if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || |
||
| 628 | ( defined( 'APP_REQUEST' ) && APP_REQUEST ) || |
||
| 629 | ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) || |
||
| 630 | ( defined( 'COOKIE_AUTH_REQUEST' ) && COOKIE_AUTH_REQUEST ) || |
||
| 631 | ( defined( 'JABBER_SERVER' ) && JABBER_SERVER ) ) { |
||
| 632 | return; |
||
| 633 | } |
||
| 634 | |||
| 635 | // Comment Likes widget has been disabled, pending performance improvements. |
||
| 636 | // add_filter( 'comment_text', array( &$this, 'comment_likes' ), 10, 2 ); |
||
| 637 | |||
| 638 | if ( $this->in_jetpack ) { |
||
| 639 | add_filter( 'the_content', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 640 | add_filter( 'the_excerpt', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 641 | |||
| 642 | } else { |
||
| 643 | add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 ); |
||
| 644 | add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) ); |
||
| 645 | |||
| 646 | wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 647 | wp_enqueue_script( 'jquery_inview', '/wp-content/js/jquery/jquery.inview.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 648 | wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 649 | wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize', 'jquery_inview' ), JETPACK__VERSION, true ); |
||
| 650 | wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Register scripts |
||
| 656 | */ |
||
| 657 | function register_scripts() { |
||
| 658 | wp_register_script( 'postmessage', plugins_url( '_inc/postmessage.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 659 | wp_register_script( 'jquery_inview', plugins_url( '_inc/jquery.inview.js', dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 660 | wp_register_script( 'jetpack_resize', plugins_url( '_inc/jquery.jetpack-resize.js' , dirname(__FILE__) ), array( 'jquery' ), JETPACK__VERSION, false ); |
||
| 661 | wp_register_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize', 'jquery_inview' ), JETPACK__VERSION, true ); |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Load the CSS needed for the wp-admin area. |
||
| 666 | */ |
||
| 667 | function load_admin_css() { |
||
| 668 | ?> |
||
| 669 | <style type="text/css"> |
||
| 670 | .vers img { display: none; } |
||
| 671 | .metabox-prefs .vers img { display: inline; } |
||
| 672 | .fixed .column-likes { width: 5.5em; padding: 8px 0; text-align: left; } |
||
| 673 | .fixed .column-stats { width: 5em; } |
||
| 674 | .fixed .column-likes .post-com-count { |
||
| 675 | -webkit-box-sizing: border-box; |
||
| 676 | -moz-box-sizing: border-box; |
||
| 677 | box-sizing: border-box; |
||
| 678 | display: inline-block; |
||
| 679 | padding: 0 8px; |
||
| 680 | height: 2em; |
||
| 681 | margin-top: 5px; |
||
| 682 | -webkit-border-radius: 5px; |
||
| 683 | border-radius: 5px; |
||
| 684 | background-color: #72777C; |
||
| 685 | color: #FFF; |
||
| 686 | font-size: 11px; |
||
| 687 | line-height: 21px; |
||
| 688 | } |
||
| 689 | .fixed .column-likes .post-com-count::after { border: none !important; } |
||
| 690 | .fixed .column-likes .post-com-count:hover { background-color: #0073AA; } |
||
| 691 | .fixed .column-likes .vers:before { |
||
| 692 | font: normal 20px/1 dashicons; |
||
| 693 | content: '\f155'; |
||
| 694 | speak: none; |
||
| 695 | -webkit-font-smoothing: antialiased; |
||
| 696 | -moz-osx-font-smoothing: grayscale; |
||
| 697 | } |
||
| 698 | @media screen and (max-width: 782px) { |
||
| 699 | .fixed .column-likes { |
||
| 700 | display: none; |
||
| 701 | } |
||
| 702 | } |
||
| 703 | </style> |
||
| 704 | <?php |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Load the JS required for loading the like counts. |
||
| 709 | */ |
||
| 710 | function enqueue_admin_scripts() { |
||
| 711 | if ( empty( $_GET['post_type'] ) || 'post' == $_GET['post_type'] || 'page' == $_GET['post_type'] ) { |
||
| 712 | if ( $this->in_jetpack ) { |
||
| 713 | wp_enqueue_script( 'likes-post-count', plugins_url( 'modules/likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 714 | wp_enqueue_script( 'likes-post-count-jetpack', plugins_url( 'modules/likes/post-count-jetpack.js', dirname( __FILE__ ) ), array( 'likes-post-count' ), JETPACK__VERSION ); |
||
| 715 | } else { |
||
| 716 | wp_enqueue_script( 'jquery.wpcom-proxy-request', "/wp-content/js/jquery/jquery.wpcom-proxy-request.js", array('jquery'), NULL, true ); |
||
| 717 | wp_enqueue_script( 'likes-post-count', plugins_url( 'likes/post-count.js', dirname( __FILE__ ) ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 718 | wp_enqueue_script( 'likes-post-count-wpcom', plugins_url( 'likes/post-count-wpcom.js', dirname( __FILE__ ) ), array( 'likes-post-count', 'jquery.wpcom-proxy-request' ), JETPACK__VERSION ); |
||
| 719 | } |
||
| 720 | } |
||
| 721 | } |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Add "Likes" column data to the post edit table in wp-admin. |
||
| 725 | * |
||
| 726 | * @param string $column_name |
||
| 727 | * @param int $post_id |
||
| 728 | */ |
||
| 729 | function likes_edit_column( $column_name, $post_id ) { |
||
| 730 | if ( 'likes' == $column_name ) { |
||
| 731 | |||
| 732 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 733 | $blog_id = get_current_blog_id(); |
||
| 734 | } else { |
||
| 735 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 736 | } |
||
| 737 | |||
| 738 | $permalink = get_permalink( get_the_ID() ); ?> |
||
| 739 | <a title="" data-post-id="<?php echo (int) $post_id; ?>" class="post-com-count post-like-count" id="post-like-count-<?php echo (int) $post_id; ?>" data-blog-id="<?php echo (int) $blog_id; ?>" href="<?php echo esc_url( $permalink ); ?>#like-<?php echo (int) $post_id; ?>"> |
||
| 740 | <span class="comment-count">0</span> |
||
| 741 | </a> |
||
| 742 | <?php |
||
| 743 | } |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Add a "Likes" column header to the post edit table in wp-admin. |
||
| 748 | * |
||
| 749 | * @param array $columns |
||
| 750 | * @return array |
||
| 751 | */ |
||
| 752 | function add_like_count_column( $columns ) { |
||
| 761 | |||
| 762 | function post_likes( $content ) { |
||
| 763 | global $post; |
||
| 764 | |||
| 765 | if ( ! $this->is_likes_visible() ) |
||
| 766 | return $content; |
||
| 767 | |||
| 768 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 769 | $blog_id = get_current_blog_id(); |
||
| 770 | $bloginfo = get_blog_details( (int) $blog_id ); |
||
| 771 | $domain = $bloginfo->domain; |
||
| 772 | } else { |
||
| 773 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 774 | $url = home_url(); |
||
| 775 | $url_parts = parse_url( $url ); |
||
| 776 | $domain = $url_parts['host']; |
||
| 777 | } |
||
| 778 | // make sure to include the scripts before the iframe otherwise weird things happen |
||
| 779 | add_action( 'wp_footer', array( $this, 'likes_master' ), 21 ); |
||
| 780 | |||
| 781 | /** |
||
| 782 | * if the same post appears more then once on a page the page goes crazy |
||
| 783 | * we need a slightly more unique id / name for the widget wrapper. |
||
| 784 | */ |
||
| 785 | $uniqid = uniqid(); |
||
| 786 | |||
| 787 | $src = sprintf( '//widgets.wp.com/likes/#blog_id=%1$d&post_id=%2$d&origin=%3$s&obj_id=%1$d-%2$d-%4$s', $blog_id, $post->ID, $domain, $uniqid ); |
||
| 788 | $name = sprintf( 'like-post-frame-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid ); |
||
| 789 | $wrapper = sprintf( 'like-post-wrapper-%1$d-%2$d-%3$s', $blog_id, $post->ID, $uniqid ); |
||
| 790 | |||
| 791 | $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>'; |
||
| 792 | $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>'; |
||
| 793 | $html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>"; |
||
| 794 | $html .= '</div>'; |
||
| 795 | |||
| 796 | // Let's make sure that the script is enqueued |
||
| 797 | wp_enqueue_script( 'jetpack_likes_queuehandler' ); |
||
| 798 | |||
| 799 | return $content . $html; |
||
| 800 | } |
||
| 801 | |||
| 802 | function comment_likes( $content, $comment = null ) { |
||
| 803 | if ( empty( $comment ) ) |
||
| 804 | return $content; |
||
| 805 | |||
| 806 | if ( ! $this->is_comments_enabled() ) |
||
| 807 | return $content; |
||
| 808 | |||
| 809 | $protocol = 'http'; |
||
| 810 | if ( is_ssl() ) |
||
| 811 | $protocol = 'https'; |
||
| 812 | |||
| 813 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 814 | $blog_id = get_current_blog_id(); |
||
| 815 | $bloginfo = get_blog_details( (int) $blog_id ); |
||
| 816 | $domain = $bloginfo->domain; |
||
| 817 | } else { |
||
| 818 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 819 | $url = home_url(); |
||
| 820 | $url_parts = parse_url( $url ); |
||
| 821 | $domain = $url_parts['host']; |
||
| 822 | } |
||
| 823 | // make sure to include the scripts before the iframe otherwise weird things happen |
||
| 824 | add_action( 'wp_footer', array( $this, 'likes_master' ), 21 ); |
||
| 825 | |||
| 826 | $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 ); |
||
| 827 | $name = sprintf( 'like-comment-frame-%1$d-%2$d', $blog_id, $comment->comment_ID ); |
||
| 828 | $wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d', $blog_id, $comment->comment_ID ); |
||
| 829 | |||
| 830 | $html = "<div><div class='jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper'>"; |
||
| 831 | $html .= "<iframe class='comment-likes-widget jetpack-likes-widget' name='$name' height='16px' width='100%' data='$src'></iframe>"; |
||
| 832 | $html .= '</div></div>'; |
||
| 833 | return $content . $html; |
||
| 834 | } |
||
| 835 | |||
| 836 | function post_flair_service_enabled_like( $classes ) { |
||
| 840 | |||
| 841 | function admin_bar_likes() { |
||
| 878 | |||
| 879 | /** |
||
| 880 | * This function needs to get loaded after the scripts get added to the page. |
||
| 881 | * |
||
| 882 | */ |
||
| 883 | function likes_master() { |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Get the 'disabled_likes' option from the DB of the current blog. |
||
| 920 | * |
||
| 921 | * @return array |
||
| 922 | */ |
||
| 923 | function get_options() { |
||
| 961 | |||
| 962 | /** _is_ functions ************************************************************/ |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Are likes visible in this context? |
||
| 966 | * |
||
| 967 | * Some of this code was taken and modified from sharing_display() to ensure |
||
| 968 | * similar logic and filters apply here, too. |
||
| 969 | */ |
||
| 970 | function is_likes_visible() { |
||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Returns the current state of the "WordPress.com Likes are" option. |
||
| 1060 | * @return boolean true if enabled sitewide, false if not |
||
| 1061 | */ |
||
| 1062 | function is_enabled_sitewide() { |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Returns the current state of the "WordPress.com Reblogs are" option. |
||
| 1078 | * @return boolean true if enabled sitewide, false if not |
||
| 1079 | */ |
||
| 1080 | function reblogs_enabled_sitewide() { |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * Returns if comment likes are enabled. Defaults to 'off' |
||
| 1096 | * @return boolean true if we should show comment likes, false if not |
||
| 1097 | */ |
||
| 1098 | function is_comments_enabled() { |
||
| 1111 | |||
| 1112 | function is_admin_bar_button_visible() { |
||
| 1113 | global $wp_admin_bar; |
||
| 1114 | |||
| 1115 | if ( ! is_object( $wp_admin_bar ) ) |
||
| 1116 | return false; |
||
| 1117 | |||
| 1118 | if ( ( ! is_singular( 'post' ) && ! is_attachment() && ! is_page() ) ) |
||
| 1119 | return false; |
||
| 1120 | |||
| 1121 | if ( ! $this->is_likes_visible() ) |
||
| 1122 | return false; |
||
| 1123 | |||
| 1124 | if ( ! $this->is_post_likeable() ) |
||
| 1125 | return false; |
||
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Filters whether the Like button is enabled in the admin bar. |
||
| 1129 | * |
||
| 1130 | * @module likes |
||
| 1131 | * |
||
| 1132 | * @since 2.2.0 |
||
| 1133 | * |
||
| 1134 | * @param bool true Should the Like button be visible in the Admin bar. Default to true. |
||
| 1135 | */ |
||
| 1136 | return (bool) apply_filters( 'jetpack_admin_bar_likes_enabled', true ); |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Are likes enabled for this post? |
||
| 1141 | * |
||
| 1142 | * @param int $post_id |
||
| 1143 | * @retun bool |
||
| 1144 | */ |
||
| 1145 | function is_post_likeable( $post_id = 0 ) { |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Are Post Likes enabled on archive/front/search pages? |
||
| 1164 | * |
||
| 1165 | * @return bool |
||
| 1166 | */ |
||
| 1167 | function is_index_enabled() { |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Are Post Likes enabled on single posts? |
||
| 1183 | * |
||
| 1184 | * @param String $post_type custom post type identifier |
||
| 1185 | * @return bool |
||
| 1186 | */ |
||
| 1187 | View Code Duplication | function is_single_post_enabled( $post_type = 'post' ) { |
|
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Are Post Likes enabled on single pages? |
||
| 1208 | * |
||
| 1209 | * @return bool |
||
| 1210 | */ |
||
| 1211 | View Code Duplication | function is_single_page_enabled() { |
|
| 1224 | |||
| 1225 | /** |
||
| 1226 | * Are Media Likes enabled on single pages? |
||
| 1227 | * |
||
| 1228 | * @return bool |
||
| 1229 | */ |
||
| 1230 | function is_attachment_enabled() { |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | Jetpack_Likes::init(); |
||
| 1246 |
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: