/** * Ajax handler for Media Manager "Query Attachments" queries * * Adapted from wp_ajax_query_attachments in /wp-admin/includes/ajax-actions.php * * @since 1.20 * * @return void passes array of post arrays to wp_send_json_success() for JSON encoding and transmission */ public static function mla_query_attachments_action() { if (!current_user_can('upload_files')) { wp_send_json_error(); } /* * Pick out and clean up the query terms we can process */ $raw_query = isset($_REQUEST['query']) ? (array) $_REQUEST['query'] : array(); $query = array_intersect_key($raw_query, array_flip(array('order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'mla_filter_month', 'mla_filter_term', 'mla_terms_search', 'mla_search_value', 's', 'mla_search_fields', 'mla_search_connector'))); $query = apply_filters('mla_media_modal_query_initial_terms', $query, $raw_query); if (isset($query['post_mime_type'])) { if ('detached' == $query['post_mime_type']) { $query['detached'] = '1'; unset($query['post_mime_type']); } elseif ('attached' == $query['post_mime_type']) { $query['detached'] = '0'; unset($query['post_mime_type']); } elseif ('trash' == $query['post_mime_type']) { $query['status'] = 'trash'; unset($query['post_mime_type']); } else { $view = $query['post_mime_type']; unset($query['post_mime_type']); $query = array_merge($query, MLAMime::mla_prepare_view_query('view', $view)); } } /* * Convert mla_filter_month back to the WordPress "m" parameter */ if (isset($query['mla_filter_month'])) { if ('0' != $query['mla_filter_month']) { $query['m'] = $query['mla_filter_month']; } unset($query['mla_filter_month']); } /* * Process the enhanced search box OR fix up the default search box */ if (isset($query['mla_search_value'])) { if (!empty($query['mla_search_value'])) { $query['s'] = $query['mla_search_value']; } unset($query['mla_search_value']); } if (isset($query['posts_per_page'])) { $count = $query['posts_per_page']; $offset = $count * (isset($query['paged']) ? $query['paged'] - 1 : 0); } else { $count = 0; $offset = 0; } /* * Check for sorting override */ $option = MLAOptions::mla_get_option(MLAOptions::MLA_MEDIA_MODAL_ORDERBY); if ('default' != $option) { /* * Make sure the current orderby choice still exists or revert to default. */ $default_orderby = array_merge(array('none' => array('none', false)), MLA_List_Table::mla_get_sortable_columns()); $found_current = false; foreach ($default_orderby as $key => $value) { if ($option == $value[0]) { $found_current = true; break; } } if (!$found_current) { MLAOptions::mla_delete_option(MLAOptions::MLA_DEFAULT_ORDERBY); $option = MLAOptions::mla_get_option(MLAOptions::MLA_DEFAULT_ORDERBY); } $query['orderby'] = $option; } $option = MLAOptions::mla_get_option(MLAOptions::MLA_MEDIA_MODAL_ORDER); if ('default' != $option) { $query['order'] = $option; } $query['post_type'] = 'attachment'; if (empty($query['status'])) { $query['post_status'] = 'inherit'; if (current_user_can(get_post_type_object('attachment')->cap->read_private_posts)) { $query['post_status'] .= ',private'; } } $query = apply_filters('mla_media_modal_query_filtered_terms', $query, $raw_query); $query = MLAData::mla_query_media_modal_items($query, $offset, $count); $posts = array_map('wp_prepare_attachment_for_js', $query->posts); $posts = array_filter($posts); wp_send_json_success($posts); }