]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/formatting.php
WordPress 4.2.4
[autoinstalls/wordpress.git] / wp-includes / formatting.php
index acdc3d782fe993e54f42b79944d28ba967ba2526..0151e1fc64ee3073600a485ed14af5cc6251b629 100644 (file)
@@ -419,7 +419,10 @@ function wpautop($pee, $br = true) {
        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
 
        // Standardize newline characters to "\n".
-       $pee = str_replace(array("\r\n", "\r"), "\n", $pee); 
+       $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
+
+       // Find newlines in all elements and add placeholders.
+       $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
 
        // Collapse line breaks before and after <option> elements so they don't get autop'd.
        if ( strpos( $pee, '<option' ) !== false ) {
@@ -506,9 +509,109 @@ function wpautop($pee, $br = true) {
        if ( !empty($pre_tags) )
                $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
 
+       // Restore newlines in all elements.
+       $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
+
        return $pee;
 }
 
+/**
+ * Separate HTML elements and comments from the text.
+ *
+ * @since 4.2.4
+ *
+ * @param string $input The text which has to be formatted.
+ * @return array The formatted text.
+ */
+function wp_html_split( $input ) {
+       static $regex;
+
+       if ( ! isset( $regex ) ) {
+               $comments =
+                         '!'           // Start of comment, after the <.
+                       . '(?:'         // Unroll the loop: Consume everything until --> is found.
+                       .     '-(?!->)' // Dash not followed by end of comment.
+                       .     '[^\-]*+' // Consume non-dashes.
+                       . ')*+'         // Loop possessively.
+                       . '(?:-->)?';   // End of comment. If not found, match all input.
+
+               $cdata =
+                         '!\[CDATA\['  // Start of comment, after the <.
+                       . '[^\]]*+'     // Consume non-].
+                       . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
+                       .     '](?!]>)' // One ] not followed by end of comment.
+                       .     '[^\]]*+' // Consume non-].
+                       . ')*+'         // Loop possessively.
+                       . '(?:]]>)?';   // End of comment. If not found, match all input.
+
+               $regex =
+                         '/('              // Capture the entire match.
+                       .     '<'           // Find start of element.
+                       .     '(?(?=!--)'   // Is this a comment?
+                       .         $comments // Find end of comment.
+                       .     '|'
+                       .         '(?(?=!\[CDATA\[)' // Is this a comment?
+                       .             $cdata // Find end of comment.
+                       .         '|'
+                       .             '[^>]*>?' // Find end of element. If not found, match all input.
+                       .         ')'
+                       .     ')'
+                       . ')/s';
+       }
+
+       return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
+}
+
+/**
+ * Replace characters or phrases within HTML elements only.
+ *
+ * @since 4.2.3
+ *
+ * @param string $haystack The text which has to be formatted.
+ * @param array $replace_pairs In the form array('from' => 'to', ...).
+ * @return string The formatted text.
+ */
+function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
+       // Find all elements.
+       $textarr = wp_html_split( $haystack );
+       $changed = false;
+
+       // Optimize when searching for one item.
+       if ( 1 === count( $replace_pairs ) ) {
+               // Extract $needle and $replace.
+               foreach ( $replace_pairs as $needle => $replace );
+
+               // Loop through delimeters (elements) only.
+               for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) { 
+                       if ( false !== strpos( $textarr[$i], $needle ) ) {
+                               $textarr[$i] = str_replace( $needle, $replace, $textarr[$i] );
+                               $changed = true;
+                       }
+               }
+       } else {
+               // Extract all $needles.
+               $needles = array_keys( $replace_pairs );
+
+               // Loop through delimeters (elements) only.
+               for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) { 
+                       foreach ( $needles as $needle ) {
+                               if ( false !== strpos( $textarr[$i], $needle ) ) {
+                                       $textarr[$i] = strtr( $textarr[$i], $replace_pairs );
+                                       $changed = true;
+                                       // After one strtr() break out of the foreach loop and look at next element.
+                                       break;
+                               }
+                       }
+               }
+       }
+
+       if ( $changed ) {
+               $haystack = implode( $textarr );
+       }
+
+       return $haystack;
+}
+
 /**
  * Newline preservation help function for wpautop
  *
@@ -3327,16 +3430,19 @@ function wp_make_link_relative( $link ) {
  */
 function sanitize_option($option, $value) {
        global $wpdb;
+       $error = '';
 
        switch ( $option ) {
                case 'admin_email' :
                case 'new_admin_email' :
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       $value = sanitize_email( $value );
-                       if ( ! is_email( $value ) ) {
-                               $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
-                               if ( function_exists( 'add_settings_error' ) )
-                                       add_settings_error( $option, 'invalid_admin_email', __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' ) );
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
+                       } else {
+                               $value = sanitize_email( $value );
+                               if ( ! is_email( $value ) ) {
+                                       $error = __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' );
+                               }
                        }
                        break;
 
@@ -3381,8 +3487,12 @@ function sanitize_option($option, $value) {
                case 'blogdescription':
                case 'blogname':
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       $value = wp_kses_post( $value );
-                       $value = esc_html( $value );
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
+                       } else {
+                               $value = wp_kses_post( $value );
+                               $value = esc_html( $value );
+                       }
                        break;
 
                case 'blog_charset':
@@ -3404,8 +3514,12 @@ function sanitize_option($option, $value) {
                case 'mailserver_pass':
                case 'upload_path':
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       $value = strip_tags( $value );
-                       $value = wp_kses_data( $value );
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
+                       } else {
+                               $value = strip_tags( $value );
+                               $value = wp_kses_data( $value );
+                       }
                        break;
 
                case 'ping_sites':
@@ -3421,23 +3535,27 @@ function sanitize_option($option, $value) {
 
                case 'siteurl':
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) {
-                               $value = esc_url_raw($value);
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
                        } else {
-                               $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
-                               if ( function_exists('add_settings_error') )
-                                       add_settings_error('siteurl', 'invalid_siteurl', __('The WordPress address you entered did not appear to be a valid URL. Please enter a valid URL.'));
+                               if ( preg_match( '#http(s?)://(.+)#i', $value ) ) {
+                                       $value = esc_url_raw( $value );
+                               } else {
+                                       $error = __( 'The WordPress address you entered did not appear to be a valid URL. Please enter a valid URL.' );
+                               }
                        }
                        break;
 
                case 'home':
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) {
-                               $value = esc_url_raw($value);
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
                        } else {
-                               $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
-                               if ( function_exists('add_settings_error') )
-                                       add_settings_error('home', 'invalid_home', __('The Site address you entered did not appear to be a valid URL. Please enter a valid URL.'));
+                               if ( preg_match( '#http(s?)://(.+)#i', $value ) ) {
+                                       $value = esc_url_raw( $value );
+                               } else {
+                                       $error = __( 'The Site address you entered did not appear to be a valid URL. Please enter a valid URL.' );
+                               }
                        }
                        break;
 
@@ -3453,38 +3571,45 @@ function sanitize_option($option, $value) {
 
                case 'illegal_names':
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       if ( ! is_array( $value ) )
-                               $value = explode( ' ', $value );
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
+                       } else {
+                               if ( ! is_array( $value ) )
+                                       $value = explode( ' ', $value );
 
-                       $value = array_values( array_filter( array_map( 'trim', $value ) ) );
+                               $value = array_values( array_filter( array_map( 'trim', $value ) ) );
 
-                       if ( ! $value )
-                               $value = '';
+                               if ( ! $value )
+                                       $value = '';
+                       }
                        break;
 
                case 'limited_email_domains':
                case 'banned_email_domains':
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       if ( ! is_array( $value ) )
-                               $value = explode( "\n", $value );
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
+                       } else {
+                               if ( ! is_array( $value ) )
+                                       $value = explode( "\n", $value );
 
-                       $domains = array_values( array_filter( array_map( 'trim', $value ) ) );
-                       $value = array();
+                               $domains = array_values( array_filter( array_map( 'trim', $value ) ) );
+                               $value = array();
 
-                       foreach ( $domains as $domain ) {
-                               if ( ! preg_match( '/(--|\.\.)/', $domain ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $domain ) )
-                                       $value[] = $domain;
+                               foreach ( $domains as $domain ) {
+                                       if ( ! preg_match( '/(--|\.\.)/', $domain ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $domain ) ) {
+                                               $value[] = $domain;
+                                       }
+                               }
+                               if ( ! $value )
+                                       $value = '';
                        }
-                       if ( ! $value )
-                               $value = '';
                        break;
 
                case 'timezone_string':
                        $allowed_zones = timezone_identifiers_list();
                        if ( ! in_array( $value, $allowed_zones ) && ! empty( $value ) ) {
-                               $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
-                               if ( function_exists('add_settings_error') )
-                                       add_settings_error('timezone_string', 'invalid_timezone_string', __('The timezone you have entered is not valid. Please select a valid timezone.') );
+                               $error = __( 'The timezone you have entered is not valid. Please select a valid timezone.' );
                        }
                        break;
 
@@ -3492,8 +3617,12 @@ function sanitize_option($option, $value) {
                case 'category_base':
                case 'tag_base':
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       $value = esc_url_raw( $value );
-                       $value = str_replace( 'http://', '', $value );
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
+                       } else {
+                               $value = esc_url_raw( $value );
+                               $value = str_replace( 'http://', '', $value );
+                       }
                        break;
 
                case 'default_role' :
@@ -3504,13 +3633,24 @@ function sanitize_option($option, $value) {
                case 'moderation_keys':
                case 'blacklist_keys':
                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
-                       $value = explode( "\n", $value );
-                       $value = array_filter( array_map( 'trim', $value ) );
-                       $value = array_unique( $value );
-                       $value = implode( "\n", $value );
+                       if ( is_wp_error( $value ) ) {
+                               $error = $value->get_error_message();
+                       } else {
+                               $value = explode( "\n", $value );
+                               $value = array_filter( array_map( 'trim', $value ) );
+                               $value = array_unique( $value );
+                               $value = implode( "\n", $value );
+                       }
                        break;
        }
 
+       if ( ! empty( $error ) ) {
+               $value = get_option( $option );
+               if ( function_exists( 'add_settings_error' ) ) {
+                       add_settings_error( $option, "invalid_{$option}", $error );
+               }
+       }
+
        /**
         * Filter an option value following sanitization.
         *
@@ -4140,8 +4280,9 @@ function print_emoji_detection_script() {
        );
 
        $version = 'ver=' . $wp_version;
+       $develop_src = false !== strpos( $wp_version, '-src' );
 
-       if ( SCRIPT_DEBUG ) {
+       if ( $develop_src || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) {
                $settings['source'] = array(
                        /** This filter is documented in wp-includes/class.wp-scripts.php */
                        'wpemoji' => apply_filters( 'script_loader_src', includes_url( "js/wp-emoji.js?$version" ), 'wpemoji' ),