PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3
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-items.php

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

362 lines 9.0 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_Items.
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes
7 * @version 3.0.0
8 */
9
10 /**
11 * Prevent loading this file directly
12 */
13 defined( 'ABSPATH' ) || exit();
14
15 if ( ! class_exists( 'LP_Modal_Search_Items' ) ) {
16
17 /**
18 * Class LP_Modal_Search_Items
19 */
20 class LP_Modal_Search_Items {
21
22 /**
23 * @var array
24 */
25 protected $_options = array();
26
27 /**
28 * @var array
29 */
30 protected $_query_args = array();
31
32 /**
33 * @var array
34 */
35 protected $_items = array();
36
37 /**
38 * @var bool
39 */
40 protected $_changed = true;
41
42 /**
43 * LP_Modal_Search_Items constructor.
44 *
45 * @param string $options
46 */
47 public function __construct( $options = '' ) {
48 add_action( 'admin_print_footer_scripts', array( $this, 'js_template' ) );
49
50 $this->_options = apply_filters(
51 'learn-press/modal-search-items-args',
52 wp_parse_args(
53 $options,
54 array(
55 'type' => '',
56 'context' => '',
57 'context_id' => '',
58 'exclude' => '',
59 'term' => '',
60 'add_button' => __( 'Add', 'learnpress' ),
61 'close_button' => __( 'Close', 'learnpress' ),
62 'title' => __( 'Search items', 'learnpress' ),
63 'limit' => 10,
64 'paged' => 1,
65 )
66 )
67 );
68
69 if ( is_string( $this->_options['exclude'] ) ) {
70 $this->_options['exclude'] = explode( ',', $this->_options['exclude'] );
71 }
72
73 add_filter( 'learn-press/modal-search-items/exclude', array( $this, 'exclude_items' ), 10, 4 );
74 add_filter( 'learn-press/modal-search-items/args', array( $this, 'query_args' ), 10, 3 );
75 }
76
77 /**
78 * Build query args from object options and get posts.
79 *
80 * @return array
81 */
82 protected function _get_items(): array {
83 $user_id = get_current_user_id();
84
85 $term = $this->_options['term'];
86 $type = $this->_options['type'];
87 $context = $this->_options['context'];
88 $context_id = $this->_options['context_id'];
89
90 $exclude = array_unique( (array) apply_filters( 'learn-press/modal-search-items/exclude', $this->_options['exclude'], $type, $context, $context_id ) );
91
92 if ( is_array( $exclude ) ) {
93 $exclude = array_map( 'intval', $exclude );
94 }
95
96 $paged = max( 1, $this->_options['paged'] );
97
98 $args = array(
99 'post_type' => array( $type ),
100 'post_status' => 'publish',
101 'order' => 'ASC',
102 'orderby' => 'parent title',
103 'exclude' => $exclude,
104 'posts_per_page' => $this->_options['limit'],
105 'offset' => ( $paged - 1 ) * $this->_options['limit'],
106 );
107
108 $context_id = apply_filters( 'learn-press/modal-search-items/context-id', $context_id, $context );
109 if ( $context_id ) {
110 // Admin can get all items
111 if ( ! user_can( $user_id, 'administrator' ) ) {
112 $args['author'] = get_post_field( 'post_author', $context_id );
113 }
114 }
115
116 if ( $term ) {
117 $args['s'] = $term;
118 }
119
120 $this->_query_args = apply_filters( 'learn-press/modal-search-items/args', $args, $context, $context_id );
121
122 $posts = get_posts( $this->_query_args );
123
124 if ( $posts ) {
125 $this->_items = wp_list_pluck( $posts, 'ID' );
126 }
127
128 return $this->_items;
129 }
130
131 /**
132 * Get the items
133 *
134 * @return array
135 */
136 public function get_items() {
137 if ( $this->_changed ) {
138 $this->_get_items();
139 }
140
141 return $this->_items;
142 }
143
144 /**
145 * Get pagination in html.
146 *
147 * @param bool $html
148 *
149 * @return array|string
150 */
151 function get_pagination( $html = true ) {
152 $pagination = '';
153 $items = $this->get_items();
154
155 if ( $items ) {
156 $args = $this->_query_args;
157
158 if ( ! empty( $args['exclude'] ) ) {
159 $args['post__not_in'] = $args['exclude'];
160 }
161
162 $q = new WP_Query( $args );
163
164 if ( $this->_options['paged'] && $q->max_num_pages > 1 ) {
165 $pagenum_link = html_entity_decode( get_pagenum_link() );
166
167 $query_args = array();
168 $url_parts = explode( '?', $pagenum_link );
169
170 if ( isset( $url_parts[1] ) ) {
171 wp_parse_str( $url_parts[1], $query_args );
172 }
173
174 $pagenum_link = esc_url_raw( remove_query_arg( array_keys( $query_args ), $pagenum_link ) );
175 $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
176
177 $pagination = array(
178 'base' => $pagenum_link,
179 'total' => $q->max_num_pages,
180 'current' => max( 1, $this->_options['paged'] ),
181 'mid_size' => 1,
182 'add_args' => array_map( 'urlencode', $query_args ),
183 'prev_text' => __( '<', 'learnpress' ),
184 'next_text' => __( '>', 'learnpress' ),
185 'type' => '',
186 );
187
188 if ( $html ) {
189 $pagination = paginate_links( $pagination );
190 }
191 }
192 $this->_changed = false;
193 }
194
195 return $pagination;
196 }
197
198 /**
199 * Return string of list items
200 *
201 * @return string
202 */
203 public function get_html_items() {
204 ob_start();
205 $items = $this->get_items();
206
207 if ( $items ) {
208 foreach ( $items as $id => $item ) {
209 $type = get_post_type( $item );
210 $type_object = get_post_type_object( $type );
211 $type_name = $type_object ? $type_object->labels->singular_name : '';
212 printf(
213 '
214 <li class="%s" data-id="%2$d" data-type="%4$s" data-text="%3$s">
215 <label>
216 <input type="checkbox" value="%2$d" name="selectedItems[]">
217 <span class="lp-item-text">%3$s (%5$s - #%6$s)</span>
218 </label>
219 </li>
220 ',
221 'lp-result-item',
222 $item,
223 esc_attr( get_the_title( $item ) ),
224 $type,
225 $type_name,
226 $item
227 );
228 }
229 } else {
230
231 // @since 3.0.0
232 $item_not_found = apply_filters( 'learn-press/modal-search-items/not-found', __( 'No item found', 'learnpress' ), $this->_options['type'] );
233
234 echo '<li>' . $item_not_found . '</li>';
235 }
236
237 return ob_get_clean();
238 }
239
240 /**
241 * JS Modal template.
242 */
243 public function js_template() {
244 $view = learn_press_get_admin_view( 'modal-search-items' );
245 include $view;
246 }
247
248 /**
249 * @param array $args
250 * @param string $context
251 * @param string $context_id
252 *
253 * @return mixed
254 */
255 public static function query_args( $args, $context, $context_id ) {
256 if ( ( LP_ORDER_CPT === get_post_type( $context_id ) ) && ( LP_COURSE_CPT === $args['post_type'] ) ) {
257 if ( ! empty( $args['author'] ) ) {
258 unset( $args['author'] );
259 }
260 }
261
262 return $args;
263 }
264
265 /**
266 * Filter to exclude the items has already added to it's parent.
267 * Each item only use one time
268 *
269 * @param $exclude
270 * @param $type
271 * @param string $context
272 * @param null $context_id
273 *
274 * @return array
275 */
276 public static function exclude_items( $exclude, $type, $context = '', $context_id = null ) {
277 global $wpdb;
278
279 $used_items = array();
280
281 switch ( $type ) {
282 case 'lp_lesson':
283 case 'lp_quiz':
284 $query = $wpdb->prepare(
285 "
286 SELECT item_id
287 FROM {$wpdb->prefix}learnpress_section_items si
288 INNER JOIN {$wpdb->prefix}learnpress_sections s ON s.section_id = si.section_id
289 INNER JOIN {$wpdb->posts} p ON p.ID = s.section_course_id
290 WHERE %d
291 AND p.post_type = %s
292 ",
293 1,
294 LP_COURSE_CPT
295 );
296 $used_items = $wpdb->get_col( $query );
297 break;
298 case 'lp_question':
299 $query = $wpdb->prepare(
300 "
301 SELECT question_id
302 FROM {$wpdb->prefix}learnpress_quiz_questions AS qq
303 INNER JOIN {$wpdb->posts} q ON q.ID = qq.quiz_id
304 WHERE %d
305 AND q.post_type = %s
306 ",
307 1,
308 LP_QUIZ_CPT
309 );
310 $used_items = $wpdb->get_col( $query );
311 break;
312
313 }
314
315 if ( $used_items && $exclude ) {
316 $exclude = array_merge( $exclude, $used_items );
317 } elseif ( $used_items ) {
318 $exclude = $used_items;
319 }
320
321 return is_array( $exclude ) ? array_unique( $exclude ) : array();
322 }
323
324 /**
325 * @param $message
326 * @param $type
327 *
328 * @return string
329 */
330 public static function items_not_found( $message, $type ) {
331 switch ( $type ) {
332 case LP_LESSON_CPT:
333 $message = __( 'There are no available lessons for this course, please use ', 'learnpress' );
334 $message .= '<a target="_blank" href="' . admin_url( 'post-new.php?post_type=lp_lesson' ) . '">' . esc_html__( 'Add new item', 'learnpress' ) . '</a>';
335 break;
336 case LP_QUIZ_CPT:
337 $message = __( 'There are no available quizzes for this course, please use ', 'learnpress' );
338 $message .= '<a target="_blank" href="' . admin_url( 'post-new.php?post_type=lp_quiz' ) . '">' . esc_html__( 'Add new item', 'learnpress' ) . '</a>';
339 break;
340 case LP_QUESTION_CPT:
341 $message = __( 'There are no available questions for this quiz, please use ', 'learnpress' );
342 $message .= '<a target="_blank" href="' . admin_url( 'post-new.php?post_type=lp_question' ) . '">' . esc_html__( 'Add new item', 'learnpress' ) . '</a>';
343 break;
344 }
345
346 return $message;
347 }
348
349 /**
350 * @return bool|LP_Modal_Search_Items
351 */
352 public static function instance() {
353 static $instance = false;
354 if ( ! $instance ) {
355 $instance = new self();
356 }
357
358 return $instance;
359 }
360 }
361 }
362