WordPressを読む 25-8 /blog/wp-includes/formatting.php 8
2014/12/09
目次
- 1 /blog/wp-includes/formatting.php 8
- 2 関数 links_add_target()
- 3 関数 _links_add_target()
- 4 関数 normalize_whitespace()
- 5 関数 wp_strip_all_tags()
- 6 関数 sanitize_text_field()
- 7 関数 wp_basename()
- 8 関数 capital_P_dangit()
- 9 関数 sanitize_mime_type()
- 10 関数 sanitize_trackback_urls()
- 11 関数 wp_slash()
- 12 関数 wp_unslash()
- 13 関数 get_url_in_content()
- 14 関数 wp_spaces_regexp()
- 15 関数 urlencode_deep()
- 16 関数 rawurlencode_deep()
- 17 関数 antispambot()
- 18 関数 _make_url_clickable_cb()
- 19 関数 _make_web_ftp_clickable_cb()
- 20 関数 _make_email_clickable_cb()
- 21 関数 make_clickable()
- 22 関数 _split_str_by_whitespace()
- 23 関数 wp_rel_nofollow()
- 24 関数 wp_rel_nofollow_callback()
- 25 関数 translate_smiley()
- 26 関数 convert_smilies()
/blog/wp-includes/formatting.php 8
関数 links_add_target()
1 2 3 4 5 6 | function links_add_target( $content, $target = '_blank', $tags = array('a') ) { global $_links_add_target; $_links_add_target = $target; $tags = implode('|', (array)$tags); return preg_replace_callback( "!<($tags)([^>]*)>!i", '_links_add_target', $content ); } |
関数 _links_add_target()
1 2 3 4 5 6 | function _links_add_target( $m ) { global $_links_add_target; $tag = $m[1]; $link = preg_replace('|( target=([\'"])(.*?)\2)|i', '', $m[2]); return '<' . $tag . $link . ' target="' . esc_attr( $_links_add_target ) . '">'; } |
関数 normalize_whitespace()
1 2 3 4 5 6 | function normalize_whitespace( $str ) { $str = trim( $str ); $str = str_replace( "\r", "\n", $str ); $str = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str ); return $str; } |
1 2 3 4 5 6 7 8 9 | function wp_strip_all_tags($string, $remove_breaks = false) { $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string ); $string = strip_tags($string); if ( $remove_breaks ) $string = preg_replace('/[\r\n\t ]+/', ' ', $string); return trim( $string ); } |
関数 sanitize_text_field()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function sanitize_text_field($str) { $filtered = wp_check_invalid_utf8( $str ); if ( strpos($filtered, '<') !== false ) { $filtered = wp_pre_kses_less_than( $filtered ); // This will strip extra whitespace for us. $filtered = wp_strip_all_tags( $filtered, true ); } else { $filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) ); } $found = false; while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) { $filtered = str_replace($match[0], '', $filtered); $found = true; } if ( $found ) { // Strip out the whitespace that may now exist after removing the octets. $filtered = trim( preg_replace('/ +/', ' ', $filtered) ); } return apply_filters( 'sanitize_text_field', $filtered, $str ); } |
関数 wp_basename()
1 2 3 | function wp_basename( $path, $suffix = '' ) { return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) ); } |
関数 capital_P_dangit()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function capital_P_dangit( $text ) { // Simple replacement for titles $current_filter = current_filter(); if ( 'the_title' === $current_filter || 'wp_title' === $current_filter ) return str_replace( 'Wordpress', 'WordPress', $text ); // Still here? Use the more judicious replacement static $dblq = false; if ( false === $dblq ) $dblq = _x( '“', 'opening curly double quote' ); return str_replace( array( ' Wordpress', '‘Wordpress', $dblq . 'Wordpress', '>Wordpress', '(Wordpress' ), array( ' WordPress', '‘WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ), $text ); } |
関数 sanitize_mime_type()
1 2 3 4 | function sanitize_mime_type( $mime_type ) { $sani_mime_type = preg_replace( '/[^-+*.a-zA-Z0-9\/]/', '', $mime_type ); return apply_filters( 'sanitize_mime_type', $sani_mime_type, $mime_type ); } |
関数 sanitize_trackback_urls()
1 2 3 4 5 6 7 8 9 10 | function sanitize_trackback_urls( $to_ping ) { $urls_to_ping = preg_split( '/[\r\n\t ]/', trim( $to_ping ), -1, PREG_SPLIT_NO_EMPTY ); foreach ( $urls_to_ping as $k => $url ) { if ( !preg_match( '#^https?://.#i', $url ) ) unset( $urls_to_ping[$k] ); } $urls_to_ping = array_map( 'esc_url_raw', $urls_to_ping ); $urls_to_ping = implode( "\n", $urls_to_ping ); return apply_filters( 'sanitize_trackback_urls', $urls_to_ping, $to_ping ); } |
関数 wp_slash()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function wp_slash( $value ) { if ( is_array( $value ) ) { foreach ( $value as $k => $v ) { if ( is_array( $v ) ) { $value[$k] = wp_slash( $v ); } else { $value[$k] = addslashes( $v ); } } } else { $value = addslashes( $value ); } return $value; } |
関数 wp_unslash()
1 2 3 | function wp_unslash( $value ) { return stripslashes_deep( $value ); } |
関数 get_url_in_content()
1 2 3 4 5 6 7 8 9 10 | function get_url_in_content( $content ) { if ( empty( $content ) ) { return false; } if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) { return esc_url_raw( $matches[2] ); } return false; } |
関数 wp_spaces_regexp()
1 2 3 4 5 6 7 8 | function wp_spaces_regexp() { static $spaces; if ( empty( $spaces ) ) { $spaces = apply_filters( 'wp_spaces_regexp', '[\r\n\t ]|\xC2\xA0| ' ); } return $spaces; } |
関数 urlencode_deep()
1 2 3 4 | function urlencode_deep($value) { $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value); return $value; } |
関数 rawurlencode_deep()
1 2 3 | function rawurlencode_deep( $value ) { return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value ); } |
関数 antispambot()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function antispambot( $email_address, $hex_encoding = 0 ) { $email_no_spam_address = ''; for ( $i = 0; $i < strlen( $email_address ); $i++ ) { $j = rand( 0, 1 + $hex_encoding ); if ( $j == 0 ) { $email_no_spam_address .= '&#' . ord( $email_address[$i] ) . ';'; } elseif ( $j == 1 ) { $email_no_spam_address .= $email_address[$i]; } elseif ( $j == 2 ) { $email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[$i] ) ), 2 ); } } $email_no_spam_address = str_replace( '@', '@', $email_no_spam_address ); return $email_no_spam_address; } |
関数 _make_url_clickable_cb()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function _make_url_clickable_cb($matches) { $url = $matches[2]; if ( ')' == $matches[3] && strpos( $url, '(' ) ) { // If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL. // Then we can let the parenthesis balancer do its thing below. $url .= $matches[3]; $suffix = ''; } else { $suffix = $matches[3]; } // Include parentheses in the URL only if paired while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) { $suffix = strrchr( $url, ')' ) . $suffix; $url = substr( $url, 0, strrpos( $url, ')' ) ); } $url = esc_url($url); if ( empty($url) ) return $matches[0]; return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix; } |
関数 _make_web_ftp_clickable_cb()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function _make_web_ftp_clickable_cb($matches) { $ret = ''; $dest = $matches[2]; $dest = 'http://' . $dest; $dest = esc_url($dest); if ( empty($dest) ) return $matches[0]; // removed trailing [.,;:)] from URL if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) { $ret = substr($dest, -1); $dest = substr($dest, 0, strlen($dest)-1); } return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret"; } |
関数 _make_email_clickable_cb()
1 2 3 4 | function _make_email_clickable_cb($matches) { $email = $matches[2] . '@' . $matches[3]; return $matches[1] . "<a href=\"mailto:$email\">$email</a>"; } |
関数 make_clickable()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | function make_clickable( $text ) { $r = ''; $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags $nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code> foreach ( $textarr as $piece ) { if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<p re[\s>]|i', $piece ) ) $nested_code_pre++; elseif ( ( '</code>' === strtolower( $piece ) || '</p re>' === strtolower( $piece ) ) && $nested_code_pre ) $nested_code_pre--; if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) { $r .= $piece; continue; } // Long strings might contain expensive edge cases ... if ( 10000 < strlen( $piece ) ) { // ... break it up foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses if ( 2101 < strlen( $chunk ) ) { $r .= $chunk; // Too big, no whitespace: bail. } else { $r .= make_clickable( $chunk ); } } } else { $ret = " $piece "; // Pad with whitespace to simplify the regexes $url_clickable = '~ ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation ( # 2: URL [\\w]{1,20}+:// # Scheme and hier-part prefix (?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character [\'.,;:!?)] # Punctuation URL character [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character )* ) (\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing) ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character. // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times. $ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret ); $ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret ); $ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret ); $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding. $r .= $ret; } } // Cleanup of accidental links within links $r = preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r ); return $r; } |
関数 _split_str_by_whitespace()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | function _split_str_by_whitespace( $string, $goal ) { $chunks = array(); $string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" ); while ( $goal < strlen( $string_nullspace ) ) { $pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" ); if ( false === $pos ) { $pos = strpos( $string_nullspace, "\000", $goal + 1 ); if ( false === $pos ) { break; } } $chunks[] = substr( $string, 0, $pos + 1 ); $string = substr( $string, $pos + 1 ); $string_nullspace = substr( $string_nullspace, $pos + 1 ); } if ( $string ) { $chunks[] = $string; } return $chunks; } |
関数 wp_rel_nofollow()
1 2 3 4 5 6 7 | function wp_rel_nofollow( $text ) { // This is a pre save filter, so text is already escaped. $text = stripslashes($text); $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text); $text = wp_slash($text); return $text; } |
関数 wp_rel_nofollow_callback()
1 2 3 4 5 | function wp_rel_nofollow_callback( $matches ) { $text = $matches[1]; $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text); return "<a $text rel=\"nofollow\">"; } |
関数 translate_smiley()
1 2 3 4 5 6 7 8 9 10 11 12 13 | function translate_smiley( $matches ) { global $wpsmiliestrans; if ( count( $matches ) == 0 ) return ''; $smiley = trim( reset( $matches ) ); $img = $wpsmiliestrans[ $smiley ]; $src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() ); return sprintf( '<img src="%s" alt="%s" class="wp-smiley" />', esc_url( $src_url ), esc_attr( $smiley ) ); } |
関数 convert_smilies()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | function convert_smilies( $text ) { global $wp_smiliessearch; $output = ''; if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) { // HTML loop taken from texturize function, could possible be consolidated $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between $stop = count( $textarr );// loop stuff // Ignore proessing of specific tags $tags_to_ignore = 'code|pre|style|script|textarea'; $ignore_block_element = ''; for ( $i = 0; $i < $stop; $i++ ) { $content = $textarr[$i]; // If we're in an ignore block, wait until we find its closing tag if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) { $ignore_block_element = $matches[1]; } // If it's not a tag and not in ignore block if ( '' == $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) { $content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content ); } // did we exit ignore block if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content ) { $ignore_block_element = ''; } $output .= $content; } } else { // return default text. $output = $text; } return $output; } |