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

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