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 | /** |
||
| 49 | * Current version of the plugin. |
||
| 50 | * |
||
| 51 | * @since 3.0.0 |
||
| 52 | */ |
||
| 53 | const VERSION = '3.5.2'; |
||
| 54 | |||
| 55 | /* Image upload directory */ |
||
| 56 | private $__upload_dir; |
||
| 57 | |||
| 58 | /* Image upload URL */ |
||
| 59 | private $__upload_url; |
||
| 60 | |||
| 61 | /* Database object */ |
||
| 62 | private $__db; |
||
| 63 | |||
| 64 | /* Plugin text domain */ |
||
| 65 | protected $_textDomain; |
||
| 66 | |||
| 67 | /* Title for dfi metabox */ |
||
| 68 | protected $_metabox_title; |
||
| 69 | |||
| 70 | /* Users post type filter for dfi metabox */ |
||
| 71 | protected $_userFilter; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Constructor. Hooks all interactions to initialize the class. |
||
| 75 | * |
||
| 76 | * @since 1.0.0 |
||
| 77 | * @access public |
||
| 78 | * @global object $wpdb |
||
| 79 | * |
||
| 80 | * @see add_action() |
||
| 81 | */ |
||
| 82 | public function __construct() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Return site protocol |
||
| 123 | * |
||
| 124 | * @since 3.5.1 |
||
| 125 | * @access public |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | private function __get_protocol() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add required admin scripts |
||
| 137 | * |
||
| 138 | * @since 1.0.0 |
||
| 139 | * @access public |
||
| 140 | * |
||
| 141 | * @see wp_enque_style() |
||
| 142 | * @see wp_register_script() |
||
| 143 | * @see wp_enqueue_script() |
||
| 144 | * |
||
| 145 | * @return Void |
||
| 146 | */ |
||
| 147 | public function enqueue_admin_scripts() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Add upgrade link |
||
| 177 | * |
||
| 178 | * @access public |
||
| 179 | * @since 3.5.1 |
||
| 180 | * @action plugin_action_links |
||
| 181 | * |
||
| 182 | * @codeCoverageIgnore |
||
| 183 | * |
||
| 184 | * @param array $links Action links |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | public function dfi_action_links( $links ) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Add featured meta boxes dynamically |
||
| 200 | * |
||
| 201 | * @since 1.0.0 |
||
| 202 | * @access public |
||
| 203 | * @global object $post |
||
| 204 | * |
||
| 205 | * @see get_post_meta() |
||
| 206 | * @see get_post_types() |
||
| 207 | * @see add_meta_box() |
||
| 208 | * @see add_filter() |
||
| 209 | * |
||
| 210 | * @return Void |
||
| 211 | */ |
||
| 212 | public function initialize_featured_box() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Translates more than one digit number digit by digit. |
||
| 248 | * |
||
| 249 | * @param Integer $number Integer to be translated |
||
| 250 | * |
||
| 251 | * @return String Translated number |
||
| 252 | */ |
||
| 253 | protected function _get_number_translation( $number ) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * adds meta boxes |
||
| 270 | * |
||
| 271 | * @param Array $postTypes post types to show featured image box |
||
| 272 | * @param Object $featured callback arguments |
||
| 273 | * @param Integer $i index of the featured image |
||
| 274 | * |
||
| 275 | * @return Void |
||
| 276 | */ |
||
| 277 | private function _dfi_add_meta_box( $postTypes, $featured = null, $i = null ) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Separate thumb and full image url from given URL string |
||
| 312 | * |
||
| 313 | * @since 3.3.1 |
||
| 314 | * |
||
| 315 | * @param string $urlString [description] |
||
| 316 | * @param string $state Thumb or full |
||
| 317 | * |
||
| 318 | * @return string|null |
||
| 319 | */ |
||
| 320 | private function _separate( $urlString, $state = 'thumb' ) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Create a nonce field |
||
| 333 | * |
||
| 334 | * @since 3.5.0 |
||
| 335 | * |
||
| 336 | * @see wp_nonce_field() |
||
| 337 | * @see plugin_basename() |
||
| 338 | * |
||
| 339 | * @codeCoverageIgnore |
||
| 340 | * |
||
| 341 | * @param string $key Nonce key |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | protected function _nonce_field( $key ) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Featured meta box as seen in the admin |
||
| 352 | * |
||
| 353 | * @since 1.0.0 |
||
| 354 | * @access public |
||
| 355 | * |
||
| 356 | * @param Object $post global post object |
||
| 357 | * @param Array $featured array containing featured image count |
||
| 358 | * |
||
| 359 | * @return Void |
||
| 360 | */ |
||
| 361 | public function featured_meta_box( $post, $featured ) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Returns featured box html content |
||
| 398 | * @since 3.1.0 |
||
| 399 | * @access private |
||
| 400 | * |
||
| 401 | * @param String $featuredImgTrimmed Medium sized image |
||
| 402 | * @param String $featuredImg Full sized image |
||
| 403 | * @param String $featuredId Attachment Id |
||
| 404 | * @param String $thumbnail Thumb sized image |
||
| 405 | * |
||
| 406 | * @return String Html content |
||
| 407 | */ |
||
| 408 | private function _get_featured_box( $featuredImgTrimmed, $featuredImg, $featuredId, $thumbnail, $postId ) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Load new featured meta box via ajax |
||
| 429 | * |
||
| 430 | * @since 1.0.0 |
||
| 431 | * @access public |
||
| 432 | * |
||
| 433 | * @return Void |
||
| 434 | */ |
||
| 435 | public function ajax_callback() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Add custom class 'featured-meta-box' to meta box |
||
| 465 | * |
||
| 466 | * @since 1.0.0 |
||
| 467 | * @access public |
||
| 468 | * |
||
| 469 | * @see add_metabox_classes |
||
| 470 | * |
||
| 471 | * @param $classes classes to add in the meta box |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function add_metabox_classes( $classes ) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Add custom fields in media uploader |
||
| 485 | * |
||
| 486 | * @since 3.4.0 |
||
| 487 | * |
||
| 488 | * @param $form_fields Array Fields to include in media attachment form |
||
| 489 | * @param $post Array Post data |
||
| 490 | * |
||
| 491 | * @return Array |
||
| 492 | */ |
||
| 493 | public function media_attachment_custom_fields( $form_fields, $post ) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Save values of media uploader custom fields |
||
| 507 | * |
||
| 508 | * @since 3.4.0 |
||
| 509 | * |
||
| 510 | * @param $post Array The post data for database |
||
| 511 | * @param $attachment Array Attachment fields from $_POST form |
||
| 512 | * |
||
| 513 | * @return Array |
||
| 514 | */ |
||
| 515 | public function media_attachment_custom_fields_save( $post, $attachment ) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Update featured images in the database |
||
| 527 | * |
||
| 528 | * @since 1.0.0 |
||
| 529 | * @access public |
||
| 530 | * |
||
| 531 | * @see plugin_basename() |
||
| 532 | * @see update_post_meta() |
||
| 533 | * @see current_user_can() |
||
| 534 | * |
||
| 535 | * @param Integer $post_id current post id |
||
| 536 | * |
||
| 537 | * @return Void |
||
| 538 | */ |
||
| 539 | public function save_meta( $post_id ) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Verify metabox nonces |
||
| 559 | * |
||
| 560 | * @access protected |
||
| 561 | * @see wp_verify_nonce() |
||
| 562 | * |
||
| 563 | * @return boolean |
||
| 564 | */ |
||
| 565 | protected function _verify_nonces() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Add update notice. Displayed in plugin update page. |
||
| 583 | * |
||
| 584 | * @since 2.0.0 |
||
| 585 | * @access public |
||
| 586 | * |
||
| 587 | * @return Void |
||
| 588 | */ |
||
| 589 | public function update_notice() |
||
| 596 | |||
| 597 | /** Helper functions */ |
||
| 598 | |||
| 599 | private function execute_query( $query ) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Get attachment id of the image by image url |
||
| 606 | * |
||
| 607 | * @since 3.1.7 |
||
| 608 | * @access protected |
||
| 609 | * @global object $wpdb |
||
| 610 | * |
||
| 611 | * @param String $image_url url of the image |
||
| 612 | * |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | protected function _get_attachment_id( $image_url ) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Get image url of the image by attachment id |
||
| 624 | * |
||
| 625 | * @since 2.0.0 |
||
| 626 | * @access public |
||
| 627 | * |
||
| 628 | * @see wp_get_attachment_image_src() |
||
| 629 | * |
||
| 630 | * @param Integer $attachment_id attachment id of an image |
||
| 631 | * @param String $size size of the image to fetch (thumbnail, medium, full) |
||
| 632 | * |
||
| 633 | * @return String |
||
| 634 | */ |
||
| 635 | public function get_image_url( $attachment_id, $size = 'full' ) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get image thumbnail url of specific size by image url |
||
| 645 | * |
||
| 646 | * @since 2.0.0 |
||
| 647 | * @access public |
||
| 648 | * |
||
| 649 | * @see get_image_id() |
||
| 650 | * @see wp_get_attachment_image_src() |
||
| 651 | * |
||
| 652 | * @param String $image_url url of an image |
||
| 653 | * @param String $size size of the image to fetch (thumbnail, medium, full) |
||
| 654 | * |
||
| 655 | * @return String |
||
| 656 | */ |
||
| 657 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Gets attachment id from given image url |
||
| 668 | * |
||
| 669 | * @param String $image_url url of an image |
||
| 670 | * |
||
| 671 | * @return Integer|Null attachment id of an image |
||
| 672 | * |
||
| 673 | * @since 2.0.0 |
||
| 674 | * @access public |
||
| 675 | */ |
||
| 676 | public function get_image_id( $image_url ) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Get image title |
||
| 695 | * |
||
| 696 | * @since 2.0.0 |
||
| 697 | * @access public |
||
| 698 | * |
||
| 699 | * @param String $image_url url of an image |
||
| 700 | * |
||
| 701 | * @return String |
||
| 702 | */ |
||
| 703 | public function get_image_title( $image_url ) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Get image title by id |
||
| 712 | * |
||
| 713 | * @since 2.0.0 |
||
| 714 | * @access public |
||
| 715 | * |
||
| 716 | * @param Integer $attachment_id attachment id of an image |
||
| 717 | * |
||
| 718 | * @return String |
||
| 719 | */ |
||
| 720 | public function get_image_title_by_id( $attachment_id ) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Get image caption |
||
| 729 | * |
||
| 730 | * @since 2.0.0 |
||
| 731 | * @access public |
||
| 732 | * |
||
| 733 | * @param String $image_url url of an image |
||
| 734 | * |
||
| 735 | * @return String |
||
| 736 | */ |
||
| 737 | public function get_image_caption( $image_url ) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Get image caption by id |
||
| 746 | * |
||
| 747 | * @since 2.0.0 |
||
| 748 | * @access public |
||
| 749 | * |
||
| 750 | * @param Integer $attachment_id attachment id of an image |
||
| 751 | * |
||
| 752 | * @return String |
||
| 753 | */ |
||
| 754 | public function get_image_caption_by_id( $attachment_id ) |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Get image alternate text |
||
| 763 | * |
||
| 764 | * @since 2.0.0 |
||
| 765 | * @access public |
||
| 766 | * |
||
| 767 | * @see get_post_meta() |
||
| 768 | * |
||
| 769 | * @param String $image_url url of an image |
||
| 770 | * |
||
| 771 | * @return String |
||
| 772 | */ |
||
| 773 | public function get_image_alt( $image_url ) |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Get image alternate text by attachment id |
||
| 789 | * |
||
| 790 | * @since 2.0.0 |
||
| 791 | * @access public |
||
| 792 | * |
||
| 793 | * @see get_post_meta() |
||
| 794 | * |
||
| 795 | * @param Integer $attachment_id attachment id of an image |
||
| 796 | * |
||
| 797 | * @return String |
||
| 798 | */ |
||
| 799 | public function get_image_alt_by_id( $attachment_id ) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Get image description |
||
| 809 | * |
||
| 810 | * @since 3.0.0 |
||
| 811 | * @access public |
||
| 812 | * |
||
| 813 | * @param String $image_url url of an image |
||
| 814 | * |
||
| 815 | * @return String |
||
| 816 | */ |
||
| 817 | public function get_image_description( $image_url ) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Get image description by id |
||
| 826 | * |
||
| 827 | * @since 3.0.0 |
||
| 828 | * @access public |
||
| 829 | * |
||
| 830 | * @param Integer $attachment_id attachment id of an image |
||
| 831 | * |
||
| 832 | * @return String |
||
| 833 | */ |
||
| 834 | public function get_image_description_by_id( $attachment_id ) |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Get link to image |
||
| 843 | * |
||
| 844 | * @since 3.4.0 |
||
| 845 | * @access public |
||
| 846 | * |
||
| 847 | * @param Integer $attachment_id attachment id of an image |
||
| 848 | * |
||
| 849 | * @return string|null |
||
| 850 | */ |
||
| 851 | public function get_link_to_image( $attachment_id ) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Get all attachment ids of the post |
||
| 859 | * |
||
| 860 | * @since 2.0.0 |
||
| 861 | * @access public |
||
| 862 | * |
||
| 863 | * @see get_post_meta() |
||
| 864 | * |
||
| 865 | * @param Integer $post_id id of the current post |
||
| 866 | * |
||
| 867 | * @return Array |
||
| 868 | */ |
||
| 869 | public function get_post_attachment_ids( $post_id ) |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Fetches featured image data of nth position |
||
| 887 | * |
||
| 888 | * @since 3.0.0 |
||
| 889 | * @access public |
||
| 890 | * |
||
| 891 | * @see get_featured_images() |
||
| 892 | * |
||
| 893 | * @param Integer $position position of the featured image |
||
| 894 | * @param Integer $post_id id of the current post |
||
| 895 | * |
||
| 896 | * @return Array if found, null otherwise |
||
| 897 | */ |
||
| 898 | public function get_nth_featured_image( $position, $post_id = null ) |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Check if the image is attached with the particular post |
||
| 913 | * |
||
| 914 | * @since 2.0.0 |
||
| 915 | * @access public |
||
| 916 | * |
||
| 917 | * @see get_post_attachment_ids() |
||
| 918 | * |
||
| 919 | * @param $attachment_id attachment id of an image |
||
| 920 | * @param $post_id id of the current post |
||
| 921 | * |
||
| 922 | * @return boolean |
||
| 923 | */ |
||
| 924 | public function is_attached( $attachment_id, $post_id ) |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Retrieve featured images for specific post(s) |
||
| 938 | * |
||
| 939 | * @since 2.0.0 |
||
| 940 | * @access public |
||
| 941 | * |
||
| 942 | * @see get_post_meta() |
||
| 943 | * |
||
| 944 | * @param Integer $post_id id of the current post |
||
| 945 | * |
||
| 946 | * @return Array |
||
| 947 | */ |
||
| 948 | public function get_featured_images( $post_id = null ) |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Check to see if the upload url is already available in path. |
||
| 985 | * |
||
| 986 | * @since 3.1.14 |
||
| 987 | * @access protected |
||
| 988 | * |
||
| 989 | * @param string $img |
||
| 990 | * |
||
| 991 | * @return string |
||
| 992 | */ |
||
| 993 | protected function _get_real_upload_path( $img ) |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Retrieve featured images for specific post(s) including the default Featured Image |
||
| 1005 | * |
||
| 1006 | * @since 3.1.7 |
||
| 1007 | * @access public |
||
| 1008 | * |
||
| 1009 | * @see $this->get_featured_images() |
||
| 1010 | * |
||
| 1011 | * @param Integer $post_id id of the current post |
||
| 1012 | * |
||
| 1013 | * @return Array An array of images or an empty array on failure |
||
| 1014 | */ |
||
| 1015 | public function get_all_featured_images( $post_id = null ) |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
| 1044 | * |
||
| 1045 | * @since 1.0.0 |
||
| 1046 | * @access public |
||
| 1047 | * |
||
| 1048 | * @see load_plugin_textdomain() |
||
| 1049 | * @see plugin_basename() |
||
| 1050 | * @action plugins_loaded |
||
| 1051 | * |
||
| 1052 | * @codeCoverageIgnore |
||
| 1053 | * |
||
| 1054 | * @return void |
||
| 1055 | */ |
||
| 1056 | public function load_plugin_textdomain() |
||
| 1065 | |||
| 1066 | } // END class Dynamic_Featured_Image |
||
| 1067 | |||
| 1079 |
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: