Completed
Push — milestone/2.0 ( e0608a...f004ab )
by
unknown
04:47
created

Sidebar_Manager::action_handler()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
nc 4
nop 0
dl 0
loc 25
rs 8.8571
c 4
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Libraries\Sidebar_Manager;
4
5
/**
6
 * This class is responsible for handling custom sidebars.
7
 */
8
class Sidebar_Manager {
9
10
	/**
11
	 * Register actions, filters, etc...
12
	 */
13
	public function boot() {
14
		// Register the custom sidebars
15
		add_action( 'widgets_init', array( $this, 'register_sidebars' ), 100 );
16
17
		// Enqueue the UI scripts on the widgets page
18
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
19
20
		// Set the default options
21
		if ( function_exists( 'crb_get_default_sidebar_options' ) ) {
22
			add_filter( 'carbon_custom_sidebar_default_options', 'crb_get_default_sidebar_options', -1 );
23
		}
24
25
		// Ajax listeners
26
		add_action( 'wp_ajax_carbon_add_sidebar', array( $this, 'action_handler' ) );
27
		add_action( 'wp_ajax_carbon_remove_sidebar', array( $this, 'action_handler' ) );
28
	}
29
30
	/**
31
	 * Handle action requests.
32
	 */
33
	public function action_handler() {
34
		$response = array(
35
			'success' => false,
36
			'error' => null,
37
			'errorCode' => null,
38
			'data' => null,
39
		);
40
41
		$input = stripslashes_deep( $_POST );
1 ignored issue
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
42
		$action = isset( $input['action'] ) ? $input['action'] : '';
43
		
44
		$result = $this->execute_action( $action, $input );
45
46
		if ( is_wp_error( $result ) ) {
47
			$response['success'] = false;
48
			$response['error'] = $result->get_error_message();
49
			$response['errorCode'] = $result->get_error_code();
50
		} else {
51
			$response['success'] = true;
52
			$response['data'] = $result;
53
		}
54
55
		wp_send_json( $response );
56
		exit;
1 ignored issue
show
Coding Style Compatibility introduced by
The method action_handler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
57
	}
58
59
	/**
60
	 * Execute an action
61
	 *
62
	 * @param string $action
63
	 * @param array $input
64
	 * @return mixed
65
	 */
66
	public function execute_action( $action, $input ) {
67
		$name = isset( $input['name'] ) ? $input['name'] : '';
68
		if ( empty( $name ) ) {
69
			return new \WP_Error( 'name-missing', __( 'Please pass a name for the sidebar.', \Carbon_Fields\TEXT_DOMAIN ) );
70
		}
71
72
		$result = new \WP_Error( 'unknown-action', __( 'Unknown action attempted.', \Carbon_Fields\TEXT_DOMAIN ) );
73
		switch ( $action ) {
74
			case 'carbon_add_sidebar':
75
				$result = $this->add_sidebar( $name );
76
				break;
77
78
			case 'carbon_remove_sidebar':
79
				$result = $this->remove_sidebar( $name );
80
				break;
81
		}
82
83
		return $result;
84
	}
85
86
	/**
87
	 * Add a new custom sidebar.
88
	 *
89
	 * @see Sidebar_Manager::register_sidebars()
90
	 * @param string $id Sidebar ID
91
	 * @param string $name Sidebar Name
92
	 * @return bool|WP_Error
93
	 */
94
	public function add_sidebar( $name, $id = '' ) {
95
		$registered_sidebars = $this->get_sidebars();
96
		$id = $id ? $id : $name;
97
98
		// Sanitize the sidebar ID the same way as dynamic_sidebar()
99
		$id = sanitize_title( $id );
100
101
		if ( isset( $registered_sidebars[ $id ] ) ) {
102
			return new \WP_Error( 'sidebar-exists', __( 'Sidebar with the same ID is already registered.', \Carbon_Fields\TEXT_DOMAIN ) );
103
		}
104
105
		$registered_sidebars[ $id ] = array(
106
			'id' => $id,
107
			'name' => $name,
108
		);
109
110
		$success = update_option( 'carbon_custom_sidebars', $registered_sidebars );
111
		if ( ! $success ) {
112
			return new \WP_Error( 'update-failed', __( 'Failed to update option storing your custom sidebars. Please contact support.', \Carbon_Fields\TEXT_DOMAIN ) );
113
		}
114
115
		return array(
116
			'id' => $id,
117
			'name' => $name,
118
		);
119
	}
120
121
	/**
122
	 * Remove a custom sidebar by ID.
123
	 *
124
	 * @see Sidebar_Manager::register_sidebars()
125
	 * @param string $id Sidebar ID
126
	 * @return bool|WP_Error
127
	 */
128
	public function remove_sidebar( $id ) {
129
		$registered_sidebars = $this->get_sidebars();
130
131
		// Sanitize the sidebar ID the same way as dynamic_sidebar()
132
		$id = sanitize_title( $id );
133
134
		if ( isset( $registered_sidebars[ $id ] ) ) {
135
			unset( $registered_sidebars[ $id ] );
136
		} else {
137
			return new \WP_Error( 'sidebar-not-found', __( 'Sidebar not found.', \Carbon_Fields\TEXT_DOMAIN ) );
138
		}
139
140
		$success = update_option( 'carbon_custom_sidebars', $registered_sidebars );
141
		if ( ! $success ) {
142
			return new \WP_Error( 'update-failed', __( 'Failed to update option storing your custom sidebars. Please contact support.', \Carbon_Fields\TEXT_DOMAIN ) );
143
		}
144
145
		return $success;
146
	}
147
148
	/**
149
	 * Get all the registered custom sidebars.
150
	 *
151
	 * @return array
152
	 */
153
	public function get_sidebars() {
154
		return apply_filters( 'carbon_custom_sidebars', get_option( 'carbon_custom_sidebars', array() ) );
155
	}
156
157
	/**
158
	 * Register the custom sidebars.
159
	 */
160
	public function register_sidebars() {
161
		$registered_sidebars = $this->get_sidebars();
162
		$default_options = apply_filters( 'carbon_custom_sidebar_default_options', array() );
163
164
		foreach ( $registered_sidebars as $id => $options ) {
165
			$options['class'] = 'carbon-sidebar';
166
			$options = wp_parse_args( $options, $default_options );
167
			$options = apply_filters( 'carbon_custom_sidebar_options', $options, $id );
168
169
			register_sidebar( $options );
170
		}
171
	}
172
173
	/**
174
	 * Enqueue the UI scripts.
175
	 */
176
	public function enqueue_scripts() {
177
		wp_enqueue_style( 'carbon-sidebar-manager', \Carbon_Fields\URL . '/core/Libraries/Sidebar_Manager/assets/css/app.css', array(), \Carbon_Fields\VERSION );
178
		wp_enqueue_script( 'carbon-sidebar-manager', \Carbon_Fields\URL . '/core/Libraries/Sidebar_Manager/assets/js/app.js', array(), \Carbon_Fields\VERSION );
179
		wp_localize_script( 'carbon-sidebar-manager', 'crbSidebarl10n',
180
			array(
181
				'add_sidebar' => __( 'Add Sidebar', \Carbon_Fields\TEXT_DOMAIN ),
182
				'enter_name_of_new_sidebar' => __( 'Please enter the name of the new sidebar:', \Carbon_Fields\TEXT_DOMAIN ),
183
				'remove_sidebar_confirmation' => __( 'Are you sure you wish to remove this sidebar?', \Carbon_Fields\TEXT_DOMAIN ),
184
			)
185
		);
186
	}
187
}
188