-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclass-fw-extension-sidebars.php
366 lines (308 loc) · 10.7 KB
/
class-fw-extension-sidebars.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php if ( ! defined( 'FW' ) ) {
die( 'Forbidden' );
}
class FW_Extension_Sidebars extends FW_Extension {
/** @var _FW_Extension_Sidebars_Backend */
private $backend;
/** @var _FW_Extension_Sidebars_Frontend */
private $frontend;
/**
* key for saving/get data from wp_option
*/
public function get_fw_option_sidebars_settings_key() {
$current_theme = wp_get_theme();
return $current_theme->get_stylesheet() . '-fw-sidebars-options';
}
public function is_missing_config() {
$positions = $this->backend->config->get_sidebar_positions();
if ( empty( $positions ) or false === is_array( $positions ) ) {
return true;
}
return false;
}
public function get_sidebars_for_select() {
$sidebars = $this->get_sidebars();
$collector = array();
foreach ( $sidebars as $sidebar ) {
$collector[ $sidebar->get_id() ] = $sidebar->get_name();
}
return array(
'choices' => $collector
);
}
public function save_sidebar_settings( $settings ) {
return $this->backend->save_sidebar_settings( $settings );
}
/**
* @param $args
* @return array
* @since 1.0.10
*/
public function delete_sidebar_preset( $args ) {
return $this->backend->remove_preset( $args );
}
/**
* @internal
*/
protected function _init() {
$this->backend = new _FW_Extension_Sidebars_Backend();
$this->backend->init_sidebars();
add_filter( 'fw_post_options', array( $this, '_admin_filter_render_sidebar_picker' ), 10, 2 );
if ( is_admin() ) {
$this->add_admin_actions();
} else {
$this->add_theme_actions();
}
add_action('fw_option_types_init', array($this, '_action_option_types_init'));
add_action('fw:option-storage-types:register', array($this, '_action_register_option_storage_types'));
}
public function _action_option_types_init() {
require_once dirname(__FILE__) .'/includes/option-type/sidebar-picker/class-fw-option-type-sidebar-picker.php';
}
public function _action_register_option_storage_types(_FW_Option_Storage_Type_Register $register) {
require_once dirname(__FILE__) .'/includes/option-storage-type/class-fw-option-storage-type-fw-ext-sidebar-picker.php';
$register->register(new FW_Option_Storage_Type_FW_Ext_Sidebar_Picker());
}
private function add_theme_actions() {
if ( $this->is_missing_config() ) {
add_filter( 'sidebars_widgets', array( $this->get_frontend_instance(), 'replace_sidebars' ) );
}
}
private function add_admin_actions() {
add_action( 'admin_enqueue_scripts', array( $this, '_admin_action_enqueue_scripts' ) );
add_action( 'sidebar_admin_page', array( $this, '_admin_action_render_partial' ) );
add_action( 'after_setup_theme', array( $this, '_admin_action_after_setup_theme' ), 11, 2 );
if ( current_user_can( 'edit_theme_options' ) ) {
add_action( 'wp_ajax_add_new_sidebar_ajax', array( $this, '_admin_action_add_new_sidebar_ajax' ) );
add_action( 'wp_ajax_sidebar_autocomplete_ajax', array($this, '_admin_action_sidebar_autocomplete_ajax') );
add_action( 'wp_ajax_save_sidebar_preset_ajax', array( $this, '_admin_action_save_sidebar_preset_ajax' ) );
add_action( 'wp_ajax_remove_sidebar_preset_ajax', array($this, '_admin_action_remove_sidebar_preset_ajax') );
add_action( 'wp_ajax_delete_sidebar_ajax', array( $this, '_admin_action_delete_sidebar_ajax' ) );
add_action( 'wp_ajax_load_sidebar_preset_ajax', array( $this, '_admin_action_load_sidebar_preset_ajax' ) );
}
}
public function _admin_filter_render_sidebar_picker($options, $post_type) {
$post_type_array = $this->get_config('post_types_support');
if ($this->get_config('show_in_post_types') === true) {
if ( ! empty($post_type_array) && ! in_array($post_type, $post_type_array) ) {
return $options;
}
return array_merge($options, array(
'sidebar-picker' => array(
'title' => __('Sidebar Picker', 'fw'),
'type' => 'box',
'context' => 'side',
'options' => array(
'sidebar' => array(
'type' => 'sidebar-picker',
'label' => false,
'fw-storage' => 'fw-ext-sidebar-picker',
)
),
),
));
}
return $options;
}
/**
* @internal
*/
public function _admin_action_enqueue_scripts( $hook ) {
if ( 'widgets.php' === $hook ) {
wp_enqueue_style(
'fw-extension-' . $this->get_name() . '-css',
$this->get_declared_URI( '/static/css/sidebar.css' ),
array( 'fw', 'fw-selectize', 'fw-backend-options' ),
fw()->manifest->get_version()
);
wp_enqueue_script( 'fw-extension-' . $this->get_name() . '-autocomplete-js',
$this->get_declared_URI( '/static/js/sidebar-autocomplete.js' ),
array( 'fw-events', 'jquery', 'jquery-ui-autocomplete', 'fw' ),
fw()->manifest->get_version()
);
wp_localize_script( 'fw-extension-' . $this->get_name() . '-autocomplete-js', 'noMatchesFoundMsg', array( 'noMatchesFoundMsg' => __( 'No matches found', 'fw' ) ) );
wp_enqueue_script( 'fw-extension-' . $this->get_name() . '-general-js',
$this->get_declared_URI( '/static/js/sidebar-general.js' ),
array( 'fw-events', 'jquery', 'fw', 'fw-selectize', 'jquery-ui-tabs' ),
fw()->manifest->get_version()
);
wp_localize_script( 'fw-extension-' . $this->get_name() . '-general-js', 'PhpVar', array(
'confirmMessage' => __( 'Do you realy want to change without saving?', 'fw' ),
'dynamicSidebars' => $this->backend->get_dynamic_sidebars_ids(),
'missingIdMessage' => __( 'Missing ID. Check that you provided all mandatory data.', 'fw' ),
'createdTabName' => __( 'Created', 'fw' ),
'groupedTabDesc' => __( '(For Grouped Pages)', 'fw' ),
'specificTabDesc' => __( '(For Specific Pages)', 'fw' ),
'missingSidebarName' => __( 'No sidebar name specified', 'fw' ),
'newSidebarPlaceholder' => __( 'Sidebar Name', 'fw' ),
'newSidebarLabel' => __( 'New Sidebar', 'fw' ),
'addSidebarButtonTxt' => __( 'Add', 'fw' ),
'msgToConfirmDelete' => __( 'Are you sure you want to delete it?', 'fw' ),
) );
}
}
/**
* @internal
*/
public function _admin_action_delete_sidebar_ajax() {
$sidebar_id = FW_Request::POST( 'sidebar' );
$result = $this->backend->delete_sidebar( $sidebar_id );
$this->ajax_response( $result );
}
/**
* @internal
*/
public function _admin_action_load_sidebar_preset_ajax() {
$params = FW_Request::POST( 'params' );
$result = $this->backend->get_preset( $params );
$this->ajax_response( $result );
}
/**
* @internal
*/
public function _admin_action_remove_sidebar_preset_ajax() {
$args = FW_Request::POST( 'data' );
$result = $this->backend->remove_preset( $args );
$this->ajax_response( $result );
}
/**
* @internal
*/
public function _admin_action_save_sidebar_preset_ajax() {
$settings = FW_Request::POST( 'settings' );
$result = $this->backend->save_sidebar_settings( $settings );
$this->ajax_response( $result );
}
/**
* @internal
*/
public function _admin_action_sidebar_autocomplete_ajax() {
$search_type = FW_Request::POST( 'searchType' );
$search_term = FW_Request::POST( 'searchTerm' );
$result = $this->backend->get_autocomplete_results( $search_type, $search_term );
$this->ajax_response( $result );
}
public function get_data_positions_options() {
$sidebar_positions = $this->backend->config->get_sidebar_positions();
$choices = array();
foreach ( $sidebar_positions as $key => $position ) {
$choices[ $key ] = array(
'label' => false,
'small' => $position['icon_url'],
'data' => array(
'colors' => fw_akg( _FW_Extension_Sidebars_Config::SIDEBARS_NR_KEY, $position )
)
);
}
$data_positions_options = array(
'type' => 'image-picker',
'choices' => $choices,
'value' => '',
'attr' => array( 'class' => 'fw-ext-sidebars-positions' )
);
return $data_positions_options;
}
/**
* Render partial on widgets.php page
* @internal
*/
public function _admin_action_render_partial() {
$specific_options = array(
'type' => 'select',
'choices' => $this->backend->config->get_specific_labels(),
'value' => ''
);
$grouped_options = array(
'type' => 'select',
'choices' => $this->backend->config->get_grouped_labels(),
'value' => ''
);
$created_sidebars = $this->backend->get_presets_sidebars();
$sidebars = $this->backend->get_all_sidebars();
echo $this->render_view( 'backend-main-view', array(
'grouped_options' => $grouped_options, //options for select grouped pages tab
'specific_options' => $specific_options, //options for select specific page tab
'created_sidebars' => $created_sidebars, //used for removable items on created tab
'data_positions_options' => $this->get_data_positions_options(), //used for image-picker
'sidebars' => $sidebars, //used for selectize options on grouped and specific tabs
) );
}
/**
* Render partial on widgets.php page
* @internal
*/
public function _admin_action_after_setup_theme() {
remove_theme_support( 'widgets-block-editor' );
}
public function get_presets_sidebars() {
return $this->backend->get_presets_sidebars();
}
public function get_sidebars() {
return $this->backend->get_all_sidebars();
}
/**
* @internal
*/
public function _admin_action_add_new_sidebar_ajax() {
$name = FW_Request::POST( 'name' );
$result = $this->backend->save_new_sidebar( $name );
$this->ajax_response( $result );
}
/**
* return standards WP AJAX responses
*/
private function ajax_response( $result ) {
if ( isset( $result['status'] ) ) {
if ( $result['status'] ) {
unset( $result['status'] );
wp_send_json_success( $result );
} else {
unset( $result['status'] );
wp_send_json_error( $result );
}
} else {
wp_send_json( $result );
}
}
public function get_specific_preset_by_id( $id ) {
return $this->backend->get_specific_preset_by_id( $id );
}
private function get_frontend_instance() {
if ( ! $this->frontend ) {
$this->frontend = new _FW_Extension_Sidebars_Frontend();
}
return $this->frontend;
}
public function render_sidebar( $color ) {
return $this->get_frontend_instance()->render_sidebar( $color );
}
public function get_current_preset() {
return $this->get_frontend_instance()->get_current_page_preset();
}
/**
* @since 1.0.6
*/
public function get_current_position() {
return $this->get_frontend_instance()->get_preset_position();
}
/**
* Old method with typo in name.
* We didn't deleted it because there may be someone that is calling it (because it is public)
* @deprecated
*/
public function get_current_positon() {
return $this->get_current_position();
}
/**
* @return array
*/
public function get_allowed_places() {
return $this->backend->_fw_get_allowed_places();
}
/**
* {@inheritdoc}
*/
public function _get_link() {
return self_admin_url( 'widgets.php' );
}
}