Skip to content

Instantly share code, notes, and snippets.

@robertuniqid
Created April 20, 2020 22:38
Show Gist options
  • Save robertuniqid/b4e295f4f8d1141714b31ed13af76151 to your computer and use it in GitHub Desktop.
Save robertuniqid/b4e295f4f8d1141714b31ed13af76151 to your computer and use it in GitHub Desktop.
eLearnCommerce - Abstract WPEP_Content_Library_Integration
<?php
require_once( WPEP_BASE_PATH . "/lib/integrations/Content_Library/Administration.php" );
abstract class WPEP_Content_Library_Integration {
public $service_name = '';
public $control_access_based_display = [];
public $meta_box_active = false;
public $meta_box_title = "";
public $meta_box_pages = [
WPEP_POST_TYPE_COURSE,
WPEP_POST_TYPE_VIDEO,
WPEP_POST_TYPE_E_BOOK,
WPEP_POST_TYPE_OFFER
];
public $primary_meta_field_label = '';
public $primary_meta_field_key = '';
public $primary_meta_field_desc = '';
public $default_purchase_cta = 'Purchase Access';
public $cta_meta_field_suffix = '_purchase_text';
public $redirect_meta_field_suffix = '_purchase_link';
public $redirect_option_field_suffix = '_sell_button_url';
public $prefix = '';
public $slug = '';
public $options_prefix = '';
public $sell_button_url = '';
public $sell_button_text = '';
private $_access_storage = [];
/**
* @var WPEP_Integration_Content_Library_Coordinator_Administration
*/
public $administrationCoordinator;
public function init() {
if( $this->options_prefix !== '' ) {
$this->sell_button_text = get_option( $this->options_prefix . '_sell_button_text', $this->default_purchase_cta );
$this->sell_button_url = get_option( $this->options_prefix . $this->redirect_option_field_suffix, '' );
if( ! get_option( $this->options_prefix . '_disable_up_sell_functionality', 0 ) )
$this->setup_up_sells();
}
if( !empty( $this->control_access_based_display ) )
$this->setup_access_based_display();
$this->administrationCoordinator = new WPEP_Integration_Content_Library_Coordinator_Administration();
if( is_admin() ) {
$this->administrationCoordinator->init($this);
}
$this->register_library();
}
public function register_library() {
wpep_controller()->contentLibrary->register( $this );
}
/**
* @param $post_id
* @return bool
*/
abstract public function has_access( $post_id );
/**
* This method should work like the has_access method, it offers an user_id parameter, if that user_id is the same as the current_user_id, or not logged it, it will fallback to has_access
* @param $post_id
* @param $user_id
* @return bool
*/
public function user_has_access( $post_id, $user_id ) {
if( !is_user_logged_in() || get_current_user_id() == $user_id )
return $this->has_access( $post_id );
$current_user_id = get_current_user_id();
wp_set_current_user( $user_id );
// Hopefully no fatal errors will happen, as we REALLY want to avoid something like that.
$response = @$this->has_access( $post_id );
wp_set_current_user( $current_user_id );
return $response;
}
/**
* This functionality is, call it, interesting, if the $user_id receives access to $post_id from a different user, it should return that user id.
* If the user has access to the $post_id, it should return that $user_id.
* If the user receives access to the $post_id, it should return the *owner_user_id* that gave him access to post_id.
* And if the $user_id does not access to $post_id, it should still return the $user_id.
* @param $post_id
* @param $user_id
* @return int
*/
public function get_post_access_source_user_id( $post_id, $user_id ) {
return null;
}
/**
* The first array response is considered valid, if your Integration does not offer sub accounts, it should return null;
* If your integration does offer sub-accounts, but this post_id is not protected, it should return null;
* If your integration does offer sub-accounts, and this post_id is protected, it should return an array
* The returned array should be empty if there are no sub accounts attached to any of the memberships that protect this post_id.
* @param $post_id
* @param $owner_user_id
* @return array|null
*/
public function get_sub_account_user_ids_by_post_id( $post_id, $owner_user_id ) {
return null;
}
/**
* @param $owner_user_id
* @return array|null
*/
public function get_sub_account_receiver_user_ids_from_provider_user_id( $owner_user_id ) {
return null;
}
/**
* The first boolean response is considered valid, if your Integration does not offer sub accounts, it should return null;
* If your integration does offer sub-accounts, it should return null;
* If your integration does offer sub-accounts, and this receiver_user_id is receiving access from owner_user_id , it should return true, else, false
* @param $owner_user_id
* @param $receiver_user_id
* @return bool|null
*/
public function has_sub_account_access_from_user_id( $owner_user_id, $receiver_user_id ) {
return null;
}
/**
* The first boolean response is considered valid, if your Integration does not offer sub accounts, it should return null;
* If your integration does offer sub-accounts, but this post_id is not protected, it should return null;
* If your integration does offer sub-accounts, and this post_id is protected, it should return an array
* @param $post_id
* @param $owner_user_id <- The account who should provide the access to the post_id
* @param $receiver_user_id <- The account who should receive access to the post_id from the $owner_user_id
* @return bool|null
*/
public function has_sub_account_access_to_post_id_from_user_id( $post_id, $owner_user_id, $receiver_user_id ) {
return null;
}
public function get_sell_text( $post_id = 0 ) {
$individual_text = get_post_meta( $post_id, $this->prefix . $this->cta_meta_field_suffix, true );
if( $individual_text !== '' && $individual_text !== null )
return $individual_text;
return $this->sell_button_text;
}
public function get_sell_link( $post_id = 0, $default_link = '' ) {
$individual_link = get_post_meta( $post_id, $this->prefix . $this->redirect_meta_field_suffix, true );
if( $individual_link !== '' && $individual_link !== null )
return $individual_link;
if( $this->sell_button_url !== '' && $this->sell_button_url !== null )
return $this->sell_button_url;
return $default_link;
}
public function get_primary_entity_ids_by_post_id( $post_id ) {
return get_post_meta( $post_id, $this->primary_meta_field_key, false );
}
public function get_post_protection_entity_map() {
return [];
}
public function setup_post_protection() {
add_action( 'init', [ $this, '_init_post_protection' ], 30 );
}
/**
* @param $post_id
* @return bool
*/
public function _has_access( $post_id ) {
if( !isset( $this->_access_storage[ $post_id ] ) )
$this->_access_storage[ $post_id ] = $this->has_access( $post_id );
return $this->_access_storage[ $post_id ];
}
public function _init_post_protection() {
$post_id = wpep_get_current_post_id();
if( $post_id == 0 )
return;
if( $this->_has_access( $post_id ) )
return;
$redirect_link = $this->get_sell_link( $post_id, false );
if( $redirect_link === false )
return;
wpep_redirect( $redirect_link );
exit;
}
public function setup_up_sells() {
if( is_callable( [ $this, 'before_grid_posts_query' ] ) ) {
add_action( 'wpep_primary_content_before_grid', [ $this, 'before_grid_posts_query' ] );
add_action( 'wpep_widget_content_before_grid', [ $this, 'before_grid_posts_query' ] );
}
if( is_callable( [ $this, 'after_grid_posts_query' ] ) ) {
add_action( 'wpep_primary_content_after_grid', [ $this, 'after_grid_posts_query' ] );
add_action( 'wpep_widget_content_after_grid', [ $this, 'after_grid_posts_query' ] );
}
add_filter( 'wpep_primary_content_item_link', [ $this, 'item_link' ] , 100, 2 );
add_filter( 'wpep_primary_content_item_link_title',[ $this, 'item_action_text' ], 100, 2 );
add_filter( 'wpep_grid_item_class', [ $this, 'grid_item_class' ], 100, 2 );
add_filter( 'wpep_widget_item_class', [ $this, 'grid_item_class' ], 100, 2 );
}
public function grid_item_class( $class, $post_id ) {
if( !$this->_has_access( $post_id ) )
$class .= ' wpep-shadowed-box';
return $class;
}
public function item_link( $link, $post_id ) {
if( !$this->_has_access( $post_id ) )
return $this->get_sell_link( $post_id, $link );
return $link;
}
public function item_action_text( $text, $post_id ) {
if( !$this->_has_access( $post_id ) )
return $this->get_sell_text( $post_id );
return $text;
}
public function setup_access_based_display() {
if( empty( $this->control_access_based_display ) || wpep_request()->is_request_type( 'admin' ) )
return;
add_action( 'wpep_primary_content_before_grid', [ $this, '_access_based_display_filters_que' ] );
add_action( 'wpep_widget_content_before_grid', [ $this, '_access_based_display_filters_que' ] );
add_action( 'wpep_primary_content_after_grid', [ $this, '_access_based_display_filters_deque' ] );
add_action( 'wpep_widget_content_after_grid', [ $this, '_access_based_display_filters_deque' ] );
}
public function _access_based_display_filters_que() {
add_filter( "pre_get_posts", [ $this, '_access_based_display_pre_get_posts' ], 100 );
}
public function _access_based_display_filters_deque() {
remove_filter( "pre_get_posts", [ $this, '_access_based_display_pre_get_posts' ], 100 );
}
/**
* @param \WP_Query $query
* @return mixed
*/
public function _access_based_display_pre_get_posts( $query ) {
if( !isset( $query->query ) || !isset( $query->query['post_type'] ) )
return $query;
if( !in_array( $query->query['post_type'], $this->control_access_based_display ) )
return $query;
add_filter( 'the_posts', [ $this, '_access_based_display_the_posts' ], 100 );
$query->set( 'suppress_filters', false );
return $query;
}
public function _access_based_display_the_posts( $the_posts ) {
foreach( $the_posts as $the_post_key => $the_post ) {
if( $this->has_access( ( isset( $the_post->ID ) ? $the_post->ID : $the_post ) ) )
continue;
unset( $the_posts[ $the_post_key ] );
}
remove_filter( 'the_posts', [ $this, '_access_based_display_the_posts' ], 100 );
return $the_posts;
}
public function clear_access_storage() {
$this->_access_storage = [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment