| Conditions | 9 |
| Paths | 145 |
| Total Lines | 159 |
| Lines | 50 |
| Ratio | 31.45 % |
| 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 |
||
| 241 | function wp_notify_moderator( $comment_id ) { |
||
| 242 | global $wpdb; |
||
| 243 | |||
| 244 | $maybe_notify = get_option( 'moderation_notify' ); |
||
| 245 | |||
| 246 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 247 | $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id ); |
||
| 248 | |||
| 249 | if ( ! $maybe_notify ) { |
||
| 250 | return true; |
||
| 251 | } |
||
| 252 | |||
| 253 | $comment = get_comment( $comment_id ); |
||
| 254 | $post = get_post( $comment->comment_post_ID ); |
||
| 255 | $user = get_userdata( $post->post_author ); |
||
| 256 | // Send to the administration and to the post author if the author can modify the comment. |
||
| 257 | $emails = array( get_option( 'admin_email' ) ); |
||
| 258 | if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) { |
||
| 259 | if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) { |
||
| 260 | $emails[] = $user->user_email; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | $switched_locale = switch_to_locale( get_locale() ); |
||
| 265 | |||
| 266 | $comment_author_domain = @gethostbyaddr( $comment->comment_author_IP ); |
||
| 267 | $comments_waiting = $wpdb->get_var( "SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'" ); |
||
| 268 | |||
| 269 | // The blogname option is escaped with esc_html on the way into the database in sanitize_option |
||
| 270 | // we want to reverse this for the plain text arena of emails. |
||
| 271 | $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
||
| 272 | $comment_content = wp_specialchars_decode( $comment->comment_content ); |
||
| 273 | |||
| 274 | switch ( $comment->comment_type ) { |
||
| 275 | View Code Duplication | case 'trackback': |
|
| 276 | /* translators: 1: Post title */ |
||
| 277 | $notify_message = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
||
| 278 | $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
||
| 279 | /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */ |
||
| 280 | $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
| 281 | /* translators: 1: Trackback/pingback/comment author URL */ |
||
| 282 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
| 283 | $notify_message .= __( 'Trackback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n"; |
||
| 284 | break; |
||
| 285 | View Code Duplication | case 'pingback': |
|
| 286 | /* translators: 1: Post title */ |
||
| 287 | $notify_message = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
||
| 288 | $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
||
| 289 | /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */ |
||
| 290 | $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
| 291 | /* translators: 1: Trackback/pingback/comment author URL */ |
||
| 292 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
| 293 | $notify_message .= __( 'Pingback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n"; |
||
| 294 | break; |
||
| 295 | View Code Duplication | default: // Comments. |
|
| 296 | /* translators: 1: Post title */ |
||
| 297 | $notify_message = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
||
| 298 | $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
||
| 299 | /* translators: 1: Comment author name, 2: comment author's IP address, 3: comment author's hostname */ |
||
| 300 | $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
| 301 | /* translators: 1: Comment author URL */ |
||
| 302 | $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; |
||
| 303 | /* translators: 1: Trackback/pingback/comment author URL */ |
||
| 304 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
| 305 | /* translators: 1: Comment text */ |
||
| 306 | $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
||
| 307 | break; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 311 | $emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id ); |
||
| 312 | |||
| 313 | // phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
||
| 314 | function is_user_connected( $email ) { |
||
| 315 | $user = get_user_by( 'email', $email ); |
||
| 316 | return Jetpack::is_user_connected( $user->ID ); |
||
| 317 | } |
||
| 318 | |||
| 319 | $moderate_on_wpcom = ! in_array( false, array_map( 'is_user_connected', $emails ) ); |
||
| 320 | |||
| 321 | $base_wpcom_edit_comment_url = Redirect::get_url( |
||
| 322 | 'calypso-edit-comment', |
||
| 323 | array( |
||
| 324 | 'path' => $comment_id, |
||
| 325 | 'query' => 'action=__action__', // __action__ will be replaced by the actual action. |
||
| 326 | ) |
||
| 327 | ); |
||
| 328 | |||
| 329 | $notify_message .= sprintf( |
||
| 330 | /* translators: Comment moderation. 1: Comment action URL */ |
||
| 331 | __( 'Approve it: %s' ), |
||
| 332 | $moderate_on_wpcom |
||
| 333 | ? str_replace( '__action__', 'approve', $base_wpcom_edit_comment_url ) |
||
| 334 | : admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) |
||
| 335 | ) . "\r\n"; |
||
| 336 | |||
| 337 | View Code Duplication | if ( EMPTY_TRASH_DAYS ) { |
|
| 338 | $notify_message .= sprintf( |
||
| 339 | /* translators: Comment moderation. 1: Comment action URL */ |
||
| 340 | __( 'Trash it: %s' ), |
||
| 341 | $moderate_on_wpcom |
||
| 342 | ? str_replace( '__action__', 'trash', $base_wpcom_edit_comment_url ) |
||
| 343 | : admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) |
||
| 344 | ) . "\r\n"; |
||
| 345 | } else { |
||
| 346 | $notify_message .= sprintf( |
||
| 347 | /* translators: Comment moderation. 1: Comment action URL */ |
||
| 348 | __( 'Delete it: %s' ), |
||
| 349 | $moderate_on_wpcom |
||
| 350 | ? str_replace( '__action__', 'delete', $base_wpcom_edit_comment_url ) |
||
| 351 | : admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) |
||
| 352 | ) . "\r\n"; |
||
| 353 | } |
||
| 354 | |||
| 355 | $notify_message .= sprintf( |
||
| 356 | /* translators: Comment moderation. 1: Comment action URL */ |
||
| 357 | __( 'Spam it: %s' ), |
||
| 358 | $moderate_on_wpcom |
||
| 359 | ? str_replace( '__action__', 'spam', $base_wpcom_edit_comment_url ) |
||
| 360 | : admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) |
||
| 361 | ) . "\r\n"; |
||
| 362 | |||
| 363 | $notify_message .= sprintf( |
||
| 364 | /* translators: Comment moderation. 1: Number of comments awaiting approval */ |
||
| 365 | _n( |
||
| 366 | 'Currently %s comment is waiting for approval. Please visit the moderation panel:', |
||
| 367 | 'Currently %s comments are waiting for approval. Please visit the moderation panel:', |
||
| 368 | $comments_waiting |
||
| 369 | ), |
||
| 370 | number_format_i18n( $comments_waiting ) |
||
| 371 | ) . "\r\n"; |
||
| 372 | |||
| 373 | $notify_message .= $moderate_on_wpcom |
||
| 374 | ? Redirect::get_url( 'calypso-comments-pending' ) |
||
| 375 | : admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ) . "\r\n"; |
||
| 376 | |||
| 377 | /* translators: Comment moderation notification email subject. 1: Site name, 2: Post title */ |
||
| 378 | $subject = sprintf( __( '[%1$s] Please moderate: "%2$s"' ), $blogname, $post->post_title ); |
||
| 379 | $message_headers = ''; |
||
| 380 | |||
| 381 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 382 | $notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id ); |
||
| 383 | |||
| 384 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 385 | $subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id ); |
||
| 386 | |||
| 387 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 388 | $message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id ); |
||
| 389 | |||
| 390 | foreach ( $emails as $email ) { |
||
| 391 | @wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); |
||
| 392 | } |
||
| 393 | |||
| 394 | if ( $switched_locale ) { |
||
| 395 | restore_previous_locale(); |
||
| 396 | } |
||
| 397 | |||
| 398 | return true; |
||
| 399 | } |
||
| 400 | endif; |
||
| 401 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.