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 |
||
45 | class Dynamic_Featured_Image { |
||
46 | /** |
||
47 | * Current version of the plugin. |
||
48 | * |
||
49 | * @since 3.0.0 |
||
50 | */ |
||
51 | const VERSION = '3.5.2'; |
||
52 | |||
53 | /* Image upload directory */ |
||
54 | private $__upload_dir; |
||
55 | |||
56 | /* Image upload URL */ |
||
57 | private $__upload_url; |
||
58 | |||
59 | /* Database object */ |
||
60 | private $__db; |
||
61 | |||
62 | /* Plugin text domain */ |
||
63 | protected $_textDomain; |
||
64 | |||
65 | /* Title for dfi metabox */ |
||
66 | protected $_metabox_title; |
||
67 | |||
68 | /* Users post type filter for dfi metabox */ |
||
69 | protected $_userFilter; |
||
70 | |||
71 | /** |
||
72 | * Constructor. Hooks all interactions to initialize the class. |
||
73 | * |
||
74 | * @since 1.0.0 |
||
75 | * @access public |
||
76 | * @global object $wpdb |
||
77 | * |
||
78 | * @see add_action() |
||
79 | */ |
||
80 | public function __construct() { |
||
117 | |||
118 | /** |
||
119 | * Return site protocol |
||
120 | * |
||
121 | * @since 3.5.1 |
||
122 | * @access public |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | private function __get_protocol() { |
||
130 | |||
131 | /** |
||
132 | * Add required admin scripts |
||
133 | * |
||
134 | * @since 1.0.0 |
||
135 | * @access public |
||
136 | * |
||
137 | * @see wp_enque_style() |
||
138 | * @see wp_register_script() |
||
139 | * @see wp_enqueue_script() |
||
140 | * |
||
141 | * @return Void |
||
142 | */ |
||
143 | public function enqueue_admin_scripts() { |
||
167 | |||
168 | /** |
||
169 | * Add upgrade link |
||
170 | * |
||
171 | * @access public |
||
172 | * @since 3.5.1 |
||
173 | * @action plugin_action_links |
||
174 | * |
||
175 | * @codeCoverageIgnore |
||
176 | * |
||
177 | * @param array $links Action links |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | public function dfi_action_links( $links ) { |
||
189 | |||
190 | /** |
||
191 | * Add featured meta boxes dynamically |
||
192 | * |
||
193 | * @since 1.0.0 |
||
194 | * @access public |
||
195 | * @global object $post |
||
196 | * |
||
197 | * @see get_post_meta() |
||
198 | * @see get_post_types() |
||
199 | * @see add_meta_box() |
||
200 | * @see add_filter() |
||
201 | * |
||
202 | * @return Void |
||
203 | */ |
||
204 | public function initialize_featured_box() { |
||
236 | |||
237 | /** |
||
238 | * Translates more than one digit number digit by digit. |
||
239 | * |
||
240 | * @param Integer $number Integer to be translated |
||
241 | * |
||
242 | * @return String Translated number |
||
243 | */ |
||
244 | protected function _get_number_translation( $number ) { |
||
257 | |||
258 | /** |
||
259 | * adds meta boxes |
||
260 | * |
||
261 | * @param Array $postTypes post types to show featured image box |
||
262 | * @param Object $featured callback arguments |
||
263 | * @param Integer $i index of the featured image |
||
264 | * |
||
265 | * @return Void |
||
266 | */ |
||
267 | private function _dfi_add_meta_box( $postTypes, $featured = null, $i = null ) { |
||
298 | |||
299 | /** |
||
300 | * Separate thumb and full image url from given URL string |
||
301 | * |
||
302 | * @since 3.3.1 |
||
303 | * |
||
304 | * @param string $urlString [description] |
||
305 | * @param string $state Thumb or full |
||
306 | * |
||
307 | * @return string|null |
||
308 | */ |
||
309 | private function _separate( $urlString, $state = 'thumb' ) { |
||
318 | |||
319 | /** |
||
320 | * Create a nonce field |
||
321 | * |
||
322 | * @since 3.5.0 |
||
323 | * |
||
324 | * @see wp_nonce_field() |
||
325 | * @see plugin_basename() |
||
326 | * |
||
327 | * @codeCoverageIgnore |
||
328 | * |
||
329 | * @param string $key Nonce key |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | protected function _nonce_field( $key ) { |
||
336 | |||
337 | /** |
||
338 | * Featured meta box as seen in the admin |
||
339 | * |
||
340 | * @since 1.0.0 |
||
341 | * @access public |
||
342 | * |
||
343 | * @param Object $post global post object |
||
344 | * @param Array $featured array containing featured image count |
||
345 | * |
||
346 | * @return Void |
||
347 | */ |
||
348 | public function featured_meta_box( $post, $featured ) { |
||
381 | |||
382 | /** |
||
383 | * Returns featured box html content |
||
384 | * @since 3.1.0 |
||
385 | * @access private |
||
386 | * |
||
387 | * @param String $featuredImgTrimmed Medium sized image |
||
388 | * @param String $featuredImg Full sized image |
||
389 | * @param String $featuredId Attachment Id |
||
390 | * @param String $thumbnail Thumb sized image |
||
391 | * |
||
392 | * @return String Html content |
||
393 | */ |
||
394 | private function _get_featured_box( $featuredImgTrimmed, $featuredImg, $featuredId, $thumbnail, $postId ) { |
||
408 | |||
409 | /** |
||
410 | * Load new featured meta box via ajax |
||
411 | * |
||
412 | * @since 1.0.0 |
||
413 | * @access public |
||
414 | * |
||
415 | * @return Void |
||
416 | */ |
||
417 | public function ajax_callback() { |
||
443 | |||
444 | /** |
||
445 | * Add custom class 'featured-meta-box' to meta box |
||
446 | * |
||
447 | * @since 1.0.0 |
||
448 | * @access public |
||
449 | * |
||
450 | * @see add_metabox_classes |
||
451 | * |
||
452 | * @param $classes classes to add in the meta box |
||
453 | * |
||
454 | * @return string |
||
455 | */ |
||
456 | public function add_metabox_classes( $classes ) { |
||
462 | |||
463 | /** |
||
464 | * Add custom fields in media uploader |
||
465 | * |
||
466 | * @since 3.4.0 |
||
467 | * |
||
468 | * @param $form_fields Array Fields to include in media attachment form |
||
469 | * @param $post Array Post data |
||
470 | * |
||
471 | * @return Array |
||
472 | */ |
||
473 | public function media_attachment_custom_fields( $form_fields, $post ) { |
||
483 | |||
484 | /** |
||
485 | * Save values of media uploader custom fields |
||
486 | * |
||
487 | * @since 3.4.0 |
||
488 | * |
||
489 | * @param $post Array The post data for database |
||
490 | * @param $attachment Array Attachment fields from $_POST form |
||
491 | * |
||
492 | * @return Array |
||
493 | */ |
||
494 | public function media_attachment_custom_fields_save( $post, $attachment ) { |
||
502 | |||
503 | /** |
||
504 | * Update featured images in the database |
||
505 | * |
||
506 | * @since 1.0.0 |
||
507 | * @access public |
||
508 | * |
||
509 | * @see plugin_basename() |
||
510 | * @see update_post_meta() |
||
511 | * @see current_user_can() |
||
512 | * |
||
513 | * @param Integer $post_id current post id |
||
514 | * |
||
515 | * @return Void |
||
516 | */ |
||
517 | public function save_meta( $post_id ) { |
||
533 | |||
534 | /** |
||
535 | * Verify metabox nonces |
||
536 | * |
||
537 | * @access protected |
||
538 | * @see wp_verify_nonce() |
||
539 | * |
||
540 | * @return boolean |
||
541 | */ |
||
542 | protected function _verify_nonces() { |
||
556 | |||
557 | /** |
||
558 | * Add update notice. Displayed in plugin update page. |
||
559 | * |
||
560 | * @since 2.0.0 |
||
561 | * @access public |
||
562 | * |
||
563 | * @return Void |
||
564 | */ |
||
565 | public function update_notice() { |
||
570 | |||
571 | /** Helper functions */ |
||
572 | |||
573 | private function execute_query( $query ) { |
||
576 | |||
577 | /** |
||
578 | * Get attachment id of the image by image url |
||
579 | * |
||
580 | * @since 3.1.7 |
||
581 | * @access protected |
||
582 | * @global object $wpdb |
||
583 | * |
||
584 | * @param String $image_url url of the image |
||
585 | * |
||
586 | * @return string |
||
587 | */ |
||
588 | protected function _get_attachment_id( $image_url ) { |
||
592 | |||
593 | /** |
||
594 | * Get image url of the image by attachment id |
||
595 | * |
||
596 | * @since 2.0.0 |
||
597 | * @access public |
||
598 | * |
||
599 | * @see wp_get_attachment_image_src() |
||
600 | * |
||
601 | * @param Integer $attachment_id attachment id of an image |
||
602 | * @param String $size size of the image to fetch (thumbnail, medium, full) |
||
603 | * |
||
604 | * @return String |
||
605 | */ |
||
606 | public function get_image_url( $attachment_id, $size = 'full' ) { |
||
612 | |||
613 | /** |
||
614 | * Get image thumbnail url of specific size by image url |
||
615 | * |
||
616 | * @since 2.0.0 |
||
617 | * @access public |
||
618 | * |
||
619 | * @see get_image_id() |
||
620 | * @see wp_get_attachment_image_src() |
||
621 | * |
||
622 | * @param String $image_url url of an image |
||
623 | * @param String $size size of the image to fetch (thumbnail, medium, full) |
||
624 | * |
||
625 | * @return String |
||
626 | */ |
||
627 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { |
||
634 | |||
635 | /** |
||
636 | * Gets attachment id from given image url |
||
637 | * |
||
638 | * @param String $image_url url of an image |
||
639 | * |
||
640 | * @return Integer|Null attachment id of an image |
||
641 | * |
||
642 | * @since 2.0.0 |
||
643 | * @access public |
||
644 | */ |
||
645 | public function get_image_id( $image_url ) { |
||
659 | |||
660 | /** |
||
661 | * Get image title |
||
662 | * |
||
663 | * @since 2.0.0 |
||
664 | * @access public |
||
665 | * |
||
666 | * @param String $image_url url of an image |
||
667 | * |
||
668 | * @return String |
||
669 | */ |
||
670 | public function get_image_title( $image_url ) { |
||
674 | |||
675 | /** |
||
676 | * Get image title by id |
||
677 | * |
||
678 | * @since 2.0.0 |
||
679 | * @access public |
||
680 | * |
||
681 | * @param Integer $attachment_id attachment id of an image |
||
682 | * |
||
683 | * @return String |
||
684 | */ |
||
685 | public function get_image_title_by_id( $attachment_id ) { |
||
689 | |||
690 | /** |
||
691 | * Get image caption |
||
692 | * |
||
693 | * @since 2.0.0 |
||
694 | * @access public |
||
695 | * |
||
696 | * @param String $image_url url of an image |
||
697 | * |
||
698 | * @return String |
||
699 | */ |
||
700 | public function get_image_caption( $image_url ) { |
||
704 | |||
705 | /** |
||
706 | * Get image caption by id |
||
707 | * |
||
708 | * @since 2.0.0 |
||
709 | * @access public |
||
710 | * |
||
711 | * @param Integer $attachment_id attachment id of an image |
||
712 | * |
||
713 | * @return String |
||
714 | */ |
||
715 | public function get_image_caption_by_id( $attachment_id ) { |
||
719 | |||
720 | /** |
||
721 | * Get image alternate text |
||
722 | * |
||
723 | * @since 2.0.0 |
||
724 | * @access public |
||
725 | * |
||
726 | * @see get_post_meta() |
||
727 | * |
||
728 | * @param String $image_url url of an image |
||
729 | * |
||
730 | * @return String |
||
731 | */ |
||
732 | public function get_image_alt( $image_url ) { |
||
743 | |||
744 | /** |
||
745 | * Get image alternate text by attachment id |
||
746 | * |
||
747 | * @since 2.0.0 |
||
748 | * @access public |
||
749 | * |
||
750 | * @see get_post_meta() |
||
751 | * |
||
752 | * @param Integer $attachment_id attachment id of an image |
||
753 | * |
||
754 | * @return String |
||
755 | */ |
||
756 | public function get_image_alt_by_id( $attachment_id ) { |
||
762 | |||
763 | /** |
||
764 | * Get image description |
||
765 | * |
||
766 | * @since 3.0.0 |
||
767 | * @access public |
||
768 | * |
||
769 | * @param String $image_url url of an image |
||
770 | * |
||
771 | * @return String |
||
772 | */ |
||
773 | public function get_image_description( $image_url ) { |
||
777 | |||
778 | /** |
||
779 | * Get image description by id |
||
780 | * |
||
781 | * @since 3.0.0 |
||
782 | * @access public |
||
783 | * |
||
784 | * @param Integer $attachment_id attachment id of an image |
||
785 | * |
||
786 | * @return String |
||
787 | */ |
||
788 | public function get_image_description_by_id( $attachment_id ) { |
||
792 | |||
793 | /** |
||
794 | * Get link to image |
||
795 | * |
||
796 | * @since 3.4.0 |
||
797 | * @access public |
||
798 | * |
||
799 | * @param Integer $attachment_id attachment id of an image |
||
800 | * |
||
801 | * @return string|null |
||
802 | */ |
||
803 | public function get_link_to_image( $attachment_id ) { |
||
807 | |||
808 | /** |
||
809 | * Get all attachment ids of the post |
||
810 | * |
||
811 | * @since 2.0.0 |
||
812 | * @access public |
||
813 | * |
||
814 | * @see get_post_meta() |
||
815 | * |
||
816 | * @param Integer $post_id id of the current post |
||
817 | * |
||
818 | * @return Array |
||
819 | */ |
||
820 | public function get_post_attachment_ids( $post_id ) { |
||
834 | |||
835 | /** |
||
836 | * Fetches featured image data of nth position |
||
837 | * |
||
838 | * @since 3.0.0 |
||
839 | * @access public |
||
840 | * |
||
841 | * @see get_featured_images() |
||
842 | * |
||
843 | * @param Integer $position position of the featured image |
||
844 | * @param Integer $post_id id of the current post |
||
845 | * |
||
846 | * @return Array if found, null otherwise |
||
847 | */ |
||
848 | public function get_nth_featured_image( $position, $post_id = null ) { |
||
859 | |||
860 | /** |
||
861 | * Check if the image is attached with the particular post |
||
862 | * |
||
863 | * @since 2.0.0 |
||
864 | * @access public |
||
865 | * |
||
866 | * @see get_post_attachment_ids() |
||
867 | * |
||
868 | * @param $attachment_id attachment id of an image |
||
869 | * @param $post_id id of the current post |
||
870 | * |
||
871 | * @return boolean |
||
872 | */ |
||
873 | public function is_attached( $attachment_id, $post_id ) { |
||
883 | |||
884 | /** |
||
885 | * Retrieve featured images for specific post(s) |
||
886 | * |
||
887 | * @since 2.0.0 |
||
888 | * @access public |
||
889 | * |
||
890 | * @see get_post_meta() |
||
891 | * |
||
892 | * @param Integer $post_id id of the current post |
||
893 | * |
||
894 | * @return Array |
||
895 | */ |
||
896 | public function get_featured_images( $post_id = null ) { |
||
929 | |||
930 | /** |
||
931 | * Check to see if the upload url is already available in path. |
||
932 | * |
||
933 | * @since 3.1.14 |
||
934 | * @access protected |
||
935 | * |
||
936 | * @param string $img |
||
937 | * |
||
938 | * @return string |
||
939 | */ |
||
940 | protected function _get_real_upload_path( $img ) { |
||
948 | |||
949 | /** |
||
950 | * Retrieve featured images for specific post(s) including the default Featured Image |
||
951 | * |
||
952 | * @since 3.1.7 |
||
953 | * @access public |
||
954 | * |
||
955 | * @see $this->get_featured_images() |
||
956 | * |
||
957 | * @param Integer $post_id id of the current post |
||
958 | * |
||
959 | * @return Array An array of images or an empty array on failure |
||
960 | */ |
||
961 | public function get_all_featured_images( $post_id = null ) { |
||
986 | |||
987 | /** |
||
988 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
989 | * |
||
990 | * @since 1.0.0 |
||
991 | * @access public |
||
992 | * |
||
993 | * @see load_plugin_textdomain() |
||
994 | * @see plugin_basename() |
||
995 | * @action plugins_loaded |
||
996 | * |
||
997 | * @codeCoverageIgnore |
||
998 | * |
||
999 | * @return void |
||
1000 | */ |
||
1001 | public function load_plugin_textdomain() { |
||
1009 | |||
1010 | } // END class Dynamic_Featured_Image |
||
1011 | |||
1023 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.