Completed
Push — master ( 48986a...95c69f )
by David
06:07
created

Wordlift_Admin_Settings_Page::admin_init()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 101
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 62
nc 3
nop 0
dl 0
loc 101
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
/**
3
 * Pages: Admin Settings.
4
 *
5
 * Handles the WordLift admin settings page.
6
 *
7
 * @since      3.11.0
8
 * @package    Wordlift
9
 * @subpackage Wordlift/admin
10
 */
11
12
/**
13
 * Define the {@link Wordlift_Admin_Settings_Page} class.
14
 *
15
 * @since      3.11.0
16
 * @package    Wordlift
17
 * @subpackage Wordlift/admin
18
 */
19
class Wordlift_Admin_Settings_Page extends Wordlift_Admin_Page {
20
21
	/**
22
	 * A {@link Wordlift_Entity_Service} instance.
23
	 *
24
	 * @since  3.11.0
25
	 * @access private
26
	 * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
27
	 */
28
	private $entity_service;
29
30
	/**
31
	 * A {@link Wordlift_Configuration_Service} instance.
32
	 *
33
	 * @since  3.11.0
34
	 * @access private
35
	 * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
36
	 */
37
	private $configuration_service;
38
39
	/**
40
	 * A {@link Wordlift_Admin_Input_Element} element renderer.
41
	 *
42
	 * @since  3.11.0
43
	 * @access private
44
	 * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer.
45
	 */
46
	private $input_element;
47
48
	/**
49
	 * A {@link Wordlift_Admin_Language_Select_Element} element renderer.
50
	 *
51
	 * @since  3.11.0
52
	 * @access private
53
	 * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer.
54
	 */
55
	private $language_select_element;
56
57
	/**
58
	 * A {@link Wordlift_Admin_Publisher_Element} element renderer.
59
	 *
60
	 * @since  3.11.0
61
	 * @access private
62
	 * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer.
63
	 */
64
	private $publisher_element;
65
66
	/**
67
	 * Create a {@link Wordlift_Admin_Settings_Page} instance.
68
	 *
69
	 * @since 3.11.0
70
	 *
71
	 * @param \Wordlift_Configuration_Service         $configuration_service
72
	 * @param \Wordlift_Entity_Service                $entity_service
73
	 * @param \Wordlift_Admin_Input_Element           $input_element
74
	 * @param \Wordlift_Admin_Language_Select_Element $language_select_element
75
	 * @param \Wordlift_Admin_Publisher_Element       $publisher_element
76
	 */
77
	function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $publisher_element ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
79
		$this->configuration_service = $configuration_service;
80
		$this->entity_service        = $entity_service;
81
82
		// Set a reference to the UI elements.
83
		$this->input_element           = $input_element;
84
		$this->language_select_element = $language_select_element;
85
		$this->publisher_element       = $publisher_element;
86
87
	}
88
89
	/**
90
	 * @inheritdoc
91
	 */
92
	function get_parent_slug() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
94
		return 'wl_admin_menu';
95
	}
96
97
	/**
98
	 * @inheritdoc
99
	 */
100
	function get_capability() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
101
102
		return 'manage_options';
103
	}
104
105
	/**
106
	 * @inheritdoc
107
	 */
108
	function get_page_title() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
109
110
		return 'WorldLift Settings';
111
	}
112
113
	/**
114
	 * @inheritdoc
115
	 */
116
	function get_menu_title() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
118
		return 'Settings';
119
	}
120
121
	/**
122
	 * @inheritdoc
123
	 */
124
	function get_menu_slug() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
125
126
		return 'wl_configuration_admin_menu';
127
	}
128
129
	/**
130
	 * @inheritdoc
131
	 */
132
	function get_partial_name() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
133
134
		return 'wordlift-admin-settings-page.php';
135
	}
136
137
	/**
138
	 * @inheritdoc
139
	 */
140
	public function enqueue_scripts() {
141
142
		// Enqueue the media scripts to be used for the publisher's logo selection.
143
		wp_enqueue_media();
144
145
		// JavaScript required for the settings page.
146
		// @todo: try to move to the `wordlift-admin.bundle.js`.
147
		wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-admin-settings-page.bundle.js', array( 'wp-util' ) );
148
149
	}
150
151
	/**
152
	 * Configure all the configuration parameters.
153
	 *
154
	 * Called by the *admin_init* hook.
155
	 *
156
	 * @since 3.11.0
157
	 */
158
	function admin_init() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
260
	/**
261
	 * Sanitize the configuration settings to be stored.
262
	 *
263
	 * If a new entity is being created for the publisher, create it and set The
264
	 * publisher setting.
265
	 *
266
	 * @since 3.11.0
267
	 *
268
	 * @param array $input The configuration settings array.
269
	 *
270
	 * @return array The sanitized input array.
271
	 */
272
	function sanitize_callback( $input ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
sanitize_callback uses the super-global variable $_POST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
273
274
		// Check whether a publisher name has been set.
275
		if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) {
276
			$name         = $_POST['wl_publisher']['name'];
277
			$type         = $_POST['wl_publisher']['type'];
278
			$thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null;
279
280
			// Set the type URI, either http://schema.org/Person or http://schema.org/Organization.
281
			$type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' );
282
283
			// Create an entity for the publisher and assign it to the input
284
			// parameter which WordPress automatically saves into the settings.
285
			$input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' );
286
		}
287
288
		return $input;
289
	}
290
291
}
292