Total Complexity | 48 |
Total Lines | 369 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Buttons 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.
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 Buttons, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Buttons { |
||
14 | |||
15 | public static function page() { |
||
16 | |||
17 | $action = null !== filter_input( INPUT_GET, 'action' ) ? filter_input( INPUT_GET, 'action' ) : null; |
||
18 | $is_network = is_network_admin(); |
||
19 | |||
20 | if ( $action == 'confirm_delete' && null !== filter_input( INPUT_GET, 'button' ) ) { |
||
21 | $button = ( $is_network === true ? \PodloveSubscribeButton\Model\NetworkButton::find_by_id( (int) filter_input( INPUT_GET, 'button' ) ) : \PodloveSubscribeButton\Model\Button::find_by_id( (int) filter_input( INPUT_GET, 'button' ) ) ); |
||
22 | ?> |
||
23 | <div class="updated"> |
||
24 | <p> |
||
25 | <strong> |
||
26 | <?php printf( __( 'You selected to delete the button "%s". Please confirm this action.', 'podlove-subscribe-button' ), $button->title ) ?> |
||
27 | </strong> |
||
28 | </p> |
||
29 | <p> |
||
30 | <?php echo self::get_action_link( $button, __( 'Delete button permanently', 'podlove-subscribe-button' ), 'delete', 'button' ) ?> |
||
31 | <?php echo self::get_action_link( $button, __( "Don't change anything", 'podlove-subscribe-button' ), 'keep', 'button-primary' ) ?> |
||
32 | </p> |
||
33 | </div> |
||
34 | <?php |
||
35 | } |
||
36 | ?> |
||
37 | <div class="wrap"> |
||
38 | <h1><?php echo __( 'Podlove Subscribe Button', 'podlove-subscribe-button' ); ?> <a href="?page=<?php echo filter_input( INPUT_GET, 'page' ); ?>&action=new&network=<?php echo $is_network; ?>" class="add-new-h2"><?php _e( 'Add New', 'podlove-subscribe-button' ); ?></a></h1> |
||
39 | <div id="poststuff"> |
||
40 | <div id="post-body" class="columns-2"> |
||
41 | <div id="post-body-content"> |
||
42 | <?php |
||
43 | switch ( $action ) { |
||
44 | case 'new': self::new_template(); break; |
||
45 | case 'edit': self::edit_template(); break; |
||
46 | case 'index': self::view_template(); break; |
||
47 | default: self::view_template(); break; |
||
48 | } |
||
49 | ?> |
||
50 | </div> |
||
51 | <div id="postbox-container-1" class="postbox-container"> |
||
52 | <?php self::sidebar(); ?> |
||
53 | </div> |
||
54 | </div> |
||
55 | </div> |
||
56 | </div><!-- .wrap --> |
||
57 | <?php |
||
58 | } |
||
59 | |||
60 | public static function sidebar() { ?> |
||
61 | <div id="submitdiv" class="postbox"> |
||
62 | <h2 class="ui-sortable-handle"><span>Podlove Subscribe Button <code><?php echo \PodloveSubscribeButton::$version; ?></code></span></h2> |
||
63 | <div class="inside"> |
||
64 | <div id="minor-publishing" style="padding:0 10px;"> |
||
65 | <p><?php _e( 'This plugin allows easy inclusion of the Podlove Subscribe Button. Put it in your sidebar with a simple widget or include the button in pages and/or posts with a simple shortcode.', 'podlove-subscribe-button' ); ?></p> |
||
66 | <p><?php _e( 'Start by adding a button for each of your podcasts here. You can then add the button to your sidebar by adding the <a href="widgets.php">Podlove Subscribe Button widget</a>.', 'podlove-subscribe-button' ); ?></p> |
||
67 | <p><?php _e( 'If you want to display the button inside a page or article, you can also use the <code>[podlove-subscribe-button]</code> shortcode anywhere.', 'podlove-subscribe-button' ); ?></p> |
||
68 | </div> |
||
69 | <div id="major-publishing-actions"> |
||
70 | <ul> |
||
71 | <li><a href="https://wordpress.org/plugins/podlove-subscribe-button/" target="_blank">WordPress.org</a></li> |
||
72 | <li>Podlove Project</li> |
||
73 | <li>Community</li> |
||
74 | <li>Donate</li> |
||
75 | </ul> |
||
76 | </div> |
||
77 | </div> |
||
78 | </div> |
||
79 | <?php |
||
80 | |||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Process form: save/update a format |
||
85 | */ |
||
86 | public static function save() { |
||
87 | if ( null == filter_input( INPUT_GET, 'button' ) ) |
||
88 | return; |
||
89 | |||
90 | $post = filter_input_array( INPUT_POST ); |
||
91 | |||
92 | $button = ( filter_input( INPUT_GET, 'network' ) === '1' ? \PodloveSubscribeButton\Model\NetworkButton::find_by_id( filter_input( INPUT_GET, 'button' ) ) : \PodloveSubscribeButton\Model\Button::find_by_id( filter_input( INPUT_GET, 'button' ) ) ); |
||
93 | $button->update_attributes( $post[ 'podlove_button' ] ); |
||
94 | |||
95 | if ( isset( $post[ 'submit_and_stay' ] ) ) { |
||
96 | self::redirect( 'edit', $button->id, array( 'network' => filter_input( INPUT_GET, 'network' ) ), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
97 | } else { |
||
98 | self::redirect( 'index', $button->id, array(), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
99 | } |
||
100 | } |
||
101 | /** |
||
102 | * Process form: create a format |
||
103 | */ |
||
104 | public static function create() { |
||
105 | global $wpdb; |
||
106 | |||
107 | $post = filter_input_array( INPUT_POST ); |
||
108 | |||
109 | $button = ( filter_input( INPUT_GET, 'network' ) === '1' ? new \PodloveSubscribeButton\Model\NetworkButton : new \PodloveSubscribeButton\Model\Button ); |
||
110 | $button->update_attributes( $post[ 'podlove_button' ] ); |
||
111 | |||
112 | if ( isset( $post[ 'submit_and_stay' ] ) ) { |
||
113 | self::redirect( 'edit', $button->id, array( 'network' => filter_input( INPUT_GET, 'network' ) ), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
114 | } else { |
||
115 | self::redirect( 'index', $button->id, array(), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Process form: delete a format |
||
121 | */ |
||
122 | public static function delete() { |
||
123 | if ( null == filter_input( INPUT_GET, 'button' ) ) |
||
124 | return; |
||
125 | |||
126 | $button = ( filter_input( INPUT_GET, 'network' ) === '1' ? \PodloveSubscribeButton\Model\NetworkButton::find_by_id( filter_input( INPUT_GET, 'button' ) ) : \PodloveSubscribeButton\Model\Button::find_by_id( filter_input( INPUT_GET, 'button' ) ) ); |
||
127 | $button->delete(); |
||
128 | |||
129 | self::redirect( 'index', null, array(), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Helper method: redirect to a certain page. |
||
134 | */ |
||
135 | public static function redirect( $action, $button_id = null, $params = array(), $network = false ) { |
||
136 | $page = ( $network ? '/network/settings' : 'options-general' ) . '.php?page=' . filter_input( INPUT_GET, 'page' ); |
||
137 | $show = ( $button_id ) ? '&button=' . $button_id : ''; |
||
138 | $action = '&action=' . $action; |
||
139 | |||
140 | array_walk( $params, function( &$value, $key ) { $value = "&$key=$value"; } ); |
||
141 | |||
142 | wp_redirect( admin_url( $page . $show . $action . implode( '', $params ) ) ); |
||
143 | } |
||
144 | |||
145 | public static function process_form() { |
||
146 | if ( null === filter_input( INPUT_GET, 'button' ) ) |
||
147 | return; |
||
148 | |||
149 | $action = ( null !== filter_input( INPUT_GET, 'action' ) ? filter_input( INPUT_GET, 'action' ) : null ); |
||
150 | |||
151 | if ( $action === 'save' ) { |
||
152 | self::save(); |
||
153 | } elseif ( $action === 'create' ) { |
||
154 | self::create(); |
||
155 | } elseif ( $action === 'delete' ) { |
||
156 | self::delete(); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | public static function new_template() { |
||
161 | if ( filter_input( INPUT_GET, 'network' ) == '1' ) { |
||
162 | $button = new \PodloveSubscribeButton\Model\NetworkButton; |
||
163 | } else { |
||
164 | $button = new \PodloveSubscribeButton\Model\Button; |
||
165 | } |
||
166 | |||
167 | echo '<h2>' . __( 'New Subscribe button', 'podlove-subscribe-button' ) . '</h2>' . |
||
168 | __( 'Please fill in your Podcast metadata to create a Podlove Subscription button', 'podlove-subscribe-button' ); |
||
169 | self::form_template( $button, 'create' ); |
||
170 | } |
||
171 | |||
172 | public static function edit_template() { |
||
181 | } |
||
182 | |||
183 | public static function view_template() { |
||
184 | |||
185 | $is_network = is_network_admin(); |
||
186 | $table = new \PodloveSubscribeButton\Button_List_Table; |
||
187 | $table->prepare_items(); |
||
188 | $table->display(); |
||
189 | |||
190 | // Get the global button settings (with fallback to default values) |
||
191 | $settings = \PodloveSubscribeButton\Model\Button::get_global_setting_with_fallback(); |
||
192 | |||
193 | if ( ! $is_network ) : |
||
194 | ?> |
||
195 | <h3><?php _e( 'Default Settings', 'podlove-subscribe-button' ); ?></h3> |
||
196 | <form method="post" action="options.php"> |
||
197 | <?php settings_fields( 'podlove-subscribe-button' ); ?> |
||
198 | <?php do_settings_sections( 'podlove-subscribe-button' ); ?> |
||
199 | <table class="form-table"> |
||
200 | <tr valign="top"> |
||
201 | <th scope="row"><label for="podlove_subscribe_button_default_size"><?php _e( 'Size', 'podlove-subscribe-button' ); ?></label></th> |
||
202 | <td> |
||
203 | <select name="podlove_subscribe_button_default_size" id="podlove_subscribe_button_default_size"> |
||
204 | <?php foreach ( \PodloveSubscribeButton\Defaults::button( 'size' ) as $value => $description ) : ?> |
||
205 | <option value="<?php echo $value; ?>" <?php selected( $settings['size'], $value ); ?>><?php echo $description; ?></option> |
||
206 | <?php endforeach; ?> |
||
207 | </select> |
||
208 | </td> |
||
209 | </tr> |
||
210 | <tr valign="top"> |
||
211 | <th scope="row"><label for="podlove_subscribe_button_default_autowidth"><?php _e( 'Autowidth', 'podlove-subscribe-button' ); ?></label></th> |
||
212 | <td> |
||
213 | <input type="checkbox" name="podlove_subscribe_button_default_autowidth" |
||
214 | id="podlove_subscribe_button_default_autowidth" <?php checked( $settings['autowidth'], 'on' ); ?> /> |
||
215 | </td> |
||
216 | </tr> |
||
217 | <tr valign="top"> |
||
218 | <th scope="row"><label for="podlove_subscribe_button_default_color"><?php _e( 'Color', 'podlove-subscribe-button' ); ?></label></th> |
||
219 | <td> |
||
220 | <input id="podlove_subscribe_button_default_color" name="podlove_subscribe_button_default_color" class="podlove_subscribe_button_color" |
||
221 | value="<?php echo $settings['color'] ?>"/> |
||
222 | </td> |
||
223 | </tr> |
||
224 | <tr valign="top"> |
||
225 | <th scope="row"><label for="podlove_subscribe_button_default_style"><?php _e( 'Style', 'podlove-subscribe-button' ); ?></label></th> |
||
226 | <td> |
||
227 | <select name="podlove_subscribe_button_default_style" id="podlove_subscribe_button_default_style"> |
||
228 | <?php foreach ( \PodloveSubscribeButton\Defaults::button( 'style' ) as $value => $description ) : ?> |
||
229 | <option value="<?php echo $value; ?>" <?php selected( $settings['style'], $value ); ?>><?php echo $description; ?></option> |
||
230 | <?php endforeach; ?> |
||
231 | </select> |
||
232 | </td> |
||
233 | </tr> |
||
234 | <tr valign="top"> |
||
235 | <th scope="row"><label for="podlove_subscribe_button_default_format"><?php _e( 'Format', 'podlove-subscribe-button' ); ?></label></th> |
||
236 | <td> |
||
237 | <select name="podlove_subscribe_button_default_format" id="podlove_subscribe_button_default_format"> |
||
238 | <?php foreach ( \PodloveSubscribeButton\Defaults::button( 'format' ) as $value => $description ) : ?> |
||
239 | <option value="<?php echo $value; ?>" <?php selected( $settings['format'], $value ); ?>><?php echo $description; ?></option> |
||
240 | <?php endforeach; ?> |
||
241 | </select> |
||
242 | </td> |
||
243 | </tr> |
||
244 | <tr valign="top"> |
||
245 | <th scope="row"><label for="podlove_subscribe_button_default_language"><?php _e( 'Language', 'podlove-subscribe-button' ); ?></label></th> |
||
246 | <td> |
||
247 | <select name="podlove_subscribe_button_default_language" id="podlove_subscribe_button_default_language"> |
||
248 | <?php foreach ( \PodloveSubscribeButton\Defaults::button( 'language' ) as $value ) : ?> |
||
249 | <option value="<?php echo $value; ?>" <?php selected( $settings['language'], $value ); ?>><?php echo $value; ?></option> |
||
250 | <?php endforeach; ?> |
||
251 | </select> |
||
252 | </td> |
||
253 | </tr> |
||
254 | </table> |
||
255 | <?php submit_button(); ?> |
||
256 | </form> |
||
257 | <?php |
||
258 | endif; |
||
259 | } |
||
260 | |||
261 | private static function form_template( $button, $action ) { |
||
262 | // Enqueue Scripts for Media Manager |
||
263 | wp_enqueue_media(); |
||
264 | // Adjust if is_network |
||
265 | $is_network = is_network_admin(); |
||
266 | ?> |
||
267 | <form method="post" action="<?php echo ( $is_network === true ? '/wp-admin/network/settings' : 'options-general' ) ?>.php?page=podlove-subscribe-button&button=<?php echo $button->id; ?>&action=<?php echo $action; ?>&network=<?php echo $is_network; ?>"> |
||
268 | <input type="hidden" value="<?php echo $button->id; ?>" name="podlove_button[id]" /> |
||
269 | <table class="form-table" border="0" cellspacing="0"> |
||
270 | <tbody> |
||
271 | <tr> |
||
272 | <th scope="row"> |
||
273 | <label for="podlove_button_name"><?php _e( 'Button ID', 'podlove-subscribe-button' ); ?></label> |
||
274 | </th> |
||
275 | <td> |
||
276 | <input type="text" class="regular-text" id="podlove_button_name" name="podlove_button[name]" value="<?php echo $button->name; ?>" /> |
||
277 | <p class="description"><?php _e( 'The ID will be used as in internal identifier for shortcodes.', 'podlove-subscribe-button' ); ?></p> |
||
278 | </td> |
||
279 | </tr> |
||
280 | <tr> |
||
281 | <th scope="row"> |
||
282 | <label for="podlove_button_title"><?php _e( 'Podcast Title', 'podlove-subscribe-button' ); ?></label> |
||
283 | </th> |
||
284 | <td> |
||
285 | <input type="text" class="regular-text" id="podlove_button_title" name="podlove_button[title]" value="<?php echo $button->title; ?>" /> |
||
286 | </td> |
||
287 | </tr> |
||
288 | <tr> |
||
289 | <th scope="row"> |
||
290 | <label for="podlove_button_subtitle"><?php _e( 'Podcast Subtitle', 'podlove-subscribe-button' ); ?></label> |
||
291 | </th> |
||
292 | <td> |
||
293 | <input type="text" class="regular-text" id="podlove_button_subtitle" name="podlove_button[subtitle]" value="<?php echo $button->subtitle; ?>" /> |
||
294 | </td> |
||
295 | </tr> |
||
296 | <tr> |
||
297 | <th scope="row"> |
||
298 | <label for="podlove_button_description"><?php _e( 'Podcast Description', 'podlove-subscribe-button' ); ?></label> |
||
299 | </th> |
||
300 | <td> |
||
301 | <textarea class="autogrow" cols="40" rows="3" id="podlove_button_description" name="podlove_button[description]"><?php echo $button->description; ?></textarea> |
||
302 | </td> |
||
303 | </tr> |
||
304 | <tr> |
||
305 | <th scope="row"> |
||
306 | <label for="podlove-button-cover"><?php _e( 'Podcast Image URL', 'podlove-subscribe-button' ); ?></label> |
||
307 | </th> |
||
308 | <td> |
||
309 | <input type="text" class="regular-text" id="podlove-button-cover" name="podlove_button[cover]" value="<?php echo $button->cover; ?>" /> |
||
310 | <a id="Podlove_cover_image_select" class="button" href="#">Select</a> |
||
311 | <br /><img src="<?php echo $button->cover; ?>" alt="" style="width: 200px" /> |
||
312 | <script type="text/javascript"> |
||
313 | (function($) { |
||
314 | $("#podlove-button-cover").on( 'change', function() { |
||
315 | url = $(this).val(); |
||
316 | $(this).parent().find("img").attr("src", url); |
||
317 | } ); |
||
318 | })(jQuery); |
||
319 | </script> |
||
320 | </td> |
||
321 | </tr> |
||
322 | <tr> |
||
323 | <th scope="row"> |
||
324 | <label for="feeds_table"><?php _e( 'Podcast Feeds', 'podlove-subscribe-button' ); ?></label> |
||
325 | </th> |
||
326 | <td> |
||
327 | <table id="feeds_table" class="podlove_alternating widefat striped"> |
||
328 | <thead> |
||
329 | <tr> |
||
330 | <th scope="col" id="url" class="manage-column column-primary"><?php _e( 'URL', 'podlove-subscribe-button' ); ?></th> |
||
331 | <th scope="col" id="itunes_id" class="manage-column"><?php _e( 'iTunes feed ID', 'podlove-subscribe-button' ); ?></th> |
||
332 | <th scope="col" id="format" class="manage-column"><?php _e( 'Media format', 'podlove-subscribe-button' ); ?></th> |
||
333 | <th scope="col" id="action" class="manage-column"><?php _e( 'Actions', 'podlove-subscribe-button' ); ?></th> |
||
334 | </tr> |
||
335 | </thead> |
||
336 | <tbody id="feeds_table_body"> |
||
337 | </tbody> |
||
338 | </table> |
||
339 | <input type="button" class="button add_feed" value="+" /> |
||
340 | <p><span class="description"><?php _e( 'Provide all Feeds with their corresponding Media File Type. The Subscribe Button will then automatically provide the most suitable feed to the subscriber with respect to their Podcast Client.', 'podlove-subscribe-button' ); ?></span></p> |
||
341 | </td> |
||
342 | </tr> |
||
343 | </tbody> |
||
344 | </table> |
||
345 | <input name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'podlove-subscribe-button' ); ?>" type="submit" /> |
||
346 | <input type="submit" name="submit_and_stay" id="submit_and_stay" class="button" value="<?php _e( 'Save Changes and Continue Editing', 'podlove-subscribe-button' ); ?>" /> |
||
347 | |||
348 | <script type="text/template" id="feed_line_template"> |
||
349 | <tr> |
||
350 | <td> |
||
351 | <input type="text" class="regular-text" name="podlove_button[feeds][{{id}}][url]" value="{{url}}" /> |
||
352 | </td> |
||
353 | <td> |
||
354 | <input type="text" class="regular-text" name="podlove_button[feeds][{{id}}][itunesfeedid]" value="{{itunesfeedid}}" /> |
||
355 | </td> |
||
356 | <td> |
||
357 | <select class="regular-text podlove-media-format" name="podlove_button[feeds][{{id}}][format]"> |
||
358 | <?php |
||
359 | foreach ( \PodloveSubscribeButton\Defaults::media_types() as $id => $audio ) { |
||
360 | echo "<option value='" . $id . "'>" . $audio[ 'title' ] . "</option>\n"; |
||
361 | } |
||
362 | ?> |
||
363 | </select> |
||
364 | </td> |
||
365 | <td><span class="dashicons dashicons-trash clickable podlove-icon-remove"></span></td> |
||
366 | </tr> |
||
367 | </script> |
||
368 | <script type="text/javascript"> |
||
369 | var feeds = <?php echo json_encode( $button->feeds ); ?>; |
||
370 | </script> |
||
371 | </form> |
||
372 | <?php |
||
373 | } |
||
374 | |||
375 | public static function get_action_link( $button, $title, $action = 'edit', $type = 'link' ) { |
||
382 | ); |
||
383 | } |
||
384 | |||
385 | } // END class |
||
386 |