PluginProbe
Chartify – WordPress Chart Plugin / 3.1.1
Chartify – WordPress Chart Plugin v3.1.1
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-db-actions.php

class-chart-builder-db-actions.php in Chartify – WordPress Chart Plugin 3.1.1, at includes/class-chart-builder-db-actions.php

1,254 lines 52.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 global $chart_builder_db_actions_notices;
3 $chart_builder_db_actions_notices = 0;
4
5 if( !class_exists( 'Chart_Builder_DB_Actions' ) ){
6 ob_start();
7
8 /**
9 * Class Chart_Builder_DB_Actions
10 * Class contains functions to interact with chart database
11 *
12 * Main functionality belong to inserting, updating and deleting of
13 * Also chart settings and options
14 *
15 * Hooks used in the class
16 * @hooks @filters ays_chart_item_save_options
17 * ays_chart_item_save_settings
18 *
19 * Database tables without prefixes
20 * @tables charts
21 * charts_meta
22 *
23 * @param $plugin_name
24 *
25 * @since 1.0.0
26 * @package Chart_Builder
27 * @subpackage Chart_Builder/includes
28 * @author Chart Builder Team <info@ays-pro.com>
29 */
30 class Chart_Builder_DB_Actions {
31
32 /**
33 * The ID of this plugin.
34 *
35 * @since 1.0.0
36 * @access private
37 * @var string $plugin_name The ID of this plugin.
38 */
39 private $plugin_name;
40
41 /**
42 * The name of table in the database.
43 *
44 * @since 1.0.0
45 * @access private
46 * @var string $db_table The name of database table.
47 */
48 private $db_table;
49
50 /**
51 * The name of meta table in the database.
52 *
53 * @since 1.0.0
54 * @access private
55 * @var string $db_table_meta The name of database table.
56 */
57 private $db_table_meta;
58
59 /**
60 * The constructor of the class
61 *
62 * @since 1.0.0
63 * @access public
64 *
65 * @param $plugin_name
66 */
67 public function __construct( $plugin_name ) {
68
69 global $wpdb;
70 global $chart_builder_db_actions_notices;
71
72 /**
73 * Assigning $plugin_name to the @plugin_name property
74 */
75 $this->plugin_name = $plugin_name;
76
77 /**
78 * Assigning database @charts table full name to the @db_table property
79 */
80 $this->db_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . "charts";
81
82 /**
83 * Assigning database @charts metas table full name to the @db_table_meta property
84 */
85 $this->db_table_meta = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . "charts_meta";
86
87
88 /**
89 * Adding action to admin_notices hook
90 * Will work when there is some notice after some action
91 */
92 if( $chart_builder_db_actions_notices === 0 ) {
93 add_action( 'admin_notices', array( $this, 'chart_notices' ) );
94 $chart_builder_db_actions_notices++;
95 }
96
97 }
98
99 /**
100 * Get instance of this class
101 *
102 * @since 1.0.0
103 * @access public
104 *
105 * @param $plugin_name
106 *
107 * @return Chart_Builder_DB_Actions
108 */
109 public static function get_instance( $plugin_name ){
110 return new self( $plugin_name );
111 }
112
113 /**
114 * Get records form database
115 * Applying filters like per page and ordering
116 *
117 * @since 1.0.0
118 * @access public
119 *
120 * @return array
121 */
122 public function get_items(){
123 global $wpdb;
124
125 $per_page = $this->get_pagination_count();
126
127 $page_number = 1;
128 if ( ! empty( $_REQUEST['paged'] ) ) {
129 $page_number = absint( sanitize_text_field( $_REQUEST['paged'] ) );
130 }
131
132 $sql = "SELECT * FROM " . $this->db_table;
133
134 $sql .= self::get_where_condition();
135
136 if ( ! empty( $_REQUEST['orderby'] ) ) {
137 $order_by = ( isset( $_REQUEST['orderby'] ) && sanitize_text_field( $_REQUEST['orderby'] ) != '' ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id';
138 $order_by .= ( ! empty( $_REQUEST['order'] ) && strtolower( $_REQUEST['order'] ) == 'asc' ) ? ' ASC' : ' DESC';
139
140 $sql_orderby = sanitize_sql_orderby( $order_by );
141
142 if ( $sql_orderby ) {
143 $sql .= ' ORDER BY ' . $sql_orderby;
144 } else {
145 $sql .= ' ORDER BY id DESC';
146 }
147 }else{
148 $sql .= ' ORDER BY id DESC';
149 }
150
151 $p_page = ($page_number - 1) * $per_page;
152 $sql .= " LIMIT " . $per_page;
153 $sql .= " OFFSET " . $p_page;
154 $result = $wpdb->get_results( $sql, 'ARRAY_A' );
155
156 return $result;
157 }
158
159 /**
160 * @return mixed
161 */
162 public function get_pagination_count(){
163 $per_page = get_user_meta( get_current_user_id(), 'cb_charts_per_page', true );
164 if( $per_page == '' ){
165 $per_page = 5;
166 }
167 $per_page = absint( $per_page );
168 return $per_page;
169 }
170
171 /**
172 * Get WHERE condition for SQL queries that trying to get records
173 *
174 * @since 1.0.0
175 * @access public
176 *
177 * @return string
178 */
179 public static function get_where_condition(){
180 global $wpdb;
181 $chart_types = array(
182 "line_chart",
183 "bar_chart",
184 "pie_chart",
185 "column_chart",
186 "org_chart",
187 "donut_chart",
188 );
189 $chart_sources = array(
190 "manual",
191 "quiz_maker",
192 "file_import",
193 "google_sheet",
194 "import_from_db",
195 "woocommerce_data",
196 );
197 $chart_source_types = array(
198 "google-charts",
199 "chart-js",
200 );
201 $chart_dates = array(
202 'today' => "",
203 'yesterday' => "day",
204 'last_week' => "week",
205 'last_month' => "month",
206 'last_year' => "year",
207 );
208 $where = array();
209 $sql = '';
210
211 $search = (isset($_REQUEST['s'])) ? sanitize_text_field($_REQUEST['s']) : false;
212 if ($search) {
213 $s = array();
214 $s[] = sprintf( "`title` LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $search ) ) );
215 $where[] = ' ( ' . implode(' OR ', $s) . ' ) ';
216 }
217
218 // if (isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '') {
219 // $where[] = ' `status` = "' . esc_sql( sanitize_text_field( $_GET['fstatus'] ) ) . '" ';
220 // } else {
221 // $where[] = ' `status` != "trashed" ';
222 // }
223 if( isset( $_REQUEST['filterbytype'] ) && absint( sanitize_text_field( $_REQUEST['filterbytype'] ) ) > 0){
224 $key = intval( sanitize_text_field( $_REQUEST['filterbytype'] ) );
225 $where[] = ' `source_chart_type` = "'. $chart_types[$key-1] .'" ';
226 }
227
228 if( isset( $_REQUEST['filterbysource'] ) && absint( sanitize_text_field( $_REQUEST['filterbysource'] ) ) > 0){
229 $key = intval( sanitize_text_field( $_REQUEST['filterbysource'] ) );
230 $where[] = ' `source_type` = "'. $chart_sources[$key-1] .'" ';
231 }
232
233 if( isset( $_REQUEST['filterbychartsource'] ) && absint( sanitize_text_field( $_REQUEST['filterbychartsource'] ) ) > 0){
234 $key = intval( sanitize_text_field( $_REQUEST['filterbychartsource'] ) );
235 $where[] = ' `type` = "'. $chart_source_types[$key-1] .'" ';
236 }
237
238 if( isset( $_REQUEST['filterbydate'] ) && sanitize_text_field( $_REQUEST['filterbydate'] ) !== ''){
239 $interval = sanitize_text_field( $_REQUEST['filterbydate'] );
240 if ($chart_dates[$interval] !== '') {
241 $where[] = ' DATE(date_created) >= DATE_SUB(CURDATE(), INTERVAL 1 '. strtoupper($chart_dates[$interval]) .') AND DATE(date_created) < CURDATE() ';
242 } else{
243 $where[] = ' DATE(date_created) = CURDATE() ';
244 }
245 }
246
247 if( isset( $_REQUEST['filterbyauthor'] ) && absint( sanitize_text_field( $_REQUEST['filterbyauthor'] ) ) > 0){
248 $where[] = ' `author_id` = "'. absint(sanitize_text_field($_REQUEST['filterbyauthor'])) .'" ';
249 }
250
251 if (!empty($where)) {
252 $sql = " WHERE " . implode( " AND ", $where );
253 }
254
255 return $sql;
256 }
257
258 public static function get_search_value () {
259 $search = (isset($_REQUEST['s'])) ? sanitize_text_field($_REQUEST['s']) : '';
260 return $search;
261 }
262
263 public static function get_searched_author_info () {
264 $id = (isset($_REQUEST['filterbyauthor'])) ? absint(sanitize_text_field($_REQUEST['filterbyauthor'])) : 0;
265 $author_data = array();
266 if ( $id && $id > 0 ) {
267 global $wpdb;
268 $users_table = esc_sql( $wpdb->prefix . 'users' );
269 $sql_users = "SELECT ID, display_name FROM {$users_table} WHERE ID = {$id}";
270
271 $author_data = $wpdb->get_row($sql_users, "ARRAY_A");
272 }
273
274 return $author_data;
275 }
276
277 /**
278 * Get record by id
279 *
280 * @since 1.0.0
281 * @access public
282 *
283 * @param $id
284 *
285 * @return array|false
286 */
287 public function get_item( $id ){
288 global $wpdb;
289
290 if( is_null( $id ) ){
291 return false;
292 }
293
294 $sql = "SELECT * FROM ". $this->db_table ." WHERE id = '". $id ."'";
295 $result = $wpdb->get_row( $sql, ARRAY_A );
296
297 if( $result ){
298 return $result;
299 }
300
301 return false;
302 }
303
304 /**
305 * Insert or update record by id
306 *
307 * @since 1.0.0
308 * @access public
309 *
310 * @redirect to specific page based on clicked button
311 * @param $id
312 *
313 * @return false|void
314 */
315 public function add_or_edit_item( $id ){
316 global $wpdb;
317
318 if( is_null( $id ) ){
319 return false;
320 }
321
322 if( isset( $_POST["chart_builder_action"] ) && wp_verify_nonce( $_POST["chart_builder_action"], 'chart_builder_action' ) ){
323 $success = 0;
324 $name_prefix = 'ays_';
325
326 // Save type
327 $save_type = isset( $_POST['save_type'] ) && $_POST['save_type'] != '' ? sanitize_text_field( $_POST['save_type'] ) : '';
328
329 // Author_id
330 $author_id = get_current_user_id();
331
332 // Title
333 $title = isset( $_POST[ $name_prefix . 'title' ] ) && $_POST[ $name_prefix . 'title' ] != '' ? stripslashes( sanitize_text_field( $_POST[ $name_prefix . 'title' ] ) ) : 'Untitled chart';
334
335 // if( $title == '' ){
336 // $message = 'empty-title';
337 // $url = esc_url_raw( remove_query_arg( false ) );
338 // $url = esc_url_raw( add_query_arg( array(
339 // 'status' => 'empty-title'
340 // ), $url ) );
341 // wp_redirect( $url );
342 // exit();
343 // }
344
345
346 // Description
347 $description = isset( $_POST[ $name_prefix . 'description' ] ) && $_POST[ $name_prefix . 'description' ] != '' ? stripslashes( sanitize_text_field($_POST[ $name_prefix . 'description' ]) ) : '';
348
349 // Type
350 $type = isset( $_POST[ $name_prefix . 'type' ] ) && $_POST[ $name_prefix . 'type' ] != '' ? sanitize_text_field( $_POST[ $name_prefix . 'type' ] ) : 'google-charts';
351
352 // Source chart type
353 $source_chart_type = isset( $_POST[ $name_prefix . 'source_chart_type' ] ) && $_POST[ $name_prefix . 'source_chart_type' ] != '' ? sanitize_text_field( $_POST[ $name_prefix . 'source_chart_type' ] ) : 'pie_chart';
354
355 // Source type
356 $source_type = isset( $_POST[ $name_prefix . 'source_type' ] ) && $_POST[ $name_prefix . 'source_type' ] != '' ? sanitize_text_field( $_POST[ $name_prefix . 'source_type' ] ) : 'manual';
357
358 // Manual data
359 $chart_source_filtered_data = array();
360 if ($source_chart_type == "org_chart") {
361 $chart_source_data_add = isset($_POST[ $name_prefix . 'chart_source_data_org_type' ]) && !empty( $_POST[ $name_prefix . 'chart_source_data_org_type' ] ) ? $_POST[ $name_prefix . 'chart_source_data_org_type' ] : array();
362 foreach($chart_source_data_add as $chart_source_data_key => $chart_source_data_value){
363 $chart_source_data_key = (int)filter_var($chart_source_data_key, FILTER_SANITIZE_NUMBER_INT);
364 foreach($chart_source_data_value as $s_data_key => $s_data_value){
365 if ($s_data_key === 5) {
366 $chart_source_filtered_data[$chart_source_data_key][] = (isset($s_data_value) && $s_data_value != '') ? esc_url( $s_data_value ) : '';
367 } else {
368 $chart_source_filtered_data[$chart_source_data_key][] = (isset($s_data_value) && $s_data_value != '') ? esc_attr(stripslashes( sanitize_text_field( $s_data_value ) )) : '';
369 }
370 }
371 }
372 } else {
373 $chart_source_data_add = isset($_POST[ $name_prefix . 'chart_source_data' ]) && !empty( $_POST[ $name_prefix . 'chart_source_data' ] ) ? $_POST[ $name_prefix . 'chart_source_data' ] : array();
374 foreach($chart_source_data_add as $chart_source_data_key => $chart_source_data_value){
375 if ($chart_source_data_key == 0) {
376 if(!empty($chart_source_data_value)){
377 foreach($chart_source_data_value as $s_data_key => $s_data_value){
378 $chart_source_filtered_data[$chart_source_data_key][] = (isset($s_data_value) && trim(esc_attr(stripslashes( sanitize_text_field( $s_data_value ) ))) != '') ? esc_attr(stripslashes( sanitize_text_field( $s_data_value ) )) : 'Title '.$s_data_key;
379 }
380 }
381 } else {
382 if(!empty($chart_source_data_value) && (isset($chart_source_data_value[0]) && trim($chart_source_data_value[0]) != '')){
383 foreach($chart_source_data_value as $s_data_key => $s_data_value){
384 if ($s_data_key === 0) {
385 $chart_source_filtered_data[$chart_source_data_key][] = (isset($s_data_value) && esc_attr(stripslashes( sanitize_text_field( $s_data_value ) )) != '') ? esc_attr(stripslashes( sanitize_text_field( $s_data_value ) )) : 'Option';
386 } else {
387 $chart_source_filtered_data[$chart_source_data_key][] = (isset($s_data_value) && esc_attr(stripslashes( sanitize_text_field( $s_data_value ) )) != '') ? esc_attr(stripslashes( sanitize_text_field( $s_data_value ) )) : '0';
388 }
389 }
390 }
391 }
392 }
393 }
394 $chart_source_filtered_data = json_encode( $chart_source_filtered_data );
395
396 // Source type import from quiz maker database
397 $quiz_maker_data_option_name_for_quiz = $id == 0 ? 'ays_chart_quiz_maker_quiz_data_temp' : 'ays_chart_quiz_maker_quiz_data_' . $id;
398 $quiz_data = get_option( $quiz_maker_data_option_name_for_quiz );
399
400 $quiz_maker_data_option_name = $id == 0 ? 'ays_chart_quiz_maker_results_temp' : 'ays_chart_quiz_maker_results_' . $id;
401 $quiz_maker_data = get_option( $quiz_maker_data_option_name, array() );
402 $query_id = isset( $_POST[ $name_prefix . 'quiz_query' ] ) && $_POST[ $name_prefix . 'quiz_query' ] != '' ? sanitize_text_field( $_POST[ $name_prefix . 'quiz_query' ] ) : '';
403 $quiz_id = isset( $_POST[ $name_prefix . 'quiz_id' ] ) && $_POST[ $name_prefix . 'quiz_id' ] != '' ? intval( $_POST[ $name_prefix . 'quiz_id' ] ) : 0;
404
405 // Source
406 switch ( $source_type ){
407 case 'quiz_maker':
408 $query_id_op = sanitize_text_field($quiz_data['quiz_query']);
409 $quiz_id_op = intval($quiz_data['quiz_id']);
410
411 $chart_id = $id;
412 $user_id = get_current_user_id();
413
414 if ( !($query_id_op == $query_id && $quiz_id_op == $quiz_id) ) {
415 if ( $chart_id >= 0 ) {
416 $return_results = CBFunctions()->get_quiz_query($query_id, $quiz_id, $user_id);
417 $result_values = $return_results['result'];
418 $query = $return_results['query'];
419
420 if ( !empty($result_values) ) {
421 $results = array();
422 $headers = array();
423 if ( $result_values ) {
424 $row_num = 0;
425 foreach ( $result_values as $row ) {
426 $result = array();
427 $col_num = 0;
428 foreach ( $row as $k => $v ) {
429 $result[] = $v;
430 if ( $row_num === 0 ) {
431 $headers[] = $k;
432 }
433 }
434 $results[] = $result;
435 $row_num++;
436 }
437 }
438 }
439
440 $source_type = 'quiz_maker';
441 $option_name_for_data = $chart_id == 0 ? 'ays_chart_quiz_maker_results_temp' : 'ays_chart_quiz_maker_results_' . $chart_id;
442 $option_name_for_quiz = $chart_id == 0 ? 'ays_chart_quiz_maker_quiz_data_temp' : 'ays_chart_quiz_maker_quiz_data_' . $chart_id;
443
444 $source = $query;
445
446 $quiz_maker_data = array(
447 'source_type' => $source_type,
448 'source' => $query,
449 'data' => $results,
450 );
451 $quiz_data = array(
452 'quiz_query' => $query_id,
453 'quiz_id' => $quiz_id
454 );
455
456 update_option( $option_name_for_data, $quiz_maker_data);
457 update_option( $option_name_for_quiz, $quiz_data);
458 }
459 } else {
460 $source = isset( $quiz_maker_data['source'] ) && $quiz_maker_data['source'] !== '' ? $quiz_maker_data['source'] : '';
461
462 $query_id = $query_id_op;
463 $quiz_id = $quiz_id_op;
464 }
465 break;
466 case 'manual':
467 default:
468 $source = $chart_source_filtered_data;
469 break;
470 }
471
472 // Status
473 $status = isset( $_POST[ $name_prefix . 'status' ] ) && $_POST[ $name_prefix . 'status' ] != '' ? sanitize_text_field( $_POST[ $name_prefix . 'status' ] ) : 'draft';
474
475 // Date created
476 $date_created = isset( $_POST[ $name_prefix . 'date_created' ] ) && CBFunctions()->validateDate( $_POST[ $name_prefix . 'date_created' ] ) ? sanitize_text_field($_POST[ $name_prefix . 'date_created' ]) : current_time( 'mysql' );
477
478 // Date modified
479 $date_modified = isset( $_POST[ $name_prefix . 'date_modified' ] ) && CBFunctions()->validateDate( $_POST[ $name_prefix . 'date_modified' ] ) ? sanitize_text_field($_POST[ $name_prefix . 'date_modified' ]) : current_time( 'mysql' );
480
481 // Change the author of the current chart
482 $create_author = ( isset($_POST[$name_prefix . 'create_author']) && $_POST[$name_prefix . 'create_author'] != "" ) ? absint( sanitize_text_field( $_POST[$name_prefix . 'create_author'] ) ) : '';
483
484 if ( $create_author != "" && $create_author > 0 ) {
485 $user = get_userdata($create_author);
486 if ( ! is_null( $user ) && $user ) {
487 $author = array(
488 'id' => $user->ID."",
489 'name' => $user->data->display_name
490 );
491
492 $author = json_encode($author, JSON_UNESCAPED_SLASHES);
493 } else {
494 $author_data = json_decode($create_author, true);
495 $create_author = (isset( $author_data['id'] ) && $author_data['id'] != "") ? absint( sanitize_text_field( $author_data['id'] ) ) : get_current_user_id();
496 }
497
498 $author_id = $create_author;
499 }
500
501 // Options
502 $options = array();
503 if( isset( $_POST[ $name_prefix . 'options' ] ) && !empty( $_POST[ $name_prefix . 'options' ] )) {
504 foreach($_POST[ $name_prefix . 'options' ] as $each_option_key => $each_option_value){
505 $each_option_value = isset($each_option_value) && $each_option_value != '' ? sanitize_text_field($each_option_value) : '';
506 $each_option_key = isset($each_option_key) && $each_option_key != '' ? sanitize_text_field($each_option_key) : '';
507 $options[$each_option_key] = sanitize_text_field($each_option_value);
508 }
509 }
510
511 $options = apply_filters( 'ays_chart_item_save_options', $options );
512
513 // Settings
514 // == Sanitize in the loop all values except checkboxes, radios (only for settings array). The latter sanitize separately (out of loop) ==
515 $settings = array();
516 if( isset( $_POST[ $name_prefix . 'settings' ] ) && !empty( $_POST[ $name_prefix . 'settings' ] )) {
517 foreach($_POST[ $name_prefix . 'settings' ] as $each_setting_key => $each_setting_value){
518 if (!is_array($each_setting_value)) {
519 $each_setting_value = isset($each_setting_value) && $each_setting_value != '' ? esc_attr(stripslashes(sanitize_text_field($each_setting_value))) : '';
520 $each_setting_key = isset($each_setting_key) && $each_setting_key != '' ? esc_attr(stripslashes(sanitize_text_field($each_setting_key))) : '';
521 $settings[$each_setting_key] = $each_setting_value;
522 } else {
523 foreach($each_setting_value as $each_index => $each_value){
524 $each_value = isset($each_value) && $each_value != '' ? esc_attr(stripslashes(sanitize_text_field($each_value))) : '';
525 $each_index = isset($each_index) && $each_index >= 0 ? intval($each_index) : -1;
526 $settings[$each_setting_key][$each_index] = $each_value;
527 }
528 $settings[$each_setting_key] = json_encode($settings[$each_setting_key]);
529 }
530 }
531 }
532
533 // == Sanitize checkboxes, radios here (only for settings array) ==
534 $settings['responsive_width'] = ( isset( $settings['responsive_width'] ) && $settings['responsive_width'] != '' ) ? sanitize_text_field($settings['responsive_width']) : 'off';
535 $settings['transparent_background'] = ( isset( $settings['transparent_background'] ) && $settings['transparent_background'] != '' ) ? sanitize_text_field($settings['transparent_background']) : 'off';
536 $settings['box_shadow'] = ( isset( $settings['box_shadow'] ) && $settings['box_shadow'] != '' ) ? sanitize_text_field($settings['box_shadow']) : 'off';
537 $settings['show_color_code'] = ( isset( $settings['show_color_code'] ) && $settings['show_color_code'] != '' ) ? sanitize_text_field($settings['show_color_code']) : 'off';
538 $settings['tooltip_italic'] = ( isset( $settings['tooltip_italic'] ) && $settings['tooltip_italic'] != '' ) ? sanitize_text_field($settings['tooltip_italic']) : 'off';
539 $settings['legend_italic'] = ( isset( $settings['legend_italic'] ) && $settings['legend_italic'] != '' ) ? sanitize_text_field($settings['legend_italic']) : 'off';
540 $settings['legend_bold'] = ( isset( $settings['legend_bold'] ) && $settings['legend_bold'] != '' ) ? sanitize_text_field($settings['legend_bold']) : 'off';
541 $settings['haxis_italic'] = ( isset( $settings['haxis_italic'] ) && $settings['haxis_italic'] != '' ) ? sanitize_text_field($settings['haxis_italic']) : 'off';
542 $settings['haxis_bold'] = ( isset( $settings['haxis_bold'] ) && $settings['haxis_bold'] != '' ) ? sanitize_text_field($settings['haxis_bold']) : 'off';
543 $settings['vaxis_italic'] = ( isset( $settings['vaxis_italic'] ) && $settings['vaxis_italic'] != '' ) ? sanitize_text_field($settings['vaxis_italic']) : 'off';
544 $settings['vaxis_bold'] = ( isset( $settings['vaxis_bold'] ) && $settings['vaxis_bold'] != '' ) ? sanitize_text_field($settings['vaxis_bold']) : 'off';
545 $settings['reverse_categories'] = ( isset( $settings['reverse_categories'] ) && $settings['reverse_categories'] != '' ) ? sanitize_text_field($settings['reverse_categories']) : 'off';
546 $settings['haxis_title_italic'] = ( isset( $settings['haxis_title_italic'] ) && $settings['haxis_title_italic'] != '' ) ? sanitize_text_field($settings['haxis_title_italic']) : 'off';
547 $settings['haxis_title_bold'] = ( isset( $settings['haxis_title_bold'] ) && $settings['haxis_title_bold'] != '' ) ? sanitize_text_field($settings['haxis_title_bold']) : 'off';
548 $settings['vaxis_title_italic'] = ( isset( $settings['vaxis_title_italic'] ) && $settings['vaxis_title_italic'] != '' ) ? sanitize_text_field($settings['vaxis_title_italic']) : 'off';
549 $settings['vaxis_title_bold'] = ( isset( $settings['vaxis_title_bold'] ) && $settings['vaxis_title_bold'] != '' ) ? sanitize_text_field($settings['vaxis_title_bold']) : 'off';
550 $settings['enable_row_settings'] = ( isset( $settings['enable_row_settings'] ) && $settings['enable_row_settings'] != '' ) ? sanitize_text_field($settings['enable_row_settings']) : 'off';
551 $settings['is_stacked'] = ( isset( $settings['is_stacked'] ) && $settings['is_stacked'] != '' ) ? sanitize_text_field($settings['is_stacked']) : 'off';
552 $settings['multiple_selection'] = ( isset( $settings['multiple_selection'] ) && $settings['multiple_selection'] != '' ) ? sanitize_text_field($settings['multiple_selection']) : 'off';
553 $settings['show_title'] = ( isset( $settings['show_title'] ) && $settings['show_title'] != '' ) ? sanitize_text_field($settings['show_title']) : 'off';
554 $settings['title_bold'] = ( isset( $settings['title_bold'] ) && $settings['title_bold'] != '' ) ? sanitize_text_field($settings['title_bold']) : 'off';
555 $settings['title_text_shadow'] = ( isset( $settings['title_text_shadow'] ) && $settings['title_text_shadow'] != '' ) ? sanitize_text_field($settings['title_text_shadow']) : 'off';
556 $settings['title_italic'] = ( isset( $settings['title_italic'] ) && $settings['title_italic'] != '' ) ? sanitize_text_field($settings['title_italic']) : 'off';
557 $settings['show_description'] = ( isset( $settings['show_description'] ) && $settings['show_description'] != '' ) ? sanitize_text_field($settings['show_description']) : 'off';
558 $settings['description_bold'] = ( isset( $settings['description_bold'] ) && $settings['description_bold'] != '' ) ? sanitize_text_field($settings['description_bold']) : 'off';
559 $settings['description_italic'] = ( isset( $settings['description_italic'] ) && $settings['description_italic'] != '' ) ? sanitize_text_field($settings['description_italic']) : 'off';
560 $settings['description_text_shadow'] = ( isset( $settings['description_text_shadow'] ) && $settings['description_text_shadow'] != '' ) ? sanitize_text_field($settings['description_text_shadow']) : 'off';
561 $settings['enable_interactivity'] = ( isset( $settings['enable_interactivity'] ) && $settings['enable_interactivity'] != '' ) ? sanitize_text_field($settings['enable_interactivity']) : 'off';
562 $settings['maximized_view'] = ( isset( $settings['maximized_view'] ) && $settings['maximized_view'] != '' ) ? sanitize_text_field($settings['maximized_view']) : 'off';
563 $settings['haxis_direction'] = ( isset( $settings['haxis_direction'] ) && $settings['haxis_direction'] != '' ) ? sanitize_text_field($settings['haxis_direction']) : '1';
564 $settings['vaxis_direction'] = ( isset( $settings['vaxis_direction'] ) && $settings['vaxis_direction'] != '' ) ? sanitize_text_field($settings['vaxis_direction']) : '1';
565 $settings['haxis_slanted_text_angle'] = ( isset( $settings['haxis_slanted_text_angle'] ) && $settings['haxis_slanted_text_angle'] != '0' ) ? sanitize_text_field($settings['haxis_slanted_text_angle']) : '30';
566 $settings['enable_animation'] = ( isset( $settings['enable_animation'] ) && $settings['enable_animation'] != '' ) ? sanitize_text_field($settings['enable_animation']) : 'off';
567 $settings['animation_startup'] = ( isset( $settings['animation_startup'] ) && $settings['animation_startup'] != '' ) ? sanitize_text_field($settings['animation_startup']) : 'off';
568 $settings['orientation'] = ( isset( $settings['orientation'] ) && $settings['orientation'] != '' ) ? sanitize_text_field($settings['orientation']) : 'off';
569 $settings['fill_nulls'] = ( isset( $settings['fill_nulls'] ) && $settings['fill_nulls'] != '' ) ? sanitize_text_field($settings['fill_nulls']) : 'off';
570 $settings['allow_collapse'] = ( isset( $settings['allow_collapse'] ) && $settings['allow_collapse'] != '' ) ? sanitize_text_field($settings['allow_collapse']) : 'off';
571 $settings['enable_img'] = ( isset( $settings['enable_img'] ) && $settings['enable_img'] != '' ) ? sanitize_text_field($settings['enable_img']) : 'off';
572 // $settings['index_axis'] = ( isset( $settings['index_axis'] ) && $settings['index_axis'] != '' ) ? sanitize_text_field($settings['index_axis']) : 'off';
573
574 $settings = apply_filters( 'ays_chart_item_save_settings', $settings );
575
576 $message = '';
577 if( $id == 0 ){
578 $result = $wpdb->insert(
579 $this->db_table,
580 array(
581 'author_id' => $author_id,
582 'title' => $title,
583 'description' => $description,
584 'type' => $type,
585 'source_chart_type' => $source_chart_type,
586 'source_type' => $source_type,
587 'source' => $source,
588 'status' => $status,
589 'date_created' => $date_created,
590 'date_modified' => $date_modified,
591 'quiz_query' => $query_id,
592 'quiz_id' => $quiz_id,
593 'options' => json_encode( $options ),
594 ),
595 array(
596 '%s', // author_id
597 '%s', // title
598 '%s', // description
599 '%s', // type
600 '%s', // source_chart_type
601 '%s', // source_type
602 '%s', // source
603 '%s', // status
604 '%s', // date_created
605 '%s', // date_modified
606 '%s', // query_id
607 '%d', // quiz_id
608 '%s', // options
609 )
610 );
611
612 $inserted_id = $wpdb->insert_id;
613
614 if( is_array( $settings ) && ! empty( $settings ) ){
615 foreach ( $settings as $key => $setting ){
616 $this->add_meta( $inserted_id, $key, $setting );
617 }
618 }
619
620 update_option( 'ays_chart_quiz_maker_results_' . $inserted_id, $quiz_maker_data );
621 delete_option( $quiz_maker_data_option_name );
622
623 update_option( 'ays_chart_quiz_maker_quiz_data_' . $inserted_id, $quiz_data );
624 delete_option( $quiz_maker_data_option_name_for_quiz );
625
626 $message = 'created';
627 }else{
628 $result = $wpdb->update(
629 $this->db_table,
630 array(
631 'author_id' => $author_id,
632 'title' => $title,
633 'description' => $description,
634 'type' => $type,
635 'source_chart_type' => $source_chart_type,
636 'source_type' => $source_type,
637 'source' => $source,
638 'status' => $status,
639 'date_modified' => $date_modified,
640 'quiz_query' => $query_id,
641 'quiz_id' => $quiz_id,
642 'options' => json_encode( $options ),
643 ),
644 array( 'id' => $id ),
645 array(
646 '%s', // author_id
647 '%s', // title
648 '%s', // description
649 '%s', // type
650 '%s', // source_chart_type
651 '%s', // source_type
652 '%s', // source
653 '%s', // status
654 '%s', // date_modified
655 '%s', // quiz_query
656 '%d', // quiz_id
657 '%s', // options
658 ),
659 array( '%d' )
660 );
661
662 $inserted_id = $id;
663
664 if( is_array( $settings ) && ! empty( $settings ) ){
665 foreach ( $settings as $key => $setting ){
666 if( $this->get_meta( $inserted_id, $key, 'id' ) ) {
667 $this->update_meta( $inserted_id, $key, $setting );
668 }else{
669 $this->add_meta( $inserted_id, $key, $setting );
670 }
671 }
672 }
673
674 $message = 'updated';
675 }
676
677 $ays_chart_tab = isset($_POST[ $name_prefix . 'chart_tab' ]) ? $_POST[ $name_prefix . 'chart_tab' ] : 'tab1';
678
679 if($message == 'created'){
680 setcookie('ays_chart_created_new', $inserted_id, time() + 3600, '/');
681 }
682
683 if( $result >= 0 ) {
684 if( $save_type == 'apply' ){
685 $url = remove_query_arg( array('type', 'source') );
686 if($id == 0){
687 $url = esc_url_raw( add_query_arg( array(
688 "action" => "edit",
689 "id" => $inserted_id,
690 "ays_chart_tab" => $ays_chart_tab,
691 "status" => $message
692 ), $url ) );
693 }else{
694 $url = esc_url_raw( add_query_arg( array(
695 "ays_chart_tab" => $ays_chart_tab,
696 "status" => $message
697 ), $url ) );
698 }
699 wp_redirect( $url );
700 exit;
701 }elseif( $save_type == 'save_new' ){
702 $url = remove_query_arg( array('id', 'type', 'source') );
703 $url = esc_url_raw( add_query_arg( array(
704 "action" => "add",
705 "ays_chart_tab" => $ays_chart_tab,
706 "status" => $message
707 ), $url ) );
708 wp_redirect( $url );
709 exit;
710 }else{
711 $url = remove_query_arg( array('action', 'id', 'type', 'source') );
712 $url = esc_url_raw( add_query_arg( array(
713 "ays_chart_tab" => $ays_chart_tab,
714 "status" => $message
715 ), $url ) );
716 wp_redirect( $url );
717 exit;
718 }
719 }
720
721 }else{
722 return false;
723 }
724
725 }
726
727 /**
728 * Delete record by id
729 *
730 * @since 1.0.0
731 * @access public
732 *
733 * @param $id
734 *
735 * @return bool
736 */
737 public function delete_item( $id ){
738 global $wpdb;
739
740 if( is_null( $id ) ){
741 return false;
742 }
743
744 $wpdb->delete(
745 $this->db_table_meta,
746 array( 'chart_id' => absint( $id ) ),
747 array( '%d' )
748 );
749
750 $wpdb->delete(
751 $this->db_table,
752 array( 'id' => absint( $id ) ),
753 array( '%d' )
754 );
755
756 return true;
757 }
758
759 /**
760 * Move to trash record by id
761 *
762 * @since 1.0.0
763 * @access public
764 *
765 * @param $id
766 *
767 * @return bool
768 */
769 public function trash_item( $id ){
770 global $wpdb;
771
772 if( is_null( $id ) ){
773 return false;
774 }
775
776 $result = $wpdb->update(
777 $this->db_table,
778 array( 'status' => 'trashed' ),
779 array( 'id' => absint( $id ) ),
780 array( '%s' ),
781 array( '%d' )
782 );
783
784 if( $result >= 0 ){
785 return true;
786 }
787
788 return false;
789 }
790
791 /**
792 * Move to publish record by id
793 *
794 * @since 1.0.0
795 * @access public
796 *
797 * @param $id
798 *
799 * @return bool
800 */
801 public function publish_item( $id ){
802 global $wpdb;
803
804 if( is_null( $id ) ){
805 return false;
806 }
807
808 $result = $wpdb->update(
809 $this->db_table,
810 array( 'status' => 'published' ),
811 array( 'id' => absint( $id ) ),
812 array( '%s' ),
813 array( '%d' )
814 );
815
816 if( $result >= 0 ){
817 return true;
818 }
819
820 return false;
821 }
822
823 /**
824 * Restore record by id
825 *
826 * @since 1.0.0
827 * @access public
828 *
829 * @param $id
830 *
831 * @return bool
832 */
833 public function restore_item( $id ){
834 global $wpdb;
835
836 if( is_null( $id ) ){
837 return false;
838 }
839
840 $result = $wpdb->update(
841 $this->db_table,
842 array( 'status' => 'draft' ),
843 array( 'id' => absint( $id ) ),
844 array( '%s' ),
845 array( '%d' )
846 );
847
848 if( $result >= 0 ){
849 return true;
850 }
851
852 return false;
853 }
854
855 /**
856 * Duplicate record by id
857 *
858 * @since 1.0.0
859 * @access public
860 *
861 * @param $id
862 *
863 * @return bool
864 */
865 public function duplicate_item( $id ){
866 global $wpdb;
867
868 if( is_null( $id ) ){
869 return false;
870 }
871
872 $current_data = $this->get_item($id);
873 array_shift($current_data);
874 $current_data['title'] .= __(' (Copy)', 'chart-builder');
875 $current_data['date_created'] = current_time( 'mysql' );
876 $current_data['date_modified'] = current_time( 'mysql' );
877 $result = $wpdb->insert(
878 $this->db_table,
879 $current_data,
880 array('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%d','%s')
881 );
882 $new_id = $wpdb->insert_id;
883 if (!$result || $new_id <= 0) {
884 return false;
885 }
886
887 $quiz_option_result = get_option('ays_chart_quiz_maker_results_'.$id);
888 $quiz_option_data = get_option('ays_chart_quiz_maker_quiz_data_'.$id);
889 if ($quiz_option_result) {
890 update_option( 'ays_chart_quiz_maker_results_' . $new_id, $quiz_option_result );
891 }
892 if ($quiz_option_data) {
893 update_option( 'ays_chart_quiz_maker_quiz_data_' . $new_id, $quiz_option_data );
894 }
895
896 $current_metadata = $this->get_metadata($id);
897 foreach ($current_metadata as $meta_id => &$meta_row) {
898 $meta_row['chart_id'] = $new_id;
899 array_shift($meta_row);
900 $result = $wpdb->insert(
901 $this->db_table_meta,
902 $meta_row,
903 array( '%s', '%s', '%s', '%s' )
904 );
905 if ($result < 0){
906 return false;
907 }
908 }
909
910 return true;
911 }
912
913 /**
914 * Get record metadata by id
915 *
916 * @since 1.0.0
917 * @access public
918 *
919 * @param $id
920 *
921 * @return array
922 */
923 public function get_metadata( $id ){
924 global $wpdb;
925
926 if( is_null( $id ) ){
927 return array();
928 }
929
930 $sql = "SELECT * FROM " . $this->db_table_meta . " WHERE chart_id = " . $id;
931
932 $results = $wpdb->get_results($sql, ARRAY_A);
933
934 if( count( $results ) > 0 ){
935 return $results;
936 }else{
937 return array();
938 }
939 }
940
941 /**
942 * Convert record metadata to useful format
943 *
944 * @since 1.0.0
945 * @access public
946 *
947 * @param $settings
948 *
949 * @return array
950 */
951 public function convert_metadata( $settings ){
952
953 if( ! is_array( $settings ) || empty( $settings ) ){
954 return array();
955 }
956
957 $data = array();
958 foreach ( $settings as $k => $setting ) {
959 $data[ $setting['meta_key'] ] = $setting['meta_value'];
960 }
961
962 return $data;
963 }
964
965 /**
966 * Add default values to record metadata of the chart
967 *
968 * @since 1.0.0
969 * @access public
970 *
971 * @param $settings
972 *
973 * @return array
974 */
975 public function apply_default_metadata( $settings ){
976
977 if( ! is_array( $settings ) || empty( $settings ) ){
978 return array();
979 }
980
981 $defaults = array(
982 'width' => '',
983 'height' => '',
984 'font_size' => '',
985 'title_color' => '',
986 );
987
988 foreach ( $defaults as $key => $default ) {
989 if( ! isset( $settings[ $key ] ) ) {
990 $settings[ $key ] = $default;
991 }
992 }
993
994 return $settings;
995 }
996
997 /**
998 * Get record meta by record id and meta key
999 *
1000 * @since 1.0.0
1001 * @access public
1002 *
1003 * @param $id
1004 * @param $meta_key
1005 *
1006 * @return false|array
1007 */
1008 public function get_meta( $id, $meta_key, $select_value = "meta_value" ){
1009 global $wpdb;
1010
1011 if( is_null( $id ) ){
1012 return false;
1013 }
1014
1015 if( is_null( $meta_key ) || trim( $meta_key ) === '' ){
1016 return false;
1017 }
1018
1019 $sql = "SELECT ".$select_value." FROM ". $this->db_table_meta ." WHERE meta_key = '".$meta_key."' AND chart_id = '".$id."'";
1020 $result = $wpdb->get_var($sql);
1021
1022 if( $result != "" ){
1023 return $result;
1024 }
1025
1026 return false;
1027 }
1028
1029 /**
1030 * Insert record meta by record id
1031 *
1032 * @since 1.0.0
1033 * @access public
1034 *
1035 * @param $id
1036 * @param $meta_key @accept string
1037 * @param $meta_value @accept JSON|serialized array|string|number
1038 * @param string $note @accept string|number
1039 * @param string $options @accept JSON
1040 *
1041 * @return bool
1042 */
1043 public function add_meta( $id, $meta_key, $meta_value, $note = "", $options = "" ){
1044 global $wpdb;
1045
1046 if( is_null( $id ) ){
1047 return false;
1048 }
1049
1050 if( is_null( $meta_key ) || trim( $meta_key ) === '' ){
1051 return false;
1052 }
1053
1054 $result = $wpdb->insert(
1055 $this->db_table_meta,
1056 array(
1057 'chart_id' => absint( $id ),
1058 'meta_key' => $meta_key,
1059 'meta_value' => $meta_value,
1060 'note' => $note,
1061 'options' => $options
1062 ),
1063 array( '%s', '%s', '%s', '%s' )
1064 );
1065
1066 if($result >= 0){
1067 return true;
1068 }
1069
1070 return false;
1071 }
1072
1073 /**
1074 * Update record meta by record id
1075 *
1076 * @since 1.0.0
1077 * @access public
1078 *
1079 * @param $id
1080 * @param $meta_key @accept string
1081 * @param $meta_value @accept JSON|serialized array|string|number
1082 * @param string $note @accept string|number
1083 * @param string $options @accept JSON
1084 *
1085 * @return bool
1086 */
1087 public function update_meta( $id, $meta_key, $meta_value, $note = "", $options = "" ){
1088 global $wpdb;
1089
1090 if( is_null( $id ) ){
1091 return false;
1092 }
1093
1094 if( is_null( $meta_key ) || trim( $meta_key ) === '' ){
1095 return false;
1096 }
1097
1098 $value = array(
1099 'meta_value' => $meta_value,
1100 );
1101
1102 $value_s = array( '%s' );
1103 if($note != null){
1104 $value['note'] = $note;
1105 $value_s[] = '%s';
1106 }
1107
1108 if($options != null){
1109 $value['options'] = $options;
1110 $value_s[] = '%s';
1111 }
1112
1113 $result = $wpdb->update(
1114 $this->db_table_meta,
1115 $value,
1116 array(
1117 'chart_id' => absint( $id ),
1118 'meta_key' => $meta_key,
1119 ),
1120 $value_s,
1121 array( '%d', '%s' )
1122 );
1123
1124 if($result >= 0){
1125 return true;
1126 }
1127
1128 return false;
1129 }
1130
1131 /**
1132 * Delete record meta by record id and meta key
1133 *
1134 * @since 1.0.0
1135 * @access public
1136 *
1137 * @param $id
1138 * @param $meta_key
1139 *
1140 * @return bool
1141 */
1142 public function delete_meta( $id, $meta_key ){
1143 global $wpdb;
1144
1145 if( is_null( $id ) ){
1146 return false;
1147 }
1148
1149 if( is_null( $meta_key ) || trim( $meta_key ) === '' ){
1150 return false;
1151 }
1152
1153 $wpdb->delete(
1154 $this->db_table_meta,
1155 array(
1156 'chart_id' => absint( $id ),
1157 'meta_key' => $meta_key,
1158 ),
1159 array( '%d', '%s' )
1160 );
1161
1162 return true;
1163 }
1164
1165 public static function ays_chart_is_elementor(){
1166 if( isset( $_GET['action'] ) && $_GET['action'] == 'elementor' ){
1167 $is_elementor = true;
1168 }elseif( isset( $_REQUEST['elementor-preview'] ) && $_REQUEST['elementor-preview'] != '' ){
1169 $is_elementor = true;
1170 }else{
1171 $is_elementor = false;
1172 }
1173
1174 if ( ! $is_elementor ) {
1175 $is_elementor = ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ) ? true : false;
1176 }
1177
1178 return $is_elementor;
1179 }
1180
1181 /**
1182 * Display notice based on action that happened to record
1183 *
1184 * @since 1.0.0
1185 * @access public
1186 *
1187 * @return void|html
1188 */
1189 public function chart_notices(){
1190 $page = (isset($_REQUEST['page'])) ? sanitize_text_field( $_REQUEST['page'] ) : '';
1191 if ( !($page == "chart-builder") )
1192 return;
1193
1194 $status = (isset($_REQUEST['status'])) ? sanitize_text_field( $_REQUEST['status'] ) : '';
1195
1196 if ( empty( $status ) )
1197 return;
1198
1199 $error = false;
1200 switch ( $status ) {
1201 case 'created':
1202 $updated_message = __( 'Chart created.', "chart-builder" ) ;
1203 break;
1204 case 'updated':
1205 $updated_message = __( 'Chart saved.', "chart-builder" ) ;
1206 break;
1207 case 'duplicated':
1208 $updated_message = __( 'Chart duplicated.', "chart-builder" ) ;
1209 break;
1210 case 'deleted':
1211 $updated_message = __( 'Chart deleted.', "chart-builder" ) ;
1212 break;
1213 case 'trashed':
1214 $updated_message = __( 'Chart moved to trash.', "chart-builder" ) ;
1215 break;
1216 case 'restored':
1217 $updated_message = __( 'Chart restored.', "chart-builder" ) ;
1218 break;
1219 case 'all-duplicated':
1220 $updated_message = __( 'Charts are duplicated.', "chart-builder" ) ;
1221 break;
1222 case 'all-deleted':
1223 $updated_message = __( 'Charts are deleted.', "chart-builder" ) ;
1224 break;
1225 case 'all-trashed':
1226 $updated_message = __( 'Charts are moved to trash.', "chart-builder" );
1227 break;
1228 case 'all-restored':
1229 $updated_message = __( 'Charts are restored.', "chart-builder" );
1230 break;
1231 case 'empty-title':
1232 $error = true;
1233 $updated_message = __( 'Error: Chart title can not be empty.', "chart-builder" ) ;
1234 break;
1235 default:
1236 break;
1237 }
1238
1239 if ( empty( $updated_message ) )
1240 return;
1241
1242 $notice_class = 'success';
1243 if( $error ){
1244 $notice_class = 'error';
1245 }
1246 ?>
1247 <div class="notice notice-<?php echo esc_attr( $notice_class ); ?> is-dismissible">
1248 <p> <?php echo esc_html($updated_message); ?> </p>
1249 </div>
1250 <?php
1251 }
1252
1253 }
1254 }