function mpp_get_adjacent_object_id($args, $post_type) { global $wpdb; $post_type_sql = ''; $sql = array(); $default = array('component' => '', 'component_id' => false, 'status' => mpp_get_accessible_statuses(mpp_get_current_component(), mpp_get_current_component_id()), 'type' => '', 'post_status' => 'any', 'next' => true, 'object_id' => '', 'object_parent' => 0); if ($post_type == mpp_get_gallery_post_type()) { $default['post_status'] = 'publish'; //for gallery, the default post type should be published status } //if component is set to user, we can simply avoid component query //may be next iteration someday $args = wp_parse_args($args, $default); extract($args); //whether we are looking for next post or previous post if ($next) { $op = '>'; } else { $op = '<'; } //do we have a component set if ($component) { $sql[] = mpp_get_tax_sql($component, mpp_get_component_taxname()); } //do we have a component set if ($status) { $sql[] = mpp_get_tax_sql($status, mpp_get_status_taxname()); } //for type, repeat it if ($type) { $sql[] = mpp_get_tax_sql($type, mpp_get_type_taxname()); } //so let us build one /* $term_object_sql = "SELECT object_id FROM ( (SELECT DISTINCT value FROM table_a) UNION ALL (SELECT DISTINCT value FROM table_b) ) AS t1 GROUP BY value HAVING count(*) >= 2; */ $post_type_sql = $wpdb->prepare("SELECT DISTINCT ID as object_id FROM {$wpdb->posts} WHERE post_type = %s ", $post_type); //if a user or group id is given if ($component_id) { $post_type_sql = $wpdb->prepare("SELECT DISTINCT p.ID as object_id FROM {$wpdb->posts} AS p INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id WHERE p.post_type= %s AND pm.meta_key=%s and pm.meta_value=%d", $post_type, '_mpp_component_id', $component_id); } $post_status_sql = ''; if ($post_status && $post_status != 'any') { $post_status_sql = $wpdb->prepare(" AND post_status =%s", $post_status); } //$sql[] = $post_type_sql; $new_sql = $join_sql = ''; //array(); //let us generate inner sub queries if ($sql) { $join_sql = ' (' . join(' AND object_id IN (', $sql); } //we need to append the ) for closing the sub queries for ($i = 0; $i < count($sql); $i++) { $join_sql .= ')'; } $new_sql = $post_type_sql . $post_status_sql; //if the join sql is present, let us append it if ($join_sql) { $new_sql .= ' AND ID IN ' . $join_sql; } //for next/prev //sorted gallery //or by date // $post = get_post($object_id); $sorted = false; if ($object_parent && mpp_is_gallery_sorted($object_parent)) { $new_sql .= $wpdb->prepare(" AND p.menu_order {$op} %d ", $post->menu_order); $sorted = true; } else { $new_sql .= $wpdb->prepare(" AND p.ID {$op} %d ", $object_id); $sorted = false; } if ($object_parent) { $new_sql .= $wpdb->prepare(" AND post_parent = %d ", $object_parent); } $oreder_by_clause = ''; if ($sorted) { $oreder_by_clause = " ORDER BY p.menu_order "; } else { $oreder_by_clause = "ORDER BY p.ID"; } if (!$next) { //for previous //find the last element les than give $oreder_by_clause .= " DESC "; } else { $oreder_by_clause .= " ASC"; } if (!empty($new_sql)) { $new_sql .= $oreder_by_clause . ' LIMIT 0, 1'; } return $wpdb->get_var($new_sql); }
function mpp_update_media($args = null) { //updating media can not change the Id & SRC, so if (!isset($args['id'])) { return false; } $default = array('user_id' => get_current_user_id(), 'gallery_id' => false, 'post_parent' => false, 'is_orphan' => false, 'is_uploaded' => '', 'is_remote' => '', 'is_imorted' => '', 'is_embedded' => '', 'embed_url' => '', 'embed_html' => '', 'component_id' => '', 'component' => '', 'context' => '', 'status' => '', 'type' => '', 'storage_method' => '', 'mime_type' => '', 'description' => '', 'sort_order' => 0); $args = wp_parse_args($args, $default); extract($args); //print_r($args ); //return ; if (!$title) { return false; } $post_data = get_post($id, ARRAY_A); if (!$gallery_id) { $gallery_id = $post_data['post_parent']; } if ($title) { $post_data['post_title'] = $title; } if ($description) { $post_data['post_content'] = $description; } if ($gallery_id) { $post_data['post_parent'] = $gallery_id; } //check if the gallery is sorted and the sorting order is not set explicitly //we update it if (!$sort_order && !$post_data['menu_order'] && mpp_is_gallery_sorted($gallery_id)) { //current max sort order +1 $sort_order = (int) mpp_get_max_media_order($gallery_id) + 1; } if ($sort_order) { $post_data['menu_order'] = absin($sort_order); } // Save the data $id = wp_insert_attachment($post_data, false, $gallery_id); if (!is_wp_error($id)) { //set component if ($component) { wp_set_object_terms($id, mpp_underscore_it($component), mpp_get_component_taxname()); } //set _component_id meta key user_id/gallery_id/group id etc if ($component_id) { mpp_update_media_meta($id, '_mpp_component_id', $component_id); } //set upload context if ($context) { mpp_update_media_meta($id, '_mpp_context', $context); } //set media privacy if ($status) { wp_set_object_terms($id, mpp_underscore_it($status), mpp_get_status_taxname()); } //set media type internally as audio/video etc if ($type) { wp_set_object_terms($id, mpp_underscore_it($type), mpp_get_type_taxname()); } // if ($storage_method) { mpp_update_media_meta($id, '_mpp_storage_method', $storage_method); } // //add all extraz here if ($is_orphan) { mpp_update_media_meta($id, '_mpp_is_orphan', $is_orphan); } else { mpp_delete_media_meta($id, '_mpp_is_orphan'); } do_action('mpp_media_updated', $id, $gallery_id); return $id; } return false; // there was an error }
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 }