LLMS_Notifications_Query

Query LifterLMS Students for a given course / membership


Source Source

File: includes/notifications/class.llms.notifications.query.php

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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
* @since 3.14.0 Unknown.
 */
class LLMS_Notifications_Query extends LLMS_Database_Query {
 
    /**
     * Identify the extending query
     *
     * @var  string
     */
    protected $id = 'notifications';
 
    /**
     * Get an array of allowed notification statuses.
     *
     * @since 3.8.0
     * @since 7.1.0 Added 'error' among the available statuses.
     *
     * @return string[]
     */
    private function get_available_statuses() {
        return array( 'new', 'sent', 'read', 'unread', 'deleted', 'failed', 'error' );
    }
 
    /**
     * Get the available notification types
     *
     * @return   string[]
     * @since    3.8.0
     * @version  3.8.0
     */
    private function get_available_types() {
        return array( 'basic', 'email' );
    }
 
    /**
     * Retrieve default arguments for a student query.
     *
     * @since 3.8.0
     * @since 3.11.0 Unknown.
     * @since 7.1.0 Explicitly exclude 'error' status.
     *               Drop usage of `this->get_filter( 'default_args' )` in favor of `'llms_notification_query_default_args'`.
     * @return array
     */
    protected function get_default_args() {
 
        $args = array(
            'post_id'    => null,
            'subscriber' => null,
            'sort'       => array(
                'updated' => 'DESC',
                'id'      => 'DESC',
            ),
            'statuses'   => array_values( array_diff( $this->get_available_statuses(), array( 'error' ) ) ),
            'triggers'   => array(),
            'types'      => array(),
            'user_id'    => null,
        );
 
        $args = wp_parse_args( $args, parent::get_default_args() );
 
        /**
         * Filters the notifications query's default args.
         *
         * @since 3.8.0
         * @since 7.1.0 Added `$notifications_query` parameter.
         *
         * @param array                    $args                Array of default arguments to set up the query with.
         * @param LLMS_Notifications_Query $notifications_query Instance of `LLMS_Notifications_Query`.
         */
        return apply_filters( 'llms_notifications_query_default_args', $args, $this );
 
    }
 
    /**
     * Convert raw results to notification objects.
     *
     * @since 3.8.0
     * @since 7.1.0 When loading a notification, if errored, exclude it when not explictly requested.
     *              Drop usage of `this->get_filter( 'default_args' )` in favor of `llms_notifications_query_get_notifications`.
     *
     * @return LLMS_Notification[]
     */
    public function get_notifications() {
 
        $notifications = array();
        $results       = $this->get_results();
 
        if ( $results ) {
 
            foreach ( $results as $result ) {
                $notification = ( new LLMS_Notification( $result->id ) )->load();
 
                // If the notification status is 'error' and errored notifications were not requested, skip it.
                if ( 'error' === $notification->get( 'status' ) && ! in_array( 'error', $this->arguments['statuses'], true ) ) {
                    continue;
                }
 
                $notifications[] = $notification;
 
            }
        }
 
        if ( $this->get( 'suppress_filters' ) ) {
            return $notifications;
        }
 
        /**
         * Filters the list of notifications.
         *
         * @since 3.8.0
         *
         * @param array                    $notifications       Array of {@see LLMS_Notification} instances.
         * @param LLMS_Notifications_Query $notifications_query Instance of `LLMS_Notifications_Query`.
         */
        return apply_filters( 'llms_notifications_query_get_notifications', $notifications, $this );
 
    }
 
    /**
     * Parse arguments needed for the query
     *
     * @return   void
     * @since    3.8.0
     * @version  3.8.0
     */
    protected function parse_args() {
 
        $this->parse_statuses();
        $this->parse_types();
 
    }
 
    /**
     * Parse submitted statuses
     *
     * @return   void
     * @since    3.8.0
     * @version  3.8.0
     */
    private function parse_statuses() {
 
        $statuses = $this->arguments['statuses'];
 
        // allow strings to be submitted when only requesting one status
        if ( is_string( $statuses ) ) {
            $statuses = array( $statuses );
        }
 
        // ensure only valid statuses are used
        $statuses = array_intersect( $statuses, $this->get_available_statuses() );
 
        $this->arguments['statuses'] = $statuses;
 
    }
 
    /**
     * Parse submitted types
     *
     * @return   void
     * @since    3.8.0
     * @version  3.8.0
     */
    private function parse_types() {
 
        $types = $this->arguments['types'];
 
        // allow strings to be submitted when only requesting one status
        if ( is_string( $types ) ) {
            $types = array( $types );
        }
 
        // ensure only valid types are used
        $types                    = array_intersect( $types, $this->get_available_types() );
        $this->arguments['types'] = $types;
 
    }
 
    /**
     * Parse submitted triggers
     *
     * @return   void
     * @since    3.11.0
     * @version  3.11.0
     */
    private function parse_triggers() {
 
        $triggers = $this->arguments['triggers'];
 
        // allow strings to be submitted when only requesting one status
        if ( is_string( $triggers ) ) {
            $triggers = array( $triggers );
        }
 
        $this->arguments['triggers'] = $triggers;
 
    }
 
    /**
     * Prepare the SQL for the query.
     *
     * @since 3.8.0
     * @since 3.9.4 Unknown.
     * @since 6.0.0 Renamed from `preprare_query()`.
     * @since 7.1.0 Use `$this->sql_select_columns({columns})` to determine the columns to select.
     *
     * @return string
     */
    protected function prepare_query() {
 
        global $wpdb;
 
        $vars = array(
            $this->get_skip(),
            $this->get( 'per_page' ),
        );
 
        // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- SQL is prepared in other functions.
        $sql = $wpdb->prepare(
            "SELECT {$this->sql_select_columns()}
            FROM {$wpdb->prefix}lifterlms_notifications AS n
            LEFT JOIN {$wpdb->posts} AS p on p.ID = n.post_id
            {$this->sql_where()}
            {$this->sql_orderby()}
            LIMIT %d, %d
            ;",
            $vars
        );
        // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
 
        return $sql;
 
    }
 
    /**
     * Retrieve the prepared SQL for the ORDER clause.
     *
     * Slightly modified from abstract to include the table name to prevent ambiguous errors.
     *
     * @since 3.9.2
     * @since 7.1.0 Drop usage of `$this->get_filter('where')` in favor of `llms_notifications_query_where`.
     *
     * @return string
     */
    protected function sql_orderby() {
 
        $sql = 'ORDER BY';
 
        $comma = false;
 
        foreach ( $this->get( 'sort' ) as $orderby => $order ) {
            $pre   = ( $comma ) ? ', ' : ' ';
            $sql  .= $pre . "n.{$orderby} {$order}";
            $comma = true;
        }
 
        if ( $this->get( 'suppress_filters' ) ) {
            return $sql;
        }
 
        /**
         * Filters the query WHERE clause.
         *
         * @since 7.1.0
         *
         * @param string                   $sql                 The WHERE clause of the query.
         * @param LLMS_Notifications_Query $notifications_query Instance of LLMS_Events_Query.
         */
        return apply_filters( 'llms_notifications_query_where', $sql, $this );
 
    }
 
    /**
     * Retrieve the prepared SQL for the WHERE clause
     *
     * @return   string
     * @since    3.8.0
     * @version  3.14.0
     */
    private function sql_where() {
 
        global $wpdb;


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
3.8.0
3.14.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.