+/**
+ * Add WordPress rewrite rule to the IIS 7 configuration file.
+ *
+ * @since 2.8.0
+ *
+ * @param string $filename The file path to the configuration file
+ * @param string $rewrite_rule The XML fragment with URL Rewrite rule
+ * @return bool
+ */
+function iis7_add_rewrite_rule($filename, $rewrite_rule) {
+ if ( ! class_exists('DOMDocument') )
+ return false;
+
+ // If configuration file does not exist then we create one.
+ if ( ! file_exists($filename) ) {
+ $fp = fopen( $filename, 'w');
+ fwrite($fp, '<configuration/>');
+ fclose($fp);
+ }
+
+ $doc = new DOMDocument();
+ $doc->preserveWhiteSpace = false;
+
+ if ( $doc->load($filename) === false )
+ return false;
+
+ $xpath = new DOMXPath($doc);
+
+ // First check if the rule already exists as in that case there is no need to re-add it
+ $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
+ if ( $wordpress_rules->length > 0 )
+ return true;
+
+ // Check the XPath to the rewrite rule and create XML nodes if they do not exist
+ $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
+ if ( $xmlnodes->length > 0 ) {
+ $rules_node = $xmlnodes->item(0);
+ } else {
+ $rules_node = $doc->createElement('rules');
+
+ $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
+ if ( $xmlnodes->length > 0 ) {
+ $rewrite_node = $xmlnodes->item(0);
+ $rewrite_node->appendChild($rules_node);
+ } else {
+ $rewrite_node = $doc->createElement('rewrite');
+ $rewrite_node->appendChild($rules_node);
+
+ $xmlnodes = $xpath->query('/configuration/system.webServer');
+ if ( $xmlnodes->length > 0 ) {
+ $system_webServer_node = $xmlnodes->item(0);
+ $system_webServer_node->appendChild($rewrite_node);
+ } else {
+ $system_webServer_node = $doc->createElement('system.webServer');
+ $system_webServer_node->appendChild($rewrite_node);
+
+ $xmlnodes = $xpath->query('/configuration');
+ if ( $xmlnodes->length > 0 ) {
+ $config_node = $xmlnodes->item(0);
+ $config_node->appendChild($system_webServer_node);
+ } else {
+ $config_node = $doc->createElement('configuration');
+ $doc->appendChild($config_node);
+ $config_node->appendChild($system_webServer_node);
+ }
+ }
+ }
+ }
+
+ $rule_fragment = $doc->createDocumentFragment();
+ $rule_fragment->appendXML($rewrite_rule);
+ $rules_node->appendChild($rule_fragment);
+
+ $doc->encoding = "UTF-8";
+ $doc->formatOutput = true;
+ saveDomDocument($doc, $filename);
+
+ return true;
+}
+
+/**
+ * Saves the XML document into a file
+ *
+ * @since 2.8.0
+ *
+ * @param DOMDocument $doc
+ * @param string $filename
+ */
+function saveDomDocument($doc, $filename) {
+ $config = $doc->saveXML();
+ $config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
+ $fp = fopen($filename, 'w');
+ fwrite($fp, $config);
+ fclose($fp);
+}
+
+/**
+ * Workaround for Windows bug in is_writable() function
+ *
+ * @since 2.8.0
+ *
+ * @param string $path
+ * @return bool
+ */
+function win_is_writable( $path ) {
+ /* will work in despite of Windows ACLs bug
+ * NOTE: use a trailing slash for folders!!!
+ * see http://bugs.php.net/bug.php?id=27609
+ * see http://bugs.php.net/bug.php?id=30931
+ */
+
+ if ( $path[strlen( $path ) - 1] == '/' ) // recursively return a temporary file path
+ return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
+ else if ( is_dir( $path ) )
+ return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
+ // check tmp file for read/write capabilities
+ $should_delete_tmp_file = !file_exists( $path );
+ $f = @fopen( $path, 'a' );
+ if ( $f === false )
+ return false;
+ fclose( $f );
+ if ( $should_delete_tmp_file )
+ unlink( $path );
+ return true;
+}
+
+/**
+ * Display the default admin color scheme picker (Used in user-edit.php)
+ *
+ * @since 3.0.0
+ */
+function admin_color_scheme_picker() {
+ global $_wp_admin_css_colors, $user_id; ?>
+<fieldset><legend class="screen-reader-text"><span><?php _e('Admin Color Scheme')?></span></legend>
+<?php
+$current_color = get_user_option('admin_color', $user_id);
+if ( empty($current_color) )
+ $current_color = 'fresh';
+foreach ( $_wp_admin_css_colors as $color => $color_info ): ?>
+<div class="color-option"><input name="admin_color" id="admin_color_<?php echo $color; ?>" type="radio" value="<?php echo esc_attr($color) ?>" class="tog" <?php checked($color, $current_color); ?> />
+ <table class="color-palette">
+ <tr>
+ <?php foreach ( $color_info->colors as $html_color ): ?>
+ <td style="background-color: <?php echo $html_color ?>" title="<?php echo $color ?>"> </td>
+ <?php endforeach; ?>
+ </tr>
+ </table>
+
+ <label for="admin_color_<?php echo $color; ?>"><?php echo $color_info->name ?></label>
+</div>
+ <?php endforeach; ?>
+</fieldset>
+<?php
+}
+
+function _ipad_meta() {
+ if ( wp_is_mobile() ) {
+ ?>
+ <meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
+ <?php
+ }
+}
+add_action('admin_head', '_ipad_meta');