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