PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / trunk
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more vtrunk
2.0.2 trunk 0.2 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 2.0.0 2.0.1
texty / includes / Notifications / WP / Comment.php

Comment.php in Texty – SMS Notification for WordPress, WooCommerce, Dokan and more trunk, at includes/Notifications/WP/Comment.php

107 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Texty\Notifications\WP;
4
5 use Texty\Notifications\Notification;
6 use WP_Comment;
7
8 class Comment extends Notification {
9
10 /**
11 * @var int
12 */
13 private $comment_id;
14
15 /**
16 * Initialize
17 */
18 public function __construct() {
19 $this->title = __( 'New Comment', 'texty' );
20 $this->id = 'comment';
21 $this->default_recipients = [ 'administrator' ];
22 $this->default = <<<'EOD'
23 A new comment added on the post "{post_title}" by {author} ({email}).
24
25 View comment: {post_url}
26 EOD;
27 }
28
29 /**
30 * Set the user ID
31 *
32 * @param int $comment_id
33 *
34 * @return self
35 */
36 public function set_comment( $comment_id ) {
37 $this->comment_id = $comment_id;
38
39 return $this;
40 }
41
42 /**
43 * Return the message
44 *
45 * @return string
46 */
47 public function get_message() {
48 $message = parent::get_message_raw();
49
50 if ( ! $this->comment_id ) {
51 return $message;
52 }
53
54 $comment = get_comment( $this->comment_id );
55
56 // The comment can vanish between the `comment_post` hook and the send
57 // (deleted, trashed, or spam). Bail to the global-key pass instead of
58 // dereferencing null and rendering an empty-bodied message.
59 if ( ! $comment instanceof WP_Comment ) {
60 return $this->replace_global_keys( $message );
61 }
62
63 foreach ( $this->replacement_keys() as $search => $value ) {
64 // `post_url` has no comment property; everything else reads off the
65 // comment via WP_Comment's magic getter (e.g. `post_title` proxies
66 // to the post). Coerce to string so a missing value never reaches
67 // str_replace as null (deprecated since PHP 8.1).
68 $replacement = ( 'post_url' === $search )
69 ? (string) get_permalink( $comment->comment_post_ID )
70 : (string) ( $comment->$value ?? '' );
71
72 $message = str_replace( '{' . $search . '}', $replacement, $message );
73 }
74
75 $message = $this->replace_global_keys( $message );
76
77 return $message;
78 }
79
80 /**
81 * Return recipients
82 *
83 * @return array
84 */
85 public function get_recipients() {
86 return $this->get_numbers_by_roles();
87 }
88
89 /**
90 * Get replacement keys
91 *
92 * @return array
93 */
94 public function replacement_keys() {
95 return [
96 'author' => 'comment_author',
97 'email' => 'comment_author_email',
98 'author_url' => 'comment_author_url',
99 'comment' => 'comment_content',
100 'ip' => 'comment_author_ip',
101 'post_id' => 'comment_post_ID',
102 'post_title' => 'post_title',
103 'post_url' => '',
104 ];
105 }
106 }
107