Checker.php
4 years ago
PostContent.php
4 years ago
Redirect.php
4 years ago
RestrictionShortcode.php
4 years ago
index.php
5 years ago
RestrictionShortcode.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ContentProtection\Frontend; |
| 4 | |
| 5 | class RestrictionShortcode |
| 6 | { |
| 7 | public function __construct() |
| 8 | { |
| 9 | add_shortcode('pp-restrict-content', [$this, 'shortcode_handler']); |
| 10 | } |
| 11 | |
| 12 | public function shortcode_handler($atts, $content = null) |
| 13 | { |
| 14 | $atts = shortcode_atts(array( |
| 15 | 'roles' => '', |
| 16 | 'users' => '', |
| 17 | 'action' => 'show' // value can be "show" or "hide" |
| 18 | ), $atts); |
| 19 | |
| 20 | if ($this->rule_matches($atts['roles'], $atts['users'])) { |
| 21 | return ($atts['action'] == 'hide') ? '' : \do_shortcode($content); |
| 22 | } |
| 23 | |
| 24 | return ''; |
| 25 | } |
| 26 | |
| 27 | public function rule_matches($roles = [], $user_ids = []) |
| 28 | { |
| 29 | if (is_user_logged_in()) { |
| 30 | |
| 31 | if ( ! empty($roles)) { |
| 32 | |
| 33 | $roles = array_map('sanitize_text_field', explode(',', $roles)); |
| 34 | |
| 35 | $user_roles = wp_get_current_user()->roles; |
| 36 | |
| 37 | if ( ! empty(array_intersect($roles, $user_roles))) return true; |
| 38 | } |
| 39 | |
| 40 | if ( ! empty($user_ids)) { |
| 41 | |
| 42 | $user_ids = array_map('sanitize_text_field', explode(',', $user_ids)); |
| 43 | |
| 44 | foreach ($user_ids as $user_id) { |
| 45 | |
| 46 | if (is_numeric($user_id) && get_current_user_id() == absint($user_id)) { |
| 47 | return true; |
| 48 | } else { |
| 49 | $user = get_user_by('login', $user_id); |
| 50 | if ( ! $user) { |
| 51 | $user = get_user_by('email', $user_id); |
| 52 | } |
| 53 | |
| 54 | if ($user instanceof \WP_User && get_current_user_id() == absint($user->ID)) { |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | public static function get_instance() |
| 66 | { |
| 67 | static $instance = null; |
| 68 | |
| 69 | if (is_null($instance)) { |
| 70 | $instance = new self(); |
| 71 | } |
| 72 | |
| 73 | return $instance; |
| 74 | } |
| 75 | } |