X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/fa11948979fd6a4ea5705dc613b239699a459db3..245e789b234afa4525862e7a6e5e3c2e7a52ef20:/wp-includes/functions.php?ds=sidebyside diff --git a/wp-includes/functions.php b/wp-includes/functions.php index aea813b7..88e59fb1 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -46,13 +46,14 @@ function mysql2date( $format, $date, $translate = true ) { * * The 'mysql' type will return the time in the format for MySQL DATETIME field. * The 'timestamp' type will return the current timestamp. + * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d'). * * If $gmt is set to either '1' or 'true', then both types will use GMT time. * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option. * * @since 1.0.0 * - * @param string $type Either 'mysql' or 'timestamp'. + * @param string $type 'mysql', 'timestamp', or PHP date format string (e.g. 'Y-m-d'). * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false. * @return int|string String if $type is 'gmt', int if $type is 'timestamp'. */ @@ -64,6 +65,9 @@ function current_time( $type, $gmt = 0 ) { case 'timestamp': return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); break; + default: + return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); + break; } } @@ -136,8 +140,18 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) { } } $j = @$datefunc( $dateformatstring, $i ); - // allow plugins to redo this entirely for languages with untypical grammars - $j = apply_filters('date_i18n', $j, $req_format, $i, $gmt); + + /** + * Filter the date formatted based on the locale. + * + * @since 2.8.0 + * + * @param string $j Formatted date string. + * @param string $req_format Format to display the date. + * @param int $i Unix timestamp. + * @param bool $gmt Whether to convert to GMT for time. Default false. + */ + $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt ); return $j; } @@ -153,6 +167,14 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) { function number_format_i18n( $number, $decimals = 0 ) { global $wp_locale; $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] ); + + /** + * Filter the number formatted based on the locale. + * + * @since 2.8.0 + * + * @param string $formatted Converted number in string format. + */ return apply_filters( 'number_format_i18n', $formatted ); } @@ -247,20 +269,24 @@ function maybe_unserialize( $original ) { */ function is_serialized( $data, $strict = true ) { // if it isn't a string, it isn't serialized - if ( ! is_string( $data ) ) + if ( ! is_string( $data ) ) { return false; + } $data = trim( $data ); - if ( 'N;' == $data ) + if ( 'N;' == $data ) { return true; - $length = strlen( $data ); - if ( $length < 4 ) + } + if ( strlen( $data ) < 4 ) { return false; - if ( ':' !== $data[1] ) + } + if ( ':' !== $data[1] ) { return false; + } if ( $strict ) { - $lastc = $data[ $length - 1 ]; - if ( ';' !== $lastc && '}' !== $lastc ) + $lastc = substr( $data, -1 ); + if ( ';' !== $lastc && '}' !== $lastc ) { return false; + } } else { $semicolon = strpos( $data, ';' ); $brace = strpos( $data, '}' ); @@ -277,8 +303,9 @@ function is_serialized( $data, $strict = true ) { switch ( $token ) { case 's' : if ( $strict ) { - if ( '"' !== $data[ $length - 2 ] ) + if ( '"' !== substr( $data, -2, 1 ) ) { return false; + } } elseif ( false === strpos( $data, '"' ) ) { return false; } @@ -305,22 +332,23 @@ function is_serialized( $data, $strict = true ) { */ function is_serialized_string( $data ) { // if it isn't a string, it isn't a serialized string - if ( !is_string( $data ) ) + if ( ! is_string( $data ) ) { return false; + } $data = trim( $data ); - $length = strlen( $data ); - if ( $length < 4 ) + if ( strlen( $data ) < 4 ) { return false; - elseif ( ':' !== $data[1] ) + } elseif ( ':' !== $data[1] ) { return false; - elseif ( ';' !== $data[$length-1] ) + } elseif ( ';' !== substr( $data, -1 ) ) { return false; - elseif ( $data[0] !== 's' ) + } elseif ( $data[0] !== 's' ) { return false; - elseif ( '"' !== $data[$length-2] ) + } elseif ( '"' !== substr( $data, -2, 1 ) ) { return false; - else + } else { return true; + } } /** @@ -349,8 +377,6 @@ function maybe_serialize( $data ) { * If the title element is not part of the XML, then the default post title from * the $post_default_title will be used instead. * - * @package WordPress - * @subpackage XMLRPC * @since 0.71 * * @global string $post_default_title Default XMLRPC post title. @@ -375,8 +401,6 @@ function xmlrpc_getposttitle( $content ) { * used. The return type then would be what $post_default_category. If the * category is found, then it will always be an array. * - * @package WordPress - * @subpackage XMLRPC * @since 0.71 * * @global string $post_default_category Default XMLRPC post category. @@ -398,8 +422,6 @@ function xmlrpc_getpostcategory( $content ) { /** * XMLRPC XML content without title and category elements. * - * @package WordPress - * @subpackage XMLRPC * @since 0.71 * * @param string $content XMLRPC XML Request content @@ -439,7 +461,6 @@ function wp_extract_urls( $content ) { * remove enclosures that are no longer in the post. This is called as * pingbacks and trackbacks. * - * @package WordPress * @since 1.5.0 * * @uses $wpdb @@ -867,10 +888,14 @@ function get_status_header_desc( $code ) { 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', + 418 => 'I\'m a teapot', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 426 => 'Upgrade Required', + 428 => 'Precondition Required', + 429 => 'Too Many Requests', + 431 => 'Request Header Fields Too Large', 500 => 'Internal Server Error', 501 => 'Not Implemented', @@ -880,7 +905,8 @@ function get_status_header_desc( $code ) { 505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', - 510 => 'Not Extended' + 510 => 'Not Extended', + 511 => 'Network Authentication Required', ); } @@ -894,27 +920,35 @@ function get_status_header_desc( $code ) { * Set HTTP status header. * * @since 2.0.0 - * @uses apply_filters() Calls 'status_header' on status header string, HTTP - * HTTP code, HTTP code description, and protocol string as separate - * parameters. + * @see get_status_header_desc() * - * @param int $header HTTP status code - * @return unknown + * @param int $code HTTP status code. */ -function status_header( $header ) { - $text = get_status_header_desc( $header ); +function status_header( $code ) { + $description = get_status_header_desc( $code ); - if ( empty( $text ) ) - return false; + if ( empty( $description ) ) + return; - $protocol = $_SERVER["SERVER_PROTOCOL"]; + $protocol = $_SERVER['SERVER_PROTOCOL']; if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) $protocol = 'HTTP/1.0'; - $status_header = "$protocol $header $text"; + $status_header = "$protocol $code $description"; if ( function_exists( 'apply_filters' ) ) - $status_header = apply_filters( 'status_header', $status_header, $header, $text, $protocol ); - return @header( $status_header, true, $header ); + /** + * Filter an HTTP status header. + * + * @since 2.2.0 + * + * @param string $status_header HTTP status header. + * @param int $code HTTP status code. + * @param string $description Description for the status code. + * @param string $protocol Server protocol. + */ + $status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol ); + + @header( $status_header, true, $code ); } /** @@ -925,7 +959,6 @@ function status_header( $header ) { * * @since 2.8.0 * - * @uses apply_filters() * @return array The associative array of header names and field values. */ function wp_get_nocache_headers() { @@ -936,7 +969,20 @@ function wp_get_nocache_headers() { ); if ( function_exists('apply_filters') ) { - $headers = (array) apply_filters('nocache_headers', $headers); + /** + * Filter the cache-controlling headers. + * + * @since 2.8.0 + * + * @param array $headers { + * Header names and field values. + * + * @type string $Expires Expires header. + * @type string $Cache-Control Cache-Control header. + * @type string $Pragma Pragma header. + * } + */ + $headers = (array) apply_filters( 'nocache_headers', $headers ); } $headers['Last-Modified'] = false; return $headers; @@ -949,7 +995,7 @@ function wp_get_nocache_headers() { * be sent so that all of them get the point that no caching should occur. * * @since 2.0.0 - * @uses wp_get_nocache_headers() + * @see wp_get_nocache_headers() */ function nocache_headers() { $headers = wp_get_nocache_headers(); @@ -1019,8 +1065,8 @@ function bool_from_yn( $yn ) { * It is better to only have one hook for each feed. * * @since 2.1.0 + * * @uses $wp_query Used to tell if the use a comment feed. - * @uses do_action() Calls 'do_feed_$feed' hook, if a hook exists for the feed. */ function do_feed() { global $wp_query; @@ -1037,6 +1083,15 @@ function do_feed() { if ( ! has_action( $hook ) ) wp_die( __( 'ERROR: This is not a valid feed template.' ), '', array( 'response' => 404 ) ); + /** + * Fires once the given feed is loaded. + * + * The dynamic hook name, $hook, refers to the feed name. + * + * @since 2.1.0 + * + * @param bool $is_comment_feed Whether the feed is a comment feed. + */ do_action( $hook, $wp_query->is_comment_feed ); } @@ -1093,11 +1148,15 @@ function do_feed_atom( $for_comments ) { * robots.txt file. * * @since 2.1.0 - * @uses do_action() Calls 'do_robotstxt' hook for displaying robots.txt rules. */ function do_robots() { header( 'Content-Type: text/plain; charset=utf-8' ); + /** + * Fires when displaying the robots.txt file. + * + * @since 2.1.0 + */ do_action( 'do_robotstxt' ); $output = "User-agent: *\n"; @@ -1111,7 +1170,15 @@ function do_robots() { $output .= "Disallow: $path/wp-includes/\n"; } - echo apply_filters('robots_txt', $output, $public); + /** + * Filter the robots.txt output. + * + * @since 3.0.0 + * + * @param string $output Robots.txt output. + * @param bool $public Whether the site is considered "public". + */ + echo apply_filters( 'robots_txt', $output, $public ); } /** @@ -1191,14 +1258,12 @@ function is_blog_installed() { /** * Retrieve URL with nonce added to URL query. * - * @package WordPress - * @subpackage Security * @since 2.0.4 * * @param string $actionurl URL to add nonce action. * @param string $action Optional. Nonce action name. * @param string $name Optional. Nonce name. - * @return string URL with nonce action added. + * @return string Escaped URL with nonce action added. */ function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) { $actionurl = str_replace( '&', '&', $actionurl ); @@ -1223,8 +1288,6 @@ function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) { * The input name will be whatever $name value you gave. The input value will be * the nonce creation value. * - * @package WordPress - * @subpackage Security * @since 2.0.4 * * @param string $action Optional. Action name. @@ -1252,8 +1315,6 @@ function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $ec * The referer link is the current Request URI from the server super global. The * input name is '_wp_http_referer', in case you wanted to check manually. * - * @package WordPress - * @subpackage Security * @since 2.0.4 * * @param bool $echo Whether to echo or return the referer field. @@ -1274,8 +1335,6 @@ function wp_referer_field( $echo = true ) { * value of {@link wp_referer_field()}, if that was posted already or it will * be the current page, if it doesn't exist. * - * @package WordPress - * @subpackage Security * @since 2.0.4 * * @param bool $echo Whether to echo the original http referer @@ -1296,8 +1355,6 @@ function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) { * Retrieve referer from '_wp_http_referer' or HTTP referer. If it's the same * as the current request URL, will return false. * - * @package WordPress - * @subpackage Security * @since 2.0.4 * * @return string|bool False on failure. Referer URL on success. @@ -1319,8 +1376,6 @@ function wp_get_referer() { /** * Retrieve original referer that was posted, if it exists. * - * @package WordPress - * @subpackage Security * @since 2.0.4 * * @return string|bool False if no original referer or original referer if set. @@ -1372,14 +1427,23 @@ function wp_mkdir_p( $target ) { } // Get the permission bits. - if ( $target_parent && '.' != $target_parent ) { - $stat = @stat( $target_parent ); + $dir_perms = false; + if ( $stat = @stat( $target_parent ) ) { $dir_perms = $stat['mode'] & 0007777; } else { $dir_perms = 0777; } if ( @mkdir( $target, $dir_perms, true ) ) { + + // If a umask is set that modifies $dir_perms, we'll have to re-set the $dir_perms correctly with chmod() + if ( $dir_perms != ( $dir_perms & ~umask() ) ) { + $folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) ); + for ( $i = 1; $i <= count( $folder_parts ); $i++ ) { + @chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms ); + } + } + return true; } @@ -1428,6 +1492,23 @@ function path_join( $base, $path ) { return rtrim($base, '/') . '/' . ltrim($path, '/'); } +/** + * Normalize a filesystem path. + * + * Replaces backslashes with forward slashes for Windows systems, + * and ensures no duplicate slashes exist. + * + * @since 3.9.0 + * + * @param string $path Path to normalize. + * @return string Normalized path. + */ +function wp_normalize_path( $path ) { + $path = str_replace( '\\', '/', $path ); + $path = preg_replace( '|/+|','/', $path ); + return $path; +} + /** * Determines a writable directory for temporary files. * Function's preference is the return value of sys_get_temp_dir(), @@ -1448,17 +1529,17 @@ function get_temp_dir() { return trailingslashit(WP_TEMP_DIR); if ( $temp ) - return trailingslashit( rtrim( $temp, '\\' ) ); + return trailingslashit( $temp ); if ( function_exists('sys_get_temp_dir') ) { $temp = sys_get_temp_dir(); if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) - return trailingslashit( rtrim( $temp, '\\' ) ); + return trailingslashit( $temp ); } $temp = ini_get('upload_tmp_dir'); if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) - return trailingslashit( rtrim( $temp, '\\' ) ); + return trailingslashit( $temp ); $temp = WP_CONTENT_DIR . '/'; if ( is_dir( $temp ) && wp_is_writable( $temp ) ) @@ -1550,7 +1631,6 @@ function win_is_writable( $path ) { * 'error' - set to false. * * @since 2.0.0 - * @uses apply_filters() Calls 'upload_dir' on returned array. * * @param string $time Optional. Time formatted in 'yyyy/mm'. * @return array See above for description. @@ -1637,6 +1717,14 @@ function wp_upload_dir( $time = null ) { $dir .= $subdir; $url .= $subdir; + /** + * Filter the uploads directory data. + * + * @since 2.0.0 + * + * @param array $uploads Array of upload directory data with keys of 'path', + * 'url', 'subdir, 'basedir', and 'error'. + */ $uploads = apply_filters( 'upload_dir', array( 'path' => $dir, @@ -1762,6 +1850,16 @@ function wp_upload_bits( $name, $deprecated, $bits, $time = null ) { if ( $upload['error'] !== false ) return $upload; + /** + * Filter whether to treat the upload bits as an error. + * + * Passing a non-array to the filter will effectively short-circuit preparing + * the upload bits, returning that value instead. + * + * @since 3.0.0 + * + * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return. + */ $upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) ); if ( !is_array( $upload_bits_error ) ) { $upload[ 'error' ] = $upload_bits_error; @@ -1805,15 +1903,25 @@ function wp_upload_bits( $name, $deprecated, $bits, $time = null ) { /** * Retrieve the file type based on the extension name. * - * @package WordPress * @since 2.5.0 - * @uses apply_filters() Calls 'ext2type' hook on default supported types. * * @param string $ext The extension to search. - * @return string|null The file type, example: audio, video, document, spreadsheet, etc. Null if not found. + * @return string|null The file type, example: audio, video, document, spreadsheet, etc. + * Null if not found. */ function wp_ext2type( $ext ) { $ext = strtolower( $ext ); + + /** + * Filter file type based on the extension name. + * + * @since 2.5.0 + * + * @see wp_ext2type() + * + * @param array $ext2type Multi-dimensional array with extensions for a default set + * of file types. + */ $ext2type = apply_filters( 'ext2type', array( 'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ), 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), @@ -1898,8 +2006,13 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) { - // This is a simplified array of MIMEs that getimagesize() can detect and their extensions - // You shouldn't need to use this filter, but it's here just in case + /** + * Filter the list mapping image mime types to their respective extensions. + * + * @since 3.0.0 + * + * @param array $mime_to_ext Array of image mime types and their matching extensions. + */ $mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array( 'image/jpeg' => 'jpg', 'image/png' => 'png', @@ -1925,8 +2038,18 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { } } - // Let plugins try and validate other types of files - // Should return an array in the style of array( 'ext' => $ext, 'type' => $type, 'proper_filename' => $proper_filename ) + /** + * Filter the "real" file type of the given file. + * + * @since 3.0.0 + * + * @param array $wp_check_filetype_and_ext File data array containing 'ext', 'type', and + * 'proper_filename' keys. + * @param string $file Full path to the file. + * @param string $filename The name of the file (may differ from $file due to + * $file being in a tmp directory). + * @param array $mimes Key is the file extension with value as the mime type. + */ return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes ); } @@ -1935,13 +2058,20 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { * * @since 3.5.0 * - * @uses apply_filters() Calls 'mime_types' on returned array. This filter should - * be used to add types, not remove them. To remove types use the upload_mimes filter. - * * @return array Array of mime types keyed by the file extension regex corresponding to those types. */ function wp_get_mime_types() { - // Accepted MIME types are set here as PCRE unless provided. + /** + * Filter the list of mime types and file extensions. + * + * This filter should be used to add, not remove, mime types. To remove + * mime types, use the 'upload_mimes' filter. + * + * @since 3.5.0 + * + * @param array $wp_get_mime_types Mime types keyed by the file extension regex + * corresponding to those types. + */ return apply_filters( 'mime_types', array( // Image formats 'jpg|jpeg|jpe' => 'image/jpeg', @@ -1972,6 +2102,7 @@ function wp_get_mime_types() { 'rtx' => 'text/richtext', 'css' => 'text/css', 'htm|html' => 'text/html', + 'vtt' => 'text/vtt', // Audio formats 'mp3|m4a|m4b' => 'audio/mpeg', 'ra|ram' => 'audio/x-realaudio', @@ -2041,7 +2172,6 @@ function wp_get_mime_types() { * * @since 2.8.6 * - * @uses apply_filters() Calls 'upload_mimes' on returned array * @uses wp_get_upload_mime_types() to fetch the list of mime types * * @param int|WP_User $user Optional. User to check. Defaults to current user. @@ -2057,6 +2187,16 @@ function get_allowed_mime_types( $user = null ) { if ( empty( $unfiltered ) ) unset( $t['htm|html'] ); + /** + * Filter list of allowed mime types and file extensions. + * + * @since 2.0.0 + * + * @param array $t Mime types keyed by the file extension regex corresponding to + * those types. 'swf' and 'exe' removed from full list. 'htm|html' also + * removed depending on '$user' capabilities. + * @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user). + */ return apply_filters( 'upload_mimes', $t, $user ); } @@ -2066,8 +2206,6 @@ function get_allowed_mime_types( $user = null ) { * If the action has the nonce explain message, then it will be displayed along * with the "Are you sure?" message. * - * @package WordPress - * @subpackage Security * @since 2.0.4 * * @param string $action The nonce action. @@ -2076,7 +2214,8 @@ function wp_nonce_ays( $action ) { $title = __( 'WordPress Failure Notice' ); if ( 'log-out' == $action ) { $html = sprintf( __( 'You are attempting to log out of %s' ), get_bloginfo( 'name' ) ) . '

'; - $html .= sprintf( __( "Do you really want to log out?"), wp_logout_url() ); + $redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; + $html .= sprintf( __( "Do you really want to log out?"), wp_logout_url( $redirect_to ) ); } else { $html = __( 'Are you sure you want to do this?' ); if ( wp_get_referer() ) @@ -2102,12 +2241,34 @@ function wp_nonce_ays( $action ) { * @param string|array $args Optional arguments to control behavior. */ function wp_die( $message = '', $title = '', $args = array() ) { - if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { + /** + * Filter callback for killing WordPress execution for AJAX requests. + * + * @since 3.4.0 + * + * @param callback $function Callback function name. + */ $function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' ); - elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) + } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { + /** + * Filter callback for killing WordPress execution for XML-RPC requests. + * + * @since 3.4.0 + * + * @param callback $function Callback function name. + */ $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' ); - else + } else { + /** + * Filter callback for killing WordPress execution for all non-AJAX, non-XML-RPC requests. + * + * @since 3.0.0 + * + * @param callback $function Callback function name. + */ $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' ); + } call_user_func( $function, $message, $title, $args ); } @@ -2183,24 +2344,23 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { <?php echo $title ?>