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

447 lines 14.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, 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 }
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( 'visualizer-library', VISUALIZER_ABSURL . 'js/library.js', array(
250 'jquery',
251 'media-views',
252 ), Visualizer_Plugin::VERSION, true );
253 wp_enqueue_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
254 wp_enqueue_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
255 wp_enqueue_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
256 'google-jsapi-old',
257 'visualizer-library',
258 ), Visualizer_Plugin::VERSION, true );
259 }
260 }
261
262 /**
263 * Adds visualizer tab for media upload tabs array.
264 *
265 * @since 1.0.0
266 *
267 * @access public
268 *
269 * @param array $tabs The array of media upload tabs.
270 *
271 * @return array Extended array of media upload tabs.
272 */
273 public function setupVisualizerTab( $tabs ) {
274 $tabs['visualizer'] = 'Visualizer';
275
276 return $tabs;
277 }
278
279 /**
280 * Registers admin menu for visualizer library.
281 *
282 * @since 1.0.0
283 *
284 * @access public
285 */
286 public function registerAdminMenu() {
287 $title = esc_html__( 'Visualizer Library', 'visualizer' );
288 $callback = array( $this, 'renderLibraryPage' );
289 $this->_libraryPage = add_submenu_page( 'upload.php', $title, $title, 'edit_posts', Visualizer_Plugin::NAME, $callback );
290 }
291
292 /**
293 * Renders visualizer library page.
294 *
295 * @since 1.0.0
296 *
297 * @access public
298 */
299 public function renderLibraryPage() {
300 // get current page
301 $page = filter_input( INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array(
302 'options' => array(
303 'min_range' => 1,
304 'default' => 1,
305 ),
306 ) );
307 // the initial query arguments to fetch charts
308 $query_args = array(
309 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
310 'posts_per_page' => 6,
311 'paged' => $page,
312 );
313 // add chart type filter to the query arguments
314 $filter = filter_input( INPUT_GET, 'type' );
315 if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
316 $query_args['meta_query'] = array(
317 array(
318 'key' => Visualizer_Plugin::CF_CHART_TYPE,
319 'value' => $filter,
320 'compare' => '=',
321 ),
322 );
323 } else {
324 $filter = 'all';
325 }
326 // Added by Ash/Upwork
327 $filterByMeta = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
328 if ( $filterByMeta ) {
329 $query = array(
330 'key' => Visualizer_Plugin::CF_SETTINGS,
331 'value' => $filterByMeta,
332 'compare' => 'LIKE',
333 );
334 $meta = isset( $query_args['meta_query'] ) ? $query_args['meta_query'] : array();
335 $meta[] = $query;
336 $query_args['meta_query'] = $meta;
337 }
338 // Added by Ash/Upwork
339 // fetch charts
340 $charts = array();
341 $query = new WP_Query( $query_args );
342 while ( $query->have_posts() ) {
343 $chart = $query->next_post();
344 // fetch and update settings
345 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
346 unset( $settings['height'], $settings['width'] );
347 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
348 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
349 $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
350 // add chart to the array
351 $charts[ 'visualizer-' . $chart->ID ] = array(
352 'id' => $chart->ID,
353 'type' => $type,
354 'series' => $series,
355 'settings' => $settings,
356 'data' => $data,
357 );
358 }
359 // enqueue charts array
360 $ajaxurl = admin_url( 'admin-ajax.php' );
361 wp_localize_script( 'visualizer-library', 'visualizer', array(
362 'charts' => $charts,
363 'urls' => array(
364 'base' => add_query_arg( 'vpage', false ),
365 'create' => add_query_arg( array(
366 'action' => Visualizer_Plugin::ACTION_CREATE_CHART,
367 'library' => 'yes',
368 ), $ajaxurl ),
369 'edit' => add_query_arg( array(
370 'action' => Visualizer_Plugin::ACTION_EDIT_CHART,
371 'library' => 'yes',
372 ), $ajaxurl ),
373 ),
374 ) );
375 // render library page
376 $render = new Visualizer_Render_Library();
377 $render->charts = $charts;
378 $render->type = $filter;
379 $render->types = self::_getChartTypesLocalized();
380 $render->pagination = paginate_links( array(
381 'base' => add_query_arg( 'vpage', '%#%' ),
382 'format' => '',
383 'current' => $page,
384 'total' => $query->max_num_pages,
385 'type' => 'array',
386 ) );
387 $render->render();
388 }
389
390 /**
391 * Updates the plugin's action links, which will be rendered at the plugins table.
392 *
393 * @since 1.0.0
394 *
395 * @access public
396 *
397 * @param array $links The array of original action links.
398 * @param string $file The plugin basename.
399 *
400 * @return array Updated array of action links.
401 */
402 public function getPluginActionLinks( $links, $file ) {
403 if ( $file == plugin_basename( VISUALIZER_BASEFILE ) ) {
404 array_unshift(
405 $links,
406 sprintf(
407 '<a href="%s">%s</a>',
408 admin_url( 'upload.php?page=' . Visualizer_Plugin::NAME ),
409 esc_html__( 'Library', 'visualizer' )
410 )
411 );
412 }
413
414 return $links;
415 }
416
417 /**
418 * Updates the plugin's meta links, which will be rendered at the plugins table.
419 *
420 * @since 1.0.0
421 *
422 * @access public
423 *
424 * @param array $plugin_meta The array of a plugin meta links.
425 * @param string $plugin_file The plugin's basename.
426 *
427 * @return array Updated array of plugin meta links.
428 */
429 public function getPluginMetaLinks( $plugin_meta, $plugin_file ) {
430 if ( $plugin_file == plugin_basename( VISUALIZER_BASEFILE ) ) {
431 // knowledge base link
432 $plugin_meta[] = sprintf(
433 '<a href="https://github.com/codeinwp/visualizer/wiki" target="_blank">%s</a>',
434 esc_html__( 'Knowledge Base', 'visualizer' )
435 );
436 // flattr link
437 $plugin_meta[] = sprintf(
438 '<a style="color:red" href="https://themeisle.com/plugins/visualizer-charts-and-graphs-pro-addon/" target="_blank">%s</a>',
439 esc_html__( 'Pro Addon', 'visualizer' )
440 );
441 }
442
443 return $plugin_meta;
444 }
445
446 }
447