Addons.php
1 year ago
Admin.php
1 year ago
Ajax.php
1 year ago
Announcements.php
1 year ago
Assets.php
1 year ago
Backend_Page_Trait.php
1 year ago
BaseController.php
1 year ago
Course.php
1 year ago
Course_Embed.php
3 years ago
Course_Filter.php
1 year ago
Course_List.php
1 year ago
Course_Settings_Tabs.php
1 year ago
Course_Widget.php
1 year ago
Custom_Validation.php
3 years ago
Dashboard.php
1 year ago
Earnings.php
1 year ago
FormHandler.php
2 years ago
Frontend.php
1 year ago
Gutenberg.php
1 year ago
Input.php
1 year ago
Instructor.php
1 year ago
Instructors_List.php
1 year ago
Lesson.php
1 year ago
Options_V2.php
1 year ago
Permalink.php
2 years ago
Post_types.php
1 year ago
Private_Course_Access.php
1 year ago
Q_And_A.php
1 year ago
Question_Answers_List.php
3 years ago
Quiz.php
1 year ago
QuizBuilder.php
1 year ago
Quiz_Attempts_List.php
1 year ago
RestAPI.php
2 years ago
Reviews.php
3 years ago
Rewrite_Rules.php
2 years ago
Shortcode.php
1 year ago
Singleton.php
1 year ago
Student.php
1 year ago
Students_List.php
3 years ago
Taxonomies.php
3 years ago
Template.php
1 year ago
Theme_Compatibility.php
3 years ago
Tools.php
1 year ago
Tools_V2.php
1 year ago
Tutor.php
1 year ago
TutorEDD.php
1 year ago
Tutor_Base.php
2 years ago
Tutor_Setup.php
1 year ago
Upgrader.php
1 year ago
User.php
1 year ago
Utils.php
1 year ago
Video_Stream.php
3 years ago
WhatsNew.php
2 years ago
Withdraw.php
1 year ago
Withdraw_Requests_List.php
1 year ago
WooCommerce.php
1 year ago
Private_Course_Access.php
117 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Private Course Access |
| 4 | * |
| 5 | * @package Tutor\Course |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace TUTOR; |
| 12 | |
| 13 | use Tutor\Cache\TutorCache; |
| 14 | use Tutor\Models\CourseModel; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Register Tutor's post types |
| 22 | * |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class Private_Course_Access extends Tutor_Base { |
| 26 | |
| 27 | /** |
| 28 | * Allow empty |
| 29 | * |
| 30 | * @var boolean |
| 31 | */ |
| 32 | private $allow_empty = false; |
| 33 | |
| 34 | /** |
| 35 | * Register hooks |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | parent::__construct(); |
| 41 | |
| 42 | add_action( 'pre_get_posts', array( $this, 'enable_private_access' ) ); |
| 43 | add_filter( 'post_type_link', array( $this, 'permalink_for_private_course' ), 10, 2 ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Enable private course access |
| 48 | * |
| 49 | * @param mixed $query query. |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | public function enable_private_access( $query = null ) { |
| 54 | |
| 55 | if ( ! is_admin() && is_user_logged_in() ) { |
| 56 | |
| 57 | global $wpdb; |
| 58 | $p_name = isset( $query->query['name'] ) ? $query->query['name'] : ''; |
| 59 | $p_name = esc_sql( $p_name ); |
| 60 | |
| 61 | if ( $this->allow_empty && empty( $p_name ) ) { |
| 62 | $query->set( 'post_status', array( 'private', 'publish' ) ); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // Get using raw query to speed up. |
| 67 | $course_post_type = tutor()->course_post_type; |
| 68 | |
| 69 | // Get data from cache. |
| 70 | $cache_key = "tutor_private_query_{$course_post_type}_{$p_name}"; |
| 71 | $result = TutorCache::get( $cache_key ); |
| 72 | |
| 73 | if ( false === $result ) { |
| 74 | $private_query = $wpdb->prepare( |
| 75 | "SELECT ID, post_parent |
| 76 | FROM {$wpdb->posts} |
| 77 | WHERE post_type = %s |
| 78 | AND post_name = %s |
| 79 | AND post_status = %s |
| 80 | ", |
| 81 | $course_post_type, |
| 82 | $p_name, |
| 83 | 'private' |
| 84 | ); |
| 85 | $result = $wpdb->get_results( $private_query ); //phpcs:ignore |
| 86 | // Set cache data. |
| 87 | TutorCache::set( $cache_key, $result ); |
| 88 | } |
| 89 | |
| 90 | $private_course_id = ( is_array( $result ) && isset( $result[0] ) ) ? $result[0]->ID : 0; |
| 91 | |
| 92 | if ( $private_course_id > 0 && tutor_utils()->is_enrolled( $private_course_id ) ) { |
| 93 | $this->allow_empty = true; |
| 94 | $query->set( 'post_status', array( 'private', 'publish' ) ); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Make permalink for private course. |
| 101 | * |
| 102 | * @since 3.0.0 |
| 103 | * |
| 104 | * @param string $url permalink. |
| 105 | * @param WP_Post $post post object. |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | public function permalink_for_private_course( $url, $post ) { |
| 110 | if ( CourseModel::STATUS_PRIVATE === $post->post_status && $this->course_post_type === $post->post_type ) { |
| 111 | $url = home_url( $this->course_base_permalink . '/' . $post->post_name ); |
| 112 | } |
| 113 | |
| 114 | return $url; |
| 115 | } |
| 116 | } |
| 117 |