| 1 |
<?php |
| 2 |
namespace TUTOR; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) |
| 5 |
exit; |
| 6 |
|
| 7 |
|
| 8 |
class Private_Course_Access { |
| 9 |
|
| 10 |
private $allow_empty=false; |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
add_action('pre_get_posts', array($this, 'enable_private_access')); |
| 14 |
} |
| 15 |
|
| 16 |
public function enable_private_access($query=null){ |
| 17 |
|
| 18 |
if(!is_admin() && is_user_logged_in() ){ |
| 19 |
|
| 20 |
global $wpdb; |
| 21 |
$p_name = isset($query->query['name']) ? $query->query['name'] : ''; |
| 22 |
$p_name = esc_sql($p_name); |
| 23 |
|
| 24 |
if($this->allow_empty && empty($p_name)){ |
| 25 |
$query->set('post_status', array('private', 'publish')); |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 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 && tutor_utils()->is_enrolled($private_course_id)){ |
| 35 |
$this->allow_empty = true; |
| 36 |
$query->set('post_status', array('private', 'publish')); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
} |