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/README.md b/README.md index e149c1b..598925a 100644 --- a/README.md +++ b/README.md @@ -14,22 +14,22 @@ 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->createMainMetaQuery("OR") + $wp_query = $builder->createMetaQuery("OR") ->addMetaQuery(MetaQuery::create('color', 'blue')) ->addMetaQuery(MetaQuery::create('size', 'XL')) ->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->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'); @@ -49,29 +49,29 @@ 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 $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)) + ->addTaxonomyQuery(TaxonomyQuery::create('custom', 'slug', array('sweet'))) ->getWPQuery(); 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,23 +83,25 @@ 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(); -### 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,4 +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/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..50583a2 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -5,39 +5,40 @@ * 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 { - 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,63 @@ 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 + * @param string $direction + * * @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 bool $numeric + * @param $meta_key + * @param string $direction + * @param bool $numeric + * * @return $this + * @throws \Exception */ - 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 +197,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 +217,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 +231,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 +245,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 createMetaQuery($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 +269,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 createTaxonomyQuery($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 +296,7 @@ public function createMainTaxonomyQuery($where_type="AND", TaxonomyQueryCollecti public function setAnyPostType() { $this->post_types = static::POST_TYPE_ANY; + return $this; } @@ -282,30 +306,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 +337,53 @@ 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 + * @throws \Wenprise\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException */ public function addMetaQueryCollection(MetaQueryCollection $collection) { - if(!$this->mainMetaQueryCollection) - $this->createMainMetaQuery(); + if ( ! $this->mainMetaQueryCollection) { + $this->createMetaQuery(); + } $this->mainMetaQueryCollection->addCollection($collection); @@ -366,12 +393,15 @@ public function addMetaQueryCollection(MetaQueryCollection $collection) /** * @param MetaQuery $metaQuery + * * @return $this + * @throws \Wenprise\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException */ public function addMetaQuery(MetaQuery $metaQuery) { - if(!$this->mainMetaQueryCollection) - $this->createMainMetaQuery(); + if ( ! $this->mainMetaQueryCollection) { + $this->createMetaQuery(); + } $this->mainMetaQueryCollection->add($metaQuery); @@ -380,12 +410,15 @@ public function addMetaQuery(MetaQuery $metaQuery) /** * @param TaxonomyQueryCollection $collection + * * @return $this + * @throws \Wenprise\WPQueryBuilder\Exception\MainMetaQueryAlreadyCreatedException */ public function addTaxonomyQueryCollection(TaxonomyQueryCollection $collection) { - if(!$this->mainTaxonomyQueryCollection) - $this->createMainMetaQuery(); + if ( ! $this->mainTaxonomyQueryCollection) { + $this->createMetaQuery(); + } $this->mainTaxonomyQueryCollection->addCollection($collection); @@ -395,12 +428,15 @@ public function addTaxonomyQueryCollection(TaxonomyQueryCollection $collection) /** * @param TaxonomyQuery $metaQuery + * * @return $this + * @throws \Wenprise\WPQueryBuilder\Exception\MainTaxonomyQueryAlreadyCreatedException */ public function addTaxonomyQuery(TaxonomyQuery $metaQuery) { - if(!$this->mainTaxonomyQueryCollection) - $this->createMainTaxonomyQuery(); + if ( ! $this->mainTaxonomyQueryCollection) { + $this->createTaxonomyQuery(); + } $this->mainTaxonomyQueryCollection->add($metaQuery); @@ -432,6 +468,7 @@ public function getWPQuery() public function getPosts() { $wp_query = $this->getWPQuery(); + return $wp_query->get_posts(); } @@ -442,7 +479,7 @@ public function getPostIDs() { $this->hydrateParametersArray(); - $this->parameters["fields"] = "ids"; + $this->parameters[ "fields" ] = "ids"; $wp_query = $this->getWPQuery(); @@ -455,27 +492,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 +521,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 +549,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 +582,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 +595,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 +608,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 +622,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 +649,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/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..bc768ac 100644 --- a/src/MetaQuery.php +++ b/src/MetaQuery.php @@ -5,7 +5,7 @@ * Time: 23:47 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; class MetaQuery @@ -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 cfae8ba..740148b 100644 --- a/src/MetaQueryCollection.php +++ b/src/MetaQueryCollection.php @@ -5,19 +5,19 @@ * Time: 0:06 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; 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 ca1a70c..bdb36a1 100644 --- a/src/TaxonomyQuery.php +++ b/src/TaxonomyQuery.php @@ -5,7 +5,7 @@ * Time: 23:47 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; class TaxonomyQuery @@ -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 808d0e0..858f70f 100644 --- a/src/TaxonomyQueryCollection.php +++ b/src/TaxonomyQueryCollection.php @@ -5,19 +5,19 @@ * Time: 0:06 */ -namespace Simettric\WPQueryBuilder; +namespace Wenprise\WPQueryBuilder; 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 d85ae4f..cbe9763 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -1,11 +1,12 @@ @@ -19,15 +20,15 @@ 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(); $this->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,54 +117,54 @@ 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" ]); } public function testTaxonomyQueryParameter() { $builder = new Builder(); - $builder->createMainTaxonomyQuery(); - $builder->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('blue'))); + $builder->createTaxonomyQuery(); + $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" ]); } }