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

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