WordPressを読む 25-4 /blog/wp-includes/formatting.php 4
2014/12/09
目次
- 1 /blog/wp-includes/formatting.php 4
- 2 関数 convert_chars()
- 3 関数 balanceTags()
- 4 関数 force_balance_tags()
- 5 関数 format_to_edit()
- 6 関数 zeroise()
- 7 関数 backslashit()
- 8 関数 trailingslashit()
- 9 関数 untrailingslashit()
- 10 関数 addslashes_gpc()
- 11 関数 stripslashes_deep()
- 12 関数 urlencode_deep()
- 13 関数 rawurlencode_deep()
- 14 関数 antispambot()
- 15 関数 _make_url_clickable_cb()
- 16 関数 _make_web_ftp_clickable_cb()
- 17 関数 _make_email_clickable_cb()
- 18 関数 make_clickable()
/blog/wp-includes/formatting.php 4
関数 convert_chars()
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 | function convert_chars($content, $deprecated = '') { if ( !empty( $deprecated ) ) _deprecated_argument( __FUNCTION__, '0.71' ); // Translation of invalid Unicode references range to valid range $wp_htmltranswinuni = array( '€' => '€', // the Euro sign '' => '', '‚' => '‚', // these are Windows CP1252 specific characters 'ƒ' => 'ƒ', // they would look weird on non-Windows browsers '„' => '„', '…' => '…', '†' => '†', '‡' => '‡', 'ˆ' => 'ˆ', '‰' => '‰', 'Š' => 'Š', '‹' => '‹', 'Œ' => 'Œ', '' => '', 'Ž' => 'Ž', '' => '', '' => '', '‘' => '‘', '’' => '’', '“' => '“', '”' => '”', '•' => '•', '–' => '–', '—' => '—', '˜' => '˜', '™' => '™', 'š' => 'š', '›' => '›', 'œ' => 'œ', '' => '', 'ž' => 'ž', 'Ÿ' => 'Ÿ' ); // Remove metadata tags $content = preg_replace('/<title>(.+?)<\/title>/','',$content); $content = preg_replace('/<category>(.+?)<\/category>/','',$content); // Converts lone & characters into & (a.k.a. &) $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&$1', $content); // Fix Word pasting $content = strtr($content, $wp_htmltranswinuni); // Just a little XHTML help $content = str_replace('<br>', '<br />', $content); $content = str_replace('<hr>', '<hr />', $content); return $content; } |
関数 balanceTags()
1 2 3 4 5 6 7 | function balanceTags( $text, $force = false ) { if ( $force || get_option('use_balanceTags') == 1 ) { return force_balance_tags( $text ); } else { return $text; } } |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | function force_balance_tags( $text ) { $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = ''; // Known single-entity/self-closing tags $single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source' ); // Tags that can be immediately nested within themselves $nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' ); // WP bug fix for comments - in case you REALLY meant to type '< !--' $text = str_replace('< !--', '< !--', $text); // WP bug fix for LOVE <3 (and other situations with '<' before a number) $text = preg_replace('#<([0-9]{1})#', '<$1', $text); while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) { $newtext .= $tagqueue; $i = strpos($text, $regex[0]); $l = strlen($regex[0]); // clear the shifter $tagqueue = ''; // Pop or Push if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag $tag = strtolower(substr($regex[1],1)); // if too many closing tags if( $stacksize <= 0 ) { $tag = ''; // or close to be safe $tag = '/' . $tag; } // if stacktop value = tag close value then pop else if ( $tagstack[$stacksize - 1] == $tag ) { // found closing tag $tag = '</' . $tag . '>'; // Close Tag // Pop array_pop( $tagstack ); $stacksize--; } else { // closing tag not at top, search for it for ( $j = $stacksize-1; $j >= 0; $j-- ) { if ( $tagstack[$j] == $tag ) { // add tag to tagqueue for ( $k = $stacksize-1; $k >= $j; $k--) { $tagqueue .= '</' . array_pop( $tagstack ) . '>'; $stacksize--; } break; } } $tag = ''; } } else { // Begin Tag $tag = strtolower($regex[1]); // Tag Cleaning // If it's an empty tag "< >", do nothing if ( '' == $tag ) { // do nothing } // ElseIf it presents itself as a self-closing tag... elseif ( substr( $regex[2], -1 ) == '/' ) { // ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such and // immediately close it with a closing tag (the tag will encapsulate no text as a result) if ( ! in_array( $tag, $single_tags ) ) $regex[2] = trim( substr( $regex[2], 0, -1 ) ) . "></$tag"; } // ElseIf it's a known single-entity tag but it doesn't close itself, do so elseif ( in_array($tag, $single_tags) ) { $regex[2] .= '/'; } // Else it's not a single-entity tag else { // If the top of the stack is the same as the tag we want to push, close previous tag if ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) { $tagqueue = '</' . array_pop( $tagstack ) . '>'; $stacksize--; } $stacksize = array_push( $tagstack, $tag ); } // Attributes $attributes = $regex[2]; if( ! empty( $attributes ) && $attributes[0] != '>' ) $attributes = ' ' . $attributes; $tag = '<' . $tag . $attributes . '>'; //If already queuing a close tag, then put this tag on, too if ( !empty($tagqueue) ) { $tagqueue .= $tag; $tag = ''; } } $newtext .= substr($text, 0, $i) . $tag; $text = substr($text, $i + $l); } // Clear Tag Queue $newtext .= $tagqueue; // Add Remaining text $newtext .= $text; // Empty Stack while( $x = array_pop($tagstack) ) $newtext .= '</' . $x . '>'; // Add remaining tags to close // WP fix for the bug with HTML comments $newtext = str_replace("< !--","<!--",$newtext); $newtext = str_replace("< !--","< !--",$newtext); return $newtext; } |
関数 format_to_edit()
1 2 3 4 5 6 | function format_to_edit( $content, $richedit = false ) { $content = apply_filters( 'format_to_edit', $content ); if ( ! $richedit ) $content = esc_textarea( $content ); return $content; } |
関数 zeroise()
1 2 3 | function zeroise($number, $threshold) { return sprintf('%0'.$threshold.'s', $number); } |
関数 backslashit()
1 2 3 4 5 | function backslashit($string) { if ( isset( $string[0] ) && $string[0] >= '0' && $string[0] <= '9' ) $string = '\\\\' . $string; return addcslashes( $string, 'A..Za..z' ); } |
関数 trailingslashit()
1 2 3 | function trailingslashit( $string ) { return untrailingslashit( $string ) . '/'; } |
関数 untrailingslashit()
1 2 3 | function untrailingslashit( $string ) { return rtrim( $string, '/\\' ); } |
関数 addslashes_gpc()
1 2 3 4 5 6 | function addslashes_gpc($gpc) { if ( get_magic_quotes_gpc() ) $gpc = stripslashes($gpc); return wp_slash($gpc); } |
関数 stripslashes_deep()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function stripslashes_deep($value) { if ( is_array($value) ) { $value = array_map('stripslashes_deep', $value); } elseif ( is_object($value) ) { $vars = get_object_vars( $value ); foreach ($vars as $key=>$data) { $value->{$key} = stripslashes_deep( $data ); } } elseif ( is_string( $value ) ) { $value = stripslashes($value); } return $value; } |
関数 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 <p re> 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; } |