| 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 CommentsRepository |
| 12 |
* @package FL\Assistant\Data\Repository |
| 13 |
*/ |
| 14 |
class CommentsRepository extends RepositoryAbstract { |
| 15 |
|
| 16 |
/** |
| 17 |
* @param $id |
| 18 |
* @param callable|null $transform |
| 19 |
* |
| 20 |
* @return array|mixed|\WP_Comment|null |
| 21 |
*/ |
| 22 |
public function find( $id, ?callable $transform = null ) { |
| 23 |
$comment = get_comment( $id ); |
| 24 |
if ( ! is_null( $transform ) ) { |
| 25 |
$comment = call_user_func( $transform, $comment ); |
| 26 |
} |
| 27 |
|
| 28 |
return $comment; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param array $args |
| 33 |
* @param callable|null $transform |
| 34 |
* |
| 35 |
* @return array|int |
| 36 |
*/ |
| 37 |
public function find_where( array $args = [], ?callable $transform = null ) { |
| 38 |
$comments = $this->query( $args )->get_comments(); |
| 39 |
if ( ! is_null( $transform ) ) { |
| 40 |
$comments = array_map( $transform, $comments ); |
| 41 |
} |
| 42 |
return $comments; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @param array $args |
| 47 |
* @param callable|null $transform |
| 48 |
* |
| 49 |
* @return Pager |
| 50 |
*/ |
| 51 |
public function paginate( array $args = [], ?callable $transform = null ) { |
| 52 |
$args['no_found_rows'] = false; // Make sure to calc the number of found rows. |
| 53 |
$query = $this->query( $args ); |
| 54 |
|
| 55 |
$pager = new Pager( |
| 56 |
$query->get_comments(), |
| 57 |
$query->found_comments, |
| 58 |
$query->query_vars['number'], |
| 59 |
$query->query_vars['offset'] |
| 60 |
); |
| 61 |
|
| 62 |
if ( ! is_null( $transform ) ) { |
| 63 |
$pager->apply_transform( $transform ); |
| 64 |
} |
| 65 |
|
| 66 |
return $pager; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @param array $args |
| 71 |
* |
| 72 |
* @return \WP_Comment_Query |
| 73 |
*/ |
| 74 |
public function query( array $args = [] ) { |
| 75 |
return new \WP_Comment_Query( $args ); |
| 76 |
} |
| 77 |
} |
| 78 |
|