Complex classes like WP2D_Post 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 WP2D_Post, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class WP2D_Post { |
||
20 | |||
21 | /** |
||
22 | * The original post object. |
||
23 | * |
||
24 | * @var WP_Post |
||
25 | * @since 1.5.0 |
||
26 | */ |
||
27 | public $post; |
||
28 | |||
29 | /** |
||
30 | * The original post ID. |
||
31 | * |
||
32 | * @var int |
||
33 | * @since 1.5.0 |
||
34 | */ |
||
35 | public $ID; |
||
36 | |||
37 | /** |
||
38 | * If this post should be shared on diaspora*. |
||
39 | * |
||
40 | * @var bool |
||
41 | * @since 1.5.0 |
||
42 | */ |
||
43 | public $post_to_diaspora; |
||
44 | |||
45 | /** |
||
46 | * If a link back to the original post should be added. |
||
47 | * |
||
48 | * @var bool |
||
49 | * @since 1.5.0 |
||
50 | */ |
||
51 | public $fullentrylink; |
||
52 | |||
53 | /** |
||
54 | * What content gets posted. |
||
55 | * |
||
56 | * @var string |
||
57 | * @since 1.5.0 |
||
58 | */ |
||
59 | public $display; |
||
60 | |||
61 | /** |
||
62 | * The types of tags to post. (global,custom,post) |
||
63 | * |
||
64 | * @var array |
||
65 | * @since 1.5.0 |
||
66 | */ |
||
67 | public $tags_to_post; |
||
68 | |||
69 | /** |
||
70 | * The post's custom tags. |
||
71 | * |
||
72 | * @var array |
||
73 | * @since 1.5.0 |
||
74 | */ |
||
75 | public $custom_tags; |
||
76 | |||
77 | /** |
||
78 | * Aspects this post gets posted to. |
||
79 | * |
||
80 | * @var array |
||
81 | * @since 1.5.0 |
||
82 | */ |
||
83 | public $aspects; |
||
84 | |||
85 | /** |
||
86 | * Services this post gets posted to. |
||
87 | * |
||
88 | * @var array |
||
89 | * @since 1.5.0 |
||
90 | */ |
||
91 | public $services; |
||
92 | |||
93 | /** |
||
94 | * The post's history of diaspora* posts. |
||
95 | * |
||
96 | * @var array |
||
97 | * @since 1.5.0 |
||
98 | */ |
||
99 | public $post_history; |
||
100 | |||
101 | /** |
||
102 | * If the post actions have all been set up already. |
||
103 | * |
||
104 | * @var boolean |
||
105 | * @since 1.5.0 |
||
106 | */ |
||
107 | private static $_is_set_up = false; |
||
108 | |||
109 | /** |
||
110 | * Setup all the necessary WP callbacks. |
||
111 | * |
||
112 | * @since 1.5.0 |
||
113 | */ |
||
114 | public static function setup() { |
||
134 | |||
135 | /** |
||
136 | * Constructor. |
||
137 | * |
||
138 | * @since 1.5.0 |
||
139 | * |
||
140 | * @param int|WP_Post $post Post ID or the post itself. |
||
141 | */ |
||
142 | public function __construct( $post ) { |
||
145 | |||
146 | /** |
||
147 | * Assign the original WP_Post object and all the custom meta data. |
||
148 | * |
||
149 | * @since 1.5.0 |
||
150 | * |
||
151 | * @param int|WP_Post $post Post ID or the post itself. |
||
152 | */ |
||
153 | private function _assign_wp_post( $post ) { |
||
183 | |||
184 | /** |
||
185 | * Post to diaspora* when saving a post. |
||
186 | * |
||
187 | * @since 1.5.0 |
||
188 | * |
||
189 | * @todo Maybe somebody wants to share a password protected post to a closed aspect. |
||
190 | * |
||
191 | * @param integer $post_id ID of the post being saved. |
||
192 | * @param WP_Post $post Post object being saved. |
||
193 | * |
||
194 | * @return bool If the post was posted successfully. |
||
195 | */ |
||
196 | public function post( $post_id, $post ) { |
||
197 | $this->_assign_wp_post( $post ); |
||
198 | |||
199 | $options = WP2D_Options::instance(); |
||
200 | |||
201 | // Is this post type enabled for posting? |
||
202 | if ( ! in_array( $post->post_type, $options->get_option( 'enabled_post_types' ), true ) ) { |
||
203 | return false; |
||
204 | } |
||
205 | |||
206 | // Make sure we're posting to diaspora* and the post isn't password protected. |
||
207 | if ( ! ( $this->post_to_diaspora && 'publish' === $post->post_status && '' === $post->post_password ) ) { |
||
208 | return false; |
||
209 | } |
||
210 | |||
211 | $status_message = $this->_get_title_link(); |
||
212 | |||
213 | // Post the full post text, just the excerpt, or nothing at all? |
||
214 | if ( 'full' === $this->display ) { |
||
215 | $status_message .= $this->_get_full_content(); |
||
216 | } elseif ( 'excerpt' === $this->display ) { |
||
217 | $status_message .= $this->_get_excerpt_content(); |
||
218 | } |
||
219 | |||
220 | // Add the tags assigned to the post. |
||
221 | $status_message .= $this->_get_tags_to_add(); |
||
222 | |||
223 | // Add the original entry link to the post? |
||
224 | $status_message .= $this->_get_posted_at_link(); |
||
225 | |||
226 | $status_converter = new HtmlConverter( [ 'strip_tags' => true ] ); |
||
227 | $status_message = $status_converter->convert( $status_message ); |
||
228 | |||
229 | // Set up the connection to diaspora*. |
||
230 | $api = WP2D_Helpers::api_quick_connect(); |
||
231 | if ( empty( $status_message ) ) { |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | if ( $api->has_last_error() ) { |
||
236 | // Save the post error as post meta data, so we can display it to the user. |
||
237 | update_post_meta( $post_id, '_wp_to_diaspora_post_error', $api->get_last_error() ); |
||
238 | |||
239 | return false; |
||
240 | } |
||
241 | |||
242 | // Add services to share to via diaspora*. |
||
243 | $extra_data = [ |
||
244 | 'services' => $this->services, |
||
245 | ]; |
||
246 | |||
247 | // Try to post to diaspora*. |
||
248 | $response = $api->post( $status_message, $this->aspects, $extra_data ); |
||
249 | if ( ! $response ) { |
||
250 | return false; |
||
251 | } |
||
252 | |||
253 | // Save certain diaspora* post data as meta data for future reference. |
||
254 | $this->_save_to_history( (object) $response ); |
||
255 | |||
256 | // If there is still a previous post error around, remove it. |
||
257 | delete_post_meta( $post_id, '_wp_to_diaspora_post_error' ); |
||
258 | |||
259 | // Unset post_to_diaspora meta field to prevent mistakenly republishing to diaspora*. |
||
260 | $meta = get_post_meta( $post_id, '_wp_to_diaspora', true ); |
||
261 | $meta['post_to_diaspora'] = false; |
||
262 | update_post_meta( $post_id, '_wp_to_diaspora', $meta ); |
||
263 | |||
264 | return true; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Get the title of the post linking to the post itself. |
||
269 | * |
||
270 | * @since 1.5.0 |
||
271 | * |
||
272 | * @return string Post title as a link. |
||
273 | */ |
||
274 | private function _get_title_link() { |
||
289 | |||
290 | /** |
||
291 | * Get the full post content with only default filters applied. |
||
292 | * |
||
293 | * @since 1.5.0 |
||
294 | * |
||
295 | * @return string The full post content. |
||
296 | */ |
||
297 | private function _get_full_content() { |
||
343 | |||
344 | /** |
||
345 | * Get the post's excerpt in a nice format. |
||
346 | * |
||
347 | * @since 1.5.0 |
||
348 | * |
||
349 | * @return string Post's excerpt. |
||
350 | */ |
||
351 | private function _get_excerpt_content() { |
||
376 | |||
377 | /** |
||
378 | * Get a string of tags that have been added to the post. |
||
379 | * |
||
380 | * @since 1.5.0 |
||
381 | * |
||
382 | * @return string Tags added to the post. |
||
383 | */ |
||
384 | private function _get_tags_to_add() { |
||
439 | |||
440 | /** |
||
441 | * Get the link to the original post. |
||
442 | * |
||
443 | * @since 1.5.0 |
||
444 | * |
||
445 | * @return string Original post link. |
||
446 | */ |
||
447 | private function _get_posted_at_link() { |
||
468 | |||
469 | /** |
||
470 | * Save the details of the new diaspora* post to this post's history. |
||
471 | * |
||
472 | * @since 1.5.0 |
||
473 | * |
||
474 | * @param object $response Response from the API containing the diaspora* post details. |
||
475 | */ |
||
476 | private function _save_to_history( $response ) { |
||
494 | |||
495 | /** |
||
496 | * Return URL from [embed] shortcode instead of generated iframe. |
||
497 | * |
||
498 | * @since 1.5.0 |
||
499 | * @see WP_Embed::shortcode() |
||
500 | * |
||
501 | * @param mixed $html The cached HTML result, stored in post meta. |
||
502 | * @param string $url The attempted embed URL. |
||
503 | * |
||
504 | * @return string URL of the embed. |
||
505 | */ |
||
506 | public function embed_url( $html, $url ) { |
||
509 | |||
510 | /** |
||
511 | * Removes '[embed]' and '[/embed]' left by embed_url. |
||
512 | * |
||
513 | * @since 1.5.0 |
||
514 | * @todo It would be great to fix it using only one filter. |
||
515 | * It's happening because embed filter is being removed by remove_all_filters('the_content') on WP2D_Post::post(). |
||
516 | * |
||
517 | * @param string $content Content of the post. |
||
518 | * |
||
519 | * @return string The content with the embed tags removed. |
||
520 | */ |
||
521 | public function embed_remove( $content ) { |
||
524 | |||
525 | /** |
||
526 | * Prettify the image caption. |
||
527 | * |
||
528 | * @since 1.5.3 |
||
529 | * |
||
530 | * @param string $caption Caption to be prettified. |
||
531 | * |
||
532 | * @return string Prettified image caption. |
||
533 | */ |
||
534 | public function get_img_caption( $caption ) { |
||
550 | |||
551 | /** |
||
552 | * Filter the default caption shortcode output. |
||
553 | * |
||
554 | * @since 1.5.3 |
||
555 | * @see img_caption_shortcode() |
||
556 | * |
||
557 | * @param string $empty The caption output. Default empty. |
||
558 | * @param array $attr Attributes of the caption shortcode. |
||
559 | * @param string $content The image element, possibly wrapped in a hyperlink. |
||
560 | * |
||
561 | * @return string The caption shortcode output. |
||
562 | */ |
||
563 | public function custom_img_caption( $empty, $attr, $content ) { |
||
573 | |||
574 | /** |
||
575 | * Create a custom gallery caption output. |
||
576 | * |
||
577 | * @since 1.5.3 |
||
578 | * |
||
579 | * @param array $attr Gallery attributes. |
||
580 | * |
||
581 | * @return string |
||
582 | */ |
||
583 | public function custom_gallery_shortcode( $attr ) { |
||
604 | |||
605 | /** |
||
606 | * Change the result of the regex match from custom_gallery_shortcode. |
||
607 | * |
||
608 | * @param array $m Regex matches. |
||
609 | * |
||
610 | * @return string Prettified gallery image caption. |
||
611 | */ |
||
612 | public function custom_gallery_regex_callback( $m ) { |
||
615 | |||
616 | /* |
||
617 | * META BOX |
||
618 | */ |
||
619 | |||
620 | /** |
||
621 | * Adds a meta box to the main column on the enabled Post Types' edit screens. |
||
622 | * |
||
623 | * @since 1.5.0 |
||
624 | */ |
||
625 | public function add_meta_boxes() { |
||
638 | |||
639 | /** |
||
640 | * Prints the meta box content. |
||
641 | * |
||
642 | * @since 1.5.0 |
||
643 | * |
||
644 | * @param WP_Post $post The object for the current post. |
||
645 | */ |
||
646 | public function meta_box_render( $post ) { |
||
682 | |||
683 | /** |
||
684 | * When the post is saved, save our meta data. |
||
685 | * |
||
686 | * @since 1.5.0 |
||
687 | * |
||
688 | * @param integer $post_id The ID of the post being saved. |
||
689 | */ |
||
690 | public function save_meta_box_data( $post_id ) { |
||
726 | |||
727 | /** |
||
728 | * Perform all checks to see if we are allowed to save the meta data. |
||
729 | * |
||
730 | * @since 1.5.0 |
||
731 | * |
||
732 | * @return bool If the verification checks have passed. |
||
733 | */ |
||
734 | private function _is_safe_to_save() { |
||
758 | |||
759 | /** |
||
760 | * Add admin notices when a post gets displayed. |
||
761 | * |
||
762 | * @since 1.5.0 |
||
763 | * |
||
764 | * @todo Ignore post error with AJAX. |
||
765 | */ |
||
766 | public function admin_notices() { |
||
798 | |||
799 | /** |
||
800 | * Delete the error post meta data if it gets ignored. |
||
801 | * |
||
802 | * @since 1.5.0 |
||
803 | */ |
||
804 | public function ignore_post_error() { |
||
810 | } |
||
811 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state