| 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 |
|