| @@ -1,29 +1,29 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | 4 | * Class LP_Query |
| 5 | + * | |
| 6 | + * @version 4.2.2.3 | |
| 5 | 7 | */ |
| 6 | 8 | |
| 9 | +use LearnPress\CourseBuilder\CourseBuilder; | |
| 10 | + | |
| 7 | 11 | defined( 'ABSPATH' ) || exit; |
| 8 | 12 | |
| 9 | 13 | class LP_Query { |
| 10 | 14 | /** |
| 11 | - * @var array | |
| 12 | - */ | |
| 13 | - public $query_vars = array(); | |
| 14 | - | |
| 15 | - /** | |
| 16 | 15 | * LP_Query constructor. |
| 17 | 16 | */ |
| 18 | 17 | 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' ) ); | |
| 18 | + // Not run in REST API LP, or heartbeat of WP | |
| 19 | + if ( LP_Helper::isRestApiLP() || isset( $_POST['action'] ) && 'heartbeat' === $_POST['action'] ) { | |
| 20 | + return; | |
| 21 | + } | |
| 22 | + | |
| 23 | + add_action( 'init', array( $this, 'add_rewrite_tags' ) ); | |
| 24 | + add_action( 'init', array( $this, 'add_rewrite_endpoints' ) ); | |
| 25 | + add_filter( 'option_rewrite_rules', [ $this, 'update_option_rewrite_rules' ], 1 ); | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * Parses request params and controls page |
| @@ -31,9 +31,9 @@ | ||
| 31 | 31 | * @param WP_Query $q |
| 32 | 32 | * |
| 33 | 33 | * @return mixed |
| 34 | 34 | */ |
| 35 | - public function parse_request( $q ) { | |
| 35 | + /*public function parse_request( $q ) { | |
| 36 | 36 | if ( did_action( 'learn_press_parse_query' ) ) { |
| 37 | 37 | return $q; |
| 38 | 38 | } |
| 39 | 39 | |
| @@ -39,274 +39,294 @@ | ||
| 39 | 39 | |
| 40 | 40 | do_action_ref_array( 'learn_press_parse_query', array( &$this ) ); |
| 41 | 41 | |
| 42 | 42 | return $q; |
| 43 | - } | |
| 43 | + }*/ | |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | - * This function is cloned from wp core function | |
| 47 | - * | |
| 48 | - * @see WP()->parse_request() | |
| 49 | - * | |
| 50 | - * @return string | |
| 46 | + * Add custom rewrite tags | |
| 51 | 47 | */ |
| 52 | - public function get_request() { | |
| 53 | - global $wp_rewrite; | |
| 48 | + public function add_rewrite_tags() { | |
| 49 | + $tags = [ | |
| 50 | + '%course-item%' => '([^&]+)', | |
| 51 | + '%item-type%' => '([^&]+)', | |
| 52 | + '%question%' => '([^&]+)', | |
| 53 | + '%user%' => '([^/]*)', | |
| 54 | + '%view%' => '([^/]*)', | |
| 55 | + '%view_id%' => '(.*)', | |
| 56 | + '%section%' => '(.*)', | |
| 57 | + '%content-item-only%' => '(.*)', | |
| 58 | + '%is_single_instructor%' => '(.*)', | |
| 59 | + '%instructor_name%' => '(.*)', | |
| 60 | + // For Course builder | |
| 61 | + '%' . CourseBuilder::QUERY_VAR_MENU_SLUG . '%' => '([^/]*)', | |
| 62 | + '%' . CourseBuilder::QUERY_VAR_ITEM_ID . '%' => '(.*)', | |
| 63 | + '%' . CourseBuilder::QUERY_VAR_IS_COURSE_BUILDER . '%' => '(.*)', | |
| 64 | + ]; | |
| 54 | 65 | |
| 55 | - $pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : ''; | |
| 56 | - list( $pathinfo ) = explode( '?', $pathinfo ); | |
| 57 | - $pathinfo = str_replace( '%', '%25', $pathinfo ); | |
| 66 | + $tags = apply_filters( 'learn-press/rewrite/tags', $tags ); | |
| 67 | + foreach ( $tags as $tag => $regex ) { | |
| 68 | + add_rewrite_tag( $tag, $regex ); | |
| 69 | + } | |
| 70 | + } | |
| 58 | 71 | |
| 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, '|' ) ); | |
| 72 | + /** | |
| 73 | + * Add custom rewrite endpoints | |
| 74 | + */ | |
| 75 | + public function add_rewrite_endpoints() { | |
| 76 | + $settings = LP_Settings::instance(); | |
| 63 | 77 | |
| 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, '/' ); | |
| 78 | + $endpoints = $settings->get_checkout_endpoints(); | |
| 79 | + if ( $endpoints ) { | |
| 80 | + foreach ( $endpoints as $endpoint => $value ) { | |
| 81 | + LearnPress::instance()->query_vars[ $endpoint ] = $value; | |
| 82 | + add_rewrite_endpoint( $value, EP_PAGES ); | |
| 83 | + } | |
| 84 | + } | |
| 74 | 85 | |
| 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 = ''; | |
| 86 | + $endpoints = $settings->get_profile_endpoints(); | |
| 87 | + if ( $endpoints ) { | |
| 88 | + foreach ( $endpoints as $endpoint => $value ) { | |
| 89 | + LearnPress::instance()->query_vars[ $endpoint ] = $value; | |
| 90 | + add_rewrite_endpoint( $value, EP_PAGES ); | |
| 80 | 91 | } |
| 81 | - $request = $req_uri; | |
| 82 | 92 | } |
| 83 | 93 | |
| 84 | - return $request; | |
| 85 | - } | |
| 94 | + $endpoints = $settings->get_course_builder_endpoints(); | |
| 95 | + if ( $endpoints ) { | |
| 96 | + foreach ( $endpoints as $endpoint => $value ) { | |
| 97 | + LearnPress::instance()->query_vars[ $endpoint ] = $value; | |
| 98 | + add_rewrite_endpoint( $value, EP_PAGES ); | |
| 99 | + } | |
| 100 | + } | |
| 86 | 101 | |
| 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%', '([^/]*)' ); | |
| 102 | + $endpoints = $settings->get( 'quiz_endpoints' ); | |
| 103 | + if ( $endpoints ) { | |
| 104 | + foreach ( $endpoints as $endpoint => $value ) { | |
| 105 | + $endpoint = preg_replace( '!_!', '-', $endpoint ); | |
| 106 | + LearnPress::instance()->query_vars[ $endpoint ] = $value; | |
| 107 | + add_rewrite_endpoint( | |
| 108 | + $value, /*EP_ROOT | */ | |
| 109 | + EP_PAGES | |
| 110 | + ); | |
| 111 | + } | |
| 112 | + } | |
| 96 | 113 | |
| 97 | - add_rewrite_tag( '%view%', '([^/]*)' ); | |
| 98 | - add_rewrite_tag( '%view_id%', '(.*)' ); | |
| 99 | - add_rewrite_tag( '%section%', '(.*)' ); | |
| 114 | + add_rewrite_endpoint( 'lp-ajax-handle', EP_ROOT | EP_PAGES ); | |
| 100 | 115 | |
| 101 | - add_rewrite_tag( '%content-item-only%', '(.*)' ); | |
| 102 | - do_action( 'learn_press_add_rewrite_tags' ); | |
| 116 | + // Code temporary to fix 404 of order receiver page | |
| 117 | + if ( LP_Page_Controller::is_page_checkout() ) { | |
| 118 | + flush_rewrite_rules(); | |
| 119 | + } | |
| 103 | 120 | } |
| 104 | 121 | |
| 105 | 122 | /** |
| 106 | - * Add more custom rewrite rules | |
| 123 | + * Add lp rewrite rules | |
| 124 | + * Format [key_parent][single_key] = [rule => match] | |
| 125 | + * | |
| 126 | + * link item course | |
| 127 | + * link profile | |
| 128 | + * @return array | |
| 129 | + * @version 1.0.4 | |
| 130 | + * @modify 4.2.2 | |
| 131 | + * @since 3.0.0 | |
| 107 | 132 | */ |
| 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; | |
| 133 | + public function add_rewrite_rules(): array { | |
| 134 | + $rules = array(); | |
| 113 | 135 | |
| 114 | - if ( preg_match( '!(%?course_category%?)!', $slug ) ) { | |
| 115 | - $slug = preg_replace( '!(%?course_category%?)!', '(.+?)/([^/]+)', $slug ); | |
| 116 | - $has_category = true; | |
| 117 | - } | |
| 136 | + try { | |
| 137 | + $course_item_slugs = LP_Settings::get_course_items_slug(); | |
| 138 | + $course_slug = LP_Settings::get_permalink_single_course(); | |
| 139 | + // For permalink has %course_category% | |
| 140 | + if ( preg_match( '!%course_category%!', $course_slug ) ) { | |
| 141 | + if ( ! preg_match( '!page!', LP_Helper::getUrlCurrent() ) ) { | |
| 142 | + $course_slug = preg_replace( '!%course_category%!', '([^/]+)/([^/]+)', $course_slug ); | |
| 118 | 143 | |
| 119 | - $custom_slug_lesson = sanitize_title_with_dashes( LP_Settings::instance()->get( 'lesson_slug' ) ); | |
| 120 | - $custom_slug_quiz = sanitize_title_with_dashes( LP_Settings::instance()->get( 'quiz_slug' ) ); | |
| 144 | + // Rule single course | |
| 145 | + $rules['single-course-with-cat'][] = [ | |
| 146 | + "^{$course_slug}/?$" => | |
| 147 | + 'index.php?' . LP_COURSE_CPT . '=$matches[2]&course_category=$matches[1]', | |
| 148 | + ]; | |
| 121 | 149 | |
| 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 | - } | |
| 150 | + // Rule single item | |
| 151 | + foreach ( $course_item_slugs as $post_type => $course_item_slug ) { | |
| 152 | + $rules['course-with-cat-items'][ $post_type ] = [ | |
| 153 | + "^{$course_slug}(?:/{$course_item_slug}/([^/]+))/?$" => | |
| 154 | + 'index.php?' . LP_COURSE_CPT . '=$matches[2]&course_category=$matches[1]&course-item=$matches[3]&item-type=' . $post_type, | |
| 155 | + ]; | |
| 156 | + } | |
| 157 | + } | |
| 158 | + } else { | |
| 159 | + // Rule single course | |
| 160 | + $rules['single-course'][] = [ | |
| 161 | + "^{$course_slug}/([^/]+)/?$" => | |
| 162 | + 'index.php?' . LP_COURSE_CPT . '=$matches[1]', | |
| 163 | + ]; | |
| 130 | 164 | |
| 131 | - if ( ! empty( $custom_slug_quiz ) ) { | |
| 132 | - $post_types['lp_quiz']->rewrite['slug'] = urldecode( $custom_slug_quiz ); | |
| 133 | - } | |
| 165 | + // Rule single item | |
| 166 | + foreach ( $course_item_slugs as $post_type => $course_item_slug ) { | |
| 167 | + $rules['course-items'][ $post_type ] = [ | |
| 168 | + "^{$course_slug}/([^/]+)(?:/{$course_item_slug}/([^/]+))/?$" => | |
| 169 | + 'index.php?' . LP_COURSE_CPT . '=$matches[1]&course-item=$matches[2]&item-type=' . $post_type, | |
| 170 | + ]; | |
| 171 | + } | |
| 172 | + } | |
| 134 | 173 | |
| 135 | - $rules = array(); | |
| 174 | + // Profile | |
| 175 | + $this->add_rewrite_rules_profile( $rules ); | |
| 136 | 176 | |
| 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 | - ); | |
| 177 | + // Instructor detail | |
| 178 | + $single_instructor_page_id = learn_press_get_page_id( 'single_instructor' ); | |
| 179 | + $instructor_slug = get_post_field( 'post_name', $single_instructor_page_id ); | |
| 180 | + $rules['instructor']['has_name'] = [ | |
| 181 | + "^{$instructor_slug}/([^/]+)/?(?:page/)?([^/][0-9]*)?/?$" => | |
| 182 | + 'index.php?page_id=' . $single_instructor_page_id . '&is_single_instructor=1&instructor_name=$matches[1]&paged=$matches[2]', | |
| 183 | + ]; | |
| 184 | + $rules['instructor']['no_name'] = [ | |
| 185 | + "^{$instructor_slug}/?$" => | |
| 186 | + 'index.php?page_id=' . $single_instructor_page_id . '&is_single_instructor=1&paged=$matches[2]', | |
| 187 | + ]; | |
| 143 | 188 | |
| 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 | - ); | |
| 189 | + // For handle request lp-ajax | |
| 190 | + $rules['lp-ajax'][] = [ | |
| 191 | + '^lp-ajax-handle/?$' => 'index.php', | |
| 192 | + ]; | |
| 149 | 193 | |
| 150 | - } else { | |
| 194 | + // Course builder | |
| 195 | + $this->add_rewrite_rules_course_builder( $rules ); | |
| 151 | 196 | |
| 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 | - ); | |
| 197 | + $rules = apply_filters( 'learn-press/rewrite/rules', $rules ); | |
| 198 | + } catch ( Throwable $e ) { | |
| 199 | + error_log( $e->getMessage() ); | |
| 162 | 200 | } |
| 163 | 201 | |
| 202 | + return $rules; | |
| 203 | + } | |
| 204 | + | |
| 205 | + /** | |
| 206 | + * Add rewrite rules for profile page. | |
| 207 | + * | |
| 208 | + * @param $rules | |
| 209 | + * | |
| 210 | + * @return void | |
| 211 | + */ | |
| 212 | + public function add_rewrite_rules_profile( &$rules ) { | |
| 164 | 213 | // Profile |
| 165 | 214 | $profile_id = learn_press_get_page_id( 'profile' ); |
| 166 | 215 | 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 | - ); | |
| 216 | + // Rule view profile of user (self or another) | |
| 217 | + $page_profile_slug = urldecode( get_post_field( 'post_name', $profile_id ) ); | |
| 218 | + $rules['profile']['user'] = [ | |
| 219 | + "^{$page_profile_slug}/([^/]*)/?$" => | |
| 220 | + "index.php?page_id={$profile_id}&user=" . '$matches[1]', | |
| 221 | + ]; | |
| 172 | 222 | |
| 173 | - $profile = learn_press_get_profile(); | |
| 174 | - $tabs = $profile->get_tabs()->get(); | |
| 175 | - | |
| 223 | + // Rule view profile of user (self or another) with tab | |
| 224 | + $tabs = LP_Profile::get_tabs_arr(); | |
| 176 | 225 | 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 | - ); | |
| 226 | + foreach ( $tabs as $tab_key => $args ) { | |
| 227 | + $tab_slug = $args['slug'] ?? $tab_key; | |
| 228 | + $rules['profile'][ $tab_key ] = [ | |
| 229 | + "^{$page_profile_slug}/([^/]*)/({$tab_slug})/?([0-9]*)/?$" => | |
| 230 | + 'index.php?page_id=' . $profile_id . '&user=$matches[1]&view=$matches[2]&view_id=$matches[3]', | |
| 231 | + ]; | |
| 184 | 232 | |
| 185 | 233 | 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 | - ); | |
| 234 | + foreach ( $args['sections'] as $section_key => $section ) { | |
| 235 | + $section_slug = $section['slug'] ?? $section_key; | |
| 236 | + $rules['profile'][ $section_key ] = [ | |
| 237 | + "^{$page_profile_slug}/([^/]*)/({$tab_slug})/({$section_slug})/?([0-9]*)?$" => | |
| 238 | + 'index.php?page_id=' . $profile_id . '&user=$matches[1]&view=$matches[2]§ion=$matches[3]&view_id=$matches[4]', | |
| 239 | + ]; | |
| 193 | 240 | } |
| 194 | 241 | } |
| 195 | 242 | } |
| 196 | 243 | } |
| 197 | - } | |
| 198 | 244 | |
| 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 | - } | |
| 245 | + apply_filters( 'learn-press/rewrite-rules/profile', $rules['profile'], $profile_id ); | |
| 229 | 246 | } |
| 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 | 247 | } |
| 262 | 248 | |
| 263 | - | |
| 264 | 249 | /** |
| 265 | - * Get current course user accessing | |
| 250 | + * Add rewrite rules for profile page. | |
| 266 | 251 | * |
| 267 | - * @param string $return | |
| 252 | + * @param $rules | |
| 268 | 253 | * |
| 269 | - * @return bool|false|int|LP_Course|mixed | |
| 254 | + * @return void | |
| 270 | 255 | */ |
| 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 | - } | |
| 256 | + public function add_rewrite_rules_course_builder( &$rules ) { | |
| 257 | + // Course Builder | |
| 258 | + $page = LP_Settings::get_option( 'course_builder', 'course-builder' ); | |
| 259 | + $menus = CourseBuilder::get_menus_arr(); | |
| 260 | + $key_rule = 'course-builder'; | |
| 279 | 261 | |
| 280 | - return $course; | |
| 281 | - } | |
| 262 | + if ( $menus ) { | |
| 263 | + $rules[ $key_rule ]['home'] = [ | |
| 264 | + "^{$page}/?$" => 'index.php?is_course_builder=1', | |
| 265 | + ]; | |
| 282 | 266 | |
| 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 | - } | |
| 267 | + foreach ( $menus as $menu_slug => $args ) { | |
| 268 | + $rules[ $key_rule ][ $menu_slug . '_create' ] = [ | |
| 269 | + "^{$page}/({$menu_slug})/(?:create)/?$" => | |
| 270 | + sprintf( | |
| 271 | + 'index.php?%s=1&%s=%s&%s=new', | |
| 272 | + CourseBuilder::QUERY_VAR_IS_COURSE_BUILDER, | |
| 273 | + CourseBuilder::QUERY_VAR_MENU_SLUG, | |
| 274 | + $menu_slug, | |
| 275 | + CourseBuilder::QUERY_VAR_ITEM_ID | |
| 276 | + ), | |
| 277 | + ]; | |
| 290 | 278 | |
| 291 | - return $item; | |
| 292 | - } | |
| 279 | + $rules[ $key_rule ][ $menu_slug . '_pagination' ] = [ | |
| 280 | + "^{$page}/{$menu_slug}/(?:page)/?([0-9]*)/?$" => | |
| 281 | + sprintf( | |
| 282 | + 'index.php?%s=1&%s=%s&paged=$matches[1]', | |
| 283 | + CourseBuilder::QUERY_VAR_IS_COURSE_BUILDER, | |
| 284 | + CourseBuilder::QUERY_VAR_MENU_SLUG, | |
| 285 | + $menu_slug | |
| 286 | + ), | |
| 287 | + ]; | |
| 293 | 288 | |
| 294 | - /** | |
| 295 | - * @param WP_Query $q | |
| 296 | - */ | |
| 297 | - public function query_taxonomy( $q ) { | |
| 289 | + $rules[ $key_rule ][ $menu_slug ] = [ | |
| 290 | + "^{$page}/{$menu_slug}/?([0-9]*)/?$" => | |
| 291 | + sprintf( | |
| 292 | + 'index.php?%s=1&%s=%s&%s=$matches[1]', | |
| 293 | + CourseBuilder::QUERY_VAR_IS_COURSE_BUILDER, | |
| 294 | + CourseBuilder::QUERY_VAR_MENU_SLUG, | |
| 295 | + $menu_slug, | |
| 296 | + CourseBuilder::QUERY_VAR_ITEM_ID | |
| 297 | + ), | |
| 298 | + ]; | |
| 298 | 299 | |
| 299 | - // We only want to affect the main query | |
| 300 | - if ( ! $q->is_main_query() ) { | |
| 301 | - return; | |
| 300 | + if ( empty( $args['sub_menu'] ) || ! is_array( $args['sub_menu'] ) ) { | |
| 301 | + continue; | |
| 302 | + } | |
| 303 | + | |
| 304 | + foreach ( $args['sub_menu'] as $sub_menu_slug => $sub_menu_args ) { | |
| 305 | + if ( is_array( $sub_menu_args ) && ! empty( $sub_menu_args['slug'] ) ) { | |
| 306 | + $sub_menu_slug = $sub_menu_args['slug']; | |
| 307 | + } | |
| 308 | + | |
| 309 | + if ( ! is_string( $sub_menu_slug ) || '' === $sub_menu_slug ) { | |
| 310 | + continue; | |
| 311 | + } | |
| 312 | + | |
| 313 | + $rules[ $key_rule ][ $menu_slug . '_' . $sub_menu_slug ] = [ | |
| 314 | + "^{$page}/{$menu_slug}/{$sub_menu_slug}/?$" => | |
| 315 | + sprintf( | |
| 316 | + 'index.php?%s=1&%s=%s&%s=%s', | |
| 317 | + CourseBuilder::QUERY_VAR_IS_COURSE_BUILDER, | |
| 318 | + CourseBuilder::QUERY_VAR_MENU_SLUG, | |
| 319 | + $menu_slug, | |
| 320 | + CourseBuilder::QUERY_VAR_ITEM_ID, | |
| 321 | + $sub_menu_slug | |
| 322 | + ), | |
| 323 | + ]; | |
| 324 | + } | |
| 325 | + } | |
| 302 | 326 | } |
| 303 | 327 | |
| 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 | - } | |
| 328 | + apply_filters( 'learn-press/rewrite-rules/course-builder', $rules[ $key_rule ], $page ); | |
| 309 | 329 | } |
| 310 | 330 | |
| 311 | 331 | /** |
| 312 | 332 | * @param string $join |
| @@ -311,10 +331,11 @@ | ||
| 311 | 331 | /** |
| 312 | 332 | * @param string $join |
| 313 | 333 | * |
| 314 | 334 | * @return string |
| 335 | + * @deprecated 4.2.6.6 | |
| 315 | 336 | */ |
| 316 | - public function join_term( $join ) { | |
| 337 | + /*public function join_term( $join ) { | |
| 317 | 338 | global $wp_query, $wpdb; |
| 318 | 339 | |
| 319 | 340 | if ( ! empty( $wp_query->query_vars['s'] ) && ! is_admin() ) { |
| 320 | 341 | if ( ! preg_match( '/' . $wpdb->term_relationships . '/', $join ) ) { |
| @@ -324,16 +345,17 @@ | ||
| 324 | 345 | $join .= "LEFT JOIN $wpdb->terms ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id "; |
| 325 | 346 | } |
| 326 | 347 | |
| 327 | 348 | return $join; |
| 328 | - } | |
| 349 | + }*/ | |
| 329 | 350 | |
| 330 | 351 | /** |
| 331 | 352 | * @param string $where |
| 332 | 353 | * |
| 333 | 354 | * @return string |
| 355 | + * @deprecated 4.2.6.6 | |
| 334 | 356 | */ |
| 335 | - public function add_tax_search( $where ) { | |
| 357 | + /*public function add_tax_search( $where ) { | |
| 336 | 358 | global $wp_query, $wpdb; |
| 337 | 359 | |
| 338 | 360 | if ( ! empty( $wp_query->query_vars['s'] ) && ! is_admin() ) { |
| 339 | 361 | $escaped_s = esc_sql( $wp_query->query_vars['s'] ); |
| @@ -340,16 +362,17 @@ | ||
| 340 | 362 | $where .= "OR $wpdb->terms.name LIKE '%{$escaped_s}%'"; |
| 341 | 363 | } |
| 342 | 364 | |
| 343 | 365 | return $where; |
| 344 | - } | |
| 366 | + }*/ | |
| 345 | 367 | |
| 346 | 368 | /** |
| 347 | 369 | * @param string $groupby |
| 348 | 370 | * |
| 349 | 371 | * @return string |
| 372 | + * @deprecated 4.2.6.6 | |
| 350 | 373 | */ |
| 351 | - public function tax_groupby( $groupby ) { | |
| 374 | + /*public function tax_groupby( $groupby ) { | |
| 352 | 375 | global $wpdb; |
| 353 | 376 | $groupby = "{$wpdb->posts}.ID"; |
| 354 | 377 | |
| 355 | 378 | $this->remove_query_tax(); |
| @@ -354,15 +377,69 @@ | ||
| 354 | 377 | |
| 355 | 378 | $this->remove_query_tax(); |
| 356 | 379 | |
| 357 | 380 | return $groupby; |
| 358 | - } | |
| 381 | + }*/ | |
| 359 | 382 | |
| 360 | 383 | /** |
| 361 | 384 | * Remove filter query |
| 385 | + * @deprecated 4.2.6.6 | |
| 362 | 386 | */ |
| 363 | - public function remove_query_tax() { | |
| 387 | + /*public function remove_query_tax() { | |
| 364 | 388 | remove_filter( 'posts_where', 'learn_press_add_tax_search' ); |
| 365 | 389 | remove_filter( 'posts_join', 'learn_press_join_term' ); |
| 366 | 390 | remove_filter( 'posts_groupby', 'learn_press_tax_groupby' ); |
| 391 | + }*/ | |
| 392 | + | |
| 393 | + /** | |
| 394 | + * Clear cache rewrite rules when update option rewrite_rules | |
| 395 | + * Fixed for case: addons Certificates (v4.0.5), FE(4.0.5), Live(4.0.2), Collections(4.0.2) not installed on site client. | |
| 396 | + * Run only one time when reload page Frontend. | |
| 397 | + * | |
| 398 | + * @return mixed|array | |
| 399 | + * @since 4.2.2 | |
| 400 | + * @version 1.0.3 | |
| 401 | + * @see get_option() hook in this function. | |
| 402 | + */ | |
| 403 | + public function update_option_rewrite_rules( $wp_rules ) { | |
| 404 | + // Check it is called from WP_Rewrite class | |
| 405 | + $debug_backtrace = debug_backtrace(); | |
| 406 | + | |
| 407 | + // Find call from WP_Rewrite class | |
| 408 | + $found = false; | |
| 409 | + foreach ( $debug_backtrace as $trace ) { | |
| 410 | + if ( isset( $trace['class'] ) && $trace['class'] == WP_Rewrite::class ) { | |
| 411 | + $found = true; | |
| 412 | + break; | |
| 413 | + } | |
| 414 | + } | |
| 415 | + | |
| 416 | + if ( ! $found ) { | |
| 417 | + return $wp_rules; | |
| 418 | + } | |
| 419 | + | |
| 420 | + /*static $handled = false; | |
| 421 | + if ( $handled ) { | |
| 422 | + return $wp_rules; | |
| 423 | + } | |
| 424 | + $handled = true;*/ | |
| 425 | + | |
| 426 | + if ( ! is_array( $wp_rules ) ) { | |
| 427 | + return $wp_rules; | |
| 428 | + } | |
| 429 | + | |
| 430 | + try { | |
| 431 | + $lp_rules = $this->add_rewrite_rules(); | |
| 432 | + foreach ( $lp_rules as $key_parent => $rules ) { | |
| 433 | + foreach ( $rules as $key => $rule ) { | |
| 434 | + if ( is_array( $rule ) ) { | |
| 435 | + $wp_rules = array_merge( $rule, $wp_rules ); | |
| 436 | + } | |
| 437 | + } | |
| 438 | + } | |
| 439 | + } catch ( Throwable $e ) { | |
| 440 | + error_log( sprintf( '%s:%s:%s', __FILE__, __LINE__, $e->getMessage() ) ); | |
| 441 | + } | |
| 442 | + | |
| 443 | + return $wp_rules; | |
| 367 | 444 | } |
| 368 | 445 | } |