/** * Custom List Users * * Custom function to generate a list of blog authors, * built based on WordPress' native wp_list_users function. * * @since Grammatizator 0.6 */ function gramm_list_authors($args = '') { // SET up variables global $wpdb; $defaults = array('orderby' => 'name', 'order' => 'ASC', 'role' => '', 'include' => array(), 'biolength' => 55, 'heading_tag' => 'h3', 'echo' => true); $args = wp_parse_args($args, $defaults); $return = ''; // GET data from database with database query // Use this to get the paramenters we need for get_users() out of the default $args $query_args = wp_array_slice_assoc($args, array('orderby', 'order', 'role', 'include')); // Used to request only an array of user IDs from get_users() $query_args['fields'] = 'ids'; $authors = get_users($query_args); // STEP through the data foreach ($authors as $authorid) { // GET individual user data and save in temporary variables $author = get_userdata($authorid); /* Test me */ // print_r(); // First Last name // Bio // Twitter handle // Link to users's posts // Image (get later) // BUILD output } // End foreach loop // OUTPUT results }
function get_image($image_id) { $attachment_data = wp_prepare_attachment_for_js($image_id); $image = wp_array_slice_assoc($attachment_data, array('sizes', 'caption', 'description')); if (empty($image)) { return false; } foreach ($this->sizes as $size) { $size_name = $size; if (!isset($image['sizes'][$size])) { $size = 'full'; $image['error'] = "wrong-size"; } // Format Data $image[$size_name]['img'] = $image['sizes'][$size]['url']; $image[$size_name]['width'] = $image['sizes'][$size]['width']; $image[$size_name]['height'] = $image['sizes'][$size]['height']; } $image['id'] = $image_id; $image['desc'] = $image['description']; unset($image['sizes'], $image['description']); if (!$this->has_descriptions && !empty($image['description'])) { $this->has_descriptions = true; } $image['desc'] = htmlspecialchars(str_replace('"', '"', $image['desc']), ENT_QUOTES); $image['caption'] = htmlspecialchars(str_replace('"', '"', $image['caption']), ENT_QUOTES); return $image; }
/** * Output the exchange rates * This is the shortcode callback function */ function cbnr_the_exchange($attrs = '') { $rates = cbnr_get_rates(); if (is_array($attrs)) { foreach ($attrs as &$atr) { $atr = strtolower($atr); } unset($atr); $rates = wp_array_slice_assoc($rates, $attrs); } if (empty($rates)) { return; } $html = ''; if (!is_array($attrs) || !in_array('nocss', $attrs)) { $html .= '<style type="text/css">@import url("' . plugins_url('style.css', __FILE__) . '");</style>'; } $html .= '<div class="cbnr">'; $html .= ' <h3>Curs Valutar <img src="' . cbnr_get_icon_url('ron') . '"></h3>'; $html .= ' <div class="cbnr_date">' . date('j F Y') . '</div>'; foreach ($rates as $key => $value) { $html .= ' <div class="cbnr_row">'; $html .= ' <div class="cnbr_flag"><img src="' . cbnr_get_icon_url($key) . '" title="' . strtoupper($key) . '"></div>'; $html .= ' <div class="cbnr_currency">' . strtoupper($key) . '</div>'; $html .= ' <div class="cbnr_value">' . $value . ' RON</div>'; $html .= ' </div>'; } $html .= ' <span class="cbnr_credits">Curs oferit de <a href="http://www.bnro.ro">Banca Națională a României</a></span>'; $html .= '</div>'; return $html; }
/** * Custom List Users * * Custom function to generate a list of blog authors, * built based on WordPress' native wp_list_users function. * * @since Grammatizator 0.6 */ function gramm_list_authors($args = '') { // SET up variables global $wpdb; $defaults = array('orderby' => 'name', 'order' => 'ASC', 'role' => '', 'include' => array(), 'biolength' => 55, 'heading_tag' => 'h5', 'echo' => true); $args = wp_parse_args($args, $defaults); $return = ''; // GET data from database with database query // Use this to get the paramenters we need for get_users() out of the default $args $query_args = wp_array_slice_assoc($args, array('orderby', 'order', 'role', 'include')); // Used to request only an array of user IDs from get_users() $query_args['fields'] = 'ids'; $authors = get_users($query_args); // STEP through the data foreach ($authors as $authorid) { // GET individual user data and save in temporary variables $author = get_userdata($authorid); // First Last name if ($author->first_name && $author->last_name) { $name = "{$author->first_name} {$author->last_name}"; } else { $name = $author->display_name; } // Bio if (get_the_author_meta('description', $author->ID)) { $bio = get_the_author_meta('description', $author->ID); } else { $bio = ''; } // Twitter handle if (get_the_author_meta('twitter', $author->ID)) { $twit = '<p class="social-links"><a class="twitter" href="https://twitter.com/' . get_the_author_meta('twitter', $author->ID) . '" title="' . esc_attr(sprintf(__("%s on Twitter"), $author->display_name)) . '">@' . get_the_author_meta('twitter', $author->ID) . '</a></p>'; } else { $twit = ''; } // Link to users's posts $authorlink = '<a class="fn" href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr(sprintf(__("Posts by %s"), $author->display_name)) . '">' . $name . '</a>'; // BUILD output $return .= '<section class="author byline vcard">'; // Image $return .= '<div class="avatar-wrap avatar-size-90">'; $return .= get_avatar($author->ID, 90); $return .= '</div>'; $return .= '<div class="bio-wrap">'; $return .= '<' . $args['heading_tag'] . '>' . $authorlink . '</' . $args['heading_tag'] . '>'; $return .= $twit; if ($args['biolength'] > 0 && $bio) { $return .= '<p class="author-bio">'; $return .= wptexturize($bio); $return .= '</p>'; } $return .= '</div><!-- close .bio-wrap -->'; $return .= '</section>'; // End output } // End foreach loop // OUTPUT results // guesses? }
static function user_query($query) { if (isset($query->_p2p_capture)) { return; } // Don't overwrite existing P2P query if (isset($query->query_vars['connected_type'])) { return; } _p2p_append($query->query_vars, wp_array_slice_assoc($_GET, P2P_URL_Query::get_custom_qv())); }
/** * Gather the parameters passed to client JavaScript via JSON. * * @since 4.1.0 * @return array The array to be exported to the client as JSON. */ public function json() { $array = wp_array_slice_assoc((array) $this, array('id', 'title', 'description', 'priority', 'type')); $array['content'] = $this->get_content(); $array['active'] = $this->active(); $array['instanceNumber'] = $this->instance_number; // BEGIN Redux Additions $array['width'] = isset($this->section['customizer_width']) ? $this->section['customizer_width'] : ''; $array['icon'] = isset($this->section['icon']) && !empty($this->section['icon']) ? $this->section['icon'] : 'hide'; // EMD Redux Additions return $array; }
static function get_url($context = 'display', $redirect_to = '') { $args = wp_array_slice_assoc($_GET, array('checkemail', 'registration', 'loggedout')); if (!empty($redirect_to)) { $args['redirect_to'] = urlencode($redirect_to); } if ($page_id = self::get_id()) { $permalink = get_permalink($page_id); } else { $permalink = site_url('wp-login.php'); } return esc_url(add_query_arg($args, $permalink), null, $context); }
/** * Get the language to filter a comments query * * @since 2.0 * * @param object $query * @return object|bool the language(s) to use in the filter, false otherwise */ protected function get_comments_queried_language($query) { // Don't filter comments if comment ids or post ids are specified $plucked = wp_array_slice_assoc($query->query_vars, array('comment__in', 'parent', 'post_id', 'post__in', 'post_parent')); $fields = array_filter($plucked); if (!empty($fields)) { return false; } // Don't filter comments if a non translated post type is specified if (!empty($query->query_vars['post_type']) && !$this->model->is_translated_post_type($query->query_vars['post_type'])) { return false; } return empty($query->query_vars['lang']) ? $this->curlang : $this->model->get_language($query->query_vars['lang']); }
/** * Run a search query. * * @since 1.5 * @uses apply_filters() geo_mashup_search_query_args Filter the geo query arguments. * * @param string|array $args Search parameters. * @return array Search results. **/ public function query($args) { $default_args = array('object_name' => 'post', 'object_ids' => null, 'exclude_object_ids' => null, 'units' => 'km', 'location_text' => '', 'radius' => null, 'sort' => 'distance_km ASC'); $this->query_vars = wp_parse_args($args, $default_args); /** @var $units */ extract($this->query_vars); $this->results = array(); $this->result_count = 0; $this->result = null; $this->current_result = -1; $this->units = $units; $this->max_km = 20000; $this->distance_factor = 'km' == $units ? 1 : self::MILES_PER_KILOMETER; $this->near_location = GeoMashupDB::blank_location(ARRAY_A); $geo_query_args = wp_array_slice_assoc($this->query_vars, array('object_name', 'sort', 'exclude_object_ids', 'limit')); if (!empty($near_lat) and !empty($near_lng)) { $this->near_location['lat'] = $near_lat; $this->near_location['lng'] = $near_lng; } else { if (!empty($location_text)) { $geocode_text = empty($geolocation) ? $location_text : $geolocation; if (!GeoMashupDB::geocode($geocode_text, $this->near_location)) { // No search center was found, we can't continue return $this->results; } } else { // No coordinates to search near return $this->results; } } $radius_km = $this->max_km; if (!empty($radius)) { $radius_km = abs($radius) / $this->distance_factor; } $geo_query_args['radius_km'] = $radius_km; $geo_query_args['near_lat'] = $this->near_location['lat']; $geo_query_args['near_lng'] = $this->near_location['lng']; if (isset($map_cat)) { $geo_query_args['map_cat'] = $map_cat; } $geo_query_args = apply_filters('geo_mashup_search_query_args', $geo_query_args); $this->results = GeoMashupDB::get_object_locations($geo_query_args); $this->result_count = count($this->results); if ($this->result_count > 0) { $this->max_km = $this->results[$this->result_count - 1]->distance_km; } else { $this->max_km = $radius_km; } return $this->results; }
/** * Build it. * * @uses wp_array_slice_assoc() * @uses wp_parse_args() * * @since 1.0.0 * * @param array $args The array of arguments. */ public function __construct($args = array()) { if (!isset($args['api_key'], $args['latitude'], $args['longitude'])) { return false; } // Limit the arguments keys listed in the default array, then parse the submitted arguments $limit_keys = array_keys($this->defaults); $args = wp_array_slice_assoc($args, $limit_keys); $this->args = wp_parse_args($args, $this->defaults); // Build the query arguments for the forecast url $query = !empty($this->query) && is_array($this->query) ? '?' . http_build_query($this->query) : ''; // Build the request url $this->request_url = self::API_ENDPOINT . esc_attr($this->api_key) . '/' . floatval($this->latitude) . ',' . floatval($this->longitude) . (is_null($this->time) ? '' : ',' . $this->time) . $query; // Get and save the response $this->response = $this->get_response($this->clear_cache); }
/** * Custom List Users * * Custom function to generate a list of blog authors, * built based on WordPress' native wp_list_users function. * * @since Grammatizator 0.6 */ function gramm_list_authors($args = '') { // SET up variables global $wpdb; $defaults = array('orderby' => 'name', 'order' => 'ASC', 'role' => '', 'include' => array(), 'biolength' => 55, 'heading_tag' => 'h3', 'echo' => true); $args = wp_parse_args($args, $defaults); $return = ''; // GET data from database with database query // Use this to get the paramenters we need for get_users() out of the default $args $query_args = wp_array_slice_assoc($args, array('orderby', 'order', 'role', 'include')); // Used to request only an array of user IDs from get_users() $query_args['fields'] = 'ids'; $authors = get_users($query_args); // STEP through the data foreach ($authors as $authorid) { // GET individual user data and save in temporary variables $author = get_userdata($authorid); // First Last name if ($author->first_name && $author->last_name) { $name = "{$author->first_name} {$author->last_name}"; } else { $name = $author->display_name; } // Bio if (get_the_author_meta('description', $author->ID)) { $bio = get_the_author_meta('description', $author->ID); } else { $bio = ''; } // Twitter handle if (get_the_author_meta('twitter', $author->ID)) { $twit = '<p class="social-links"><a class="twitter" href="https://twitter.com/' . get_the_author_meta('twitter', $author->ID) . '" title="' . esc_attr(sprintf(__("%s on Twitter"), $author->display_name)) . '">@' . get_the_author_meta('twitter', $author->ID) . '</a></p>'; } else { $twit = ''; } // Link to users's posts $authorlink = '<a class="fn" href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr(sprintf(__("Posts by %s"), $author->display_name)) . '">' . $name . '</a>'; /* Test me INSIDE the loop */ // TESTHERE // Image (get later) // BUILD output } // End foreach loop /* Test me OUTSIDE the loop too! */ // TESTHERE // OUTPUT results }
public static function register($args) { if (isset($args['name'])) { if (strlen($args['name']) > 44) { trigger_error(sprintf("Connection name '%s' is longer than 44 characters.", $args['name']), E_USER_WARNING); return false; } } else { trigger_error("Connection types without a 'name' parameter are deprecated.", E_USER_WARNING); } $args = wp_parse_args($args, array('name' => false, 'from_object' => 'post', 'to_object' => 'post', 'from' => 'post', 'to' => 'post', 'from_query_vars' => array(), 'to_query_vars' => array(), 'data' => array(), 'cardinality' => 'many-to-many', 'prevent_duplicates' => true, 'self_connections' => false, 'sortable' => false, 'title' => array(), 'from_labels' => '', 'to_labels' => '', 'reciprocal' => false)); $sides = array(); foreach (array('from', 'to') as $direction) { $object = _p2p_pluck($args, $direction); if ('user' == $object) { $args[$direction . '_object'] = 'user'; } elseif ('attachment' == $object) { $args[$direction . '_object'] = 'attachment'; } if ('post' == $args[$direction . '_object']) { $validated = array(); foreach ((array) $object as $ptype) { if (!post_type_exists($ptype)) { trigger_error("Post type '{$ptype}' is not defined."); } else { $validated[] = $ptype; } } if (empty($validated)) { $validated = array('post'); } $args[$direction . '_query_vars']['post_type'] = $validated; } } if (!$args['name']) { $args['name'] = md5(serialize(array_values(wp_array_slice_assoc($args, array('from_object', 'to_object', 'from_query_vars', 'to_query_vars', 'data'))))); } $args = apply_filters('p2p_connection_type_args', $args); $ctype = new P2P_Connection_Type($args); if (isset(self::$instances[$ctype->name])) { trigger_error("Connection type '{$ctype->name}' is already defined.", E_USER_NOTICE); } self::$instances[$ctype->name] = $ctype; return $ctype; }
static function user_query($query) { if (!function_exists('get_current_screen')) { return; } $current_screen = get_current_screen(); if ($current_screen && 'users' != $current_screen->id) { return; } if (isset($query->_p2p_capture)) { return; } // Don't overwrite existing P2P query if (isset($query->query_vars['connected_type'])) { return; } _p2p_append($query->query_vars, wp_array_slice_assoc($_GET, self::get_custom_qv())); }
/** * Gather the parameters passed to client JavaScript via JSON. * * @since 4.1.0 * @return array The array to be exported to the client as JSON. */ public function json() { $array = wp_array_slice_assoc((array) $this, array('id', 'title', 'description', 'priority', 'panel', 'type')); $array['content'] = $this->get_content(); $array['active'] = $this->active(); $array['instanceNumber'] = $this->instance_number; if ($this->panel) { /* translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ $array['customizeAction'] = sprintf(__('Customizing ▸ %s', 'redux-framework'), esc_html($this->manager->get_panel($this->panel)->title)); } else { $array['customizeAction'] = __('Customizing', 'redux-framework'); } // BEGIN Redux Additions $array['width'] = isset($this->section['customizer_width']) ? $this->section['customizer_width'] : ''; $array['icon'] = isset($this->section['icon']) && !empty($this->section['icon']) ? $this->section['icon'] : 'hide'; // EMD Redux Additions return $array; }
function stt2extat_default_setting($option = '') { global $stt2extat_sanitize; $args = $stt2extat_sanitize->sanitize(); switch ($option) { case 'update': return $args; break; case 'shortcode': $default = array('text_header', 'html_heading', 'number', 'display', 'count', 'convert'); $args = wp_array_slice_assoc($args, $default); return $args; break; default: $args = apply_filters('stt2extat_default_settings', $args); return $args; break; } }
function wpex_active_skin_class_file() { // Get active skin $active_skin = wpex_active_skin(); // Lets bail if the active skin is the base skin if ('base' == $active_skin || !$active_skin) { return; } // Get currect skin class to load later $skins = wpex_skins(); $active_skin_array = wp_array_slice_assoc($skins, array($active_skin)); if (is_array($active_skin_array)) { $is_core = !empty($active_skin_array[$active_skin]['core']) ? true : false; $class_file = !empty($active_skin_array[$active_skin]['class']) ? $active_skin_array[$active_skin]['class'] : false; } // Return class file if one exists if ($is_core && $class_file) { return $class_file; } }
public static function register($args) { $args = wp_parse_args($args, array('name' => false, 'from_object' => 'post', 'to_object' => 'post', 'from' => 'post', 'to' => 'post', 'from_query_vars' => array(), 'to_query_vars' => array(), 'data' => array(), 'cardinality' => 'many-to-many', 'prevent_duplicates' => true, 'sortable' => false, 'title' => array(), 'from_labels' => '', 'to_labels' => '', 'reciprocal' => false)); $sides = array(); foreach (array('from', 'to') as $direction) { $object = _p2p_pluck($args, $direction); if ('user' == $object) { $args[$direction . '_object'] = 'user'; } elseif ('attachment' == $object) { $args[$direction . '_object'] = 'attachment'; } if ('post' == $args[$direction . '_object']) { $validated = array(); foreach ((array) $object as $ptype) { if (!post_type_exists($ptype)) { trigger_error("Post type '{$ptype}' is not defined."); } else { $validated[] = $ptype; } } if (empty($validated)) { $validated = array('post'); } $args[$direction . '_query_vars']['post_type'] = $validated; } } if (!$args['name']) { $args['name'] = md5(serialize(array_values(wp_array_slice_assoc($args, array('from_object', 'to_object', 'from_query_vars', 'to_query_vars', 'data'))))); } if ($args['from_object'] == $args['to_object'] && 'post' == $args['from_object']) { $class = 'P2P_Connection_Type'; } else { $class = 'Generic_Connection_Type'; } $ctype = new $class($args); if (isset(self::$instances[$ctype->name])) { trigger_error('Connection type is already defined.', E_USER_NOTICE); } self::$instances[$ctype->name] = $ctype; return $ctype; }
/** * Instantiate SCB framework classes. */ public static function action_plugins_loaded() { $invite_subject = sprintf(__('You\'re invited to subscribe to %s', 'Postmatic'), get_option('blogname')); $invite_intro = __('This is an invitation to subscribe to email updates from this website. We hope it is welcome, but we promise we won\'t contact you again unless you respond.', 'Postmatic'); $subscribed_introduction = '<h2>' . __('Thanks for signing up!', 'Postmatic') . '</h2>' . '<p>' . __('We\'re glad you\'ve decided to join and hope you enjoy our posts.', 'Postmatic') . '</p>'; $default_options = array('auto_subscribe_authors' => true, 'prompt_key' => '', 'site_subscription_post_types' => array('post'), 'skip_notices' => array(), 'skip_widget_intro' => false, 'skip_akismet_intro' => false, 'skip_zero_spam_intro' => false, 'skip_local_mail_intro' => false, 'skip_moderation_user_intro' => false, 'redirect_to_options_page' => true, 'send_login_info' => false, 'email_header_type' => Prompt_Enum_Email_Header_Types::TEXT, 'email_header_image' => 0, 'email_header_text' => get_option('blogname'), 'email_footer_type' => Prompt_Enum_Email_Footer_Types::WIDGETS, 'email_footer_text' => '', 'email_footer_credit' => true, 'plan' => '', 'email_transport' => Prompt_Enum_Email_Transports::LOCAL, 'messages' => array('welcome' => __('Welcome!', 'Postmatic')), 'invite_subject' => $invite_subject, 'invite_introduction' => $invite_intro, 'last_version' => 0, 'enable_collection' => false, 'site_icon' => 0, 'no_post_featured_image_default' => false, 'no_post_email_default' => false, 'enabled_message_types' => array(), 'excerpt_default' => false, 'comment_opt_in_default' => false, 'comment_opt_in_text' => __('Continue this conversation via email', 'Postmatic'), 'comment_flood_control_trigger_count' => 6, 'upgrade_required' => false, 'enable_optins' => false, 'enable_skimlinks' => false, 'skimlinks_publisher_id' => '', 'emails_per_chunk' => 25, 'enable_digests' => false, 'digest_plans' => array(), 'site_styles' => array(), 'enable_invites' => false, 'enable_mailchimp_import' => false, 'enable_jetpack_import' => false, 'enable_mailpoet_import' => false, 'enable_post_delivery' => true, 'enable_comment_delivery' => true, 'subscribed_introduction' => $subscribed_introduction, 'connected' => false, 'scr_import_done' => false); $default_options = array_merge($default_options, Prompt_Optins::options_fields()); self::prevent_options_errors(); self::$options = new scbOptions('prompt_options', __FILE__, $default_options); /** * Filter overridden options. * * @param array $overridden_options * @param array $current_options */ $filtered_options = apply_filters('prompt/override_options', array(), self::$options->get()); self::$overridden_options = wp_array_slice_assoc($filtered_options, array_keys(self::$options->get())); if (!empty(self::$overridden_options)) { self::$options->set(self::$overridden_options); } // Until we have a key we won't do much $key = self::$options->get('prompt_key'); if ($key) { self::add_hooks(); } if (is_admin()) { self::settings_page(); self::delivery_metabox(); self::text_metabox(); self::$activate_notice = new Prompt_Admin_Activate_Notice($key, self::$settings_page); } /** * Fires when Postmatic has loaded. * * This happens after plugins are loaded {@see 'plugins_loaded'}, and always fires when Postmatic is active. * * @since 1.0.0 */ do_action('prompt/core_loaded'); }
/** * filters the comments according to the current language * used by the recent comments widget and admin language filter * * @since 0.2 * * @param array $clauses sql clauses * @param object $query WP_Comment_Query object * @return array modified $clauses */ public function comments_clauses($clauses, $query) { global $wpdb; // don't filter comments if comment ids or post ids are specified $plucked = wp_array_slice_assoc($query->query_vars, array('comment__in', 'parent', 'post_id', 'post__in', 'post_parent')); $fields = array_filter($plucked); if (!empty($fields)) { return $clauses; } // don't filter comments if a non translated post type is specified if (!empty($query->query_vars['post_type']) && !$this->model->is_translated_post_type($query->query_vars['post_type'])) { return $clauses; } $lang = empty($query->query_vars['lang']) ? $this->curlang : $this->model->get_language($query->query_vars['lang']); if (!empty($lang)) { // if this clause is not already added by WP if (!strpos($clauses['join'], '.ID')) { $clauses['join'] .= " JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID"; } $clauses['join'] .= $this->model->post->join_clause(); $clauses['where'] .= $this->model->post->where_clause($lang); } return $clauses; }
/** * Create dropdown HTML content of users. * * The content can either be displayed, which it is by default or retrieved by * setting the 'echo' argument. The 'include' and 'exclude' arguments do not * need to be used; all users will be displayed in that case. Only one can be * used, either 'include' or 'exclude', but not both. * * The available arguments are as follows: * * @since 2.3.0 * * @global int $blog_id * * @param array|string $args { * Optional. Array or string of arguments to generate a drop-down of users. * {@see WP_User_Query::prepare_query() for additional available arguments. * * @type string $show_option_all Text to show as the drop-down default (all). * Default empty. * @type string $show_option_none Text to show as the drop-down default when no * users were found. Default empty. * @type int|string $option_none_value Value to use for $show_option_non when no users * were found. Default -1. * @type string $hide_if_only_one_author Whether to skip generating the drop-down * if only one user was found. Default empty. * @type string $orderby Field to order found users by. Accepts user fields. * Default 'display_name'. * @type string $order Whether to order users in ascending or descending * order. Accepts 'ASC' (ascending) or 'DESC' (descending). * Default 'ASC'. * @type array|string $include Array or comma-separated list of user IDs to include. * Default empty. * @type array|string $exclude Array or comma-separated list of user IDs to exclude. * Default empty. * @type bool|int $multi Whether to skip the ID attribute on the 'select' element. * Accepts 1|true or 0|false. Default 0|false. * @type string $show User table column to display. If the selected item is empty * then the 'user_login' will be displayed in parentheses. * Accepts user fields. Default 'display_name'. * @type int|bool $echo Whether to echo or return the drop-down. Accepts 1|true (echo) * or 0|false (return). Default 1|true. * @type int $selected Which user ID should be selected. Default 0. * @type bool $include_selected Whether to always include the selected user ID in the drop- * down. Default false. * @type string $name Name attribute of select element. Default 'user'. * @type string $id ID attribute of the select element. Default is the value of $name. * @type string $class Class attribute of the select element. Default empty. * @type int $blog_id ID of blog (Multisite only). Default is ID of the current blog. * @type string $who Which type of users to query. Accepts only an empty string or * 'authors'. Default empty. * } * @return string String of HTML content. */ function wp_dropdown_users($args = '') { $defaults = array('show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '', 'orderby' => 'display_name', 'order' => 'ASC', 'include' => '', 'exclude' => '', 'multi' => 0, 'show' => 'display_name', 'echo' => 1, 'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '', 'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false, 'option_none_value' => -1); $defaults['selected'] = is_author() ? get_query_var('author') : 0; $r = wp_parse_args($args, $defaults); $show = $r['show']; $show_option_all = $r['show_option_all']; $show_option_none = $r['show_option_none']; $option_none_value = $r['option_none_value']; $query_args = wp_array_slice_assoc($r, array('blog_id', 'include', 'exclude', 'orderby', 'order', 'who')); $query_args['fields'] = array('ID', 'user_login', $show); /** * Filter the query arguments for the user drop-down. * * @since 4.4.0 * * @param array $query_args The query arguments for wp_dropdown_users(). * @param array $r The default arguments for wp_dropdown_users(). */ $query_args = apply_filters('wp_dropdown_users_args', $query_args, $r); $users = get_users($query_args); $output = ''; if (!empty($users) && (empty($r['hide_if_only_one_author']) || count($users) > 1)) { $name = esc_attr($r['name']); if ($r['multi'] && !$r['id']) { $id = ''; } else { $id = $r['id'] ? " id='" . esc_attr($r['id']) . "'" : " id='{$name}'"; } $output = "<select name='{$name}'{$id} class='" . $r['class'] . "'>\n"; if ($show_option_all) { $output .= "\t<option value='0'>{$show_option_all}</option>\n"; } if ($show_option_none) { $_selected = selected($option_none_value, $r['selected'], false); $output .= "\t<option value='" . esc_attr($option_none_value) . "'{$_selected}>{$show_option_none}</option>\n"; } $found_selected = false; foreach ((array) $users as $user) { $user->ID = (int) $user->ID; $_selected = selected($user->ID, $r['selected'], false); if ($_selected) { $found_selected = true; } $display = !empty($user->{$show}) ? $user->{$show} : '(' . $user->user_login . ')'; $output .= "\t<option value='{$user->ID}'{$_selected}>" . esc_html($display) . "</option>\n"; } if ($r['include_selected'] && !$found_selected && $r['selected'] > 0) { $user = get_userdata($r['selected']); $_selected = selected($user->ID, $r['selected'], false); $display = !empty($user->{$show}) ? $user->{$show} : '(' . $user->user_login . ')'; $output .= "\t<option value='{$user->ID}'{$_selected}>" . esc_html($display) . "</option>\n"; } $output .= "</select>"; } /** * Filter the wp_dropdown_users() HTML output. * * @since 2.3.0 * * @param string $output HTML output generated by wp_dropdown_users(). */ $html = apply_filters('wp_dropdown_users', $output); if ($r['echo']) { echo $html; } return $html; }
/** * Used internally to get a list of comment IDs matching the query vars. * * @since 4.4.0 * @access protected * * @global wpdb $wpdb WordPress database abstraction object. */ protected function get_comment_ids() { global $wpdb; // Assemble clauses related to 'comment_approved'. $approved_clauses = array(); // 'status' accepts an array or a comma-separated string. $status_clauses = array(); $statuses = $this->query_vars['status']; if (!is_array($statuses)) { $statuses = preg_split('/[\\s,]+/', $statuses); } // 'any' overrides other statuses. if (!in_array('any', $statuses)) { foreach ($statuses as $status) { switch ($status) { case 'hold': $status_clauses[] = "comment_approved = '0'"; break; case 'approve': $status_clauses[] = "comment_approved = '1'"; break; case 'all': case '': $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; break; default: $status_clauses[] = $wpdb->prepare("comment_approved = %s", $status); break; } } if (!empty($status_clauses)) { $approved_clauses[] = '( ' . implode(' OR ', $status_clauses) . ' )'; } } // User IDs or emails whose unapproved comments are included, regardless of $status. if (!empty($this->query_vars['include_unapproved'])) { $include_unapproved = $this->query_vars['include_unapproved']; // Accepts arrays or comma-separated strings. if (!is_array($include_unapproved)) { $include_unapproved = preg_split('/[\\s,]+/', $include_unapproved); } $unapproved_ids = $unapproved_emails = array(); foreach ($include_unapproved as $unapproved_identifier) { // Numeric values are assumed to be user ids. if (is_numeric($unapproved_identifier)) { $approved_clauses[] = $wpdb->prepare("( user_id = %d AND comment_approved = '0' )", $unapproved_identifier); // Otherwise we match against email addresses. } else { $approved_clauses[] = $wpdb->prepare("( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier); } } } // Collapse comment_approved clauses into a single OR-separated clause. if (!empty($approved_clauses)) { if (1 === count($approved_clauses)) { $this->sql_clauses['where']['approved'] = $approved_clauses[0]; } else { $this->sql_clauses['where']['approved'] = '( ' . implode(' OR ', $approved_clauses) . ' )'; } } $order = 'ASC' == strtoupper($this->query_vars['order']) ? 'ASC' : 'DESC'; // Disable ORDER BY with 'none', an empty array, or boolean false. if (in_array($this->query_vars['orderby'], array('none', array(), false), true)) { $orderby = ''; } elseif (!empty($this->query_vars['orderby'])) { $ordersby = is_array($this->query_vars['orderby']) ? $this->query_vars['orderby'] : preg_split('/[,\\s]/', $this->query_vars['orderby']); $orderby_array = array(); $found_orderby_comment_ID = false; foreach ($ordersby as $_key => $_value) { if (!$_value) { continue; } if (is_int($_key)) { $_orderby = $_value; $_order = $order; } else { $_orderby = $_key; $_order = $_value; } if (!$found_orderby_comment_ID && in_array($_orderby, array('comment_ID', 'comment__in'))) { $found_orderby_comment_ID = true; } $parsed = $this->parse_orderby($_orderby); if (!$parsed) { continue; } if ('comment__in' === $_orderby) { $orderby_array[] = $parsed; continue; } $orderby_array[] = $parsed . ' ' . $this->parse_order($_order); } // If no valid clauses were found, order by comment_date_gmt. if (empty($orderby_array)) { $orderby_array[] = "{$wpdb->comments}.comment_date_gmt {$order}"; } // To ensure determinate sorting, always include a comment_ID clause. if (!$found_orderby_comment_ID) { $comment_ID_order = ''; // Inherit order from comment_date or comment_date_gmt, if available. foreach ($orderby_array as $orderby_clause) { if (preg_match('/comment_date(?:_gmt)*\\ (ASC|DESC)/', $orderby_clause, $match)) { $comment_ID_order = $match[1]; break; } } // If no date-related order is available, use the date from the first available clause. if (!$comment_ID_order) { foreach ($orderby_array as $orderby_clause) { if (false !== strpos('ASC', $orderby_clause)) { $comment_ID_order = 'ASC'; } else { $comment_ID_order = 'DESC'; } break; } } // Default to DESC. if (!$comment_ID_order) { $comment_ID_order = 'DESC'; } $orderby_array[] = "{$wpdb->comments}.comment_ID {$comment_ID_order}"; } $orderby = implode(', ', $orderby_array); } else { $orderby = "{$wpdb->comments}.comment_date_gmt {$order}"; } $number = absint($this->query_vars['number']); $offset = absint($this->query_vars['offset']); if (!empty($number)) { if ($offset) { $limits = 'LIMIT ' . $offset . ',' . $number; } else { $limits = 'LIMIT ' . $number; } } if ($this->query_vars['count']) { $fields = 'COUNT(*)'; } else { $fields = "{$wpdb->comments}.comment_ID"; } $post_id = absint($this->query_vars['post_id']); if (!empty($post_id)) { $this->sql_clauses['where']['post_id'] = $wpdb->prepare('comment_post_ID = %d', $post_id); } // Parse comment IDs for an IN clause. if (!empty($this->query_vars['comment__in'])) { $this->sql_clauses['where']['comment__in'] = "{$wpdb->comments}.comment_ID IN ( " . implode(',', wp_parse_id_list($this->query_vars['comment__in'])) . ' )'; } // Parse comment IDs for a NOT IN clause. if (!empty($this->query_vars['comment__not_in'])) { $this->sql_clauses['where']['comment__not_in'] = "{$wpdb->comments}.comment_ID NOT IN ( " . implode(',', wp_parse_id_list($this->query_vars['comment__not_in'])) . ' )'; } // Parse comment parent IDs for an IN clause. if (!empty($this->query_vars['parent__in'])) { $this->sql_clauses['where']['parent__in'] = 'comment_parent IN ( ' . implode(',', wp_parse_id_list($this->query_vars['parent__in'])) . ' )'; } // Parse comment parent IDs for a NOT IN clause. if (!empty($this->query_vars['parent__not_in'])) { $this->sql_clauses['where']['parent__not_in'] = 'comment_parent NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['parent__not_in'])) . ' )'; } // Parse comment post IDs for an IN clause. if (!empty($this->query_vars['post__in'])) { $this->sql_clauses['where']['post__in'] = 'comment_post_ID IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post__in'])) . ' )'; } // Parse comment post IDs for a NOT IN clause. if (!empty($this->query_vars['post__not_in'])) { $this->sql_clauses['where']['post__not_in'] = 'comment_post_ID NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post__not_in'])) . ' )'; } if ('' !== $this->query_vars['author_email']) { $this->sql_clauses['where']['author_email'] = $wpdb->prepare('comment_author_email = %s', $this->query_vars['author_email']); } if ('' !== $this->query_vars['author_url']) { $this->sql_clauses['where']['author_url'] = $wpdb->prepare('comment_author_url = %s', $this->query_vars['author_url']); } if ('' !== $this->query_vars['karma']) { $this->sql_clauses['where']['karma'] = $wpdb->prepare('comment_karma = %d', $this->query_vars['karma']); } // Filtering by comment_type: 'type', 'type__in', 'type__not_in'. $raw_types = array('IN' => array_merge((array) $this->query_vars['type'], (array) $this->query_vars['type__in']), 'NOT IN' => (array) $this->query_vars['type__not_in']); $comment_types = array(); foreach ($raw_types as $operator => $_raw_types) { $_raw_types = array_unique($_raw_types); foreach ($_raw_types as $type) { switch ($type) { // An empty translates to 'all', for backward compatibility case '': case 'all': break; case 'comment': case 'comments': $comment_types[$operator][] = "''"; break; case 'pings': $comment_types[$operator][] = "'pingback'"; $comment_types[$operator][] = "'trackback'"; break; default: $comment_types[$operator][] = $wpdb->prepare('%s', $type); break; } } if (!empty($comment_types[$operator])) { $types_sql = implode(', ', $comment_types[$operator]); $this->sql_clauses['where']['comment_type__' . strtolower(str_replace(' ', '_', $operator))] = "comment_type {$operator} ({$types_sql})"; } } if ($this->query_vars['hierarchical'] && !$this->query_vars['parent']) { $this->query_vars['parent'] = 0; } if ('' !== $this->query_vars['parent']) { $this->sql_clauses['where']['parent'] = $wpdb->prepare('comment_parent = %d', $this->query_vars['parent']); } if (is_array($this->query_vars['user_id'])) { $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode(',', array_map('absint', $this->query_vars['user_id'])) . ')'; } elseif ('' !== $this->query_vars['user_id']) { $this->sql_clauses['where']['user_id'] = $wpdb->prepare('user_id = %d', $this->query_vars['user_id']); } // Falsy search strings are ignored. if (strlen($this->query_vars['search'])) { $search_sql = $this->get_search_sql($this->query_vars['search'], array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content')); // Strip leading 'AND'. $this->sql_clauses['where']['search'] = preg_replace('/^\\s*AND\\s*/', '', $search_sql); } // If any post-related query vars are passed, join the posts table. $join_posts_table = false; $plucked = wp_array_slice_assoc($this->query_vars, array('post_author', 'post_name', 'post_parent')); $post_fields = array_filter($plucked); if (!empty($post_fields)) { $join_posts_table = true; foreach ($post_fields as $field_name => $field_value) { // $field_value may be an array. $esses = array_fill(0, count((array) $field_value), '%s'); $this->sql_clauses['where'][$field_name] = $wpdb->prepare(" {$wpdb->posts}.{$field_name} IN (" . implode(',', $esses) . ')', $field_value); } } // 'post_status' and 'post_type' are handled separately, due to the specialized behavior of 'any'. foreach (array('post_status', 'post_type') as $field_name) { $q_values = array(); if (!empty($this->query_vars[$field_name])) { $q_values = $this->query_vars[$field_name]; if (!is_array($q_values)) { $q_values = explode(',', $q_values); } // 'any' will cause the query var to be ignored. if (in_array('any', $q_values, true) || empty($q_values)) { continue; } $join_posts_table = true; $esses = array_fill(0, count($q_values), '%s'); $this->sql_clauses['where'][$field_name] = $wpdb->prepare(" {$wpdb->posts}.{$field_name} IN (" . implode(',', $esses) . ")", $q_values); } } // Comment author IDs for an IN clause. if (!empty($this->query_vars['author__in'])) { $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode(',', wp_parse_id_list($this->query_vars['author__in'])) . ' )'; } // Comment author IDs for a NOT IN clause. if (!empty($this->query_vars['author__not_in'])) { $this->sql_clauses['where']['author__not_in'] = 'user_id NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['author__not_in'])) . ' )'; } // Post author IDs for an IN clause. if (!empty($this->query_vars['post_author__in'])) { $join_posts_table = true; $this->sql_clauses['where']['post_author__in'] = 'post_author IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post_author__in'])) . ' )'; } // Post author IDs for a NOT IN clause. if (!empty($this->query_vars['post_author__not_in'])) { $join_posts_table = true; $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post_author__not_in'])) . ' )'; } $join = ''; if ($join_posts_table) { $join .= "JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID"; } if (!empty($this->meta_query_clauses)) { $join .= $this->meta_query_clauses['join']; // Strip leading 'AND'. $this->sql_clauses['where']['meta_query'] = preg_replace('/^\\s*AND\\s*/', '', $this->meta_query_clauses['where']); if (!$this->query_vars['count']) { $groupby = "{$wpdb->comments}.comment_ID"; } } if (!empty($this->query_vars['date_query']) && is_array($this->query_vars['date_query'])) { $this->date_query = new WP_Date_Query($this->query_vars['date_query'], 'comment_date'); $this->sql_clauses['where']['date_query'] = preg_replace('/^\\s*AND\\s*/', '', $this->date_query->get_sql()); } $where = implode(' AND ', $this->sql_clauses['where']); $pieces = array('fields', 'join', 'where', 'orderby', 'limits', 'groupby'); /** * Filter the comment query clauses. * * @since 3.1.0 * * @param array $pieces A compacted array of comment query clauses. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. */ $clauses = apply_filters_ref_array('comments_clauses', array(compact($pieces), &$this)); $fields = isset($clauses['fields']) ? $clauses['fields'] : ''; $join = isset($clauses['join']) ? $clauses['join'] : ''; $where = isset($clauses['where']) ? $clauses['where'] : ''; $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : ''; $limits = isset($clauses['limits']) ? $clauses['limits'] : ''; $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : ''; $this->filtered_where_clause = $where; if ($where) { $where = 'WHERE ' . $where; } if ($groupby) { $groupby = 'GROUP BY ' . $groupby; } if ($orderby) { $orderby = "ORDER BY {$orderby}"; } $found_rows = ''; if (!$this->query_vars['no_found_rows']) { $found_rows = 'SQL_CALC_FOUND_ROWS'; } $this->sql_clauses['select'] = "SELECT {$found_rows} {$fields}"; $this->sql_clauses['from'] = "FROM {$wpdb->comments} {$join}"; $this->sql_clauses['groupby'] = $groupby; $this->sql_clauses['orderby'] = $orderby; $this->sql_clauses['limits'] = $limits; $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; if ($this->query_vars['count']) { return intval($wpdb->get_var($this->request)); } else { $comment_ids = $wpdb->get_col($this->request); return array_map('intval', $comment_ids); } }
/** * Set the autofocused constructs. * * @since 4.4.0 * @access public * * @param array $autofocus { * Mapping of 'panel', 'section', 'control' to the ID which should be autofocused. * * @type string [$control] ID for control to be autofocused. * @type string [$section] ID for section to be autofocused. * @type string [$panel] ID for panel to be autofocused. * } */ public function set_autofocus($autofocus) { $this->autofocus = array_filter(wp_array_slice_assoc($autofocus, array('panel', 'section', 'control')), 'is_string'); }
/** * Gather the parameters passed to client JavaScript via JSON. * * @since 4.1.0 * * @return array The array to be exported to the client as JSON. */ public function json() { $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type' ) ); $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) ); $array['content'] = $this->get_content(); $array['active'] = $this->active(); $array['instanceNumber'] = $this->instance_number; if ( $this->panel ) { /* translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ $array['customizeAction'] = sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) ); } else { $array['customizeAction'] = __( 'Customizing' ); } return $array; }
/** * Retrieve a list of pages. * * @global wpdb $wpdb WordPress database abstraction object. * * @since 1.5.0 * * @param array|string $args { * Optional. Array or string of arguments to retrieve pages. * * @type int $child_of Page ID to return child and grandchild pages of. Note: The value * of `$hierarchical` has no bearing on whether `$child_of` returns * hierarchical results. Default 0, or no restriction. * @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. * @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author', * 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', * 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. * 'post_' can be omitted for any values that start with it. * Default 'post_title'. * @type bool $hierarchical Whether to return pages hierarchically. If false in conjunction with * `$child_of` also being false, both arguments will be disregarded. * Default true. * @type array $exclude Array of page IDs to exclude. Default empty array. * @type array $include Array of page IDs to include. Cannot be used with `$child_of`, * `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. * Default empty array. * @type string $meta_key Only include pages with this meta key. Default empty. * @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. * Default empty. * @type string $authors A comma-separated list of author IDs. Default empty. * @type int $parent Page ID to return direct children of. Default -1, or no restriction. * @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude. * Default empty array. * @type int $number The number of pages to return. Default 0, or all pages. * @type int $offset The number of pages to skip before returning. Requires `$number`. * Default 0. * @type string $post_type The post type to query. Default 'page'. * @type string $post_status A comma-separated list of post status types to include. * Default 'publish'. * } * @return array|false List of pages matching defaults or `$args`. */ function get_pages($args = array()) { global $wpdb; $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title', 'hierarchical' => 1, 'exclude' => array(), 'include' => array(), 'meta_key' => '', 'meta_value' => '', 'authors' => '', 'parent' => -1, 'exclude_tree' => array(), 'number' => '', 'offset' => 0, 'post_type' => 'page', 'post_status' => 'publish'); $r = wp_parse_args($args, $defaults); $number = (int) $r['number']; $offset = (int) $r['offset']; $child_of = (int) $r['child_of']; $hierarchical = $r['hierarchical']; $exclude = $r['exclude']; $meta_key = $r['meta_key']; $meta_value = $r['meta_value']; $parent = $r['parent']; $post_status = $r['post_status']; // Make sure the post type is hierarchical. $hierarchical_post_types = get_post_types(array('hierarchical' => true)); if (!in_array($r['post_type'], $hierarchical_post_types)) { return false; } if ($parent > 0 && !$child_of) { $hierarchical = false; } // Make sure we have a valid post status. if (!is_array($post_status)) { $post_status = explode(',', $post_status); } if (array_diff($post_status, get_post_stati())) { return false; } // $args can be whatever, only use the args defined in defaults to compute the key. $key = md5(serialize(wp_array_slice_assoc($r, array_keys($defaults)))); $last_changed = wp_cache_get('last_changed', 'posts'); if (!$last_changed) { $last_changed = microtime(); wp_cache_set('last_changed', $last_changed, 'posts'); } $cache_key = "get_pages:{$key}:{$last_changed}"; if ($cache = wp_cache_get($cache_key, 'posts')) { // Convert to WP_Post instances. $pages = array_map('get_post', $cache); /** This filter is documented in wp-includes/post.php */ $pages = apply_filters('get_pages', $pages, $r); return $pages; } $inclusions = ''; if (!empty($r['include'])) { $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include $parent = -1; $exclude = ''; $meta_key = ''; $meta_value = ''; $hierarchical = false; $incpages = wp_parse_id_list($r['include']); if (!empty($incpages)) { $inclusions = ' AND ID IN (' . implode(',', $incpages) . ')'; } } $exclusions = ''; if (!empty($exclude)) { $expages = wp_parse_id_list($exclude); if (!empty($expages)) { $exclusions = ' AND ID NOT IN (' . implode(',', $expages) . ')'; } } $author_query = ''; if (!empty($r['authors'])) { $post_authors = preg_split('/[\\s,]+/', $r['authors']); if (!empty($post_authors)) { foreach ($post_authors as $post_author) { //Do we have an author id or an author login? if (0 == intval($post_author)) { $post_author = get_user_by('login', $post_author); if (empty($post_author)) { continue; } if (empty($post_author->ID)) { continue; } $post_author = $post_author->ID; } if ('' == $author_query) { $author_query = $wpdb->prepare(' post_author = %d ', $post_author); } else { $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author); } } if ('' != $author_query) { $author_query = " AND ({$author_query})"; } } } $join = ''; $where = "{$exclusions} {$inclusions} "; if ('' !== $meta_key || '' !== $meta_value) { $join = " LEFT JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id )"; // meta_key and meta_value might be slashed $meta_key = wp_unslash($meta_key); $meta_value = wp_unslash($meta_value); if ('' !== $meta_key) { $where .= $wpdb->prepare(" AND {$wpdb->postmeta}.meta_key = %s", $meta_key); } if ('' !== $meta_value) { $where .= $wpdb->prepare(" AND {$wpdb->postmeta}.meta_value = %s", $meta_value); } } if (is_array($parent)) { $post_parent__in = implode(',', array_map('absint', (array) $parent)); if (!empty($post_parent__in)) { $where .= " AND post_parent IN ({$post_parent__in})"; } } elseif ($parent >= 0) { $where .= $wpdb->prepare(' AND post_parent = %d ', $parent); } if (1 == count($post_status)) { $where_post_type = $wpdb->prepare("post_type = %s AND post_status = %s", $r['post_type'], reset($post_status)); } else { $post_status = implode("', '", $post_status); $where_post_type = $wpdb->prepare("post_type = %s AND post_status IN ('{$post_status}')", $r['post_type']); } $orderby_array = array(); $allowed_keys = array('author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified', 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent', 'ID', 'rand', 'comment_count'); foreach (explode(',', $r['sort_column']) as $orderby) { $orderby = trim($orderby); if (!in_array($orderby, $allowed_keys)) { continue; } switch ($orderby) { case 'menu_order': break; case 'ID': $orderby = "{$wpdb->posts}.ID"; break; case 'rand': $orderby = 'RAND()'; break; case 'comment_count': $orderby = "{$wpdb->posts}.comment_count"; break; default: if (0 === strpos($orderby, 'post_')) { $orderby = "{$wpdb->posts}." . $orderby; } else { $orderby = "{$wpdb->posts}.post_" . $orderby; } } $orderby_array[] = $orderby; } $sort_column = !empty($orderby_array) ? implode(',', $orderby_array) : "{$wpdb->posts}.post_title"; $sort_order = strtoupper($r['sort_order']); if ('' !== $sort_order && !in_array($sort_order, array('ASC', 'DESC'))) { $sort_order = 'ASC'; } $query = "SELECT * FROM {$wpdb->posts} {$join} WHERE ({$where_post_type}) {$where} "; $query .= $author_query; $query .= " ORDER BY " . $sort_column . " " . $sort_order; if (!empty($number)) { $query .= ' LIMIT ' . $offset . ',' . $number; } $pages = $wpdb->get_results($query); if (empty($pages)) { /** This filter is documented in wp-includes/post.php */ $pages = apply_filters('get_pages', array(), $r); return $pages; } // Sanitize before caching so it'll only get done once. $num_pages = count($pages); for ($i = 0; $i < $num_pages; $i++) { $pages[$i] = sanitize_post($pages[$i], 'raw'); } // Update cache. update_post_cache($pages); if ($child_of || $hierarchical) { $pages = get_page_children($child_of, $pages); } if (!empty($r['exclude_tree'])) { $exclude = wp_parse_id_list($r['exclude_tree']); foreach ($exclude as $id) { $children = get_page_children($id, $pages); foreach ($children as $child) { $exclude[] = $child->ID; } } $num_pages = count($pages); for ($i = 0; $i < $num_pages; $i++) { if (in_array($pages[$i]->ID, $exclude)) { unset($pages[$i]); } } } $page_structure = array(); foreach ($pages as $page) { $page_structure[] = $page->ID; } wp_cache_set($cache_key, $page_structure, 'posts'); // Convert to WP_Post instances $pages = array_map('get_post', $pages); /** * Filter the retrieved list of pages. * * @since 2.1.0 * * @param array $pages List of pages to retrieve. * @param array $r Array of get_pages() arguments. */ return apply_filters('get_pages', $pages, $r); }
/** * Retrieve the terms in a given taxonomy or list of taxonomies. * * You can fully inject any customizations to the query before it is sent, as * well as control the output with a filter. * * The {@see 'get_terms'} filter will be called when the cache has the term and will * pass the found term along with the array of $taxonomies and array of $args. * This filter is also called before the array of terms is passed and will pass * the array of terms, along with the $taxonomies and $args. * * The {@see 'list_terms_exclusions'} filter passes the compiled exclusions along with * the $args. * * The {@see 'get_terms_orderby'} filter passes the `ORDER BY` clause for the query * along with the $args array. * * @since 2.3.0 * @since 4.2.0 Introduced 'name' and 'childless' parameters. * @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter. * Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return * a list of WP_Term objects. * * @global wpdb $wpdb WordPress database abstraction object. * @global array $wp_filter * * @param string|array $taxonomies Taxonomy name or list of Taxonomy names. * @param array|string $args { * Optional. Array or string of arguments to get terms. * * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug', * 'term_group', 'term_id', 'id', 'description'), 'count' for term * taxonomy count, 'include' to match the 'order' of the $include param, * or 'none' to skip ORDER BY. Defaults to 'name'. * @type string $order Whether to order terms in ascending or descending order. * Accepts 'ASC' (ascending) or 'DESC' (descending). * Default 'ASC'. * @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts * 1|true or 0|false. Default 1|true. * @type array|string $include Array or comma/space-separated string of term ids to include. * Default empty array. * @type array|string $exclude Array or comma/space-separated string of term ids to exclude. * If $include is non-empty, $exclude is ignored. * Default empty array. * @type array|string $exclude_tree Array or comma/space-separated string of term ids to exclude * along with all of their descendant terms. If $include is * non-empty, $exclude_tree is ignored. Default empty array. * @type int|string $number Maximum number of terms to return. Accepts ''|0 (all) or any * positive number. Default ''|0 (all). * @type int $offset The number by which to offset the terms query. Default empty. * @type string $fields Term fields to query for. Accepts 'all' (returns an array of complete * term objects), 'ids' (returns an array of ids), 'id=>parent' (returns * an associative array with ids as keys, parent term IDs as values), * 'names' (returns an array of term names), 'count' (returns the number * of matching terms), 'id=>name' (returns an associative array with ids * as keys, term names as values), or 'id=>slug' (returns an associative * array with ids as keys, term slugs as values). Default 'all'. * @type string|array $name Optional. Name or array of names to return term(s) for. Default empty. * @type string|array $slug Optional. Slug or array of slugs to return term(s) for. Default empty. * @type bool $hierarchical Whether to include terms that have non-empty descendants (even * if $hide_empty is set to true). Default true. * @type string $search Search criteria to match terms. Will be SQL-formatted with * wildcards before and after. Default empty. * @type string $name__like Retrieve terms with criteria by which a term is LIKE $name__like. * Default empty. * @type string $description__like Retrieve terms where the description is LIKE $description__like. * Default empty. * @type bool $pad_counts Whether to pad the quantity of a term's children in the quantity * of each term's "count" object variable. Default false. * @type string $get Whether to return terms regardless of ancestry or whether the terms * are empty. Accepts 'all' or empty (disabled). Default empty. * @type int $child_of Term ID to retrieve child terms of. If multiple taxonomies * are passed, $child_of is ignored. Default 0. * @type int|string $parent Parent term ID to retrieve direct-child terms of. Default empty. * @type bool $childless True to limit results to terms that have no children. This parameter * has no effect on non-hierarchical taxonomies. Default false. * @type string $cache_domain Unique cache key to be produced when this query is stored in an * object cache. Default is 'core'. * @type bool $update_term_meta_cache Whether to prime meta caches for matched terms. Default true. * @type array $meta_query Meta query clauses to limit retrieved terms by. * See `WP_Meta_Query`. Default empty. * } * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies * do not exist. */ function get_terms($taxonomies, $args = '') { global $wpdb; $empty_array = array(); $single_taxonomy = !is_array($taxonomies) || 1 === count($taxonomies); if (!is_array($taxonomies)) { $taxonomies = array($taxonomies); } foreach ($taxonomies as $taxonomy) { if (!taxonomy_exists($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); } } $defaults = array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false, 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core', 'update_term_meta_cache' => true, 'meta_query' => ''); $args = wp_parse_args($args, $defaults); $args['number'] = absint($args['number']); $args['offset'] = absint($args['offset']); // Save queries by not crawling the tree in the case of multiple taxes or a flat tax. $has_hierarchical_tax = false; foreach ($taxonomies as $_tax) { if (is_taxonomy_hierarchical($_tax)) { $has_hierarchical_tax = true; } } if (!$has_hierarchical_tax) { $args['hierarchical'] = false; $args['pad_counts'] = false; } // 'parent' overrides 'child_of'. if (0 < intval($args['parent'])) { $args['child_of'] = false; } if ('all' == $args['get']) { $args['childless'] = false; $args['child_of'] = 0; $args['hide_empty'] = 0; $args['hierarchical'] = false; $args['pad_counts'] = false; } /** * Filter the terms query arguments. * * @since 3.1.0 * * @param array $args An array of get_term() arguments. * @param array $taxonomies An array of taxonomies. */ $args = apply_filters('get_terms_args', $args, $taxonomies); // Avoid the query if the queried parent/child_of term has no descendants. $child_of = $args['child_of']; $parent = $args['parent']; if ($child_of) { $_parent = $child_of; } elseif ($parent) { $_parent = $parent; } else { $_parent = false; } if ($_parent) { $in_hierarchy = false; foreach ($taxonomies as $_tax) { $hierarchy = _get_term_hierarchy($_tax); if (isset($hierarchy[$_parent])) { $in_hierarchy = true; } } if (!$in_hierarchy) { return $empty_array; } } $_orderby = strtolower($args['orderby']); if ('count' == $_orderby) { $orderby = 'tt.count'; } elseif ('name' == $_orderby) { $orderby = 't.name'; } elseif ('slug' == $_orderby) { $orderby = 't.slug'; } elseif ('include' == $_orderby && !empty($args['include'])) { $include = implode(',', array_map('absint', $args['include'])); $orderby = "FIELD( t.term_id, {$include} )"; } elseif ('term_group' == $_orderby) { $orderby = 't.term_group'; } elseif ('description' == $_orderby) { $orderby = 'tt.description'; } elseif ('none' == $_orderby) { $orderby = ''; } elseif (empty($_orderby) || 'id' == $_orderby || 'term_id' === $_orderby) { $orderby = 't.term_id'; } else { $orderby = 't.name'; } /** * Filter the ORDERBY clause of the terms query. * * @since 2.8.0 * * @param string $orderby `ORDERBY` clause of the terms query. * @param array $args An array of terms query arguments. * @param array $taxonomies An array of taxonomies. */ $orderby = apply_filters('get_terms_orderby', $orderby, $args, $taxonomies); $order = strtoupper($args['order']); if (!empty($orderby)) { $orderby = "ORDER BY {$orderby}"; } else { $order = ''; } if ('' !== $order && !in_array($order, array('ASC', 'DESC'))) { $order = 'ASC'; } $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')"; $exclude = $args['exclude']; $exclude_tree = $args['exclude_tree']; $include = $args['include']; $inclusions = ''; if (!empty($include)) { $exclude = ''; $exclude_tree = ''; $inclusions = implode(',', wp_parse_id_list($include)); } if (!empty($inclusions)) { $inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )'; $where .= $inclusions; } $exclusions = array(); if (!empty($exclude_tree)) { $exclude_tree = wp_parse_id_list($exclude_tree); $excluded_children = $exclude_tree; foreach ($exclude_tree as $extrunk) { $excluded_children = array_merge($excluded_children, (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids', 'hide_empty' => 0))); } $exclusions = array_merge($excluded_children, $exclusions); } if (!empty($exclude)) { $exclusions = array_merge(wp_parse_id_list($exclude), $exclusions); } // 'childless' terms are those without an entry in the flattened term hierarchy. $childless = (bool) $args['childless']; if ($childless) { foreach ($taxonomies as $_tax) { $term_hierarchy = _get_term_hierarchy($_tax); $exclusions = array_merge(array_keys($term_hierarchy), $exclusions); } } if (!empty($exclusions)) { $exclusions = ' AND t.term_id NOT IN (' . implode(',', array_map('intval', $exclusions)) . ')'; } else { $exclusions = ''; } /** * Filter the terms to exclude from the terms query. * * @since 2.3.0 * * @param string $exclusions `NOT IN` clause of the terms query. * @param array $args An array of terms query arguments. * @param array $taxonomies An array of taxonomies. */ $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args, $taxonomies); if (!empty($exclusions)) { $where .= $exclusions; } if (!empty($args['name'])) { $names = (array) $args['name']; foreach ($names as &$_name) { $_name = sanitize_term_field('name', $_name, 0, reset($taxonomies), 'db'); } $where .= " AND t.name IN ('" . implode("', '", array_map('esc_sql', $names)) . "')"; } if (!empty($args['slug'])) { if (is_array($args['slug'])) { $slug = array_map('sanitize_title', $args['slug']); $where .= " AND t.slug IN ('" . implode("', '", $slug) . "')"; } else { $slug = sanitize_title($args['slug']); $where .= " AND t.slug = '{$slug}'"; } } if (!empty($args['name__like'])) { $where .= $wpdb->prepare(" AND t.name LIKE %s", '%' . $wpdb->esc_like($args['name__like']) . '%'); } if (!empty($args['description__like'])) { $where .= $wpdb->prepare(" AND tt.description LIKE %s", '%' . $wpdb->esc_like($args['description__like']) . '%'); } if ('' !== $parent) { $parent = (int) $parent; $where .= " AND tt.parent = '{$parent}'"; } $hierarchical = $args['hierarchical']; if ('count' == $args['fields']) { $hierarchical = false; } if ($args['hide_empty'] && !$hierarchical) { $where .= ' AND tt.count > 0'; } $number = $args['number']; $offset = $args['offset']; // Don't limit the query results when we have to descend the family tree. if ($number && !$hierarchical && !$child_of && '' === $parent) { if ($offset) { $limits = 'LIMIT ' . $offset . ',' . $number; } else { $limits = 'LIMIT ' . $number; } } else { $limits = ''; } if (!empty($args['search'])) { $like = '%' . $wpdb->esc_like($args['search']) . '%'; $where .= $wpdb->prepare(' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like); } // Meta query support. $join = ''; if (!empty($args['meta_query'])) { $mquery = new WP_Meta_Query($args['meta_query']); $mq_sql = $mquery->get_sql('term', 't', 'term_id'); $join .= $mq_sql['join']; $where .= $mq_sql['where']; } $selects = array(); switch ($args['fields']) { case 'all': $selects = array('t.*', 'tt.*'); break; case 'ids': case 'id=>parent': $selects = array('t.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy'); break; case 'names': $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy'); break; case 'count': $orderby = ''; $order = ''; $selects = array('COUNT(*)'); break; case 'id=>name': $selects = array('t.term_id', 't.name', 'tt.count', 'tt.taxonomy'); break; case 'id=>slug': $selects = array('t.term_id', 't.slug', 'tt.count', 'tt.taxonomy'); break; } $_fields = $args['fields']; /** * Filter the fields to select in the terms query. * * Field lists modified using this filter will only modify the term fields returned * by the function when the `$fields` parameter set to 'count' or 'all'. In all other * cases, the term fields in the results array will be determined by the `$fields` * parameter alone. * * Use of this filter can result in unpredictable behavior, and is not recommended. * * @since 2.8.0 * * @param array $selects An array of fields to select for the terms query. * @param array $args An array of term query arguments. * @param array $taxonomies An array of taxonomies. */ $fields = implode(', ', apply_filters('get_terms_fields', $selects, $args, $taxonomies)); $join .= " INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id"; $pieces = array('fields', 'join', 'where', 'orderby', 'order', 'limits'); /** * Filter the terms query SQL clauses. * * @since 3.1.0 * * @param array $pieces Terms query SQL clauses. * @param array $taxonomies An array of taxonomies. * @param array $args An array of terms query arguments. */ $clauses = apply_filters('terms_clauses', compact($pieces), $taxonomies, $args); $fields = isset($clauses['fields']) ? $clauses['fields'] : ''; $join = isset($clauses['join']) ? $clauses['join'] : ''; $where = isset($clauses['where']) ? $clauses['where'] : ''; $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : ''; $order = isset($clauses['order']) ? $clauses['order'] : ''; $limits = isset($clauses['limits']) ? $clauses['limits'] : ''; $query = "SELECT {$fields} FROM {$wpdb->terms} AS t {$join} WHERE {$where} {$orderby} {$order} {$limits}"; // $args can be anything. Only use the args defined in defaults to compute the key. $key = md5(serialize(wp_array_slice_assoc($args, array_keys($defaults))) . serialize($taxonomies) . $query); $last_changed = wp_cache_get('last_changed', 'terms'); if (!$last_changed) { $last_changed = microtime(); wp_cache_set('last_changed', $last_changed, 'terms'); } $cache_key = "get_terms:{$key}:{$last_changed}"; $cache = wp_cache_get($cache_key, 'terms'); if (false !== $cache) { if ('all' === $_fields) { $cache = array_map('get_term', $cache); } /** * Filter the given taxonomy's terms cache. * * @since 2.3.0 * * @param array $cache Cached array of terms for the given taxonomy. * @param array $taxonomies An array of taxonomies. * @param array $args An array of get_terms() arguments. */ return apply_filters('get_terms', $cache, $taxonomies, $args); } if ('count' == $_fields) { return $wpdb->get_var($query); } $terms = $wpdb->get_results($query); if ('all' == $_fields) { update_term_cache($terms); } // Prime termmeta cache. if ($args['update_term_meta_cache']) { $term_ids = wp_list_pluck($terms, 'term_id'); update_termmeta_cache($term_ids); } if (empty($terms)) { wp_cache_add($cache_key, array(), 'terms', DAY_IN_SECONDS); /** This filter is documented in wp-includes/taxonomy-functions.php */ return apply_filters('get_terms', array(), $taxonomies, $args); } if ($child_of) { foreach ($taxonomies as $_tax) { $children = _get_term_hierarchy($_tax); if (!empty($children)) { $terms = _get_term_children($child_of, $terms, $_tax); } } } // Update term counts to include children. if ($args['pad_counts'] && 'all' == $_fields) { foreach ($taxonomies as $_tax) { _pad_term_counts($terms, $_tax); } } // Make sure we show empty categories that have children. if ($hierarchical && $args['hide_empty'] && is_array($terms)) { foreach ($terms as $k => $term) { if (!$term->count) { $children = get_term_children($term->term_id, $term->taxonomy); if (is_array($children)) { foreach ($children as $child_id) { $child = get_term($child_id, $term->taxonomy); if ($child->count) { continue 2; } } } // It really is empty. unset($terms[$k]); } } } $_terms = array(); if ('id=>parent' == $_fields) { foreach ($terms as $term) { $_terms[$term->term_id] = $term->parent; } } elseif ('ids' == $_fields) { foreach ($terms as $term) { $_terms[] = $term->term_id; } } elseif ('names' == $_fields) { foreach ($terms as $term) { $_terms[] = $term->name; } } elseif ('id=>name' == $_fields) { foreach ($terms as $term) { $_terms[$term->term_id] = $term->name; } } elseif ('id=>slug' == $_fields) { foreach ($terms as $term) { $_terms[$term->term_id] = $term->slug; } } if (!empty($_terms)) { $terms = $_terms; } if ($number && is_array($terms) && count($terms) > $number) { $terms = array_slice($terms, $offset, $number); } wp_cache_add($cache_key, $terms, 'terms', DAY_IN_SECONDS); if ('all' === $_fields) { $terms = array_map('get_term', $terms); } /** This filter is documented in wp-includes/taxonomy */ return apply_filters('get_terms', $terms, $taxonomies, $args); }
/** * List all the authors of the blog, with several options available. * * <ul> * <li>optioncount (boolean) (false): Show the count in parenthesis next to the * author's name.</li> * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is * installed bydefault.</li> * <li>show_fullname (boolean) (false): Show their full names.</li> * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li> * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li> * <li>feed_image (string) (''): If isn't empty, use this image to link to * feeds.</li> * <li>echo (boolean) (true): Set to false to return the output, instead of * echoing.</li> * <li>style (string) ('list'): Whether to display list of authors in list form * or as a string.</li> * <li>html (bool) (true): Whether to list the items in html form or plaintext. * </li> * </ul> * * @link http://codex.wordpress.org/Template_Tags/wp_list_authors * @since 1.2.0 * @param array $args The argument array. * @return null|string The output, if echo is set to false. */ function wp_list_authors($args = '') { global $wpdb; $defaults = array('orderby' => 'name', 'order' => 'ASC', 'number' => '', 'optioncount' => false, 'exclude_admin' => true, 'show_fullname' => false, 'hide_empty' => true, 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, 'style' => 'list', 'html' => true); $args = wp_parse_args($args, $defaults); extract($args, EXTR_SKIP); $return = ''; $query_args = wp_array_slice_assoc($args, array('orderby', 'order', 'number')); $query_args['fields'] = 'ids'; $authors = get_users($query_args); $author_count = array(); foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM {$wpdb->posts} WHERE post_type = 'post' AND " . get_private_posts_cap_sql('post') . " GROUP BY post_author") as $row) { $author_count[$row->post_author] = $row->count; } foreach ($authors as $author_id) { $author = get_userdata($author_id); if ($exclude_admin && 'admin' == $author->display_name) { continue; } $posts = isset($author_count[$author->ID]) ? $author_count[$author->ID] : 0; if (!$posts && $hide_empty) { continue; } $link = ''; if ($show_fullname && $author->first_name && $author->last_name) { $name = "{$author->first_name} {$author->last_name}"; } else { $name = $author->display_name; } if (!$html) { $return .= $name . ', '; continue; // No need to go further to process HTML. } if ('list' == $style) { $return .= '<li>'; } $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr(sprintf(__("Posts by %s"), $author->display_name)) . '">' . $name . '</a>'; if (!empty($feed_image) || !empty($feed)) { $link .= ' '; if (empty($feed_image)) { $link .= '('; } $link .= '<a href="' . get_author_feed_link($author->ID) . '"'; $alt = $title = ''; if (!empty($feed)) { $title = ' title="' . esc_attr($feed) . '"'; $alt = ' alt="' . esc_attr($feed) . '"'; $name = $feed; $link .= $title; } $link .= '>'; if (!empty($feed_image)) { $link .= '<img src="' . esc_url($feed_image) . '" style="border: none;"' . $alt . $title . ' />'; } else { $link .= $name; } $link .= '</a>'; if (empty($feed_image)) { $link .= ')'; } } if ($optioncount) { $link .= ' (' . $posts . ')'; } $return .= $link; $return .= 'list' == $style ? '</li>' : ', '; } $return = rtrim($return, ', '); if (!$echo) { return $return; } echo $return; }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $commentarr Contains information on the comment. * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); if (empty($comment)) { return 0; } // Make sure that the comment post ID is valid (if specified). if (isset($commentarr['comment_post_ID']) && !get_post($commentarr['comment_post_ID'])) { return 0; } // Escape data pulled from DB. $comment = wp_slash($comment); $old_status = $comment['comment_approved']; // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. $data = wp_unslash($commentarr); /** * Filter the comment content before it is updated in the database. * * @since 1.5.0 * * @param string $comment_content The comment data. */ $data['comment_content'] = apply_filters('comment_save_pre', $data['comment_content']); $data['comment_date_gmt'] = get_gmt_from_date($data['comment_date']); if (!isset($data['comment_approved'])) { $data['comment_approved'] = 1; } elseif ('hold' == $data['comment_approved']) { $data['comment_approved'] = 0; } elseif ('approve' == $data['comment_approved']) { $data['comment_approved'] = 1; } $comment_ID = $data['comment_ID']; $comment_post_ID = $data['comment_post_ID']; $keys = array('comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id'); $data = wp_array_slice_assoc($data, $keys); $rval = $wpdb->update($wpdb->comments, $data, compact('comment_ID')); clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); /** * Fires immediately after a comment is updated in the database. * * The hook also fires immediately before comment status transition hooks are fired. * * @since 1.2.0 * * @param int $comment_ID The comment ID. */ do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment->comment_approved, $old_status, $comment); return $rval; }
/** * Create dropdown HTML content of users. * * The content can either be displayed, which it is by default or retrieved by * setting the 'echo' argument. The 'include' and 'exclude' arguments do not * need to be used; all users will be displayed in that case. Only one can be * used, either 'include' or 'exclude', but not both. * * The available arguments are as follows: * <ol> * <li>show_option_all - Text to show all and whether HTML option exists.</li> * <li>show_option_none - Text for show none and whether HTML option exists.</li> * <li>hide_if_only_one_author - Don't create the dropdown if there is only one user.</li> * <li>orderby - SQL order by clause for what order the users appear. Default is 'display_name'.</li> * <li>order - Default is 'ASC'. Can also be 'DESC'.</li> * <li>include - User IDs to include.</li> * <li>exclude - User IDs to exclude.</li> * <li>multi - Default is 'false'. Whether to skip the ID attribute on the 'select' element. A 'true' value is overridden when id argument is set.</li> * <li>show - Default is 'display_name'. User table column to display. If the selected item is empty then the user_login will be displayed in parentheses</li> * <li>echo - Default is '1'. Whether to display or retrieve content.</li> * <li>selected - Which User ID is selected.</li> * <li>include_selected - Always include the selected user ID in the dropdown. Default is false.</li> * <li>name - Default is 'user'. Name attribute of select element.</li> * <li>id - Default is the value of the 'name' parameter. ID attribute of select element.</li> * <li>class - Class attribute of select element.</li> * <li>blog_id - ID of blog (Multisite only). Defaults to ID of current blog.</li> * <li>who - Which users to query. Currently only 'authors' is supported. Default is all users.</li> * </ol> * * @since 2.3.0 * @uses $wpdb WordPress database object for queries * * @param string|array $args Optional. Override defaults. * @return string|null Null on display. String of HTML content on retrieve. */ function wp_dropdown_users($args = '') { $defaults = array('show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '', 'orderby' => 'display_name', 'order' => 'ASC', 'include' => '', 'exclude' => '', 'multi' => 0, 'show' => 'display_name', 'echo' => 1, 'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '', 'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false); $defaults['selected'] = is_author() ? get_query_var('author') : 0; $r = wp_parse_args($args, $defaults); extract($r, EXTR_SKIP); $query_args = wp_array_slice_assoc($r, array('blog_id', 'include', 'exclude', 'orderby', 'order', 'who')); $query_args['fields'] = array('ID', $show); $users = get_users($query_args); $output = ''; if (!empty($users) && (empty($hide_if_only_one_author) || count($users) > 1)) { $name = esc_attr($name); if ($multi && !$id) { $id = ''; } else { $id = $id ? " id='" . esc_attr($id) . "'" : " id='{$name}'"; } $output = "<select name='{$name}'{$id} class='{$class}'>\n"; if ($show_option_all) { $output .= "\t<option value='0'>{$show_option_all}</option>\n"; } if ($show_option_none) { $_selected = selected(-1, $selected, false); $output .= "\t<option value='-1'{$_selected}>{$show_option_none}</option>\n"; } $found_selected = false; foreach ((array) $users as $user) { $user->ID = (int) $user->ID; $_selected = selected($user->ID, $selected, false); if ($_selected) { $found_selected = true; } $display = !empty($user->{$show}) ? $user->{$show} : '(' . $user->user_login . ')'; $output .= "\t<option value='{$user->ID}'{$_selected}>" . esc_html($display) . "</option>\n"; } if ($include_selected && !$found_selected && $selected > 0) { $user = get_userdata($selected); $_selected = selected($user->ID, $selected, false); $display = !empty($user->{$show}) ? $user->{$show} : '(' . $user->user_login . ')'; $output .= "\t<option value='{$user->ID}'{$_selected}>" . esc_html($display) . "</option>\n"; } $output .= "</select>"; } $output = apply_filters('wp_dropdown_users', $output); if ($echo) { echo $output; } return $output; }
/** * Fetch descendants for located comments. * * Instead of calling `get_children()` separately on each child comment, we do a single set of queries to fetch * the descendant trees for all matched top-level comments. * * @since 4.4.0 * * @param array $comments Array of top-level comments whose descendants should be filled in. * @return array */ protected function fill_descendants($comments) { global $wpdb; $levels = array(0 => wp_list_pluck($comments, 'comment_ID')); /* * The WHERE clause for the descendant query is the same as for the top-level * query, minus the `parent`, `parent__in`, and `parent__not_in` sub-clauses. */ $_where = $this->filtered_where_clause; $exclude_keys = array('parent', 'parent__in', 'parent__not_in'); foreach ($exclude_keys as $exclude_key) { if (isset($this->sql_clauses['where'][$exclude_key])) { $clause = $this->sql_clauses['where'][$exclude_key]; // Strip the clause as well as any adjacent ANDs. $pattern = '|(?:AND)?\\s*' . $clause . '\\s*(?:AND)?|'; $_where_parts = preg_split($pattern, $_where); // Remove empties. $_where_parts = array_filter(array_map('trim', $_where_parts)); // Reassemble with an AND. $_where = implode(' AND ', $_where_parts); } } $key = md5(serialize(wp_array_slice_assoc($this->query_vars, array_keys($this->query_var_defaults)))); $last_changed = wp_cache_get_last_changed('comment'); // Fetch an entire level of the descendant tree at a time. $level = 0; do { // Parent-child relationships may be cached. Only query for those that are not. $child_ids = $uncached_parent_ids = array(); $_parent_ids = $levels[$level]; foreach ($_parent_ids as $parent_id) { $cache_key = "get_comment_child_ids:{$parent_id}:{$key}:{$last_changed}"; $parent_child_ids = wp_cache_get($cache_key, 'comment'); if (false !== $parent_child_ids) { $child_ids = array_merge($child_ids, $parent_child_ids); } else { $uncached_parent_ids[] = $parent_id; } } if ($uncached_parent_ids) { // Fetch this level of comments. $parent_query_args = $this->query_vars; foreach ($exclude_keys as $exclude_key) { $parent_query_args[$exclude_key] = ''; } $parent_query_args['parent__in'] = $uncached_parent_ids; $parent_query_args['no_found_rows'] = true; $parent_query_args['hierarchical'] = false; $parent_query_args['offset'] = 0; $parent_query_args['number'] = 0; $level_comments = get_comments($parent_query_args); // Cache parent-child relationships. $parent_map = array_fill_keys($uncached_parent_ids, array()); foreach ($level_comments as $level_comment) { $parent_map[$level_comment->comment_parent][] = $level_comment->comment_ID; $child_ids[] = $level_comment->comment_ID; } foreach ($parent_map as $parent_id => $children) { $cache_key = "get_comment_child_ids:{$parent_id}:{$key}:{$last_changed}"; wp_cache_set($cache_key, $children, 'comment'); } } $level++; $levels[$level] = $child_ids; } while ($child_ids); // Prime comment caches for non-top-level comments. $descendant_ids = array(); for ($i = 1, $c = count($levels); $i < $c; $i++) { $descendant_ids = array_merge($descendant_ids, $levels[$i]); } _prime_comment_caches($descendant_ids, $this->query_vars['update_comment_meta_cache']); // Assemble a flat array of all comments + descendants. $all_comments = $comments; foreach ($descendant_ids as $descendant_id) { $all_comments[] = get_comment($descendant_id); } // If a threaded representation was requested, build the tree. if ('threaded' === $this->query_vars['hierarchical']) { $threaded_comments = $ref = array(); foreach ($all_comments as $k => $c) { $_c = get_comment($c->comment_ID); // If the comment isn't in the reference array, it goes in the top level of the thread. if (!isset($ref[$c->comment_parent])) { $threaded_comments[$_c->comment_ID] = $_c; $ref[$_c->comment_ID] = $threaded_comments[$_c->comment_ID]; // Otherwise, set it as a child of its parent. } else { $ref[$_c->comment_parent]->add_child($_c); $ref[$_c->comment_ID] = $ref[$_c->comment_parent]->get_child($_c->comment_ID); } } // Set the 'populated_children' flag, to ensure additional database queries aren't run. foreach ($ref as $_ref) { $_ref->populated_children(true); } $comments = $threaded_comments; } else { $comments = $all_comments; } return $comments; }
/** * Gets a list of networks matching the query vars. * * @since 4.6.0 * @access public * * @return int|array The list of networks. */ public function get_networks() { $this->parse_query(); /** * Fires before networks are retrieved. * * @since 4.6.0 * * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference. */ do_action_ref_array('pre_get_networks', array(&$this)); // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. $key = md5(serialize(wp_array_slice_assoc($this->query_vars, array_keys($this->query_var_defaults)))); $last_changed = wp_cache_get('last_changed', 'networks'); if (!$last_changed) { $last_changed = microtime(); wp_cache_set('last_changed', $last_changed, 'networks'); } $cache_key = "get_network_ids:{$key}:{$last_changed}"; $cache_value = wp_cache_get($cache_key, 'networks'); if (false === $cache_value) { $network_ids = $this->get_network_ids(); if ($network_ids) { $this->set_found_networks(); } $cache_value = array('network_ids' => $network_ids, 'found_networks' => $this->found_networks); wp_cache_add($cache_key, $cache_value, 'networks'); } else { $network_ids = $cache_value['network_ids']; $this->found_networks = $cache_value['found_networks']; } if ($this->found_networks && $this->query_vars['number']) { $this->max_num_pages = ceil($this->found_networks / $this->query_vars['number']); } // If querying for a count only, there's nothing more to do. if ($this->query_vars['count']) { // $network_ids is actually a count in this case. return intval($network_ids); } $network_ids = array_map('intval', $network_ids); if ('ids' == $this->query_vars['fields']) { $this->networks = $network_ids; return $this->networks; } if ($this->query_vars['update_network_cache']) { _prime_network_caches($network_ids); } // Fetch full network objects from the primed cache. $_networks = array(); foreach ($network_ids as $network_id) { if ($_network = get_network($network_id)) { $_networks[] = $_network; } } /** * Filters the network query results. * * @since 4.6.0 * * @param array $results An array of networks. * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference. */ $_networks = apply_filters_ref_array('the_networks', array($_networks, &$this)); // Convert to WP_Network instances $this->networks = array_map('get_network', $_networks); return $this->networks; }