| @@ -41,12 +41,19 @@ | ||
| 41 | 41 | */ |
| 42 | 42 | public function __construct( Visualizer_Plugin $plugin ) { |
| 43 | 43 | parent::__construct( $plugin ); |
| 44 | 44 | |
| 45 | + register_activation_hook( VISUALIZER_BASEFILE, array( $this, 'activate' ) ); | |
| 46 | + register_deactivation_hook( VISUALIZER_BASEFILE, array( $this, 'deactivate' ) ); | |
| 47 | + $this->_addAction( 'visualizer_schedule_refresh_db', 'refreshDbChart' ); | |
| 48 | + $this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 ); | |
| 49 | + | |
| 50 | + $this->_addAction( 'activated_plugin', 'onActivation' ); | |
| 45 | 51 | $this->_addAction( 'init', 'setupCustomPostTypes' ); |
| 46 | 52 | $this->_addAction( 'plugins_loaded', 'loadTextDomain' ); |
| 47 | 53 | $this->_addFilter( 'visualizer_logger_data', 'getLoggerData' ); |
| 48 | 54 | $this->_addFilter( 'visualizer_get_chart_counts', 'getChartCountsByTypeAndMeta' ); |
| 55 | + | |
| 49 | 56 | } |
| 50 | 57 | /** |
| 51 | 58 | * Fetches the SDK logger data. |
| 52 | 59 | * |
| @@ -106,12 +113,16 @@ | ||
| 106 | 113 | * @access public |
| 107 | 114 | */ |
| 108 | 115 | public function setupCustomPostTypes() { |
| 109 | 116 | register_post_type( |
| 110 | - Visualizer_Plugin::CPT_VISUALIZER, array( | |
| 117 | + Visualizer_Plugin::CPT_VISUALIZER, | |
| 118 | + array( | |
| 111 | 119 | 'label' => 'Visualizer Charts', |
| 112 | 120 | 'public' => false, |
| 113 | 121 | 'supports' => array( 'revisions' ), |
| 122 | + 'show_in_rest' => true, | |
| 123 | + 'rest_base' => 'visualizer', | |
| 124 | + 'rest_controller_class' => 'WP_REST_Posts_Controller', | |
| 114 | 125 | ) |
| 115 | 126 | ); |
| 116 | 127 | } |
| 117 | 128 | |
| @@ -124,7 +135,151 @@ | ||
| 124 | 135 | * @access public |
| 125 | 136 | */ |
| 126 | 137 | public function loadTextDomain() { |
| 127 | 138 | load_plugin_textdomain( Visualizer_Plugin::NAME, false, dirname( plugin_basename( VISUALIZER_BASEFILE ) ) . '/languages/' ); |
| 139 | + } | |
| 140 | + | |
| 141 | + /** | |
| 142 | + * Activate the plugin | |
| 143 | + */ | |
| 144 | + public function activate() { | |
| 145 | + wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' ); | |
| 146 | + wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, 'hourly', 'visualizer_schedule_refresh_db' ); | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | + * On activation of the plugin | |
| 151 | + */ | |
| 152 | + public function onActivation( $plugin ) { | |
| 153 | + if ( defined( 'TI_UNIT_TESTING' ) ) { | |
| 154 | + return; | |
| 155 | + } | |
| 156 | + | |
| 157 | + if ( $plugin === VISUALIZER_BASENAME ) { | |
| 158 | + wp_redirect( admin_url( 'upload.php?page=' . Visualizer_Plugin::NAME ) ); | |
| 159 | + exit(); | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Deactivate the plugin | |
| 166 | + */ | |
| 167 | + public function deactivate() { | |
| 168 | + wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' ); | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | + * Refresh the specific chart from the db. | |
| 173 | + * | |
| 174 | + * @param WP_Post $chart The chart object. | |
| 175 | + * @param int $chart_id The chart id. | |
| 176 | + * @param bool $force If this is true, then the chart data will be force refreshed. If false, data will be refreshed only if the chart requests live data. | |
| 177 | + * | |
| 178 | + * @access public | |
| 179 | + */ | |
| 180 | + public function refresh_db_for_chart( $chart, $chart_id, $force = false ) { | |
| 181 | + if ( ! $chart_id ) { | |
| 182 | + return $chart; | |
| 183 | + } | |
| 184 | + | |
| 185 | + if ( ! $chart ) { | |
| 186 | + $chart = get_post( $chart_id ); | |
| 187 | + } | |
| 188 | + | |
| 189 | + if ( ! $chart ) { | |
| 190 | + return $chart; | |
| 191 | + } | |
| 192 | + | |
| 193 | + // check if the source is correct. | |
| 194 | + $source = get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ); | |
| 195 | + $load_series = false; | |
| 196 | + switch ( $source ) { | |
| 197 | + case 'Visualizer_Source_Query': | |
| 198 | + // check if its a live-data chart or a cached-data chart. | |
| 199 | + if ( ! $force ) { | |
| 200 | + $hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 201 | + if ( ! empty( $hours ) ) { | |
| 202 | + // cached, bail! | |
| 203 | + return $chart; | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + $params = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, true ); | |
| 208 | + $source = new Visualizer_Source_Query( $params ); | |
| 209 | + $source->fetch( false ); | |
| 210 | + $load_series = true; | |
| 211 | + break; | |
| 212 | + case 'Visualizer_Source_Json': | |
| 213 | + // check if its a live-data chart or a cached-data chart. | |
| 214 | + if ( ! $force ) { | |
| 215 | + $hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true ); | |
| 216 | + if ( ! empty( $hours ) ) { | |
| 217 | + // cached, bail! | |
| 218 | + return $chart; | |
| 219 | + } | |
| 220 | + } | |
| 221 | + | |
| 222 | + $url = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ); | |
| 223 | + $root = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT, true ); | |
| 224 | + $paging = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING, true ); | |
| 225 | + $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ); | |
| 226 | + $source = new Visualizer_Source_Json( array( 'url' => $url, 'root' => $root, 'paging' => $paging ) ); | |
| 227 | + $source->refresh( $series ); | |
| 228 | + break; | |
| 229 | + default: | |
| 230 | + return $chart; | |
| 231 | + } | |
| 232 | + | |
| 233 | + $error = $source->get_error(); | |
| 234 | + if ( empty( $error ) ) { | |
| 235 | + add_filter( 'wp_revisions_to_keep', '__return_false' ); | |
| 236 | + if ( $load_series ) { | |
| 237 | + update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); | |
| 238 | + } | |
| 239 | + | |
| 240 | + wp_update_post( | |
| 241 | + array( | |
| 242 | + 'ID' => $chart_id, | |
| 243 | + 'post_content' => $source->getData(), | |
| 244 | + ) | |
| 245 | + ); | |
| 246 | + | |
| 247 | + $chart = get_post( $chart_id ); | |
| 248 | + } | |
| 249 | + | |
| 250 | + return $chart; | |
| 251 | + } | |
| 252 | + | |
| 253 | + /** | |
| 254 | + * Refresh the db chart. | |
| 255 | + * | |
| 256 | + * @access public | |
| 257 | + */ | |
| 258 | + public function refreshDbChart() { | |
| 259 | + $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() ); | |
| 260 | + if ( ! $schedules ) { | |
| 261 | + return; | |
| 262 | + } | |
| 263 | + if ( ! defined( 'VISUALIZER_DO_NOT_DIE' ) ) { | |
| 264 | + // define this so that the ajax call does not die | |
| 265 | + // this means that if the new version of pro and the old version of free are installed, only the first chart will be updated | |
| 266 | + define( 'VISUALIZER_DO_NOT_DIE', true ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + $new_schedules = array(); | |
| 270 | + $now = time(); | |
| 271 | + foreach ( $schedules as $chart_id => $time ) { | |
| 272 | + $new_schedules[ $chart_id ] = $time; | |
| 273 | + if ( $time > $now ) { | |
| 274 | + continue; | |
| 275 | + } | |
| 276 | + | |
| 277 | + // if the time is nigh, we force an update. | |
| 278 | + $this->refresh_db_for_chart( null, $chart_id, true ); | |
| 279 | + $hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 280 | + $new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS; | |
| 281 | + } | |
| 282 | + update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $new_schedules ); | |
| 128 | 283 | } |
| 129 | 284 | |
| 130 | 285 | } |