| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends Shortcodes |
| 4 |
* |
| 5 |
* This contains the functions for shortcodes. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Friends; |
| 11 |
|
| 12 |
/** |
| 13 |
* This is the class for the Friends Plugin shortcodes. |
| 14 |
* |
| 15 |
* @since 0.8 |
| 16 |
* |
| 17 |
* @package Friends |
| 18 |
* @author Alex Kirk |
| 19 |
*/ |
| 20 |
class Shortcodes { |
| 21 |
/** |
| 22 |
* Contains a reference to the Friends class. |
| 23 |
* |
| 24 |
* @var Friends |
| 25 |
*/ |
| 26 |
private $friends = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor |
| 30 |
* |
| 31 |
* @param Friends $friends A reference to the Friends object. |
| 32 |
*/ |
| 33 |
public function __construct( Friends $friends ) { |
| 34 |
$this->friends = $friends; |
| 35 |
$this->register_shortcodes(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register the WordPress shortcodes |
| 40 |
*/ |
| 41 |
private function register_shortcodes() { |
| 42 |
add_shortcode( 'only-friends', array( $this, 'only_friends_shortcode' ) ); |
| 43 |
add_shortcode( 'not-friends', array( $this, 'not_friends_shortcode' ) ); |
| 44 |
add_shortcode( 'friends-list', array( $this, 'friends_list_shortcode' ) ); |
| 45 |
add_shortcode( 'friends-count', array( $this, 'friends_count_shortcode' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Display the content of this shortcode just to friends. |
| 50 |
* |
| 51 |
* @param array $atts Attributes provided by the user. |
| 52 |
* @param string $content Enclosed content provided by the user. |
| 53 |
* @return string The content to be output. |
| 54 |
*/ |
| 55 |
public function only_friends_shortcode( $atts, $content = null ) { |
| 56 |
if ( current_user_can( 'friend' ) ) { |
| 57 |
return do_shortcode( $content ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( friends::has_required_privileges() ) { |
| 61 |
return '<div class="only-friends"><span class="watermark">' . __( 'Only friends', 'friends' ) . '</span>' . do_shortcode( $content ) . '</div>'; |
| 62 |
} |
| 63 |
|
| 64 |
return ''; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Display the content of this shortcode to everyone except friends. |
| 69 |
* |
| 70 |
* @param array $atts Attributes provided by the user. |
| 71 |
* @param string $content Enclosed content provided by the user. |
| 72 |
* @return string The content to be output. |
| 73 |
*/ |
| 74 |
public function not_friends_shortcode( $atts, $content = null ) { |
| 75 |
if ( current_user_can( 'friend' ) ) { |
| 76 |
return ''; |
| 77 |
} |
| 78 |
|
| 79 |
if ( friends::has_required_privileges() ) { |
| 80 |
return '<div class="not-friends"><span class="watermark">' . __( 'Not friends', 'friends' ) . '</span>' . do_shortcode( $content ) . '</div>'; |
| 81 |
} |
| 82 |
|
| 83 |
return do_shortcode( $content ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Display a list of your friends. |
| 88 |
* |
| 89 |
* @param array $atts Attributes provided by the user. |
| 90 |
* @return string The content to be output. |
| 91 |
*/ |
| 92 |
public function friends_list_shortcode( $atts ) { |
| 93 |
$a = shortcode_atts( |
| 94 |
array( |
| 95 |
'include-links' => false, |
| 96 |
), |
| 97 |
$atts |
| 98 |
); |
| 99 |
|
| 100 |
$friends = User_Query::all_friends(); |
| 101 |
$ret = '<ul class="friend-list">'; |
| 102 |
|
| 103 |
foreach ( $friends->get_results() as $friend_user ) { |
| 104 |
$ret .= '<li>'; |
| 105 |
|
| 106 |
if ( $a['include-links'] ) { |
| 107 |
$ret .= '<a href="' . esc_url( $friend_user->user_url ) . '">'; |
| 108 |
} |
| 109 |
|
| 110 |
$ret .= esc_html( $friend_user->display_name ); |
| 111 |
|
| 112 |
if ( $a['include-links'] ) { |
| 113 |
$ret .= '</a>'; |
| 114 |
} |
| 115 |
|
| 116 |
$ret .= '</li>'; |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
$ret .= '</ul>'; |
| 121 |
|
| 122 |
return $ret; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Display the number of your friends. |
| 127 |
* |
| 128 |
* @param array $atts Attributes provided by the user. |
| 129 |
* @return string The content to be output. |
| 130 |
*/ |
| 131 |
public function friends_count_shortcode( $atts ) { |
| 132 |
exit; |
| 133 |
$friends = User_Query::all_friends(); |
| 134 |
return $friends->get_total(); |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|