Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.13 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Notes class
  4.  *
  5.  * @author  Yithemes
  6.  * @package YITH Desktop Notifications for WooCommerce
  7.  * @version 1.0.0
  8.  */
  9.  
  10. if ( ! defined( 'YITH_WCDN_VERSION' ) ) {
  11.     exit( 'Direct access forbidden.' );
  12. }
  13.  
  14.  
  15. if ( !class_exists( 'YITH_WCDN_Register_Notifications_Premium' ) ) {
  16.     /**
  17.      * YITH_WCDN_Register_Notifications
  18.      *
  19.      * @since 1.0.0
  20.      */
  21.     class YITH_WCDN_Register_Notifications_Premium extends YITH_WCDN_Register_Notifications {
  22.  
  23.         /**
  24.          * Single instance of the class
  25.          *
  26.          * @var \YITH_WCDN_Register_Notifications
  27.          * @since 1.0.0
  28.          */
  29.  
  30.         public $table_name = '';
  31.  
  32.         /**
  33.          * Constructor
  34.          *
  35.          * @since  1.0.0
  36.          * @author Carlos Rodríguez <carlos.rodriguez@yourinspiration.it>
  37.          */
  38.         public function __construct() {
  39.             global $wpdb;
  40.             $this->table_name = $wpdb->prefix . YITH_WCDN_DB::$notification_table;
  41.  
  42.             parent::__construct();
  43.         }
  44.  
  45.         /**
  46.          * @param  $notifications
  47.          */
  48.         public function add_notification($notifications,$is_parent_order = true ) {
  49.             global $wpdb;
  50.             foreach($notifications as $key =>$type) {
  51.  
  52.                 $type_notificacion = $type['notification'];
  53.                 $key_notification = $key;
  54.                 $data_notification = array();
  55.                 $data_notification['title'] = $type['title'];
  56.                 $data_notification['description'] = $type['description'];
  57.                 $data_notification['icon'] = $type['icon'];
  58.                 $data_notification['sound'] = $type['sound'];
  59.                 $data_notification['time_notification'] = $type['time_notification'];
  60.                 if($is_parent_order &&  defined( 'YITH_WPV_PREMIUM' ) && YITH_WPV_PREMIUM ) {
  61.                     $key = array_search('yith_vendor', $type['role_user']);
  62.                     if($key) {
  63.                         unset($type['role_user'][$key]);
  64.                     }
  65.                 }
  66.                 $role_user_notification = $type['role_user'];
  67.                 $url_notificated = $type['url'];
  68.                 $vendors = (isset($type['vendors'])) ? $type['vendors']: "NULL";
  69.                 $user_notificated = array();
  70.                 $insert_query = "INSERT INTO $this->table_name (`key`, `notification`, `data`, `user_roles_to_notify`,`notified_users`,`url`,`vendors`) VALUES ('" . $key_notification . "', '" . $type_notificacion . "', '" . serialize($data_notification) . "' , '" . serialize($role_user_notification) . "' , '" . serialize($user_notificated) . "','" . $url_notificated . "','" . serialize($vendors) . "')";
  71.                 $wpdb->query( $insert_query );
  72.             }
  73.         }
  74.  
  75.         /**
  76.          *
  77.          * @return array|null|object
  78.          */
  79.         public function get_notifications_by_user($user) {
  80.             global $wpdb;
  81.             $user_id = $user->ID;
  82.             $user_roles = $user->roles;
  83.             $datetime = 'NOW() - INTERVAL 15 MINUTE' ;
  84.             $table = $wpdb->prefix . YITH_WCDN_DB::$notification_table;
  85.  
  86.             $where      = '';
  87.  
  88.             if ($user_roles) {
  89.                 $where = "WHERE (";
  90.                 $first = true;
  91.                 foreach ($user_roles as $user_role) {
  92.                     if (!$first)
  93.                         $where .= ' OR ';
  94.  
  95.                     $where .= "user_roles_to_notify LIKE '%\"$user_role\"%'";
  96.  
  97.                     $first = false;
  98.                 }
  99.             }
  100.  
  101.             if ( defined( 'YITH_WPV_PREMIUM' ) && YITH_WPV_PREMIUM ) {
  102.  
  103.                 $where .= ") AND (notified_users NOT LIKE '%i:$user_id;%') AND (date >= '%\"$datetime\"%') AND (vendors LIKE '%NULL%')";
  104.                 $where .= " OR (notified_users NOT LIKE '%i:$user_id;%') AND (date >= '%\"$datetime\"%') AND (vendors LIKE '%\"$user_id\";%')";
  105.  
  106.             } else {
  107.                 $where .= ") AND (notified_users NOT LIKE '%i:$user_id;%') AND (date >= '%\"$datetime\"%')";
  108.             }
  109.             $results = $wpdb->get_results( "SELECT * FROM $table $where" );
  110.  
  111.             return $results;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement