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.5.2'; |
||
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 | public function __construct() { |
||
120 | // plugin update warning. |
||
121 | add_action( 'in_plugin_update_message-' . plugin_basename( __FILE__ ), array( $this, 'update_notice' ) ); |
||
122 | |||
123 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
||
124 | add_action( 'add_meta_boxes', array( $this, 'initialize_featured_box' ) ); |
||
125 | add_action( 'save_post', array( $this, 'save_meta' ) ); |
||
126 | add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) ); |
||
127 | |||
128 | // handle ajax request. |
||
129 | add_action( 'wp_ajax_dfiMetaBox_callback', array( $this, 'ajax_callback' ) ); |
||
130 | |||
131 | // add action links. |
||
132 | add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'dfi_action_links' ) ); |
||
133 | |||
134 | // media uploader custom fields. |
||
135 | add_filter( 'attachment_fields_to_edit', array( $this, 'media_attachment_custom_fields' ), 10, 2 ); |
||
136 | add_filter( 'attachment_fields_to_save', array( $this, 'media_attachment_custom_fields_save' ), 10, 2 ); |
||
137 | |||
138 | // get the site protocol. |
||
139 | $protocol = $this->get_protocol(); |
||
140 | |||
141 | $this->upload_dir = wp_upload_dir(); |
||
142 | $this->upload_url = preg_replace( '#^https?://#', '', $this->upload_dir['baseurl'] ); |
||
143 | |||
144 | // add protocol to the upload url. |
||
145 | $this->upload_url = $protocol . $this->upload_url; |
||
146 | |||
147 | // post type filter added by user. |
||
148 | $this->user_filter = array(); |
||
149 | |||
150 | global $wpdb; |
||
151 | $this->db = $wpdb; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Return site protocol. |
||
156 | * |
||
157 | * @since 3.5.1 |
||
158 | * @access public |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | private function get_protocol() { |
||
163 | return is_ssl() ? 'https://' : 'http://'; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Add required admin scripts. |
||
168 | * |
||
169 | * @since 1.0.0 |
||
170 | * @access public |
||
171 | * |
||
172 | * @see wp_enque_style() |
||
173 | * @see wp_register_script() |
||
174 | * @see wp_enqueue_script() |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | public function enqueue_admin_scripts() { |
||
179 | // enqueue styles. |
||
180 | wp_enqueue_style( 'style-dfi', plugins_url( '/css/style-dfi.css', __FILE__ ), array(), self::VERSION ); |
||
181 | |||
182 | // register script. |
||
183 | wp_register_script( 'scripts-dfi', plugins_url( '/js/script-dfi.js', __FILE__ ), array( 'jquery' ), self::VERSION ); |
||
184 | |||
185 | // localize the script with required data. |
||
186 | wp_localize_script( |
||
187 | 'scripts-dfi', |
||
188 | 'DFI_SPECIFIC', |
||
189 | array( |
||
190 | 'upload_url' => $this->upload_url, |
||
191 | 'metabox_title' => __( $this->metabox_title, self::TEXT_DOMAIN ), |
||
192 | 'mediaSelector_title' => __( 'Dynamic Featured Image - Media Selector', self::TEXT_DOMAIN ), |
||
193 | 'mediaSelector_buttonText' => __( 'Set Featured Image', self::TEXT_DOMAIN ), |
||
194 | 'ajax_nonce' => wp_create_nonce( plugin_basename( __FILE__ ) ), |
||
195 | ) |
||
196 | ); |
||
197 | |||
198 | // enqueue scripts. |
||
199 | wp_enqueue_script( 'scripts-dfi' ); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Add upgrade link. |
||
204 | * |
||
205 | * @access public |
||
206 | * @since 3.5.1 |
||
207 | * @action plugin_action_links |
||
208 | * |
||
209 | * @codeCoverageIgnore |
||
210 | * |
||
211 | * @param array $links Action links. |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | public function dfi_action_links( $links ) { |
||
216 | $upgrade_link = array( |
||
217 | '<a href="' . self::UPGRADE_LINK . '" target="_blank">Upgrade to Premium</a>' |
||
218 | ); |
||
219 | |||
220 | return array_merge( $links, $upgrade_link ); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Add featured meta boxes dynamically. |
||
225 | * |
||
226 | * @since 1.0.0 |
||
227 | * @access public |
||
228 | * @global object $post |
||
229 | * |
||
230 | * @see get_post_meta() |
||
231 | * @see get_post_types() |
||
232 | * @see add_meta_box() |
||
233 | * @see add_filter() |
||
234 | * |
||
235 | * @return void |
||
236 | */ |
||
237 | public function initialize_featured_box() { |
||
238 | global $post; |
||
239 | |||
240 | // make metabox title dynamic. |
||
241 | $this->metabox_title = apply_filters( 'dfi_set_metabox_title', __( 'Featured Image', self::TEXT_DOMAIN ) ); |
||
242 | |||
243 | $featured_data = get_post_meta( $post->ID, 'dfiFeatured', true ); |
||
244 | $total_featured = count( $featured_data ); |
||
245 | |||
246 | $default_filter = array( 'attachment', 'revision', 'nav_menu_item' ); |
||
247 | $this->user_filter = apply_filters( 'dfi_post_type_user_filter', $this->user_filter ); |
||
248 | |||
249 | $post_types = array_diff( get_post_types(), array_merge( $default_filter, $this->user_filter ) ); |
||
250 | $post_types = apply_filters( 'dfi_post_types', $post_types ); |
||
251 | |||
252 | if ( ! empty( $featured_data ) && $total_featured >= 1 ) { |
||
253 | $i = 2; |
||
254 | foreach ( $featured_data as $featured ) { |
||
255 | $this->dfi_add_meta_box( $post_types, $featured, $i++ ); |
||
256 | } |
||
257 | } else { |
||
258 | $this->dfi_add_meta_box( $post_types ); |
||
259 | } |
||
260 | } |
||
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 ) { |
||
396 | |||
397 | /** |
||
398 | * Returns featured box html content. |
||
399 | * |
||
400 | * @since 3.1.0 |
||
401 | * @access private |
||
402 | * |
||
403 | * @param string $featured_img_trimmed Medium sized image. |
||
404 | * @param string $featured_img Full sized image. |
||
405 | * @param string $featured_id Attachment Id. |
||
406 | * @param string $thumbnail Thumb sized image. |
||
407 | * @param int $post_id Post id. |
||
408 | * |
||
409 | * @return string Html content |
||
410 | */ |
||
411 | private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id ) { |
||
425 | |||
426 | /** |
||
427 | * Load new featured meta box via ajax. |
||
428 | * |
||
429 | * @since 1.0.0 |
||
430 | * @access public |
||
431 | * |
||
432 | * @return void |
||
433 | */ |
||
434 | public function ajax_callback() { |
||
463 | |||
464 | /** |
||
465 | * Add custom class 'featured-meta-box' to meta box. |
||
466 | * |
||
467 | * @since 1.0.0 |
||
468 | * @access public |
||
469 | * |
||
470 | * @see add_metabox_classes |
||
471 | * |
||
472 | * @param array $classes Classes to add in the meta box. |
||
473 | * |
||
474 | * @return array |
||
475 | */ |
||
476 | public function add_metabox_classes( $classes ) { |
||
481 | |||
482 | /** |
||
483 | * Add custom fields in media uploader. |
||
484 | * |
||
485 | * @since 3.4.0 |
||
486 | * |
||
487 | * @param array $form_fields Fields to include in media attachment form. |
||
488 | * @param array $post Post data. |
||
489 | * |
||
490 | * @return array |
||
491 | */ |
||
492 | public function media_attachment_custom_fields( $form_fields, $post ) { |
||
501 | |||
502 | /** |
||
503 | * Save values of media uploader custom fields. |
||
504 | * |
||
505 | * @since 3.4.0 |
||
506 | * |
||
507 | * @param array $post Post data for database. |
||
508 | * @param array $attachment Attachment fields from $_POST form. |
||
509 | * |
||
510 | * @return array |
||
511 | */ |
||
512 | public function media_attachment_custom_fields_save( $post, $attachment ) { |
||
519 | |||
520 | /** |
||
521 | * Update featured images in the database. |
||
522 | * |
||
523 | * @since 1.0.0 |
||
524 | * @access public |
||
525 | * |
||
526 | * @see plugin_basename() |
||
527 | * @see update_post_meta() |
||
528 | * @see current_user_can() |
||
529 | * |
||
530 | * @param int $post_id Current post id. |
||
531 | * |
||
532 | * @return bool|null |
||
533 | */ |
||
534 | public function save_meta( $post_id ) { |
||
551 | |||
552 | /** |
||
553 | * Sanitize array. |
||
554 | * |
||
555 | * @since 3.6.0 |
||
556 | * @access protected |
||
557 | * |
||
558 | * @param array $input_array Input array. |
||
559 | * |
||
560 | * @return array |
||
561 | */ |
||
562 | protected function sanitize_array( $input_array ) { |
||
571 | |||
572 | /** |
||
573 | * Verify metabox nonces. |
||
574 | * |
||
575 | * @access protected |
||
576 | * @see wp_verify_nonce() |
||
577 | * |
||
578 | * @return bool |
||
579 | */ |
||
580 | protected function verify_nonces() { |
||
598 | |||
599 | /** |
||
600 | * Add update notice. Displayed in plugin update page. |
||
601 | * |
||
602 | * @since 2.0.0 |
||
603 | * @access public |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | public function update_notice() { |
||
613 | |||
614 | /** |
||
615 | * Execute query. |
||
616 | * |
||
617 | * @param string $query Query to execute. |
||
618 | * |
||
619 | * @return null|string |
||
620 | */ |
||
621 | private function execute_query( $query ) { |
||
624 | |||
625 | /** |
||
626 | * Get attachment id of the image by image url. |
||
627 | * |
||
628 | * @since 3.1.7 |
||
629 | * @access protected |
||
630 | * @global object $wpdb |
||
631 | * |
||
632 | * @param string $image_url URL of an image. |
||
633 | * |
||
634 | * @return string |
||
635 | */ |
||
636 | protected function get_attachment_id( $image_url ) { |
||
639 | |||
640 | /** |
||
641 | * Get image url of the image by attachment id. |
||
642 | * |
||
643 | * @since 2.0.0 |
||
644 | * @access public |
||
645 | * |
||
646 | * @see wp_get_attachment_image_src() |
||
647 | * |
||
648 | * @param int $attachment_id attachment id of an image. |
||
649 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
650 | * |
||
651 | * @return string |
||
652 | */ |
||
653 | public function get_image_url( $attachment_id, $size = 'full' ) { |
||
658 | |||
659 | /** |
||
660 | * Get image thumbnail url of specific size by image url. |
||
661 | * |
||
662 | * @since 2.0.0 |
||
663 | * @access public |
||
664 | * |
||
665 | * @see get_image_id() |
||
666 | * @see wp_get_attachment_image_src() |
||
667 | * |
||
668 | * @param string $image_url url of an image. |
||
669 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
670 | * |
||
671 | * @return string |
||
672 | */ |
||
673 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { |
||
679 | |||
680 | /** |
||
681 | * Gets attachment id from given image url. |
||
682 | * |
||
683 | * @param string $image_url url of an image. |
||
684 | * |
||
685 | * @since 2.0.0 |
||
686 | * @access public |
||
687 | * |
||
688 | * @return int|null attachment id of an image |
||
689 | */ |
||
690 | public function get_image_id( $image_url ) { |
||
705 | |||
706 | /** |
||
707 | * Get image title. |
||
708 | * |
||
709 | * @since 2.0.0 |
||
710 | * @access public |
||
711 | * |
||
712 | * @param string $image_url URL of an image. |
||
713 | * |
||
714 | * @return string |
||
715 | */ |
||
716 | public function get_image_title( $image_url ) { |
||
719 | |||
720 | /** |
||
721 | * Get image title by id. |
||
722 | * |
||
723 | * @since 2.0.0 |
||
724 | * @access public |
||
725 | * |
||
726 | * @param int $attachment_id Attachment id of an image. |
||
727 | * |
||
728 | * @return string |
||
729 | */ |
||
730 | public function get_image_title_by_id( $attachment_id ) { |
||
733 | |||
734 | /** |
||
735 | * Get image caption. |
||
736 | * |
||
737 | * @since 2.0.0 |
||
738 | * @access public |
||
739 | * |
||
740 | * @param string $image_url URL of an image. |
||
741 | * |
||
742 | * @return string |
||
743 | */ |
||
744 | public function get_image_caption( $image_url ) { |
||
747 | |||
748 | /** |
||
749 | * Get image caption by id. |
||
750 | * |
||
751 | * @since 2.0.0 |
||
752 | * @access public |
||
753 | * |
||
754 | * @param int $attachment_id Attachment id of an image. |
||
755 | * |
||
756 | * @return string |
||
757 | */ |
||
758 | public function get_image_caption_by_id( $attachment_id ) { |
||
761 | |||
762 | /** |
||
763 | * Get image alternate text. |
||
764 | * |
||
765 | * @since 2.0.0 |
||
766 | * @access public |
||
767 | * |
||
768 | * @see get_post_meta() |
||
769 | * |
||
770 | * @param string $image_url URL of an image. |
||
771 | * |
||
772 | * @return string |
||
773 | */ |
||
774 | public function get_image_alt( $image_url ) { |
||
784 | |||
785 | /** |
||
786 | * Get image alternate text by attachment id. |
||
787 | * |
||
788 | * @since 2.0.0 |
||
789 | * @access public |
||
790 | * |
||
791 | * @see get_post_meta() |
||
792 | * |
||
793 | * @param int $attachment_id Attachment id of an image. |
||
794 | * |
||
795 | * @return string |
||
796 | */ |
||
797 | public function get_image_alt_by_id( $attachment_id ) { |
||
802 | |||
803 | /** |
||
804 | * Get image description. |
||
805 | * |
||
806 | * @since 3.0.0 |
||
807 | * @access public |
||
808 | * |
||
809 | * @param string $image_url URL of an image. |
||
810 | * |
||
811 | * @return string |
||
812 | */ |
||
813 | public function get_image_description( $image_url ) { |
||
816 | |||
817 | /** |
||
818 | * Get image description by id. |
||
819 | * |
||
820 | * @since 3.0.0 |
||
821 | * @access public |
||
822 | * |
||
823 | * @param int $attachment_id attachment id of an image. |
||
824 | * |
||
825 | * @return string |
||
826 | */ |
||
827 | public function get_image_description_by_id( $attachment_id ) { |
||
830 | |||
831 | /** |
||
832 | * Get link to image. |
||
833 | * |
||
834 | * @since 3.4.0 |
||
835 | * @access public |
||
836 | * |
||
837 | * @param int $attachment_id Attachment id of an image. |
||
838 | * |
||
839 | * @return string|null |
||
840 | */ |
||
841 | public function get_link_to_image( $attachment_id ) { |
||
844 | |||
845 | /** |
||
846 | * Get all attachment ids of the post. |
||
847 | * |
||
848 | * @since 2.0.0 |
||
849 | * @access public |
||
850 | * |
||
851 | * @see get_post_meta() |
||
852 | * |
||
853 | * @param int $post_id id of the current post. |
||
854 | * |
||
855 | * @return array |
||
856 | */ |
||
857 | public function get_post_attachment_ids( $post_id ) { |
||
870 | |||
871 | /** |
||
872 | * Fetches featured image data of nth position. |
||
873 | * |
||
874 | * @since 3.0.0 |
||
875 | * @access public |
||
876 | * |
||
877 | * @see get_featured_images() |
||
878 | * |
||
879 | * @param int $position Position of the featured image. |
||
880 | * @param int $post_id Current post id. |
||
881 | * |
||
882 | * @return array if found, null otherwise. |
||
883 | */ |
||
884 | public function get_nth_featured_image( $position, $post_id = null ) { |
||
894 | |||
895 | /** |
||
896 | * Check if the image is attached with the particular post. |
||
897 | * |
||
898 | * @since 2.0.0 |
||
899 | * @access public |
||
900 | * |
||
901 | * @see get_post_attachment_ids() |
||
902 | * |
||
903 | * @param int $attachment_id Attachment id of an image. |
||
904 | * @param int $post_id Current post id. |
||
905 | * |
||
906 | * @return bool |
||
907 | */ |
||
908 | public function is_attached( $attachment_id, $post_id ) { |
||
917 | |||
918 | /** |
||
919 | * Retrieve featured images for specific post(s). |
||
920 | * |
||
921 | * @since 2.0.0 |
||
922 | * @access public |
||
923 | * |
||
924 | * @see get_post_meta() |
||
925 | * |
||
926 | * @param integer $post_id id of the current post. |
||
927 | * |
||
928 | * @return array |
||
929 | */ |
||
930 | public function get_featured_images( $post_id = null ) { |
||
962 | |||
963 | /** |
||
964 | * Check to see if the upload url is already available in path. |
||
965 | * |
||
966 | * @since 3.1.14 |
||
967 | * @access protected |
||
968 | * |
||
969 | * @param string $img Uploaded image. |
||
970 | * |
||
971 | * @return string |
||
972 | */ |
||
973 | protected function get_real_upload_path( $img ) { |
||
981 | |||
982 | /** |
||
983 | * Retrieve featured images for specific post(s) including the default Featured Image. |
||
984 | * |
||
985 | * @since 3.1.7 |
||
986 | * @access public |
||
987 | * |
||
988 | * @see $this->get_featured_images() |
||
989 | * |
||
990 | * @param int $post_id Current post id. |
||
991 | * |
||
992 | * @return array An array of images or an empty array on failure |
||
993 | */ |
||
994 | public function get_all_featured_images( $post_id = null ) { |
||
1016 | |||
1017 | /** |
||
1018 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
1019 | * |
||
1020 | * @since 1.0.0 |
||
1021 | * @access public |
||
1022 | * |
||
1023 | * @see load_plugin_textdomain() |
||
1024 | * @see plugin_basename() |
||
1025 | * @action plugins_loaded |
||
1026 | * |
||
1027 | * @codeCoverageIgnore |
||
1028 | * |
||
1029 | * @return void |
||
1030 | */ |
||
1031 | public function load_plugin_textdomain() { |
||
1038 | } |
||
1039 | |||
1050 |
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: