3 * Misc WordPress Administration API.
6 * @subpackage Administration
10 * {@internal Missing Short Description}}
16 function got_mod_rewrite() {
17 $got_rewrite = apache_mod_loaded('mod_rewrite', true);
18 return apply_filters('got_rewrite', $got_rewrite);
22 * {@internal Missing Short Description}}
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.
30 function extract_from_markers( $filename, $marker ) {
33 if (!file_exists( $filename ) ) {
37 if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
40 foreach ( $markerdata as $markerline ) {
41 if (strpos($markerline, '# END ' . $marker) !== false)
44 $result[] = $markerline;
45 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
54 * {@internal Missing Short Description}}
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.
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.
67 function insert_with_markers( $filename, $marker, $insertion ) {
68 if (!file_exists( $filename ) || is_writeable( $filename ) ) {
69 if (!file_exists( $filename ) ) {
72 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
75 if ( !$f = @fopen( $filename, 'w' ) )
81 foreach ( $markerdata as $n => $markerline ) {
82 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
85 if ( $n + 1 < count( $markerdata ) )
86 fwrite( $f, "{$markerline}\n" );
88 fwrite( $f, "{$markerline}" );
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" );
102 fwrite( $f, "\n# BEGIN {$marker}\n" );
103 foreach ( $insertion as $insertline )
104 fwrite( $f, "{$insertline}\n" );
105 fwrite( $f, "# END {$marker}\n" );
115 * Updates the htaccess file with the current rules if it is writable.
117 * Always writes to the file if it exists and is writable to ensure that we
118 * blank out old rules.
122 function save_mod_rewrite_rules() {
125 $home_path = get_home_path();
126 $htaccess_file = $home_path.'.htaccess';
128 // If the file doesn't already exists check for write access to the directory and whether of not we have some rules.
129 // else check for write access to the file.
130 if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
131 if ( got_mod_rewrite() ) {
132 $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
133 return insert_with_markers( $htaccess_file, 'WordPress', $rules );
141 * Updates the IIS web.config file with the current rules if it is writable.
142 * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
146 * @return bool True if web.config was updated successfully
148 function iis7_save_url_rewrite_rules(){
151 $home_path = get_home_path();
152 $web_config_file = $home_path . 'web.config';
154 // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
155 if ( ( ! file_exists($web_config_file) && win_is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable($web_config_file) ) {
156 if ( iis7_supports_permalinks() ) {
157 $rule = $wp_rewrite->iis7_url_rewrite_rules();
158 if ( ! empty($rule) ) {
159 return iis7_add_rewrite_rule($web_config_file, $rule);
161 return iis7_delete_rewrite_rule($web_config_file);
169 * {@internal Missing Short Description}}
173 * @param unknown_type $file
175 function update_recently_edited( $file ) {
176 $oldfiles = (array ) get_option( 'recently_edited' );
178 $oldfiles = array_reverse( $oldfiles );
180 $oldfiles = array_reverse( $oldfiles );
181 $oldfiles = array_unique( $oldfiles );
182 if ( 5 < count( $oldfiles ))
183 array_pop( $oldfiles );
187 update_option( 'recently_edited', $oldfiles );
191 * If siteurl or home changed, flush rewrite rules.
195 * @param unknown_type $old_value
196 * @param unknown_type $value
198 function update_home_siteurl( $old_value, $value ) {
201 if ( defined( "WP_INSTALLING" ) )
204 // If home changed, write rewrite rules to new location.
205 $wp_rewrite->flush_rules();
208 add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
209 add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
212 * {@internal Missing Short Description}}
216 * @param unknown_type $url
219 function url_shorten( $url ) {
220 $short_url = str_replace( 'http://', '', stripslashes( $url ));
221 $short_url = str_replace( 'www.', '', $short_url );
222 if ('/' == substr( $short_url, -1 ))
223 $short_url = substr( $short_url, 0, -1 );
224 if ( strlen( $short_url ) > 35 )
225 $short_url = substr( $short_url, 0, 32 ).'...';
230 * {@internal Missing Short Description}}
234 * @param unknown_type $vars
236 function wp_reset_vars( $vars ) {
237 for ( $i=0; $i<count( $vars ); $i += 1 ) {
241 if (!isset( $$var ) ) {
242 if ( empty( $_POST["$var"] ) ) {
243 if ( empty( $_GET["$var"] ) )
246 $$var = $_GET["$var"];
248 $$var = $_POST["$var"];
255 * {@internal Missing Short Description}}
259 * @param unknown_type $message
261 function show_message($message) {
262 if( is_wp_error($message) ){
263 if( $message->get_error_data() )
264 $message = $message->get_error_message() . ': ' . $message->get_error_data();
266 $message = $message->get_error_message();
268 echo "<p>$message</p>\n";
271 function wp_doc_link_parse( $content ) {
272 if ( !is_string( $content ) || empty( $content ) )
275 if ( !function_exists('token_get_all') )
278 $tokens = token_get_all( $content );
279 $functions = array();
280 $ignore_functions = array();
281 for ( $t = 0, $count = count( $tokens ); $t < $count; $t++ ) {
282 if ( !is_array( $tokens[$t] ) ) continue;
283 if ( T_STRING == $tokens[$t][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
284 // If it's a function or class defined locally, there's not going to be any docs available
285 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] ) ) {
286 $ignore_functions[] = $tokens[$t][1];
288 // Add this to our stack of unique references
289 $functions[] = $tokens[$t][1];
293 $functions = array_unique( $functions );
295 $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
296 $ignore_functions = array_unique( $ignore_functions );
299 foreach ( $functions as $function ) {
300 if ( in_array( $function, $ignore_functions ) )
309 * Determines the language to use for CodePress syntax highlighting,
310 * based only on a filename.
314 * @param string $filename The name of the file to be highlighting
316 function codepress_get_lang( $filename ) {
317 $codepress_supported_langs = apply_filters( 'codepress_supported_langs',
318 array( '.css' => 'css',
319 '.js' => 'javascript',
325 $extension = substr( $filename, strrpos( $filename, '.' ) );
326 if ( $extension && array_key_exists( $extension, $codepress_supported_langs ) )
327 return $codepress_supported_langs[$extension];
333 * Adds Javascript required to make CodePress work on the theme/plugin editors.
335 * This code is attached to the action admin_print_footer_scripts.
339 function codepress_footer_js() {
340 // Script-loader breaks CP's automatic path-detection, thus CodePress.path
341 // CP edits in an iframe, so we need to grab content back into normal form
342 ?><script type="text/javascript">
344 var codepress_path = '<?php echo includes_url('js/codepress/'); ?>';
345 jQuery('#template').submit(function(){
346 if (jQuery('#newcontent_cp').length)
347 jQuery('#newcontent_cp').val(newcontent.getCode()).removeAttr('disabled');
349 jQuery('#codepress-on').hide();
350 jQuery('#codepress-off').show();
357 * Determine whether to use CodePress or not.
361 function use_codepress() {
363 if ( isset($_GET['codepress']) ) {
364 $on = 'on' == $_GET['codepress'] ? 'on' : 'off';
365 set_user_setting( 'codepress', $on );
367 $on = get_user_setting('codepress', 'on');
371 add_action( 'admin_print_footer_scripts', 'codepress_footer_js' );
379 * Saves option for number of rows when listing posts, pages, comments, etc.
383 function set_screen_options() {
385 if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
386 check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
388 if ( !$user = wp_get_current_user() )
390 $option = $_POST['wp_screen_options']['option'];
391 $value = $_POST['wp_screen_options']['value'];
393 if ( !preg_match( '/^[a-z_-]+$/', $option ) )
396 $option = str_replace('-', '_', $option);
399 case 'edit_per_page':
400 case 'edit_pages_per_page':
401 case 'edit_comments_per_page':
402 case 'upload_per_page':
403 case 'categories_per_page':
404 case 'edit_tags_per_page':
405 case 'plugins_per_page':
406 $value = (int) $value;
407 if ( $value < 1 || $value > 999 )
411 $value = apply_filters('set-screen-option', false, $option, $value);
412 if ( false === $value )
417 update_usermeta($user->ID, $option, $value);
418 wp_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) );
423 function wp_menu_unfold() {
424 if ( isset($_GET['unfoldmenu']) ) {
425 delete_user_setting('mfold');
426 wp_redirect( remove_query_arg( 'unfoldmenu', stripslashes($_SERVER['REQUEST_URI']) ) );
432 * Check if IIS 7 supports pretty permalinks
438 function iis7_supports_permalinks() {
441 $supports_permalinks = false;
443 /* First we check if the DOMDocument class exists. If it does not exist,
444 * which is the case for PHP 4.X, then we cannot easily update the xml configuration file,
445 * hence we just bail out and tell user that pretty permalinks cannot be used.
446 * This is not a big issue because PHP 4.X is going to be depricated and for IIS it
447 * is recommended to use PHP 5.X NTS.
448 * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
449 * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
450 * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
451 * via ISAPI then pretty permalinks will not work.
453 $supports_permalinks = class_exists('DOMDocument') && isset($_SERVER['IIS_UrlRewriteModule']) && ( php_sapi_name() == 'cgi-fcgi' );
456 return apply_filters('iis7_supports_permalinks', $supports_permalinks);
460 * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
465 * @param string $filename The file path to the configuration file
467 function iis7_rewrite_rule_exists($filename) {
468 if ( ! file_exists($filename) )
470 if ( ! class_exists('DOMDocument') )
473 $doc = new DOMDocument();
474 if ( $doc->load($filename) === false )
476 $xpath = new DOMXPath($doc);
477 $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
478 if ( $rules->length == 0 )
485 * Delete WordPress rewrite rule from web.config file if it exists there
489 * @param string $filename Name of the configuration file
492 function iis7_delete_rewrite_rule($filename) {
493 // If configuration file does not exist then rules also do not exist so there is nothing to delete
494 if ( ! file_exists($filename) )
497 if ( ! class_exists('DOMDocument') )
500 $doc = new DOMDocument();
501 $doc->preserveWhiteSpace = false;
503 if ( $doc -> load($filename) === false )
505 $xpath = new DOMXPath($doc);
506 $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
507 if ( $rules->length > 0 ) {
508 $child = $rules->item(0);
509 $parent = $child->parentNode;
510 $parent->removeChild($child);
511 $doc->formatOutput = true;
512 saveDomDocument($doc, $filename);
518 * Add WordPress rewrite rule to the IIS 7 configuration file.
522 * @param string $filename The file path to the configuration file
523 * @param string $rewrite_rule The XML fragment with URL Rewrite rule
526 function iis7_add_rewrite_rule($filename, $rewrite_rule) {
527 if ( ! class_exists('DOMDocument') )
530 // If configuration file does not exist then we create one.
531 if ( ! file_exists($filename) ) {
532 $fp = fopen( $filename, 'w');
533 fwrite($fp, '<configuration/>');
537 $doc = new DOMDocument();
538 $doc->preserveWhiteSpace = false;
540 if ( $doc->load($filename) === false )
543 $xpath = new DOMXPath($doc);
545 // First check if the rule already exists as in that case there is no need to re-add it
546 $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
547 if ( $wordpress_rules->length > 0 )
550 // Check the XPath to the rewrite rule and create XML nodes if they do not exist
551 $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
552 if ( $xmlnodes->length > 0 ) {
553 $rules_node = $xmlnodes->item(0);
555 $rules_node = $doc->createElement('rules');
557 $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
558 if ( $xmlnodes->length > 0 ) {
559 $rewrite_node = $xmlnodes->item(0);
560 $rewrite_node->appendChild($rules_node);
562 $rewrite_node = $doc->createElement('rewrite');
563 $rewrite_node->appendChild($rules_node);
565 $xmlnodes = $xpath->query('/configuration/system.webServer');
566 if ( $xmlnodes->length > 0 ) {
567 $system_webServer_node = $xmlnodes->item(0);
568 $system_webServer_node->appendChild($rewrite_node);
570 $system_webServer_node = $doc->createElement('system.webServer');
571 $system_webServer_node->appendChild($rewrite_node);
573 $xmlnodes = $xpath->query('/configuration');
574 if ( $xmlnodes->length > 0 ) {
575 $config_node = $xmlnodes->item(0);
576 $config_node->appendChild($system_webServer_node);
578 $config_node = $doc->createElement('configuration');
579 $doc->appendChild($config_node);
580 $config_node->appendChild($system_webServer_node);
586 $rule_fragment = $doc->createDocumentFragment();
587 $rule_fragment->appendXML($rewrite_rule);
588 $rules_node->appendChild($rule_fragment);
590 $doc->formatOutput = true;
591 saveDomDocument($doc, $filename);
597 * Saves the XML document into a file
601 * @param DOMDocument $doc
602 * @param string $filename
604 function saveDomDocument($doc, $filename) {
605 $config = $doc->saveXML();
606 $config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
607 $fp = fopen($filename, 'w');
608 fwrite($fp, $config);
613 * Workaround for Windows bug in is_writable() function
617 * @param object $path
620 function win_is_writable($path) {
621 /* will work in despite of Windows ACLs bug
622 * NOTE: use a trailing slash for folders!!!
623 * see http://bugs.php.net/bug.php?id=27609
624 * see http://bugs.php.net/bug.php?id=30931
627 if ( $path{strlen($path)-1} == '/' ) // recursively return a temporary file path
628 return win_is_writable($path . uniqid(mt_rand()) . '.tmp');
629 else if ( is_dir($path) )
630 return win_is_writable($path . '/' . uniqid(mt_rand()) . '.tmp');
631 // check tmp file for read/write capabilities
632 $rm = file_exists($path);
633 $f = @fopen($path, 'a');