Completed
Push — milestone/2.0 ( 8a1186...26a446 )
by
unknown
04:33
created

Theme_Options_Container::is_valid_save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Datastore\Datastore;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
/**
9
 * Theme options container class.
10
 */
11
class Theme_Options_Container extends Container {
12
	
13
	protected static $registered_pages = array();
14
15
	public $settings = array(
16
		'parent' => 'self',
17
		'file' => '',
18
		'permissions' => 'manage_options',
19
	);
20
21
	public $icon = '';
22
23
	/**
24
	 * Create a new container
25
	 *
26
	 * @param string $unique_id Unique id of the container
27
	 * @param string $title title of the container
28
	 * @param string $type Type of the container
29
	 **/
30 View Code Duplication
	public function __construct( $unique_id, $title, $type ) {
31
		parent::__construct( $unique_id, $title, $type );
32
33
		if ( ! $this->get_datastore() ) {
34
			$this->set_datastore( Datastore::make( 'theme_options' ), $this->has_default_datastore() );
35
		}
36
	}
37
38
	/**
39
	 * Attach container as a theme options page/subpage.
40
	 **/
41
	public function init() {
42
		if ( ! $this->settings['parent'] || $this->settings['parent'] == 'self' ) {
43
			$this->settings['parent'] = '';
44
		} else if ( strpos( $this->settings['parent'], '.php' ) === false ) {
45
			$clear_title = $this->clear_string( $this->settings['parent'] );
46
			$this->settings['parent'] = 'crbn-' . $clear_title . '.php';
47
		}
48
49
		if ( ! $this->settings['file'] ) {
50
			$clear_title = $this->clear_string( $this->title );
51
			$this->settings['file'] .= 'crbn-' . $clear_title . '.php';
52
		}
53
54
		$this->verify_unique_page();
55
56
		add_action( 'admin_menu', array( $this, '_attach' ) );
57
	}
58
59
	/**
60
	 * Perform checks whether the current save() request is valid.
61
	 *
62
	 * @return bool
63
	 **/
64
	public function is_valid_save() {
65
		return $this->verified_nonce_in_request();
66
	}
67
68
	/**
69
	 * Perform save operation after successful is_valid_save() check.
70
	 * The call is propagated to all fields in the container.
71
	 *
72
	 * @param mixed $user_data
73
	 **/
74
	public function save( $user_data = null ) {
75
		try {
76
			parent::save( $user_data );
77
		} catch ( Incorrect_Syntax_Exception $e ) {
78
			$this->errors[] = $e->getMessage();
79
		}
80
81
		do_action( 'carbon_after_save_theme_options', $user_data );
82
83
		if ( ! headers_sent() ) {
84
			wp_redirect( add_query_arg( array( 'settings-updated' => 'true' ) ) );
85
		}
86
	}
87
88
	/**
89
	 * Perform checks whether the container should be attached during the current request
90
	 *
91
	 * @return bool True if the container is allowed to be attached
92
	 **/
93
	public function is_valid_attach_for_request() {
94
		return true;
95
	}
96
97
	/**
98
	 * Check container attachment rules against object id
99
	 *
100
	 * @return bool
101
	 **/
102
	public function is_valid_attach_for_object( $object_id = null ) {
103
		return true;
104
	}
105
106
	/**
107
	 * Add theme options container pages.
108
	 * Hook the container saving action.
109
	 **/
110
	public function attach() {
111
112
		// Add menu page
113
		if ( ! $this->settings['parent'] ) {
114
			add_menu_page(
115
				$this->title,
116
				$this->title,
117
				$this->settings['permissions'],
118
				$this->settings['file'],
119
				array( $this, 'render' ),
120
				$this->icon
121
			);
122
		}
123
124
		add_submenu_page(
125
			$this->settings['parent'],
126
			$this->title,
127
			$this->title,
128
			$this->settings['permissions'],
129
			$this->settings['file'],
130
			array( $this, 'render' ),
131
			$this->icon
132
		);
133
134
		$page_hook = get_plugin_page_hookname( $this->settings['file'], '' );
135
		add_action( 'load-' . $page_hook, array( $this, '_save' ) );
136
	}
137
138
	/**
139
	 * Whether this container is currently viewed.
140
	 **/
141 View Code Duplication
	public function is_active() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
		$request_page = isset( $_GET['page'] ) ? $_GET['page'] : '';
1 ignored issue
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
143
		if ( ! empty( $request_page ) && $request_page === $this->settings['file'] ) {
144
			return true;
145
		}
146
147
		return false;
148
	}
