Conditions | 14 |
Paths | 640 |
Total Lines | 69 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
87 | public function manage_add_on_license() { |
||
88 | |||
89 | $addon = isset( $_POST['add_on'] ) ? sanitize_key( $_POST['add_on'] ) : false; |
||
90 | $action = isset( $_POST['license_action'] ) ? esc_attr( $_POST['license_action'] ) : false; |
||
91 | $key = isset( $_POST['license_key'] ) ? esc_attr( $_POST['license_key'] ) : ''; |
||
92 | $nonce = isset( $_POST['nonce'] ) ? esc_attr( $_POST['nonce'] ) : ''; |
||
93 | |||
94 | // Verify that there are valid variables to process. |
||
95 | if ( false === $addon || ! in_array( $action, array( 'activate_license', 'deactivate_license' ) ) ) { |
||
96 | wp_send_json_error( __( 'Add-on unspecified or invalid action.', 'google-calendar-events' ) ); |
||
97 | } |
||
98 | |||
99 | // Verify this request comes from the add-ons licenses activation settings page. |
||
100 | if ( ! wp_verify_nonce( $nonce, 'simcal_license_manager' ) ) { |
||
101 | wp_send_json_error( sprintf( __( 'An error occurred: %s', 'google-calendar-events' ), 'Nonce verification failed.' ) ); |
||
102 | } |
||
103 | |||
104 | // Removes the prefix and converts simcal_{id_no} to {id_no}. |
||
105 | $id = intval( substr( $addon, 7 ) ); |
||
106 | |||
107 | // Data to send in API request. |
||
108 | $api_request = array( |
||
109 | 'edd_action' => $action, |
||
110 | 'license' => $key, |
||
111 | 'item_id' => urlencode( $id ), |
||
112 | 'url' => home_url() |
||
113 | ); |
||
114 | |||
115 | // Call the custom API. |
||
116 | $response = wp_remote_post( |
||
117 | defined( 'SIMPLE_CALENDAR_STORE_URL' ) ? SIMPLE_CALENDAR_STORE_URL : simcal_get_url( 'home' ), |
||
118 | array( |
||
119 | 'timeout' => 15, |
||
120 | 'sslverify' => false, |
||
121 | 'body' => $api_request |
||
122 | ) |
||
123 | ); |
||
124 | |||
125 | // Update license in db. |
||
126 | $keys = get_option( 'simple-calendar_settings_licenses', array() ); |
||
127 | $keys['keys'][ $addon ] = $key; |
||
128 | update_option( 'simple-calendar_settings_licenses', $keys ); |
||
129 | |||
130 | // Make sure there is a response. |
||
131 | if ( is_wp_error( $response ) ) { |
||
132 | wp_send_json_error( sprintf( __( 'There was an error processing your request: %s', 'google-calendar-events' ), $response->get_error_message() ) ); |
||
133 | } |
||
134 | |||
135 | // Decode the license data and save. |
||
136 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
||
137 | $status = simcal_get_license_status(); |
||
138 | |||
139 | if ( ! empty( $license_data ) ) { |
||
140 | if ('deactivated' == $license_data->license) { |
||
141 | unset($status[$addon]); |
||
142 | update_option('simple-calendar_licenses_status', $status); |
||
143 | wp_send_json_success($license_data->license); |
||
144 | } elseif (in_array($license_data->license, array('valid', 'invalid'))) { |
||
145 | $status[$addon] = $license_data->license; |
||
146 | update_option('simple-calendar_licenses_status', $status); |
||
147 | $message = 'valid' == $license_data->license ? 'valid' : __('License key is invalid.', 'google-calendar-events'); |
||
148 | wp_send_json_success($message); |
||
149 | } else { |
||
150 | wp_send_json_error( '' ); |
||
151 | } |
||
152 | } else { |
||
153 | wp_send_json_error( __( 'An error has occurred, please try again.', 'google-calendar-events' ) ); |
||
154 | } |
||
155 | } |
||
156 | |||
178 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: