[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> misc.php (source)

   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   */
  16  function 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   */
  46  function 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   */
  68  function 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   */
 114  function 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
 144  dynamically generated, and should only be modified via WordPress filters.
 145  Any 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   */
 262  function 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   */
 302  function 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 Path to the recently edited file.
 338   */
 339  function 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   */
 367  function 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   */
 399  function 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   */
 472  function 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   * @global string $file   Path to the file being edited.
 496   * @global string $plugin Path to the plugin file relative to the plugins directory.
 497   *
 498   * @param array|string $tree  List of file/folder paths, or filename.
 499   * @param string       $label Name of file or folder to print.
 500   * @param int          $level The aria-level for the current iteration.
 501   * @param int          $size  The aria-setsize for the current iteration.
 502   * @param int          $index The aria-posinset for the current iteration.
 503   */
 504  function wp_print_plugin_file_tree( $tree, $label = '', $level = 2, $size = 1, $index = 1 ) {
 505      global $file, $plugin;
 506  
 507      if ( is_array( $tree ) ) {
 508          $index = 0;
 509          $size  = count( $tree );
 510  
 511          foreach ( $tree as $label => $plugin_file ) :
 512              ++$index;
 513  
 514              if ( ! is_array( $plugin_file ) ) {
 515                  wp_print_plugin_file_tree( $plugin_file, $label, $level, $index, $size );
 516                  continue;
 517              }
 518              ?>
 519              <li role="treeitem" aria-expanded="true" tabindex="-1"
 520                  aria-level="<?php echo esc_attr( $level ); ?>"
 521                  aria-setsize="<?php echo esc_attr( $size ); ?>"
 522                  aria-posinset="<?php echo esc_attr( $index ); ?>">
 523                  <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text">
 524                      <?php
 525                      /* translators: Hidden accessibility text. */
 526                      _e( 'folder' );
 527                      ?>
 528                  </span><span aria-hidden="true" class="icon"></span></span>
 529                  <ul role="group" class="tree-folder"><?php wp_print_plugin_file_tree( $plugin_file, '', $level + 1, $index, $size ); ?></ul>
 530              </li>
 531              <?php
 532          endforeach;
 533      } else {
 534          $url = add_query_arg(
 535              array(
 536                  'file'   => rawurlencode( $tree ),
 537                  'plugin' => rawurlencode( $plugin ),
 538              ),
 539              self_admin_url( 'plugin-editor.php' )
 540          );
 541          ?>
 542          <li role="none" class="<?php echo esc_attr( $file === $tree ? 'current-file' : '' ); ?>">
 543              <a role="treeitem" tabindex="<?php echo esc_attr( $file === $tree ? '0' : '-1' ); ?>"
 544                  href="<?php echo esc_url( $url ); ?>"
 545                  aria-level="<?php echo esc_attr( $level ); ?>"
 546                  aria-setsize="<?php echo esc_attr( $size ); ?>"
 547                  aria-posinset="<?php echo esc_attr( $index ); ?>">
 548                  <?php
 549                  if ( $file === $tree ) {
 550                      echo '<span class="notice notice-info">' . esc_html( $label ) . '</span>';
 551                  } else {
 552                      echo esc_html( $label );
 553                  }
 554                  ?>
 555              </a>
 556          </li>
 557          <?php
 558      }
 559  }
 560  
 561  /**
 562   * Flushes rewrite rules if `siteurl`, `home` or `page_on_front` changed.
 563   *
 564   * @since 2.1.0
 565   *
 566   * @param string $old_value The old value of the option. Unused.
 567   * @param string $value     The new value of the option. Unused.
 568   */
 569  function update_home_siteurl( $old_value, $value ) {
 570      if ( wp_installing() ) {
 571          return;
 572      }
 573  
 574      if ( is_multisite() && ms_is_switched() ) {
 575          delete_option( 'rewrite_rules' );
 576      } else {
 577          flush_rewrite_rules();
 578      }
 579  }
 580  
 581  /**
 582   * Resets global variables based on `$_GET` and `$_POST`.
 583   *
 584   * This function resets global variables based on the names passed
 585   * in the `$vars` array to the value of `$_POST[$var]` or `$_GET[$var]` or an
 586   * empty string if neither is defined.
 587   *
 588   * @since 2.0.0
 589   *
 590   * @param array $vars An array of globals to reset.
 591   */
 592  function wp_reset_vars( $vars ) {
 593      foreach ( $vars as $var ) {
 594          if ( empty( $_POST[ $var ] ) ) {
 595              if ( empty( $_GET[ $var ] ) ) {
 596                  $GLOBALS[ $var ] = '';
 597              } else {
 598                  $GLOBALS[ $var ] = $_GET[ $var ];
 599              }
 600          } else {
 601              $GLOBALS[ $var ] = $_POST[ $var ];
 602          }
 603      }
 604  }
 605  
 606  /**
 607   * Displays the given administration message.
 608   *
 609   * @since 2.1.0
 610   *
 611   * @param string|WP_Error $message The message to display, or a WP_Error object.
 612   */
 613  function show_message( $message ) {
 614      if ( is_wp_error( $message ) ) {
 615          if ( $message->get_error_data() && is_string( $message->get_error_data() ) ) {
 616              $message = $message->get_error_message() . ': ' . $message->get_error_data();
 617          } else {
 618              $message = $message->get_error_message();
 619          }
 620      }
 621  
 622      echo "<p>$message</p>\n";
 623      wp_ob_end_flush_all();
 624      flush();
 625  }
 626  
 627  /**
 628   * Parses the PHP content and finds function calls to be used for documentation linking.
 629   *
 630   * @since 2.8.0
 631   *
 632   * @param string $content The PHP content to parse.
 633   * @return string[] Array of function names.
 634   */
 635  function wp_doc_link_parse( $content ) {
 636      if ( ! is_string( $content ) || empty( $content ) ) {
 637          return array();
 638      }
 639  
 640      if ( ! function_exists( 'token_get_all' ) ) {
 641          return array();
 642      }
 643  
 644      $tokens           = token_get_all( $content );
 645      $count            = count( $tokens );
 646      $functions        = array();
 647      $ignore_functions = array();
 648  
 649      for ( $t = 0; $t < $count - 2; $t++ ) {
 650          if ( ! is_array( $tokens[ $t ] ) ) {
 651              continue;
 652          }
 653  
 654          if ( T_STRING === $tokens[ $t ][0] && ( '(' === $tokens[ $t + 1 ] || '(' === $tokens[ $t + 2 ] ) ) {
 655              // If it's a function or class defined locally, there's not going to be any docs available.
 656              if ( ( isset( $tokens[ $t - 2 ][1] ) && in_array( $tokens[ $t - 2 ][1], array( 'function', 'class' ), true ) )
 657                  || ( isset( $tokens[ $t - 2 ][0] ) && T_OBJECT_OPERATOR === $tokens[ $t - 1 ][0] )
 658              ) {
 659                  $ignore_functions[] = $tokens[ $t ][1];
 660              }
 661  
 662              // Add this to our stack of unique references.
 663              $functions[] = $tokens[ $t ][1];
 664          }
 665      }
 666  
 667      $functions = array_unique( $functions );
 668      sort( $functions );
 669  
 670      /**
 671       * Filters the list of functions and classes to be ignored from the documentation lookup.
 672       *
 673       * @since 2.8.0
 674       *
 675       * @param string[] $ignore_functions Array of names of functions and classes to be ignored.
 676       */
 677      $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
 678  
 679      $ignore_functions = array_unique( $ignore_functions );
 680  
 681      $output = array();
 682  
 683      foreach ( $functions as $function ) {
 684          if ( in_array( $function, $ignore_functions, true ) ) {
 685              continue;
 686          }
 687  
 688          $output[] = $function;
 689      }
 690  
 691      return $output;
 692  }
 693  
 694  /**
 695   * Saves option for number of rows when listing posts, pages, comments, etc.
 696   *
 697   * @since 2.8.0
 698   */
 699  function set_screen_options() {
 700      if ( ! isset( $_POST['wp_screen_options'] ) || ! is_array( $_POST['wp_screen_options'] ) ) {
 701          return;
 702      }
 703  
 704      check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
 705  
 706      $user = wp_get_current_user();
 707  
 708      if ( ! $user ) {
 709          return;
 710      }
 711  
 712      $option = $_POST['wp_screen_options']['option'];
 713      $value  = $_POST['wp_screen_options']['value'];
 714  
 715      if ( sanitize_key( $option ) !== $option ) {
 716          return;
 717      }
 718  
 719      $map_option = $option;
 720      $type       = str_replace( 'edit_', '', $map_option );
 721      $type       = str_replace( '_per_page', '', $type );
 722  
 723      if ( in_array( $type, get_taxonomies(), true ) ) {
 724          $map_option = 'edit_tags_per_page';
 725      } elseif ( in_array( $type, get_post_types(), true ) ) {
 726          $map_option = 'edit_per_page';
 727      } else {
 728          $option = str_replace( '-', '_', $option );
 729      }
 730  
 731      switch ( $map_option ) {
 732          case 'edit_per_page':
 733          case 'users_per_page':
 734          case 'edit_comments_per_page':
 735          case 'upload_per_page':
 736          case 'edit_tags_per_page':
 737          case 'plugins_per_page':
 738          case 'export_personal_data_requests_per_page':
 739          case 'remove_personal_data_requests_per_page':
 740              // Network admin.
 741          case 'sites_network_per_page':
 742          case 'users_network_per_page':
 743          case 'site_users_network_per_page':
 744          case 'plugins_network_per_page':
 745          case 'themes_network_per_page':
 746          case 'site_themes_network_per_page':
 747              $value = (int) $value;
 748  
 749              if ( $value < 1 || $value > 999 ) {
 750                  return;
 751              }
 752  
 753              break;
 754  
 755          default:
 756              $screen_option = false;
 757  
 758              if ( str_ends_with( $option, '_page' ) || 'layout_columns' === $option ) {
 759                  /**
 760                   * Filters a screen option value before it is set.
 761                   *
 762                   * The filter can also be used to modify non-standard `[items]_per_page`
 763                   * settings. See the parent function for a full list of standard options.
 764                   *
 765                   * Returning false from the filter will skip saving the current option.
 766                   *
 767                   * @since 2.8.0
 768                   * @since 5.4.2 Only applied to options ending with '_page',
 769                   *              or the 'layout_columns' option.
 770                   *
 771                   * @see set_screen_options()
 772                   *
 773                   * @param mixed  $screen_option The value to save instead of the option value.
 774                   *                              Default false (to skip saving the current option).
 775                   * @param string $option        The option name.
 776                   * @param int    $value         The option value.
 777                   */
 778                  $screen_option = apply_filters( 'set-screen-option', $screen_option, $option, $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 779              }
 780  
 781              /**
 782               * Filters a screen option value before it is set.
 783               *
 784               * The dynamic portion of the hook name, `$option`, refers to the option name.
 785               *
 786               * Returning false from the filter will skip saving the current option.
 787               *
 788               * @since 5.4.2
 789               *
 790               * @see set_screen_options()
 791               *
 792               * @param mixed   $screen_option The value to save instead of the option value.
 793               *                               Default false (to skip saving the current option).
 794               * @param string  $option        The option name.
 795               * @param int     $value         The option value.
 796               */
 797              $value = apply_filters( "set_screen_option_{$option}", $screen_option, $option, $value );
 798  
 799              if ( false === $value ) {
 800                  return;
 801              }
 802  
 803              break;
 804      }
 805  
 806      update_user_meta( $user->ID, $option, $value );
 807  
 808      $url = remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() );
 809  
 810      if ( isset( $_POST['mode'] ) ) {
 811          $url = add_query_arg( array( 'mode' => $_POST['mode'] ), $url );
 812      }
 813  
 814      wp_safe_redirect( $url );
 815      exit;
 816  }
 817  
 818  /**
 819   * Checks if rewrite rule for WordPress already exists in the IIS 7+ configuration file.
 820   *
 821   * @since 2.8.0
 822   *
 823   * @param string $filename The file path to the configuration file.
 824   * @return bool Whether the rule exists.
 825   */
 826  function iis7_rewrite_rule_exists( $filename ) {
 827      if ( ! file_exists( $filename ) ) {
 828          return false;
 829      }
 830  
 831      if ( ! class_exists( 'DOMDocument', false ) ) {
 832          return false;
 833      }
 834  
 835      $doc = new DOMDocument();
 836  
 837      if ( $doc->load( $filename ) === false ) {
 838          return false;
 839      }
 840  
 841      $xpath = new DOMXPath( $doc );
 842      $rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
 843  
 844      if ( 0 === $rules->length ) {
 845          return false;
 846      }
 847  
 848      return true;
 849  }
 850  
 851  /**
 852   * Deletes WordPress rewrite rule from web.config file if it exists there.
 853   *
 854   * @since 2.8.0
 855   *
 856   * @param string $filename Name of the configuration file.
 857   * @return bool Whether the rule was deleted.
 858   */
 859  function iis7_delete_rewrite_rule( $filename ) {
 860      // If configuration file does not exist then rules also do not exist, so there is nothing to delete.
 861      if ( ! file_exists( $filename ) ) {
 862          return true;
 863      }
 864  
 865      if ( ! class_exists( 'DOMDocument', false ) ) {
 866          return false;
 867      }
 868  
 869      $doc                     = new DOMDocument();
 870      $doc->preserveWhiteSpace = false;
 871  
 872      if ( $doc->load( $filename ) === false ) {
 873          return false;
 874      }
 875  
 876      $xpath = new DOMXPath( $doc );
 877      $rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
 878  
 879      if ( $rules->length > 0 ) {
 880          $child  = $rules->item( 0 );
 881          $parent = $child->parentNode;
 882          $parent->removeChild( $child );
 883          $doc->formatOutput = true;
 884          saveDomDocument( $doc, $filename );
 885      }
 886  
 887      return true;
 888  }
 889  
 890  /**
 891   * Adds WordPress rewrite rule to the IIS 7+ configuration file.
 892   *
 893   * @since 2.8.0
 894   *
 895   * @param string $filename     The file path to the configuration file.
 896   * @param string $rewrite_rule The XML fragment with URL Rewrite rule.
 897   * @return bool Whether the rule was added.
 898   */
 899  function iis7_add_rewrite_rule( $filename, $rewrite_rule ) {
 900      if ( ! class_exists( 'DOMDocument', false ) ) {
 901          return false;
 902      }
 903  
 904      // If configuration file does not exist then we create one.
 905      if ( ! file_exists( $filename ) ) {
 906          $fp = fopen( $filename, 'w' );
 907          fwrite( $fp, '<configuration/>' );
 908          fclose( $fp );
 909      }
 910  
 911      $doc                     = new DOMDocument();
 912      $doc->preserveWhiteSpace = false;
 913  
 914      if ( $doc->load( $filename ) === false ) {
 915          return false;
 916      }
 917  
 918      $xpath = new DOMXPath( $doc );
 919  
 920      // First check if the rule already exists as in that case there is no need to re-add it.
 921      $wordpress_rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
 922  
 923      if ( $wordpress_rules->length > 0 ) {
 924          return true;
 925      }
 926  
 927      // Check the XPath to the rewrite rule and create XML nodes if they do not exist.
 928      $xml_nodes = $xpath->query( '/configuration/system.webServer/rewrite/rules' );
 929  
 930      if ( $xml_nodes->length > 0 ) {
 931          $rules_node = $xml_nodes->item( 0 );
 932      } else {
 933          $rules_node = $doc->createElement( 'rules' );
 934  
 935          $xml_nodes = $xpath->query( '/configuration/system.webServer/rewrite' );
 936  
 937          if ( $xml_nodes->length > 0 ) {
 938              $rewrite_node = $xml_nodes->item( 0 );
 939              $rewrite_node->appendChild( $rules_node );
 940          } else {
 941              $rewrite_node = $doc->createElement( 'rewrite' );
 942              $rewrite_node->appendChild( $rules_node );
 943  
 944              $xml_nodes = $xpath->query( '/configuration/system.webServer' );
 945  
 946              if ( $xml_nodes->length > 0 ) {
 947                  $system_web_server_node = $xml_nodes->item( 0 );
 948                  $system_web_server_node->appendChild( $rewrite_node );
 949              } else {
 950                  $system_web_server_node = $doc->createElement( 'system.webServer' );
 951                  $system_web_server_node->appendChild( $rewrite_node );
 952  
 953                  $xml_nodes = $xpath->query( '/configuration' );
 954  
 955                  if ( $xml_nodes->length > 0 ) {
 956                      $config_node = $xml_nodes->item( 0 );
 957                      $config_node->appendChild( $system_web_server_node );
 958                  } else {
 959                      $config_node = $doc->createElement( 'configuration' );
 960                      $doc->appendChild( $config_node );
 961                      $config_node->appendChild( $system_web_server_node );
 962                  }
 963              }
 964          }
 965      }
 966  
 967      $rule_fragment = $doc->createDocumentFragment();
 968      $rule_fragment->appendXML( $rewrite_rule );
 969      $rules_node->appendChild( $rule_fragment );
 970  
 971      $doc->encoding     = 'UTF-8';
 972      $doc->formatOutput = true;
 973      saveDomDocument( $doc, $filename );
 974  
 975      return true;
 976  }
 977  
 978  /**
 979   * Saves the XML document into a file.
 980   *
 981   * @since 2.8.0
 982   *
 983   * @param DOMDocument $doc      The DOMDocument object to save.
 984   * @param string      $filename The file path to save the XML document to.
 985   */
 986  function saveDomDocument( $doc, $filename ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
 987      $config = $doc->saveXML();
 988      $config = preg_replace( "/([^\r])\n/", "$1\r\n", $config );
 989  
 990      $fp = fopen( $filename, 'w' );
 991      fwrite( $fp, $config );
 992      fclose( $fp );
 993  }
 994  
 995  /**
 996   * Displays the default administration color scheme picker (Used in user-edit.php).
 997   *
 998   * @since 3.0.0
 999   *
1000   * @global array $_wp_admin_css_colors
1001   *
1002   * @param int $user_id User ID.
1003   */
1004  function admin_color_scheme_picker( $user_id ) {
1005      global $_wp_admin_css_colors;
1006  
1007      ksort( $_wp_admin_css_colors );
1008  
1009      if ( isset( $_wp_admin_css_colors['modern'] ) ) {
1010          // Set Modern (new default), Classic ('fresh'), and Light first.
1011          $_wp_admin_css_colors = array_filter(
1012              array_merge(
1013                  array(
1014                      'modern' => '',
1015                      'fresh'  => '',
1016                      'light'  => '',
1017                  ),
1018                  $_wp_admin_css_colors
1019              )
1020          );
1021      }
1022  
1023      $current_color = get_user_option( 'admin_color', $user_id );
1024  
1025      if ( empty( $current_color ) || ! isset( $_wp_admin_css_colors[ $current_color ] ) ) {
1026          $current_color = 'modern';
1027      }
1028      ?>
1029      <fieldset id="color-picker" class="scheme-list">
1030          <legend class="screen-reader-text"><span><?php _e( 'Administration Color Scheme' ); ?></span></legend>
1031          <?php
1032          wp_nonce_field( 'save-color-scheme', 'color-nonce', false );
1033          foreach ( $_wp_admin_css_colors as $color => $color_info ) :
1034  
1035              ?>
1036              <div class="color-option <?php echo ( $color === $current_color ) ? 'selected' : ''; ?>">
1037                  <input name="admin_color" id="admin_color_<?php echo esc_attr( $color ); ?>" type="radio" value="<?php echo esc_attr( $color ); ?>" <?php checked( $color, $current_color ); ?> />
1038                  <input type="hidden" class="css_url" value="<?php echo esc_url( $color_info->url ); ?>" />
1039                  <input type="hidden" class="icon_colors" value="<?php echo esc_attr( wp_json_encode( array( 'icons' => $color_info->icon_colors ) ) ); ?>" />
1040                  <input type="hidden" class="menu_icon_styles" value="<?php echo esc_attr( _wp_get_color_scheme_menu_icon_styles( $color ) ); ?>" />
1041                  <label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
1042                  <div class="color-palette">
1043                  <?php
1044                  foreach ( $color_info->colors as $html_color ) {
1045                      ?>
1046                      <div class="color-palette-shade" style="background-color: <?php echo esc_attr( $html_color ); ?>">&nbsp;</div>
1047                      <?php
1048                  }
1049                  ?>
1050                  </div>
1051              </div>
1052              <?php
1053  
1054          endforeach;
1055          ?>
1056      </fieldset>
1057      <?php
1058  }
1059  
1060  /**
1061   * Outputs the JavaScript for the admin color scheme settings.
1062   *
1063   * @since 3.8.0
1064   *
1065   * @global array $_wp_admin_css_colors
1066   */
1067  function wp_color_scheme_settings() {
1068      global $_wp_admin_css_colors;
1069  
1070      $color_scheme = get_user_option( 'admin_color' );
1071  
1072      // It's possible to have a color scheme set that is no longer registered.
1073      if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
1074          $color_scheme = 'modern';
1075      }
1076  
1077      if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
1078          $icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
1079      } elseif ( ! empty( $_wp_admin_css_colors['modern']->icon_colors ) ) {
1080          $icon_colors = $_wp_admin_css_colors['modern']->icon_colors;
1081      } else {
1082          // Fall back to the default set of icon colors if the default scheme is missing.
1083          $icon_colors = array(
1084              'base'    => '#a7aaad',
1085              'focus'   => '#72aee6',
1086              'current' => '#fff',
1087          );
1088      }
1089  
1090      echo '<script>var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ";</script>\n";
1091  }
1092  
1093  /**
1094   * Colors inline SVG admin menu icons with the icon colors of the current admin color scheme.
1095   *
1096   * These are the colors a scheme registers through the `$icons` argument of
1097   * wp_admin_css_color(). svg-painter.js applies the same colors to menu icons
1098   * given as base64-encoded SVG; this does it for inline SVG icons, with CSS.
1099   *
1100   * The rules are printed before the stylesheet of the scheme, so a scheme
1101   * that colors the icon element itself overrides them.
1102   *
1103   * @since 7.2.0
1104   *
1105   * @see wp_color_scheme_settings()
1106   */
1107  function wp_color_scheme_menu_icon_styles() {
1108      $styles = _wp_get_color_scheme_menu_icon_styles( get_user_option( 'admin_color' ) );
1109  
1110      if ( '' !== $styles ) {
1111          wp_add_inline_style( 'admin-menu', $styles );
1112      }
1113  }
1114  
1115  /**
1116   * Returns the CSS that colors inline SVG admin menu icons with the icon colors of an admin color scheme.
1117   *
1118   * A scheme without icon colors gets CSS that makes the icons follow the
1119   * text color of the menu links instead.
1120   *
1121   * The color scheme picker also passes it to user-profile.js, which applies
1122   * it when another scheme is selected.
1123   *
1124   * @since 7.2.0
1125   * @access private
1126   *
1127   * @global array $_wp_admin_css_colors
1128   *
1129   * @param string $color_scheme The name of the color scheme.
1130   * @return string The CSS, or an empty string if the scheme needs none.
1131   */
1132  function _wp_get_color_scheme_menu_icon_styles( $color_scheme ) {
1133      global $_wp_admin_css_colors;
1134  
1135      if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
1136          return '';
1137      }
1138  
1139      // The core schemes have no stylesheet of their own, or one from the core colors directory.
1140      $url = $_wp_admin_css_colors[ $color_scheme ]->url;
1141      if ( ! is_string( $url ) || '' === $url || str_starts_with( $url, admin_url( 'css/colors/' ) ) ) {
1142          return '';
1143      }
1144  
1145      $base_selector    = '#adminmenu div.wp-menu-image';
1146      $focus_selector   = '#adminmenu li:hover div.wp-menu-image,#adminmenu li a:focus div.wp-menu-image,#adminmenu li.opensub div.wp-menu-image';
1147      $current_selector = '#adminmenu li.wp-has-current-submenu div.wp-menu-image,#adminmenu li.wp-has-current-submenu:hover div.wp-menu-image,#adminmenu a.wp-has-current-submenu:hover div.wp-menu-image,#adminmenu a.current:hover div.wp-menu-image,#adminmenu li.current div.wp-menu-image,#adminmenu li.wp-has-current-submenu a:focus div.wp-menu-image,#adminmenu li.wp-has-current-submenu.opensub div.wp-menu-image';
1148  
1149      $icon_colors = array_map( 'sanitize_hex_color', wp_parse_args( (array) $_wp_admin_css_colors[ $color_scheme ]->icon_colors, array_fill_keys( array( 'base', 'focus', 'current' ), '' ) ) );
1150  
1151      // Without icon colors, the icons follow the text color that the scheme gives to the menu links.
1152      if ( ! $icon_colors['base'] || ! $icon_colors['focus'] || ! $icon_colors['current'] ) {
1153          return "$base_selector,$focus_selector,$current_selector{color:inherit}";
1154      }
1155  
1156      return "$base_selector{color:{$icon_colors['base']}}" .
1157          "$focus_selector{color:{$icon_colors['focus']}}" .
1158          "$current_selector{color:{$icon_colors['current']}}";
1159  }
1160  
1161  /**
1162   * Displays the viewport meta in the admin.
1163   *
1164   * @since 5.5.0
1165   */
1166  function wp_admin_viewport_meta() {
1167      /**
1168       * Filters the viewport meta in the admin.
1169       *
1170       * @since 5.5.0
1171       *
1172       * @param string $viewport_meta The viewport meta.
1173       */
1174      $viewport_meta = apply_filters( 'admin_viewport_meta', 'width=device-width,initial-scale=1.0' );
1175  
1176      if ( empty( $viewport_meta ) ) {
1177          return;
1178      }
1179  
1180      echo '<meta name="viewport" content="' . esc_attr( $viewport_meta ) . '">';
1181  }
1182  
1183  /**
1184   * Adds viewport meta for mobile in Customizer.
1185   *
1186   * Hooked to the {@see 'admin_viewport_meta'} filter.
1187   *
1188   * @since 5.5.0
1189   *
1190   * @param string $viewport_meta The viewport meta.
1191   * @return string Filtered viewport meta.
1192   */
1193  function _customizer_mobile_viewport_meta( $viewport_meta ) {
1194      return trim( $viewport_meta, ',' ) . ',minimum-scale=0.5,maximum-scale=1.2';
1195  }
1196  
1197  /**
1198   * Checks lock status for posts displayed on the Posts screen.
1199   *
1200   * @since 3.6.0
1201   *
1202   * @param array  $response  The Heartbeat response.
1203   * @param array  $data      The $_POST data sent.
1204   * @param string $screen_id The screen ID.
1205   * @return array The Heartbeat response.
1206   */
1207  function wp_check_locked_posts( $response, $data, $screen_id ) {
1208      $checked = array();
1209  
1210      if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
1211          foreach ( $data['wp-check-locked-posts'] as $key ) {
1212              $post_id = absint( substr( $key, 5 ) );
1213  
1214              if ( ! $post_id ) {
1215                  continue;
1216              }
1217  
1218              $user_id = wp_check_post_lock( $post_id );
1219  
1220              if ( $user_id ) {
1221                  $user = get_userdata( $user_id );
1222  
1223                  if ( $user && current_user_can( 'edit_post', $post_id ) ) {
1224                      $send = array(
1225                          'name' => $user->display_name,
1226                          /* translators: %s: User's display name. */
1227                          'text' => sprintf( __( '%s is currently editing' ), $user->display_name ),
1228                      );
1229  
1230                      if ( get_option( 'show_avatars' ) ) {
1231                          $send['avatar_src']    = get_avatar_url( $user->ID, array( 'size' => 18 ) );
1232                          $send['avatar_src_2x'] = get_avatar_url( $user->ID, array( 'size' => 36 ) );
1233                      }
1234  
1235                      $checked[ $key ] = $send;
1236                  }
1237              }
1238          }
1239      }
1240  
1241      if ( ! empty( $checked ) ) {
1242          $response['wp-check-locked-posts'] = $checked;
1243      }
1244  
1245      return $response;
1246  }
1247  
1248  /**
1249   * Checks lock status on the New/Edit Post screen and refresh the lock.
1250   *
1251   * @since 3.6.0
1252   *
1253   * @param array  $response  The Heartbeat response.
1254   * @param array  $data      The $_POST data sent.
1255   * @param string $screen_id The screen ID.
1256   * @return array The Heartbeat response.
1257   */
1258  function wp_refresh_post_lock( $response, $data, $screen_id ) {
1259      if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
1260          $received = $data['wp-refresh-post-lock'];
1261          $send     = array();
1262  
1263          $post_id = absint( $received['post_id'] );
1264  
1265          if ( ! $post_id ) {
1266              return $response;
1267          }
1268  
1269          if ( ! current_user_can( 'edit_post', $post_id ) ) {
1270              return $response;
1271          }
1272  
1273          $user_id = wp_check_post_lock( $post_id );
1274          $user    = get_userdata( $user_id );
1275  
1276          if ( $user ) {
1277              $error = array(
1278                  'name' => $user->display_name,
1279                  /* translators: %s: User's display name. */
1280                  'text' => sprintf( __( '%s has taken over and is currently editing.' ), $user->display_name ),
1281              );
1282  
1283              if ( get_option( 'show_avatars' ) ) {
1284                  $error['avatar_src']    = get_avatar_url( $user->ID, array( 'size' => 64 ) );
1285                  $error['avatar_src_2x'] = get_avatar_url( $user->ID, array( 'size' => 128 ) );
1286              }
1287  
1288              $send['lock_error'] = $error;
1289          } else {
1290              $new_lock = wp_set_post_lock( $post_id );
1291  
1292              if ( $new_lock ) {
1293                  $send['new_lock'] = implode( ':', $new_lock );
1294              }
1295          }
1296  
1297          $response['wp-refresh-post-lock'] = $send;
1298      }
1299  
1300      return $response;
1301  }
1302  
1303  /**
1304   * Checks nonce expiration on the New/Edit Post screen and refresh if needed.
1305   *
1306   * @since 3.6.0
1307   *
1308   * @param array  $response  The Heartbeat response.
1309   * @param array  $data      The $_POST data sent.
1310   * @param string $screen_id The screen ID.
1311   * @return array The Heartbeat response.
1312   */
1313  function wp_refresh_post_nonces( $response, $data, $screen_id ) {
1314      if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
1315          $received = $data['wp-refresh-post-nonces'];
1316  
1317          $response['wp-refresh-post-nonces'] = array( 'check' => 1 );
1318  
1319          $post_id = absint( $received['post_id'] );
1320  
1321          if ( ! $post_id ) {
1322              return $response;
1323          }
1324  
1325          if ( ! current_user_can( 'edit_post', $post_id ) ) {
1326              return $response;
1327          }
1328  
1329          $response['wp-refresh-post-nonces'] = array(
1330              'replace' => array(
1331                  'getpermalinknonce'    => wp_create_nonce( 'getpermalink' ),
1332                  'samplepermalinknonce' => wp_create_nonce( 'samplepermalink' ),
1333                  'closedpostboxesnonce' => wp_create_nonce( 'closedpostboxes' ),
1334                  '_ajax_linking_nonce'  => wp_create_nonce( 'internal-linking' ),
1335                  '_wpnonce'             => wp_create_nonce( 'update-post_' . $post_id ),
1336              ),
1337          );
1338      }
1339  
1340      return $response;
1341  }
1342  
1343  /**
1344   * Refresh nonces used with meta boxes in the block editor.
1345   *
1346   * @since 6.1.0
1347   *
1348   * @param array  $response  The Heartbeat response.
1349   * @param array  $data      The $_POST data sent.
1350   * @return array The Heartbeat response.
1351   */
1352  function wp_refresh_metabox_loader_nonces( $response, $data ) {
1353      if ( empty( $data['wp-refresh-metabox-loader-nonces'] ) ) {
1354          return $response;
1355      }
1356  
1357      $received = $data['wp-refresh-metabox-loader-nonces'];
1358      $post_id  = (int) $received['post_id'];
1359  
1360      if ( ! $post_id ) {
1361          return $response;
1362      }
1363  
1364      if ( ! current_user_can( 'edit_post', $post_id ) ) {
1365          return $response;
1366      }
1367  
1368      $response['wp-refresh-metabox-loader-nonces'] = array(
1369          'replace' => array(
1370              'metabox_loader_nonce' => wp_create_nonce( 'meta-box-loader' ),
1371              '_wpnonce'             => wp_create_nonce( 'update-post_' . $post_id ),
1372          ),
1373      );
1374  
1375      return $response;
1376  }
1377  
1378  /**
1379   * Adds the latest Heartbeat and REST API nonce to the Heartbeat response.
1380   *
1381   * @since 5.0.0
1382   *
1383   * @param array $response The Heartbeat response.
1384   * @return array The Heartbeat response.
1385   */
1386  function wp_refresh_heartbeat_nonces( $response ) {
1387      // Refresh the Rest API nonce.
1388      $response['rest_nonce'] = wp_create_nonce( 'wp_rest' );
1389  
1390      // Refresh the Heartbeat nonce.
1391      $response['heartbeat_nonce'] = wp_create_nonce( 'heartbeat-nonce' );
1392  
1393      return $response;
1394  }
1395  
1396  /**
1397   * Disables suspension of Heartbeat on the Add/Edit Post screens.
1398   *
1399   * @since 3.8.0
1400   *
1401   * @global string $pagenow The filename of the current screen.
1402   *
1403   * @param array $settings An array of Heartbeat settings.
1404   * @return array Filtered Heartbeat settings.
1405   */
1406  function wp_heartbeat_set_suspension( $settings ) {
1407      global $pagenow;
1408  
1409      if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
1410          $settings['suspension'] = 'disable';
1411      }
1412  
1413      return $settings;
1414  }
1415  
1416  /**
1417   * Performs autosave with heartbeat.
1418   *
1419   * @since 3.9.0
1420   *
1421   * @param array $response The Heartbeat response.
1422   * @param array $data     The $_POST data sent.
1423   * @return array The Heartbeat response.
1424   */
1425  function heartbeat_autosave( $response, $data ) {
1426      if ( ! empty( $data['wp_autosave'] ) ) {
1427          $saved = wp_autosave( $data['wp_autosave'] );
1428  
1429          if ( is_wp_error( $saved ) ) {
1430              $response['wp_autosave'] = array(
1431                  'success' => false,
1432                  'message' => $saved->get_error_message(),
1433              );
1434          } elseif ( empty( $saved ) ) {
1435              $response['wp_autosave'] = array(
1436                  'success' => false,
1437                  'message' => __( 'Error while saving.' ),
1438              );
1439          } else {
1440              /* translators: Draft saved date format, see https://www.php.net/manual/datetime.format.php */
1441              $draft_saved_date_format = __( 'g:i:s a' );
1442              $response['wp_autosave'] = array(
1443                  'success' => true,
1444                  /* translators: %s: Date and time. */
1445                  'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ),
1446              );
1447          }
1448      }
1449  
1450      return $response;
1451  }
1452  
1453  /**
1454   * Removes single-use URL parameters and create canonical link based on new URL.
1455   *
1456   * Removes specific query string parameters from a URL, create the canonical link,
1457   * put it in the admin header, and change the current URL to match.
1458   *
1459   * @since 4.2.0
1460   */
1461  function wp_admin_canonical_url() {
1462      $removable_query_args = wp_removable_query_args();
1463  
1464      if ( empty( $removable_query_args ) ) {
1465          return;
1466      }
1467  
1468      // Ensure we're using an absolute URL.
1469      $current_url  = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
1470      $filtered_url = remove_query_arg( $removable_query_args, $current_url );
1471  
1472      /**
1473       * Filters the admin canonical URL value.
1474       *
1475       * @since 6.5.0
1476       *
1477       * @param string $filtered_url The admin canonical URL value.
1478       */
1479      $filtered_url = apply_filters( 'wp_admin_canonical_url', $filtered_url );
1480      ?>
1481      <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
1482      <script>
1483          if ( window.history.replaceState ) {
1484              window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
1485          }
1486      </script>
1487      <?php
1488  }
1489  
1490  /**
1491   * Outputs JS that reloads the page if the user navigated to it with the Back or Forward button.
1492   *
1493   * Used on the Edit Post and Add New Post screens. Needed to ensure the page is not loaded from browser cache,
1494   * so the post title and editor content are the last saved versions. Ideally this script should run first in the head.
1495   *
1496   * @since 4.6.0
1497   */
1498  function wp_page_reload_on_back_button_js() {
1499      ?>
1500      <script>
1501          if ( typeof performance !== 'undefined' && performance.navigation && performance.navigation.type === 2 ) {
1502              document.location.reload( true );
1503          }
1504      </script>
1505      <?php
1506  }
1507  
1508  /**
1509   * Sends a confirmation request email when a change of site admin email address is attempted.
1510   *
1511   * The new site admin address will not become active until confirmed.
1512   *
1513   * @since 3.0.0
1514   * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific.
1515   *
1516   * @param string $old_value The old site admin email address.
1517   * @param string $value     The proposed new site admin email address.
1518   */
1519  function update_option_new_admin_email( $old_value, $value ) {
1520      if ( get_option( 'admin_email' ) === $value || ! is_email( $value ) ) {
1521          return;
1522      }
1523  
1524      $hash            = md5( $value . time() . wp_rand() );
1525      $new_admin_email = array(
1526          'hash'     => $hash,
1527          'newemail' => $value,
1528      );
1529      update_option( 'adminhash', $new_admin_email, false );
1530  
1531      $switched_locale = switch_to_user_locale( get_current_user_id() );
1532  
1533      /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
1534      $email_text = __(
1535          'Howdy,
1536  
1537  A site administrator (###USERNAME###) recently requested to have the
1538  administration email address changed on this site:
1539  ###SITEURL###
1540  
1541  To confirm this change, please click on the following link:
1542  ###ADMIN_URL###
1543  
1544  You can safely ignore and delete this email if you do not want to
1545  take this action.
1546  
1547  This email has been sent to ###EMAIL###
1548  
1549  Regards,
1550  All at ###SITENAME###
1551  ###SITEURL###'
1552      );
1553  
1554      /**
1555       * Filters the text of the email sent when a change of site admin email address is attempted.
1556       *
1557       * The following strings have a special meaning and will get replaced dynamically:
1558       *
1559       *  - `###USERNAME###`  The current user's username.
1560       *  - `###ADMIN_URL###` The link to click on to confirm the email change.
1561       *  - `###EMAIL###`     The proposed new site admin email address.
1562       *  - `###SITENAME###`  The name of the site.
1563       *  - `###SITEURL###`   The URL to the site.
1564       *
1565       * @since MU (3.0.0)
1566       * @since 4.9.0 This filter is no longer Multisite specific.
1567       *
1568       * @param string $email_text      Text in the email.
1569       * @param array  $new_admin_email {
1570       *     Data relating to the new site admin email address.
1571       *
1572       *     @type string $hash     The secure hash used in the confirmation link URL.
1573       *     @type string $newemail The proposed new site admin email address.
1574       * }
1575       */
1576      $content = apply_filters( 'new_admin_email_content', $email_text, $new_admin_email );
1577  
1578      $current_user = wp_get_current_user();
1579      $content      = str_replace( '###USERNAME###', $current_user->user_login, $content );
1580      $content      = str_replace( '###ADMIN_URL###', esc_url( self_admin_url( 'options.php?adminhash=' . $hash ) ), $content );
1581      $content      = str_replace( '###EMAIL###', $value, $content );
1582      $content      = str_replace( '###SITENAME###', wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $content );
1583      $content      = str_replace( '###SITEURL###', home_url(), $content );
1584  
1585      if ( '' !== get_option( 'blogname' ) ) {
1586          $site_title = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
1587      } else {
1588          $site_title = parse_url( home_url(), PHP_URL_HOST );
1589      }
1590  
1591      $subject = sprintf(
1592          /* translators: New admin email address notification email subject. %s: Site title. */
1593          __( '[%s] New Admin Email Address' ),
1594          $site_title
1595      );
1596  
1597      /**
1598       * Filters the subject of the email sent when a change of site admin email address is attempted.
1599       *
1600       * @since 6.5.0
1601       *
1602       * @param string $subject Subject of the email.
1603       */
1604      $subject = apply_filters( 'new_admin_email_subject', $subject );
1605  
1606      wp_mail( $value, $subject, $content );
1607  
1608      if ( $switched_locale ) {
1609          restore_previous_locale();
1610      }
1611  }
1612  
1613  /**
1614   * Appends '(Draft)' to draft page titles in the privacy page dropdown
1615   * so that unpublished content is obvious.
1616   *
1617   * @since 4.9.8
1618   * @access private
1619   *
1620   * @param string  $title Page title.
1621   * @param WP_Post $page  Page data object.
1622   * @return string Page title.
1623   */
1624  function _wp_privacy_settings_filter_draft_page_titles( $title, $page ) {
1625      if ( 'draft' === $page->post_status && 'privacy' === get_current_screen()->id ) {
1626          /* translators: %s: Page title. */
1627          $title = sprintf( __( '%s (Draft)' ), $title );
1628      }
1629  
1630      return $title;
1631  }
1632  
1633  /**
1634   * Checks if the user needs to update PHP.
1635   *
1636   * @since 5.1.0
1637   * @since 5.1.1 Added the {@see 'wp_is_php_version_acceptable'} filter.
1638   *
1639   * @return array|false {
1640   *     Array of PHP version data. False on failure.
1641   *
1642   *     @type string $recommended_version          The PHP version recommended by WordPress.
1643   *     @type string $minimum_version              The minimum required PHP version.
1644   *     @type bool   $is_supported                 Whether the PHP version is actively supported.
1645   *     @type bool   $is_secure                    Whether the PHP version receives security updates.
1646   *     @type bool   $is_acceptable                Whether the PHP version is still acceptable or warnings
1647   *                                                should be shown and an update recommended.
1648   *     @type bool   $is_lower_than_future_minimum Whether the PHP version is lower than the minimum PHP
1649   *                                                version WordPress will require in a future release.
1650   * }
1651   */
1652  function wp_check_php_version() {
1653      $version = PHP_VERSION;
1654      $key     = md5( $version );
1655  
1656      $response = get_site_transient( 'php_check_' . $key );
1657  
1658      if ( false === $response ) {
1659          $url = 'http://api.wordpress.org/core/serve-happy/1.0/';
1660  
1661          if ( wp_http_supports( array( 'ssl' ) ) ) {
1662              $url = set_url_scheme( $url, 'https' );
1663          }
1664  
1665          $url = add_query_arg( 'php_version', $version, $url );
1666  
1667          $response = wp_remote_get( $url );
1668  
1669          if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
1670              return false;
1671          }
1672  
1673          $response = json_decode( wp_remote_retrieve_body( $response ), true );
1674  
1675          if ( ! is_array( $response ) ) {
1676              return false;
1677          }
1678  
1679          set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
1680      }
1681  
1682      if ( isset( $response['is_acceptable'] ) && $response['is_acceptable'] ) {
1683          /**
1684           * Filters whether the active PHP version is considered acceptable by WordPress.
1685           *
1686           * Returning false will trigger a PHP version warning to show up in the admin dashboard to administrators.
1687           *
1688           * This filter is only run if the wordpress.org Serve Happy API considers the PHP version acceptable, ensuring
1689           * that this filter can only make this check stricter, but not loosen it.
1690           *
1691           * @since 5.1.1
1692           *
1693           * @param bool   $is_acceptable Whether the PHP version is considered acceptable. Default true.
1694           * @param string $version       PHP version checked.
1695           */
1696          $response['is_acceptable'] = (bool) apply_filters( 'wp_is_php_version_acceptable', true, $version );
1697      }
1698  
1699      $response['is_lower_than_future_minimum'] = false;
1700  
1701      // The minimum supported PHP version will be updated to at least 8.0 in the future. Check if the current version is lower.
1702      if ( version_compare( $version, '8.0', '<' ) ) {
1703          $response['is_lower_than_future_minimum'] = true;
1704  
1705          // Force showing of warnings.
1706          $response['is_acceptable'] = false;
1707      }
1708  
1709      return $response;
1710  }


Generated : Tue Oct 20 08:20:34 2026 Cross-referenced by PHPXref