post-meta-view.php
3 years ago
relation.php
3 years ago
view-base-count-trait.php
3 years ago
view-base.php
3 years ago
view-custom-table-base.php
3 years ago
view-base-count-trait.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Db_Queries\Views; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 7 | |
| 8 | trait View_Base_Count_Trait { |
| 9 | |
| 10 | /** |
| 11 | * @return array[] |
| 12 | */ |
| 13 | public function select_columns(): array { |
| 14 | return array( |
| 15 | array( |
| 16 | 'as' => sprintf( 'COUNT( %s ) as total', $this->column( 'id' ) ), |
| 17 | ), |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | public function limit(): array { |
| 22 | return array( 1 ); |
| 23 | } |
| 24 | |
| 25 | public function prepare_row( $value ) { |
| 26 | return (int) $value; |
| 27 | } |
| 28 | |
| 29 | public function get_count(): int { |
| 30 | try { |
| 31 | return (int) $this->query() |
| 32 | ->query_var(); |
| 33 | |
| 34 | } catch ( Query_Builder_Exception $exception ) { |
| 35 | return 0; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public static function count( $args = array() ) { |
| 40 | $filters = $args['filters'] ?? $args; |
| 41 | |
| 42 | return ( new static() )->set_filters( $filters )->get_count(); |
| 43 | } |
| 44 | } |
| 45 |