function mpp_filter_archive_page_galleries($query) { if (is_admin()) { return; } if (!$query->is_main_query() || !$query->is_post_type_archive(mpp_get_gallery_post_type())) { return; } //confirmed that we are on gallery archive page $active_components = mpp_get_active_components(); $active_types = mpp_get_active_types(); $status = 'public'; $tax_query = $query->get('tax_query'); if (empty($tax_query)) { $tax_query = array(); } //it will be always true if ($status) { $status = mpp_string_to_array($status); //future proofing $status_keys = array_map('mpp_underscore_it', $status); $tax_query[] = array('taxonomy' => mpp_get_status_taxname(), 'field' => 'slug', 'terms' => $status_keys, 'operator' => 'IN'); } //should we only show sitewide galleries here? will update based on feedback if (!empty($active_components)) { $component_keys = array_keys($active_components); $component_keys = array_map('mpp_underscore_it', $component_keys); $tax_query[] = array('taxonomy' => mpp_get_component_taxname(), 'field' => 'slug', 'terms' => $component_keys, 'operator' => 'IN'); } if (!empty($active_types)) { $type_keys = array_keys($active_types); $type_keys = array_map('mpp_underscore_it', $type_keys); $tax_query[] = array('taxonomy' => mpp_get_type_taxname(), 'field' => 'slug', 'terms' => $type_keys, 'operator' => 'IN'); } if (count($tax_query) > 1) { $tax_query['relation'] = 'AND'; } $query->set('tax_query', $tax_query); $query->set('update_post_term_cache', true); $query->set('update_post_meta_cache', true); $query->set('cache_results', true); }
/** * Deregister an already registered media size * * @param mixed $args { * * @type string $name required, the name of the registered media size * @type string|array type(s) for which to be deregistered. e.g 'audio,video,photo' or 'audio,photo' or array('audio', 'photo') * * } */ function mpp_deregister_media_size($args) { extract($args); if (!$name || !$type) { return false; // can not de register } $mp = mediapress(); $types = mpp_string_to_array($type); //remove the size setting for each type foreach ($types as $media_type) { unset($mp->media_sizes[$media_type][$name]); } return true; }
/** * Map gallery parameters to wp_query native parameters * * @param type $args * @return string */ public function build_params($args) { $defaults = array('type' => array_keys(mpp_get_active_types()), 'id' => false, 'in' => false, 'exclude' => false, 'slug' => false, 'status' => array_keys(mpp_get_active_statuses()), 'component' => array_keys(mpp_get_active_components()), 'component_id' => false, 'per_page' => mpp_get_option('galleries_per_page'), 'offset' => false, 'page' => false, 'nopaging' => false, 'order' => 'DESC', 'orderby' => 'date', 'user_id' => false, 'include_users' => false, 'exclude_users' => false, 'user_name' => false, 'scope' => false, 'search_terms' => '', 'year' => false, 'month' => false, 'week' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '', 'yearmonth' => false, 'fields' => false); //build params for WP_Query $r = wp_parse_args($args, $defaults); extract($r, EXTR_SKIP); //build the wp_query args $wp_query_args = array('post_type' => mpp_get_gallery_post_type(), 'post_status' => 'any', 'p' => $id, 'post__in' => $in, 'post__not_in' => $exclude, 'name' => $slug, 'posts_per_page' => $per_page, 'paged' => $page, 'offset' => $offset, 'nopaging' => $nopaging, 'author' => $user_id, 'author_name' => $user_name, 'author__in' => $include_users, 'author__not_in' => $exclude_users, 'year' => $year, 'monthnum' => $month, 'w' => $week, 'day' => $day, 'hour' => $hour, 'minute' => $minute, 'second' => $second, 'm' => $yearmonth, 'order' => $order, 'orderby' => $orderby, 's' => $search_terms, 'fields' => $fields, '_mpp_mapped_query' => true); $tax_query = array(); $gmeta_query = array(); //meta if (isset($meta_key) && $meta_key) { $wp_query_args['meta_key'] = $meta_key; } if (isset($meta_value)) { $wp_query_args['meta_value'] = $meta_value; } if (isset($meta_query)) { $gmeta_query = $meta_query; } //TODO: SCOPE // //we will need to build tax query/meta query //type, audio video etc //if type is given and it is valid gallery type //Pass one or more types //should we restrict to active types only here? I guss no, Insteacd the calling scope should take care of that if (!empty($type) && mpp_are_registered_types($type)) { $type = mpp_string_to_array($type); $type = array_map('mpp_underscore_it', $type); $tax_query[] = array('taxonomy' => mpp_get_type_taxname(), 'field' => 'slug', 'terms' => $type, 'operator' => 'IN'); } //privacy //pass ne or more privacy level if (!empty($status) && mpp_are_registered_statuses($status)) { $status = mpp_string_to_array($status); $status = array_map('mpp_underscore_it', $status); $tax_query[] = array('taxonomy' => mpp_get_status_taxname(), 'field' => 'slug', 'terms' => $status, 'operator' => 'IN'); } if (!empty($component) && mpp_are_registered_components($component)) { $component = mpp_string_to_array($component); $component = array_map('mpp_underscore_it', $component); $tax_query[] = array('taxonomy' => mpp_get_component_taxname(), 'field' => 'slug', 'terms' => $component, 'operator' => 'IN'); } //done with the tax query if (count($tax_query) > 1) { $tax_query['relation'] = 'AND'; } $wp_query_args['tax_query'] = $tax_query; // print_nice($wp_query_args); //meta query //now, for component if (!empty($component_id)) { $meta_compare = '='; if (is_array($component_id)) { $meta_compare = 'IN'; } $gmeta_query[] = array('key' => '_mpp_component_id', 'value' => $component_id, 'compare' => $meta_compare, 'type' => 'UNSIGNED'); } //reset meta query if (!empty($gmeta_query)) { $wp_query_args['meta_query'] = $gmeta_query; } // print_r($wp_query_args); return $wp_query_args; }
/** * Are these types valid * * Used to validated agains a list of types * * @param type $types * @return boolean */ function mpp_are_active_types($types) { if (empty($types)) { return false; } $types = mpp_string_to_array($types); $valid_types = mpp_get_active_types(); $valid_types = array_keys($valid_types); //get keys as array $diff = array_diff($types, $valid_types); if (!empty($diff)) { //there exists atleast one unregistered type return false; } return true; }
/** * generate sql for tax queries where terms slugs and taxonomy is given * @param type $terms * @param type $taxonomy * @return string|boolean */ function mpp_get_tax_sql($terms, $taxonomy) { //for type, repeat it if ($terms) { //if the comma separated terms are given, convert it to array $terms = mpp_string_to_array($terms); //prepend underscore to each of the term //$terms = array_map( 'mpp_underscore_it', $terms ); //get the term_taxonomy ids array for component $term_tax_ids = mpp_get_tt_ids($terms, $taxonomy); $objects_in_terms_sql = mpp_get_objects_in_terms_sql($term_tax_ids); return $objects_in_terms_sql; //find all posts in associated with this tt } return false; }
public function build_params($args) { $defaults = array('type' => array_keys(mpp_get_active_types()), 'id' => false, 'in' => false, 'exclude' => false, 'slug' => false, 'status' => array_keys(mpp_get_active_statuses()), 'component' => array_keys(mpp_get_active_components()), 'component_id' => false, 'gallery_id' => false, 'galleries' => false, 'galleries_exclude' => false, 'storage' => '', 'per_page' => mpp_get_option('media_per_page'), 'offset' => false, 'page' => false, 'nopaging' => false, 'order' => 'DESC', 'orderby' => false, 'user_id' => false, 'user_name' => false, 'scope' => false, 'search_terms' => '', 'year' => false, 'month' => false, 'week' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '', 'yearmonth' => '', 'meta_query' => false, 'fields' => false); //build params for WP_Query /** * If are querying for a single gallery * and the gallery media were sorted by the user, show the media s in the sort order insted of the default date */ if (isset($args['gallery_id']) && mpp_is_gallery_sorted($args['gallery_id'])) { $defaults['orderby'] = 'menu_order'; } $r = wp_parse_args($args, $defaults); extract($r, EXTR_SKIP); //build the wp_query args $wp_query_args = array('post_type' => mpp_get_media_post_type(), 'post_status' => 'any', 'p' => $id, 'post__in' => $in, 'post__not_in' => $exclude, 'name' => $slug, 'post_parent' => $gallery_id, 'post_parent__in' => !empty($galleries) ? (array) $galleries : 0, 'post_parent__not_in' => !empty($galleries_exclude) ? (array) $galleries_exclude : 0, 'posts_per_page' => $per_page, 'paged' => $page, 'offset' => $offset, 'nopaging' => $nopaging, 'author' => $user_id, 'author_name' => $user_name, 'year' => $year, 'monthnum' => $month, 'w' => $week, 'day' => $day, 'hour' => $hour, 'minute' => $minute, 'second' => $second, 'm' => $yearmonth, 'order' => $order, 'orderby' => $orderby, 's' => $search_terms, 'fields' => $fields, '_mpp_mapped_query' => true); //TODO: SCOPE // //we will need to build tax query/meta query //taxonomy query to filter by component|status|privacy $tax_query = array(); //meta query $gmeta_query = array(); if (isset($meta_key) && $meta_key) { $wp_query_args['meta_key'] = $meta_key; } if (isset($meta_key) && $meta_key && isset($meta_value)) { $wp_query_args['meta_value'] = $meta_value; } //if meta query was specified, let us keep it and we will add our conditions if (!empty($meta_query)) { $gmeta_query = $meta_query; } //we will need to build tax query/meta query //type, audio video etc //if type is given and it is valid gallery type //Pass one or more types if ($gallery_id) { //if gallery id is given, avoid worrying about type $type = ''; $component = ''; } if (!empty($type) && mpp_are_registered_types($type)) { $type = mpp_string_to_array($type); //we store the terms with _name such as private becomes _private, members become _members to avoid conflicting terms $type = array_map('mpp_underscore_it', $type); $tax_query[] = array('taxonomy' => mpp_get_type_taxname(), 'field' => 'slug', 'terms' => $type, 'operator' => 'IN'); } //privacy //pass one or more privacy level if (!empty($status) && mpp_are_registered_statuses($status)) { $status = mpp_string_to_array($status); $status = array_map('mpp_underscore_it', $status); $tax_query[] = array('taxonomy' => mpp_get_status_taxname(), 'field' => 'slug', 'terms' => $status, 'operator' => 'IN'); } if (!empty($component) && mpp_are_registered_components($component)) { $component = mpp_string_to_array($component); $component = array_map('mpp_underscore_it', $component); $tax_query[] = array('taxonomy' => mpp_get_component_taxname(), 'field' => 'slug', 'terms' => $component, 'operator' => 'IN'); } //done with the tax query if (count($tax_query) > 1) { $tax_query['relation'] = 'AND'; } if (!empty($tax_query)) { $wp_query_args['tax_query'] = $tax_query; } //now, for component if (!empty($component_id)) { $meta_compare = '='; if (is_array($component_id)) { $meta_compare = 'IN'; } $gmeta_query[] = array('key' => '_mpp_component_id', 'value' => $component_id, 'compare' => $meta_compare, 'type' => 'UNSIGNED'); } //also make sure that it only looks for gallery media $gmeta_query[] = array('key' => '_mpp_is_mpp_media', 'value' => 1, 'compare' => '=', 'type' => 'UNSIGNED'); //should we avoid the orphaned media //Let us discuss with the community and get it here if (!mpp_get_option('show_orphaned_media')) { $gmeta_query[] = array('key' => '_mpp_is_orphan', 'compare' => 'NOT EXISTS'); } //Let us filter the media by storage method if (!empty($storage)) { $gmeta_query[] = array('key' => '_mpp_storage_method', 'value' => $storage, 'compare' => '='); } //and what to do when a user searches by the media source(say youtube|vimeo|xyz.. how do we do that?) //reset meta query if (!empty($gmeta_query)) { $wp_query_args['meta_query'] = $gmeta_query; } return $wp_query_args; //http://wordpress.stackexchange.com/questions/53783/cant-sort-get-posts-by-post-mime-type }