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

305 lines 12.8 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 global $wp_version;
13 add_action( 'admin_init', array( $this, 'password_protected_settings' ), 5 );
14 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
15 add_action( 'password_protected_help_tabs', array( $this, 'help_tabs' ), 5 );
16 add_action( 'admin_notices', array( $this, 'password_protected_admin_notices' ) );
17 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 4 );
18 add_filter( 'plugin_action_links_password-protected/password-protected.php', array( $this, 'plugin_action_links' ) );
19 add_filter( 'pre_update_option_password_protected_password', array( $this, 'pre_update_option_password_protected_password' ), 10, 2 );
20 }
21
22 /**
23 * Admin Menu
24 */
25 function admin_menu() {
26 $this->settings_page_id = add_options_page( __( 'Password Protected', 'password-protected' ), __( 'Password Protected', 'password-protected' ), 'manage_options', 'password-protected', array( $this, 'settings_page' ) );
27 add_action( 'load-' . $this->settings_page_id, array( $this, 'add_help_tabs' ), 20 );
28 }
29
30 /**
31 * Settings Page
32 */
33 function settings_page() {
34 echo '<div class="wrap">
35 <div id="icon-options-general" class="icon32"><br /></div>
36 <h2>' . __( 'Password Protected Settings', 'password-protected' ) . '</h2>
37 <form method="post" action="options.php">';
38 settings_fields( 'password-protected' );
39 do_settings_sections( 'password-protected' );
40 echo '<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="' . __( 'Save Changes' ) . '"></p>
41 </form>
42 </div>';
43 }
44
45 /**
46 * Add Help Tabs
47 */
48 function add_help_tabs() {
49 global $wp_version;
50 if ( version_compare( $wp_version, '3.3', '<' ) ) {
51 return;
52 }
53 do_action( 'password_protected_help_tabs', get_current_screen() );
54 }
55
56 /**
57 * Help Tabs
58 *
59 * @param object $current_screen Screen object.
60 */
61 function help_tabs( $current_screen ) {
62 $current_screen->add_help_tab( array(
63 'id' => 'PASSWORD_PROTECTED_SETTINGS',
64 'title' => __( 'Password Protected', 'password-protected' ),
65 'content' => __( '<p><strong>Password Protected Status</strong><br />Turn on/off password protection.</p>', 'password-protected' )
66 . __( '<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' )
67 . __( '<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' )
68 ) );
69 }
70
71 /**
72 * Settings API
73 */
74 function password_protected_settings() {
75 add_settings_section(
76 'password_protected',
77 '',
78 array( $this, 'password_protected_settings_section' ),
79 $this->options_group
80 );
81 add_settings_field(
82 'password_protected_status',
83 __( 'Password Protected Status', 'password-protected' ),
84 array( $this, 'password_protected_status_field' ),
85 $this->options_group,
86 'password_protected'
87 );
88 add_settings_field(
89 'password_protected_permissions',
90 __( 'Protected Permissions', 'password-protected' ),
91 array( $this, 'password_protected_permissions_field' ),
92 $this->options_group,
93 'password_protected'
94 );
95 add_settings_field(
96 'password_protected_password',
97 __( 'New Password', 'password-protected' ),
98 array( $this, 'password_protected_password_field' ),
99 $this->options_group,
100 'password_protected'
101 );
102 register_setting( $this->options_group, 'password_protected_status', 'intval' );
103 register_setting( $this->options_group, 'password_protected_feeds', 'intval' );
104 register_setting( $this->options_group, 'password_protected_administrators', 'intval' );
105 register_setting( $this->options_group, 'password_protected_users', 'intval' );
106 register_setting( $this->options_group, 'password_protected_password', array( $this, 'sanitize_password_protected_password' ) );
107 }
108
109 /**
110 * Sanitize Password Field Input
111 *
112 * @param string $val Password.
113 * @return string Sanitized password.
114 */
115 function sanitize_password_protected_password( $val ) {
116 $old_val = get_option( 'password_protected_password' );
117 if ( is_array( $val ) ) {
118 if ( empty( $val['new'] ) ) {
119 return $old_val;
120 } elseif ( empty( $val['confirm'] ) ) {
121 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' ) );
122 return $old_val;
123 } elseif ( $val['new'] != $val['confirm'] ) {
124 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password not saved. Password fields did not match.', 'password-protected' ) );
125 return $old_val;
126 } elseif ( $val['new'] == $val['confirm'] ) {
127 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password saved.', 'password-protected' ), 'updated' );
128 return $val['new'];
129 }
130 return get_option( 'password_protected_password' );
131 }
132 return $val;
133 }
134
135 /**
136 * Password Protected Section
137 */
138 function password_protected_settings_section() {
139 echo '<p>' . __( 'Password protect your web site. Users will be asked to enter a password to view the site.', 'password-protected' ) . '<br />
140 ' . __( 'For more information about Password Protected settings, view the "Help" tab at the top of this page.', 'password-protected' ) . '</p>';
141 }
142
143 /**
144 * Password Protection Status Field
145 */
146 function password_protected_status_field() {
147 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>';
148 }
149
150 /**
151 * Password Protection Permissions Field
152 */
153 function password_protected_permissions_field() {
154 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>';
155 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>';
156 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>';
157 }
158
159 /**
160 * Password Field
161 */
162 function password_protected_password_field() {
163 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>
164 <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>';
165 }
166
167 /**
168 * Pre-update 'password_protected_password' Option
169 *
170 * Before the password is saved, MD5 it!
171 * Doing it in this way allows developers to intercept with an earlier filter if they
172 * need to do something with the plaintext password.
173 *
174 * @param string $newvalue New Value.
175 * @param string $oldvalue Old Value.
176 * @return string Filtered new value.
177 */
178 function pre_update_option_password_protected_password( $newvalue, $oldvalue ) {
179 global $Password_Protected;
180 if ( $newvalue != $oldvalue ) {
181 $newvalue = $Password_Protected->encrypt_password( $newvalue );
182 }
183 return $newvalue;
184 }
185
186 /**
187 * Plugin Row Meta
188 *
189 * Adds GitHub and translate links below the plugin description on the plugins page.
190 *
191 * @param array $plugin_meta Plugin meta display array.
192 * @param string $plugin_file Plugin reference.
193 * @param array $plugin_data Plugin data.
194 * @param string $status Plugin status.
195 * @return array Plugin meta array.
196 */
197 function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
198 if ( 'password-protected/password-protected.php' == $plugin_file ) {
199 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'http://github.com/benhuson/password-protected', 'password-protected' ), __( 'GitHub', 'password-protected' ) );
200 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'https://www.transifex.com/projects/p/password-protected/resource/password-protected/', 'password-protected' ), __( 'Translate', 'password-protected' ) );
201 }
202 return $plugin_meta;
203 }
204
205 /**
206 * Plugin Action Links
207 *
208 * Adds settings link on the plugins page.
209 *
210 * @param array $actions Plugin action links array.
211 * @return array Plugin action links array.
212 */
213 function plugin_action_links( $actions ) {
214 $actions[] = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=password-protected' ), __( 'Settings', 'password-protected' ) );
215 return $actions;
216 }
217
218 /**
219 * Password Admin Notice
220 * Warns the user if they have enabled password protection but not entered a password
221 */
222 function password_protected_admin_notices(){
223 global $Password_Protected;
224
225 // Check Support
226 $screens = $this->plugin_screen_ids( array( 'dashboard', 'plugins' ) );
227 if ( $this->is_current_screen( $screens ) ) {
228 $supported = $Password_Protected->is_plugin_supported();
229 if ( is_wp_error( $supported ) ) {
230 echo $this->admin_error_display( $supported->get_error_message( $supported->get_error_code() ) );
231 }
232 }
233
234 // Settings
235 if ( $this->is_current_screen( $this->plugin_screen_ids() ) ) {
236 $status = get_option( 'password_protected_status' );
237 $pwd = get_option( 'password_protected_password' );
238 if ( (bool) $status && empty( $pwd ) ) {
239 echo $this->admin_error_display( __( 'You have enabled password protection but not yet set a password. Please set one below.', 'password-protected' ) );
240 }
241 if ( current_user_can( 'manage_options' ) && ( (bool) get_option( 'password_protected_administrators' ) || (bool) get_option( 'password_protected_users' ) ) ) {
242 if ( (bool) get_option( 'password_protected_administrators' ) && (bool) get_option( 'password_protected_users' ) ) {
243 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' ) );
244 } elseif ( (bool) get_option( 'password_protected_administrators' ) ) {
245 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' ) );
246 } elseif ( (bool) get_option( 'password_protected_users' ) ) {
247 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' ) );
248 }
249 }
250 }
251 }
252
253 /**
254 * Admin Error Display
255 *
256 * Returns a string wrapped in HTML to display an admin error.
257 *
258 * @param string $string Error string.
259 * @return string HTML error.
260 */
261 function admin_error_display( $string ) {
262 return '<div class="error"><p>' . $string . '</p></div>';
263 }
264
265 /**
266 * Is Current Screen
267 *
268 * Checks wether the admin is displaying a specific screen.
269 *
270 * @param string|array $screen_id Admin screen ID(s).
271 * @return boolean
272 */
273 function is_current_screen( $screen_id ) {
274 if ( function_exists( 'get_current_screen' ) ) {
275 $current_screen = get_current_screen();
276 if ( ! is_array( $screen_id ) ) {
277 $screen_id = array( $screen_id );
278 }
279 if ( in_array( $current_screen->id, $screen_id ) ) {
280 return true;
281 }
282 }
283 return false;
284 }
285
286 /**
287 * Plugin Screen IDs
288 *
289 * @param string|array $screen_id Additional screen IDs to add to the returned array.
290 * @return array Screen IDs.
291 */
292 function plugin_screen_ids( $screen_id = '' ) {
293 $screen_ids = array( 'options-' . $this->options_group, 'settings_page_' . $this->options_group );
294 if ( ! empty( $screen_id ) ) {
295 if ( is_array( $screen_id ) ) {
296 $screen_ids = array_merge( $screen_ids, $screen_id );
297 } else {
298 $screen_ids[] = $screen_id;
299 }
300 }
301 return $screen_ids;
302 }
303
304 }
305