PluginProbe
Restrict User Access – Ultimate Membership & Content Protection / 2.8
Restrict User Access – Ultimate Membership & Content Protection v2.8
trunk 1.3 2.0 2.1.3 2.2.3 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.7.1 2.8 2.8.1
restrict-user-access / src / Shortcode / Restrict.php

Restrict.php in Restrict User Access – Ultimate Membership & Content Protection 2.8, at src/Shortcode/Restrict.php

122 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RestrictUserAccess\Shortcode;
4
5 /**
6 * Class Restrict
7 *
8 * @author Joachim Jensen <joachim@dev.institute>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html
10 */
11 class Restrict implements ShortcodeInterface
12 {
13 public function get_names()
14 {
15 return [
16 'restrict',
17 'restrict-inner'
18 ];
19 }
20
21 public function get_callback($atts, $content = null)
22 {
23 $user = rua_get_user();
24 if ($user->has_global_access()) {
25 return do_shortcode($content);
26 }
27
28 $a = shortcode_atts([
29 'role' => '',
30 'level' => '',
31 'page' => 0,
32 'drip_days' => 0
33 ], $atts, 'restrict');
34
35 $has_access = false;
36 $legacy_app = \RUA_App::instance();
37
38 if ($a['level'] !== '') {
39 $has_negation = strpos($a['level'], '!') !== false;
40 $user_levels = array_flip($user->get_level_ids());
41 if (!empty($user_levels) || $has_negation) {
42 $level_names = explode(',', str_replace(' ', '', $a['level']));
43 $not_found = 0;
44 foreach ($level_names as $level_name) {
45 $level = $legacy_app->level_manager->get_level_by_name(ltrim($level_name, '!'));
46 if (!$level) {
47 $not_found++;
48 continue;
49 }
50 //if level param is negated, give access only if user does not have it
51 if ($level->post_name != $level_name) {
52 $has_access = !isset($user_levels[$level->ID]);
53 } elseif (isset($user_levels[$level->ID])) {
54 $drip = (int)$a['drip_days'];
55 if ($drip > 0 && $user->has_level($level->ID)) {
56 //@todo if extended level drips content, use start date
57 //of level user is member of
58 $start = $user->level_memberships()->get($level->ID)->get_start();
59 if ($start > 0) {
60 $drip_time = strtotime('+'.$drip.' days 00:00', $start);
61 $should_drip = apply_filters(
62 'rua/auth/content-drip',
63 time() <= $drip_time,
64 $user,
65 $level->ID
66 );
67 if ($should_drip) {
68 continue;
69 }
70 }
71 }
72 $has_access = true;
73 }
74 if ($has_access) {
75 break;
76 }
77 }
78 //if levels do not exist, make content visible
79 if (!$has_access && $not_found && $not_found === count($level_names)) {
80 $has_access = true;
81 }
82 }
83 } elseif ($a['role'] !== '') {
84 $user_roles = array_flip(wp_get_current_user()->roles);
85 if (!empty($user_roles)) {
86 $roles = explode(',', str_replace(' ', '', $a['role']));
87 foreach ($roles as $role_name) {
88 $role = ltrim($role_name, '!');
89 $not = $role != $role_name;
90 //when role is negated, give access if user does not have it
91 //otherwise give access only if user has it
92 if ($not xor isset($user_roles[$role])) {
93 $has_access = true;
94 break;
95 }
96 }
97 }
98 }
99
100 /**
101 * @var bool $has_access
102 * @var \RUA_User_Interface $user
103 * @var array $a
104 */
105 $has_access = apply_filters('rua/shortcode/restrict', $has_access, $user, $a);
106
107 if (!$has_access) {
108 $content = '';
109
110 // Only apply the page content if it exists
111 $page = $a['page'] ? get_post($a['page']) : null;
112 if ($page) {
113 setup_postdata($page);
114 $content = get_the_content();
115 wp_reset_postdata();
116 }
117 }
118
119 return do_shortcode($content);
120 }
121 }
122