149
150
	/**
151
	 * Output the container markup
152
	 **/
153
	public function render() {
154
		$request_settings_updated = isset( $_GET['settings-updated'] ) ? $_GET['settings-updated'] : '';
1 ignored issue
show
Comprehensibility Naming introduced by
The variable name $request_settings_updated exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
155
		if ( $request_settings_updated === 'true' ) {
156
			$this->notifications[] = __( 'Settings saved.', \Carbon_Fields\TEXT_DOMAIN );
157
		}
158
159
		include \Carbon_Fields\DIR . '/templates/Container/theme_options.php';
160
	}
161
162
	/**
163
	 * Make sure that there are no duplicate containers with the same name.
164
	 **/
165
	public function verify_unique_page() {
166
		$file = $this->settings['file'];
167
		$parent = $this->settings['parent'];
168
169
		// Register top level page
170
		if ( ! $parent ) {
171
			if ( isset( static::$registered_pages[ $file ] ) ) {
172
				Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" already registered' );
173
			}
174
175
			static::$registered_pages[ $file ] = array();
176
			return;
177
		}
178
179
		// Register sub-page
180
		if ( ! isset( static::$registered_pages[ $parent ] ) ) {
181
			static::$registered_pages[ $parent ] = array( $file );
182
		} elseif ( in_array( $file, static::$registered_pages[ $parent ] ) ) {
183
			Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" with parent "' . $parent . '" is already registered. Please set a name for the container.' );
184
		} else {
185
			static::$registered_pages[ $parent ][] = $file;
186
		}
187
	}
188
189
	/**
190
	 * Unregister the container parent and child pages.
191
	 **/
192
	public function drop_unique_page() {
193
		$file = $this->settings['file'];
194
		$parent = $this->settings['parent'];
195
196
		// Register top level page
197
		if ( ! $parent ) {
198
			if ( isset( static::$registered_pages[ $file ] ) && empty( static::$registered_pages[ $file ] ) ) {
199
				unset( static::$registered_pages[ $file ] );
200
			}
201
202
			return;
203
		}
204
205
		// Register sub-page
206
		if ( isset( static::$registered_pages[ $parent ] ) && in_array( $file, static::$registered_pages[ $parent ] ) ) {
207
208
			$index = array_search( $file, static::$registered_pages[ $parent ] );
209
			if ( $index !== false ) {
210
				unset( static::$registered_pages[ $parent ][ $index ] );
211
			}
212
		}
213
	}
214
215
	/**
216
	 * Sanitize the container title for use in
217
	 * the theme options file name.
218
	 **/
219
	protected function clear_string( $string ) {
220
		return preg_replace( array( '~ +~', '~[^\w\d-]+~u', '~-+~' ), array( '-', '-', '-' ), strtolower( remove_accents( $string ) ) );
221
	}
222
223
	/**
224
	 * COMMON USAGE METHODS
225
	 */
226
227
	/**
228
	 * Change the parent theme options page of this container
229
	 **/
230
	public function set_page_parent( $parent ) {
231
		if ( is_a( $parent, 'Carbon_Container' ) ) {
232
			$parent = $parent->title;
233
		}
234
235
		$this->settings['parent'] = $parent;
236
		return $this;
237
	}
238
239
	/**
240
	 * Set the icon of this theme options page.
241
	 * Applicable only for parent theme option pages.
242
	 **/
243
	public function set_icon( $icon ) {
244
		$this->icon = $icon;
245
		return $this;
246
	}
247
248
	/**
249
	 * Set the theme options file name of this container.
250
	 **/
251
	public function set_page_file( $file ) {
252
		$this->settings['file'] = $file;
253
		return $this;
254
	}
255
256
	/**
257
	 * Set the permissions necessary to view
258
	 * the corresponding theme options page
259
	 **/
260
	public function set_page_permissions( $permissions ) {
261
		$this->settings['permissions'] = $permissions;
262
		return $this;
263
	}
264
}
265
266