class-auxels-plugin-check-update.php
6 years ago
class-auxin-dashboard-notice.php
7 years ago
class-auxin-license-activation.php
7 years ago
class-auxin-notices.php
6 years ago
class-auxin-upgrader-ajax-handlers.php
7 years ago
class-auxin-upgrader-http-api.php
6 years ago
class-auxin-upgrader-plugin.php
7 years ago
class-auxin-upgrader-prepare.php
6 years ago
class-auxin-upgrader-theme.php
7 years ago
class-auxin-dashboard-notice.php
241 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Auxin admin notices |
| 4 | */ |
| 5 | class Auxin_Dashboard_Notice { |
| 6 | |
| 7 | /** |
| 8 | * Instance of this class. |
| 9 | * |
| 10 | * @var object |
| 11 | */ |
| 12 | protected static $instance = null; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * The list of notice IDs |
| 17 | * |
| 18 | * @var object |
| 19 | */ |
| 20 | private $notice_ids = array(); |
| 21 | |
| 22 | /** |
| 23 | * Notices |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private $notices = array(); |
| 28 | |
| 29 | /** |
| 30 | * Base API URL |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private $base_url = ''; |
| 35 | |
| 36 | /** |
| 37 | * Prefix |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | private $prefix = 'auxin-'; |
| 42 | |
| 43 | |
| 44 | function __construct(){ |
| 45 | |
| 46 | $this->notice_ids = array( |
| 47 | 'auxels-notice-info-dashboard' |
| 48 | ); |
| 49 | |
| 50 | $this->prefix = THEME_ID . '-'; |
| 51 | |
| 52 | $this->base_url = 'http://cdn.averta.net/project/phlox/free/info/'; |
| 53 | } |
| 54 | |
| 55 | // Methods using the plugin API |
| 56 | // ========================================================================= |
| 57 | |
| 58 | private function get_option( $option_key ){ |
| 59 | return auxin_get_option( $this->prefix . $option_key ); |
| 60 | } |
| 61 | |
| 62 | private function update_option( $option_key, $option_value ){ |
| 63 | return auxin_update_option( $this->prefix . $option_key, $option_value ); |
| 64 | } |
| 65 | |
| 66 | private function get_transient( $transient ){ |
| 67 | return auxin_get_transient( $this->prefix . $transient ); |
| 68 | } |
| 69 | |
| 70 | private function set_transient( $transient, $value, $expiration = 0 ){ |
| 71 | return auxin_set_transient( $this->prefix . $transient, $value, $expiration ); |
| 72 | } |
| 73 | |
| 74 | private function delete_transient( $transient ){ |
| 75 | return auxin_delete_transient( $this->prefix . $transient ); |
| 76 | } |
| 77 | |
| 78 | private function get_remote_post( $url ){ |
| 79 | return auxin_remote_post( $url ); |
| 80 | } |
| 81 | |
| 82 | // ========================================================================= |
| 83 | |
| 84 | private function get_notice_info_transient_id( $notice_id ){ |
| 85 | return 'notice-info-' . esc_attr( $notice_id ); |
| 86 | } |
| 87 | |
| 88 | private function fetch_notice_info( $notice_id, $force_update = false ){ |
| 89 | |
| 90 | if( empty( $notice_id ) ){ |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // defaults |
| 95 | $defaults = array( |
| 96 | 'remote_url' => '', // the remote notice url |
| 97 | 'beta_url' => '', // beta remote content |
| 98 | 'revision' => '', // empty means don't display |
| 99 | 'first_delay' => 0, // in seconds |
| 100 | 'id' => $notice_id, |
| 101 | 'enabled' => true, |
| 102 | |
| 103 | 'content' => '', // the remote notice content |
| 104 | 'delay_passed' => false, // the remote notice content |
| 105 | 'debug' => array() |
| 106 | ); |
| 107 | |
| 108 | // info transient id |
| 109 | $transient_id = $this->get_notice_info_transient_id( $notice_id ); |
| 110 | |
| 111 | if( isset( $_GET['msafi'] ) ){ |
| 112 | $this->delete_transient( $transient_id ); |
| 113 | } |
| 114 | |
| 115 | if( ! $force_update && false !== ( $result = $this->get_transient( $transient_id ) ) ){ |
| 116 | // wp_parse_args to prevent the errors while new args implemented in new versions |
| 117 | $defaults['debug'][] = '1.1'; |
| 118 | return wp_parse_args( $result, $defaults ); |
| 119 | } |
| 120 | |
| 121 | if( false === $info = $this->get_remote_post( $this->base_url . $notice_id . '.json' ) ){ |
| 122 | $defaults['debug'][] = '1.2'; |
| 123 | return $defaults; |
| 124 | } else { |
| 125 | $info = json_decode( $info, true ); |
| 126 | $info = wp_parse_args( $info, $defaults ); |
| 127 | $info['debug'][] = '1.3'; |
| 128 | } |
| 129 | |
| 130 | // get remote content |
| 131 | $remote_url = isset( $_GET['msbeta'] ) ? $info["beta_url"] : $info["remote_url"]; |
| 132 | $info["content"] = $this->fetch_file_content( $remote_url ); |
| 133 | |
| 134 | if( empty( $info["revision"] ) ){ |
| 135 | $info["enabled"] = false; |
| 136 | $info['debug'][] = '1.4'; |
| 137 | |
| 138 | } elseif( is_numeric( $info['revision'] ) && $info['revision'] != $this->get_option( 'notice-'. $notice_id .'-latest-revision' ) ){ |
| 139 | $info["enabled"] = true; |
| 140 | $info['debug'][] = '1.5'; |
| 141 | } else { |
| 142 | $info["enabled"] = false; |
| 143 | $info['debug'][] = '1.55'; |
| 144 | } |
| 145 | |
| 146 | if( ! $this->get_option( 'notice-installtion-time' ) ){ |
| 147 | $this->update_option( 'notice-installtion-time', time() ); |
| 148 | $info['debug'][] = '1.6'; |
| 149 | } |
| 150 | |
| 151 | $first_delay_diff = ( ( $this->get_option( 'notice-installtion-time' ) + ( (int) $info['first_delay'] ) ) - time() ); |
| 152 | $info['debug'][] = 'Due - Now: '. ( $first_delay_diff < 0 ? 'Passed ' . abs($first_delay_diff) : ' Remaining ' . abs($first_delay_diff) ) . ' seconds'; |
| 153 | $info['debug'][] = 'Previous revision: '. $this->get_option( 'notice-'. $notice_id .'-latest-revision' ); |
| 154 | $info['debug'][] = 'Remote revision: '. $info['revision']; |
| 155 | |
| 156 | // check for initial delay |
| 157 | if( $info['first_delay'] ){ |
| 158 | if( $this->get_option( 'notice-installtion-time' ) + ( (int) $info['first_delay'] ) > time() ){ |
| 159 | $info['debug'][] = '1.7'; |
| 160 | $info["delay_passed"] = false; |
| 161 | } else { |
| 162 | $info["delay_passed"] = true; |
| 163 | $info['debug'][] = '1.8'; |
| 164 | } |
| 165 | } else { |
| 166 | $info["delay_passed"] = true; |
| 167 | } |
| 168 | |
| 169 | $this->set_transient( $transient_id, $info, 6 * HOUR_IN_SECONDS ); |
| 170 | |
| 171 | return $info; |
| 172 | } |
| 173 | |
| 174 | private function fetch_file_content( $url ){ |
| 175 | if( false === $result = $this->get_remote_post( $url ) ){ |
| 176 | return ''; |
| 177 | } |
| 178 | return $result; |
| 179 | } |
| 180 | |
| 181 | public function get_content( $notice_id ){ |
| 182 | $result = $this->fetch_notice_info( $notice_id ); |
| 183 | |
| 184 | if( isset( $_GET['msdebug'] ) ){ |
| 185 | $debug_info = $result; |
| 186 | unset( $debug_info['content'] ); |
| 187 | $debug = axpp( $debug_info, false, true ); |
| 188 | } else { |
| 189 | $debug = ''; |
| 190 | } |
| 191 | |
| 192 | if( ! empty( $result['content'] ) && $result['enabled'] && $result['delay_passed'] ){ |
| 193 | return $result['content'] . $debug; |
| 194 | } |
| 195 | |
| 196 | return $debug; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | public function get_notice( $notice_id ){ |
| 201 | if( $content = $this->get_content( $notice_id ) ){ |
| 202 | return '<div class="aux-welcome-notice-wrapper aux-welcome-banner-wrapper">' . $content . '</div>'; |
| 203 | } |
| 204 | return ''; |
| 205 | } |
| 206 | |
| 207 | public function disable_notice( $notice_id ){ |
| 208 | |
| 209 | if( ! in_array( $notice_id, $this->notice_ids ) ){ |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // info transient id |
| 214 | $transient_id = $this->get_notice_info_transient_id( $notice_id ); |
| 215 | |
| 216 | if( false !== ( $info = $this->fetch_notice_info( $notice_id ) ) ){ |
| 217 | $this->update_option( 'notice-'. $notice_id .'-latest-revision', $info['revision'] ); |
| 218 | $info['enabled'] = false; |
| 219 | $this->set_transient( $transient_id, $info, 6 * HOUR_IN_SECONDS ); |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Return an instance of this class. |
| 227 | * |
| 228 | * @return object A single instance of this class. |
| 229 | */ |
| 230 | public static function get_instance() { |
| 231 | |
| 232 | // If the single instance hasn't been set, set it now. |
| 233 | if ( null == self::$instance ) { |
| 234 | self::$instance = new self; |
| 235 | } |
| 236 | |
| 237 | return self::$instance; |
| 238 | } |
| 239 | |
| 240 | } |
| 241 |