PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.1.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.1.3
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
← All changes | classes/Visualizer/Module/Setup.php +132 -1 3.0.93.1.3 View file →
@@ -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,127 @@
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 + if ( $source !== 'Visualizer_Source_Query' ) {
196 + return $chart;
197 + }
198 +
199 + // check if its a live-data chart or a cached-data chart.
200 + if ( ! $force ) {
201 + $hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
202 + if ( ! empty( $hours ) ) {
203 + // cached, bail!
204 + return $chart;
205 + }
206 + }
207 +
208 + $params = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, true );
209 + $source = new Visualizer_Source_Query( $params );
210 + $source->fetch( false );
211 +
212 + $error = $source->get_error();
213 + if ( empty( $error ) ) {
214 + update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
215 +
216 + wp_update_post(
217 + array(
218 + 'ID' => $chart_id,
219 + 'post_content' => $source->getData(),
220 + )
221 + );
222 +
223 + $chart = get_post( $chart_id );
224 + }
225 +
226 + return $chart;
227 + }
228 +
229 + /**
230 + * Refresh the db chart.
231 + *
232 + * @access public
233 + */
234 + public function refreshDbChart() {
235 + $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
236 + if ( ! $schedules ) {
237 + return;
238 + }
239 + if ( ! defined( 'VISUALIZER_DO_NOT_DIE' ) ) {
240 + // define this so that the ajax call does not die
241 + // this means that if the new version of pro and the old version of free are installed, only the first chart will be updated
242 + define( 'VISUALIZER_DO_NOT_DIE', true );
243 + }
244 +
245 + $new_schedules = array();
246 + $now = time();
247 + foreach ( $schedules as $chart_id => $time ) {
248 + $new_schedules[ $chart_id ] = $time;
249 + if ( $time > $now ) {
250 + continue;
251 + }
252 +
253 + // if the time is nigh, we force an update.
254 + $this->refresh_db_for_chart( null, $chart_id, true );
255 + $hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
256 + $new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
257 + }
258 + update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $new_schedules );
128 259 }
129 260
130 261 }