| 1 |
<?php |
| 2 |
|
| 3 |
class WPC_Shortcodes { |
| 4 |
|
| 5 |
public $user_id; |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
$this->user_id = get_current_user_id(); |
| 9 |
|
| 10 |
add_shortcode('wpc_courses', array($this, 'renderAjaxView')); |
| 11 |
add_shortcode( 'courses', array($this, 'renderAjaxView') ); // legacy shortcode |
| 12 |
add_shortcode('wpc_profile', array($this, 'profilePage')); |
| 13 |
add_shortcode('lesson_count', array($this, 'lessonCount')); |
| 14 |
add_shortcode('course_count', array($this, 'courseCount')); |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
function lessonCount(){ |
| 19 |
$args = array( |
| 20 |
'post_type' => 'lesson', |
| 21 |
'post_status' => 'publish', |
| 22 |
'posts_per_page' => -1 |
| 23 |
); |
| 24 |
$query = new WP_Query($args); |
| 25 |
return $query->post_count; |
| 26 |
} |
| 27 |
|
| 28 |
function courseCount(){ |
| 29 |
$args = array( |
| 30 |
'post_type' => 'course', |
| 31 |
'post_status' => 'publish', |
| 32 |
'posts_per_page' => -1 |
| 33 |
); |
| 34 |
$query = new WP_Query($args); |
| 35 |
return $query->post_count; |
| 36 |
} |
| 37 |
|
| 38 |
function renderAjaxView() { |
| 39 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 40 |
|
| 41 |
if( is_plugin_active( 'wp-courses-woocommerce/wp-courses-woocommerce.php') !== false ) { |
| 42 |
return 'Courses have moved.' . '<br><a class="wpc-btn" href="' . get_post_type_archive_link( 'course' ) . '">' . __('View Courses', 'wp-courses') . '</a>'; |
| 43 |
} |
| 44 |
|
| 45 |
$logged_in = is_user_logged_in() === true ? 'true' : 'false'; ?> |
| 46 |
|
| 47 |
<?php ob_start(); ?> |
| 48 |
|
| 49 |
<div id="wpc-course-app" class="wpc-main"></div> |
| 50 |
|
| 51 |
<script type="text/javascript"> |
| 52 |
jQuery(document).ready(function(){ |
| 53 |
new WPC_UI({ |
| 54 |
'initView' : 'course-archive', |
| 55 |
'loggedIn' : <?php echo $logged_in; ?>, |
| 56 |
'userID' : <?php echo get_current_user_id(); ?>, |
| 57 |
ajaxLinks : true, |
| 58 |
fixedToolbar : <?php echo get_option('wpc_fix_toolbar_top') == 'true' ? 'true' : 'false'; ?>, |
| 59 |
fixedToolbarOffset : <?php echo get_option('wpc_fixed_toolbar_offset') == 'true' ? 'true' : 'false'; ?>, |
| 60 |
adminBar : <?php echo is_admin_bar_showing() === true ? 'true' : 'false'; ?>, |
| 61 |
}); |
| 62 |
}); |
| 63 |
</script> |
| 64 |
|
| 65 |
<?php if(current_user_can('administrator')) { |
| 66 |
$post_id = get_the_ID(); |
| 67 |
wpc_front_end_editor($post_id); // FEE injected via [wpc_courses] and [courses] shortcode |
| 68 |
} ?> |
| 69 |
|
| 70 |
<?php return ob_get_clean(); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
function profilePage($atts){ |
| 75 |
|
| 76 |
ob_start(); |
| 77 |
|
| 78 |
$args = shortcode_atts( array( |
| 79 |
'user_id' => get_current_user_id(), |
| 80 |
), $atts ); |
| 81 |
|
| 82 |
if(!is_user_logged_in() && !isset($args['user_id'])){ |
| 83 |
return '<div class="wpc-msg">' . esc_html__('You must be logged in to view your profile', 'wp-courses') . '.</div>'; |
| 84 |
} ?> |
| 85 |
|
| 86 |
<div id="wpc-profile-page"></div> |
| 87 |
|
| 88 |
<script> |
| 89 |
jQuery(document).ready(function($){ |
| 90 |
var userID = <?php echo (int) $args['user_id']; ?>; |
| 91 |
|
| 92 |
// Used to overwrite window.wpcd.user.ID etc. in ui.js |
| 93 |
// And to load the profile window |
| 94 |
new WPC_UI({ |
| 95 |
loggedIn : true, |
| 96 |
userID : userID, |
| 97 |
onLoad : false, |
| 98 |
ajaxLinks : false, |
| 99 |
}); |
| 100 |
}); |
| 101 |
</script> |
| 102 |
|
| 103 |
<?php return ob_get_clean(); ?> |
| 104 |
|
| 105 |
<?php } |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
add_action('init', 'wpc_shortcodes_init'); |
| 110 |
|
| 111 |
function wpc_shortcodes_init(){ |
| 112 |
new WPC_Shortcodes(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
function wpc_get_main_shortcode_page_url() |
| 117 |
{ |
| 118 |
$pages = get_pages(); |
| 119 |
$pattern = get_shortcode_regex(array('wpc_courses')); |
| 120 |
|
| 121 |
foreach ($pages as $page) { |
| 122 |
if ( |
| 123 |
preg_match_all('/' . $pattern . '/s', $page->post_content, $matches) |
| 124 |
&& array_key_exists(2, $matches) |
| 125 |
&& in_array('wpc_courses', $matches[2]) |
| 126 |
) { |
| 127 |
return get_permalink( $page->ID ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
function wpc_get_courses_hash($params) |
| 133 |
{ |
| 134 |
$paramStr = '?'; |
| 135 |
$index = 0; |
| 136 |
|
| 137 |
foreach ($params as $key => $value) { |
| 138 |
$paramStr .= ($index + 1) <= count($params) && $index !== 0 ? '&' : ''; |
| 139 |
$paramStr .= $key . '=' . $value; |
| 140 |
|
| 141 |
$index++; |
| 142 |
} |
| 143 |
|
| 144 |
return '#' . base64_encode($paramStr); |
| 145 |
} |