PluginProbe
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content / 1.8
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content v1.8
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.8, at admin/admin.php

363 lines 12.9 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 register_setting( $this->options_group, 'password_protected_status', 'intval' );
125 register_setting( $this->options_group, 'password_protected_feeds', 'intval' );
126 register_setting( $this->options_group, 'password_protected_administrators', 'intval' );
127 register_setting( $this->options_group, 'password_protected_users', 'intval' );
128 register_setting( $this->options_group, 'password_protected_password', array( $this, 'sanitize_password_protected_password' ) );
129
130 }
131
132 /**
133 * Sanitize Password Field Input
134 *
135 * @param string $val Password.
136 * @return string Sanitized password.
137 */
138 function sanitize_password_protected_password( $val ) {
139
140 $old_val = get_option( 'password_protected_password' );
141
142 if ( is_array( $val ) ) {
143 if ( empty( $val['new'] ) ) {
144 return $old_val;
145 } elseif ( empty( $val['confirm'] ) ) {
146 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' ) );
147 return $old_val;
148 } elseif ( $val['new'] != $val['confirm'] ) {
149 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password not saved. Password fields did not match.', 'password-protected' ) );
150 return $old_val;
151 } elseif ( $val['new'] == $val['confirm'] ) {
152 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password saved.', 'password-protected' ), 'updated' );
153 return $val['new'];
154 }
155 return get_option( 'password_protected_password' );
156 }
157
158 return $val;
159
160 }
161
162 /**
163 * Password Protected Section
164 */
165 function password_protected_settings_section() {
166
167 echo '<p>' . __( 'Password protect your web site. Users will be asked to enter a password to view the site.', 'password-protected' ) . '<br />
168 ' . __( 'For more information about Password Protected settings, view the "Help" tab at the top of this page.', 'password-protected' ) . '</p>';
169
170 }
171
172 /**
173 * Password Protection Status Field
174 */
175 function password_protected_status_field() {
176
177 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>';
178
179 }
180
181 /**
182 * Password Protection Permissions Field
183 */
184 function password_protected_permissions_field() {
185
186 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>';
187 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>';
188 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>';
189
190 }
191
192 /**
193 * Password Field
194 */
195 function password_protected_password_field() {
196
197 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>
198 <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>';
199
200 }
201
202 /**
203 * Pre-update 'password_protected_password' Option
204 *
205 * Before the password is saved, MD5 it!
206 * Doing it in this way allows developers to intercept with an earlier filter if they
207 * need to do something with the plaintext password.
208 *
209 * @param string $newvalue New Value.
210 * @param string $oldvalue Old Value.
211 * @return string Filtered new value.
212 */
213 function pre_update_option_password_protected_password( $newvalue, $oldvalue ) {
214
215 global $Password_Protected;
216
217 if ( $newvalue != $oldvalue ) {
218 $newvalue = $Password_Protected->encrypt_password( $newvalue );
219 }
220
221 return $newvalue;
222
223 }
224
225 /**
226 * Plugin Row Meta
227 *
228 * Adds GitHub and translate links below the plugin description on the plugins page.
229 *
230 * @param array $plugin_meta Plugin meta display array.
231 * @param string $plugin_file Plugin reference.
232 * @param array $plugin_data Plugin data.
233 * @param string $status Plugin status.
234 * @return array Plugin meta array.
235 */
236 function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
237
238 if ( 'password-protected/password-protected.php' == $plugin_file ) {
239 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'http://github.com/benhuson/password-protected', 'password-protected' ), __( 'GitHub', 'password-protected' ) );
240 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'https://www.transifex.com/projects/p/password-protected/resource/password-protected/', 'password-protected' ), __( 'Translate', 'password-protected' ) );
241 }
242
243 return $plugin_meta;
244
245 }
246
247 /**
248 * Plugin Action Links
249 *
250 * Adds settings link on the plugins page.
251 *
252 * @param array $actions Plugin action links array.
253 * @return array Plugin action links array.
254 */
255 function plugin_action_links( $actions ) {
256
257 $actions[] = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=password-protected' ), __( 'Settings', 'password-protected' ) );
258 return $actions;
259
260 }
261
262 /**
263 * Password Admin Notice
264 * Warns the user if they have enabled password protection but not entered a password
265 */
266 function password_protected_admin_notices() {
267
268 global $Password_Protected;
269
270 // Check Support
271 $screens = $this->plugin_screen_ids( array( 'dashboard', 'plugins' ) );
272 if ( $this->is_current_screen( $screens ) ) {
273 $supported = $Password_Protected->is_plugin_supported();
274 if ( is_wp_error( $supported ) ) {
275 echo $this->admin_error_display( $supported->get_error_message( $supported->get_error_code() ) );
276 }
277 }
278
279 // Settings
280 if ( $this->is_current_screen( $this->plugin_screen_ids() ) ) {
281 $status = get_option( 'password_protected_status' );
282 $pwd = get_option( 'password_protected_password' );
283
284 if ( (bool) $status && empty( $pwd ) ) {
285 echo $this->admin_error_display( __( 'You have enabled password protection but not yet set a password. Please set one below.', 'password-protected' ) );
286 }
287
288 if ( current_user_can( 'manage_options' ) && ( (bool) get_option( 'password_protected_administrators' ) || (bool) get_option( 'password_protected_users' ) ) ) {
289 if ( (bool) get_option( 'password_protected_administrators' ) && (bool) get_option( 'password_protected_users' ) ) {
290 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' ) );
291 } elseif ( (bool) get_option( 'password_protected_administrators' ) ) {
292 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' ) );
293 } elseif ( (bool) get_option( 'password_protected_users' ) ) {
294 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' ) );
295 }
296 }
297
298 }
299
300 }
301
302 /**
303 * Admin Error Display
304 *
305 * Returns a string wrapped in HTML to display an admin error.
306 *
307 * @param string $string Error string.
308 * @return string HTML error.
309 */
310 function admin_error_display( $string ) {
311
312 return '<div class="error"><p>' . $string . '</p></div>';
313
314 }
315
316 /**
317 * Is Current Screen
318 *
319 * Checks wether the admin is displaying a specific screen.
320 *
321 * @param string|array $screen_id Admin screen ID(s).
322 * @return boolean
323 */
324 function is_current_screen( $screen_id ) {
325
326 if ( function_exists( 'get_current_screen' ) ) {
327 $current_screen = get_current_screen();
328 if ( ! is_array( $screen_id ) ) {
329 $screen_id = array( $screen_id );
330 }
331 if ( in_array( $current_screen->id, $screen_id ) ) {
332 return true;
333 }
334 }
335
336 return false;
337
338 }
339
340 /**
341 * Plugin Screen IDs
342 *
343 * @param string|array $screen_id Additional screen IDs to add to the returned array.
344 * @return array Screen IDs.
345 */
346 function plugin_screen_ids( $screen_id = '' ) {
347
348 $screen_ids = array( 'options-' . $this->options_group, 'settings_page_' . $this->options_group );
349
350 if ( ! empty( $screen_id ) ) {
351 if ( is_array( $screen_id ) ) {
352 $screen_ids = array_merge( $screen_ids, $screen_id );
353 } else {
354 $screen_ids[] = $screen_id;
355 }
356 }
357
358 return $screen_ids;
359
360 }
361
362 }
363