Completed
Push — milestone/2.0 ( 77f3ad...371be2 )
by
unknown
05:56
created

Sidebar_Manager::get_sidebars()   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\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'] ) ? $_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'] ) ? $_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
		$result = false;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
45
		if ( empty( $action ) || empty( $name ) ) {
46
			return false;
47
		}
48
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
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
70
			wp_send_json( $response );
71
		} else {
72
			return $response;
73
		}
74
	}
75
76
	/**
77
	 * Add a new custom sidebar.
78
	 *
79
	 * @see Sidebar_Manager::register_sidebars()
80
	 * @param string $id Sidebar ID
81
	 * @param string $name Sidebar Name
82
	 * @return bool|WP_Error
83
	 */
84
	public function add_sidebar( $name, $id = '' ) {
85
		$registered_sidebars = $this->get_sidebars();
86
		$id = $id ? $id : $name;
87
88
		// Sanitize the sidebar ID the same way as dynamic_sidebar()
89
		$id = sanitize_title( $id );
90
91
		if ( isset( $registered_sidebars[ $id ] ) ) {
92
			return new \WP_Error( 'sidebar-exists', __( 'Sidebar with the same ID is already registered.', \Carbon_Fields\TEXT_DOMAIN ) );
93
		}
94
95
		$registered_sidebars[ $id ] = array(
96
			'id' => $id,
97
			'name' => $name,
98
		);
99
100
		return update_option( 'carbon_custom_sidebars', $registered_sidebars );
101
	}
102
103
	/**
104
	 * Remove a custom sidebar by ID.
105
	 *
106
	 * @see Sidebar_Manager::register_sidebars()
107
	 * @param string $id Sidebar ID
108
	 * @return bool|WP_Error
109
	 */
110
	public function remove_sidebar( $id ) {
111
		$registered_sidebars = $this->get_sidebars();
112
113
		// Sanitize the sidebar ID the same way as dynamic_sidebar()
114
		$id = sanitize_title( $id );
115
116
		if ( isset( $registered_sidebars[ $id ] ) ) {
117
			unset( $registered_sidebars[ $id ] );
118
		} else {
119
			return new \WP_Error( 'sidebar-not-found', __( 'Sidebar not found.', \Carbon_Fields\TEXT_DOMAIN ) );
120
		}
121
122
		return update_option( 'carbon_custom_sidebars', $registered_sidebars );
123
	}
124
125
	/**
126
	 * Get all the registered custom sidebars.
127
	 *
128
	 * @return array
129
	 */
130
	public function get_sidebars() {
131
		return apply_filters( 'carbon_custom_sidebars', get_option( 'carbon_custom_sidebars', array() ) );
132
	}
133
134
	/**
135
	 * Register the custom sidebars.
136
	 */
137
	public function register_sidebars() {
138
		$registered_sidebars = $this->get_sidebars();
139
		$default_options = apply_filters( 'carbon_custom_sidebar_default_options', array() );
140
141
		foreach ( $registered_sidebars as $id => $options ) {
142
			$options['class'] = 'carbon-sidebar';
143
			$options = wp_parse_args( $options, $default_options );
144
			$options = apply_filters( 'carbon_custom_sidebar_options', $options, $id );
145
146
			register_sidebar( $options );
147
		}
148
	}
149
150
	/**
151
	 * Enqueue the UI scripts.
152
	 */
153
	public function enqueue_scripts() {
154
		wp_enqueue_style( 'carbon-sidebar-manager', \Carbon_Fields\URL . '/core/Libraries/Sidebar_Manager/assets/css/app.css', array(), \Carbon_Fields\VERSION );
155
		wp_enqueue_script( 'carbon-sidebar-manager', \Carbon_Fields\URL . '/core/Libraries/Sidebar_Manager/assets/js/app.js', array(), \Carbon_Fields\VERSION );
156
		wp_localize_script( 'carbon-sidebar-manager', 'crbSidebarl10n',
157
			array(
158
				'add_sidebar' => __( 'Add Sidebar', \Carbon_Fields\TEXT_DOMAIN ),
159
				'enter_name_of_new_sidebar' => __( 'Please enter the name of the new sidebar:', \Carbon_Fields\TEXT_DOMAIN ),
160
				'remove_sidebar_confirmation' => __( 'Are you sure you wish to remove this sidebar?', \Carbon_Fields\TEXT_DOMAIN ),
161
			)
162
		);
163
	}
164
}
165