-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathSetup.php
More file actions
472 lines (422 loc) · 15.3 KB
/
Setup.php
File metadata and controls
472 lines (422 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
// +----------------------------------------------------------------------+
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License, version 2, as |
// | published by the Free Software Foundation. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
// | MA 02110-1301 USA |
// +----------------------------------------------------------------------+
// | Author: Eugene Manuilov <eugene@manuilov.org> |
// +----------------------------------------------------------------------+
/**
* General module what setups all required environment.
*
* @category Visualizer
* @package Module
*
* @since 1.0.0
*/
class Visualizer_Module_Setup extends Visualizer_Module {
const NAME = __CLASS__;
/**
* Constructor.
*
* @since 1.0.0
*
* @access public
* @param Visualizer_Plugin $plugin The instance of the plugin.
*/
public function __construct( Visualizer_Plugin $plugin ) {
parent::__construct( $plugin );
register_activation_hook( VISUALIZER_BASEFILE, array( $this, 'activate' ) );
register_deactivation_hook( VISUALIZER_BASEFILE, array( $this, 'deactivate' ) );
$this->_addAction( 'visualizer_schedule_refresh_db', 'refreshDbChart' );
$this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 );
$this->_addAction( 'admin_init', 'adminInit' );
$this->_addAction( 'init', 'setupCustomPostTypes' );
$this->_addFilter( 'cron_schedules', 'custom_cron_schedules' );
$this->_addAction( 'plugins_loaded', 'loadTextDomain' );
$this->_addFilter( 'visualizer_logger_data', 'getLoggerData' );
$this->_addFilter( 'visualizer_get_chart_counts', 'getUsage', 10, 2 );
$this->_addAction( 'init', 'checkIsExistingUser' );
// only for testing
// $this->_addAction( 'admin_init', 'getUsage' );
}
/**
* Fetches the SDK logger data.
*
* @param array $data The default data that needs to be sent.
*
* @access public
*/
public function getLoggerData( $data ) {
return $this->getUsage( $data );
}
/**
* Fetches the usage of charts.
*
* @param array $data The default data that needs to be sent.
* @param array $meta_keys An array of name vs. meta keys - to return how many charts have these keys.
*
* @access public
*/
public function getUsage( $data, $meta_keys = array() ) {
$charts = array( 'types' => array(), 'sources' => array(), 'library' => array(), 'permissions' => 0, 'manual_config' => 0, 'scheduled' => 0 );
$query_args = array(
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'posts_per_page' => 300,
'post_status' => 'publish',
'fields' => 'ids',
'no_rows_found' => false,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
// default permissions.
$default_perms = array(
'read' => 'all',
'read-specific' => null,
'edit' => 'roles',
'edit-specific' => array( 'administrator' ),
);
// collect all schedules chart ids.
$scheduled = array_merge(
array_keys( get_option( Visualizer_Plugin::CF_CHART_SCHEDULE, array() ) ),
array_keys( get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() ) )
);
$query = new WP_Query( $query_args );
while ( $query->have_posts() ) {
$chart_id = $query->next_post();
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
$charts['types'][ $type ] = isset( $charts['types'][ $type ] ) ? $charts['types'][ $type ] + 1 : 1;
$source = get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true );
$charts['sources'][ $source ] = isset( $charts['sources'][ $source ] ) ? $charts['sources'][ $source ] + 1 : 1;
$lib = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
$charts['library'][ $lib ] = isset( $charts['library'][ $lib ] ) ? $charts['library'][ $lib ] + 1 : 1;
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
if ( array_key_exists( 'manual', $settings ) && ! empty( $settings['manual'] ) ) {
$charts['manual_config'] = $charts['manual_config'] + 1;
}
// phpcs:ignore WordPress.PHP.StrictInArray.FoundNonStrictFalse
if ( in_array( $chart_id, $scheduled, false ) ) {
$charts['scheduled'] = $charts['scheduled'] + 1;
}
if ( Visualizer_Module::is_pro() ) {
$permissions = get_post_meta( $chart_id, Visualizer_Pro::CF_PERMISSIONS, true );
if ( empty( $permissions ) ) {
continue;
}
$permissions = $permissions['permissions'];
$customized = false;
foreach ( $default_perms as $key => $val ) {
if ( ! is_array( $val ) && ! is_null( $val ) && isset( $permissions[ $key ] ) && $permissions[ $key ] !== $val ) {
$customized = true;
} elseif ( is_array( $val ) && ! is_null( $val ) && isset( $permissions[ $key ] ) && count( $permissions[ $key ] ) !== count( $val ) ) {
$customized = true;
}
}
if ( $customized ) {
$charts['permissions'] = $charts['permissions'] + 1;
}
}
if ( ! empty( $meta_keys ) ) {
foreach ( $meta_keys as $name => $key ) {
$data = get_post_meta( $chart_id, $key, true );
if ( ! empty( $data ) ) {
$charts[ $name ] = isset( $charts[ $name ] ) ? $charts[ $name ] + 1 : 1;
} else {
$charts[ $name ] = 0;
}
}
}
}
return $charts;
}
/**
* Registers custom post type for charts.
*
* @since 1.0.0
* @uses register_post_type() To register custom post type for charts.
*
* @access public
*/
public function setupCustomPostTypes() {
register_post_type(
Visualizer_Plugin::CPT_VISUALIZER,
array(
'label' => 'Visualizer Charts',
'public' => false,
'supports' => array( 'revisions' ),
'show_in_rest' => true,
'rest_base' => 'visualizer',
'rest_controller_class' => 'WP_REST_Posts_Controller',
)
);
}
/**
* Loads plugin text domain translations.
*
* @since 1.0.0
* @uses load_plugin_textdomain() To load translations for the plugin.
*
* @access public
*/
public function loadTextDomain() {
load_plugin_textdomain( Visualizer_Plugin::NAME, false, dirname( plugin_basename( VISUALIZER_BASEFILE ) ) . '/languages/' );
}
/**
* Activate the plugin
*/
public function activate( $network_wide ) {
if ( is_multisite() && $network_wide ) {
foreach ( get_sites( array( 'fields' => 'ids' ) ) as $blog_id ) {
switch_to_blog( $blog_id );
$this->activate_on_site();
restore_current_blog();
}
} else {
$this->activate_on_site();
}
}
/**
* Activates the plugin on a particular blog instance (supports multisite and single site).
*/
private function activate_on_site() {
wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' );
wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, apply_filters( 'visualizer_chart_schedule_interval', 'visualizer_ten_minutes' ), 'visualizer_schedule_refresh_db' );
add_option( 'visualizer-activated', true );
$is_fresh_install = get_option( 'visualizer_fresh_install', false );
if ( ! defined( 'TI_CYPRESS_TESTING' ) && false === $is_fresh_install ) {
update_option( 'visualizer_fresh_install', '1' );
}
}
/**
* Deactivate the plugin
*/
public function deactivate( $network_wide ) {
if ( is_multisite() && $network_wide ) {
foreach ( get_sites( array( 'fields' => 'ids' ) ) as $blog_id ) {
switch_to_blog( $blog_id );
$this->deactivate_on_site();
restore_current_blog();
}
} else {
$this->deactivate_on_site();
}
}
/**
* Deactivates the plugin on a particular blog instance (supports multisite and single site).
*/
private function deactivate_on_site() {
wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' );
delete_option( 'visualizer-activated', true );
}
/**
* Check if plugin has been activated and then redirect to the correct page.
*/
public function adminInit() {
if ( defined( 'TI_UNIT_TESTING' ) ) {
return;
}
define( 'VISUALIZER_SURVEY', Visualizer_Module::is_pro() ? 'https://forms.gle/7Zo7FuZbvQ8DTvRi6' : 'https://forms.gle/muMtbcyvHn1aTvmJ7' );
// fire any upgrades necessary.
Visualizer_Module_Upgrade::upgrade();
if ( get_option( 'visualizer-activated' ) ) {
delete_option( 'visualizer-activated' );
if ( ! headers_sent() ) {
if ( ! Visualizer_Module::is_pro() && ! empty( get_option( 'visualizer_fresh_install', false ) ) ) {
$redirect_url = array(
'page' => 'visualizer-setup-wizard',
'tab' => '#step-1',
);
} else {
$page_name = Visualizer_Module::numberOfCharts() > 0 ? Visualizer_Plugin::NAME : 'viz-support';
$redirect_url = array(
'page' => $page_name,
);
}
wp_safe_redirect(
add_query_arg(
$redirect_url,
admin_url( 'admin.php' )
)
);
exit();
}
}
}
/**
* Refresh the specific chart from the db.
*
* @param WP_Post|null $chart The chart object.
* @param int $chart_id The chart id.
* @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.
*
* @access public
*/
public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
if ( ! $chart_id ) {
return $chart;
}
if ( ! $chart ) {
$chart = get_post( $chart_id );
}
if ( ! $chart ) {
return $chart;
}
// check if the source is correct.
$source = get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true );
$load_series = false;
switch ( $source ) {
case 'Visualizer_Source_Query':
// check if its a live-data chart or a cached-data chart.
if ( ! $force ) {
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
if ( ! empty( $hours ) ) {
// cached, bail!
return $chart;
}
}
$params = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, true );
$source = new Visualizer_Source_Query( $params, $chart_id );
$source->fetch( false );
$load_series = true;
break;
case 'Visualizer_Source_Json':
// check if its a live-data chart or a cached-data chart.
if ( ! $force ) {
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true );
if ( ! empty( $hours ) ) {
// cached, bail!
return $chart;
}
}
$url = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true );
$root = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT, true );
$paging = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING, true );
$series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
$source = new Visualizer_Source_Json( array( 'url' => $url, 'root' => $root, 'paging' => $paging ) );
$source->refresh( $series );
break;
case 'Visualizer_Source_Csv_Remote':
// check if its a live-data chart or a cached-data chart.
if ( ! $force ) {
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true );
if ( ! empty( $hours ) ) {
// cached, bail!
return $chart;
}
}
do_action( 'visualizer_schedule_import' );
return get_post( $chart_id );
default:
return $chart;
}
$error = $source->get_error();
if ( empty( $error ) ) {
$this->disableRevisionsTemporarily();
if ( $load_series ) {
update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
}
$allow_html = false;
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
if ( isset( $settings['allowHtml'] ) && intval( $settings['allowHtml'] ) === 1 ) {
$allow_html = true;
}
$allow_html = apply_filters( 'visualizer_allow_html_content', $allow_html, $chart_id, $chart );
if ( $allow_html ) {
kses_remove_filters();
}
wp_update_post(
array(
'ID' => $chart_id,
'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ),
)
);
if ( $allow_html ) {
kses_init_filters();
}
$chart = get_post( $chart_id );
delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );
} else {
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, sprintf( 'Error while updating chart: %s', $error ) );
}
return $chart;
}
/**
* Refresh the Database chart type.
*
* @access public
*/
public function refreshDbChart() {
// NOTE: This use a different key from normal schedule. Updated only by Database chart. Check `visualizer_schedule_import` action.
$chart_schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
if ( ! $chart_schedules ) {
return;
}
if ( ! defined( 'VISUALIZER_DO_NOT_DIE' ) ) {
// define this so that the ajax call does not die
// this means that if the new version of pro and the old version of free are installed, only the first chart will be updated
define( 'VISUALIZER_DO_NOT_DIE', true );
}
$new_schedules = array();
$current_time = time();
foreach ( $chart_schedules as $chart_id => $scheduled_time ) {
// Skip deleted charts.
if ( false === get_post_status( $chart_id ) ) {
continue;
}
$new_schedules[ $chart_id ] = $scheduled_time;
// Should we do an update?
if ( $scheduled_time > $current_time ) {
continue;
}
$this->refresh_db_for_chart( null, $chart_id, true );
// Clear existing chart cache.
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
if ( get_transient( $cache_key ) ) {
delete_transient( $cache_key );
}
$scheduled_hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
$new_schedules[ $chart_id ] = $current_time + $scheduled_hours * HOUR_IN_SECONDS;
}
update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $new_schedules );
}
/**
* Save flag for existing users.
*/
public function checkIsExistingUser() {
$chart_exists = get_option( 'visualizer-new-user', '' );
if ( '' === $chart_exists ) {
$charts = get_posts(
array(
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'fields' => 'ids',
)
);
update_option( 'visualizer-new-user', ! empty( $charts ) ? 'no' : 'yes' );
}
}
/**
* Add custom cron schedules.
*
* @param array $schedules The current schedules options.
* @return array The modified schedules options.
*/
public function custom_cron_schedules( $schedules ) {
$schedules['visualizer_ten_minutes'] = array(
'interval' => 600,
'display' => __( 'Every 10 minutes', 'visualizer' ),
);
return $schedules;
}
}