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:
| 1 | <?php |
||
| 23 | class Jetpack_Comment_Likes { |
||
| 24 | public static function init() { |
||
| 25 | static $instance = NULL; |
||
| 26 | |||
| 27 | if ( ! $instance ) { |
||
| 28 | $instance = new Jetpack_Comment_Likes; |
||
| 29 | } |
||
| 30 | |||
| 31 | return $instance; |
||
| 32 | } |
||
| 33 | |||
| 34 | private function __construct() { |
||
| 35 | $this->settings = new Jetpack_Likes_Settings(); |
||
|
|
|||
| 36 | $this->blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 37 | $this->url = home_url(); |
||
| 38 | $this->url_parts = parse_url( $this->url ); |
||
| 39 | $this->domain = $this->url_parts['host']; |
||
| 40 | |||
| 41 | add_action( 'init', array( $this, 'frontend_init' ) ); |
||
| 42 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
| 43 | |||
| 44 | if ( ! Jetpack::is_module_active( 'likes' ) ) { |
||
| 45 | $active = Jetpack::get_active_modules(); |
||
| 46 | |||
| 47 | View Code Duplication | if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) { |
|
| 48 | // we don't have a sharing page yet |
||
| 49 | add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) ); |
||
| 50 | } |
||
| 51 | |||
| 52 | View Code Duplication | if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) { |
|
| 53 | // we have a sharing page but not the global options area |
||
| 54 | add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 ); |
||
| 55 | add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 ); |
||
| 56 | } |
||
| 57 | |||
| 58 | View Code Duplication | if( ! in_array( 'sharedaddy', $active ) ) { |
|
| 59 | add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) ); |
||
| 60 | add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 ); |
||
| 61 | add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 ); |
||
| 62 | add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) ); |
||
| 63 | } else { |
||
| 64 | add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) ); |
||
| 65 | add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) ); |
||
| 66 | } |
||
| 67 | |||
| 68 | add_action( 'save_post', array( $this->settings, 'meta_box_save' ) ); |
||
| 69 | add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) ); |
||
| 70 | add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 ); |
||
| 71 | add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_callback' ), 20 ); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | public function admin_init() { |
||
| 76 | add_filter( 'manage_edit-comments_columns', array( $this, 'add_like_count_column' ) ); |
||
| 77 | add_action( 'manage_comments_custom_column', array( $this, 'comment_likes_edit_column' ), 10, 2 ); |
||
| 78 | add_action( 'admin_print_styles-edit-comments.php', array( $this, 'enqueue_admin_styles_scripts' ) ); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function comment_likes_edit_column( $column_name, $comment_id ) { |
||
| 82 | if ( 'comment_likes' !== $column_name ) { |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | $permalink = get_permalink( get_the_ID() ); |
||
| 87 | ?> |
||
| 88 | <a |
||
| 89 | data-comment-id="<?php echo absint( $comment_id ); ?>" |
||
| 90 | data-blog-id="<?php echo absint( $this->blog_id ); ?>" |
||
| 91 | class="comment-like-count" |
||
| 92 | id="comment-like-count-<?php echo absint( $comment_id ); ?>" |
||
| 93 | href="<?php echo esc_url( $permalink ); ?>#comment-<?php echo absint( $comment_id ); ?>" |
||
| 94 | > |
||
| 95 | <span class="like-count">0</span> |
||
| 96 | </a> |
||
| 97 | <?php |
||
| 98 | } |
||
| 99 | |||
| 100 | function enqueue_admin_styles_scripts() { |
||
| 101 | wp_enqueue_style( 'comment-like-count', plugins_url( 'comment-likes/admin-style.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
| 102 | wp_enqueue_script( |
||
| 103 | 'comment-like-count', |
||
| 104 | Jetpack::get_file_url_for_environment( |
||
| 105 | '_inc/build/comment-likes/comment-like-count.min.js', |
||
| 106 | 'modules/comment-likes/comment-like-count.js' |
||
| 107 | ), |
||
| 108 | array( 'jquery' ), |
||
| 109 | JETPACK__VERSION |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function add_like_count_column( $columns ) { |
||
| 114 | $columns['comment_likes'] = '<span class="vers"></span>'; |
||
| 115 | |||
| 116 | return $columns; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function frontend_init() { |
||
| 120 | if ( is_admin() ) { |
||
| 121 | return; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ( Jetpack_AMP_Support::is_amp_request() ) { |
||
| 125 | return; |
||
| 126 | } |
||
| 127 | |||
| 128 | add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) ); |
||
| 129 | add_filter( 'comment_text', array( $this, 'comment_likes' ), 10, 2 ); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function load_styles_register_scripts() { |
||
| 156 | |||
| 157 | public function comment_likes( $content, $comment = null ) { |
||
| 207 | } |
||
| 208 | |||
| 209 | Jetpack_Comment_Likes::init(); |
||
| 210 |
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: