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

466 lines 15.1 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( 'plugin_row_meta', 'getPluginMetaLinks', 10, 2 );
62 $this->_addAjaxAction( Visualizer_Plugin::ACTION_TRACK, 'visualizer_enable_track' );
63 }
64
65 /**
66 * Enable track ajax action.
67 */
68 public function visualizer_enable_track() {
69 check_admin_referer( Visualizer_Plugin::ACTION_TRACK, 'nonce' );
70 $status = $_GET['status'];
71 if ( $status == 'yes' ) {
72 update_option( 'visualizer_logger_flag', 'yes' );
73 } else {
74 update_option( 'visualizer_logger_flag', 'no' );
75 }
76 wp_send_json_success( array( 'status' => $status ) );
77 }
78
79 /**
80 * Enqueues media scripts and styles.
81 *
82 * @since 1.0.0
83 * @uses wp_enqueue_style To enqueue style file.
84 * @uses wp_enqueue_script To enqueue script file.
85 *
86 * @access public
87 */
88 public function enqueueMediaScripts() {
89 global $typenow;
90 if ( post_type_supports( $typenow, 'editor' ) ) {
91 wp_enqueue_style( 'visualizer-media', VISUALIZER_ABSURL . 'css/media.css', array( 'media-views' ), Visualizer_Plugin::VERSION );
92 wp_enqueue_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array( 'media-editor' ), null, true );
93 wp_enqueue_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true );
94 wp_enqueue_script( 'visualizer-media-model', VISUALIZER_ABSURL . 'js/media/model.js', array( 'visualizer-google-jsapi-old' ), Visualizer_Plugin::VERSION, true );
95 wp_enqueue_script( 'visualizer-media-collection', VISUALIZER_ABSURL . 'js/media/collection.js', array( 'visualizer-media-model' ), Visualizer_Plugin::VERSION, true );
96 wp_enqueue_script( 'visualizer-media-controller', VISUALIZER_ABSURL . 'js/media/controller.js', array( 'visualizer-media-collection' ), Visualizer_Plugin::VERSION, true );
97 wp_enqueue_script( 'visualizer-media-view', VISUALIZER_ABSURL . 'js/media/view.js', array( 'visualizer-media-controller' ), Visualizer_Plugin::VERSION, true );
98 wp_enqueue_script( 'visualizer-media-toolbar', VISUALIZER_ABSURL . 'js/media/toolbar.js', array( 'visualizer-media-view' ), Visualizer_Plugin::VERSION, true );
99 wp_enqueue_script( 'visualizer-media', VISUALIZER_ABSURL . 'js/media.js', array( 'visualizer-media-toolbar' ), Visualizer_Plugin::VERSION, true );
100 }
101 }
102
103 /**
104 * Extends media view strings with visualizer strings.
105 *
106 * @since 1.0.0
107 *
108 * @access public
109 *
110 * @param array $strings The array of media view strings.
111 *
112 * @return array The extended array of media view strings.
113 */
114 public function setupMediaViewStrings( $strings ) {
115 $strings['visualizer'] = array(
116 'actions' => array(
117 'get_charts' => Visualizer_Plugin::ACTION_GET_CHARTS,
118 'delete_chart' => Visualizer_Plugin::ACTION_DELETE_CHART,
119 ),
120 'controller' => array(
121 'title' => esc_html__( 'Visualizations', 'visualizer' ),
122 ),
123 'routers' => array(
124 'library' => esc_html__( 'From Library', 'visualizer' ),
125 'create' => esc_html__( 'Create New', 'visualizer' ),
126 ),
127 'library' => array(
128 'filters' => self::_getChartTypesLocalized( true, true ),
129 'types' => array_keys( self::_getChartTypesLocalized( true, true ) ),
130 ),
131 'nonce' => wp_create_nonce(),
132 'buildurl' => add_query_arg( 'action', Visualizer_Plugin::ACTION_CREATE_CHART, admin_url( 'admin-ajax.php' ) ),
133 );
134
135 return $strings;
136 }
137
138 /**
139 * Returns associated array of chart types and localized names.
140 *
141 * @since 1.0.0
142 *
143 * @static
144 * @access private
145 * @return array The associated array of chart types with localized names.
146 */
147 public static function _getChartTypesLocalized( $enabledOnly = false, $get2Darray = false ) {
148 $types = array(
149 'pie' => array(
150 'name' => esc_html__( 'Pie', 'visualizer' ),
151 'enabled' => true,
152 ),
153 'line' => array(
154 'name' => esc_html__( 'Line', 'visualizer' ),
155 'enabled' => true,
156 ),
157 'area' => array(
158 'name' => esc_html__( 'Area', 'visualizer' ),
159 'enabled' => true,
160 ),
161 'geo' => array(
162 'name' => esc_html__( 'Geo', 'visualizer' ),
163 'enabled' => true,
164 ),
165 'bar' => array(
166 'name' => esc_html__( 'Bar', 'visualizer' ),
167 'enabled' => true,
168 ),
169 'column' => array(
170 'name' => esc_html__( 'Column', 'visualizer' ),
171 'enabled' => true,
172 ),
173 'gauge' => array(
174 'name' => esc_html__( 'Gauge', 'visualizer' ),
175 'enabled' => true,
176 ),
177 'scatter' => array(
178 'name' => esc_html__( 'Scatter', 'visualizer' ),
179 'enabled' => true,
180 ),
181 'candlestick' => array(
182 'name' => esc_html__( 'Candlestick', 'visualizer' ),
183 'enabled' => true,
184 ),
185 // pro types
186 'table' => array(
187 'name' => esc_html__( 'Table', 'visualizer' ),
188 'enabled' => false,
189 ),
190 'timeline' => array(
191 'name' => esc_html__( 'Timeline', 'visualizer' ),
192 'enabled' => false,
193 ),
194 'combo' => array(
195 'name' => esc_html__( 'Combo', 'visualizer' ),
196 'enabled' => false,
197 ),
198 );
199 $types = apply_filters( 'visualizer_pro_chart_types', $types );
200 if ( $enabledOnly ) {
201 $filtered = array();
202 foreach ( $types as $type => $array ) {
203 if ( ! is_array( $array ) ) {
204 // support for old pro
205 $array = array( 'enabled' => true, 'name' => $array );
206 }
207 if ( ! $array['enabled'] ) {
208 continue;
209 }
210 $filtered[ $type ] = $array;
211 }
212 $types = $filtered;
213 }
214 if ( $get2Darray ) {
215 $doubleD = array();
216 foreach ( $types as $type => $array ) {
217 if ( ! is_array( $array ) ) {
218 // support for old pro
219 $array = array( 'enabled' => true, 'name' => $array );
220 }
221 $doubleD[ $type ] = $array['name'];
222 }
223 $types = $doubleD;
224 }
225
226 return $types;
227 }
228
229 /**
230 * Renders templates to use in media popup.
231 *
232 * @since 1.0.0
233 * @global string $pagenow The name of the current page.
234 *
235 * @access public
236 */
237 public function renderTempaltes() {
238 global $pagenow;
239 if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow ) {
240 return;
241 }
242 $render = new Visualizer_Render_Templates();
243 $render->render();
244 }
245
246 /**
247 * Enqueues library scripts and styles.
248 *
249 * @since 1.0.0
250 * @uses wp_enqueue_style() To enqueue library stylesheet.
251 * @uses wp_enqueue_script() To enqueue javascript file.
252 * @uses wp_enqueue_media() To enqueue media stuff.
253 *
254 * @access public
255 *
256 * @param string $suffix The current page suffix.
257 */
258 public function enqueueLibraryScripts( $suffix ) {
259 if ( $suffix == $this->_libraryPage ) {
260 wp_enqueue_style( 'visualizer-library', VISUALIZER_ABSURL . 'css/library.css', array(), Visualizer_Plugin::VERSION );
261 $this->_addFilter( 'media_upload_tabs', 'setupVisualizerTab' );
262 wp_enqueue_media();
263 wp_enqueue_script( 'visualizer-library', VISUALIZER_ABSURL . 'js/library.js', array(
264 'jquery',
265 'media-views',
266 ), Visualizer_Plugin::VERSION, true );
267 wp_enqueue_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
268 wp_enqueue_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
269 wp_enqueue_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
270 'google-jsapi-old',
271 'visualizer-library',
272 ), Visualizer_Plugin::VERSION, true );
273 }
274 }
275
276 /**
277 * Adds visualizer tab for media upload tabs array.
278 *
279 * @since 1.0.0
280 *
281 * @access public
282 *
283 * @param array $tabs The array of media upload tabs.
284 *
285 * @return array Extended array of media upload tabs.
286 */
287 public function setupVisualizerTab( $tabs ) {
288 $tabs['visualizer'] = 'Visualizer';
289
290 return $tabs;
291 }
292
293 /**
294 * Registers admin menu for visualizer library.
295 *
296 * @since 1.0.0
297 *
298 * @access public
299 */
300 public function registerAdminMenu() {
301 $title = esc_html__( 'Visualizer Library', 'visualizer' );
302 $callback = array( $this, 'renderLibraryPage' );
303 $this->_libraryPage = add_submenu_page( 'upload.php', $title, $title, 'edit_posts', Visualizer_Plugin::NAME, $callback );
304 }
305
306 /**
307 * Renders visualizer library page.
308 *
309 * @since 1.0.0
310 *
311 * @access public
312 */
313 public function renderLibraryPage() {
314 // get current page
315 $page = filter_input( INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array(
316 'options' => array(
317 'min_range' => 1,
318 'default' => 1,
319 ),
320 ) );
321 // the initial query arguments to fetch charts
322 $query_args = array(
323 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
324 'posts_per_page' => 6,
325 'paged' => $page,
326 );
327 // add chart type filter to the query arguments
328 $filter = filter_input( INPUT_GET, 'type' );
329 if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
330 $query_args['meta_query'] = array(
331 array(
332 'key' => Visualizer_Plugin::CF_CHART_TYPE,
333 'value' => $filter,
334 'compare' => '=',
335 ),
336 );
337 } else {
338 $filter = 'all';
339 }
340 // Added by Ash/Upwork
341 $filterByMeta = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
342 if ( $filterByMeta ) {
343 $query = array(
344 'key' => Visualizer_Plugin::CF_SETTINGS,
345 'value' => $filterByMeta,
346 'compare' => 'LIKE',
347 );
348 $meta = isset( $query_args['meta_query'] ) ? $query_args['meta_query'] : array();
349 $meta[] = $query;
350 $query_args['meta_query'] = $meta;
351 }
352 // Added by Ash/Upwork
353 // fetch charts
354 $charts = array();
355 $query = new WP_Query( $query_args );
356 while ( $query->have_posts() ) {
357 $chart = $query->next_post();
358 // fetch and update settings
359 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
360 unset( $settings['height'], $settings['width'] );
361 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
362 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
363 $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
364 // add chart to the array
365 $charts[ 'visualizer-' . $chart->ID ] = array(
366 'id' => $chart->ID,
367 'type' => $type,
368 'series' => $series,
369 'settings' => $settings,
370 'data' => $data,
371 );
372 }
373 // enqueue charts array
374 $ajaxurl = admin_url( 'admin-ajax.php' );
375 wp_localize_script( 'visualizer-library', 'visualizer', array(
376 'charts' => $charts,
377 'urls' => array(
378 'base' => add_query_arg( 'vpage', false ),
379 'create' => add_query_arg( array(
380 'action' => Visualizer_Plugin::ACTION_CREATE_CHART,
381 'library' => 'yes',
382 ), $ajaxurl ),
383 'edit' => add_query_arg( array(
384 'action' => Visualizer_Plugin::ACTION_EDIT_CHART,
385 'library' => 'yes',
386 ), $ajaxurl ),
387 'logger' => add_query_arg( array(
388 'action' => Visualizer_Plugin::ACTION_TRACK,
389 'library' => 'yes',
390 'nonce' => wp_create_nonce( Visualizer_Plugin::ACTION_TRACK ),
391 ), $ajaxurl ),
392 ),
393 ) );
394 // render library page
395 $render = new Visualizer_Render_Library();
396 $render->charts = $charts;
397 $render->type = $filter;
398 $render->types = self::_getChartTypesLocalized();
399 $render->pagination = paginate_links( array(
400 'base' => add_query_arg( 'vpage', '%#%' ),
401 'format' => '',
402 'current' => $page,
403 'total' => $query->max_num_pages,
404 'type' => 'array',
405 ) );
406 $render->render();
407 }
408
409 /**
410 * Updates the plugin's action links, which will be rendered at the plugins table.
411 *
412 * @since 1.0.0
413 *
414 * @access public
415 *
416 * @param array $links The array of original action links.
417 * @param string $file The plugin basename.
418 *
419 * @return array Updated array of action links.
420 */
421 public function getPluginActionLinks( $links, $file ) {
422 if ( $file == plugin_basename( VISUALIZER_BASEFILE ) ) {
423 array_unshift(
424 $links,
425 sprintf(
426 '<a href="%s">%s</a>',
427 admin_url( 'upload.php?page=' . Visualizer_Plugin::NAME ),
428 esc_html__( 'Library', 'visualizer' )
429 )
430 );
431 }
432
433 return $links;
434 }
435
436 /**
437 * Updates the plugin's meta links, which will be rendered at the plugins table.
438 *
439 * @since 1.0.0
440 *
441 * @access public
442 *
443 * @param array $plugin_meta The array of a plugin meta links.
444 * @param string $plugin_file The plugin's basename.
445 *
446 * @return array Updated array of plugin meta links.
447 */
448 public function getPluginMetaLinks( $plugin_meta, $plugin_file ) {
449 if ( $plugin_file == plugin_basename( VISUALIZER_BASEFILE ) ) {
450 // knowledge base link
451 $plugin_meta[] = sprintf(
452 '<a href="https://github.com/codeinwp/visualizer/wiki" target="_blank">%s</a>',
453 esc_html__( 'Knowledge Base', 'visualizer' )
454 );
455 // flattr link
456 $plugin_meta[] = sprintf(
457 '<a style="color:red" href="https://themeisle.com/plugins/visualizer-charts-and-graphs-pro-addon/" target="_blank">%s</a>',
458 esc_html__( 'Pro Addon', 'visualizer' )
459 );
460 }
461
462 return $plugin_meta;
463 }
464
465 }
466