| @@ -1,116 +1,40 @@ | ||
| 1 | 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 | 2 | namespace TUTOR; |
| 12 | 3 | |
| 13 | -use Tutor\Cache\TutorCache; | |
| 14 | -use Tutor\Models\CourseModel; | |
| 15 | - | |
| 16 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) | |
| 17 | 5 | exit; |
| 18 | -} | |
| 19 | 6 | |
| 20 | -/** | |
| 21 | - * Register Tutor's post types | |
| 22 | - * | |
| 23 | - * @since 1.0.0 | |
| 24 | - */ | |
| 25 | -class Private_Course_Access extends Tutor_Base { | |
| 26 | 7 | |
| 27 | - /** | |
| 28 | - * Allow empty | |
| 29 | - * | |
| 30 | - * @var boolean | |
| 31 | - */ | |
| 32 | - private $allow_empty = false; | |
| 8 | +class Private_Course_Access { | |
| 9 | + | |
| 10 | + private $allow_empty=false; | |
| 33 | 11 | |
| 34 | - /** | |
| 35 | - * Register hooks | |
| 36 | - * | |
| 37 | - * @since 1.0.0 | |
| 38 | - */ | |
| 39 | - public function __construct() { | |
| 40 | - parent::__construct(); | |
| 12 | + public function __construct() { | |
| 13 | + add_action('pre_get_posts', array($this, 'enable_private_access')); | |
| 14 | + } | |
| 41 | 15 | |
| 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 | - } | |
| 16 | + public function enable_private_access($query=null){ | |
| 45 | 17 | |
| 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 ) { | |
| 18 | + if(!is_admin() && is_user_logged_in() ){ | |
| 54 | 19 | |
| 55 | - if ( ! is_admin() && is_user_logged_in() ) { | |
| 20 | + global $wpdb; | |
| 21 | + $p_name = isset($query->query['name']) ? $query->query['name'] : ''; | |
| 22 | + $p_name = esc_sql($p_name); | |
| 56 | 23 | |
| 57 | - global $wpdb; | |
| 58 | - $p_name = isset( $query->query['name'] ) ? $query->query['name'] : ''; | |
| 59 | - $p_name = esc_sql( $p_name ); | |
| 24 | + if($this->allow_empty && empty($p_name)){ | |
| 25 | + $query->set('post_status', array('private', 'publish')); | |
| 26 | + return; | |
| 27 | + } | |
| 60 | 28 | |
| 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 | -} | |
| 29 | + // Get using raw query to speed up | |
| 30 | + $private_query = "SELECT ID, post_parent FROM {$wpdb->posts} WHERE post_type='courses' AND post_name='{$p_name}' AND post_status='private'"; | |
| 31 | + $result = $wpdb->get_results($private_query); | |
| 32 | + $private_course_id = (is_array($result) && isset($result[0])) ? $result[0]->ID : 0; | |
| 33 | + | |
| 34 | + if($private_course_id>0 && tutils()->is_enrolled($private_course_id)){ | |
| 35 | + $this->allow_empty = true; | |
| 36 | + $query->set('post_status', array('private', 'publish')); | |
| 37 | + } | |
| 38 | + } | |
| 39 | + } | |
| 40 | +} | |