| 1 |
<?php |
| 2 |
|
| 3 |
namespace JP\CC\Admin\Widget; |
| 4 |
|
| 5 |
use JP\CC\Widget; |
| 6 |
use JP\CC\Roles; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class JP\CC\Admin\Widget_Settings |
| 14 |
*/ |
| 15 |
class Settings { |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize Widget Settings |
| 19 |
*/ |
| 20 |
public static function init() { |
| 21 |
add_action( 'in_widget_form', array( __CLASS__, 'fields' ), 5, 3 ); |
| 22 |
add_filter( 'widget_update_callback', array( __CLASS__, 'save' ), 5, 3 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Renders additional widget option fields. |
| 27 |
* |
| 28 |
* @param $widget |
| 29 |
* @param $return |
| 30 |
* @param $instance |
| 31 |
*/ |
| 32 |
public static function fields( $widget, $return, $instance ) { |
| 33 |
|
| 34 |
$allowed_user_roles = Roles::allowed_user_roles(); |
| 35 |
|
| 36 |
wp_nonce_field( 'jpcc-menu-editor-nonce', 'jpcc-menu-editor-nonce' ); |
| 37 |
|
| 38 |
$which_users_options = array( |
| 39 |
'' => __( 'Everyone', 'content-control' ), |
| 40 |
'logged_out' => __( 'Logged Out Users', 'content-control' ), |
| 41 |
'logged_in' => __( 'Logged In Users', 'content-control' ), |
| 42 |
); |
| 43 |
|
| 44 |
$instance = Widget::parse_options( $instance ); ?> |
| 45 |
|
| 46 |
<p class="widget_options-which_users"> |
| 47 |
|
| 48 |
<label for="<?php echo $widget->get_field_id( 'which_users' ); ?>"> |
| 49 |
|
| 50 |
<?php _e( 'Who can see this widget?', 'content-control' ); ?><br /> |
| 51 |
|
| 52 |
<select name="<?php echo $widget->get_field_name( 'which_users' ); ?>" id="<?php echo $widget->get_field_id( 'which_users' ); ?>" class="widefat"> |
| 53 |
<?php foreach ( $which_users_options as $option => $label ) : ?> |
| 54 |
<option value="<?php echo $option; ?>" <?php selected( $option, $instance['which_users'] ); ?>> |
| 55 |
<?php echo esc_html( $label ); ?> |
| 56 |
</option> |
| 57 |
<?php endforeach; ?> |
| 58 |
</select> |
| 59 |
|
| 60 |
</label> |
| 61 |
|
| 62 |
</p> |
| 63 |
|
| 64 |
<p class="widget_options-roles"> |
| 65 |
|
| 66 |
<?php _e( 'Choose which roles can see this widget', 'content-control' ); ?><br /> |
| 67 |
|
| 68 |
<?php foreach ( $allowed_user_roles as $option => $label ) : ?> |
| 69 |
<label> |
| 70 |
<input type="checkbox" name="<?php echo $widget->get_field_name( 'roles' ); ?>[]" value="<?php echo $option; ?>" <?php checked( in_array( $option, $instance['roles'] ), true ); ?>/> |
| 71 |
<?php echo esc_html( $label ); ?> |
| 72 |
</label> |
| 73 |
<?php endforeach; ?> |
| 74 |
|
| 75 |
</p> |
| 76 |
|
| 77 |
<?php |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Validates & saves additional widget options. |
| 82 |
* |
| 83 |
* @param $instance |
| 84 |
* @param $new_instance |
| 85 |
* |
| 86 |
* @return array|bool |
| 87 |
*/ |
| 88 |
public static function save( $instance, $new_instance, $old_instance ) { |
| 89 |
|
| 90 |
if (isset( $_POST['jpcc-menu-editor-nonce'] ) && wp_verify_nonce( $_POST['jpcc-menu-editor-nonce'], 'jpcc-menu-editor-nonce' ) ) { |
| 91 |
$new_instance = Widget::parse_options( $new_instance ); |
| 92 |
$instance['which_users'] = $new_instance['which_users']; |
| 93 |
$instance['roles'] = $new_instance['roles']; |
| 94 |
|
| 95 |
if ( $instance['which_users'] == 'logged_in' ) { |
| 96 |
|
| 97 |
$allowed_roles = Roles::allowed_user_roles(); |
| 98 |
|
| 99 |
// Validate chosen roles and remove non-allowed roles. |
| 100 |
foreach ( (array) $instance['roles'] as $key => $role ) { |
| 101 |
if ( ! array_key_exists( $role, $allowed_roles ) ) { |
| 102 |
unset( $instance['roles'][ $key ] ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
} else { |
| 107 |
unset( $instance['roles'] ); |
| 108 |
} |
| 109 |
} else { |
| 110 |
$old_instance = Widget::parse_options( $old_instance ); |
| 111 |
$instance['which_users'] = $old_instance['which_users']; |
| 112 |
|
| 113 |
if ( empty( $old_instance['roles'] ) ) { |
| 114 |
unset( $instance['roles'] ); |
| 115 |
} else { |
| 116 |
$instance['roles'] = $old_instance['roles']; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $instance; |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|