Completed
Push — milestone/2.0 ( 9cc217...20a31b )
by
unknown
02:47
created

Sidebar_Manager::add_sidebar()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 2
dl 0
loc 18
rs 9.4285
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_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
	 * @return array|void Output JSON if DOING_AJAX, otherwise return an array
34
	 */
35
	public function action_handler() {
36
		$response = array(
37
			'success' => false,
38
			'error' => null,
39
		);
40
41
		$action = isset( $_POST['action'] ) ? stripslashes_deep( $_POST['action'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
42
		$name = isset( $_POST['name'] ) ? stripslashes_deep( $_POST['name'] ) : '';
1 ignored issue
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
43
44
		if ( empty( $action ) || empty( $name ) ) {
45
			return false;
46
		}
47
48
		$result = false;
49
		switch ( $action ) {
50
			case 'carbon_add_sidebar':
51
				$result = $this->add_sidebar( $name );
52
				break;
53
54
			case 'carbon_remove_sidebar':
55
				$result = $this->remove_sidebar( $name );
56
				break;
57
58
			default:
59
				$result = new \WP_Error( 'unknown-action', __( 'Unknown action attempted.', \Carbon_Fields\TEXT_DOMAIN ) );
60
				break;
61
		}
62
63
		if ( is_wp_error( $result ) ) {
64
			$response['error'] = $result->get_error_message();
65
		} else {
66
			$response['success'] = (bool) $result;
67
		}
68
69
		wp_send_json( $response );
70
		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...
71
	}
72
73
	/**
74
	 * Add a new custom sidebar.
75
	 *
76
	 * @see Sidebar_Manager::register_sidebars()
77
	 * @param string $id Sidebar ID
78
	 * @param string $name Sidebar Name
79
	 * @return bool|WP_Error
80
	 */
81
	public function add_sidebar( $name, $id = '' ) {
82
		$registered_sidebars = $this->get_sidebars();
83
		$id = $id ? $id : $name;
84
85
		// Sanitize the sidebar ID the same way as dynamic_sidebar()
86
		$id = sanitize_title( $id );
87
88
		if ( isset( $registered_sidebars[ $id ] ) ) {
89
			return new \WP_Error( 'sidebar-exists', __( 'Sidebar with the same ID is already registered.', \Carbon_Fields\TEXT_DOMAIN ) );
90
		}
91
92
		$registered_sidebars[ $id ] = array(
93
			'id' => $id,
94
			'name' => $name,
95
		);
96
97
		return update_option( 'carbon_custom_sidebars', $registered_sidebars );
98
	}
99
100
	/**
101
	 * Remove a custom sidebar by ID.
102
	 *
103
	 * @see Sidebar_Manager::register_sidebars()
104
	 * @param string $id Sidebar ID
105
	 * @return bool|WP_Error
106
	 */
107
	public function remove_sidebar( $id ) {
108
		$registered_sidebars = $this->get_sidebars();
109
110
		// Sanitize the sidebar ID the same way as dynamic_sidebar()
111
		$id = sanitize_title( $id );
112
113
		if ( isset( $registered_sidebars[ $id ] ) ) {
114
			unset( $registered_sidebars[ $id ] );
115
		} else {
116
			return new \WP_Error( 'sidebar-not-found', __( 'Sidebar not found.', \Carbon_Fields\TEXT_DOMAIN ) );
117
		}
118
119
		return update_option( 'carbon_custom_sidebars', $registered_sidebars );
120
	}
121
122
	/**
123
	 * Get all the registered custom sidebars.
124
	 *
125
	 * @return array
126
	 */
127
	public function get_sidebars() {
128
		return apply_filters( 'carbon_custom_sidebars', get_option( 'carbon_custom_sidebars', array() ) );
129
	}
130
131
	/**
132
	 * Register the custom sidebars.
133
	 */
134
	public function register_sidebars() {
135
		$registered_sidebars = $this->get_sidebars();
136
		$default_options = apply_filters( 'carbon_custom_sidebar_default_options', array() );
137
138
		foreach ( $registered_sidebars as $id => $options ) {
139
			$options['class'] = 'carbon-sidebar';
140
			$options = wp_parse_args( $options, $default_options );
141
			$options = apply_filters( 'carbon_custom_sidebar_options', $options, $id );
142
143
			register_sidebar( $options );
144
		}
145
	}
146
147
	/**
148
	 * Enqueue the UI scripts.
149
	 */
150
	public function enqueue_scripts() {
151
		wp_enqueue_style( 'carbon-sidebar-manager', \Carbon_Fields\URL . '/core/Libraries/Sidebar_Manager/assets/css/app.css', array(), \Carbon_Fields\VERSION );
152
		wp_enqueue_script( 'carbon-sidebar-manager', \Carbon_Fields\URL . '/core/Libraries/Sidebar_Manager/assets/js/app.js', array(), \Carbon_Fields\VERSION );
153
		wp_localize_script( 'carbon-sidebar-manager', 'crbSidebarl10n',
154
			array(
155
				'add_sidebar' => __( 'Add Sidebar', \Carbon_Fields\TEXT_DOMAIN ),
156
				'enter_name_of_new_sidebar' => __( 'Please enter the name of the new sidebar:', \Carbon_Fields\TEXT_DOMAIN ),
157
				'remove_sidebar_confirmation' => __( 'Are you sure you wish to remove this sidebar?', \Carbon_Fields\TEXT_DOMAIN ),
158
			)
159
		);
160
	}
161
}
162