| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friend Stats Widget |
| 4 |
* |
| 5 |
* A widget that displays a stats of your friends. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
* @since 2.9.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Friends; |
| 12 |
|
| 13 |
/** |
| 14 |
* This is the class for the Friend Stats Widget. |
| 15 |
* |
| 16 |
* @package Friends |
| 17 |
* @author Alex Kirk |
| 18 |
*/ |
| 19 |
class Widget_Friend_Stats extends \WP_Widget { |
| 20 |
/** |
| 21 |
* Constructor |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
parent::__construct( |
| 25 |
'friends-widget-stats', |
| 26 |
__( 'Friend Stats', 'friends' ), |
| 27 |
array( |
| 28 |
'description' => __( 'Show stats about yourself and friends.', 'friends' ), |
| 29 |
) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Render the widget. |
| 35 |
* |
| 36 |
* @param array $args Sidebar arguments. |
| 37 |
* @param array $instance Widget instance settings. |
| 38 |
*/ |
| 39 |
public function widget( $args, $instance ) { |
| 40 |
$subscriptions = User_Query::all_subscriptions(); |
| 41 |
$subscriptions_count = $subscriptions->get_total(); |
| 42 |
|
| 43 |
$follower_count = 0; |
| 44 |
$blog_follower_count = 0; |
| 45 |
|
| 46 |
$instance = wp_parse_args( $instance, $this->defaults() ); |
| 47 |
$show_followers = false; |
| 48 |
$show_blog_followers = false; |
| 49 |
if ( class_exists( '\ActivityPub\Collection\Followers' ) && \defined( 'ACTIVITYPUB_ACTOR_MODE' ) ) { |
| 50 |
$activitypub_actor_mode = \get_option( 'activitypub_actor_mode', \ACTIVITYPUB_ACTOR_MODE ); |
| 51 |
if ( \ACTIVITYPUB_ACTOR_MODE === $activitypub_actor_mode || \ACTIVITYPUB_ACTOR_AND_BLOG_MODE === $activitypub_actor_mode ) { |
| 52 |
$follower_count = Feed_Parser_ActivityPub::count_followers( get_current_user_id() ); |
| 53 |
$show_followers = true; |
| 54 |
} |
| 55 |
if ( \ACTIVITYPUB_BLOG_MODE === $activitypub_actor_mode || \ACTIVITYPUB_ACTOR_AND_BLOG_MODE === $activitypub_actor_mode ) { |
| 56 |
if ( class_exists( '\ActivityPub\Collection\Actors' ) ) { |
| 57 |
$blog_follower_count = Feed_Parser_ActivityPub::count_followers( \ActivityPub\Collection\Actors::BLOG_USER_ID ); |
| 58 |
$show_blog_followers = true; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
echo $args['before_widget']; |
| 63 |
|
| 64 |
$open = true; |
| 65 |
$widget_id = ''; |
| 66 |
if ( ! empty( $args['widget_id'] ) ) { |
| 67 |
$widget_id = $args['widget_id']; |
| 68 |
$open = Frontend::get_widget_open_state( $widget_id ); |
| 69 |
} |
| 70 |
?> |
| 71 |
<details class="accordion" <?php echo esc_attr( $open ); ?> data-id="<?php echo esc_attr( $widget_id ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends_widget_state' ) ); ?>"> |
| 72 |
<summary class="accordion-header"> |
| 73 |
<?php |
| 74 |
echo $args['before_title']; |
| 75 |
echo '<span class="dashicons dashicons-chart-area"></span> '; |
| 76 |
echo wp_kses( |
| 77 |
$instance['title'], |
| 78 |
array( |
| 79 |
'span' => array( 'class' => array() ), |
| 80 |
'a' => array( |
| 81 |
'class' => array(), |
| 82 |
'href' => array(), |
| 83 |
), |
| 84 |
) |
| 85 |
); |
| 86 |
echo $args['after_title']; |
| 87 |
?> |
| 88 |
</summary> |
| 89 |
<ul class="friend-stats menu menu-nav"> |
| 90 |
<?php if ( $show_followers ) : ?> |
| 91 |
<li class="friend-stats-mutual menu-item"> |
| 92 |
<a href="<?php echo esc_url( home_url( '/friends/mutual/' ) ); ?>"> |
| 93 |
<?php |
| 94 |
$mutual_count = Feed_Parser_ActivityPub::count_mutual_followers( get_current_user_id() ); |
| 95 |
echo esc_html( |
| 96 |
sprintf( |
| 97 |
/* translators: %s: number of mutual friends */ |
| 98 |
_n( '%s Friend', '%s Friends', $mutual_count, 'friends' ), |
| 99 |
$mutual_count |
| 100 |
) |
| 101 |
); |
| 102 |
?> |
| 103 |
</a> |
| 104 |
</li> |
| 105 |
<li class="friend-stats-followers menu-item"> |
| 106 |
<a class="followers" href="<?php echo esc_url( home_url( '/friends/followers/' ) ); ?>"> |
| 107 |
<?php |
| 108 |
echo esc_html( |
| 109 |
sprintf( |
| 110 |
/* translators: %s: number of followers */ |
| 111 |
_n( '%s Follower', '%s Followers', $follower_count, 'friends' ), |
| 112 |
$follower_count |
| 113 |
) |
| 114 |
); |
| 115 |
?> |
| 116 |
</a> |
| 117 |
</li> |
| 118 |
<?php endif; ?> |
| 119 |
<?php if ( $show_blog_followers ) : ?> |
| 120 |
<li class="friend-stats-followers menu-item"> |
| 121 |
<a class="followers" href="<?php echo esc_url( home_url( '/friends/blog-followers/' ) ); ?>"> |
| 122 |
<?php |
| 123 |
echo esc_html( |
| 124 |
sprintf( |
| 125 |
/* translators: %s: number of followers */ |
| 126 |
_n( '%s Blog Follower', '%s Blog Followers', $blog_follower_count, 'friends' ), |
| 127 |
$blog_follower_count |
| 128 |
) |
| 129 |
); |
| 130 |
?> |
| 131 |
</a> |
| 132 |
</li> |
| 133 |
<?php endif; ?> |
| 134 |
|
| 135 |
<li class="friend-stats-subscriptions menu-item"> |
| 136 |
<a href="<?php echo esc_attr( home_url( '/friends/following/' ) ); ?>"> |
| 137 |
<?php |
| 138 |
echo wp_kses( |
| 139 |
sprintf( |
| 140 |
/* translators: %s: number of subscriptions */ |
| 141 |
_n( '%s Following', '%s Following', $subscriptions_count, 'friends' ), |
| 142 |
'<a class="subscriptions">' . $subscriptions_count . '</a>' |
| 143 |
), |
| 144 |
array( 'span' => array( 'class' => true ) ) |
| 145 |
); |
| 146 |
?> |
| 147 |
</a> |
| 148 |
</li> |
| 149 |
|
| 150 |
</ul> |
| 151 |
</details> |
| 152 |
<?php |
| 153 |
|
| 154 |
do_action( 'friends_widget_starred_friend_list_after', $this, $args ); |
| 155 |
|
| 156 |
echo $args['after_widget']; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Update widget configuration. |
| 161 |
* |
| 162 |
* @param array $new_instance New settings. |
| 163 |
* @param array $old_instance Old settings. |
| 164 |
* @return array Sanitized instance settings. |
| 165 |
*/ |
| 166 |
public function update( $new_instance, $old_instance ) { |
| 167 |
$instance = $this->defaults(); |
| 168 |
|
| 169 |
return $instance; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Return an associative array of default values |
| 174 |
* |
| 175 |
* These values are used in new widgets. |
| 176 |
* |
| 177 |
* @return array Array of default values for the Widget's options |
| 178 |
*/ |
| 179 |
public function defaults() { |
| 180 |
return array( |
| 181 |
'title' => __( 'Friend Stats', 'friends' ), |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Register this widget. |
| 187 |
*/ |
| 188 |
public static function register() { |
| 189 |
register_widget( __CLASS__ ); |
| 190 |
} |
| 191 |
} |
| 192 |
|