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

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