PluginProbe ʕ •ᴥ•ʔ
Smart Custom 404 Error Page / trunk
Smart Custom 404 Error Page vtrunk
trunk 11.4.6 11.4.7 11.4.8
404page / inc / class-404page-admin.php
404page / inc Last commit date
admin 1 year ago ppf 1 year ago class-404page-admin.php 1 year ago class-404page-block-editor.php 1 year ago class-404page-classic-editor.php 1 year ago class-404page-deprecated.php 1 year ago class-404page-settings.php 1 year ago class-404page.php 1 year ago index.php 9 years ago
class-404page-admin.php
498 lines
1 <?php
2
3 /**
4 * The 404page admin plugin class
5 *
6 * @since 10
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * The admin plugin class
15 */
16 if ( !class_exists( 'PP_404Page_Admin' ) ) {
17
18 class PP_404Page_Admin extends PPF09_Admin {
19
20
21 /**
22 * Do Init
23 *
24 * @since 10
25 * @access public
26 */
27 public function init() {
28
29 $this->add_actions( array(
30 'admin_init',
31 'admin_menu',
32 'admin_head'
33 ) );
34
35 add_action( 'admin_enqueue_scripts', array( $this, 'admin_js' ) );
36 add_action( 'admin_enqueue_scripts', array( $this, 'admin_css' ) );
37
38 }
39
40
41 /**
42 * init admin
43 * moved to PP_404Page_Admin in v 10
44 */
45 function action_admin_init() {
46
47 $this->settings()->set_method();
48
49 // @since 11.0.0
50 $this->add_setting_sections(
51
52 array(
53
54 array(
55
56 'section' => 'general',
57 'order' => 10,
58 'title' => esc_html__( 'General', '404page' ),
59 'icon' => 'general',
60 'fields' => array(
61 array(
62 'key' => 'page_id',
63 'callback' => 'admin_404page'
64 )
65 )
66
67 ),
68
69 array(
70
71 'section' => 'advanced',
72 'order' => 20,
73 'title' => esc_html__( 'Advanced', '404page' ),
74 'icon' => 'advanced',
75 'fields' => array(
76 array(
77 'key' => 'hide',
78 'callback' => 'admin_hide'
79 ),
80 array(
81 'key' => 'fire_error',
82 'callback' => 'admin_fire404'
83 ),
84 array(
85 'key' => 'force_error',
86 'callback' => 'admin_force404'
87 ),
88 array(
89 'key' => 'no_url_guessing',
90 'callback' => 'admin_noguess'
91 ),
92 array(
93 'key' => 'http410_if_trashed',
94 'callback' => 'admin_http410'
95 ),
96 array(
97 'key' => 'http410_always',
98 'callback' => 'admin_http410_always'
99 ),
100 array(
101 'key' => 'method',
102 'callback' => 'admin_method'
103 )
104
105 )
106
107 )
108
109 )
110
111 );
112
113 do_action( '404page_addtional_setting_sections' );
114
115 }
116
117
118 /**
119 * sanitize settings
120 * was handle_method() in previous versions
121 * as of version 11.0.0 the method is part of the settings array
122 *
123 * @since 11.0.0
124 * @param array $settings array of settings to save
125 * @access public
126 */
127 public function sanitize_settings( $settings ) {
128
129 if ( ! array_key_exists( 'method', $settings ) || ( $settings['method'] != 'STD' && $settings['method'] != 'CMP' ) ) {
130
131 $settings['method'] = 'STD';
132
133 }
134
135 return $settings;
136
137 }
138
139
140 /**
141 * handle the settings field page id
142 * moved to PP_404Page_Admin in v 10
143 */
144 function admin_404page() {
145
146 echo esc_html__( 'Page to be displayed as 404 page', '404page' );
147
148 if ( $this->settings()->get( 'page_id' ) < 0 ) {
149
150 echo '<div class="error form-invalid" style="line-height: 3em">' . esc_html__( 'The page you have selected as 404 page does not exist anymore. Please choose another page.', '404page' ) . '</div>';
151 }
152
153 wp_dropdown_pages( array( 'name' => $this->settings()->get_option_name() . '[page_id]', 'id' => 'select404page', 'echo' => 1, 'show_option_none' => esc_html__( '&mdash; NONE (WP default 404 page) &mdash;', '404page'), 'option_none_value' => '0', 'selected' => $this->settings()->get( 'page_id' ) ) );
154
155 echo '<div id="404page_edit_link" style="display: none">' . get_edit_post_link( $this->settings()->get( 'page_id' ) ) . '</div>';
156 echo '<div id="404page_test_link" style="display: none">' . get_site_url() . '/404page-test-' . md5( rand() ) . '</div>';
157 echo '<div id="404page_current_value" style="display: none">' . $this->settings()->get( 'page_id' ) . '</div>';
158 echo '<p class="submit"><input type="button" name="edit_404_page" id="edit_404_page" class="button secondary" value="' . esc_html__( 'Edit Page', '404page' ) . '" />&nbsp;<input type="button" name="test_404_page" id="test_404_page" class="button secondary" value="' . esc_html__( 'Test 404 error', '404page' ) . '" /></p>';
159
160 if ( defined( 'WPSEO_VERSION' ) && method_exists( 'WPSEO_Options', 'get' ) && WPSEO_Options::get( 'enable_xml_sitemap' ) ) {
161
162 // as of version 11.1.1 we not only check inf Yoast SEO is active but also if Yoast SEO sitemap feature is activated
163
164 echo '<p class="pp-404page-info">';
165
166 if ( $this->settings()->get( 'fire_error' ) ) {
167
168 echo esc_html__( 'Yoast SEO sitemap detected. Your 404 page is automatically excluded from the XML sitemap created by Yoast.', '404page' );
169
170 } else {
171
172 echo esc_html__( 'Yoast SEO sitemap detected. Your 404 page is NOT automatically excluded from the XML sitemap created by Yoast, because you disabled the option "Send an 404 error if the page is accessed directly by its URL" on the "Advanced" tab.', '404page' );
173
174 }
175
176 echo '</p><br />';
177
178 }
179
180 if ( defined( 'JETPACK__VERSION' ) && method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'sitemaps' ) ) {
181
182 // Jetpack since version 11.1.2
183
184 echo '<p class="pp-404page-info">';
185
186 if ( $this->settings()->get( 'fire_error' ) ) {
187
188 echo esc_html__( 'Jetpack sitemap detected. Your 404 page is automatically excluded from the XML sitemap created by Jetpack.', '404page' );
189
190 } else {
191
192 echo esc_html__( 'Jetpack sitemap detected. Your 404 page is NOT automatically excluded from the XML sitemap created by Jetpack, because you disabled the option "Send an 404 error if the page is accessed directly by its URL" on the "Advanced" tab.', '404page' );
193
194 }
195
196 echo '</p><br />';
197
198 }
199
200
201 // WP Super Cache
202 // since 11.2.0
203 if ( defined('WPCACHEHOME') ) {
204
205 global $cache_enabled;
206
207 // is caching active?
208 if ( $cache_enabled ) {
209
210 echo '<p class="pp-404page-info">';
211 echo esc_html__( 'WP Super Cache detected. All 404 errors are automatically excluded from caching.', '404page' );
212 echo '</p><br />';
213
214 }
215
216 }
217
218
219 // W3 Total Cache
220 // since 11.2.1
221 if ( defined( 'W3TC' ) ) {
222
223 if ( class_exists( 'W3TC\Dispatcher' ) ) {
224
225 // is caching active?
226 if ( W3TC\Dispatcher::config()->get_boolean( 'pgcache.enabled' ) ) {
227
228 echo '<p class="pp-404page-info">';
229 echo esc_html__( 'W3 Total Cache detected. All 404 errors are automatically excluded from caching.', '404page' );
230 echo '</p><br />';
231
232 }
233
234 }
235
236 }
237
238 }
239
240
241 /**
242 * handle the settings field hide
243 * moved to PP_404Page_Admin in v 10
244 */
245 function admin_hide() {
246
247 $this->print_slider_check(
248 'hide',
249 esc_html__( 'Hide the selected page from the Pages list', '404page' ),
250 false,
251 false,
252 '<span class="dashicons dashicons-info"></span>&nbsp;' . esc_html__( 'For Administrators the page is always visible.', '404page' )
253 );
254
255 }
256
257
258 /**
259 * handle the settings field fire 404 error
260 * moved to PP_404Page_Admin in v 10
261 */
262 function admin_fire404() {
263
264 $this->print_slider_check(
265 'fire_error',
266 esc_html__( 'Send an 404 error if the page is accessed directly by its URL', '404page' ),
267 false,
268 false,
269 '<span class="dashicons dashicons-info"></span>&nbsp;' . esc_html__( 'Uncheck this if you want the selected page to be accessible.', '404page' )
270 );
271
272 }
273
274
275 /**
276 * handle the settings field to force an 404 error
277 * moved to PP_404Page_Admin in v 10
278 */
279 function admin_force404() {
280
281 $this->print_slider_check(
282 'force_error',
283 esc_html__( 'Force 404 error after loading page', '404page' ),
284 false,
285 '09OOCbFLfnI',
286 '<span class="dashicons dashicons-warning"></span>&nbsp;' . esc_html__( 'Generally this is not needed. It is not recommended to activate this option, unless it is necessary. Please note that this may cause problems with your theme.', '404page' )
287 );
288
289 }
290
291
292 /**
293 * handle the settings field to stop URL guessing
294 * moved to PP_404Page_Admin in v 10
295 */
296 function admin_noguess() {
297
298 $this->print_slider_check(
299 'no_url_guessing',
300 esc_html__( 'Disable URL autocorrection guessing', '404page' ),
301 false,
302 'H0EdtFcAGl4',
303 '<span class="dashicons dashicons-warning"></span>&nbsp;' . esc_html__( 'This stops WordPress from URL autocorrection guessing. Only activate, if you are sure about the consequences.', '404page' )
304 );
305
306 }
307
308
309 /**
310 * handle the settings field to send an http 410 error in case the object is trashed
311 * @since 3.2
312 * moved to PP_404Page_Admin in v 10
313 */
314 function admin_http410() {
315
316 $this->print_slider_check(
317 'http410_if_trashed',
318 esc_html__( 'Send an HTTP 410 error instead of HTTP 404 in case the requested object is in trash', '404page' ),
319 false,
320 'O5xPM0BMZxM',
321 '<span class="dashicons dashicons-info"></span>&nbsp;' . esc_html__( 'Check this if you want to inform search engines that the resource requested is no longer available and will not be available again so it can be removed from the search index immediately.', '404page' ),
322 $this->settings()->get( 'http410_always' )
323 );
324
325 }
326
327
328 /**
329 * handle the settings field to always send an http 410 error
330 * @since 11.3.0
331 */
332 function admin_http410_always() {
333
334 $this->print_slider_check(
335 'http410_always',
336 esc_html__( 'Always send an HTTP 410 error instead of HTTP 404', '404page' ),
337 false,
338 false,
339 '<span class="dashicons dashicons-info"></span>&nbsp;' . esc_html__( 'Check this if you always want to send an HTTP 410 error instead of an HTTP 404 error.', '404page' )
340 );
341
342 }
343
344
345 /**
346 * handle the settings field method
347 * moved to PP_404Page_Admin in v 10
348 */
349 function admin_method() {
350
351 // unfortunately we can't use print_slider_check() here
352
353 if ( $this->core()->is_native() || defined( 'CUSTOMIZR_VER' ) || defined( 'ICL_SITEPRESS_VERSION' ) ) {
354
355 $dis = ' disabled="disabled"';
356
357 } else {
358
359 $dis = '';
360 }
361
362 echo '<p class="toggle"><span class="slider"><input type="checkbox" id="404page-method" name="404page_settings[method]" value="CMP"' . checked( 'CMP', $this->settings()->get( 'method' ), false ) . $dis . '/>';
363 echo '<label for="404page-method" class="check"></label></span><span class="caption">' . esc_html__( 'Activate Compatibility Mode', '404page' ) . '<br />';
364 echo '<span class="dashicons dashicons-info"></span>&nbsp;';
365
366 if ( $this->core()->is_native() ) {
367
368 esc_html_e( 'This setting is not available because the Theme you are using natively supports the 404page plugin.', '404page' );
369
370 } elseif ( defined( 'CUSTOMIZR_VER' ) ) {
371
372 esc_html_e( 'This setting is not availbe because the 404page Plugin works in Customizr Compatibility Mode.', '404page' );
373
374 } elseif ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
375
376 esc_html_e( 'This setting is not availbe because the 404page Plugin works in WPML Mode.', '404page' );
377
378 } else {
379
380 esc_html_e( 'If you are using a theme or plugin that modifies the WordPress Template System, the 404page plugin may not work properly. Compatibility Mode maybe can fix the problem. Activate Compatibility Mode only if you have any problems.', '404page' );
381
382 }
383
384 echo '</span></p>';
385
386 }
387
388
389 /**
390 * create the menu entry
391 * moved to PP_404Page_Admin in v 10
392 */
393 function action_admin_menu() {
394 $screen_id = add_theme_page ( esc_html__( '404 Error Page', "404page" ), esc_html__( '404 Error Page', '404page' ), 'manage_options', '404pagesettings', array( $this, 'show_admin' ) );
395 $this->set_screen_id( $screen_id );
396 }
397
398
399 /**
400 * add admin css to header
401 * moved to PP_404Page_Admin in v 10
402 */
403 function action_admin_head() {
404
405 if ( $this->settings()->get( 'page_id' ) > 0 ) {
406
407 echo '<style type="text/css">';
408
409 foreach ( $this->core()->get_all_page_ids() as $pid ) {
410
411 echo '#the-list #post-' . $pid . ' .column-title .row-title:before { content: "404"; background-color: #333; color: #FFF; display: inline-block; padding: 0 5px; margin-right: 10px; }';
412
413 }
414
415 echo '</style>';
416
417 }
418
419 }
420
421
422 /**
423 * add admin css file
424 * moved to PP_404Page_Admin in v 10
425 */
426 function admin_css() {
427
428 if ( get_current_screen()->id == $this->get_screen_id() ) {
429
430 wp_enqueue_style( '404pagelity', $this->core()->get_asset_url( 'css', 'lity.min.css' ) );
431 wp_enqueue_style( '404pagecss', $this->core()->get_asset_url( 'css', '404page-ui.css' ) );
432
433 do_action( '404page_enqueue_css' );
434
435 }
436
437 }
438
439
440 /**
441 * add admin js files
442 * moved to PP_404Page_Admin in v 10
443 */
444 function admin_js() {
445
446 if ( get_current_screen()->id == $this->get_screen_id() ) {
447
448 wp_enqueue_script( '404page-ui', $this->core()->get_asset_url( 'js', '404page-ui.js' ), 'jquery', $this->core()->get_plugin_version(), true );
449 wp_enqueue_script( '404page-lity', $this->core()->get_asset_url( 'js', 'lity.min.js' ), 'jquery', $this->core()->get_plugin_version(), true );
450
451 do_action( '404page_enqueue_js' );
452
453 }
454
455 }
456
457
458 /**
459 * show admin page
460 * moved to PP_404Page_Admin in v 10
461 */
462 function show_admin() {
463
464 $this->show( 'manage_options' );
465
466 }
467
468
469 /**
470 * create nonce
471 *
472 * @since 10.4
473 * @access private
474 * @return string Nonce
475 */
476 private function get_nonce() {
477
478 return wp_create_nonce( 'pp_404page_dismiss_admin_notice' );
479
480 }
481
482
483 /**
484 * check nonce
485 *
486 * @since 10.4
487 * @access private
488 * @return boolean
489 */
490 private function check_nonce() {
491
492 return check_ajax_referer( 'pp_404page_dismiss_admin_notice', 'securekey', false );
493
494 }
495
496 }
497
498 }