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