Completed
Push — development ( 5ed9d2...646735 )
by
unknown
04:14
created

Sidebar_Manager::execute_action()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 2
dl 0
loc 19
rs 9.3222
c 0
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_fields_sidebar_default_options', 'crb_get_default_sidebar_options', -1 );
23
		}
24
25
		// Ajax listeners
26
		add_action( 'wp_ajax_carbon_fields_add_sidebar', array( $this, 'action_handler' ) );
27
		add_action( 'wp_ajax_carbon_fields_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 );
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;
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 enter a name for the sidebar.', 'carbon-fields' ) );
70
		}
71
72
		$result = new \WP_Error( 'unknown-action', __( 'Unknown action attempted.', 'carbon-fields' ) );
73
		switch ( $action ) {
74
			case 'carbon_fields_add_sidebar':
75
				$result = $this->add_sidebar( $name );
76
				break;
77
78
			case 'carbon_fields_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' ) );
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' ) );
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' ) );
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' ) );
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_fields_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_fields_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_fields_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' ),
182
				'enter_name_of_new_sidebar' => __( 'Please enter the name of the new sidebar:', 'carbon-fields' ),
183
				'remove_sidebar_confirmation' => __( 'Are you sure you wish to remove this sidebar?', 'carbon-fields' ),
184
			)
185
		);
186
	}
187
}
188