| 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 $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 |
} |
| 909 |
} |