Completed
Push — milestone/2_0/react-ui ( feeecc...e46ba4 )
by
unknown
07:22
created

Theme_Options_Container::init()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 0
dl 0
loc 15
ccs 0
cts 12
cp 0
crap 20
rs 9.2
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' => '',
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'] !== '' && strpos( $this->settings['parent'], '.php' ) === false ) {
43
			$clear_title = $this->clear_string( $this->settings['parent'] );
44
			$this->settings['parent'] = 'crbn-' . $clear_title . '.php';
45
		}
46
47
		if ( ! $this->settings['file'] ) {
48
			$clear_title = $this->clear_string( $this->title );
49
			$this->settings['file'] .= 'crbn-' . $clear_title . '.php';
50
		}
51
52
		$this->verify_unique_page();
53
54
		add_action( 'admin_menu', array( $this, '_attach' ) );
55
	}
56
57
	/**
58
	 * Perform checks whether the current save() request is valid.
59
	 *
60
	 * @return bool
61
	 **/
62
	public function is_valid_save() {
63
		return $this->verified_nonce_in_request();
64
	}
65
66
	/**
67
	 * Perform save operation after successful is_valid_save() check.
68
	 * The call is propagated to all fields in the container.
69
	 *
70
	 * @param mixed $user_data
71
	 **/
72
	public function save( $user_data = null ) {
73
		try {
74
			parent::save( $user_data );
75
		} catch ( Incorrect_Syntax_Exception $e ) {
76
			$this->errors[] = $e->getMessage();
77
		}
78
79
		do_action( 'carbon_after_save_theme_options', $user_data );
80
81
		if ( ! headers_sent() ) {
82
			wp_redirect( add_query_arg( array( 'settings-updated' => 'true' ) ) );
83
		}
84
	}
85
86
	/**
87
	 * Perform checks whether the container should be attached during the current request
88
	 *
89
	 * @return bool True if the container is allowed to be attached
90
	 **/
91
	public function is_valid_attach_for_request() {
92
		return true;
93
	}
94
95
	/**
96
	 * Check container attachment rules against object id
97
	 *
98
	 * @param int $object_id
99
	 * @return bool
100
	 **/
101
	public function is_valid_attach_for_object( $object_id = null ) {
102
		return true;
103
	}
104
105
	/**
106
	 * Add theme options container pages.
107
	 * Hook the container saving action.
108
	 **/
109
	public function attach() {
110
111
		// Add menu page
112
		if ( ! $this->settings['parent'] ) {
113
			add_menu_page(
114
				$this->title,
115
				$this->title,
116
				$this->settings['permissions'],
117
				$this->settings['file'],
118
				array( $this, 'render' ),
119
				$this->icon
120
			);
121
		}
122
123
		add_submenu_page(
124
			$this->settings['parent'],
125
			$this->title,
126
			$this->title,
127
			$this->settings['permissions'],
128
			$this->settings['file'],
129
			array( $this, 'render' ),
130
			$this->icon
131
		);
132
133
		$page_hook = get_plugin_page_hookname( $this->settings['file'], '' );
134
		add_action( 'load-' . $page_hook, array( $this, '_save' ) );
135
	}
136
137
	/**
138
	 * Whether this container is currently viewed.
139
	 **/
140 View Code Duplication
	public function should_activate() {
141
		$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...
142
		if ( ! empty( $request_page ) && $request_page === $this->settings['file'] ) {
143
			return true;
144
		}
145
146
		return false;
147
	}
148
149
	/**
150
	 * Output the container markup
151
	 **/
152
	public function render() {
153
		$request_settings_updated = isset( $_GET['settings-updated'] ) ? $_GET['settings-updated'] : '';
0 ignored issues
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...
154
		if ( $request_settings_updated === 'true' ) {
155
			$this->notifications[] = __( 'Settings saved.', \Carbon_Fields\TEXT_DOMAIN );
156
		}
157
158
		include \Carbon_Fields\DIR . '/templates/Container/theme_options.php';
159
	}
160
161
	/**
162
	 * Make sure that there are no duplicate containers with the same name.
163
	 **/
164
	public function verify_unique_page() {
165
		$file = $this->settings['file'];
166
		$parent = $this->settings['parent'];
167
168
		if ( ! $parent ) {
169
			// Register top level page
170
			if ( isset( static::$registered_pages[ $file ] ) ) {
171
				Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" already registered' );
172
			}
173
174
			static::$registered_pages[ $file ] = array();
175
			return;
176
		}
177
178
		// Register sub-page
179
		if ( ! isset( static::$registered_pages[ $parent ] ) ) {
180
			static::$registered_pages[ $parent ] = array();
181
		}
182
183
		if ( in_array( $file, static::$registered_pages[ $parent ] ) ) {
184
			Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" with parent "' . $parent . '" is already registered. Please set a name for the container.' );
185
		}
186
187
		static::$registered_pages[ $parent ][] = $file;
188
	}
189
190
	/**
191
	 * Sanitize the container title for use in
192
	 * the theme options file name.
193
	 **/
194
	protected function clear_string( $string ) {
195
		return preg_replace( array( '~ +~', '~[^\w\d-]+~u', '~-+~' ), array( '-', '-', '-' ), strtolower( remove_accents( $string ) ) );
196
	}
197
198
	/**
199
	 * COMMON USAGE METHODS
200
	 */
201
202
	/**
203
	 * Change the parent theme options page of this container
204
	 **/
205
	public function set_page_parent( $parent ) {
206
		if ( is_a( $parent, 'Carbon_Container' ) ) {
207
			$parent = $parent->title;
208
		}
209
210
		$this->settings['parent'] = $parent;
211
		return $this;
212
	}
213
214
	/**
215
	 * Set the icon of this theme options page.
216
	 * Applicable only for parent theme option pages.
217
	 **/
218
	public function set_icon( $icon ) {
219
		$this->icon = $icon;
220
		return $this;
221
	}
222
223
	/**
224
	 * Set the theme options file name of this container.
225
	 **/
226
	public function set_page_file( $file ) {
227
		$this->settings['file'] = $file;
228
		return $this;
229
	}
230
231
	/**
232
	 * Set the permissions necessary to view
233
	 * the corresponding theme options page
234
	 **/
235
	public function set_page_permissions( $permissions ) {
236
		$this->settings['permissions'] = $permissions;
237
		return $this;
238
	}
239
}
240
241