PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 1.5.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v1.5.3
4.0.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 All 149 releases
← All changes | classes/Visualizer/Module.php +145 -342 3.0.71.5.3 View file →
@@ -1,342 +1,145 @@
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 - * Base class for all modules. Implements routine methods required by all modules.
24 - *
25 - * @category Visualizer
26 - * @package Module
27 - *
28 - * @since 1.0.0
29 - */
30 -class Visualizer_Module {
31 -
32 - /**
33 - * The instance of wpdb class.
34 - *
35 - * @since 1.0.0
36 - *
37 - * @access protected
38 - * @var wpdb
39 - */
40 - protected $_wpdb = null;
41 -
42 - /**
43 - * The plugin instance.
44 - *
45 - * @since 1.0.0
46 - *
47 - * @access protected
48 - * @var Visualizer_Plugin
49 - */
50 - protected $_plugin = null;
51 -
52 - /**
53 - * Constructor.
54 - *
55 - * @since 1.0.0
56 - * @global wpdb $wpdb Current database connection.
57 - *
58 - * @access public
59 - * @param Visualizer_Plugin $plugin The instance of the plugin.
60 - */
61 - public function __construct( Visualizer_Plugin $plugin ) {
62 - global $wpdb;
63 -
64 - $this->_wpdb = $wpdb;
65 - $this->_plugin = $plugin;
66 - }
67 -
68 - /**
69 - * Registers an action hook.
70 - *
71 - * @since 1.0.0
72 - * @uses add_action() To register action hook.
73 - *
74 - * @access protected
75 - * @param string $tag The name of the action to which the $method is hooked.
76 - * @param string $method The name of the method to be called.
77 - * @param bool $methodClass The root of the method.
78 - * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
79 - * @param int $accepted_args optional. The number of arguments the function accept (default 1).
80 - * @return Visualizer_Module
81 - */
82 - protected function _addAction( $tag, $method, $methodClass = null, $priority = 10, $accepted_args = 1 ) {
83 - add_action( $tag, array( $methodClass ? $methodClass : $this, $method ), $priority, $accepted_args );
84 - return $this;
85 - }
86 -
87 - /**
88 - * Registers AJAX action hook.
89 - *
90 - * @since 1.0.0
91 - *
92 - * @access public
93 - * @param string $tag The name of the AJAX action to which the $method is hooked.
94 - * @param string $method Optional. The name of the method to be called. If the name of the method is not provided, tag name will be used as method name.
95 - * @param bool $methodClass The root of the method.
96 - * @param boolean $private Optional. Determines if we should register hook for logged in users.
97 - * @param boolean $public Optional. Determines if we should register hook for not logged in users.
98 - * @return Visualizer_Module
99 - */
100 - protected function _addAjaxAction( $tag, $method = '', $methodClass = null, $private = true, $public = false ) {
101 - if ( $private ) {
102 - $this->_addAction( 'wp_ajax_' . $tag, $method, $methodClass );
103 - }
104 -
105 - if ( $public ) {
106 - $this->_addAction( 'wp_ajax_nopriv_' . $tag, $method, $methodClass );
107 - }
108 -
109 - return $this;
110 - }
111 -
112 - /**
113 - * Registers a filter hook.
114 - *
115 - * @since 1.0.0
116 - * @uses add_filter() To register filter hook.
117 - *
118 - * @access protected
119 - * @param string $tag The name of the filter to hook the $method to.
120 - * @param type $method The name of the method to be called when the filter is applied.
121 - * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
122 - * @param int $accepted_args optional. The number of arguments the function accept (default 1).
123 - * @return Visualizer_Module
124 - */
125 - protected function _addFilter( $tag, $method, $priority = 10, $accepted_args = 1 ) {
126 - add_filter( $tag, array( $this, $method ), $priority, $accepted_args );
127 - return $this;
128 - }
129 -
130 - /**
131 - * Registers a hook for shortcode tag.
132 - *
133 - * @since 1.0.0
134 - * @uses add_shortcode() To register shortcode hook.
135 - *
136 - * @access protected
137 - * @param string $tag Shortcode tag to be searched in post content.
138 - * @param string $method Hook to run when shortcode is found.
139 - * @return Visualizer_Module
140 - */
141 - protected function _addShortcode( $tag, $method ) {
142 - add_shortcode( $tag, array( $this, $method ) );
143 - return $this;
144 - }
145 -
146 - /**
147 - * Extracts the data for a chart and prepares it for the given type.
148 - *
149 - * @access public
150 - * @param int $chart_id The chart id.
151 - * @param string $type The exported type.
152 - */
153 - public function _getDataAs( $chart_id, $type ) {
154 - $final = null;
155 - $success = false;
156 - if ( $chart_id ) {
157 - $chart = get_post( $chart_id );
158 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
159 - }
160 - if ( $success ) {
161 - $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
162 - $rows = array();
163 - $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
164 - $data = unserialize( $chart->post_content );
165 - if ( ! empty( $series ) ) {
166 - $row = array();
167 - foreach ( $series as $array ) {
168 - $row[] = $array['label'];
169 - }
170 - $rows[] = $row;
171 - $row = array();
172 - foreach ( $series as $array ) {
173 - $row[] = $array['type'];
174 - }
175 - $rows[] = $row;
176 - }
177 - if ( ! empty( $data ) ) {
178 - foreach ( $data as $array ) {
179 - // ignore strings
180 - if ( ! is_array( $array ) ) {
181 - continue;
182 - }
183 - // if this is an array of arrays...
184 - if ( is_array( $array[0] ) ) {
185 - foreach ( $array as $arr ) {
186 - $rows[] = $arr;
187 - }
188 - } else {
189 - // just an array
190 - $rows[] = $array;
191 - }
192 - }
193 - }
194 -
195 - $filename = isset( $settings['title'] ) && ! empty( $settings['title'] ) ? $settings['title'] : 'visualizer#' . $chart_id;
196 -
197 - switch ( $type ) {
198 - case 'csv':
199 - $final = $this->_getCSV( $rows, $filename );
200 - break;
201 - case 'xls':
202 - $final = $this->_getExcel( $rows, $filename );
203 - break;
204 - case 'print':
205 - $final = $this->_getHTML( $rows );
206 - break;
207 - }
208 - }
209 - return $final;
210 - }
211 -
212 - /**
213 - * Prepares a CSV.
214 - *
215 - * @access private
216 - * @param array $rows The array of data.
217 - * @param string $filename The name of the file to use.
218 - */
219 - private function _getCSV( $rows, $filename ) {
220 - $filename .= '.csv';
221 -
222 - $fp = tmpfile();
223 - // support for MS Excel
224 - fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) );
225 - foreach ( $rows as $row ) {
226 - fputcsv( $fp, $row );
227 - }
228 - rewind( $fp );
229 - $csv = '';
230 - while ( ( $array = fgetcsv( $fp ) ) !== false ) {
231 - if ( strlen( $csv ) > 0 ) {
232 - $csv .= PHP_EOL;
233 - }
234 - $csv .= implode( ',', $array );
235 - }
236 - fclose( $fp );
237 -
238 - return array(
239 - 'csv' => $csv,
240 - 'name' => $filename,
241 - );
242 - }
243 -
244 - /**
245 - * Prepares an Excel file.
246 - *
247 - * @access private
248 - * @param array $rows The array of data.
249 - * @param string $filename The name of the file to use.
250 - */
251 - private function _getExcel( $rows, $filename ) {
252 - // PHPExcel does not like sheet names longer than 31 characters.
253 - $chart = substr( $filename, 0, 30 );
254 - $filename .= '.xlsx';
255 -
256 - $xlsData = '';
257 - if ( class_exists( 'PHPExcel' ) ) {
258 - $doc = new PHPExcel();
259 - $doc->getActiveSheet()->fromArray( $rows, null, 'A1' );
260 - $doc->getActiveSheet()->setTitle( sanitize_title( $chart ) );
261 - $doc = apply_filters( 'visualizer_excel_doc', $doc );
262 - $writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' );
263 - ob_start();
264 - $writer->save( 'php://output' );
265 - $xlsData = ob_get_contents();
266 - ob_end_clean();
267 - } else {
268 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PHPExcel does not exist!', 'error', __FILE__, __LINE__ );
269 - error_log( 'Class PHPExcel does not exist!' );
270 - }
271 - return array(
272 - 'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ),
273 - 'name' => $filename,
274 - );
275 - }
276 -
277 - /**
278 - * Prepares an HTML table.
279 - *
280 - * @access private
281 - * @param array $rows The array of data.
282 - */
283 - private function _getHTML( $rows ) {
284 - $css = '
285 - table.visualizer-print {
286 - border-collapse: collapse;
287 - }
288 - table.visualizer-print, table.visualizer-print th, table.visualizer-print td {
289 - border: 1px solid #000;
290 - }
291 - ';
292 - $html = '';
293 - $html .= '
294 - <html>
295 - <head>
296 - <style>
297 - ' . apply_filters( 'visualizer_print_css', $css ) . '
298 - </style>
299 - </head>
300 - <body>';
301 -
302 - $table = '<table class="visualizer-print">';
303 - $index = 0;
304 - foreach ( $rows as $row ) {
305 - $table .= '<tr>';
306 - foreach ( $row as $col ) {
307 - if ( $index === 0 ) {
308 - $table .= '<th>' . $col . '</th>';
309 - } else {
310 - $table .= '<td>' . $col . '</td>';
311 - }
312 - }
313 - $table .= '</tr>';
314 - $index++;
315 - }
316 - $table .= '</table>';
317 -
318 - $html .= apply_filters( 'visualizer_print_table', $table ) . '
319 - </body>
320 - </html>';
321 - return array(
322 - 'csv' => $html,
323 - );
324 - }
325 -
326 - /**
327 - * Returns the language of the locale.
328 - *
329 - * @access protected
330 - */
331 - protected function get_language() {
332 - $locale = get_locale();
333 - if ( empty( $locale ) ) {
334 - return '';
335 - }
336 - $array = explode( '_', $locale );
337 - if ( count( $array ) < 2 ) {
338 - return '';
339 - }
340 - return reset( $array );
341 - }
342 -}
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 +/**
24 + * Base class for all modules. Implements routine methods required by all modules.
25 + *
26 + * @category Visualizer
27 + * @package Module
28 + *
29 + * @since 1.0.0
30 + */
31 +class Visualizer_Module {
32 +
33 + /**
34 + * The instance of wpdb class.
35 + *
36 + * @since 1.0.0
37 + *
38 + * @access protected
39 + * @var wpdb
40 + */
41 + protected $_wpdb = null;
42 +
43 + /**
44 + * The plugin instance.
45 + *
46 + * @since 1.0.0
47 + *
48 + * @access protected
49 + * @var Visualizer_Plugin
50 + */
51 + protected $_plugin = null;
52 +
53 + /**
54 + * Constructor.
55 + *
56 + * @since 1.0.0
57 + * @global wpdb $wpdb Current database connection.
58 + *
59 + * @access public
60 + * @param Visualizer_Plugin $plugin The instance of the plugin.
61 + */
62 + public function __construct( Visualizer_Plugin $plugin ) {
63 + global $wpdb;
64 +
65 + $this->_wpdb = $wpdb;
66 + $this->_plugin = $plugin;
67 + }
68 +
69 + /**
70 + * Registers an action hook.
71 + *
72 + * @since 1.0.0
73 + * @uses add_action() To register action hook.
74 + *
75 + * @access protected
76 + * @param string $tag The name of the action to which the $method is hooked.
77 + * @param string $method The name of the method to be called.
78 + * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
79 + * @param int $accepted_args optional. The number of arguments the function accept (default 1).
80 + * @return Visualizer_Module
81 + */
82 + protected function _addAction( $tag, $method, $methodClass=NULL, $priority = 10, $accepted_args = 1 ) {
83 + add_action( $tag, array( $methodClass ? $methodClass : $this, $method ), $priority, $accepted_args );
84 + return $this;
85 + }
86 +
87 + /**
88 + * Registers AJAX action hook.
89 + *
90 + * @since 1.0.0
91 + *
92 + * @access public
93 + * @param string $tag The name of the AJAX action to which the $method is hooked.
94 + * @param string $method Optional. The name of the method to be called. If the name of the method is not provided, tag name will be used as method name.
95 + * @param boolean $private Optional. Determines if we should register hook for logged in users.
96 + * @param boolean $public Optional. Determines if we should register hook for not logged in users.
97 + * @return Visualizer_Module
98 + */
99 + protected function _addAjaxAction( $tag, $method = '', $methodClass=NULL, $private = true, $public = false ) {
100 + if ( $private ) {
101 + $this->_addAction( 'wp_ajax_' . $tag, $method, $methodClass );
102 + }
103 +
104 + if ( $public ) {
105 + $this->_addAction( 'wp_ajax_nopriv_' . $tag, $method, $methodClass );
106 + }
107 +
108 + return $this;
109 + }
110 +
111 + /**
112 + * Registers a filter hook.
113 + *
114 + * @since 1.0.0
115 + * @uses add_filter() To register filter hook.
116 + *
117 + * @access protected
118 + * @param string $tag The name of the filter to hook the $method to.
119 + * @param type $method The name of the method to be called when the filter is applied.
120 + * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
121 + * @param int $accepted_args optional. The number of arguments the function accept (default 1).
122 + * @return Visualizer_Module
123 + */
124 + protected function _addFilter( $tag, $method, $priority = 10, $accepted_args = 1 ) {
125 + add_filter( $tag, array( $this, $method ), $priority, $accepted_args );
126 + return $this;
127 + }
128 +
129 + /**
130 + * Registers a hook for shortcode tag.
131 + *
132 + * @since 1.0.0
133 + * @uses add_shortcode() To register shortcode hook.
134 + *
135 + * @access protected
136 + * @param string $tag Shortcode tag to be searched in post content.
137 + * @param string $method Hook to run when shortcode is found.
138 + * @return Visualizer_Module
139 + */
140 + protected function _addShortcode( $tag, $method ) {
141 + add_shortcode( $tag, array( $this, $method ) );
142 + return $this;
143 + }
144 +
145 +}