| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friend List Widget |
| 4 |
* |
| 5 |
* A widget that displays a list of your friends. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
* @since 0.3 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Friends; |
| 12 |
|
| 13 |
/** |
| 14 |
* This is the class for the Friend List Widget. |
| 15 |
* |
| 16 |
* @package Friends |
| 17 |
* @author Alex Kirk |
| 18 |
*/ |
| 19 |
class Widget_Friends_List extends Widget_Base_Friends_List { |
| 20 |
/** |
| 21 |
* Constructor |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
parent::__construct( |
| 25 |
'friends-widget-friend-list', |
| 26 |
__( 'Friend List', 'friends' ), |
| 27 |
array( |
| 28 |
'description' => __( 'Shows a list of your friends.', 'friends' ), |
| 29 |
) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Return default values for this widget's options. |
| 35 |
* |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public function defaults() { |
| 39 |
return array( |
| 40 |
'title' => '', |
| 41 |
'user_types' => 'folders', |
| 42 |
'folder' => 0, |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Save updated settings from the widget form. |
| 48 |
* |
| 49 |
* @param array $new_instance New settings. |
| 50 |
* @param array $old_instance Old settings. |
| 51 |
* @return array Sanitized settings. |
| 52 |
*/ |
| 53 |
public function update( $new_instance, $old_instance ) { |
| 54 |
$instance = $this->defaults(); |
| 55 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 56 |
$instance['user_types'] = in_array( $new_instance['user_types'], array( 'subscriptions', 'starred', 'folders' ), true ) ? $new_instance['user_types'] : 'folders'; |
| 57 |
$instance['folder'] = intval( $new_instance['folder'] ); |
| 58 |
return $instance; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Output the widget settings form. |
| 63 |
* |
| 64 |
* @param array $instance Current settings. |
| 65 |
*/ |
| 66 |
public function form( $instance ) { |
| 67 |
$instance = wp_parse_args( $instance, $this->defaults() ); |
| 68 |
$user_types = $instance['user_types']; |
| 69 |
$folder = intval( $instance['folder'] ); |
| 70 |
$folders = Subscription::get_folders(); |
| 71 |
?> |
| 72 |
<p> |
| 73 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'friends' ); ?></label> |
| 74 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>"> |
| 75 |
</p> |
| 76 |
<p> |
| 77 |
<label for="<?php echo esc_attr( $this->get_field_id( 'user_types' ) ); ?>"><?php esc_html_e( 'Display:', 'friends' ); ?></label> |
| 78 |
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'user_types' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'user_types' ) ); ?>"> |
| 79 |
<option value="subscriptions"<?php selected( $user_types, 'subscriptions' ); ?>><?php esc_html_e( 'Following', 'friends' ); ?></option> |
| 80 |
<option value="starred"<?php selected( $user_types, 'starred' ); ?>><?php esc_html_e( 'Starred', 'friends' ); ?></option> |
| 81 |
<option value="folders"<?php selected( $user_types, 'folders' ); ?>><?php esc_html_e( 'Grouped by Folder', 'friends' ); ?></option> |
| 82 |
</select> |
| 83 |
</p> |
| 84 |
<?php if ( ! empty( $folders ) ) : ?> |
| 85 |
<p> |
| 86 |
<label for="<?php echo esc_attr( $this->get_field_id( 'folder' ) ); ?>"><?php esc_html_e( 'Folder:', 'friends' ); ?></label> |
| 87 |
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'folder' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'folder' ) ); ?>"> |
| 88 |
<option value="0"<?php selected( $folder, 0 ); ?>><?php esc_html_e( '— All —', 'friends' ); ?></option> |
| 89 |
<?php foreach ( $folders as $f ) : ?> |
| 90 |
<option value="<?php echo esc_attr( $f->term_id ); ?>"<?php selected( $folder, $f->term_id ); ?>><?php echo esc_html( $f->name ); ?></option> |
| 91 |
<?php endforeach; ?> |
| 92 |
</select> |
| 93 |
</p> |
| 94 |
<?php endif; ?> |
| 95 |
<?php |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Render the widget. |
| 100 |
* |
| 101 |
* @param array $args Sidebar arguments. |
| 102 |
* @param array $instance Widget instance settings. |
| 103 |
*/ |
| 104 |
public function widget( $args, $instance ) { |
| 105 |
$instance = wp_parse_args( $instance, $this->defaults() ); |
| 106 |
$user_types = $instance['user_types']; |
| 107 |
$folder = intval( $instance['folder'] ); |
| 108 |
$widget_id = ! empty( $args['widget_id'] ) ? $args['widget_id'] : ''; |
| 109 |
|
| 110 |
if ( 'starred' === $user_types ) { |
| 111 |
$users = User_Query::starred_friends_subscriptions(); |
| 112 |
} elseif ( $folder > 0 ) { |
| 113 |
$users = User_Query::subscriptions_in_folder( $folder ); |
| 114 |
} else { |
| 115 |
$users = User_Query::all_subscriptions(); |
| 116 |
} |
| 117 |
|
| 118 |
if ( 0 === $users->get_total() && 'folders' !== $user_types ) { |
| 119 |
do_action( 'friends_widget_friend_list_after', $this, $args ); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
echo $args['before_widget']; |
| 124 |
|
| 125 |
if ( 'folders' === $user_types ) { |
| 126 |
$folders = Subscription::get_folders(); |
| 127 |
foreach ( $folders as $f ) { |
| 128 |
$folder_subs = User_Query::subscriptions_in_folder( $f->term_id ); |
| 129 |
if ( $folder_subs->get_total() > 0 ) { |
| 130 |
$this->list_friends( |
| 131 |
array_merge( $args, array( 'widget_id' => $widget_id . '-folder-' . $f->term_id ) ), |
| 132 |
'📁 ' . esc_html( $f->name ) . ' <span class="subscription-count">' . $folder_subs->get_total() . '</span>', |
| 133 |
$folder_subs |
| 134 |
); |
| 135 |
} |
| 136 |
} |
| 137 |
$unfoldered = User_Query::unfoldered_subscriptions(); |
| 138 |
if ( $unfoldered->get_total() > 0 ) { |
| 139 |
$this->list_friends( |
| 140 |
array_merge( $args, array( 'widget_id' => $widget_id . '-subscriptions' ) ), |
| 141 |
'<span class="dashicons dashicons-admin-users"></span> ' . sprintf( |
| 142 |
// translators: %s is the number of subscriptions. |
| 143 |
_n( 'Following %s', 'Following %s', $unfoldered->get_total(), 'friends' ), |
| 144 |
'<span class="subscription-count">' . $unfoldered->get_total() . '</span>' |
| 145 |
), |
| 146 |
$unfoldered |
| 147 |
); |
| 148 |
} |
| 149 |
} else { |
| 150 |
$folder_term = $folder > 0 ? get_term( $folder, Subscription::TAXONOMY ) : null; |
| 151 |
if ( $folder_term && ! is_wp_error( $folder_term ) ) { |
| 152 |
$title = '📁 ' . esc_html( $folder_term->name ) . ' <span class="subscription-count">' . $users->get_total() . '</span>'; |
| 153 |
} else { |
| 154 |
$title = '<span class="dashicons dashicons-admin-users"></span> ' . sprintf( |
| 155 |
// translators: %s is the number of subscriptions. |
| 156 |
_n( 'Following %s', 'Following %s', $users->get_total(), 'friends' ), |
| 157 |
'<span class="subscription-count">' . $users->get_total() . '</span>' |
| 158 |
); |
| 159 |
} |
| 160 |
$this->list_friends( |
| 161 |
array_merge( $args, array( 'widget_id' => $widget_id . '-subscriptions' ) ), |
| 162 |
$title, |
| 163 |
$users |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
echo $args['after_widget']; |
| 168 |
|
| 169 |
do_action( 'friends_widget_friend_list_after', $this, $args ); |
| 170 |
} |
| 171 |
} |
| 172 |
|