Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Comic 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 Jetpack_Comic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Jetpack_Comic { |
||
4 | const POST_TYPE = 'jetpack-comic'; |
||
5 | |||
6 | static function init() { |
||
7 | static $instance = false; |
||
8 | |||
9 | if ( ! $instance ) |
||
10 | $instance = new Jetpack_Comic; |
||
11 | |||
12 | return $instance; |
||
13 | } |
||
14 | |||
15 | /** |
||
16 | * Conditionally hook into WordPress. |
||
17 | * |
||
18 | * Themes must declare that they support this module by adding |
||
19 | * add_theme_support( 'jetpack-comic' ); during after_setup_theme. |
||
20 | * |
||
21 | * If no theme support is found there is no need to hook into |
||
22 | * WordPress. We'll just return early instead. |
||
23 | */ |
||
24 | function __construct() { |
||
25 | // Make sure the post types are loaded for imports |
||
26 | add_action( 'import_start', array( $this, 'register_post_types' ) ); |
||
27 | |||
28 | // Add to REST API post type whitelist |
||
29 | add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_type' ) ); |
||
30 | |||
31 | // If called via REST API, we need to register later in lifecycle |
||
32 | add_action( 'restapi_theme_init', array( $this, 'maybe_register_post_types' ) ); |
||
33 | |||
34 | // Return early if theme does not support Jetpack Comic. |
||
35 | if ( ! ( $this->site_supports_comics() ) ) |
||
36 | return; |
||
37 | |||
38 | $this->register_post_types(); |
||
39 | |||
40 | add_action( 'pre_get_posts', array( $this, 'add_posts_to_loop' ) ); |
||
41 | |||
42 | // In order for the Feedbag job to find Comic posts, we need to circumvent any pretty |
||
43 | // URLs in the RSS feed given to Feedbag in favor of /?p=123&post_type=jetpack-comic |
||
44 | add_filter( 'the_permalink_rss', array( $this, 'custom_permalink_for_feedbag' ) ); |
||
45 | |||
46 | // There are some cases (like when Feedbag is fetching posts) that the comics |
||
47 | // post type needs to be registered no matter what, but none of the UI needs to be |
||
48 | // available. |
||
49 | |||
50 | // Enable Omnisearch for Comic posts. |
||
51 | if ( class_exists( 'Jetpack_Omnisearch_Posts' ) ) |
||
52 | new Jetpack_Omnisearch_Posts( self::POST_TYPE ); |
||
53 | |||
54 | add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) ); |
||
55 | |||
56 | if ( function_exists( 'queue_publish_post' ) ) { |
||
57 | add_action( 'publish_jetpack-comic', 'queue_publish_post', 10, 2 ); |
||
58 | } |
||
59 | |||
60 | add_action( 'pre_get_posts', array( $this, 'include_in_feeds' ) ); |
||
61 | |||
62 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
||
63 | |||
64 | add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', array( $this, 'manage_posts_columns' ) ); |
||
65 | add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column', array( $this, 'manage_posts_custom_column' ), 10, 2 ); |
||
66 | add_image_size( 'jetpack-comic-thumb', 150, 0, false ); |
||
67 | |||
68 | // Enable front-end uploading for users special enough. |
||
69 | if ( current_user_can( 'upload_files' ) && current_user_can( 'edit_posts' ) ) { |
||
70 | add_action( 'wp_ajax_jetpack_comic_upload', array( $this, 'upload' ) ); |
||
71 | add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Add a "Convert to Comic" and "Convert to Post" option to the bulk |
||
76 | * edit dropdowns. |
||
77 | */ |
||
78 | add_action( 'admin_footer-edit.php', array( $this, 'admin_footer' ) ); |
||
79 | add_action( 'load-edit.php', array( $this, 'bulk_edit' ) ); |
||
80 | add_action( 'admin_notices', array( $this, 'bulk_edit_notices' ) ); |
||
81 | |||
82 | } |
||
83 | |||
84 | public function admin_footer() { |
||
85 | $post_type = get_post_type(); |
||
86 | |||
87 | ?> |
||
88 | <script type="text/javascript"> |
||
89 | jQuery( document ).ready( function( $ ) { |
||
90 | View Code Duplication | <?php if ( ! $post_type || 'post' == $post_type ) { ?> |
|
91 | $( '<option>' ) |
||
92 | .val( 'post2comic' ) |
||
93 | .text( <?php echo json_encode( __( 'Convert to Comic', 'jetpack' ) ); ?> ) |
||
94 | .appendTo( "select[name='action'], select[name='action2']" ); |
||
95 | <?php } ?> |
||
96 | View Code Duplication | <?php if ( ! $post_type || self::POST_TYPE == $post_type ) { ?> |
|
97 | $( '<option>' ) |
||
98 | .val( 'comic2post' ) |
||
99 | .text( <?php echo json_encode( __( 'Convert to Post', 'jetpack' ) ); ?> ) |
||
100 | .appendTo( "select[name='action'], select[name='action2']" ); |
||
101 | <?php } ?> |
||
102 | |||
103 | $( '#message.jetpack-comic-post-type-conversion' ).remove().insertAfter( $( '.wrap h2:first' ) ).show(); |
||
104 | }); |
||
105 | </script> |
||
106 | <?php |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Handle the "Convert to [Post|Comic]" bulk action. |
||
111 | */ |
||
112 | public function bulk_edit() { |
||
113 | if ( empty( $_REQUEST['post'] ) ) |
||
114 | return; |
||
115 | |||
116 | $wp_list_table = _get_list_table( 'WP_Posts_List_Table' ); |
||
117 | $action = $wp_list_table->current_action(); |
||
118 | |||
119 | check_admin_referer( 'bulk-posts' ); |
||
120 | |||
121 | if ( 'post2comic' == $action || 'comic2post' == $action ) { |
||
122 | if ( ! current_user_can( 'publish_posts' ) ) |
||
123 | wp_die( __( 'You are not allowed to make this change.', 'jetpack' ) ); |
||
124 | |||
125 | $post_ids = array_map( 'intval', $_REQUEST['post'] ); |
||
126 | |||
127 | $modified_count = 0; |
||
128 | |||
129 | foreach ( $post_ids as $post_id ) { |
||
130 | $destination_post_type = ( $action == 'post2comic' ) ? self::POST_TYPE : 'post'; |
||
131 | $origin_post_type = ( $destination_post_type == 'post' ) ? self::POST_TYPE : 'post'; |
||
132 | |||
133 | if ( current_user_can( 'edit_post', $post_id ) ) { |
||
134 | $post = get_post( $post_id ); |
||
135 | |||
136 | // Only convert posts that are post => comic or comic => post. |
||
137 | // (e.g., Ignore comic => comic, page => post, etc. ) |
||
138 | if ( $post->post_type != $destination_post_type && $post->post_type == $origin_post_type ) { |
||
139 | $post_type_object = get_post_type_object( $destination_post_type ); |
||
140 | |||
141 | if ( current_user_can( $post_type_object->cap->publish_posts ) ) { |
||
142 | set_post_type( $post_id, $destination_post_type ); |
||
143 | $modified_count++; |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | $sendback = remove_query_arg( array( 'exported', 'untrashed', 'deleted', 'ids' ), wp_get_referer() ); |
||
150 | |||
151 | if ( ! $sendback ) |
||
152 | $sendback = add_query_arg( array( 'post_type', get_post_type() ), admin_url( 'edit.php' ) ); |
||
153 | |||
154 | $pagenum = $wp_list_table->get_pagenum(); |
||
155 | $sendback = add_query_arg( array( 'paged' => $pagenum, 'post_type_changed' => $modified_count ), $sendback ); |
||
156 | |||
157 | wp_safe_redirect( $sendback ); |
||
158 | exit(); |
||
|
|||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Show the post conversion success notice. |
||
164 | */ |
||
165 | public function bulk_edit_notices() { |
||
166 | global $pagenow; |
||
167 | |||
168 | if ( 'edit.php' == $pagenow && ! empty( $_GET['post_type_changed'] ) ) { |
||
169 | ?><div id="message" class="updated below-h2 jetpack-comic-post-type-conversion" style="display: none;"><p><?php |
||
170 | printf( _n( 'Post converted.', '%s posts converted', $_GET['post_type_changed'], 'jetpack' ), number_format_i18n( $_GET['post_type_changed'] ) ); |
||
171 | ?></p></div><?php |
||
172 | } |
||
173 | } |
||
174 | |||
175 | public function register_scripts() { |
||
176 | if( is_rtl() ) { |
||
177 | wp_enqueue_style( 'jetpack-comics-style', plugins_url( 'comics/rtl/comics-rtl.css', __FILE__ ) ); |
||
178 | } else { |
||
179 | wp_enqueue_style( 'jetpack-comics-style', plugins_url( 'comics/comics.css', __FILE__ ) ); |
||
180 | } |
||
181 | |||
182 | wp_enqueue_script( 'jetpack-comics', plugins_url( 'comics/comics.js', __FILE__ ), array( 'jquery', 'jquery.spin' ) ); |
||
183 | |||
184 | $options = array( |
||
185 | 'nonce' => wp_create_nonce( 'jetpack_comic_upload_nonce' ), |
||
186 | 'writeURL' => admin_url( 'admin-ajax.php?action=jetpack_comic_upload' ), |
||
187 | 'labels' => array( |
||
188 | 'dragging' => __( 'Drop images to upload', 'jetpack' ), |
||
189 | 'uploading' => __( 'Uploading...', 'jetpack' ), |
||
190 | 'processing' => __( 'Processing...', 'jetpack' ), |
||
191 | 'unsupported' => __( "Sorry, your browser isn't supported. Upgrade at browsehappy.com.", 'jetpack' ), |
||
192 | 'invalidUpload' => __( 'Only images can be uploaded here.', 'jetpack' ), |
||
193 | 'error' => __( "Your upload didn't complete; try again later or cross your fingers and try again right now.", 'jetpack' ), |
||
194 | ) |
||
195 | ); |
||
196 | |||
197 | wp_localize_script( 'jetpack-comics', 'Jetpack_Comics_Options', $options ); |
||
198 | } |
||
199 | |||
200 | public function admin_enqueue_scripts() { |
||
203 | |||
204 | public function maybe_register_post_types() { |
||
205 | // Return early if theme does not support Jetpack Comic. |
||
206 | if ( ! ( $this->site_supports_comics() ) ) |
||
207 | return; |
||
208 | |||
209 | $this->register_post_types(); |
||
211 | |||
212 | function register_post_types() { |
||
263 | |||
264 | public function manage_posts_columns( $columns ) { |
||
270 | |||
271 | public function manage_posts_custom_column( $column_name, $post_ID ) { |
||
276 | |||
277 | /** |
||
278 | * The function url_to_postid() doesn't handle pretty permalinks |
||
279 | * for CPTs very well. When we're generating an RSS feed to be consumed |
||
280 | * for Feedbag (the Reader's feed storage mechanism), eschew |
||
281 | * a pretty URL for one that will get the post into the Reader. |
||
282 | * |
||
283 | * @see http://core.trac.wordpress.org/ticket/19744 |
||
284 | * @param string $permalink The existing (possibly pretty) permalink. |
||
285 | */ |
||
286 | public function custom_permalink_for_feedbag( $permalink ) { |
||
295 | |||
296 | /* |
||
297 | * Update messages for the Comic admin. |
||
298 | */ |
||
299 | View Code Duplication | function updated_messages( $messages ) { |
|
321 | |||
322 | /** |
||
323 | * Should this Custom Post Type be made available? |
||
324 | */ |
||
325 | public function site_supports_comics() { |
||
366 | |||
367 | /** |
||
368 | * Anywhere that a feed is displaying posts, show comics too. |
||
369 | * |
||
370 | * @param WP_Query $query |
||
371 | */ |
||
372 | public function include_in_feeds( $query ) { |
||
393 | |||
394 | /** |
||
395 | * API endpoint for front-end image uploading. |
||
396 | */ |
||
397 | public function upload() { |
||
488 | |||
489 | public function add_posts_to_loop( $query ) { |
||
504 | |||
505 | /** |
||
506 | * Add to REST API post type whitelist |
||
507 | */ |
||
508 | public function allow_rest_api_type( $post_types ) { |
||
512 | |||
513 | } |
||
514 | |||
534 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.