css class to put on link we build
// $cohort => text id of the moodle cohort in which to enrol this user
// $group => text id of the moodle group in which to enrol this user
// $course => text id of the course, if you just want to enrol a user directly to a course
// $authtext => string containing text content to display when not logged on (defaults to content between tags when empty / missing)
// $activity => index of the first activity to open, if autoopen is enabled in moodle
extract(shortcode_atts(array(
"cohort" => '',
"group" => '',
"course" => '',
"class" => 'wp2moodle',
"target" => '_self',
"authtext" => '',
"activity" => 0
), $atts));
if ($content == null || !is_user_logged_in() ) {
if (trim($authtext) == "") {
$url = '' . do_shortcode($content) . ''; // return content text linked to registration page (value between start and end tag)
} else {
$url = '' . do_shortcode($authtext) . ''; // return authtext linked to registration page (value of attribute, if set)
}
} else {
// url = moodle_url + "?data=" +
$url = ''.do_shortcode($content).''; // hyperlinked content
}
return $url;
}
// wp2m filters are set at higher priority so they execute first
// over-ride the url for Marketpress *if* the download is a file named something-wp2moodle.txt
add_filter('mp_download_url', 'wp2m_download_url', 10, 3);
// over-ride the url for WooCommerce (has various download filters we have to try)
add_filter('woocommerce_download_file_redirect','woo_wp2m_download_url', 5, 2);
add_filter('woocommerce_download_file_force','woo_wp2m_download_url', 5, 2);
add_filter('woocommerce_download_file_xsendfile','woo_wp2m_download_url', 5, 2);
// woo shim to handle different arguments
function woo_wp2m_download_url($filepath, $filename) {
wp2m_download_url($filepath, "", "");
}
// the download file is actually a text file containing the shortcode values
function wp2m_download_url($url, $order, $download) {
if (strpos($url, 'wp2moodle.txt') !== false) {
// mp url is full url = including http:// and so on... we want the file url
$path = $_SERVER['DOCUMENT_ROOT'] . parse_url($url)["path"];
$cohort = "";
$group = "";
$data = file($path); // now it's an array!
foreach ($data as $row) {
$pair = explode("=",$row);
switch (strtolower(trim($pair[0]))) {
case "group":
$group = trim(str_replace(array('\'','"'), '', $pair[1]));
break;
case "cohort":
$group = trim(str_replace(array('\'','"'), '', $pair[1]));
break;
case "course":
$course = trim(str_replace(array('\'','"'), '', $pair[1]));
break;
case "activity":
$activity = trim(str_replace(array('\'','"'), '', $pair[1]));
break;
}
}
$url = wp2moodle_generate_hyperlink($cohort,$group,$course,$activity);
if (ob_get_contents()) { ob_clean(); }
header('Location: ' . $url, true, 301); // redirect to this url
exit();
}
return $url;
}
/*
If you are only using wp2moodle and not offering other marketpress products, you can hide the shipping info like this:
add_action('add_meta_boxes_product','remove_unwanted_mp_meta_boxes',999);
function remove_unwanted_mp_meta_boxes() {
// shipping meta box
remove_meta_box('mp-meta-shipping','product','normal');
// download file box
// remove_meta_box('mp-meta-download','product','normal');
}
*/
/*
* Function to build the encrypted hyperlink
*/
function wp2moodle_generate_hyperlink($cohort,$group,$course,$activity = 0) {
// needs authentication; ensure userinfo globals are populated
global $current_user;
wp_get_current_user();
$update = get_option('wp2m_update_details') ?: "true";
$enc = array(
"offset" => rand(1234,5678), // just some junk data to mix into the encryption
"stamp" => time(), // unix timestamp so we can check that the link isn't expired
"firstname" => $current_user->user_firstname, // first name
"lastname" => $current_user->user_lastname, // last name
"email" => $current_user->user_email, // email
"username" => $current_user->user_login, // username
"passwordhash" => $current_user->user_pass, // hash of password (we don't know/care about the raw password)
"idnumber" => $current_user->ID, // int id of user in this db (for user matching on services, etc)
"cohort" => $cohort, // string containing cohort to enrol this user into
"group" => $group, // string containing group to enrol this user into
"course" => $course, // string containing course id, optional
"updatable" => $update, // if user profile fields can be updated in moodle
"activity" => $activity // index of first [visible] activity to go to, if auto-open is enabled in moodle
);
// encode array as querystring
$details = http_build_query($enc);
// encryption = 3des using shared_secret
return rtrim(get_option('wp2m_moodle_url'),"/").WP2M_MOODLE_PLUGIN_URL.encrypt_string($details, get_option('wp2m_shared_secret'));
//return get_option('wp2m_moodle_url').WP2M_MOODLE_PLUGIN_URL.'=>'.$details;
}
/**
* initialiser for registering scripts to the rich editor
*/
function wp2m_register_addbutton() {
if ( current_user_can('edit_posts') && current_user_can('edit_pages') ) {
add_filter('mce_external_plugins', 'wp2m_add_plugin');
add_filter('mce_buttons', 'wp2m_register_button');
}
}
function wp2m_register_button($buttons) {
array_push($buttons,"|","wp2m"); // pipe = break on toolbar
return $buttons;
}
function wp2m_add_plugin($plugin_array) {
$plugin_array['wp2m'] = plugin_dir_url(__FILE__).'wp2m.js';
return $plugin_array;
}
?>