| 1 |
<?php |
| 2 |
|
| 3 |
namespace JP\CC\Site; |
| 4 |
|
| 5 |
use JP\CC\Helpers; |
| 6 |
use JP\CC\Widget; |
| 7 |
use JP\CC\Is; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Class JP\CC\Site\Widgets |
| 15 |
*/ |
| 16 |
class Widgets { |
| 17 |
|
| 18 |
/** |
| 19 |
* Initialize Widgets |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
add_action( 'sidebars_widgets', array( __CLASS__, 'exclude_widgets' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Checks for and excludes widgets based on their chosen options. |
| 27 |
* |
| 28 |
* @param array $widget_areas An array of widget areas and their widgets. |
| 29 |
* |
| 30 |
* @return array The modified $widget_area array. |
| 31 |
*/ |
| 32 |
public static function exclude_widgets( $widget_areas ) { |
| 33 |
|
| 34 |
if ( is_admin() || Helpers::is_customize_preview() ) { |
| 35 |
return $widget_areas; |
| 36 |
} |
| 37 |
|
| 38 |
foreach ( $widget_areas as $widget_area => $widgets ) { |
| 39 |
|
| 40 |
if ( ! empty( $widgets ) && 'wp_inactive_widgets' != $widget_area ) { |
| 41 |
|
| 42 |
foreach ( $widgets as $position => $widget_id ) { |
| 43 |
|
| 44 |
$options = Widget::get_options( $widget_id ); |
| 45 |
|
| 46 |
// If not accessible then exclude this item. |
| 47 |
$exclude = ! Is::accessible( $options['which_users'], $options['roles'], 'widget' ); |
| 48 |
|
| 49 |
$exclude = apply_filters( 'jp_cc_should_exclude_widget', $exclude, $options, $widget_id ); |
| 50 |
|
| 51 |
// unset non-visible item |
| 52 |
if ( $exclude ) { |
| 53 |
unset( $widget_areas[ $widget_area ][ $position ] ); |
| 54 |
} |
| 55 |
|
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return $widget_areas; |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
|