Conditions | 18 |
Paths | 289 |
Total Lines | 157 |
Lines | 50 |
Ratio | 31.85 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
233 | function jetpack_notify_moderator( $notify_moderator, $comment_id ) { |
||
234 | |||
235 | // If Jetpack is not active, or if Notify moderators options is not set, let the default flow go on. |
||
236 | if ( ! $notify_moderator || ! Jetpack::is_active() ) { |
||
237 | return $notify_moderator; |
||
238 | } |
||
239 | |||
240 | // Original function modified: Removed code before the notify_moderator filter. |
||
241 | |||
242 | global $wpdb; |
||
243 | |||
244 | $comment = get_comment( $comment_id ); |
||
245 | $post = get_post( $comment->comment_post_ID ); |
||
246 | $user = get_userdata( $post->post_author ); |
||
247 | // Send to the administration and to the post author if the author can modify the comment. |
||
248 | $emails = array( get_option( 'admin_email' ) ); |
||
249 | if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) { |
||
250 | if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) { |
||
251 | $emails[] = $user->user_email; |
||
252 | } |
||
253 | } |
||
254 | |||
255 | $switched_locale = switch_to_locale( get_locale() ); |
||
256 | |||
257 | $comment_author_domain = ''; |
||
258 | if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) { |
||
259 | $comment_author_domain = gethostbyaddr( $comment->comment_author_IP ); |
||
260 | } |
||
261 | $comments_waiting = $wpdb->get_var( "SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'" ); |
||
262 | |||
263 | // The blogname option is escaped with esc_html on the way into the database in sanitize_option |
||
264 | // we want to reverse this for the plain text arena of emails. |
||
265 | $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
||
266 | $comment_content = wp_specialchars_decode( $comment->comment_content ); |
||
267 | |||
268 | switch ( $comment->comment_type ) { |
||
269 | View Code Duplication | case 'trackback': |
|
270 | /* translators: 1: Post title */ |
||
271 | $notify_message = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
||
272 | $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
||
273 | /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */ |
||
274 | $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
275 | /* translators: 1: Trackback/pingback/comment author URL */ |
||
276 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
277 | $notify_message .= __( 'Trackback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n"; |
||
278 | break; |
||
279 | View Code Duplication | case 'pingback': |
|
280 | /* translators: 1: Post title */ |
||
281 | $notify_message = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
||
282 | $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
||
283 | /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */ |
||
284 | $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
285 | /* translators: 1: Trackback/pingback/comment author URL */ |
||
286 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
287 | $notify_message .= __( 'Pingback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n"; |
||
288 | break; |
||
289 | View Code Duplication | default: // Comments. |
|
290 | /* translators: 1: Post title */ |
||
291 | $notify_message = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
||
292 | $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
||
293 | /* translators: 1: Comment author name, 2: comment author's IP address, 3: comment author's hostname */ |
||
294 | $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
295 | /* translators: 1: Comment author URL */ |
||
296 | $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; |
||
297 | /* translators: 1: Trackback/pingback/comment author URL */ |
||
298 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
299 | /* translators: 1: Comment text */ |
||
300 | $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
||
301 | break; |
||
302 | } |
||
303 | |||
304 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
305 | $emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id ); |
||
306 | |||
307 | // Original function modified. |
||
308 | $moderate_on_wpcom = ! in_array( false, array_map( 'jetpack_notify_is_user_connected_by_email', $emails ) ); |
||
309 | |||
310 | $base_wpcom_edit_comment_url = Redirect::get_url( |
||
311 | 'calypso-edit-comment', |
||
312 | array( |
||
313 | 'path' => $comment_id, |
||
314 | 'query' => 'action=__action__', // __action__ will be replaced by the actual action. |
||
315 | ) |
||
316 | ); |
||
317 | |||
318 | // Original function modified: Consider $moderate_on_wpcom when building $notify_message. |
||
319 | $notify_message .= sprintf( |
||
320 | /* translators: Comment moderation. 1: Comment action URL */ |
||
321 | __( 'Approve it: %s' ), |
||
322 | $moderate_on_wpcom |
||
323 | ? str_replace( '__action__', 'approve', $base_wpcom_edit_comment_url ) |
||
324 | : admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) |
||
325 | ) . "\r\n"; |
||
326 | |||
327 | View Code Duplication | if ( EMPTY_TRASH_DAYS ) { |
|
328 | $notify_message .= sprintf( |
||
329 | /* translators: Comment moderation. 1: Comment action URL */ |
||
330 | __( 'Trash it: %s' ), |
||
331 | $moderate_on_wpcom |
||
332 | ? str_replace( '__action__', 'trash', $base_wpcom_edit_comment_url ) |
||
333 | : admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) |
||
334 | ) . "\r\n"; |
||
335 | } else { |
||
336 | $notify_message .= sprintf( |
||
337 | /* translators: Comment moderation. 1: Comment action URL */ |
||
338 | __( 'Delete it: %s' ), |
||
339 | $moderate_on_wpcom |
||
340 | ? str_replace( '__action__', 'delete', $base_wpcom_edit_comment_url ) |
||
341 | : admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) |
||
342 | ) . "\r\n"; |
||
343 | } |
||
344 | |||
345 | $notify_message .= sprintf( |
||
346 | /* translators: Comment moderation. 1: Comment action URL */ |
||
347 | __( 'Spam it: %s' ), |
||
348 | $moderate_on_wpcom |
||
349 | ? str_replace( '__action__', 'spam', $base_wpcom_edit_comment_url ) |
||
350 | : admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) |
||
351 | ) . "\r\n"; |
||
352 | |||
353 | $notify_message .= sprintf( |
||
354 | /* translators: Comment moderation. 1: Number of comments awaiting approval */ |
||
355 | _n( |
||
356 | 'Currently %s comment is waiting for approval. Please visit the moderation panel:', |
||
357 | 'Currently %s comments are waiting for approval. Please visit the moderation panel:', |
||
358 | $comments_waiting |
||
359 | ), |
||
360 | number_format_i18n( $comments_waiting ) |
||
361 | ) . "\r\n"; |
||
362 | |||
363 | $notify_message .= $moderate_on_wpcom |
||
364 | ? Redirect::get_url( 'calypso-comments-pending' ) |
||
365 | : admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ) . "\r\n"; |
||
366 | |||
367 | /* translators: Comment moderation notification email subject. 1: Site name, 2: Post title */ |
||
368 | $subject = sprintf( __( '[%1$s] Please moderate: "%2$s"' ), $blogname, $post->post_title ); |
||
369 | $message_headers = ''; |
||
370 | |||
371 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
372 | $notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id ); |
||
373 | |||
374 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
375 | $subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id ); |
||
376 | |||
377 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
378 | $message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id ); |
||
379 | |||
380 | foreach ( $emails as $email ) { |
||
381 | wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); |
||
382 | } |
||
383 | |||
384 | if ( $switched_locale ) { |
||
385 | restore_previous_locale(); |
||
386 | } |
||
387 | |||
388 | return false; |
||
389 | } |
||
390 | |||
401 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.