PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.2
4.4.8 4.4.7 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 All 139 releases
learnpress / inc / admin / class-lp-modal-search-users.php

class-lp-modal-search-users.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.2, at inc/admin/class-lp-modal-search-users.php

221 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Modal_Search_Users
4 */
5 class LP_Modal_Search_Users {
6
7 /**
8 * @var array
9 */
10 protected $_options = array();
11
12 /**
13 * @var array
14 */
15 protected $_query_args = array();
16
17 /**
18 * @var WP_User_Query
19 */
20 protected $_query = null;
21
22 /**
23 * @var array
24 */
25 protected $_items = array();
26
27 /**
28 * @var bool
29 */
30 protected $_changed = true;
31
32 /**
33 * LP_Modal_Search_Users constructor.
34 *
35 * @param string $options
36 */
37 public function __construct( $options = '' ) {
38 add_action( 'admin_print_footer_scripts', array( $this, 'js_template' ) );
39 $this->_options = wp_parse_args(
40 $options,
41 array(
42 'type' => '',
43 'context' => '',
44 'context_id' => '',
45 'exclude' => '',
46 'term' => '',
47 'text_format' => '{{display_name}} ({{email}})',
48 'add_button' => __( 'Add', 'learnpress' ),
49 'close_button' => __( 'Close', 'learnpress' ),
50 'title' => __( 'Search users', 'learnpress' ),
51 'number' => 10,
52 'paged' => 1,
53 )
54 );
55
56 if ( is_string( $this->_options['exclude'] ) ) {
57 $this->_options['exclude'] = explode( ',', $this->_options['exclude'] );
58 }
59 }
60
61 protected function _get_items() {
62 $term = $this->_options['term'];
63 $type = $this->_options['type'];
64 $context = $this->_options['context'];
65 $context_id = $this->_options['context_id'];
66
67 $exclude = array_unique( (array) apply_filters( 'learn-press/modal-search-user/exclude', $this->_options['exclude'], $type, $context, $context_id ) );
68
69 if ( ! empty( $exclude ) ) {
70 $exclude = array_map( 'intval', $exclude );
71 }
72
73 $args = array(
74 'number' => $this->_options['number'],
75 'offset' => ( $this->_options['paged'] - 1 ) * $this->_options['number'],
76 'exclude' => $exclude,
77 );
78 if ( $term ) {
79 $args['search'] = sprintf( '*%s*', esc_attr( $term ) );
80 $args['search_columns'] = array( 'user_login', 'user_email' );
81 }
82 $this->_query_args = apply_filters( 'learn-press/modal-search-users/args', $args, $context, $context_id );
83
84 // The Query
85 $this->_query = new WP_User_Query( $args );
86 $this->_items = array();
87
88 $results = $this->_query->get_results();
89 if ( $results ) {
90 foreach ( $results as $user ) {
91 $this->_items[ $user->ID ] = $user->user_login;
92 }
93 }
94
95 return $this->_items;
96 }
97
98 public function get_items() {
99 if ( $this->_changed ) {
100 $this->_get_items();
101 }
102
103 return $this->_items;
104 }
105
106 function get_pagination() {
107
108 $pagination = '';
109 $items = $this->get_items();
110
111 if ( $items && $this->_options['number'] > 0 ) {
112
113 $number = $this->_options['number'];
114 $total = $this->_query->get_total();
115 $max_num_pages = intval( $total / $number );
116
117 if ( $total % $number ) {
118 $max_num_pages ++;
119 }
120
121 if ( $this->_options['paged'] && $max_num_pages > 1 ) {
122 $pagenum_link = html_entity_decode( get_pagenum_link() );
123
124 $query_args = array();
125 $url_parts = explode( '?', $pagenum_link );
126
127 if ( isset( $url_parts[1] ) ) {
128 wp_parse_str( $url_parts[1], $query_args );
129 }
130
131 $pagenum_link = esc_url_raw( remove_query_arg( array_keys( $query_args ), $pagenum_link ) );
132 $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
133
134 $pagination = paginate_links(
135 array(
136 'base' => $pagenum_link,
137 'total' => $max_num_pages,
138 'current' => max( 1, $this->_options['paged'] ),
139 'mid_size' => 1,
140 'add_args' => array_map( 'urlencode', $query_args ),
141 'prev_text' => __( '<', 'learnpress' ),
142 'next_text' => __( '>', 'learnpress' ),
143 'type' => '',
144 )
145 );
146 }
147 }
148
149 return $pagination;
150 }
151
152 public function get_html_items() {
153
154 $variables = array(
155 'id',
156 'email',
157 'user_login',
158 'description',
159 'first_name',
160 'last_name',
161 'nickname',
162 'display_name',
163 );
164
165 ob_start();
166
167 $items = $this->get_items();
168
169 if ( $items ) {
170 foreach ( $items as $id => $item ) {
171 $the_user = learn_press_get_user( $id );
172 $text = str_replace( '{{id}}', $the_user->get_id(), $this->_options['text_format'] );
173 $data = array();
174 foreach ( $variables as $variable ) {
175 $text = str_replace( '{{' . $variable . '}}', $the_user->get_data( $variable ), $text );
176 $data[ $variable ] = $the_user->get_data( $variable );
177 }
178 $data['id'] = $id;
179 printf( '<li class="%s" data-id="%d" data-data="%s"><label>', 'lp-result-item user-' . $id, $id, esc_attr( wp_json_encode( $data ) ) );
180 if ( $this->_options['multiple'] ) {
181 printf(
182 '
183 <input type="checkbox" value="%d" name="selectedItems[]">
184 <span class="lp-item-text">%s</span>
185 ',
186 $id,
187 esc_attr( $text )
188 );
189 } else {
190 printf(
191 '
192 <a href=""><span class="lp-item-text">%s</span></a>
193 ',
194 esc_attr( $text )
195 );
196 }
197
198 echo '</li>';
199 }
200 } else {
201 echo '<li>' . apply_filters( 'learn-press/modal-search-users/not-found', __( 'No item found', 'learnpress' ), $this->_options['type'] ) . '</li>';
202 }
203
204 return ob_get_clean();
205 }
206
207 public function js_template() {
208 $view = learn_press_get_admin_view( 'modal-search-users' );
209 include $view;
210 }
211
212 public static function instance() {
213 static $instance = false;
214 if ( ! $instance ) {
215 $instance = new self();
216 }
217
218 return $instance;
219 }
220 }
221