/** * @param array $posts * @param array $args */ function __construct($posts, $args = array()) { if (isset($posts) && is_array($posts)) { $args = wp_parse_args($args, array('list_owner' => false)); /** * @var WPLib_Posts $list_owner */ $list_owner = $args['list_owner']; if (!$list_owner) { foreach ($posts as $index => $post) { $posts[$index] = $list_owner::make_new_item($post, $args); } } else { foreach ($posts as $index => $post) { $list_owner = $args['list_owner'] = WPLib_Posts::get_post_type_class($post->post_type); $posts[$index] = $list_owner::make_new_item($post, $args); } } } parent::__construct($posts, $args); }
/** * Returns a WPLib_Item_Base from the queried object, if the queried object is a post. * * Useful for accessing the post prior to $wp_the_query * Similar to single_post_title() in concept * * @return WPLib_Post_Base */ function single_post_item() { /** * @var WP_Post $_post */ return $_post = $this->single_post() ? WPLib_Posts::make_new_item($_post) : null; }
/** * @return WPLib_User_List */ function queried_list() { global $wp_the_query; return WPLib_Posts::get_list($wp_the_query, 'default_list=WPLib_Post_List_Default'); }
/** * @param int $value */ static function set_max_posts_per_page($value) { self::$_max_posts_per_page = absint($value); }
/** * @param array|string|WPLib_Query $query * @param array $args * @return WPLib_Post_List_Default[] */ static function get_list($query = array(), $args = array()) { $args = wp_parse_args($args, array('list_owner' => get_called_class())); $try_class = $args['list_owner']; $args = wp_parse_args($args, array('list_class' => "{$try_class}_List", 'default_list' => 'WPLib_Post_List_Default')); if (!class_exists($args['list_class'])) { do { /* * Check first to see if it already exists. Maybe it was passed in and thus does? */ if (class_exists($args['list_class'])) { break; } /* * Add '_Default' to last list class checked, i.e. WPLib_Posts_List_Default for WPLib_Posts::get_list() */ $args['list_class'] = "{$args['list_class']}_Default"; if (class_exists($args['list_class'])) { break; } $args['list_class'] = false; $try_class = preg_replace('#^(.+)_Base$#', '$1', get_parent_class($try_class)); if (!$try_class) { break; } /* * Add '_List' to element class, i.e. WPLib_Posts_List for WPLib_Posts::get_list() */ $args['list_class'] = "{$try_class}_List"; if (class_exists($args['list_class'])) { break; } } while ($try_class); } if (!$args['list_class']) { /* * Give up and use default, i.e. WPLib_List_Default */ $args['list_class'] = $args['default_list']; } $list_class = $args['list_class']; unset($args['list_class'], $args['list_default']); $args['instance_class'] = WPLib::get_constant('INSTANCE_CLASS', $args['list_owner']); if (!($post_type = WPLib::get_constant('POST_TYPE', $args['instance_class']))) { $post_type = 'post'; } if (!is_array($query)) { $query = wp_parse_args($query); } $query = WPLib_Posts::get_query(wp_parse_args($query, array('post_type' => $post_type))); $list = isset($query->posts) ? new $list_class($query->posts, $args) : null; return $list; }
/** * Query the posts, return a post list. * * @param array $args * @return WP_Post[] */ static function get_posts($args = array()) { $query = WPLib_Posts::get_query($args); return $query->posts; }
/** * Query the posts. Equivalent to creating a new WP_Query which both instantiates and queries the DB. * * @param array $args * @return WP_Post[] */ static function get_query($args = array()) { $args = wp_parse_args($args); if (!is_null(static::POST_TYPE)) { $args['post_type'] = static::POST_TYPE; } /** * Query the posts and set the $this->_query with the query used. */ $query = WPLib_Posts::get_query(array_filter($args)); return $query; }
/** * @param WPLib_Item_Base|WP_Post|WP_Term $item * @param array $args * * @return WPLib_Term_Base|WPLib_Post_Base */ static function make_new_item($item, $args = array()) { $class = get_called_class(); if (WPLib::get_constant('POST_TYPE', $class)) { $item = WPLib_Posts::make_new_item($item, $args); } else { if (WPLib::get_constant('TAXONOMY', $class)) { $item = WPLib_Terms::make_new_item($item, $args); } else { $err_msg = __('Cannot make new item. Class %s does not have POST_TYPE or TAXONOMY constant.', 'wplib'); WPLib::trigger_error(sprintf($err_msg, $class)); } } return $item; }