-
Notifications
You must be signed in to change notification settings - Fork 14
/
class-user.php
860 lines (765 loc) · 23.9 KB
/
class-user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
<?php
/**
* Friend User
*
* This wraps \WP_User and adds friend specific functions.
*
* @package Friends
*/
namespace Friends;
/**
* This is the class for the User part of the Friends Plugin.
*
* @since 0.21
*
* @package Friends
* @author Alex Kirk
*/
class User extends \WP_User {
/**
* Caches the feed rules.
*
* @var array
*/
public static $feed_rules = array();
/**
* Caches the feed catch all action.
*
* @var array
*/
public static $feed_catch_all = array();
/**
* Create a User with a specific Friends-related role
*
* @param string $user_login The user login.
* @param string $role The role: subscription,
* pending_friend_request,
* or friend_request.
* @param string $url The site URL for which
* to create the user.
* @param string $display_name The user's display name.
* @param string $icon_url The user_icon_url URL.
*
* @return User|\WP_Error The created user or an error.
*/
public static function create( $user_login, $role, $url, $display_name = null, $icon_url = null ) {
$role_rank = array_flip(
array(
'subscription',
'pending_friend_request',
'friend_request',
)
);
if ( ! isset( $role_rank[ $role ] ) ) {
return new \WP_Error( 'invalid_role', 'Invalid role for creation specified' );
}
if ( is_multisite() ) {
$user = get_user_by( 'login', $user_login );
if ( $user && ! self::is_friends_plugin_user( $user ) ) {
if ( ! is_user_member_of_blog( $user->ID, get_current_blog_id() ) ) {
add_user_to_blog( get_current_blog_id(), $user->ID, $role );
}
}
}
$friend_user = self::get_user( $user_login );
if ( $friend_user && ! is_wp_error( $friend_user ) ) {
foreach ( $role_rank as $_role => $rank ) {
if ( $rank > $role_rank[ $role ] ) {
break;
}
if ( $friend_user->has_cap( $_role ) ) {
// Upgrade user role.
$friend_user->set_role( $role );
break;
}
}
return $friend_user;
}
$userdata = array(
'user_login' => $user_login,
'display_name' => $display_name,
'first_name' => $display_name,
'nickname' => $display_name,
'user_url' => $url,
'user_pass' => wp_generate_password( 256 ),
'role' => $role,
);
$friend_id = wp_insert_user( $userdata );
update_user_option( $friend_id, 'friends_new_friend', true );
$friend_user = new User( $friend_id );
$friend_user->update_user_icon_url( $icon_url );
return $friend_user;
}
/**
* Convert a site URL to a username
*
* @param string $url The site URL in question.
* @return string The corresponding username.
*/
public static function get_user_login_for_url( $url ) {
$multisite_user = self::get_multisite_user( $url );
if ( $multisite_user ) {
return $multisite_user->user_login;
}
$user_login = self::sanitize_username( self::get_display_name_for_url( $url ) );
return $user_login;
}
/**
* Convert a site URL to a display name
*
* @param string $url The site URL in question.
* @return string The corresponding display name.
*/
public static function get_display_name_for_url( $url ) {
$multisite_user = self::get_multisite_user( $url );
if ( $multisite_user ) {
return $multisite_user->display_name;
}
$host = wp_parse_url( $url, PHP_URL_HOST );
$path = wp_parse_url( $url, PHP_URL_PATH );
$display_name = sanitize_text_field( preg_replace( '#^www\.#', '', preg_replace( '#[^a-z0-9.-]+#i', ' ', strtolower( $host . ' ' . $path ) ) ) );
return $display_name;
}
/**
* Discover a display name from feeds
*
* @param array $feeds A list of feeds.
* @return string The corresponding display name.
*/
public static function get_display_name_from_feeds( $feeds ) {
foreach ( $feeds as $feed ) {
if ( 'self' === $feed['rel'] ) {
return sanitize_text_field( $feed['title'] );
}
}
return false;
}
/**
* If the URL is on the same multisite, get the main user.
*
* @param string $url The site URL in question.
* @return bool|WP_User false or the user.
*/
public static function get_multisite_user( $url ) {
if ( ! is_multisite() ) {
return false;
}
$host = wp_parse_url( $url, PHP_URL_HOST );
$path = wp_parse_url( $url, PHP_URL_PATH );
$site_id = get_blog_id_from_url( $host, trailingslashit( $path ) );
if ( ! $site_id ) {
return false;
}
// If the friend was added via URL.
switch_to_blog( $site_id );
$friend_user_id = Friends::get_main_friend_user_id();
restore_current_blog();
return get_user_by( 'id', $friend_user_id );
}
/**
* Sanitize the username according to some more rules than just sanitize_user()
*
* @param string $username The username.
*
* @return string The sanitized username.
*/
public static function sanitize_username( $username ) {
$username = preg_replace( '/[^a-z0-9.]+/', '-', strtolower( $username ) );
$username = sanitize_user( $username );
return $username;
}
/**
* Determines whether the specified user is a friends plugin user.
*
* @param \WP_User $user The user.
*
* @return bool True if the specified user is a friends plugin user, False otherwise.
*/
public static function is_friends_plugin_user( \WP_User $user ) {
return $user->has_cap( 'friend' ) || $user->has_cap( 'pending_friend_request' ) || $user->has_cap( 'friend_request' ) || $user->has_cap( 'subscription' );
}
/**
* Get a friend user for a user_login.
*
* @param string $user_login The user login.
* @return User|false The friend user or false.
*/
public static function get_user( $user_login ) {
$user = get_user_by( 'login', $user_login );
if ( $user ) {
if ( self::is_friends_plugin_user( $user ) ) {
return new self( $user );
}
return false;
}
return $user;
}
/**
* Get a friend user for a user_id.
*
* @param string $user_id The user ID.
* @return User|false The friend user or false.
*/
public static function get_user_by_id( $user_id ) {
$user = get_user_by( 'ID', $user_id );
if ( $user ) {
if ( $user->has_cap( 'friend' ) || $user->has_cap( 'pending_friend_request' ) || $user->has_cap( 'friend_request' ) || $user->has_cap( 'subscription' ) ) {
return new self( $user );
}
return false;
}
return $user;
}
public function __get( $key ) {
if ( 'user_url' === $key && empty( $this->data->user_url ) && is_multisite() ) {
$site = get_active_blog_for_user( $this->ID );
if ( ! $site ) {
var_dump( $this->ID );
exit;
}
// Ensure we're using the same URL protocol.
$this->data->user_url = set_url_scheme( $site->siteurl );
return $this->data->user_url;
}
return parent::__get( $key );
}
/**
* Sends a message to the friend..
*
* @param string $message The message.
* @param string $subject The subject.
*
* @return \WP_Error|bool True if the message was sent successfully.
*/
public function send_message( $message, $subject = null ) {
$friends = Friends::get_instance();
return $friends->messages->send_message( $this, $message, $subject );
}
/**
* Save multiple feeds for a user.
*
* @param string $feeds The feed URLs to subscribe to.
*
* @return \WP_User|\WP_error $user The new associated user or an error object.
*/
public function save_feeds( $feeds = array() ) {
$errors = new \WP_Error();
foreach ( $feeds as $feed_url => $options ) {
if ( ! is_string( $feed_url ) || ! Friends::check_url( $feed_url ) ) {
$errors->add( 'invalid-url', 'An invalid URL was provided' );
unset( $feeds[ $feed_url ] );
continue;
}
$default_options = array(
'active' => false,
'parser' => 'simplepie',
'post-format' => 'standard',
'mime-type' => 'application/rss+xml',
'title' => $this->display_name . ' RSS Feed',
);
$feed_options = array();
foreach ( $default_options as $key => $value ) {
if ( isset( $options[ $key ] ) ) {
$feed_options[ $key ] = $options[ $key ];
} else {
$feed_options[ $key ] = $value;
}
}
$feeds[ $feed_url ] = $feed_options;
}
$user_feeds = User_Feed::save_multiple(
$this,
$feeds
);
if ( $errors->has_errors() ) {
return $errors;
}
return $user_feeds;
}
/**
* Save a feed url for a user.
*
* @param string $feed_url The feed URL to subscribe to.
* @param array $options The options.
*
* @return \WP_User|\WP_error $user The new associated user or an error object.
*/
public function save_feed( $feed_url, $options = array() ) {
if ( ! is_string( $feed_url ) || ! Friends::check_url( $feed_url ) ) {
return new \WP_Error( 'invalid-url', 'An invalid URL was provided' );
}
$default_options = array(
'active' => false,
'parser' => 'simplepie',
'post-format' => 'standard',
'mime-type' => 'application/rss+xml',
'title' => $this->display_name . ' RSS Feed',
);
$feed_options = array();
foreach ( $default_options as $key => $value ) {
if ( isset( $options[ $key ] ) ) {
$feed_options[ $key ] = $options[ $key ];
} else {
$feed_options[ $key ] = $value;
}
}
$user_feed = User_Feed::save(
$this,
$feed_url,
$feed_options
);
return $user_feed;
}
/**
* Subscribe to a friends site without becoming a friend
*
* @param string $feed_url The feed URL to subscribe to.
* @param array $options The options.
*
* @return \WP_User|\WP_error $user The new associated user or an error object.
*/
public function subscribe( $feed_url, $options = array() ) {
$options['active'] = true;
return $this->save_feed( $feed_url, $options );
}
/**
* Retrieve the posts for this user
*
* @return array The new posts.
*/
public function retrieve_posts() {
$friends = Friends::get_instance();
$new_posts = array();
foreach ( $this->get_active_feeds() as $feed ) {
$posts = $friends->feed->retrieve_feed( $feed );
if ( ! is_wp_error( $posts ) ) {
$new_posts = array_merge( $new_posts, $posts );
}
}
$this->delete_outdated_posts();
return $new_posts;
}
/**
* Delete posts the user decided to automatically delete.
*/
public function delete_outdated_posts() {
$count = 0;
if ( $this->is_retention_days_enabled() ) {
$date_before = gmdate( 'Y-m-d H:i:s', strtotime( '-' . ( $this->get_retention_days() * 24 ) . 'hours' ) );
$args = array(
'post_type' => Friends::CPT,
'author' => $this->ID,
'nopaging' => true,
'date_query' => array(
'before' => $date_before,
),
);
$query = new \WP_Query( $args );
while ( $query->have_posts() ) {
$count ++;
$query->the_post();
if ( apply_filters( 'friends_debug', false ) ) {
echo 'Deleting ', get_the_ID(), '<br/>';
}
wp_delete_post( get_the_ID(), true );
}
}
if ( $this->is_retention_number_enabled() ) {
$args = array(
'post_type' => Friends::CPT,
'author' => $this->ID,
'offset' => $this->get_retention_number(),
);
$query = new \WP_Query( $args );
while ( $query->have_posts() ) {
$count ++;
$query->the_post();
if ( apply_filters( 'friends_debug', false ) ) {
echo 'Deleting ', get_the_ID(), '<br/>';
}
wp_delete_post( get_the_ID(), true );
}
}
}
/**
* Check whether the retention by number of posts is enabled.
*
* @return boolean Whether the retention by number of posts is enabled.
*/
public function is_retention_number_enabled() {
return $this->get_user_option( 'friends_enable_retention_number' );
}
/**
* Enable or disable the retention by number of posts.
*
* @param boolean $enabled Whether the retention by number of posts should be enabled.
* @return boolean Whether the retention by number of posts is enabled.
*/
public function set_retention_number_enabled( $enabled ) {
$this->update_user_option( 'friends_enable_retention_number', boolval( $enabled ) );
return boolval( $enabled );
}
/**
* Get the retention by number of posts.
*
* @return int The retention by number of posts.
*/
public function get_retention_number() {
$number = $this->get_user_option( 'friends_retention_number' );
if ( $number <= 0 ) {
return 200;
}
return $number;
}
/**
* Set the retention by number of posts.
*
* @param int $number The retention by number of posts.
* @return int The retention by number of posts.
*/
public function set_retention_number( $number ) {
$number = max( 1, $number );
$this->update_user_option( 'friends_retention_number', $number );
return $number;
}
/**
* Check whether the retention by days of posts is enabled.
*
* @return boolean Whether the retention by days of posts is enabled.
*/
public function is_retention_days_enabled() {
return $this->get_user_option( 'friends_enable_retention_days' );
}
/**
* Enable or disable the retention by days of posts.
*
* @param boolean $enabled Whether the retention by days of posts should be enabled.
* @return boolean Whether the retention by days of posts is enabled.
*/
public function set_retention_days_enabled( $enabled ) {
$this->update_user_option( 'friends_enable_retention_days', boolval( $enabled ) );
return boolval( $enabled );
}
/**
* Get the retention by days of posts.
*
* @return int The retention by days of posts.
*/
public function get_retention_days() {
$days = $this->get_user_option( 'friends_retention_days' );
if ( $days <= 0 ) {
return 14;
}
return $days;
}
/**
* Set the retention by days of posts.
*
* @param int $days The retention by days of posts.
* @return int The retention by days of posts.
*/
public function set_retention_days( $days ) {
$days = max( 1, intval( $days ) );
return $this->update_user_option( 'friends_retention_days', $days );
return $days;
}
/**
* Gets the post counts by post format.
*
* @return array The post counts.
*/
public function get_post_count_by_post_format() {
$friends = Friends::get_instance();
return $friends->get_post_count_by_post_format( $this->ID );
}
/**
* Update a friend's avatar URL
*
* @param string $user_icon_url The user icon URL.
* @return string|false The URL that was set or false.
*/
public function update_user_icon_url( $user_icon_url ) {
if ( ! $user_icon_url ) {
$user_icon_url = Mf2\resolveUrl( $this->user_url, '/favicon.ico' );
}
if ( $user_icon_url && Friends::check_url( $user_icon_url ) ) {
if ( $this->has_cap( 'friend' ) || $this->has_cap( 'pending_friend_request' ) || $this->has_cap( 'friend_request' ) || $this->has_cap( 'subscription' ) ) {
$icon_host_parts = array_reverse( explode( '.', parse_url( strtolower( $user_icon_url ), PHP_URL_HOST ) ) );
if ( 'gravatar.com' === $icon_host_parts[1] . '.' . $icon_host_parts[0] ) {
update_user_option( $this->ID, 'friends_user_icon_url', $user_icon_url );
return $user_icon_url;
}
$user_host_parts = array_reverse( explode( '.', parse_url( strtolower( $this->user_url ), PHP_URL_HOST ) ) );
if ( $user_host_parts[1] . '.' . $user_host_parts[0] === $icon_host_parts[1] . '.' . $icon_host_parts[0] ) {
update_user_option( $this->ID, 'friends_user_icon_url', $user_icon_url );
return $user_icon_url;
}
} elseif ( $this->has_cap( 'subscription' ) ) {
update_user_option( $this->ID, 'friends_user_icon_url', $user_icon_url );
return $user_icon_url;
}
}
return false;
}
/**
* Retrieve the rules for this feed.
*
* @return array The rules set by the user for this feed.
*/
public function get_feed_rules() {
$friends = Friends::get_instance();
if ( ! isset( self::$feed_rules[ $this->ID ] ) ) {
self::$feed_rules[ $this->ID ] = $friends->feed->validate_feed_rules( get_option( 'friends_feed_rules_' . $this->ID ) );
}
return self::$feed_rules[ $this->ID ];
}
/**
* Retrieve the catch_all value for this feed.
*
* @return array The rules set by the user for this feed.
*/
public function get_feed_catch_all() {
$friends = Friends::get_instance();
if ( ! isset( self::$feed_catch_all[ $this->ID ] ) ) {
self::$feed_catch_all[ $this->ID ] = $friends->feed->validate_feed_catch_all( get_option( 'friends_feed_catch_all_' . $this->ID ) );
}
return self::$feed_catch_all[ $this->ID ];
}
/**
* Retrieve the remote post ids.
*
* @return array A mapping of the remote post ids.
*/
public function get_remote_post_ids() {
$remote_post_ids = array();
$existing_posts = new \WP_Query(
array(
'post_type' => Friends::CPT,
'post_status' => array( 'publish', 'private', 'trash' ),
'author' => $this->ID,
'nopaging' => true,
)
);
if ( $existing_posts->have_posts() ) {
while ( $existing_posts->have_posts() ) {
$post = $existing_posts->next_post();
$remote_post_id = get_post_meta( $post->ID, 'remote_post_id', true );
if ( $remote_post_id ) {
$remote_post_ids[ $remote_post_id ] = $post->ID;
}
$permalink = get_permalink( $post );
$remote_post_ids[ $permalink ] = $post->ID;
$permalink = str_replace( array( '&', '&' ), '&', ent2ncr( $permalink ) );
$remote_post_ids[ $permalink ] = $post->ID;
}
}
do_action( 'friends_remote_post_ids', $remote_post_ids );
return $remote_post_ids;
}
/**
* Get the user's feeds (and potentially convert old-style feed URL).
*
* @return array An array of User_Feed items.
*/
public function get_feeds() {
$feeds = User_Feed::get_for_user( $this );
if ( empty( $feeds ) ) {
$feeds = User_Feed::convert_user( $this );
}
return $feeds;
}
/**
* Get just the user's active feeds.
*
* @return array An array of active User_Feed items.
*/
public function get_active_feeds() {
$active_feeds = array();
foreach ( $this->get_feeds() as $feed ) {
if ( $feed->is_active() ) {
$active_feeds[] = $feed;
}
}
return $active_feeds;
}
/**
* Determines whether the user can have feeds refreshed.
*
* @return bool True if able to refresh feeds, False otherwise.
*/
public function can_refresh_feeds() {
return $this->has_cap( 'subscription' ) ||
$this->has_cap( 'acquaintance' ) ||
$this->has_cap( 'friend' ) ||
$this->has_cap( 'pending_friend_request' );
}
/**
* Convert a user to a friend
*
* @param string $out_token The token to authenticate against the remote.
* @param string $in_token The token the remote needs to use to authenticate to us.
*/
public function make_friend( $out_token, $in_token ) {
$this->update_user_option( 'friends_out_token', $out_token );
if ( $this->update_user_option( 'friends_in_token', $in_token ) ) {
update_option( 'friends_in_token_' . $in_token, $this->ID );
}
$this->set_role( get_option( 'friends_default_friend_role', 'friend' ) );
}
/**
* Check whether this is a valid friend
*
* @return boolean Whether the user has valid friend data.
*/
public function is_valid_friend() {
if ( ! $this->has_cap( 'friend' ) ) {
return false;
}
if ( ! $this->data->user_url ) {
return false;
}
if ( ! $this->get_user_option( 'friends_in_token' ) ) {
return false;
}
if ( ! $this->get_user_option( 'friends_out_token' ) ) {
return false;
}
return true;
}
/**
* Gets the role name (for a specific count).
*
* @param bool $group_subscriptions Whether to group all types of subscriptions into the name "Subscriptions".
* @param int $count The count if more than one.
*
* @return string The role name.
*/
public function get_role_name( $group_subscriptions = false, $count = 1 ) {
$name = false;
if ( ! $name && in_array( 'acquaintance', $this->roles ) ) {
return _nx( 'Acquaintance', 'Acquaintances', $count, 'User role', 'friends' );
}
if ( ! $name && in_array( 'friend', $this->roles ) && $this->is_valid_friend() ) {
return _nx( 'Friend', 'Friends', $count, 'User role', 'friends' );
}
if ( ! $name && in_array( 'subscription', $this->roles ) || ( $group_subscriptions && ( in_array( 'friend_request', $this->roles ) || in_array( 'pending_friend_request', $this->roles ) ) ) ) {
return _nx( 'Subscription', 'Subscriptions', $count, 'User role', 'friends' );
}
if ( ! $name && in_array( 'friend_request', $this->roles ) ) {
return _nx( 'Friend Request', 'Friend Requests', $count, 'User role', 'friends' );
}
if ( ! $name && in_array( 'pending_friend_request', $this->roles ) ) {
return _nx( 'Pending Friend Request', 'Pending Friend Requests', $count, 'User role', 'friends' );
}
$name = apply_filters( 'friend_user_role_name', $name, $this );
if ( ! $name ) {
$name = _x( 'Unknown', 'User role', 'friends' );
}
return $name;
}
/**
* Gets the local friends page url.
*
* @param integer $post_id The post identifier.
*
* @return string The local friends page url.
*/
function get_local_friends_page_url( $post_id = null ) {
$path = '/';
if ( $post_id ) {
$path = '/' . $post_id . '/';
}
return home_url( '/friends/' . self::get_user_login_for_url( $this->user_login ) . $path );
}
/**
* Gets the local friends page url for a post format.
*
* @param string $post_format The post format.
*
* @return string The local friends page url.
*/
function get_local_friends_page_post_format_url( $post_format ) {
return home_url( '/friends/' . $this->user_login . '/type/' . $post_format . '/' );
}
/**
* Gets the friend auth to be used as a GET parameter.
*
* @param integer $validity The validity in seconds.
*
* @return string The friend auth.
*/
function get_friend_auth( $validity = 3600 ) {
$friends = Friends::get_instance();
$friend_auth = $friends->access_control->get_friend_auth( $this, $validity );
if ( empty( $friend_auth ) ) {
return '';
}
return $friend_auth['me'] . '-' . $friend_auth['until'] . '-' . $friend_auth['auth'];
}
/**
* Determines whether the specified url is friend url.
*
* @param string $url The url.
*
* @return bool True if the specified url is friend url, False otherwise.
*/
function is_friend_url( $url ) {
if ( ! $this->user_url ) {
return false;
}
if ( false === strpos( $url, $this->user_url ) ) {
return false;
}
return true;
}
/**
* Get the REST URL for the friend
*
* @return string The REST URL.
*/
public function get_rest_url() {
$friends = Friends::get_instance();
$rest_url = $this->get_user_option( 'friends_rest_url' );
if ( ! $rest_url || false === strpos( $rest_url, REST::PREFIX ) ) {
$rest_url = $friends->rest->discover_rest_url( $this->user_url );
if ( $rest_url ) {
$this->update_user_option( 'friends_rest_url', $rest_url );
}
}
return $rest_url;
}
/**
* Wrap get_user_option
*
* @param string $option_name User option name.
* @return int|bool User meta ID if the option didn't exist, true on successful update,
* false on failure.
*/
function get_user_option( $option_name ) {
return get_user_option( $option_name, $this->ID );
}
/**
* Wrap update_user_option
*
* @param string $option_name User option name.
* @param mixed $newvalue User option value.
* @param bool $global Optional. Whether option name is global or blog specific.
* Default false (blog specific).
* @return int|bool User meta ID if the option didn't exist, true on successful update,
* false on failure.
*/
function update_user_option( $option_name, $newvalue, $global = false ) {
return update_user_option( $this->ID, $option_name, $newvalue, $global );
}
/**
* Wrap delete_user_option
*
* @param string $option_name User option name.
* @param bool $global Optional. Whether option name is global or blog specific.
* Default false (blog specific).
* @return bool True on success, false on failure.
*/
function delete_user_option( $option_name, $global = false ) {
return delete_user_option( $this->ID, $option_name, $global );
}
}