Exemplo n.º 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)) {
         error_log('ERROR: _prepare_list_table_query $raw_request = ' . 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)), MLA_List_Table::mla_get_sortable_columns());
     $current_orderby = MLAOptions::mla_get_option(MLAOptions::MLA_DEFAULT_ORDERBY);
     $found_current = false;
     foreach ($default_orderby as $key => $value) {
         if ($current_orderby == $value[0]) {
             $found_current = true;
             break;
         }
     }
     if (!$found_current) {
         MLAOptions::mla_delete_option(MLAOptions::MLA_DEFAULT_ORDERBY);
         $current_orderby = MLAOptions::mla_get_option(MLAOptions::MLA_DEFAULT_ORDERBY);
     }
     $clean_request = array('m' => 0, 'orderby' => $current_orderby, 'order' => MLAOptions::mla_get_option(MLAOptions::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 ('none' == $value) {
                     $clean_request[$key] = $value;
                 } else {
                     $sortable_columns = MLA_List_Table::mla_get_sortable_columns();
                     foreach ($sortable_columns as $sort_key => $sort_value) {
                         if ($value == $sort_value[0]) {
                             $clean_request[$key] = $value;
                             break;
                         }
                     }
                     // foreach
                 }
                 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 ('1' == $value) {
                     $clean_request['detached'] = '1';
                 }
                 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_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 parameters to the filters for _execute_list_table_query
      */
     self::$query_parameters = array('use_postmeta_view' => false, 'orderby' => $clean_request['orderby'], 'order' => $clean_request['order']);
     self::$query_parameters['detached'] = isset($clean_request['detached']);
     /*
      * 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'];
         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']) && ' ' == $clean_request['mla-metavalue'][0]) {
         self::$query_parameters['mla-metavalue'] = $clean_request['mla-metavalue'];
     }
     /*
      * We will handle keyword search in the mla_query_posts_search_filter.
      * There must be at least one search field to do a search.
      */
     if (isset($clean_request['s'])) {
         if (!empty($clean_request['mla_search_fields'])) {
             self::$query_parameters['s'] = $clean_request['s'];
             self::$query_parameters['mla_search_connector'] = $clean_request['mla_search_connector'];
             self::$query_parameters['mla_search_fields'] = $clean_request['mla_search_fields'];
             self::$query_parameters['sentence'] = isset($clean_request['sentence']);
             self::$query_parameters['exact'] = isset($clean_request['exact']);
             if (in_array('alt-text', self::$query_parameters['mla_search_fields'])) {
                 self::$query_parameters['use_postmeta_view'] = true;
             }
             self::$query_parameters['postmeta_key'] = '_wp_attachment_image_alt';
         }
         // !empty
         // unset( $clean_request['s'] ); // WP v3.7 requires this to be present for posts_search filter
         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(self::$query_parameters['orderby'], 0, 2)) {
         $option_value = MLAOptions::mla_custom_field_option_value(self::$query_parameters['orderby']);
         if (isset($option_value['name'])) {
             self::$query_parameters['use_postmeta_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 {
         switch (self::$query_parameters['orderby']) {
             /*
              * '_wp_attachment_image_alt' is special; we'll handle it in the JOIN and ORDERBY filters
              */
             case '_wp_attachment_image_alt':
                 self::$query_parameters['use_postmeta_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 = MLAOptions::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 {
                 $clean_request['tax_query'] = array(array('taxonomy' => $tax_filter, 'field' => 'id', 'terms' => array((int) $clean_request['mla_filter_term']), 'include_children' => 'checked' == MLAOptions::mla_get_option(MLAOptions::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;
 }
 /**
  * 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);
 }
 /**
  * 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'), 'MLAData::_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)), MLA_List_Table::mla_get_sortable_columns());
     $current_orderby = MLAOptions::mla_get_option(MLAOptions::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 = MLAOptions::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 {
         MLAOptions::mla_delete_option(MLAOptions::MLA_DEFAULT_ORDERBY);
         $current_orderby = MLAOptions::mla_get_option(MLAOptions::MLA_DEFAULT_ORDERBY);
     }
     $clean_request = array('m' => 0, 'orderby' => $current_orderby, 'order' => MLAOptions::mla_get_option(MLAOptions::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 = MLAOptions::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 = MLA_List_Table::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_postmeta_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'];
         MLA::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_postmeta_view'] = true;
             self::$query_parameters['postmeta_key'] = '_wp_attachment_image_alt';
         }
         if (in_array('terms', self::$search_parameters['mla_search_fields'])) {
             self::$search_parameters['mla_search_taxonomies'] = MLAOptions::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 = MLAOptions::mla_custom_field_option_value($clean_request['orderby']);
         if (isset($option_value['name'])) {
             self::$query_parameters['use_postmeta_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; we'll handle it in the JOIN and ORDERBY filters
              */
             case '_wp_attachment_image_alt':
                 self::$query_parameters['use_postmeta_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 = MLAOptions::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' == MLAOptions::mla_get_option(MLAOptions::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;
 }
Exemplo n.º 4
0
 /**
  * Compose the General tab content for the Settings subpage
  *
  * @since 0.80
  * @uses $page_template_array contains tab content template(s)
  *
  * @return	array	'message' => status/error messages, 'body' => tab content
  */
 private static function _compose_general_tab()
 {
     /*
      * Check for submit buttons to change or reset settings.
      * Initialize page messages and content.
      */
     if (!empty($_REQUEST['mla-general-options-save'])) {
         check_admin_referer(MLA::MLA_ADMIN_NONCE_ACTION, MLA::MLA_ADMIN_NONCE_NAME);
         $page_content = self::_save_general_settings();
     } elseif (!empty($_REQUEST['mla-general-options-export'])) {
         check_admin_referer(MLA::MLA_ADMIN_NONCE_ACTION, MLA::MLA_ADMIN_NONCE_NAME);
         $page_content = self::_export_settings();
     } elseif (!empty($_REQUEST['mla-general-options-import'])) {
         check_admin_referer(MLA::MLA_ADMIN_NONCE_ACTION, MLA::MLA_ADMIN_NONCE_NAME);
         $page_content = self::_import_settings();
     } elseif (!empty($_REQUEST['mla-general-options-reset'])) {
         check_admin_referer(MLA::MLA_ADMIN_NONCE_ACTION, MLA::MLA_ADMIN_NONCE_NAME);
         $page_content = self::_reset_general_settings();
     } else {
         $page_content = array('message' => '', 'body' => '');
     }
     if (!empty($page_content['body'])) {
         return $page_content;
     }
     $page_values = array('General Processing Options' => __('General Processing Options', 'media-library-assistant'), 'In this tab' => sprintf(__('In this tab you can find a number of options for controlling the plugin&rsquo;s operation. Scroll down to find options for %1$s, %2$s, %3$s and %4$s. Be sure to click "Save Changes" at the bottom of the tab to save any changes you make.', 'media-library-assistant'), '<strong>' . __('Where-used Reporting', 'media-library-assistant') . '</strong>', '<strong>' . __('Taxonomy Support', 'media-library-assistant') . '</strong>', '<strong>' . __('Media/Assistant Table Defaults', 'media-library-assistant') . '</strong>', '<strong>' . __('Media Manager Enhancements', 'media-library-assistant') . '</strong>'), 'Save Changes' => __('Save Changes', 'media-library-assistant'), 'Export ALL Settings' => __('Export ALL Settings', 'media-library-assistant'), 'Delete General options' => __('Delete General options and restore default settings', 'media-library-assistant'), '_wpnonce' => wp_nonce_field(MLA::MLA_ADMIN_NONCE_ACTION, MLA::MLA_ADMIN_NONCE_NAME, true, false), '_wp_http_referer' => wp_referer_field(false), 'Go to Top' => __('Go to Top', 'media-library-assistant'), 'Support Our Work' => __('Support Our Work', 'media-library-assistant'), 'Donate to FTJ' => __('Donate to FTJ', 'media-library-assistant'), 'Donate' => __('Donate', 'media-library-assistant'), 'This plugin was' => sprintf(__('This plugin was inspired by my work on the WordPress web site for our nonprofit, Fair Trade Judaica. If you find the Media Library Assistant plugin useful and would like to support a great cause, consider a %1$s to our work. Thank you!', 'media-library-assistant'), '<a href="http://fairtradejudaica.org/make-a-difference/donate/" title="' . __('Donate to FTJ', 'media-library-assistant') . '" target="_blank" style="font-weight:bold">' . __('tax-deductible donation', 'media-library-assistant') . '</a>'), 'shortcode_list' => '', 'form_url' => admin_url('options-general.php') . '?page=mla-settings-menu-general&mla_tab=general', 'options_list' => '', 'import_settings' => '');
     /*
      * $custom_fields documents the name and description of custom fields
      */
     $custom_fields = array();
     /* 
      * $shortcodes documents the name and description of plugin shortcodes
      */
     $shortcodes = array(array('name' => 'mla_gallery', 'description' => __('enhanced version of the WordPress [gallery] shortcode.', 'media-library-assistant') . sprintf(' %1$s <a href="%2$s">%3$s</a>.', __('For complete documentation', 'media-library-assistant'), admin_url('options-general.php?page=' . self::MLA_SETTINGS_SLUG . '-documentation&amp;mla_tab=documentation#mla_gallery'), __('click here', 'media-library-assistant'))), array('name' => 'mla_tag_cloud', 'description' => __('enhanced version of the WordPress Tag Cloud.', 'media-library-assistant') . sprintf(' %1$s <a href="%2$s">%3$s</a>.', __('For complete documentation', 'media-library-assistant'), admin_url('options-general.php?page=' . self::MLA_SETTINGS_SLUG . '-documentation&amp;mla_tab=documentation#mla_tag_cloud'), __('click here', 'media-library-assistant'))));
     $shortcode_list = '';
     foreach ($shortcodes as $shortcode) {
         $shortcode_values = array('name' => $shortcode['name'], 'description' => $shortcode['description']);
         $shortcode_list .= MLAData::mla_parse_template(self::$page_template_array['shortcode-item'], $shortcode_values);
     }
     if (!empty($shortcode_list)) {
         $shortcode_values = array('shortcode_list' => $shortcode_list, 'Shortcodes made available' => __('Shortcodes made available by this plugin', 'media-library-assistant'));
         $page_values['shortcode_list'] = MLAData::mla_parse_template(self::$page_template_array['shortcode-list'], $shortcode_values);
     }
     /*
      * Fill in the current list of Media/Assistant table sortable columns, sorted by their labels.
      * Make sure the current choice still exists or revert to default.
      */
     $columns = array();
     foreach (MLA_List_Table::mla_get_sortable_columns() as $key => $value) {
         if (!array_key_exists($value[1], $columns)) {
             $columns[$value[1]] = $value[0];
         }
     }
     uksort($columns, 'strnatcasecmp');
     $options = array_merge(array('None' => 'none'), $columns);
     $current = MLAOptions::mla_get_option(MLAOptions::MLA_DEFAULT_ORDERBY);
     MLAOptions::$mla_option_definitions[MLAOptions::MLA_DEFAULT_ORDERBY]['options'] = array();
     MLAOptions::$mla_option_definitions[MLAOptions::MLA_DEFAULT_ORDERBY]['texts'] = array();
     $found_current = false;
     foreach ($options as $key => $value) {
         MLAOptions::$mla_option_definitions[MLAOptions::MLA_DEFAULT_ORDERBY]['options'][] = $value;
         MLAOptions::$mla_option_definitions[MLAOptions::MLA_DEFAULT_ORDERBY]['texts'][] = $key;
         if ($current == $value) {
             $found_current = true;
         }
     }
     if (!$found_current) {
         MLAOptions::mla_delete_option(MLAOptions::MLA_DEFAULT_ORDERBY);
     }
     /*
      * Validate the Media Manager sort order or revert to default
      */
     $options = array_merge(array('&mdash; ' . __('Media Manager Default', 'media-library-assistant') . ' &mdash;' => 'default', 'None' => 'none'), $columns);
     $current = MLAOptions::mla_get_option(MLAOptions::MLA_MEDIA_MODAL_ORDERBY);
     MLAOptions::$mla_option_definitions[MLAOptions::MLA_MEDIA_MODAL_ORDERBY]['options'] = array();
     MLAOptions::$mla_option_definitions[MLAOptions::MLA_MEDIA_MODAL_ORDERBY]['texts'] = array();
     $found_current = false;
     foreach ($options as $key => $value) {
         MLAOptions::$mla_option_definitions[MLAOptions::MLA_MEDIA_MODAL_ORDERBY]['options'][] = $value;
         MLAOptions::$mla_option_definitions[MLAOptions::MLA_MEDIA_MODAL_ORDERBY]['texts'][] = $key;
         if ($current == $value) {
             $found_current = true;
         }
     }
     if (!$found_current) {
         MLAOptions::mla_delete_option(MLAOptions::MLA_MEDIA_MODAL_ORDERBY);
     }
     $options_list = '';
     foreach (MLAOptions::$mla_option_definitions as $key => $value) {
         if ('general' == $value['tab']) {
             $options_list .= self::mla_compose_option_row($key, $value);
         }
     }
     $page_values['options_list'] = $options_list;
     $page_values['import_settings'] = self::_compose_import_settings();
     $page_content['body'] = MLAData::mla_parse_template(self::$page_template_array['general-tab'], $page_values);
     return $page_content;
 }
Exemplo n.º 5
0
 /**
  * Compose the General tab content for the Settings subpage
  *
  * @since 0.80
  * @uses $page_template_array contains tab content template(s)
  *
  * @return	array	'message' => status/error messages, 'body' => tab content
  */
 private static function _compose_general_tab()
 {
     /*
      * Check for submit buttons to change or reset settings.
      * Initialize page messages and content.
      */
     if (!empty($_REQUEST['mla-general-options-save'])) {
         check_admin_referer(MLA::MLA_ADMIN_NONCE, '_wpnonce');
         $page_content = self::_save_general_settings();
     } elseif (!empty($_REQUEST['mla-general-options-export'])) {
         check_admin_referer(MLA::MLA_ADMIN_NONCE, '_wpnonce');
         $page_content = self::_export_settings();
     } elseif (!empty($_REQUEST['mla-general-options-import'])) {
         check_admin_referer(MLA::MLA_ADMIN_NONCE, '_wpnonce');
         $page_content = self::_import_settings();
     } elseif (!empty($_REQUEST['mla-general-options-reset'])) {
         check_admin_referer(MLA::MLA_ADMIN_NONCE, '_wpnonce');
         $page_content = self::_reset_general_settings();
     } else {
         $page_content = array('message' => '', 'body' => '');
     }
     if (!empty($page_content['body'])) {
         return $page_content;
     }
     $page_values = array('shortcode_list' => '', 'options_list' => '', 'donateURL' => MLA_PLUGIN_URL . 'images/DonateButton.jpg', 'form_url' => admin_url('options-general.php') . '?page=mla-settings-menu-general&mla_tab=general', '_wpnonce' => wp_nonce_field(MLA::MLA_ADMIN_NONCE, '_wpnonce', true, false), '_wp_http_referer' => wp_referer_field(false));
     /*
      * $custom_fields documents the name and description of custom fields
      */
     $custom_fields = array();
     /* 
      * $shortcodes documents the name and description of plugin shortcodes
      */
     $shortcodes = array(array('name' => 'mla_attachment_list', 'description' => 'renders a complete list of all attachments and references to them.'), array('name' => 'mla_gallery', 'description' => 'enhanced version of the WordPress [gallery] shortcode. For complete documentation <a href="' . admin_url('options-general.php?page=' . self::MLA_SETTINGS_SLUG . '-documentation&amp;mla_tab=documentation') . '">click here</a>.'));
     $shortcode_list = '';
     foreach ($shortcodes as $shortcode) {
         $shortcode_values = array('name' => $shortcode['name'], 'description' => $shortcode['description']);
         $shortcode_list .= MLAData::mla_parse_template(self::$page_template_array['shortcode-item'], $shortcode_values);
     }
     if (!empty($shortcode_list)) {
         $shortcode_values = array('shortcode_list' => $shortcode_list);
         $page_values['shortcode_list'] = MLAData::mla_parse_template(self::$page_template_array['shortcode-list'], $shortcode_values);
     }
     /*
      * Fill in the current list of Media/Assistant table sortable columns, sorted by their labels.
      * Make sure the current choice still exists or revert to default.
      */
     $columns = array();
     foreach (MLA_List_Table::mla_get_sortable_columns() as $key => $value) {
         if (!array_key_exists($value[1], $columns)) {
             $columns[$value[1]] = $value[0];
         }
     }
     uksort($columns, 'strnatcasecmp');
     $options = array_merge(array('None' => 'none'), $columns);
     $current = MLAOptions::mla_get_option(MLAOptions::MLA_DEFAULT_ORDERBY);
     MLAOptions::$mla_option_definitions[MLAOptions::MLA_DEFAULT_ORDERBY]['options'] = array();
     MLAOptions::$mla_option_definitions[MLAOptions::MLA_DEFAULT_ORDERBY]['texts'] = array();
     $found_current = false;
     foreach ($options as $key => $value) {
         MLAOptions::$mla_option_definitions[MLAOptions::MLA_DEFAULT_ORDERBY]['options'][] = $value;
         MLAOptions::$mla_option_definitions[MLAOptions::MLA_DEFAULT_ORDERBY]['texts'][] = $key;
         if ($current == $value) {
             $found_current = true;
         }
     }
     if (!$found_current) {
         MLAOptions::mla_delete_option(MLAOptions::MLA_DEFAULT_ORDERBY);
     }
     /*
      * Validate the Media Manager sort order or revert to default
      */
     $options = array_merge(array(' -- Media Manager Default -- ' => 'default', 'None' => 'none'), $columns);
     $current = MLAOptions::mla_get_option(MLAOptions::MLA_MEDIA_MODAL_ORDERBY);
     MLAOptions::$mla_option_definitions[MLAOptions::MLA_MEDIA_MODAL_ORDERBY]['options'] = array();
     MLAOptions::$mla_option_definitions[MLAOptions::MLA_MEDIA_MODAL_ORDERBY]['texts'] = array();
     $found_current = false;
     foreach ($options as $key => $value) {
         MLAOptions::$mla_option_definitions[MLAOptions::MLA_MEDIA_MODAL_ORDERBY]['options'][] = $value;
         MLAOptions::$mla_option_definitions[MLAOptions::MLA_MEDIA_MODAL_ORDERBY]['texts'][] = $key;
         if ($current == $value) {
             $found_current = true;
         }
     }
     if (!$found_current) {
         MLAOptions::mla_delete_option(MLAOptions::MLA_MEDIA_MODAL_ORDERBY);
     }
     $options_list = '';
     foreach (MLAOptions::$mla_option_definitions as $key => $value) {
         if ('general' == $value['tab']) {
             $options_list .= self::_compose_option_row($key, $value);
         }
     }
     $page_values['options_list'] = $options_list;
     $page_values['import_settings'] = self::_compose_import_settings();
     $page_content['body'] = MLAData::mla_parse_template(self::$page_template_array['general-tab'], $page_values);
     return $page_content;
 }