PluginProbe
Passster – Password Protect Pages and Content / 4.3.9
Passster – Password Protect Pages and Content v4.3.9
4.3.17 4.3.16 4.3.15 4.3.14 4.3.12 4.3.13 4.3.11 4.3.10 4.3.9 4.3.8 4.3.7 4.3.6 4.3.5 trunk 3.5.4 3.5.5.2 3.5.5.8 3.5.5.9 4.0 4.1.4 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 All 49 releases
content-protector / inc / admin / inc / class-ps-admin-settings.php

class-ps-admin-settings.php in Passster – Password Protect Pages and Content 4.3.9, at inc/admin/inc/class-ps-admin-settings.php

333 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace passster;
4
5 class PS_Admin_Settings {
6 /**
7 * Contains instance or null
8 *
9 * @var object|null
10 */
11 private static $instance = null;
12
13 /**
14 * Returns instance of PS_Admin_Settings.
15 *
16 * @return object
17 */
18 public static function get_instance() {
19 if ( null === self::$instance ) {
20 self::$instance = new self();
21 }
22 return self::$instance;
23 }
24
25 /**
26 * Setting up admin fields
27 *
28 * @return void
29 */
30 public function __construct() {
31 add_action( 'admin_menu', array($this, 'add_menu') );
32 add_action( 'admin_menu', array($this, 'add_settings_submenu'), 99 );
33 // Add Settings last
34 add_action( 'rest_api_init', array($this, 'rest_api_init') );
35 }
36
37 public function add_menu() {
38 add_menu_page(
39 __( 'Passster', 'content-protector' ),
40 'Passster',
41 apply_filters( 'passster_user_capability', 'manage_options' ),
42 'passster',
43 array($this, 'render_settings'),
44 'dashicons-lock',
45 85
46 );
47 }
48
49 /**
50 * Add Settings submenu at the end (priority 99)
51 */
52 public function add_settings_submenu() {
53 $settings_suffix = add_submenu_page(
54 'passster',
55 __( 'Options', 'content-protector' ),
56 __( 'Settings', 'content-protector' ),
57 apply_filters( 'passster_user_capability', 'manage_options' ),
58 'passster-settings',
59 array($this, 'render_settings')
60 );
61 add_action( "admin_print_scripts-{$settings_suffix}", array($this, 'add_settings_scripts') );
62 }
63
64 public function add_settings_scripts() {
65 $screen = get_current_screen();
66 wp_enqueue_script(
67 'passster-settings',
68 PASSSTER_URL . '/inc/admin/build/index.js',
69 array(
70 'wp-api',
71 'wp-components',
72 'wp-element',
73 'wp-api-fetch',
74 'wp-data',
75 'wp-i18n'
76 ),
77 PASSSTER_VERSION,
78 true
79 );
80 $options = get_option( 'passster' );
81 // Freemius
82 $fs_slug = 'content-protector';
83 $fs_id = '1938';
84 $fs_affix = \passster_fs()->get_unique_affix();
85 $args = array(
86 'screen' => 'passster-settings',
87 'version' => PASSSTER_VERSION,
88 'logo' => PASSSTER_URL . '/assets/admin/passster-logo.svg',
89 'is_pro' => \passster_fs()->is_plan_or_trial__premium_only( 'pro' ),
90 'is_premium_build' => \passster_fs()->is_premium(),
91 'account_url' => admin_url( 'admin.php?page=' . $fs_slug . '-account' ),
92 'activation_url' => '#',
93 'upgrade_url' => \passster_fs()->get_upgrade_url(),
94 'fs_affix' => $fs_affix,
95 );
96 // Add Freemius license activation dialog on settings page
97 add_action( 'admin_footer', array($this, 'add_license_activation_dialog') );
98 if ( isset( $options['global_protection_id'] ) ) {
99 $args['global_edit_url'] = admin_url( 'post.php?post=' . esc_html( $options['global_protection_id'] ) . '&action=edit', 'https' );
100 }
101 wp_localize_script( 'passster-settings', 'options', $args );
102 // Make the blocks translatable.
103 if ( function_exists( 'wp_set_script_translations' ) ) {
104 wp_set_script_translations( 'passster-settings', 'content-protector', PASSSTER_PATH . '/languages' );
105 }
106 wp_enqueue_style( 'passster-settings-style', PASSSTER_URL . '/inc/admin/build/index.css', array('wp-components') );
107 }
108
109 public function render_settings() {
110 ?>
111 <div id="passster-settings"></div>
112 <?php
113 }
114
115 /**
116 * Add Freemius license activation dialog to settings page.
117 */
118 public function add_license_activation_dialog() {
119 $fs = \passster_fs();
120 // On the free build the AJAX handler is not registered by Freemius, so the dialog is useless.
121 if ( !$fs->is_premium() ) {
122 return;
123 }
124 $vars = array(
125 'id' => $fs->get_id(),
126 );
127 fs_require_template( 'forms/license-activation.php', $vars );
128 fs_require_template( 'forms/resend-key.php', $vars );
129 }
130
131 public function rest_api_init() {
132 register_rest_route( 'passster/v1', '/settings', array(
133 'methods' => 'GET',
134 'callback' => [$this, 'get_settings'],
135 'permission_callback' => function () {
136 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
137 },
138 ) );
139 register_rest_route( 'passster/v1', '/system-status', array(
140 'methods' => 'GET',
141 'callback' => [$this, 'get_system_status'],
142 'permission_callback' => function () {
143 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
144 },
145 ) );
146 register_rest_route( 'passster/v1', '/settings', array(
147 'methods' => 'POST',
148 'callback' => [$this, 'save_settings'],
149 'permission_callback' => function () {
150 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
151 },
152 ) );
153 register_rest_route( 'passster/v1', '/migrate', array(
154 'methods' => 'POST',
155 'callback' => [$this, 'migrate_settings'],
156 'permission_callback' => function () {
157 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
158 },
159 ) );
160 register_rest_route( 'passster/v1', '/pages', array(
161 'methods' => 'GET',
162 'callback' => [$this, 'get_pages'],
163 'permission_callback' => function () {
164 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
165 },
166 ) );
167 register_rest_route( 'passster/v1', '/excludable-pages', array(
168 'methods' => 'GET',
169 'callback' => [$this, 'get_excludable_pages'],
170 'permission_callback' => function () {
171 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
172 },
173 ) );
174 register_rest_route( 'passster/v1', '/edit-url', array(
175 'methods' => 'GET',
176 'callback' => [$this, 'get_edit_url'],
177 'permission_callback' => function () {
178 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
179 },
180 ) );
181 register_rest_route( 'passster/v1', '/post-title', array(
182 'methods' => 'GET',
183 'callback' => [$this, 'get_post_title'],
184 'permission_callback' => function () {
185 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
186 },
187 ) );
188 register_rest_route( 'passster/v1', '/child-pages', array(
189 'methods' => 'GET',
190 'callback' => [$this, 'get_child_pages'],
191 'permission_callback' => function () {
192 return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
193 },
194 ) );
195 }
196
197 public function get_settings() {
198 return get_option( 'passster' );
199 }
200
201 public function get_system_status() {
202 return array(
203 'PHP' => array(
204 'Version' => phpversion(),
205 ),
206 'WordPress' => array(
207 'Permalinks' => strlen( get_option( 'permalink_structure' ) ) !== 0,
208 'SSL' => is_ssl(),
209 ),
210 );
211 }
212
213 public function save_settings( $request ) {
214 if ( $request->get_params() ) {
215 $options = $request->get_params();
216 foreach ( $options as $key => $value ) {
217 // Allow HTML in instruction field (links, basic formatting)
218 if ( 'instruction' === $key ) {
219 $options[$key] = wp_kses_post( $value );
220 continue;
221 }
222 // Allow HTML in error message field too
223 if ( 'error' === $key ) {
224 $options[$key] = wp_kses_post( $value );
225 continue;
226 }
227 // Arrays (like exclude_pages) - sanitize each item
228 if ( is_array( $value ) ) {
229 $options[$key] = array_map( 'sanitize_text_field', $value );
230 continue;
231 }
232 // Everything else - sanitize as text
233 $options[$key] = sanitize_text_field( $value );
234 }
235 update_option( 'passster', $options );
236 }
237 return wp_json_encode( [
238 "status" => 200,
239 "message" => "Ok",
240 ] );
241 }
242
243 public function migrate_settings() {
244 PS_Migrator::migrate();
245 return wp_json_encode( [
246 "status" => 200,
247 "message" => "Ok",
248 ] );
249 }
250
251 public function get_pages() {
252 $args = array(
253 'post_type' => 'page',
254 'post_status' => 'publish',
255 'numberposts' => -1,
256 );
257 $pages = get_posts( $args );
258 // Build selectable pages array.
259 $selectable_pages = array();
260 foreach ( $pages as $page ) {
261 $selectable_pages[] = array(
262 'label' => $page->post_title,
263 'value' => $page->ID,
264 );
265 }
266 return $selectable_pages;
267 }
268
269 public function get_excludable_pages() {
270 $args = array(
271 'post_type' => 'page',
272 'post_status' => 'publish',
273 'numberposts' => -1,
274 );
275 $pages = get_posts( $args );
276 // Build selectable pages array.
277 $selectable_pages = array();
278 foreach ( $pages as $page ) {
279 $object = new \StdClass();
280 $object->value = $page->ID;
281 $object->label = $page->post_title;
282 $selectable_pages[] = $object;
283 }
284 return $selectable_pages;
285 }
286
287 public function get_edit_url( $request ) {
288 if ( $request->get_params() ) {
289 $options = $request->get_params();
290 return admin_url( 'post.php?post=' . esc_html( $options['post_id'] ) . '&action=edit', 'https' );
291 }
292 return wp_json_encode( [
293 "status" => 400,
294 "message" => "Not found",
295 ] );
296 }
297
298 public function get_post_title( $request ) {
299 if ( $request->get_params() ) {
300 $options = $request->get_params();
301 $post_id = esc_html( $options['post_id'] );
302 if ( get_post_status( $post_id ) && 'publish' === get_post_status( $post_id ) ) {
303 return get_the_title( $post_id );
304 }
305 }
306 return false;
307 }
308
309 public function get_child_pages( $request ) {
310 if ( $request->get_params() ) {
311 $options = $request->get_params();
312 $parent_id = esc_html( $options['post_id'] );
313 $args = array(
314 'post_type' => 'page',
315 'post_status' => 'publish',
316 'numberposts' => -1,
317 );
318 $pages = get_posts( $args );
319 $childs = get_page_children( $parent_id, $pages );
320 $child_ids = array();
321 foreach ( $childs as $child ) {
322 $child_ids[] = $child->ID;
323 }
324 return $child_ids;
325 }
326 return wp_json_encode( [
327 "status" => 400,
328 "message" => "Not found",
329 ] );
330 }
331
332 }
333