/** * Returns HTML markup for one view that can be used with this table * * @since 1.40 * * @param string View slug, key to MLA_POST_MIME_TYPES array * @param string Slug for current view * * @return string | false HTML for link to display the view, false if count = zero */ function _get_view($view_slug, $current_view) { global $wpdb; static $mla_types = NULL, $posts_per_type, $post_mime_types, $avail_post_mime_types, $matches, $num_posts; /* * Calculate the common values once per page load */ if (is_null($mla_types)) { $query_types = MLAMime::mla_query_view_items(array('orderby' => 'menu_order'), 0, 0); if (!is_array($query_types)) { $query_types = array(); } $mla_types = array(); foreach ($query_types as $value) { $mla_types[$value->slug] = $value; } $posts_per_type = (array) wp_count_attachments(); $post_mime_types = get_post_mime_types(); $avail_post_mime_types = $this->_avail_mime_types($posts_per_type); $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($posts_per_type)); foreach ($matches as $type => $reals) { foreach ($reals as $real) { $num_posts[$type] = isset($num_posts[$type]) ? $num_posts[$type] + $posts_per_type[$real] : $posts_per_type[$real]; } } } $class = $view_slug == $current_view ? ' class="current"' : ''; $base_url = 'upload.php?page=' . MLA::ADMIN_PAGE_SLUG; /* * Handle the special cases: all, unattached and trash */ switch ($view_slug) { case 'all': $total_items = array_sum($posts_per_type) - $posts_per_type['trash']; return "<a href='{$base_url}'{$class}>" . sprintf(_nx('All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_items, 'uploaded files'), number_format_i18n($total_items)) . '</a>'; case 'unattached': $total_items = $wpdb->get_var("\r\n\t\t\t\t\t\tSELECT COUNT( * ) FROM {$wpdb->posts}\r\n\t\t\t\t\t\tWHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1\r\n\t\t\t\t\t\t"); if ($total_items) { return '<a href="' . add_query_arg(array('detached' => '1'), $base_url) . '"' . $class . '>' . sprintf(_nx('Unattached <span class="count">(%s)</span>', 'Unattached <span class="count">(%s)</span>', $total_items, 'detached files'), number_format_i18n($total_items)) . '</a>'; } else { return false; } case 'trash': if ($posts_per_type['trash']) { return '<a href="' . add_query_arg(array('status' => 'trash'), $base_url) . '"' . $class . '>' . sprintf(_nx('Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>', $posts_per_type['trash'], 'uploaded files'), number_format_i18n($posts_per_type['trash'])) . '</a>'; } else { return false; } } // switch special cases /* * Make sure the slug is in our list */ if (array_key_exists($view_slug, $mla_types)) { $mla_type = $mla_types[$view_slug]; } else { return false; } /* * Handle post_mime_types */ if ($mla_type->post_mime_type) { if (!empty($num_posts[$view_slug])) { return "<a href='" . add_query_arg(array('post_mime_type' => $view_slug), $base_url) . "'{$class}>" . sprintf(translate_nooped_plural($post_mime_types[$view_slug][2], $num_posts[$view_slug]), number_format_i18n($num_posts[$view_slug])) . '</a>'; } else { return false; } } /* * Handle extended specification types */ if (empty($mla_type->specification)) { $query = array('post_mime_type' => $view_slug); } else { $query = MLAMime::mla_prepare_view_query($view_slug, $mla_type->specification); } $total_items = MLAData::mla_count_list_table_items($query); if ($total_items) { $singular = sprintf('%s <span class="count">(%%s)</span>', $mla_type->singular); $plural = sprintf('%s <span class="count">(%%s)</span>', $mla_type->plural); $nooped_plural = _n_noop($singular, $plural); if (isset($query['post_mime_type'])) { $query['post_mime_type'] = urlencode($query['post_mime_type']); } else { $query['meta_query'] = urlencode(serialize($query['meta_query'])); } return "<a href='" . add_query_arg($query, $base_url) . "'{$class}>" . sprintf(translate_nooped_plural($nooped_plural, $total_items), number_format_i18n($total_items)) . '</a>'; } return false; }
/** * 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); }