Complex classes like Dynamic_Featured_Image 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 Dynamic_Featured_Image, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Dynamic_Featured_Image { |
||
| 47 | /** |
||
| 48 | * Current version of the plugin. |
||
| 49 | * |
||
| 50 | * @since 3.0.0 |
||
| 51 | */ |
||
| 52 | const VERSION = '3.5.2'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Text domain. |
||
| 56 | * |
||
| 57 | * @since 3.6.0 |
||
| 58 | */ |
||
| 59 | const TEXT_DOMAIN = 'dynamic-featured-image'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Documentation Link. |
||
| 63 | * |
||
| 64 | * @since 3.6.0 |
||
| 65 | */ |
||
| 66 | const WIKI_LINK = 'https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Upgrade Link. |
||
| 70 | * |
||
| 71 | * @since 3.6.0 |
||
| 72 | */ |
||
| 73 | const UPGRADE_LINK = 'https://ankitpokhrel.com/explore/dynamic-featured-image-pro/'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Image upload directory. |
||
| 77 | * |
||
| 78 | * @var $upload_dir string |
||
| 79 | */ |
||
| 80 | private $upload_dir; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Image upload URL. |
||
| 84 | * |
||
| 85 | * @var $upload_url string |
||
| 86 | */ |
||
| 87 | private $upload_url; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Database object. |
||
| 91 | * |
||
| 92 | * @var $db wpdb |
||
| 93 | */ |
||
| 94 | private $db; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Title for dfi metabox. |
||
| 98 | * |
||
| 99 | * @var $metabox_title string |
||
| 100 | */ |
||
| 101 | protected $metabox_title; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Users post type filter for dfi metabox. |
||
| 105 | * |
||
| 106 | * @var $user_filter array |
||
| 107 | */ |
||
| 108 | protected $user_filter; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Constructor. Hooks all interactions to initialize the class. |
||
| 112 | * |
||
| 113 | * @since 1.0.0 |
||
| 114 | * @access public |
||
| 115 | * @global object $wpdb |
||
| 116 | * |
||
| 117 | * @see add_action() |
||
| 118 | */ |
||
| 119 | public function __construct() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return site protocol. |
||
| 156 | * |
||
| 157 | * @since 3.5.1 |
||
| 158 | * @access public |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | private function get_protocol() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Add required admin scripts. |
||
| 169 | * |
||
| 170 | * @since 1.0.0 |
||
| 171 | * @access public |
||
| 172 | * |
||
| 173 | * @see wp_enque_style() |
||
| 174 | * @see wp_register_script() |
||
| 175 | * @see wp_enqueue_script() |
||
| 176 | * |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function enqueue_admin_scripts() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Add upgrade link. |
||
| 205 | * |
||
| 206 | * @access public |
||
| 207 | * @since 3.5.1 |
||
| 208 | * @action plugin_action_links |
||
| 209 | * |
||
| 210 | * @codeCoverageIgnore |
||
| 211 | * |
||
| 212 | * @param array $links Action links. |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function dfi_action_links( $links ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add featured meta boxes dynamically. |
||
| 226 | * |
||
| 227 | * @since 1.0.0 |
||
| 228 | * @access public |
||
| 229 | * @global object $post |
||
| 230 | * |
||
| 231 | * @see get_post_meta() |
||
| 232 | * @see get_post_types() |
||
| 233 | * @see add_meta_box() |
||
| 234 | * @see add_filter() |
||
| 235 | * |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | public function initialize_featured_box() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Translates more than one digit number digit by digit. |
||
| 269 | * |
||
| 270 | * @param int $number Integer to be translated. |
||
| 271 | * |
||
| 272 | * @return string Translated number |
||
| 273 | */ |
||
| 274 | protected function get_number_translation( $number ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Adds meta boxes. |
||
| 290 | * |
||
| 291 | * @param array $post_types Post types to show featured image box. |
||
| 292 | * @param object $featured Callback arguments. |
||
| 293 | * @param int $i Index of the featured image. |
||
| 294 | * |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | private function dfi_add_meta_box( $post_types, $featured = null, $i = null ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Separate thumb and full image url from given URL string. |
||
| 331 | * |
||
| 332 | * @since 3.3.1 |
||
| 333 | * |
||
| 334 | * @param string $url_string Url string. |
||
| 335 | * @param string $state Thumb or full. |
||
| 336 | * |
||
| 337 | * @return string|null |
||
| 338 | */ |
||
| 339 | private function separate( $url_string, $state = 'thumb' ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Create a nonce field. |
||
| 351 | * |
||
| 352 | * @since 3.5.0 |
||
| 353 | * |
||
| 354 | * @see wp_nonce_field() |
||
| 355 | * @see plugin_basename() |
||
| 356 | * |
||
| 357 | * @codeCoverageIgnore |
||
| 358 | * |
||
| 359 | * @param string $key Nonce key. |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | protected function nonce_field( $key ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Featured meta box as seen in the admin. |
||
| 369 | * |
||
| 370 | * @since 1.0.0 |
||
| 371 | * @access public |
||
| 372 | * |
||
| 373 | * @param object $post Global post object. |
||
| 374 | * @param array $featured Array containing featured image count. |
||
| 375 | * |
||
| 376 | * @throws Exception Medium size image not found. |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function featured_meta_box( $post, $featured ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Returns featured box html content. |
||
| 409 | * |
||
| 410 | * @since 3.1.0 |
||
| 411 | * @access private |
||
| 412 | * |
||
| 413 | * @param string $featured_img_trimmed Medium sized image. |
||
| 414 | * @param string $featured_img Full sized image. |
||
| 415 | * @param string $featured_id Attachment Id. |
||
| 416 | * @param string $thumbnail Thumb sized image. |
||
| 417 | * @param int $post_id Post id. |
||
| 418 | * |
||
| 419 | * @return string Html content |
||
| 420 | */ |
||
| 421 | private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id ) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Load new featured meta box via ajax. |
||
| 438 | * |
||
| 439 | * @since 1.0.0 |
||
| 440 | * @access public |
||
| 441 | * |
||
| 442 | * @return void |
||
| 443 | */ |
||
| 444 | public function ajax_callback() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Add custom class 'featured-meta-box' to meta box. |
||
| 476 | * |
||
| 477 | * @since 1.0.0 |
||
| 478 | * @access public |
||
| 479 | * |
||
| 480 | * @see add_metabox_classes |
||
| 481 | * |
||
| 482 | * @param array $classes Classes to add in the meta box. |
||
| 483 | * |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | public function add_metabox_classes( $classes ) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Add custom fields in media uploader. |
||
| 494 | * |
||
| 495 | * @since 3.4.0 |
||
| 496 | * |
||
| 497 | * @param array $form_fields Fields to include in media attachment form. |
||
| 498 | * @param array $post Post data. |
||
| 499 | * |
||
| 500 | * @return array |
||
| 501 | */ |
||
| 502 | public function media_attachment_custom_fields( $form_fields, $post ) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Save values of media uploader custom fields. |
||
| 514 | * |
||
| 515 | * @since 3.4.0 |
||
| 516 | * |
||
| 517 | * @param array $post Post data for database. |
||
| 518 | * @param array $attachment Attachment fields from $_POST form. |
||
| 519 | * |
||
| 520 | * @return array |
||
| 521 | */ |
||
| 522 | public function media_attachment_custom_fields_save( $post, $attachment ) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Update featured images in the database. |
||
| 532 | * |
||
| 533 | * @since 1.0.0 |
||
| 534 | * @access public |
||
| 535 | * |
||
| 536 | * @see plugin_basename() |
||
| 537 | * @see update_post_meta() |
||
| 538 | * @see current_user_can() |
||
| 539 | * |
||
| 540 | * @param int $post_id Current post id. |
||
| 541 | * |
||
| 542 | * @return bool|null |
||
| 543 | */ |
||
| 544 | public function save_meta( $post_id ) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Verify metabox nonces. |
||
| 569 | * |
||
| 570 | * @access protected |
||
| 571 | * @see wp_verify_nonce() |
||
| 572 | * |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | protected function verify_nonces() { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Add update notice. Displayed in plugin update page. |
||
| 596 | * |
||
| 597 | * @since 2.0.0 |
||
| 598 | * @access public |
||
| 599 | * |
||
| 600 | * @return void |
||
| 601 | */ |
||
| 602 | public function update_notice() { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Execute query. |
||
| 611 | * |
||
| 612 | * @param string $query Query to execute. |
||
| 613 | * |
||
| 614 | * @return null|string |
||
| 615 | */ |
||
| 616 | private function execute_query( $query ) { |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Get attachment id of the image by image url. |
||
| 622 | * |
||
| 623 | * @since 3.1.7 |
||
| 624 | * @access protected |
||
| 625 | * @global object $wpdb |
||
| 626 | * |
||
| 627 | * @param string $image_url URL of an image. |
||
| 628 | * |
||
| 629 | * @return string |
||
| 630 | */ |
||
| 631 | protected function get_attachment_id( $image_url ) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Get image url of the image by attachment id. |
||
| 637 | * |
||
| 638 | * @since 2.0.0 |
||
| 639 | * @access public |
||
| 640 | * |
||
| 641 | * @see wp_get_attachment_image_src() |
||
| 642 | * |
||
| 643 | * @param int $attachment_id attachment id of an image. |
||
| 644 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
| 645 | * |
||
| 646 | * @return string |
||
| 647 | */ |
||
| 648 | public function get_image_url( $attachment_id, $size = 'full' ) { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Get image thumbnail url of specific size by image url. |
||
| 656 | * |
||
| 657 | * @since 2.0.0 |
||
| 658 | * @access public |
||
| 659 | * |
||
| 660 | * @see get_image_id() |
||
| 661 | * @see wp_get_attachment_image_src() |
||
| 662 | * |
||
| 663 | * @param string $image_url url of an image. |
||
| 664 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
| 665 | * |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Gets attachment id from given image url. |
||
| 677 | * |
||
| 678 | * @param string $image_url url of an image. |
||
| 679 | * |
||
| 680 | * @since 2.0.0 |
||
| 681 | * @access public |
||
| 682 | * |
||
| 683 | * @return int|null attachment id of an image |
||
| 684 | */ |
||
| 685 | public function get_image_id( $image_url ) { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Get image title. |
||
| 703 | * |
||
| 704 | * @since 2.0.0 |
||
| 705 | * @access public |
||
| 706 | * |
||
| 707 | * @param string $image_url URL of an image. |
||
| 708 | * |
||
| 709 | * @return string |
||
| 710 | */ |
||
| 711 | public function get_image_title( $image_url ) { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Get image title by id. |
||
| 717 | * |
||
| 718 | * @since 2.0.0 |
||
| 719 | * @access public |
||
| 720 | * |
||
| 721 | * @param int $attachment_id Attachment id of an image. |
||
| 722 | * |
||
| 723 | * @return string |
||
| 724 | */ |
||
| 725 | public function get_image_title_by_id( $attachment_id ) { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Get image caption. |
||
| 731 | * |
||
| 732 | * @since 2.0.0 |
||
| 733 | * @access public |
||
| 734 | * |
||
| 735 | * @param string $image_url URL of an image. |
||
| 736 | * |
||
| 737 | * @return string |
||
| 738 | */ |
||
| 739 | public function get_image_caption( $image_url ) { |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Get image caption by id. |
||
| 745 | * |
||
| 746 | * @since 2.0.0 |
||
| 747 | * @access public |
||
| 748 | * |
||
| 749 | * @param int $attachment_id Attachment id of an image. |
||
| 750 | * |
||
| 751 | * @return string |
||
| 752 | */ |
||
| 753 | public function get_image_caption_by_id( $attachment_id ) { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Get image alternate text. |
||
| 759 | * |
||
| 760 | * @since 2.0.0 |
||
| 761 | * @access public |
||
| 762 | * |
||
| 763 | * @see get_post_meta() |
||
| 764 | * |
||
| 765 | * @param string $image_url URL of an image. |
||
| 766 | * |
||
| 767 | * @return string |
||
| 768 | */ |
||
| 769 | public function get_image_alt( $image_url ) { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Get image alternate text by attachment id. |
||
| 782 | * |
||
| 783 | * @since 2.0.0 |
||
| 784 | * @access public |
||
| 785 | * |
||
| 786 | * @see get_post_meta() |
||
| 787 | * |
||
| 788 | * @param int $attachment_id Attachment id of an image. |
||
| 789 | * |
||
| 790 | * @return string |
||
| 791 | */ |
||
| 792 | public function get_image_alt_by_id( $attachment_id ) { |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Get image description. |
||
| 800 | * |
||
| 801 | * @since 3.0.0 |
||
| 802 | * @access public |
||
| 803 | * |
||
| 804 | * @param string $image_url URL of an image. |
||
| 805 | * |
||
| 806 | * @return string |
||
| 807 | */ |
||
| 808 | public function get_image_description( $image_url ) { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Get image description by id. |
||
| 814 | * |
||
| 815 | * @since 3.0.0 |
||
| 816 | * @access public |
||
| 817 | * |
||
| 818 | * @param int $attachment_id attachment id of an image. |
||
| 819 | * |
||
| 820 | * @return string |
||
| 821 | */ |
||
| 822 | public function get_image_description_by_id( $attachment_id ) { |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Get link to image. |
||
| 828 | * |
||
| 829 | * @since 3.4.0 |
||
| 830 | * @access public |
||
| 831 | * |
||
| 832 | * @param int $attachment_id Attachment id of an image. |
||
| 833 | * |
||
| 834 | * @return string|null |
||
| 835 | */ |
||
| 836 | public function get_link_to_image( $attachment_id ) { |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Get all attachment ids of the post. |
||
| 842 | * |
||
| 843 | * @since 2.0.0 |
||
| 844 | * @access public |
||
| 845 | * |
||
| 846 | * @see get_post_meta() |
||
| 847 | * |
||
| 848 | * @param int $post_id id of the current post. |
||
| 849 | * |
||
| 850 | * @return array |
||
| 851 | */ |
||
| 852 | public function get_post_attachment_ids( $post_id ) { |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Fetches featured image data of nth position. |
||
| 868 | * |
||
| 869 | * @since 3.0.0 |
||
| 870 | * @access public |
||
| 871 | * |
||
| 872 | * @see get_featured_images() |
||
| 873 | * |
||
| 874 | * @param int $position Position of the featured image. |
||
| 875 | * @param int $post_id Current post id. |
||
| 876 | * |
||
| 877 | * @return array if found, null otherwise. |
||
| 878 | */ |
||
| 879 | public function get_nth_featured_image( $position, $post_id = null ) { |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Check if the image is attached with the particular post. |
||
| 892 | * |
||
| 893 | * @since 2.0.0 |
||
| 894 | * @access public |
||
| 895 | * |
||
| 896 | * @see get_post_attachment_ids() |
||
| 897 | * |
||
| 898 | * @param int $attachment_id Attachment id of an image. |
||
| 899 | * @param int $post_id Current post id. |
||
| 900 | * |
||
| 901 | * @return bool |
||
| 902 | */ |
||
| 903 | public function is_attached( $attachment_id, $post_id ) { |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Retrieve featured images for specific post(s). |
||
| 915 | * |
||
| 916 | * @since 2.0.0 |
||
| 917 | * @access public |
||
| 918 | * |
||
| 919 | * @see get_post_meta() |
||
| 920 | * |
||
| 921 | * @param integer $post_id id of the current post. |
||
| 922 | * |
||
| 923 | * @return array |
||
| 924 | */ |
||
| 925 | public function get_featured_images( $post_id = null ) { |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Check to see if the upload url is already available in path. |
||
| 961 | * |
||
| 962 | * @since 3.1.14 |
||
| 963 | * @access protected |
||
| 964 | * |
||
| 965 | * @param string $img Uploaded image. |
||
| 966 | * |
||
| 967 | * @return string |
||
| 968 | */ |
||
| 969 | protected function get_real_upload_path( $img ) { |
||
| 977 | |||
| 978 | /** |
||
| 979 | * Retrieve featured images for specific post(s) including the default Featured Image. |
||
| 980 | * |
||
| 981 | * @since 3.1.7 |
||
| 982 | * @access public |
||
| 983 | * |
||
| 984 | * @see $this->get_featured_images() |
||
| 985 | * |
||
| 986 | * @param int $post_id Current post id. |
||
| 987 | * |
||
| 988 | * @return array An array of images or an empty array on failure |
||
| 989 | */ |
||
| 990 | public function get_all_featured_images( $post_id = null ) { |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
| 1015 | * |
||
| 1016 | * @since 1.0.0 |
||
| 1017 | * @access public |
||
| 1018 | * |
||
| 1019 | * @see load_plugin_textdomain() |
||
| 1020 | * @see plugin_basename() |
||
| 1021 | * @action plugins_loaded |
||
| 1022 | * |
||
| 1023 | * @codeCoverageIgnore |
||
| 1024 | * |
||
| 1025 | * @return void |
||
| 1026 | */ |
||
| 1027 | public function load_plugin_textdomain() { |
||
| 1034 | } |
||
| 1035 | |||
| 1046 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: