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 Replicastore 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 Replicastore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Replicastore implements Replicastore_Interface { |
||
| 15 | /** |
||
| 16 | * Empty and reset the replicastore. |
||
| 17 | * |
||
| 18 | * @access public |
||
| 19 | */ |
||
| 20 | public function reset() { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Ran when full sync has just started. |
||
| 50 | * |
||
| 51 | * @access public |
||
| 52 | * |
||
| 53 | * @param array $config Full sync configuration for this sync module. |
||
| 54 | */ |
||
| 55 | public function full_sync_start( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Ran when full sync has just finished. |
||
| 61 | * |
||
| 62 | * @access public |
||
| 63 | * |
||
| 64 | * @param string $checksum Deprecated since 7.3.0. |
||
| 65 | */ |
||
| 66 | public function full_sync_end( $checksum ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Retrieve the number of terms. |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * |
||
| 75 | * @return int Number of terms. |
||
| 76 | */ |
||
| 77 | public function term_count() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Retrieve the number of rows in the `term_taxonomy` table. |
||
| 84 | * |
||
| 85 | * @access public |
||
| 86 | * |
||
| 87 | * @return int Number of terms. |
||
| 88 | */ |
||
| 89 | public function term_taxonomy_count() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Retrieve the number of term relationships. |
||
| 96 | * |
||
| 97 | * @access public |
||
| 98 | * |
||
| 99 | * @return int Number of rows in the term relationships table. |
||
| 100 | */ |
||
| 101 | public function term_relationship_count() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Retrieve the number of posts with a particular post status within a certain range. |
||
| 108 | * |
||
| 109 | * @access public |
||
| 110 | * |
||
| 111 | * @todo Prepare the SQL query before executing it. |
||
| 112 | * |
||
| 113 | * @param string $status Post status. |
||
|
|
|||
| 114 | * @param int $min_id Minimum post ID. |
||
| 115 | * @param int $max_id Maximum post ID. |
||
| 116 | * @return int Number of posts. |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function post_count( $status = null, $min_id = null, $max_id = null ) { |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Retrieve the posts with a particular post status. |
||
| 143 | * |
||
| 144 | * @access public |
||
| 145 | * |
||
| 146 | * @todo Implement range and actually use max_id/min_id arguments. |
||
| 147 | * |
||
| 148 | * @param string $status Post status. |
||
| 149 | * @param int $min_id Minimum post ID. |
||
| 150 | * @param int $max_id Maximum post ID. |
||
| 151 | * @return array Array of posts. |
||
| 152 | */ |
||
| 153 | public function get_posts( $status = null, $min_id = null, $max_id = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Retrieve a post object by the post ID. |
||
| 170 | * |
||
| 171 | * @access public |
||
| 172 | * |
||
| 173 | * @param int $id Post ID. |
||
| 174 | * @return \WP_Post Post object. |
||
| 175 | */ |
||
| 176 | public function get_post( $id ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Update or insert a post. |
||
| 182 | * |
||
| 183 | * @access public |
||
| 184 | * |
||
| 185 | * @param \WP_Post $post Post object. |
||
| 186 | * @param bool $silent Whether to perform a silent action. Not used in this implementation. |
||
| 187 | */ |
||
| 188 | public function upsert_post( $post, $silent = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Delete a post by the post ID. |
||
| 250 | * |
||
| 251 | * @access public |
||
| 252 | * |
||
| 253 | * @param int $post_id Post ID. |
||
| 254 | */ |
||
| 255 | public function delete_post( $post_id ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Retrieve the checksum for posts within a range. |
||
| 261 | * |
||
| 262 | * @access public |
||
| 263 | * |
||
| 264 | * @param int $min_id Minimum post ID. |
||
| 265 | * @param int $max_id Maximum post ID. |
||
| 266 | * @return int The checksum. |
||
| 267 | */ |
||
| 268 | public function posts_checksum( $min_id = null, $max_id = null ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Retrieve the checksum for post meta within a range. |
||
| 275 | * |
||
| 276 | * @access public |
||
| 277 | * |
||
| 278 | * @param int $min_id Minimum post meta ID. |
||
| 279 | * @param int $max_id Maximum post meta ID. |
||
| 280 | * @return int The checksum. |
||
| 281 | */ |
||
| 282 | public function post_meta_checksum( $min_id = null, $max_id = null ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Retrieve the number of comments with a particular comment status within a certain range. |
||
| 289 | * |
||
| 290 | * @access public |
||
| 291 | * |
||
| 292 | * @todo Prepare the SQL query before executing it. |
||
| 293 | * |
||
| 294 | * @param string $status Comment status. |
||
| 295 | * @param int $min_id Minimum comment ID. |
||
| 296 | * @param int $max_id Maximum comment ID. |
||
| 297 | * @return int Number of comments. |
||
| 298 | */ |
||
| 299 | View Code Duplication | public function comment_count( $status = null, $min_id = null, $max_id = null ) { |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Translate a comment status to a value of the comment_approved field. |
||
| 324 | * |
||
| 325 | * @access private |
||
| 326 | * |
||
| 327 | * @param string $status Comment status. |
||
| 328 | * @return string|bool New comment_approved value, false if the status doesn't affect it. |
||
| 329 | */ |
||
| 330 | private function comment_status_to_approval_value( $status ) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Retrieve the comments with a particular comment status. |
||
| 353 | * |
||
| 354 | * @access public |
||
| 355 | * |
||
| 356 | * @todo Implement range and actually use max_id/min_id arguments. |
||
| 357 | * |
||
| 358 | * @param string $status Comment status. |
||
| 359 | * @param int $min_id Minimum comment ID. |
||
| 360 | * @param int $max_id Maximum comment ID. |
||
| 361 | * @return array Array of comments. |
||
| 362 | */ |
||
| 363 | public function get_comments( $status = null, $min_id = null, $max_id = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Retrieve a comment object by the comment ID. |
||
| 378 | * |
||
| 379 | * @access public |
||
| 380 | * |
||
| 381 | * @param int $id Comment ID. |
||
| 382 | * @return \WP_Comment Comment object. |
||
| 383 | */ |
||
| 384 | public function get_comment( $id ) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Update or insert a comment. |
||
| 390 | * |
||
| 391 | * @access public |
||
| 392 | * |
||
| 393 | * @param \WP_Comment $comment Comment object. |
||
| 394 | */ |
||
| 395 | public function upsert_comment( $comment ) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Trash a comment by the comment ID. |
||
| 445 | * |
||
| 446 | * @access public |
||
| 447 | * |
||
| 448 | * @param int $comment_id Comment ID. |
||
| 449 | */ |
||
| 450 | public function trash_comment( $comment_id ) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Delete a comment by the comment ID. |
||
| 456 | * |
||
| 457 | * @access public |
||
| 458 | * |
||
| 459 | * @param int $comment_id Comment ID. |
||
| 460 | */ |
||
| 461 | public function delete_comment( $comment_id ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Mark a comment by the comment ID as spam. |
||
| 467 | * |
||
| 468 | * @access public |
||
| 469 | * |
||
| 470 | * @param int $comment_id Comment ID. |
||
| 471 | */ |
||
| 472 | public function spam_comment( $comment_id ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Trash the comments of a post. |
||
| 478 | * |
||
| 479 | * @access public |
||
| 480 | * |
||
| 481 | * @param int $post_id Post ID. |
||
| 482 | * @param array $statuses Post statuses. Not used in this implementation. |
||
| 483 | */ |
||
| 484 | public function trashed_post_comments( $post_id, $statuses ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Untrash the comments of a post. |
||
| 490 | * |
||
| 491 | * @access public |
||
| 492 | * |
||
| 493 | * @param int $post_id Post ID. |
||
| 494 | */ |
||
| 495 | public function untrashed_post_comments( $post_id ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Retrieve the checksum for comments within a range. |
||
| 501 | * |
||
| 502 | * @access public |
||
| 503 | * |
||
| 504 | * @param int $min_id Minimum comment ID. |
||
| 505 | * @param int $max_id Maximum comment ID. |
||
| 506 | * @return int The checksum. |
||
| 507 | */ |
||
| 508 | public function comments_checksum( $min_id = null, $max_id = null ) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Retrieve the checksum for comment meta within a range. |
||
| 515 | * |
||
| 516 | * @access public |
||
| 517 | * |
||
| 518 | * @param int $min_id Minimum comment meta ID. |
||
| 519 | * @param int $max_id Maximum comment meta ID. |
||
| 520 | * @return int The checksum. |
||
| 521 | */ |
||
| 522 | public function comment_meta_checksum( $min_id = null, $max_id = null ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Retrieve the checksum for all options. |
||
| 529 | * |
||
| 530 | * @access public |
||
| 531 | * |
||
| 532 | * @return int The checksum. |
||
| 533 | */ |
||
| 534 | public function options_checksum() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Update the value of an option. |
||
| 544 | * |
||
| 545 | * @access public |
||
| 546 | * |
||
| 547 | * @param string $option Option name. |
||
| 548 | * @param mixed $value Option value. |
||
| 549 | * @return bool False if value was not updated and true if value was updated. |
||
| 550 | */ |
||
| 551 | public function update_option( $option, $value ) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Retrieve an option value based on an option name. |
||
| 557 | * |
||
| 558 | * @access public |
||
| 559 | * |
||
| 560 | * @param string $option Name of option to retrieve. |
||
| 561 | * @param mixed $default Optional. Default value to return if the option does not exist. |
||
| 562 | * @return mixed Value set for the option. |
||
| 563 | */ |
||
| 564 | public function get_option( $option, $default = false ) { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Remove an option by name. |
||
| 570 | * |
||
| 571 | * @access public |
||
| 572 | * |
||
| 573 | * @param string $option Name of option to remove. |
||
| 574 | * @return bool True, if option is successfully deleted. False on failure. |
||
| 575 | */ |
||
| 576 | public function delete_option( $option ) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Change the info of the current theme. |
||
| 582 | * |
||
| 583 | * @access public |
||
| 584 | * |
||
| 585 | * @param array $theme_info Theme info array. |
||
| 586 | */ |
||
| 587 | public function set_theme_info( $theme_info ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Whether the current theme supports a certain feature. |
||
| 593 | * |
||
| 594 | * @access public |
||
| 595 | * |
||
| 596 | * @param string $feature Name of the feature. |
||
| 597 | */ |
||
| 598 | public function current_theme_supports( $feature ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Retrieve metadata for the specified object. |
||
| 604 | * |
||
| 605 | * @access public |
||
| 606 | * |
||
| 607 | * @param string $type Meta type. |
||
| 608 | * @param int $object_id ID of the object. |
||
| 609 | * @param string $meta_key Meta key. |
||
| 610 | * @param bool $single If true, return only the first value of the specified meta_key. |
||
| 611 | * |
||
| 612 | * @return mixed Single metadata value, or array of values. |
||
| 613 | */ |
||
| 614 | public function get_metadata( $type, $object_id, $meta_key = '', $single = false ) { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Stores remote meta key/values alongside an ID mapping key. |
||
| 620 | * |
||
| 621 | * @access public |
||
| 622 | * |
||
| 623 | * @todo Refactor to not use interpolated values when preparing the SQL query. |
||
| 624 | * |
||
| 625 | * @param string $type Meta type. |
||
| 626 | * @param int $object_id ID of the object. |
||
| 627 | * @param string $meta_key Meta key. |
||
| 628 | * @param mixed $meta_value Meta value. |
||
| 629 | * @param int $meta_id ID of the meta. |
||
| 630 | * |
||
| 631 | * @return bool False if meta table does not exist, true otherwise. |
||
| 632 | */ |
||
| 633 | public function upsert_metadata( $type, $object_id, $meta_key, $meta_value, $meta_id ) { |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Delete metadata for the specified object. |
||
| 678 | * |
||
| 679 | * @access public |
||
| 680 | * |
||
| 681 | * @todo Refactor to not use interpolated values when preparing the SQL query. |
||
| 682 | * |
||
| 683 | * @param string $type Meta type. |
||
| 684 | * @param int $object_id ID of the object. |
||
| 685 | * @param array $meta_ids IDs of the meta objects to delete. |
||
| 686 | */ |
||
| 687 | public function delete_metadata( $type, $object_id, $meta_ids ) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Delete metadata with a certain key for the specified objects. |
||
| 708 | * |
||
| 709 | * @access public |
||
| 710 | * |
||
| 711 | * @todo Test this out to make sure it works as expected. |
||
| 712 | * @todo Refactor to not use interpolated values when preparing the SQL query. |
||
| 713 | * |
||
| 714 | * @param string $type Meta type. |
||
| 715 | * @param array $object_ids IDs of the objects. |
||
| 716 | * @param string $meta_key Meta key. |
||
| 717 | */ |
||
| 718 | public function delete_batch_metadata( $type, $object_ids, $meta_key ) { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Retrieve value of a constant based on the constant name. |
||
| 737 | * |
||
| 738 | * We explicitly return null instead of false if the constant doesn't exist. |
||
| 739 | * |
||
| 740 | * @access public |
||
| 741 | * |
||
| 742 | * @param string $constant Name of constant to retrieve. |
||
| 743 | * @return mixed Value set for the constant. |
||
| 744 | */ |
||
| 745 | public function get_constant( $constant ) { |
||
| 746 | $value = get_option( 'jetpack_constant_' . $constant ); |
||
| 747 | |||
| 748 | if ( $value ) { |
||
| 749 | return $value; |
||
| 750 | } |
||
| 751 | |||
| 752 | return null; |
||
| 753 | } |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Set the value of a constant. |
||
| 757 | * |
||
| 758 | * @access public |
||
| 759 | * |
||
| 760 | * @param string $constant Name of constant to retrieve. |
||
| 761 | * @param mixed $value Value set for the constant. |
||
| 762 | */ |
||
| 763 | public function set_constant( $constant, $value ) { |
||
| 764 | update_option( 'jetpack_constant_' . $constant, $value ); |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Retrieve the number of the available updates of a certain type. |
||
| 769 | * Type is one of: `plugins`, `themes`, `wordpress`, `translations`, `total`, `wp_update_version`. |
||
| 770 | * |
||
| 771 | * @access public |
||
| 772 | * |
||
| 773 | * @param string $type Type of updates to retrieve. |
||
| 774 | * @return int|null Number of updates available, `null` if type is invalid or missing. |
||
| 775 | */ |
||
| 776 | public function get_updates( $type ) { |
||
| 777 | $all_updates = get_option( 'jetpack_updates', array() ); |
||
| 778 | |||
| 779 | if ( isset( $all_updates[ $type ] ) ) { |
||
| 780 | return $all_updates[ $type ]; |
||
| 781 | } else { |
||
| 782 | return null; |
||
| 783 | } |
||
| 784 | } |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Set the available updates of a certain type. |
||
| 788 | * Type is one of: `plugins`, `themes`, `wordpress`, `translations`, `total`, `wp_update_version`. |
||
| 789 | * |
||
| 790 | * @access public |
||
| 791 | * |
||
| 792 | * @param string $type Type of updates to set. |
||
| 793 | * @param int $updates Total number of updates. |
||
| 794 | */ |
||
| 795 | public function set_updates( $type, $updates ) { |
||
| 796 | $all_updates = get_option( 'jetpack_updates', array() ); |
||
| 797 | $all_updates[ $type ] = $updates; |
||
| 798 | update_option( 'jetpack_updates', $all_updates ); |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Retrieve a callable value based on its name. |
||
| 803 | * |
||
| 804 | * @access public |
||
| 805 | * |
||
| 806 | * @param string $name Name of the callable to retrieve. |
||
| 807 | * @return mixed Value of the callable. |
||
| 808 | */ |
||
| 809 | public function get_callable( $name ) { |
||
| 810 | $value = get_option( 'jetpack_' . $name ); |
||
| 811 | |||
| 812 | if ( $value ) { |
||
| 813 | return $value; |
||
| 814 | } |
||
| 815 | |||
| 816 | return null; |
||
| 817 | } |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Update the value of a callable. |
||
| 821 | * |
||
| 822 | * @access public |
||
| 823 | * |
||
| 824 | * @param string $name Callable name. |
||
| 825 | * @param mixed $value Callable value. |
||
| 826 | */ |
||
| 827 | public function set_callable( $name, $value ) { |
||
| 828 | update_option( 'jetpack_' . $name, $value ); |
||
| 829 | } |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Retrieve a network option value based on a network option name. |
||
| 833 | * |
||
| 834 | * @access public |
||
| 835 | * |
||
| 836 | * @param string $option Name of network option to retrieve. |
||
| 837 | * @return mixed Value set for the network option. |
||
| 838 | */ |
||
| 839 | public function get_site_option( $option ) { |
||
| 840 | return get_option( 'jetpack_network_' . $option ); |
||
| 841 | } |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Update the value of a network option. |
||
| 845 | * |
||
| 846 | * @access public |
||
| 847 | * |
||
| 848 | * @param string $option Network option name. |
||
| 849 | * @param mixed $value Network option value. |
||
| 850 | * @return bool False if value was not updated and true if value was updated. |
||
| 851 | */ |
||
| 852 | public function update_site_option( $option, $value ) { |
||
| 853 | return update_option( 'jetpack_network_' . $option, $value ); |
||
| 854 | } |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Remove a network option by name. |
||
| 858 | * |
||
| 859 | * @access public |
||
| 860 | * |
||
| 861 | * @param string $option Name of option to remove. |
||
| 862 | * @return bool True, if option is successfully deleted. False on failure. |
||
| 863 | */ |
||
| 864 | public function delete_site_option( $option ) { |
||
| 865 | return delete_option( 'jetpack_network_' . $option ); |
||
| 866 | } |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Retrieve the terms from a particular taxonomy. |
||
| 870 | * |
||
| 871 | * @access public |
||
| 872 | * |
||
| 873 | * @param string $taxonomy Taxonomy slug. |
||
| 874 | * @return array|\WP_Error Array of terms or WP_Error object on failure. |
||
| 875 | */ |
||
| 876 | public function get_terms( $taxonomy ) { |
||
| 877 | $t = $this->ensure_taxonomy( $taxonomy ); |
||
| 878 | if ( ! $t || is_wp_error( $t ) ) { |
||
| 879 | return $t; |
||
| 880 | } |
||
| 881 | return get_terms( $taxonomy ); |
||
| 882 | } |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Retrieve a particular term. |
||
| 886 | * |
||
| 887 | * @access public |
||
| 888 | * |
||
| 889 | * @param string $taxonomy Taxonomy slug. |
||
| 890 | * @param int $term_id ID of the term. |
||
| 891 | * @param string $term_key ID Field `term_id` or `term_taxonomy_id`. |
||
| 892 | * @return \WP_Term|\WP_Error Term object on success, \WP_Error object on failure. |
||
| 893 | */ |
||
| 894 | public function get_term( $taxonomy, $term_id, $term_key = 'term_id' ) { |
||
| 895 | |||
| 896 | // Full Sync will pass false for the $taxonomy so a check for term_taxonomy_id is needed before ensure_taxonomy. |
||
| 897 | if ( 'term_taxonomy_id' === $term_key ) { |
||
| 898 | return get_term_by( 'term_taxonomy_id', $term_id ); |
||
| 899 | } |
||
| 900 | |||
| 901 | $t = $this->ensure_taxonomy( $taxonomy ); |
||
| 902 | if ( ! $t || is_wp_error( $t ) ) { |
||
| 903 | return $t; |
||
| 904 | } |
||
| 905 | |||
| 906 | return get_term( $term_id, $taxonomy ); |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Verify a taxonomy is legitimate and register it if necessary. |
||
| 911 | * |
||
| 912 | * @access private |
||
| 913 | * |
||
| 914 | * @param string $taxonomy Taxonomy slug. |
||
| 915 | * @return bool|void|\WP_Error True if already exists; void if it was registered; \WP_Error on error. |
||
| 916 | */ |
||
| 917 | private function ensure_taxonomy( $taxonomy ) { |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Retrieve all terms from a taxonomy that are related to an object with a particular ID. |
||
| 939 | * |
||
| 940 | * @access public |
||
| 941 | * |
||
| 942 | * @param int $object_id Object ID. |
||
| 943 | * @param string $taxonomy Taxonomy slug. |
||
| 944 | * @return array|bool|\WP_Error Array of terms on success, `false` if no terms or post doesn't exist, \WP_Error on failure. |
||
| 945 | */ |
||
| 946 | public function get_the_terms( $object_id, $taxonomy ) { |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Insert or update a term. |
||
| 952 | * |
||
| 953 | * @access public |
||
| 954 | * |
||
| 955 | * @param \WP_Term $term_object Term object. |
||
| 956 | * @return array|bool|\WP_Error Array of term_id and term_taxonomy_id if updated, true if inserted, \WP_Error on failure. |
||
| 957 | */ |
||
| 958 | public function update_term( $term_object ) { |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Delete a term by the term ID and its corresponding taxonomy. |
||
| 994 | * |
||
| 995 | * @access public |
||
| 996 | * |
||
| 997 | * @param int $term_id Term ID. |
||
| 998 | * @param string $taxonomy Taxonomy slug. |
||
| 999 | * @return bool|int|\WP_Error True on success, false if term doesn't exist. Zero if trying with default category. \WP_Error on invalid taxonomy. |
||
| 1000 | */ |
||
| 1001 | public function delete_term( $term_id, $taxonomy ) { |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Add/update terms of a particular taxonomy of an object with the specified ID. |
||
| 1008 | * |
||
| 1009 | * @access public |
||
| 1010 | * |
||
| 1011 | * @param int $object_id The object to relate to. |
||
| 1012 | * @param string $taxonomy The context in which to relate the term to the object. |
||
| 1013 | * @param string|int|array $terms A single term slug, single term id, or array of either term slugs or ids. |
||
| 1014 | * @param bool $append Optional. If false will delete difference of terms. Default false. |
||
| 1015 | */ |
||
| 1016 | public function update_object_terms( $object_id, $taxonomy, $terms, $append ) { |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Remove certain term relationships from the specified object. |
||
| 1023 | * |
||
| 1024 | * @access public |
||
| 1025 | * |
||
| 1026 | * @todo Refactor to not use interpolated values when preparing the SQL query. |
||
| 1027 | * |
||
| 1028 | * @param int $object_id ID of the object. |
||
| 1029 | * @param array $tt_ids Term taxonomy IDs. |
||
| 1030 | * @return bool True on success, false on failure. |
||
| 1031 | */ |
||
| 1032 | public function delete_object_terms( $object_id, $tt_ids ) { |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Retrieve the number of users. |
||
| 1080 | * Not supported in this replicastore. |
||
| 1081 | * |
||
| 1082 | * @access public |
||
| 1083 | */ |
||
| 1084 | public function user_count() { |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * Retrieve a user object by the user ID. |
||
| 1090 | * |
||
| 1091 | * @access public |
||
| 1092 | * |
||
| 1093 | * @param int $user_id User ID. |
||
| 1094 | * @return \WP_User User object. |
||
| 1095 | */ |
||
| 1096 | public function get_user( $user_id ) { |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Insert or update a user. |
||
| 1102 | * Not supported in this replicastore. |
||
| 1103 | * |
||
| 1104 | * @access public |
||
| 1105 | * @throws \Exception If this method is invoked. |
||
| 1106 | * |
||
| 1107 | * @param \WP_User $user User object. |
||
| 1108 | */ |
||
| 1109 | public function upsert_user( $user ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * Delete a user. |
||
| 1115 | * Not supported in this replicastore. |
||
| 1116 | * |
||
| 1117 | * @access public |
||
| 1118 | * @throws \Exception If this method is invoked. |
||
| 1119 | * |
||
| 1120 | * @param int $user_id User ID. |
||
| 1121 | */ |
||
| 1122 | public function delete_user( $user_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Update/insert user locale. |
||
| 1128 | * Not supported in this replicastore. |
||
| 1129 | * |
||
| 1130 | * @access public |
||
| 1131 | * @throws \Exception If this method is invoked. |
||
| 1132 | * |
||
| 1133 | * @param int $user_id User ID. |
||
| 1134 | * @param string $local The user locale. |
||
| 1135 | */ |
||
| 1136 | public function upsert_user_locale( $user_id, $local ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Delete user locale. |
||
| 1142 | * Not supported in this replicastore. |
||
| 1143 | * |
||
| 1144 | * @access public |
||
| 1145 | * @throws \Exception If this method is invoked. |
||
| 1146 | * |
||
| 1147 | * @param int $user_id User ID. |
||
| 1148 | */ |
||
| 1149 | public function delete_user_locale( $user_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Retrieve the user locale. |
||
| 1155 | * |
||
| 1156 | * @access public |
||
| 1157 | * |
||
| 1158 | * @param int $user_id User ID. |
||
| 1159 | * @return string The user locale. |
||
| 1160 | */ |
||
| 1161 | public function get_user_locale( $user_id ) { |
||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Retrieve the allowed mime types for the user. |
||
| 1167 | * Not supported in this replicastore. |
||
| 1168 | * |
||
| 1169 | * @access public |
||
| 1170 | * |
||
| 1171 | * @param int $user_id User ID. |
||
| 1172 | */ |
||
| 1173 | public function get_allowed_mime_types( $user_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * Retrieve all the checksums we are interested in. |
||
| 1179 | * Currently that is posts, comments, post meta and comment meta. |
||
| 1180 | * |
||
| 1181 | * @access public |
||
| 1182 | * |
||
| 1183 | * @return array Checksums. |
||
| 1184 | */ |
||
| 1185 | public function checksum_all() { |
||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Retrieve the columns that are needed to calculate a checksum for an object type. |
||
| 1199 | * |
||
| 1200 | * @access public |
||
| 1201 | * |
||
| 1202 | * @todo Refactor to not use interpolated values and prepare the SQL query. |
||
| 1203 | * |
||
| 1204 | * @param string $object_type Object type. |
||
| 1205 | * @return array|bool Columns, or false if invalid object type is specified. |
||
| 1206 | */ |
||
| 1207 | public function get_checksum_columns_for_object_type( $object_type ) { |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Grabs the minimum and maximum object ids for the given parameters. |
||
| 1230 | * |
||
| 1231 | * @access public |
||
| 1232 | * |
||
| 1233 | * @param string $id_field The id column in the table to query. |
||
| 1234 | * @param string $object_table The table to query. |
||
| 1235 | * @param string $where A sql where clause without 'WHERE'. |
||
| 1236 | * @param int $bucket_size The maximum amount of objects to include in the query. |
||
| 1237 | * For `term_relationships` table, the bucket size will refer to the amount |
||
| 1238 | * of distinct object ids. This will likely include more database rows than |
||
| 1239 | * the bucket size implies. |
||
| 1240 | * |
||
| 1241 | * @return object An object with min_id and max_id properties. |
||
| 1242 | */ |
||
| 1243 | public function get_min_max_object_id( $id_field, $object_table, $where, $bucket_size ) { |
||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * Retrieve the checksum histogram for a specific object type. |
||
| 1265 | * |
||
| 1266 | * @access public |
||
| 1267 | * |
||
| 1268 | * @todo Refactor to not use interpolated values and properly prepare the SQL query. |
||
| 1269 | * |
||
| 1270 | * @param string $object_type Object type. |
||
| 1271 | * @param int $buckets Number of buckets to split the objects to. |
||
| 1272 | * @param int $start_id Minimum object ID. |
||
| 1273 | * @param int $end_id Maximum object ID. |
||
| 1274 | * @param array $columns Table columns to calculate the checksum from. |
||
| 1275 | * @param bool $strip_non_ascii Whether to strip non-ASCII characters. |
||
| 1276 | * @param string $salt Salt, used for $wpdb->prepare()'s args. |
||
| 1277 | * @return array The checksum histogram. |
||
| 1278 | */ |
||
| 1279 | public function checksum_histogram( $object_type, $buckets, $start_id = null, $end_id = null, $columns = null, $strip_non_ascii = true, $salt = '' ) { |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Retrieve the checksum for a specific database table. |
||
| 1386 | * |
||
| 1387 | * @access private |
||
| 1388 | * |
||
| 1389 | * @todo Refactor to properly prepare the SQL query. |
||
| 1390 | * |
||
| 1391 | * @param string $table Table name. |
||
| 1392 | * @param array $columns Table columns to calculate the checksum from. |
||
| 1393 | * @param int $id_column Name of the unique ID column. |
||
| 1394 | * @param string $where_sql Additional WHERE clause SQL. |
||
| 1395 | * @param int $min_id Minimum object ID. |
||
| 1396 | * @param int $max_id Maximum object ID. |
||
| 1397 | * @param bool $strip_non_ascii Whether to strip non-ASCII characters. |
||
| 1398 | * @param string $salt Salt, used for $wpdb->prepare()'s args. |
||
| 1399 | * @return int|\WP_Error The table histogram, or \WP_Error on failure. |
||
| 1400 | */ |
||
| 1401 | private function table_checksum( $table, $columns, $id_column, $where_sql = '1=1', $min_id = null, $max_id = null, $strip_non_ascii = true, $salt = '' ) { |
||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * Retrieve the type of the checksum. |
||
| 1451 | * |
||
| 1452 | * @access public |
||
| 1453 | * |
||
| 1454 | * @return string Type of the checksum. |
||
| 1455 | */ |
||
| 1456 | public function get_checksum_type() { |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Count the meta values in a table, within a specified range. |
||
| 1462 | * |
||
| 1463 | * @access private |
||
| 1464 | * |
||
| 1465 | * @todo Refactor to not use interpolated values when preparing the SQL query. |
||
| 1466 | * |
||
| 1467 | * @param string $table Table name. |
||
| 1468 | * @param string $where_sql Additional WHERE SQL. |
||
| 1469 | * @param int $min_id Minimum meta ID. |
||
| 1470 | * @param int $max_id Maximum meta ID. |
||
| 1471 | * @return int Number of meta values. |
||
| 1472 | */ |
||
| 1473 | private function meta_count( $table, $where_sql, $min_id, $max_id ) { |
||
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Wraps a column name in SQL which strips non-ASCII chars. |
||
| 1490 | * This helps normalize data to avoid checksum differences caused by |
||
| 1491 | * badly encoded data in the DB. |
||
| 1492 | * |
||
| 1493 | * @param string $column_name Name of the column. |
||
| 1494 | * @return string Column name, without the non-ASCII chars. |
||
| 1495 | */ |
||
| 1496 | public function strip_non_ascii_sql( $column_name ) { |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Used in methods that are not implemented and shouldn't be invoked. |
||
| 1502 | * |
||
| 1503 | * @access private |
||
| 1504 | * @throws \Exception If this method is invoked. |
||
| 1505 | */ |
||
| 1506 | private function invalid_call() { |
||
| 1512 | } |
||
| 1513 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.