| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Query |
| 5 |
*/ |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
class LP_Query { |
| 10 |
/** |
| 11 |
* @var array |
| 12 |
*/ |
| 13 |
public $query_vars = array(); |
| 14 |
|
| 15 |
/** |
| 16 |
* LP_Query constructor. |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'init', array( $this, 'add_rewrite_tags' ), 1000, 0 ); |
| 20 |
add_action( 'init', array( $this, 'add_rewrite_rules' ), 1000, 0 ); |
| 21 |
//add_action( 'parse_query', array( $this, 'parse_request' ), 1000, 1 ); |
| 22 |
/** |
| 23 |
* Add searching post by taxonomies |
| 24 |
*/ |
| 25 |
add_action( 'pre_get_posts', array( $this, 'query_taxonomy' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Parses request params and controls page |
| 30 |
* |
| 31 |
* @param WP_Query $q |
| 32 |
* |
| 33 |
* @return mixed |
| 34 |
*/ |
| 35 |
public function parse_request( $q ) { |
| 36 |
if ( did_action( 'learn_press_parse_query' ) ) { |
| 37 |
return $q; |
| 38 |
} |
| 39 |
|
| 40 |
do_action_ref_array( 'learn_press_parse_query', array( &$this ) ); |
| 41 |
|
| 42 |
return $q; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* This function is cloned from wp core function |
| 47 |
* |
| 48 |
* @see WP()->parse_request() |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function get_request() { |
| 53 |
global $wp_rewrite; |
| 54 |
|
| 55 |
$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : ''; |
| 56 |
list( $pathinfo ) = explode( '?', $pathinfo ); |
| 57 |
$pathinfo = str_replace( '%', '%25', $pathinfo ); |
| 58 |
|
| 59 |
list( $req_uri ) = explode( '?', esc_url_raw( $_SERVER['REQUEST_URI'] ) ); |
| 60 |
$self = $_SERVER['PHP_SELF']; |
| 61 |
$home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); |
| 62 |
$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); |
| 63 |
|
| 64 |
$req_uri = str_replace( $pathinfo, '', $req_uri ); |
| 65 |
$req_uri = trim( $req_uri, '/' ); |
| 66 |
$req_uri = preg_replace( $home_path_regex, '', $req_uri ); |
| 67 |
$req_uri = trim( $req_uri, '/' ); |
| 68 |
$pathinfo = trim( $pathinfo, '/' ); |
| 69 |
$pathinfo = preg_replace( $home_path_regex, '', $pathinfo ); |
| 70 |
$pathinfo = trim( $pathinfo, '/' ); |
| 71 |
$self = trim( $self, '/' ); |
| 72 |
$self = preg_replace( $home_path_regex, '', $self ); |
| 73 |
$self = trim( $self, '/' ); |
| 74 |
|
| 75 |
if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) { |
| 76 |
$request = $pathinfo; |
| 77 |
} else { |
| 78 |
if ( $req_uri == $wp_rewrite->index ) { |
| 79 |
$req_uri = ''; |
| 80 |
} |
| 81 |
$request = $req_uri; |
| 82 |
} |
| 83 |
|
| 84 |
return $request; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Add custom rewrite tags |
| 89 |
*/ |
| 90 |
function add_rewrite_tags() { |
| 91 |
add_rewrite_tag( '%course-item%', '([^&]+)' ); |
| 92 |
add_rewrite_tag( '%item-type%', '([^&]+)' ); |
| 93 |
// add_rewrite_tag( '%quiz%', '([^&]+)' ); |
| 94 |
add_rewrite_tag( '%question%', '([^&]+)' ); |
| 95 |
add_rewrite_tag( '%user%', '([^/]*)' ); |
| 96 |
|
| 97 |
add_rewrite_tag( '%view%', '([^/]*)' ); |
| 98 |
add_rewrite_tag( '%view_id%', '(.*)' ); |
| 99 |
add_rewrite_tag( '%section%', '(.*)' ); |
| 100 |
|
| 101 |
add_rewrite_tag( '%content-item-only%', '(.*)' ); |
| 102 |
do_action( 'learn_press_add_rewrite_tags' ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Add more custom rewrite rules |
| 107 |
*/ |
| 108 |
function add_rewrite_rules() { |
| 109 |
$course_type = LP_COURSE_CPT; |
| 110 |
$post_types = get_post_types( '', 'objects' ); |
| 111 |
$slug = preg_replace( '!^/!', '', $post_types[ $course_type ]->rewrite['slug'] ); |
| 112 |
$has_category = false; |
| 113 |
|
| 114 |
if ( preg_match( '!(%?course_category%?)!', $slug ) ) { |
| 115 |
$slug = preg_replace( '!(%?course_category%?)!', '(.+?)/([^/]+)', $slug ); |
| 116 |
$has_category = true; |
| 117 |
} |
| 118 |
|
| 119 |
$custom_slug_lesson = sanitize_title_with_dashes( LP_Settings::get_option( 'lesson_slug', 'lessons' ) ); |
| 120 |
$custom_slug_quiz = sanitize_title_with_dashes( LP_Settings::get_option( 'quiz_slug', 'quizzes' ) ); |
| 121 |
|
| 122 |
/** |
| 123 |
* Use urldecode to convert an encoded string to normal. |
| 124 |
* This fixed the issue with custom slug of lesson/quiz in some languages |
| 125 |
* Eg: урока |
| 126 |
*/ |
| 127 |
if ( ! empty( $custom_slug_lesson ) ) { |
| 128 |
$post_types['lp_lesson']->rewrite['slug'] = urldecode( $custom_slug_lesson ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! empty( $custom_slug_quiz ) ) { |
| 132 |
$post_types['lp_quiz']->rewrite['slug'] = urldecode( $custom_slug_quiz ); |
| 133 |
} |
| 134 |
|
| 135 |
$rules = array(); |
| 136 |
|
| 137 |
if ( $has_category ) { |
| 138 |
$rules[] = array( |
| 139 |
'^' . $slug . '(?:/' . $post_types['lp_lesson']->rewrite['slug'] . '/([^/]+))/?$', |
| 140 |
'index.php?' . $course_type . '=$matches[2]&course_category=$matches[1]&course-item=$matches[3]&item-type=lp_lesson', |
| 141 |
'top', |
| 142 |
); |
| 143 |
|
| 144 |
$rules[] = array( |
| 145 |
'^' . $slug . '(?:/' . $post_types['lp_quiz']->rewrite['slug'] . '/([^/]+)/?([^/]+)?)/?$', |
| 146 |
'index.php?' . $course_type . '=$matches[2]&course_category=$matches[1]&course-item=$matches[3]&question=$matches[4]&item-type=lp_quiz', |
| 147 |
'top', |
| 148 |
); |
| 149 |
|
| 150 |
} else { |
| 151 |
|
| 152 |
$rules[] = array( |
| 153 |
'^' . $slug . '/([^/]+)(?:/' . $post_types['lp_lesson']->rewrite['slug'] . '/([^/]+))/?$', |
| 154 |
'index.php?' . $course_type . '=$matches[1]&course-item=$matches[2]&item-type=lp_lesson', |
| 155 |
'top', |
| 156 |
); |
| 157 |
$rules[] = array( |
| 158 |
'^' . $slug . '/([^/]+)(?:/' . $post_types['lp_quiz']->rewrite['slug'] . '/([^/]+)/?([^/]+)?)/?$', |
| 159 |
'index.php?' . $course_type . '=$matches[1]&course-item=$matches[2]&question=$matches[3]&item-type=lp_quiz', |
| 160 |
'top', |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
// Profile |
| 165 |
$profile_id = learn_press_get_page_id( 'profile' ); |
| 166 |
if ( $profile_id ) { |
| 167 |
$rules[] = array( |
| 168 |
'^' . get_post_field( 'post_name', $profile_id ) . '/([^/]*)/?$', |
| 169 |
'index.php?page_id=' . $profile_id . '&user=$matches[1]', |
| 170 |
'top', |
| 171 |
); |
| 172 |
|
| 173 |
$profile = learn_press_get_profile(); |
| 174 |
$tabs = $profile->get_tabs()->get(); |
| 175 |
|
| 176 |
if ( $tabs ) { |
| 177 |
foreach ( $tabs as $slug => $args ) { |
| 178 |
$tab_slug = $args['slug'] ?? $slug; |
| 179 |
$rules[] = array( |
| 180 |
'^' . get_post_field( 'post_name', $profile_id ) . '/([^/]*)/?(' . $tab_slug . ')/?([0-9]*)/?$', |
| 181 |
'index.php?page_id=' . $profile_id . '&user=$matches[1]&view=$matches[2]&view_id=$matches[3]', |
| 182 |
'top', |
| 183 |
); |
| 184 |
|
| 185 |
if ( ! empty( $args['sections'] ) ) { |
| 186 |
foreach ( $args['sections'] as $section_slug => $section ) { |
| 187 |
$section_slug = $section['slug'] ?? $section_slug; |
| 188 |
$rules[] = array( |
| 189 |
'^' . get_post_field( 'post_name', $profile_id ) . '/([^/]*)/?(' . $tab_slug . ')/(' . $section_slug . ')/?([0-9]*)?$', |
| 190 |
'index.php?page_id=' . $profile_id . '&user=$matches[1]&view=$matches[2]§ion=$matches[3]&view_id=$matches[4]', |
| 191 |
'top', |
| 192 |
); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// Archive course |
| 200 |
/*$course_page_id = learn_press_get_page_id( 'courses' ); |
| 201 |
if ( $course_page_id ) { |
| 202 |
$rules[] = array( |
| 203 |
'^' . get_post_field( 'post_name', $course_page_id ) . '/page/([0-9]{1,})/?$', |
| 204 |
'index.php?pagename=' . get_post_field( 'post_name', $course_page_id ) . '&page=$matches[1]', |
| 205 |
'top', |
| 206 |
); |
| 207 |
}*/ |
| 208 |
|
| 209 |
global $wp_rewrite; |
| 210 |
|
| 211 |
/** |
| 212 |
* Polylang compatibility |
| 213 |
*/ |
| 214 |
if ( function_exists( 'PLL' ) ) { |
| 215 |
$pll = PLL(); |
| 216 |
$pll_languages = $pll->model->get_languages_list( array( 'fields' => 'slug' ) ); |
| 217 |
|
| 218 |
if ( $pll->options['hide_default'] ) { |
| 219 |
if ( isset( $pll->options['default_lang'] ) ) { |
| 220 |
$pll_languages = array_diff( $pll_languages, array( $pll->options['default_lang'] ) ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if ( ! empty( $pll_languages ) ) { |
| 225 |
$pll_languages = $wp_rewrite->root . ( $pll->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $pll_languages ) . ')/'; |
| 226 |
} else { |
| 227 |
$pll_languages = ''; |
| 228 |
} |
| 229 |
} |
| 230 |
$new_rules = array(); |
| 231 |
foreach ( $rules as $k => $rule ) { |
| 232 |
$new_rules[] = $rule; |
| 233 |
call_user_func_array( 'add_rewrite_rule', $rule ); |
| 234 |
|
| 235 |
/** |
| 236 |
* Modify rewrite rule |
| 237 |
*/ |
| 238 |
if ( isset( $pll_languages ) ) { |
| 239 |
|
| 240 |
$rule[0] = $pll_languages . str_replace( $wp_rewrite->root, '', ltrim( $rule[0], '^' ) ); |
| 241 |
$rule[1] = str_replace( |
| 242 |
array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ), |
| 243 |
array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ), |
| 244 |
$rule[1] |
| 245 |
); |
| 246 |
$new_rules[] = $rule; |
| 247 |
call_user_func_array( 'add_rewrite_rule', $rule ); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
$new_rules = md5( serialize( $new_rules ) ); |
| 252 |
$old_rules = get_transient( 'lp_rewrite_rules_hash' ); |
| 253 |
|
| 254 |
if ( $old_rules !== $new_rules ) { |
| 255 |
set_transient( 'lp_rewrite_rules_hash', $new_rules, DAY_IN_SECONDS ); |
| 256 |
flush_rewrite_rules(); |
| 257 |
} |
| 258 |
|
| 259 |
do_action( 'learn_press_add_rewrite_rules' ); |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
/** |
| 265 |
* Get current course user accessing |
| 266 |
* |
| 267 |
* @param string $return |
| 268 |
* |
| 269 |
* @return bool|false|int|LP_Course|mixed |
| 270 |
*/ |
| 271 |
public function get_course( $return = 'id' ) { |
| 272 |
$course = false; |
| 273 |
if ( learn_press_is_course() ) { |
| 274 |
$course = get_the_ID(); |
| 275 |
} |
| 276 |
if ( $course && $return == 'object' ) { |
| 277 |
$course = learn_press_get_course( $course ); |
| 278 |
} |
| 279 |
|
| 280 |
return $course; |
| 281 |
} |
| 282 |
|
| 283 |
public function get_course_item( $return = 'id' ) { |
| 284 |
$course = $this->get_course( 'object' ); |
| 285 |
$user = learn_press_get_current_user(); |
| 286 |
$item = isset( $item ) ? $item : LearnPress::instance()->global['course-item']; |
| 287 |
if ( $item && $return == 'object' ) { |
| 288 |
$item = LP_Course::get_item( $item ); |
| 289 |
} |
| 290 |
|
| 291 |
return $item; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* @param WP_Query $q |
| 296 |
*/ |
| 297 |
public function query_taxonomy( $q ) { |
| 298 |
|
| 299 |
// We only want to affect the main query |
| 300 |
if ( ! $q->is_main_query() ) { |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
if ( is_search() ) { |
| 305 |
add_filter( 'posts_where', array( $this, 'add_tax_search' ) ); |
| 306 |
add_filter( 'posts_join', array( $this, 'join_term' ) ); |
| 307 |
add_filter( 'posts_groupby', array( $this, 'tax_groupby' ) ); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* @param string $join |
| 313 |
* |
| 314 |
* @return string |
| 315 |
*/ |
| 316 |
public function join_term( $join ) { |
| 317 |
global $wp_query, $wpdb; |
| 318 |
|
| 319 |
if ( ! empty( $wp_query->query_vars['s'] ) && ! is_admin() ) { |
| 320 |
if ( ! preg_match( '/' . $wpdb->term_relationships . '/', $join ) ) { |
| 321 |
$join .= "LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id "; |
| 322 |
} |
| 323 |
$join .= "LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id "; |
| 324 |
$join .= "LEFT JOIN $wpdb->terms ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id "; |
| 325 |
} |
| 326 |
|
| 327 |
return $join; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* @param string $where |
| 332 |
* |
| 333 |
* @return string |
| 334 |
*/ |
| 335 |
public function add_tax_search( $where ) { |
| 336 |
global $wp_query, $wpdb; |
| 337 |
|
| 338 |
if ( ! empty( $wp_query->query_vars['s'] ) && ! is_admin() ) { |
| 339 |
$escaped_s = esc_sql( $wp_query->query_vars['s'] ); |
| 340 |
$where .= "OR $wpdb->terms.name LIKE '%{$escaped_s}%'"; |
| 341 |
} |
| 342 |
|
| 343 |
return $where; |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* @param string $groupby |
| 348 |
* |
| 349 |
* @return string |
| 350 |
*/ |
| 351 |
public function tax_groupby( $groupby ) { |
| 352 |
global $wpdb; |
| 353 |
$groupby = "{$wpdb->posts}.ID"; |
| 354 |
|
| 355 |
$this->remove_query_tax(); |
| 356 |
|
| 357 |
return $groupby; |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Remove filter query |
| 362 |
*/ |
| 363 |
public function remove_query_tax() { |
| 364 |
remove_filter( 'posts_where', 'learn_press_add_tax_search' ); |
| 365 |
remove_filter( 'posts_join', 'learn_press_join_term' ); |
| 366 |
remove_filter( 'posts_groupby', 'learn_press_tax_groupby' ); |
| 367 |
} |
| 368 |
} |
| 369 |
|