From c195d190fe3e927b53014a7f7c5be8171520b700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=8C=E5=BA=86=EF=BC=88Amos=20Lee=EF=BC=89?= Date: Fri, 25 May 2018 08:48:14 +0800 Subject: [PATCH 1/5] fix document error --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e149c1b..8179440 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Retrieve the contents under ("pets" OR "tools") values in the "category" taxonom $builder = new Builder(); $wp_query = $builder->createMainTaxonomyQuery("AND") ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'))) - ->addTaxonomyQuery(TaxonomyQuery::create('custom', 'slug', array('sweet)) + ->addTaxonomyQuery(TaxonomyQuery::create('custom', 'slug', array('sweet'))) ->getWPQuery(); @@ -226,3 +226,5 @@ Get an array containing only the post IDs. This is useful when you want to retur $builder = new Builder(); $wp_query = $builder->inPostIDs($ids)->getWPQuery(); + + From a112434109aeea245f468c21bf5aeee6b69c7f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=8C=E5=BA=86=EF=BC=88Amos=20Lee=EF=BC=89?= Date: Mon, 29 Oct 2018 15:09:55 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=98=8E=E6=98=8E?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=EF=BC=8C=E5=87=86=E5=A4=87=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E5=88=B0=20packagist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 12 ++++++++---- src/Builder.php | 6 +++--- .../MainMetaQueryAlreadyCreatedException.php | 2 +- .../MainTaxonomyQueryAlreadyCreatedException.php | 2 +- src/MetaQuery.php | 2 +- src/MetaQueryCollection.php | 2 +- src/TaxonomyQuery.php | 2 +- src/TaxonomyQueryCollection.php | 2 +- tests/BuilderTest.php | 12 ++++++------ 9 files changed, 23 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 601e076..31d7536 100644 --- a/composer.json +++ b/composer.json @@ -1,15 +1,19 @@ { - "name": "simettric/wp-query-builder", + "name": "wenprise/wp-query-builder", "description": "A query builder for WordPress WP_Query, inspired by Doctrine Query Builder", "keywords": [ "wordpress" ], - "homepage": "http://simettric.com", + "homepage": "https://www.wpzhiku.com", "license": "MIT", "authors": [ { "name": "Asier Marqués", "email": "asier@simettric.com" + }, + { + "name": "Amos Lee", + "email": "amos@wpcio.com" } ], "require": { @@ -20,12 +24,12 @@ }, "autoload": { "psr-4": { - "Simettric\\WPQueryBuilder\\": "src/" + "Wenprise\\WPQueryBuilder\\": "src/" } }, "autoload-dev": { "psr-4": { - "Simettric\\WPQueryBuilder\\Test\\": "tests/" + "Wenprise\\WPQueryBuilder\\Test\\": "tests/" } }, "minimum-stability": "dev" diff --git a/src/Builder.php b/src/Builder.php index 0b4371a..98eacab 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -5,10 +5,10 @@ * Time: 22:36 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; use Collections\Exceptions\Exception; -use Simettric\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException; -use Simettric\WPQueryBuilder\Exception\MainTaxonomyQueryAlreadyCreatedException; +use Wenprise\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException; +use Wenprise\WPQueryBuilder\Exception\MainTaxonomyQueryAlreadyCreatedException; class Builder diff --git a/src/Exception/MainMetaQueryAlreadyCreatedException.php b/src/Exception/MainMetaQueryAlreadyCreatedException.php index ee79dcd..5b49589 100644 --- a/src/Exception/MainMetaQueryAlreadyCreatedException.php +++ b/src/Exception/MainMetaQueryAlreadyCreatedException.php @@ -5,7 +5,7 @@ * Time: 0:22 */ -namespace Simettric\WPQueryBuilder\Exception; +namespace Wenprise\WPQueryBuilder\Exception; class MainMetaQueryAlreadyCreatedException extends \Exception diff --git a/src/Exception/MainTaxonomyQueryAlreadyCreatedException.php b/src/Exception/MainTaxonomyQueryAlreadyCreatedException.php index 681dc57..1a309fc 100644 --- a/src/Exception/MainTaxonomyQueryAlreadyCreatedException.php +++ b/src/Exception/MainTaxonomyQueryAlreadyCreatedException.php @@ -5,7 +5,7 @@ * Time: 0:22 */ -namespace Simettric\WPQueryBuilder\Exception; +namespace Wenprise\WPQueryBuilder\Exception; class MainTaxonomyQueryAlreadyCreatedException extends \Exception diff --git a/src/MetaQuery.php b/src/MetaQuery.php index ae47256..d19e299 100644 --- a/src/MetaQuery.php +++ b/src/MetaQuery.php @@ -5,7 +5,7 @@ * Time: 23:47 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; class MetaQuery diff --git a/src/MetaQueryCollection.php b/src/MetaQueryCollection.php index cfae8ba..3680e23 100644 --- a/src/MetaQueryCollection.php +++ b/src/MetaQueryCollection.php @@ -5,7 +5,7 @@ * Time: 0:06 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; class MetaQueryCollection implements \Iterator diff --git a/src/TaxonomyQuery.php b/src/TaxonomyQuery.php index ca1a70c..6816296 100644 --- a/src/TaxonomyQuery.php +++ b/src/TaxonomyQuery.php @@ -5,7 +5,7 @@ * Time: 23:47 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; class TaxonomyQuery diff --git a/src/TaxonomyQueryCollection.php b/src/TaxonomyQueryCollection.php index 808d0e0..d28408b 100644 --- a/src/TaxonomyQueryCollection.php +++ b/src/TaxonomyQueryCollection.php @@ -5,7 +5,7 @@ * Time: 0:06 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; class TaxonomyQueryCollection implements \Iterator diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index d85ae4f..58d8c06 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -1,11 +1,11 @@ From 6fffdc754db766d385c396b335e4ef2cfe9aca1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=8C=E5=BA=86=EF=BC=88Amos=20Lee=EF=BC=89?= Date: Mon, 29 Oct 2018 15:22:20 +0800 Subject: [PATCH 3/5] format code --- .gitignore | 1 + src/Builder.php | 283 +++++++++++++++++--------------- src/MetaQuery.php | 14 +- src/MetaQueryCollection.php | 11 +- src/TaxonomyQuery.php | 26 ++- src/TaxonomyQueryCollection.php | 11 +- tests/BuilderTest.php | 87 +++++----- 7 files changed, 224 insertions(+), 209 deletions(-) diff --git a/.gitignore b/.gitignore index 7579f74..0ad492d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor composer.lock +.idea/ \ No newline at end of file diff --git a/src/Builder.php b/src/Builder.php index 98eacab..5f22b5e 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -6,6 +6,7 @@ */ namespace Wenprise\WPQueryBuilder; + use Collections\Exceptions\Exception; use Wenprise\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException; use Wenprise\WPQueryBuilder\Exception\MainTaxonomyQueryAlreadyCreatedException; @@ -13,31 +14,31 @@ class Builder { - const POST_TYPE_POST = 'post'; - const POST_TYPE_PAGE = 'page'; - const POST_TYPE_REVISION = 'revision'; + const POST_TYPE_POST = 'post'; + const POST_TYPE_PAGE = 'page'; + const POST_TYPE_REVISION = 'revision'; const POST_TYPE_ATTACHMENT = 'attachment'; - const POST_TYPE_MENU_ITEM = 'nav_menu_item'; - const POST_TYPE_ANY = 'any'; - const POST_STATUS_ANY = 'any'; + const POST_TYPE_MENU_ITEM = 'nav_menu_item'; + const POST_TYPE_ANY = 'any'; + const POST_STATUS_ANY = 'any'; const POST_STATUS_PUBLISHED = 'publish'; - const POST_STATUS_DRAFT = 'draft'; + const POST_STATUS_DRAFT = 'draft'; - private $offset=0; + private $offset = 0; private $posts_per_page; private $order_by; - private $order_direction="DESC"; + private $order_direction = "DESC"; /** * @var array */ - private $parameters=array(); + private $parameters = []; - private $post_types=array(); + private $post_types = []; - private $post_status=array(); + private $post_status = []; private $author = false; @@ -47,12 +48,12 @@ class Builder /** * @var MetaQueryCollection */ - private $mainMetaQueryCollection=null; + private $mainMetaQueryCollection = null; /** * @var TaxonomyQueryCollection */ - private $mainTaxonomyQueryCollection=null; + private $mainTaxonomyQueryCollection = null; /** * @var string @@ -78,42 +79,49 @@ public function __construct() /** * @param $author_id + * * @return $this */ public function setAuthor($author_id) { $this->author = $author_id; + return $this; } /** * @param $limit + * * @return $this */ public function setLimit($limit) { $this->posts_per_page = $limit; + return $this; } /** * @param $offset + * * @return $this */ public function setOffset($offset) { $this->offset = $offset; + return $this; } /** * @param string $direction + * * @return $this */ - public function setOrderDirection($direction="DESC") + public function setOrderDirection($direction = "DESC") { $this->order_direction = $direction; @@ -122,57 +130,60 @@ public function setOrderDirection($direction="DESC") /** * @param $order_by + * * @return $this */ public function setOrderBy($order_by) { $this->order_by = $order_by; + return $this; } /** * @param $order_by + * * @return $this */ - public function addOrderBy($order_by, $direction="DESC") + public function addOrderBy($order_by, $direction = "DESC") { - if(!$this->order_by) - $this->order_by = array(); + if ( ! $this->order_by) { + $this->order_by = []; + } - if($this->order_by && !is_array($this->order_by)) - { - $this->order_by = array((string)$this->order_by => $this->order_direction); + if ($this->order_by && ! is_array($this->order_by)) { + $this->order_by = [(string)$this->order_by => $this->order_direction]; $this->order_direction = null; } - $this->order_by[$order_by] = $direction; + $this->order_by[ $order_by ] = $direction; return $this; } /** - * @param $meta_key + * @param $meta_key * @param bool $numeric + * * @return $this */ - public function setOrderByMeta($meta_key, $direction = "DESC", $numeric=false) + public function setOrderByMeta($meta_key, $direction = "DESC", $numeric = false) { - if($this->meta_key_order || $this->meta_key_order_numeric) + if ($this->meta_key_order || $this->meta_key_order_numeric) { throw new \Exception("You only can order by one meta key"); + } - if($numeric) - { + if ($numeric) { - $this->meta_key_order_numeric = $meta_key; - $this->order_by["meta_value_num"] = $direction; - }else{ - $this->meta_key_order = $meta_key; - $this->order_by["meta_value"] = $direction; + $this->meta_key_order_numeric = $meta_key; + $this->order_by[ "meta_value_num" ] = $direction; + } else { + $this->meta_key_order = $meta_key; + $this->order_by[ "meta_value" ] = $direction; } - return $this; } @@ -183,13 +194,14 @@ public function setOrderByMeta($meta_key, $direction = "DESC", $numeric=false) public function withAnyLimit() { $this->posts_per_page = -1; - $this->offset = 0; + $this->offset = 0; return $this; } /** * @param $search + * * @return $this */ public function search($search) @@ -202,11 +214,12 @@ public function search($search) /** * @param $in_array array + * * @return $this */ public function inPostIDs($in_array) { - $in_array = !is_array($in_array) ? array($in_array) : $in_array; + $in_array = ! is_array($in_array) ? [$in_array] : $in_array; $this->in_array = $in_array; @@ -215,11 +228,12 @@ public function inPostIDs($in_array) /** * @param $in_array array + * * @return $this */ public function notInPostIDs($in_array) { - $in_array = !is_array($in_array) ? array($in_array) : $in_array; + $in_array = ! is_array($in_array) ? [$in_array] : $in_array; $this->not_in_array = $in_array; @@ -228,20 +242,23 @@ public function notInPostIDs($in_array) /** - * @param string $where_type + * @param string $where_type * @param MetaQueryCollection|null $collection + * * @return $this * @throws MainMetaQueryAlreadyCreatedException */ - public function createMainMetaQuery($where_type="AND", MetaQueryCollection $collection=null) + public function createMainMetaQuery($where_type = "AND", MetaQueryCollection $collection = null) { - if($this->mainMetaQueryCollection) + if ($this->mainMetaQueryCollection) { throw new MainMetaQueryAlreadyCreatedException(); + } $this->mainMetaQueryCollection = new MetaQueryCollection($where_type); - if($collection) + if ($collection) { $this->mainMetaQueryCollection->addCollection($collection); + } return $this; @@ -249,20 +266,23 @@ public function createMainMetaQuery($where_type="AND", MetaQueryCollection $coll /** - * @param string $where_type + * @param string $where_type * @param TaxonomyQueryCollection|null $collection + * * @return $this * @throws MainTaxonomyQueryAlreadyCreatedException */ - public function createMainTaxonomyQuery($where_type="AND", TaxonomyQueryCollection $collection=null) + public function createMainTaxonomyQuery($where_type = "AND", TaxonomyQueryCollection $collection = null) { - if($this->mainTaxonomyQueryCollection) + if ($this->mainTaxonomyQueryCollection) { throw new MainTaxonomyQueryAlreadyCreatedException(); + } $this->mainTaxonomyQueryCollection = new TaxonomyQueryCollection($where_type); - if($collection) + if ($collection) { $this->mainTaxonomyQueryCollection->addCollection($collection); + } return $this; } @@ -273,6 +293,7 @@ public function createMainTaxonomyQuery($where_type="AND", TaxonomyQueryCollecti public function setAnyPostType() { $this->post_types = static::POST_TYPE_ANY; + return $this; } @@ -282,30 +303,29 @@ public function setAnyPostType() public function setAnyPostStatus() { $this->post_status = static::POST_STATUS_ANY; + return $this; } /** * @param $type + * * @return $this */ public function addPostType($type) { - if($this->post_types == static::POST_TYPE_ANY) - { - $this->post_types = array(); + if ($this->post_types == static::POST_TYPE_ANY) { + $this->post_types = []; } - if(is_array($type)) - { - foreach ($type as $value) - { - $this->post_types[$value] = $value; + if (is_array($type)) { + foreach ($type as $value) { + $this->post_types[ $value ] = $value; } - }else{ + } else { - $this->post_types[$type] = $type; + $this->post_types[ $type ] = $type; } return $this; @@ -314,49 +334,52 @@ public function addPostType($type) /** * @param $status + * * @return $this */ public function addPostStatus($status) { - if($this->post_status == static::POST_STATUS_ANY) - { - $this->post_status = array(); + if ($this->post_status == static::POST_STATUS_ANY) { + $this->post_status = []; } - if(is_array($status)) - { - foreach ($status as $value) - { - $this->post_status[$value] = $value; + if (is_array($status)) { + foreach ($status as $value) { + $this->post_status[ $value ] = $value; } - }else{ + } else { - $this->post_status[$status] = $status; + $this->post_status[ $status ] = $status; } return $this; } + /** * @param $type + * * @return $this */ public function removePostType($type) { - if(isset($this->post_types[$type])) - unset($this->post_types[$type]); + if (isset($this->post_types[ $type ])) { + unset($this->post_types[ $type ]); + } return $this; } /** * @param MetaQueryCollection $collection + * * @return $this */ public function addMetaQueryCollection(MetaQueryCollection $collection) { - if(!$this->mainMetaQueryCollection) + if ( ! $this->mainMetaQueryCollection) { $this->createMainMetaQuery(); + } $this->mainMetaQueryCollection->addCollection($collection); @@ -366,12 +389,14 @@ public function addMetaQueryCollection(MetaQueryCollection $collection) /** * @param MetaQuery $metaQuery + * * @return $this */ public function addMetaQuery(MetaQuery $metaQuery) { - if(!$this->mainMetaQueryCollection) + if ( ! $this->mainMetaQueryCollection) { $this->createMainMetaQuery(); + } $this->mainMetaQueryCollection->add($metaQuery); @@ -380,12 +405,14 @@ public function addMetaQuery(MetaQuery $metaQuery) /** * @param TaxonomyQueryCollection $collection + * * @return $this */ public function addTaxonomyQueryCollection(TaxonomyQueryCollection $collection) { - if(!$this->mainTaxonomyQueryCollection) + if ( ! $this->mainTaxonomyQueryCollection) { $this->createMainMetaQuery(); + } $this->mainTaxonomyQueryCollection->addCollection($collection); @@ -395,12 +422,14 @@ public function addTaxonomyQueryCollection(TaxonomyQueryCollection $collection) /** * @param TaxonomyQuery $metaQuery + * * @return $this */ public function addTaxonomyQuery(TaxonomyQuery $metaQuery) { - if(!$this->mainTaxonomyQueryCollection) + if ( ! $this->mainTaxonomyQueryCollection) { $this->createMainTaxonomyQuery(); + } $this->mainTaxonomyQueryCollection->add($metaQuery); @@ -432,6 +461,7 @@ public function getWPQuery() public function getPosts() { $wp_query = $this->getWPQuery(); + return $wp_query->get_posts(); } @@ -442,7 +472,7 @@ public function getPostIDs() { $this->hydrateParametersArray(); - $this->parameters["fields"] = "ids"; + $this->parameters[ "fields" ] = "ids"; $wp_query = $this->getWPQuery(); @@ -455,27 +485,23 @@ public function getPostIDs() */ private function hydrateParametersArray() { - $this->parameters["post_type"] = $this->getPostTypeParametersArray(); - $this->parameters["post_status"] = $this->getPostStatusParametersArray(); + $this->parameters[ "post_type" ] = $this->getPostTypeParametersArray(); + $this->parameters[ "post_status" ] = $this->getPostStatusParametersArray(); - if($this->author) - { - $this->parameters["author"] = $this->author; + if ($this->author) { + $this->parameters[ "author" ] = $this->author; } - if($this->search_parameter) - { - $this->parameters["s"] = $this->search_parameter; + if ($this->search_parameter) { + $this->parameters[ "s" ] = $this->search_parameter; } - if($this->mainMetaQueryCollection) - { - $this->parameters["meta_query"] = $this->getMetaParametersArray($this->mainMetaQueryCollection); + if ($this->mainMetaQueryCollection) { + $this->parameters[ "meta_query" ] = $this->getMetaParametersArray($this->mainMetaQueryCollection); } - if($this->mainTaxonomyQueryCollection) - { - $this->parameters["tax_query"] = $this->getTaxonomyParametersArray($this->mainTaxonomyQueryCollection); + if ($this->mainTaxonomyQueryCollection) { + $this->parameters[ "tax_query" ] = $this->getTaxonomyParametersArray($this->mainTaxonomyQueryCollection); } $this->hydrateLimitsParameters(); @@ -488,27 +514,25 @@ private function hydrateParametersArray() /** * @param MetaQueryCollection $collection - * @param array $return_array + * @param array $return_array + * * @return array */ - private function getMetaParametersArray(MetaQueryCollection $collection, $return_array=array()) + private function getMetaParametersArray(MetaQueryCollection $collection, $return_array = []) { - $return_array["relation"] = $collection->getRelationType(); + $return_array[ "relation" ] = $collection->getRelationType(); - foreach ($collection as $meta) - { - if($meta instanceof MetaQuery) - { - $return_array[] = array( + foreach ($collection as $meta) { + if ($meta instanceof MetaQuery) { + $return_array[] = [ "key" => $meta->key, "value" => $meta->value, "compare" => $meta->compare, "type" => $meta->type, - ); + ]; - }else if($meta instanceof MetaQueryCollection) - { + } elseif ($meta instanceof MetaQueryCollection) { $return_array[] = $this->getMetaParametersArray($meta); } } @@ -518,28 +542,26 @@ private function getMetaParametersArray(MetaQueryCollection $collection, $return /** * @param TaxonomyQueryCollection $collection - * @param array $return_array + * @param array $return_array + * * @return array */ - private function getTaxonomyParametersArray(TaxonomyQueryCollection $collection, $return_array=array()) + private function getTaxonomyParametersArray(TaxonomyQueryCollection $collection, $return_array = []) { - $return_array["relation"] = $collection->getRelationType(); + $return_array[ "relation" ] = $collection->getRelationType(); - foreach ($collection as $tax) - { - if($tax instanceof TaxonomyQuery) - { - $return_array[] = array( + foreach ($collection as $tax) { + if ($tax instanceof TaxonomyQuery) { + $return_array[] = [ "taxonomy" => $tax->taxonomy, "field" => $tax->field, "terms" => $tax->terms, "include_children" => $tax->include_children, - "operator" => $tax->operator - ); + "operator" => $tax->operator, + ]; - }else if($tax instanceof TaxonomyQueryCollection) - { + } elseif ($tax instanceof TaxonomyQueryCollection) { $return_array[] = $this->getTaxonomyParametersArray($tax); } } @@ -553,8 +575,7 @@ private function getTaxonomyParametersArray(TaxonomyQueryCollection $collection, */ private function getPostTypeParametersArray() { - if(is_array($this->post_types)) - { + if (is_array($this->post_types)) { return array_values($this->post_types); } @@ -567,8 +588,7 @@ private function getPostTypeParametersArray() */ private function getPostStatusParametersArray() { - if(is_array($this->post_status)) - { + if (is_array($this->post_status)) { return array_values($this->post_status); } @@ -581,10 +601,9 @@ private function getPostStatusParametersArray() private function hydrateLimitsParameters() { - if($this->posts_per_page) - { - $this->parameters["posts_per_page"] = $this->posts_per_page; - $this->parameters["offset"] = (int) $this->offset; + if ($this->posts_per_page) { + $this->parameters[ "posts_per_page" ] = $this->posts_per_page; + $this->parameters[ "offset" ] = (int)$this->offset; } } @@ -596,25 +615,21 @@ private function hydrateLimitsParameters() private function hydrateOrderParameters() { - if($this->order_by) - { + if ($this->order_by) { - $this->parameters["orderby"] = $this->order_by; - if(!is_array($this->order_by)) - { - $this->parameters["order"] = $this->order_direction?$this->order_direction:"DESC"; + $this->parameters[ "orderby" ] = $this->order_by; + if ( ! is_array($this->order_by)) { + $this->parameters[ "order" ] = $this->order_direction ? $this->order_direction : "DESC"; } } - if($this->meta_key_order || $this->meta_key_order_numeric) - { + if ($this->meta_key_order || $this->meta_key_order_numeric) { - if($this->meta_key_order) - { - $this->parameters["meta_key"] = $this->meta_key_order; + if ($this->meta_key_order) { + $this->parameters[ "meta_key" ] = $this->meta_key_order; - }else{ - $this->parameters["meta_key"] = $this->meta_key_order_numeric; + } else { + $this->parameters[ "meta_key" ] = $this->meta_key_order_numeric; } } @@ -627,14 +642,12 @@ private function hydrateOrderParameters() private function hydrateInParameters() { - if(is_array($this->in_array)) - { - $this->parameters["post__in"] = $this->in_array; + if (is_array($this->in_array)) { + $this->parameters[ "post__in" ] = $this->in_array; } - if(is_array($this->not_in_array)) - { - $this->parameters["post__not_in"] = $this->not_in_array; + if (is_array($this->not_in_array)) { + $this->parameters[ "post__not_in" ] = $this->not_in_array; } } diff --git a/src/MetaQuery.php b/src/MetaQuery.php index d19e299..bc768ac 100644 --- a/src/MetaQuery.php +++ b/src/MetaQuery.php @@ -21,15 +21,17 @@ class MetaQuery /** - * @param $key - * @param $value + * @param $key + * @param $value * @param string $compare ( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' and 'NOT EXISTS') - * @param string $type ('NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', You can also specify precision and scale for the 'DECIMAL' and 'NUMERIC' types (for example, 'DECIMAL(10,5)' or 'NUMERIC(10)' are valid) + * @param string $type ('NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', You can also specify precision and + * scale for the 'DECIMAL' and 'NUMERIC' types (for example, 'DECIMAL(10,5)' or 'NUMERIC(10)' are valid) + * * @return MetaQuery */ - public static function create($key, $value, $compare="=", $type="CHAR") + public static function create($key, $value, $compare = "=", $type = "CHAR") { - $instance = new MetaQuery(); + $instance = new MetaQuery(); $instance->key = $key; $instance->value = $value; $instance->compare = $compare; @@ -39,6 +41,4 @@ public static function create($key, $value, $compare="=", $type="CHAR") } - - } diff --git a/src/MetaQueryCollection.php b/src/MetaQueryCollection.php index 3680e23..740148b 100644 --- a/src/MetaQueryCollection.php +++ b/src/MetaQueryCollection.php @@ -11,13 +11,13 @@ class MetaQueryCollection implements \Iterator { - private $position = 0; - private $meta_queries = array(); + private $position = 0; + private $meta_queries = []; private $where_type_relation; - public function __construct($where_type="AND") + public function __construct($where_type = "AND") { $this->where_type_relation = $where_type; } @@ -33,6 +33,7 @@ public function getRelationType() /** * @param MetaQuery $query + * * @return $this */ public function add(MetaQuery $query) @@ -56,7 +57,7 @@ public function addCollection(MetaQueryCollection $collection) */ public function current() { - return $this->meta_queries[$this->position]; + return $this->meta_queries[ $this->position ]; } /** @@ -80,7 +81,7 @@ public function key() */ public function valid() { - return isset($this->meta_queries[$this->position]); + return isset($this->meta_queries[ $this->position ]); } /** diff --git a/src/TaxonomyQuery.php b/src/TaxonomyQuery.php index 6816296..bdb36a1 100644 --- a/src/TaxonomyQuery.php +++ b/src/TaxonomyQuery.php @@ -15,33 +15,33 @@ class TaxonomyQuery public $field; - public $terms=array(); + public $terms = []; - public $include_children=true; + public $include_children = true; /** * @var string string ('IN', 'NOT IN', 'AND', 'EXISTS' and 'NOT EXISTS') */ - public $operator="IN"; + public $operator = "IN"; /** - * @param $taxonomy - * @param $field - * @param $terms - * @param bool $include_children + * @param $taxonomy + * @param $field + * @param $terms + * @param bool $include_children * @param string $operator ('IN', 'NOT IN', 'AND', 'EXISTS' and 'NOT EXISTS') + * * @return TaxonomyQuery */ - public static function create($taxonomy, $field, $terms=array(), $include_children=true, $operator="IN") + public static function create($taxonomy, $field, $terms = [], $include_children = true, $operator = "IN") { - if(!is_array($terms)) - { - $terms = array($terms); + if ( ! is_array($terms)) { + $terms = [$terms]; } - $instance = new TaxonomyQuery(); + $instance = new TaxonomyQuery(); $instance->taxonomy = $taxonomy; $instance->field = $field; $instance->terms = $terms; @@ -52,6 +52,4 @@ public static function create($taxonomy, $field, $terms=array(), $include_childr } - - } diff --git a/src/TaxonomyQueryCollection.php b/src/TaxonomyQueryCollection.php index d28408b..858f70f 100644 --- a/src/TaxonomyQueryCollection.php +++ b/src/TaxonomyQueryCollection.php @@ -11,13 +11,13 @@ class TaxonomyQueryCollection implements \Iterator { - private $position = 0; - private $tax_queries = array(); + private $position = 0; + private $tax_queries = []; private $where_type_relation; - public function __construct($where_type="AND") + public function __construct($where_type = "AND") { $this->where_type_relation = $where_type; } @@ -33,6 +33,7 @@ public function getRelationType() /** * @param TaxonomyQuery $query + * * @return $this */ public function add(TaxonomyQuery $query) @@ -56,7 +57,7 @@ public function addCollection(TaxonomyQueryCollection $collection) */ public function current() { - return $this->tax_queries[$this->position]; + return $this->tax_queries[ $this->position ]; } /** @@ -80,7 +81,7 @@ public function key() */ public function valid() { - return isset($this->tax_queries[$this->position]); + return isset($this->tax_queries[ $this->position ]); } /** diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 58d8c06..e18cfa9 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -1,6 +1,7 @@ assertArrayHasKey('meta_query', $parameters); - $this->assertEquals("AND", $parameters["meta_query"]["relation"]); - $this->assertEquals("test", $parameters["meta_query"][0]["key"]); + $this->assertEquals("AND", $parameters[ "meta_query" ][ "relation" ]); + $this->assertEquals("test", $parameters[ "meta_query" ][ 0 ][ "key" ]); $collection = new MetaQueryCollection('OR'); $collection->add(MetaQuery::create('test', 'value_test')); @@ -35,7 +36,7 @@ public function testMetaParameters() $parameters = $builder->getParameters(); - $this->assertEquals("OR", $parameters["meta_query"][1]["relation"]); + $this->assertEquals("OR", $parameters[ "meta_query" ][ 1 ][ "relation" ]); } @@ -47,35 +48,35 @@ public function testPostTypeParameters() $this->assertArrayHasKey('post_type', $parameters); - $this->assertContains(Builder::POST_TYPE_ANY, $parameters["post_type"]); + $this->assertContains(Builder::POST_TYPE_ANY, $parameters[ "post_type" ]); $builder->addPostType(Builder::POST_TYPE_PAGE); $builder->addPostType(Builder::POST_TYPE_POST); $parameters = $builder->getParameters(); - $this->assertCount(2, $parameters["post_type"]); + $this->assertCount(2, $parameters[ "post_type" ]); $builder->removePostType(Builder::POST_TYPE_PAGE); $parameters = $builder->getParameters(); - $this->assertCount(1, $parameters["post_type"]); + $this->assertCount(1, $parameters[ "post_type" ]); - $this->assertContains(Builder::POST_TYPE_POST, $parameters["post_type"]); + $this->assertContains(Builder::POST_TYPE_POST, $parameters[ "post_type" ]); $builder->setAnyPostType(); $parameters = $builder->getParameters(); - $this->assertEquals(Builder::POST_TYPE_ANY, $parameters["post_type"]); + $this->assertEquals(Builder::POST_TYPE_ANY, $parameters[ "post_type" ]); - $builder->addPostType([Builder::POST_TYPE_PAGE,Builder::POST_TYPE_POST]); + $builder->addPostType([Builder::POST_TYPE_PAGE, Builder::POST_TYPE_POST]); $parameters = $builder->getParameters(); - $this->assertCount(2, $parameters["post_type"]); - $this->assertContains(Builder::POST_TYPE_POST, $parameters["post_type"]); - $this->assertContains(Builder::POST_TYPE_PAGE, $parameters["post_type"]); + $this->assertCount(2, $parameters[ "post_type" ]); + $this->assertContains(Builder::POST_TYPE_POST, $parameters[ "post_type" ]); + $this->assertContains(Builder::POST_TYPE_PAGE, $parameters[ "post_type" ]); } @@ -91,21 +92,21 @@ public function testLimitsParameters() $builder->setLimit(10); $parameters = $builder->getParameters(); - $this->assertEquals(10, $parameters["posts_per_page"]); - $this->assertEquals(0, $parameters["offset"]); + $this->assertEquals(10, $parameters[ "posts_per_page" ]); + $this->assertEquals(0, $parameters[ "offset" ]); $builder->setLimit(8); $builder->setOffset(2); $parameters = $builder->getParameters(); - $this->assertEquals(8, $parameters["posts_per_page"]); - $this->assertEquals(2, $parameters["offset"]); + $this->assertEquals(8, $parameters[ "posts_per_page" ]); + $this->assertEquals(2, $parameters[ "offset" ]); $builder->withAnyLimit(); $parameters = $builder->getParameters(); - $this->assertEquals(-1, $parameters["posts_per_page"]); - $this->assertEquals(0, $parameters["offset"]); + $this->assertEquals(-1, $parameters[ "posts_per_page" ]); + $this->assertEquals(0, $parameters[ "offset" ]); } public function testOrderParameters() @@ -116,29 +117,29 @@ public function testOrderParameters() $this->assertArrayHasKey('order', $parameters); $this->assertArrayHasKey('orderby', $parameters); - $this->assertEquals("DESC", $parameters["order"]); - $this->assertEquals("date", $parameters["orderby"]); + $this->assertEquals("DESC", $parameters[ "order" ]); + $this->assertEquals("date", $parameters[ "orderby" ]); $parameters = $builder->addOrderBy('title', "ASC")->getParameters(); - $this->assertArrayHasKey("title", $parameters["orderby"]); - $this->assertArrayHasKey("date", $parameters["orderby"]); + $this->assertArrayHasKey("title", $parameters[ "orderby" ]); + $this->assertArrayHasKey("date", $parameters[ "orderby" ]); - $this->assertEquals("DESC", $parameters["orderby"]["date"]); - $this->assertEquals("ASC", $parameters["orderby"]["title"]); + $this->assertEquals("DESC", $parameters[ "orderby" ][ "date" ]); + $this->assertEquals("ASC", $parameters[ "orderby" ][ "title" ]); $parameters = $builder->setOrderByMeta('color', "DESC")->getParameters(); - $this->assertArrayHasKey("meta_value", $parameters["orderby"]); - $this->assertEquals("color", $parameters["meta_key"]); - $this->assertEquals("DESC", $parameters["orderby"]["meta_value"]); + $this->assertArrayHasKey("meta_value", $parameters[ "orderby" ]); + $this->assertEquals("color", $parameters[ "meta_key" ]); + $this->assertEquals("DESC", $parameters[ "orderby" ][ "meta_value" ]); - $builder = new Builder(); + $builder = new Builder(); $parameters = $builder->setOrderByMeta('price', "ASC", true)->getParameters(); - $this->assertArrayHasKey("meta_value_num", $parameters["orderby"]); - $this->assertEquals("price", $parameters["meta_key"]); - $this->assertEquals("ASC", $parameters["orderby"]["meta_value_num"]); + $this->assertArrayHasKey("meta_value_num", $parameters[ "orderby" ]); + $this->assertEquals("price", $parameters[ "meta_key" ]); + $this->assertEquals("ASC", $parameters[ "orderby" ][ "meta_value_num" ]); } @@ -146,24 +147,24 @@ public function testTaxonomyQueryParameter() { $builder = new Builder(); $builder->createMainTaxonomyQuery(); - $builder->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('blue'))); + $builder->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', ['blue'])); $parameters = $builder->getParameters(); $this->assertArrayHasKey('tax_query', $parameters); - $this->assertEquals("AND", $parameters["tax_query"]["relation"]); - $this->assertEquals("category", $parameters["tax_query"][0]["taxonomy"]); - $this->assertEquals("slug", $parameters["tax_query"][0]["field"]); - $this->assertEquals("blue", $parameters["tax_query"][0]["terms"][0]); + $this->assertEquals("AND", $parameters[ "tax_query" ][ "relation" ]); + $this->assertEquals("category", $parameters[ "tax_query" ][ 0 ][ "taxonomy" ]); + $this->assertEquals("slug", $parameters[ "tax_query" ][ 0 ][ "field" ]); + $this->assertEquals("blue", $parameters[ "tax_query" ][ 0 ][ "terms" ][ 0 ]); $collection = new TaxonomyQueryCollection('OR'); - $collection->add(TaxonomyQuery::create('tag', 'slug', array('pets'))); + $collection->add(TaxonomyQuery::create('tag', 'slug', ['pets'])); $builder->addTaxonomyQueryCollection($collection); $parameters = $builder->getParameters(); - $this->assertEquals("OR", $parameters["tax_query"][1]["relation"]); + $this->assertEquals("OR", $parameters[ "tax_query" ][ 1 ][ "relation" ]); } @@ -175,22 +176,22 @@ public function testSearchParameter() $parameters = $builder->search('test')->getParameters(); $this->assertArrayHasKey('s', $parameters); - $this->assertEquals("test", $parameters["s"]); + $this->assertEquals("test", $parameters[ "s" ]); } public function testInParameters() { $builder = new Builder(); - $parameters = $builder->inPostIDs(array(1, 2))->getParameters(); + $parameters = $builder->inPostIDs([1, 2])->getParameters(); $this->assertArrayHasKey('post__in', $parameters); - $this->assertContains(1, $parameters["post__in"]); + $this->assertContains(1, $parameters[ "post__in" ]); - $parameters = $builder->notInPostIDs(array(3))->getParameters(); + $parameters = $builder->notInPostIDs([3])->getParameters(); $this->assertArrayHasKey('post__not_in', $parameters); - $this->assertContains(3, $parameters["post__not_in"]); + $this->assertContains(3, $parameters[ "post__not_in" ]); } } From 54c520d7460fe697f3771347b8f76f307a9f43bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=8C=E5=BA=86=EF=BC=88Amos=20Lee=EF=BC=89?= Date: Mon, 29 Oct 2018 15:33:04 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=AE=80=E5=8C=96=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++++++------- src/Builder.php | 12 ++++++------ tests/BuilderTest.php | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8179440..f838ac6 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ USAGE Retrieve any post type where post meta color value equals to blue OR size meta value equals to XL $builder = new Builder(); - $wp_query = $builder->createMainMetaQuery("OR") + $wp_query = $builder->createMetaQuery("OR") ->addMetaQuery(MetaQuery::create('color', 'blue')) ->addMetaQuery(MetaQuery::create('size', 'XL')) ->getWPQuery(); @@ -29,7 +29,7 @@ Retrieve any post type where post meta color value equals to blue OR size meta v Retrieve any post type where post meta price is equal or greater than 10 OR size meta value equals to XL $builder = new Builder(); - $wp_query = $builder->createMainMetaQuery("AND") + $wp_query = $builder->createMetaQuery("AND") ->addMetaQuery(MetaQuery::create('price', 10, '>=', 'NUMERIC')) ->addMetaQuery(MetaQuery::create('size', 'XL')) ->getWPQuery(); @@ -38,7 +38,7 @@ Retrieve any post type where post meta price is equal or greater than 10 OR size Retrieve any post type where post meta price is equal or greater than 10 AND (size meta value equals to XL OR post meta color value equals to blue) $builder = new Builder(); - $builder->createMainMetaQuery("AND") + $builder->createMetaQuery("AND") ->addMetaQuery(MetaQuery::create('price', 10, '>=', 'NUMERIC')); $condition = new MetaQueryCollection('OR'); @@ -55,7 +55,7 @@ Retrieve any post type where post meta price is equal or greater than 10 OR size Retrieve the contents under ("pets" OR "tools") values in the "category" taxonomy AND in under 'sweet' in "custom" taxonomy $builder = new Builder(); - $wp_query = $builder->createMainTaxonomyQuery("AND") + $wp_query = $builder->createTaxonomyQuery("AND") ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'))) ->addTaxonomyQuery(TaxonomyQuery::create('custom', 'slug', array('sweet'))) ->getWPQuery(); @@ -64,14 +64,14 @@ Retrieve the contents under ("pets" OR "tools") values in the "category" taxonom Retrieve the contents under ("pets" OR "tools") values in the "category" taxonomy BUT exclude contents in their children $builder = new Builder(); - $wp_query = $builder->createMainTaxonomyQuery("AND") + $wp_query = $builder->createTaxonomyQuery("AND") ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'), false)) ->getWPQuery(); Retrieve the contents those are NOT under ("pets" OR "tools") values in the "category" taxonomy $builder = new Builder(); - $wp_query = $builder->createMainTaxonomyQuery("AND") + $wp_query = $builder->createTaxonomyQuery("AND") ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'), true, 'NOT IN')) ->getWPQuery(); @@ -83,7 +83,7 @@ You can have nested relations too $collection->add(TaxonomyQuery::create('tag', 'slug', array('cats'))); $collection->add(TaxonomyQuery::create('custom', 'slug', array('sweet'))); - $wp_query = $builder->createMainTaxonomyQuery("AND") + $wp_query = $builder->createTaxonomyQuery("AND") ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'))) ->addTaxonomyQueryCollection($collection) ->getWPQuery(); diff --git a/src/Builder.php b/src/Builder.php index 5f22b5e..7611656 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -248,7 +248,7 @@ public function notInPostIDs($in_array) * @return $this * @throws MainMetaQueryAlreadyCreatedException */ - public function createMainMetaQuery($where_type = "AND", MetaQueryCollection $collection = null) + public function createMetaQuery($where_type = "AND", MetaQueryCollection $collection = null) { if ($this->mainMetaQueryCollection) { throw new MainMetaQueryAlreadyCreatedException(); @@ -272,7 +272,7 @@ public function createMainMetaQuery($where_type = "AND", MetaQueryCollection $co * @return $this * @throws MainTaxonomyQueryAlreadyCreatedException */ - public function createMainTaxonomyQuery($where_type = "AND", TaxonomyQueryCollection $collection = null) + public function createTaxonomyQuery($where_type = "AND", TaxonomyQueryCollection $collection = null) { if ($this->mainTaxonomyQueryCollection) { throw new MainTaxonomyQueryAlreadyCreatedException(); @@ -378,7 +378,7 @@ public function removePostType($type) public function addMetaQueryCollection(MetaQueryCollection $collection) { if ( ! $this->mainMetaQueryCollection) { - $this->createMainMetaQuery(); + $this->createMetaQuery(); } $this->mainMetaQueryCollection->addCollection($collection); @@ -395,7 +395,7 @@ public function addMetaQueryCollection(MetaQueryCollection $collection) public function addMetaQuery(MetaQuery $metaQuery) { if ( ! $this->mainMetaQueryCollection) { - $this->createMainMetaQuery(); + $this->createMetaQuery(); } $this->mainMetaQueryCollection->add($metaQuery); @@ -411,7 +411,7 @@ public function addMetaQuery(MetaQuery $metaQuery) public function addTaxonomyQueryCollection(TaxonomyQueryCollection $collection) { if ( ! $this->mainTaxonomyQueryCollection) { - $this->createMainMetaQuery(); + $this->createMetaQuery(); } $this->mainTaxonomyQueryCollection->addCollection($collection); @@ -428,7 +428,7 @@ public function addTaxonomyQueryCollection(TaxonomyQueryCollection $collection) public function addTaxonomyQuery(TaxonomyQuery $metaQuery) { if ( ! $this->mainTaxonomyQueryCollection) { - $this->createMainTaxonomyQuery(); + $this->createTaxonomyQuery(); } $this->mainTaxonomyQueryCollection->add($metaQuery); diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index e18cfa9..cbe9763 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -20,7 +20,7 @@ class BuilderTest extends \PHPUnit_Framework_TestCase public function testMetaParameters() { $builder = new Builder(); - $builder->createMainMetaQuery(); + $builder->createMetaQuery(); $builder->addMetaQuery(MetaQuery::create('test', 'value_test')); $parameters = $builder->getParameters(); @@ -146,7 +146,7 @@ public function testOrderParameters() public function testTaxonomyQueryParameter() { $builder = new Builder(); - $builder->createMainTaxonomyQuery(); + $builder->createTaxonomyQuery(); $builder->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', ['blue'])); $parameters = $builder->getParameters(); From c72b88b4a92594e2adcee5ffdae1cdb3ce79323c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=8C=E5=BA=86=EF=BC=88Amos=20Lee=EF=BC=89?= Date: Mon, 29 Oct 2018 15:45:22 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 43 +++++++++++++++++++++++++++---------------- src/Builder.php | 11 +++++++++-- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f838ac6..598925a 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ INSTALLATION USAGE ===== -### META QUERIES +### 元数据查询 -Retrieve any post type where post meta color value equals to blue OR size meta value equals to XL +获取元数据 "color" 为 "blue" 或者 "size" 为 "XL" 的所有文章类型中的文章 $builder = new Builder(); $wp_query = $builder->createMetaQuery("OR") @@ -26,7 +26,7 @@ Retrieve any post type where post meta color value equals to blue OR size meta v ->getWPQuery(); -Retrieve any post type where post meta price is equal or greater than 10 OR size meta value equals to XL +获取元数据 "price" 大于等于 "10" 并且 "size" 为 "XL" 的所有文章类型中的文章 $builder = new Builder(); $wp_query = $builder->createMetaQuery("AND") @@ -49,7 +49,7 @@ Retrieve any post type where post meta price is equal or greater than 10 OR size $wp_query = $builder->addMetaQueryCollection($condition) ->getWPQuery(); -### TAXONOMY QUERIES +### 自定义分类法查询 Retrieve the contents under ("pets" OR "tools") values in the "category" taxonomy AND in under 'sweet' in "custom" taxonomy @@ -89,17 +89,19 @@ You can have nested relations too ->getWPQuery(); -### POST TYPES +### 文章类型 Retrieve all PAGES $builder = new Builder(); - $wp_query = $builder->addPostType(Builder::POST_TYPE_PAGE)->getWPQuery(); + $wp_query = $builder->addPostType(Builder::POST_TYPE_PAGE) + ->getWPQuery(); Retrieve all CUSTOM POST TYPE $builder = new Builder(); - $wp_query = $builder->addPostType('your_custom')->getWPQuery(); + $wp_query = $builder->addPostType('your_custom') + ->getWPQuery(); Retrieve all CUSTOM POST TYPE and PAGES @@ -109,13 +111,14 @@ Retrieve all CUSTOM POST TYPE and PAGES ->getWPQuery(); -### SEARCH +### 搜索 Search contents $builder = new Builder(); - $wp_query = $builder->search("search query")->getWPQuery(); + $wp_query = $builder->search("search query") + ->getWPQuery(); ### IN and NOT IN @@ -124,13 +127,15 @@ Retrieve contents with ID in array of IDS $builder = new Builder(); - $wp_query = $builder->inPostIDs(array(1,2,3))->getWPQuery(); + $wp_query = $builder->inPostIDs(array(1,2,3)) + ->getWPQuery(); Retrieve contents with ID not in array of IDS $builder = new Builder(); - $wp_query = $builder->notInPostIDs(array(1,2,3))->getWPQuery(); + $wp_query = $builder->notInPostIDs(array(1,2,3)) + ->getWPQuery(); ### ORDERBY @@ -139,7 +144,8 @@ Order contents by title descending $builder = new Builder(); - $wp_query = $builder->setOrderBy("title")->getWPQuery(); + $wp_query = $builder->setOrderBy("title") + ->getWPQuery(); Order contents by date, ascending @@ -164,7 +170,8 @@ Order contents by custom meta $builder = new Builder(); - $wp_query = $builder->setOrderByMeta("color", "DESC")->getWPQuery(); + $wp_query = $builder->setOrderByMeta("color", "DESC") + ->getWPQuery(); Order contents by custom numeric meta @@ -181,14 +188,17 @@ Retrieve only 10 contents $builder = new Builder(); - $wp_query = $builder->setLimit(10)->getWPQuery(); + $wp_query = $builder->setLimit(10) + ->getWPQuery(); Retrieve 20 contents starting from the 10th position $builder = new Builder(); - $wp_query = $builder->setLimit(20)->setOffset(10)->getWPQuery(); + $wp_query = $builder->setLimit(20) + ->setOffset(10) + ->getWPQuery(); ### RETRIEVING @@ -225,6 +235,7 @@ Get an array containing only the post IDs. This is useful when you want to retur $builder = new Builder(); - $wp_query = $builder->inPostIDs($ids)->getWPQuery(); + $wp_query = $builder->inPostIDs($ids) + ->getWPQuery(); diff --git a/src/Builder.php b/src/Builder.php index 7611656..50583a2 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -142,6 +142,7 @@ public function setOrderBy($order_by) /** * @param $order_by + * @param string $direction * * @return $this */ @@ -163,10 +164,12 @@ public function addOrderBy($order_by, $direction = "DESC") /** - * @param $meta_key - * @param bool $numeric + * @param $meta_key + * @param string $direction + * @param bool $numeric * * @return $this + * @throws \Exception */ public function setOrderByMeta($meta_key, $direction = "DESC", $numeric = false) { @@ -374,6 +377,7 @@ public function removePostType($type) * @param MetaQueryCollection $collection * * @return $this + * @throws \Wenprise\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException */ public function addMetaQueryCollection(MetaQueryCollection $collection) { @@ -391,6 +395,7 @@ public function addMetaQueryCollection(MetaQueryCollection $collection) * @param MetaQuery $metaQuery * * @return $this + * @throws \Wenprise\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException */ public function addMetaQuery(MetaQuery $metaQuery) { @@ -407,6 +412,7 @@ public function addMetaQuery(MetaQuery $metaQuery) * @param TaxonomyQueryCollection $collection * * @return $this + * @throws \Wenprise\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException */ public function addTaxonomyQueryCollection(TaxonomyQueryCollection $collection) { @@ -424,6 +430,7 @@ public function addTaxonomyQueryCollection(TaxonomyQueryCollection $collection) * @param TaxonomyQuery $metaQuery * * @return $this + * @throws \Wenprise\WPQueryBuilder\Exception\MainTaxonomyQueryAlreadyCreatedException */ public function addTaxonomyQuery(TaxonomyQuery $metaQuery) {