Changeset 11842
- Timestamp:
- 05/13/2022 04:45:59 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-profiles-wp-activity-notifier/wporg-profiles-wp-activity-notifier.php
r10663 r11842 8 8 9 9 class WPOrg_WP_Activity_Notifier { 10 11 10 private $activity_handler_url = 'https://profiles.wordpress.org/wp-admin/admin-ajax.php'; 12 11 … … 19 18 * Returns always the same instance of this plugin. 20 19 * 21 * @return Plugin20 * @return WPOrg_WP_Activity_Notifier 22 21 */ 23 22 public static function get_instance() { … … 25 24 self::$instance = new self(); 26 25 } 26 27 27 return self::$instance; 28 28 } … … 62 62 * 63 63 * @param WP_Post $post The post 64 * 64 65 * @return boolean True == the post can be notified about. 65 66 */ 66 67 public function is_post_notifiable( $post ) { 67 68 68 // Sanity check the argument is a post 69 if ( ! $post || ! is_a( $post, 'WP_Post' ) ) 69 if ( ! $post || ! is_a( $post, 'WP_Post' ) ) { 70 70 return false; 71 } 71 72 72 73 // Don't notify if the site is for subscribers only 73 if ( class_exists( 'Subscribers_Only' ) ) 74 if ( class_exists( 'Subscribers_Only' ) ) { 74 75 $notifiable = false; 76 } 75 77 76 78 // Don't notify if not of 'post' post_type 77 elseif ( 'post' != $post->post_type ) 79 elseif ( 'post' != $post->post_type ) { 78 80 $notifiable = false; 81 } 79 82 80 83 // Don't notify if not publicly published 81 elseif ( 'publish' != $post->post_status ) 84 elseif ( 'publish' != $post->post_status ) { 82 85 $notifiable = false; 86 } 83 87 84 88 // Don't notify if password is required 85 elseif ( ! empty( $post->post_password ) ) 89 elseif ( ! empty( $post->post_password ) ) { 86 90 $notifiable = false; 91 } 87 92 88 93 // At this point it is permitted to notify about the post 89 else 94 else { 90 95 $notifiable = true; 96 } 91 97 92 98 // Return filtered value to allow overriding or extending checks 93 99 return apply_filters( 'wporg_profiles_wp_activity-is_post_notifiable', $notifiable, $post ); 94 95 100 } 96 101 … … 98 103 * Only send notification for post getting published. 99 104 * 100 * @param string $new_status The new status for the post101 * @param string $old_status The old status for the post105 * @param string $new_status The new status for the post 106 * @param string $old_status The old status for the post 102 107 * @param WP_Post $post The post 103 108 */ 104 109 public function maybe_notify_new_published_post( $new_status, $old_status, $post ) { 105 106 110 // Only proceed if the post is transitioning to the publish status 107 if ( 'publish' != $new_status ) 108 return; 111 if ( 'publish' != $new_status ) { 112 return; 113 } 109 114 110 115 // Only proceed if the post is actually changing status 111 if ( $old_status == $new_status ) 112 return; 116 if ( $old_status == $new_status ) { 117 return; 118 } 113 119 114 120 // Only proceed if permitted to notify about the post 115 if ( ! $this->is_post_notifiable( $post ) ) 116 return; 121 if ( ! $this->is_post_notifiable( $post ) ) { 122 return; 123 } 117 124 118 125 // Send notification for the post … … 149 156 'content' => $content, 150 157 'url' => get_permalink( $post->ID ), 151 ) 158 ), 152 159 ); 153 160 … … 158 165 * Handler for comment creation. 159 166 * 160 * @param int $id Comment ID 161 * @param object $comment Comment 162 * @return void 163 */ 167 * @param int $id Comment ID 168 * @param WP_Comment $comment Comment 169 */ 164 170 function insert_comment( $id, $comment ) { 165 if ( 1 == $comment->comment_approved ) 171 if ( 1 == $comment->comment_approved ) { 166 172 $this->maybe_notify_new_approved_comment( 'approved', '', $comment ); 173 } 167 174 } 168 175 … … 170 177 * Only send notification for comment getting published on a public post. 171 178 * 172 * @param string $new_status The new status for the comment 173 * @param string $old_status The old status for the comment 179 * @param string $new_status The new status for the comment 180 * @param string $old_status The old status for the comment 181 * @param WP_Comment $comment The comment 182 */ 183 public function maybe_notify_new_approved_comment( $new_status, $old_status, $comment ) { 184 // Only proceed if the comment is transitioning to the approved status 185 if ( 'approved' != $new_status ) { 186 return; 187 } 188 189 $post = get_post( $comment->comment_post_ID ); 190 191 // Only proceed if permitted to notify about the post 192 if ( ! $this->is_post_notifiable( $post ) ) { 193 return; 194 } 195 196 // Only proceed if there are no objections to the comment notification 197 if ( apply_filters( 'wporg_profiles_wp_activity-is_comment_notifiable', true, $comment, $post ) ) { 198 $this->notify_new_approved_comment( $comment, $post ); 199 } 200 } 201 202 /** 203 * Sends activity notification for new comment. 204 * 174 205 * @param WP_Comment $comment The comment 175 */ 176 public function maybe_notify_new_approved_comment( $new_status, $old_status, $comment ) { 177 178 // Only proceed if the comment is transitioning to the approved status 179 if ( 'approved' != $new_status ) 180 return; 181 182 $post = get_post( $comment->comment_post_ID ); 183 184 // Only proceed if permitted to notify about the post 185 if ( ! $this->is_post_notifiable( $post ) ) 186 return; 187 188 // Only proceed if there are no objections to the comment notification 189 if ( apply_filters( 'wporg_profiles_wp_activity-is_comment_notifiable', true, $comment, $post ) ) 190 $this->notify_new_approved_comment( $comment, $post ); 191 } 192 193 /** 194 * Sends activity notification for new comment. 195 * 196 * @param WP_Comment $comment The comment 197 * @param WP_Post $post The comment's post 206 * @param WP_Post $post The comment's post 198 207 */ 199 208 private function notify_new_approved_comment( $comment, $post ) { … … 203 212 } 204 213 205 if ( ! $comment->user_id ) 206 return; 207 208 if ( ! $user = get_user_by( 'id', $comment->user_id ) ) 209 return; 214 if ( ! $comment->user_id ) { 215 return; 216 } 217 218 if ( ! $user = get_user_by( 'id', $comment->user_id ) ) { 219 return; 220 } 210 221 211 222 $args = array( … … 220 231 'blog_url' => site_url(), 221 232 'url' => get_comment_link( $comment ), 222 ) 233 ), 223 234 ); 224 235 … … 235 246 */ 236 247 private function _notify_forum_topic_payload( $activity, $topic_id ) { 237 238 248 // Don't notify if importing. 239 249 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { … … 258 268 $url = bbp_get_topic_permalink( $topic_id ); 259 269 // Remove moderator flags 260 $url = remove_query_arg( [ 'view' ], $url );270 $url = remove_query_arg( array( 'view' ), $url ); 261 271 262 272 $args = array( … … 274 284 'site' => get_bloginfo( 'name' ), 275 285 'site_url' => site_url(), 276 ) 286 ), 277 287 ); 278 288 … … 307 317 */ 308 318 private function _notify_forum_reply_payload( $activity, $reply_id ) { 309 310 319 // Don't notify if importing. 311 320 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { … … 330 339 $url = bbp_get_reply_url( $reply_id ); 331 340 // Remove moderator flags 332 $url = remove_query_arg( [ 'view' ], $url );341 $url = remove_query_arg( array( 'view' ), $url ); 333 342 334 343 $args = array( … … 346 355 'site' => get_bloginfo( 'name' ), 347 356 'site_url' => site_url(), 348 ) 357 ), 349 358 ); 350 359 351 360 wp_remote_post( $this->activity_handler_url, $args ); 352 353 361 } 354 362 … … 381 389 * @param int $reply_id Optional. The reply id. 382 390 * @param int $words Optional. The number of words for the excerpt. Default 15. 391 * 383 392 * @return string 384 393 */ … … 401 410 * @param int $length Optional. The number of words or characters to try down to. Default 15. 402 411 * @param string $trim_style Optional. The manner in which the text should be trimmed. Either 'chars' or 'words'. Default 'words'. 412 * 403 413 * @return string 404 414 */ … … 419 429 420 430 // If trimming by chars, behave like a more multibyte-aware 421 // bbp_get_reply_excerp().431 // /* bbp_get_reply_excerpt */(). 422 432 if ( 'chars' === $trim_style ) { 423 433 // Multibyte support … … 437 447 } 438 448 } 449 439 450 // Else trim by words. 440 451 else { … … 444 455 return $text; 445 456 } 446 447 457 } 448 458
Note: See TracChangeset
for help on using the changeset viewer.