| @@ -1,115 +1,115 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | - | |
| 4 | -namespace FL\Assistant\Data\Repository; | |
| 5 | - | |
| 6 | - | |
| 7 | -use FL\Assistant\Data\Pager; | |
| 8 | -use FL\Assistant\System\Contracts\RepositoryAbstract; | |
| 9 | - | |
| 10 | -/** | |
| 11 | - * Class TermsRepository | |
| 12 | - * @package FL\Assistant\Data\Repository | |
| 13 | - */ | |
| 14 | -class TermsRepository extends RepositoryAbstract { | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * @param $id | |
| 18 | - * | |
| 19 | - * @return array|\WP_Error|\WP_Term|null | |
| 20 | - */ | |
| 21 | - public function find( $id, ?callable $transform = null ) { | |
| 22 | - $term = get_term( $id ); | |
| 23 | - if ( ! is_null( $transform ) ) { | |
| 24 | - $term = call_user_func( $transform, $term ); | |
| 25 | - } | |
| 26 | - | |
| 27 | - return $term; | |
| 28 | - } | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * @param array $args | |
| 32 | - * @param callable|null $transform | |
| 33 | - * | |
| 34 | - * @return array | |
| 35 | - */ | |
| 36 | - public function find_where( array $args = [], ?callable $transform = null ) { | |
| 37 | - $terms = $this->query( $args )->get_terms(); | |
| 38 | - if ( ! is_null( $transform ) ) { | |
| 39 | - $terms = array_map( $transform, $terms ); | |
| 40 | - } | |
| 41 | - return $terms; | |
| 42 | - } | |
| 43 | - | |
| 44 | - /** | |
| 45 | - * @param array $args | |
| 46 | - * @param callable|null $transform | |
| 47 | - * | |
| 48 | - * @return Pager | |
| 49 | - */ | |
| 50 | - public function paginate( array $args = [], ?callable $transform = null ) { | |
| 51 | - $query = $this->query( $args ); | |
| 52 | - $total_terms = $this->terms_query_count( $query ); | |
| 53 | - | |
| 54 | - $pager = new Pager( | |
| 55 | - $query->get_terms(), | |
| 56 | - $total_terms, | |
| 57 | - $query->query_vars['number'], | |
| 58 | - $query->query_vars['offset'] | |
| 59 | - ); | |
| 60 | - | |
| 61 | - if ( ! is_null( $transform ) ) { | |
| 62 | - $pager->apply_transform( $transform ); | |
| 63 | - } | |
| 64 | - | |
| 65 | - return $pager; | |
| 66 | - } | |
| 67 | - | |
| 68 | - /** | |
| 69 | - * Returns an array of child terms for the given term. | |
| 70 | - * A $children array must be passed to search for children. | |
| 71 | - * @param $term | |
| 72 | - * @param $children | |
| 73 | - * @param callable $transformer | |
| 74 | - * @return array | |
| 75 | - */ | |
| 76 | - public function get_child_terms( $term, $children, ?callable $transformer = null ) { | |
| 77 | - if ( isset( $children[ $term->term_id ] ) ) { | |
| 78 | - $term_children = $children[ $term->term_id ]; | |
| 79 | - | |
| 80 | - foreach ( $term_children as $i => $child ) { | |
| 81 | - $term_children[ $i ]->children = $this->get_child_terms( $child, $children, $transformer ); | |
| 82 | - } | |
| 83 | - | |
| 84 | - if ( ! is_null( $transformer ) ) { | |
| 85 | - $term_children = array_map( $transformer, $term_children ); | |
| 86 | - } | |
| 87 | - | |
| 88 | - return $term_children; | |
| 89 | - } | |
| 90 | - | |
| 91 | - return []; | |
| 92 | - } | |
| 93 | - | |
| 94 | - /** | |
| 95 | - * @param array $args | |
| 96 | - * | |
| 97 | - * @return \WP_Term_Query | |
| 98 | - */ | |
| 99 | - public function query( array $args = [] ) { | |
| 100 | - return new \WP_Term_Query( $args ); | |
| 101 | - } | |
| 102 | - | |
| 103 | - /** | |
| 104 | - * @param \WP_Term_Query $query | |
| 105 | - * | |
| 106 | - * @return array|int|\WP_Error | |
| 107 | - */ | |
| 108 | - protected function terms_query_count( \WP_Term_Query $query ) { | |
| 109 | - $taxonomy = $query->query_vars['taxonomy']; | |
| 110 | - $args = $query->query_vars; | |
| 111 | - | |
| 112 | - return \wp_count_terms( $taxonomy, $args ); | |
| 113 | - } | |
| 114 | - | |
| 115 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | + | |
| 4 | +namespace FL\Assistant\Data\Repository; | |
| 5 | + | |
| 6 | + | |
| 7 | +use FL\Assistant\Data\Pager; | |
| 8 | +use FL\Assistant\System\Contracts\RepositoryAbstract; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Class TermsRepository | |
| 12 | + * @package FL\Assistant\Data\Repository | |
| 13 | + */ | |
| 14 | +class TermsRepository extends RepositoryAbstract { | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * @param $id | |
| 18 | + * | |
| 19 | + * @return array|\WP_Error|\WP_Term|null | |
| 20 | + */ | |
| 21 | + public function find( $id, callable $transform = null ) { | |
| 22 | + $term = get_term( $id ); | |
| 23 | + if ( ! is_null( $transform ) ) { | |
| 24 | + $term = call_user_func( $transform, $term ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + return $term; | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @param array $args | |
| 32 | + * @param callable|null $transform | |
| 33 | + * | |
| 34 | + * @return array | |
| 35 | + */ | |
| 36 | + public function find_where( array $args = [], callable $transform = null ) { | |
| 37 | + $terms = $this->query( $args )->get_terms(); | |
| 38 | + if ( ! is_null( $transform ) ) { | |
| 39 | + $terms = array_map( $transform, $terms ); | |
| 40 | + } | |
| 41 | + return $terms; | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * @param array $args | |
| 46 | + * @param callable|null $transform | |
| 47 | + * | |
| 48 | + * @return Pager | |
| 49 | + */ | |
| 50 | + public function paginate( array $args = [], callable $transform = null ) { | |
| 51 | + $query = $this->query( $args ); | |
| 52 | + $total_terms = $this->terms_query_count( $query ); | |
| 53 | + | |
| 54 | + $pager = new Pager( | |
| 55 | + $query->get_terms(), | |
| 56 | + $total_terms, | |
| 57 | + $query->query_vars['number'], | |
| 58 | + $query->query_vars['offset'] | |
| 59 | + ); | |
| 60 | + | |
| 61 | + if ( ! is_null( $transform ) ) { | |
| 62 | + $pager->apply_transform( $transform ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + return $pager; | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Returns an array of child terms for the given term. | |
| 70 | + * A $children array must be passed to search for children. | |
| 71 | + * @param $term | |
| 72 | + * @param $children | |
| 73 | + * @param callable $transformer | |
| 74 | + * @return array | |
| 75 | + */ | |
| 76 | + public function get_child_terms( $term, $children, callable $transformer = null ) { | |
| 77 | + if ( isset( $children[ $term->term_id ] ) ) { | |
| 78 | + $term_children = $children[ $term->term_id ]; | |
| 79 | + | |
| 80 | + foreach ( $term_children as $i => $child ) { | |
| 81 | + $term_children[ $i ]->children = $this->get_child_terms( $child, $children, $transformer ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + if ( ! is_null( $transformer ) ) { | |
| 85 | + $term_children = array_map( $transformer, $term_children ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + return $term_children; | |
| 89 | + } | |
| 90 | + | |
| 91 | + return []; | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * @param array $args | |
| 96 | + * | |
| 97 | + * @return \WP_Term_Query | |
| 98 | + */ | |
| 99 | + public function query( array $args = [] ) { | |
| 100 | + return new \WP_Term_Query( $args ); | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * @param \WP_Term_Query $query | |
| 105 | + * | |
| 106 | + * @return array|int|\WP_Error | |
| 107 | + */ | |
| 108 | + protected function terms_query_count( \WP_Term_Query $query ) { | |
| 109 | + $taxonomy = $query->query_vars['taxonomy']; | |
| 110 | + $args = $query->query_vars; | |
| 111 | + | |
| 112 | + return \wp_count_terms( $taxonomy, $args ); | |
| 113 | + } | |
| 114 | + | |
| 115 | +} | |