| Conditions | 3 |
| Paths | 3 |
| Total Lines | 101 |
| Code Lines | 62 |
| 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 |
||
| 158 | function admin_init() { |
||
| 159 | |||
| 160 | // Register WordLift's general settings, providing our own sanitize callback |
||
| 161 | // which will also check whether the user filled the WL Publisher form. |
||
| 162 | register_setting( |
||
| 163 | 'wl_general_settings', |
||
| 164 | 'wl_general_settings', |
||
| 165 | array( $this, 'sanitize_callback', ) |
||
| 166 | ); |
||
| 167 | |||
| 168 | // Add the general settings section. |
||
| 169 | add_settings_section( |
||
| 170 | 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
||
| 171 | '', // Section header. |
||
| 172 | '', // Callback used to render the description of the section. |
||
| 173 | 'wl_general_settings' // Page on which to add this section of options. |
||
| 174 | ); |
||
| 175 | |||
| 176 | $key_args = array( |
||
| 177 | 'id' => 'wl-key', |
||
| 178 | 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
||
| 179 | 'value' => $this->configuration_service->get_key(), |
||
| 180 | 'description' => _x( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ), |
||
| 181 | ); |
||
| 182 | |||
| 183 | // Set the class for the key field based on the validity of the key. |
||
| 184 | // Class should be "untouched" for an empty (virgin) value, "valid" |
||
| 185 | // if the key is valid, or "invalid" otherwise. |
||
| 186 | $validation_service = new Wordlift_Key_Validation_Service(); |
||
| 187 | |||
| 188 | if ( empty( $key_args['value'] ) ) { |
||
| 189 | $key_args['css_class'] = 'untouched'; |
||
| 190 | } elseif ( $validation_service->is_valid( $key_args['value'] ) ) { |
||
| 191 | $key_args['css_class'] = 'valid'; |
||
| 192 | } else { |
||
| 193 | $key_args['css_class'] = 'invalid'; |
||
| 194 | } |
||
| 195 | |||
| 196 | // Add the `key` field. |
||
| 197 | add_settings_field( |
||
| 198 | 'wl-key', // Element id used to identify the field throughout the theme. |
||
| 199 | _x( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
||
| 200 | // The name of the function responsible for rendering the option interface. |
||
| 201 | array( $this->input_element, 'render', ), |
||
| 202 | 'wl_general_settings', // The page on which this option will be displayed. |
||
| 203 | 'wl_general_settings_section', // The name of the section to which this field belongs. |
||
| 204 | $key_args // The array of arguments to pass to the callback. In this case, just a description. |
||
| 205 | ); |
||
| 206 | |||
| 207 | // Entity Base Path input. |
||
| 208 | $entity_base_path_args = array( |
||
| 209 | // The array of arguments to pass to the callback. In this case, just a description. |
||
| 210 | 'id' => 'wl-entity-base-path', |
||
| 211 | 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
||
| 212 | 'value' => $this->configuration_service->get_entity_base_path(), |
||
| 213 | 'description' => sprintf( _x( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
||
| 214 | ); |
||
| 215 | |||
| 216 | $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
||
| 217 | |||
| 218 | // Add the `wl_entity_base_path` field. |
||
| 219 | add_settings_field( |
||
| 220 | 'wl-entity-base-path', // ID used to identify the field throughout the theme |
||
| 221 | _x( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
||
| 222 | // The name of the function responsible for rendering the option interface |
||
| 223 | array( $this->input_element, 'render', ), |
||
| 224 | 'wl_general_settings', // The page on which this option will be displayed |
||
| 225 | 'wl_general_settings_section', // The name of the section to which this field belongs |
||
| 226 | $entity_base_path_args |
||
| 227 | ); |
||
| 228 | |||
| 229 | // Add the `language_name` field. |
||
| 230 | add_settings_field( |
||
| 231 | 'wl-site-language', |
||
| 232 | _x( 'Site Language', 'wordlift' ), |
||
| 233 | array( $this->language_select_element, 'render' ), |
||
| 234 | 'wl_general_settings', |
||
| 235 | 'wl_general_settings_section', |
||
| 236 | array( |
||
| 237 | // The array of arguments to pass to the callback. In this case, just a description. |
||
| 238 | 'id' => 'wl-site-language', |
||
| 239 | 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
||
| 240 | 'value' => $this->configuration_service->get_language_code(), |
||
| 241 | 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
||
| 242 | ) |
||
| 243 | ); |
||
| 244 | |||
| 245 | // Add the `publisher` field. |
||
| 246 | add_settings_field( |
||
| 247 | 'wl-publisher-id', |
||
| 248 | _x( 'Publisher', 'wordlift' ), |
||
| 249 | array( $this->publisher_element, 'render' ), |
||
| 250 | 'wl_general_settings', |
||
| 251 | 'wl_general_settings_section', |
||
| 252 | array( |
||
| 253 | 'id' => 'wl-publisher-id', |
||
| 254 | 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
||
| 255 | ) |
||
| 256 | ); |
||
| 257 | |||
| 258 | } |
||
| 259 | |||
| 292 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.