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.6.8'; |
||
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 | 1 | public function __construct() { |
|
156 | |||
157 | /** |
||
158 | * Return site protocol. |
||
159 | * |
||
160 | * @since 3.5.1 |
||
161 | * @access public |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | private function get_protocol() { |
||
168 | |||
169 | /** |
||
170 | * Add required admin scripts. |
||
171 | * |
||
172 | * @since 1.0.0 |
||
173 | * @access public |
||
174 | * |
||
175 | * @see wp_enqueue_style() |
||
176 | * @see wp_register_script() |
||
177 | * @see wp_enqueue_script() |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | 1 | public function enqueue_admin_scripts() { |
|
204 | |||
205 | /** |
||
206 | * Add upgrade link. |
||
207 | * |
||
208 | * @access public |
||
209 | * @since 3.5.1 |
||
210 | * @action plugin_action_links |
||
211 | * |
||
212 | * @codeCoverageIgnore |
||
213 | * |
||
214 | * @param array $links Action links. |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public function dfi_action_links( $links ) { |
||
225 | |||
226 | /** |
||
227 | * Add featured meta boxes dynamically. |
||
228 | * |
||
229 | * @since 1.0.0 |
||
230 | * @access public |
||
231 | * @global object $post |
||
232 | * |
||
233 | * @see get_post_meta() |
||
234 | * @see get_post_types() |
||
235 | * @see add_meta_box() |
||
236 | * @see add_filter() |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function initialize_featured_box() { |
||
264 | |||
265 | /** |
||
266 | * Translates more than one digit number digit by digit. |
||
267 | * |
||
268 | * @param int $number Integer to be translated. |
||
269 | * |
||
270 | * @return string Translated number |
||
271 | */ |
||
272 | 2 | protected function get_number_translation( $number ) { |
|
285 | |||
286 | /** |
||
287 | * Adds meta boxes. |
||
288 | * |
||
289 | * @param array $post_types Post types to show featured image box. |
||
290 | * @param object $featured Callback arguments. |
||
291 | * @param int $i Index of the featured image. |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | private function dfi_add_meta_box( $post_types, $featured = null, $i = null ) { |
||
326 | |||
327 | /** |
||
328 | * Separate thumb and full image url from given URL string. |
||
329 | * |
||
330 | * @since 3.3.1 |
||
331 | * |
||
332 | * @param string $url_string Url string. |
||
333 | * @param string $state Thumb or full. |
||
334 | * |
||
335 | * @return string|null |
||
336 | */ |
||
337 | 3 | private function separate( $url_string, $state = 'thumb' ) { |
|
346 | |||
347 | /** |
||
348 | * Create a nonce field. |
||
349 | * |
||
350 | * @since 3.5.0 |
||
351 | * |
||
352 | * @see wp_nonce_field() |
||
353 | * @see plugin_basename() |
||
354 | * |
||
355 | * @codeCoverageIgnore |
||
356 | * |
||
357 | * @param string $key Nonce key. |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | protected function nonce_field( $key ) { |
||
364 | |||
365 | /** |
||
366 | * Featured meta box as seen in the admin. |
||
367 | * |
||
368 | * @since 1.0.0 |
||
369 | * @access public |
||
370 | * |
||
371 | * @param object $post Global post object. |
||
372 | * @param array $featured Array containing featured image count. |
||
373 | * |
||
374 | * @throws Exception Medium size image not found. |
||
375 | * @return void |
||
376 | */ |
||
377 | 2 | public function featured_meta_box( $post, $featured ) { |
|
378 | 2 | $featured_img = $featured['args'][0]; |
|
379 | 2 | $featured_id = is_null( $featured['args'][1] ) ? 2 : --$featured['args'][1]; |
|
380 | 2 | $featured_img_full = $featured_img; |
|
381 | 2 | $featured_img_trimmed = $featured_img; |
|
382 | |||
383 | 2 | if ( ! is_null( $featured_img ) ) { |
|
384 | 2 | $featured_img_trimmed = $this->separate( $featured_img ); |
|
385 | 2 | $featured_img_full = $this->separate( $featured_img, 'full' ); |
|
386 | 2 | } |
|
387 | |||
388 | 2 | $attachment_id = $this->get_image_id( $this->upload_url . $featured_img_full ); |
|
389 | |||
390 | 2 | $thumbnail = $this->get_image_thumb_by_attachment_id( $attachment_id, 'medium' ); |
|
391 | 2 | if ( empty( $thumbnail ) ) { |
|
392 | // since medium sized thumbnail image is missing, |
||
393 | // let's set full image url as thumbnail. |
||
394 | $thumbnail = $featured_img_full; |
||
395 | } |
||
396 | |||
397 | // Add a nonce field. |
||
398 | 2 | echo $this->nonce_field( 'dfi_fimageplug-' . $featured_id ); // WPCS: XSS ok. |
|
399 | 2 | echo $this->get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post->ID, $attachment_id ); // WPCS: XSS ok. |
|
400 | 2 | } |
|
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 Featured id number for translation. |
||
411 | * @param string $thumbnail Thumb sized image. |
||
412 | * @param int $post_id Post id. |
||
413 | * @param int $attachment_id Attachment id. |
||
414 | * |
||
415 | * @return string Html content |
||
416 | */ |
||
417 | 2 | private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id, $attachment_id ) { |
|
431 | |||
432 | /** |
||
433 | * Load new featured meta box via ajax. |
||
434 | * |
||
435 | * @since 1.0.0 |
||
436 | * @access public |
||
437 | * |
||
438 | * @return void |
||
439 | */ |
||
440 | 2 | public function ajax_callback() { |
|
469 | |||
470 | /** |
||
471 | * Add custom class 'featured-meta-box' to meta box. |
||
472 | * |
||
473 | * @since 1.0.0 |
||
474 | * @access public |
||
475 | * |
||
476 | * @see add_metabox_classes |
||
477 | * |
||
478 | * @param array $classes Classes to add in the meta box. |
||
479 | * |
||
480 | * @return array |
||
481 | */ |
||
482 | 1 | public function add_metabox_classes( $classes ) { |
|
487 | |||
488 | /** |
||
489 | * Add custom fields in media uploader. |
||
490 | * |
||
491 | * @since 3.4.0 |
||
492 | * |
||
493 | * @param array $form_fields Fields to include in media attachment form. |
||
494 | * @param array $post Post data. |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | 1 | public function media_attachment_custom_fields( $form_fields, $post ) { |
|
507 | |||
508 | /** |
||
509 | * Save values of media uploader custom fields. |
||
510 | * |
||
511 | * @since 3.4.0 |
||
512 | * |
||
513 | * @param array $post Post data for database. |
||
514 | * @param array $attachment Attachment fields from $_POST form. |
||
515 | * |
||
516 | * @return array |
||
517 | */ |
||
518 | 1 | public function media_attachment_custom_fields_save( $post, $attachment ) { |
|
525 | |||
526 | /** |
||
527 | * Update featured images in the database. |
||
528 | * |
||
529 | * @since 1.0.0 |
||
530 | * @access public |
||
531 | * |
||
532 | * @see plugin_basename() |
||
533 | * @see update_post_meta() |
||
534 | * @see current_user_can() |
||
535 | * |
||
536 | * @param int $post_id Current post id. |
||
537 | * |
||
538 | * @return bool|null |
||
539 | */ |
||
540 | 2 | public function save_meta( $post_id ) { |
|
557 | |||
558 | /** |
||
559 | * Sanitize array. |
||
560 | * |
||
561 | * @since 3.6.0 |
||
562 | * @access protected |
||
563 | * |
||
564 | * @param array $input_array Input array. |
||
565 | * |
||
566 | * @return array |
||
567 | */ |
||
568 | 1 | protected function sanitize_array( $input_array ) { |
|
577 | |||
578 | /** |
||
579 | * Verify metabox nonces. |
||
580 | * |
||
581 | * @access protected |
||
582 | * @see wp_verify_nonce() |
||
583 | * |
||
584 | * @return bool |
||
585 | */ |
||
586 | protected function verify_nonces() { |
||
604 | |||
605 | /** |
||
606 | * Add update notice. Displayed in plugin update page. |
||
607 | * |
||
608 | * @since 2.0.0 |
||
609 | * @access public |
||
610 | * |
||
611 | * @return void |
||
612 | */ |
||
613 | 1 | public function update_notice() { |
|
619 | |||
620 | /** |
||
621 | * Execute query. |
||
622 | * |
||
623 | * @param string $query Query to execute. |
||
624 | * |
||
625 | * @return null|string |
||
626 | */ |
||
627 | 6 | private function execute_query( $query ) { |
|
630 | |||
631 | /** |
||
632 | * Get attachment id of the image by image url. |
||
633 | * |
||
634 | * @since 3.1.7 |
||
635 | * @access protected |
||
636 | * @global object $wpdb |
||
637 | * |
||
638 | * @param string $image_url URL of an image. |
||
639 | * |
||
640 | * @return string |
||
641 | */ |
||
642 | 1 | protected function get_attachment_id( $image_url ) { |
|
645 | |||
646 | /** |
||
647 | * Get image url of the image by attachment id. |
||
648 | * |
||
649 | * @since 2.0.0 |
||
650 | * @access public |
||
651 | * |
||
652 | * @see wp_get_attachment_image_src() |
||
653 | * |
||
654 | * @param int $attachment_id attachment id of an image. |
||
655 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
656 | * |
||
657 | * @return string |
||
658 | */ |
||
659 | 1 | public function get_image_url( $attachment_id, $size = 'full' ) { |
|
664 | |||
665 | /** |
||
666 | * Get image thumbnail url of specific size by image url. |
||
667 | * |
||
668 | * @since 2.0.0 |
||
669 | * @access public |
||
670 | * |
||
671 | * @see get_image_id() |
||
672 | * @see wp_get_attachment_image_src() |
||
673 | * |
||
674 | * @param int $attachment_id attachment id of an image. |
||
675 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
676 | * |
||
677 | * @return string |
||
678 | */ |
||
679 | 1 | public function get_image_thumb_by_attachment_id( $attachment_id, $size = 'thumbnail' ) { |
|
684 | |||
685 | /** |
||
686 | * Get image thumbnail url of specific size by image url. |
||
687 | * |
||
688 | * @since 2.0.0 |
||
689 | * @access public |
||
690 | * |
||
691 | * @see get_image_id() |
||
692 | * @see wp_get_attachment_image_src() |
||
693 | * |
||
694 | * @param string $image_url url of an image. |
||
695 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
696 | * |
||
697 | * @return string |
||
698 | */ |
||
699 | 1 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { |
|
705 | |||
706 | /** |
||
707 | * Gets attachment id from given image url. |
||
708 | * |
||
709 | * @param string $image_url url of an image. |
||
710 | * |
||
711 | * @since 2.0.0 |
||
712 | * @access public |
||
713 | * |
||
714 | * @return int|null attachment id of an image |
||
715 | */ |
||
716 | 5 | public function get_image_id( $image_url ) { |
|
732 | |||
733 | /** |
||
734 | * Get image title. |
||
735 | * |
||
736 | * @since 2.0.0 |
||
737 | * @access public |
||
738 | * |
||
739 | * @param string $image_url URL of an image. |
||
740 | * |
||
741 | * @return string |
||
742 | */ |
||
743 | 1 | public function get_image_title( $image_url ) { |
|
746 | |||
747 | /** |
||
748 | * Get image title by id. |
||
749 | * |
||
750 | * @since 2.0.0 |
||
751 | * @access public |
||
752 | * |
||
753 | * @param int $attachment_id Attachment id of an image. |
||
754 | * |
||
755 | * @return string |
||
756 | */ |
||
757 | 1 | public function get_image_title_by_id( $attachment_id ) { |
|
760 | |||
761 | /** |
||
762 | * Get image caption. |
||
763 | * |
||
764 | * @since 2.0.0 |
||
765 | * @access public |
||
766 | * |
||
767 | * @param string $image_url URL of an image. |
||
768 | * |
||
769 | * @return string |
||
770 | */ |
||
771 | 1 | public function get_image_caption( $image_url ) { |
|
774 | |||
775 | /** |
||
776 | * Get image caption by id. |
||
777 | * |
||
778 | * @since 2.0.0 |
||
779 | * @access public |
||
780 | * |
||
781 | * @param int $attachment_id Attachment id of an image. |
||
782 | * |
||
783 | * @return string |
||
784 | */ |
||
785 | 1 | public function get_image_caption_by_id( $attachment_id ) { |
|
788 | |||
789 | /** |
||
790 | * Get image alternate text. |
||
791 | * |
||
792 | * @since 2.0.0 |
||
793 | * @access public |
||
794 | * |
||
795 | * @see get_post_meta() |
||
796 | * |
||
797 | * @param string $image_url URL of an image. |
||
798 | * |
||
799 | * @return string |
||
800 | */ |
||
801 | 1 | public function get_image_alt( $image_url ) { |
|
811 | |||
812 | /** |
||
813 | * Get image alternate text by attachment id. |
||
814 | * |
||
815 | * @since 2.0.0 |
||
816 | * @access public |
||
817 | * |
||
818 | * @see get_post_meta() |
||
819 | * |
||
820 | * @param int $attachment_id Attachment id of an image. |
||
821 | * |
||
822 | * @return string |
||
823 | */ |
||
824 | 1 | public function get_image_alt_by_id( $attachment_id ) { |
|
829 | |||
830 | /** |
||
831 | * Get image description. |
||
832 | * |
||
833 | * @since 3.0.0 |
||
834 | * @access public |
||
835 | * |
||
836 | * @param string $image_url URL of an image. |
||
837 | * |
||
838 | * @return string |
||
839 | */ |
||
840 | 1 | public function get_image_description( $image_url ) { |
|
843 | |||
844 | /** |
||
845 | * Get image description by id. |
||
846 | * |
||
847 | * @since 3.0.0 |
||
848 | * @access public |
||
849 | * |
||
850 | * @param int $attachment_id attachment id of an image. |
||
851 | * |
||
852 | * @return string |
||
853 | */ |
||
854 | 1 | public function get_image_description_by_id( $attachment_id ) { |
|
857 | |||
858 | /** |
||
859 | * Get link to image. |
||
860 | * |
||
861 | * @since 3.4.0 |
||
862 | * @access public |
||
863 | * |
||
864 | * @param int $attachment_id Attachment id of an image. |
||
865 | * |
||
866 | * @return string|null |
||
867 | */ |
||
868 | 1 | public function get_link_to_image( $attachment_id ) { |
|
871 | |||
872 | /** |
||
873 | * Get all attachment ids of the post. |
||
874 | * |
||
875 | * @since 2.0.0 |
||
876 | * @access public |
||
877 | * |
||
878 | * @see get_post_meta() |
||
879 | * |
||
880 | * @param int $post_id id of the current post. |
||
881 | * |
||
882 | * @return array |
||
883 | */ |
||
884 | 2 | public function get_post_attachment_ids( $post_id ) { |
|
897 | |||
898 | /** |
||
899 | * Get real post id. |
||
900 | * |
||
901 | * @since 3.6.0 |
||
902 | * @access protected |
||
903 | * |
||
904 | * @param int|null $post_id Post id. |
||
905 | * |
||
906 | * @return int|null |
||
907 | */ |
||
908 | 6 | protected function get_real_post_id( $post_id = null ) { |
|
917 | |||
918 | /** |
||
919 | * Fetches featured image data of nth position. |
||
920 | * |
||
921 | * @since 3.0.0 |
||
922 | * @access public |
||
923 | * |
||
924 | * @see get_featured_images() |
||
925 | * |
||
926 | * @param int $position Position of the featured image. |
||
927 | * @param int $post_id Current post id. |
||
928 | * |
||
929 | * @return array if found, null otherwise. |
||
930 | */ |
||
931 | 2 | public function get_nth_featured_image( $position, $post_id = null ) { |
|
938 | |||
939 | /** |
||
940 | * Check if the image is attached with the particular post. |
||
941 | * |
||
942 | * @since 2.0.0 |
||
943 | * @access public |
||
944 | * |
||
945 | * @see get_post_attachment_ids() |
||
946 | * |
||
947 | * @param int $attachment_id Attachment id of an image. |
||
948 | * @param int $post_id Current post id. |
||
949 | * |
||
950 | * @return bool |
||
951 | */ |
||
952 | 1 | public function is_attached( $attachment_id, $post_id ) { |
|
961 | |||
962 | /** |
||
963 | * Retrieve featured images for specific post(s). |
||
964 | * |
||
965 | * @since 2.0.0 |
||
966 | * @access public |
||
967 | * |
||
968 | * @see get_post_meta() |
||
969 | * |
||
970 | * @param int $post_id id of the current post. |
||
971 | * |
||
972 | * @return array |
||
973 | */ |
||
974 | 7 | public function get_featured_images( $post_id = null ) { |
|
1001 | |||
1002 | /** |
||
1003 | * Check to see if the upload url is already available in path. |
||
1004 | * |
||
1005 | * @since 3.1.14 |
||
1006 | * @access protected |
||
1007 | * |
||
1008 | * @param string $img Uploaded image. |
||
1009 | * |
||
1010 | * @return string |
||
1011 | */ |
||
1012 | 2 | protected function get_real_upload_path( $img ) { |
|
1020 | |||
1021 | /** |
||
1022 | * Retrieve featured images for specific post(s) including the default Featured Image. |
||
1023 | * |
||
1024 | * @since 3.1.7 |
||
1025 | * @access public |
||
1026 | * |
||
1027 | * @see $this->get_featured_images() |
||
1028 | * |
||
1029 | * @param int $post_id Current post id. |
||
1030 | * |
||
1031 | * @return array An array of images or an empty array on failure |
||
1032 | */ |
||
1033 | 2 | public function get_all_featured_images( $post_id = null ) { |
|
1050 | |||
1051 | /** |
||
1052 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
1053 | * |
||
1054 | * @since 1.0.0 |
||
1055 | * @access public |
||
1056 | * |
||
1057 | * @see load_plugin_textdomain() |
||
1058 | * @see plugin_basename() |
||
1059 | * @action plugins_loaded |
||
1060 | * |
||
1061 | * @codeCoverageIgnore |
||
1062 | * |
||
1063 | * @return void |
||
1064 | */ |
||
1065 | public function load_plugin_textdomain() { |
||
1072 | } |
||
1073 | |||
1087 |