-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclass-accessibility-statement-plugin.php
247 lines (218 loc) · 7.16 KB
/
class-accessibility-statement-plugin.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* Main Plugin file for the Accessibility Statement Generator
*/
require_once 'class-statement-generator.php';
/**
* Main Plugin File
*/
class AccessibilityStatementPlugin {
/**
* Plugin admin sections
*
* @var array
*/
private $sections = array();
/**
* Contructor
*
* Add admin pages and actions
*
* @return void
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
add_action( 'display_post_states', array( $this, 'add_post_state' ), 10, 2 );
add_action( 'admin_notices', array( $this, 'add_admin_notices' ) );
add_action( 'admin_post_set_accessibility_statement', array( $this, 'set_accessibility_statement' ) );
add_action( 'admin_post_create_accessibility_statement', array( $this, 'create_accessibility_statement' ) );
}
/**
* Create the accessibility statement page
*
* @return void
*/
public function create_accessibility_statement() {
if ( ! $this->check_nonce( 'create_accessibility_statement' ) ) {
wp_redirect( admin_url( 'options-general.php?page=accessibility-statement&failed-nonce=1' ) );
exit;
}
$default_accessibility_statement_content = StatementGenerator::get_default_content();
$accessibility_statement_id = wp_insert_post(
array(
'post_title' => __( 'Accessibility Statement' ),
'post_status' => 'draft',
'post_type' => 'page',
'post_content' => $default_accessibility_statement_content,
),
true
);
if ( is_wp_error( $accessibility_statement_id ) ) {
wp_redirect( admin_url( 'options-general.php?page=accessibility-statement&page-created-error=1' ) );
exit;
} else {
update_option( 'wp_page_for_accessibility_statement', $accessibility_statement_id );
wp_redirect( admin_url( 'post.php?post=' . $accessibility_statement_id . '&action=edit' ) );
exit;
}
}
/**
* Check a nonce
*
* @param string $nonce_field Nonce name.
*
* @return boolean True if we can verify the nonce, else false
*/
private function check_nonce( $nonce_field ) {
if ( ! isset( $_POST[ $nonce_field ] ) || ! wp_verify_nonce( $_POST[ $nonce_field ], $nonce_field ) ) {
return false;
}
return true;
}
/**
* Set the accessibility statement page
*
* @return void
*/
public function set_accessibility_statement() {
if ( ! $this->check_nonce( 'set_accessibility_statement' ) ) {
wp_redirect( admin_url( 'options-general.php?page=accessibility-statement&failed-nonce=1' ) );
exit;
}
$accessibility_page_id = isset( $_POST['page_for_accessibility_statement'] ) ? (int) $_POST['page_for_accessibility_statement'] : 0;
update_option( 'wp_page_for_accessibility_statement', $accessibility_page_id );
wp_redirect( admin_url( 'options-general.php?page=accessibility-statement&page-updated=1' ) );
exit;
}
/**
* Display notices on settings page
*
* @return void
*/
public function add_admin_notices() {
$screen = get_current_screen();
if ( 'settings_page_accessibility-statement' === $screen->id ) {
$this->display_nonce_notices();
$this->display_update_statement_notices();
$this->display_create_statement_notices();
$this->display_selected_statement_notices();
}
}
/**
* Display notices relating to nonce checks
*
* @return void
*/
private function display_nonce_notices() {
if ( isset( $_GET['failed-nonce'] ) && $_GET['failed-nonce'] ) {
add_settings_error(
'page_for_accessibility_statement',
'page_for_accessibility_statement',
__( 'Nonce couldn\'t be verified.' ),
'error'
);
}
}
/**
* Display the notices related to the chosen Accessibility Statement page
*
* @return void
*/
private function display_selected_statement_notices() {
$accessibility_statement_id = (int) get_option( 'wp_page_for_accessibility_statement' );
if ( ! empty( $accessibility_statement_id ) ) {
$accessibility_statement = get_post( $accessibility_statement_id );
if ( ! $accessibility_statement instanceof WP_Post ) {
add_settings_error(
'page_for_accessibility_statement',
'page_for_accessibility_statement',
__( 'The currently selected Accessibility Statement page does not exist. Please create or select a new page.' ),
'error'
);
} else {
if ( 'trash' === $accessibility_statement->post_status ) {
add_settings_error(
'page_for_accessibility_statement',
'page_for_accessibility_statement',
sprintf(
/* translators: %s: URL to Pages Trash. */
__( 'The currently selected Accessibility Statement page is in the Trash. Please create or select a new Accessibility Statement page or <a href="%s">restore the current page</a>.' ),
'edit.php?post_status=trash&post_type=page'
),
'error'
);
}
}
}
}
/**
* Display the notices related to creating an Accessibility Statement
*
* @return void
*/
private function display_create_statement_notices() {
if ( isset( $_GET['page-created-error'] ) && $_GET['page-created-error'] ) {
add_settings_error(
'page_for_accessibility_statement',
'page_for_accessibility_statement',
__( 'Unable to create an Accessibility Statement.' ),
'error'
);
}
}
/**
* Display the notices related to updating the Accessiblity Statement page
*
* @return void
*/
private function display_update_statement_notices() {
if ( isset( $_GET['page-updated'] ) && $_GET['page-updated'] ) {
$accessibility_statement_updated_message = __( 'Accessibility Statement page updated successfully.' );
$accessibility_page_id = get_option( 'wp_page_for_accessibility_statement' );
if ( $accessibility_page_id ) {
if (
'publish' === get_post_status( $accessibility_page_id )
&& current_user_can( 'edit_theme_options' )
&& current_theme_supports( 'menus' )
) {
$accessibility_statement_updated_message = sprintf(
/* translators: %s: URL to Customizer -> Menus. */
__( 'Accessibility Statement page setting updated successfully. Remember to <a href="%s">update your menus</a>!' ),
esc_url( add_query_arg( 'autofocus[panel]', 'nav_menus', admin_url( 'customize.php' ) ) )
);
}
}
add_settings_error( 'page_for_accessibility_statement', 'page_for_accessibility_statement', $accessibility_statement_updated_message, 'success' );
}
}
/**
* Add the post state for the Accessibility Statement
*
* @param array $post_states Array of post states.
* @param WP_Post $post Current Post.
*
* @return array Array of post states
*/
public function add_post_state( $post_states, $post ) {
if ( intval( get_option( 'wp_page_for_accessibility_statement' ) ) === $post->ID ) {
$post_states['page_for_accessibility_statement'] = _x( 'Accessibility Statement Page', 'page label' );
}
return $post_states;
}
/**
* Add Settings page to Admin dashboard
*
* @return void
*/
public function add_settings_page() {
add_options_page( __( 'Accessibility Statement', 'a11y-statement' ), __( 'Accessibility', 'a11y-statement' ), 'manage_options', 'accessibility-statement', array( $this, 'page_contents' ), 7 );
}
/**
* Get the Settings page contents
*
* @return void
*/
public function page_contents() {
include 'settings-page.php';
}
}