| Conditions | 15 |
| Paths | 18470 |
| Total Lines | 205 |
| Lines | 39 |
| Ratio | 19.02 % |
| 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 |
||
| 20 | function wp_notify_postauthor( $comment_id, $deprecated = null ) { |
||
| 21 | if ( null !== $deprecated ) { |
||
| 22 | _deprecated_argument( __FUNCTION__, '3.8.0' ); |
||
| 23 | } |
||
| 24 | |||
| 25 | $comment = get_comment( $comment_id ); |
||
| 26 | |||
| 27 | if ( empty( $comment ) || empty( $comment->comment_post_ID ) ) { |
||
| 28 | return false; |
||
| 29 | } |
||
| 30 | |||
| 31 | $post = get_post( $comment->comment_post_ID ); |
||
| 32 | $author = get_userdata( $post->post_author ); |
||
| 33 | |||
| 34 | // Who to notify? By default, just the post author, but others can be added. |
||
| 35 | $emails = array(); |
||
| 36 | if ( $author ) { |
||
| 37 | $emails[] = $author->user_email; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 41 | $emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID ); |
||
| 42 | $emails = array_filter( $emails ); |
||
| 43 | |||
| 44 | // If there are no addresses to send the comment to, bail. |
||
| 45 | if ( ! count( $emails ) ) { |
||
| 46 | return false; |
||
| 47 | } |
||
| 48 | |||
| 49 | // Facilitate unsetting below without knowing the keys. |
||
| 50 | $emails = array_flip( $emails ); |
||
| 51 | |||
| 52 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 53 | $notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID ); |
||
| 54 | |||
| 55 | // The comment was left by the author. |
||
| 56 | if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) { |
||
| 57 | unset( $emails[ $author->user_email ] ); |
||
| 58 | } |
||
| 59 | |||
| 60 | // The author moderated a comment on their own post. |
||
| 61 | if ( $author && ! $notify_author && get_current_user_id() == $post->post_author ) { |
||
| 62 | unset( $emails[ $author->user_email ] ); |
||
| 63 | } |
||
| 64 | |||
| 65 | // The post author is no longer a member of the blog. |
||
| 66 | if ( $author && ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) { |
||
| 67 | unset( $emails[ $author->user_email ] ); |
||
| 68 | } |
||
| 69 | |||
| 70 | // If there's no email to send the comment to, bail, otherwise flip array back around for use below. |
||
| 71 | if ( ! count( $emails ) ) { |
||
| 72 | return false; |
||
| 73 | } else { |
||
| 74 | $emails = array_flip( $emails ); |
||
| 75 | } |
||
| 76 | |||
| 77 | $switched_locale = switch_to_locale( get_locale() ); |
||
| 78 | |||
| 79 | $comment_author_domain = @gethostbyaddr( $comment->comment_author_IP ); |
||
| 80 | |||
| 81 | // The blogname option is escaped with esc_html on the way into the database in sanitize_option |
||
| 82 | // we want to reverse this for the plain text arena of emails. |
||
| 83 | $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
||
| 84 | $comment_content = wp_specialchars_decode( $comment->comment_content ); |
||
| 85 | |||
| 86 | // phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
||
| 87 | function is_user_connected( $email ) { |
||
| 88 | $user = get_user_by( 'email', $email ); |
||
| 89 | return Jetpack::is_user_connected( $user->ID ); |
||
| 90 | } |
||
| 91 | |||
| 92 | $moderate_on_wpcom = ! in_array( false, array_map( 'is_user_connected', $emails ) ); |
||
| 93 | |||
| 94 | switch ( $comment->comment_type ) { |
||
| 95 | View Code Duplication | case 'trackback': |
|
| 96 | /* translators: 1: Post title */ |
||
| 97 | $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n"; |
||
| 98 | /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */ |
||
| 99 | $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
| 100 | /* translators: %s: Site URL */ |
||
| 101 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
| 102 | /* translators: %s: Comment Content */ |
||
| 103 | $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
||
| 104 | $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n"; |
||
| 105 | /* translators: 1: blog name, 2: post title */ |
||
| 106 | $subject = sprintf( __( '[%1$s] Trackback: "%2$s"' ), $blogname, $post->post_title ); |
||
| 107 | break; |
||
| 108 | View Code Duplication | case 'pingback': |
|
| 109 | /* translators: 1: Post title */ |
||
| 110 | $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n"; |
||
| 111 | /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */ |
||
| 112 | $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
| 113 | /* translators: %s: Site URL */ |
||
| 114 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
| 115 | /* translators: %s: Comment Content */ |
||
| 116 | $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
||
| 117 | $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n"; |
||
| 118 | /* translators: 1: blog name, 2: post title */ |
||
| 119 | $subject = sprintf( __( '[%1$s] Pingback: "%2$s"' ), $blogname, $post->post_title ); |
||
| 120 | break; |
||
| 121 | default: // Comments. |
||
| 122 | /* translators: 1: Post title */ |
||
| 123 | $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n"; |
||
| 124 | /* translators: 1: comment author, 2: comment author's IP address, 3: comment author's hostname */ |
||
| 125 | $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
| 126 | /* translators: %s: Email address */ |
||
| 127 | $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; |
||
| 128 | /* translators: %s: Site URL */ |
||
| 129 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
| 130 | /* translators: %s: Comment Content */ |
||
| 131 | $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
||
| 132 | $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n"; |
||
| 133 | /* translators: 1: blog name, 2: post title */ |
||
| 134 | $subject = sprintf( __( '[%1$s] Comment: "%2$s"' ), $blogname, $post->post_title ); |
||
| 135 | break; |
||
| 136 | } |
||
| 137 | |||
| 138 | $notify_message .= $moderate_on_wpcom |
||
| 139 | ? Redirect::get_url( |
||
| 140 | 'calypso-comments-all', |
||
| 141 | array( |
||
| 142 | 'path' => $comment->comment_post_ID, |
||
| 143 | ) |
||
| 144 | ) . "/\r\n\r\n" |
||
| 145 | : get_permalink( $comment->comment_post_ID ) . "#comments\r\n\r\n"; |
||
| 146 | |||
| 147 | /* translators: %s: URL */ |
||
| 148 | $notify_message .= sprintf( __( 'Permalink: %s' ), get_comment_link( $comment ) ) . "\r\n"; |
||
| 149 | |||
| 150 | $base_wpcom_edit_comment_url = Redirect::get_url( |
||
| 151 | 'calypso-edit-comment', |
||
| 152 | array( |
||
| 153 | 'path' => $comment_id, |
||
| 154 | 'query' => 'action=__action__', // __action__ will be replaced by the actual action. |
||
| 155 | ) |
||
| 156 | ); |
||
| 157 | |||
| 158 | if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) { |
||
| 159 | View Code Duplication | if ( EMPTY_TRASH_DAYS ) { |
|
| 160 | $notify_message .= sprintf( |
||
| 161 | /* translators: Placeholder is the edit URL */ |
||
| 162 | __( 'Trash it: %s' ), |
||
| 163 | $moderate_on_wpcom |
||
| 164 | ? str_replace( '__action__', 'trash', $base_wpcom_edit_comment_url ) |
||
| 165 | : admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) |
||
| 166 | ) . "\r\n"; |
||
| 167 | } else { |
||
| 168 | $notify_message .= sprintf( |
||
| 169 | /* translators: Placeholder is the edit URL */ |
||
| 170 | __( 'Delete it: %s' ), |
||
| 171 | $moderate_on_wpcom |
||
| 172 | ? str_replace( '__action__', 'delete', $base_wpcom_edit_comment_url ) |
||
| 173 | : admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) |
||
| 174 | ) . "\r\n"; |
||
| 175 | } |
||
| 176 | $notify_message .= sprintf( |
||
| 177 | /* translators: Placeholder is the edit URL */ |
||
| 178 | __( 'Spam it: %s' ), |
||
| 179 | $moderate_on_wpcom |
||
| 180 | ? str_replace( '__action__', 'spam', $base_wpcom_edit_comment_url ) |
||
| 181 | : admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) |
||
| 182 | ) . "\r\n"; |
||
| 183 | } |
||
| 184 | |||
| 185 | $wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', strtolower( $_SERVER['SERVER_NAME'] ) ); |
||
| 186 | |||
| 187 | if ( '' == $comment->comment_author ) { |
||
| 188 | $from = "From: \"$blogname\" <$wp_email>"; |
||
| 189 | if ( '' != $comment->comment_author_email ) { |
||
| 190 | $reply_to = "Reply-To: $comment->comment_author_email"; |
||
| 191 | } |
||
| 192 | } else { |
||
| 193 | $from = "From: \"$comment->comment_author\" <$wp_email>"; |
||
| 194 | if ( '' != $comment->comment_author_email ) { |
||
| 195 | $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>"; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | $message_headers = "$from\n" |
||
| 200 | . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n"; |
||
| 201 | |||
| 202 | if ( isset( $reply_to ) ) { |
||
| 203 | $message_headers .= $reply_to . "\n"; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 207 | $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID ); |
||
| 208 | |||
| 209 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 210 | $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID ); |
||
| 211 | |||
| 212 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 213 | $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID ); |
||
| 214 | |||
| 215 | foreach ( $emails as $email ) { |
||
| 216 | @wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); |
||
| 217 | } |
||
| 218 | |||
| 219 | if ( $switched_locale ) { |
||
| 220 | restore_previous_locale(); |
||
| 221 | } |
||
| 222 | |||
| 223 | return true; |
||
| 224 | } |
||
| 225 | 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.