-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathPostPasswords.php
106 lines (89 loc) · 2.34 KB
/
PostPasswords.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Optionally disable post password protection
*
* @package 10up-experience
*/
namespace TenUpExperience\PostPasswords;
use TenUpExperience\Singleton;
/**
* Post passwords class
*/
class PostPasswords {
use Singleton;
/**
* Setup module
*
* @since 1.7
*/
public function setup() {
add_action( 'admin_init', [ $this, 'password_protection_setting' ] );
add_action( 'admin_print_footer_scripts', [ $this, 'print_admin_css' ] );
}
/**
* Register setting.
*/
public function password_protection_setting() {
$settings_args = array(
'type' => 'integer',
'sanitize_callback' => 'intval',
);
register_setting( 'writing', 'tenup_password_protect', $settings_args );
add_settings_field(
'tenup_password_protect',
esc_html__( 'Enable Password Protected Content', 'tenup' ),
[ $this, 'settings_ui' ],
'writing',
'default',
array(
'label_for' => 'password-protect',
)
);
}
/**
* Display UI the setting.
*
* @return void
*/
public function settings_ui() {
$password_protect = get_option( 'tenup_password_protect', 0 );
?>
<fieldset>
<legend class="screen-reader-text"><?php esc_html_e( 'Password Protected Content', 'tenup' ); ?></legend>
<p>
<input id="password-protect" name="tenup_password_protect" type="checkbox" value="1" <?php checked( $password_protect, 1 ); ?> />
</p>
<p class="description"><?php esc_html_e( 'Enables password protected content. WordPress default password protected post functionality is insecure and does not work with page caching.', 'tenup' ); ?></p>
</fieldset>
<?php
}
/**
* Disable password protect optionally
*/
public function print_admin_css() {
global $pagenow, $post;
if ( empty( $pagenow ) || ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) ) {
return;
}
$password_protect = (bool) get_option( 'tenup_password_protect', 0 );
if ( ! empty( $password_protect ) || ! empty( $post->post_password ) ) {
return;
}
?>
<style type="text/css">
#visibility-radio-password,
label[for="visibility-radio-password"] {
display: none;
}
#editor-post-password-0,
label[for="editor-post-password-0"],
#editor-post-password-0-description,
#editor-post-password-1,
label[for="editor-post-password-1"],
#editor-post-password-1-description {
display: none;
}
</style>
<?php
}
}