VariationSwatches
1 week ago
Wishlist
1 week ago
AdminMessage.php
1 week ago
AjaxSearch.php
1 week ago
Backorder.php
1 week ago
CatalogMode.php
1 year ago
Compare.php
1 week ago
DefaultTemplates.php
2 years ago
FlashSalesCountdown.php
1 year ago
MenuCart.php
4 months ago
MobilePanel.php
1 week ago
MultiStep.php
1 year ago
Notifications.php
1 week ago
QuickView.php
1 week ago
RecentlyViewedProducts.php
1 year ago
RenameLabel.php
4 months ago
Shopify.php
1 year ago
SingleAjaxAddToCart.php
1 week ago
SizeChart.php
1 week ago
StickyAddToCart.php
1 week ago
Notifications.php
490 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Notifications |
| 5 | * |
| 6 | * @package ShopPress |
| 7 | */ |
| 8 | |
| 9 | namespace ShopPress\Modules; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | class Notifications { |
| 14 | /** |
| 15 | * Init. |
| 16 | * |
| 17 | * @since 1.2.0 |
| 18 | */ |
| 19 | public static function init() { |
| 20 | |
| 21 | if ( ! sp_get_module_settings( 'notifications', 'status' ) ) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | add_action( 'init', array( __CLASS__, 'notifications_post_type' ) ); |
| 26 | |
| 27 | add_filter( 'query_vars', array( __CLASS__, 'add_notifications_query_vars' ) ); |
| 28 | add_filter( 'woocommerce_account_menu_items', array( __CLASS__, 'add_notifications_links_to_my_account' ) ); |
| 29 | add_action( 'init', array( __CLASS__, 'add_notifications_endpoint' ) ); |
| 30 | add_action( 'woocommerce_account_notifications_endpoint', array( __CLASS__, 'display_notifications_endpoint' ) ); |
| 31 | |
| 32 | add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ), 99, 99 ); |
| 33 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_wp_media' ) ); |
| 34 | |
| 35 | add_action( 'woocommerce_new_order', array( __CLASS__, 'notification_new_order' ), 10, 1 ); |
| 36 | |
| 37 | add_action( 'woocommerce_order_status_cancelled', array( __CLASS__, 'notification_order_status' ), 10, 1 ); |
| 38 | add_action( 'woocommerce_order_status_refunded', array( __CLASS__, 'notification_order_status' ), 10, 1 ); |
| 39 | add_action( 'woocommerce_order_status_completed', array( __CLASS__, 'notification_order_status' ), 10, 1 ); |
| 40 | add_action( 'woocommerce_order_status_processing', array( __CLASS__, 'notification_order_status' ), 10, 1 ); |
| 41 | add_action( 'woocommerce_order_status_pending', array( __CLASS__, 'notification_order_status' ), 10, 1 ); |
| 42 | add_action( 'woocommerce_order_status_failed', array( __CLASS__, 'notification_order_status' ), 10, 1 ); |
| 43 | add_action( 'woocommerce_order_status_on-hold', array( __CLASS__, 'notification_order_status' ), 10, 1 ); |
| 44 | |
| 45 | add_action( 'shoppress_notification_order_status', array( __CLASS__, 'add_comment_notification_after_order_completed' ), 10, 4 ); |
| 46 | |
| 47 | // add_action( 'woocommerce_account_dashboard', array( __CLASS__, 'display_notifications_widget' ) ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Load WP Media |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public static function load_wp_media() { |
| 56 | wp_enqueue_media(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * enqueue scripts. |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | */ |
| 64 | public static function enqueue_scripts() { |
| 65 | |
| 66 | if ( is_account_page() ) { |
| 67 | |
| 68 | wp_enqueue_style( 'sp-my-account-notifications' ); |
| 69 | |
| 70 | if ( is_rtl() ) { |
| 71 | wp_enqueue_style( 'sp-my-account-notifications-rtl' ); |
| 72 | } |
| 73 | |
| 74 | wp_enqueue_script( 'sp-my-account-notifications' ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Notifications post type. |
| 80 | * |
| 81 | * @since 1.1.3 |
| 82 | */ |
| 83 | public static function notifications_post_type() { |
| 84 | |
| 85 | register_post_type( |
| 86 | 'shoppress_notif', |
| 87 | array( |
| 88 | 'labels' => array( |
| 89 | 'name' => __( 'Notifications', 'shop-press' ), |
| 90 | 'singular_name' => __( 'Notifications', 'shop-press' ), |
| 91 | ), |
| 92 | 'public' => false, |
| 93 | 'show_ui' => true, |
| 94 | 'show_in_menu' => false, |
| 95 | 'show_in_nav_menus' => false, |
| 96 | 'supports' => array( 'editor', 'title' ), |
| 97 | ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Is reviews enabled |
| 103 | * |
| 104 | * @since 1.1.3 |
| 105 | * |
| 106 | * @return boolean |
| 107 | */ |
| 108 | public static function is_reviews_enabled() { |
| 109 | |
| 110 | return 'yes' === get_option( 'woocommerce_enable_reviews' ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Is review rating enabled |
| 115 | * |
| 116 | * @since 1.1.3 |
| 117 | * |
| 118 | * @return boolean |
| 119 | */ |
| 120 | public static function is_review_rating_enabled() { |
| 121 | |
| 122 | return 'yes' === get_option( 'woocommerce_enable_review_rating' ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Return Notifications |
| 127 | * |
| 128 | * @param array $args |
| 129 | * |
| 130 | * @since 1.1.3 |
| 131 | * |
| 132 | * @return array |
| 133 | */ |
| 134 | public static function get_notifications( $args = array() ) { |
| 135 | |
| 136 | $datetime_format = get_option( 'date_format' ); |
| 137 | |
| 138 | $defaults = array( |
| 139 | 'post_type' => 'shoppress_notif', |
| 140 | 'posts_per_page' => 12, |
| 141 | 'paged' => 1, |
| 142 | 'for' => get_current_user_id(), |
| 143 | ); |
| 144 | |
| 145 | $args = wp_parse_args( $args, $defaults ); |
| 146 | |
| 147 | if ( isset( $args['for'] ) && $args['for'] && 'all' !== $args['for'] ) { |
| 148 | |
| 149 | $args['meta_query']['notification_for'] = array( |
| 150 | 'key' => '_shopperss_notification_for', |
| 151 | 'value' => $args['for'], |
| 152 | 'compare' => '=', |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | $notifications = array(); |
| 157 | $wp_notifications = new \WP_Query( $args ); |
| 158 | if ( $wp_notifications->have_posts() ) { |
| 159 | while ( $wp_notifications->have_posts() ) { |
| 160 | $wp_notifications->the_post(); |
| 161 | |
| 162 | $notification_id = get_the_ID(); |
| 163 | $notification_type = get_post_meta( $notification_id, '_shopperss_notification_type', true ); |
| 164 | |
| 165 | $notification = array( |
| 166 | 'id' => $notification_id, |
| 167 | 'title' => get_the_title(), |
| 168 | 'content' => get_the_content(), |
| 169 | 'type' => $notification_type, |
| 170 | 'for' => get_post_meta( $notification_id, '_shopperss_notification_for', true ), |
| 171 | 'date' => date_i18n( $datetime_format, strtotime( get_the_date() ) ), |
| 172 | 'link_url' => '', |
| 173 | 'link_text' => '', |
| 174 | ); |
| 175 | |
| 176 | switch ( $notification_type ) { |
| 177 | case 'order_created': |
| 178 | case 'order_status_changed': |
| 179 | $order_id = get_post_meta( $notification_id, '_shoppress_order_id', true ); |
| 180 | $notification['order_id'] = $order_id; |
| 181 | |
| 182 | $notification['link_url'] = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) ); |
| 183 | $notification['link_text'] = __( 'View order', 'shop-press' ); |
| 184 | |
| 185 | break; |
| 186 | case 'order_comment': |
| 187 | $order_id = get_post_meta( $notification_id, '_shoppress_order_id', true ); |
| 188 | $notification['order_id'] = $order_id; |
| 189 | |
| 190 | $notification['link_url'] = "#shppress-add-review-{$order_id}"; |
| 191 | $notification['link_text'] = __( 'Add review', 'shop-press' ); |
| 192 | |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | $notifications[ $notification_id ] = $notification; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | wp_reset_postdata(); |
| 201 | |
| 202 | $notifications = array( |
| 203 | 'notifications' => $notifications, |
| 204 | 'page_count' => $wp_notifications->max_num_pages, |
| 205 | ); |
| 206 | |
| 207 | return $notifications; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Update Notification |
| 212 | * |
| 213 | * @param array $args |
| 214 | * |
| 215 | * @since 1.1.3 |
| 216 | * |
| 217 | * @return array |
| 218 | */ |
| 219 | public static function update_notification( $args = array() ) { |
| 220 | |
| 221 | $args = wp_parse_args( |
| 222 | $args, |
| 223 | array( |
| 224 | 'id' => 0, |
| 225 | 'title' => '', |
| 226 | 'content' => '', |
| 227 | 'type' => 'normal', |
| 228 | 'for' => get_current_user_id(), |
| 229 | 'date' => '', |
| 230 | ) |
| 231 | ); |
| 232 | |
| 233 | $post_args = array( |
| 234 | 'ID' => $args['id'], |
| 235 | 'post_type' => 'shoppress_notif', |
| 236 | 'post_status' => 'publish', |
| 237 | 'post_title' => $args['title'], |
| 238 | 'post_content' => $args['content'], |
| 239 | 'meta_input' => $args['meta_input'] ?? array(), |
| 240 | ); |
| 241 | |
| 242 | if ( ! empty( $args['date'] ) ) { |
| 243 | |
| 244 | $post_args['post_date'] = $args['date']; |
| 245 | } |
| 246 | |
| 247 | if ( $args['type'] ) { |
| 248 | |
| 249 | $post_args['meta_input']['_shopperss_notification_type'] = $args['type']; |
| 250 | } |
| 251 | |
| 252 | if ( $args['for'] ) { |
| 253 | |
| 254 | $post_args['meta_input']['_shopperss_notification_for'] = $args['for']; |
| 255 | } |
| 256 | |
| 257 | if ( $post_args['ID'] ) { |
| 258 | |
| 259 | $notification_id = wp_update_post( $post_args ); |
| 260 | } else { |
| 261 | |
| 262 | $notification_id = wp_insert_post( $post_args ); |
| 263 | } |
| 264 | |
| 265 | return $notification_id; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Add notifications query vars |
| 270 | * |
| 271 | * @param array $vars |
| 272 | * |
| 273 | * @since 1.1.3 |
| 274 | * |
| 275 | * @return array |
| 276 | */ |
| 277 | public static function add_notifications_query_vars( $vars ) { |
| 278 | |
| 279 | $vars['notifications'] = 'notifications'; |
| 280 | |
| 281 | return $vars; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Add notifications menu to woocommerce my account |
| 286 | * |
| 287 | * @param array $menu_links |
| 288 | * |
| 289 | * @since 1.1.3 |
| 290 | * |
| 291 | * @return array |
| 292 | */ |
| 293 | public static function add_notifications_links_to_my_account( $menu_links ) { |
| 294 | |
| 295 | $menu_links = array_slice( $menu_links, 0, 2 ) |
| 296 | + array( 'notifications' => __( 'Notifications', 'shop-press' ) ) |
| 297 | + array_slice( $menu_links, 2 ); |
| 298 | |
| 299 | return $menu_links; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Add notifications menu to woocommerce my account |
| 304 | * |
| 305 | * @param array $menu_links |
| 306 | * |
| 307 | * @since 1.1.3 |
| 308 | * |
| 309 | * @return array |
| 310 | */ |
| 311 | public static function add_notifications_endpoint() { |
| 312 | |
| 313 | add_rewrite_endpoint( 'notifications', EP_PAGES ); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /** |
| 318 | * Notifications endpoint content. |
| 319 | * |
| 320 | * @since 1.1.3 |
| 321 | * |
| 322 | * @return void |
| 323 | */ |
| 324 | public static function display_notifications_endpoint() { |
| 325 | echo wp_kses_post( self::get_notifications_content() ); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Returns the notifications content. |
| 330 | * |
| 331 | * @since 1.2.0 |
| 332 | */ |
| 333 | private static function get_notifications_content() { |
| 334 | ob_start(); |
| 335 | |
| 336 | $builder_id = sp_get_template_settings( 'my_account_notifications', 'page_builder' ); |
| 337 | |
| 338 | echo '<div id="shoppress-wrap" class="shoppress-wrap">'; |
| 339 | if ( $builder_id ) { |
| 340 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 341 | echo sp_get_builder_content( $builder_id ); |
| 342 | } else { |
| 343 | require_once sp_get_template_path( 'notifications/notifications' ); |
| 344 | } |
| 345 | echo '</div>'; |
| 346 | |
| 347 | return ob_get_clean(); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Add notification after add new order |
| 352 | * |
| 353 | * @param int $order_id |
| 354 | * |
| 355 | * @since 1.1.3 |
| 356 | * |
| 357 | * @return void |
| 358 | */ |
| 359 | public static function notification_new_order( $order_id ) { |
| 360 | |
| 361 | $order = wc_get_order( $order_id ); |
| 362 | |
| 363 | $title = sprintf( |
| 364 | /* translators: %s: order ID */ |
| 365 | __( 'New order #%s ', 'shop-press' ), // TODO: get from settings |
| 366 | $order_id |
| 367 | ); |
| 368 | $message = sprintf( |
| 369 | __( 'A new order has been created.', 'shop-press' ) // TODO: get from settings |
| 370 | ); |
| 371 | |
| 372 | $notification = array( |
| 373 | 'title' => $title, |
| 374 | 'content' => $message, |
| 375 | 'for' => $order->get_customer_id(), |
| 376 | 'type' => 'order_created', |
| 377 | 'meta_input' => array( |
| 378 | '_shoppress_order_id' => $order_id, |
| 379 | ), |
| 380 | ); |
| 381 | |
| 382 | $notification_id = static::update_notification( $notification ); |
| 383 | |
| 384 | do_action( 'shoppress_notification_new_order', $notification_id, $notification ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Add notification after change order status |
| 389 | * |
| 390 | * @param int $order_id |
| 391 | * |
| 392 | * @since 1.1.3 |
| 393 | * |
| 394 | * @return void |
| 395 | */ |
| 396 | public static function notification_order_status( $order_id ) { |
| 397 | |
| 398 | $order_status = str_replace( 'woocommerce_order_status_', '', current_action() ); |
| 399 | |
| 400 | $order = wc_get_order( $order_id ); |
| 401 | $order_statuses = wc_get_order_statuses(); |
| 402 | |
| 403 | $order_status_text = $order_statuses[ "wc-$order_status" ] ?? $order_status; |
| 404 | |
| 405 | $title = sprintf( |
| 406 | /* translators: %s: order ID */ |
| 407 | __( 'Order #%s', 'shop-press' ), // TODO: get from settings |
| 408 | $order_id |
| 409 | ); |
| 410 | $message = sprintf( |
| 411 | /* translators: %s: order status text */ |
| 412 | __( 'Order status changed to %s.', 'shop-press' ), // TODO: get from settings |
| 413 | $order_status_text |
| 414 | ); |
| 415 | |
| 416 | $notification = array( |
| 417 | 'title' => $title, |
| 418 | 'content' => $message, |
| 419 | 'for' => $order->get_customer_id(), |
| 420 | 'type' => 'order_status_changed', |
| 421 | 'meta_input' => array( |
| 422 | '_shoppress_order_id' => $order_id, |
| 423 | '_shoppress_order_status' => $order_status, |
| 424 | ), |
| 425 | ); |
| 426 | |
| 427 | $notification_id = static::update_notification( $notification ); |
| 428 | |
| 429 | do_action( 'shoppress_notification_order_status', $notification_id, $notification, $order_id, $order_status ); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Add comment notification after change order status changed to order completed |
| 434 | * |
| 435 | * @param int $order_id |
| 436 | * |
| 437 | * @since 1.1.3 |
| 438 | * |
| 439 | * @return void |
| 440 | */ |
| 441 | public static function add_comment_notification_after_order_completed( $notification_id, $notification, $order_id, $order_status ) { |
| 442 | |
| 443 | if ( 'completed' !== $order_status ) { |
| 444 | |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | $order = wc_get_order( $order_id ); |
| 449 | |
| 450 | $title = sprintf( |
| 451 | /* translators: %s: order ID */ |
| 452 | __( 'Order #%s', 'shop-press' ), // TODO: get from settings |
| 453 | $order_id |
| 454 | ); |
| 455 | $message = sprintf( |
| 456 | __( 'Please add your comment about the purchased products.', 'shop-press' ), // TODO: get from settings |
| 457 | ); |
| 458 | |
| 459 | $notification = array( |
| 460 | 'title' => $title, |
| 461 | 'content' => $message, |
| 462 | 'for' => $order->get_customer_id(), |
| 463 | 'type' => 'order_comment', |
| 464 | 'date' => date_i18n( 'Y-m-d H:i', strtotime( '+7 days' ) ), // TODO: add for 7 days last |
| 465 | 'meta_input' => array( |
| 466 | '_shoppress_order_id' => $order_id, |
| 467 | '_shoppress_order_status' => $order_status, |
| 468 | ), |
| 469 | ); |
| 470 | |
| 471 | $notification_id = static::update_notification( $notification ); |
| 472 | |
| 473 | do_action( 'shoppress_add_comment_notification_after_order_completed', $notification_id, $notification, $order_status ); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Display last notifications |
| 478 | * |
| 479 | * @param int $order_id |
| 480 | * |
| 481 | * @since 1.1.3 |
| 482 | * |
| 483 | * @return void |
| 484 | */ |
| 485 | public static function display_notifications_widget() { |
| 486 | |
| 487 | include sp_get_template_path( 'notifications/notifications-dashboard-widget' ); |
| 488 | } |
| 489 | } |
| 490 |