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_Comments 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_Comments, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Jetpack_Comments extends Highlander_Comments_Base { |
||
13 | |||
14 | /** Variables *************************************************************/ |
||
15 | |||
16 | /** |
||
17 | * Possible comment form sources |
||
18 | * @var array |
||
19 | */ |
||
20 | public $id_sources = array(); |
||
21 | |||
22 | /** |
||
23 | * URL |
||
24 | * @var string |
||
25 | */ |
||
26 | public $signed_url = ''; |
||
27 | |||
28 | /** |
||
29 | * The default comment form color scheme |
||
30 | * @var string |
||
31 | * @see ::set_default_color_theme_based_on_theme_settings() |
||
32 | */ |
||
33 | public $default_color_scheme = 'light'; |
||
34 | |||
35 | /** Methods ***************************************************************/ |
||
36 | |||
37 | public static function init() { |
||
46 | |||
47 | /** |
||
48 | * Main constructor for Comments |
||
49 | * |
||
50 | * @since JetpackComments (1.4) |
||
51 | */ |
||
52 | public function __construct() { |
||
69 | |||
70 | public function set_default_color_theme_based_on_theme_settings() { |
||
86 | |||
87 | /** Private Methods *******************************************************/ |
||
88 | |||
89 | /** |
||
90 | * Set any global variables or class variables |
||
91 | * @since JetpackComments (1.4) |
||
92 | */ |
||
93 | protected function setup_globals() { |
||
105 | |||
106 | /** |
||
107 | * Setup actions for methods in this class |
||
108 | * @since JetpackComments (1.4) |
||
109 | */ |
||
110 | protected function setup_actions() { |
||
127 | |||
128 | /** |
||
129 | * Setup filters for methods in this class |
||
130 | * @since 1.6.2 |
||
131 | */ |
||
132 | protected function setup_filters() { |
||
138 | |||
139 | /** |
||
140 | * Get the comment avatar from Gravatar, Twitter, or Facebook |
||
141 | * |
||
142 | * @since JetpackComments (1.4) |
||
143 | * @param string $avatar Current avatar URL |
||
144 | * @param string $comment Comment for the avatar |
||
145 | * @param int $size Size of the avatar |
||
146 | * @param string $default Not used |
||
147 | * @return string New avatar |
||
148 | */ |
||
149 | public function get_avatar( $avatar, $comment, $size, $default ) { |
||
170 | |||
171 | /** Output Methods ********************************************************/ |
||
172 | |||
173 | /** |
||
174 | * Start capturing the core comment_form() output |
||
175 | * @since JetpackComments (1.4) |
||
176 | */ |
||
177 | public function comment_form_before() { |
||
197 | |||
198 | /** |
||
199 | * Noop the default comment form output, get some options, and output our |
||
200 | * tricked out totally radical comment form. |
||
201 | * |
||
202 | * @since JetpackComments (1.4) |
||
203 | */ |
||
204 | public function comment_form_after() { |
||
205 | /** This filter is documented in modules/comments/comments.php */ |
||
206 | if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type(), true ) ) { |
||
207 | return; |
||
208 | } |
||
209 | |||
210 | // Throw it all out and drop in our replacement |
||
211 | ob_end_clean(); |
||
212 | |||
213 | // If users are required to be logged in, and they're not, then we don't need to do anything else |
||
214 | if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) { |
||
215 | /** |
||
216 | * Changes the log in to comment prompt. |
||
217 | * |
||
218 | * @module comments |
||
219 | * |
||
220 | * @since 1.4.0 |
||
221 | * |
||
222 | * @param string $var Default is "You must log in to post a comment." |
||
223 | */ |
||
224 | echo '<p class="must-log-in">' . sprintf( apply_filters( 'jetpack_must_log_in_to_comment', __( 'You must <a href="%s">log in</a> to post a comment.', 'jetpack' ) ), wp_login_url( get_permalink() . '#respond' ) ) . '</p>'; |
||
225 | return; |
||
226 | } |
||
227 | |||
228 | if ( in_array( 'subscriptions', Jetpack::get_active_modules() ) ) { |
||
229 | $stb_enabled = get_option( 'stb_enabled', 1 ); |
||
230 | $stb_enabled = empty( $stb_enabled ) ? 0 : 1; |
||
231 | |||
232 | $stc_enabled = get_option( 'stc_enabled', 1 ); |
||
233 | $stc_enabled = empty( $stc_enabled ) ? 0 : 1; |
||
234 | } else { |
||
235 | $stb_enabled = 0; |
||
236 | $stc_enabled = 0; |
||
237 | } |
||
238 | |||
239 | $params = array( |
||
240 | 'blogid' => Jetpack_Options::get_option( 'id' ), |
||
241 | 'postid' => get_the_ID(), |
||
242 | 'comment_registration' => ( get_option( 'comment_registration' ) ? '1' : '0' ), // Need to explicitly send a '1' or a '0' for these |
||
243 | 'require_name_email' => ( get_option( 'require_name_email' ) ? '1' : '0' ), |
||
244 | 'stc_enabled' => $stc_enabled, |
||
245 | 'stb_enabled' => $stb_enabled, |
||
246 | 'show_avatars' => ( get_option( 'show_avatars' ) ? '1' : '0' ), |
||
247 | 'avatar_default' => get_option( 'avatar_default' ), |
||
248 | 'greeting' => get_option( 'highlander_comment_form_prompt', __( 'Leave a Reply', 'jetpack' ) ), |
||
249 | /** |
||
250 | * Changes the comment form prompt. |
||
251 | * |
||
252 | * @module comments |
||
253 | * |
||
254 | * @since 2.3.0 |
||
255 | * |
||
256 | * @param string $var Default is "Leave a Reply to %s." |
||
257 | */ |
||
258 | 'greeting_reply' => apply_filters( 'jetpack_comment_form_prompt_reply', __( 'Leave a Reply to %s' , 'jetpack' ) ), |
||
259 | 'color_scheme' => get_option( 'jetpack_comment_form_color_scheme', $this->default_color_scheme ), |
||
260 | 'lang' => get_bloginfo( 'language' ), |
||
261 | 'jetpack_version' => JETPACK__VERSION, |
||
262 | ); |
||
263 | |||
264 | // Extra parameters for logged in user |
||
265 | if ( is_user_logged_in() ) { |
||
266 | $current_user = wp_get_current_user(); |
||
267 | $params['hc_post_as'] = 'jetpack'; |
||
268 | $params['hc_userid'] = $current_user->ID; |
||
269 | $params['hc_username'] = $current_user->display_name; |
||
270 | $params['hc_userurl'] = $current_user->user_url; |
||
271 | $params['hc_useremail'] = md5( strtolower( trim( $current_user->user_email ) ) ); |
||
272 | if ( current_user_can( 'unfiltered_html' ) ) |
||
273 | $params['_wp_unfiltered_html_comment'] = wp_create_nonce( 'unfiltered-html-comment_' . get_the_ID() ); |
||
274 | } |
||
275 | |||
276 | $signature = Jetpack_Comments::sign_remote_comment_parameters( $params, Jetpack_Options::get_option( 'blog_token' ) ); |
||
277 | if ( is_wp_error( $signature ) ) { |
||
278 | $signature = 'error'; |
||
279 | } |
||
280 | |||
281 | $params['sig'] = $signature; |
||
282 | $url = "https://jetpack.wordpress.com/jetpack-comment/?" . http_build_query( $params ); |
||
283 | $url = "{$url}#parent=" . urlencode( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ); |
||
284 | $this->signed_url = $url; |
||
285 | $height = $params['comment_registration'] || is_user_logged_in() ? '315' : '430'; // Iframe can be shorter if we're not allowing guest commenting |
||
286 | $transparent = ( $params['color_scheme'] == 'transparent' ) ? 'true' : 'false'; |
||
287 | |||
288 | if ( isset( $_GET['replytocom'] ) ) { |
||
289 | $url .= '&replytocom=' . (int) $_GET['replytocom']; |
||
290 | } |
||
291 | |||
292 | // The actual iframe (loads comment form from Jetpack server) |
||
293 | ?> |
||
294 | |||
295 | <div id="respond" class="comment-respond"> |
||
296 | <h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( esc_html( $params['greeting'] ), esc_html( $params['greeting_reply'] ) ); ?> <small><?php cancel_comment_reply_link( esc_html__( 'Cancel reply' , 'jetpack') ); ?></small></h3> |
||
297 | <form id="commentform" class="comment-form"> |
||
298 | <iframe src="<?php echo esc_url( $url ); ?>" allowtransparency="<?php echo $transparent; ?>" style="width:100%; height: <?php echo $height; ?>px;border:0;" frameBorder="0" scrolling="no" name="jetpack_remote_comment" id="jetpack_remote_comment"></iframe> |
||
299 | </form> |
||
300 | </div> |
||
301 | |||
302 | <?php // Below is required for comment reply JS to work ?> |
||
303 | |||
304 | <input type="hidden" name="comment_parent" id="comment_parent" value="" /> |
||
305 | |||
306 | <?php |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Add some JS to wp_footer to watch for hierarchical reply parent change |
||
311 | * |
||
312 | * @since JetpackComments (1.4) |
||
313 | */ |
||
314 | public function watch_comment_parent() { |
||
406 | |||
407 | /** |
||
408 | * Verify the hash included in remote comments. |
||
409 | * |
||
410 | * @since JetpackComments (1.4) |
||
411 | * @param type $comment Not used |
||
412 | */ |
||
413 | public function pre_comment_on_post( $comment ) { |
||
442 | |||
443 | /** Capabilities **********************************************************/ |
||
444 | |||
445 | /** |
||
446 | * Add some additional comment meta after comment is saved about what |
||
447 | * service the comment is from, the avatar, user_id, etc... |
||
448 | * |
||
449 | * @since JetpackComments (1.4) |
||
450 | * @param type $comment_id |
||
451 | */ |
||
452 | public function add_comment_meta( $comment_id ) { |
||
572 | } |
||
573 | |||
575 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.