| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* WooCommerce Extension |
| 5 |
* |
| 6 |
* @package NotificationX\Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace NotificationX\Extensions\WooCommerce; |
| 10 |
|
| 11 |
use NotificationX\Admin\Settings; |
| 12 |
use NotificationX\Core\Database; |
| 13 |
use NotificationX\Core\Helper; |
| 14 |
use NotificationX\Core\Rules; |
| 15 |
use NotificationX\GetInstance; |
| 16 |
use NotificationX\Extensions\Extension; |
| 17 |
use NotificationX\Extensions\GlobalFields; |
| 18 |
use NotificationX\Types\Conversions; |
| 19 |
use NotificationX\Types\CustomNotification; |
| 20 |
use NotificationX\Types\DownloadStats; |
| 21 |
|
| 22 |
/** |
| 23 |
* WooCommerce Extension Class |
| 24 |
* @method static WooReviews get_instance($args = null) |
| 25 |
*/ |
| 26 |
class WooReviews extends Extension { |
| 27 |
/** |
| 28 |
* Instance of WooReviews |
| 29 |
* |
| 30 |
* @var WooReviews |
| 31 |
*/ |
| 32 |
use GetInstance; |
| 33 |
use Woo; |
| 34 |
|
| 35 |
public $priority = 10; |
| 36 |
public $id = 'woo_reviews'; |
| 37 |
public $img = NOTIFICATIONX_ADMIN_URL . 'images/extensions/sources/woocommerce.png'; |
| 38 |
public $doc_link = 'https://notificationx.com/docs/woocommerce-sales-notifications/'; |
| 39 |
public $types = 'reviews'; |
| 40 |
public $module = 'modules_woocommerce'; |
| 41 |
public $module_priority = 3; |
| 42 |
public $class = '\WooCommerce'; |
| 43 |
public $default_theme = 'woo_reviews_total-rated'; |
| 44 |
public $exclude_custom_themes = true; |
| 45 |
|
| 46 |
/** |
| 47 |
* Initially Invoked when initialized. |
| 48 |
*/ |
| 49 |
public function __construct() { |
| 50 |
parent::__construct(); |
| 51 |
} |
| 52 |
|
| 53 |
public function init_extension() |
| 54 |
{ |
| 55 |
$this->title = $this->title ? $this->title : __('WooCommerce', 'notificationx'); |
| 56 |
$this->module_title = $this->module_title ? $this->module_title : __('WooCommerce', 'notificationx'); |
| 57 |
$this->themes = [ |
| 58 |
'total-rated' => [ |
| 59 |
'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/wporg/total-rated.png', |
| 60 |
'image_shape' => 'square', |
| 61 |
'template' => [ |
| 62 |
'first_param' => 'tag_rated', |
| 63 |
'custom_first_param' => __('Someone', 'notificationx'), |
| 64 |
'second_param' => __('people rated', 'notificationx'), |
| 65 |
'third_param' => 'tag_product_title', |
| 66 |
'fourth_param' => 'tag_rating', |
| 67 |
'custom_fourth_param' => __('Some time ago', 'notificationx'), |
| 68 |
] |
| 69 |
], |
| 70 |
'reviewed' => [ |
| 71 |
'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/wporg/reviewed.png', |
| 72 |
'image_shape' => 'circle', |
| 73 |
'template' => [ |
| 74 |
'first_param' => 'tag_username', |
| 75 |
'custom_first_param' => __('Someone', 'notificationx'), |
| 76 |
'second_param' => __('just reviewed', 'notificationx'), |
| 77 |
'third_param' => 'tag_product_title', |
| 78 |
'fourth_param' => 'tag_rating', |
| 79 |
'custom_fourth_param' => __('Some time ago', 'notificationx'), |
| 80 |
] |
| 81 |
], |
| 82 |
'review_saying' => [ |
| 83 |
'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/wporg/saying-review.png', |
| 84 |
'image_shape' => 'circle', |
| 85 |
'template' => [ |
| 86 |
'first_param' => 'tag_username', |
| 87 |
'custom_first_param' => __('Someone', 'notificationx'), |
| 88 |
'second_param' => __('saying', 'notificationx'), |
| 89 |
'third_param' => 'tag_title', |
| 90 |
'custom_third_param' => __('Excellent', 'notificationx'), |
| 91 |
'review_fourth_param' => __('about', 'notificationx'), |
| 92 |
'fifth_param' => 'tag_product_title', |
| 93 |
'sixth_param' => 'tag_custom', |
| 94 |
'custom_sixth_param' => __('Try it now', 'notificationx'), |
| 95 |
] |
| 96 |
], |
| 97 |
'review-comment' => [ |
| 98 |
'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/wporg/review-with-comment.jpg', |
| 99 |
'image_shape' => 'rounded', |
| 100 |
'template' => [ |
| 101 |
'first_param' => 'tag_username', |
| 102 |
'custom_first_param' => __('Someone', 'notificationx'), |
| 103 |
'second_param' => __('just reviewed', 'notificationx'), |
| 104 |
'third_param' => 'tag_plugin_review', |
| 105 |
'fourth_param' => 'tag_rating', |
| 106 |
'custom_fourth_param' => __('Some time ago', 'notificationx'), |
| 107 |
] |
| 108 |
], |
| 109 |
'review-comment-2' => [ |
| 110 |
'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/wporg/review-with-comment-2.jpg', |
| 111 |
'template' => [ |
| 112 |
'first_param' => 'tag_username', |
| 113 |
'custom_first_param' => __('Someone', 'notificationx'), |
| 114 |
'second_param' => __('just reviewed', 'notificationx'), |
| 115 |
'third_param' => 'tag_plugin_review', |
| 116 |
'fourth_param' => 'tag_rating', |
| 117 |
'custom_fourth_param' => __('Some time ago', 'notificationx'), |
| 118 |
] |
| 119 |
], |
| 120 |
'review-comment-3' => [ |
| 121 |
'source' => NOTIFICATIONX_ADMIN_URL . 'images/extensions/themes/wporg/review-with-comment-3.jpg', |
| 122 |
'image_shape' => 'circle', |
| 123 |
'template' => [ |
| 124 |
'first_param' => 'tag_username', |
| 125 |
'custom_first_param' => __('Someone', 'notificationx'), |
| 126 |
'second_param' => __('just reviewed', 'notificationx'), |
| 127 |
'third_param' => 'tag_plugin_review', |
| 128 |
'fourth_param' => 'tag_time', |
| 129 |
'custom_fourth_param' => __('Some time ago', 'notificationx'), |
| 130 |
] |
| 131 |
], |
| 132 |
]; |
| 133 |
$this->templates = [ |
| 134 |
'wp_reviews_template_new' => [ |
| 135 |
'first_param' => [ |
| 136 |
'tag_username' => __('Username', 'notificationx'), |
| 137 |
'tag_rated' => __('Rated', 'notificationx'), |
| 138 |
], |
| 139 |
'third_param' => [ |
| 140 |
'tag_product_title' => __('Product Title', 'notificationx'), |
| 141 |
'tag_plugin_review' => __('Review', 'notificationx'), |
| 142 |
'tag_anonymous_title' => __('Anonymous Title', 'notificationx'), |
| 143 |
], |
| 144 |
'fourth_param' => [ |
| 145 |
'tag_rating' => __('Rating', 'notificationx'), |
| 146 |
'tag_time' => __('Definite Time', 'notificationx'), |
| 147 |
], |
| 148 |
'_themes' => [ |
| 149 |
'woo_reviews_total-rated', |
| 150 |
'woo_reviews_reviewed', |
| 151 |
'woo_reviews_review-comment', |
| 152 |
'woo_reviews_review-comment-2', |
| 153 |
'woo_reviews_review-comment-3', |
| 154 |
], |
| 155 |
], |
| 156 |
'review_saying_template_new' => [ |
| 157 |
'first_param' => [ |
| 158 |
'tag_username' => __('Username', 'notificationx'), |
| 159 |
], |
| 160 |
'third_param' => [ |
| 161 |
'tag_title' => __('Review Title', 'notificationx'), |
| 162 |
'tag_anonymous_title' => __('Anonymous Title', 'notificationx'), |
| 163 |
], |
| 164 |
'fifth_param' => [ |
| 165 |
'tag_product_title' => __('Product Title', 'notificationx'), |
| 166 |
], |
| 167 |
'sixth_param' => [ |
| 168 |
// @todo maybe add some predefined texts. |
| 169 |
], |
| 170 |
'_themes' => [ |
| 171 |
'woo_reviews_review_saying', |
| 172 |
], |
| 173 |
], |
| 174 |
]; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
public function init() { |
| 179 |
parent::init(); |
| 180 |
add_filter("nx_filtered_data_{$this->id}", array($this, 'rated_woo_review'), 10, 2); |
| 181 |
add_action('comment_post', array($this, 'post_comment'), 10, 2); |
| 182 |
add_action('trash_comment', array($this, 'delete_comment'), 10); |
| 183 |
add_action('deleted_comment', array($this, 'delete_comment'), 10); |
| 184 |
add_action('transition_comment_status', array($this, 'transition_comment_status'), 10, 3); |
| 185 |
} |
| 186 |
|
| 187 |
public function init_fields(){ |
| 188 |
parent::init_fields(); |
| 189 |
$this->_init_fields(); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* This functions is hooked |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
public function admin_actions() { |
| 198 |
parent::admin_actions(); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
public function public_actions() { |
| 207 |
parent::public_actions(); |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
// @todo |
| 212 |
public function fallback_data($data, $saved_data, $settings) { |
| 213 |
$data['username'] = __('someone', 'notificationx'); |
| 214 |
$data['plugin_name_text'] = __('try it out', 'notificationx'); |
| 215 |
$data['anonymous_title'] = __('Anonymous', 'notificationx'); |
| 216 |
$data['plugin_review'] = __('Some review content', 'notificationx'); |
| 217 |
$data['rating'] = 5; |
| 218 |
|
| 219 |
if ( empty( $entry['product_title'] ) && ! empty( $saved_data['title'] ) ) { |
| 220 |
$data['product_title'] = $saved_data['title']; |
| 221 |
} |
| 222 |
return $data; |
| 223 |
} |
| 224 |
|
| 225 |
public function source_error_message($messages) { |
| 226 |
if (!$this->class_exists()) { |
| 227 |
$url = admin_url('plugin-install.php?s=woocommerce&tab=search&type=term'); |
| 228 |
$messages[$this->id] = [ |
| 229 |
'message' => sprintf( |
| 230 |
'%s <a href="%s" target="_blank">%s</a> %s', |
| 231 |
__('You have to install', 'notificationx'), |
| 232 |
$url, |
| 233 |
__('WooCommerce', 'notificationx'), |
| 234 |
__('plugin first.', 'notificationx') |
| 235 |
), |
| 236 |
'html' => true, |
| 237 |
'type' => 'error', |
| 238 |
'rules' => Rules::is('source', $this->id), |
| 239 |
]; |
| 240 |
} |
| 241 |
return $messages; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Image Action |
| 246 |
*/ |
| 247 |
public function image_action() { |
| 248 |
} |
| 249 |
|
| 250 |
// @todo |
| 251 |
public function notification_image($image_data, $data, $settings) { |
| 252 |
if (!$settings['show_default_image'] && $settings['show_notification_image'] === 'featured_image') { |
| 253 |
if (!empty($data['product_id']) && has_post_thumbnail($data['product_id'])) { |
| 254 |
$image_size = (string) Settings::get_instance()->get('settings.notification_image_size', '100_100'); |
| 255 |
$image_size = explode('_', $image_size); |
| 256 |
$width = 100; |
| 257 |
$height = 100; |
| 258 |
if( !empty( $image_size[0] ) && !empty( $image_size[1] ) ) { |
| 259 |
$width = !empty($image_size[0]) ? intval($image_size[0]) : 100; |
| 260 |
$height = !empty($image_size[1]) ? intval($image_size[1]) : 100; |
| 261 |
} |
| 262 |
$product_image = wp_get_attachment_image_src( |
| 263 |
get_post_thumbnail_id($data['product_id']), |
| 264 |
[$width, $height], |
| 265 |
false |
| 266 |
); |
| 267 |
$image_data['url'] = is_array($product_image) ? $product_image[0] : ''; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
$alt_title = isset($data['plugin_name']) ? $data['plugin_name'] : ''; |
| 272 |
$alt_title = empty($alt_title) && isset($data['username']) ? $data['username'] : $alt_title; |
| 273 |
$image_data['alt'] = $alt_title; |
| 274 |
return $image_data; |
| 275 |
} |
| 276 |
|
| 277 |
// @todo convert to nx_frontend_get_entries |
| 278 |
public function rated_woo_review($entries, $settings) { |
| 279 |
if ($settings['notification-template']['first_param'] !== 'tag_rated') { |
| 280 |
return $entries; |
| 281 |
} |
| 282 |
|
| 283 |
$from = isset($settings['display_from']) ? intval($settings['display_from']) : 0; |
| 284 |
$needed = isset($settings['display_last']) ? intval($settings['display_last']) : 0; |
| 285 |
|
| 286 |
$ratings = []; |
| 287 |
foreach ($entries as $key => $item) { |
| 288 |
$rating = $item['rating']; |
| 289 |
if ($rating === '5') { |
| 290 |
if (isset($ratings[$item['product_id']])) { |
| 291 |
$ratings[$item['product_id']] = [ |
| 292 |
'comment_ID' => $item['id'], |
| 293 |
'rated' => ++$ratings[$item['product_id']]['rated'], |
| 294 |
'image_data' => $item['image_data'], |
| 295 |
]; |
| 296 |
} else { |
| 297 |
$ratings[$item['product_id']] = [ |
| 298 |
'comment_ID' => $item['id'], |
| 299 |
'rated' => 1, |
| 300 |
'image_data' => $item['image_data'], |
| 301 |
]; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
$entries = []; |
| 306 |
foreach ($ratings as $key => $item) { |
| 307 |
$data = $this->add($item['comment_ID']); |
| 308 |
$data['nx_id'] = $settings['nx_id']; |
| 309 |
$data['rated'] = $item['rated']; |
| 310 |
$data['image_data'] = $item['image_data']; |
| 311 |
|
| 312 |
$entries[] = $data; |
| 313 |
} |
| 314 |
|
| 315 |
return $entries; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* This function is responsible for making comment notifications ready if comments is approved. |
| 320 |
* |
| 321 |
* @param int $comment_ID |
| 322 |
* @param bool $comment_approved |
| 323 |
* @return void |
| 324 |
*/ |
| 325 |
public function post_comment($comment_ID, $comment_approved) { |
| 326 |
// @todo @mukul |
| 327 |
// if (count($this->notifications) === $this->cache_limit) { |
| 328 |
// $sorted_data = NotificationX_Helper::sorter($this->notifications, 'key'); |
| 329 |
// array_pop($sorted_data); |
| 330 |
// $this->notifications = $sorted_data; |
| 331 |
// } |
| 332 |
|
| 333 |
if (1 === $comment_approved) { |
| 334 |
$comment = $this->add($comment_ID); |
| 335 |
/** |
| 336 |
* Save the data to |
| 337 |
* notificationx_data ( options DB. ) |
| 338 |
*/ |
| 339 |
$this->update_notification([ |
| 340 |
'source' => $this->id, |
| 341 |
'entry_key' => $comment_ID, |
| 342 |
'data' => $comment, |
| 343 |
]); |
| 344 |
} |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* This function is responsible for transition comment status |
| 350 |
* from approved to unapproved or unapproved to approved |
| 351 |
* |
| 352 |
* @param string $new_status |
| 353 |
* @param string $old_status |
| 354 |
* @param WP_Comment $comment |
| 355 |
* @return void |
| 356 |
*/ |
| 357 |
public function transition_comment_status($new_status, $old_status, $comment) { |
| 358 |
if ('unapproved' === $new_status) { |
| 359 |
$this->delete_comment($comment->comment_ID, $comment); |
| 360 |
} |
| 361 |
if ('approved' === $new_status) { |
| 362 |
$this->delete_notification($comment->comment_ID); |
| 363 |
$this->post_comment($comment->comment_ID, 1); |
| 364 |
} |
| 365 |
} |
| 366 |
/** |
| 367 |
* If a comment delete, than the notifications data set has to be updated as well. |
| 368 |
* this function is responsible for doing this. |
| 369 |
* |
| 370 |
* @param int $comment_ID |
| 371 |
* @param WP_Comment $comment |
| 372 |
* @return void |
| 373 |
*/ |
| 374 |
public function delete_comment($comment_ID) { |
| 375 |
$this->delete_notification($comment_ID); |
| 376 |
} |
| 377 |
|
| 378 |
public function saved_post($post, $data, $nx_id) { |
| 379 |
$this->delete_notification(null, $nx_id); |
| 380 |
$this->get_notification_ready($data); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* This function is responsible for making the notification ready for first time we make the notification. |
| 385 |
* |
| 386 |
* @param string $type |
| 387 |
* @param array $data |
| 388 |
* @return void |
| 389 |
*/ |
| 390 |
public function get_notification_ready($data = array()) { |
| 391 |
if (!is_null($comments = $this->get_comments($data))) { |
| 392 |
$entries = []; |
| 393 |
foreach ($comments as $comment) { |
| 394 |
$entries[] = [ |
| 395 |
'nx_id' => $data['nx_id'], |
| 396 |
'source' => $this->id, |
| 397 |
'entry_key' => $comment['id'], |
| 398 |
'data' => $comment, |
| 399 |
]; |
| 400 |
} |
| 401 |
$this->update_notifications($entries); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* This function is responsible for getting the comments from wp_comments data table. |
| 407 |
* |
| 408 |
* @param array $data |
| 409 |
* @return array |
| 410 |
*/ |
| 411 |
public function get_comments($data) { |
| 412 |
if (empty($data)) return null; |
| 413 |
|
| 414 |
// $from = isset($data['display_from']) ? intval($data['display_from']) : 0; |
| 415 |
$from = date('Y-m-d H:i:s', Helper::generate_time_string($data)); |
| 416 |
$needed = isset($data['display_last']) ? intval($data['display_last']) : 0; |
| 417 |
|
| 418 |
$comments = get_comments([ |
| 419 |
'status' => 'approve', |
| 420 |
'number' => $needed, |
| 421 |
'post_type' => 'product', |
| 422 |
'date_query' => [ |
| 423 |
'after' => $from, |
| 424 |
'inclusive' => true, |
| 425 |
] |
| 426 |
]); |
| 427 |
|
| 428 |
if (empty($comments)) return null; |
| 429 |
$new_comments = []; |
| 430 |
foreach ($comments as $comment) { |
| 431 |
$new_comments[$comment->comment_ID] = $this->add($comment); |
| 432 |
} |
| 433 |
return $new_comments; |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* This function is responsible for making ready the comments data! |
| 438 |
* |
| 439 |
* @param int|WP_Comment $comment |
| 440 |
* @return void |
| 441 |
*/ |
| 442 |
public function add($comment) { |
| 443 |
$comment_data = []; |
| 444 |
|
| 445 |
if (!$comment instanceof \WP_Comment) { |
| 446 |
$comment_id = intval($comment); |
| 447 |
$comment = get_comment($comment_id, 'OBJECT'); |
| 448 |
} |
| 449 |
if ($comment->comment_type !== 'review') { |
| 450 |
return; |
| 451 |
} |
| 452 |
|
| 453 |
$comment_data['id'] = $comment->comment_ID; |
| 454 |
$comment_data['product_id'] = $comment->comment_post_ID; |
| 455 |
$comment_data['content'] = $comment->comment_content; |
| 456 |
$comment_data['link'] = get_comment_link($comment->comment_ID); |
| 457 |
$comment_data['title'] = get_the_title($comment->comment_post_ID); |
| 458 |
$comment_data['post_link'] = get_permalink($comment->comment_post_ID); |
| 459 |
$comment_data['timestamp'] = get_gmt_from_date($comment->comment_date); |
| 460 |
$comment_data['rating'] = get_comment_meta($comment->comment_ID, 'rating', true); |
| 461 |
|
| 462 |
// @todo move to somewhere. |
| 463 |
$comment_data['ip'] = $comment->comment_author_IP; |
| 464 |
|
| 465 |
if ($comment->user_id) { |
| 466 |
$comment_data['user_id'] = $comment->user_id; |
| 467 |
$user = get_userdata($comment->user_id); |
| 468 |
$comment_data['first_name'] = $user->first_name; |
| 469 |
$comment_data['last_name'] = $user->last_name; |
| 470 |
$comment_data['username'] = $user->display_name; |
| 471 |
$comment_data['name'] = $user->first_name . ' ' . mb_substr($user->last_name, 0, 1); |
| 472 |
$trimed = trim($comment_data['name']); |
| 473 |
if (empty($trimed)) { |
| 474 |
$comment_data['name'] = $user->user_nicename; |
| 475 |
} |
| 476 |
} else { |
| 477 |
$commenter_name = get_comment_author($comment->comment_ID); |
| 478 |
$comment_data['username'] = $commenter_name; |
| 479 |
$commenter_name = explode(' ', $commenter_name); |
| 480 |
if (isset($commenter_name[0])) { |
| 481 |
$comment_data['first_name'] = $commenter_name[0]; |
| 482 |
} |
| 483 |
$commenter_count = count($commenter_name); |
| 484 |
if (isset($commenter_name[$commenter_count - 1])) { |
| 485 |
$comment_data['last_name'] = $commenter_name[$commenter_count - 1]; |
| 486 |
} |
| 487 |
} |
| 488 |
$comment_data['email'] = get_comment_author_email($comment->comment_ID); |
| 489 |
return $comment_data; |
| 490 |
} |
| 491 |
|
| 492 |
public function doc() { |
| 493 |
return sprintf(__('<p>Make sure that you have <a target="_blank" href="%1$s">WooCommerce installed & activated</a> to use this campaign. For further assistance, check out our step by step <a target="_blank" href="%2$s">documentation</a>.</p> |
| 494 |
<p>🎦 Watch <a target="_blank" href="%3$s">video tutorial</a> to learn quickly</p> |
| 495 |
<p><strong>Recommended Blog:</strong></p> |
| 496 |
<p>🚀 How to <a target="_blank" href="%4$s">boost WooCommerce Sales</a> Using NotificationX</p>', 'notificationx'), |
| 497 |
'https://wordpress.org/plugins/woocommerce/', |
| 498 |
'https://notificationx.com/docs/woocommerce-product-reviews/', |
| 499 |
'https://www.youtube.com/watch?v=bHuaOs9JWvI', |
| 500 |
'https://wpdeveloper.com/ecommerce-sales-social-proof/' |
| 501 |
); |
| 502 |
} |
| 503 |
} |
| 504 |
|