-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsidebar.php
937 lines (843 loc) · 29.5 KB
/
sidebar.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
<?php
/**
* @package Content Aware Sidebars
* @author Joachim Jensen <[email protected]>
* @license GPLv3
* @copyright 2024 by Joachim Jensen
*/
defined('ABSPATH') || exit;
final class CAS_Sidebar_Manager
{
/**
* Sidebar metadata
* @var WPCACollection
*/
protected $metadata;
/**
* Custom sidebars
* @var array
*/
public $sidebars = [];
/**
* Cache replaced sidebars
* @var array
*/
protected $replaced_sidebars = [];
/**
* Sidebar replacement map
* @var array
*/
protected $replace_map = [];
/**
* @var array
* Constructor
*
* @since 3.1
*/
public function __construct()
{
add_action(
'wpca/loaded',
[$this,'late_init']
);
add_action(
'wp_head',
[$this,'sidebar_notify_theme_customizer']
);
add_action(
'init',
[$this,'init_sidebar_type'],
99
);
add_action(
'widgets_init',
[$this,'create_sidebars'],
99
);
add_action(
'wp_loaded',
[$this,'set_sidebar_styles'],
99
);
add_filter('get_edit_post_link', [$this,'get_edit_post_link'], 10, 3);
add_filter('get_delete_post_link', [$this,'get_delete_post_link'], 10, 3);
add_shortcode(
'ca-sidebar',
[$this,'sidebar_shortcode']
);
}
/**
* Initialize after WPCA has been loaded
* Makes sure the SDK can be used in actions/filters
* forcefully called earlier
*
* @since 3.4
* @return void
*/
public function late_init()
{
if (!is_admin()) {
add_filter(
'sidebars_widgets',
[$this,'replace_sidebar']
);
add_filter(
'wpca/posts/sidebar',
[__CLASS__,'filter_password_protection']
);
add_filter(
'wpca/posts/sidebar',
[$this,'filter_visibility']
);
add_filter(
'cas/shortcode/display',
[$this,'filter_shortcode_visibility'],
10,
2
);
add_action(
'dynamic_sidebar_before',
[$this,'render_sidebar_before'],
9,
2
);
add_action(
'dynamic_sidebar_after',
[$this,'render_sidebar_after'],
99,
2
);
add_filter(
'generate_sidebar_layout',
[$this, 'generatepress_adjust_column_width'],
9999
);
}
}
/**
* Get instance of metadata manager
*
* @since 3.0
* @return WPCACollection
*/
public function metadata()
{
if (!$this->metadata) {
$this->init_metadata();
}
return $this->metadata;
}
/**
* Create post meta fields
* @global array $wp_registered_sidebars
* @return void
*/
private function init_metadata()
{
$this->metadata = new WPCACollection();
$this->metadata
->put('visibility', new WPCAMeta(
'visibility',
__('User Visibility', 'content-aware-sidebars'),
[],
'multi',
[
'general' => [
'label' => 'General',
'options' => [
-1 => __('Logged-in', 'content-aware-sidebars')
]
]
]
))
->put('handle', new WPCAMeta(
'handle',
_x('Action', 'option', 'content-aware-sidebars'),
CAS_App::ACTION_REPLACE,
'select',
[
CAS_App::ACTION_REPLACE => __('Replace', 'content-aware-sidebars'),
CAS_App::ACTION_MERGE => __('Merge', 'content-aware-sidebars'),
CAS_App::ACTION_REPLACE_FORCED => __('Forced replace', 'content-aware-sidebars'),
CAS_App::ACTION_SHORTCODE => __('Shortcode')
],
__('Replace host sidebar, merge with it or add sidebar manually.', 'content-aware-sidebars')
))
->put('host', new WPCAMeta(
'host',
__('Target Sidebar', 'content-aware-sidebars'),
[],
'multi',
[]
))
->put('merge_pos', new WPCAMeta(
'merge_pos',
__('Merge Position', 'content-aware-sidebars'),
1,
'select',
[
__('Top', 'content-aware-sidebars'),
__('Bottom', 'content-aware-sidebars')
],
__('Place sidebar on top or bottom of host when merging.', 'content-aware-sidebars')
))
->put('html', new WPCAMeta(
'html',
__('HTML', 'content-aware-sidebars'),
[],
'select',
['']
))
->put('protected_content', new WPCAMeta(
'protected_content',
__('Password Protected Content', 'content-aware-sidebars'),
false,
'checkbox'
));
apply_filters('cas/metadata/init', $this->metadata);
}
/**
* Populate metadata with dynamic content
* for use in admin
*
* @since 3.2
* @return void
*/
public function populate_metadata()
{
if ($this->metadata) {
global $wp_registered_sidebars;
// List of sidebars
$sidebar_list = [];
foreach ($wp_registered_sidebars as $sidebar) {
$sidebar_list[$sidebar['id']] = $sidebar['name'];
}
// Remove ability to set self to host
if (get_the_ID()) {
unset($sidebar_list[CAS_App::SIDEBAR_PREFIX . get_the_ID()]);
}
asort($sidebar_list);
$this->metadata->get('host')->set_input_list($sidebar_list);
if (!cas_fs()->can_use_premium_code()) {
$pro_label = ' (Pro)';
$actions = $this->metadata->get('handle');
$action_list = $actions->get_input_list();
$action_list['__infuse'] = __('Infuse', 'content-aware-sidebars') . $pro_label;
$action_list['__after_paragraph'] = __('After Paragraph', 'content-aware-sidebars') . $pro_label;
$action_list['__totem'] = __('Totem - Floating Button', 'content-aware-sidebars') . $pro_label;
$actions->set_input_list($action_list);
}
apply_filters('cas/metadata/populate', $this->metadata);
}
}
/**
* Create sidebar post type
* Add it to content aware engine
*
* @return void
*/
public function init_sidebar_type()
{
register_post_type(CAS_App::TYPE_SIDEBAR, [
'labels' => [
'name' => __('Sidebars', 'content-aware-sidebars'),
'singular_name' => __('Sidebar', 'content-aware-sidebars'),
'add_new' => _x('Add New', 'sidebar', 'content-aware-sidebars'),
'add_new_item' => __('Add New Sidebar', 'content-aware-sidebars'),
'edit_item' => __('Edit Sidebar', 'content-aware-sidebars'),
'new_item' => __('New Sidebar', 'content-aware-sidebars'),
'all_items' => __('All Sidebars', 'content-aware-sidebars'),
'view_item' => __('View Sidebar', 'content-aware-sidebars'),
'search_items' => __('Search Sidebars', 'content-aware-sidebars'),
'not_found' => __('No sidebars found', 'content-aware-sidebars'),
'not_found_in_trash' => __('No sidebars found in Trash', 'content-aware-sidebars'),
//wp-content-aware-engine specific
'ca_title' => __('Where to display', 'content-aware-sidebars')
],
'capabilities' => [
'edit_post' => CAS_App::CAPABILITY,
'read_post' => CAS_App::CAPABILITY,
'delete_post' => CAS_App::CAPABILITY,
'edit_posts' => CAS_App::CAPABILITY,
'delete_posts' => CAS_App::CAPABILITY,
'edit_others_posts' => CAS_App::CAPABILITY,
'publish_posts' => CAS_App::CAPABILITY,
'read_private_posts' => CAS_App::CAPABILITY
],
'public' => false,
'hierarchical' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'has_archive' => false,
'rewrite' => false,
'query_var' => false,
'supports' => ['title','page-attributes'],
'menu_icon' => 'dashicons-welcome-widgets-menus',
'can_export' => false,
'delete_with_user' => false
]);
WPCACore::types()->add(CAS_App::TYPE_SIDEBAR);
}
/**
* Add sidebars to widgets area
* Triggered in widgets_init to save location for each theme
* @return void
*/
public function create_sidebars()
{
$sidebars = get_posts([
'numberposts' => -1,
'post_type' => CAS_App::TYPE_SIDEBAR,
'post_status' => [
CAS_App::STATUS_ACTIVE,
CAS_App::STATUS_INACTIVE,
CAS_App::STATUS_SCHEDULED
],
'orderby' => 'title',
'order' => 'ASC'
]);
//Register sidebars to add them to the list
foreach ($sidebars as $post) {
$this->sidebars[CAS_App::SIDEBAR_PREFIX . $post->ID] = $post;
register_sidebar([
'name' => $post->post_title ? $post->post_title : __('(no title)'),
'id' => CAS_App::SIDEBAR_PREFIX . $post->ID,
'before_sidebar' => '',
'after_sidebar' => ''
]);
}
}
/**
* Set styles of created sidebars
*
* @since 3.6
*/
public function set_sidebar_styles()
{
global $wp_registered_sidebars;
//todo: only for manual
$default_styles = [
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>'
];
$has_host = [
CAS_App::ACTION_REPLACE => 1,
CAS_App::ACTION_MERGE => 1,
CAS_App::ACTION_REPLACE_FORCED => 1
];
$metadata = $this->metadata();
foreach ($this->sidebars as $id => $post) {
$args = $default_styles;
if (isset($has_host[$metadata->get('handle')->get_data($post->ID)])) {
//Set style from host to fix when content aware sidebar
//is called directly by other sidebar managers
$host_id = $metadata->get('host')->get_data($post->ID);
if (isset($wp_registered_sidebars[$host_id])) {
foreach ([
'before_widget',
'after_widget',
'before_title',
'after_title',
'before_sidebar',
'after_sidebar'
] as $pos) {
if (isset($wp_registered_sidebars[$host_id][$pos])) {
$args[$pos] = $wp_registered_sidebars[$host_id][$pos];
}
}
}
}
$wp_registered_sidebars[$id] = array_merge($wp_registered_sidebars[$id], $args);
}
}
/**
* @return array
*/
public function get_replacement_map()
{
return $this->replace_map;
}
/**
* @param string $i
* @return void
*/
public function override_sidebar_styles($i)
{
//Get nested styles
$html = $this->get_sidebar_styles($i);
if ($html) {
global $wp_registered_sidebars;
//bail if trying to style non-existing sidebar.
//this can happen after theme change
if (!isset($wp_registered_sidebars[$i])) {
return;
}
$styles = $wp_registered_sidebars[$i];
//Set user styles
foreach ([
'widget',
'title',
'sidebar'
] as $pos) {
if (empty($styles['before_' . $pos])) {
$html[$pos] = 'div';
}
//if we have both custom element and class, rebuild
if (isset($html[$pos],$html[$pos . '_class'])) {
$e = esc_html($html[$pos]);
$class = esc_html($html[$pos . '_class']);
$id = '';
if (isset($html[$pos . '_id'])) {
$id = ' id="' . $html[$pos . '_id'] . '"';
}
$styles['before_' . $pos] = '<' . $e . $id . ' class="' . $class . '">';
$styles['after_' . $pos] = "</$e>";
//if we only have custom class, inject it
} elseif (isset($html[$pos . '_class'])) {
$class = esc_html($html[$pos . '_class']);
$classPos = stripos($styles['before_' . $pos], 'class=');
if ($classPos !== false) {
$styles['before_' . $pos] = substr_replace(
$styles['before_' . $pos],
$class . ' ',
$classPos + 7,
0
);
continue;
}
$classPos = stripos($styles['before_' . $pos], '>');
if ($classPos !== false) {
$styles['before_' . $pos] = substr_replace(
$styles['before_' . $pos],
' class="' . $class . '"',
$classPos,
0
);
}
}
}
$wp_registered_sidebars[$i] = $styles;
}
}
/**
* Replace or merge a sidebar with content aware sidebars.
* @since .
* @param array $sidebars_widgets
* @return array
*/
public function replace_sidebar($sidebars_widgets)
{
//customizer requires sidebars_widgets filter. cache for repeat calls
if ($this->replaced_sidebars) {
return $this->replaced_sidebars;
}
$posts = WPCACore::get_posts(CAS_App::TYPE_SIDEBAR);
if ($posts) {
$metadata = $this->metadata();
$has_host = [
CAS_App::ACTION_REPLACE => 1,
CAS_App::ACTION_MERGE => 1,
CAS_App::ACTION_REPLACE_FORCED => 1
];
//replace and merge widgets, build replacement map
foreach ($posts as $post) {
$id = CAS_App::SIDEBAR_PREFIX . $post->ID;
// Check for correct handling
if (!isset($has_host[$post->handle])) {
continue;
}
$hosts = $metadata->get('host')->get_data($post->ID, false, false);
foreach ($hosts as $host) {
// Check if host exist
if (!isset($sidebars_widgets[$host])) {
continue;
}
$this->override_sidebar_styles($host);
// Sidebar might not have any widgets. Get it anyway!
if (!isset($sidebars_widgets[$id])) {
$sidebars_widgets[$id] = [];
}
// If handle is merge or if handle is replace and host has already been replaced
if ($post->handle == CAS_App::ACTION_MERGE || ($post->handle == CAS_App::ACTION_REPLACE && isset($handled_already[$host]))) {
//do not merge forced replace
//todo: maybe reverse order of fetched sidebars instead?
if (isset($handled_already[$host]) && $handled_already[$host] == CAS_App::ACTION_REPLACE_FORCED) {
continue;
}
if ($metadata->get('merge_pos')->get_data($post->ID)) {
$sidebars_widgets[$host] = array_merge($sidebars_widgets[$host], $sidebars_widgets[$id]);
} else {
$sidebars_widgets[$host] = array_merge($sidebars_widgets[$id], $sidebars_widgets[$host]);
}
} else {
$sidebars_widgets[$host] = $sidebars_widgets[$id];
$handled_already[$host] = $post->handle;
}
//last replacement will take priority
//todo: extend to work for widgets too
$this->replace_map[$host] = $id;
}
}
$this->replaced_sidebars = $sidebars_widgets;
//override styles
foreach ($posts as $post) {
$id = CAS_App::SIDEBAR_PREFIX . $post->ID;
// Check for correct handling
if (!isset($has_host[$post->handle])) {
$this->override_sidebar_styles($id);
continue;
}
$hosts = $metadata->get('host')->get_data($post->ID, false, false);
foreach ($hosts as $host) {
// Check if host exist
if (!isset($sidebars_widgets[$host])) {
continue;
}
$this->override_sidebar_styles($host);
}
}
}
return $sidebars_widgets;
}
/**
* Show manually handled content aware sidebars
* @global array $_wp_sidebars_widgets
* @param string|array $args
* @return void
*/
public function manual_sidebar($args)
{
global $_wp_sidebars_widgets;
// Grab args or defaults
$args = wp_parse_args($args, [
'include' => '',
'before' => '',
'after' => ''
]);
extract($args, EXTR_SKIP);
// Get sidebars
$posts = WPCACore::get_posts(CAS_App::TYPE_SIDEBAR);
if (!$posts) {
return;
}
// Handle include argument
if (!empty($include)) {
if (!is_array($include)) {
$include = explode(',', $include);
}
// Fast lookup
$include = array_flip($include);
}
$i = $host = 0;
foreach ($posts as $post) {
$id = CAS_App::SIDEBAR_PREFIX . $post->ID;
// Check for manual handling, if sidebar exists and if id should be included
if ($post->handle != CAS_App::ACTION_SHORTCODE || !isset($_wp_sidebars_widgets[$id]) || (!empty($include) && !isset($include[$post->ID]))) {
continue;
}
// Merge if more than one. First one is host.
if ($i > 0) {
if ($this->metadata()->get('merge_pos')->get_data($post->ID)) {
$_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$host], $_wp_sidebars_widgets[$id]);
} else {
$_wp_sidebars_widgets[$host] = array_merge($_wp_sidebars_widgets[$id], $_wp_sidebars_widgets[$host]);
}
} else {
$host = $id;
}
$i++;
}
if ($host) {
echo $before;
dynamic_sidebar($host);
echo $after;
}
}
/**
* Display sidebar with shortcode
* @version 2.5
* @param array $atts
* @param string $content
* @return string
*/
public function sidebar_shortcode($atts, $content = '')
{
$a = shortcode_atts([
'id' => 0,
'conditional' => 0
], $atts);
$id = CAS_App::SIDEBAR_PREFIX . esc_attr($a['id']);
if (!isset($this->sidebars[$id]) || $this->sidebars[$id]->post_status != CAS_App::STATUS_ACTIVE) {
return $content;
}
if ($this->metadata()->get('handle')->get_data($a['id']) != CAS_App::ACTION_SHORTCODE) {
return $content;
}
$display = true;
if ($a['conditional']) {
$sidebars = WPCACore::get_posts(CAS_App::TYPE_SIDEBAR);
$display = !empty($sidebars) && isset($sidebars[$id]);
}
if (is_active_sidebar($id) && apply_filters('cas/shortcode/display', $display, $a['id'])) {
ob_start();
do_action('cas/shortcode/before', $a['id']);
$this->override_sidebar_styles($id);
dynamic_sidebar($id);
$content = ob_get_clean();
}
return $content;
}
/**
* Get styles from nested sidebars
*
* @since 3.7
* @param string $i
* @return array
*/
public function get_sidebar_styles($i)
{
$styles = [];
$metadata = $this->metadata()->get('html');
while ($i) {
if (isset($this->sidebars[$i])) {
$style = apply_filters('cas/sidebar/html', $metadata->get_data($this->sidebars[$i]->ID, true), $this->sidebars[$i]->ID);
if ($style) {
$styles = array_merge($styles, $style);
$styles['widget_id'] = '%1$s';
$styles['sidebar_id'] = CAS_App::SIDEBAR_PREFIX . $this->sidebars[$i]->ID;
}
}
$i = isset($this->replace_map[$i]) ? $this->replace_map[$i] : false;
}
return $styles;
}
/**
* Render html if present before sidebar
*
* @deprecated since WP5.6
* @since 3.6
* @param string $i
* @param boolean $has_widgets
* @return void
*/
public function render_sidebar_before($i, $has_widgets)
{
//wp5.6 introduced before_sidebar without notice
if (version_compare(get_bloginfo('version'), '5.6', '>=')) {
return;
}
global $wp_registered_sidebars;
if ($has_widgets && isset($wp_registered_sidebars[$i]['before_sidebar'])) {
echo $wp_registered_sidebars[$i]['before_sidebar'];
}
}
/**
* Render html if present after sidebar
*
* @deprecated since WP5.6
* @since 3.6
* @param string $i
* @param boolean $has_widgets
* @return void
*/
public function render_sidebar_after($i, $has_widgets)
{
//wp5.6 introduced after_sidebar without notice
if (version_compare(get_bloginfo('version'), '5.6', '>=')) {
return;
}
global $wp_registered_sidebars;
if ($has_widgets && isset($wp_registered_sidebars[$i]['after_sidebar'])) {
echo $wp_registered_sidebars[$i]['after_sidebar'];
}
}
/**
* Filter out all sidebars if post is password protected
*
* @since 3.7
* @param array $sidebars
* @return array
*/
public static function filter_password_protection($sidebars)
{
if (!is_singular()) {
return $sidebars;
}
if (!post_password_required()) {
return $sidebars;
}
$metadata = CAS_App::instance()->manager()->metadata()->get('protected_content');
foreach ($sidebars as $id => $sidebar) {
if ((bool)$metadata->get_data($id) === false) {
unset($sidebars[$id]);
}
}
return $sidebars;
}
/**
* Filter out sidebars based on current user
*
* @since 3.7
* @param array $sidebars
* @return array
*/
public function filter_visibility($sidebars)
{
if ($sidebars) {
$metadata = $this->metadata()->get('visibility');
//temporary filter until WPCACore allows filtering
$user_visibility = is_user_logged_in() ? [-1] : [];
$user_visibility = apply_filters('cas/user_visibility', $user_visibility);
foreach ($sidebars as $id => $sidebar) {
$visibility = $metadata->get_data($id, true, false);
// Check visibility
if ($visibility && !array_intersect($visibility, $user_visibility)) {
unset($sidebars[$id]);
}
}
}
return $sidebars;
}
/**
* Filter shortcode sidebar based on current user
*
* @since 3.7.1
* @param boolean $retval
* @param int $id
* @return boolean
*/
public function filter_shortcode_visibility($retval, $id)
{
if (!$retval) {
return $retval;
}
$metadata = $this->metadata()->get('visibility');
$visibility = $metadata->get_data($id, true, false);
if (!$visibility) {
return $retval;
}
//temporary filter until WPCACore allows filtering
$user_visibility = is_user_logged_in() ? [-1] : [];
$user_visibility = apply_filters('cas/user_visibility', $user_visibility);
// Check visibility
if (!array_intersect($visibility, $user_visibility)) {
$retval = false;
}
return $retval;
}
/**
* Runs is_active_sidebar for sidebars
* Widget management in Theme Customizer
* expects this
*
* @global WP_Customize_Manager $wp_customize
* @since 2.2
* @return void
*/
public function sidebar_notify_theme_customizer()
{
global $wp_customize;
if (!empty($wp_customize)) {
$sidebars = WPCACore::get_posts(CAS_App::TYPE_SIDEBAR);
if ($sidebars) {
foreach ($sidebars as $sidebar) {
is_active_sidebar(CAS_App::SIDEBAR_PREFIX . $sidebar->ID);
}
}
}
}
/**
* Get sidebar edit link
* TODO: Consider changing post type _edit_link instead
*
* @since 3.4
* @param string $link
* @param int $post_id
* @param string $context
* @return string
*/
public function get_edit_post_link($link, $post_id, $context)
{
$post = get_post($post_id);
if ($post->post_type == CAS_App::TYPE_SIDEBAR) {
$sep = '&';
if ($context == 'display') {
$sep = '&';
}
$link = admin_url('admin.php?page=wpcas-edit' . $sep . 'sidebar_id=' . $post_id);
//load page in all languages for wpml, polylang,
//ensures post type conditions are not filtered
if (defined('ICL_SITEPRESS_VERSION') || defined('POLYLANG_VERSION')) {
$link .= $sep . 'lang=all';
}
}
return $link;
}
/**
* Get sidebar delete link
* TODO: Consider changing post type _edit_link instead
*
* @since 3.4
* @param string $link
* @param int $post_id
* @param boolean $force_delete
* @return string
*/
public function get_delete_post_link($link, $post_id, $force_delete)
{
$post = get_post($post_id);
if ($post->post_type == CAS_App::TYPE_SIDEBAR) {
$action = ($force_delete || !EMPTY_TRASH_DAYS) ? 'delete' : 'trash';
$link = add_query_arg(
'action',
$action,
admin_url('admin.php?page=wpcas-edit&sidebar_id=' . $post_id)
);
$link = wp_nonce_url($link, "$action-post_$post_id");
}
return $link;
}
/**
* @param string $layout
* @return string
*/
public function generatepress_adjust_column_width($layout)
{
if ($layout === 'no-sidebar') {
return 'no-sidebar';
}
//only adjust layout if custom sidebars are empty (keep theme sidebar behavior intact)
$replaced_sidebars = CAS_App::instance()->manager()->get_replacement_map();
$active_left = !isset($replaced_sidebars['sidebar-2']) || is_active_sidebar('sidebar-2');
$active_right = !isset($replaced_sidebars['sidebar-1']) || is_active_sidebar('sidebar-1');
if ($active_left && $active_right) {
return $layout;
}
if ($layout === 'right-sidebar' && !$active_right) {
return 'no-sidebar';
}
if ($layout === 'left-sidebar' && !$active_left) {
return 'no-sidebar';
}
if (in_array($layout, ['both-sidebars', 'both-right', 'both-left'])) {
if (!$active_left && !$active_right) {
return 'no-sidebar';
}
if ($active_left) {
//todo if layout==both-right, we need to move left-sidebar widgets into right-sidebar
return 'left-sidebar';
}
if ($active_right) {
//todo if layout==both-left, we need to move right-sidebar widgets into left-sidebar
return 'right-sidebar';
}
}
return $layout;
}
}