Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created October 16, 2018 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/3592b816a94f3439997c72177ee82d3e to your computer and use it in GitHub Desktop.
Save wpmudev-sls/3592b816a94f3439997c72177ee82d3e to your computer and use it in GitHub Desktop.
Adds shortcode in Pay Per View for Elementor builder
<?php
/**
* Plugin Name: [Pay Per View] - Elementor Patch
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds shortcode in Pay Per View for Elementor builder
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_PPV_Shortcode' ) ) {
class WPMUDEV_PPV_Shortcode {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_PPV_Shortcode();
}
return self::$_instance;
}
private function __construct() {
add_action( 'the_content', array( $this, 'rm_filter' ), 1 );
add_shortcode( 'ppw', array( $this, 'shortcode' ) );
}
public function rm_filter( $the_content ) {
global $ppw;
remove_filter( 'the_content', array( $ppw, 'content' ), 8 );
return $the_content;
}
public function shortcode( $atts, $content ) {
//global $ppw;
return $this->content( $content );
}
private function content( $content ) {
global $ppw, $post;
$method = false;
$out = '';
// Unsupported post type. Maybe a temporary page, like checkout of MarketPress
if ( ! is_object( $post ) && ! $content ) {
return;
}
// If caching is allowed no need to continue
if ( $ppw->is_cachable && ! $force ) {
return $ppw->clear( $content );
}
// Display the admin full content, if selected so
if ( $ppw->options["admin"] == 'true' && current_user_can( 'administrator' ) ) {
return $ppw->clear( $content );
}
// Display the bot full content, if selected so
if ( $ppw->options["bot"] == 'true' && $ppw->is_bot() ) {
return $ppw->clear( $content );
}
// Check if current user has been authorized to see full content
if ( $ppw->is_authorised() ) {
return $ppw->clear( $content );
}
$is_subscription_valid = $ppw->is_subscription_valid( get_current_user_id() );
// If user has already subscribed content
if ( is_user_logged_in() && $is_subscription_valid ) {
return $ppw->clear( $content );
}
//PDT Integration
if ( is_user_logged_in() && ! empty( $_GET['ppw_paypal_subscribe'] ) && ! empty( $_GET['tx'] ) && ! empty( $_GET['st'] ) ) {
$ppl = $ppw->call_gateway();
//Get Transaction Details
$transaction_details = $ppl->pdt_get_transaction_details( $_GET['tx'], $ppw->options['gateways']['paypal-express']['identity_token'] );
$transaction_details = $ppw->process_pdt_response( $transaction_details );
//Check Status
if ( ! empty( $transaction_details['status'] ) && $transaction_details['status'] == 'success' ) {
//Update Subscription and show the content
$ppw->update_subscription( $transaction_details );
//Show the content
return $ppw->clear( $content );
} else {
//Store Payment pending message in $_SESSION, we can use this to display a message
$_SESSION['ppv_payment_status'] = 'pending';
$_SESSION['ppv_message'] = ! empty( $transaction_details['message'] ) ? $transaction_details['message'] : '';
}
}
// Find method if it is not forced
if ( ! $method && is_object( $post ) ) {
$method = get_post_meta( $post->ID, 'ppw_method', true );
}
// Apply default method, if there is none
if ( ! $method ) {
$method = $ppw->options["method"];
}
//Get content as per the method and permissions
$result = $ppw->subscription_status( $post, $method );
if ( $result ) {
// Visitor did paid for this content!
return $ppw->clear( $content );
}
// Find the price
if ( ! $price = get_post_meta( $post->ID, "ppw_price", true ) ) {
$price = $ppw->options["price"];
} // Apply default price if it is not set for the post/page
if ( $method == "automatic" ) {
if ( ! $excerpt_len = get_post_meta( $post->ID, 'ppw_excerpt', true ) ) {
$excerpt_len = $ppw->options["excerpt"];
}
$out = wp_trim_words( $content, $excerpt_len, '...' );
}
elseif ( $method == "manual" ) {
$out = $post->post_excerpt;
}
elseif ( $method == "tool" ) {
$contents = array();
if ( preg_match_all( '%\[ppw( +)id="(.*?)"( +)description="(.*?)"( +)price="(.*?)"(.*?)\](.*?)\[( *)\/ppw( *)\]%is', $content, $matches, PREG_SET_ORDER ) ) {
if ( isset( $_COOKIE["pay_per_view"] ) ) {
$orders = unserialize( stripslashes( $_COOKIE["pay_per_view"] ) );
if ( is_array( $orders ) ) {
foreach ( $orders as $order ) {
if ( is_object( $post ) && $order["post_id"] == $post->ID ) {
$contents[] = $order["content_id"];
} // Take only values related to this post
}
}
}
// Prepare the content
foreach ( $matches as $m ) {
if ( in_array( $m[2], $contents ) ) {
// This is paid
$content = str_replace( $m[0], $m[8], $content );
} else {
$content = str_replace( $m[0], $ppw->mask( $m[6], $m[2], $m[4] ), $content );
}
}
}
return $ppw->clear( $content );
}
return $out . $ppw->mask( $price );
}
}
if ( ! function_exists( 'wpmudev_ppv_shortcode' ) ) {
function wpmudev_ppv_shortcode(){
return WPMUDEV_PPV_Shortcode::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_ppv_shortcode', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment