PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 2.0.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v2.0.3
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 2.0.3, at classes/Visualizer/Module/Admin.php

478 lines 15.4 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, 9 );
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( 'plugin_row_meta', 'getPluginMetaLinks', 10, 2 );
62 $this->_addFilter( 'visualizer_logger_flag', 'get_logger_flag', 10, 1 );
63 $this->_addAjaxAction( Visualizer_Plugin::ACTION_TRACK, 'visualizer_enable_track' );
64 }
65
66 /**
67 * Enable track ajax action.
68 */
69 public function visualizer_enable_track() {
70 check_admin_referer( Visualizer_Plugin::ACTION_TRACK, 'nonce' );
71 $status = $_GET['status'];
72 if ( $status == 'yes' ) {
73 update_option( 'visualizer_logger_flag', 'yes' );
74 } else {
75 update_option( 'visualizer_logger_flag', 'no' );
76 }
77 wp_send_json_success( array( 'status' => $status ) );
78 }
79
80 /**
81 * Either the tracking is active or not.
82 *
83 * @return bool The flag status.
84 */
85 public function get_logger_flag() {
86 $flag = get_option( 'visualizer_logger_flag', 'no' );
87
88 return ( $flag === 'yes' );
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( 'visualizer-library', VISUALIZER_ABSURL . 'js/library.js', array(
276 'jquery',
277 'media-views',
278 ), Visualizer_Plugin::VERSION, true );
279 wp_enqueue_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
280 wp_enqueue_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
281 wp_enqueue_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
282 'google-jsapi-old',
283 'visualizer-library',
284 ), Visualizer_Plugin::VERSION, true );
285 }
286 }
287
288 /**
289 * Adds visualizer tab for media upload tabs array.
290 *
291 * @since 1.0.0
292 *
293 * @access public
294 *
295 * @param array $tabs The array of media upload tabs.
296 *
297 * @return array Extended array of media upload tabs.
298 */
299 public function setupVisualizerTab( $tabs ) {
300 $tabs['visualizer'] = 'Visualizer';
301
302 return $tabs;
303 }
304
305 /**
306 * Registers admin menu for visualizer library.
307 *
308 * @since 1.0.0
309 *
310 * @access public
311 */
312 public function registerAdminMenu() {
313 $title = esc_html__( 'Visualizer Library', 'visualizer' );
314 $callback = array( $this, 'renderLibraryPage' );
315 $this->_libraryPage = add_submenu_page( 'upload.php', $title, $title, 'edit_posts', Visualizer_Plugin::NAME, $callback );
316 }
317
318 /**
319 * Renders visualizer library page.
320 *
321 * @since 1.0.0
322 *
323 * @access public
324 */
325 public function renderLibraryPage() {
326 // get current page
327 $page = filter_input( INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array(
328 'options' => array(
329 'min_range' => 1,
330 'default' => 1,
331 ),
332 ) );
333 // the initial query arguments to fetch charts
334 $query_args = array(
335 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
336 'posts_per_page' => 6,
337 'paged' => $page,
338 );
339 // add chart type filter to the query arguments
340 $filter = filter_input( INPUT_GET, 'type' );
341 if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
342 $query_args['meta_query'] = array(
343 array(
344 'key' => Visualizer_Plugin::CF_CHART_TYPE,
345 'value' => $filter,
346 'compare' => '=',
347 ),
348 );
349 } else {
350 $filter = 'all';
351 }
352 // Added by Ash/Upwork
353 $filterByMeta = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
354 if ( $filterByMeta ) {
355 $query = array(
356 'key' => Visualizer_Plugin::CF_SETTINGS,
357 'value' => $filterByMeta,
358 'compare' => 'LIKE',
359 );
360 $meta = isset( $query_args['meta_query'] ) ? $query_args['meta_query'] : array();
361 $meta[] = $query;
362 $query_args['meta_query'] = $meta;
363 }
364 // Added by Ash/Upwork
365 // fetch charts
366 $charts = array();
367 $query = new WP_Query( $query_args );
368 while ( $query->have_posts() ) {
369 $chart = $query->next_post();
370 // fetch and update settings
371 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
372 unset( $settings['height'], $settings['width'] );
373 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
374 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
375 $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
376 // add chart to the array
377 $charts[ 'visualizer-' . $chart->ID ] = array(
378 'id' => $chart->ID,
379 'type' => $type,
380 'series' => $series,
381 'settings' => $settings,
382 'data' => $data,
383 );
384 }
385 // enqueue charts array
386 $ajaxurl = admin_url( 'admin-ajax.php' );
387 wp_localize_script( 'visualizer-library', 'visualizer', array(
388 'charts' => $charts,
389 'urls' => array(
390 'base' => add_query_arg( 'vpage', false ),
391 'create' => add_query_arg( array(
392 'action' => Visualizer_Plugin::ACTION_CREATE_CHART,
393 'library' => 'yes',
394 ), $ajaxurl ),
395 'edit' => add_query_arg( array(
396 'action' => Visualizer_Plugin::ACTION_EDIT_CHART,
397 'library' => 'yes',
398 ), $ajaxurl ),
399 'logger' => add_query_arg( array(
400 'action' => Visualizer_Plugin::ACTION_TRACK,
401 'library' => 'yes',
402 'nonce' => wp_create_nonce( Visualizer_Plugin::ACTION_TRACK ),
403 ), $ajaxurl ),
404 ),
405 ) );
406 // render library page
407 $render = new Visualizer_Render_Library();
408 $render->charts = $charts;
409 $render->type = $filter;
410 $render->types = self::_getChartTypesLocalized();
411 $render->pagination = paginate_links( array(
412 'base' => add_query_arg( 'vpage', '%#%' ),
413 'format' => '',
414 'current' => $page,
415 'total' => $query->max_num_pages,
416 'type' => 'array',
417 ) );
418 $render->render();
419 }
420
421 /**
422 * Updates the plugin's action links, which will be rendered at the plugins table.
423 *
424 * @since 1.0.0
425 *
426 * @access public
427 *
428 * @param array $links The array of original action links.
429 * @param string $file The plugin basename.
430 *
431 * @return array Updated array of action links.
432 */
433 public function getPluginActionLinks( $links, $file ) {
434 if ( $file == plugin_basename( VISUALIZER_BASEFILE ) ) {
435 array_unshift(
436 $links,
437 sprintf(
438 '<a href="%s">%s</a>',
439 admin_url( 'upload.php?page=' . Visualizer_Plugin::NAME ),
440 esc_html__( 'Library', 'visualizer' )
441 )
442 );
443 }
444
445 return $links;
446 }
447
448 /**
449 * Updates the plugin's meta links, which will be rendered at the plugins table.
450 *
451 * @since 1.0.0
452 *
453 * @access public
454 *
455 * @param array $plugin_meta The array of a plugin meta links.
456 * @param string $plugin_file The plugin's basename.
457 *
458 * @return array Updated array of plugin meta links.
459 */
460 public function getPluginMetaLinks( $plugin_meta, $plugin_file ) {
461 if ( $plugin_file == plugin_basename( VISUALIZER_BASEFILE ) ) {
462 // knowledge base link
463 $plugin_meta[] = sprintf(
464 '<a href="https://github.com/codeinwp/visualizer/wiki" target="_blank">%s</a>',
465 esc_html__( 'Knowledge Base', 'visualizer' )
466 );
467 // flattr link
468 $plugin_meta[] = sprintf(
469 '<a style="color:red" href="https://themeisle.com/plugins/visualizer-charts-and-graphs-pro-addon/" target="_blank">%s</a>',
470 esc_html__( 'Pro Addon', 'visualizer' )
471 );
472 }
473
474 return $plugin_meta;
475 }
476
477 }
478