Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Give_Cache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Cache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Give_Cache { |
||
|
|
|||
| 19 | /** |
||
| 20 | * Instance. |
||
| 21 | * |
||
| 22 | * @since 1.8.7 |
||
| 23 | * @access private |
||
| 24 | * @var Give_Cache |
||
| 25 | */ |
||
| 26 | static private $instance; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Flag to check if caching enabled or not. |
||
| 30 | * |
||
| 31 | * @since 2.0 |
||
| 32 | * @access private |
||
| 33 | * @var |
||
| 34 | */ |
||
| 35 | private $is_cache; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Singleton pattern. |
||
| 39 | * |
||
| 40 | * @since 1.8.7 |
||
| 41 | * @access private |
||
| 42 | * Give_Cache constructor. |
||
| 43 | */ |
||
| 44 | private function __construct() { |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Get instance. |
||
| 50 | * |
||
| 51 | * @since 1.8.7 |
||
| 52 | * @access public |
||
| 53 | * @return static |
||
| 54 | */ |
||
| 55 | public static function get_instance() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Setup hooks. |
||
| 65 | * |
||
| 66 | * @since 1.8.7 |
||
| 67 | * @access public |
||
| 68 | */ |
||
| 69 | public function setup() { |
||
| 70 | // Currently enable cache only for backend. |
||
| 71 | self::$instance->is_cache = give_is_setting_enabled( give_get_option( 'cache', 'enabled' ) ) && is_admin(); |
||
| 72 | |||
| 73 | // weekly delete all expired cache. |
||
| 74 | Give_Cron::add_weekly_event( array( $this, 'delete_all_expired' ) ); |
||
| 75 | |||
| 76 | add_action( 'save_post_give_forms', array( $this, 'delete_form_related_cache' ) ); |
||
| 77 | add_action( 'save_post_give_payment', array( $this, 'delete_payment_related_cache' ) ); |
||
| 78 | add_action( 'give_deleted_give-donors_cache', array( $this, 'delete_donor_related_cache' ), 10, 3 ); |
||
| 79 | add_action( 'give_deleted_give-donations_cache', array( $this, 'delete_donations_related_cache' ), 10, 3 ); |
||
| 80 | |||
| 81 | add_action( 'give_save_settings_give_settings', array( $this, 'flush_cache' ) ); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get cache key. |
||
| 86 | * |
||
| 87 | * @since 1.8.7 |
||
| 88 | * |
||
| 89 | * @param string $action Cache key prefix. |
||
| 90 | * @param array $query_args (optional) Query array. |
||
| 91 | * @param bool $is_prefix |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | |||
| 96 | public static function get_key( $action, $query_args = null, $is_prefix = true ) { |
||
| 97 | // Bailout. |
||
| 98 | if ( empty( $action ) ) { |
||
| 99 | return new WP_Error( 'give_invalid_cache_key_action', __( 'Do not pass empty action to generate cache key.', 'give' ) ); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Set cache key. |
||
| 103 | $cache_key = $is_prefix ? "give_cache_{$action}" : $action; |
||
| 104 | |||
| 105 | // Bailout. |
||
| 106 | if ( ! empty( $query_args ) ) { |
||
| 107 | $cache_key = "{$cache_key}_" . substr( md5( serialize( $query_args ) ), 0, 15 ); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Filter the cache key name. |
||
| 112 | * |
||
| 113 | * @since 2.0 |
||
| 114 | */ |
||
| 115 | return apply_filters( 'give_get_cache_key', $cache_key, $action, $query_args ); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get cache. |
||
| 120 | * |
||
| 121 | * @since 1.8.7 |
||
| 122 | * |
||
| 123 | * @param string $cache_key |
||
| 124 | * @param bool $custom_key |
||
| 125 | * @param mixed $query_args |
||
| 126 | * |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | |||
| 130 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 131 | View Code Duplication | if ( ! self::is_valid_cache_key( $cache_key ) ) { |
|
| 132 | if ( ! $custom_key ) { |
||
| 133 | return new WP_Error( 'give_invalid_cache_key', __( 'Cache key format should be give_cache_*', 'give' ) ); |
||
| 134 | } |
||
| 135 | |||
| 136 | $cache_key = self::get_key( $cache_key, $query_args ); |
||
| 137 | } |
||
| 138 | |||
| 139 | $option = get_option( $cache_key ); |
||
| 140 | |||
| 141 | // Backward compatibility (<1.8.7). |
||
| 142 | if ( ! is_array( $option ) || empty( $option ) || ! array_key_exists( 'expiration', $option ) ) { |
||
| 143 | return $option; |
||
| 144 | } |
||
| 145 | |||
| 146 | // Get current time. |
||
| 147 | $current_time = current_time( 'timestamp', 1 ); |
||
| 148 | |||
| 149 | if ( empty( $option['expiration'] ) || ( $current_time < $option['expiration'] ) ) { |
||
| 150 | $option = $option['data']; |
||
| 151 | } else { |
||
| 152 | $option = false; |
||
| 153 | } |
||
| 154 | |||
| 155 | return $option; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Set cache. |
||
| 160 | * |
||
| 161 | * @since 1.8.7 |
||
| 162 | * |
||
| 163 | * @param string $cache_key |
||
| 164 | * @param mixed $data |
||
| 165 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 166 | * @param bool $custom_key |
||
| 167 | * @param mixed $query_args |
||
| 168 | * |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | |||
| 172 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 173 | View Code Duplication | if ( ! self::is_valid_cache_key( $cache_key ) ) { |
|
| 174 | if ( ! $custom_key ) { |
||
| 175 | return new WP_Error( 'give_invalid_cache_key', __( 'Cache key format should be give_cache_*', 'give' ) ); |
||
| 176 | } |
||
| 177 | |||
| 178 | $cache_key = self::get_key( $cache_key, $query_args ); |
||
| 179 | } |
||
| 180 | |||
| 181 | $option_value = array( |
||
| 182 | 'data' => $data, |
||
| 183 | 'expiration' => ! is_null( $expiration ) |
||
| 184 | ? ( $expiration + current_time( 'timestamp', 1 ) ) |
||
| 185 | : null, |
||
| 186 | ); |
||
| 187 | |||
| 188 | $result = update_option( $cache_key, $option_value, 'no' ); |
||
| 189 | |||
| 190 | return $result; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Delete cache. |
||
| 195 | * |
||
| 196 | * Note: only for internal use |
||
| 197 | * |
||
| 198 | * @since 1.8.7 |
||
| 199 | * |
||
| 200 | * @param string|array $cache_keys |
||
| 201 | * |
||
| 202 | * @return bool|WP_Error |
||
| 203 | */ |
||
| 204 | |||
| 205 | public static function delete( $cache_keys ) { |
||
| 206 | $result = true; |
||
| 207 | $invalid_keys = array(); |
||
| 208 | |||
| 209 | if ( ! empty( $cache_keys ) ) { |
||
| 210 | $cache_keys = is_array( $cache_keys ) ? $cache_keys : array( $cache_keys ); |
||
| 211 | |||
| 212 | foreach ( $cache_keys as $cache_key ) { |
||
| 213 | if ( ! self::is_valid_cache_key( $cache_key ) ) { |
||
| 214 | $invalid_keys[] = $cache_key; |
||
| 215 | $result = false; |
||
| 216 | } |
||
| 217 | |||
| 218 | delete_option( $cache_key ); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | if ( ! $result ) { |
||
| 223 | $result = new WP_Error( |
||
| 224 | 'give_invalid_cache_key', |
||
| 225 | __( 'Cache key format should be give_cache_*', 'give' ), |
||
| 226 | $invalid_keys |
||
| 227 | ); |
||
| 228 | } |
||
| 229 | |||
| 230 | return $result; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Delete all logging cache. |
||
| 235 | * |
||
| 236 | * Note: only for internal use |
||
| 237 | * |
||
| 238 | * @since 1.8.7 |
||
| 239 | * @access public |
||
| 240 | * @global wpdb $wpdb |
||
| 241 | * |
||
| 242 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public static function delete_all_expired( $force = false ) { |
||
| 247 | global $wpdb; |
||
| 248 | $options = $wpdb->get_results( |
||
| 249 | $wpdb->prepare( |
||
| 250 | "SELECT option_name, option_value |
||
| 251 | FROM {$wpdb->options} |
||
| 252 | Where option_name |
||
| 253 | LIKE '%%%s%%'", |
||
| 254 | 'give_cache' |
||
| 255 | ), |
||
| 256 | ARRAY_A |
||
| 257 | ); |
||
| 258 | |||
| 259 | // Bailout. |
||
| 260 | if ( empty( $options ) ) { |
||
| 261 | return false; |
||
| 262 | } |
||
| 263 | |||
| 264 | $current_time = current_time( 'timestamp', 1 ); |
||
| 265 | |||
| 266 | // Delete log cache. |
||
| 267 | foreach ( $options as $option ) { |
||
| 268 | $option['option_value'] = maybe_unserialize( $option['option_value'] ); |
||
| 269 | |||
| 270 | if ( |
||
| 271 | ( |
||
| 272 | ! self::is_valid_cache_key( $option['option_name'] ) |
||
| 273 | || ! is_array( $option['option_value'] ) // Backward compatibility (<1.8.7). |
||
| 274 | || ! array_key_exists( 'expiration', $option['option_value'] ) // Backward compatibility (<1.8.7). |
||
| 275 | || empty( $option['option_value']['expiration'] ) |
||
| 276 | || ( $current_time < $option['option_value']['expiration'] ) |
||
| 277 | ) |
||
| 278 | && ! $force |
||
| 279 | ) { |
||
| 280 | continue; |
||
| 281 | } |
||
| 282 | |||
| 283 | self::delete( $option['option_name'] ); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * Get list of options like. |
||
| 290 | * |
||
| 291 | * Note: only for internal use |
||
| 292 | * |
||
| 293 | * @since 1.8.7 |
||
| 294 | * @access public |
||
| 295 | * |
||
| 296 | * @param string $option_name |
||
| 297 | * @param bool $fields |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 302 | global $wpdb; |
||
| 303 | |||
| 304 | if ( empty( $option_name ) ) { |
||
| 305 | return array(); |
||
| 306 | } |
||
| 307 | |||
| 308 | $field_names = $fields ? 'option_name, option_value' : 'option_name'; |
||
| 309 | |||
| 310 | if ( $fields ) { |
||
| 311 | $options = $wpdb->get_results( |
||
| 312 | $wpdb->prepare( |
||
| 313 | "SELECT {$field_names } |
||
| 314 | FROM {$wpdb->options} |
||
| 315 | Where option_name |
||
| 316 | LIKE '%%%s%%'", |
||
| 317 | "give_cache_{$option_name}" |
||
| 318 | ), |
||
| 319 | ARRAY_A |
||
| 320 | ); |
||
| 321 | } else { |
||
| 322 | $options = $wpdb->get_col( |
||
| 323 | $wpdb->prepare( |
||
| 324 | "SELECT * |
||
| 325 | FROM {$wpdb->options} |
||
| 326 | Where option_name |
||
| 327 | LIKE '%%%s%%'", |
||
| 328 | "give_cache_{$option_name}" |
||
| 329 | ), |
||
| 330 | 1 |
||
| 331 | ); |
||
| 332 | } |
||
| 333 | |||
| 334 | if ( ! empty( $options ) && $fields ) { |
||
| 335 | foreach ( $options as $index => $option ) { |
||
| 336 | $option['option_value'] = maybe_unserialize( $option['option_value'] ); |
||
| 337 | $options[ $index ] = $option; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | return $options; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Check cache key validity. |
||
| 346 | * |
||
| 347 | * @since 1.8.7 |
||
| 348 | * @access public |
||
| 349 | * |
||
| 350 | * @param $cache_key |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | public static function is_valid_cache_key( $cache_key ) { |
||
| 355 | $is_valid = ( false !== strpos( $cache_key, 'give_cache_' ) ); |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * Filter the flag which tell about cache key valid or not |
||
| 360 | * |
||
| 361 | * @since 2.0 |
||
| 362 | */ |
||
| 363 | return apply_filters( 'give_is_valid_cache_key', $is_valid, $cache_key ); |
||
| 364 | } |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Get cache from group |
||
| 369 | * |
||
| 370 | * @since 2.0 |
||
| 371 | * @access public |
||
| 372 | * |
||
| 373 | * @param int $id |
||
| 374 | * @param string $group |
||
| 375 | * |
||
| 376 | * @return mixed |
||
| 377 | */ |
||
| 378 | View Code Duplication | public static function get_group( $id, $group = '' ) { |
|
| 379 | $cached_data = false; |
||
| 380 | |||
| 381 | // Bailout. |
||
| 382 | if ( ! self::$instance->is_cache || empty( $id ) ) { |
||
| 383 | return $cached_data; |
||
| 384 | } |
||
| 385 | |||
| 386 | $group = self::$instance->filter_group_name( $group ); |
||
| 387 | |||
| 388 | $cached_data = wp_cache_get( $id, $group ); |
||
| 389 | |||
| 390 | return $cached_data; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Cache small chunks inside group |
||
| 395 | * |
||
| 396 | * @since 2.0 |
||
| 397 | * @access public |
||
| 398 | * |
||
| 399 | * @param int $id |
||
| 400 | * @param mixed $data |
||
| 401 | * @param string $group |
||
| 402 | * @param int $expire |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | View Code Duplication | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
|
| 407 | $status = false; |
||
| 408 | |||
| 409 | // Bailout. |
||
| 410 | if ( ! self::$instance->is_cache || empty( $id ) ) { |
||
| 411 | return $status; |
||
| 412 | } |
||
| 413 | |||
| 414 | $group = self::$instance->filter_group_name( $group ); |
||
| 415 | |||
| 416 | $status = wp_cache_set( $id, $data, $group, $expire ); |
||
| 417 | |||
| 418 | return $status; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Delete group cache |
||
| 423 | * |
||
| 424 | * @since 2.0 |
||
| 425 | * @access public |
||
| 426 | * |
||
| 427 | * @param int|array $ids |
||
| 428 | * @param string $group |
||
| 429 | * @param int $expire |
||
| 430 | * |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | public static function delete_group( $ids, $group = '', $expire = 0 ) { |
||
| 434 | $status = false; |
||
| 435 | |||
| 436 | // Bailout. |
||
| 437 | if ( ! self::$instance->is_cache || empty( $ids ) ) { |
||
| 438 | return $status; |
||
| 439 | } |
||
| 440 | |||
| 441 | $group = self::$instance->filter_group_name( $group ); |
||
| 442 | |||
| 443 | // Delete single or multiple cache items from cache. |
||
| 444 | if ( ! is_array( $ids ) ) { |
||
| 445 | $status = wp_cache_delete( $ids, $group, $expire ); |
||
| 446 | self::$instance->get_incrementer( true ); |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Fire action when cache deleted for specific id. |
||
| 450 | * |
||
| 451 | * @since 2.0 |
||
| 452 | * |
||
| 453 | * @param string $ids |
||
| 454 | * @param string $group |
||
| 455 | * @param int $expire |
||
| 456 | */ |
||
| 457 | do_action( "give_deleted_{$group}_cache", $ids, $group, $expire, $status ); |
||
| 458 | |||
| 459 | } else { |
||
| 460 | foreach ( $ids as $id ) { |
||
| 461 | $status = wp_cache_delete( $id, $group, $expire ); |
||
| 462 | self::$instance->get_incrementer( true ); |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Fire action when cache deleted for specific id . |
||
| 466 | * |
||
| 467 | * @since 2.0 |
||
| 468 | * |
||
| 469 | * @param string $ids |
||
| 470 | * @param string $group |
||
| 471 | * @param int $expire |
||
| 472 | */ |
||
| 473 | do_action( "give_deleted_{$group}_cache", $id, $group, $expire, $status ); |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | return $status; |
||
| 478 | } |
||
| 479 | |||
| 480 | |||
| 481 | /** |
||
| 482 | * Delete form related cache |
||
| 483 | * Note: only use for internal purpose. |
||
| 484 | * |
||
| 485 | * @since 2.0 |
||
| 486 | * @access public |
||
| 487 | * |
||
| 488 | * @param int $form_id |
||
| 489 | */ |
||
| 490 | public function delete_form_related_cache( $form_id ) { |
||
| 491 | // If this is just a revision, don't send the email. |
||
| 492 | if ( wp_is_post_revision( $form_id ) ) { |
||
| 493 | return; |
||
| 494 | } |
||
| 495 | |||
| 496 | $donation_query = new Give_Payments_Query( |
||
| 497 | array( |
||
| 498 | 'number' => - 1, |
||
| 499 | 'give_forms' => $form_id |
||
| 500 | ) |
||
| 501 | ); |
||
| 502 | |||
| 503 | $donations = $donation_query->get_payments(); |
||
| 504 | |||
| 505 | if ( ! empty( $donations ) ) { |
||
| 506 | /* @var Give_Payment $donation */ |
||
| 507 | foreach ( $donations as $donation ) { |
||
| 508 | wp_cache_delete( $donation->ID, 'give-donations' ); |
||
| 509 | wp_cache_delete( $donation->donor_id, 'give-donors' ); |
||
| 510 | } |
||
| 511 | } |
||
| 512 | |||
| 513 | self::$instance->get_incrementer( true ); |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Delete payment related cache |
||
| 518 | * Note: only use for internal purpose. |
||
| 519 | * |
||
| 520 | * @since 2.0 |
||
| 521 | * @access public |
||
| 522 | * |
||
| 523 | * @param int $donation_id |
||
| 524 | */ |
||
| 525 | public function delete_payment_related_cache( $donation_id ) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Delete donor related cache |
||
| 545 | * Note: only use for internal purpose. |
||
| 546 | * |
||
| 547 | * @since 2.0 |
||
| 548 | * @access public |
||
| 549 | * |
||
| 550 | * @param string $id |
||
| 551 | * @param string $group |
||
| 552 | * @param int $expire |
||
| 553 | */ |
||
| 554 | public function delete_donor_related_cache( $id, $group, $expire ) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Delete donations related cache |
||
| 569 | * Note: only use for internal purpose. |
||
| 570 | * |
||
| 571 | * @since 2.0 |
||
| 572 | * @access public |
||
| 573 | * |
||
| 574 | * @param string $id |
||
| 575 | * @param string $group |
||
| 576 | * @param int $expire |
||
| 577 | */ |
||
| 578 | public function delete_donations_related_cache( $id, $group, $expire ) { |
||
| 588 | |||
| 589 | |||
| 590 | /** |
||
| 591 | * Get unique incrementer. |
||
| 592 | * |
||
| 593 | * @see https://core.trac.wordpress.org/ticket/4476 |
||
| 594 | * @see https://www.tollmanz.com/invalidation-schemes/ |
||
| 595 | * |
||
| 596 | * @since 2.0 |
||
| 597 | * @access private |
||
| 598 | * |
||
| 599 | * @param bool $refresh |
||
| 600 | * @param string $incrementer_key |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | private function get_incrementer( $refresh = false, $incrementer_key = 'give-cahce-incrementer-db-queries' ) { |
||
| 614 | |||
| 615 | |||
| 616 | /** |
||
| 617 | * Flush cache on cache setting enable/disable |
||
| 618 | * Note: only for internal use |
||
| 619 | * |
||
| 620 | * @since 2.0 |
||
| 621 | * @access public |
||
| 622 | */ |
||
| 623 | public function flush_cache() { |
||
| 633 | |||
| 634 | |||
| 635 | /** |
||
| 636 | * Filter the group name |
||
| 637 | * |
||
| 638 | * @since 2.0 |
||
| 639 | * @access private |
||
| 640 | * |
||
| 641 | * @param $group |
||
| 642 | * |
||
| 643 | * @return mixed |
||
| 644 | */ |
||
| 645 | private function filter_group_name( $group ) { |
||
| 663 | |||
| 664 | |||
| 665 | /** |
||
| 666 | * Disable cache. |
||
| 667 | * |
||
| 668 | * @since 2.0 |
||
| 669 | * @access public |
||
| 670 | */ |
||
| 671 | public static function disable() { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Enable cache. |
||
| 677 | * |
||
| 678 | * @since 2.0 |
||
| 679 | * @access public |
||
| 680 | */ |
||
| 681 | public static function enable() { |
||
| 684 | } |
||
| 685 | |||
| 688 |