/** * Check if a media exists or not * @param type $media_slug * @param type $component * @param type $component_id * @return type */ function mpp_media_exists($media_slug, $component, $component_id) { if (!$media_slug) { return false; } return mpp_post_exists(array('component' => $component, 'component_id' => $component_id, 'slug' => $media_slug, 'post_status' => 'inherit', 'post_type' => mpp_get_media_post_type())); }
public function query($args) { if (isset($args['in'])) { $args['post__in'] = $args['in']; $args['post_type'] = mpp_get_media_post_type(); unset($args['in']); } //setup query vars $this->init(); //store $this->query = $this->query_vars = wp_parse_args($args); //do the fake query return $this->get_posts(); }
public function register_taxonomies() { //register type //register status // register component $this->register_taxonomy(mpp_get_type_taxname(), array('label' => _x('Media Type', 'Gallery Media Type', 'mediapress'), 'labels' => _x('Media Types', 'Gallery Media Type Plural Name', 'mediapress'), 'hierarchical' => false)); $this->register_taxonomy(mpp_get_component_taxname(), array('label' => _x('Component', 'Gallery Associated Type', 'mediapress'), 'labels' => _x('Components', 'Gallery Associated Component Plural Name', 'mediapress'), 'hierarchical' => false)); $this->register_taxonomy(mpp_get_status_taxname(), array('label' => _x('Gallery Status', 'Gallery privacy/status Type', 'mediapress'), 'labels' => _x('Galery Statuses', 'Gallery Privacy Plural Name', 'mediapress'), 'hierarchical' => false)); register_taxonomy_for_object_type(mpp_get_type_taxname(), mpp_get_gallery_post_type()); register_taxonomy_for_object_type(mpp_get_component_taxname(), mpp_get_gallery_post_type()); register_taxonomy_for_object_type(mpp_get_status_taxname(), mpp_get_gallery_post_type()); register_taxonomy_for_object_type(mpp_get_type_taxname(), mpp_get_media_post_type()); register_taxonomy_for_object_type(mpp_get_component_taxname(), mpp_get_media_post_type()); register_taxonomy_for_object_type(mpp_get_status_taxname(), mpp_get_media_post_type()); }
function mpp_get_previous_media_id($media_id) { if (!$media_id) { return false; } $media = mpp_get_media($media_id); $args = array('component' => $media->component, 'component_id' => $media->component_id, 'object_id' => $media->id, 'object_parent' => $media->gallery_id, 'next' => false); $prev_gallery_id = mpp_get_adjacent_object_id($args, mpp_get_media_post_type()); return $prev_gallery_id; }
/** * Remove a Media entry from gallery * * @param type $media_id */ function mpp_delete_media($media_id) { global $wpdb; if (!($media = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $media_id)))) { return $post; } if (mpp_get_media_post_type() != $media->post_type) { return false; } //firs of all delete all media associated with this post $storage_manager = mpp_get_storage_manager($media_id); $storage_manager->delete($media_id); //now proceed to delete the post mpp_delete_media_meta($media_id, '_wp_trash_meta_status'); mpp_delete_media_meta($media_id, '_wp_trash_meta_time'); do_action('mpp_delete_media', $media_id); wp_delete_object_term_relationships($media_id, array('category', 'post_tag')); wp_delete_object_term_relationships($media_id, get_object_taxonomies(mpp_get_media_post_type())); delete_metadata('post', null, '_thumbnail_id', $media_id, true); // delete all for any posts. //dele if it is set as cover delete_metadata('post', null, '_mpp_cover_id', $media_id, true); // delete all for any posts. $comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d", $media_id)); foreach ($comment_ids as $comment_id) { wp_delete_comment($comment_id, true); } //if media has cover, delete the cover if (mpp_media_has_cover_image($media_id)) { mpp_delete_media(mpp_get_media_cover_id($media_id)); } //delete met $post_meta_ids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d ", $media_id)); foreach ($post_meta_ids as $mid) { delete_metadata_by_mid('post', $mid); } $result = $wpdb->delete($wpdb->posts, array('ID' => $media_id)); if (!$result) { return false; } //decrease the media_count in gallery by 1 mpp_gallery_decrement_media_count($media->post_parent); //delete all activities related to this media mpp_media_delete_activities($media_id); //delete all activity meta key where this media is associated mpp_media_delete_activity_meta($media_id); clean_post_cache($media); do_action('mpp_media_deleted', $media_id); return $media; }
/** * Check and get single media id else false * * @param type $slug * @param type $component * @param type $component_id * @return type */ public function get_media_id($slug, $component, $component_id) { if (!$component_id || !$slug || !$component) { return false; } //on single post, why bother about the component etc, that makes our query slow, just do a simple post query instead global $wpdb; $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_name = %s and post_type = %s ", $slug, mpp_get_media_post_type())); return $post; $query = new MPP_Media_Query(array('slug' => $slug, 'component' => $component, 'component_id' => $component_id)); $posts = $query->get_media(); $this->single_media_query = $query; if (!empty($posts)) { return array_pop($posts); } return false; //return mpp_media_exists( $slug, $component, $component_id ); }
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 }
/** * Get the id of the latest media in the given gallery * * @param type $gallery_id * * @return boolean|int false or the media id */ function mpp_gallery_get_latest_media_id($gallery_id) { if (!$gallery_id) { return false; } $media = get_posts(array('post_parent' => $gallery_id, 'meta_key' => '_mpp_is_mpp_media', 'post_type' => mpp_get_media_post_type(), 'post_status' => 'any', 'meta_value' => 1, 'posts_per_page' => 1, 'fields' => 'ids')); if (!empty($media)) { return array_pop($media); // } return false; }