PluginProbe
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content / 1.9
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content v1.9
2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.5 1.6 1.6.1 1.6.2 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.0.1 2.0.2 2.0.3 All 63 releases
password-protected / admin / admin.php

admin.php in Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content 1.9, at admin/admin.php

401 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Password_Protected_Admin {
4
5 var $settings_page_id;
6 var $options_group = 'password-protected';
7
8 /**
9 * Constructor
10 */
11 function Password_Protected_Admin() {
12
13 global $wp_version;
14
15 add_action( 'admin_init', array( $this, 'password_protected_settings' ), 5 );
16 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
17 add_action( 'password_protected_help_tabs', array( $this, 'help_tabs' ), 5 );
18 add_action( 'admin_notices', array( $this, 'password_protected_admin_notices' ) );
19 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 4 );
20 add_filter( 'plugin_action_links_password-protected/password-protected.php', array( $this, 'plugin_action_links' ) );
21 add_filter( 'pre_update_option_password_protected_password', array( $this, 'pre_update_option_password_protected_password' ), 10, 2 );
22
23 }
24
25 /**
26 * Admin Menu
27 */
28 function admin_menu() {
29
30 $this->settings_page_id = add_options_page( __( 'Password Protected', 'password-protected' ), __( 'Password Protected', 'password-protected' ), 'manage_options', 'password-protected', array( $this, 'settings_page' ) );
31 add_action( 'load-' . $this->settings_page_id, array( $this, 'add_help_tabs' ), 20 );
32
33 }
34
35 /**
36 * Settings Page
37 */
38 function settings_page() {
39 ?>
40
41 <div class="wrap">
42 <div id="icon-options-general" class="icon32"><br /></div>
43 <h2><?php _e( 'Password Protected Settings', 'password-protected' ) ?></h2>
44 <form method="post" action="options.php">
45 <?php
46 settings_fields( 'password-protected' );
47 do_settings_sections( 'password-protected' );
48 ?>
49 <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes' ) ?>"></p>
50 </form>
51 </div>
52
53 <?php
54 }
55
56 /**
57 * Add Help Tabs
58 */
59 function add_help_tabs() {
60
61 global $wp_version;
62
63 if ( version_compare( $wp_version, '3.3', '<' ) ) {
64 return;
65 }
66
67 do_action( 'password_protected_help_tabs', get_current_screen() );
68
69 }
70
71 /**
72 * Help Tabs
73 *
74 * @param object $current_screen Screen object.
75 */
76 function help_tabs( $current_screen ) {
77
78 $current_screen->add_help_tab( array(
79 'id' => 'PASSWORD_PROTECTED_SETTINGS',
80 'title' => __( 'Password Protected', 'password-protected' ),
81 'content' => __( '<p><strong>Password Protected Status</strong><br />Turn on/off password protection.</p>', 'password-protected' )
82 . __( '<p><strong>Protected Permissions</strong><br />Allow access for logged in users and administrators without needing to enter a password. You will need to enable this option if you want administrators to be able to preview the site in the Theme Customizer. Also allow RSS Feeds to be accessed when the site is password protected.</p>', 'password-protected' )
83 . __( '<p><strong>Password Fields</strong><br />To set a new password, enter it into both fields. You cannot set an `empty` password. To disable password protection uncheck the Enabled checkbox.</p>', 'password-protected' )
84 ) );
85
86 }
87
88 /**
89 * Settings API
90 */
91 function password_protected_settings() {
92
93 add_settings_section(
94 'password_protected',
95 '',
96 array( $this, 'password_protected_settings_section' ),
97 $this->options_group
98 );
99
100 add_settings_field(
101 'password_protected_status',
102 __( 'Password Protected Status', 'password-protected' ),
103 array( $this, 'password_protected_status_field' ),
104 $this->options_group,
105 'password_protected'
106 );
107
108 add_settings_field(
109 'password_protected_permissions',
110 __( 'Protected Permissions', 'password-protected' ),
111 array( $this, 'password_protected_permissions_field' ),
112 $this->options_group,
113 'password_protected'
114 );
115
116 add_settings_field(
117 'password_protected_password',
118 __( 'New Password', 'password-protected' ),
119 array( $this, 'password_protected_password_field' ),
120 $this->options_group,
121 'password_protected'
122 );
123
124 add_settings_field(
125 'password_protected_allowed_ip_addresses',
126 __( 'Allow IP Addresses', 'password-protected' ),
127 array( $this, 'password_protected_allowed_ip_addresses_field' ),
128 $this->options_group,
129 'password_protected'
130 );
131
132 register_setting( $this->options_group, 'password_protected_status', 'intval' );
133 register_setting( $this->options_group, 'password_protected_feeds', 'intval' );
134 register_setting( $this->options_group, 'password_protected_administrators', 'intval' );
135 register_setting( $this->options_group, 'password_protected_users', 'intval' );
136 register_setting( $this->options_group, 'password_protected_password', array( $this, 'sanitize_password_protected_password' ) );
137 register_setting( $this->options_group, 'password_protected_allowed_ip_addresses', array( $this, 'sanitize_ip_addresses' ) );
138
139 }
140
141 /**
142 * Sanitize Password Field Input
143 *
144 * @param string $val Password.
145 * @return string Sanitized password.
146 */
147 function sanitize_password_protected_password( $val ) {
148
149 $old_val = get_option( 'password_protected_password' );
150
151 if ( is_array( $val ) ) {
152 if ( empty( $val['new'] ) ) {
153 return $old_val;
154 } elseif ( empty( $val['confirm'] ) ) {
155 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password not saved. When setting a new password please enter it in both fields.', 'password-protected' ) );
156 return $old_val;
157 } elseif ( $val['new'] != $val['confirm'] ) {
158 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password not saved. Password fields did not match.', 'password-protected' ) );
159 return $old_val;
160 } elseif ( $val['new'] == $val['confirm'] ) {
161 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password saved.', 'password-protected' ), 'updated' );
162 return $val['new'];
163 }
164 return get_option( 'password_protected_password' );
165 }
166
167 return $val;
168
169 }
170
171 /**
172 * Sanitize IP Addresses
173 *
174 * @param string $val IP addresses.
175 * @return string Sanitized IP addresses.
176 */
177 function sanitize_ip_addresses( $val ) {
178
179 $ip_addresses = explode( "\n", $val );
180 $ip_addresses = array_map( 'sanitize_text_field', $ip_addresses );
181 $ip_addresses = array_map( 'trim', $ip_addresses );
182 $ip_addresses = array_filter( $ip_addresses );
183
184 $val = implode( "\n", $ip_addresses );
185
186 return $val;
187
188 }
189
190 /**
191 * Password Protected Section
192 */
193 function password_protected_settings_section() {
194
195 echo '<p>' . __( 'Password protect your web site. Users will be asked to enter a password to view the site.', 'password-protected' ) . '<br />
196 ' . __( 'For more information about Password Protected settings, view the "Help" tab at the top of this page.', 'password-protected' ) . '</p>';
197
198 }
199
200 /**
201 * Password Protection Status Field
202 */
203 function password_protected_status_field() {
204
205 echo '<label><input name="password_protected_status" id="password_protected_status" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_status' ), false ) . ' /> ' . __( 'Enabled', 'password-protected' ) . '</label>';
206
207 }
208
209 /**
210 * Password Protection Permissions Field
211 */
212 function password_protected_permissions_field() {
213
214 echo '<label><input name="password_protected_administrators" id="password_protected_administrators" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_administrators' ), false ) . ' /> ' . __( 'Allow Administrators', 'password-protected' ) . '</label>';
215 echo '<label><input name="password_protected_users" id="password_protected_users" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_users' ), false ) . ' style="margin-left: 20px;" /> ' . __( 'Allow Logged In Users', 'password-protected' ) . '</label>';
216 echo '<label><input name="password_protected_feeds" id="password_protected_feeds" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_feeds' ), false ) . ' style="margin-left: 20px;" /> ' . __( 'Allow RSS Feeds', 'password-protected' ) . '</label>';
217
218 }
219
220 /**
221 * Password Field
222 */
223 function password_protected_password_field() {
224
225 echo '<input type="password" name="password_protected_password[new]" id="password_protected_password_new" size="16" value="" autocomplete="off"> <span class="description">' . __( 'If you would like to change the password type a new one. Otherwise leave this blank.', 'password-protected' ) . '</span><br>
226 <input type="password" name="password_protected_password[confirm]" id="password_protected_password_confirm" size="16" value="" autocomplete="off"> <span class="description">' . __( 'Type your new password again.', 'password-protected' ) . '</span>';
227
228 }
229
230 /**
231 * Allowed IP Addresses Field
232 */
233 function password_protected_allowed_ip_addresses_field() {
234
235 echo '<textarea name="password_protected_allowed_ip_addresses" id="password_protected_allowed_ip_addresses" rows="3" class="regular-text" />' . get_option( 'password_protected_allowed_ip_addresses' ) . '</textarea>';
236 echo '<p class="description">' . __( 'Enter one IP address per line', 'password-protected' ) .'</p>';
237
238 }
239
240 /**
241 * Pre-update 'password_protected_password' Option
242 *
243 * Before the password is saved, MD5 it!
244 * Doing it in this way allows developers to intercept with an earlier filter if they
245 * need to do something with the plaintext password.
246 *
247 * @param string $newvalue New Value.
248 * @param string $oldvalue Old Value.
249 * @return string Filtered new value.
250 */
251 function pre_update_option_password_protected_password( $newvalue, $oldvalue ) {
252
253 global $Password_Protected;
254
255 if ( $newvalue != $oldvalue ) {
256 $newvalue = $Password_Protected->encrypt_password( $newvalue );
257 }
258
259 return $newvalue;
260
261 }
262
263 /**
264 * Plugin Row Meta
265 *
266 * Adds GitHub and translate links below the plugin description on the plugins page.
267 *
268 * @param array $plugin_meta Plugin meta display array.
269 * @param string $plugin_file Plugin reference.
270 * @param array $plugin_data Plugin data.
271 * @param string $status Plugin status.
272 * @return array Plugin meta array.
273 */
274 function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
275
276 if ( 'password-protected/password-protected.php' == $plugin_file ) {
277 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'http://github.com/benhuson/password-protected', 'password-protected' ), __( 'GitHub', 'password-protected' ) );
278 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'https://www.transifex.com/projects/p/password-protected/resource/password-protected/', 'password-protected' ), __( 'Translate', 'password-protected' ) );
279 }
280
281 return $plugin_meta;
282
283 }
284
285 /**
286 * Plugin Action Links
287 *
288 * Adds settings link on the plugins page.
289 *
290 * @param array $actions Plugin action links array.
291 * @return array Plugin action links array.
292 */
293 function plugin_action_links( $actions ) {
294
295 $actions[] = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=password-protected' ), __( 'Settings', 'password-protected' ) );
296 return $actions;
297
298 }
299
300 /**
301 * Password Admin Notice
302 * Warns the user if they have enabled password protection but not entered a password
303 */
304 function password_protected_admin_notices() {
305
306 global $Password_Protected;
307
308 // Check Support
309 $screens = $this->plugin_screen_ids( array( 'dashboard', 'plugins' ) );
310 if ( $this->is_current_screen( $screens ) ) {
311 $supported = $Password_Protected->is_plugin_supported();
312 if ( is_wp_error( $supported ) ) {
313 echo $this->admin_error_display( $supported->get_error_message( $supported->get_error_code() ) );
314 }
315 }
316
317 // Settings
318 if ( $this->is_current_screen( $this->plugin_screen_ids() ) ) {
319 $status = get_option( 'password_protected_status' );
320 $pwd = get_option( 'password_protected_password' );
321
322 if ( (bool) $status && empty( $pwd ) ) {
323 echo $this->admin_error_display( __( 'You have enabled password protection but not yet set a password. Please set one below.', 'password-protected' ) );
324 }
325
326 if ( current_user_can( 'manage_options' ) && ( (bool) get_option( 'password_protected_administrators' ) || (bool) get_option( 'password_protected_users' ) ) ) {
327 if ( (bool) get_option( 'password_protected_administrators' ) && (bool) get_option( 'password_protected_users' ) ) {
328 echo $this->admin_error_display( __( 'You have enabled password protection and allowed administrators and logged in users - other users will still need to enter a password to view the site.', 'password-protected' ) );
329 } elseif ( (bool) get_option( 'password_protected_administrators' ) ) {
330 echo $this->admin_error_display( __( 'You have enabled password protection and allowed administrators - other users will still need to enter a password to view the site.', 'password-protected' ) );
331 } elseif ( (bool) get_option( 'password_protected_users' ) ) {
332 echo $this->admin_error_display( __( 'You have enabled password protection and allowed logged in users - other users will still need to enter a password to view the site.', 'password-protected' ) );
333 }
334 }
335
336 }
337
338 }
339
340 /**
341 * Admin Error Display
342 *
343 * Returns a string wrapped in HTML to display an admin error.
344 *
345 * @param string $string Error string.
346 * @return string HTML error.
347 */
348 function admin_error_display( $string ) {
349
350 return '<div class="error"><p>' . $string . '</p></div>';
351
352 }
353
354 /**
355 * Is Current Screen
356 *
357 * Checks wether the admin is displaying a specific screen.
358 *
359 * @param string|array $screen_id Admin screen ID(s).
360 * @return boolean
361 */
362 function is_current_screen( $screen_id ) {
363
364 if ( function_exists( 'get_current_screen' ) ) {
365 $current_screen = get_current_screen();
366 if ( ! is_array( $screen_id ) ) {
367 $screen_id = array( $screen_id );
368 }
369 if ( in_array( $current_screen->id, $screen_id ) ) {
370 return true;
371 }
372 }
373
374 return false;
375
376 }
377
378 /**
379 * Plugin Screen IDs
380 *
381 * @param string|array $screen_id Additional screen IDs to add to the returned array.
382 * @return array Screen IDs.
383 */
384 function plugin_screen_ids( $screen_id = '' ) {
385
386 $screen_ids = array( 'options-' . $this->options_group, 'settings_page_' . $this->options_group );
387
388 if ( ! empty( $screen_id ) ) {
389 if ( is_array( $screen_id ) ) {
390 $screen_ids = array_merge( $screen_ids, $screen_id );
391 } else {
392 $screen_ids[] = $screen_id;
393 }
394 }
395
396 return $screen_ids;
397
398 }
399
400 }
401