| Total Complexity | 53 |
| Total Lines | 344 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 2 | Features | 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 |
||
| 5 | class Buttons { |
||
| 6 | |||
| 7 | public static function page() { |
||
| 8 | |||
| 9 | $action = null !== filter_input(INPUT_GET, 'action') ? filter_input(INPUT_GET, 'action') : null; |
||
| 10 | $is_network = is_network_admin(); |
||
| 11 | |||
| 12 | if ( $action == 'confirm_delete' && null !== filter_input(INPUT_GET, 'button') ) { |
||
| 13 | $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') ) ); |
||
| 14 | ?> |
||
| 15 | <div class="updated"> |
||
| 16 | <p> |
||
| 17 | <strong> |
||
| 18 | <?php printf( __( 'You selected to delete the button "%s". Please confirm this action.', 'podlove-subscribe-button' ), sanitize_title($button->title) ) ?> |
||
| 19 | </strong> |
||
| 20 | </p> |
||
| 21 | <p> |
||
| 22 | <?php echo self::get_action_link( $button, __( 'Delete button permanently', 'podlove-subscribe-button' ), 'delete', 'button' ) ?> |
||
| 23 | <?php echo self::get_action_link( $button, __( "Don't change anything", 'podlove-subscribe-button' ), 'keep', 'button-primary' ) ?> |
||
| 24 | </p> |
||
| 25 | </div> |
||
| 26 | <?php |
||
| 27 | } |
||
| 28 | ?> |
||
| 29 | <div class="wrap"> |
||
| 30 | <h2><?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></h2> |
||
| 31 | <?php |
||
| 32 | |||
| 33 | switch ( $action ) { |
||
| 34 | case 'new': self::new_template(); break; |
||
| 35 | case 'edit': self::edit_template(); break; |
||
| 36 | case 'index': self::view_template(); break; |
||
| 37 | default: self::view_template(); break; |
||
| 38 | } |
||
| 39 | ?> |
||
| 40 | </div> |
||
| 41 | <?php |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Process form: save/update a format |
||
| 46 | */ |
||
| 47 | public static function save() { |
||
| 48 | if ( null == filter_input(INPUT_GET, 'button') ) |
||
| 49 | return; |
||
| 50 | |||
| 51 | if (!wp_verify_nonce($_REQUEST['_psb_nonce'])) { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | $post = filter_input_array(INPUT_POST); |
||
| 56 | |||
| 57 | $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') ) ); |
||
| 58 | $button->update_attributes( $post['podlove_button'] ); |
||
| 59 | |||
| 60 | if ( isset($post['submit_and_stay']) ) { |
||
| 61 | self::redirect( 'edit', $button->id, array( 'network' => filter_input(INPUT_GET, 'network') ), ( filter_input(INPUT_GET, 'network') === '1' ? true : false ) ); |
||
| 62 | } else { |
||
| 63 | self::redirect( 'index', $button->id, array(), ( filter_input(INPUT_GET, 'network') === '1' ? true : false ) ); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | /** |
||
| 67 | * Process form: create a format |
||
| 68 | */ |
||
| 69 | public static function create() { |
||
| 70 | global $wpdb; |
||
| 71 | |||
| 72 | $post = filter_input_array(INPUT_POST); |
||
| 73 | |||
| 74 | $button = ( filter_input(INPUT_GET, 'network') === '1' ? new \PodloveSubscribeButton\Model\NetworkButton : new \PodloveSubscribeButton\Model\Button ); |
||
| 75 | $button->update_attributes( $post['podlove_button'] ); |
||
| 76 | |||
| 77 | if ( isset($post['submit_and_stay']) ) { |
||
| 78 | self::redirect( 'edit', $button->id, array( 'network' => filter_input(INPUT_GET, 'network') ), ( filter_input(INPUT_GET, 'network') === '1' ? true : false ) ); |
||
| 79 | } else { |
||
| 80 | self::redirect( 'index', $button->id, array(), ( filter_input(INPUT_GET, 'network') === '1' ? true : false ) ); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Process form: delete a format |
||
| 86 | */ |
||
| 87 | public static function delete() { |
||
| 88 | if ( null == filter_input(INPUT_GET, 'button') ) |
||
| 89 | return; |
||
| 90 | |||
| 91 | $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') ) ); |
||
| 92 | $button->delete(); |
||
| 93 | |||
| 94 | self::redirect( 'index', null, array(), ( filter_input(INPUT_GET, 'network') === '1' ? true : false ) ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Helper method: redirect to a certain page. |
||
| 99 | */ |
||
| 100 | public static function redirect( $action, $button_id = null, $params = array(), $network = false ) { |
||
| 101 | $page = ( $network ? '/network/settings' : 'options-general' ) . '.php?page=' . filter_input(INPUT_GET, 'page'); |
||
| 102 | $show = ( $button_id ) ? '&button=' . $button_id : ''; |
||
| 103 | $action = '&action=' . $action; |
||
| 104 | |||
| 105 | array_walk( $params, function(&$value, $key) { $value = "&$key=$value"; } ); |
||
| 106 | |||
| 107 | wp_redirect( admin_url( $page . $show . $action . implode( '', $params ) ) ); |
||
| 108 | } |
||
| 109 | |||
| 110 | public static function process_form() { |
||
| 111 | if ( null === filter_input(INPUT_GET, 'button') ) |
||
| 112 | return; |
||
| 113 | |||
| 114 | $action = ( null !== filter_input(INPUT_GET, 'action') ? filter_input(INPUT_GET, 'action') : null ); |
||
| 115 | |||
| 116 | if (!in_array($action, ['save', 'create', 'delete'])) { |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | if (!wp_verify_nonce($_REQUEST['_psb_nonce'])) { |
||
| 121 | return; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ( $action === 'save' ) { |
||
| 125 | self::save(); |
||
| 126 | } elseif ( $action === 'create' ) { |
||
| 127 | self::create(); |
||
| 128 | } elseif ( $action === 'delete' ) { |
||
| 129 | self::delete(); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | public static function new_template() { |
||
| 134 | if ( filter_input(INPUT_GET, 'network') == '1' ) { |
||
| 135 | $button = new \PodloveSubscribeButton\Model\NetworkButton; |
||
| 136 | } else { |
||
| 137 | $button = new \PodloveSubscribeButton\Model\Button; |
||
| 138 | } |
||
| 139 | |||
| 140 | echo '<h3>' . __( 'New Subscribe button', 'podlove-subscribe-button' ) . '</h3>'. |
||
| 141 | __( 'Please fill in your Podcast metadata to create a Podlove Subscription button', 'podlove-subscribe-button' ); |
||
| 142 | self::form_template( $button, 'create' ); |
||
| 143 | } |
||
| 144 | |||
| 145 | public static function edit_template() { |
||
| 146 | if ( filter_input(INPUT_GET, 'network') == '1' ) { |
||
| 147 | $button = \PodloveSubscribeButton\Model\NetworkButton::find_by_id( filter_input(INPUT_GET, 'button') ); |
||
| 148 | } else { |
||
| 149 | $button = \PodloveSubscribeButton\Model\Button::find_by_id( filter_input(INPUT_GET, 'button') ); |
||
| 150 | } |
||
| 151 | |||
| 152 | echo '<h3>' . sprintf( __( 'Edit Subscribe button: %s', 'podlove-subscribe-button' ), sanitize_text_field($button->title) ) . '</h3>'; |
||
| 153 | self::form_template( $button, 'save' ); |
||
| 154 | } |
||
| 155 | |||
| 156 | public static function view_template() { |
||
| 157 | $is_network = is_network_admin(); |
||
| 158 | ?> |
||
| 159 | <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> |
||
| 160 | <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> |
||
| 161 | <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> |
||
| 162 | <?php |
||
| 163 | $table = new \PodloveSubscribeButton\Button_List_Table; |
||
| 164 | $table->prepare_items(); |
||
| 165 | $table->display(); |
||
| 166 | |||
| 167 | // Get the global button settings (with fallback to default values) |
||
| 168 | $settings = \PodloveSubscribeButton\Model\Button::get_global_setting_with_fallback(); |
||
| 169 | |||
| 170 | if ( ! $is_network ) : |
||
| 171 | ?> |
||
| 172 | <h3><?php _e('Default Settings', 'podlove-subscribe-button' ); ?></h3> |
||
| 173 | <form method="post" action="options.php"> |
||
| 174 | <?php settings_fields( 'podlove-subscribe-button' ); ?> |
||
| 175 | <?php do_settings_sections( 'podlove-subscribe-button' ); ?> |
||
| 176 | <table class="form-table"> |
||
| 177 | <tr valign="top"> |
||
| 178 | <th scope="row"><label for="podlove_subscribe_button_default_size"><?php _e('Size', 'podlove-subscribe-button' ); ?></label></th> |
||
| 179 | <td> |
||
| 180 | <select name="podlove_subscribe_button_default_size" id="podlove_subscribe_button_default_size"> |
||
| 181 | <?php foreach (\PodloveSubscribeButton\Model\Button::$size as $value => $description) : ?> |
||
| 182 | <option value="<?php echo $value; ?>" <?php echo ( $settings['size'] == $value ? "selected" : '' ); ?>><?php echo $description; ?></option> |
||
| 183 | <?php endforeach; ?> |
||
| 184 | </select> |
||
| 185 | </td> |
||
| 186 | </tr> |
||
| 187 | <tr valign="top"> |
||
| 188 | <th scope="row"><label for="podlove_subscribe_button_default_autowidth"><?php _e('Autowidth', 'podlove-subscribe-button' ); ?></label></th> |
||
| 189 | <td> |
||
| 190 | <input type="checkbox" name="podlove_subscribe_button_default_autowidth" id="podlove_subscribe_button_default_autowidth" <?php echo ( $settings['autowidth'] == 'on' ? 'checked' : '' ) ?> /> |
||
| 191 | </td> |
||
| 192 | </tr> |
||
| 193 | <tr valign="top"> |
||
| 194 | <th scope="row"><label for="podlove_subscribe_button_default_color"><?php _e('Color', 'podlove-subscribe-button' ); ?></label></th> |
||
| 195 | <td> |
||
| 196 | <input id="podlove_subscribe_button_default_color" name="podlove_subscribe_button_default_color" class="podlove_subscribe_button_color" value="<?php echo $settings['color'] ?>" /> |
||
| 197 | </td> |
||
| 198 | </tr> |
||
| 199 | <tr valign="top"> |
||
| 200 | <th scope="row"><label for="podlove_subscribe_button_default_style"><?php _e('Style', 'podlove-subscribe-button' ); ?></label></th> |
||
| 201 | <td> |
||
| 202 | <select name="podlove_subscribe_button_default_style" id="podlove_subscribe_button_default_style"> |
||
| 203 | <?php foreach (\PodloveSubscribeButton\Model\Button::$style as $value => $description) : ?> |
||
| 204 | <option value="<?php echo $value; ?>" <?php echo ( $settings['style'] == $value ? "selected" : '' ); ?>><?php echo $description; ?></option> |
||
| 205 | <?php endforeach; ?> |
||
| 206 | </select> |
||
| 207 | </td> |
||
| 208 | </tr> |
||
| 209 | <tr valign="top"> |
||
| 210 | <th scope="row"><label for="podlove_subscribe_button_default_format"><?php _e('Format', 'podlove-subscribe-button' ); ?></label></th> |
||
| 211 | <td> |
||
| 212 | <select name="podlove_subscribe_button_default_format" id="podlove_subscribe_button_default_format"> |
||
| 213 | <?php foreach (\PodloveSubscribeButton\Model\Button::$format as $value => $description) : ?> |
||
| 214 | <option value="<?php echo $value; ?>" <?php echo ( $settings['format'] == $value ? "selected" : '' ); ?>><?php echo $description; ?></option> |
||
| 215 | <?php endforeach; ?> |
||
| 216 | </select> |
||
| 217 | </td> |
||
| 218 | </tr> |
||
| 219 | </table> |
||
| 220 | <?php submit_button(); ?> |
||
| 221 | </form> |
||
| 222 | <?php |
||
| 223 | endif; |
||
| 224 | } |
||
| 225 | |||
| 226 | private static function form_template( $button, $action ) { |
||
| 227 | // Enqueue Scripts for Media Manager |
||
| 228 | wp_enqueue_media(); |
||
| 229 | // Adjust if is_network |
||
| 230 | $is_network = is_network_admin(); |
||
| 231 | ?> |
||
| 232 | <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; ?>"> |
||
| 233 | <?php wp_nonce_field(-1, '_psb_nonce'); ?> |
||
| 234 | <input type="hidden" value="<?php echo $button->id; ?>" name="podlove_button[id]" /> |
||
| 235 | <table class="form-table" border="0" cellspacing="0"> |
||
| 236 | <tbody> |
||
| 237 | <tr> |
||
| 238 | <td scope="row"> |
||
| 239 | <label for="podlove_button_name"><?php _e('Button ID', 'podlove-subscribe-button' ); ?></label> |
||
| 240 | </td> |
||
| 241 | <td> |
||
| 242 | <input type="text" class="regular-text" id="podlove_button_name" name="podlove_button[name]" value="<?php echo esc_attr($button->name); ?>" /> |
||
| 243 | <br /><span class="description"><?php _e('The ID will be used as in internal identifier for shortcodes.', 'podlove-subscribe-button' ); ?></span> |
||
| 244 | </td> |
||
| 245 | </tr> |
||
| 246 | <tr> |
||
| 247 | <td scope="row"> |
||
| 248 | <label for="podlove_button_title"><?php _e('Podcast Title', 'podlove-subscribe-button' ); ?></label> |
||
| 249 | </td> |
||
| 250 | <td> |
||
| 251 | <input type="text" class="regular-text" id="podlove_button_title" name="podlove_button[title]" value="<?php echo esc_attr($button->title); ?>" /> |
||
| 252 | </td> |
||
| 253 | </tr> |
||
| 254 | <tr> |
||
| 255 | <td scope="row"> |
||
| 256 | <label for="podlove_button_subtitle"><?php _e('Podcast Subtitle', 'podlove-subscribe-button' ); ?></label> |
||
| 257 | </td> |
||
| 258 | <td> |
||
| 259 | <input type="text" class="regular-text" id="podlove_button_subtitle" name="podlove_button[subtitle]" value="<?php echo esc_attr($button->subtitle); ?>" /> |
||
| 260 | </td> |
||
| 261 | </tr> |
||
| 262 | <tr> |
||
| 263 | <td scope="row"> |
||
| 264 | <label for="podlove_button_description"><?php _e('Podcast Description', 'podlove-subscribe-button' ); ?></label> |
||
| 265 | </td> |
||
| 266 | <td> |
||
| 267 | <textarea class="autogrow" cols="40" rows="3" id="podlove_button_description" name="podlove_button[description]"><?php echo esc_attr($button->description); ?></textarea> |
||
| 268 | </td> |
||
| 269 | </tr> |
||
| 270 | <tr> |
||
| 271 | <td scope="row"> |
||
| 272 | <label for="podlove-button-cover"><?php _e('Podcast Image URL', 'podlove-subscribe-button' ); ?></label> |
||
| 273 | </td> |
||
| 274 | <td> |
||
| 275 | <input type="text" class="regular-text" id="podlove-button-cover" name="podlove_button[cover]" value="<?php echo esc_attr($button->cover); ?>" /> |
||
| 276 | <a id="Podlove_cover_image_select" class="button" href="#">Select</a> |
||
| 277 | <br /><img src="<?php echo sanitize_text_field($button->cover); ?>" alt="" style="width: 200px" /> |
||
| 278 | <script type="text/javascript"> |
||
| 279 | (function($) { |
||
| 280 | $("#podlove-button-cover").on( 'change', function() { |
||
| 281 | url = $(this).val(); |
||
| 282 | $(this).parent().find("img").attr("src", url); |
||
| 283 | } ); |
||
| 284 | })(jQuery); |
||
| 285 | </script> |
||
| 286 | </td> |
||
| 287 | </tr> |
||
| 288 | <tr> |
||
| 289 | <td scope="row"> |
||
| 290 | <label for="feeds_table"><?php _e('Podcast Feeds', 'podlove-subscribe-button' ); ?></label> |
||
| 291 | </td> |
||
| 292 | <td> |
||
| 293 | <table id="feeds_table" class="podlove_alternating" border="0" cellspacing="0"> |
||
| 294 | <thead> |
||
| 295 | <tr> |
||
| 296 | <th><?php _e('URL', 'podlove-subscribe-button' ); ?></th> |
||
| 297 | <th><?php _e('iTunes feed ID', 'podlove-subscribe-button' ); ?></th> |
||
| 298 | <th><?php _e('Media format', 'podlove-subscribe-button' ); ?></th> |
||
| 299 | <th><?php _e('Actions', 'podlove-subscribe-button' ); ?></th> |
||
| 300 | </tr> |
||
| 301 | </thead> |
||
| 302 | <tbody id="feeds_table_body"> |
||
| 303 | </tbody> |
||
| 304 | </table> |
||
| 305 | <input type="button" class="button add_feed" value="+" /> |
||
| 306 | <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> |
||
| 307 | </td> |
||
| 308 | </tr> |
||
| 309 | </tbody> |
||
| 310 | </table> |
||
| 311 | <input name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'podlove-subscribe-button' ); ?>" type="submit" /> |
||
| 312 | <input type="submit" name="submit_and_stay" id="submit_and_stay" class="button" value="<?php _e('Save Changes and Continue Editing', 'podlove-subscribe-button' ); ?>" /> |
||
| 313 | |||
| 314 | <script type="text/template" id="feed_line_template"> |
||
| 315 | <tr> |
||
| 316 | <td> |
||
| 317 | <input type="text" class="regular-text" name="podlove_button[feeds][{{id}}][url]" value="{{url}}" /> |
||
| 318 | </td> |
||
| 319 | <td> |
||
| 320 | <input type="text" class="regular-text" name="podlove_button[feeds][{{id}}][itunesfeedid]" value="{{itunesfeedid}}" /> |
||
| 321 | </td> |
||
| 322 | <td> |
||
| 323 | <select class="regular-text podlove-media-format" name="podlove_button[feeds][{{id}}][format]"> |
||
| 324 | <?php |
||
| 325 | foreach (\PodloveSubscribeButton\MediaTypes::$audio as $id => $audio) { |
||
| 326 | echo "<option value='".$id."'>".$audio['title']."</option>\n"; |
||
| 327 | } |
||
| 328 | ?> |
||
| 329 | </select> |
||
| 330 | </td> |
||
| 331 | <td><i class="clickable podlove-icon-remove"></i></td> |
||
| 332 | </tr> |
||
| 333 | </script> |
||
| 334 | <script type="text/javascript"> |
||
| 335 | var feeds = <?php echo json_encode($button->feeds); ?>; |
||
| 336 | </script> |
||
| 337 | </form> |
||
| 338 | <?php |
||
| 339 | } |
||
| 340 | |||
| 341 | public static function get_action_link( $button, $title, $action = 'edit', $type = 'link' ) { |
||
| 349 | ); |
||
| 350 | } |
||
| 351 | |||
| 352 | } |
||
| 353 |