1<?php
2/**
3 * Misc WordPress Administration API.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/**
10 * Returns whether the server is running Apache with the mod_rewrite module loaded.
11 *
12 * @since 2.0.0
13 *
14 * @return bool Whether the server is running Apache with the mod_rewrite module loaded.
15 */
16function got_mod_rewrite() {
17 $got_rewrite = apache_mod_loaded( 'mod_rewrite', true );
18
19 /**
20 * Filters whether Apache and mod_rewrite are present.
21 *
22 * This filter was previously used to force URL rewriting for other servers,
23 * like nginx. Use the {@see 'got_url_rewrite'} filter in got_url_rewrite() instead.
24 *
25 * @since 2.5.0
26 *
27 * @see got_url_rewrite()
28 *
29 * @param bool $got_rewrite Whether Apache and mod_rewrite are present.
30 */
31 return apply_filters( 'got_rewrite', $got_rewrite );
32}
33
34/**
35 * Returns whether the server supports URL rewriting.
36 *
37 * Detects Apache's mod_rewrite, IIS 7.0+ permalink support, and nginx.
38 *
39 * @since 3.7.0
40 *
41 * @global bool $is_nginx
42 * @global bool $is_caddy
43 *
44 * @return bool Whether the server supports URL rewriting.
45 */
46function got_url_rewrite() {
47 $got_url_rewrite = ( got_mod_rewrite() || $GLOBALS['is_nginx'] || $GLOBALS['is_caddy'] || iis7_supports_permalinks() );
48
49 /**
50 * Filters whether URL rewriting is available.
51 *
52 * @since 3.7.0
53 *
54 * @param bool $got_url_rewrite Whether URL rewriting is available.
55 */
56 return apply_filters( 'got_url_rewrite', $got_url_rewrite );
57}
58
59/**
60 * Extracts strings from between the BEGIN and END markers in the .htaccess file.
61 *
62 * @since 1.5.0
63 *
64 * @param string $filename Filename to extract the strings from.
65 * @param string $marker The marker to extract the strings from.
66 * @return string[] An array of strings from a file (.htaccess) from between BEGIN and END markers.
67 */
68function extract_from_markers( $filename, $marker ) {
69 $result = array();
70
71 if ( ! file_exists( $filename ) ) {
72 return $result;
73 }
74
75 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
76
77 $state = false;
78
79 foreach ( $markerdata as $markerline ) {
80 if ( str_contains( $markerline, '# END ' . $marker ) ) {
81 $state = false;
82 }
83
84 if ( $state ) {
85 if ( str_starts_with( $markerline, '#' ) ) {
86 continue;
87 }
88
89 $result[] = $markerline;
90 }
91
92 if ( str_contains( $markerline, '# BEGIN ' . $marker ) ) {
93 $state = true;
94 }
95 }
96
97 return $result;
98}
99
100/**
101 * Inserts an array of strings into a file (.htaccess), placing it between
102 * BEGIN and END markers.
103 *
104 * Replaces existing marked info. Retains surrounding
105 * data. Creates file if none exists.
106 *
107 * @since 1.5.0
108 *
109 * @param string $filename Filename to alter.
110 * @param string $marker The marker to alter.
111 * @param array|string $insertion The new content to insert.
112 * @return bool True on write success, false on failure.
113 */
114function insert_with_markers( $filename, $marker, $insertion ) {
115 if ( ! file_exists( $filename ) ) {
116 if ( ! is_writable( dirname( $filename ) ) ) {
117 return false;
118 }
119
120 if ( ! touch( $filename ) ) {
121 return false;
122 }
123
124 // Make sure the file is created with a minimum set of permissions.
125 $perms = fileperms( $filename );
126
127 if ( $perms ) {
128 chmod( $filename, $perms | 0644 );
129 }
130 } elseif ( ! is_writable( $filename ) ) {
131 return false;
132 }
133
134 if ( ! is_array( $insertion ) ) {
135 $insertion = explode( "\n", $insertion );
136 }
137
138 $switched_locale = switch_to_locale( get_locale() );
139
140 $instructions = sprintf(
141 /* translators: 1: Marker. */
142 __(
143 'The directives (lines) between "BEGIN %1$s" and "END %1$s" are
144dynamically generated, and should only be modified via WordPress filters.
145Any changes to the directives between these markers will be overwritten.'
146 ),
147 $marker
148 );
149
150 $instructions = explode( "\n", $instructions );
151
152 foreach ( $instructions as $line => $text ) {
153 $instructions[ $line ] = '# ' . $text;
154 }
155
156 /**
157 * Filters the inline instructions inserted before the dynamically generated content.
158 *
159 * @since 5.3.0
160 *
161 * @param string[] $instructions Array of lines with inline instructions.
162 * @param string $marker The marker being inserted.
163 */
164 $instructions = apply_filters( 'insert_with_markers_inline_instructions', $instructions, $marker );
165
166 if ( $switched_locale ) {
167 restore_previous_locale();
168 }
169
170 $insertion = array_merge( $instructions, $insertion );
171
172 $start_marker = "# BEGIN {$marker}";
173 $end_marker = "# END {$marker}";
174
175 $fp = fopen( $filename, 'r+' );
176
177 if ( ! $fp ) {
178 return false;
179 }
180
181 // Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired.
182 flock( $fp, LOCK_EX );
183
184 $lines = array();
185
186 while ( ! feof( $fp ) ) {
187 $lines[] = rtrim( fgets( $fp ), "\r\n" );
188 }
189
190 // Split out the existing file into the preceding lines, and those that appear after the marker.
191 $pre_lines = array();
192 $post_lines = array();
193 $existing_lines = array();
194 $found_marker = false;
195 $found_end_marker = false;
196
197 foreach ( $lines as $line ) {
198 if ( ! $found_marker && str_contains( $line, $start_marker ) ) {
199 $found_marker = true;
200 continue;
201 } elseif ( ! $found_end_marker && str_contains( $line, $end_marker ) ) {
202 $found_end_marker = true;
203 continue;
204 }
205
206 if ( ! $found_marker ) {
207 $pre_lines[] = $line;
208 } elseif ( $found_end_marker ) {
209 $post_lines[] = $line;
210 } else {
211 $existing_lines[] = $line;
212 }
213 }
214
215 // Check to see if there was a change.
216 if ( $existing_lines === $insertion ) {
217 flock( $fp, LOCK_UN );
218 fclose( $fp );
219
220 return true;
221 }
222
223 // Generate the new file data.
224 $new_file_data = implode(
225 "\n",
226 array_merge(
227 $pre_lines,
228 array( $start_marker ),
229 $insertion,
230 array( $end_marker ),
231 $post_lines
232 )
233 );
234
235 // Write to the start of the file, and truncate it to that length.
236 fseek( $fp, 0 );
237 $bytes = fwrite( $fp, $new_file_data );
238
239 if ( $bytes ) {
240 ftruncate( $fp, ftell( $fp ) );
241 }
242
243 fflush( $fp );
244 flock( $fp, LOCK_UN );
245 fclose( $fp );
246
247 return (bool) $bytes;
248}
249
250/**
251 * Updates the htaccess file with the current rules if it is writable.
252 *
253 * Always writes to the file if it exists and is writable to ensure that we
254 * blank out old rules.
255 *
256 * @since 1.5.0
257 *
258 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
259 *
260 * @return bool|null True on write success, false on failure. Null in multisite.
261 */
262function save_mod_rewrite_rules() {
263 global $wp_rewrite;
264
265 if ( is_multisite() ) {
266 return null;
267 }
268
269 // Ensure get_home_path() is declared.
270 require_once ABSPATH . 'wp-admin/includes/file.php';
271
272 $home_path = get_home_path();
273 $htaccess_file = $home_path . '.htaccess';
274
275 /*
276 * If the file doesn't already exist check for write access to the directory
277 * and whether we have some rules. Else check for write access to the file.
278 */
279 if ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks()
280 || is_writable( $htaccess_file )
281 ) {
282 if ( got_mod_rewrite() ) {
283 $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
284
285 return insert_with_markers( $htaccess_file, 'WordPress', $rules );
286 }
287 }
288
289 return false;
290}
291
292/**
293 * Updates the IIS web.config file with the current rules if it is writable.
294 * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
295 *
296 * @since 2.8.0
297 *
298 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
299 *
300 * @return bool|null True on write success, false on failure. Null in multisite.
301 */
302function iis7_save_url_rewrite_rules() {
303 global $wp_rewrite;
304
305 if ( is_multisite() ) {
306 return null;
307 }
308
309 // Ensure get_home_path() is declared.
310 require_once ABSPATH . 'wp-admin/includes/file.php';
311
312 $home_path = get_home_path();
313 $web_config_file = $home_path . 'web.config';
314
315 // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP.
316 if ( iis7_supports_permalinks()
317 && ( ! file_exists( $web_config_file ) && win_is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks()
318 || win_is_writable( $web_config_file ) )
319 ) {
320 $rule = $wp_rewrite->iis7_url_rewrite_rules( false );
321
322 if ( ! empty( $rule ) ) {
323 return iis7_add_rewrite_rule( $web_config_file, $rule );
324 } else {
325 return iis7_delete_rewrite_rule( $web_config_file );
326 }
327 }
328
329 return false;
330}
331
332/**
333 * Updates the "recently-edited" file for the plugin or theme file editor.
334 *
335 * @since 1.5.0
336 *
337 * @param string $file
338 */
339function update_recently_edited( $file ) {
340 $oldfiles = (array) get_option( 'recently_edited' );
341
342 if ( $oldfiles ) {
343 $oldfiles = array_reverse( $oldfiles );
344 $oldfiles[] = $file;
345 $oldfiles = array_reverse( $oldfiles );
346 $oldfiles = array_unique( $oldfiles );
347
348 if ( 5 < count( $oldfiles ) ) {
349 array_pop( $oldfiles );
350 }
351 } else {
352 $oldfiles[] = $file;
353 }
354
355 update_option( 'recently_edited', $oldfiles );
356}
357
358/**
359 * Makes a tree structure for the theme file editor's file list.
360 *
361 * @since 4.9.0
362 * @access private
363 *
364 * @param array $allowed_files List of theme file paths.
365 * @return array Tree structure for listing theme files.
366 */
367function wp_make_theme_file_tree( $allowed_files ) {
368 $tree_list = array();
369
370 foreach ( $allowed_files as $file_name => $absolute_filename ) {
371 $list = explode( '/', $file_name );
372 $last_dir = &$tree_list;
373
374 foreach ( $list as $dir ) {
375 $last_dir =& $last_dir[ $dir ];
376 }
377
378 $last_dir = $file_name;
379 }
380
381 return $tree_list;
382}
383
384/**
385 * Outputs the formatted file list for the theme file editor.
386 *
387 * @since 4.9.0
388 * @access private
389 *
390 * @global string $relative_file Name of the file being edited relative to the
391 * theme directory.
392 * @global string $stylesheet The stylesheet name of the theme being edited.
393 *
394 * @param array|string $tree List of file/folder paths, or filename.
395 * @param int $level The aria-level for the current iteration.
396 * @param int $size The aria-setsize for the current iteration.
397 * @param int $index The aria-posinset for the current iteration.
398 */
399function wp_print_theme_file_tree( $tree, $level = 2, $size = 1, $index = 1 ) {
400 global $relative_file, $stylesheet;
401
402 if ( is_array( $tree ) ) {
403 $index = 0;
404 $size = count( $tree );
405
406 foreach ( $tree as $label => $theme_file ) :
407 ++$index;
408
409 if ( ! is_array( $theme_file ) ) {
410 wp_print_theme_file_tree( $theme_file, $level, $index, $size );
411 continue;
412 }
413 ?>
414 <li role="treeitem" aria-expanded="true" tabindex="-1"
415 aria-level="<?php echo esc_attr( $level ); ?>"
416 aria-setsize="<?php echo esc_attr( $size ); ?>"
417 aria-posinset="<?php echo esc_attr( $index ); ?>">
418 <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text">
419 <?php
420 /* translators: Hidden accessibility text. */
421 _e( 'folder' );
422 ?>
423 </span><span aria-hidden="true" class="icon"></span></span>
424 <ul role="group" class="tree-folder"><?php wp_print_theme_file_tree( $theme_file, $level + 1, $index, $size ); ?></ul>
425 </li>
426 <?php
427 endforeach;
428 } else {
429 $filename = $tree;
430 $url = add_query_arg(
431 array(
432 'file' => rawurlencode( $tree ),
433 'theme' => rawurlencode( $stylesheet ),
434 ),
435 self_admin_url( 'theme-editor.php' )
436 );
437 ?>
438 <li role="none" class="<?php echo esc_attr( $relative_file === $filename ? 'current-file' : '' ); ?>">
439 <a role="treeitem" tabindex="<?php echo esc_attr( $relative_file === $filename ? '0' : '-1' ); ?>"
440 href="<?php echo esc_url( $url ); ?>"
441 aria-level="<?php echo esc_attr( $level ); ?>"
442 aria-setsize="<?php echo esc_attr( $size ); ?>"
443 aria-posinset="<?php echo esc_attr( $index ); ?>">
444 <?php
445 $file_description = esc_html( get_file_description( $filename ) );
446
447 if ( $file_description !== $filename && wp_basename( $filename ) !== $file_description ) {
448 $file_description .= '<br /><span class="nonessential">(' . esc_html( $filename ) . ')</span>';
449 }
450
451 if ( $relative_file === $filename ) {
452 echo '<span class="notice notice-info">' . $file_description . '</span>';
453 } else {
454 echo $file_description;
455 }
456 ?>
457 </a>
458 </li>
459 <?php
460 }
461}
462
463/**
464 * Makes a tree structure for the plugin file editor's file list.
465 *
466 * @since 4.9.0
467 * @access private
468 *
469 * @param array $plugin_editable_files List of plugin file paths.
470 * @return array Tree structure for listing plugin files.
471 */
472function wp_make_plugin_file_tree( $plugin_editable_files ) {
473 $tree_list = array();
474
475 foreach ( $plugin_editable_files as $plugin_file ) {
476 $list = explode( '/', preg_replace( '#^.+?/#', '', $plugin_file ) );
477 $last_dir = &$tree_list;
478
479 foreach ( $list as $dir ) {
480 $last_dir =& $last_dir[ $dir ];
481 }
482
483 $last_dir = $plugin_file;
484 }
485
486 return $tree_list;
487}
488
489/**
490 * Outputs the formatted file list for the plugin file editor.
491 *
492 * @since 4.9.0
493 * @access private
494 *
495 * @param array|string $tree List of file/folder paths, or filename.
496 * @param string $label Name of file or folder to print.
497 * @param int $level The aria-level for the current iteration.
498 * @param int $size The aria-setsize for the current iteration.
499 * @param int $index The aria-posinset for the current iteration.
500 */
501function wp_print_plugin_file_tree( $tree, $label = '', $level = 2, $size = 1, $index = 1 ) {
502 global $file, $plugin;
503
504 if ( is_array( $tree ) ) {
505 $index = 0;
506 $size = count( $tree );
507
508 foreach ( $tree as $label => $plugin_file ) :
509 ++$index;
510
511 if ( ! is_array( $plugin_file ) ) {
512 wp_print_plugin_file_tree( $plugin_file, $label, $level, $index, $size );
513 continue;
514 }
515 ?>
516 <li role="treeitem" aria-expanded="true" tabindex="-1"
517 aria-level="<?php echo esc_attr( $level ); ?>"
518 aria-setsize="<?php echo esc_attr( $size ); ?>"
519 aria-posinset="<?php echo esc_attr( $index ); ?>">
520 <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text">
521 <?php
522 /* translators: Hidden accessibility text. */
523 _e( 'folder' );
524 ?>
525 </span><span aria-hidden="true" class="icon"></span></span>
526 <ul role="group" class="tree-folder"><?php wp_print_plugin_file_tree( $plugin_file, '', $level + 1, $index, $size ); ?></ul>
527 </li>
528 <?php
529 endforeach;
530 } else {
531 $url = add_query_arg(
532 array(
533 'file' => rawurlencode( $tree ),
534 'plugin' => rawurlencode( $plugin ),
535 ),
536 self_admin_url( 'plugin-editor.php' )
537 );
538 ?>
539 <li role="none" class="<?php echo esc_attr( $file === $tree ? 'current-file' : '' ); ?>">
540 <a role="treeitem" tabindex="<?php echo esc_attr( $file === $tree ? '0' : '-1' ); ?>"
541 href="<?php echo esc_url( $url ); ?>"
542 aria-level="<?php echo esc_attr( $level ); ?>"
543 aria-setsize="<?php echo esc_attr( $size ); ?>"
544 aria-posinset="<?php echo esc_attr( $index ); ?>">
545 <?php
546 if ( $file === $tree ) {
547 echo '<span class="notice notice-info">' . esc_html( $label ) . '</span>';
548 } else {
549 echo esc_html( $label );
550 }
551 ?>
552 </a>
553 </li>
554 <?php
555 }
556}
557
558/**
559 * Flushes rewrite rules if `siteurl`, `home` or `page_on_front` changed.
560 *
561 * @since 2.1.0
562 *
563 * @param string $old_value
564 * @param string $value
565 */
566function update_home_siteurl( $old_value, $value ) {
567 if ( wp_installing() ) {
568 return;
569 }
570
571 if ( is_multisite() && ms_is_switched() ) {
572 delete_option( 'rewrite_rules' );
573 } else {
574 flush_rewrite_rules();
575 }
576}
577
578/**
579 * Resets global variables based on `$_GET` and `$_POST`.
580 *
581 * This function resets global variables based on the names passed
582 * in the `$vars` array to the value of `$_POST[$var]` or `$_GET[$var]` or an
583 * empty string if neither is defined.
584 *
585 * @since 2.0.0
586 *
587 * @param array $vars An array of globals to reset.
588 */
589function wp_reset_vars( $vars ) {
590 foreach ( $vars as $var ) {
591 if ( empty( $_POST[ $var ] ) ) {
592 if ( empty( $_GET[ $var ] ) ) {
593 $GLOBALS[ $var ] = '';
594 } else {
595 $GLOBALS[ $var ] = $_GET[ $var ];
596 }
597 } else {
598 $GLOBALS[ $var ] = $_POST[ $var ];
599 }
600 }
601}
602
603/**
604 * Displays the given administration message.
605 *
606 * @since 2.1.0
607 *
608 * @param string|WP_Error $message
609 */
610function show_message( $message ) {
611 if ( is_wp_error( $message ) ) {
612 if ( $message->get_error_data() && is_string( $message->get_error_data() ) ) {
613 $message = $message->get_error_message() . ': ' . $message->get_error_data();
614 } else {
615 $message = $message->get_error_message();
616 }
617 }
618
619 echo "<p>$message</p>\n";
620 wp_ob_end_flush_all();
621 flush();
622}
623
624/**
625 * @since 2.8.0
626 *
627 * @param string $content
628 * @return string[] Array of function names.
629 */
630function wp_doc_link_parse( $content ) {
631 if ( ! is_string( $content ) || empty( $content ) ) {
632 return array();
633 }
634
635 if ( ! function_exists( 'token_get_all' ) ) {
636 return array();
637 }
638
639 $tokens = token_get_all( $content );
640 $count = count( $tokens );
641 $functions = array();
642 $ignore_functions = array();
643
644 for ( $t = 0; $t < $count - 2; $t++ ) {
645 if ( ! is_array( $tokens[ $t ] ) ) {
646 continue;
647 }
648
649 if ( T_STRING === $tokens[ $t ][0] && ( '(' === $tokens[ $t + 1 ] || '(' === $tokens[ $t + 2 ] ) ) {
650 // If it's a function or class defined locally, there's not going to be any docs available.
651 if ( ( isset( $tokens[ $t - 2 ][1] ) && in_array( $tokens[ $t - 2 ][1], array( 'function', 'class' ), true ) )
652 || ( isset( $tokens[ $t - 2 ][0] ) && T_OBJECT_OPERATOR === $tokens[ $t - 1 ][0] )
653 ) {
654 $ignore_functions[] = $tokens[ $t ][1];
655 }
656
657 // Add this to our stack of unique references.
658 $functions[] = $tokens[ $t ][1];
659 }
660 }
661
662 $functions = array_unique( $functions );
663 sort( $functions );
664
665 /**
666 * Filters the list of functions and classes to be ignored from the documentation lookup.
667 *
668 * @since 2.8.0
669 *
670 * @param string[] $ignore_functions Array of names of functions and classes to be ignored.
671 */
672 $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
673
674 $ignore_functions = array_unique( $ignore_functions );
675
676 $output = array();
677
678 foreach ( $functions as $function ) {
679 if ( in_array( $function, $ignore_functions, true ) ) {
680 continue;
681 }
682
683 $output[] = $function;
684 }
685
686 return $output;
687}
688
689/**
690 * Saves option for number of rows when listing posts, pages, comments, etc.
691 *
692 * @since 2.8.0
693 */
694function set_screen_options() {
695 if ( ! isset( $_POST['wp_screen_options'] ) || ! is_array( $_POST['wp_screen_options'] ) ) {
696 return;
697 }
698
699 check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
700
701 $user = wp_get_current_user();
702
703 if ( ! $user ) {
704 return;
705 }
706
707 $option = $_POST['wp_screen_options']['option'];
708 $value = $_POST['wp_screen_options']['value'];
709
710 if ( sanitize_key( $option ) !== $option ) {
711 return;
712 }
713
714 $map_option = $option;
715 $type = str_replace( 'edit_', '', $map_option );
716 $type = str_replace( '_per_page', '', $type );
717
718 if ( in_array( $type, get_taxonomies(), true ) ) {
719 $map_option = 'edit_tags_per_page';
720 } elseif ( in_array( $type, get_post_types(), true ) ) {
721 $map_option = 'edit_per_page';
722 } else {
723 $option = str_replace( '-', '_', $option );
724 }
725
726 switch ( $map_option ) {
727 case 'edit_per_page':
728 case 'users_per_page':
729 case 'edit_comments_per_page':
730 case 'upload_per_page':
731 case 'edit_tags_per_page':
732 case 'plugins_per_page':
733 case 'export_personal_data_requests_per_page':
734 case 'remove_personal_data_requests_per_page':
735 // Network admin.
736 case 'sites_network_per_page':
737 case 'users_network_per_page':
738 case 'site_users_network_per_page':
739 case 'plugins_network_per_page':
740 case 'themes_network_per_page':
741 case 'site_themes_network_per_page':
742 $value = (int) $value;
743
744 if ( $value < 1 || $value > 999 ) {
745 return;
746 }
747
748 break;
749
750 default:
751 $screen_option = false;
752
753 if ( str_ends_with( $option, '_page' ) || 'layout_columns' === $option ) {
754 /**
755 * Filters a screen option value before it is set.
756 *
757 * The filter can also be used to modify non-standard `[items]_per_page`
758 * settings. See the parent function for a full list of standard options.
759 *
760 * Returning false from the filter will skip saving the current option.
761 *
762 * @since 2.8.0
763 * @since 5.4.2 Only applied to options ending with '_page',
764 * or the 'layout_columns' option.
765 *
766 * @see set_screen_options()
767 *
768 * @param mixed $screen_option The value to save instead of the option value.
769 * Default false (to skip saving the current option).
770 * @param string $option The option name.
771 * @param int $value The option value.
772 */
773 $screen_option = apply_filters( 'set-screen-option', $screen_option, $option, $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
774 }
775
776 /**
777 * Filters a screen option value before it is set.
778 *
779 * The dynamic portion of the hook name, `$option`, refers to the option name.
780 *
781 * Returning false from the filter will skip saving the current option.
782 *
783 * @since 5.4.2
784 *
785 * @see set_screen_options()
786 *
787 * @param mixed $screen_option The value to save instead of the option value.
788 * Default false (to skip saving the current option).
789 * @param string $option The option name.
790 * @param int $value The option value.
791 */
792 $value = apply_filters( "set_screen_option_{$option}", $screen_option, $option, $value );
793
794 if ( false === $value ) {
795 return;
796 }
797
798 break;
799 }
800
801 update_user_meta( $user->ID, $option, $value );
802
803 $url = remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() );
804
805 if ( isset( $_POST['mode'] ) ) {
806 $url = add_query_arg( array( 'mode' => $_POST['mode'] ), $url );
807 }
808
809 wp_safe_redirect( $url );
810 exit;
811}
812
813/**
814 * Checks if rewrite rule for WordPress already exists in the IIS 7+ configuration file.
815 *
816 * @since 2.8.0
817 *
818 * @param string $filename The file path to the configuration file.
819 * @return bool
820 */
821function iis7_rewrite_rule_exists( $filename ) {
822 if ( ! file_exists( $filename ) ) {
823 return false;
824 }
825
826 if ( ! class_exists( 'DOMDocument', false ) ) {
827 return false;
828 }
829
830 $doc = new DOMDocument();
831
832 if ( $doc->load( $filename ) === false ) {
833 return false;
834 }
835
836 $xpath = new DOMXPath( $doc );
837 $rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
838
839 if ( 0 === $rules->length ) {
840 return false;
841 }
842
843 return true;
844}
845
846/**
847 * Deletes WordPress rewrite rule from web.config file if it exists there.
848 *
849 * @since 2.8.0
850 *
851 * @param string $filename Name of the configuration file.
852 * @return bool
853 */
854function iis7_delete_rewrite_rule( $filename ) {
855 // If configuration file does not exist then rules also do not exist, so there is nothing to delete.
856 if ( ! file_exists( $filename ) ) {
857 return true;
858 }
859
860 if ( ! class_exists( 'DOMDocument', false ) ) {
861 return false;
862 }
863
864 $doc = new DOMDocument();
865 $doc->preserveWhiteSpace = false;
866
867 if ( $doc->load( $filename ) === false ) {
868 return false;
869 }
870
871 $xpath = new DOMXPath( $doc );
872 $rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
873
874 if ( $rules->length > 0 ) {
875 $child = $rules->item( 0 );
876 $parent = $child->parentNode;
877 $parent->removeChild( $child );
878 $doc->formatOutput = true;
879 saveDomDocument( $doc, $filename );
880 }
881
882 return true;
883}
884
885/**
886 * Adds WordPress rewrite rule to the IIS 7+ configuration file.
887 *
888 * @since 2.8.0
889 *
890 * @param string $filename The file path to the configuration file.
891 * @param string $rewrite_rule The XML fragment with URL Rewrite rule.
892 * @return bool
893 */
894function iis7_add_rewrite_rule( $filename, $rewrite_rule ) {
895 if ( ! class_exists( 'DOMDocument', false ) ) {
896 return false;
897 }
898
899 // If configuration file does not exist then we create one.
900 if ( ! file_exists( $filename ) ) {
901 $fp = fopen( $filename, 'w' );
902 fwrite( $fp, '<configuration/>' );
903 fclose( $fp );
904 }
905
906 $doc = new DOMDocument();
907 $doc->preserveWhiteSpace = false;
908
909 if ( $doc->load( $filename ) === false ) {
910 return false;
911 }
912
913 $xpath = new DOMXPath( $doc );
914
915 // First check if the rule already exists as in that case there is no need to re-add it.
916 $wordpress_rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
917
918 if ( $wordpress_rules->length > 0 ) {
919 return true;
920 }
921
922 // Check the XPath to the rewrite rule and create XML nodes if they do not exist.
923 $xml_nodes = $xpath->query( '/configuration/system.webServer/rewrite/rules' );
924
925 if ( $xml_nodes->length > 0 ) {
926 $rules_node = $xml_nodes->item( 0 );
927 } else {
928 $rules_node = $doc->createElement( 'rules' );
929
930 $xml_nodes = $xpath->query( '/configuration/system.webServer/rewrite' );
931
932 if ( $xml_nodes->length > 0 ) {
933 $rewrite_node = $xml_nodes->item( 0 );
934 $rewrite_node->appendChild( $rules_node );
935 } else {
936 $rewrite_node = $doc->createElement( 'rewrite' );
937 $rewrite_node->appendChild( $rules_node );
938
939 $xml_nodes = $xpath->query( '/configuration/system.webServer' );
940
941 if ( $xml_nodes->length > 0 ) {
942 $system_web_server_node = $xml_nodes->item( 0 );
943 $system_web_server_node->appendChild( $rewrite_node );
944 } else {
945 $system_web_server_node = $doc->createElement( 'system.webServer' );
946 $system_web_server_node->appendChild( $rewrite_node );
947
948 $xml_nodes = $xpath->query( '/configuration' );
949
950 if ( $xml_nodes->length > 0 ) {
951 $config_node = $xml_nodes->item( 0 );
952 $config_node->appendChild( $system_web_server_node );
953 } else {
954 $config_node = $doc->createElement( 'configuration' );
955 $doc->appendChild( $config_node );
956 $config_node->appendChild( $system_web_server_node );
957 }
958 }
959 }
960 }
961
962 $rule_fragment = $doc->createDocumentFragment();
963 $rule_fragment->appendXML( $rewrite_rule );
964 $rules_node->appendChild( $rule_fragment );
965
966 $doc->encoding = 'UTF-8';
967 $doc->formatOutput = true;
968 saveDomDocument( $doc, $filename );
969
970 return true;
971}
972
973/**
974 * Saves the XML document into a file.
975 *
976 * @since 2.8.0
977 *
978 * @param DOMDocument $doc
979 * @param string $filename
980 */
981function saveDomDocument( $doc, $filename ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
982 $config = $doc->saveXML();
983 $config = preg_replace( "/([^\r])\n/", "$1\r\n", $config );
984
985 $fp = fopen( $filename, 'w' );
986 fwrite( $fp, $config );
987 fclose( $fp );
988}
989
990/**
991 * Displays the default administration color scheme picker (Used in user-edit.php).
992 *
993 * @since 3.0.0
994 *
995 * @global array $_wp_admin_css_colors
996 *
997 * @param int $user_id User ID.
998 */
999function admin_color_scheme_picker( $user_id ) {
1000 global $_wp_admin_css_colors;
1001
1002 ksort( $_wp_admin_css_colors );
1003
1004 if ( isset( $_wp_admin_css_colors['fresh'] ) ) {
1005 // Set Default ('fresh') and Light should go first.
1006 $_wp_admin_css_colors = array_filter(
1007 array_merge(
1008 array(
1009 'fresh' => '',
1010 'light' => '',
1011 'modern' => '',
1012 ),
1013 $_wp_admin_css_colors
1014 )
1015 );
1016 }
1017
1018 $current_color = get_user_option( 'admin_color', $user_id );
1019
1020 if ( empty( $current_color ) || ! isset( $_wp_admin_css_colors[ $current_color ] ) ) {
1021 $current_color = 'fresh';
1022 }
1023 ?>
1024 <fieldset id="color-picker" class="scheme-list">
1025 <legend class="screen-reader-text"><span><?php _e( 'Administration Color Scheme' ); ?></span></legend>
1026 <?php
1027 wp_nonce_field( 'save-color-scheme', 'color-nonce', false );
1028 foreach ( $_wp_admin_css_colors as $color => $color_info ) :
1029
1030 ?>
1031 <div class="color-option <?php echo ( $color === $current_color ) ? 'selected' : ''; ?>">
1032 <input name="admin_color" id="admin_color_<?php echo esc_attr( $color ); ?>" type="radio" value="<?php echo esc_attr( $color ); ?>" class="tog" <?php checked( $color, $current_color ); ?> />
1033 <input type="hidden" class="css_url" value="<?php echo esc_url( $color_info->url ); ?>" />
1034 <input type="hidden" class="icon_colors" value="<?php echo esc_attr( wp_json_encode( array( 'icons' => $color_info->icon_colors ) ) ); ?>" />
1035 <label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
1036 <div class="color-palette">
1037 <?php
1038 foreach ( $color_info->colors as $html_color ) {
1039 ?>
1040 <div class="color-palette-shade" style="background-color: <?php echo esc_attr( $html_color ); ?>"> </div>
1041 <?php
1042 }
1043 ?>
1044 </div>
1045 </div>
1046 <?php
1047
1048 endforeach;
1049 ?>
1050 </fieldset>
1051 <?php
1052}
1053
1054/**
1055 *
1056 * @since 3.8.0
1057 *
1058 * @global array $_wp_admin_css_colors
1059 */
1060function wp_color_scheme_settings() {
1061 global $_wp_admin_css_colors;
1062
1063 $color_scheme = get_user_option( 'admin_color' );
1064
1065 // It's possible to have a color scheme set that is no longer registered.
1066 if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
1067 $color_scheme = 'fresh';
1068 }
1069
1070 if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
1071 $icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
1072 } elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
1073 $icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
1074 } else {
1075 // Fall back to the default set of icon colors if the default scheme is missing.
1076 $icon_colors = array(
1077 'base' => '#a7aaad',
1078 'focus' => '#72aee6',
1079 'current' => '#fff',
1080 );
1081 }
1082
1083 echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ";</script>\n";
1084}
1085
1086/**
1087 * Displays the viewport meta in the admin.
1088 *
1089 * @since 5.5.0
1090 */
1091function wp_admin_viewport_meta() {
1092 /**
1093 * Filters the viewport meta in the admin.
1094 *
1095 * @since 5.5.0
1096 *
1097 * @param string $viewport_meta The viewport meta.
1098 */
1099 $viewport_meta = apply_filters( 'admin_viewport_meta', 'width=device-width,initial-scale=1.0' );
1100
1101 if ( empty( $viewport_meta ) ) {
1102 return;
1103 }
1104
1105 echo '<meta name="viewport" content="' . esc_attr( $viewport_meta ) . '">';
1106}
1107
1108/**
1109 * Adds viewport meta for mobile in Customizer.
1110 *
1111 * Hooked to the {@see 'admin_viewport_meta'} filter.
1112 *
1113 * @since 5.5.0
1114 *
1115 * @param string $viewport_meta The viewport meta.
1116 * @return string Filtered viewport meta.
1117 */
1118function _customizer_mobile_viewport_meta( $viewport_meta ) {
1119 return trim( $viewport_meta, ',' ) . ',minimum-scale=0.5,maximum-scale=1.2';
1120}
1121
1122/**
1123 * Checks lock status for posts displayed on the Posts screen.
1124 *
1125 * @since 3.6.0
1126 *
1127 * @param array $response The Heartbeat response.
1128 * @param array $data The $_POST data sent.
1129 * @param string $screen_id The screen ID.
1130 * @return array The Heartbeat response.
1131 */
1132function wp_check_locked_posts( $response, $data, $screen_id ) {
1133 $checked = array();
1134
1135 if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
1136 foreach ( $data['wp-check-locked-posts'] as $key ) {
1137 $post_id = absint( substr( $key, 5 ) );
1138
1139 if ( ! $post_id ) {
1140 continue;
1141 }
1142
1143 $user_id = wp_check_post_lock( $post_id );
1144
1145 if ( $user_id ) {
1146 $user = get_userdata( $user_id );
1147
1148 if ( $user && current_user_can( 'edit_post', $post_id ) ) {
1149 $send = array(
1150 'name' => $user->display_name,
1151 /* translators: %s: User's display name. */
1152 'text' => sprintf( __( '%s is currently editing' ), $user->display_name ),
1153 );
1154
1155 if ( get_option( 'show_avatars' ) ) {
1156 $send['avatar_src'] = get_avatar_url( $user->ID, array( 'size' => 18 ) );
1157 $send['avatar_src_2x'] = get_avatar_url( $user->ID, array( 'size' => 36 ) );
1158 }
1159
1160 $checked[ $key ] = $send;
1161 }
1162 }
1163 }
1164 }
1165
1166 if ( ! empty( $checked ) ) {
1167 $response['wp-check-locked-posts'] = $checked;
1168 }
1169
1170 return $response;
1171}
1172
1173/**
1174 * Checks lock status on the New/Edit Post screen and refresh the lock.
1175 *
1176 * @since 3.6.0
1177 *
1178 * @param array $response The Heartbeat response.
1179 * @param array $data The $_POST data sent.
1180 * @param string $screen_id The screen ID.
1181 * @return array The Heartbeat response.
1182 */
1183function wp_refresh_post_lock( $response, $data, $screen_id ) {
1184 if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
1185 $received = $data['wp-refresh-post-lock'];
1186 $send = array();
1187
1188 $post_id = absint( $received['post_id'] );
1189
1190 if ( ! $post_id ) {
1191 return $response;
1192 }
1193
1194 if ( ! current_user_can( 'edit_post', $post_id ) ) {
1195 return $response;
1196 }
1197
1198 $user_id = wp_check_post_lock( $post_id );
1199 $user = get_userdata( $user_id );
1200
1201 if ( $user ) {
1202 $error = array(
1203 'name' => $user->display_name,
1204 /* translators: %s: User's display name. */
1205 'text' => sprintf( __( '%s has taken over and is currently editing.' ), $user->display_name ),
1206 );
1207
1208 if ( get_option( 'show_avatars' ) ) {
1209 $error['avatar_src'] = get_avatar_url( $user->ID, array( 'size' => 64 ) );
1210 $error['avatar_src_2x'] = get_avatar_url( $user->ID, array( 'size' => 128 ) );
1211 }
1212
1213 $send['lock_error'] = $error;
1214 } else {
1215 $new_lock = wp_set_post_lock( $post_id );
1216
1217 if ( $new_lock ) {
1218 $send['new_lock'] = implode( ':', $new_lock );
1219 }
1220 }
1221
1222 $response['wp-refresh-post-lock'] = $send;
1223 }
1224
1225 return $response;
1226}
1227
1228/**
1229 * Checks nonce expiration on the New/Edit Post screen and refresh if needed.
1230 *
1231 * @since 3.6.0
1232 *
1233 * @param array $response The Heartbeat response.
1234 * @param array $data The $_POST data sent.
1235 * @param string $screen_id The screen ID.
1236 * @return array The Heartbeat response.
1237 */
1238function wp_refresh_post_nonces( $response, $data, $screen_id ) {
1239 if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
1240 $received = $data['wp-refresh-post-nonces'];
1241
1242 $response['wp-refresh-post-nonces'] = array( 'check' => 1 );
1243
1244 $post_id = absint( $received['post_id'] );
1245
1246 if ( ! $post_id ) {
1247 return $response;
1248 }
1249
1250 if ( ! current_user_can( 'edit_post', $post_id ) ) {
1251 return $response;
1252 }
1253
1254 $response['wp-refresh-post-nonces'] = array(
1255 'replace' => array(
1256 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ),
1257 'samplepermalinknonce' => wp_create_nonce( 'samplepermalink' ),
1258 'closedpostboxesnonce' => wp_create_nonce( 'closedpostboxes' ),
1259 '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
1260 '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
1261 ),
1262 );
1263 }
1264
1265 return $response;
1266}
1267
1268/**
1269 * Refresh nonces used with meta boxes in the block editor.
1270 *
1271 * @since 6.1.0
1272 *
1273 * @param array $response The Heartbeat response.
1274 * @param array $data The $_POST data sent.
1275 * @return array The Heartbeat response.
1276 */
1277function wp_refresh_metabox_loader_nonces( $response, $data ) {
1278 if ( empty( $data['wp-refresh-metabox-loader-nonces'] ) ) {
1279 return $response;
1280 }
1281
1282 $received = $data['wp-refresh-metabox-loader-nonces'];
1283 $post_id = (int) $received['post_id'];
1284
1285 if ( ! $post_id ) {
1286 return $response;
1287 }
1288
1289 if ( ! current_user_can( 'edit_post', $post_id ) ) {
1290 return $response;
1291 }
1292
1293 $response['wp-refresh-metabox-loader-nonces'] = array(
1294 'replace' => array(
1295 'metabox_loader_nonce' => wp_create_nonce( 'meta-box-loader' ),
1296 '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
1297 ),
1298 );
1299
1300 return $response;
1301}
1302
1303/**
1304 * Adds the latest Heartbeat and REST API nonce to the Heartbeat response.
1305 *
1306 * @since 5.0.0
1307 *
1308 * @param array $response The Heartbeat response.
1309 * @return array The Heartbeat response.
1310 */
1311function wp_refresh_heartbeat_nonces( $response ) {
1312 // Refresh the Rest API nonce.
1313 $response['rest_nonce'] = wp_create_nonce( 'wp_rest' );
1314
1315 // Refresh the Heartbeat nonce.
1316 $response['heartbeat_nonce'] = wp_create_nonce( 'heartbeat-nonce' );
1317
1318 return $response;
1319}
1320
1321/**
1322 * Disables suspension of Heartbeat on the Add/Edit Post screens.
1323 *
1324 * @since 3.8.0
1325 *
1326 * @global string $pagenow The filename of the current screen.
1327 *
1328 * @param array $settings An array of Heartbeat settings.
1329 * @return array Filtered Heartbeat settings.
1330 */
1331function wp_heartbeat_set_suspension( $settings ) {
1332 global $pagenow;
1333
1334 if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
1335 $settings['suspension'] = 'disable';
1336 }
1337
1338 return $settings;
1339}
1340
1341/**
1342 * Performs autosave with heartbeat.
1343 *
1344 * @since 3.9.0
1345 *
1346 * @param array $response The Heartbeat response.
1347 * @param array $data The $_POST data sent.
1348 * @return array The Heartbeat response.
1349 */
1350function heartbeat_autosave( $response, $data ) {
1351 if ( ! empty( $data['wp_autosave'] ) ) {
1352 $saved = wp_autosave( $data['wp_autosave'] );
1353
1354 if ( is_wp_error( $saved ) ) {
1355 $response['wp_autosave'] = array(
1356 'success' => false,
1357 'message' => $saved->get_error_message(),
1358 );
1359 } elseif ( empty( $saved ) ) {
1360 $response['wp_autosave'] = array(
1361 'success' => false,
1362 'message' => __( 'Error while saving.' ),
1363 );
1364 } else {
1365 /* translators: Draft saved date format, see https://www.php.net/manual/datetime.format.php */
1366 $draft_saved_date_format = __( 'g:i:s a' );
1367 $response['wp_autosave'] = array(
1368 'success' => true,
1369 /* translators: %s: Date and time. */
1370 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ),
1371 );
1372 }
1373 }
1374
1375 return $response;
1376}
1377
1378/**
1379 * Removes single-use URL parameters and create canonical link based on new URL.
1380 *
1381 * Removes specific query string parameters from a URL, create the canonical link,
1382 * put it in the admin header, and change the current URL to match.
1383 *
1384 * @since 4.2.0
1385 */
1386function wp_admin_canonical_url() {
1387 $removable_query_args = wp_removable_query_args();
1388
1389 if ( empty( $removable_query_args ) ) {
1390 return;
1391 }
1392
1393 // Ensure we're using an absolute URL.
1394 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
1395 $filtered_url = remove_query_arg( $removable_query_args, $current_url );
1396
1397 /**
1398 * Filters the admin canonical URL value.
1399 *
1400 * @since 6.5.0
1401 *
1402 * @param string $filtered_url The admin canonical URL value.
1403 */
1404 $filtered_url = apply_filters( 'wp_admin_canonical_url', $filtered_url );
1405 ?>
1406 <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
1407 <script>
1408 if ( window.history.replaceState ) {
1409 window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
1410 }
1411 </script>
1412 <?php
1413}
1414
1415/**
1416 * Outputs JS that reloads the page if the user navigated to it with the Back or Forward button.
1417 *
1418 * Used on the Edit Post and Add New Post screens. Needed to ensure the page is not loaded from browser cache,
1419 * so the post title and editor content are the last saved versions. Ideally this script should run first in the head.
1420 *
1421 * @since 4.6.0
1422 */
1423function wp_page_reload_on_back_button_js() {
1424 ?>
1425 <script>
1426 if ( typeof performance !== 'undefined' && performance.navigation && performance.navigation.type === 2 ) {
1427 document.location.reload( true );
1428 }
1429 </script>
1430 <?php
1431}
1432
1433/**
1434 * Sends a confirmation request email when a change of site admin email address is attempted.
1435 *
1436 * The new site admin address will not become active until confirmed.
1437 *
1438 * @since 3.0.0
1439 * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific.
1440 *
1441 * @param string $old_value The old site admin email address.
1442 * @param string $value The proposed new site admin email address.
1443 */
1444function update_option_new_admin_email( $old_value, $value ) {
1445 if ( get_option( 'admin_email' ) === $value || ! is_email( $value ) ) {
1446 return;
1447 }
1448
1449 $hash = md5( $value . time() . wp_rand() );
1450 $new_admin_email = array(
1451 'hash' => $hash,
1452 'newemail' => $value,
1453 );
1454 update_option( 'adminhash', $new_admin_email, false );
1455
1456 $switched_locale = switch_to_user_locale( get_current_user_id() );
1457
1458 /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
1459 $email_text = __(
1460 'Howdy,
1461
1462A site administrator (###USERNAME###) recently requested to have the
1463administration email address changed on this site:
1464###SITEURL###
1465
1466To confirm this change, please click on the following link:
1467###ADMIN_URL###
1468
1469You can safely ignore and delete this email if you do not want to
1470take this action.
1471
1472This email has been sent to ###EMAIL###
1473
1474Regards,
1475All at ###SITENAME###
1476###SITEURL###'
1477 );
1478
1479 /**
1480 * Filters the text of the email sent when a change of site admin email address is attempted.
1481 *
1482 * The following strings have a special meaning and will get replaced dynamically:
1483 *
1484 * - `###USERNAME###` The current user's username.
1485 * - `###ADMIN_URL###` The link to click on to confirm the email change.
1486 * - `###EMAIL###` The proposed new site admin email address.
1487 * - `###SITENAME###` The name of the site.
1488 * - `###SITEURL###` The URL to the site.
1489 *
1490 * @since MU (3.0.0)
1491 * @since 4.9.0 This filter is no longer Multisite specific.
1492 *
1493 * @param string $email_text Text in the email.
1494 * @param array $new_admin_email {
1495 * Data relating to the new site admin email address.
1496 *
1497 * @type string $hash The secure hash used in the confirmation link URL.
1498 * @type string $newemail The proposed new site admin email address.
1499 * }
1500 */
1501 $content = apply_filters( 'new_admin_email_content', $email_text, $new_admin_email );
1502
1503 $current_user = wp_get_current_user();
1504 $content = str_replace( '###USERNAME###', $current_user->user_login, $content );
1505 $content = str_replace( '###ADMIN_URL###', esc_url( self_admin_url( 'options.php?adminhash=' . $hash ) ), $content );
1506 $content = str_replace( '###EMAIL###', $value, $content );
1507 $content = str_replace( '###SITENAME###', wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $content );
1508 $content = str_replace( '###SITEURL###', home_url(), $content );
1509
1510 if ( '' !== get_option( 'blogname' ) ) {
1511 $site_title = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
1512 } else {
1513 $site_title = parse_url( home_url(), PHP_URL_HOST );
1514 }
1515
1516 $subject = sprintf(
1517 /* translators: New admin email address notification email subject. %s: Site title. */
1518 __( '[%s] New Admin Email Address' ),
1519 $site_title
1520 );
1521
1522 /**
1523 * Filters the subject of the email sent when a change of site admin email address is attempted.
1524 *
1525 * @since 6.5.0
1526 *
1527 * @param string $subject Subject of the email.
1528 */
1529 $subject = apply_filters( 'new_admin_email_subject', $subject );
1530
1531 wp_mail( $value, $subject, $content );
1532
1533 if ( $switched_locale ) {
1534 restore_previous_locale();
1535 }
1536}
1537
1538/**
1539 * Appends '(Draft)' to draft page titles in the privacy page dropdown
1540 * so that unpublished content is obvious.
1541 *
1542 * @since 4.9.8
1543 * @access private
1544 *
1545 * @param string $title Page title.
1546 * @param WP_Post $page Page data object.
1547 * @return string Page title.
1548 */
1549function _wp_privacy_settings_filter_draft_page_titles( $title, $page ) {
1550 if ( 'draft' === $page->post_status && 'privacy' === get_current_screen()->id ) {
1551 /* translators: %s: Page title. */
1552 $title = sprintf( __( '%s (Draft)' ), $title );
1553 }
1554
1555 return $title;
1556}
1557
1558/**
1559 * Checks if the user needs to update PHP.
1560 *
1561 * @since 5.1.0
1562 * @since 5.1.1 Added the {@see 'wp_is_php_version_acceptable'} filter.
1563 *
1564 * @return array|false {
1565 * Array of PHP version data. False on failure.
1566 *
1567 * @type string $recommended_version The PHP version recommended by WordPress.
1568 * @type string $minimum_version The minimum required PHP version.
1569 * @type bool $is_supported Whether the PHP version is actively supported.
1570 * @type bool $is_secure Whether the PHP version receives security updates.
1571 * @type bool $is_acceptable Whether the PHP version is still acceptable or warnings
1572 * should be shown and an update recommended.
1573 * }
1574 */
1575function wp_check_php_version() {
1576 $version = PHP_VERSION;
1577 $key = md5( $version );
1578
1579 $response = get_site_transient( 'php_check_' . $key );
1580
1581 if ( false === $response ) {
1582 $url = 'http://api.wordpress.org/core/serve-happy/1.0/';
1583
1584 if ( wp_http_supports( array( 'ssl' ) ) ) {
1585 $url = set_url_scheme( $url, 'https' );
1586 }
1587
1588 $url = add_query_arg( 'php_version', $version, $url );
1589
1590 $response = wp_remote_get( $url );
1591
1592 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
1593 return false;
1594 }
1595
1596 $response = json_decode( wp_remote_retrieve_body( $response ), true );
1597
1598 if ( ! is_array( $response ) ) {
1599 return false;
1600 }
1601
1602 set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
1603 }
1604
1605 if ( isset( $response['is_acceptable'] ) && $response['is_acceptable'] ) {
1606 /**
1607 * Filters whether the active PHP version is considered acceptable by WordPress.
1608 *
1609 * Returning false will trigger a PHP version warning to show up in the admin dashboard to administrators.
1610 *
1611 * This filter is only run if the wordpress.org Serve Happy API considers the PHP version acceptable, ensuring
1612 * that this filter can only make this check stricter, but not loosen it.
1613 *
1614 * @since 5.1.1
1615 *
1616 * @param bool $is_acceptable Whether the PHP version is considered acceptable. Default true.
1617 * @param string $version PHP version checked.
1618 */
1619 $response['is_acceptable'] = (bool) apply_filters( 'wp_is_php_version_acceptable', true, $version );
1620 }
1621
1622 $response['is_lower_than_future_minimum'] = false;
1623
1624 // The minimum supported PHP version will be updated to 7.4 in the future. Check if the current version is lower.
1625 if ( version_compare( $version, '7.4', '<' ) ) {
1626 $response['is_lower_than_future_minimum'] = true;
1627
1628 // Force showing of warnings.
1629 $response['is_acceptable'] = false;
1630 }
1631
1632 return $response;
1633}
1634