Пример #1
0
 /**
  * Load Multiple Pods Objects
  *
  * $params['type'] string The Object type
  * $params['options'] array Pod Option(s) key=>value array to filter by
  * $params['orderby'] string ORDER BY clause of query
  * $params['limit'] string Number of objects to return
  * $params['where'] string WHERE clause of query
  * $params['ids'] string|array IDs of Objects
  *
  * @param array|object $params An associative array of parameters
  *
  * @return array
  * @since 2.0
  */
 public function load_objects($params)
 {
     $params = (object) pods_sanitize($params);
     if (!isset($params->type) || empty($params->type)) {
         return pods_error(__('Pods Object type is required', 'pods'), $this);
     }
     $order = 'ASC';
     $orderby = 'menu_order';
     $limit = -1;
     $ids = false;
     $meta_query = array();
     $cache_key = '';
     if (isset($params->options) && !empty($params->options) && is_array($params->options)) {
         foreach ($params->options as $option => $value) {
             if (!is_array($value)) {
                 $value = array($value);
             }
             $value = pods_trim($value);
             sort($value);
             $meta_query[] = array('key' => $option, 'value' => pods_sanitize($value), 'compare' => 'IN');
         }
     }
     if (isset($params->where) && is_array($params->where)) {
         $meta_query = array_merge($meta_query, (array) $params->where);
     }
     if (isset($params->order) && !empty($params->order) && in_array(strtoupper($params->order), array('ASC', 'DESC'))) {
         $order = strtoupper($params->order);
     }
     if (isset($params->orderby) && !empty($params->orderby)) {
         $orderby = strtoupper($params->orderby);
     }
     if (isset($params->limit) && !empty($params->limit)) {
         $limit = pods_absint($params->limit);
     }
     if (isset($params->ids) && !empty($params->ids)) {
         $ids = $params->ids;
         if (!is_array($ids)) {
             $ids = explode(',', $ids);
         }
     }
     if (empty($ids)) {
         $ids = false;
     }
     if (pods_api_cache() && empty($meta_query) && empty($limit) && (empty($orderby) || 'menu_order' == $orderby) && empty($ids)) {
         $cache_key = 'pods_objects_' . $params->type;
         $the_objects = pods_transient_get($cache_key);
         if (false !== $the_objects) {
             return $the_objects;
         }
     }
     $the_objects = array();
     $args = array('post_type' => '_pods_' . $params->type, 'nopaging' => true, 'posts_per_page' => $limit, 'order' => $order, 'orderby' => $orderby, 'meta_query' => $meta_query);
     // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
     if (false != $ids) {
         $args['post__in'] = $ids;
     }
     $objects = get_posts($args);
     foreach ($objects as $object) {
         $object = $this->load_object($object);
         $the_objects[$object['name']] = $object;
     }
     if (pods_api_cache() && !empty($cache_key)) {
         pods_transient_set($cache_key, $the_objects);
     }
     return $the_objects;
 }
/**
 * Filter input and return sanitized output
 *
 * @param mixed $input The string, array, or object to sanitize
 * @param string $charlist (optional) List of characters to be stripped from the input.
 * @param string $lr Direction of the trim, can either be 'l' or 'r'.
 *
 * @return array|object|string
 * @since 1.2.0
 */
function pods_trim($input, $charlist = null, $lr = null)
{
    $output = array();
    if (is_object($input)) {
        $input = get_object_vars($input);
        foreach ($input as $key => $val) {
            $output[pods_sanitize($key)] = pods_trim($val, $charlist, $lr);
        }
        $output = (object) $output;
    } elseif (is_array($input)) {
        foreach ($input as $key => $val) {
            $output[pods_sanitize($key)] = pods_trim($val, $charlist, $lr);
        }
    } else {
        if ('l' == $lr) {
            $output = ltrim($input, $charlist);
        } elseif ('r' == $lr) {
            $output = rtrim($input, $charlist);
        } else {
            $output = trim($input, $charlist);
        }
    }
    return $output;
}