| Conditions | 13 |
| Paths | 20 |
| Total Lines | 86 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 33 | function handle_extension_action() { |
||
| 34 | $action = safe_get_from_request( 'action' ); |
||
| 35 | $extension_slug = safe_get_from_request( 'extension' ); |
||
| 36 | $has_error = safe_get_from_request( 'has_error' ); |
||
| 37 | |||
| 38 | if ( ( 'download' === $action || 'activate' === $action || 'deactivate' === $action ) && $extension_slug ) { |
||
| 39 | $api = new FooGallery_Extensions_API(); |
||
| 40 | |||
| 41 | $fatal_error_redirect = remove_query_arg( 'action' ); |
||
| 42 | wp_redirect( add_query_arg( 'has_error', 'yes', $fatal_error_redirect ) ); // we'll override this later if the plugin can be included without fatal error |
||
| 43 | ob_start(); |
||
| 44 | |||
| 45 | switch ( $action ) { |
||
| 46 | case 'download': |
||
| 47 | $result = $api->download( $extension_slug ); |
||
| 48 | break; |
||
| 49 | case 'activate': |
||
| 50 | $result = $api->activate( $extension_slug ); |
||
| 51 | break; |
||
| 52 | case 'deactivate': |
||
| 53 | $result = $api->deactivate( $extension_slug ); |
||
| 54 | break; |
||
| 55 | } |
||
| 56 | |||
| 57 | //if we get here then no fatal error - cool! |
||
| 58 | ob_end_clean(); |
||
| 59 | |||
| 60 | //store the result in a short-lived transient |
||
| 61 | if ( isset($result) ) { |
||
| 62 | set_transient( FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY, $result, 30 ); |
||
| 63 | } |
||
| 64 | |||
| 65 | //first, remove unwanted query args |
||
| 66 | $redirect_url = remove_query_arg( array( 'extension', 'action' ) ); |
||
| 67 | //then add a query arg for our message |
||
| 68 | $redirect_url = add_query_arg( 'show_message', 'yes', $redirect_url ); |
||
| 69 | //finally, allow extensions to override their own redirect |
||
| 70 | $redirect_url = apply_filters( 'foogallery_extensions_redirect_url-' . $extension_slug, $redirect_url, $action ); |
||
| 71 | |||
| 72 | //redirect to this page, so the plugin can be properly activated/deactivated etc |
||
| 73 | if ( $redirect_url ) { |
||
| 74 | wp_redirect( $redirect_url ); |
||
| 75 | die(); |
||
| 76 | } |
||
| 77 | } else if ( 'reload' === $action ) { |
||
| 78 | $api = new FooGallery_Extensions_API(); |
||
| 79 | $api->reload(); |
||
| 80 | |||
| 81 | //first, remove unwanted query args |
||
| 82 | $redirect_url = remove_query_arg( array( 'extension', 'action' ) ); |
||
| 83 | |||
| 84 | if ( ! $api->has_extension_loading_errors() ) { |
||
| 85 | $result = array( |
||
| 86 | 'message' => __( 'The extensions have been reloaded', 'foogallery' ), |
||
| 87 | 'type' => 'success', |
||
| 88 | ); |
||
| 89 | |||
| 90 | set_transient( FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY, $result, 30 ); |
||
| 91 | |||
| 92 | //then add a query arg for our message |
||
| 93 | $redirect_url = add_query_arg( 'show_message', 'yes', $redirect_url ); |
||
| 94 | } |
||
| 95 | |||
| 96 | wp_redirect( $redirect_url ); |
||
| 97 | die(); |
||
| 98 | } else if ( $has_error ) { |
||
| 99 | $api = new FooGallery_Extensions_API(); |
||
| 100 | $api->deactivate( $extension_slug, true, false ); |
||
| 101 | |||
| 102 | $result = array( |
||
| 103 | 'message' => __( 'The extension could not be activated due to an error!', 'foogallery' ), |
||
| 104 | 'type' => 'error', |
||
| 105 | ); |
||
| 106 | |||
| 107 | set_transient( FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY, $result, 30 ); |
||
| 108 | |||
| 109 | $api->add_to_error_extensions( $extension_slug, __( 'Activation Error!', 'foogallery' ) ); |
||
| 110 | |||
| 111 | //first, remove unwanted query args |
||
| 112 | $redirect_url = remove_query_arg( array( 'extension', 'action', 'has_error' ) ); |
||
| 113 | //then add a query arg for our message |
||
| 114 | $redirect_url = add_query_arg( 'show_message', 'yes', $redirect_url ); |
||
| 115 | |||
| 116 | wp_redirect( $redirect_url ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 139 |