LLMS_Events_Query
LLMS_Events_Query class
Source Source
File: includes/class-llms-events-query.php
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | class LLMS_Events_Query extends LLMS_Database_Query { /** * Identify the Query * * @var string */ protected $id = 'events' ; /** * Retrieve default arguments for a query * * @since 3.36.0 * @since 4.7.0 Drop usage of `this->get_filter( 'default_args' )` in favor of `'llms_events_query_default_args'`. * * @return array */ protected function get_default_args() { $args = array ( 'actor' => array (), 'actor_not_in' => array (), 'date_after' => '' , 'date_before' => '' , 'exclude' => array (), 'include' => array (), 'object_type' => '' , 'object' => array (), 'object_not_in' => array (), 'event_type' => '' , 'event_action' => '' , 'sort' => array ( 'date' => 'DESC' , ), ); $args = wp_parse_args( $args , parent::get_default_args() ); if ( $args [ 'suppress_filters' ] ) { return $args ; } /** * Filters the events query default args * * @since 3.36.0 * * @param array $args Array of default arguments to set up the query with. * @param LLMS_Events_Query $events_query Instance of LLMS_Events_Query. */ return apply_filters( 'llms_events_query_default_args' , $args , $this ); } /** * Retrieve an array of LLMS_Event objects for the given result set returned by the query * * @since 3.36.0 * @since 4.7.0 Drop usage of `$this->get_filter('get_events')` in favor of `'llms_events_query_get_events'`. * * @return array */ public function get_events() { $events = array (); $results = $this ->get_results(); if ( $results ) { foreach ( $results as $result ) { $events [] = new LLMS_Event( $result ->id, true ); } } if ( $this ->get( 'suppress_filters' ) ) { return $events ; } /** * Filters the list of events * * @since 3.36.0 * * @param LLMS_Event[] $events Array of LLMS_Event instances. * @param LLMS_Events_Query $events_query Instance of LLMS_Events_Query. */ return apply_filters( 'llms_events_query_get_events' , $events , $this ); } /** * Parses argument data * * @since 3.36.0 * * @return void */ protected function parse_args() { // Sanitize post & user ids. foreach ( array ( 'actor' , 'actor_not_in' , 'object' , 'object_not_in' , 'include' , 'exclude' ) as $key ) { $this ->arguments[ $key ] = $this ->sanitize_id_array( $this ->arguments[ $key ] ); } foreach ( array ( 'date_before' , 'date_after' ) as $key ) { if ( ! empty ( $this ->arguments[ $key ] ) ) { $date = $this ->arguments[ $key ]; if ( ! is_numeric ( $date ) ) { $date = strtotime ( $date ); } $this ->arguments[ $key ] = date ( 'Y-m-d H:i:s' , $date ); } } } /** * Prepare the SQL for the query. * * @since 3.36.0 * @since 4.7.0 Use `$this->sql_select_columns({columns})` to determine the columns to select. * @since 6.0.0 Renamed from `preprare_query()`. * * @return string */ protected function prepare_query() { global $wpdb ; return "SELECT { $this ->sql_select_columns( 'id' )} FROM { $wpdb ->prefix}lifterlms_events { $this ->sql_where()} { $this ->sql_orderby()} { $this ->sql_limit()};"; } /** * SQL "where" clause for the query * * @since 3.36.0 * @since 4.7.0 Drop usage of `$this->get_filter('where')` in favor of `'llms_events_query_where'`. * * @return string */ protected function sql_where() { global $wpdb ; $sql = 'WHERE 1' ; // "IN" clauses for id fields. $ids_include = array ( 'actor' => 'actor_id' , 'object' => 'object_id' , 'include' => 'id' , ); foreach ( $ids_include as $query_key => $db_key ) { $ids = $this ->get( $query_key ); if ( $ids ) { $prepared = implode( ',' , $ids ); $sql .= " AND {$db_key} IN ({$prepared})" ; } } // "NOT IN" clauses for id fields. $ids_exclude = array ( 'actor_not_in' => 'actor_id' , 'object_not_in' => 'object_id' , 'exclude' => 'id' , ); foreach ( $ids_exclude as $query_key => $db_key ) { $ids = $this ->get( $query_key ); if ( $ids ) { $prepared = implode( ',' , $ids ); $sql .= " AND {$db_key} NOT IN ({$prepared})" ; } } // Matching fields. $matching = array ( 'object_type' , 'event_type' , 'event_action' ); foreach ( $matching as $key ) { $val = $this ->get( $key ); if ( $val ) { $sql .= sprintf( " AND {$key} = '%s'" , esc_sql( $val ) ); } } // Date fields. $before = $this ->get( 'date_before' ); if ( $before ) { $sql .= $wpdb ->prepare( ' AND date < %s' , $before ); } $after = $this ->get( 'date_after' ); if ( $after ) { $sql .= $wpdb ->prepare( ' AND date > %s' , $after ); } if ( $this ->get( 'suppress_filters' ) ) { return $sql ; } /** * Filters the query WHERE clause * * @since 3.36.0 * * @param string $sql The WHERE clause of the query. * @param LLMS_Events_Query $events_query Instance of LLMS_Events_Query. */ return apply_filters( 'llms_events_query_where' , $sql , $this ); } } |
Expand full source code Collapse full source code View on GitHub
Methods Methods
- get_default_args — Retrieve default arguments for a query
- get_events — Retrieve an array of LLMS_Event objects for the given result set returned by the query
- parse_args — Parses argument data
- prepare_query — Prepare the SQL for the query.
- preprare_query — Prepare the SQL for the query.
- sql_where — SQL "where" clause for the query
Changelog Changelog
Version | Description |
---|---|
3.36.0 | Introduced. |