| @@ -1,0 +1,70 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\WPEP\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Overrides GamiPress AJAX Helper for selecting posts | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +function gamipress_wpep_ajax_get_posts() { | |
| 18 | + | |
| 19 | + // Security check, forces to die if not security passed | |
| 20 | + check_ajax_referer( 'gamipress_admin', 'nonce' ); | |
| 21 | + | |
| 22 | + // Check if user can manage GamiPress | |
| 23 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) { | |
| 24 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + global $wpdb; | |
| 28 | + | |
| 29 | + if( isset( $_REQUEST['post_type'] ) && in_array( 'wpep_lessons', $_REQUEST['post_type'] ) ) { | |
| 30 | + | |
| 31 | + // Get the user input | |
| 32 | + $search = isset( $_REQUEST['q'] ) ? esc_sql( $wpdb->esc_like( $_REQUEST['q'] ) ) : ''; | |
| 33 | + $table = $wpdb->prefix . ( defined( 'WPEP_DB_TABLE_COURSE_SECTION_LESSON' ) ? WPEP_DB_TABLE_COURSE_SECTION_LESSON : 'wpep_section_lesson' ); | |
| 34 | + | |
| 35 | + if ( ! empty( $search ) ){ | |
| 36 | + $lessons = $wpdb->get_results( $wpdb->prepare( | |
| 37 | + "SELECT * FROM {$table} | |
| 38 | + WHERE ( title LIKE %s OR title LIKE %s ) | |
| 39 | + ORDER BY post_id ASC, order ASC", | |
| 40 | + "%%{$search}%%", | |
| 41 | + "{$search}%%" | |
| 42 | + ) ); | |
| 43 | + } else { | |
| 44 | + $lessons = $wpdb->get_results( "SELECT * FROM {$table} ORDER BY post_id ASC, order ASC" ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + // Build the results array | |
| 48 | + $results = array(); | |
| 49 | + | |
| 50 | + foreach ( $lessons as $lesson ) { | |
| 51 | + | |
| 52 | + $section = WPEP\Entity\Course::instance()->get_section_by_lesson_id( $lesson->id ); | |
| 53 | + $course_title = gamipress_get_post_field( 'post_title', $lesson->post_id ); | |
| 54 | + | |
| 55 | + // Results should meet same structure like posts | |
| 56 | + $results[] = array( | |
| 57 | + 'ID' => $lesson->id, | |
| 58 | + 'post_title' => $lesson->title, | |
| 59 | + 'post_type' => $course_title . ' (' . $section->title . ')', | |
| 60 | + ); | |
| 61 | + | |
| 62 | + } | |
| 63 | + | |
| 64 | + // Return our results | |
| 65 | + wp_send_json_success( $results ); | |
| 66 | + die; | |
| 67 | + } | |
| 68 | + | |
| 69 | +} | |
| 70 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_wpep_ajax_get_posts', 5 ); | |