Created
February 12, 2018 19:45
-
-
Save codeiscode-dev/fad324bf2a8d10ba0c261aa064bcfd82 to your computer and use it in GitHub Desktop.
WPEP_Content_Library_Integration
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 | |
require_once( WPEP_BASE_PATH . "/lib/integrations/Content_Library/Administration.php" ); | |
abstract class WPEP_Content_Library_Integration { | |
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 ); | |
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 ); | |
} | |
/** | |
* If someone says you don't have access, well, then you don't. | |
* @param $has_access | |
* @param $post_id | |
* @return bool | |
*/ | |
public function _has_post_access( $has_access, $post_id ) { | |
if( !$has_access ) | |
return false; | |
return $this->_has_access( $post_id ); | |
} | |
/** | |
* @param $post_id | |
* @return bool | |
*/ | |
private 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; | |
wp_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' ] ); | |
} | |
if( !in_array( WPEP_POST_TYPE_COURSE, $this->control_access_based_display ) ) { | |
add_filter( 'wpep_course_link', [ $this, 'item_link' ] , 100, 2 ); | |
add_filter( 'wpep_text_view_course', [ $this, 'item_action_text' ], 100, 2 ); | |
} | |
if( !in_array( WPEP_POST_TYPE_E_BOOK, $this->control_access_based_display ) ) { | |
add_filter( 'wpep_ebook_link', [ $this, 'item_link' ] , 100, 2 ); | |
add_filter( 'wpep_text_download_ebook', [ $this, 'item_action_text' ], 100, 2 ); | |
} | |
if( !in_array( WPEP_POST_TYPE_VIDEO, $this->control_access_based_display ) ) { | |
add_filter( 'wpep_video_link', [ $this, 'item_link' ] , 100, 2 ); | |
add_filter( 'wpep_text_view_video', [ $this, 'item_action_text' ], 100, 2 ); | |
} | |
if( !in_array( WPEP_POST_TYPE_OFFER, $this->control_access_based_display ) ) { | |
add_filter( 'wpep_offer_link', [ $this, 'item_link' ] , 100, 2 ); | |
add_filter( 'wpep_text_view_offer', [ $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( is_admin() && !( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) | |
return; | |
if( empty( $this->control_access_based_display ) ) | |
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( $the_post->ID ) ) | |
continue; | |
unset( $the_posts[ $the_post_key ] ); | |
} | |
remove_filter( 'the_posts', [ $this, '_access_based_display_the_posts' ], 100 ); | |
return $the_posts; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment