| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blockroll integration. |
| 4 |
* |
| 5 |
* @package Friends |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Friends; |
| 9 |
|
| 10 |
/** |
| 11 |
* Provides Friends subscriptions as a Blockroll source. |
| 12 |
*/ |
| 13 |
class Blockroll { |
| 14 |
const HIDE_META = 'hide_from_blockroll'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Register hooks. |
| 18 |
*/ |
| 19 |
public static function init() { |
| 20 |
add_filter( 'blockroll_sources', array( __CLASS__, 'sources' ) ); |
| 21 |
add_filter( 'blockroll_source_help', array( __CLASS__, 'source_help' ), 10, 2 ); |
| 22 |
add_filter( 'blockroll_source_help_url', array( __CLASS__, 'source_help_url' ), 10, 2 ); |
| 23 |
add_filter( 'blockroll_source_links', array( __CLASS__, 'source_links' ), 10, 2 ); |
| 24 |
add_action( 'friends_subscription_actions', array( __CLASS__, 'subscription_action' ) ); |
| 25 |
add_action( 'friends_edit_friend_table_end', array( __CLASS__, 'settings_table_row' ) ); |
| 26 |
add_action( 'friends_edit_feeds_table_end', array( __CLASS__, 'settings_table_row' ) ); |
| 27 |
add_action( 'friends_edit_friend_after_form_submit', array( __CLASS__, 'save_settings' ) ); |
| 28 |
add_action( 'friends_edit_feeds_after_form_submit', array( __CLASS__, 'save_settings' ) ); |
| 29 |
add_action( 'wp_ajax_friends-blockroll-visibility', array( __CLASS__, 'ajax_visibility' ) ); |
| 30 |
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_script' ), 20 ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register the Friends subscriptions source. |
| 35 |
* |
| 36 |
* @param array $sources Source slug => source label. |
| 37 |
* @return array Sources. |
| 38 |
*/ |
| 39 |
public static function sources( $sources ) { |
| 40 |
$sources['friends-subscriptions'] = __( 'Friends subscriptions', 'friends' ); |
| 41 |
return $sources; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Provide help text for the Friends subscriptions source. |
| 46 |
* |
| 47 |
* @param string $help Current help text. |
| 48 |
* @param string $source Selected source. |
| 49 |
* @return string Help text. |
| 50 |
*/ |
| 51 |
public static function source_help( $help, $source ) { |
| 52 |
if ( 'friends-subscriptions' !== $source ) { |
| 53 |
return $help; |
| 54 |
} |
| 55 |
|
| 56 |
return __( 'To hide someone from this Blogroll, choose "Hide from Blogroll" for that subscription.', 'friends' ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Provide a help URL for the Friends subscriptions source. |
| 61 |
* |
| 62 |
* @param string $url Current help URL. |
| 63 |
* @param string $source Selected source. |
| 64 |
* @return string Help URL. |
| 65 |
*/ |
| 66 |
public static function source_help_url( $url, $source ) { |
| 67 |
if ( 'friends-subscriptions' !== $source ) { |
| 68 |
return $url; |
| 69 |
} |
| 70 |
|
| 71 |
return home_url( '/friends/following/' ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Provide Friends subscriptions as Blockroll links. |
| 76 |
* |
| 77 |
* @param array $links Current links. |
| 78 |
* @param string $source Selected source. |
| 79 |
* @return array Links. |
| 80 |
*/ |
| 81 |
public static function source_links( $links, $source ) { |
| 82 |
if ( 'friends-subscriptions' !== $source ) { |
| 83 |
return $links; |
| 84 |
} |
| 85 |
|
| 86 |
$links = array(); |
| 87 |
foreach ( User_Query::all_subscriptions()->get_results() as $subscription ) { |
| 88 |
if ( ! $subscription->user_url || self::is_hidden( $subscription ) ) { |
| 89 |
continue; |
| 90 |
} |
| 91 |
|
| 92 |
$links[] = array( |
| 93 |
'url' => $subscription->user_url, |
| 94 |
'name' => $subscription->display_name ? $subscription->display_name : $subscription->user_login, |
| 95 |
'description' => $subscription->description, |
| 96 |
'feedUrl' => self::feed_url( $subscription ), |
| 97 |
'photo' => $subscription->get_avatar_url(), |
| 98 |
'xfn' => array(), |
| 99 |
'added' => $subscription->user_registered, |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
return $links; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Render the Blockroll visibility toggle for a subscription. |
| 108 |
* |
| 109 |
* @param User $subscription Friend user or virtual subscription. |
| 110 |
*/ |
| 111 |
public static function subscription_action( User $subscription ) { |
| 112 |
if ( ! self::is_available() || ! $subscription instanceof Subscription ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
$hidden = self::is_hidden( $subscription ); |
| 117 |
?> |
| 118 |
<span class="subscription-actions"> |
| 119 |
<label class="subscription-action subscription-blockroll-visibility"> |
| 120 |
<input |
| 121 |
type="checkbox" |
| 122 |
class="friends-blockroll-visibility" |
| 123 |
value="1" |
| 124 |
data-id="<?php echo esc_attr( $subscription->user_login ); ?>" |
| 125 |
data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends-blockroll-visibility-' . $subscription->user_login ) ); ?>" |
| 126 |
<?php checked( $hidden ); ?> |
| 127 |
/> |
| 128 |
<span><?php echo esc_html( self::visibility_label( $hidden ) ); ?></span> |
| 129 |
</label> |
| 130 |
</span> |
| 131 |
<?php |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Render the Blockroll visibility setting on friend settings forms. |
| 136 |
* |
| 137 |
* @param User $subscription Friend user or virtual subscription. |
| 138 |
*/ |
| 139 |
public static function settings_table_row( User $subscription ) { |
| 140 |
if ( ! self::is_available() || ! $subscription instanceof Subscription ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
$hidden = self::is_hidden( $subscription ); |
| 145 |
?> |
| 146 |
<tr> |
| 147 |
<th scope="row"><?php esc_html_e( 'Blogroll', 'friends' ); ?></th> |
| 148 |
<td> |
| 149 |
<label for="hide_from_blockroll"> |
| 150 |
<input name="hide_from_blockroll" type="checkbox" id="hide_from_blockroll" value="1" <?php checked( $hidden ); ?>> |
| 151 |
<?php echo esc_html( self::visibility_label( $hidden ) ); ?> |
| 152 |
</label> |
| 153 |
</td> |
| 154 |
</tr> |
| 155 |
<?php |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Save the Blockroll visibility setting from friend settings forms. |
| 160 |
* |
| 161 |
* @param User $subscription Friend user or virtual subscription. |
| 162 |
*/ |
| 163 |
public static function save_settings( User $subscription ) { |
| 164 |
if ( ! self::is_available() || ! $subscription instanceof Subscription ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
self::set_hidden( $subscription, isset( $_POST['hide_from_blockroll'] ) && boolval( $_POST['hide_from_blockroll'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Enqueue the front-end behavior for the Blockroll visibility toggle. |
| 173 |
*/ |
| 174 |
public static function enqueue_script() { |
| 175 |
if ( ! self::is_available() || ! Friends::on_frontend() ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
wp_add_inline_script( |
| 180 |
'friends', |
| 181 |
<<<'JS' |
| 182 |
( function ( $, wp ) { |
| 183 |
$( document ).on( 'change', '.friends-blockroll-visibility', function () { |
| 184 |
const input = $( this ); |
| 185 |
const label = input.closest( 'label' ).find( 'span' ); |
| 186 |
const previous = ! input.prop( 'checked' ); |
| 187 |
|
| 188 |
input.prop( 'disabled', true ); |
| 189 |
wp.ajax.send( 'friends-blockroll-visibility', { |
| 190 |
data: { |
| 191 |
friend_id: input.data( 'id' ), |
| 192 |
hidden: input.prop( 'checked' ) ? 1 : 0, |
| 193 |
_ajax_nonce: input.data( 'nonce' ), |
| 194 |
}, |
| 195 |
success( response ) { |
| 196 |
input.prop( 'checked', !! response.hidden ); |
| 197 |
label.text( response.label ); |
| 198 |
}, |
| 199 |
error() { |
| 200 |
input.prop( 'checked', previous ); |
| 201 |
}, |
| 202 |
} ).always( function () { |
| 203 |
input.prop( 'disabled', false ); |
| 204 |
} ); |
| 205 |
} ); |
| 206 |
} )( jQuery, wp ); |
| 207 |
JS |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Ajax handler to hide or show a subscription in Blockroll. |
| 213 |
*/ |
| 214 |
public static function ajax_visibility() { |
| 215 |
if ( ! current_user_can( Friends::REQUIRED_ROLE ) || ! self::is_available() || ! isset( $_POST['friend_id'] ) || ! isset( $_POST['hidden'] ) ) { |
| 216 |
wp_send_json_error(); |
| 217 |
exit; |
| 218 |
} |
| 219 |
|
| 220 |
$friend_id = sanitize_text_field( wp_unslash( $_POST['friend_id'] ) ); |
| 221 |
check_ajax_referer( "friends-blockroll-visibility-$friend_id" ); |
| 222 |
|
| 223 |
$friend_user = User::get_by_username( $friend_id ); |
| 224 |
if ( ! $friend_user || is_wp_error( $friend_user ) || ! ( $friend_user instanceof Subscription ) ) { |
| 225 |
wp_send_json_error( 'invalid-user' ); |
| 226 |
exit; |
| 227 |
} |
| 228 |
|
| 229 |
self::set_hidden( $friend_user, boolval( $_POST['hidden'] ) ); |
| 230 |
$hidden = self::is_hidden( $friend_user ); |
| 231 |
|
| 232 |
wp_send_json_success( |
| 233 |
array( |
| 234 |
'hidden' => $hidden, |
| 235 |
'label' => self::visibility_label( $hidden ), |
| 236 |
) |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Get a representative feed URL for a subscription. |
| 242 |
* |
| 243 |
* @param User $subscription Friend user or virtual subscription. |
| 244 |
* @return string Feed URL. |
| 245 |
*/ |
| 246 |
private static function feed_url( User $subscription ) { |
| 247 |
foreach ( $subscription->get_active_feeds() as $feed ) { |
| 248 |
return $feed->get_url(); |
| 249 |
} |
| 250 |
|
| 251 |
foreach ( $subscription->get_feeds() as $feed ) { |
| 252 |
return $feed->get_url(); |
| 253 |
} |
| 254 |
|
| 255 |
return ''; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Whether Blockroll is available. |
| 260 |
* |
| 261 |
* @return bool True if Blockroll is active. |
| 262 |
*/ |
| 263 |
public static function is_available() { |
| 264 |
return defined( 'BLOCKROLL_PLUGIN_FILE' ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Get the label for the Blockroll visibility toggle. |
| 269 |
* |
| 270 |
* @param bool $hidden Whether the subscription is hidden from Blockroll. |
| 271 |
* @return string Toggle label. |
| 272 |
*/ |
| 273 |
private static function visibility_label( $hidden ) { |
| 274 |
return $hidden ? __( 'Hidden from Blogroll', 'friends' ) : __( 'Hide from Blogroll', 'friends' ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Whether a subscription is hidden from Blockroll for the current user. |
| 279 |
* |
| 280 |
* @param User $subscription Friend user or virtual subscription. |
| 281 |
* @return bool True if hidden from Blockroll. |
| 282 |
*/ |
| 283 |
public static function is_hidden( User $subscription ) { |
| 284 |
if ( ! $subscription instanceof Subscription ) { |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
return (bool) get_term_meta( $subscription->get_term_id(), self::HIDE_META, true ); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Set whether a subscription is hidden from Blockroll. |
| 293 |
* |
| 294 |
* @param User $subscription Friend user or virtual subscription. |
| 295 |
* @param bool $hide Whether to hide the subscription. |
| 296 |
*/ |
| 297 |
public static function set_hidden( User $subscription, $hide ) { |
| 298 |
if ( ! $subscription instanceof Subscription ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
if ( $hide ) { |
| 303 |
update_term_meta( $subscription->get_term_id(), self::HIDE_META, true ); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
delete_term_meta( $subscription->get_term_id(), self::HIDE_META ); |
| 308 |
} |
| 309 |
} |
| 310 |
|