WordPress PHP Cross Reference Web Logs

Source: /wp-admin/includes/misc.php - 563 lines - 15591 bytes - Summary - Text - Print

Description: Misc WordPress Administration API.

   1  <?php
   2  /**
   3   * Misc WordPress Administration API.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * {@internal Missing Short Description}}
  11   *
  12   * @since 2.0.0
  13   *
  14   * @return unknown
  15   */
  16  function got_mod_rewrite() {
  17      $got_rewrite = apache_mod_loaded('mod_rewrite', true);
  18      return apply_filters('got_rewrite', $got_rewrite);
  19  }
  20  
  21  /**
  22   * {@internal Missing Short Description}}
  23   *
  24   * @since 1.5.0
  25   *
  26   * @param unknown_type $filename
  27   * @param unknown_type $marker
  28   * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
  29   */
  30  function extract_from_markers( $filename, $marker ) {
  31      $result = array ();
  32  
  33      if (!file_exists( $filename ) ) {
  34          return $result;
  35      }
  36  
  37      if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
  38      {
  39          $state = false;
  40          foreach ( $markerdata as $markerline ) {
  41              if (strpos($markerline, '# END ' . $marker) !== false)
  42                  $state = false;
  43              if ( $state )
  44                  $result[] = $markerline;
  45              if (strpos($markerline, '# BEGIN ' . $marker) !== false)
  46                  $state = true;
  47          }
  48      }
  49  
  50      return $result;
  51  }
  52  
  53  /**
  54   * {@internal Missing Short Description}}
  55   *
  56   * Inserts an array of strings into a file (.htaccess ), placing it between
  57   * BEGIN and END markers. Replaces existing marked info. Retains surrounding
  58   * data. Creates file if none exists.
  59   *
  60   * @since 1.5.0
  61   *
  62   * @param unknown_type $filename
  63   * @param unknown_type $marker
  64   * @param unknown_type $insertion
  65   * @return bool True on write success, false on failure.
  66   */
  67  function insert_with_markers( $filename, $marker, $insertion ) {
  68      if (!file_exists( $filename ) || is_writeable( $filename ) ) {
  69          if (!file_exists( $filename ) ) {
  70              $markerdata = '';
  71          } else {
  72              $markerdata = explode( "\n", implode( '', file( $filename ) ) );
  73          }
  74  
  75          if ( !$f = @fopen( $filename, 'w' ) )
  76              return false;
  77  
  78          $foundit = false;
  79          if ( $markerdata ) {
  80              $state = true;
  81              foreach ( $markerdata as $n => $markerline ) {
  82                  if (strpos($markerline, '# BEGIN ' . $marker) !== false)
  83                      $state = false;
  84                  if ( $state ) {
  85                      if ( $n + 1 < count( $markerdata ) )
  86                          fwrite( $f, "{$markerline}\n" );
  87                      else
  88                          fwrite( $f, "{$markerline}" );
  89                  }
  90                  if (strpos($markerline, '# END ' . $marker) !== false) {
  91                      fwrite( $f, "# BEGIN {$marker}\n" );
  92                      if ( is_array( $insertion ))
  93                          foreach ( $insertion as $insertline )
  94                              fwrite( $f, "{$insertline}\n" );
  95                      fwrite( $f, "# END {$marker}\n" );
  96                      $state = true;
  97                      $foundit = true;
  98                  }
  99              }
 100          }
 101          if (!$foundit) {
 102              fwrite( $f, "\n# BEGIN {$marker}\n" );
 103              foreach ( $insertion as $insertline )
 104                  fwrite( $f, "{$insertline}\n" );
 105              fwrite( $f, "# END {$marker}\n" );
 106          }
 107          fclose( $f );
 108          return true;
 109      } else {
 110          return false;
 111      }
 112  }
 113  
 114  /**
 115   * Updates the htaccess file with the current rules if it is writable.
 116   *
 117   * Always writes to the file if it exists and is writable to ensure that we
 118   * blank out old rules.
 119   *
 120   * @since 1.5.0
 121   */
 122  function save_mod_rewrite_rules() {
 123      if ( is_multisite() )
 124          return;
 125  
 126      global $wp_rewrite;
 127  
 128      $home_path = get_home_path();
 129      $htaccess_file = $home_path.'.htaccess';
 130  
 131      // If the file doesn't already exist check for write access to the directory and whether we have some rules.
 132      // else check for write access to the file.
 133      if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
 134          if ( got_mod_rewrite() ) {
 135              $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
 136              return insert_with_markers( $htaccess_file, 'WordPress', $rules );
 137          }
 138      }
 139  
 140      return false;
 141  }
 142  
 143  /**
 144   * Updates the IIS web.config file with the current rules if it is writable.
 145   * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
 146   *
 147   * @since 2.8.0
 148   *
 149   * @return bool True if web.config was updated successfully
 150   */
 151  function iis7_save_url_rewrite_rules(){
 152      if ( is_multisite() )
 153          return;
 154  
 155      global $wp_rewrite;
 156  
 157      $home_path = get_home_path();
 158      $web_config_file = $home_path . 'web.config';
 159  
 160      // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
 161      if ( iis7_supports_permalinks() && ( ( ! file_exists($web_config_file) && win_is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable($web_config_file) ) ) {
 162          $rule = $wp_rewrite->iis7_url_rewrite_rules(false, '', '');
 163          if ( ! empty($rule) ) {
 164              return iis7_add_rewrite_rule($web_config_file, $rule);
 165          } else {
 166              return iis7_delete_rewrite_rule($web_config_file);
 167          }
 168      }
 169      return false;
 170  }
 171  
 172  /**
 173   * {@internal Missing Short Description}}
 174   *
 175   * @since 1.5.0
 176   *
 177   * @param unknown_type $file
 178   */
 179  function update_recently_edited( $file ) {
 180      $oldfiles = (array ) get_option( 'recently_edited' );
 181      if ( $oldfiles ) {
 182          $oldfiles = array_reverse( $oldfiles );
 183          $oldfiles[] = $file;
 184          $oldfiles = array_reverse( $oldfiles );
 185          $oldfiles = array_unique( $oldfiles );
 186          if ( 5 < count( $oldfiles ))
 187              array_pop( $oldfiles );
 188      } else {
 189          $oldfiles[] = $file;
 190      }
 191      update_option( 'recently_edited', $oldfiles );
 192  }
 193  
 194  /**
 195   * If siteurl, home or page_on_front changed, flush rewrite rules.
 196   *
 197   * @since 2.1.0
 198   *
 199   * @param string $old_value
 200   * @param string $value
 201   */
 202  function update_home_siteurl( $old_value, $value ) {
 203      if ( defined( "WP_INSTALLING" ) )
 204          return;
 205  
 206      // If home changed, write rewrite rules to new location.
 207      flush_rewrite_rules();
 208  }
 209  
 210  add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
 211  add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
 212  add_action( 'update_option_page_on_front', 'update_home_siteurl', 10, 2 );
 213  
 214  /**
 215   * Shorten an URL, to be used as link text
 216   *
 217   * @since 1.2.1
 218   *
 219   * @param string $url
 220   * @return string
 221   */
 222  function url_shorten( $url ) {
 223      $short_url = str_replace( 'http://', '', stripslashes( $url ));
 224      $short_url = str_replace( 'www.', '', $short_url );
 225      $short_url = untrailingslashit( $short_url );
 226      if ( strlen( $short_url ) > 35 )
 227          $short_url = substr( $short_url, 0, 32 ) . '...';
 228      return $short_url;
 229  }
 230  
 231  /**
 232   * Resets global variables based on $_GET and $_POST
 233   *
 234   * This function resets global variables based on the names passed
 235   * in the $vars array to the value of $_POST[$var] or $_GET[$var] or ''
 236   * if neither is defined.
 237   *
 238   * @since 2.0.0
 239   *
 240   * @param array $vars An array of globals to reset.
 241   */
 242  function wp_reset_vars( $vars ) {
 243      for ( $i=0; $i<count( $vars ); $i += 1 ) {
 244          $var = $vars[$i];
 245          global $$var;
 246  
 247          if ( empty( $_POST[$var] ) ) {
 248              if ( empty( $_GET[$var] ) )
 249                  $$var = '';
 250              else
 251                  $$var = $_GET[$var];
 252          } else {
 253              $$var = $_POST[$var];
 254          }
 255      }
 256  }
 257  
 258  /**
 259   * {@internal Missing Short Description}}
 260   *
 261   * @since 2.1.0
 262   *
 263   * @param unknown_type $message
 264   */
 265  function show_message($message) {
 266      if ( is_wp_error($message) ){
 267          if ( $message->get_error_data() )
 268              $message = $message->get_error_message() . ': ' . $message->get_error_data();
 269          else
 270              $message = $message->get_error_message();
 271      }
 272      echo "<p>$message</p>\n";
 273      wp_ob_end_flush_all();
 274      flush();
 275  }
 276  
 277  function wp_doc_link_parse( $content ) {
 278      if ( !is_string( $content ) || empty( $content ) )
 279          return array();
 280  
 281      if ( !function_exists('token_get_all') )
 282          return array();
 283  
 284      $tokens = token_get_all( $content );
 285      $functions = array();
 286      $ignore_functions = array();
 287      for ( $t = 0, $count = count( $tokens ); $t < $count; $t++ ) {
 288          if ( !is_array( $tokens[$t] ) ) continue;
 289          if ( T_STRING == $tokens[$t][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
 290              // If it's a function or class defined locally, there's not going to be any docs available
 291              if ( ( isset( $tokens[ $t - 2 ][1] ) && in_array( $tokens[ $t - 2 ][1], array( 'function', 'class' ) ) ) || ( isset( $tokens[ $t - 2 ][0] ) && T_OBJECT_OPERATOR == $tokens[ $t - 1 ][0] ) ) {
 292                  $ignore_functions[] = $tokens[$t][1];
 293              }
 294              // Add this to our stack of unique references
 295              $functions[] = $tokens[$t][1];
 296          }
 297      }
 298  
 299      $functions = array_unique( $functions );
 300      sort( $functions );
 301      $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
 302      $ignore_functions = array_unique( $ignore_functions );
 303  
 304      $out = array();
 305      foreach ( $functions as $function ) {
 306          if ( in_array( $function, $ignore_functions ) )
 307              continue;
 308          $out[] = $function;
 309      }
 310  
 311      return $out;
 312  }
 313  
 314  /**
 315   * Saves option for number of rows when listing posts, pages, comments, etc.
 316   *
 317   * @since 2.8
 318  **/
 319  function set_screen_options() {
 320  
 321      if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
 322          check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
 323  
 324          if ( !$user = wp_get_current_user() )
 325              return;
 326          $option = $_POST['wp_screen_options']['option'];
 327          $value = $_POST['wp_screen_options']['value'];
 328  
 329          if ( $option != sanitize_key( $option ) )
 330              return;
 331  
 332          $map_option = $option;
 333          $type = str_replace('edit_', '', $map_option);
 334          $type = str_replace('_per_page', '', $type);
 335          if ( in_array( $type, get_taxonomies() ) )
 336              $map_option = 'edit_tags_per_page';
 337          elseif ( in_array( $type, get_post_types() ) )
 338              $map_option = 'edit_per_page';
 339          else
 340              $option = str_replace('-', '_', $option);
 341  
 342          switch ( $map_option ) {
 343              case 'edit_per_page':
 344              case 'users_per_page':
 345              case 'edit_comments_per_page':
 346              case 'upload_per_page':
 347              case 'edit_tags_per_page':
 348              case 'plugins_per_page':
 349              // Network admin
 350              case 'sites_network_per_page':
 351              case 'users_network_per_page':
 352              case 'site_users_network_per_page':
 353              case 'plugins_network_per_page':
 354              case 'themes_network_per_page':
 355              case 'site_themes_network_per_page':
 356                  $value = (int) $value;
 357                  if ( $value < 1 || $value > 999 )
 358                      return;
 359                  break;
 360              default:
 361                  $value = apply_filters('set-screen-option', false, $option, $value);
 362                  if ( false === $value )
 363                      return;
 364                  break;
 365          }
 366  
 367          update_user_meta($user->ID, $option, $value);
 368          wp_safe_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) );
 369          exit;
 370      }
 371  }
 372  
 373  /**
 374   * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
 375   *
 376   * @since 2.8.0
 377   *
 378   * @return bool
 379   * @param string $filename The file path to the configuration file
 380   */
 381  function iis7_rewrite_rule_exists($filename) {
 382      if ( ! file_exists($filename) )
 383          return false;
 384      if ( ! class_exists('DOMDocument') )
 385          return false;
 386  
 387      $doc = new DOMDocument();
 388      if ( $doc->load($filename) === false )
 389          return false;
 390      $xpath = new DOMXPath($doc);
 391      $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
 392      if ( $rules->length == 0 )
 393          return false;
 394      else
 395          return true;
 396  }
 397  
 398  /**
 399   * Delete WordPress rewrite rule from web.config file if it exists there
 400   *
 401   * @since 2.8.0
 402   *
 403   * @param string $filename Name of the configuration file
 404   * @return bool
 405   */
 406  function iis7_delete_rewrite_rule($filename) {
 407      // If configuration file does not exist then rules also do not exist so there is nothing to delete
 408      if ( ! file_exists($filename) )
 409          return true;
 410  
 411      if ( ! class_exists('DOMDocument') )
 412          return false;
 413  
 414      $doc = new DOMDocument();
 415      $doc->preserveWhiteSpace = false;
 416  
 417      if ( $doc -> load($filename) === false )
 418          return false;
 419      $xpath = new DOMXPath($doc);
 420      $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
 421      if ( $rules->length > 0 ) {
 422          $child = $rules->item(0);
 423          $parent = $child->parentNode;
 424          $parent->removeChild($child);
 425          $doc->formatOutput = true;
 426          saveDomDocument($doc, $filename);
 427      }
 428      return true;
 429  }
 430  
 431  /**
 432   * Add WordPress rewrite rule to the IIS 7 configuration file.
 433   *
 434   * @since 2.8.0
 435   *
 436   * @param string $filename The file path to the configuration file
 437   * @param string $rewrite_rule The XML fragment with URL Rewrite rule
 438   * @return bool
 439   */
 440  function iis7_add_rewrite_rule($filename, $rewrite_rule) {
 441      if ( ! class_exists('DOMDocument') )
 442          return false;
 443  
 444      // If configuration file does not exist then we create one.
 445      if ( ! file_exists($filename) ) {
 446          $fp = fopen( $filename, 'w');
 447          fwrite($fp, '<configuration/>');
 448          fclose($fp);
 449      }
 450  
 451      $doc = new DOMDocument();
 452      $doc->preserveWhiteSpace = false;
 453  
 454      if ( $doc->load($filename) === false )
 455          return false;
 456  
 457      $xpath = new DOMXPath($doc);
 458  
 459      // First check if the rule already exists as in that case there is no need to re-add it
 460      $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
 461      if ( $wordpress_rules->length > 0 )
 462          return true;
 463  
 464      // Check the XPath to the rewrite rule and create XML nodes if they do not exist
 465      $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
 466      if ( $xmlnodes->length > 0 ) {
 467          $rules_node = $xmlnodes->item(0);
 468      } else {
 469          $rules_node = $doc->createElement('rules');
 470  
 471          $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
 472          if ( $xmlnodes->length > 0 ) {
 473              $rewrite_node = $xmlnodes->item(0);
 474              $rewrite_node->appendChild($rules_node);
 475          } else {
 476              $rewrite_node = $doc->createElement('rewrite');
 477              $rewrite_node->appendChild($rules_node);
 478  
 479              $xmlnodes = $xpath->query('/configuration/system.webServer');
 480              if ( $xmlnodes->length > 0 ) {
 481                  $system_webServer_node = $xmlnodes->item(0);
 482                  $system_webServer_node->appendChild($rewrite_node);
 483              } else {
 484                  $system_webServer_node = $doc->createElement('system.webServer');
 485                  $system_webServer_node->appendChild($rewrite_node);
 486  
 487                  $xmlnodes = $xpath->query('/configuration');
 488                  if ( $xmlnodes->length > 0 ) {
 489                      $config_node = $xmlnodes->item(0);
 490                      $config_node->appendChild($system_webServer_node);
 491                  } else {
 492                      $config_node = $doc->createElement('configuration');
 493                      $doc->appendChild($config_node);
 494                      $config_node->appendChild($system_webServer_node);
 495                  }
 496              }
 497          }
 498      }
 499  
 500      $rule_fragment = $doc->createDocumentFragment();
 501      $rule_fragment->appendXML($rewrite_rule);
 502      $rules_node->appendChild($rule_fragment);
 503  
 504      $doc->encoding = "UTF-8";
 505      $doc->formatOutput = true;
 506      saveDomDocument($doc, $filename);
 507  
 508      return true;
 509  }
 510  
 511  /**
 512   * Saves the XML document into a file
 513   *
 514   * @since 2.8.0
 515   *
 516   * @param DOMDocument $doc
 517   * @param string $filename
 518   */
 519  function saveDomDocument($doc, $filename) {
 520      $config = $doc->saveXML();
 521      $config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
 522      $fp = fopen($filename, 'w');
 523      fwrite($fp, $config);
 524      fclose($fp);
 525  }
 526  
 527  /**
 528   * Display the default admin color scheme picker (Used in user-edit.php)
 529   *
 530   * @since 3.0.0
 531   */
 532  function admin_color_scheme_picker() {
 533      global $_wp_admin_css_colors, $user_id; ?>
 534  <fieldset><legend class="screen-reader-text"><span><?php _e('Admin Color Scheme')?></span></legend>
 535  <?php
 536  $current_color = get_user_option('admin_color', $user_id);
 537  if ( empty($current_color) )
 538      $current_color = 'fresh';
 539  foreach ( $_wp_admin_css_colors as $color => $color_info ): ?>
 540  <div class="color-option"><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); ?> />
 541      <table class="color-palette">
 542      <tr>
 543      <?php foreach ( $color_info->colors as $html_color ): ?>
 544      <td style="background-color: <?php echo esc_attr( $html_color ); ?>" title="<?php echo esc_attr( $color ); ?>">&nbsp;</td>
 545      <?php endforeach; ?>
 546      </tr>
 547      </table>
 548  
 549      <label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
 550  </div>
 551      <?php endforeach; ?>
 552  </fieldset>
 553  <?php
 554  }
 555  
 556  function _ipad_meta() {
 557      if ( wp_is_mobile() ) {
 558          ?>
 559          <meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
 560          <?php
 561      }
 562  }
 563  add_action('admin_head', '_ipad_meta');

title

Description

title

Description

title

Description

title

title

Body