| 1 |
<?php |
| 2 |
|
| 3 |
if( ! class_exists( 'Chart_Builder_Actions' ) ){ |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Chart_Builder_Actions |
| 7 |
* Class contains chart data filtering and hooks related to chart |
| 8 |
* |
| 9 |
* Main functionality belong to chart source and source type |
| 10 |
* Also chart settings and options |
| 11 |
* |
| 12 |
* Hooks used in the class |
| 13 |
* |
| 14 |
* @hooks @actions ays_chart_get_chart_data |
| 15 |
* |
| 16 |
* @filters ays_chart_source_type |
| 17 |
* ays_chart_source |
| 18 |
* ays_chart_source_{source_type} |
| 19 |
* ays_chart_get_settings |
| 20 |
* ays_chart_get_options |
| 21 |
* ays_chart_get_title |
| 22 |
* ays_chart_get_type |
| 23 |
* ays_chart_get_status |
| 24 |
* |
| 25 |
* @param $plugin_name |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @package Chart_Builder |
| 29 |
* @subpackage Chart_Builder/includes |
| 30 |
* @author Chart Builder Team <info@ays-pro.com> |
| 31 |
*/ |
| 32 |
class Chart_Builder_Actions { |
| 33 |
|
| 34 |
/** |
| 35 |
* The ID of this plugin. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @access private |
| 39 |
* @var string $plugin_name The ID of this plugin. |
| 40 |
*/ |
| 41 |
private $plugin_name; |
| 42 |
|
| 43 |
/** |
| 44 |
* The object of the database. |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* @access private |
| 48 |
* @var string $db_obj The database object of the chart. |
| 49 |
*/ |
| 50 |
private $db_obj; |
| 51 |
|
| 52 |
/** |
| 53 |
* The constructor of the class |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* |
| 57 |
* @param $plugin_name |
| 58 |
*/ |
| 59 |
public function __construct( $plugin_name ) { |
| 60 |
|
| 61 |
/** |
| 62 |
* Assigning $plugin_name to the @plugin_name property |
| 63 |
*/ |
| 64 |
$this->plugin_name = $plugin_name; |
| 65 |
|
| 66 |
/** |
| 67 |
* Assigning database object to the @db_obj property |
| 68 |
* |
| 69 |
* Creating instance of Chart_Builder_DB_Actions class |
| 70 |
*/ |
| 71 |
$this->db_obj = new Chart_Builder_DB_Actions( $this->plugin_name ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get instance of this class |
| 76 |
* |
| 77 |
* @since 1.0.0 |
| 78 |
* @access public |
| 79 |
* |
| 80 |
* @param $plugin_name |
| 81 |
* |
| 82 |
* @return Chart_Builder_Actions |
| 83 |
*/ |
| 84 |
public static function get_instance( $plugin_name ){ |
| 85 |
return new self( $plugin_name ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get chart data by id |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
* @access public |
| 93 |
* |
| 94 |
* @param $id |
| 95 |
* |
| 96 |
* @return null|non-empty-array |
| 97 |
*/ |
| 98 |
public function get_chart_data( $id ){ |
| 99 |
|
| 100 |
/** |
| 101 |
* Checking if given valid @id |
| 102 |
* |
| 103 |
* @return null if not valid |
| 104 |
*/ |
| 105 |
if( is_null( $id ) || intval( $id ) === 0 ){ |
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Retrieving chart row from database |
| 111 |
* |
| 112 |
* Passing @id as a parameter |
| 113 |
*/ |
| 114 |
$chart = $this->db_obj->get_item( $id ); |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* Validate chart general data by calling validation function |
| 119 |
* |
| 120 |
* Function must @return array |
| 121 |
*/ |
| 122 |
$chart = $this->validate_item_data( $chart ); |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Getting source chart type from @chart object |
| 127 |
*/ |
| 128 |
$source_chart_type = $chart['source_chart_type']; |
| 129 |
$source_chart_type = apply_filters( 'ays_chart_source_chart_type', $source_chart_type ); |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* Getting source type from @chart object |
| 134 |
*/ |
| 135 |
$source_type = $chart['source_type']; |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Applying filter to source type for development purposes |
| 140 |
* |
| 141 |
* @accept manual, custom, etc. |
| 142 |
*/ |
| 143 |
$source_type = apply_filters( 'ays_chart_source_type', $source_type ); |
| 144 |
|
| 145 |
/** |
| 146 |
* Getting source from @chart object |
| 147 |
*/ |
| 148 |
$source = $chart['source']; |
| 149 |
|
| 150 |
/** |
| 151 |
* Defining filter function name that will filter source data |
| 152 |
*/ |
| 153 |
$filter_function = 'chart_data_' . $source_type; |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* Calling filter function |
| 158 |
* |
| 159 |
* Before calling checking if the function exists and is callable |
| 160 |
* |
| 161 |
* Passing @source as a parameter |
| 162 |
* Expecting an array which must be returned after filter |
| 163 |
*/ |
| 164 |
$source_type_data = array(); |
| 165 |
if( method_exists( $this , $filter_function ) && is_callable( array( $this, $filter_function ) ) ) { |
| 166 |
$source_type_data = $this->$filter_function( $source, $id ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Checking that returning data was array |
| 171 |
*/ |
| 172 |
if( ! is_array( $source_type_data ) ){ |
| 173 |
$source_type_data = array(); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Checking availability of data in the returned value from filter function |
| 178 |
*/ |
| 179 |
if( empty( $source_type_data ) ){ |
| 180 |
$source_type_data = array(); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Final source after validations and filters |
| 185 |
* And final filter that applies to the source for giving access to change the data after all |
| 186 |
* |
| 187 |
* Applying filter to @source for development purposes |
| 188 |
* |
| 189 |
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 190 |
* Please don't use this filter often when you need to change the source |
| 191 |
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 192 |
*/ |
| 193 |
$final_source = apply_filters( 'ays_chart_source', $source_type_data, $source_type ); |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Getting @settings data from database by calling get_metadata function from db object |
| 198 |
* |
| 199 |
* get_metadata function must return an array that contains chart @settings |
| 200 |
*/ |
| 201 |
$chart_settings = $this->db_obj->get_metadata( $id ); |
| 202 |
|
| 203 |
/** |
| 204 |
* Settings mustn't be empty or not array type data |
| 205 |
* So checking is array and not empty |
| 206 |
* |
| 207 |
* If validation not passed assigning empty array |
| 208 |
*/ |
| 209 |
if( ! is_array( $chart_settings ) || empty( $chart_settings ) ){ |
| 210 |
$chart_settings = array(); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Converting @settings data to usable format |
| 215 |
* |
| 216 |
* convert_metadata function must return an array that contains formatted chart @settings |
| 217 |
*/ |
| 218 |
$chart_settings = $this->db_obj->convert_metadata( $chart_settings ); |
| 219 |
|
| 220 |
/** |
| 221 |
* Applying defaults to @settings |
| 222 |
* |
| 223 |
* apply_default_metadata function must return an array |
| 224 |
* Expecting @settings with default values applied |
| 225 |
*/ |
| 226 |
$chart_settings = $this->db_obj->apply_default_metadata( $chart_settings ); |
| 227 |
|
| 228 |
|
| 229 |
/** |
| 230 |
* Applying filter to @settings data for development purposes |
| 231 |
*/ |
| 232 |
$chart_settings = apply_filters( 'ays_chart_get_settings', $chart_settings ); |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Getting @options from @chart object |
| 237 |
*/ |
| 238 |
$chart_options = $chart['options']; |
| 239 |
|
| 240 |
/** |
| 241 |
* After getting @options from @chart object |
| 242 |
*/ |
| 243 |
unset( $chart['options'] ); |
| 244 |
|
| 245 |
/** |
| 246 |
* Applying filter to @options for development purposes |
| 247 |
*/ |
| 248 |
$chart_options = apply_filters( 'ays_chart_get_options', $chart_options ); |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Collecting chart whole data into one variable |
| 253 |
* |
| 254 |
* @array $chart_data |
| 255 |
*/ |
| 256 |
$chart_data = array( |
| 257 |
'chart' => $chart, |
| 258 |
'source_chart_type' => $source_chart_type, |
| 259 |
'source_type' => $source_type, |
| 260 |
'source' => $final_source, |
| 261 |
'settings' => $chart_settings, |
| 262 |
'options' => $chart_options, |
| 263 |
); |
| 264 |
|
| 265 |
/** |
| 266 |
* Action for get chart data function |
| 267 |
*/ |
| 268 |
do_action( 'ays_chart_get_chart_data', $chart_data ); |
| 269 |
|
| 270 |
/** |
| 271 |
* Returning chart whole data |
| 272 |
* |
| 273 |
* @return $chart_data |
| 274 |
*/ |
| 275 |
return $chart_data; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Chart data type manual |
| 280 |
* Validating the data and returning validated array |
| 281 |
* |
| 282 |
* Returning applied filter to array for development purposes |
| 283 |
* |
| 284 |
* @since 1.0.0 |
| 285 |
* @access protected |
| 286 |
* |
| 287 |
* @accept JSON, empty string |
| 288 |
* @param $source |
| 289 |
* |
| 290 |
* @return array |
| 291 |
*/ |
| 292 |
protected function chart_data_manual( $source, $chart_id ){ |
| 293 |
|
| 294 |
/** |
| 295 |
* Defining an empty array that will return after validation |
| 296 |
*/ |
| 297 |
$filtered_data = array(); |
| 298 |
|
| 299 |
/** |
| 300 |
* @source mustn't be empty or not string type data |
| 301 |
* So checking is string and not empty |
| 302 |
* |
| 303 |
* If validation not passed function will return an empty array |
| 304 |
*/ |
| 305 |
if( is_string( $source ) && trim( $source ) !== '' ){ |
| 306 |
|
| 307 |
/** |
| 308 |
* Data are expected to be of type JSON |
| 309 |
* Trying to parse the data |
| 310 |
*/ |
| 311 |
$decoded_data = json_decode( $source, true ); |
| 312 |
|
| 313 |
/** |
| 314 |
* @decoded_data mustn't be empty or not array type data |
| 315 |
* So checking is array and not empty |
| 316 |
*/ |
| 317 |
if( is_array( $decoded_data ) && ! empty( $decoded_data ) ){ |
| 318 |
|
| 319 |
/** |
| 320 |
* Assigning parsed data to the @filtered_data variable |
| 321 |
*/ |
| 322 |
$filtered_data = $decoded_data; |
| 323 |
} |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Returning the data that has passed the validation |
| 329 |
* Returning applied filter to array for development purposes |
| 330 |
* |
| 331 |
* @return array |
| 332 |
*/ |
| 333 |
return apply_filters( 'ays_chart_source_manual', $filtered_data ); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Chart data type custom |
| 338 |
* For developers |
| 339 |
* Returning data must be an array |
| 340 |
* |
| 341 |
* @since 1.0.0 |
| 342 |
* @access protected |
| 343 |
* |
| 344 |
* @accept mixed |
| 345 |
* @param $source |
| 346 |
* |
| 347 |
* @return array |
| 348 |
*/ |
| 349 |
protected function chart_data_custom( $source, $chart_id ){ |
| 350 |
|
| 351 |
/** |
| 352 |
* Getting the data that will some developers pass via this filter |
| 353 |
*/ |
| 354 |
$filtered_data = apply_filters( 'ays_chart_source_custom', array(), $chart_id ); |
| 355 |
|
| 356 |
/** |
| 357 |
* Checking that returning data was array |
| 358 |
*/ |
| 359 |
if( ! is_array( $filtered_data ) ){ |
| 360 |
return array(); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Checking availability of data in array |
| 365 |
*/ |
| 366 |
if( empty( $filtered_data ) ){ |
| 367 |
return array(); |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Returning the data that has passed the validation |
| 372 |
* |
| 373 |
* @return array |
| 374 |
*/ |
| 375 |
return $filtered_data; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Validation function for chart general data |
| 380 |
* |
| 381 |
* Validating title, description, type, status, etc. |
| 382 |
* |
| 383 |
* @since 1.0.0 |
| 384 |
* @access protected |
| 385 |
* |
| 386 |
* @param $chart |
| 387 |
* |
| 388 |
* @return array |
| 389 |
*/ |
| 390 |
protected function validate_item_data( $chart ){ |
| 391 |
|
| 392 |
/** |
| 393 |
* @chart mustn't be empty or not array type data |
| 394 |
* So checking is array and not empty |
| 395 |
* |
| 396 |
* If validation not passed returning an empty array |
| 397 |
*/ |
| 398 |
if( ! is_array( $chart ) || empty( $chart ) ){ |
| 399 |
return array(); |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Defining an empty array that will return after validation |
| 404 |
* |
| 405 |
* During validation array will be filled with valid data |
| 406 |
*/ |
| 407 |
$chart_data = array(); |
| 408 |
|
| 409 |
|
| 410 |
/** |
| 411 |
* Validating the @title |
| 412 |
* |
| 413 |
* Title mustn't be empty |
| 414 |
* |
| 415 |
* Applying esc_html filtering function to escape HTML from the title |
| 416 |
* Then applying filter to @title for development purposes |
| 417 |
* |
| 418 |
* @default "Chart example" |
| 419 |
*/ |
| 420 |
$chart_title = __( 'Chart example', "chart-builder" ); |
| 421 |
if( isset( $chart['title'] ) && $chart['title'] !== '' ) { |
| 422 |
$chart_title = esc_html( $chart['title'] ); |
| 423 |
} |
| 424 |
|
| 425 |
$chart_data['title'] = apply_filters( 'ays_chart_get_title', $chart_title ); |
| 426 |
|
| 427 |
|
| 428 |
/** |
| 429 |
* Validating the @description |
| 430 |
* |
| 431 |
* Description is optional, so it can be empty |
| 432 |
* Applying stripslashes function to escape unnecessary slashes |
| 433 |
* |
| 434 |
* @default empty string |
| 435 |
*/ |
| 436 |
$chart_data['description'] = ''; |
| 437 |
if( isset( $chart['description'] ) && $chart['description'] !== '' ){ |
| 438 |
$chart_data['description'] = stripslashes( $chart['description'] ); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
/** |
| 443 |
* Validating the @type |
| 444 |
* |
| 445 |
* Type is required, it can't be empty |
| 446 |
* Applying esc_html filtering function to escape HTML from the type |
| 447 |
* |
| 448 |
* Then applying filter to @type for development purposes |
| 449 |
* |
| 450 |
* @accept values are google-charts, etc. |
| 451 |
* @default google-charts |
| 452 |
*/ |
| 453 |
$chart_type = 'google-charts'; |
| 454 |
if( isset( $chart['type'] ) && $chart['type'] !== '' ){ |
| 455 |
$chart_type = esc_html( $chart['type'] ); |
| 456 |
} |
| 457 |
|
| 458 |
$chart_data['type'] = apply_filters( 'ays_chart_get_type', $chart_type ); |
| 459 |
|
| 460 |
/** |
| 461 |
* Validating the @source_chart_type |
| 462 |
* |
| 463 |
* Source chart type is required, it can't be empty |
| 464 |
* Applying esc_html filtering function to escape HTML from the @source_type |
| 465 |
* |
| 466 |
* @accept values are manual, file, WordPress, database, etc. |
| 467 |
* @default manual |
| 468 |
*/ |
| 469 |
$chart_data['source_chart_type'] = ( isset( $chart['source_chart_type'] ) && $chart['source_chart_type'] !== '' ) ? esc_html( $chart['source_chart_type'] ) : 'pie_chart'; |
| 470 |
|
| 471 |
/** |
| 472 |
* Validating the @source_type |
| 473 |
* |
| 474 |
* Source type is required, it can't be empty |
| 475 |
* Applying esc_html filtering function to escape HTML from the @source_type |
| 476 |
* |
| 477 |
* @accept values are manual, file, WordPress, database, etc. |
| 478 |
* @default manual |
| 479 |
*/ |
| 480 |
$chart_data['source_type'] = 'manual'; |
| 481 |
if( isset( $chart['source_type'] ) && $chart['source_type'] !== '' ){ |
| 482 |
$chart_data['source_type'] = esc_html( $chart['source_type'] ); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
/** |
| 487 |
* Validating the @source |
| 488 |
* |
| 489 |
* Source is required, but it can be empty |
| 490 |
* Applying esc_html filtering function to escape HTML from the @source |
| 491 |
* |
| 492 |
* @accept values are JSON, CSV, serialized data, etc. |
| 493 |
* @default empty string |
| 494 |
*/ |
| 495 |
$chart_data['source'] = ''; |
| 496 |
if( isset( $chart['source'] ) && $chart['source'] !== '' ){ |
| 497 |
$chart_data['source'] = $chart['source'] ; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
/** |
| 502 |
* Validating the @status |
| 503 |
* |
| 504 |
* Status is required, it can't be empty |
| 505 |
* Applying esc_html filtering function to escape HTML from the @status |
| 506 |
* |
| 507 |
* Then applying filter to type for development purposes |
| 508 |
* |
| 509 |
* @accept values are published and draft |
| 510 |
* @default draft |
| 511 |
*/ |
| 512 |
$chart_status = 'draft'; |
| 513 |
if( isset( $chart['status'] ) && $chart['status'] !== '' ){ |
| 514 |
$chart_status = esc_html( $chart['status'] ); |
| 515 |
} |
| 516 |
|
| 517 |
$chart_data['status'] = apply_filters( 'ays_chart_get_status', $chart_status ); |
| 518 |
|
| 519 |
|
| 520 |
/** |
| 521 |
* Validating the @options |
| 522 |
* Options is optional, it can be empty |
| 523 |
* |
| 524 |
* @accept JSON |
| 525 |
* @default empty string |
| 526 |
* @return array |
| 527 |
*/ |
| 528 |
$chart_data['options'] = array(); |
| 529 |
if( isset( $chart['options'] ) && $chart['options'] !== '' ){ |
| 530 |
|
| 531 |
/** |
| 532 |
* Data are expected to be of type JSON |
| 533 |
* Trying to parse the data |
| 534 |
*/ |
| 535 |
$chart_options = json_decode( $chart['options'], true ); |
| 536 |
|
| 537 |
/** |
| 538 |
* Options mustn't be empty or not array type data |
| 539 |
* So checking is array and not empty |
| 540 |
* |
| 541 |
* If validation not passed assigning empty array |
| 542 |
*/ |
| 543 |
if( ! is_array( $chart_options ) || empty( $chart_options ) ){ |
| 544 |
$chart_options = array(); |
| 545 |
} |
| 546 |
|
| 547 |
/** |
| 548 |
* Pushing the result to the returning variable |
| 549 |
*/ |
| 550 |
$chart_data['options'] = $chart_options; |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* Returning validated data |
| 555 |
* Returning an array |
| 556 |
* |
| 557 |
* @return array |
| 558 |
*/ |
| 559 |
return $chart_data; |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Returns default array for charts |
| 564 |
*/ |
| 565 |
public function get_charts_default_data(){ |
| 566 |
return array( |
| 567 |
'1' => array( |
| 568 |
'Administrator', |
| 569 |
'11' |
| 570 |
), |
| 571 |
'2' => array( |
| 572 |
'Author', |
| 573 |
'2' |
| 574 |
), |
| 575 |
'3' => array( |
| 576 |
'Editor', |
| 577 |
'5' |
| 578 |
), |
| 579 |
'4' => array( |
| 580 |
'Guest', |
| 581 |
'2' |
| 582 |
), |
| 583 |
'5' => array( |
| 584 |
'Subscriber', |
| 585 |
'7' |
| 586 |
) |
| 587 |
); |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
if( ! function_exists( 'CBActions' ) ){ |
| 593 |
/** |
| 594 |
* Function for quick access to Chart_Builder_Actions class |
| 595 |
* |
| 596 |
* @since 1.0.0 |
| 597 |
* @return Chart_Builder_Actions |
| 598 |
*/ |
| 599 |
function CBActions(){ |
| 600 |
|
| 601 |
static $instance = null; |
| 602 |
|
| 603 |
if( $instance === null ){ |
| 604 |
$instance = Chart_Builder_Actions::get_instance( CHART_BUILDER_NAME ); |
| 605 |
} |
| 606 |
|
| 607 |
if( $instance instanceof Chart_Builder_Actions ){ |
| 608 |
return $instance; |
| 609 |
} |
| 610 |
|
| 611 |
return Chart_Builder_Actions::get_instance( CHART_BUILDER_NAME ); |
| 612 |
} |
| 613 |
} |
| 614 |
|