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() { |
||
196 | |||
197 | /** |
||
198 | * Add upgrade link. |
||
199 | * |
||
200 | * @access public |
||
201 | * @since 3.5.1 |
||
202 | * @action plugin_action_links |
||
203 | * |
||
204 | * @codeCoverageIgnore |
||
205 | * |
||
206 | * @param array $links Action links. |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | public function dfi_action_links( $links ) { |
||
217 | |||
218 | /** |
||
219 | * Add featured meta boxes dynamically. |
||
220 | * |
||
221 | * @since 1.0.0 |
||
222 | * @access public |
||
223 | * @global object $post |
||
224 | * |
||
225 | * @see get_post_meta() |
||
226 | * @see get_post_types() |
||
227 | * @see add_meta_box() |
||
228 | * @see add_filter() |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | public function initialize_featured_box() { |
||
260 | |||
261 | /** |
||
262 | * Translates more than one digit number digit by digit. |
||
263 | * |
||
264 | * @param int $number Integer to be translated. |
||
265 | * |
||
266 | * @return string Translated number |
||
267 | */ |
||
268 | protected function get_number_translation( $number ) { |
||
281 | |||
282 | /** |
||
283 | * Adds meta boxes. |
||
284 | * |
||
285 | * @param array $post_types Post types to show featured image box. |
||
286 | * @param object $featured Callback arguments. |
||
287 | * @param int $i Index of the featured image. |
||
288 | * |
||
289 | * @return void |
||
290 | */ |
||
291 | private function dfi_add_meta_box( $post_types, $featured = null, $i = null ) { |
||
322 | |||
323 | /** |
||
324 | * Separate thumb and full image url from given URL string. |
||
325 | * |
||
326 | * @since 3.3.1 |
||
327 | * |
||
328 | * @param string $url_string Url string. |
||
329 | * @param string $state Thumb or full. |
||
330 | * |
||
331 | * @return string|null |
||
332 | */ |
||
333 | private function separate( $url_string, $state = 'thumb' ) { |
||
342 | |||
343 | /** |
||
344 | * Create a nonce field |
||
345 | * |
||
346 | * @since 3.5.0 |
||
347 | * |
||
348 | * @see wp_nonce_field() |
||
349 | * @see plugin_basename() |
||
350 | * |
||
351 | * @codeCoverageIgnore |
||
352 | * |
||
353 | * @param string $key Nonce key. |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | protected function nonce_field( $key ) { |
||
360 | |||
361 | /** |
||
362 | * Featured meta box as seen in the admin |
||
363 | * |
||
364 | * @since 1.0.0 |
||
365 | * @access public |
||
366 | * |
||
367 | * @param object $post Global post object. |
||
368 | * @param array $featured Array containing featured image count. |
||
369 | * |
||
370 | * @throws Exception Medium size image not found. |
||
371 | * @return void |
||
372 | */ |
||
373 | public function featured_meta_box( $post, $featured ) { |
||
400 | |||
401 | /** |
||
402 | * Returns featured box html content. |
||
403 | * |
||
404 | * @since 3.1.0 |
||
405 | * @access private |
||
406 | * |
||
407 | * @param string $featured_img_trimmed Medium sized image. |
||
408 | * @param string $featured_img Full sized image. |
||
409 | * @param string $featured_id Attachment Id. |
||
410 | * @param string $thumbnail Thumb sized image. |
||
411 | * @param int $post_id Post id. |
||
412 | * |
||
413 | * @return string Html content |
||
414 | */ |
||
415 | private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id ) { |
||
429 | |||
430 | /** |
||
431 | * Load new featured meta box via ajax |
||
432 | * |
||
433 | * @since 1.0.0 |
||
434 | * @access public |
||
435 | * |
||
436 | * @return void |
||
437 | */ |
||
438 | public function ajax_callback() { |
||
465 | |||
466 | /** |
||
467 | * Add custom class 'featured-meta-box' to meta box. |
||
468 | * |
||
469 | * @since 1.0.0 |
||
470 | * @access public |
||
471 | * |
||
472 | * @see add_metabox_classes |
||
473 | * |
||
474 | * @param array $classes Classes to add in the meta box. |
||
475 | * |
||
476 | * @return array |
||
477 | */ |
||
478 | public function add_metabox_classes( $classes ) { |
||
483 | |||
484 | /** |
||
485 | * Add custom fields in media uploader. |
||
486 | * |
||
487 | * @since 3.4.0 |
||
488 | * |
||
489 | * @param array $form_fields Fields to include in media attachment form. |
||
490 | * @param array $post Post data. |
||
491 | * |
||
492 | * @return array |
||
493 | */ |
||
494 | public function media_attachment_custom_fields( $form_fields, $post ) { |
||
503 | |||
504 | /** |
||
505 | * Save values of media uploader custom fields. |
||
506 | * |
||
507 | * @since 3.4.0 |
||
508 | * |
||
509 | * @param array $post Post data for database. |
||
510 | * @param array $attachment Attachment fields from $_POST form. |
||
511 | * |
||
512 | * @return array |
||
513 | */ |
||
514 | public function media_attachment_custom_fields_save( $post, $attachment ) { |
||
521 | |||
522 | /** |
||
523 | * Update featured images in the database. |
||
524 | * |
||
525 | * @since 1.0.0 |
||
526 | * @access public |
||
527 | * |
||
528 | * @see plugin_basename() |
||
529 | * @see update_post_meta() |
||
530 | * @see current_user_can() |
||
531 | * |
||
532 | * @param int $post_id Current post id. |
||
533 | * |
||
534 | * @return bool|null |
||
535 | */ |
||
536 | public function save_meta( $post_id ) { |
||
558 | |||
559 | /** |
||
560 | * Verify metabox nonces. |
||
561 | * |
||
562 | * @access protected |
||
563 | * @see wp_verify_nonce() |
||
564 | * |
||
565 | * @return bool |
||
566 | */ |
||
567 | protected function verify_nonces() { |
||
586 | |||
587 | /** |
||
588 | * Add update notice. Displayed in plugin update page. |
||
589 | * |
||
590 | * @since 2.0.0 |
||
591 | * @access public |
||
592 | * |
||
593 | * @return void |
||
594 | */ |
||
595 | public function update_notice() { |
||
601 | |||
602 | /** |
||
603 | * Execute query. |
||
604 | * |
||
605 | * @param string $query Query to execute. |
||
606 | * |
||
607 | * @return null|string |
||
608 | */ |
||
609 | private function execute_query( $query ) { |
||
612 | |||
613 | /** |
||
614 | * Get attachment id of the image by image url. |
||
615 | * |
||
616 | * @since 3.1.7 |
||
617 | * @access protected |
||
618 | * @global object $wpdb |
||
619 | * |
||
620 | * @param string $image_url URL of an image. |
||
621 | * |
||
622 | * @return string |
||
623 | */ |
||
624 | protected function get_attachment_id( $image_url ) { |
||
627 | |||
628 | /** |
||
629 | * Get image url of the image by attachment id. |
||
630 | * |
||
631 | * @since 2.0.0 |
||
632 | * @access public |
||
633 | * |
||
634 | * @see wp_get_attachment_image_src() |
||
635 | * |
||
636 | * @param int $attachment_id attachment id of an image. |
||
637 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
638 | * |
||
639 | * @return string |
||
640 | */ |
||
641 | public function get_image_url( $attachment_id, $size = 'full' ) { |
||
646 | |||
647 | /** |
||
648 | * Get image thumbnail url of specific size by image url. |
||
649 | * |
||
650 | * @since 2.0.0 |
||
651 | * @access public |
||
652 | * |
||
653 | * @see get_image_id() |
||
654 | * @see wp_get_attachment_image_src() |
||
655 | * |
||
656 | * @param string $image_url url of an image. |
||
657 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
658 | * |
||
659 | * @return string |
||
660 | */ |
||
661 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { |
||
667 | |||
668 | /** |
||
669 | * Gets attachment id from given image url. |
||
670 | * |
||
671 | * @param string $image_url url of an image. |
||
672 | * |
||
673 | * @since 2.0.0 |
||
674 | * @access public |
||
675 | * |
||
676 | * @return int|null attachment id of an image |
||
677 | */ |
||
678 | public function get_image_id( $image_url ) { |
||
693 | |||
694 | /** |
||
695 | * Get image title. |
||
696 | * |
||
697 | * @since 2.0.0 |
||
698 | * @access public |
||
699 | * |
||
700 | * @param string $image_url URL of an image. |
||
701 | * |
||
702 | * @return string |
||
703 | */ |
||
704 | public function get_image_title( $image_url ) { |
||
707 | |||
708 | /** |
||
709 | * Get image title by id. |
||
710 | * |
||
711 | * @since 2.0.0 |
||
712 | * @access public |
||
713 | * |
||
714 | * @param int $attachment_id Attachment id of an image. |
||
715 | * |
||
716 | * @return string |
||
717 | */ |
||
718 | public function get_image_title_by_id( $attachment_id ) { |
||
721 | |||
722 | /** |
||
723 | * Get image caption. |
||
724 | * |
||
725 | * @since 2.0.0 |
||
726 | * @access public |
||
727 | * |
||
728 | * @param string $image_url URL of an image. |
||
729 | * |
||
730 | * @return string |
||
731 | */ |
||
732 | public function get_image_caption( $image_url ) { |
||
735 | |||
736 | /** |
||
737 | * Get image caption by id. |
||
738 | * |
||
739 | * @since 2.0.0 |
||
740 | * @access public |
||
741 | * |
||
742 | * @param int $attachment_id Attachment id of an image. |
||
743 | * |
||
744 | * @return string |
||
745 | */ |
||
746 | public function get_image_caption_by_id( $attachment_id ) { |
||
749 | |||
750 | /** |
||
751 | * Get image alternate text. |
||
752 | * |
||
753 | * @since 2.0.0 |
||
754 | * @access public |
||
755 | * |
||
756 | * @see get_post_meta() |
||
757 | * |
||
758 | * @param string $image_url URL of an image. |
||
759 | * |
||
760 | * @return string |
||
761 | */ |
||
762 | public function get_image_alt( $image_url ) { |
||
772 | |||
773 | /** |
||
774 | * Get image alternate text by attachment id. |
||
775 | * |
||
776 | * @since 2.0.0 |
||
777 | * @access public |
||
778 | * |
||
779 | * @see get_post_meta() |
||
780 | * |
||
781 | * @param int $attachment_id Attachment id of an image. |
||
782 | * |
||
783 | * @return string |
||
784 | */ |
||
785 | public function get_image_alt_by_id( $attachment_id ) { |
||
790 | |||
791 | /** |
||
792 | * Get image description. |
||
793 | * |
||
794 | * @since 3.0.0 |
||
795 | * @access public |
||
796 | * |
||
797 | * @param string $image_url URL of an image. |
||
798 | * |
||
799 | * @return string |
||
800 | */ |
||
801 | public function get_image_description( $image_url ) { |
||
804 | |||
805 | /** |
||
806 | * Get image description by id. |
||
807 | * |
||
808 | * @since 3.0.0 |
||
809 | * @access public |
||
810 | * |
||
811 | * @param int $attachment_id attachment id of an image. |
||
812 | * |
||
813 | * @return string |
||
814 | */ |
||
815 | public function get_image_description_by_id( $attachment_id ) { |
||
818 | |||
819 | /** |
||
820 | * Get link to image. |
||
821 | * |
||
822 | * @since 3.4.0 |
||
823 | * @access public |
||
824 | * |
||
825 | * @param int $attachment_id Attachment id of an image. |
||
826 | * |
||
827 | * @return string|null |
||
828 | */ |
||
829 | public function get_link_to_image( $attachment_id ) { |
||
832 | |||
833 | /** |
||
834 | * Get all attachment ids of the post. |
||
835 | * |
||
836 | * @since 2.0.0 |
||
837 | * @access public |
||
838 | * |
||
839 | * @see get_post_meta() |
||
840 | * |
||
841 | * @param int $post_id id of the current post. |
||
842 | * |
||
843 | * @return array |
||
844 | */ |
||
845 | public function get_post_attachment_ids( $post_id ) { |
||
858 | |||
859 | /** |
||
860 | * Fetches featured image data of nth position. |
||
861 | * |
||
862 | * @since 3.0.0 |
||
863 | * @access public |
||
864 | * |
||
865 | * @see get_featured_images() |
||
866 | * |
||
867 | * @param int $position Position of the featured image. |
||
868 | * @param int $post_id Current post id. |
||
869 | * |
||
870 | * @return array if found, null otherwise. |
||
871 | */ |
||
872 | public function get_nth_featured_image( $position, $post_id = null ) { |
||
882 | |||
883 | /** |
||
884 | * Check if the image is attached with the particular post. |
||
885 | * |
||
886 | * @since 2.0.0 |
||
887 | * @access public |
||
888 | * |
||
889 | * @see get_post_attachment_ids() |
||
890 | * |
||
891 | * @param int $attachment_id Attachment id of an image. |
||
892 | * @param int $post_id Current post id. |
||
893 | * |
||
894 | * @return bool |
||
895 | */ |
||
896 | public function is_attached( $attachment_id, $post_id ) { |
||
905 | |||
906 | /** |
||
907 | * Retrieve featured images for specific post(s). |
||
908 | * |
||
909 | * @since 2.0.0 |
||
910 | * @access public |
||
911 | * |
||
912 | * @see get_post_meta() |
||
913 | * |
||
914 | * @param integer $post_id id of the current post. |
||
915 | * |
||
916 | * @return array |
||
917 | */ |
||
918 | public function get_featured_images( $post_id = null ) { |
||
951 | |||
952 | /** |
||
953 | * Check to see if the upload url is already available in path. |
||
954 | * |
||
955 | * @since 3.1.14 |
||
956 | * @access protected |
||
957 | * |
||
958 | * @param string $img Uploaded image. |
||
959 | * |
||
960 | * @return string |
||
961 | */ |
||
962 | protected function get_real_upload_path( $img ) { |
||
970 | |||
971 | /** |
||
972 | * Retrieve featured images for specific post(s) including the default Featured Image. |
||
973 | * |
||
974 | * @since 3.1.7 |
||
975 | * @access public |
||
976 | * |
||
977 | * @see $this->get_featured_images() |
||
978 | * |
||
979 | * @param int $post_id Current post id. |
||
980 | * |
||
981 | * @return array An array of images or an empty array on failure |
||
982 | */ |
||
983 | public function get_all_featured_images( $post_id = null ) { |
||
1005 | |||
1006 | /** |
||
1007 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
1008 | * |
||
1009 | * @since 1.0.0 |
||
1010 | * @access public |
||
1011 | * |
||
1012 | * @see load_plugin_textdomain() |
||
1013 | * @see plugin_basename() |
||
1014 | * @action plugins_loaded |
||
1015 | * |
||
1016 | * @codeCoverageIgnore |
||
1017 | * |
||
1018 | * @return void |
||
1019 | */ |
||
1020 | public function load_plugin_textdomain() { |
||
1027 | } |
||
1028 | |||
1039 |
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: