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