NotificationsManager.php (6459B)
1 <?php 2 3 4 namespace Materialis\Notify; 5 6 7 class NotificationsManager 8 { 9 const DISMISSED_NOTIFICATIONS_OPTION = "cp_dismissed_notifications"; 10 const INITIALIZATION_NOTIFICATIONS_OPTION = "cp_initialize_notifications"; 11 12 13 private static $remote_data_url_base = "http://extendthemes.com/wp-json/extendthemes/v1/notifications"; 14 15 public static function initializationTS() 16 { 17 $time = get_option(static::INITIALIZATION_NOTIFICATIONS_OPTION, false); 18 if ( ! $time) { 19 $time = time(); 20 update_option(static::INITIALIZATION_NOTIFICATIONS_OPTION, $time); 21 } 22 23 return (floor($time / 86400) * 86400); 24 } 25 26 public static function isDevMode() 27 { 28 return (defined('EXTENDTHEMES_NOTIFICATIONS_DEV_MODE') && EXTENDTHEMES_NOTIFICATIONS_DEV_MODE); 29 } 30 31 32 public static function getRemoteNotificationsURL() 33 { 34 $dev_mode = NotificationsManager::isDevMode(); 35 $base = NotificationsManager::$remote_data_url_base; 36 37 $query = array( 38 'theme' => get_template(), 39 'stylesheet' => get_stylesheet(), 40 'license' => urlencode(''), 41 'dev_mode' => $dev_mode ? "1" : "0", 42 ); 43 44 $query_string = build_query($query); 45 46 if ($query_string) { 47 $query_string = "?" . $query_string; 48 } 49 50 return apply_filters('extendthemes_notifications_url', $base . $query_string, $base, $query); 51 52 } 53 54 public static function requestTimeout($value) 55 { 56 return 30; 57 } 58 59 public static function getRemoteNotifications() 60 { 61 62 $transientKey = apply_filters('extendthemes_demo_import_transient_key', get_template() . '_notifications'); 63 $notifications = null; 64 if ( ! NotificationsManager::isDevMode()) { 65 $notifications = get_transient($transientKey); 66 } 67 68 if ( ! $notifications) { 69 add_filter('http_request_timeout', array(__CLASS__, 'requestTimeout')); 70 $data = wp_remote_get(NotificationsManager::getRemoteNotificationsURL(), array( 71 'body' => array(), 72 )); 73 remove_filter('http_request_timeout', array(__CLASS__, 'requestTimeout')); 74 75 if ($data instanceof \WP_Error) { 76 if (NotificationsManager::isDevMode()) { 77 die($data->get_error_message()); 78 } 79 } else { 80 $data = json_decode($data['body'], true); 81 82 if (isset($data['code']) && $data['code'] === 'extendthemes_notifications_data_ok') { 83 $notifications = $data['data']; 84 set_transient($transientKey, $data['data'], 1800); 85 } 86 87 } 88 } 89 90 wp_json_encode($notifications); 91 } 92 93 94 public static function addRemoteNotifications($current_notifications) 95 { 96 $transientKey = apply_filters('extendthemes_demo_import_transient_key', get_template() . '_notifications'); 97 $notifications = get_transient($transientKey); 98 $notifications = is_array($notifications) ? $notifications : array(); 99 100 return array_merge($notifications, $current_notifications); 101 } 102 103 public static function load($notifications) 104 { 105 106 add_action("wp_ajax_extendthemes_get_remote_data_notifications", array(__CLASS__, 'getRemoteNotifications')); 107 if ( ! is_admin()) { 108 return; 109 } 110 static::initializationTS(); 111 112 if ( ! get_option('cp_initialize_notifications', false)) { 113 update_option('cp_initialize_notifications', time()); 114 } 115 116 $notifications = NotificationsManager::addRemoteNotifications($notifications); 117 $notifications = apply_filters('cp_load_notifications', $notifications); 118 119 foreach ($notifications as $notification) { 120 new Notification($notification); 121 } 122 123 if (count($notifications)) { 124 add_action('admin_head', function () { 125 ?> 126 <style type="text/css"> 127 .cp-notification { 128 padding-top: 0rem; 129 padding-bottom: 0rem; 130 } 131 132 .cp-notification-card { 133 padding: 30px 20px; 134 margin: 0px 10px 20px 10px; 135 display: inline-block; 136 background: #fff; 137 box-shadow: 0 1px 20px 5px rgba(0, 0, 0, 0.1); 138 } 139 140 .cp-notification-card:first-of-type { 141 margin-left: 0px; 142 } 143 144 .cp-notification-card:last-of-type { 145 margin-right: 0px; 146 } 147 148 </style> 149 <?php 150 }); 151 152 add_action('wp_ajax_cp_dismiss_notification', array(__CLASS__, 'dismissNotification')); 153 } 154 155 add_action('admin_footer', function () { 156 $transientKey = apply_filters('extendthemes_demo_import_transient_key', get_template() . '_notifications'); 157 $notifications = get_transient($transientKey); 158 159 if (is_array($notifications) && ! NotificationsManager::isDevMode()) { 160 return; 161 } 162 163 ?> 164 <script> 165 jQuery.post( 166 "<?php echo admin_url("/admin-ajax.php"); ?>", 167 { 168 action: "extendthemes_get_remote_data_notifications" 169 } 170 ) 171 </script> 172 <?php 173 }); 174 175 } 176 177 178 public static function dismissNotification() 179 { 180 if ( ! is_user_logged_in() || ! current_user_can('edit_theme_options')) { 181 die(); 182 } 183 184 $notification = isset($_REQUEST['notification']) ? $_REQUEST['notification'] : false; 185 186 if ($notification) { 187 $dismissedNotifications = get_option(static::DISMISSED_NOTIFICATIONS_OPTION, array()); 188 if ( ! in_array($notification, $dismissedNotifications)) { 189 $dismissedNotifications[] = $notification; 190 } 191 192 update_option(static::DISMISSED_NOTIFICATIONS_OPTION, $dismissedNotifications); 193 } 194 195 } 196 197 }