| Total Complexity | 45 |
| Total Lines | 348 |
| 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() { |
||
| 54 | </div> |
||
| 55 | </div> |
||
| 56 | </div><!-- .wrap --> |
||
| 57 | <?php |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Render the sidebar HTML |
||
| 62 | */ |
||
| 63 | public static function sidebar() { ?> |
||
| 64 | <div id="submitdiv" class="postbox"> |
||
| 65 | <h2 class="ui-sortable-handle"><span>Podlove Subscribe Button <code><?php echo \PodloveSubscribeButton::$version; ?></code></span></h2> |
||
| 66 | <div class="inside"> |
||
| 67 | <div id="minor-publishing" style="padding:0 10px;"> |
||
| 68 | <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> |
||
| 69 | <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> |
||
| 70 | <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> |
||
| 71 | </div> |
||
| 72 | <div id="major-publishing-actions"> |
||
| 73 | <ul> |
||
| 74 | <li> |
||
| 75 | <a href="https://subscribe-button.podlove.org/" target="_blank">Podlove Subscribe Button</a> |
||
| 76 | </li> |
||
| 77 | <li> |
||
| 78 | <a href="https://podlove.org" target="_blank">Podlove Initiative</a> |
||
| 79 | </li> |
||
| 80 | <li> |
||
| 81 | <a href="https://community.podlove.org/" target="_blank">Podlove Community</a> |
||
| 82 | </li> |
||
| 83 | <li> |
||
| 84 | <a href="https://docs.podlove.org" target="_blank">Documentation & Guides</a> |
||
| 85 | </li> |
||
| 86 | <li> |
||
| 87 | <a href="https://podlove.org/donations/" target="_blank">Donate</a> |
||
| 88 | </li> |
||
| 89 | </ul> |
||
| 90 | </div> |
||
| 91 | </div> |
||
| 92 | </div> |
||
| 93 | <?php |
||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Process form: save/update a format |
||
| 99 | */ |
||
| 100 | public static function save() { |
||
| 101 | if ( null == filter_input( INPUT_GET, 'button' ) ) |
||
| 102 | return; |
||
| 103 | |||
| 104 | $post = filter_input_array( INPUT_POST ); |
||
| 105 | |||
| 106 | $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' ) ) ); |
||
| 107 | $button->update_attributes( $post[ 'podlove_button' ] ); |
||
| 108 | |||
| 109 | if ( isset( $post[ 'submit_and_stay' ] ) ) { |
||
| 110 | self::redirect( 'edit', $button->id, array( 'network' => filter_input( INPUT_GET, 'network' ) ), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
| 111 | } else { |
||
| 112 | self::redirect( 'index', $button->id, array(), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | /** |
||
| 116 | * Process form: create a format |
||
| 117 | */ |
||
| 118 | public static function create() { |
||
| 119 | global $wpdb; |
||
| 120 | |||
| 121 | $post = filter_input_array( INPUT_POST ); |
||
| 122 | |||
| 123 | $button = ( filter_input( INPUT_GET, 'network' ) === '1' ? new \PodloveSubscribeButton\Model\NetworkButton : new \PodloveSubscribeButton\Model\Button ); |
||
| 124 | $button->update_attributes( $post[ 'podlove_button' ] ); |
||
| 125 | |||
| 126 | if ( isset( $post[ 'submit_and_stay' ] ) ) { |
||
| 127 | self::redirect( 'edit', $button->id, array( 'network' => filter_input( INPUT_GET, 'network' ) ), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
| 128 | } else { |
||
| 129 | self::redirect( 'index', $button->id, array(), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Process form: delete a format |
||
| 135 | */ |
||
| 136 | public static function delete() { |
||
| 137 | if ( null == filter_input( INPUT_GET, 'button' ) ) |
||
| 138 | return; |
||
| 139 | |||
| 140 | $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' ) ) ); |
||
| 141 | $button->delete(); |
||
| 142 | |||
| 143 | self::redirect( 'index', null, array(), ( filter_input( INPUT_GET, 'network' ) === '1' ? true : false ) ); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Helper method: redirect to a certain page. |
||
| 148 | */ |
||
| 149 | public static function redirect( $action, $button_id = null, $params = array(), $network = false ) { |
||
| 150 | $page = ( $network ? '/network/settings' : 'options-general' ) . '.php?page=' . filter_input( INPUT_GET, 'page' ); |
||
| 151 | $show = ( $button_id ) ? '&button=' . $button_id : ''; |
||
| 152 | $action = '&action=' . $action; |
||
| 153 | |||
| 154 | array_walk( $params, function( &$value, $key ) { $value = "&$key=$value"; } ); |
||
| 155 | |||
| 156 | wp_redirect( admin_url( $page . $show . $action . implode( '', $params ) ) ); |
||
| 157 | } |
||
| 158 | |||
| 159 | public static function process_form() { |
||
| 160 | if ( null === filter_input( INPUT_GET, 'button' ) ) |
||
| 161 | return; |
||
| 162 | |||
| 163 | $action = ( null !== filter_input( INPUT_GET, 'action' ) ? filter_input( INPUT_GET, 'action' ) : null ); |
||
| 164 | |||
| 165 | if ( $action === 'save' ) { |
||
| 166 | self::save(); |
||
| 167 | } elseif ( $action === 'create' ) { |
||
| 168 | self::create(); |
||
| 169 | } elseif ( $action === 'delete' ) { |
||
| 170 | self::delete(); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | public static function new_template() { |
||
| 175 | if ( filter_input( INPUT_GET, 'network' ) == '1' ) { |
||
| 176 | $button = new \PodloveSubscribeButton\Model\NetworkButton; |
||
| 177 | } else { |
||
| 178 | $button = new \PodloveSubscribeButton\Model\Button; |
||
| 179 | } |
||
| 180 | |||
| 181 | echo '<h2>' . __( 'New Subscribe button', 'podlove-subscribe-button' ) . '</h2>' . |
||
| 182 | __( 'Please fill in your Podcast metadata to create a Podlove Subscription button', 'podlove-subscribe-button' ); |
||
| 183 | self::form_template( $button, 'create' ); |
||
| 184 | } |
||
| 185 | |||
| 186 | public static function edit_template() { |
||
| 187 | if ( filter_input( INPUT_GET, 'network' ) == '1' ) { |
||
| 188 | $button = \PodloveSubscribeButton\Model\NetworkButton::find_by_id( filter_input( INPUT_GET, 'button' ) ); |
||
| 189 | } else { |
||
| 190 | $button = \PodloveSubscribeButton\Model\Button::find_by_id( filter_input( INPUT_GET, 'button' ) ); |
||
| 191 | } |
||
| 192 | |||
| 193 | echo '<h2>' . sprintf( __( 'Edit Subscribe button: %s', 'podlove-subscribe-button' ), $button->title ) . '</h2>'; |
||
| 194 | self::form_template( $button, 'save' ); |
||
| 195 | } |
||
| 196 | |||
| 197 | public static function view_template() { |
||
| 198 | |||
| 199 | $is_network = is_network_admin(); |
||
| 200 | $table = new \PodloveSubscribeButton\Button_List_Table; |
||
| 201 | $table->prepare_items(); |
||
| 202 | $table->display(); |
||
| 203 | |||
| 204 | if ( $is_network ) { |
||
| 205 | // https://vedovini.net/2015/10/using-the-wordpress-settings-api-with-network-admin-pages/ |
||
| 206 | ?> |
||
| 207 | <form method="post" action="edit.php?action=podlove_psb_update_network_options"> |
||
| 208 | <?php |
||
| 209 | settings_fields( 'podlove-psb' ); |
||
| 210 | do_settings_sections( 'podlove-psb' ); |
||
| 211 | submit_button(); |
||
| 212 | ?> |
||
| 213 | </form> |
||
| 214 | <?php |
||
| 215 | } else { |
||
| 216 | ?> |
||
| 217 | <form method="post" action="options.php"> |
||
| 218 | <?php |
||
| 219 | settings_fields( 'podlove-psb' ); |
||
| 220 | do_settings_sections( 'podlove-psb' ); |
||
| 221 | submit_button(); |
||
| 222 | ?> |
||
| 223 | </form> |
||
| 224 | <?php } |
||
| 225 | } |
||
| 226 | |||
| 227 | private static function form_template( $button, $action ) { |
||
| 228 | // Enqueue Scripts for Media Manager |
||
| 229 | wp_enqueue_media(); |
||
| 230 | // Adjust if is_network |
||
| 231 | $is_network = is_network_admin(); |
||
| 232 | ?> |
||
| 233 | <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; ?>"> |
||
| 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 | <th scope="row"> |
||
| 239 | <label for="podlove_button_name"><?php _e( 'Button ID', 'podlove-subscribe-button' ); ?></label> |
||
| 240 | </th> |
||
| 241 | <td> |
||
| 242 | <input type="text" class="regular-text" id="podlove_button_name" name="podlove_button[name]" value="<?php echo $button->name; ?>" /> |
||
| 243 | <p class="description"><?php _e( 'The ID will be used as in internal identifier for shortcodes.', 'podlove-subscribe-button' ); ?></p> |
||
| 244 | </td> |
||
| 245 | </tr> |
||
| 246 | <tr> |
||
| 247 | <th scope="row"> |
||
| 248 | <label for="podlove_button_title"><?php _e( 'Podcast Title', 'podlove-subscribe-button' ); ?></label> |
||
| 249 | </th> |
||
| 250 | <td> |
||
| 251 | <input type="text" class="regular-text" id="podlove_button_title" name="podlove_button[title]" value="<?php echo $button->title; ?>" /> |
||
| 252 | </td> |
||
| 253 | </tr> |
||
| 254 | <tr> |
||
| 255 | <th scope="row"> |
||
| 256 | <label for="podlove_button_subtitle"><?php _e( 'Podcast Subtitle', 'podlove-subscribe-button' ); ?></label> |
||
| 257 | </th> |
||
| 258 | <td> |
||
| 259 | <input type="text" class="regular-text" id="podlove_button_subtitle" name="podlove_button[subtitle]" value="<?php echo $button->subtitle; ?>" /> |
||
| 260 | </td> |
||
| 261 | </tr> |
||
| 262 | <tr> |
||
| 263 | <th scope="row"> |
||
| 264 | <label for="podlove_button_description"><?php _e( 'Podcast Description', 'podlove-subscribe-button' ); ?></label> |
||
| 265 | </th> |
||
| 266 | <td> |
||
| 267 | <textarea class="autogrow" cols="40" rows="3" id="podlove_button_description" name="podlove_button[description]"><?php echo $button->description; ?></textarea> |
||
| 268 | </td> |
||
| 269 | </tr> |
||
| 270 | <tr> |
||
| 271 | <th scope="row"> |
||
| 272 | <label for="podlove-button-cover"><?php _e( 'Podcast Image URL', 'podlove-subscribe-button' ); ?></label> |
||
| 273 | </th> |
||
| 274 | <td> |
||
| 275 | <input type="text" class="regular-text" id="podlove-button-cover" name="podlove_button[cover]" value="<?php echo $button->cover; ?>" /> |
||
| 276 | <a id="Podlove_cover_image_select" class="button" href="#">Select</a> |
||
| 277 | <br /><img src="<?php echo $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 | <th scope="row"> |
||
| 290 | <label for="feeds_table"><?php _e( 'Podcast Feeds', 'podlove-subscribe-button' ); ?></label> |
||
| 291 | </th> |
||
| 292 | <td> |
||
| 293 | <table id="feeds_table" class="podlove_alternating widefat striped"> |
||
| 294 | <thead> |
||
| 295 | <tr> |
||
| 296 | <th scope="col" id="url" class="manage-column column-primary"><?php _e( 'URL', 'podlove-subscribe-button' ); ?></th> |
||
| 297 | <th scope="col" id="itunes_id" class="manage-column"><?php _e( 'iTunes feed ID', 'podlove-subscribe-button' ); ?></th> |
||
| 298 | <th scope="col" id="format" class="manage-column"><?php _e( 'Media format', 'podlove-subscribe-button' ); ?></th> |
||
| 299 | <th scope="col" id="action" class="manage-column"><?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\Defaults::media_types() as $id => $audio ) { |
||
| 326 | echo "<option value='" . $id . "'>" . $audio[ 'title' ] . "</option>\n"; |
||
| 327 | } |
||
| 328 | ?> |
||
| 329 | </select> |
||
| 330 | </td> |
||
| 331 | <td><span class="dashicons dashicons-trash clickable podlove-icon-remove"></span></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' ) { |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | |||
| 351 | public static function podlove_psb_update_network_options() { |
||
| 352 | |||
| 353 | //Verify Post Referring Page |
||
| 354 | check_admin_referer( 'podlove-psb-options' ); |
||
| 361 | } |
||
| 362 | |||
| 363 | } // END class |
||
| 364 |