PluginProbe
Chartify – WordPress Chart Plugin / trunk
Chartify – WordPress Chart Plugin vtrunk
3.8.0 3.7.9 3.7.8 3.7.7 3.7.6 3.7.5 trunk 1.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 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.1.4 3.1.5 3.1.6 All 83 releases
chart-builder / includes / class-chart-builder-functions.php

class-chart-builder-functions.php in Chartify – WordPress Chart Plugin trunk, at includes/class-chart-builder-functions.php

342 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( !class_exists( 'Chart_Builder_Functions' ) ){
4
5 /**
6 * Class Chart_Builder_Functions
7 * Class contains useful functions that uses in common
8 *
9 *
10 * Hooks used in the class
11 * There are not hooks yet
12 * @hooks @actions
13 * @filters
14 *
15 *
16 * @param $plugin_name
17 *
18 * @since 1.0.0
19 * @package Chart_Builder
20 * @subpackage Chart_Builder/includes
21 * @author Chart Builder Team <info@ays-pro.com>
22 */
23 class Chart_Builder_Functions {
24
25 /**
26 * The ID of this plugin.
27 *
28 * @since 1.0.0
29 * @access private
30 * @var string $plugin_name The ID of this plugin.
31 */
32 private $plugin_name;
33
34 /**
35 * The constructor of the class
36 *
37 * @since 1.0.0
38 * @param $plugin_name
39 */
40 public function __construct( $plugin_name ) {
41 global $wpdb;
42 /**
43 * Assigning $plugin_name to the @plugin_name property
44 */
45 $this->plugin_name = $plugin_name;
46 $this->db_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . "charts";
47 }
48
49
50 /**
51 * Get instance of this class
52 *
53 * @access public
54 * @since 1.0.0
55 * @param $plugin_name
56 * @return Chart_Builder_Functions
57 */
58 public static function get_instance( $plugin_name ){
59 return new self( $plugin_name );
60 }
61
62 /**
63 * Date validation function
64 *
65 * @accept two parameters
66 * @param $date
67 * @param string $format
68 *
69 * @return bool
70 */
71 public function validateDate( $date, $format = 'Y-m-d H:i:s' ){
72 $d = DateTime::createFromFormat($format, $date);
73 return $d && $d->format($format) == $date;
74 }
75
76
77 /**
78 * Version compare function
79 *
80 * @accept two parameters
81 * @param $version1
82 * @param $operator
83 * @param $version2
84 *
85 * @return bool|int
86 */
87 public function versionCompare( $version1, $operator, $version2 ) {
88
89 $_fv = intval ( trim ( str_replace ( '.', '', $version1 ) ) );
90 $_sv = intval ( trim ( str_replace ( '.', '', $version2 ) ) );
91
92 if (strlen ( $_fv ) > strlen ( $_sv )) {
93 $_sv = str_pad ( $_sv, strlen ( $_fv ), 0 );
94 }
95
96 if (strlen ( $_fv ) < strlen ( $_sv )) {
97 $_fv = str_pad ( $_fv, strlen ( $_sv ), 0 );
98 }
99
100 return version_compare ( ( string ) $_fv, ( string ) $_sv, $operator );
101 }
102
103 public function get_all_charts_count() {
104 global $wpdb;
105 $sql = "SELECT COUNT(id) FROM " . $this->db_table . " WHERE `status`='published'";
106 return intval( $wpdb->get_var( $sql ) );
107 }
108
109
110 /**
111 * Gets the properties of the post type
112 *
113 * @access friendly
114 */
115 // public function fetch_post_type_properties() {
116 // $array = $this->get_post_type_properties( $_POST['post_type'] );
117 // $this->_sendResponse(
118 // array(
119 // 'success' => true,
120 // 'fields' => $array,
121 // )
122 // );
123 // wp_die();
124 // }
125
126 /**
127 * Gets the allowed types
128 *
129 * @access private
130 */
131 public function getAllowedTypes() {
132 return array(
133 'string',
134 'number',
135 'boolean',
136 'date',
137 'datetime',
138 'timeofday',
139 );
140 }
141
142 /**
143 * Gets the properties of the post type
144 *
145 * @access private
146 */
147 public function get_post_type_properties( $post_type ) {
148 $array = null;
149
150 $query = new WP_Query(
151 array(
152 'post_type' => $post_type,
153 'no_rows_found' => false,
154 'post_per_page' => 1,
155 'orderby' => 'post_date',
156 'order' => 'DESC',
157 'fields' => 'ids',
158 'update_post_meta_cache' => false,
159 'update_post_term_cache' => false,
160 )
161 );
162
163 $array = array(
164 'post_title',
165 'post_status',
166 'comment_count',
167 'post_date',
168 'post_modified',
169 );
170
171 if ( $query->have_posts() ) {
172 $id = $query->posts[0];
173 $meta = get_post_meta( $id, '', true );
174 foreach ( $meta as $key => $values ) {
175 $array[] = $key;
176 }
177 }
178
179 return $array;
180 }
181
182 /**
183 * Gets all tables and their columns.
184 *
185 * @access public
186 * @return array
187 */
188 public function get_all_db_tables_column_mapping( $chart_id, $use_filter = true ) {
189 $mapping = array();
190 $tables = $this->get_db_tables();
191 foreach ( $tables as $table ) {
192 $cols = $this->get_db_table_columns( $table, true );
193 $names = wp_list_pluck( $cols, 'name' );
194 $mapping[ $table ] = $names;
195 }
196
197 return $mapping;
198 }
199
200 /**
201 * Gets the tables in the database;
202 *
203 * @access public
204 * @return array
205 */
206 public function get_db_tables() {
207 global $wpdb;
208 $tables = get_transient( CHART_BUILDER_DB_PREFIX . 'db_tables' );
209 if ( $tables ) {
210 return $tables;
211 }
212
213 $sql = $wpdb->get_col( 'SHOW TABLES', 0 );
214 foreach ( $sql as $table ) {
215 if ( empty( $prefix ) || 0 === strpos( $table, $wpdb->prefix ) ) {
216 $tables[] = $table;
217 }
218 }
219
220 set_transient( CHART_BUILDER_DB_PREFIX . 'db_tables', $tables, HOUR_IN_SECONDS );
221 return $tables;
222 }
223
224 /**
225 * Gets the column information for the table.
226 *
227 * @param string $table The table.
228 * @param bool $prefix_with_table Whether to prefix column name with the name of the table.
229 * @access private
230 * @return array
231 */
232 private function get_db_table_columns( $table, $prefix_with_table = false ) {
233 global $wpdb;
234 $columns = get_transient( CHART_BUILDER_DB_PREFIX . "db_{$table}_columns" );
235 if ( $columns ) {
236 return $columns;
237 }
238 $columns = array();
239 // @codingStandardsIgnoreStart
240 $rows = $wpdb->get_results( "SHOW COLUMNS IN `$table`", ARRAY_N );
241 // @codingStandardsIgnoreEnd
242 if ( $rows ) {
243 // n => numeric, d => date-ish, s => string-ish.
244 foreach ( $rows as $row ) {
245 $col = ( $prefix_with_table ? "$table." : '' ) . $row[0];
246 $type = $row[1];
247 if ( strpos( $type, 'int' ) !== false || strpos( $type, 'float' ) !== false ) {
248 $type = 'n';
249 } elseif ( strpos( $type, 'date' ) !== false || strpos( $type, 'time' ) !== false ) {
250 $type = 'd';
251 } else {
252 $type = 's';
253 }
254 $columns[] = array( 'name' => $col, 'type' => $type );
255 }
256 }
257
258 set_transient( CHART_BUILDER_DB_PREFIX . "db_{$table}_columns", $columns, DAY_IN_SECONDS );
259 return $columns;
260 }
261
262 /**
263 * Gets the dependent tables and then gets column information for all the tables.
264 *
265 * @param string $table The table.
266 * @access public
267 * @return array
268 */
269 public function get_table_columns( $table ) {
270 $columns = array();
271 if ( ! $table ) {
272 return $columns;
273 }
274
275 $tables = array( $table );
276 $mapping = $this->get_db_table_mapping();
277 if ( array_key_exists( $table, $mapping ) ) {
278 $tables[] = $mapping[ $table ];
279 }
280 foreach ( $tables as $table ) {
281 $cols = $this->get_db_table_columns( $table, count( $tables ) > 1 );
282 $columns = array_merge( $columns, $cols );
283 }
284 return $columns;
285 }
286
287 /**
288 * Gets the relationship between tables in the database.
289 *
290 * @access public
291 * @return array
292 */
293 public function get_db_table_mapping() {
294 global $wpdb;
295 $mapping = get_transient( CHART_BUILDER_DB_PREFIX . 'db_table_mapping' );
296 if ( $mapping ) {
297 return $mapping;
298 }
299 // no need to provide x=>y and then y=>x as we will flip the array shortly.
300 $mapping = array(
301 $wpdb->prefix . 'posts' => $wpdb->prefix . 'postmeta',
302 $wpdb->prefix . 'users' => $wpdb->prefix . 'usermeta',
303 $wpdb->prefix . 'terms' => $wpdb->prefix . 'termmeta',
304 $wpdb->prefix . 'comments' => $wpdb->prefix . 'commentmeta',
305 );
306
307 $mapping += array_flip( $mapping );
308 set_transient( CHART_BUILDER_DB_PREFIX . 'db_table_mapping', $mapping, HOUR_IN_SECONDS );
309 return $mapping;
310 }
311
312 /**
313 * Creates HTML table from passed data
314 *
315 * @access public
316 * @return string
317 */
318 }
319 }
320
321 if( ! function_exists( 'CBFunctions' ) ){
322 /**
323 * Function for quick access to Chart_Builder_Functions class
324 *
325 * @since 1.0.0
326 * @return Chart_Builder_Functions
327 */
328 function CBFunctions(){
329
330 static $instance = null;
331
332 if( $instance === null ){
333 $instance = Chart_Builder_Functions::get_instance( CHART_BUILDER_NAME );
334 }
335
336 if( $instance instanceof Chart_Builder_Functions ){
337 return $instance;
338 }
339
340 return Chart_Builder_Functions::get_instance( CHART_BUILDER_NAME );
341 }
342 }