Last active
April 14, 2021 19:12
-
-
Save strangerstudios/6d697728d7a5be494422 to your computer and use it in GitHub Desktop.
Sync PMPro Addon Package Page Purchases with WP Courseware Courses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: PMPro Addon Packages for WP Courseware | |
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-addon-packages-for-wp-courseware/ | |
Description: Sync PMPro Addon Package Page Purchases with WP Courseware Courses | |
Version: .1 | |
Author: Stranger Studios | |
Author URI: http://www.strangerstudios.com | |
*/ | |
/* | |
Define which pages give access to which courses. | |
Format is {WP Post ID} => {Course ID} | |
!! Uncomment and edit this array here or add this to a custom plugin. !! | |
*/ | |
//global $pmpro_addon_pages_for_courses; | |
//$pmpro_addon_pages_for_courses = array( | |
// 3366 => 1 | |
//); | |
/* | |
If on a Course, Module, or Unit, make sure they have access. | |
*/ | |
function pmproap_wpcw_template_redirect() | |
{ | |
global $post, $pmpro_addon_pages_for_courses; | |
//make sure both pmpro and wp courseware are loaded | |
if(!function_exists("pmpro_has_membership_access") || !function_exists("WPCW_plugin_hasAdminRights")) | |
return; | |
//lock down units | |
if(!empty($post) && $post->post_type == "course_unit") | |
{ | |
//what is the course for this unit | |
require_once(dirname(__FILE__) . "/../wp-courseware/lib/class_courses_map.inc.php"); | |
$course = new WPCW_CourseMap(); | |
$course->loadDetails_byUnitID($post->ID); | |
$details = $course->getCourseDetails(); | |
$courses2pages = array_flip($pmpro_addon_pages_for_courses); | |
if(!empty($courses2pages[$details->course_id])) | |
{ | |
//if the user doesn't have access to this page, redirect them there | |
if(!pmpro_has_membership_access($courses2pages[$details->course_id])) | |
{ | |
wp_redirect(get_permalink($courses2pages[$details->course_id])); | |
exit; | |
} | |
} | |
} | |
} | |
add_action('template_redirect', 'pmproap_wpcw_template_redirect'); | |
/* | |
When purchasing a page, give them access to the corresponding course. | |
*/ | |
function wpcw_pmproap_action_add_to_package($user_id, $post_id) | |
{ | |
global $pmpro_addon_pages_for_courses; | |
//make sure both pmpro and wp courseware are loaded | |
if(!function_exists("pmpro_has_membership_access") || !function_exists("WPCW_plugin_hasAdminRights")) | |
return; | |
//course for this page? | |
if(!empty($pmpro_addon_pages_for_courses[$post_id])) | |
{ | |
WPCW_courses_syncUserAccess($user_id, array($pmpro_addon_pages_for_courses[$post_id])); | |
} | |
} | |
add_action('pmproap_action_add_to_package', 'wpcw_pmproap_action_add_to_package', 10, 2); | |
/* | |
When losing access to a page, remove access to the corresponding course. | |
*/ | |
function wpcw_pmproap_action_remove_from_package($user_id, $post_id) | |
{ | |
global $pmpro_addon_pages_for_courses; | |
//make sure both pmpro and wp courseware are loaded | |
if(!function_exists("pmpro_has_membership_access") || !function_exists("WPCW_plugin_hasAdminRights")) | |
return; | |
//course for this page? | |
if(!empty($pmpro_addon_pages_for_courses[$post_id])) | |
{ | |
//get all user's courses | |
$courses = WPCW_users_getUserCourseList($user_id); | |
//krumo($courses); | |
//create array of ids minus the one we are removing | |
$course_ids = array(); | |
foreach($courses as $course) | |
{ | |
if($course->course_id != $pmpro_addon_pages_for_courses[$post_id]) | |
$course_ids[] = $course->course_id ; | |
} | |
//sync | |
WPCW_courses_syncUserAccess($user_id, $course_ids); | |
} | |
} | |
add_action('pmproap_action_remove_from_package', 'wpcw_pmproap_action_remove_from_package', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Sell Single Courses using WP Courseware and Paid Memberships Pro" at Paid Memberships Pro here: https://www.paidmembershipspro.com/sell-single-courses-using-wp-courseware-and-paid-memberships-pro/