コード例 #1
0
 /**
  * Sanitize and expand query arguments from request variables
  *
  * Prepare the arguments for WP_Query.
  * Modeled after wp_edit_attachments_query in wp-admin/post.php
  *
  * @since 0.1
  *
  * @param	array	query parameters from web page, usually found in $_REQUEST
  * @param	int		Optional number of rows (default 0) to skip over to reach desired page
  * @param	int		Optional number of rows on each page (0 = all rows, default)
  *
  * @return	array	revised arguments suitable for WP_Query
  */
 private static function _prepare_list_table_query($raw_request, $offset = 0, $count = 0)
 {
     /*
      * Go through the $raw_request, take only the arguments that are used in the query and
      * sanitize or validate them.
      */
     if (!is_array($raw_request)) {
         /* translators: 1: ERROR tag 2: function name 3: non-array value */
         error_log(sprintf(_x('%1$s: %2$s non-array "%3$s"', 'error_log', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), 'MLAQuery::_prepare_list_table_query', var_export($raw_request, true)), 0);
         return NULL;
     }
     /*
      * Make sure the current orderby choice still exists or revert to default.
      */
     $default_orderby = array_merge(array('none' => array('none', false)), self::mla_get_sortable_columns());
     $current_orderby = MLACore::mla_get_option(MLACore::MLA_DEFAULT_ORDERBY);
     $found_current = false;
     foreach ($default_orderby as $key => $value) {
         if ($current_orderby == $value[0]) {
             $found_current = true;
             break;
         }
     }
     if ($found_current) {
         /*
          * Custom fields can have HTML reserved characters, which are encoded by
          * mla_get_sortable_columns, so a separate, unencoded list is required.
          */
         $default_orderby = MLACore::mla_custom_field_support('custom_sortable_columns');
         foreach ($default_orderby as $sort_key => $sort_value) {
             if ($current_orderby == $sort_key) {
                 $current_orderby = 'c_' . $sort_value[0];
                 break;
             }
         }
         // foreach
     } else {
         MLACore::mla_delete_option(MLACore::MLA_DEFAULT_ORDERBY);
         $current_orderby = MLACore::mla_get_option(MLACore::MLA_DEFAULT_ORDERBY);
     }
     $clean_request = array('m' => 0, 'orderby' => $current_orderby, 'order' => MLACore::mla_get_option(MLACore::MLA_DEFAULT_ORDER), 'post_type' => 'attachment', 'post_status' => 'inherit', 'mla_search_connector' => 'AND', 'mla_search_fields' => array());
     foreach ($raw_request as $key => $value) {
         switch ($key) {
             /*
              * 'sentence' and 'exact' modify the keyword search ('s')
              * Their value is not important, only their presence.
              */
             case 'sentence':
             case 'exact':
             case 'mla-tax':
             case 'mla-term':
                 $clean_request[$key] = sanitize_key($value);
                 break;
             case 'orderby':
                 if (in_array($value, array('none', 'post__in'))) {
                     $clean_request[$key] = $value;
                 } else {
                     $orderby = NULL;
                     /*
                      * Custom fields can have HTML reserved characters, which are encoded by
                      * mla_get_sortable_columns, so a separate, unencoded list is required.
                      */
                     $sortable_columns = MLACore::mla_custom_field_support('custom_sortable_columns');
                     foreach ($sortable_columns as $sort_key => $sort_value) {
                         if ($value == $sort_key) {
                             $orderby = 'c_' . $sort_value[0];
                             break;
                         }
                     }
                     // foreach
                     if (NULL === $orderby) {
                         $sortable_columns = MLAQuery::mla_get_sortable_columns();
                         foreach ($sortable_columns as $sort_key => $sort_value) {
                             if ($value == $sort_value[0]) {
                                 $orderby = $value;
                                 break;
                             }
                         }
                         // foreach
                     }
                     if (NULL !== $orderby) {
                         $clean_request[$key] = $orderby;
                     }
                 }
                 break;
                 /*
                  * ids allows hooks to supply a persistent list of items
                  */
             /*
              * ids allows hooks to supply a persistent list of items
              */
             case 'ids':
                 if (is_array($value)) {
                     $clean_request['post__in'] = $value;
                 } else {
                     $clean_request['post__in'] = array_map('absint', explode(',', $value));
                 }
                 break;
                 /*
                  * post__in and post__not_in are used in the Media Modal Ajax queries
                  */
             /*
              * post__in and post__not_in are used in the Media Modal Ajax queries
              */
             case 'post__in':
             case 'post__not_in':
             case 'post_mime_type':
                 $clean_request[$key] = $value;
                 break;
             case 'parent':
             case 'post_parent':
                 $clean_request['post_parent'] = absint($value);
                 break;
                 /*
                  * ['m'] - filter by year and month of post, e.g., 201204
                  */
             /*
              * ['m'] - filter by year and month of post, e.g., 201204
              */
             case 'author':
             case 'm':
                 $clean_request[$key] = absint($value);
                 break;
                 /*
                  * ['mla_filter_term'] - filter by category or tag ID; -1 allowed
                  */
             /*
              * ['mla_filter_term'] - filter by category or tag ID; -1 allowed
              */
             case 'mla_filter_term':
                 $clean_request[$key] = intval($value);
                 break;
             case 'order':
                 switch ($value = strtoupper($value)) {
                     case 'ASC':
                     case 'DESC':
                         $clean_request[$key] = $value;
                         break;
                     default:
                         $clean_request[$key] = 'ASC';
                 }
                 break;
             case 'detached':
                 if ('0' == $value || '1' == $value) {
                     $clean_request['detached'] = $value;
                 }
                 break;
             case 'status':
                 if ('trash' == $value) {
                     $clean_request['post_status'] = 'trash';
                 }
                 break;
                 /*
                  * ['s'] - Search Media by one or more keywords
                  * ['mla_search_connector'], ['mla_search_fields'] - Search Media options
                  */
             /*
              * ['s'] - Search Media by one or more keywords
              * ['mla_search_connector'], ['mla_search_fields'] - Search Media options
              */
             case 's':
                 switch (substr($value, 0, 3)) {
                     case '>|<':
                         $clean_request['debug'] = 'console';
                         break;
                     case '<|>':
                         $clean_request['debug'] = 'log';
                         break;
                 }
                 if (isset($clean_request['debug'])) {
                     $value = substr($value, 3);
                 }
                 $value = stripslashes(trim($value));
                 if (!empty($value)) {
                     $clean_request[$key] = $value;
                 }
                 break;
             case 'mla_terms_search':
                 if (!empty($value['phrases']) && !empty($value['taxonomies'])) {
                     $value['phrases'] = stripslashes(trim($value['phrases']));
                     if (!empty($value['phrases'])) {
                         $clean_request[$key] = $value;
                     }
                 }
                 break;
             case 'mla_search_connector':
             case 'mla_search_fields':
                 $clean_request[$key] = $value;
                 break;
             case 'mla-metakey':
             case 'mla-metavalue':
                 $clean_request[$key] = stripslashes($value);
                 break;
             case 'meta_query':
                 if (!empty($value)) {
                     if (is_array($value)) {
                         $clean_request[$key] = $value;
                     } else {
                         $clean_request[$key] = unserialize(stripslashes($value));
                         unset($clean_request[$key]['slug']);
                     }
                     // not array
                 }
                 break;
             default:
                 // ignore anything else in $_REQUEST
         }
         // switch $key
     }
     // foreach $raw_request
     /*
      * Pass query and search parameters to the filters for _execute_list_table_query
      */
     self::$query_parameters = array('use_alt_text_view' => false, 'use_postmeta_view' => false, 'use_orderby_view' => false, 'orderby' => $clean_request['orderby'], 'order' => $clean_request['order']);
     self::$query_parameters['detached'] = isset($clean_request['detached']) ? $clean_request['detached'] : NULL;
     self::$search_parameters = array('debug' => 'none');
     /*
      * Matching a meta_value to NULL requires a LEFT JOIN to a view and a special WHERE clause
      * Matching a wildcard pattern requires mainpulating the WHERE clause, too
      */
     if (isset($clean_request['meta_query']['key'])) {
         self::$query_parameters['use_postmeta_view'] = true;
         self::$query_parameters['postmeta_key'] = $clean_request['meta_query']['key'];
         self::$query_parameters['postmeta_value'] = NULL;
         unset($clean_request['meta_query']);
     } elseif (isset($clean_request['meta_query']['patterns'])) {
         self::$query_parameters['patterns'] = $clean_request['meta_query']['patterns'];
         unset($clean_request['meta_query']['patterns']);
     }
     if (isset($clean_request['debug'])) {
         self::$query_parameters['debug'] = $clean_request['debug'];
         self::$search_parameters['debug'] = $clean_request['debug'];
         MLACore::mla_debug_mode($clean_request['debug']);
         unset($clean_request['debug']);
     }
     /*
      * We must patch the WHERE clause if there are leading spaces in the meta_value
      */
     if (isset($clean_request['mla-metavalue']) && 0 < strlen($clean_request['mla-metavalue']) && ' ' == $clean_request['mla-metavalue'][0]) {
         self::$query_parameters['mla-metavalue'] = $clean_request['mla-metavalue'];
     }
     /*
      * We will handle "Terms Search" in the mla_query_posts_search_filter.
      */
     if (isset($clean_request['mla_terms_search'])) {
         self::$search_parameters['mla_terms_search'] = $clean_request['mla_terms_search'];
         /*
          * The Terms Search overrides any terms-based keyword search for now; too complicated.
          */
         if (isset($clean_request['mla_search_fields'])) {
             foreach ($clean_request['mla_search_fields'] as $index => $field) {
                 if ('terms' == $field) {
                     unset($clean_request['mla_search_fields'][$index]);
                 }
             }
         }
     }
     /*
      * We will handle keyword search in the mla_query_posts_search_filter.
      */
     if (isset($clean_request['s'])) {
         self::$search_parameters['s'] = $clean_request['s'];
         self::$search_parameters['mla_search_fields'] = apply_filters('mla_list_table_search_filter_fields', $clean_request['mla_search_fields'], array('content', 'title', 'excerpt', 'alt-text', 'name', 'terms'));
         self::$search_parameters['mla_search_connector'] = $clean_request['mla_search_connector'];
         self::$search_parameters['sentence'] = isset($clean_request['sentence']);
         self::$search_parameters['exact'] = isset($clean_request['exact']);
         if (in_array('alt-text', self::$search_parameters['mla_search_fields'])) {
             self::$query_parameters['use_alt_text_view'] = true;
         }
         if (in_array('terms', self::$search_parameters['mla_search_fields'])) {
             self::$search_parameters['mla_search_taxonomies'] = MLACore::mla_supported_taxonomies('term-search');
         }
         unset($clean_request['s']);
         unset($clean_request['mla_search_connector']);
         unset($clean_request['mla_search_fields']);
         unset($clean_request['sentence']);
         unset($clean_request['exact']);
     }
     /*
      * We have to handle custom field/post_meta values here
      * because they need a JOIN clause supplied by WP_Query
      */
     if ('c_' == substr($clean_request['orderby'], 0, 2)) {
         $option_value = MLAQuery::mla_custom_field_option_value($clean_request['orderby']);
         if (isset($option_value['name'])) {
             self::$query_parameters['use_orderby_view'] = true;
             self::$query_parameters['postmeta_key'] = $option_value['name'];
             if (isset($clean_request['orderby'])) {
                 unset($clean_request['orderby']);
             }
             if (isset($clean_request['order'])) {
                 unset($clean_request['order']);
             }
         }
     } else {
         // custom field
         switch (self::$query_parameters['orderby']) {
             /*
              * '_wp_attachment_image_alt' is special; it can have NULL values,
              * so we'll handle it in the JOIN and ORDERBY filters
              */
             case '_wp_attachment_image_alt':
                 self::$query_parameters['use_orderby_view'] = true;
                 self::$query_parameters['postmeta_key'] = '_wp_attachment_image_alt';
                 if (isset($clean_request['orderby'])) {
                     unset($clean_request['orderby']);
                 }
                 if (isset($clean_request['order'])) {
                     unset($clean_request['order']);
                 }
                 break;
             case '_wp_attached_file':
                 $clean_request['meta_key'] = '_wp_attached_file';
                 $clean_request['orderby'] = 'meta_value';
                 $clean_request['order'] = self::$query_parameters['order'];
                 break;
         }
         // switch $orderby
     }
     /*
      * Ignore incoming paged value; use offset and count instead
      */
     if ((int) $count > 0) {
         $clean_request['offset'] = $offset;
         $clean_request['posts_per_page'] = $count;
     } elseif ((int) $count == -1) {
         $clean_request['posts_per_page'] = $count;
     }
     /*
      * ['mla_filter_term'] - filter by taxonomy
      *
      * cat =  0 is "All Categories", i.e., no filtering
      * cat = -1 is "No Categories"
      */
     if (isset($clean_request['mla_filter_term'])) {
         if ($clean_request['mla_filter_term'] != 0) {
             $tax_filter = MLACore::mla_taxonomy_support('', 'filter');
             if ($clean_request['mla_filter_term'] == -1) {
                 $term_list = get_terms($tax_filter, array('fields' => 'ids', 'hide_empty' => false));
                 $clean_request['tax_query'] = array(array('taxonomy' => $tax_filter, 'field' => 'id', 'terms' => $term_list, 'operator' => 'NOT IN'));
             } else {
                 // mla_filter_term == -1
                 $clean_request['tax_query'] = array(array('taxonomy' => $tax_filter, 'field' => 'id', 'terms' => array((int) $clean_request['mla_filter_term']), 'include_children' => 'checked' == MLACore::mla_get_option(MLACore::MLA_TAXONOMY_FILTER_INCLUDE_CHILDREN)));
             }
             // mla_filter_term != -1
         }
         // mla_filter_term != 0
         unset($clean_request['mla_filter_term']);
     }
     // isset mla_filter_term
     if (isset($clean_request['mla-tax']) && isset($clean_request['mla-term'])) {
         $clean_request['tax_query'] = array(array('taxonomy' => $clean_request['mla-tax'], 'field' => 'slug', 'terms' => $clean_request['mla-term'], 'include_children' => false));
         unset($clean_request['mla-tax']);
         unset($clean_request['mla-term']);
     }
     // isset mla_tax
     if (isset($clean_request['mla-metakey']) && isset($clean_request['mla-metavalue'])) {
         $clean_request['meta_key'] = $clean_request['mla-metakey'];
         $clean_request['meta_value'] = $clean_request['mla-metavalue'];
         unset($clean_request['mla-metakey']);
         unset($clean_request['mla-metavalue']);
     }
     // isset mla_tax
     return $clean_request;
 }