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 | * Notify an author (and/or others) of a comment/trackback/pingback on a post. |
||
21 | * |
||
22 | * @since 5.8.0 |
||
23 | * @since 9.3.0 Switched from pluggable function to filter callback |
||
24 | * |
||
25 | * @param array $emails List of recipients. |
||
26 | * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. |
||
27 | * @return array Empty array to shortcircuit wp_notify_postauthor execution. $emails if we want to disable the filter. |
||
28 | */ |
||
29 | function jetpack_notify_postauthor( $emails, $comment_id ) { |
||
30 | // Don't do anything if Jetpack isn't active. |
||
31 | if ( ! Jetpack::is_active() || empty( $emails ) ) { |
||
32 | return $emails; |
||
33 | } |
||
34 | |||
35 | // Original function modified: Code before the comment_notification_recipients filter removed. |
||
36 | |||
37 | $comment = get_comment( $comment_id ); |
||
38 | |||
39 | $post = get_post( $comment->comment_post_ID ); |
||
40 | $author = get_userdata( $post->post_author ); |
||
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 && get_current_user_id() == $post->post_author ) { |
||
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 array(); // Original function modified. Return empty array instead of false. |
||
66 | } else { |
||
67 | $emails = array_flip( $emails ); |
||
68 | } |
||
69 | |||
70 | $switched_locale = switch_to_locale( get_locale() ); |
||
71 | |||
72 | $comment_author_domain = ''; |
||
73 | if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) { |
||
74 | $comment_author_domain = gethostbyaddr( $comment->comment_author_IP ); |
||
75 | } |
||
76 | |||
77 | // The blogname option is escaped with esc_html on the way into the database in sanitize_option |
||
78 | // we want to reverse this for the plain text arena of emails. |
||
79 | $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
||
80 | $comment_content = wp_specialchars_decode( $comment->comment_content ); |
||
81 | |||
82 | // Original function modified. |
||
83 | $moderate_on_wpcom = ! in_array( false, array_map( 'jetpack_notify_is_user_connected_by_email', $emails ) ); |
||
84 | |||
85 | switch ( $comment->comment_type ) { |
||
86 | View Code Duplication | case 'trackback': |
|
87 | /* translators: 1: Post title */ |
||
88 | $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n"; |
||
89 | /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */ |
||
90 | $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
91 | /* translators: %s: Site URL */ |
||
92 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
93 | /* translators: %s: Comment Content */ |
||
94 | $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
||
95 | $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n"; |
||
96 | /* translators: 1: blog name, 2: post title */ |
||
97 | $subject = sprintf( __( '[%1$s] Trackback: "%2$s"' ), $blogname, $post->post_title ); |
||
98 | break; |
||
99 | View Code Duplication | case 'pingback': |
|
100 | /* translators: 1: Post title */ |
||
101 | $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n"; |
||
102 | /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */ |
||
103 | $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
104 | /* translators: %s: Site URL */ |
||
105 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
106 | /* translators: %s: Comment Content */ |
||
107 | $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
||
108 | $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n"; |
||
109 | /* translators: 1: blog name, 2: post title */ |
||
110 | $subject = sprintf( __( '[%1$s] Pingback: "%2$s"' ), $blogname, $post->post_title ); |
||
111 | break; |
||
112 | default: // Comments. |
||
113 | /* translators: 1: Post title */ |
||
114 | $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n"; |
||
115 | /* translators: 1: comment author, 2: comment author's IP address, 3: comment author's hostname */ |
||
116 | $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
||
117 | /* translators: %s: Email address */ |
||
118 | $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; |
||
119 | /* translators: %s: Site URL */ |
||
120 | $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
||
121 | /* translators: %s: Comment Content */ |
||
122 | $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
||
123 | $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n"; |
||
124 | /* translators: 1: blog name, 2: post title */ |
||
125 | $subject = sprintf( __( '[%1$s] Comment: "%2$s"' ), $blogname, $post->post_title ); |
||
126 | break; |
||
127 | } |
||
128 | |||
129 | // Original function modified: Consider $moderate_on_wpcom when building $notify_message. |
||
130 | $notify_message .= $moderate_on_wpcom |
||
131 | ? Redirect::get_url( |
||
132 | 'calypso-comments-all', |
||
133 | array( |
||
134 | 'path' => $comment->comment_post_ID, |
||
135 | ) |
||
136 | ) . "/\r\n\r\n" |
||
137 | : get_permalink( $comment->comment_post_ID ) . "#comments\r\n\r\n"; |
||
138 | |||
139 | /* translators: %s: URL */ |
||
140 | $notify_message .= sprintf( __( 'Permalink: %s' ), get_comment_link( $comment ) ) . "\r\n"; |
||
141 | |||
142 | $base_wpcom_edit_comment_url = Redirect::get_url( |
||
143 | 'calypso-edit-comment', |
||
144 | array( |
||
145 | 'path' => $comment_id, |
||
146 | 'query' => 'action=__action__', // __action__ will be replaced by the actual action. |
||
147 | ) |
||
148 | ); |
||
149 | |||
150 | // Original function modified: Consider $moderate_on_wpcom when building $notify_message. |
||
151 | if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) { |
||
152 | View Code Duplication | if ( EMPTY_TRASH_DAYS ) { |
|
153 | $notify_message .= sprintf( |
||
154 | /* translators: Placeholder is the edit URL */ |
||
155 | __( 'Trash it: %s' ), |
||
156 | $moderate_on_wpcom |
||
157 | ? str_replace( '__action__', 'trash', $base_wpcom_edit_comment_url ) |
||
158 | : admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) |
||
159 | ) . "\r\n"; |
||
160 | } else { |
||
161 | $notify_message .= sprintf( |
||
162 | /* translators: Placeholder is the edit URL */ |
||
163 | __( 'Delete it: %s' ), |
||
164 | $moderate_on_wpcom |
||
165 | ? str_replace( '__action__', 'delete', $base_wpcom_edit_comment_url ) |
||
166 | : admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) |
||
167 | ) . "\r\n"; |
||
168 | } |
||
169 | $notify_message .= sprintf( |
||
170 | /* translators: Placeholder is the edit URL */ |
||
171 | __( 'Spam it: %s' ), |
||
172 | $moderate_on_wpcom |
||
173 | ? str_replace( '__action__', 'spam', $base_wpcom_edit_comment_url ) |
||
174 | : admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) |
||
175 | ) . "\r\n"; |
||
176 | } |
||
177 | |||
178 | $wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', strtolower( $_SERVER['SERVER_NAME'] ) ); |
||
179 | |||
180 | if ( '' == $comment->comment_author ) { |
||
181 | $from = "From: \"$blogname\" <$wp_email>"; |
||
182 | if ( '' != $comment->comment_author_email ) { |
||
183 | $reply_to = "Reply-To: $comment->comment_author_email"; |
||
184 | } |
||
185 | } else { |
||
186 | $from = "From: \"$comment->comment_author\" <$wp_email>"; |
||
187 | if ( '' != $comment->comment_author_email ) { |
||
188 | $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>"; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | $message_headers = "$from\n" |
||
193 | . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n"; |
||
194 | |||
195 | if ( isset( $reply_to ) ) { |
||
196 | $message_headers .= $reply_to . "\n"; |
||
197 | } |
||
198 | |||
199 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
200 | $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID ); |
||
201 | |||
202 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
203 | $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID ); |
||
204 | |||
205 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
206 | $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID ); |
||
207 | |||
208 | foreach ( $emails as $email ) { |
||
209 | wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); |
||
210 | } |
||
211 | |||
212 | if ( $switched_locale ) { |
||
213 | restore_previous_locale(); |
||
214 | } |
||
215 | |||
216 | return array(); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Short circuits the {@see `wp_notify_moderator`} function via the `notify_moderator` filter. |
||
221 | * |
||
222 | * Notifies the moderator of the site about a new comment that is awaiting approval. |
||
223 | * |
||
224 | * @since 5.8.0 |
||
225 | * @since 9.2.0 Switched from pluggable function to filter callback |
||
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.