Ejemplo n.º 1
0
 /**
  * Convert a Library View/Post MIME Type specification to WP_Query parameters
  *
  * Compatibility shim for MLACore::mla_prepare_view_query
  *
  * @since 1.40
  *
  * @param	string	View slug, unique identifier
  * @param	string	A specification, e.g., "custom:Field,null" or "audio,application/vnd.*ms*"
  *
  * @return	array	post_mime_type specification or custom field query
  */
 public static function mla_prepare_view_query($slug, $specification)
 {
     return MLACore::mla_prepare_view_query($slug, $specification);
 }
 /**
  * Ajax handler for Media Manager "Query Attachments" queries 
  *
  * Adapted from wp_ajax_query_attachments in /wp-admin/includes/ajax-actions.php
  *
  * @since 2.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, MLACore::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 = MLACore::mla_get_option(MLACoreOptions::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)), MLAQuery::mla_get_sortable_columns());
         $found_current = false;
         foreach ($default_orderby as $key => $value) {
             if ($option == $value[0]) {
                 $found_current = true;
                 break;
             }
         }
         if (!$found_current) {
             MLACore::mla_delete_option(MLACoreOptions::MLA_DEFAULT_ORDERBY);
             $option = MLACore::mla_get_option(MLACoreOptions::MLA_DEFAULT_ORDERBY);
         }
         $query['orderby'] = $option;
     }
     $option = MLACore::mla_get_option(MLACoreOptions::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 = MLAQuery::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);
 }
 /**
  * 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
  */
 private static function _get_view($view_slug, $current_view)
 {
     global $wpdb;
     static $mla_types = NULL, $default_types, $posts_per_type, $post_mime_types, $avail_post_mime_types, $matches, $num_posts, $detached_items;
     /*
      * 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;
         }
         $default_types = MLACore::mla_get_option(MLACoreOptions::MLA_POST_MIME_TYPES, true);
         $posts_per_type = (array) wp_count_attachments();
         $post_mime_types = get_post_mime_types();
         $avail_post_mime_types = self::_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];
             }
         }
         $detached_items = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1");
     }
     $class = $view_slug == $current_view ? ' class="current"' : '';
     $base_url = 'upload.php?page=' . MLACore::ADMIN_PAGE_SLUG;
     /*
      * Handle the special cases: all, detached, attached 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', 'All', $total_items, 'uploaded files', 'media-library-assistant') . ' <span class="count">(%1$s)</span></a>', number_format_i18n($total_items));
         case 'detached':
             if ($detached_items) {
                 $value = $default_types['detached'];
                 $singular = sprintf('%s <span class="count">(%%s)</span>', $value['singular']);
                 $plural = sprintf('%s <span class="count">(%%s)</span>', $value['plural']);
                 return '<a href="' . add_query_arg(array('detached' => '1'), $base_url) . '"' . $class . '>' . sprintf(_nx($singular, $plural, $detached_items, 'detached files', 'media-library-assistant'), number_format_i18n($detached_items)) . '</a>';
             }
             return false;
         case 'attached':
             if ($attached_items = array_sum($posts_per_type) - $posts_per_type['trash'] - $detached_items) {
                 $value = $default_types['attached'];
                 $singular = sprintf('%s <span class="count">(%%s)</span>', $value['singular']);
                 $plural = sprintf('%s <span class="count">(%%s)</span>', $value['plural']);
                 return '<a href="' . add_query_arg(array('detached' => '0'), $base_url) . '"' . $class . '>' . sprintf(_nx($singular, $plural, $attached_items, 'attached files', 'media-library-assistant'), number_format_i18n($attached_items)) . '</a>';
             }
             return false;
         case 'trash':
             if ($posts_per_type['trash']) {
                 $value = $default_types['trash'];
                 $singular = sprintf('%s <span class="count">(%%s)</span>', $value['singular']);
                 $plural = sprintf('%s <span class="count">(%%s)</span>', $value['plural']);
                 return '<a href="' . add_query_arg(array('status' => 'trash'), $base_url) . '"' . $class . '>' . sprintf(_nx($singular, $plural, $posts_per_type['trash'], 'uploaded files', 'media-library-assistant'), number_format_i18n($posts_per_type['trash'])) . '</a>';
             }
             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], 'media-library-assistant'), number_format_i18n($num_posts[$view_slug])) . '</a>';
         }
         return false;
     }
     /*
      * Handle extended specification types
      */
     if (empty($mla_type->specification)) {
         $query = array('post_mime_type' => $view_slug);
     } else {
         $query = MLACore::mla_prepare_view_query($view_slug, $mla_type->specification);
     }
     $total_items = MLAQuery::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, 'media-library-assistant');
         if (isset($query['post_mime_type'])) {
             $query['post_mime_type'] = urlencode($query['post_mime_type']);
         } else {
             $query['meta_slug'] = $view_slug;
             $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, 'media-library-assistant'), number_format_i18n($total_items)) . '</a>';
     }
     return false;
 }