PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.0.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.0.1
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 3.10.4 All 148 releases
visualizer / classes / Visualizer / Module / Admin.php

Admin.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.0.1, at classes/Visualizer/Module/Admin.php

487 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Copyright 2013 Madpixels (email : visualizer@madpixels.net) |
4 // +----------------------------------------------------------------------+
5 // | This program is free software; you can redistribute it and/or modify |
6 // | it under the terms of the GNU General Public License, version 2, as |
7 // | published by the Free Software Foundation. |
8 // | |
9 // | This program is distributed in the hope that it will be useful, |
10 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 // | GNU General Public License for more details. |
13 // | |
14 // | You should have received a copy of the GNU General Public License |
15 // | along with this program; if not, write to the Free Software |
16 // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
17 // | MA 02110-1301 USA |
18 // +----------------------------------------------------------------------+
19 // | Author: Eugene Manuilov <eugene@manuilov.org> |
20 // +----------------------------------------------------------------------+
21 /**
22 * The module for all admin stuff.
23 *
24 * @category Visualizer
25 * @package Module
26 *
27 * @since 1.0.0
28 */
29 class Visualizer_Module_Admin extends Visualizer_Module {
30
31 const NAME = __CLASS__;
32
33 /**
34 * Library page suffix.
35 *
36 * @since 1.0.0
37 *
38 * @access private
39 * @var string
40 */
41 private $_libraryPage;
42
43 /**
44 * Constructor.
45 *
46 * @since 1.0.0
47 *
48 * @access public
49 *
50 * @param Visualizer_Plugin $plugin The instance of the plugin.
51 */
52 public function __construct( Visualizer_Plugin $plugin ) {
53 parent::__construct( $plugin );
54 $this->_addAction( 'load-post.php', 'enqueueMediaScripts' );
55 $this->_addAction( 'load-post-new.php', 'enqueueMediaScripts' );
56 $this->_addAction( 'admin_footer', 'renderTempaltes' );
57 $this->_addAction( 'admin_enqueue_scripts', 'enqueueLibraryScripts', null, 8 );
58 $this->_addAction( 'admin_menu', 'registerAdminMenu' );
59 $this->_addFilter( 'media_view_strings', 'setupMediaViewStrings' );
60 $this->_addFilter( 'plugin_action_links', 'getPluginActionLinks', 10, 2 );
61 $this->_addFilter( 'visualizer_logger_data', 'getLoggerData' );
62 $this->_addFilter( 'visualizer_get_chart_counts', 'getChartCountsByTypeAndMeta' );
63 $this->_addFilter( 'visualizer_feedback_review_trigger', 'feedbackReviewTrigger' );
64 }
65
66 /**
67 * Whether to show the feedback review or not.
68 *
69 * @access public
70 */
71 public function feedbackReviewTrigger( $dumb ) {
72 $query = new WP_Query(
73 array(
74 'posts_per_page' => 50,
75 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
76 'fields' => 'ids',
77 'update_post_meta_cache' => false,
78 'update_post_term_cache' => false,
79 )
80 );
81
82 if ( $query->have_posts() && $query->post_count > 0 ) {
83 return array(
84 'heading' => __( 'Some title from visualizer', 'visualizer' ),
85 'msg' => __( 'Some msg from visualizer', 'visualizer' ),
86 );
87 }
88 return false;
89 }
90
91 /**
92 * Enqueues media scripts and styles.
93 *
94 * @since 1.0.0
95 * @uses wp_enqueue_style To enqueue style file.
96 * @uses wp_enqueue_script To enqueue script file.
97 *
98 * @access public
99 */
100 public function enqueueMediaScripts() {
101 global $typenow;
102 if ( post_type_supports( $typenow, 'editor' ) ) {
103 wp_enqueue_style( 'visualizer-media', VISUALIZER_ABSURL . 'css/media.css', array( 'media-views' ), Visualizer_Plugin::VERSION );
104 wp_enqueue_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array( 'media-editor' ), null, true );
105 wp_enqueue_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true );
106 wp_enqueue_script( 'visualizer-media-model', VISUALIZER_ABSURL . 'js/media/model.js', array( 'visualizer-google-jsapi-old' ), Visualizer_Plugin::VERSION, true );
107 wp_enqueue_script( 'visualizer-media-collection', VISUALIZER_ABSURL . 'js/media/collection.js', array( 'visualizer-media-model' ), Visualizer_Plugin::VERSION, true );
108 wp_enqueue_script( 'visualizer-media-controller', VISUALIZER_ABSURL . 'js/media/controller.js', array( 'visualizer-media-collection' ), Visualizer_Plugin::VERSION, true );
109 wp_enqueue_script( 'visualizer-media-view', VISUALIZER_ABSURL . 'js/media/view.js', array( 'visualizer-media-controller' ), Visualizer_Plugin::VERSION, true );
110 wp_enqueue_script( 'visualizer-media-toolbar', VISUALIZER_ABSURL . 'js/media/toolbar.js', array( 'visualizer-media-view' ), Visualizer_Plugin::VERSION, true );
111 wp_enqueue_script( 'visualizer-media', VISUALIZER_ABSURL . 'js/media.js', array( 'visualizer-media-toolbar' ), Visualizer_Plugin::VERSION, true );
112 }
113 }
114
115 /**
116 * Extends media view strings with visualizer strings.
117 *
118 * @since 1.0.0
119 *
120 * @access public
121 *
122 * @param array $strings The array of media view strings.
123 *
124 * @return array The extended array of media view strings.
125 */
126 public function setupMediaViewStrings( $strings ) {
127 $strings['visualizer'] = array(
128 'actions' => array(
129 'get_charts' => Visualizer_Plugin::ACTION_GET_CHARTS,
130 'delete_chart' => Visualizer_Plugin::ACTION_DELETE_CHART,
131 ),
132 'controller' => array(
133 'title' => esc_html__( 'Visualizations', 'visualizer' ),
134 ),
135 'routers' => array(
136 'library' => esc_html__( 'From Library', 'visualizer' ),
137 'create' => esc_html__( 'Create New', 'visualizer' ),
138 ),
139 'library' => array(
140 'filters' => self::_getChartTypesLocalized( true, true ),
141 'types' => array_keys( self::_getChartTypesLocalized( true, true ) ),
142 ),
143 'nonce' => wp_create_nonce(),
144 'buildurl' => add_query_arg( 'action', Visualizer_Plugin::ACTION_CREATE_CHART, admin_url( 'admin-ajax.php' ) ),
145 );
146
147 return $strings;
148 }
149
150 /**
151 * Returns associated array of chart types and localized names.
152 *
153 * @since 1.0.0
154 *
155 * @static
156 * @access private
157 * @return array The associated array of chart types with localized names.
158 */
159 public static function _getChartTypesLocalized( $enabledOnly = false, $get2Darray = false ) {
160 $types = array(
161 'pie' => array(
162 'name' => esc_html__( 'Pie', 'visualizer' ),
163 'enabled' => true,
164 ),
165 'line' => array(
166 'name' => esc_html__( 'Line', 'visualizer' ),
167 'enabled' => true,
168 ),
169 'area' => array(
170 'name' => esc_html__( 'Area', 'visualizer' ),
171 'enabled' => true,
172 ),
173 'geo' => array(
174 'name' => esc_html__( 'Geo', 'visualizer' ),
175 'enabled' => true,
176 ),
177 'bar' => array(
178 'name' => esc_html__( 'Bar', 'visualizer' ),
179 'enabled' => true,
180 ),
181 'column' => array(
182 'name' => esc_html__( 'Column', 'visualizer' ),
183 'enabled' => true,
184 ),
185 'gauge' => array(
186 'name' => esc_html__( 'Gauge', 'visualizer' ),
187 'enabled' => true,
188 ),
189 'scatter' => array(
190 'name' => esc_html__( 'Scatter', 'visualizer' ),
191 'enabled' => true,
192 ),
193 'candlestick' => array(
194 'name' => esc_html__( 'Candlestick', 'visualizer' ),
195 'enabled' => true,
196 ),
197 // pro types
198 'table' => array(
199 'name' => esc_html__( 'Table', 'visualizer' ),
200 'enabled' => false,
201 ),
202 'timeline' => array(
203 'name' => esc_html__( 'Timeline', 'visualizer' ),
204 'enabled' => false,
205 ),
206 'combo' => array(
207 'name' => esc_html__( 'Combo', 'visualizer' ),
208 'enabled' => false,
209 ),
210 );
211 $types = apply_filters( 'visualizer_pro_chart_types', $types );
212 if ( $enabledOnly ) {
213 $filtered = array();
214 foreach ( $types as $type => $array ) {
215 if ( ! is_array( $array ) ) {
216 // support for old pro
217 $array = array( 'enabled' => true, 'name' => $array );
218 }
219 if ( ! $array['enabled'] ) {
220 continue;
221 }
222 $filtered[ $type ] = $array;
223 }
224 $types = $filtered;
225 }
226 if ( $get2Darray ) {
227 $doubleD = array();
228 foreach ( $types as $type => $array ) {
229 if ( ! is_array( $array ) ) {
230 // support for old pro
231 $array = array( 'enabled' => true, 'name' => $array );
232 }
233 $doubleD[ $type ] = $array['name'];
234 }
235 $types = $doubleD;
236 }
237
238 return $types;
239 }
240
241 /**
242 * Renders templates to use in media popup.
243 *
244 * @since 1.0.0
245 * @global string $pagenow The name of the current page.
246 *
247 * @access public
248 */
249 public function renderTempaltes() {
250 global $pagenow;
251 if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow ) {
252 return;
253 }
254 $render = new Visualizer_Render_Templates();
255 $render->render();
256 }
257
258 /**
259 * Enqueues library scripts and styles.
260 *
261 * @since 1.0.0
262 * @uses wp_enqueue_style() To enqueue library stylesheet.
263 * @uses wp_enqueue_script() To enqueue javascript file.
264 * @uses wp_enqueue_media() To enqueue media stuff.
265 *
266 * @access public
267 *
268 * @param string $suffix The current page suffix.
269 */
270 public function enqueueLibraryScripts( $suffix ) {
271 if ( $suffix == $this->_libraryPage ) {
272 wp_enqueue_style( 'visualizer-library', VISUALIZER_ABSURL . 'css/library.css', array(), Visualizer_Plugin::VERSION );
273 $this->_addFilter( 'media_upload_tabs', 'setupVisualizerTab' );
274 wp_enqueue_media();
275 wp_enqueue_script(
276 'visualizer-library', VISUALIZER_ABSURL . 'js/library.js', array(
277 'jquery',
278 'media-views',
279 ), Visualizer_Plugin::VERSION, true
280 );
281 wp_enqueue_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
282 wp_enqueue_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
283 wp_enqueue_script(
284 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
285 'google-jsapi-old',
286 'visualizer-library',
287 ), Visualizer_Plugin::VERSION, true
288 );
289 }
290 }
291
292 /**
293 * Adds visualizer tab for media upload tabs array.
294 *
295 * @since 1.0.0
296 *
297 * @access public
298 *
299 * @param array $tabs The array of media upload tabs.
300 *
301 * @return array Extended array of media upload tabs.
302 */
303 public function setupVisualizerTab( $tabs ) {
304 $tabs['visualizer'] = 'Visualizer';
305
306 return $tabs;
307 }
308
309 /**
310 * Registers admin menu for visualizer library.
311 *
312 * @since 1.0.0
313 *
314 * @access public
315 */
316 public function registerAdminMenu() {
317 $title = esc_html__( 'Visualizer Library', 'visualizer' );
318 $callback = array( $this, 'renderLibraryPage' );
319 $this->_libraryPage = add_submenu_page( 'upload.php', $title, $title, 'edit_posts', Visualizer_Plugin::NAME, $callback );
320 }
321
322 /**
323 * Renders visualizer library page.
324 *
325 * @since 1.0.0
326 *
327 * @access public
328 */
329 public function renderLibraryPage() {
330 // get current page
331 $page = filter_input(
332 INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array(
333 'options' => array(
334 'min_range' => 1,
335 'default' => 1,
336 ),
337 )
338 );
339 // the initial query arguments to fetch charts
340 $query_args = array(
341 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
342 'posts_per_page' => 6,
343 'paged' => $page,
344 );
345 // add chart type filter to the query arguments
346 $filter = filter_input( INPUT_GET, 'type' );
347 if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
348 $query_args['meta_query'] = array(
349 array(
350 'key' => Visualizer_Plugin::CF_CHART_TYPE,
351 'value' => $filter,
352 'compare' => '=',
353 ),
354 );
355 } else {
356 $filter = 'all';
357 }
358 // Added by Ash/Upwork
359 $filterByMeta = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
360 if ( $filterByMeta ) {
361 $query = array(
362 'key' => Visualizer_Plugin::CF_SETTINGS,
363 'value' => $filterByMeta,
364 'compare' => 'LIKE',
365 );
366 $meta = isset( $query_args['meta_query'] ) ? $query_args['meta_query'] : array();
367 $meta[] = $query;
368 $query_args['meta_query'] = $meta;
369 }
370 // Added by Ash/Upwork
371 // fetch charts
372 $charts = array();
373 $query = new WP_Query( $query_args );
374 while ( $query->have_posts() ) {
375 $chart = $query->next_post();
376 // fetch and update settings
377 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
378 unset( $settings['height'], $settings['width'] );
379 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
380 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
381 $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
382 // add chart to the array
383 $charts[ 'visualizer-' . $chart->ID ] = array(
384 'id' => $chart->ID,
385 'type' => $type,
386 'series' => $series,
387 'settings' => $settings,
388 'data' => $data,
389 );
390 }
391 // enqueue charts array
392 $ajaxurl = admin_url( 'admin-ajax.php' );
393 wp_localize_script(
394 'visualizer-library', 'visualizer', array(
395 'charts' => $charts,
396 'urls' => array(
397 'base' => add_query_arg( 'vpage', false ),
398 'create' => add_query_arg(
399 array(
400 'action' => Visualizer_Plugin::ACTION_CREATE_CHART,
401 'library' => 'yes',
402 ), $ajaxurl
403 ),
404 'edit' => add_query_arg(
405 array(
406 'action' => Visualizer_Plugin::ACTION_EDIT_CHART,
407 'library' => 'yes',
408 ), $ajaxurl
409 ),
410 ),
411 )
412 );
413 // render library page
414 $render = new Visualizer_Render_Library();
415 $render->charts = $charts;
416 $render->type = $filter;
417 $render->types = self::_getChartTypesLocalized();
418 $render->pagination = paginate_links(
419 array(
420 'base' => add_query_arg( 'vpage', '%#%' ),
421 'format' => '',
422 'current' => $page,
423 'total' => $query->max_num_pages,
424 'type' => 'array',
425 )
426 );
427 $render->render();
428 }
429
430 /**
431 * Updates the plugin's action links, which will be rendered at the plugins table.
432 *
433 * @since 1.0.0
434 *
435 * @access public
436 *
437 * @param array $links The array of original action links.
438 * @param string $file The plugin basename.
439 *
440 * @return array Updated array of action links.
441 */
442 public function getPluginActionLinks( $links, $file ) {
443 if ( $file == plugin_basename( VISUALIZER_BASEFILE ) ) {
444 array_unshift(
445 $links,
446 sprintf(
447 '<a href="%s">%s</a>',
448 admin_url( 'upload.php?page=' . Visualizer_Plugin::NAME ),
449 esc_html__( 'Library', 'visualizer' )
450 )
451 );
452 }
453
454 return $links;
455 }
456
457 /**
458 * Updates the plugin's meta links, which will be rendered at the plugins table.
459 *
460 * @since 1.0.0
461 *
462 * @access public
463 *
464 * @param array $plugin_meta The array of a plugin meta links.
465 * @param string $plugin_file The plugin's basename.
466 *
467 * @return array Updated array of plugin meta links.
468 */
469 public function getPluginMetaLinks( $plugin_meta, $plugin_file ) {
470 if ( $plugin_file == plugin_basename( VISUALIZER_BASEFILE ) ) {
471 // knowledge base link
472 $plugin_meta[] = sprintf(
473 '<a href="https://github.com/codeinwp/visualizer/wiki" target="_blank">%s</a>',
474 esc_html__( 'Knowledge Base', 'visualizer' )
475 );
476 // flattr link
477 $plugin_meta[] = sprintf(
478 '<a style="color:red" href="https://themeisle.com/plugins/visualizer-charts-and-graphs-pro-addon/" target="_blank">%s</a>',
479 esc_html__( 'Pro Addon', 'visualizer' )
480 );
481 }
482
483 return $plugin_meta;
484 }
485
486 }
487