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 |
||
| 47 | class Dynamic_Featured_Image { |
||
| 48 | /** |
||
| 49 | * Current version of the plugin. |
||
| 50 | * |
||
| 51 | * @since 3.0.0 |
||
| 52 | */ |
||
| 53 | const VERSION = '3.5.2'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Text domain. |
||
| 57 | * |
||
| 58 | * @since 3.6.0 |
||
| 59 | */ |
||
| 60 | const TEXT_DOMAIN = 'dynamic-featured-image'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Documentation Link. |
||
| 64 | * |
||
| 65 | * @since 3.6.0 |
||
| 66 | */ |
||
| 67 | const WIKI_LINK = 'https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Image upload directory. |
||
| 71 | * |
||
| 72 | * @var $upload_dir string |
||
| 73 | */ |
||
| 74 | private $upload_dir; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Image upload URL. |
||
| 78 | * |
||
| 79 | * @var $upload_url string |
||
| 80 | */ |
||
| 81 | private $upload_url; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Database object. |
||
| 85 | * |
||
| 86 | * @var $db wpdb |
||
| 87 | */ |
||
| 88 | private $db; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Title for dfi metabox. |
||
| 92 | * |
||
| 93 | * @var $metabox_title string |
||
| 94 | */ |
||
| 95 | protected $metabox_title; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Users post type filter for dfi metabox. |
||
| 99 | * |
||
| 100 | * @var $user_filter array |
||
| 101 | */ |
||
| 102 | protected $user_filter; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Constructor. Hooks all interactions to initialize the class. |
||
| 106 | * |
||
| 107 | * @since 1.0.0 |
||
| 108 | * @access public |
||
| 109 | * @global object $wpdb |
||
| 110 | * |
||
| 111 | * @see add_action() |
||
| 112 | */ |
||
| 113 | public function __construct() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Return site protocol. |
||
| 150 | * |
||
| 151 | * @since 3.5.1 |
||
| 152 | * @access public |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | private function get_protocol() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Add required admin scripts. |
||
| 163 | * |
||
| 164 | * @since 1.0.0 |
||
| 165 | * @access public |
||
| 166 | * |
||
| 167 | * @see wp_enque_style() |
||
| 168 | * @see wp_register_script() |
||
| 169 | * @see wp_enqueue_script() |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | public function enqueue_admin_scripts() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Add upgrade link. |
||
| 200 | * |
||
| 201 | * @access public |
||
| 202 | * @since 3.5.1 |
||
| 203 | * @action plugin_action_links |
||
| 204 | * |
||
| 205 | * @codeCoverageIgnore |
||
| 206 | * |
||
| 207 | * @param array $links Action links. |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function dfi_action_links( $links ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Add featured meta boxes dynamically. |
||
| 221 | * |
||
| 222 | * @since 1.0.0 |
||
| 223 | * @access public |
||
| 224 | * @global object $post |
||
| 225 | * |
||
| 226 | * @see get_post_meta() |
||
| 227 | * @see get_post_types() |
||
| 228 | * @see add_meta_box() |
||
| 229 | * @see add_filter() |
||
| 230 | * |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | public function initialize_featured_box() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Translates more than one digit number digit by digit. |
||
| 264 | * |
||
| 265 | * @param int $number Integer to be translated. |
||
| 266 | * |
||
| 267 | * @return string Translated number |
||
| 268 | */ |
||
| 269 | protected function get_number_translation( $number ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Adds meta boxes. |
||
| 285 | * |
||
| 286 | * @param array $post_types Post types to show featured image box. |
||
| 287 | * @param object $featured Callback arguments. |
||
| 288 | * @param int $i Index of the featured image. |
||
| 289 | * |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | private function dfi_add_meta_box( $post_types, $featured = null, $i = null ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Separate thumb and full image url from given URL string. |
||
| 326 | * |
||
| 327 | * @since 3.3.1 |
||
| 328 | * |
||
| 329 | * @param string $url_string Url string. |
||
| 330 | * @param string $state Thumb or full. |
||
| 331 | * |
||
| 332 | * @return string|null |
||
| 333 | */ |
||
| 334 | private function separate( $url_string, $state = 'thumb' ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Create a nonce field |
||
| 346 | * |
||
| 347 | * @since 3.5.0 |
||
| 348 | * |
||
| 349 | * @see wp_nonce_field() |
||
| 350 | * @see plugin_basename() |
||
| 351 | * |
||
| 352 | * @codeCoverageIgnore |
||
| 353 | * |
||
| 354 | * @param string $key Nonce key. |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | protected function nonce_field( $key ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Featured meta box as seen in the admin |
||
| 364 | * |
||
| 365 | * @since 1.0.0 |
||
| 366 | * @access public |
||
| 367 | * |
||
| 368 | * @param object $post Global post object. |
||
| 369 | * @param array $featured Array containing featured image count. |
||
| 370 | * |
||
| 371 | * @throws Exception Medium size image not found. |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | public function featured_meta_box( $post, $featured ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Returns featured box html content. |
||
| 404 | * |
||
| 405 | * @since 3.1.0 |
||
| 406 | * @access private |
||
| 407 | * |
||
| 408 | * @param string $featured_img_trimmed Medium sized image. |
||
| 409 | * @param string $featured_img Full sized image. |
||
| 410 | * @param string $featured_id Attachment Id. |
||
| 411 | * @param string $thumbnail Thumb sized image. |
||
| 412 | * @param int $post_id Post id. |
||
| 413 | * |
||
| 414 | * @return string Html content |
||
| 415 | */ |
||
| 416 | private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Load new featured meta box via ajax |
||
| 433 | * |
||
| 434 | * @since 1.0.0 |
||
| 435 | * @access public |
||
| 436 | * |
||
| 437 | * @return void |
||
| 438 | */ |
||
| 439 | public function ajax_callback() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Add custom class 'featured-meta-box' to meta box. |
||
| 471 | * |
||
| 472 | * @since 1.0.0 |
||
| 473 | * @access public |
||
| 474 | * |
||
| 475 | * @see add_metabox_classes |
||
| 476 | * |
||
| 477 | * @param array $classes Classes to add in the meta box. |
||
| 478 | * |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | public function add_metabox_classes( $classes ) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Add custom fields in media uploader. |
||
| 489 | * |
||
| 490 | * @since 3.4.0 |
||
| 491 | * |
||
| 492 | * @param array $form_fields Fields to include in media attachment form. |
||
| 493 | * @param array $post Post data. |
||
| 494 | * |
||
| 495 | * @return array |
||
| 496 | */ |
||
| 497 | public function media_attachment_custom_fields( $form_fields, $post ) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Save values of media uploader custom fields. |
||
| 509 | * |
||
| 510 | * @since 3.4.0 |
||
| 511 | * |
||
| 512 | * @param array $post Post data for database. |
||
| 513 | * @param array $attachment Attachment fields from $_POST form. |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | 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 int $post_id Current post id. |
||
| 536 | * |
||
| 537 | * @return bool|null |
||
| 538 | */ |
||
| 539 | public function save_meta( $post_id ) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Verify metabox nonces. |
||
| 564 | * |
||
| 565 | * @access protected |
||
| 566 | * @see wp_verify_nonce() |
||
| 567 | * |
||
| 568 | * @return bool |
||
| 569 | */ |
||
| 570 | protected function verify_nonces() { |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Add update notice. Displayed in plugin update page. |
||
| 591 | * |
||
| 592 | * @since 2.0.0 |
||
| 593 | * @access public |
||
| 594 | * |
||
| 595 | * @return void |
||
| 596 | */ |
||
| 597 | public function update_notice() { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Execute query. |
||
| 606 | * |
||
| 607 | * @param string $query Query to execute. |
||
| 608 | * |
||
| 609 | * @return null|string |
||
| 610 | */ |
||
| 611 | private function execute_query( $query ) { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Get attachment id of the image by image url. |
||
| 617 | * |
||
| 618 | * @since 3.1.7 |
||
| 619 | * @access protected |
||
| 620 | * @global object $wpdb |
||
| 621 | * |
||
| 622 | * @param string $image_url URL of an image. |
||
| 623 | * |
||
| 624 | * @return string |
||
| 625 | */ |
||
| 626 | protected function get_attachment_id( $image_url ) { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Get image url of the image by attachment id. |
||
| 632 | * |
||
| 633 | * @since 2.0.0 |
||
| 634 | * @access public |
||
| 635 | * |
||
| 636 | * @see wp_get_attachment_image_src() |
||
| 637 | * |
||
| 638 | * @param int $attachment_id attachment id of an image. |
||
| 639 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | public function get_image_url( $attachment_id, $size = 'full' ) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Get image thumbnail url of specific size by image url. |
||
| 651 | * |
||
| 652 | * @since 2.0.0 |
||
| 653 | * @access public |
||
| 654 | * |
||
| 655 | * @see get_image_id() |
||
| 656 | * @see wp_get_attachment_image_src() |
||
| 657 | * |
||
| 658 | * @param string $image_url url of an image. |
||
| 659 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
| 660 | * |
||
| 661 | * @return string |
||
| 662 | */ |
||
| 663 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Gets attachment id from given image url. |
||
| 672 | * |
||
| 673 | * @param string $image_url url of an image. |
||
| 674 | * |
||
| 675 | * @since 2.0.0 |
||
| 676 | * @access public |
||
| 677 | * |
||
| 678 | * @return int|null attachment id of an image |
||
| 679 | */ |
||
| 680 | public function get_image_id( $image_url ) { |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Get image title. |
||
| 698 | * |
||
| 699 | * @since 2.0.0 |
||
| 700 | * @access public |
||
| 701 | * |
||
| 702 | * @param string $image_url URL of an image. |
||
| 703 | * |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | 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 int $attachment_id Attachment id of an image. |
||
| 717 | * |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | public function get_image_title_by_id( $attachment_id ) { |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Get image caption. |
||
| 726 | * |
||
| 727 | * @since 2.0.0 |
||
| 728 | * @access public |
||
| 729 | * |
||
| 730 | * @param string $image_url URL of an image. |
||
| 731 | * |
||
| 732 | * @return string |
||
| 733 | */ |
||
| 734 | public function get_image_caption( $image_url ) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Get image caption by id. |
||
| 740 | * |
||
| 741 | * @since 2.0.0 |
||
| 742 | * @access public |
||
| 743 | * |
||
| 744 | * @param int $attachment_id Attachment id of an image. |
||
| 745 | * |
||
| 746 | * @return string |
||
| 747 | */ |
||
| 748 | public function get_image_caption_by_id( $attachment_id ) { |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Get image alternate text. |
||
| 754 | * |
||
| 755 | * @since 2.0.0 |
||
| 756 | * @access public |
||
| 757 | * |
||
| 758 | * @see get_post_meta() |
||
| 759 | * |
||
| 760 | * @param string $image_url URL of an image. |
||
| 761 | * |
||
| 762 | * @return string |
||
| 763 | */ |
||
| 764 | public function get_image_alt( $image_url ) { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Get image alternate text by attachment id. |
||
| 777 | * |
||
| 778 | * @since 2.0.0 |
||
| 779 | * @access public |
||
| 780 | * |
||
| 781 | * @see get_post_meta() |
||
| 782 | * |
||
| 783 | * @param int $attachment_id Attachment id of an image. |
||
| 784 | * |
||
| 785 | * @return string |
||
| 786 | */ |
||
| 787 | public function get_image_alt_by_id( $attachment_id ) { |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Get image description. |
||
| 795 | * |
||
| 796 | * @since 3.0.0 |
||
| 797 | * @access public |
||
| 798 | * |
||
| 799 | * @param string $image_url URL of an image. |
||
| 800 | * |
||
| 801 | * @return string |
||
| 802 | */ |
||
| 803 | public function get_image_description( $image_url ) { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Get image description by id. |
||
| 809 | * |
||
| 810 | * @since 3.0.0 |
||
| 811 | * @access public |
||
| 812 | * |
||
| 813 | * @param int $attachment_id attachment id of an image. |
||
| 814 | * |
||
| 815 | * @return string |
||
| 816 | */ |
||
| 817 | public function get_image_description_by_id( $attachment_id ) { |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Get link to image. |
||
| 823 | * |
||
| 824 | * @since 3.4.0 |
||
| 825 | * @access public |
||
| 826 | * |
||
| 827 | * @param int $attachment_id Attachment id of an image. |
||
| 828 | * |
||
| 829 | * @return string|null |
||
| 830 | */ |
||
| 831 | public function get_link_to_image( $attachment_id ) { |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Get all attachment ids of the post. |
||
| 837 | * |
||
| 838 | * @since 2.0.0 |
||
| 839 | * @access public |
||
| 840 | * |
||
| 841 | * @see get_post_meta() |
||
| 842 | * |
||
| 843 | * @param int $post_id id of the current post. |
||
| 844 | * |
||
| 845 | * @return array |
||
| 846 | */ |
||
| 847 | public function get_post_attachment_ids( $post_id ) { |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Fetches featured image data of nth position. |
||
| 863 | * |
||
| 864 | * @since 3.0.0 |
||
| 865 | * @access public |
||
| 866 | * |
||
| 867 | * @see get_featured_images() |
||
| 868 | * |
||
| 869 | * @param int $position Position of the featured image. |
||
| 870 | * @param int $post_id Current post id. |
||
| 871 | * |
||
| 872 | * @return array if found, null otherwise. |
||
| 873 | */ |
||
| 874 | public function get_nth_featured_image( $position, $post_id = null ) { |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Check if the image is attached with the particular post. |
||
| 887 | * |
||
| 888 | * @since 2.0.0 |
||
| 889 | * @access public |
||
| 890 | * |
||
| 891 | * @see get_post_attachment_ids() |
||
| 892 | * |
||
| 893 | * @param int $attachment_id Attachment id of an image. |
||
| 894 | * @param int $post_id Current post id. |
||
| 895 | * |
||
| 896 | * @return bool |
||
| 897 | */ |
||
| 898 | public function is_attached( $attachment_id, $post_id ) { |
||
| 907 | |||
| 908 | /** |
||
| 909 | * Retrieve featured images for specific post(s). |
||
| 910 | * |
||
| 911 | * @since 2.0.0 |
||
| 912 | * @access public |
||
| 913 | * |
||
| 914 | * @see get_post_meta() |
||
| 915 | * |
||
| 916 | * @param integer $post_id id of the current post. |
||
| 917 | * |
||
| 918 | * @return array |
||
| 919 | */ |
||
| 920 | public function get_featured_images( $post_id = null ) { |
||
| 953 | |||
| 954 | /** |
||
| 955 | * Check to see if the upload url is already available in path. |
||
| 956 | * |
||
| 957 | * @since 3.1.14 |
||
| 958 | * @access protected |
||
| 959 | * |
||
| 960 | * @param string $img Uploaded image. |
||
| 961 | * |
||
| 962 | * @return string |
||
| 963 | */ |
||
| 964 | protected function get_real_upload_path( $img ) { |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Retrieve featured images for specific post(s) including the default Featured Image. |
||
| 975 | * |
||
| 976 | * @since 3.1.7 |
||
| 977 | * @access public |
||
| 978 | * |
||
| 979 | * @see $this->get_featured_images() |
||
| 980 | * |
||
| 981 | * @param int $post_id Current post id. |
||
| 982 | * |
||
| 983 | * @return array An array of images or an empty array on failure |
||
| 984 | */ |
||
| 985 | public function get_all_featured_images( $post_id = null ) { |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
| 1010 | * |
||
| 1011 | * @since 1.0.0 |
||
| 1012 | * @access public |
||
| 1013 | * |
||
| 1014 | * @see load_plugin_textdomain() |
||
| 1015 | * @see plugin_basename() |
||
| 1016 | * @action plugins_loaded |
||
| 1017 | * |
||
| 1018 | * @codeCoverageIgnore |
||
| 1019 | * |
||
| 1020 | * @return void |
||
| 1021 | */ |
||
| 1022 | public function load_plugin_textdomain() { |
||
| 1029 | } |
||
| 1030 | |||
| 1041 |
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: