ListTable.php
1 year ago
Log.php
1 year ago
Record.php
1 year ago
RecordQuery.php
1 year ago
Records.php
1 year ago
Repository.php
1 year ago
RecordQuery.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | // phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 4 | /** @noinspection PhpIllegalPsrClassPathInspection */ |
| 5 | |
| 6 | namespace WPForms\Logger; |
| 7 | |
| 8 | /** |
| 9 | * Class RecordQuery. |
| 10 | * |
| 11 | * @since 1.6.3 |
| 12 | */ |
| 13 | class RecordQuery { |
| 14 | |
| 15 | /** |
| 16 | * Build query. |
| 17 | * |
| 18 | * @since 1.6.3 |
| 19 | * |
| 20 | * @param int $limit Query limit of records. |
| 21 | * @param int $offset Offset of records. |
| 22 | * @param string $search Search. |
| 23 | * @param string $type Type of records. |
| 24 | * |
| 25 | * @return array |
| 26 | */ |
| 27 | public function get( $limit, $offset = 0, $search = '', $type = '' ) { |
| 28 | |
| 29 | global $wpdb; |
| 30 | //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 31 | return (array) $wpdb->get_results( |
| 32 | $this->build_query( $limit, $offset, $search, $type ) |
| 33 | ); |
| 34 | //phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Build query. |
| 39 | * |
| 40 | * @since 1.6.3 |
| 41 | * |
| 42 | * @param int $limit Query limit of records. |
| 43 | * @param int $offset Offset of records. |
| 44 | * @param string $search Search. |
| 45 | * @param string $type Type of records. |
| 46 | * |
| 47 | * @return string |
| 48 | */ |
| 49 | private function build_query( $limit, $offset = 0, $search = '', $type = '' ) { |
| 50 | |
| 51 | global $wpdb; |
| 52 | |
| 53 | $sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM ' . Repository::get_table_name(); |
| 54 | $where = []; |
| 55 | |
| 56 | if ( ! empty( $search ) ) { |
| 57 | $where[] = $wpdb->prepare( |
| 58 | '`title` REGEXP %s OR `message` REGEXP %s', |
| 59 | $search, |
| 60 | $search |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | if ( ! empty( $type ) ) { |
| 65 | $where[] = $wpdb->prepare( |
| 66 | '`types` REGEXP %s', |
| 67 | $type |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | if ( $where ) { |
| 72 | $sql .= ' WHERE ' . implode( ' AND ', $where ); |
| 73 | } |
| 74 | |
| 75 | $sql .= ' ORDER BY `create_at` DESC, `id` DESC'; |
| 76 | $sql .= $wpdb->prepare( ' LIMIT %d, %d', absint( $offset ), absint( $limit ) ); |
| 77 | |
| 78 | return $sql; |
| 79 | } |
| 80 | } |
| 81 |