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_Notices 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_Notices, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Give_Notices { |
||
| 23 | /** |
||
| 24 | * List of notices |
||
| 25 | * @var array |
||
| 26 | * @since 1.8 |
||
| 27 | * @access private |
||
| 28 | */ |
||
| 29 | private static $notices = array(); |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Flag to check if any notice auto dismissible among all notices |
||
| 34 | * |
||
| 35 | * @since 1.8.9 |
||
| 36 | * @access private |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | private static $has_auto_dismissible_notice = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Flag to check if any notice has dismiss interval among all notices |
||
| 43 | * |
||
| 44 | * @since 1.8.9 |
||
| 45 | * @access private |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | private static $has_dismiss_interval_notice = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get things started. |
||
| 52 | * |
||
| 53 | * @since 1.0 |
||
| 54 | */ |
||
| 55 | public function __construct() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Register notice. |
||
| 66 | * |
||
| 67 | * @since 1.8.9 |
||
| 68 | * @access public |
||
| 69 | * |
||
| 70 | * @param $notice_args |
||
| 71 | * |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | public function register_notice( $notice_args ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Display notice. |
||
| 140 | * |
||
| 141 | * @since 1.8.9 |
||
| 142 | * |
||
| 143 | */ |
||
| 144 | public function render_admin_notices() { |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Render give frontend notices. |
||
| 195 | * |
||
| 196 | * @since 1.8.9 |
||
| 197 | * @access public |
||
| 198 | * |
||
| 199 | * @param int $form_id |
||
| 200 | */ |
||
| 201 | public function render_frontend_notices( $form_id = 0 ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Print notice js. |
||
| 220 | * |
||
| 221 | * @since 1.8.9 |
||
| 222 | * @access private |
||
| 223 | */ |
||
| 224 | private function print_js() { |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * Hide notice. |
||
| 302 | * |
||
| 303 | * @since 1.8.9 |
||
| 304 | * @access public |
||
| 305 | */ |
||
| 306 | public function dismiss_notices() { |
||
| 307 | $_post = give_clean( $_POST ); |
||
| 308 | $notice_id = esc_attr( $_post['notice_id'] ); |
||
| 309 | |||
| 310 | // Bailout. |
||
| 311 | if ( |
||
| 312 | empty( $notice_id ) || |
||
| 313 | empty( $_post['dismissible_type'] ) || |
||
| 314 | empty( $_post['dismiss_interval'] ) || |
||
| 315 | ! check_ajax_referer( "give_edit_{$notice_id}_notice", '_wpnonce' ) |
||
| 316 | ) { |
||
| 317 | wp_send_json_error(); |
||
| 318 | } |
||
| 319 | |||
| 320 | $notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'] ); |
||
| 321 | View Code Duplication | if ( 'user' === $_post['dismissible_type'] ) { |
|
| 322 | $current_user = wp_get_current_user(); |
||
| 323 | $notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'], $current_user->ID ); |
||
| 324 | } |
||
| 325 | |||
| 326 | $notice_dismiss_time = ! empty( $_post['dismiss_interval_time'] ) ? $_post['dismiss_interval_time'] : null; |
||
| 327 | |||
| 328 | // Save option to hide notice. |
||
| 329 | Give_Cache::set( $notice_key, true, $notice_dismiss_time, true ); |
||
| 330 | |||
| 331 | wp_send_json_success(); |
||
| 332 | } |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * Get notice key. |
||
| 337 | * |
||
| 338 | * @since 1.8.9 |
||
| 339 | * @access public |
||
| 340 | * |
||
| 341 | * @param string $notice_id |
||
| 342 | * @param string $dismiss_interval |
||
| 343 | * @param int $user_id |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function get_notice_key( $notice_id, $dismiss_interval = null, $user_id = 0 ) { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Get notice dismiss link. |
||
| 366 | * |
||
| 367 | * @param $notice_args |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function get_dismiss_link( $notice_args ) { |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * Check if notice dismissed or not |
||
| 394 | * |
||
| 395 | * @since 1.8.9 |
||
| 396 | * @access public |
||
| 397 | * |
||
| 398 | * @param array $notice |
||
| 399 | * |
||
| 400 | * @return bool|null |
||
| 401 | */ |
||
| 402 | public function is_notice_dismissed( $notice ) { |
||
| 403 | $notice_key = $this->get_notice_key( $notice['id'], $notice['dismiss_interval'] ); |
||
| 404 | $is_notice_dismissed = false; |
||
| 405 | |||
| 406 | View Code Duplication | if ( 'user' === $notice['dismissible_type'] ) { |
|
| 407 | $current_user = wp_get_current_user(); |
||
| 408 | $notice_key = Give()->notices->get_notice_key( $notice['id'], $notice['dismiss_interval'], $current_user->ID ); |
||
| 409 | } |
||
| 410 | |||
| 411 | $notice_data = Give_Cache::get( $notice_key, true ); |
||
| 412 | |||
| 413 | // Find notice dismiss link status if notice has extra dismissible links. |
||
| 414 | if ( ( empty( $notice_data ) || is_wp_error( $notice_data ) ) && ! empty( $notice['extra_links'] ) ) { |
||
| 415 | |||
| 416 | foreach ( $notice['extra_links'] as $extra_link ) { |
||
| 417 | $new_notice_data = wp_parse_args( $extra_link, $notice ); |
||
| 418 | unset( $new_notice_data['extra_links'] ); |
||
| 419 | |||
| 420 | if ( $is_notice_dismissed = $this->is_notice_dismissed( $new_notice_data ) ) { |
||
| 421 | return $is_notice_dismissed; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | $is_notice_dismissed = ! empty( $notice_data ) && ! is_wp_error( $notice_data ); |
||
| 427 | |||
| 428 | return $is_notice_dismissed; |
||
| 429 | } |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Print frontend errors. |
||
| 434 | * |
||
| 435 | * @since 1.8.9 |
||
| 436 | * @access public |
||
| 437 | * |
||
| 438 | * @param $errors |
||
| 439 | */ |
||
| 440 | static function print_frontend_errors( $errors ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Print frontend notice. |
||
| 484 | * Notice: notice type can be success/error/warning |
||
| 485 | * |
||
| 486 | * @since 1.8.9 |
||
| 487 | * @access public |
||
| 488 | * |
||
| 489 | * @param $message |
||
| 490 | * @param bool $echo |
||
| 491 | * @param string $notice_type |
||
| 492 | * @param array $notice_args |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | static function print_frontend_notice( $message, $echo = true, $notice_type = 'warning', $notice_args = array() ) { |
||
| 527 | } |
PHP provides two ways to mark string literals. Either with single quotes
'literal'or with double quotes"literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\') and the backslash (\\). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is ValueIf your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.