PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / trunk
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses vtrunk
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 4.2.1.1 All 137 releases
learnpress / inc / Databases / UserDB.php

UserDB.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses trunk, at inc/Databases/UserDB.php

102 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Databases;
4
5 use Exception;
6 use LearnPress\Filters\UserFilter;
7
8 /**
9 * Class UserDB
10 *
11 * Refactor of LP_User_DB
12 *
13 * @since 4.3.6
14 * @version 1.0.0
15 */
16 class UserDB extends DataBase {
17 /**
18 * @var UserDB|null
19 */
20 private static ?UserDB $_instance = null;
21
22 /**
23 * Constructor
24 */
25 protected function __construct() {
26 parent::__construct();
27 }
28
29 /**
30 * Get instance
31 *
32 * @return UserDB
33 */
34 public static function getInstance(): UserDB {
35 if ( is_null( self::$_instance ) ) {
36 self::$_instance = new self();
37 }
38
39 return self::$_instance;
40 }
41
42 /**
43 * Get users
44 *
45 * @param UserFilter $filter
46 * @param int $total_rows
47 *
48 * @return array|object|null|int|string
49 * @throws Exception
50 * @since 4.3.0
51 * @version 1.0.0
52 */
53 public function get_users( UserFilter $filter, int &$total_rows = 0 ) {
54 $filter->fields = array_merge( $filter->all_fields, $filter->fields );
55
56 if ( empty( $filter->collection ) ) {
57 $filter->collection = $this->tb_users;
58 }
59
60 if ( empty( $filter->collection_alias ) ) {
61 $filter->collection_alias = 'u';
62 }
63
64 $ca = $filter->collection_alias;
65
66 // Find ID
67 if ( isset( $filter->ID ) ) {
68 $filter->where[] = $this->wpdb->prepare( "AND $ca.ID = %d", $filter->ID );
69 }
70
71 // Find by IDs
72 if ( ! empty( $filter->ids ) ) {
73 $ids_format = implode( ', ', array_fill( 0, count( $filter->ids ), '%d' ) );
74 $filter->where[] = $this->wpdb->prepare( "AND $ca.ID IN ($ids_format)", $filter->ids );
75 }
76
77 // Find by user_nicename
78 if ( ! empty( $filter->user_nicename ) ) {
79 $filter->where[] = $this->wpdb->prepare( "AND $ca.user_nicename LIKE %s", '%' . $filter->user_nicename . '%' );
80 }
81
82 // Find by user_email
83 if ( ! empty( $filter->user_email ) ) {
84 $filter->where[] = $this->wpdb->prepare( "AND $ca.user_email = %s", $filter->user_email );
85 }
86
87 // Find by user_login
88 if ( ! empty( $filter->user_login ) ) {
89 $filter->where[] = $this->wpdb->prepare( "AND $ca.user_login = %s", $filter->user_login );
90 }
91
92 // Find by display_name
93 if ( ! empty( $filter->display_name ) ) {
94 $filter->where[] = $this->wpdb->prepare( "AND $ca.display_name LIKE %s", '%' . $filter->display_name . '%' );
95 }
96
97 $filter = apply_filters( 'lp/user/query/filter', $filter );
98
99 return $this->execute( $filter, $total_rows );
100 }
101 }
102