/** * Parses shortcode parameters and returns the gallery objects * * @since .50 * * @param int Post ID of the parent * @param array Attributes of the shortcode * @param boolean true to calculate and return ['found_posts'] as an array element * * @return array List of attachments returned from WP_Query */ public static function mla_get_shortcode_attachments($post_parent, $attr, $return_found_rows = NULL) { global $wp_query; /* * Parameters passed to the where and orderby filter functions */ self::$query_parameters = array(); /* * Parameters passed to the posts_search filter function in MLAData */ MLAData::$search_parameters = array('debug' => 'none'); /* * Make sure $attr is an array, even if it's empty */ if (empty($attr)) { $attr = array(); } elseif (is_string($attr)) { $attr = shortcode_parse_atts($attr); } /* * The "where used" queries have no $_REQUEST context available to them, * so tax_, date_ and meta_query evaluation will fail if they contain "{+request:" * parameters. Ignore these errors. */ if (isset($attr['where_used_query']) && 'this-is-a-where-used-query' == $attr['where_used_query']) { $where_used_query = true; unset($attr['where_used_query']); } else { $where_used_query = false; } /* * Merge input arguments with defaults, then extract the query arguments. * * $return_found_rows is used to indicate that the call comes from gallery_shortcode(), * which is the only call that supplies it. */ if (!is_null($return_found_rows)) { $attr = apply_filters('mla_gallery_query_attributes', $attr); } $arguments = shortcode_atts(self::$mla_get_shortcode_attachments_parameters, $attr); $mla_page_parameter = $arguments['mla_page_parameter']; unset($arguments['mla_page_parameter']); /* * $mla_page_parameter, if set, doesn't make it through the shortcode_atts filter, * so we handle it separately */ if (!isset($arguments[$mla_page_parameter])) { if (isset($attr[$mla_page_parameter])) { $arguments[$mla_page_parameter] = $attr[$mla_page_parameter]; } else { $arguments[$mla_page_parameter] = NULL; } } /* * 'RAND' is not documented in the codex, but is present in the code. * CODE REMOVED in WordPress 4.1 * / if ( 'RAND' == strtoupper( $arguments['order'] ) ) { $arguments['orderby'] = 'none'; unset( $arguments['order'] ); } // */ if (!empty($arguments['ids'])) { // 'ids' is explicitly ordered, unless you specify otherwise. if (empty($attr['orderby'])) { $arguments['orderby'] = 'post__in'; } $arguments['include'] = $arguments['ids']; } unset($arguments['ids']); if (!is_null($return_found_rows)) { $arguments = apply_filters('mla_gallery_query_arguments', $arguments); } /* * Extract taxonomy arguments */ $query_arguments = array(); if (!empty($attr)) { $all_taxonomies = get_taxonomies(array('show_ui' => true), 'names'); $simple_tax_queries = array(); foreach ($attr as $key => $value) { if ('tax_query' == $key) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $tax_query = NULL; $value = self::_sanitize_query_specification($value); /* * Replace invalid queries from "where-used" callers with a harmless equivalent */ if ($where_used_query && false !== strpos($value, '{+')) { $value = "array( array( 'taxonomy' => 'none', 'field' => 'slug', 'terms' => 'none' ) )"; } $function = @create_function('', 'return ' . $value . ';'); if (is_callable($function)) { $tax_query = $function(); } if (is_array($tax_query)) { $query_arguments[$key] = $tax_query; break; // Done - the tax_query overrides all other taxonomy parameters } else { return '<p>' . __('ERROR', 'media-library-assistant') . ': ' . __('Invalid mla_gallery', 'media-library-assistant') . ' tax_query = ' . var_export($value, true) . '</p>'; } } // not array } elseif (array_key_exists($key, $all_taxonomies)) { $simple_tax_queries[$key] = implode(',', array_filter(array_map('trim', explode(',', $value)))); } // array_key_exists } //foreach $attr /* * One of five outcomes: * 1) An explicit tax_query was found; use it and ignore all other taxonomy parameters * 2) No tax query is present; no further processing required * 3) Two or more simple tax queries are present; compose a tax_query * 4) One simple tax query and (tax_operator or tax_include_children) are present; compose a tax_query * 5) One simple tax query is present; use it as-is or convert 'category' to 'category_name' */ if (isset($query_arguments['tax_query']) || empty($simple_tax_queries)) { // No further action required } elseif (1 < count($simple_tax_queries) || isset($attr['tax_operator']) || isset($attr['tax_include_children'])) { // Build a tax_query if (1 < count($simple_tax_queries)) { $tax_relation = 'AND'; if (isset($attr['tax_relation'])) { if ('OR' == strtoupper($attr['tax_relation'])) { $tax_relation = 'OR'; } } $tax_query = array('relation' => $tax_relation); } else { $tax_query = array(); } // Validate other tax_query parameters or set defaults $tax_operator = 'IN'; if (isset($attr['tax_operator'])) { $attr_value = strtoupper($attr['tax_operator']); if (in_array($attr_value, array('IN', 'NOT IN', 'AND'))) { $tax_operator = $attr_value; } } $tax_include_children = true; if (isset($attr['tax_include_children'])) { if ('FALSE' == strtoupper($attr['tax_include_children'])) { $tax_include_children = false; } } foreach ($simple_tax_queries as $key => $value) { $tax_query[] = array('taxonomy' => $key, 'field' => 'slug', 'terms' => explode(',', $value), 'operator' => $tax_operator, 'include_children' => $tax_include_children); } $query_arguments['tax_query'] = $tax_query; } else { // exactly one simple query is present if (isset($simple_tax_queries['category'])) { $arguments['category_name'] = $simple_tax_queries['category']; } else { $query_arguments = $simple_tax_queries; } } } // ! empty /* * $query_arguments has been initialized in the taxonomy code above. */ $is_tax_query = !($use_children = empty($query_arguments)); foreach ($arguments as $key => $value) { /* * There are several "fallthru" cases in this switch statement that decide * whether or not to limit the query to children of a specific post. */ $children_ok = true; switch ($key) { case 'post_parent': switch (strtolower($value)) { case 'all': $value = NULL; $use_children = false; break; case 'any': self::$query_parameters['post_parent'] = 'any'; $value = NULL; $use_children = false; break; case 'current': $value = $post_parent; $use_children = true; break; case 'none': self::$query_parameters['post_parent'] = 'none'; $value = NULL; $use_children = false; break; } // fallthru // fallthru case 'id': if (is_numeric($value)) { $query_arguments[$key] = intval($value); if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'numberposts': case 'posts_per_page': case 'posts_per_archive_page': if (is_numeric($value)) { $value = intval($value); if (!empty($value)) { $query_arguments[$key] = $value; } } unset($arguments[$key]); break; case 'meta_value_num': $children_ok = false; // fallthru // fallthru case 'offset': if (is_numeric($value)) { $query_arguments[$key] = intval($value); if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'paged': if ('current' == strtolower($value)) { /* * Note: The query variable 'page' holds the pagenumber for a single paginated * Post or Page that includes the <!--nextpage--> Quicktag in the post content. */ if (get_query_var('page')) { $query_arguments[$key] = get_query_var('page'); } else { $query_arguments[$key] = get_query_var('paged') ? get_query_var('paged') : 1; } } elseif (is_numeric($value)) { $query_arguments[$key] = intval($value); } elseif ('' === $value) { $query_arguments[$key] = 1; } unset($arguments[$key]); break; case $mla_page_parameter: case 'mla_paginate_total': if (is_numeric($value)) { $query_arguments[$key] = intval($value); } elseif ('' === $value) { $query_arguments[$key] = 1; } unset($arguments[$key]); break; case 'author': case 'cat': case 'tag_id': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = array_filter($value); } else { $query_arguments[$key] = array_filter(array_map('intval', explode(",", $value))); } if (1 == count($query_arguments[$key])) { $query_arguments[$key] = $query_arguments[$key][0]; } else { $query_arguments[$key] = implode(',', $query_arguments[$key]); } $use_children = false; } unset($arguments[$key]); break; case 'category__and': case 'category__in': case 'category__not_in': case 'tag__and': case 'tag__in': case 'tag__not_in': case 'include': $children_ok = false; // fallthru // fallthru case 'exclude': if (!empty($value)) { if (is_array($value)) { $value = array_filter($value); } else { $value = array_filter(array_map('intval', explode(",", $value))); } if (!empty($value)) { $query_arguments[$key] = $value; if (!$children_ok) { $use_children = false; } } } unset($arguments[$key]); break; case 'tag_slug__and': case 'tag_slug__in': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $query_arguments[$key] = array_filter(array_map('trim', explode(",", $value))); } $use_children = false; } unset($arguments[$key]); break; case 'nopaging': // boolean value, default false if (!empty($value) && 'false' != strtolower($value)) { $query_arguments[$key] = true; } unset($arguments[$key]); break; // boolean values, default true // boolean values, default true case 'cache_results': case 'update_post_meta_cache': case 'update_post_term_cache': if (!empty($value) && 'true' != strtolower($value)) { $query_arguments[$key] = false; } unset($arguments[$key]); break; case 'sentence': case 'exact': if (!empty($value) && 'true' == strtolower($value)) { MLAData::$search_parameters[$key] = true; } else { MLAData::$search_parameters[$key] = false; } unset($arguments[$key]); break; case 'mla_search_connector': case 'mla_phrase_connector': case 'mla_term_connector': if (!empty($value) && 'OR' == strtoupper($value)) { MLAData::$search_parameters[$key] = 'OR'; } else { MLAData::$search_parameters[$key] = 'AND'; } unset($arguments[$key]); break; case 'mla_terms_phrases': $children_ok = false; // fallthru // fallthru case 'mla_terms_taxonomies': case 'mla_search_fields': if (!empty($value)) { MLAData::$search_parameters[$key] = $value; if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 's': MLAData::$search_parameters['s'] = $value; // fallthru // fallthru case 'author_name': case 'category_name': case 'tag': case 'meta_key': case 'meta_value': case 'meta_compare': $children_ok = false; // fallthru // fallthru case 'post_type': case 'post_status': case 'post_mime_type': case 'orderby': if (!empty($value)) { $query_arguments[$key] = $value; if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'order': if (!empty($value)) { $value = strtoupper($value); if (in_array($value, array('ASC', 'DESC'))) { $query_arguments[$key] = $value; } } unset($arguments[$key]); break; case 'date_query': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $date_query = NULL; $value = self::_sanitize_query_specification($value); /* * Replace invalid queries from "where-used" callers with a harmless equivalent */ if ($where_used_query && false !== strpos($value, '{+')) { $value = "array( array( 'key' => 'unlikely', 'value' => 'none or otherwise unlikely' ) )"; } $function = @create_function('', 'return ' . $value . ';'); if (is_callable($function)) { $date_query = $function(); } if (is_array($date_query)) { $query_arguments[$key] = $date_query; } else { return '<p>' . __('ERROR', 'media-library-assistant') . ': ' . __('Invalid mla_gallery', 'media-library-assistant') . ' date_query = ' . var_export($value, true) . '</p>'; } } // not array $use_children = false; } unset($arguments[$key]); break; case 'meta_query': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $meta_query = NULL; $value = self::_sanitize_query_specification($value); /* * Replace invalid queries from "where-used" callers with a harmless equivalent */ if ($where_used_query && false !== strpos($value, '{+')) { $value = "array( array( 'key' => 'unlikely', 'value' => 'none or otherwise unlikely' ) )"; } $function = @create_function('', 'return ' . $value . ';'); if (is_callable($function)) { $meta_query = $function(); } if (is_array($meta_query)) { $query_arguments[$key] = $meta_query; } else { return '<p>' . __('ERROR', 'media-library-assistant') . ': ' . __('Invalid mla_gallery', 'media-library-assistant') . ' meta_query = ' . var_export($value, true) . '</p>'; } } // not array $use_children = false; } unset($arguments[$key]); break; case 'fields': if (!empty($value)) { $value = strtolower($value); if (in_array($value, array('ids', 'id=>parent'))) { $query_arguments[$key] = $value; } } unset($arguments[$key]); break; default: // ignore anything else } // switch $key } // foreach $arguments /* * Decide whether to use a "get_children" style query */ self::$query_parameters['disable_tax_join'] = $is_tax_query && !$use_children; if ($use_children && !isset($query_arguments['post_parent'])) { if (!isset($query_arguments['id'])) { $query_arguments['post_parent'] = $post_parent; } else { $query_arguments['post_parent'] = $query_arguments['id']; } unset($query_arguments['id']); } if (isset($query_arguments['numberposts']) && !isset($query_arguments['posts_per_page'])) { $query_arguments['posts_per_page'] = $query_arguments['numberposts']; } unset($query_arguments['numberposts']); /* * MLA pagination will override WordPress pagination */ if (isset($query_arguments[$mla_page_parameter])) { unset($query_arguments['nopaging']); unset($query_arguments['offset']); unset($query_arguments['paged']); if (isset($query_arguments['mla_paginate_total']) && $query_arguments[$mla_page_parameter] > $query_arguments['mla_paginate_total']) { $query_arguments['offset'] = 0x7fffffff; // suppress further output } else { $query_arguments['paged'] = $query_arguments[$mla_page_parameter]; } } else { if (isset($query_arguments['posts_per_page']) || isset($query_arguments['posts_per_archive_page']) || isset($query_arguments['paged']) || isset($query_arguments['offset'])) { unset($query_arguments['nopaging']); } } unset($query_arguments[$mla_page_parameter]); unset($query_arguments['mla_paginate_total']); if (isset($query_arguments['post_mime_type']) && 'all' == strtolower($query_arguments['post_mime_type'])) { unset($query_arguments['post_mime_type']); } if (!empty($query_arguments['include'])) { $incposts = wp_parse_id_list($query_arguments['include']); $query_arguments['posts_per_page'] = count($incposts); // only the number of posts included $query_arguments['post__in'] = $incposts; unset($query_arguments['include']); } elseif (!empty($query_arguments['exclude'])) { $query_arguments['post__not_in'] = wp_parse_id_list($query_arguments['exclude']); unset($query_arguments['exclude']); } $query_arguments['ignore_sticky_posts'] = true; $query_arguments['no_found_rows'] = is_null($return_found_rows) ? true : !$return_found_rows; /* * We will always handle "orderby" in our filter */ self::$query_parameters['orderby'] = self::_validate_sql_orderby($query_arguments); if (false === self::$query_parameters['orderby']) { unset(self::$query_parameters['orderby']); } unset($query_arguments['orderby']); unset($query_arguments['order']); if (self::$mla_debug) { add_filter('posts_clauses', 'MLAShortcodes::mla_shortcode_query_posts_clauses_filter', 0x7fffffff, 1); add_filter('posts_clauses_request', 'MLAShortcodes::mla_shortcode_query_posts_clauses_request_filter', 0x7fffffff, 1); } add_filter('posts_join', 'MLAShortcodes::mla_shortcode_query_posts_join_filter', 0x7fffffff, 1); add_filter('posts_where', 'MLAShortcodes::mla_shortcode_query_posts_where_filter', 0x7fffffff, 1); add_filter('posts_orderby', 'MLAShortcodes::mla_shortcode_query_posts_orderby_filter', 0x7fffffff, 1); /* * Handle the keyword and terms search in the posts_search filter. * One or both of 'mla_terms_phrases' and 's' must be present to * trigger the search. */ if (empty(MLAData::$search_parameters['mla_terms_phrases']) && empty(MLAData::$search_parameters['s'])) { MLAData::$search_parameters = array('debug' => 'none'); } else { /* * Convert Terms Search parameters to the filter's requirements. * mla_terms_taxonomies is shared with keyword search. */ if (empty(MLAData::$search_parameters['mla_terms_taxonomies'])) { MLAData::$search_parameters['mla_terms_search']['taxonomies'] = MLAOptions::mla_supported_taxonomies('term-search'); } else { MLAData::$search_parameters['mla_terms_search']['taxonomies'] = array_filter(array_map('trim', explode(',', MLAData::$search_parameters['mla_terms_taxonomies']))); } if (!empty(MLAData::$search_parameters['mla_terms_phrases'])) { MLAData::$search_parameters['mla_terms_search']['phrases'] = MLAData::$search_parameters['mla_terms_phrases']; if (empty(MLAData::$search_parameters['mla_phrase_connector'])) { MLAData::$search_parameters['mla_terms_search']['radio_phrases'] = 'AND'; } else { MLAData::$search_parameters['mla_terms_search']['radio_phrases'] = MLAData::$search_parameters['mla_phrase_connector']; } if (empty(MLAData::$search_parameters['mla_term_connector'])) { MLAData::$search_parameters['mla_terms_search']['radio_terms'] = 'OR'; } else { MLAData::$search_parameters['mla_terms_search']['radio_terms'] = MLAData::$search_parameters['mla_phrase_connector']; } } unset(MLAData::$search_parameters['mla_terms_phrases']); unset(MLAData::$search_parameters['mla_terms_taxonomies']); unset(MLAData::$search_parameters['mla_phrase_connector']); unset(MLAData::$search_parameters['mla_term_connector']); if (empty(MLAData::$search_parameters['mla_search_fields'])) { MLAData::$search_parameters['mla_search_fields'] = array('title', 'content'); } else { MLAData::$search_parameters['mla_search_fields'] = array_filter(array_map('trim', explode(',', MLAData::$search_parameters['mla_search_fields']))); MLAData::$search_parameters['mla_search_fields'] = array_intersect(array('title', 'content', 'excerpt', 'name', 'terms'), MLAData::$search_parameters['mla_search_fields']); /* * Look for keyword search including 'terms' */ foreach (MLAData::$search_parameters['mla_search_fields'] as $index => $field) { if ('terms' == $field) { if (isset(MLAData::$search_parameters['mla_terms_search']['phrases'])) { /* * The Terms Search overrides any terms-based keyword search for now; too complicated. */ unset(MLAData::$search_parameters['mla_search_fields'][$index]); } else { MLAData::$search_parameters['mla_search_taxonomies'] = MLAData::$search_parameters['mla_terms_search']['taxonomies']; unset(MLAData::$search_parameters['mla_terms_search']['taxonomies']); } } // terms in search fields } } // mla_search_fields present if (empty(MLAData::$search_parameters['mla_search_connector'])) { MLAData::$search_parameters['mla_search_connector'] = 'AND'; } if (empty(MLAData::$search_parameters['sentence'])) { MLAData::$search_parameters['sentence'] = false; } if (empty(MLAData::$search_parameters['exact'])) { MLAData::$search_parameters['exact'] = false; } MLAData::$search_parameters['debug'] = self::$mla_debug ? 'shortcode' : 'none'; add_filter('posts_search', 'MLAData::mla_query_posts_search_filter', 10, 2); add_filter('posts_groupby', 'MLAData::mla_query_posts_groupby_filter'); } if (self::$mla_debug) { global $wp_filter; foreach ($wp_filter['posts_where'] as $priority => $filters) { $debug_message = '<strong>mla_debug $wp_filter[posts_where]</strong> priority = ' . var_export($priority, true) . '<br />'; foreach ($filters as $name => $descriptor) { $debug_message .= 'filter name = ' . var_export($name, true) . '<br />'; } MLA::mla_debug_add($debug_message); } foreach ($wp_filter['posts_orderby'] as $priority => $filters) { $debug_message = '<strong>mla_debug $wp_filter[posts_orderby]</strong> priority = ' . var_export($priority, true) . '<br />'; foreach ($filters as $name => $descriptor) { $debug_message .= 'filter name = ' . var_export($name, true) . '<br />'; } MLA::mla_debug_add($debug_message); } } /* * Disable Relevanssi - A Better Search, v3.2 by Mikko Saari * relevanssi_prevent_default_request( $request, $query ) * apply_filters('relevanssi_admin_search_ok', $admin_search_ok, $query ); */ if (function_exists('relevanssi_prevent_default_request')) { add_filter('relevanssi_admin_search_ok', 'MLAData::mla_query_relevanssi_admin_search_ok_filter'); } self::$mla_gallery_wp_query_object = new WP_Query(); $attachments = self::$mla_gallery_wp_query_object->query($query_arguments); /* * $return_found_rows is used to indicate that the call comes from gallery_shortcode(), * which is the only call that supplies it. */ if (is_null($return_found_rows)) { $return_found_rows = false; } else { do_action('mla_gallery_wp_query_object', $query_arguments); } if ($return_found_rows) { $attachments['found_rows'] = self::$mla_gallery_wp_query_object->found_posts; } if (!empty(MLAData::$search_parameters)) { remove_filter('posts_search', 'MLAData::mla_query_posts_search_filter'); } if (function_exists('relevanssi_prevent_default_request')) { remove_filter('relevanssi_admin_search_ok', 'MLAData::mla_query_relevanssi_admin_search_ok_filter'); } remove_filter('posts_join', 'MLAShortcodes::mla_shortcode_query_posts_join_filter', 0x7fffffff); remove_filter('posts_where', 'MLAShortcodes::mla_shortcode_query_posts_where_filter', 0x7fffffff); remove_filter('posts_orderby', 'MLAShortcodes::mla_shortcode_query_posts_orderby_filter', 0x7fffffff); if (self::$mla_debug) { remove_filter('posts_clauses', 'MLAShortcodes::mla_shortcode_query_posts_clauses_filter', 0x7fffffff); remove_filter('posts_clauses_request', 'MLAShortcodes::mla_shortcode_query_posts_clauses_request_filter', 0x7fffffff); MLA::mla_debug_add('<strong>' . __('mla_debug query', 'media-library-assistant') . '</strong> = ' . var_export($query_arguments, true)); MLA::mla_debug_add('<strong>' . __('mla_debug request', 'media-library-assistant') . '</strong> = ' . var_export(self::$mla_gallery_wp_query_object->request, true)); MLA::mla_debug_add('<strong>' . __('mla_debug query_vars', 'media-library-assistant') . '</strong> = ' . var_export(self::$mla_gallery_wp_query_object->query_vars, true)); MLA::mla_debug_add('<strong>' . __('mla_debug post_count', 'media-library-assistant') . '</strong> = ' . var_export(self::$mla_gallery_wp_query_object->post_count, true)); } self::$mla_gallery_wp_query_object = NULL; return $attachments; }
/** * Parses shortcode parameters and returns the gallery objects * * @since .50 * * @param int Post ID of the parent * @param array Attributes of the shortcode * @param boolean true to calculate and return ['found_posts'] as an array element * * @return array List of attachments returned from WP_Query */ public static function mla_get_shortcode_attachments($post_parent, $attr, $return_found_rows = NULL) { global $wp_query; /* * Parameters passed to the where and orderby filter functions */ self::$query_parameters = array(); /* * Make sure $attr is an array, even if it's empty */ if (empty($attr)) { $attr = array(); } elseif (is_string($attr)) { $attr = shortcode_parse_atts($attr); } /* * Merge input arguments with defaults, then extract the query arguments. * * $return_found_rows is used to indicate that the call comes from gallery_shortcode(), * which is the only call that supplies it. */ if (!is_null($return_found_rows)) { $attr = apply_filters('mla_gallery_query_attributes', $attr); } $arguments = shortcode_atts(self::$data_selection_parameters, $attr); $mla_page_parameter = $arguments['mla_page_parameter']; unset($arguments['mla_page_parameter']); /* * $mla_page_parameter, if set, doesn't make it through the shortcode_atts filter, * so we handle it separately */ if (!isset($arguments[$mla_page_parameter])) { if (isset($attr[$mla_page_parameter])) { $arguments[$mla_page_parameter] = $attr[$mla_page_parameter]; } else { $arguments[$mla_page_parameter] = NULL; } } /* * 'RAND' is not documented in the codex, but is present in the code. */ if ('RAND' == strtoupper($arguments['order'])) { $arguments['orderby'] = 'none'; unset($arguments['order']); } if (!empty($arguments['ids'])) { // 'ids' is explicitly ordered, unless you specify otherwise. if (empty($attr['orderby'])) { $arguments['orderby'] = 'post__in'; } $arguments['include'] = $arguments['ids']; } unset($arguments['ids']); if (!is_null($return_found_rows)) { $arguments = apply_filters('mla_gallery_query_arguments', $arguments); } /* * Extract taxonomy arguments */ $taxonomies = get_taxonomies(array('show_ui' => true), 'names'); // 'objects' $query_arguments = array(); if (!empty($attr)) { foreach ($attr as $key => $value) { if ('tax_query' == $key) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $tax_query = NULL; $value = self::_sanitize_query_specification($value); $function = @create_function('', 'return ' . $value . ';'); if (is_callable($function)) { $tax_query = $function(); } if (is_array($tax_query)) { $query_arguments[$key] = $tax_query; } else { return '<p>ERROR: invalid mla_gallery tax_query = ' . var_export($value, true) . '</p>'; } } // not array } elseif (array_key_exists($key, $taxonomies)) { $query_arguments[$key] = implode(',', array_filter(array_map('trim', explode(',', $value)))); if ('false' == strtolower(trim($arguments['tax_include_children']))) { $arguments['tax_include_children'] = false; if ('' == $arguments['tax_operator']) { $arguments['tax_operator'] = 'OR'; } } else { $arguments['tax_include_children'] = true; } if (in_array(strtoupper($arguments['tax_operator']), array('OR', 'IN', 'NOT IN', 'AND'))) { $query_arguments['tax_query'] = array(array('taxonomy' => $key, 'field' => 'slug', 'terms' => explode(',', $query_arguments[$key]), 'operator' => strtoupper($arguments['tax_operator']), 'include_children' => $arguments['tax_include_children'])); unset($query_arguments[$key]); } } // array_key_exists } //foreach $attr } // ! empty unset($arguments['tax_operator']); unset($arguments['tax_include_children']); /* * $query_arguments has been initialized in the taxonomy code above. */ $use_children = empty($query_arguments); foreach ($arguments as $key => $value) { /* * There are several "fallthru" cases in this switch statement that decide * whether or not to limit the query to children of a specific post. */ $children_ok = true; switch ($key) { case 'post_parent': switch (strtolower($value)) { case 'all': $value = NULL; $use_children = false; break; case 'any': self::$query_parameters['post_parent'] = 'any'; $value = NULL; $use_children = false; break; case 'current': $value = $post_parent; break; case 'none': self::$query_parameters['post_parent'] = 'none'; $value = NULL; $use_children = false; break; } // fallthru // fallthru case 'id': if (is_numeric($value)) { $query_arguments[$key] = intval($value); if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'numberposts': case 'posts_per_page': case 'posts_per_archive_page': if (is_numeric($value)) { $value = intval($value); if (!empty($value)) { $query_arguments[$key] = $value; } } unset($arguments[$key]); break; case 'meta_value_num': $children_ok = false; // fallthru // fallthru case 'offset': if (is_numeric($value)) { $query_arguments[$key] = intval($value); if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'paged': if ('current' == strtolower($value)) { /* * Note: The query variable 'page' holds the pagenumber for a single paginated * Post or Page that includes the <!--nextpage--> Quicktag in the post content. */ if (get_query_var('page')) { $query_arguments[$key] = get_query_var('page'); } else { $query_arguments[$key] = get_query_var('paged') ? get_query_var('paged') : 1; } } elseif (is_numeric($value)) { $query_arguments[$key] = intval($value); } elseif ('' === $value) { $query_arguments[$key] = 1; } unset($arguments[$key]); break; case $mla_page_parameter: case 'mla_paginate_total': if (is_numeric($value)) { $query_arguments[$key] = intval($value); } elseif ('' === $value) { $query_arguments[$key] = 1; } unset($arguments[$key]); break; case 'author': case 'cat': case 'tag_id': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = array_filter($value); } else { $query_arguments[$key] = array_filter(array_map('intval', explode(",", $value))); } if (1 == count($query_arguments[$key])) { $query_arguments[$key] = $query_arguments[$key][0]; } else { $query_arguments[$key] = implode(',', $query_arguments[$key]); } $use_children = false; } unset($arguments[$key]); break; case 'category__and': case 'category__in': case 'category__not_in': case 'tag__and': case 'tag__in': case 'tag__not_in': case 'include': $children_ok = false; // fallthru // fallthru case 'exclude': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = array_filter($value); } else { $query_arguments[$key] = array_filter(array_map('intval', explode(",", $value))); } if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'tag_slug__and': case 'tag_slug__in': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $query_arguments[$key] = array_filter(array_map('trim', explode(",", $value))); } $use_children = false; } unset($arguments[$key]); break; case 'nopaging': // boolean if (!empty($value) && 'false' != strtolower($value)) { $query_arguments[$key] = true; } unset($arguments[$key]); break; case 'author_name': case 'category_name': case 'tag': case 'meta_key': case 'meta_value': case 'meta_compare': case 's': $children_ok = false; // fallthru // fallthru case 'post_type': case 'post_status': case 'post_mime_type': case 'orderby': if (!empty($value)) { $query_arguments[$key] = $value; if (!$children_ok) { $use_children = false; } } unset($arguments[$key]); break; case 'order': if (!empty($value)) { $value = strtoupper($value); if (in_array($value, array('ASC', 'DESC'))) { $query_arguments[$key] = $value; } } unset($arguments[$key]); break; case 'meta_query': if (!empty($value)) { if (is_array($value)) { $query_arguments[$key] = $value; } else { $meta_query = NULL; $value = self::_sanitize_query_specification($value); $function = @create_function('', 'return ' . $value . ';'); if (is_callable($function)) { $meta_query = $function(); } if (is_array($meta_query)) { $query_arguments[$key] = $meta_query; } else { return '<p>ERROR: invalid mla_gallery meta_query = ' . var_export($value, true) . '</p>'; } } // not array $use_children = false; } unset($arguments[$key]); break; default: // ignore anything else } // switch $key } // foreach $arguments /* * Decide whether to use a "get_children" style query */ if ($use_children && !isset($query_arguments['post_parent'])) { if (!isset($query_arguments['id'])) { $query_arguments['post_parent'] = $post_parent; } else { $query_arguments['post_parent'] = $query_arguments['id']; } unset($query_arguments['id']); } if (isset($query_arguments['numberposts']) && !isset($query_arguments['posts_per_page'])) { $query_arguments['posts_per_page'] = $query_arguments['numberposts']; } unset($query_arguments['numberposts']); /* * MLA pagination will override WordPress pagination */ if (isset($query_arguments[$mla_page_parameter])) { unset($query_arguments['nopaging']); unset($query_arguments['offset']); unset($query_arguments['paged']); if (isset($query_arguments['mla_paginate_total']) && $query_arguments[$mla_page_parameter] > $query_arguments['mla_paginate_total']) { $query_arguments['offset'] = 0x7fffffff; } else { $query_arguments['paged'] = $query_arguments[$mla_page_parameter]; } } else { if (isset($query_arguments['posts_per_page']) || isset($query_arguments['posts_per_archive_page']) || isset($query_arguments['paged']) || isset($query_arguments['offset'])) { unset($query_arguments['nopaging']); } } unset($query_arguments[$mla_page_parameter]); unset($query_arguments['mla_paginate_total']); if (isset($query_arguments['post_mime_type']) && 'all' == strtolower($query_arguments['post_mime_type'])) { unset($query_arguments['post_mime_type']); } if (!empty($query_arguments['include'])) { $incposts = wp_parse_id_list($query_arguments['include']); $query_arguments['posts_per_page'] = count($incposts); // only the number of posts included $query_arguments['post__in'] = $incposts; } elseif (!empty($query_arguments['exclude'])) { $query_arguments['post__not_in'] = wp_parse_id_list($query_arguments['exclude']); } $query_arguments['ignore_sticky_posts'] = true; $query_arguments['no_found_rows'] = is_null($return_found_rows) ? true : !$return_found_rows; /* * We will always handle "orderby" in our filter */ self::$query_parameters['orderby'] = self::_validate_sql_orderby($query_arguments); if (false === self::$query_parameters['orderby']) { unset(self::$query_parameters['orderby']); } unset($query_arguments['orderby']); unset($query_arguments['order']); if (self::$mla_debug) { add_filter('posts_clauses', 'MLAShortcodes::mla_shortcode_query_posts_clauses_filter', 0x7fffffff, 1); add_filter('posts_clauses_request', 'MLAShortcodes::mla_shortcode_query_posts_clauses_request_filter', 0x7fffffff, 1); } add_filter('posts_orderby', 'MLAShortcodes::mla_shortcode_query_posts_orderby_filter', 0x7fffffff, 1); add_filter('posts_where', 'MLAShortcodes::mla_shortcode_query_posts_where_filter', 0x7fffffff, 1); if (self::$mla_debug) { global $wp_filter; self::$mla_debug_messages .= '<p><strong>mla_debug $wp_filter[posts_where]</strong> = ' . var_export($wp_filter['posts_where'], true) . '</p>'; self::$mla_debug_messages .= '<p><strong>mla_debug $wp_filter[posts_orderby]</strong> = ' . var_export($wp_filter['posts_orderby'], true) . '</p>'; } self::$mla_gallery_wp_query_object = new WP_Query(); $attachments = self::$mla_gallery_wp_query_object->query($query_arguments); /* * $return_found_rows is used to indicate that the call comes from gallery_shortcode(), * which is the only call that supplies it. */ if (is_null($return_found_rows)) { $return_found_rows = false; } else { do_action('mla_gallery_wp_query_object', $query_arguments); } if ($return_found_rows) { $attachments['found_rows'] = self::$mla_gallery_wp_query_object->found_posts; } remove_filter('posts_where', 'MLAShortcodes::mla_shortcode_query_posts_where_filter', 0x7fffffff, 1); remove_filter('posts_orderby', 'MLAShortcodes::mla_shortcode_query_posts_orderby_filter', 0x7fffffff, 1); if (self::$mla_debug) { remove_filter('posts_clauses', 'MLAShortcodes::mla_shortcode_query_posts_clauses_filter', 0x7fffffff, 1); remove_filter('posts_clauses_request', 'MLAShortcodes::mla_shortcode_query_posts_clauses_request_filter', 0x7fffffff, 1); self::$mla_debug_messages .= '<p><strong>mla_debug query</strong> = ' . var_export($query_arguments, true) . '</p>'; self::$mla_debug_messages .= '<p><strong>mla_debug request</strong> = ' . var_export(self::$mla_gallery_wp_query_object->request, true) . '</p>'; self::$mla_debug_messages .= '<p><strong>mla_debug query_vars</strong> = ' . var_export(self::$mla_gallery_wp_query_object->query_vars, true) . '</p>'; self::$mla_debug_messages .= '<p><strong>mla_debug post_count</strong> = ' . var_export(self::$mla_gallery_wp_query_object->post_count, true) . '</p>'; } self::$mla_gallery_wp_query_object = NULL; return $attachments; }