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

242 lines 11.2 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 do_action( 'password_protected_help_tabs', get_current_screen() );
53 }
54
55 /**
56 * Help Tabs
57 *
58 * @param object $current_screen Screen object.
59 */
60 function help_tabs( $current_screen ) {
61 $current_screen->add_help_tab( array(
62 'id' => 'PASSWORD_PROTECTED_SETTINGS',
63 'title' => __( 'Password Protected', 'password-protected' ),
64 'content' => __( '<p><strong>Password Protected Status</strong><br />Turn on/off password protection.</p>', 'password-protected' )
65 . __( '<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' )
66 . __( '<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' )
67 ) );
68 }
69
70 /**
71 * Settings API
72 */
73 function password_protected_settings() {
74 add_settings_section(
75 'password_protected',
76 '',
77 array( $this, 'password_protected_settings_section' ),
78 $this->options_group
79 );
80 add_settings_field(
81 'password_protected_status',
82 __( 'Password Protected Status', 'password-protected' ),
83 array( $this, 'password_protected_status_field' ),
84 $this->options_group,
85 'password_protected'
86 );
87 add_settings_field(
88 'password_protected_permissions',
89 __( 'Protected Permissions', 'password-protected' ),
90 array( $this, 'password_protected_permissions_field' ),
91 $this->options_group,
92 'password_protected'
93 );
94 add_settings_field(
95 'password_protected_password',
96 __( 'New Password', 'password-protected' ),
97 array( $this, 'password_protected_password_field' ),
98 $this->options_group,
99 'password_protected'
100 );
101 register_setting( $this->options_group, 'password_protected_status', 'intval' );
102 register_setting( $this->options_group, 'password_protected_feeds', 'intval' );
103 register_setting( $this->options_group, 'password_protected_administrators', 'intval' );
104 register_setting( $this->options_group, 'password_protected_users', 'intval' );
105 register_setting( $this->options_group, 'password_protected_password', array( $this, 'sanitize_password_protected_password' ) );
106 }
107
108 /**
109 * Sanitize Password Field Input
110 *
111 * @param string $val Password.
112 * @return string Sanitized password.
113 */
114 function sanitize_password_protected_password( $val ) {
115 $old_val = get_option( 'password_protected_password' );
116 if ( is_array( $val ) ) {
117 if ( empty( $val['new'] ) ) {
118 return $old_val;
119 } elseif ( empty( $val['confirm'] ) ) {
120 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' ) );
121 return $old_val;
122 } elseif ( $val['new'] != $val['confirm'] ) {
123 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password not saved. Password fields did not match.', 'password-protected' ) );
124 return $old_val;
125 } elseif ( $val['new'] == $val['confirm'] ) {
126 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password saved.', 'password-protected' ), 'updated' );
127 return $val['new'];
128 }
129 return get_option( 'password_protected_password' );
130 }
131 return $val;
132 }
133
134 /**
135 * Password Protected Section
136 */
137 function password_protected_settings_section() {
138 echo '<p>' . __( 'Password protect your web site. Users will be asked to enter a password to view the site.', 'password-protected' ) . '<br />
139 ' . __( 'For more information about Password Protected settings, view the "Help" tab at the top of this page.', 'password-protected' ) . '</p>';
140 }
141
142 /**
143 * Password Protection Status Field
144 */
145 function password_protected_status_field() {
146 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>';
147 }
148
149 /**
150 * Password Protection Permissions Field
151 */
152 function password_protected_permissions_field() {
153 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>';
154 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>';
155 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>';
156 }
157
158 /**
159 * Password Field
160 */
161 function password_protected_password_field() {
162 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>
163 <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>';
164 }
165
166 /**
167 * Pre-update 'password_protected_password' Option
168 *
169 * Before the password is saved, MD5 it!
170 * Doing it in this way allows developers to intercept with an earlier filter if they
171 * need to do something with the plaintext password.
172 *
173 * @param string $newvalue New Value.
174 * @param string $oldvalue Old Value.
175 * @return string Filtered new value.
176 */
177 function pre_update_option_password_protected_password( $newvalue, $oldvalue ) {
178 global $Password_Protected;
179 if ( $newvalue != $oldvalue ) {
180 $newvalue = $Password_Protected->encrypt_password( $newvalue );
181 }
182 return $newvalue;
183 }
184
185 /**
186 * Plugin Row Meta
187 *
188 * Adds GitHub and translate links below the plugin description on the plugins page.
189 *
190 * @param array $plugin_meta Plugin meta display array.
191 * @param string $plugin_file Plugin reference.
192 * @param array $plugin_data Plugin data.
193 * @param string $status Plugin status.
194 * @return array Plugin meta array.
195 */
196 function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
197 if ( 'password-protected/password-protected.php' == $plugin_file ) {
198 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'http://github.com/benhuson/password-protected', 'password-protected' ), __( 'GitHub', 'password-protected' ) );
199 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'https://www.transifex.com/projects/p/password-protected/resource/password-protected/', 'password-protected' ), __( 'Translate', 'password-protected' ) );
200 }
201 return $plugin_meta;
202 }
203
204 /**
205 * Plugin Action Links
206 *
207 * Adds settings link on the plugins page.
208 *
209 * @param array $actions Plugin action links array.
210 * @return array Plugin action links array.
211 */
212 function plugin_action_links( $actions ) {
213 $actions[] = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=password-protected' ), __( 'Settings', 'password-protected' ) );
214 return $actions;
215 }
216
217 /**
218 * Password Admin Notice
219 * Warns the user if they have enabled password protection but not entered a password
220 */
221 function password_protected_admin_notices(){
222 $current_screen = get_current_screen();
223 if ( $current_screen->id == 'options-' . $this->options_group ) {
224 $status = get_option( 'password_protected_status' );
225 $pwd = get_option( 'password_protected_password' );
226 if ( (bool) $status && empty( $pwd ) ) {
227 echo '<div class="error"><p>' . __( 'You have enabled password protection but not yet set a password. Please set one below.', 'password-protected' ) . '</p></div>';
228 }
229 if ( current_user_can( 'manage_options' ) && ( (bool) get_option( 'password_protected_administrators' ) || (bool) get_option( 'password_protected_users' ) ) ) {
230 if ( (bool) get_option( 'password_protected_administrators' ) && (bool) get_option( 'password_protected_users' ) ) {
231 echo '<div class="error"><p>' . __( 'You have enabled password protection and allowed administrators and logged in users - other users will still need to login to view the site.', 'password-protected' ) . '</p></div>';
232 } elseif ( (bool) get_option( 'password_protected_administrators' ) ) {
233 echo '<div class="error"><p>' . __( 'You have enabled password protection and allowed administrators - other users will still need to login to view the site.', 'password-protected' ) . '</p></div>';
234 } elseif ( (bool) get_option( 'password_protected_users' ) ) {
235 echo '<div class="error"><p>' . __( 'You have enabled password protection and allowed logged in users - other users will still need to login to view the site.', 'password-protected' ) . '</p></div>';
236 }
237 }
238 }
239 }
240
241 }
242