← All changes
|
modules/cost-tracker/classes/class-cost-tracker-db.php
+268
-63
5.2
→
6.2.1
View file →
| @@ -13,8 +13,13 @@ | ||
| 13 | 13 | use MainWP\Dashboard\MainWP_DB; |
| 14 | 14 | use MainWP\Dashboard\MainWP_DB_Client; |
| 15 | 15 | use MainWP\Dashboard\MainWP_Exception; |
| 16 | 16 | |
| 17 | +// Exit if accessed directly. | |
| 18 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 19 | + exit; | |
| 20 | +} | |
| 21 | + | |
| 17 | 22 | /** |
| 18 | 23 | * Class Cost_Tracker_DB |
| 19 | 24 | */ |
| 20 | 25 | class Cost_Tracker_DB extends MainWP_Install { |
| @@ -127,12 +132,14 @@ | ||
| 127 | 132 | */ |
| 128 | 133 | public function update_db_cost( $current_version ) { //phpcs:ignore -- NOSONAR - complex. |
| 129 | 134 | if ( ! empty( $current_version ) ) { |
| 130 | 135 | if ( version_compare( $current_version, '1.0.8', '<' ) ) { |
| 131 | - $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'cost_tracker' ) . ' MODIFY COLUMN price decimal(26,8)' ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 136 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 137 | + $this->wpdb->query( "ALTER TABLE {$table} MODIFY COLUMN price decimal(26,8)" ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd and a table name cannot be a prepare() placeholder. | |
| 132 | 138 | } |
| 133 | 139 | if ( version_compare( $current_version, '1.0.9', '<' ) ) { |
| 134 | - $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'cost_tracker' ) . ' DROP COLUMN author' ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 140 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 141 | + $this->wpdb->query( "ALTER TABLE {$table} DROP COLUMN author" ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd and a table name cannot be a prepare() placeholder. | |
| 135 | 142 | } |
| 136 | 143 | |
| 137 | 144 | if ( version_compare( $current_version, '1.0.13', '<' ) ) { |
| 138 | 145 | $costs = $this->get_cost_tracker_by( 'all' ); |
| @@ -229,19 +236,15 @@ | ||
| 229 | 236 | unset( $update['id'] ); |
| 230 | 237 | } |
| 231 | 238 | |
| 232 | 239 | if ( ! empty( $id ) ) { |
| 233 | - | |
| 240 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching -- write operation; caching not applicable for UPDATE. | |
| 234 | 241 | $wpdb->update( $this->table_name( 'cost_tracker' ), $update, array( 'id' => intval( $id ) ) ); |
| 242 | + $this->invalidate_cost_tracker_caches( $id ); | |
| 235 | 243 | return $this->get_cost_tracker_by( 'id', $id ); |
| 236 | - } else { | |
| 237 | - if ( isset( $update['id'] ) ) { | |
| 238 | - unset( $update['id'] ); | |
| 239 | - } | |
| 240 | - | |
| 241 | - if ( $wpdb->insert( $this->table_name( 'cost_tracker' ), $update ) ) { | |
| 242 | - return $this->get_cost_tracker_by( 'id', $wpdb->insert_id ); | |
| 243 | - } | |
| 244 | + } elseif ( $wpdb->insert( $this->table_name( 'cost_tracker' ), $update ) ) { | |
| 245 | + $this->invalidate_cost_tracker_caches(); | |
| 246 | + return $this->get_cost_tracker_by( 'id', $wpdb->insert_id ); | |
| 244 | 247 | } |
| 245 | 248 | return false; |
| 246 | 249 | } |
| 247 | 250 | |
| @@ -260,26 +263,15 @@ | ||
| 260 | 263 | if ( empty( $item_id ) ) { |
| 261 | 264 | return false; |
| 262 | 265 | } |
| 263 | 266 | |
| 264 | - $obj_name = ''; | |
| 267 | + $selected_lists = array( | |
| 268 | + 'site' => is_array( $selected_sites ) ? $selected_sites : array(), | |
| 269 | + 'tag' => is_array( $selected_groups ) ? $selected_groups : array(), | |
| 270 | + 'client' => is_array( $selected_clients ) ? $selected_clients : array(), | |
| 271 | + ); | |
| 265 | 272 | |
| 266 | - $new_obj_ids = array(); | |
| 267 | - | |
| 268 | - if ( ! empty( $selected_sites ) ) { | |
| 269 | - $obj_name = 'site'; | |
| 270 | - $new_obj_ids = $selected_sites; | |
| 271 | - } elseif ( ! empty( $selected_groups ) ) { | |
| 272 | - $obj_name = 'tag'; | |
| 273 | - $new_obj_ids = $selected_groups; | |
| 274 | - } elseif ( ! empty( $selected_clients ) ) { | |
| 275 | - $obj_name = 'client'; | |
| 276 | - $new_obj_ids = $selected_clients; | |
| 277 | - } | |
| 278 | - | |
| 279 | - if ( ! empty( $obj_name ) ) { | |
| 280 | - static::get_instance()->update_lookup_cost( $item_id, $obj_name, $new_obj_ids ); | |
| 281 | - } else { | |
| 273 | + if ( ! array_filter( $selected_lists ) ) { | |
| 282 | 274 | // to support saving cost without selected sites, tags, clients. |
| 283 | 275 | MainWP_DB::instance()->delete_lookup_items( |
| 284 | 276 | 'object_name', |
| 285 | 277 | array( |
| @@ -287,10 +279,17 @@ | ||
| 287 | 279 | 'item_id' => $item_id, |
| 288 | 280 | 'object_names' => array( 'site', 'tag', 'client' ), |
| 289 | 281 | ) |
| 290 | 282 | ); |
| 283 | + return true; | |
| 284 | + } | |
| 291 | 285 | |
| 286 | + // A cost can be saved against sites, tags and clients at once, so each list is | |
| 287 | + // synced against its own object name; an empty list prunes its own rows. | |
| 288 | + foreach ( $selected_lists as $obj_name => $new_obj_ids ) { | |
| 289 | + static::get_instance()->update_lookup_cost( $item_id, $obj_name, $new_obj_ids ); | |
| 292 | 290 | } |
| 291 | + | |
| 293 | 292 | return true; |
| 294 | 293 | } |
| 295 | 294 | |
| 296 | 295 | /** |
| @@ -309,10 +308,8 @@ | ||
| 309 | 308 | if ( empty( $item_id ) || ! is_array( $new_obj_ids ) || ! in_array( $obj_name, $allows ) ) { |
| 310 | 309 | return false; |
| 311 | 310 | } |
| 312 | 311 | |
| 313 | - $remove_obj_names = array_diff( $allows, array( $obj_name ) ); | |
| 314 | - | |
| 315 | 312 | $found_look_ids = array(); |
| 316 | 313 | $existed_look_id = array(); |
| 317 | 314 | |
| 318 | 315 | $results = MainWP_DB::instance()->get_lookup_items( 'cost', $item_id, $obj_name ); |
| @@ -340,20 +337,8 @@ | ||
| 340 | 337 | $remove_ids = array_diff( $found_look_ids, $new_look_ids ); |
| 341 | 338 | if ( $remove_ids ) { |
| 342 | 339 | MainWP_DB::instance()->delete_lookup_items( 'lookup_id', array( 'lookup_ids' => $remove_ids ) ); |
| 343 | 340 | } |
| 344 | - | |
| 345 | - if ( ! empty( $remove_obj_names ) ) { | |
| 346 | - MainWP_DB::instance()->delete_lookup_items( | |
| 347 | - 'object_name', | |
| 348 | - array( | |
| 349 | - 'item_name' => 'cost', | |
| 350 | - 'item_id' => $item_id, | |
| 351 | - 'object_names' => $remove_obj_names, | |
| 352 | - | |
| 353 | - ) | |
| 354 | - ); | |
| 355 | - } | |
| 356 | 341 | } |
| 357 | 342 | |
| 358 | 343 | /** |
| 359 | 344 | * Method get_cost_tracker_by(). |
| @@ -433,9 +418,11 @@ | ||
| 433 | 418 | if ( 'all' === $by ) { |
| 434 | 419 | if ( ! empty( $selected_ids ) ) { |
| 435 | 420 | $where .= ' AND ct.id IN (' . implode( ',', $selected_ids ) . ') '; |
| 436 | 421 | } |
| 437 | - return $wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ) . ' ct WHERE 1 ' . $where . $limit ); //phpcs:ignore -- good. | |
| 422 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 423 | + // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table is escaped with esc_sql(); $where holds id lists from wp_parse_id_list() and values escaped through prepare_fields_array(); $limit is built from intval()ed paging. | |
| 424 | + return $wpdb->get_results( "SELECT * FROM {$table} ct WHERE 1 {$where}{$limit}" ); | |
| 438 | 425 | } |
| 439 | 426 | |
| 440 | 427 | $where = ''; |
| 441 | 428 | $limit = ''; |
| @@ -441,37 +428,86 @@ | ||
| 441 | 428 | $limit = ''; |
| 442 | 429 | |
| 443 | 430 | $sql = ''; |
| 444 | 431 | if ( 'id' === $by && is_numeric( $value ) ) { |
| 445 | - $sql = $wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ) . ' WHERE `id`=%d ', $value ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 446 | - return $wpdb->get_row( $sql, OBJECT ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 432 | + $cache_key = $this->get_cache_key_for_cost_tracker( $by, $value, $params ); | |
| 433 | + $cached = wp_cache_get( $cache_key, 'mainwp_cost_tracker' ); | |
| 434 | + if ( false !== $cached ) { | |
| 435 | + return $cached; | |
| 436 | + } | |
| 437 | + | |
| 438 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 439 | + $sql = $wpdb->prepare( "SELECT * FROM {$table} WHERE `id`=%d ", $value ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd. | |
| 440 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql is safely constructed with $wpdb->prepare() above; table name is escaped with esc_sql(). | |
| 441 | + $result = $wpdb->get_row( $sql, OBJECT ); | |
| 442 | + wp_cache_set( $cache_key, $result, 'mainwp_cost_tracker' ); | |
| 443 | + return $result; | |
| 447 | 444 | } elseif ( 'count' === $by ) { |
| 448 | - $sql = 'SELECT count(*) FROM ' . $this->table_name( 'cost_tracker' ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 449 | - return $wpdb->get_var( $sql ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 445 | + $cache_key = 'mainwp_cost_tracker_' . md5( // NOSONAR - MD5 used for cache key generation only, not cryptographic (security) purposes. | |
| 446 | + wp_json_encode( | |
| 447 | + array( | |
| 448 | + 'method' => 'get_cost_tracker_by', | |
| 449 | + 'by' => 'count', | |
| 450 | + ) | |
| 451 | + ) | |
| 452 | + ); | |
| 453 | + $cached = wp_cache_get( $cache_key, 'mainwp_cost_tracker' ); | |
| 454 | + if ( false !== $cached ) { | |
| 455 | + return $cached; | |
| 456 | + } | |
| 457 | + | |
| 458 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 459 | + $sql = "SELECT count(*) FROM {$table}"; | |
| 460 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql is safely constructed with static table name that is escaped with esc_sql(); no dynamic input. | |
| 461 | + $result = $wpdb->get_var( $sql ); | |
| 462 | + wp_cache_set( $cache_key, $result, 'mainwp_cost_tracker' ); | |
| 463 | + return $result; | |
| 450 | 464 | } elseif ( 'id' === $by && false !== strpos( $value, ',' ) ) { |
| 451 | 465 | $cost_ids = explode( ',', $value ); |
| 452 | 466 | $cost_ids = MainWP_Utility::array_numeric_filter( $cost_ids ); |
| 453 | 467 | if ( ! empty( $cost_ids ) ) { |
| 454 | - $sql = 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ) . ' WHERE `id` IN (' . implode( ',', $cost_ids ) . ' )'; | |
| 468 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 469 | + $sql = "SELECT * FROM {$table} WHERE `id` IN (" . implode( ',', $cost_ids ) . ' )'; | |
| 455 | 470 | } |
| 456 | 471 | } elseif ( 'site_id' === $by || 'client_id' === $by ) { |
| 457 | - $sql = 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ); | |
| 472 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 473 | + $sql = "SELECT * FROM {$table}"; | |
| 458 | 474 | } elseif ( 'slug' === $by && is_string( $value ) ) { |
| 459 | 475 | if ( in_array( $product_type, array( 'plugin', 'theme' ), true ) ) { |
| 460 | - $sql = $wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ) . ' WHERE `slug`=%s AND product_type = %s ', $value, $product_type ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 461 | - return $wpdb->get_row( $sql, OBJECT ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 476 | + $cache_key = $this->get_cache_key_for_cost_tracker( $by, $value, array( 'product_type' => $product_type ) ); | |
| 477 | + $cached = wp_cache_get( $cache_key, 'mainwp_cost_tracker' ); | |
| 478 | + if ( false !== $cached ) { | |
| 479 | + return $cached; | |
| 480 | + } | |
| 481 | + | |
| 482 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 483 | + $sql = $wpdb->prepare( "SELECT * FROM {$table} WHERE `slug`=%s AND product_type = %s ", $value, $product_type ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd. | |
| 484 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $sql is safely constructed with $wpdb->prepare() above; table name is escaped with esc_sql(). | |
| 485 | + $result = $wpdb->get_row( $sql, OBJECT ); | |
| 486 | + wp_cache_set( $cache_key, $result, 'mainwp_cost_tracker' ); | |
| 487 | + return $result; | |
| 462 | 488 | } else { |
| 463 | 489 | $where = ''; |
| 464 | 490 | if ( ! empty( $product_type ) ) { |
| 465 | 491 | $where = $wpdb->prepare( ' AND product_type = %s ', $product_type ); |
| 466 | 492 | } |
| 467 | - $sql = $wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ) . ' WHERE `slug`=%s ' . $where, $value ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 493 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 494 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is esc_sql'd; $where is itself from $wpdb->prepare(), so concatenation into prepare is safe here. | |
| 495 | + $sql = $wpdb->prepare( "SELECT * FROM {$table} WHERE `slug`=%s " . $where, $value ); | |
| 468 | 496 | } |
| 469 | 497 | } |
| 470 | 498 | |
| 471 | 499 | $data = array(); |
| 472 | 500 | if ( ! empty( $sql ) ) { |
| 473 | - $result = $wpdb->get_results( $sql, OBJECT ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 501 | + $cache_key = $this->get_cache_key_for_cost_tracker( $by, $value, $params ); | |
| 502 | + $cached = wp_cache_get( $cache_key, 'mainwp_cost_tracker' ); | |
| 503 | + if ( false !== $cached ) { | |
| 504 | + return $cached; | |
| 505 | + } | |
| 506 | + | |
| 507 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql is safely constructed with $wpdb->prepare() above; table name is escaped with esc_sql(). | |
| 508 | + $result = $wpdb->get_results( $sql, OBJECT ); | |
| 509 | + wp_cache_set( $cache_key, $result, 'mainwp_cost_tracker' ); | |
| 474 | 510 | if ( $result ) { |
| 475 | 511 | if ( 'site_id' === $by ) { |
| 476 | 512 | $site_id = intval( $value ); |
| 477 | 513 | foreach ( $result as $cost ) { |
| @@ -542,19 +578,39 @@ | ||
| 542 | 578 | } |
| 543 | 579 | $deleted = false; |
| 544 | 580 | |
| 545 | 581 | if ( 'id' === $by ) { |
| 546 | - if ( $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'cost_tracker' ) . ' WHERE id=%d ', $value ) ) ) { //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 582 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 583 | + if ( $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE id=%d ", $value ) ) ) { //phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- delete operation that invalidates caches below; table name is esc_sql'd. | |
| 547 | 584 | $deleted = true; |
| 548 | 585 | } |
| 549 | - } elseif ( $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'cost_tracker' ) . ' WHERE site_id=%d ', $value ) ) ) { //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 550 | - $deleted = true; | |
| 586 | + } else { | |
| 587 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 588 | + if ( $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE site_id=%d ", $value ) ) ) { //phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- delete operation that invalidates caches below; table name is esc_sql'd. | |
| 589 | + $deleted = true; | |
| 590 | + } | |
| 551 | 591 | } |
| 552 | 592 | |
| 553 | 593 | if ( $deleted ) { |
| 554 | - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'lookup_item_objects' ) . ' WHERE item_id=%d AND item_name = "cost"', $value ) ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 594 | + $lookup_table = esc_sql( $this->table_name( 'lookup_item_objects' ) ); | |
| 595 | + $wpdb->query( $wpdb->prepare( "DELETE FROM {$lookup_table} WHERE item_id=%d AND item_name = \"cost\"", $value ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- delete operation; caching is not applicable for write operations; table name is esc_sql'd. | |
| 596 | + $this->invalidate_cost_tracker_caches( 'id' === $by ? $value : null ); | |
| 555 | 597 | } |
| 556 | 598 | |
| 599 | + $snapshot_info = Cost_Tracker_Admin::get_costs_snapshot_info(); | |
| 600 | + | |
| 601 | + /** | |
| 602 | + * Action mainwp_cost_tracker_deleted. | |
| 603 | + * | |
| 604 | + * @param string $by Delete by. | |
| 605 | + * @param mixed $value Value. | |
| 606 | + * @param bool $deleted Deleted. | |
| 607 | + * @param array $snapshot_info costs snapshot info. | |
| 608 | + * | |
| 609 | + * @since 6.0 | |
| 610 | + */ | |
| 611 | + do_action( 'mainwp_cost_tracker_deleted', $by, $value, $deleted, $snapshot_info ); | |
| 612 | + | |
| 557 | 613 | return $deleted; |
| 558 | 614 | } |
| 559 | 615 | |
| 560 | 616 | /** |
| @@ -598,10 +654,20 @@ | ||
| 598 | 654 | global $wpdb; |
| 599 | 655 | |
| 600 | 656 | $clients_costs = array(); |
| 601 | 657 | $clients_costs_ids = array(); |
| 602 | - $sql = 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ); | |
| 603 | - $result = $wpdb->get_results( $sql, OBJECT ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- get all cost trackers. | |
| 658 | + | |
| 659 | + $cache_key = 'mainwp_cost_tracker_all'; | |
| 660 | + $result = wp_cache_get( $cache_key, 'mainwp_cost_tracker' ); | |
| 661 | + | |
| 662 | + if ( false === $result ) { | |
| 663 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 664 | + $sql = "SELECT * FROM {$table}"; | |
| 665 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql is safely constructed with $wpdb->prepare() above; table name is escaped with esc_sql(); results cached immediately after retrieval | |
| 666 | + $result = $wpdb->get_results( $sql, OBJECT ); | |
| 667 | + wp_cache_set( $cache_key, $result, 'mainwp_cost_tracker' ); | |
| 668 | + } | |
| 669 | + | |
| 604 | 670 | if ( $result ) { |
| 605 | 671 | foreach ( $result as $cost ) { |
| 606 | 672 | $sites = ! empty( $cost->sites ) ? json_decode( $cost->sites, true ) : array(); |
| 607 | 673 | $groups = ! empty( $cost->groups ) ? json_decode( $cost->groups, true ) : array(); |
| @@ -695,9 +761,51 @@ | ||
| 695 | 761 | |
| 696 | 762 | return $clients_costs; |
| 697 | 763 | } |
| 698 | 764 | |
| 765 | + /** | |
| 766 | + * Method get_sites_of_cost(). | |
| 767 | + * | |
| 768 | + * Get sites of cost. | |
| 769 | + * | |
| 770 | + * @param object $cost Cost data. | |
| 771 | + * | |
| 772 | + * @return mixed Result | |
| 773 | + */ | |
| 774 | + public function get_sites_of_cost( $cost ) { //phpcs:ignore -- NOSONAR - complex method. | |
| 699 | 775 | |
| 776 | + if ( empty( $cost ) || ! is_object( $cost ) ) { | |
| 777 | + return array(); | |
| 778 | + } | |
| 779 | + | |
| 780 | + $sites = ! empty( $cost->sites ) ? json_decode( $cost->sites, true ) : array(); // NOSONAR -- duplicate. | |
| 781 | + $groups = ! empty( $cost->groups ) ? json_decode( $cost->groups, true ) : array(); // NOSONAR -- duplicate. | |
| 782 | + $clients = ! empty( $cost->clients ) ? json_decode( $cost->clients, true ) : array(); // NOSONAR -- duplicate. | |
| 783 | + | |
| 784 | + if ( ! is_array( $sites ) ) { // NOSONAR -- duplicate. | |
| 785 | + $sites = array(); // NOSONAR -- duplicate. | |
| 786 | + } | |
| 787 | + if ( ! is_array( $groups ) ) { // NOSONAR -- duplicate. | |
| 788 | + $groups = array(); // NOSONAR -- duplicate. | |
| 789 | + } | |
| 790 | + if ( ! is_array( $clients ) ) { // NOSONAR -- duplicate. | |
| 791 | + $clients = array(); // NOSONAR -- duplicate. | |
| 792 | + } | |
| 793 | + | |
| 794 | + if ( empty( $sites ) && empty( $groups ) && empty( $clients ) ) { // NOSONAR -- duplicate. | |
| 795 | + return array(); // NOSONAR -- duplicate. | |
| 796 | + } | |
| 797 | + | |
| 798 | + $params = array( // NOSONAR -- duplicate. | |
| 799 | + 'sites' => $sites, // NOSONAR -- duplicate. | |
| 800 | + 'groups' => $groups, // NOSONAR -- duplicate. | |
| 801 | + 'clients' => $clients, // NOSONAR -- duplicate. | |
| 802 | + ); | |
| 803 | + | |
| 804 | + return MainWP_DB::instance()->get_db_sites( $params ); // NOSONAR - get sites of cost tracker. | |
| 805 | + } | |
| 806 | + | |
| 807 | + | |
| 700 | 808 | /** |
| 701 | 809 | * Method get_all_cost_trackers_by_sites(). |
| 702 | 810 | * |
| 703 | 811 | * @param array $sites_ids Sites ids. |
| @@ -719,10 +827,20 @@ | ||
| 719 | 827 | global $wpdb; |
| 720 | 828 | |
| 721 | 829 | $sites_costs = array(); |
| 722 | 830 | $sites_costs_ids = array(); |
| 723 | - $sql = 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ); | |
| 724 | - $result = $wpdb->get_results( $sql, OBJECT ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 831 | + | |
| 832 | + $cache_key = 'mainwp_cost_tracker_all'; | |
| 833 | + $result = wp_cache_get( $cache_key, 'mainwp_cost_tracker' ); | |
| 834 | + | |
| 835 | + if ( false === $result ) { | |
| 836 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 837 | + $sql = "SELECT * FROM {$table}"; | |
| 838 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql is safely constructed with $wpdb->prepare() above; table name is escaped with esc_sql(); results cached immediately after retrieval | |
| 839 | + $result = $wpdb->get_results( $sql, OBJECT ); | |
| 840 | + wp_cache_set( $cache_key, $result, 'mainwp_cost_tracker' ); | |
| 841 | + } | |
| 842 | + | |
| 725 | 843 | if ( $result ) { |
| 726 | 844 | foreach ( $result as $cost ) { |
| 727 | 845 | $sites = ! empty( $cost->sites ) ? json_decode( $cost->sites, true ) : array(); |
| 728 | 846 | $groups = ! empty( $cost->groups ) ? json_decode( $cost->groups, true ) : array(); |
| @@ -816,14 +934,31 @@ | ||
| 816 | 934 | $sum_data = isset( $params['sum_data'] ) ? $params['sum_data'] : ''; |
| 817 | 935 | $where = ''; |
| 818 | 936 | $sql = ''; |
| 819 | 937 | if ( 'all' === $sum_data ) { |
| 938 | + $cache_key = 'mainwp_cost_tracker_' . md5( // NOSONAR - MD5 used for cache key generation only, not cryptographic (security) purposes. | |
| 939 | + wp_json_encode( | |
| 940 | + array( | |
| 941 | + 'method' => 'get_summary_data', | |
| 942 | + 'sum_data' => 'all', | |
| 943 | + ) | |
| 944 | + ) | |
| 945 | + ); | |
| 946 | + $cached = wp_cache_get( $cache_key, 'mainwp_cost_tracker' ); | |
| 947 | + if ( false !== $cached ) { | |
| 948 | + return $cached; | |
| 949 | + } | |
| 950 | + | |
| 820 | 951 | $where .= ' AND co.cost_status = "active" AND co.type = "subscription" '; |
| 821 | - $sql .= 'SELECT * FROM ' . $this->table_name( 'cost_tracker' ) . ' co WHERE 1 ' . $where . ' ORDER BY co.next_renewal ASC '; | |
| 952 | + $table = esc_sql( $this->table_name( 'cost_tracker' ) ); | |
| 953 | + $sql = "SELECT * FROM {$table} co WHERE 1 {$where} ORDER BY co.next_renewal ASC "; | |
| 954 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql is safely constructed with $wpdb->prepare() above; table name is escaped with esc_sql(). | |
| 955 | + $result = $wpdb->get_results( $sql ); | |
| 956 | + wp_cache_set( $cache_key, $result, 'mainwp_cost_tracker' ); | |
| 957 | + return $result; | |
| 822 | 958 | } else { |
| 823 | 959 | return false; |
| 824 | 960 | } |
| 825 | - return $wpdb->get_results( $sql ); //phpcs:ignore -- good. | |
| 826 | 961 | } |
| 827 | 962 | |
| 828 | 963 | /** |
| 829 | 964 | * Method update_next_renewal_today() |
| @@ -846,6 +981,76 @@ | ||
| 846 | 981 | } |
| 847 | 982 | } |
| 848 | 983 | } |
| 849 | 984 | update_option( 'module_cost_tracker_calc_today_next_renewal', gmdate( 'Y-m-d' ) ); |
| 985 | + } | |
| 986 | + | |
| 987 | + /** | |
| 988 | + * Generate cache key for get_cost_tracker_by queries. | |
| 989 | + * | |
| 990 | + * @param string $by Query type. | |
| 991 | + * @param mixed $value Query value. | |
| 992 | + * @param array $params Additional parameters. | |
| 993 | + * | |
| 994 | + * @return string Cache key. | |
| 995 | + */ | |
| 996 | + private function get_cache_key_for_cost_tracker( $by, $value, $params = array() ) { | |
| 997 | + $identifier = wp_json_encode( | |
| 998 | + array( | |
| 999 | + 'method' => 'get_cost_tracker_by', | |
| 1000 | + 'by' => $by, | |
| 1001 | + 'value' => $value, | |
| 1002 | + 'params' => $params, | |
| 1003 | + ) | |
| 1004 | + ); | |
| 1005 | + return 'mainwp_cost_tracker_' . md5( $identifier ); // NOSONAR - MD5 used for cache key generation only, not cryptographic (security) purposes. | |
| 1006 | + } | |
| 1007 | + | |
| 1008 | + /** | |
| 1009 | + * Invalidate cost tracker caches. | |
| 1010 | + * | |
| 1011 | + * @param int|null $cost_id Cost ID to invalidate specific caches, or null for all caches. | |
| 1012 | + * | |
| 1013 | + * @return void | |
| 1014 | + */ | |
| 1015 | + private function invalidate_cost_tracker_caches( $cost_id = null ) { | |
| 1016 | + wp_cache_delete( | |
| 1017 | + 'mainwp_cost_tracker_' . md5( // NOSONAR - MD5 used for cache key generation only, not cryptographic (security) purposes. | |
| 1018 | + wp_json_encode( | |
| 1019 | + array( | |
| 1020 | + 'method' => 'get_cost_tracker_by', | |
| 1021 | + 'by' => 'count', | |
| 1022 | + ) | |
| 1023 | + ) | |
| 1024 | + ), | |
| 1025 | + 'mainwp_cost_tracker' | |
| 1026 | + ); | |
| 1027 | + wp_cache_delete( 'mainwp_cost_tracker_all', 'mainwp_cost_tracker' ); | |
| 1028 | + wp_cache_delete( | |
| 1029 | + 'mainwp_cost_tracker_' . md5( // NOSONAR - MD5 used for cache key generation only, not cryptographic (security) purposes. | |
| 1030 | + wp_json_encode( | |
| 1031 | + array( | |
| 1032 | + 'method' => 'get_summary_data', | |
| 1033 | + 'sum_data' => 'all', | |
| 1034 | + ) | |
| 1035 | + ) | |
| 1036 | + ), | |
| 1037 | + 'mainwp_cost_tracker' | |
| 1038 | + ); | |
| 1039 | + | |
| 1040 | + if ( $cost_id ) { | |
| 1041 | + $cost_id = absint( $cost_id ); | |
| 1042 | + wp_cache_delete( | |
| 1043 | + 'mainwp_cost_tracker_' . md5( // NOSONAR - MD5 used for cache key generation only, not cryptographic (security) purposes. | |
| 1044 | + wp_json_encode( | |
| 1045 | + array( | |
| 1046 | + 'method' => 'get_cost_tracker_by', | |
| 1047 | + 'by' => 'id', | |
| 1048 | + 'value' => $cost_id, | |
| 1049 | + ) | |
| 1050 | + ) | |
| 1051 | + ), | |
| 1052 | + 'mainwp_cost_tracker' | |
| 1053 | + ); | |
| 1054 | + } | |
| 850 | 1055 | } |
| 851 | 1056 | } |