WordPressを読む 25-11 /blog/wp-includes/formatting.php 11
2014/12/10
目次
- 1 /blog/wp-includes/formatting.php 11
- 2 関数 sanitize_option()
- 3 関数 wp_parse_str()
- 4 関数 wp_pre_kses_less_than()
- 5 関数 wp_pre_kses_less_than_callback()
- 6 関数 wp_sprintf()
- 7 関数 wp_sprintf_l()
- 8 関数 wp_html_excerpt()
- 9 関数 links_add_base_url()
- 10 関数 _links_add_base()
- 11 関数 links_add_target()
- 12 関数 _links_add_target()
- 13 関数 normalize_whitespace()
- 14 関数 wp_strip_all_tags()
- 15 関数 sanitize_text_field()
- 16 関数 wp_basename()
/blog/wp-includes/formatting.php 11
関数 sanitize_option()
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | function sanitize_option($option, $value) { switch ( $option ) { case 'admin_email' : case 'new_admin_email' : $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.' ) ); } break; case 'thumbnail_size_w': case 'thumbnail_size_h': case 'medium_size_w': case 'medium_size_h': case 'large_size_w': case 'large_size_h': case 'mailserver_port': case 'comment_max_links': case 'page_on_front': case 'page_for_posts': case 'rss_excerpt_length': case 'default_category': case 'default_email_category': case 'default_link_category': case 'close_comments_days_old': case 'comments_per_page': case 'thread_comments_depth': case 'users_can_register': case 'start_of_week': $value = absint( $value ); break; case 'posts_per_page': case 'posts_per_rss': $value = (int) $value; if ( empty($value) ) $value = 1; if ( $value < -1 ) $value = abs($value); break; case 'default_ping_status': case 'default_comment_status': // Options that if not there have 0 value but need to be something like "closed" if ( $value == '0' || $value == '') $value = 'closed'; break; case 'blogdescription': case 'blogname': $value = wp_kses_post( $value ); $value = esc_html( $value ); break; case 'blog_charset': $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes break; case 'blog_public': // This is the value if the settings checkbox is not checked on POST. Don't rely on this. if ( null === $value ) $value = 1; else $value = intval( $value ); break; case 'date_format': case 'time_format': case 'mailserver_url': case 'mailserver_login': case 'mailserver_pass': case 'upload_path': $value = strip_tags( $value ); $value = wp_kses_data( $value ); break; case 'ping_sites': $value = explode( "\n", $value ); $value = array_filter( array_map( 'trim', $value ) ); $value = array_filter( array_map( 'esc_url_raw', $value ) ); $value = implode( "\n", $value ); break; case 'gmt_offset': $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes break; case 'siteurl': if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) { $value = esc_url_raw($value); } 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.')); } break; case 'home': if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) { $value = esc_url_raw($value); } 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.')); } break; case 'WPLANG': $allowed = get_available_languages(); if ( ! is_multisite() && defined( 'WPLANG' ) && '' !== WPLANG && 'en_US' !== WPLANG ) { $allowed[] = WPLANG; } if ( ! in_array( $value, $allowed ) && ! empty( $value ) ) { $value = get_option( $option ); } break; case 'illegal_names': if ( ! is_array( $value ) ) $value = explode( ' ', $value ); $value = array_values( array_filter( array_map( 'trim', $value ) ) ); if ( ! $value ) $value = ''; break; case 'limited_email_domains': case 'banned_email_domains': if ( ! is_array( $value ) ) $value = explode( "\n", $value ); $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; } 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.') ); } break; case 'permalink_structure': case 'category_base': case 'tag_base': $value = esc_url_raw( $value ); $value = str_replace( 'http://', '', $value ); break; case 'default_role' : if ( ! get_role( $value ) && get_role( 'subscriber' ) ) $value = 'subscriber'; break; case 'moderation_keys': case 'blacklist_keys': $value = explode( "\n", $value ); $value = array_filter( array_map( 'trim', $value ) ); $value = array_unique( $value ); $value = implode( "\n", $value ); break; } $value = apply_filters( "sanitize_option_{$option}", $value, $option ); return $value; } |
関数 wp_parse_str()
1 2 3 4 5 6 7 | function wp_parse_str( $string, &$array ) { parse_str( $string, $array ); if ( get_magic_quotes_gpc() ) $array = stripslashes_deep( $array ); $array = apply_filters( 'wp_parse_str', $array ); } |
関数 wp_pre_kses_less_than()
1 2 3 | function wp_pre_kses_less_than( $text ) { return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text); } |
関数 wp_pre_kses_less_than_callback()
1 2 3 4 5 | function wp_pre_kses_less_than_callback( $matches ) { if ( false === strpos($matches[0], '>') ) return esc_html($matches[0]); return $matches[0]; } |
関数 wp_sprintf()
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 | function wp_sprintf( $pattern ) { $args = func_get_args(); $len = strlen($pattern); $start = 0; $result = ''; $arg_index = 0; while ( $len > $start ) { // Last character: append and break if ( strlen($pattern) - 1 == $start ) { $result .= substr($pattern, -1); break; } // Literal %: append and continue if ( substr($pattern, $start, 2) == '%%' ) { $start += 2; $result .= '%'; continue; } // Get fragment before next % $end = strpos($pattern, '%', $start + 1); if ( false === $end ) $end = $len; $fragment = substr($pattern, $start, $end - $start); // Fragment has a specifier if ( $pattern[$start] == '%' ) { // Find numbered arguments or take the next one in order if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) { $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : ''; $fragment = str_replace("%{$matches[1]}$", '%', $fragment); } else { ++$arg_index; $arg = isset($args[$arg_index]) ? $args[$arg_index] : ''; } $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg ); if ( $_fragment != $fragment ) $fragment = $_fragment; else $fragment = sprintf($fragment, strval($arg) ); } // Append to result and move to next fragment $result .= $fragment; $start = $end; } return $result; } |
関数 wp_sprintf_l()
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 | function wp_sprintf_l($pattern, $args) { // Not a match if ( substr($pattern, 0, 2) != '%l' ) return $pattern; // Nothing to work with if ( empty($args) ) return ''; $l = apply_filters( 'wp_sprintf_l', array( /* translators: used to join items in a list with more than 2 items */ 'between' => sprintf( __('%s, %s'), '', '' ), /* translators: used to join last two items in a list with more than 2 times */ 'between_last_two' => sprintf( __('%s, and %s'), '', '' ), /* translators: used to join items in a list with only 2 items */ 'between_only_two' => sprintf( __('%s and %s'), '', '' ), ) ); $args = (array) $args; $result = array_shift($args); if ( count($args) == 1 ) $result .= $l['between_only_two'] . array_shift($args); // Loop when more than two args $i = count($args); while ( $i ) { $arg = array_shift($args); $i--; if ( 0 == $i ) $result .= $l['between_last_two'] . $arg; else $result .= $l['between'] . $arg; } return $result . substr($pattern, 2); } |
関数 wp_html_excerpt()
1 2 3 4 5 6 7 8 9 10 11 | function wp_html_excerpt( $str, $count, $more = null ) { if ( null === $more ) $more = ''; $str = wp_strip_all_tags( $str, true ); $excerpt = mb_substr( $str, 0, $count ); // remove part of an entity at the end $excerpt = preg_replace( '/&[^;\s]{0,6}$/', '', $excerpt ); if ( $str != $excerpt ) $excerpt = trim( $excerpt ) . $more; return $excerpt; } |
関数 links_add_base_url()
1 2 3 4 5 6 | function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) { global $_links_add_base; $_links_add_base = $base; $attrs = implode('|', (array)$attrs); return preg_replace_callback( "!($attrs)=(['\"])(.+?)\\2!i", '_links_add_base', $content ); } |
関数 _links_add_base()
1 2 3 4 5 6 7 8 9 | function _links_add_base($m) { global $_links_add_base; //1 = attribute name 2 = quotation mark 3 = URL return $m[1] . '=' . $m[2] . ( preg_match( '#^(\w{1,20}):#', $m[3], $protocol ) && in_array( $protocol[1], wp_allowed_protocols() ) ? $m[3] : path_join( $_links_add_base, $m[3] ) ) . $m[2]; } |
関数 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 ) ); } |