| @@ -1,147 +1,147 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Class LP_Course_Query | |
| 5 | - * | |
| 6 | - * @version 3.3.0 | |
| 7 | - */ | |
| 8 | -class LP_Course_Query extends LP_Object_Query { | |
| 9 | - | |
| 10 | - protected $course_query_vars = array(); | |
| 11 | - | |
| 12 | - /** | |
| 13 | - * LP_Course_Query constructor. | |
| 14 | - * | |
| 15 | - * @param string $query | |
| 16 | - */ | |
| 17 | - public function __construct( $query = '' ) { | |
| 18 | - $limit = LP_Settings::get_option( 'archive_course_limit', 6 ); | |
| 19 | - if ( empty( $limit ) ) { | |
| 20 | - $limit = 6; | |
| 21 | - } | |
| 22 | - | |
| 23 | - $this->course_query_vars = array( | |
| 24 | - 'post_type' => LP_COURSE_CPT, | |
| 25 | - 'post_status' => array( 'draft', 'pending', 'private', 'publish' ), | |
| 26 | - 'limit' => $limit, | |
| 27 | - 'author' => '', | |
| 28 | - ); | |
| 29 | - | |
| 30 | - parent::__construct( $query ); | |
| 31 | - } | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * @return array | |
| 35 | - */ | |
| 36 | - protected function get_default_query_vars() { | |
| 37 | - return array_merge( | |
| 38 | - parent::get_default_query_vars(), | |
| 39 | - $this->course_query_vars | |
| 40 | - ); | |
| 41 | - } | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * Transform our query vars to wp query vars that | |
| 45 | - * can read from db. | |
| 46 | - * | |
| 47 | - * @param array $query_vars | |
| 48 | - * | |
| 49 | - * @return array|mixed | |
| 50 | - * @since 4.0.0 | |
| 51 | - */ | |
| 52 | - public function get_wp_query_vars( $query_vars = array() ) { | |
| 53 | - $query_vars = apply_filters( 'learn-press/course-object-query-args', wp_parse_args( $query_vars, $this->get_query_vars() ) ); | |
| 54 | - $map_keys = array( | |
| 55 | - 'status' => 'post_status', | |
| 56 | - 'page' => 'paged', | |
| 57 | - 'include' => 'post__in', | |
| 58 | - 'return' => 'fields', | |
| 59 | - 'parent' => 'post_parent', | |
| 60 | - 'parent_exclude' => 'post_parent__not_in', | |
| 61 | - 'exclude' => 'post__not_in', | |
| 62 | - 'limit' => 'posts_per_page', | |
| 63 | - 'type' => 'post_type', | |
| 64 | - ); | |
| 65 | - | |
| 66 | - foreach ( $map_keys as $query_key => $db_key ) { | |
| 67 | - if ( isset( $query_vars[ $query_key ] ) ) { | |
| 68 | - $query_vars[ $db_key ] = $query_vars[ $query_key ]; | |
| 69 | - unset( $query_vars[ $query_key ] ); | |
| 70 | - } | |
| 71 | - } | |
| 72 | - | |
| 73 | - $custom_keys = array( | |
| 74 | - 'featured' => '', | |
| 75 | - ); | |
| 76 | - | |
| 77 | - foreach ( $custom_keys as $key => $custom_key ) { | |
| 78 | - if ( isset( $query_vars[ $key ] ) ) { | |
| 79 | - $custom_keys[ $key ] = $query_vars[ $key ]; | |
| 80 | - unset( $query_vars[ $key ] ); | |
| 81 | - } | |
| 82 | - } | |
| 83 | - | |
| 84 | - // Query by post meta | |
| 85 | - if ( ! isset( $query_vars['meta_query'] ) ) { | |
| 86 | - $query_vars['meta_query'] = array(); | |
| 87 | - } | |
| 88 | - | |
| 89 | - // Featured | |
| 90 | - if ( '' !== $custom_keys['featured'] ) { | |
| 91 | - $featured = $custom_keys['featured']; | |
| 92 | - | |
| 93 | - if ( $featured === 'yes' || $featured === 1 || $featured === true || $featured === '1' ) { | |
| 94 | - $query_vars['meta_query'] = array( | |
| 95 | - array( | |
| 96 | - 'key' => '_lp_featured', | |
| 97 | - 'value' => 'yes', | |
| 98 | - 'compare' => '=', | |
| 99 | - ), | |
| 100 | - ); | |
| 101 | - } else { | |
| 102 | - $query_vars['meta_query'] = array( | |
| 103 | - array( | |
| 104 | - 'key' => '_lp_featured', | |
| 105 | - 'value' => 'yes', | |
| 106 | - 'compare' => '!=', | |
| 107 | - ), | |
| 108 | - ); | |
| 109 | - } | |
| 110 | - } | |
| 111 | - | |
| 112 | - return apply_filters( 'learn-press/course-object-wp-query-args', $query_vars ); | |
| 113 | - } | |
| 114 | - | |
| 115 | - /** | |
| 116 | - * Applies our query vars to read courses. | |
| 117 | - * | |
| 118 | - * @return array|mixed | |
| 119 | - * @since 4.0.0 | |
| 120 | - */ | |
| 121 | - public function get_courses() { | |
| 122 | - global $wpdb; | |
| 123 | - $query_vars = $this->get_wp_query_vars(); | |
| 124 | - | |
| 125 | - $query = new WP_Query( $query_vars ); | |
| 126 | - | |
| 127 | - $courses = $query->posts; | |
| 128 | - | |
| 129 | - if ( isset( $query_vars['return'] ) && 'objects' === $query_vars['return'] ) { | |
| 130 | - $courses = array_filter( array_map( 'learn_press_get_course', $courses ) ); | |
| 131 | - } | |
| 132 | - | |
| 133 | - if ( isset( $query_vars['paginate'] ) && $query_vars['paginate'] ) { | |
| 134 | - $courses = (object) array( | |
| 135 | - 'courses' => $courses, | |
| 136 | - 'total' => $query->found_posts, | |
| 137 | - 'max_num_pages' => $query->max_num_pages, | |
| 138 | - ); | |
| 139 | - } | |
| 140 | - | |
| 141 | - return apply_filters( 'learn-press/course-object-query', $courses, $query_vars ); | |
| 142 | - } | |
| 143 | -} | |
| 144 | - | |
| 145 | -// Backward compatibility | |
| 146 | -class LP_Query_Course extends LP_Course_Query { | |
| 147 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Class LP_Course_Query | |
| 5 | + * | |
| 6 | + * @version 3.3.0 | |
| 7 | + */ | |
| 8 | +class LP_Course_Query extends LP_Object_Query { | |
| 9 | + | |
| 10 | + protected $course_query_vars = array(); | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * LP_Course_Query constructor. | |
| 14 | + * | |
| 15 | + * @param string $query | |
| 16 | + */ | |
| 17 | + public function __construct( $query = '' ) { | |
| 18 | + $limit = LP_Settings::get_option( 'archive_course_limit', 6 ); | |
| 19 | + if ( empty( $limit ) ) { | |
| 20 | + $limit = 6; | |
| 21 | + } | |
| 22 | + | |
| 23 | + $this->course_query_vars = array( | |
| 24 | + 'post_type' => LP_COURSE_CPT, | |
| 25 | + 'post_status' => array( 'draft', 'pending', 'private', 'publish' ), | |
| 26 | + 'limit' => $limit, | |
| 27 | + 'author' => '', | |
| 28 | + ); | |
| 29 | + | |
| 30 | + parent::__construct( $query ); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @return array | |
| 35 | + */ | |
| 36 | + protected function get_default_query_vars() { | |
| 37 | + return array_merge( | |
| 38 | + parent::get_default_query_vars(), | |
| 39 | + $this->course_query_vars | |
| 40 | + ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Transform our query vars to wp query vars that | |
| 45 | + * can read from db. | |
| 46 | + * | |
| 47 | + * @param array $query_vars | |
| 48 | + * | |
| 49 | + * @return array|mixed | |
| 50 | + * @since 4.0.0 | |
| 51 | + */ | |
| 52 | + public function get_wp_query_vars( $query_vars = array() ) { | |
| 53 | + $query_vars = apply_filters( 'learn-press/course-object-query-args', wp_parse_args( $query_vars, $this->get_query_vars() ) ); | |
| 54 | + $map_keys = array( | |
| 55 | + 'status' => 'post_status', | |
| 56 | + 'page' => 'paged', | |
| 57 | + 'include' => 'post__in', | |
| 58 | + 'return' => 'fields', | |
| 59 | + 'parent' => 'post_parent', | |
| 60 | + 'parent_exclude' => 'post_parent__not_in', | |
| 61 | + 'exclude' => 'post__not_in', | |
| 62 | + 'limit' => 'posts_per_page', | |
| 63 | + 'type' => 'post_type', | |
| 64 | + ); | |
| 65 | + | |
| 66 | + foreach ( $map_keys as $query_key => $db_key ) { | |
| 67 | + if ( isset( $query_vars[ $query_key ] ) ) { | |
| 68 | + $query_vars[ $db_key ] = $query_vars[ $query_key ]; | |
| 69 | + unset( $query_vars[ $query_key ] ); | |
| 70 | + } | |
| 71 | + } | |
| 72 | + | |
| 73 | + $custom_keys = array( | |
| 74 | + 'featured' => '', | |
| 75 | + ); | |
| 76 | + | |
| 77 | + foreach ( $custom_keys as $key => $custom_key ) { | |
| 78 | + if ( isset( $query_vars[ $key ] ) ) { | |
| 79 | + $custom_keys[ $key ] = $query_vars[ $key ]; | |
| 80 | + unset( $query_vars[ $key ] ); | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | + // Query by post meta | |
| 85 | + if ( ! isset( $query_vars['meta_query'] ) ) { | |
| 86 | + $query_vars['meta_query'] = array(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + // Featured | |
| 90 | + if ( '' !== $custom_keys['featured'] ) { | |
| 91 | + $featured = $custom_keys['featured']; | |
| 92 | + | |
| 93 | + if ( $featured === 'yes' || $featured === 1 || $featured === true || $featured === '1' ) { | |
| 94 | + $query_vars['meta_query'] = array( | |
| 95 | + array( | |
| 96 | + 'key' => '_lp_featured', | |
| 97 | + 'value' => 'yes', | |
| 98 | + 'compare' => '=', | |
| 99 | + ), | |
| 100 | + ); | |
| 101 | + } else { | |
| 102 | + $query_vars['meta_query'] = array( | |
| 103 | + array( | |
| 104 | + 'key' => '_lp_featured', | |
| 105 | + 'value' => 'yes', | |
| 106 | + 'compare' => '!=', | |
| 107 | + ), | |
| 108 | + ); | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + return apply_filters( 'learn-press/course-object-wp-query-args', $query_vars ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + /** | |
| 116 | + * Applies our query vars to read courses. | |
| 117 | + * | |
| 118 | + * @return array|mixed | |
| 119 | + * @since 4.0.0 | |
| 120 | + */ | |
| 121 | + public function get_courses() { | |
| 122 | + global $wpdb; | |
| 123 | + $query_vars = $this->get_wp_query_vars(); | |
| 124 | + | |
| 125 | + $query = new WP_Query( $query_vars ); | |
| 126 | + | |
| 127 | + $courses = $query->posts; | |
| 128 | + | |
| 129 | + if ( isset( $query_vars['return'] ) && 'objects' === $query_vars['return'] ) { | |
| 130 | + $courses = array_filter( array_map( 'learn_press_get_course', $courses ) ); | |
| 131 | + } | |
| 132 | + | |
| 133 | + if ( isset( $query_vars['paginate'] ) && $query_vars['paginate'] ) { | |
| 134 | + $courses = (object) array( | |
| 135 | + 'courses' => $courses, | |
| 136 | + 'total' => $query->found_posts, | |
| 137 | + 'max_num_pages' => $query->max_num_pages, | |
| 138 | + ); | |
| 139 | + } | |
| 140 | + | |
| 141 | + return apply_filters( 'learn-press/course-object-query', $courses, $query_vars ); | |
| 142 | + } | |
| 143 | +} | |
| 144 | + | |
| 145 | +// Backward compatibility | |
| 146 | +class LP_Query_Course extends LP_Course_Query { | |
| 147 | +} | |