Skip to content

Commit

Permalink
+Fixed single course permalink does not work with category slug
Browse files Browse the repository at this point in the history
+Fixed content does not show with Yoast SEO plugin installed
  • Loading branch information
tunn1234 committed Nov 8, 2016
1 parent 25fd4b9 commit a757b14
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 27 deletions.
11 changes: 7 additions & 4 deletions assets/js/frontend/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
//if (!that.get('content')) {
that.set(response.question);
if (response.permalink) {
LP.setUrl(response.permalink);
LP.setUrl(response.permalink);console.log('6666');
console.log(response)
}
//}
$.isFunction(args.complete) && args.complete.call(that, response);
Expand Down Expand Up @@ -558,7 +559,8 @@
this.start();
$(window).trigger('load');
$(document).trigger('resize');
windowTarget.LP.setUrl(question.get('url'));
windowTarget.LP.setUrl(question.get('url'));console.log('5555');
console.log(question)
windowTarget.LP.unblockContent();
},
_showHint : function (e) {
Expand Down Expand Up @@ -634,11 +636,12 @@
if (item.get('id') == this.model.get('id')) {
var questionName = this.model.getCurrent('name'), reg;
if (questionName && this.model.get('status') !== 'completed') {
reg = new RegExp(questionName, '');
reg = new RegExp('!' + questionName + '/?$!', '');
if (!url.match(reg)) {
url = url.replace(/\/$/, '') + '/' + questionName + '/';
}
}

}
return url;
},
Expand Down Expand Up @@ -719,7 +722,7 @@
callback : function (response, item) {
windowTarget.LP.unblockContent();
that.$('#learn-press-content-item').html(response.html.content);
windowTarget.LP.setUrl(that.model.get('permalink'));
windowTarget.LP.setUrl(that.model.get('permalink'));console.log('4444');
var data = response.course_result;
data.messageType = 'update-course';
LP.sendMessage(data, windowTarget);
Expand Down
6 changes: 3 additions & 3 deletions assets/js/frontend/single-course.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ if (typeof LearnPress === 'undefined') {
var passingCondition = parseInt(this.$('.course-progress .lp-course-progress').data('passing-condition'));
this.$('.button-finish-course').toggleClass('hide-if-js', !(data.results >= passingCondition));
if (data.setUrl) {
LP.setUrl(data.setUrl);
LP.setUrl(data.setUrl);console.log('3333');
}
},
getHash : function (url) {
Expand Down Expand Up @@ -550,7 +550,7 @@ if (typeof LearnPress === 'undefined') {
url = LP.Hook.applyFilters('learn_press_get_current_item_url', this.currentItem.get('url'), this.currentItem);
}
if (url) {
LP.setUrl(url);
LP.setUrl(url);console.log('2222');
}
},
blockContent : function () {
Expand Down Expand Up @@ -651,7 +651,7 @@ if (typeof LearnPress === 'undefined') {
},
_closePopup : function (e) {
e.preventDefault();
LP.setUrl(this.course.model.get('url'));
LP.setUrl(this.course.model.get('url'));console.log('1111');
$('#learn-press-content-item').hide();
this.curriculumPlaceholder.replaceWith(this.$('#learn-press-course-curriculum'));
this.progressPlaceholder.replaceWith(this.$('.learn-press-course-results-progress'));
Expand Down
1 change: 1 addition & 0 deletions assets/js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ if (typeof window.LP == 'undefined') {
if (url) {
history.pushState({}, title, url);
LP.Hook.doAction('learn_press_set_location_url', url);
console.log('set url:'+url);
}
},
toggleGroupSection: function (el, target) {
Expand Down
3 changes: 1 addition & 2 deletions inc/class-lp-page-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LP_Page_Controller {
*/
protected $has_filter_content = false;

protected $_filter_content_priority = 10000000;
protected $_filter_content_priority = 10;

/**
* LP_Page_Controller constructor.
Expand Down Expand Up @@ -181,7 +181,6 @@ public function template_loader2( $template ) {
public function single_content( $content ) {
remove_filter( 'the_content', array( $this, 'single_content' ), $this->_filter_content_priority );
add_filter( 'the_content', 'wpautop' );

ob_start();
learn_press_get_template( 'content-single-course.php' );
$content = ob_get_clean();
Expand Down
127 changes: 114 additions & 13 deletions inc/class-lp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct() {
/**
* Parses request params and controls page
*
* @param $q
* @param WP_Query $q
*
* @return mixed
*/
Expand All @@ -44,6 +44,52 @@ public function parse_request( $q ) {
if ( empty( $post_types[$course_type] ) ) {
return;
}
/********************/
if ( empty( $q->query_vars[LP_COURSE_CPT] ) ) {
return;
}
$this->query_vars['course'] = $q->query_vars[LP_COURSE_CPT];
$course_id = learn_press_setup_course_data( $this->query_vars['course'] );
$quiz_name = '';
$question_name = '';
$lesson_name = '';
if ( !empty( $q->query_vars['quiz'] ) ) {
$quiz_name = $this->query_vars['quiz'] = $q->query_vars['quiz'];
$this->query_vars['item_type'] = LP_QUIZ_CPT;
if ( !empty( $q->query_vars['question'] ) ) {
$question_name = $this->query_vars['question'] = $q->query_vars['question'];
}
} elseif ( !empty( $q->query_vars['lesson'] ) ) {
$lesson_name = $this->query_vars['lesson'] = $q->query_vars['lesson'];
$this->query_vars['item_type'] = LP_LESSON_CPT;
}
if ( $quiz_name && $question_name ) {
if ( $quiz = learn_press_get_post_by_name( $quiz_name, LP_QUIZ_CPT ) ) {
if ( $user->has_quiz_status( 'completed', $quiz->ID, $course_id ) ) {
//remove question name from uri
$course = learn_press_get_course( $course_id );
$redirect = $course->get_item_link( $quiz->ID );// get_site_url() . '/' . dirname( $request_match );
wp_redirect( $redirect );
exit();
}
if ( $question = learn_press_get_post_by_name( $question_name, 'lp_question' ) ) {
global $wpdb;
$query = $wpdb->prepare( "
SELECT MAX(user_item_id)
FROM {$wpdb->prefix}learnpress_user_items
WHERE user_id = %d AND item_id = %d AND item_type = %s and status <> %s
", learn_press_get_current_user_id(), $quiz->ID, 'lp_quiz', 'completed' );
if ( $history_id = $wpdb->get_var( $query ) ) {
learn_press_update_user_item_meta( $history_id, 'current_question', $question->ID );
}
}
}
}
$this->query_vars['course_id'] = $course_id;
do_action_ref_array( 'learn_press_parse_query', array( &$this ) );

return $q;
/************************/
$slug = preg_replace( '!^/!', '', $post_types[$course_type]->rewrite['slug'] );

$match = '^' . $slug . '/([^/]*)/(' . $post_types['lp_quiz']->rewrite['slug'] . ')?/([^/]*)?/?([^/]*)?';
Expand All @@ -63,10 +109,12 @@ public function parse_request( $q ) {
$this->query_vars['item_type'] = LP_LESSON_CPT;
}
}

/**
* Match request URI with quiz permalink
*/
if ( preg_match( "#^$match#", $request_match, $matches ) || preg_match( "#^$match#", urldecode( $request_match ), $matches ) ) {

// If request URI is a quiz permalink
if ( !empty( $matches[3] ) ) {
if ( !$post = learn_press_get_post_by_name( $matches[3], 'lp_quiz', true ) ) {
Expand Down Expand Up @@ -178,21 +226,37 @@ function add_rewrite_rules() {
$course_type = 'lp_course';
$post_types = get_post_types( '', 'objects' );
$slug = preg_replace( '!^/!', '', $post_types[$course_type]->rewrite['slug'] );
$has_category = false;
if ( preg_match( '!(%?course_category%?)!', $slug ) ) {
$slug = preg_replace( '!(%?course_category%?)!', '(.+?)/([^/]+)', $slug );
$has_category = true;
}
$current_url = learn_press_get_current_url();
$query_string = str_replace( trailingslashit( get_site_url() ), '', $current_url );

add_rewrite_rule(
'^' . $slug . '/([^/]*)/(' . $post_types['lp_lesson']->rewrite['slug'] . ')?/(.*)?',
'index.php?' . $course_type . '=$matches[1]&lesson=$matches[3]',
'top'
);

/** quiz */
add_rewrite_rule(
'^' . $slug . '/([^/]*)/(' . $post_types['lp_quiz']->rewrite['slug'] . ')?/([^/]*)?/?([^/]*)?',
'index.php?' . $course_type . '=$matches[1]&quiz=$matches[3]&question=$matches[4]',
'top'
);
if ( $has_category ) {
add_rewrite_rule(
'^' . $slug . '(?:/' . $post_types['lp_lesson']->rewrite['slug'] . '/([^/]+))/?$',
'index.php?' . $course_type . '=$matches[2]&course_category=$matches[1]&lesson=$matches[3]',
'top'
);
add_rewrite_rule(
'^' . $slug . '(?:/' . $post_types['lp_quiz']->rewrite['slug'] . '/([^/]+)/?([^/]+)?)/?$',
'index.php?' . $course_type . '=$matches[2]&course_category=$matches[1]&quiz=$matches[3]&question=$matches[4]',
'top'
);
} else {
add_rewrite_rule(
'^' . $slug . '/([^/]+)(?:/' . $post_types['lp_lesson']->rewrite['slug'] . '/([^/]+))/?$',
'index.php?' . $course_type . '=$matches[1]&lesson=$matches[2]',
'top'
);
add_rewrite_rule(
'^' . $slug . '/([^/]+)(?:/' . $post_types['lp_quiz']->rewrite['slug'] . '/([^/]+)/?([^/]+)?)/?$',
'index.php?' . $course_type . '=$matches[1]&quiz=$matches[2]&question=$matches[3]',
'top'
);
}

if ( $profile_id = learn_press_get_page_id( 'profile' ) ) {
add_rewrite_rule(
Expand All @@ -202,6 +266,43 @@ function add_rewrite_rules() {
);
}
do_action( 'learn_press_add_rewrite_rules' );
return;

/**
* Lesson permalink without category
*/
/*add_rewrite_rule(
'^' . $slug . '/([^/]*)/(' . $post_types['lp_lesson']->rewrite['slug'] . ')/([^/]+)/?$',
'index.php?' . $course_type . '=$matches[1]&lesson=$matches[3]',
'top'
);*/

/**
* Quiz permalink with category inside
*/
add_rewrite_rule(
'^course/(.+?)/([^/]+)(?:/' . $post_types['lp_quiz']->rewrite['slug'] . '/([^/]+))/?$',
'index.php?' . $course_type . '=$matches[2]&course_category=$matches[1]&quiz=$matches[3]',
'top'
);

/**
* Lesson permalink without category
*/
add_rewrite_rule(
'^' . $slug . '/([^/]*)/(' . $post_types['lp_quiz']->rewrite['slug'] . ')/([^/]+)/?$',
'index.php?' . $course_type . '=$matches[1]&quiz=$matches[3]',
'top'
);


/*add_rewrite_rule(
'^' . $slug . '/([^/]*)/(' . $post_types['lp_quiz']->rewrite['slug'] . ')?/([^/]*)?/?([^/]*)?',
'index.php?' . $course_type . '=$matches[1]&quiz=$matches[3]&question=$matches[4]',
'top'
);*/


}

/**
Expand Down
4 changes: 4 additions & 0 deletions inc/course/abstract-lp-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ public function get_title() {
return apply_filters( 'learn_press_course_title', $this->post ? $this->post->post_title : '', $this );
}

public function get_description() {
return apply_filters( 'the_content', $this->post->post_content );
}

/**
* Get all curriculum of this course
*
Expand Down
6 changes: 3 additions & 3 deletions inc/custom-post-types/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ public function register() {
'show_in_nav_menus' => true,
'taxonomies' => array( 'course_category', 'course_tag' ),
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'comments', 'excerpt' ),
'hierarchical' => true,
'hierarchical' => false,

This comment has been minimized.

Copy link
@rslonik

rslonik Nov 15, 2016

This change broke the "Duplicate this course" link, because page_row_actions filter is evaluated only for hierarchical posts.

https://developer.wordpress.org/reference/hooks/page_row_actions-4/

I didn't understand whats this commit fixes, thus I don't know if I can change it back to true.

@tunnhn can you help me on this?

'rewrite' => $course_permalink ? array(
'slug' => untrailingslashit( $course_permalink ),
'with_front' => false
'slug' => untrailingslashit( $course_permalink ),
'with_front' => false
) : false
);
return $args;
Expand Down
2 changes: 1 addition & 1 deletion learnpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,4 +668,4 @@ function load_learn_press() {
* Done! entry point of the plugin
* Create new instance of LearnPress and put it to global
*/
$GLOBALS['LearnPress'] = LP();
$GLOBALS['LearnPress'] = LP();
2 changes: 1 addition & 1 deletion templates/single-course/tabs/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<?php do_action( 'learn_press_begin_single_course_description' ); ?>

<?php the_content(); ?>
<?php echo $course->get_description();?>

<?php do_action( 'learn_press_end_single_course_description' ); ?>

Expand Down

0 comments on commit a757b14

Please sign in to comment.