PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/misc.php

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