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