PluginProbe
Restrict User Access – Ultimate Membership & Content Protection / 2.7
Restrict User Access – Ultimate Membership & Content Protection v2.7
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 / admin / admin.php

admin.php in Restrict User Access – Ultimate Membership & Content Protection 2.7, at admin/admin.php

233 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Restrict User Access
4 * @author Joachim Jensen <joachim@dev.institute>
5 * @license GPLv3
6 * @copyright 2024 by Joachim Jensen
7 */
8
9 defined('ABSPATH') || exit;
10
11 abstract class RUA_Admin
12 {
13 /**
14 * Screen identifier
15 * @var string
16 */
17 protected $_screen;
18
19 protected $enable_navbar = true;
20
21 public function __construct()
22 {
23 if (is_admin()) {
24 $this->add_action('admin_menu', 'add_menu', 99);
25 $this->admin_hooks();
26 }
27 }
28
29 /**
30 * Set up screen and menu if necessary
31 *
32 * @since 0.15
33 */
34 public function add_menu()
35 {
36 $this->_screen = $this->get_screen();
37 $this->add_action('load-' . $this->_screen, 'load_screen');
38 }
39
40 /**
41 * Add filters and actions for admin dashboard
42 * e.g. AJAX calls
43 *
44 * @since 0.15
45 * @return void
46 */
47 abstract public function admin_hooks();
48
49 /**
50 * Get current screen
51 *
52 * @since 0.15
53 * @return string
54 */
55 abstract public function get_screen();
56
57 /**
58 * Prepare screen load
59 *
60 * @since 0.15
61 * @return void
62 */
63 abstract public function prepare_screen();
64
65 /**
66 * Authorize user for screen
67 *
68 * @since 0.15
69 * @return boolean
70 */
71 abstract public function authorize_user();
72
73 /**
74 * Register and enqueue scripts styles
75 * for screen
76 *
77 * @since 0.15
78 */
79 abstract public function add_scripts_styles();
80
81 /**
82 * Prepare plugin upgrade modal
83 *
84 * @since 0.15
85 * @return void
86 */
87 public function load_screen()
88 {
89 if (!$this->authorize_user()) {
90 wp_die(
91 '<p>' . __('You do not have access to this screen.', 'restrict-user-access') . '</p>',
92 403
93 );
94 }
95
96 $freemius = rua_fs();
97
98 if ($this->enable_navbar && !$freemius->is_activation_mode()) {
99 $path = plugin_dir_path(__FILE__) . '../view/';
100 $view = WPCAView::make($path . '/top_bar.php', [
101 'freemius' => $freemius,
102 'post_type' => $this->get_restrict_type()
103 ]);
104
105 add_action('in_admin_header', [$view, 'render']);
106 }
107
108 $this->prepare_screen();
109 $this->add_action('admin_enqueue_scripts', 'add_general_scripts_styles', 11);
110 }
111
112 /**
113 * Add general scripts to admin screens
114 *
115 * @since 1.2
116 */
117 public function add_general_scripts_styles()
118 {
119 $this->enqueue_style('rua/admin/style', 'style');
120 $this->add_scripts_styles();
121 }
122
123 /**
124 * @return WP_Post_Type
125 */
126 protected function get_restrict_type()
127 {
128 return get_post_type_object(RUA_App::TYPE_RESTRICT);
129 }
130
131 /**
132 * @since 1.2
133 * @param string $tag
134 * @param string $callback
135 * @param int $priority
136 * @param int $accepted_args
137 *
138 * @return void
139 */
140 protected function add_action($tag, $callback, $priority = 10, $accepted_args = 1)
141 {
142 if (is_string($callback)) {
143 $callback = [$this, $callback];
144 }
145 add_action($tag, $callback, $priority, $accepted_args);
146 }
147
148 /**
149 * @since 1.2
150 * @param string $tag
151 * @param string $callback
152 * @param int $priority
153 * @param int $accepted_args
154 *
155 * @return void
156 */
157 protected function add_filter($tag, $callback, $priority = 10, $accepted_args = 1)
158 {
159 if (is_string($callback)) {
160 $callback = [$this, $callback];
161 }
162 add_filter($tag, $callback, $priority, $accepted_args);
163 }
164
165 /**
166 * @since 1.2
167 * @param string $handle
168 * @param string $filename
169 * @param array $deps
170 * @param bool $in_footer
171 * @param string $ver
172 *
173 * @return void
174 */
175 protected function enqueue_script($handle, $filename, $deps = [], $ver = '', $in_footer = false)
176 {
177 $this->register_script($handle, $filename, $deps, $ver, $in_footer);
178 wp_enqueue_script($handle);
179 }
180
181 /**
182 * @since 1.2
183 * @param string $handle
184 * @param string $filename
185 * @param array $deps
186 * @param bool $in_footer
187 * @param string $ver
188 *
189 * @return void
190 */
191 protected function register_script($handle, $filename, $deps = [], $ver = '', $in_footer = false)
192 {
193 $suffix = '.min.js';
194 if ($ver === '') {
195 $ver = RUA_App::PLUGIN_VERSION;
196 }
197 wp_register_script($handle, plugins_url('assets/js/' . $filename . $suffix, dirname(__FILE__)), $deps, $ver, $in_footer);
198 }
199
200 /**
201 * @since 1.2
202 * @param string $handle
203 * @param string $filename
204 * @param array $deps
205 * @param string $ver
206 *
207 * @return void
208 */
209 protected function enqueue_style($handle, $filename, $deps = [], $ver = '')
210 {
211 $this->register_style($handle, $filename, $deps, $ver);
212 wp_enqueue_style($handle);
213 }
214
215 /**
216 * @since 1.2
217 * @param string $handle
218 * @param string $filename
219 * @param array $deps
220 * @param string $ver
221 *
222 * @return void
223 */
224 protected function register_style($handle, $filename, $deps = [], $ver = '')
225 {
226 $suffix = '.css';
227 if ($ver === '') {
228 $ver = RUA_App::PLUGIN_VERSION;
229 }
230 wp_enqueue_style($handle, plugins_url('assets/css/' . $filename . $suffix, dirname(__FILE__)), $deps, $ver);
231 }
232 }
233