| @@ -1,0 +1,291 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * WPComments Extension | |
| 5 | + * | |
| 6 | + * @package NotificationX\Extensions | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// @todo trim comment to length | |
| 10 | + | |
| 11 | +namespace NotificationX\Extensions\WordPress; | |
| 12 | + | |
| 13 | +use NotificationX\Core\Helper; | |
| 14 | +use NotificationX\GetInstance; | |
| 15 | +use NotificationX\Extensions\Extension; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * WPComments Extension | |
| 19 | + * @method static WPComments get_instance($args = null) | |
| 20 | + */ | |
| 21 | +class WPComments extends Extension { | |
| 22 | + /** | |
| 23 | + * Instance of WPComments | |
| 24 | + * | |
| 25 | + * @var WPComments | |
| 26 | + */ | |
| 27 | + use GetInstance; | |
| 28 | + use WordPress; | |
| 29 | + | |
| 30 | + public $priority = 5; | |
| 31 | + public $id = 'wp_comments'; | |
| 32 | + public $img = NOTIFICATIONX_ADMIN_URL . 'images/extensions/sources/wp-comments.png'; | |
| 33 | + public $doc_link = 'https://notificationx.com/docs-category/configurations/'; | |
| 34 | + public $types = 'comments'; | |
| 35 | + public $module = 'modules_wordpress'; | |
| 36 | + public $module_priority = 2; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Initially Invoked when initialized. | |
| 40 | + */ | |
| 41 | + public function __construct() { | |
| 42 | + parent::__construct(); | |
| 43 | + } | |
| 44 | + | |
| 45 | + public function init_extension() | |
| 46 | + { | |
| 47 | + $this->title = __('WP Comments', 'notificationx'); | |
| 48 | + $this->module_title = __('WordPress', 'notificationx'); | |
| 49 | + } | |
| 50 | + | |
| 51 | + | |
| 52 | + public function init() { | |
| 53 | + parent::init(); | |
| 54 | + add_action('comment_post', array($this, 'post_comment'), 10, 2); | |
| 55 | + add_action('trash_comment', array($this, 'delete_comment'), 10, 2); | |
| 56 | + add_action('deleted_comment', array($this, 'delete_comment'), 10, 2); | |
| 57 | + add_action('transition_comment_status', array($this, 'transition_comment_status'), 10, 3); | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * This functions is hooked | |
| 62 | + * | |
| 63 | + * @return void | |
| 64 | + */ | |
| 65 | + public function admin_actions() { | |
| 66 | + parent::admin_actions(); | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * This functions is hooked | |
| 71 | + * | |
| 72 | + * @hooked nx_public_action | |
| 73 | + * | |
| 74 | + * @return void | |
| 75 | + */ | |
| 76 | + public function public_actions() { | |
| 77 | + parent::public_actions(); | |
| 78 | + | |
| 79 | + add_filter("nx_filtered_entry_{$this->id}", array($this, 'conversion_data'), 10, 2); | |
| 80 | + } | |
| 81 | + | |
| 82 | + public function saved_post($post, $data, $nx_id) { | |
| 83 | + $this->delete_notification(null, $nx_id); | |
| 84 | + $this->get_notification_ready($data); | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * This function responsible for making ready the notifications for the first time | |
| 89 | + * we have made a notification. | |
| 90 | + * | |
| 91 | + * @param string $type | |
| 92 | + * @param array $data | |
| 93 | + * @return void | |
| 94 | + */ | |
| 95 | + public function get_notification_ready($data = array()) { | |
| 96 | + if (!is_null($comments = $this->get_comments($data))) { | |
| 97 | + $entries = []; | |
| 98 | + foreach ($comments as $comment) { | |
| 99 | + if ($comment) { | |
| 100 | + // $comment, $comment['id'], $data['nx_id'] | |
| 101 | + $entries[] = [ | |
| 102 | + 'nx_id' => $data['nx_id'], | |
| 103 | + 'source' => $this->id, | |
| 104 | + 'entry_key' => $comment['id'], | |
| 105 | + 'data' => $comment, | |
| 106 | + ]; | |
| 107 | + } | |
| 108 | + } | |
| 109 | + $this->update_notifications($entries); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + /** | |
| 113 | + * This function is responsible for getting the comments from wp_comments data table. | |
| 114 | + * | |
| 115 | + * @param array $data | |
| 116 | + * @return array | |
| 117 | + */ | |
| 118 | + public function get_comments($data) { | |
| 119 | + if (empty($data)) return null; | |
| 120 | + | |
| 121 | + global $wp_version; | |
| 122 | + | |
| 123 | + // $from = isset($data['display_from']) ? intval($data['display_from']) : 0; | |
| 124 | + $from = gmdate('Y-m-d H:i:s', Helper::generate_time_string($data)); | |
| 125 | + $needed = isset($data['display_last']) ? intval($data['display_last']) : 0; | |
| 126 | + | |
| 127 | + $args = [ | |
| 128 | + 'status' => 'approve', | |
| 129 | + 'number' => $needed, | |
| 130 | + 'date_query' => [ | |
| 131 | + 'after' => $from, | |
| 132 | + 'inclusive' => true, | |
| 133 | + ] | |
| 134 | + ]; | |
| 135 | + | |
| 136 | + if (version_compare($wp_version, '5.5', '==')) { | |
| 137 | + $args['type'] = 'comment'; | |
| 138 | + } | |
| 139 | + | |
| 140 | + $comments = get_comments($args); | |
| 141 | + | |
| 142 | + if (empty($comments)) return null; | |
| 143 | + $new_comments = []; | |
| 144 | + foreach ($comments as $comment) { | |
| 145 | + if ($_comment = $this->add($comment)) { | |
| 146 | + $new_comments[$comment->comment_ID] = $_comment; | |
| 147 | + } | |
| 148 | + } | |
| 149 | + return $new_comments; | |
| 150 | + } | |
| 151 | + /** | |
| 152 | + * This function is responsible for transition comment status | |
| 153 | + * from approved to unapproved or unapproved to approved | |
| 154 | + * | |
| 155 | + * @param string $new_status | |
| 156 | + * @param string $old_status | |
| 157 | + * @param WP_Comment $comment | |
| 158 | + * @return void | |
| 159 | + */ | |
| 160 | + public function transition_comment_status($new_status, $old_status, $comment) { | |
| 161 | + if ('unapproved' === $new_status) { | |
| 162 | + $this->delete_comment($comment->comment_ID, $comment); | |
| 163 | + } | |
| 164 | + if ('approved' === $new_status) { | |
| 165 | + $this->delete_notification($comment->comment_ID); | |
| 166 | + $this->post_comment($comment->comment_ID, 1); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + /** | |
| 170 | + * This function is responsible for making comment notifications ready if comments is approved. | |
| 171 | + * | |
| 172 | + * @param int $comment_ID | |
| 173 | + * @param bool $comment_approved | |
| 174 | + * @return void | |
| 175 | + */ | |
| 176 | + public function post_comment($comment_ID, $comment_approved) { | |
| 177 | + if (1 === $comment_approved) { | |
| 178 | + $comment = $this->add($comment_ID); | |
| 179 | + if (is_array($comment)) { | |
| 180 | + $this->update_notification([ | |
| 181 | + 'source' => $this->id, | |
| 182 | + 'entry_key' => $comment['id'], | |
| 183 | + 'data' => $comment, | |
| 184 | + ]); | |
| 185 | + } | |
| 186 | + } | |
| 187 | + return; | |
| 188 | + } | |
| 189 | + | |
| 190 | + /** | |
| 191 | + * This function is responsible for making ready the comments data! | |
| 192 | + * | |
| 193 | + * @param int|WP_Comment $comment | |
| 194 | + * @return void | |
| 195 | + */ | |
| 196 | + public function add($comment) { | |
| 197 | + global $wp_version; | |
| 198 | + $comment_data = []; | |
| 199 | + | |
| 200 | + if (!$comment instanceof \WP_Comment) { | |
| 201 | + $comment_id = intval($comment); | |
| 202 | + $comment = get_comment($comment_id, 'OBJECT'); | |
| 203 | + } | |
| 204 | + if ( | |
| 205 | + ($comment->comment_type !== '' && version_compare($wp_version, '5.5', '<')) || | |
| 206 | + ($comment->comment_type !== 'comment' && version_compare($wp_version, '5.5', '>=')) | |
| 207 | + ) { | |
| 208 | + return; | |
| 209 | + } | |
| 210 | + | |
| 211 | + $comment_data['id'] = $comment->comment_ID; | |
| 212 | + $comment_data['link'] = get_comment_link($comment->comment_ID); | |
| 213 | + $comment_data['post_title'] = get_the_title($comment->comment_post_ID); | |
| 214 | + $comment_data['post_comment'] = $comment->comment_content; | |
| 215 | + $comment_data['post_link'] = get_permalink($comment->comment_post_ID); | |
| 216 | + $comment_data['timestamp'] = get_gmt_from_date($comment->comment_date); | |
| 217 | + | |
| 218 | + $comment_data['ip'] = $comment->comment_author_IP; | |
| 219 | + | |
| 220 | + if ($comment->user_id) { | |
| 221 | + $comment_data['user_id'] = $comment->user_id; | |
| 222 | + $user = get_userdata($comment->user_id); | |
| 223 | + $comment_data['first_name'] = $user->first_name; | |
| 224 | + $comment_data['last_name'] = $user->last_name; | |
| 225 | + $comment_data['display_name'] = $user->display_name; | |
| 226 | + $comment_data['name'] = $user->first_name . ' ' . mb_substr($user->last_name, 0, 1); | |
| 227 | + $trimed = trim($comment_data['name']); | |
| 228 | + if (empty($trimed)) { | |
| 229 | + $comment_data['name'] = $user->user_nicename; | |
| 230 | + } | |
| 231 | + } else { | |
| 232 | + $commenter_name = get_comment_author($comment->comment_ID); | |
| 233 | + $comment_data['name'] = $commenter_name; | |
| 234 | + $commenter_name = explode(' ', $commenter_name); | |
| 235 | + if (isset($commenter_name[0])) { | |
| 236 | + $comment_data['first_name'] = $commenter_name[0]; | |
| 237 | + } | |
| 238 | + $commenter_count = count($commenter_name); | |
| 239 | + if (isset($commenter_name[$commenter_count - 1])) { | |
| 240 | + $comment_data['last_name'] = $commenter_name[$commenter_count - 1]; | |
| 241 | + } | |
| 242 | + } | |
| 243 | + $comment_data['email'] = get_comment_author_email($comment->comment_ID); | |
| 244 | + return $comment_data; | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * If a comment delete, than the notifications data set has to be updated as well. | |
| 249 | + * this function is responsible for doing this. | |
| 250 | + * | |
| 251 | + * @param int $comment_ID | |
| 252 | + * @param WP_Comment $comment | |
| 253 | + * @return void | |
| 254 | + */ | |
| 255 | + public function delete_comment($comment_ID, $comment) { | |
| 256 | + $this->delete_notification($comment_ID); | |
| 257 | + } | |
| 258 | + | |
| 259 | + // @todo | |
| 260 | + public function fallback_data($data, $saved_data, $settings) { | |
| 261 | + $data['name'] = __('Someone', 'notificationx'); | |
| 262 | + $data['first_name'] = __('Someone', 'notificationx'); | |
| 263 | + $data['last_name'] = __('Someone', 'notificationx'); | |
| 264 | + $data['display_name'] = __('Someone', 'notificationx'); | |
| 265 | + $data['anonymous_post'] = __('Anonymous Post', 'notificationx'); | |
| 266 | + $data['sometime'] = __('Some time ago', 'notificationx'); | |
| 267 | + $data['post_comment'] = __('Some comment', 'notificationx'); | |
| 268 | + return $data; | |
| 269 | + } | |
| 270 | + | |
| 271 | + // @todo Frontend | |
| 272 | + public function conversion_data($saved_data, $settings) { | |
| 273 | + if (!empty($saved_data['post_comment'])) { | |
| 274 | + $trim_length = 100; | |
| 275 | + if ($settings['themes'] == 'comments_theme-seven-free' || $settings['themes'] == 'comments_theme-eight-free') { | |
| 276 | + $trim_length = 80; | |
| 277 | + } | |
| 278 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 279 | + $nx_trimmed_length = apply_filters('nx_text_trim_length', $trim_length, $settings); | |
| 280 | + $comment = $saved_data['post_comment']; | |
| 281 | + if (strlen($comment) > $nx_trimmed_length) { | |
| 282 | + $comment = substr($comment, 0, $nx_trimmed_length) . '...'; | |
| 283 | + } | |
| 284 | + if ($settings['themes'] == 'comments_theme-seven-free') { // || $settings['theme'] == 'comments_theme-six-free' | |
| 285 | + $comment = '" ' . $comment . ' "'; | |
| 286 | + } | |
| 287 | + $saved_data['post_comment'] = $comment; | |
| 288 | + } | |
| 289 | + return $saved_data; | |
| 290 | + } | |
| 291 | +} | |