/**
* Comment API: WP_Comment class
*
* @package WordPress
* @subpackage Comments
* @since 4.4.0
*/
/**
* Core class used to organize comments as instantiated objects with defined members.
*
* @since 4.4.0
*/
final class WP_Comment {
/**
* Comment ID.
*
* A numeric string, for compatibility reasons.
*
* @since 4.4.0
* @var string
*/
public $comment_ID;
/**
* ID of the post the comment is associated with.
*
* A numeric string, for compatibility reasons.
*
* @since 4.4.0
* @var string
*/
public $comment_post_ID = 0;
/**
* Comment author name.
*
* @since 4.4.0
* @var string
*/
public $comment_author = '';
/**
* Comment author email address.
*
* @since 4.4.0
* @var string
*/
public $comment_author_email = '';
/**
* Comment author URL.
*
* @since 4.4.0
* @var string
*/
public $comment_author_url = '';
/**
* Comment author IP address (IPv4 format).
*
* @since 4.4.0
* @var string
*/
public $comment_author_IP = '';
/**
* Comment date in YYYY-MM-DD HH:MM:SS format.
*
* @since 4.4.0
* @var string
*/
public $comment_date = '0000-00-00 00:00:00';
/**
* Comment GMT date in YYYY-MM-DD HH::MM:SS format.
*
* @since 4.4.0
* @var string
*/
public $comment_date_gmt = '0000-00-00 00:00:00';
/**
* Comment content.
*
* @since 4.4.0
* @var string
*/
public $comment_content;
/**
* Comment karma count.
*
* A numeric string, for compatibility reasons.
*
* @since 4.4.0
* @var string
*/
public $comment_karma = 0;
/**
* Comment approval status.
*
* @since 4.4.0
* @var string
*/
public $comment_approved = '1';
/**
* Comment author HTTP user agent.
*
* @since 4.4.0
* @var string
*/
public $comment_agent = '';
/**
* Comment type.
*
* @since 4.4.0
* @since 5.5.0 Default value changed to `comment`.
* @var string
*/
public $comment_type = 'comment';
/**
* Parent comment ID.
*
* A numeric string, for compatibility reasons.
*
* @since 4.4.0
* @var string
*/
public $comment_parent = 0;
/**
* Comment author ID.
*
* A numeric string, for compatibility reasons.
*
* @since 4.4.0
* @var string
*/
public $user_id = 0;
/**
* Comment children.
*
* @since 4.4.0
* @var array
*/
protected $children;
/**
* Whether children have been populated for this comment object.
*
* @since 4.4.0
* @var bool
*/
protected $populated_children = false;
/**
* Post fields.
*
* @since 4.4.0
* @var array
*/
protected $post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' );
/**
* Retrieves a WP_Comment instance.
*
* @since 4.4.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $id Comment ID.
* @return WP_Comment|false Comment object, otherwise false.
*/
public static function get_instance( $id ) {
global $wpdb;
$comment_id = (int) $id;
if ( ! $comment_id ) {
return false;
}
$_comment = wp_cache_get( $comment_id, 'comment' );
if ( ! $_comment ) {
$_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );
if ( ! $_comment ) {
return false;
}
wp_cache_add( $_comment->comment_ID, $_comment, 'comment' );
}
return new WP_Comment( $_comment );
}
/**
* Constructor.
*
* Populates properties with object vars.
*
* @since 4.4.0
*
* @param WP_Comment $comment Comment object.
*/
public function __construct( $comment ) {
foreach ( get_object_vars( $comment ) as $key => $value ) {
$this->$key = $value;
}
}
/**
* Convert object to array.
*
* @since 4.4.0
*
* @return array Object as array.
*/
public function to_array() {
return get_object_vars( $this );
}
/**
* Get the children of a comment.
*
* @since 4.4.0
*
* @param array $args {
* Array of arguments used to pass to get_comments() and determine format.
*
* @type string $format Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array.
* Default 'tree'.
* @type string $status Comment status to limit results by. Accepts 'hold' (`comment_status=0`),
* 'approve' (`comment_status=1`), 'all', or a custom comment status.
* Default 'all'.
* @type string $hierarchical Whether to include comment descendants in the results.
* 'threaded' returns a tree, with each comment's children
* stored in a `children` property on the `WP_Comment` object.
* 'flat' returns a flat array of found comments plus their children.
* Pass `false` to leave out descendants.
* The parameter is ignored (forced to `false`) when `$fields` is 'ids' or 'counts'.
* Accepts 'threaded', 'flat', or false. Default: 'threaded'.
* @type string|array $orderby Comment status or array of statuses. To use 'meta_value'
* or 'meta_value_num', `$meta_key` must also be defined.
* To sort by a specific `$meta_query` clause, use that
* clause's array key. Accepts 'comment_agent',
* 'comment_approved', 'comment_author',
* 'comment_author_email', 'comment_author_IP',
* 'comment_author_url', 'comment_content', 'comment_date',
* 'comment_date_gmt', 'comment_ID', 'comment_karma',
* 'comment_parent', 'comment_post_ID', 'comment_type',
* 'user_id', 'comment__in', 'meta_value', 'meta_value_num',
* the value of $meta_key, and the array keys of
* `$meta_query`. Also accepts false, an empty array, or
* 'none' to disable `ORDER BY` clause.
* }
* @return WP_Comment[] Array of `WP_Comment` objects.
*/
public function get_children( $args = array() ) {
$defaults = array(
'format' => 'tree',
'status' => 'all',
'hierarchical' => 'threaded',
'orderby' => '',
);
$_args = wp_parse_args( $args, $defaults );
$_args['parent'] = $this->comment_ID;
if ( is_null( $this->children ) ) {
if ( $this->populated_children ) {
$this->children = array();
} else {
$this->children = get_comments( $_args );
}
}
if ( 'flat' === $_args['format'] ) {
$children = array();
foreach ( $this->children as $child ) {
$child_args = $_args;
$child_args['format'] = 'flat';
// get_children() resets this value automatically.
unset( $child_args['parent'] );
$children = array_merge( $children, array( $child ), $child->get_children( $child_args ) );
}
} else {
$children = $this->children;
}
return $children;
}
/**
* Add a child to the comment.
*
* Used by `WP_Comment_Query` when bulk-filling descendants.
*
* @since 4.4.0
*
* @param WP_Comment $child Child comment.
*/
public function add_child( WP_Comment $child ) {
$this->children[ $child->comment_ID ] = $child;
}
/**
* Get a child comment by ID.
*
* @since 4.4.0
*
* @param int $child_id ID of the child.
* @return WP_Comment|false Returns the comment object if found, otherwise false.
*/
public function get_child( $child_id ) {
if ( isset( $this->children[ $child_id ] ) ) {
return $this->children[ $child_id ];
}
return false;
}
/**
* Set the 'populated_children' flag.
*
* This flag is important for ensuring that calling `get_children()` on a childless comment will not trigger
* unneeded database queries.
*
* @since 4.4.0
*
* @param bool $set Whether the comment's children have already been populated.
*/
public function populated_children( $set ) {
$this->populated_children = (bool) $set;
}
/**
* Check whether a non-public property is set.
*
* If `$name` matches a post field, the comment post will be loaded and the post's value checked.
*
* @since 4.4.0
*
* @param string $name Property name.
* @return bool
*/
public function __isset( $name ) {
if ( in_array( $name, $this->post_fields, true ) && 0 !== (int) $this->comment_post_ID ) {
$post = get_post( $this->comment_post_ID );
return property_exists( $post, $name );
}
}
/**
* Magic getter.
*
* If `$name` matches a post field, the comment post will be loaded and the post's value returned.
*
* @since 4.4.0
*
* @param string $name
* @return mixed
*/
public function __get( $name ) {
if ( in_array( $name, $this->post_fields, true ) ) {
$post = get_post( $this->comment_post_ID );
return $post->$name;
}
}
}
/**
* Taxonomy API: WP_Taxonomy class
*
* @package WordPress
* @subpackage Taxonomy
* @since 4.7.0
*/
/**
* Core class used for interacting with taxonomies.
*
* @since 4.7.0
*/
final class WP_Taxonomy {
/**
* Taxonomy key.
*
* @since 4.7.0
* @var string
*/
public $name;
/**
* Name of the taxonomy shown in the menu. Usually plural.
*
* @since 4.7.0
* @var string
*/
public $label;
/**
* Labels object for this taxonomy.
*
* If not set, tag labels are inherited for non-hierarchical types
* and category labels for hierarchical ones.
*
* @see get_taxonomy_labels()
*
* @since 4.7.0
* @var stdClass
*/
public $labels;
/**
* A short descriptive summary of what the taxonomy is for.
*
* @since 4.7.0
* @var string
*/
public $description = '';
/**
* Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.
*
* @since 4.7.0
* @var bool
*/
public $public = true;
/**
* Whether the taxonomy is publicly queryable.
*
* @since 4.7.0
* @var bool
*/
public $publicly_queryable = true;
/**
* Whether the taxonomy is hierarchical.
*
* @since 4.7.0
* @var bool
*/
public $hierarchical = false;
/**
* Whether to generate and allow a UI for managing terms in this taxonomy in the admin.
*
* @since 4.7.0
* @var bool
*/
public $show_ui = true;
/**
* Whether to show the taxonomy in the admin menu.
*
* If true, the taxonomy is shown as a submenu of the object type menu. If false, no menu is shown.
*
* @since 4.7.0
* @var bool
*/
public $show_in_menu = true;
/**
* Whether the taxonomy is available for selection in navigation menus.
*
* @since 4.7.0
* @var bool
*/
public $show_in_nav_menus = true;
/**
* Whether to list the taxonomy in the tag cloud widget controls.
*
* @since 4.7.0
* @var bool
*/
public $show_tagcloud = true;
/**
* Whether to show the taxonomy in the quick/bulk edit panel.
*
* @since 4.7.0
* @var bool
*/
public $show_in_quick_edit = true;
/**
* Whether to display a column for the taxonomy on its post type listing screens.
*
* @since 4.7.0
* @var bool
*/
public $show_admin_column = false;
/**
* The callback function for the meta box display.
*
* @since 4.7.0
* @var bool|callable
*/
public $meta_box_cb = null;
/**
* The callback function for sanitizing taxonomy data saved from a meta box.
*
* @since 5.1.0
* @var callable
*/
public $meta_box_sanitize_cb = null;
/**
* An array of object types this taxonomy is registered for.
*
* @since 4.7.0
* @var array
*/
public $object_type = null;
/**
* Capabilities for this taxonomy.
*
* @since 4.7.0
* @var stdClass
*/
public $cap;
/**
* Rewrites information for this taxonomy.
*
* @since 4.7.0
* @var array|false
*/
public $rewrite;
/**
* Query var string for this taxonomy.
*
* @since 4.7.0
* @var string|false
*/
public $query_var;
/**
* Function that will be called when the count is updated.
*
* @since 4.7.0
* @var callable
*/
public $update_count_callback;
/**
* Whether this taxonomy should appear in the REST API.
*
* Default false. If true, standard endpoints will be registered with
* respect to $rest_base and $rest_controller_class.
*
* @since 4.7.4
* @var bool $show_in_rest
*/
public $show_in_rest;
/**
* The base path for this taxonomy's REST API endpoints.
*
* @since 4.7.4
* @var string|bool $rest_base
*/
public $rest_base;
/**
* The controller for this taxonomy's REST API endpoints.
*
* Custom controllers must extend WP_REST_Controller.
*
* @since 4.7.4
* @var string|bool $rest_controller_class
*/
public $rest_controller_class;
/**
* The controller instance for this taxonomy's REST API endpoints.
*
* Lazily computed. Should be accessed using {@see WP_Taxonomy::get_rest_controller()}.
*
* @since 5.5.0
* @var WP_REST_Controller $rest_controller
*/
public $rest_controller;
/**
* The default term name for this taxonomy. If you pass an array you have
* to set 'name' and optionally 'slug' and 'description'.
*
* @since 5.5.0
* @var array|string
*/
public $default_term;
/**
* Whether terms in this taxonomy should be sorted in the order they are provided to `wp_set_object_terms()`.
*
* Use this in combination with `'orderby' => 'term_order'` when fetching terms.
*
* @since 2.5.0
* @var bool|null
*/
public $sort = null;
/**
* Array of arguments to automatically use inside `wp_get_object_terms()` for this taxonomy.
*
* @since 2.6.0
* @var array|null
*/
public $args = null;
/**
* Whether it is a built-in taxonomy.
*
* @since 4.7.0
* @var bool
*/
public $_builtin;
/**
* Constructor.
*
* See the register_taxonomy() function for accepted arguments for `$args`.
*
* @since 4.7.0
*
* @global WP $wp Current WordPress environment instance.
*
* @param string $taxonomy Taxonomy key, must not exceed 32 characters.
* @param array|string $object_type Name of the object type for the taxonomy object.
* @param array|string $args Optional. Array or query string of arguments for registering a taxonomy.
* Default empty array.
*/
public function __construct( $taxonomy, $object_type, $args = array() ) {
$this->name = $taxonomy;
$this->set_props( $object_type, $args );
}
/**
* Sets taxonomy properties.
*
* See the register_taxonomy() function for accepted arguments for `$args`.
*
* @since 4.7.0
*
* @param array|string $object_type Name of the object type for the taxonomy object.
* @param array|string $args Array or query string of arguments for registering a taxonomy.
*/
public function set_props( $object_type, $args ) {
$args = wp_parse_args( $args );
/**
* Filters the arguments for registering a taxonomy.
*
* @since 4.4.0
*
* @param array $args Array of arguments for registering a taxonomy.
* See the register_taxonomy() function for accepted arguments.
* @param string $taxonomy Taxonomy key.
* @param string[] $object_type Array of names of object types for the taxonomy.
*/
$args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type );
$defaults = array(
'labels' => array(),
'description' => '',
'public' => true,
'publicly_queryable' => null,
'hierarchical' => false,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
'show_tagcloud' => null,
'show_in_quick_edit' => null,
'show_admin_column' => false,
'meta_box_cb' => null,
'meta_box_sanitize_cb' => null,
'capabilities' => array(),
'rewrite' => true,
'query_var' => $this->name,
'update_count_callback' => '',
'show_in_rest' => false,
'rest_base' => false,
'rest_controller_class' => false,
'default_term' => null,
'sort' => null,
'args' => null,
'_builtin' => false,
);
$args = array_merge( $defaults, $args );
// If not set, default to the setting for 'public'.
if ( null === $args['publicly_queryable'] ) {
$args['publicly_queryable'] = $args['public'];
}
if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) {
if ( true === $args['query_var'] ) {
$args['query_var'] = $this->name;
} else {
$args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
}
} else {
// Force 'query_var' to false for non-public taxonomies.
$args['query_var'] = false;
}
if ( false !== $args['rewrite'] && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
$args['rewrite'] = wp_parse_args(
$args['rewrite'],
array(
'with_front' => true,
'hierarchical' => false,
'ep_mask' => EP_NONE,
)
);
if ( empty( $args['rewrite']['slug'] ) ) {
$args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name );
}
}
// If not set, default to the setting for 'public'.
if ( null === $args['show_ui'] ) {
$args['show_ui'] = $args['public'];
}
// If not set, default to the setting for 'show_ui'.
if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
$args['show_in_menu'] = $args['show_ui'];
}
// If not set, default to the setting for 'public'.
if ( null === $args['show_in_nav_menus'] ) {
$args['show_in_nav_menus'] = $args['public'];
}
// If not set, default to the setting for 'show_ui'.
if ( null === $args['show_tagcloud'] ) {
$args['show_tagcloud'] = $args['show_ui'];
}
// If not set, default to the setting for 'show_ui'.
if ( null === $args['show_in_quick_edit'] ) {
$args['show_in_quick_edit'] = $args['show_ui'];
}
$default_caps = array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_posts',
);
$args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
unset( $args['capabilities'] );
$args['object_type'] = array_unique( (array) $object_type );
// If not set, use the default meta box.
if ( null === $args['meta_box_cb'] ) {
if ( $args['hierarchical'] ) {
$args['meta_box_cb'] = 'post_categories_meta_box';
} else {
$args['meta_box_cb'] = 'post_tags_meta_box';
}
}
$args['name'] = $this->name;
// Default meta box sanitization callback depends on the value of 'meta_box_cb'.
if ( null === $args['meta_box_sanitize_cb'] ) {
switch ( $args['meta_box_cb'] ) {
case 'post_categories_meta_box':
$args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_checkboxes';
break;
case 'post_tags_meta_box':
default:
$args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_input';
break;
}
}
// Default taxonomy term.
if ( ! empty( $args['default_term'] ) ) {
if ( ! is_array( $args['default_term'] ) ) {
$args['default_term'] = array( 'name' => $args['default_term'] );
}
$args['default_term'] = wp_parse_args(
$args['default_term'],
array(
'name' => '',
'slug' => '',
'description' => '',
)
);
}
foreach ( $args as $property_name => $property_value ) {
$this->$property_name = $property_value;
}
$this->labels = get_taxonomy_labels( $this );
$this->label = $this->labels->name;
}
/**
* Adds the necessary rewrite rules for the taxonomy.
*
* @since 4.7.0
*
* @global WP $wp Current WordPress environment instance.
*/
public function add_rewrite_rules() {
/* @var WP $wp */
global $wp;
// Non-publicly queryable taxonomies should not register query vars, except in the admin.
if ( false !== $this->query_var && $wp ) {
$wp->add_query_var( $this->query_var );
}
if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
if ( $this->hierarchical && $this->rewrite['hierarchical'] ) {
$tag = '(.+?)';
} else {
$tag = '([^/]+)';
}
add_rewrite_tag( "%$this->name%", $tag, $this->query_var ? "{$this->query_var}=" : "taxonomy=$this->name&term=" );
add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $this->rewrite );
}
}
/**
* Removes any rewrite rules, permastructs, and rules for the taxonomy.
*
* @since 4.7.0
*
* @global WP $wp Current WordPress environment instance.
*/
public function remove_rewrite_rules() {
/* @var WP $wp */
global $wp;
// Remove query var.
if ( false !== $this->query_var ) {
$wp->remove_query_var( $this->query_var );
}
// Remove rewrite tags and permastructs.
if ( false !== $this->rewrite ) {
remove_rewrite_tag( "%$this->name%" );
remove_permastruct( $this->name );
}
}
/**
* Registers the ajax callback for the meta box.
*
* @since 4.7.0
*/
public function add_hooks() {
add_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
}
/**
* Removes the ajax callback for the meta box.
*
* @since 4.7.0
*/
public function remove_hooks() {
remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
}
/**
* Gets the REST API controller for this taxonomy.
*
* Will only instantiate the controller class once per request.
*
* @since 5.5.0
*
* @return WP_REST_Controller|null The controller instance, or null if the taxonomy
* is set not to show in rest.
*/
public function get_rest_controller() {
if ( ! $this->show_in_rest ) {
return null;
}
$class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Terms_Controller::class;
if ( ! class_exists( $class ) ) {
return null;
}
if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
return null;
}
if ( ! $this->rest_controller ) {
$this->rest_controller = new $class( $this->name );
}
if ( ! ( $this->rest_controller instanceof $class ) ) {
return null;
}
return $this->rest_controller;
}
}
/**
* HTTP API: WP_Http class
*
* @package WordPress
* @subpackage HTTP
* @since 2.7.0
*/
if ( ! class_exists( 'Requests' ) ) {
require ABSPATH . WPINC . '/class-requests.php';
Requests::register_autoloader();
Requests::set_certificate_path( ABSPATH . WPINC . '/certificates/ca-bundle.crt' );
}
/**
* Core class used for managing HTTP transports and making HTTP requests.
*
* This class is used to consistently make outgoing HTTP requests easy for developers
* while still being compatible with the many PHP configurations under which
* WordPress runs.
*
* Debugging includes several actions, which pass different variables for debugging the HTTP API.
*
* @since 2.7.0
*/
class WP_Http {
// Aliases for HTTP response codes.
const HTTP_CONTINUE = 100;
const SWITCHING_PROTOCOLS = 101;
const PROCESSING = 102;
const EARLY_HINTS = 103;
const OK = 200;
const CREATED = 201;
const ACCEPTED = 202;
const NON_AUTHORITATIVE_INFORMATION = 203;
const NO_CONTENT = 204;
const RESET_CONTENT = 205;
const PARTIAL_CONTENT = 206;
const MULTI_STATUS = 207;
const IM_USED = 226;
const MULTIPLE_CHOICES = 300;
const MOVED_PERMANENTLY = 301;
const FOUND = 302;
const SEE_OTHER = 303;
const NOT_MODIFIED = 304;
const USE_PROXY = 305;
const RESERVED = 306;
const TEMPORARY_REDIRECT = 307;
const PERMANENT_REDIRECT = 308;
const BAD_REQUEST = 400;
const UNAUTHORIZED = 401;
const PAYMENT_REQUIRED = 402;
const FORBIDDEN = 403;
const NOT_FOUND = 404;
const METHOD_NOT_ALLOWED = 405;
const NOT_ACCEPTABLE = 406;
const PROXY_AUTHENTICATION_REQUIRED = 407;
const REQUEST_TIMEOUT = 408;
const CONFLICT = 409;
const GONE = 410;
const LENGTH_REQUIRED = 411;
const PRECONDITION_FAILED = 412;
const REQUEST_ENTITY_TOO_LARGE = 413;
const REQUEST_URI_TOO_LONG = 414;
const UNSUPPORTED_MEDIA_TYPE = 415;
const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
const EXPECTATION_FAILED = 417;
const IM_A_TEAPOT = 418;
const MISDIRECTED_REQUEST = 421;
const UNPROCESSABLE_ENTITY = 422;
const LOCKED = 423;
const FAILED_DEPENDENCY = 424;
const UPGRADE_REQUIRED = 426;
const PRECONDITION_REQUIRED = 428;
const TOO_MANY_REQUESTS = 429;
const REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
const UNAVAILABLE_FOR_LEGAL_REASONS = 451;
const INTERNAL_SERVER_ERROR = 500;
const NOT_IMPLEMENTED = 501;
const BAD_GATEWAY = 502;
const SERVICE_UNAVAILABLE = 503;
const GATEWAY_TIMEOUT = 504;
const HTTP_VERSION_NOT_SUPPORTED = 505;
const VARIANT_ALSO_NEGOTIATES = 506;
const INSUFFICIENT_STORAGE = 507;
const NOT_EXTENDED = 510;
const NETWORK_AUTHENTICATION_REQUIRED = 511;
/**
* Send an HTTP request to a URI.
*
* Please note: The only URI that are supported in the HTTP Transport implementation
* are the HTTP and HTTPS protocols.
*
* @since 2.7.0
*
* @param string $url The request URL.
* @param string|array $args {
* Optional. Array or string of HTTP request arguments.
*
* @type string $method Request method. Accepts 'GET', 'POST', 'HEAD', 'PUT', 'DELETE',
* 'TRACE', 'OPTIONS', or 'PATCH'.
* Some transports technically allow others, but should not be
* assumed. Default 'GET'.
* @type float $timeout How long the connection should stay open in seconds. Default 5.
* @type int $redirection Number of allowed redirects. Not supported by all transports
* Default 5.
* @type string $httpversion Version of the HTTP protocol to use. Accepts '1.0' and '1.1'.
* Default '1.0'.
* @type string $user-agent User-agent value sent.
* Default 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ).
* @type bool $reject_unsafe_urls Whether to pass URLs through wp_http_validate_url().
* Default false.
* @type bool $blocking Whether the calling code requires the result of the request.
* If set to false, the request will be sent to the remote server,
* and processing returned to the calling code immediately, the caller
* will know if the request succeeded or failed, but will not receive
* any response from the remote server. Default true.
* @type string|array $headers Array or string of headers to send with the request.
* Default empty array.
* @type array $cookies List of cookies to send with the request. Default empty array.
* @type string|array $body Body to send with the request. Default null.
* @type bool $compress Whether to compress the $body when sending the request.
* Default false.
* @type bool $decompress Whether to decompress a compressed response. If set to false and
* compressed content is returned in the response anyway, it will
* need to be separately decompressed. Default true.
* @type bool $sslverify Whether to verify SSL for the request. Default true.
* @type string $sslcertificates Absolute path to an SSL certificate .crt file.
* Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.
* @type bool $stream Whether to stream to a file. If set to true and no filename was
* given, it will be droped it in the WP temp dir and its name will
* be set using the basename of the URL. Default false.
* @type string $filename Filename of the file to write to when streaming. $stream must be
* set to true. Default null.
* @type int $limit_response_size Size in bytes to limit the response to. Default null.
*
* }
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error.
*/
public function request( $url, $args = array() ) {
$defaults = array(
'method' => 'GET',
/**
* Filters the timeout value for an HTTP request.
*
* @since 2.7.0
* @since 5.1.0 The `$url` parameter was added.
*
* @param float $timeout_value Time in seconds until a request times out. Default 5.
* @param string $url The request URL.
*/
'timeout' => apply_filters( 'http_request_timeout', 5, $url ),
/**
* Filters the number of redirects allowed during an HTTP request.
*
* @since 2.7.0
* @since 5.1.0 The `$url` parameter was added.
*
* @param int $redirect_count Number of redirects allowed. Default 5.
* @param string $url The request URL.
*/
'redirection' => apply_filters( 'http_request_redirection_count', 5, $url ),
/**
* Filters the version of the HTTP protocol used in a request.
*
* @since 2.7.0
* @since 5.1.0 The `$url` parameter was added.
*
* @param string $version Version of HTTP used. Accepts '1.0' and '1.1'. Default '1.0'.
* @param string $url The request URL.
*/
'httpversion' => apply_filters( 'http_request_version', '1.0', $url ),
/**
* Filters the user agent value sent with an HTTP request.
*
* @since 2.7.0
* @since 5.1.0 The `$url` parameter was added.
*
* @param string $user_agent WordPress user agent string.
* @param string $url The request URL.
*/
'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ), $url ),
/**
* Filters whether to pass URLs through wp_http_validate_url() in an HTTP request.
*
* @since 3.6.0
* @since 5.1.0 The `$url` parameter was added.
*
* @param bool $pass_url Whether to pass URLs through wp_http_validate_url(). Default false.
* @param string $url The request URL.
*/
'reject_unsafe_urls' => apply_filters( 'http_request_reject_unsafe_urls', false, $url ),
'blocking' => true,
'headers' => array(),
'cookies' => array(),
'body' => null,
'compress' => false,
'decompress' => true,
'sslverify' => true,
'sslcertificates' => ABSPATH . WPINC . '/certificates/ca-bundle.crt',
'stream' => false,
'filename' => null,
'limit_response_size' => null,
);
// Pre-parse for the HEAD checks.
$args = wp_parse_args( $args );
// By default, HEAD requests do not cause redirections.
if ( isset( $args['method'] ) && 'HEAD' === $args['method'] ) {
$defaults['redirection'] = 0;
}
$parsed_args = wp_parse_args( $args, $defaults );
/**
* Filters the arguments used in an HTTP request.
*
* @since 2.7.0
*
* @param array $parsed_args An array of HTTP request arguments.
* @param string $url The request URL.
*/
$parsed_args = apply_filters( 'http_request_args', $parsed_args, $url );
// The transports decrement this, store a copy of the original value for loop purposes.
if ( ! isset( $parsed_args['_redirection'] ) ) {
$parsed_args['_redirection'] = $parsed_args['redirection'];
}
/**
* Filters the preemptive return value of an HTTP request.
*
* Returning a non-false value from the filter will short-circuit the HTTP request and return
* early with that value. A filter should return one of:
*
* - An array containing 'headers', 'body', 'response', 'cookies', and 'filename' elements
* - A WP_Error instance
* - boolean false to avoid short-circuiting the response
*
* Returning any other value may result in unexpected behaviour.
*
* @since 2.9.0
*
* @param false|array|WP_Error $preempt A preemptive return value of an HTTP request. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
*/
$pre = apply_filters( 'pre_http_request', false, $parsed_args, $url );
if ( false !== $pre ) {
return $pre;
}
if ( function_exists( 'wp_kses_bad_protocol' ) ) {
if ( $parsed_args['reject_unsafe_urls'] ) {
$url = wp_http_validate_url( $url );
}
if ( $url ) {
$url = wp_kses_bad_protocol( $url, array( 'http', 'https', 'ssl' ) );
}
}
$arrURL = parse_url( $url );
if ( empty( $url ) || empty( $arrURL['scheme'] ) ) {
$response = new WP_Error( 'http_request_failed', __( 'A valid URL was not provided.' ) );
/** This action is documented in wp-includes/class-http.php */
do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
return $response;
}
if ( $this->block_request( $url ) ) {
$response = new WP_Error( 'http_request_not_executed', __( 'User has blocked requests through HTTP.' ) );
/** This action is documented in wp-includes/class-http.php */
do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
return $response;
}
// If we are streaming to a file but no filename was given drop it in the WP temp dir
// and pick its name using the basename of the $url.
if ( $parsed_args['stream'] ) {
if ( empty( $parsed_args['filename'] ) ) {
$parsed_args['filename'] = get_temp_dir() . basename( $url );
}
// Force some settings if we are streaming to a file and check for existence
// and perms of destination directory.
$parsed_args['blocking'] = true;
if ( ! wp_is_writable( dirname( $parsed_args['filename'] ) ) ) {
$response = new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) );
/** This action is documented in wp-includes/class-http.php */
do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
return $response;
}
}
if ( is_null( $parsed_args['headers'] ) ) {
$parsed_args['headers'] = array();
}
// WP allows passing in headers as a string, weirdly.
if ( ! is_array( $parsed_args['headers'] ) ) {
$processedHeaders = WP_Http::processHeaders( $parsed_args['headers'] );
$parsed_args['headers'] = $processedHeaders['headers'];
}
// Setup arguments.
$headers = $parsed_args['headers'];
$data = $parsed_args['body'];
$type = $parsed_args['method'];
$options = array(
'timeout' => $parsed_args['timeout'],
'useragent' => $parsed_args['user-agent'],
'blocking' => $parsed_args['blocking'],
'hooks' => new WP_HTTP_Requests_Hooks( $url, $parsed_args ),
);
// Ensure redirects follow browser behaviour.
$options['hooks']->register( 'requests.before_redirect', array( get_class(), 'browser_redirect_compatibility' ) );
// Validate redirected URLs.
if ( function_exists( 'wp_kses_bad_protocol' ) && $parsed_args['reject_unsafe_urls'] ) {
$options['hooks']->register( 'requests.before_redirect', array( get_class(), 'validate_redirects' ) );
}
if ( $parsed_args['stream'] ) {
$options['filename'] = $parsed_args['filename'];
}
if ( empty( $parsed_args['redirection'] ) ) {
$options['follow_redirects'] = false;
} else {
$options['redirects'] = $parsed_args['redirection'];
}
// Use byte limit, if we can.
if ( isset( $parsed_args['limit_response_size'] ) ) {
$options['max_bytes'] = $parsed_args['limit_response_size'];
}
// If we've got cookies, use and convert them to Requests_Cookie.
if ( ! empty( $parsed_args['cookies'] ) ) {
$options['cookies'] = WP_Http::normalize_cookies( $parsed_args['cookies'] );
}
// SSL certificate handling.
if ( ! $parsed_args['sslverify'] ) {
$options['verify'] = false;
$options['verifyname'] = false;
} else {
$options['verify'] = $parsed_args['sslcertificates'];
}
// All non-GET/HEAD requests should put the arguments in the form body.
if ( 'HEAD' !== $type && 'GET' !== $type ) {
$options['data_format'] = 'body';
}
/**
* Filters whether SSL should be verified for non-local requests.
*
* @since 2.8.0
* @since 5.1.0 The `$url` parameter was added.
*
* @param bool $ssl_verify Whether to verify the SSL connection. Default true.
* @param string $url The request URL.
*/
$options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'], $url );
// Check for proxies.
$proxy = new WP_HTTP_Proxy();
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
$options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' . $proxy->port() );
if ( $proxy->use_authentication() ) {
$options['proxy']->use_authentication = true;
$options['proxy']->user = $proxy->username();
$options['proxy']->pass = $proxy->password();
}
}
// Avoid issues where mbstring.func_overload is enabled.
mbstring_binary_safe_encoding();
try {
$requests_response = Requests::request( $url, $headers, $data, $type, $options );
// Convert the response into an array.
$http_response = new WP_HTTP_Requests_Response( $requests_response, $parsed_args['filename'] );
$response = $http_response->to_array();
// Add the original object to the array.
$response['http_response'] = $http_response;
} catch ( Requests_Exception $e ) {
$response = new WP_Error( 'http_request_failed', $e->getMessage() );
}
reset_mbstring_encoding();
/**
* Fires after an HTTP API response is received and before the response is returned.
*
* @since 2.8.0
*
* @param array|WP_Error $response HTTP response or WP_Error object.
* @param string $context Context under which the hook is fired.
* @param string $class HTTP transport used.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
*/
do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
if ( is_wp_error( $response ) ) {
return $response;
}
if ( ! $parsed_args['blocking'] ) {
return array(
'headers' => array(),
'body' => '',
'response' => array(
'code' => false,
'message' => false,
),
'cookies' => array(),
'http_response' => null,
);
}
/**
* Filters the HTTP API response immediately before the response is returned.
*
* @since 2.9.0
*
* @param array $response HTTP response.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
*/
return apply_filters( 'http_response', $response, $parsed_args, $url );
}
/**
* Normalizes cookies for using in Requests.
*
* @since 4.6.0
*
* @param array $cookies Array of cookies to send with the request.
* @return Requests_Cookie_Jar Cookie holder object.
*/
public static function normalize_cookies( $cookies ) {
$cookie_jar = new Requests_Cookie_Jar();
foreach ( $cookies as $name => $value ) {
if ( $value instanceof WP_Http_Cookie ) {
$cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $value->get_attributes(), array( 'host-only' => $value->host_only ) );
} elseif ( is_scalar( $value ) ) {
$cookie_jar[ $name ] = new Requests_Cookie( $name, $value );
}
}
return $cookie_jar;
}
/**
* Match redirect behaviour to browser handling.
*
* Changes 302 redirects from POST to GET to match browser handling. Per
* RFC 7231, user agents can deviate from the strict reading of the
* specification for compatibility purposes.
*
* @since 4.6.0
*
* @param string $location URL to redirect to.
* @param array $headers Headers for the redirect.
* @param string|array $data Body to send with the request.
* @param array $options Redirect request options.
* @param Requests_Response $original Response object.
*/
public static function browser_redirect_compatibility( $location, $headers, $data, &$options, $original ) {
// Browser compatibility.
if ( 302 === $original->status_code ) {
$options['type'] = Requests::GET;
}
}
/**
* Validate redirected URLs.
*
* @since 4.7.5
*
* @throws Requests_Exception On unsuccessful URL validation.
* @param string $location URL to redirect to.
*/
public static function validate_redirects( $location ) {
if ( ! wp_http_validate_url( $location ) ) {
throw new Requests_Exception( __( 'A valid URL was not provided.' ), 'wp_http.redirect_failed_validation' );
}
}
/**
* Tests which transports are capable of supporting the request.
*
* @since 3.2.0
*
* @param array $args Request arguments.
* @param string $url URL to request.
* @return string|false Class name for the first transport that claims to support the request.
* False if no transport claims to support the request.
*/
public function _get_first_available_transport( $args, $url = null ) {
$transports = array( 'curl', 'streams' );
/**
* Filters which HTTP transports are available and in what order.
*
* @since 3.7.0
*
* @param string[] $transports Array of HTTP transports to check. Default array contains
* 'curl' and 'streams', in that order.
* @param array $args HTTP request arguments.
* @param string $url The URL to request.
*/
$request_order = apply_filters( 'http_api_transports', $transports, $args, $url );
// Loop over each transport on each HTTP request looking for one which will serve this request's needs.
foreach ( $request_order as $transport ) {
if ( in_array( $transport, $transports, true ) ) {
$transport = ucfirst( $transport );
}
$class = 'WP_Http_' . $transport;
// Check to see if this transport is a possibility, calls the transport statically.
if ( ! call_user_func( array( $class, 'test' ), $args, $url ) ) {
continue;
}
return $class;
}
return false;
}
/**
* Dispatches a HTTP request to a supporting transport.
*
* Tests each transport in order to find a transport which matches the request arguments.
* Also caches the transport instance to be used later.
*
* The order for requests is cURL, and then PHP Streams.
*
* @since 3.2.0
* @deprecated 5.1.0 Use WP_Http::request()
* @see WP_Http::request()
*
* @param string $url URL to request.
* @param array $args Request arguments.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error.
*/
private function _dispatch_request( $url, $args ) {
static $transports = array();
$class = $this->_get_first_available_transport( $args, $url );
if ( ! $class ) {
return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) );
}
// Transport claims to support request, instantiate it and give it a whirl.
if ( empty( $transports[ $class ] ) ) {
$transports[ $class ] = new $class;
}
$response = $transports[ $class ]->request( $url, $args );
/** This action is documented in wp-includes/class-http.php */
do_action( 'http_api_debug', $response, 'response', $class, $args, $url );
if ( is_wp_error( $response ) ) {
return $response;
}
/** This filter is documented in wp-includes/class-http.php */
return apply_filters( 'http_response', $response, $args, $url );
}
/**
* Uses the POST HTTP method.
*
* Used for sending data that is expected to be in the body.
*
* @since 2.7.0
*
* @param string $url The request URL.
* @param string|array $args Optional. Override the defaults.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error.
*/
public function post( $url, $args = array() ) {
$defaults = array( 'method' => 'POST' );
$parsed_args = wp_parse_args( $args, $defaults );
return $this->request( $url, $parsed_args );
}
/**
* Uses the GET HTTP method.
*
* Used for sending data that is expected to be in the body.
*
* @since 2.7.0
*
* @param string $url The request URL.
* @param string|array $args Optional. Override the defaults.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error.
*/
public function get( $url, $args = array() ) {
$defaults = array( 'method' => 'GET' );
$parsed_args = wp_parse_args( $args, $defaults );
return $this->request( $url, $parsed_args );
}
/**
* Uses the HEAD HTTP method.
*
* Used for sending data that is expected to be in the body.
*
* @since 2.7.0
*
* @param string $url The request URL.
* @param string|array $args Optional. Override the defaults.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error.
*/
public function head( $url, $args = array() ) {
$defaults = array( 'method' => 'HEAD' );
$parsed_args = wp_parse_args( $args, $defaults );
return $this->request( $url, $parsed_args );
}
/**
* Parses the responses and splits the parts into headers and body.
*
* @since 2.7.0
*
* @param string $strResponse The full response string.
* @return array {
* Array with response headers and body.
*
* @type string $headers HTTP response headers.
* @type string $body HTTP response body.
* }
*/
public static function processResponse( $strResponse ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$res = explode( "\r\n\r\n", $strResponse, 2 );
return array(
'headers' => $res[0],
'body' => isset( $res[1] ) ? $res[1] : '',
);
}
/**
* Transforms header string into an array.
*
* @since 2.7.0
*
* @param string|array $headers The original headers. If a string is passed, it will be converted
* to an array. If an array is passed, then it is assumed to be
* raw header data with numeric keys with the headers as the values.
* No headers must be passed that were already processed.
* @param string $url Optional. The URL that was requested. Default empty.
* @return array {
* Processed string headers. If duplicate headers are encountered,
* then a numbered array is returned as the value of that header-key.
*
* @type array $response {
* @type int $code The response status code. Default 0.
* @type string $message The response message. Default empty.
* }
* @type array $newheaders The processed header data as a multidimensional array.
* @type WP_Http_Cookie[] $cookies If the original headers contain the 'Set-Cookie' key,
* an array containing `WP_Http_Cookie` objects is returned.
* }
*/
public static function processHeaders( $headers, $url = '' ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
// Split headers, one per array element.
if ( is_string( $headers ) ) {
// Tolerate line terminator: CRLF = LF (RFC 2616 19.3).
$headers = str_replace( "\r\n", "\n", $headers );
/*
* Unfold folded header fields. LWS = [CRLF] 1*( SP | HT )