| @@ -1,613 +1,862 @@ | ||
| 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 | -} | |
| 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 | + if ( !$chart ) { | |
| 116 | + return null; | |
| 117 | + } | |
| 118 | + | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Validate chart general data by calling validation function | |
| 122 | + * | |
| 123 | + * Function must @return array | |
| 124 | + */ | |
| 125 | + $chart = $this->validate_item_data( $chart ); | |
| 126 | + | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Getting source chart type from @chart object | |
| 130 | + */ | |
| 131 | + $source_chart_type = $chart['source_chart_type']; | |
| 132 | + $source_chart_type = apply_filters( 'ays_chart_source_chart_type', $source_chart_type ); | |
| 133 | + | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * Getting source type from @chart object | |
| 137 | + */ | |
| 138 | + $source_type = $chart['source_type']; | |
| 139 | + | |
| 140 | + | |
| 141 | + /** | |
| 142 | + * Applying filter to source type for development purposes | |
| 143 | + * | |
| 144 | + * @accept manual, custom, etc. | |
| 145 | + */ | |
| 146 | + $source_type = apply_filters( 'ays_chart_source_type', $source_type ); | |
| 147 | + | |
| 148 | + /** | |
| 149 | + * Getting source from @chart object | |
| 150 | + */ | |
| 151 | + $source = $chart['source']; | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * Getting chart author id | |
| 155 | + */ | |
| 156 | + $author_id = $chart['author_id']; | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Defining filter function name that will filter source data | |
| 160 | + */ | |
| 161 | + $filter_function = 'chart_data_' . $source_type; | |
| 162 | + | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Calling filter function | |
| 166 | + * | |
| 167 | + * Before calling checking if the function exists and is callable | |
| 168 | + * | |
| 169 | + * Passing @source as a parameter | |
| 170 | + * Expecting an array which must be returned after filter | |
| 171 | + */ | |
| 172 | + $source_type_data = array(); | |
| 173 | + if( method_exists( $this , $filter_function ) && is_callable( array( $this, $filter_function ) ) ) { | |
| 174 | + $source_type_data = $this->$filter_function( $source, $id ); | |
| 175 | + } | |
| 176 | + | |
| 177 | + /** | |
| 178 | + * Checking that returning data was array | |
| 179 | + */ | |
| 180 | + if( ! is_array( $source_type_data ) ){ | |
| 181 | + $source_type_data = array(); | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Checking availability of data in the returned value from filter function | |
| 186 | + */ | |
| 187 | + if( empty( $source_type_data ) ){ | |
| 188 | + $source_type_data = array(); | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Final source after validations and filters | |
| 193 | + * And final filter that applies to the source for giving access to change the data after all | |
| 194 | + * | |
| 195 | + * Applying filter to @source for development purposes | |
| 196 | + * | |
| 197 | + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
| 198 | + * Please don't use this filter often when you need to change the source | |
| 199 | + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
| 200 | + */ | |
| 201 | + $final_source = apply_filters( 'ays_chart_source', $source_type_data, $source_type ); | |
| 202 | + | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Getting @settings data from database by calling get_metadata function from db object | |
| 206 | + * | |
| 207 | + * get_metadata function must return an array that contains chart @settings | |
| 208 | + */ | |
| 209 | + $chart_settings = $this->db_obj->get_metadata( $id ); | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Settings mustn't be empty or not array type data | |
| 213 | + * So checking is array and not empty | |
| 214 | + * | |
| 215 | + * If validation not passed assigning empty array | |
| 216 | + */ | |
| 217 | + if( ! is_array( $chart_settings ) || empty( $chart_settings ) ){ | |
| 218 | + $chart_settings = array(); | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * Converting @settings data to usable format | |
| 223 | + * | |
| 224 | + * convert_metadata function must return an array that contains formatted chart @settings | |
| 225 | + */ | |
| 226 | + $chart_settings = $this->db_obj->convert_metadata( $chart_settings ); | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * Applying defaults to @settings | |
| 230 | + * | |
| 231 | + * apply_default_metadata function must return an array | |
| 232 | + * Expecting @settings with default values applied | |
| 233 | + */ | |
| 234 | + $chart_settings = $this->db_obj->apply_default_metadata( $chart_settings ); | |
| 235 | + | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * Applying filter to @settings data for development purposes | |
| 239 | + */ | |
| 240 | + $chart_settings = apply_filters( 'ays_chart_get_settings', $chart_settings ); | |
| 241 | + | |
| 242 | + | |
| 243 | + /** | |
| 244 | + * Getting @options from @chart object | |
| 245 | + */ | |
| 246 | + $chart_options = $chart['options']; | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * After getting @options from @chart object | |
| 250 | + */ | |
| 251 | + unset( $chart['options'] ); | |
| 252 | + | |
| 253 | + /** | |
| 254 | + * Applying filter to @options for development purposes | |
| 255 | + */ | |
| 256 | + $chart_options = apply_filters( 'ays_chart_get_options', $chart_options ); | |
| 257 | + | |
| 258 | + /** | |
| 259 | + * Getting quiz data | |
| 260 | + */ | |
| 261 | + $quiz_query = $chart['quiz_query']; | |
| 262 | + $quiz_id = $chart['quiz_id']; | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Collecting chart whole data into one variable | |
| 266 | + * | |
| 267 | + * @array $chart_data | |
| 268 | + */ | |
| 269 | + $chart_data = array( | |
| 270 | + 'chart' => $chart, | |
| 271 | + 'author_id' => $author_id, | |
| 272 | + 'source_chart_type' => $source_chart_type, | |
| 273 | + 'source_type' => $source_type, | |
| 274 | + 'source' => $final_source, | |
| 275 | + 'settings' => $chart_settings, | |
| 276 | + 'options' => $chart_options, | |
| 277 | + 'quiz_query' => $quiz_query, | |
| 278 | + 'quiz_id' => $quiz_id, | |
| 279 | + ); | |
| 280 | + | |
| 281 | + /** | |
| 282 | + * Action for get chart data function | |
| 283 | + */ | |
| 284 | + do_action( 'ays_chart_get_chart_data', $chart_data ); | |
| 285 | + | |
| 286 | + /** | |
| 287 | + * Returning chart whole data | |
| 288 | + * | |
| 289 | + * @return $chart_data | |
| 290 | + */ | |
| 291 | + return $chart_data; | |
| 292 | + } | |
| 293 | + | |
| 294 | + /** | |
| 295 | + * Chart data type manual | |
| 296 | + * Validating the data and returning validated array | |
| 297 | + * | |
| 298 | + * Returning applied filter to array for development purposes | |
| 299 | + * | |
| 300 | + * @since 1.0.0 | |
| 301 | + * @access protected | |
| 302 | + * | |
| 303 | + * @accept JSON, empty string | |
| 304 | + * @param $source | |
| 305 | + * | |
| 306 | + * @return array | |
| 307 | + */ | |
| 308 | + protected function chart_data_manual( $source, $chart_id ){ | |
| 309 | + | |
| 310 | + /** | |
| 311 | + * Defining an empty array that will return after validation | |
| 312 | + */ | |
| 313 | + $filtered_data = array(); | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * @source mustn't be empty or not string type data | |
| 317 | + * So checking is string and not empty | |
| 318 | + * | |
| 319 | + * If validation not passed function will return an empty array | |
| 320 | + */ | |
| 321 | + if( is_string( $source ) && trim( $source ) !== '' ){ | |
| 322 | + | |
| 323 | + /** | |
| 324 | + * Data are expected to be of type JSON | |
| 325 | + * Trying to parse the data | |
| 326 | + */ | |
| 327 | + $decoded_data = json_decode( $source, true ); | |
| 328 | + | |
| 329 | + /** | |
| 330 | + * @decoded_data mustn't be empty or not array type data | |
| 331 | + * So checking is array and not empty | |
| 332 | + */ | |
| 333 | + if( is_array( $decoded_data ) && ! empty( $decoded_data ) ){ | |
| 334 | + | |
| 335 | + /** | |
| 336 | + * Assigning parsed data to the @filtered_data variable | |
| 337 | + */ | |
| 338 | + $filtered_data = $decoded_data; | |
| 339 | + } | |
| 340 | + | |
| 341 | + } | |
| 342 | + | |
| 343 | + /** | |
| 344 | + * Returning the data that has passed the validation | |
| 345 | + * Returning applied filter to array for development purposes | |
| 346 | + * | |
| 347 | + * @return array | |
| 348 | + */ | |
| 349 | + return apply_filters( 'ays_chart_source_manual', $filtered_data ); | |
| 350 | + } | |
| 351 | + | |
| 352 | + /** | |
| 353 | + * Chart data type quiz_maker | |
| 354 | + * Validating the data and returning validated array | |
| 355 | + * | |
| 356 | + * Returning applied filter to array for development purposes | |
| 357 | + * | |
| 358 | + * @since 1.0.0 | |
| 359 | + * @access protected | |
| 360 | + * | |
| 361 | + * @accept JSON, empty string | |
| 362 | + * @param $source | |
| 363 | + * | |
| 364 | + * @return array | |
| 365 | + */ | |
| 366 | + protected function chart_data_quiz_maker( $source, $chart_id ){ | |
| 367 | + | |
| 368 | + /** | |
| 369 | + * Defining an empty array that will return after validation | |
| 370 | + */ | |
| 371 | + $filtered_data = array(); | |
| 372 | + | |
| 373 | + /** | |
| 374 | + * @source mustn't be empty or not string type data | |
| 375 | + * So checking is string and not empty | |
| 376 | + * | |
| 377 | + * If validation not passed function will return an empty array | |
| 378 | + */ | |
| 379 | + if( is_string( $source ) && trim( $source ) !== '' ){ | |
| 380 | + | |
| 381 | + /** | |
| 382 | + * Data are expected to be of type array | |
| 383 | + * Trying to parse the data | |
| 384 | + */ | |
| 385 | + // $quiz_maker_data_option_name = 'ays_chart_quiz_maker_results_' . $chart_id; | |
| 386 | + // $quiz_maker_data = get_option( $quiz_maker_data_option_name, array() ); | |
| 387 | + // $decoded_data = isset( $quiz_maker_data['source'] ) && $quiz_maker_data['source'] !== '' ? $quiz_maker_data['source'] : ''; | |
| 388 | + | |
| 389 | + $quiz_maker_data_option_name = 'ays_chart_quiz_maker_quiz_data_' . $chart_id; | |
| 390 | + $quiz_query_data = get_option( $quiz_maker_data_option_name ); | |
| 391 | + $quiz_query = isset( $quiz_query_data['quiz_query'] ) && $quiz_query_data['quiz_query'] !== '' ? $quiz_query_data['quiz_query'] : ''; | |
| 392 | + | |
| 393 | + /** | |
| 394 | + * @quiz_query mustn't be empty or not array type data | |
| 395 | + * So checking is array and not empty | |
| 396 | + */ | |
| 397 | + if( $quiz_query !== '' ){ | |
| 398 | + | |
| 399 | + $quiz_id = isset( $quiz_query_data['quiz_id'] ) && $quiz_query_data['quiz_id'] !== '' ? $quiz_query_data['quiz_id'] : 0; | |
| 400 | + | |
| 401 | + $return_results = CBFunctions()->get_quiz_query( $quiz_query, $quiz_id, get_current_user_id() ); | |
| 402 | + | |
| 403 | + $result_values = $return_results['result']; | |
| 404 | + | |
| 405 | + $results = array(); | |
| 406 | + if ( !empty($result_values) ) { | |
| 407 | + if (count($return_results['result'][0]) != 1 ) { | |
| 408 | + $results = array(); | |
| 409 | + $headers = array(); | |
| 410 | + if ( $result_values ) { | |
| 411 | + $row_num = 0; | |
| 412 | + foreach ( $result_values as $row ) { | |
| 413 | + $result = array(); | |
| 414 | + $col_num = 0; | |
| 415 | + foreach ( $row as $k => $v ) { | |
| 416 | + $result[] = $v; | |
| 417 | + if ( $row_num === 0 ) { | |
| 418 | + $headers[] = $k; | |
| 419 | + } | |
| 420 | + } | |
| 421 | + $results[] = $result; | |
| 422 | + $row_num++; | |
| 423 | + } | |
| 424 | + } | |
| 425 | + } else if (count($return_results['result'][0]) == 1) { | |
| 426 | + $results = array(); | |
| 427 | + $headers = array(); | |
| 428 | + if ( $result_values ) { | |
| 429 | + $row_num = 0; | |
| 430 | + foreach ( $result_values as $index => $row ) { | |
| 431 | + $result = array(); | |
| 432 | + foreach ( $row as $k => $v ) { | |
| 433 | + $result[] = strval($index) + 1; | |
| 434 | + $result[] = $v; | |
| 435 | + if ( $row_num === 0 ) { | |
| 436 | + $headers[] = 'Quiz'; | |
| 437 | + $headers[] = $k; | |
| 438 | + } | |
| 439 | + } | |
| 440 | + $results[] = $result; | |
| 441 | + $row_num++; | |
| 442 | + } | |
| 443 | + } | |
| 444 | + } | |
| 445 | + array_unshift($results, $headers); | |
| 446 | + } | |
| 447 | + | |
| 448 | + $filtered_data = $results; | |
| 449 | + } | |
| 450 | + } | |
| 451 | + | |
| 452 | + /** | |
| 453 | + * Returning the data that has passed the validation | |
| 454 | + * Returning applied filter to array for development purposes | |
| 455 | + * | |
| 456 | + * @return array | |
| 457 | + */ | |
| 458 | + return apply_filters( 'ays_chart_source_quiz_maker', $filtered_data ); | |
| 459 | + } | |
| 460 | + | |
| 461 | + /** | |
| 462 | + * Chart data type custom | |
| 463 | + * For developers | |
| 464 | + * Returning data must be an array | |
| 465 | + * | |
| 466 | + * @since 1.0.0 | |
| 467 | + * @access protected | |
| 468 | + * | |
| 469 | + * @accept mixed | |
| 470 | + * @param $source | |
| 471 | + * | |
| 472 | + * @return array | |
| 473 | + */ | |
| 474 | + protected function chart_data_custom( $source, $chart_id ){ | |
| 475 | + | |
| 476 | + /** | |
| 477 | + * Getting the data that will some developers pass via this filter | |
| 478 | + */ | |
| 479 | + $filtered_data = apply_filters( 'ays_chart_source_custom', array(), $chart_id ); | |
| 480 | + | |
| 481 | + /** | |
| 482 | + * Checking that returning data was array | |
| 483 | + */ | |
| 484 | + if( ! is_array( $filtered_data ) ){ | |
| 485 | + return array(); | |
| 486 | + } | |
| 487 | + | |
| 488 | + /** | |
| 489 | + * Checking availability of data in array | |
| 490 | + */ | |
| 491 | + if( empty( $filtered_data ) ){ | |
| 492 | + return array(); | |
| 493 | + } | |
| 494 | + | |
| 495 | + /** | |
| 496 | + * Returning the data that has passed the validation | |
| 497 | + * | |
| 498 | + * @return array | |
| 499 | + */ | |
| 500 | + return $filtered_data; | |
| 501 | + } | |
| 502 | + | |
| 503 | + /** | |
| 504 | + * Validation function for chart general data | |
| 505 | + * | |
| 506 | + * Validating title, description, type, status, etc. | |
| 507 | + * | |
| 508 | + * @since 1.0.0 | |
| 509 | + * @access protected | |
| 510 | + * | |
| 511 | + * @param $chart | |
| 512 | + * | |
| 513 | + * @return array | |
| 514 | + */ | |
| 515 | + protected function validate_item_data( $chart ){ | |
| 516 | + | |
| 517 | + /** | |
| 518 | + * @chart mustn't be empty or not array type data | |
| 519 | + * So checking is array and not empty | |
| 520 | + * | |
| 521 | + * If validation not passed returning an empty array | |
| 522 | + */ | |
| 523 | + if( ! is_array( $chart ) || empty( $chart ) ){ | |
| 524 | + return array(); | |
| 525 | + } | |
| 526 | + | |
| 527 | + /** | |
| 528 | + * Defining an empty array that will return after validation | |
| 529 | + * | |
| 530 | + * During validation array will be filled with valid data | |
| 531 | + */ | |
| 532 | + $chart_data = array(); | |
| 533 | + | |
| 534 | + | |
| 535 | + /** | |
| 536 | + * Validating the @title | |
| 537 | + * | |
| 538 | + * Title mustn't be empty | |
| 539 | + * | |
| 540 | + * Applying esc_html filtering function to escape HTML from the title | |
| 541 | + * Then applying filter to @title for development purposes | |
| 542 | + * | |
| 543 | + * @default "Chart example" | |
| 544 | + */ | |
| 545 | + $chart_title = __( 'Chart example', "chart-builder" ); | |
| 546 | + if( isset( $chart['title'] ) && $chart['title'] !== '' ) { | |
| 547 | + $chart_title = esc_html( $chart['title'] ); | |
| 548 | + } | |
| 549 | + | |
| 550 | + $chart_data['title'] = apply_filters( 'ays_chart_get_title', $chart_title ); | |
| 551 | + | |
| 552 | + | |
| 553 | + /** | |
| 554 | + * Validating the @description | |
| 555 | + * | |
| 556 | + * Description is optional, so it can be empty | |
| 557 | + * Applying stripslashes function to escape unnecessary slashes | |
| 558 | + * | |
| 559 | + * @default empty string | |
| 560 | + */ | |
| 561 | + $chart_data['description'] = ''; | |
| 562 | + if( isset( $chart['description'] ) && $chart['description'] !== '' ){ | |
| 563 | + $chart_data['description'] = stripslashes( $chart['description'] ); | |
| 564 | + } | |
| 565 | + | |
| 566 | + /** | |
| 567 | + * Validating the @author_id | |
| 568 | + */ | |
| 569 | + $chart_data['author_id'] = ( isset( $chart['author_id'] ) && $chart['author_id'] !== '' ) ? esc_html( intval($chart['author_id']) ) : get_current_user_id(); | |
| 570 | + | |
| 571 | + | |
| 572 | + /** | |
| 573 | + * Validating the @type | |
| 574 | + * | |
| 575 | + * Type is required, it can't be empty | |
| 576 | + * Applying esc_html filtering function to escape HTML from the type | |
| 577 | + * | |
| 578 | + * Then applying filter to @type for development purposes | |
| 579 | + * | |
| 580 | + * @accept values are google-charts, etc. | |
| 581 | + * @default google-charts | |
| 582 | + */ | |
| 583 | + $chart_type = 'google-charts'; | |
| 584 | + if( isset( $chart['type'] ) && $chart['type'] !== '' ){ | |
| 585 | + $chart_type = esc_html( $chart['type'] ); | |
| 586 | + } | |
| 587 | + | |
| 588 | + $chart_data['type'] = apply_filters( 'ays_chart_get_type', $chart_type ); | |
| 589 | + | |
| 590 | + /** | |
| 591 | + * Validating the @source_chart_type | |
| 592 | + * | |
| 593 | + * Source chart type is required, it can't be empty | |
| 594 | + * Applying esc_html filtering function to escape HTML from the @source_type | |
| 595 | + * | |
| 596 | + * @accept values are manual, file, WordPress, database, etc. | |
| 597 | + * @default manual | |
| 598 | + */ | |
| 599 | + $chart_data['source_chart_type'] = ( isset( $chart['source_chart_type'] ) && $chart['source_chart_type'] !== '' ) ? esc_html( $chart['source_chart_type'] ) : 'pie_chart'; | |
| 600 | + | |
| 601 | + /** | |
| 602 | + * Validating the @source_type | |
| 603 | + * | |
| 604 | + * Source type is required, it can't be empty | |
| 605 | + * Applying esc_html filtering function to escape HTML from the @source_type | |
| 606 | + * | |
| 607 | + * @accept values are manual, file, WordPress, database, etc. | |
| 608 | + * @default manual | |
| 609 | + */ | |
| 610 | + $chart_data['source_type'] = 'manual'; | |
| 611 | + if( isset( $chart['source_type'] ) && $chart['source_type'] !== '' ){ | |
| 612 | + $chart_data['source_type'] = esc_html( $chart['source_type'] ); | |
| 613 | + } | |
| 614 | + | |
| 615 | + $chart_data['quiz_query'] = isset($chart['quiz_query']) && $chart['quiz_query'] != '' ? sanitize_text_field($chart['quiz_query']) : ''; | |
| 616 | + $chart_data['quiz_id'] = isset( $chart['quiz_id'] ) && $chart['quiz_id'] != '' ? intval( $chart['quiz_id'] ) : 0; | |
| 617 | + | |
| 618 | + /** | |
| 619 | + * Validating the @source | |
| 620 | + * | |
| 621 | + * Source is required, but it can be empty | |
| 622 | + * Applying esc_html filtering function to escape HTML from the @source | |
| 623 | + * | |
| 624 | + * @accept values are JSON, CSV, serialized data, etc. | |
| 625 | + * @default empty string | |
| 626 | + */ | |
| 627 | + $chart_data['source'] = ''; | |
| 628 | + if( isset( $chart['source'] ) && $chart['source'] !== '' ){ | |
| 629 | + $chart_data['source'] = $chart['source'] ; | |
| 630 | + } | |
| 631 | + | |
| 632 | + | |
| 633 | + /** | |
| 634 | + * Validating the @status | |
| 635 | + * | |
| 636 | + * Status is required, it can't be empty | |
| 637 | + * Applying esc_html filtering function to escape HTML from the @status | |
| 638 | + * | |
| 639 | + * Then applying filter to type for development purposes | |
| 640 | + * | |
| 641 | + * @accept values are published and draft | |
| 642 | + * @default draft | |
| 643 | + */ | |
| 644 | + $chart_status = 'draft'; | |
| 645 | + if( isset( $chart['status'] ) && $chart['status'] !== '' ){ | |
| 646 | + $chart_status = esc_html( $chart['status'] ); | |
| 647 | + } | |
| 648 | + | |
| 649 | + $chart_data['status'] = apply_filters( 'ays_chart_get_status', $chart_status ); | |
| 650 | + | |
| 651 | + | |
| 652 | + /** | |
| 653 | + * Validating the @options | |
| 654 | + * Options is optional, it can be empty | |
| 655 | + * | |
| 656 | + * @accept JSON | |
| 657 | + * @default empty string | |
| 658 | + * @return array | |
| 659 | + */ | |
| 660 | + $chart_data['options'] = array(); | |
| 661 | + if( isset( $chart['options'] ) && $chart['options'] !== '' ){ | |
| 662 | + | |
| 663 | + /** | |
| 664 | + * Data are expected to be of type JSON | |
| 665 | + * Trying to parse the data | |
| 666 | + */ | |
| 667 | + $chart_options = json_decode( $chart['options'], true ); | |
| 668 | + | |
| 669 | + /** | |
| 670 | + * Options mustn't be empty or not array type data | |
| 671 | + * So checking is array and not empty | |
| 672 | + * | |
| 673 | + * If validation not passed assigning empty array | |
| 674 | + */ | |
| 675 | + if( ! is_array( $chart_options ) || empty( $chart_options ) ){ | |
| 676 | + $chart_options = array(); | |
| 677 | + } | |
| 678 | + | |
| 679 | + /** | |
| 680 | + * Pushing the result to the returning variable | |
| 681 | + */ | |
| 682 | + $chart_data['options'] = $chart_options; | |
| 683 | + } | |
| 684 | + | |
| 685 | + /** | |
| 686 | + * Returning validated data | |
| 687 | + * Returning an array | |
| 688 | + * | |
| 689 | + * @return array | |
| 690 | + */ | |
| 691 | + return $chart_data; | |
| 692 | + } | |
| 693 | + | |
| 694 | + /** | |
| 695 | + * Returns default array for google charts | |
| 696 | + */ | |
| 697 | + public function get_charts_default_data_google(){ | |
| 698 | + return array( | |
| 699 | + "commonTypeCharts" => array( | |
| 700 | + '0' => array( | |
| 701 | + 'Country', | |
| 702 | + 'Population', | |
| 703 | + ), | |
| 704 | + '1' => array( | |
| 705 | + 'United States', | |
| 706 | + '189', | |
| 707 | + ), | |
| 708 | + '2' => array( | |
| 709 | + 'China', | |
| 710 | + '43', | |
| 711 | + ), | |
| 712 | + '3' => array( | |
| 713 | + 'Egypt', | |
| 714 | + '256', | |
| 715 | + ), | |
| 716 | + '4' => array( | |
| 717 | + 'France', | |
| 718 | + '123', | |
| 719 | + ), | |
| 720 | + '5' => array( | |
| 721 | + 'Australia', | |
| 722 | + '88', | |
| 723 | + ) | |
| 724 | + ), | |
| 725 | + "orgTypeChart" => array( | |
| 726 | + '0' => array( | |
| 727 | + 'Name', | |
| 728 | + 'Description', | |
| 729 | + 'Image', | |
| 730 | + 'Parent name', | |
| 731 | + 'Tooltip', | |
| 732 | + 'Url', | |
| 733 | + 'Parent id', | |
| 734 | + 'Level' | |
| 735 | + ), | |
| 736 | + '1' => array( | |
| 737 | + 'Mike', | |
| 738 | + 'President', | |
| 739 | + '', | |
| 740 | + '', | |
| 741 | + 'The President', | |
| 742 | + '', | |
| 743 | + '', | |
| 744 | + '1' | |
| 745 | + ), | |
| 746 | + '2' => array( | |
| 747 | + 'Jim', | |
| 748 | + 'Vice President', | |
| 749 | + '', | |
| 750 | + 'Mike', | |
| 751 | + 'VP', | |
| 752 | + '', | |
| 753 | + '1', | |
| 754 | + '2' | |
| 755 | + ), | |
| 756 | + '3' => array( | |
| 757 | + 'Bob', | |
| 758 | + '', | |
| 759 | + '', | |
| 760 | + 'Jim', | |
| 761 | + 'Bob Sponge', | |
| 762 | + '', | |
| 763 | + '2', | |
| 764 | + '3' | |
| 765 | + ), | |
| 766 | + '4' => array( | |
| 767 | + 'Carol', | |
| 768 | + '', | |
| 769 | + '', | |
| 770 | + 'Bob', | |
| 771 | + '', | |
| 772 | + '', | |
| 773 | + '3', | |
| 774 | + '4' | |
| 775 | + ), | |
| 776 | + '5' => array( | |
| 777 | + 'Alice', | |
| 778 | + '', | |
| 779 | + '', | |
| 780 | + 'Mike', | |
| 781 | + '', | |
| 782 | + '', | |
| 783 | + '1', | |
| 784 | + '2' | |
| 785 | + ), | |
| 786 | + ) | |
| 787 | + ); | |
| 788 | + } | |
| 789 | + | |
| 790 | + /** | |
| 791 | + * Returns default array for chart.js charts | |
| 792 | + */ | |
| 793 | + public function get_charts_default_data_chartjs(){ | |
| 794 | + return array( | |
| 795 | + '0' => array( | |
| 796 | + 'Country', | |
| 797 | + 'Population' | |
| 798 | + ), | |
| 799 | + '1' => array( | |
| 800 | + 'United States', | |
| 801 | + '189' | |
| 802 | + ), | |
| 803 | + '2' => array( | |
| 804 | + 'China', | |
| 805 | + '43' | |
| 806 | + ), | |
| 807 | + '3' => array( | |
| 808 | + 'Egypt', | |
| 809 | + '256' | |
| 810 | + ), | |
| 811 | + '4' => array( | |
| 812 | + 'France', | |
| 813 | + '123' | |
| 814 | + ), | |
| 815 | + '5' => array( | |
| 816 | + 'Australia', | |
| 817 | + '88' | |
| 818 | + ) | |
| 819 | + ); | |
| 820 | + } | |
| 821 | + | |
| 822 | + public function get_charts($ordering = ''){ | |
| 823 | + global $wpdb; | |
| 824 | + $charts_table = esc_sql( $wpdb->prefix . CHART_BUILDER_DB_PREFIX ) . "charts"; | |
| 825 | + | |
| 826 | + $sql = "SELECT id, title FROM {$charts_table} WHERE `status` IN (%s, %s)"; | |
| 827 | + $params = array('published', 'draft'); | |
| 828 | + | |
| 829 | + if ( $ordering != '' ) { | |
| 830 | + $sql .= " ORDER BY id {$ordering}"; | |
| 831 | + } | |
| 832 | + // phpcs:ignore | |
| 833 | + $charts = $wpdb->get_results( $wpdb->prepare( $sql, ...$params ), 'ARRAY_A' ); | |
| 834 | + | |
| 835 | + | |
| 836 | + return $charts; | |
| 837 | + } | |
| 838 | + } | |
| 839 | +} | |
| 840 | + | |
| 841 | +if( ! function_exists( 'CBActions' ) ){ | |
| 842 | + /** | |
| 843 | + * Function for quick access to Chart_Builder_Actions class | |
| 844 | + * | |
| 845 | + * @since 1.0.0 | |
| 846 | + * @return Chart_Builder_Actions | |
| 847 | + */ | |
| 848 | + function CBActions(){ | |
| 849 | + | |
| 850 | + static $instance = null; | |
| 851 | + | |
| 852 | + if( $instance === null ){ | |
| 853 | + $instance = Chart_Builder_Actions::get_instance( CHART_BUILDER_NAME ); | |
| 854 | + } | |
| 855 | + | |
| 856 | + if( $instance instanceof Chart_Builder_Actions ){ | |
| 857 | + return $instance; | |
| 858 | + } | |
| 859 | + | |
| 860 | + return Chart_Builder_Actions::get_instance( CHART_BUILDER_NAME ); | |
| 861 | + } | |
| 862 | +} | |