Advertisement
designbymerovingi

myCRED Sell Content Add-on: With bbPress Support

Jan 24th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 46.42 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Addon: Sell Content
  4.  * Addon URI: http://mycred.me/add-ons/sell-content/
  5.  * Version: 1.2.1
  6.  * Description: This add-on allows you to sell posts, pages or any public post types on your website. You can either sell the entire content or using our shortcode, sell parts of your content allowing you to offer "teasers".
  7.  * Author: Gabriel S Merovingi
  8.  * Author URI: http://www.merovingi.com
  9.  */
  10. // Translate Header (by Dan bp-fr)
  11. $mycred_addon_header_translate = array(
  12.     __( 'Sell Content', 'mycred' ),
  13.     __( 'This add-on allows you to sell posts, pages or any public post types on your website. You can either sell the entire content or using our shortcode, sell parts of your content allowing you to offer "teasers".', 'mycred' )
  14. );
  15.  
  16. if ( !defined( 'myCRED_VERSION' ) ) exit;
  17.  
  18. define( 'myCRED_SELL',         __FILE__ );
  19. define( 'myCRED_SELL_VERSION', myCRED_VERSION . '.1' );
  20. /**
  21.  * myCRED_Sell_Content class
  22.  *
  23.  *
  24.  * @since 0.1
  25.  * @version 1.0
  26.  */
  27. if ( !class_exists( 'myCRED_Sell_Content' ) ) {
  28.     class myCRED_Sell_Content extends myCRED_Module {
  29.  
  30.         /**
  31.          * Construct
  32.          */
  33.         function __construct() {
  34.             parent::__construct( 'myCRED_Sell_Content', array(
  35.                 'module_name' => 'sell_content',
  36.                 'register'    => false,
  37.                 'defaults'    => array(
  38.                     'post_types'  => 'post,page',
  39.                     'pay'         => 'none',
  40.                     'pay_percent' => 100,
  41.                     'templates'   => array(
  42.                         'members'     => __( '<p>Buy this %post_type% for only %price% %buy_button%</p>', 'mycred' ),
  43.                         'visitors'    => __( '<p><a href="%login_url_here%">Login</a> to buy access to this %post_type%.</p>', 'mycred' ),
  44.                         'cantafford'  => __( "<p>You do not have enough %plural% to buy access to this %post_type%.</p>\n<p><strong>Price</strong>: %price%</p>", 'mycred' )
  45.                     ),
  46.                     'defaults'    => array(
  47.                         'price'                 => 10,
  48.                         'overwrite_price'       => 0,
  49.                         'button_label'          => __( 'Buy Now', 'mycred' ),
  50.                         'overwrite_buttonlabel' => 0,
  51.                         'expire'                => 0
  52.                     ),
  53.                     'logs'        => array(
  54.                         'buy'  => __( 'Purchase of %link_with_title%', 'mycred' ),
  55.                         'sell' => __( 'Sale of %link_with_title%', 'mycred' )
  56.                     )
  57.                 ),
  58.                 'add_to_core' => true
  59.             ) );
  60.  
  61.             add_filter( 'mycred_email_before_send', array( $this, 'email_notices' ), 10, 2 );
  62.         }
  63.  
  64.         /**
  65.          * Load
  66.          * @since 0.1
  67.          * @version 1.1
  68.          */
  69.         public function module_init() {
  70.             $this->make_purchase();
  71.             $this->exp_title = apply_filters( 'mycred_sell_exp_title', __( 'Hours', 'mycred' ) );
  72.  
  73.             add_filter( 'the_content',                array( $this, 'the_content' ), 30       );
  74.            
  75.             add_shortcode( 'mycred_sell_this',        array( $this, 'render_shortcode' )      );
  76.             add_shortcode( 'mycred_sell_this_ajax',   array( $this, 'render_ajax_shortcode' ) );
  77.             add_shortcode( 'mycred_sales_history',    array( $this, 'render_sales_history' )  );
  78.  
  79.             add_action( 'add_meta_boxes',             array( $this, 'add_metabox' )           );
  80.             add_action( 'save_post',                  array( $this, 'save_metabox' )          );
  81.  
  82.             add_action( 'mycred_admin_enqueue',       array( $this, 'admin_enqueue' )         );
  83.             add_action( 'mycred_front_enqueue',       array( $this, 'front_enqueue' )         );
  84.             add_action( 'wp_footer',                  array( $this, 'footer' )                );
  85.             add_action( 'wp_ajax_mycred-buy-content', array( $this, 'make_purchase_ajax' )    );
  86.         }
  87.  
  88.         /**
  89.          * Make Purchase
  90.          * @since 0.1
  91.          * @version 1.0
  92.          */
  93.         public function make_purchase() {
  94.             global $mycred_content_purchase;
  95.  
  96.             $mycred_content_purchase = false;
  97.             if ( !$this->is_installed() ) return;
  98.             if ( !isset( $_POST['mycred_purchase_token'] ) || !isset( $_POST['mycred_purchase'] ) || !isset( $_POST['mycred_purchase']['action'] ) || !isset( $_POST['mycred_purchase']['author'] ) || !isset( $_POST['mycred_purchase']['post_id'] ) || !isset( $_POST['mycred_purchase']['post_type'] ) || !isset( $_POST['mycred_purchase']['user_id'] ) ) return;
  99.             if ( !wp_verify_nonce( $_POST['mycred_purchase_token'], 'buy-content' ) ) return;
  100.  
  101.             $action = $_POST['mycred_purchase']['action'];
  102.             $post_id = $_POST['mycred_purchase']['post_id'];
  103.             $post_type = $_POST['mycred_purchase']['post_type'];
  104.             $user_id = $_POST['mycred_purchase']['user_id'];
  105.             $author = $_POST['mycred_purchase']['author'];
  106.  
  107.             $sell_content = $this->sell_content;
  108.             $prefs = $this->get_sale_prefs( $post_id );
  109.  
  110.             $request = compact( 'action', 'post_id', 'user_id', 'author', 'post_type', 'sell_content', 'prefs' );
  111.             do_action( 'mycred_sell_content_purchase_request', $request );
  112.  
  113.             if ( is_user_logged_in() && !$this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
  114.                 // Charge
  115.                 $log = $sell_content['logs']['buy'];
  116.                 $data = array(
  117.                     'ref_type'    => 'post',
  118.                     'purchase_id' => 'TXID' . date_i18n( 'U' ),
  119.                     'seller'      => $author
  120.                 );
  121.                 $this->core->add_creds( 'buy_content', $user_id, 0-$prefs['price'], $log, $post_id, $data );
  122.  
  123.                 do_action( 'mycred_sell_content_purchase_ready', $request );
  124.  
  125.                 // Pay
  126.                 if ( $sell_content['pay'] == 'author' ) {
  127.                     $content_price = $prefs['price'];
  128.                     // If we are paying the author less then 100%
  129.                     if ( (int) $sell_content['pay_percent'] != 100 ) {
  130.                         $percent = (int) $sell_content['pay_percent']/100;
  131.                         $price = $percent*$content_price;
  132.                         $content_price = number_format( $price, $this->core->format['decimals'], '.', '' );
  133.                     }
  134.                     $log = $sell_content['logs']['sell'];
  135.                     $data = array(
  136.                         'ref_type'    => 'post',
  137.                         'purchase_id' => 'TXID' . date_i18n( 'U' ),
  138.                         'buyer'       => $user_id
  139.                     );
  140.                     $this->core->add_creds( 'buy_content', $author, $content_price, $log, $post_id, $data );
  141.                 }
  142.  
  143.                 $mycred_content_purchase = true;
  144.                 do_action( 'mycred_sell_content_payment_complete', $request );
  145.             }
  146.         }
  147.  
  148.         /**
  149.          * Make Purchase AJAX
  150.          * @since 1.1
  151.          * @version 1.0
  152.          */
  153.         public function make_purchase_ajax() {
  154.             // We must be logged in
  155.             if ( !is_user_logged_in() ) die();
  156.  
  157.             // Security
  158.             check_ajax_referer( 'mycred-buy-content-ajax', 'token' );
  159.  
  160.             // Prep
  161.             $post_id = $_POST['postid'];
  162.             $user_id = get_current_user_id();
  163.             $action = 'buy-content-ajax';
  164.  
  165.             $sell_content = $this->sell_content;
  166.             $prefs = $this->get_sale_prefs( $post_id );
  167.  
  168.             if ( !$this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
  169.                 $post = get_post( $post_id );
  170.                 $author = $post->post_author;
  171.                 $post_type = $post->post_type;
  172.  
  173.                 // Charge
  174.                 $log = $sell_content['logs']['buy'];
  175.                 $data = array(
  176.                     'ref_type'    => 'post',
  177.                     'purchase_id' => 'TXID' . date_i18n( 'U' ),
  178.                     'seller'      => $author
  179.                 );
  180.                 $this->core->add_creds( 'buy_content', $user_id, 0-$prefs['price'], $log, $post_id, $data );
  181.  
  182.                 $request = compact( 'action', 'post_id', 'user_id', 'author', 'post_type', 'sell_content', 'prefs' );
  183.                 do_action( 'mycred_sell_content_purchase_ready', $request );
  184.  
  185.                 // Pay
  186.                 if ( $sell_content['pay'] == 'author' ) {
  187.                     $content_price = $prefs['price'];
  188.                     // If we are paying the author less then 100%
  189.                     if ( (int) $sell_content['pay_percent'] != 100 ) {
  190.                         $percent = (int) $sell_content['pay_percent']/100;
  191.                         $price = $percent*$content_price;
  192.                         $content_price = number_format( $price, $this->core->format['decimals'], '.', '' );
  193.                     }
  194.                     $log = $sell_content['logs']['sell'];
  195.                     $data = array(
  196.                         'ref_type'    => 'post',
  197.                         'purchase_id' => 'TXID' . date_i18n( 'U' ),
  198.                         'buyer'       => $user_id
  199.                     );
  200.                     $this->core->add_creds( 'buy_content', $author, $content_price, $log, $post_id, $data );
  201.                 }
  202.  
  203.                 // $match[1] = start tag, $match[2] = settings, $match[3] = content, $match[4] = end tag
  204.                 preg_match( "'(\[mycred_sell_this_ajax(.{1,})\])(.*?)(\[\/mycred_sell_this_ajax\])'si", $post->post_content, $match );
  205.  
  206.                 // Filter content before returning
  207.                 $content = apply_filters( 'the_content', $match[3] );
  208.                 $content = str_replace( ']]>', ']]&gt;', $content );
  209.                 $content = do_shortcode( $content );
  210.             }
  211.             // Someone is trying to make a purchase but not allowed
  212.             else {
  213.                 $content = '<p>' . __( 'You can not buy this content.', 'mycred' ) . '</p>';
  214.             }
  215.  
  216.             die( $content );
  217.         }
  218.  
  219.         /**
  220.          * Enqueue Admin
  221.          * @since 0.1
  222.          * @version 1.0
  223.          */
  224.         public function admin_enqueue() {
  225.             wp_register_style(
  226.                 'mycred-buy-edit',
  227.                 plugins_url( 'css/edit.css', myCRED_SELL ),
  228.                 false,
  229.                 myCRED_SELL_VERSION . '.1',
  230.                 'all'
  231.             );
  232.  
  233.             $screen = get_current_screen();
  234.             $sell_content = $this->sell_content;
  235.             $post_types = $sell_content['post_types'];
  236.             if ( !empty( $post_types ) ) {
  237.                 $pts = explode( ',', $post_types );
  238.                 if ( !in_array( $screen->id , $pts ) ) return;
  239.  
  240.                 wp_enqueue_style( 'mycred-buy-edit' );
  241.             }
  242.         }
  243.  
  244.         /**
  245.          * Enqueue Front
  246.          * @since 1.1
  247.          * @version 1.0
  248.          */
  249.         public function front_enqueue() {
  250.             global $mycred_buy_content;
  251.  
  252.             wp_register_script(
  253.                 'mycred-buy-content',
  254.                 plugins_url( 'js/buy-content.js', myCRED_SELL ),
  255.                 array( 'jquery' ),
  256.                 myCRED_SELL_VERSION . '.1',
  257.                 true
  258.             );
  259.         }
  260.  
  261.         /**
  262.          * Footer
  263.          * @since 1.1
  264.          * @version 1.0
  265.          */
  266.         public function footer() {
  267.             global $mycred_buy_content;
  268.  
  269.             if ( $mycred_buy_content === true ) {
  270.                 wp_localize_script(
  271.                     'mycred-buy-content',
  272.                     'myCREDsell',
  273.                     array(
  274.                         'ajaxurl' => admin_url( 'admin-ajax.php' ),
  275.                         'working' => __( 'Processing...', 'mycred' ),
  276.                         'error'   => __( 'Error. Try Again', 'mycred' ),
  277.                         'token'   => wp_create_nonce( 'mycred-buy-content-ajax' )
  278.                     )
  279.                 );
  280.                 wp_enqueue_script( 'mycred-buy-content' );
  281.             }
  282.         }
  283.  
  284.         /**
  285.          * Settings Page
  286.          * @since 0.1
  287.          * @version 1.1
  288.          */
  289.         public function after_general_settings() {
  290.             $sell_content = $this->sell_content;
  291.             if ( !isset( $sell_content['defaults']['expire'] ) )
  292.                 $sell_content['defaults']['expire'] = 0;
  293.  
  294.             $before = $this->core->before;
  295.             $after = $this->core->after;
  296.  
  297.             $payees = array(
  298.                 'none'   => __( 'No Payout. Just charge.' ),
  299.                 'author' => __( 'Pay Content Author.' )
  300.             );
  301.             $available_payees = apply_filters( 'mycred_sell_content_payees', $payees, $sell_content ); ?>
  302.  
  303.                 <h4><div class="icon icon-active"></div><?php _e( 'Sell Content', 'mycred' ); ?></h4>
  304.                 <div class="body" style="display:none;">
  305.                     <label class="subheader" for="<?php echo $this->field_id( 'post_types' ); ?>"><?php _e( 'Post Types', 'mycred' ); ?></label>
  306.                     <ol id="myCRED-buy-postypes">
  307.                         <li>
  308.                             <div class="h2"><input type="text" name="<?php echo $this->field_name( 'post_types' ); ?>" id="<?php echo $this->field_id( 'post_types' ); ?>" value="<?php echo $sell_content['post_types']; ?>" class="long" /></div>
  309.                             <span class="description"><?php _e( 'Comma separated list of post types that can be sold.', 'mycred' ); ?></span>
  310.                         </li>
  311.                     </ol>
  312.                     <label class="subheader"><?php _e( 'Payments', 'mycred' ); ?></label>
  313.                     <ol id="myCRED-buy-payments">
  314. <?php
  315.             if ( !empty( $available_payees ) ) {
  316.                 foreach ( $available_payees as $key => $description ) { ?>
  317.  
  318.                         <li>
  319.                             <input type="radio" name="<?php echo $this->field_name( 'pay' ); ?>" id="<?php echo $this->field_id( array( 'pay' => $key ) ); ?>" <?php checked( $sell_content['pay'], $key ); ?> value="<?php echo $key; ?>" />
  320.                             <label for="<?php echo $this->field_id( array( 'pay' => $key ) ); ?>"><?php echo $description; ?></label>
  321.                         </li>
  322. <?php
  323.                     if ( $key == 'author' ) { ?>
  324.  
  325.                         <li>
  326.                             <label for="<?php echo $this->field_id( 'pay_percent' ); ?>"><?php _e( 'Percentage to pay Author', 'mycred' ); ?></label>
  327.                             <div class="h2"><input type="text" size="5" maxlength="3" name="<?php echo $this->field_name( 'pay_percent' ); ?>" id="<?php echo $this->field_id( 'pay_percent' ); ?>" value="<?php echo $sell_content['pay_percent']; ?>" /> %</div>
  328.                             <span class="description"><?php _e( 'Percentage of the price to pay the author. Can not be zero and is ignored if authors are not paid.', 'mycred' ); ?></span>
  329.                         </li>
  330. <?php
  331.                     }
  332.                 }
  333.             } ?>
  334.  
  335.                     </ol>
  336.                     <label class="subheader"><?php _e( 'Defaults', 'mycred' ); ?></label>
  337.                     <ol id="myCRED-buy-defaults">
  338.                         <li>
  339.                             <label for="<?php echo $this->field_id( array( 'defaults' => 'price' ) ); ?>"><?php _e( 'Price', 'mycred' ); ?></label>
  340.                             <div class="h2"><?php echo $before; ?> <input type="text" name="<?php echo $this->field_name( array( 'defaults' => 'price' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'price' ) ); ?>" value="<?php echo $sell_content['defaults']['price']; ?>" size="8" /> <?php echo $after; ?></div>
  341.                         </li>
  342.                         <li>
  343.                             <input type="checkbox" name="<?php echo $this->field_name( array( 'defaults' => 'overwrite_price' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'overwrite_price' ) ); ?>" <?php checked( $sell_content['defaults']['overwrite_price'], 1 ); ?> value="1" />
  344.                             <label for="<?php echo $this->field_id( array( 'defaults' => 'overwrite_price' ) ); ?>"><?php _e( 'Allow authors to change price.', 'mycred' ); ?></label>
  345.                         </li>
  346.                         <li class="empty">&nbsp;</li>
  347.                         <li>
  348.                             <label for="<?php echo $this->field_id( array( 'defaults' => 'button_label' ) ); ?>"><?php _e( 'Button Label', 'mycred' ); ?></label>
  349.                             <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'defaults' => 'button_label' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'button_label' ) ); ?>" value="<?php echo $sell_content['defaults']['button_label']; ?>" size="12" /></div>
  350.                         </li>
  351.                         <li>
  352.                             <input type="checkbox" name="<?php echo $this->field_name( array( 'defaults' => 'overwrite_buttonlabel' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'overwrite_buttonlabel' ) ); ?>" <?php checked( $sell_content['defaults']['overwrite_buttonlabel'], 1 ); ?> value="1" />
  353.                             <label for="<?php echo $this->field_id( array( 'defaults' => 'overwrite_buttonlabel' ) ); ?>"><?php _e( 'Allow authors to change button label.', 'mycred' ); ?></label>
  354.                         </li>
  355.                         <li class="empty">&nbsp;</li>
  356.                         <li>
  357.                             <label for="<?php echo $this->field_id( array( 'defaults' => 'expire' ) ); ?>"><?php _e( 'Purchases expire after', 'mycred' ); ?></label>
  358.                             <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'defaults' => 'expire' ) ); ?>" id="<?php echo $this->field_id( array( 'defaults' => 'expire' ) ); ?>" value="<?php echo $sell_content['defaults']['expire']; ?>" size="6" /> <?php echo $this->exp_title; ?></div>
  359.                             <span class="description"><?php _e( 'Use zero for permanent sales.', 'mycred' ); ?></span>
  360.                         </li>
  361.                     </ol>
  362.                     <label class="subheader" for="<?php echo $this->field_id( array( 'templates' => 'visitors' ) ); ?>"><?php _e( 'Sale Template for non members', 'mycred' ); ?></label>
  363.                     <ol id="myCRED-buy-template-visitors">
  364.                         <li>
  365.                             <textarea rows="10" cols="50" name="<?php echo $this->field_name( array( 'templates' => 'visitors' ) ); ?>" id="<?php echo $this->field_id( array( 'templates' => 'visitors' ) ); ?>" class="large-text code"><?php echo $sell_content['templates']['visitors']; ?></textarea>
  366.                             <span class="description"><?php _e( 'Do <strong>not</strong> use the %buy_button% in this template as a user must be logged in to buy content!', 'mycred' ); ?><br />
  367.                             <?php _e( 'Available template tags are: %singular%, %plural%, %post_title%, %post_url%, %link_with_title%, %price%', 'mycred' ); ?></span>
  368.                         </li>
  369.                     </ol>
  370.                     <label class="subheader" for="<?php echo $this->field_id( array( 'templates' => 'members' ) ); ?>"><?php _e( 'Sale Template for members', 'mycred' ); ?></label>
  371.                     <ol id="myCRED-buy-template-members">
  372.                         <li>
  373.                             <textarea rows="10" cols="50" name="<?php echo $this->field_name( array( 'templates' => 'members' ) ); ?>" id="<?php echo $this->field_id( array( 'templates' => 'members' ) ); ?>" class="large-text code"><?php echo $sell_content['templates']['members']; ?></textarea>
  374.                             <span class="description"><?php _e( 'Your template must contain the %buy_button% tag for purchases to work!', 'mycred' ); ?><br />
  375.                             <?php _e( 'Available template tags are: %singular%, %plural%, %post_title%, %post_url%, %link_with_title%, %buy_button%, %price%', 'mycred' ); ?></span>
  376.                         </li>
  377.                     </ol>
  378.                     <label class="subheader" for="<?php echo $this->field_id( array( 'templates' => 'cantafford' ) ); ?>"><?php _e( 'Insufficient funds template', 'mycred' ); ?></label>
  379.                     <ol id="myCRED-buy-template-insufficient">
  380.                         <li>
  381.                             <textarea rows="10" cols="50" name="<?php echo $this->field_name( array( 'templates' => 'cantafford' ) ); ?>" id="<?php echo $this->field_id( array( 'templates' => 'cantafford' ) ); ?>" class="large-text code"><?php echo $sell_content['templates']['cantafford']; ?></textarea>
  382.                             <span class="description"><?php _e( 'Your template must contain the %buy_button% tag for purchases to work!', 'mycred' ); ?><br />
  383.                             <?php _e( 'Available template tags are: %singular%, %plural%, %post_title%, %post_url%, %link_with_title%, %buy_button%, %price%', 'mycred' ); ?></span>
  384.                         </li>
  385.                     </ol>
  386.                     <label class="subheader" for="<?php echo $this->field_id( array( 'logs' => 'buy' ) ); ?>"><?php _e( 'Log template for Purchases', 'mycred' ); ?></label>
  387.                     <ol id="myCRED-buy-template-purchase">
  388.                         <li>
  389.                             <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'logs' => 'buy' ) ); ?>" id="<?php echo $this->field_id( array( 'logs' => 'buy' ) ); ?>" value="<?php echo $sell_content['logs']['buy']; ?>" class="long" /></div>
  390.                                 <span class="description"><?php _e( 'Available template tags are: %singular%, %plural%, %post_title%, %post_url% or %link_with_title%', 'mycred' ); ?></span>
  391.                         </li>
  392.                     </ol>
  393.                     <label class="subheader" for="<?php echo $this->field_id( array( 'logs' => 'sell' ) ); ?>"><?php _e( 'Log template for Sales', 'mycred' ); ?></label>
  394.                     <ol id="myCRED-buy-template-sale">
  395.                         <li>
  396.                             <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'logs' => 'sell' ) ); ?>" id="<?php echo $this->field_id( array( 'logs' => 'sell' ) ); ?>" value="<?php echo $sell_content['logs']['sell']; ?>" class="long" /></div>
  397.                             <span class="description"><?php _e( 'Available template tags are: %singular%, %plural%, %post_title%, %post_url% or %link_with_title%', 'mycred' ); ?></span>
  398.                         </li>
  399.                     </ol>
  400.                 </div>
  401. <?php
  402.         }
  403.  
  404.         /**
  405.          * Sanitize & Save Settings
  406.          * @since 0.1
  407.          * @version 1.1
  408.          */
  409.         public function sanitize_extra_settings( $new_data, $data, $general ) {
  410.             // Post Types
  411.             $settings = $data['sell_content'];
  412.  
  413.             $new_data['sell_content']['post_types'] = sanitize_text_field( $settings['post_types'] );
  414.             $new_data['sell_content']['pay'] = sanitize_text_field( $settings['pay'] );
  415.             $new_data['sell_content']['pay_percent'] = abs( $settings['pay_percent'] );
  416.             if ( $new_data['sell_content']['pay_percent'] == 0 || $new_data['sell_content']['pay_percent'] > 100 )
  417.                 $new_data['sell_content']['pay_percent'] = 100;
  418.  
  419.             $new_data['sell_content']['defaults']['price'] = $this->core->format_number( $settings['defaults']['price'] );
  420.             $new_data['sell_content']['defaults']['overwrite_price'] = ( isset( $settings['defaults']['overwrite_price'] ) ) ? 1 : 0;
  421.             $new_data['sell_content']['defaults']['button_label'] = sanitize_text_field( $settings['defaults']['button_label'] );
  422.             $new_data['sell_content']['defaults']['overwrite_buttonlabel'] = ( isset( $settings['defaults']['overwrite_buttonlabel'] ) ) ? 1 : 0;
  423.             $new_data['sell_content']['defaults']['expire'] = abs( $settings['defaults']['expire'] );
  424.  
  425.             $new_data['sell_content']['templates']['members'] = trim( $settings['templates']['members'] );
  426.             $new_data['sell_content']['templates']['visitors'] = trim( $settings['templates']['visitors'] );
  427.             $new_data['sell_content']['templates']['cantafford'] = trim( $settings['templates']['cantafford'] );
  428.  
  429.             $new_data['sell_content']['logs']['buy'] = sanitize_text_field( $settings['logs']['buy'] );
  430.             $new_data['sell_content']['logs']['sell'] = sanitize_text_field( $settings['logs']['sell'] );
  431.  
  432.             unset( $settings );
  433.             return $new_data;
  434.         }
  435.  
  436.         /**
  437.          * Add Meta Box to Content
  438.          * @since 0.1
  439.          * @version 1.0
  440.          */
  441.         public function add_metabox() {
  442.             $sell_content = $this->sell_content;
  443.             $post_types = explode( ',', $sell_content['post_types'] );
  444.             $name = sprintf( __( '%s Sell This', 'mycred' ), mycred_label() );
  445.             foreach ( (array) $post_types as $post_type ) {
  446.                 $post_type = trim( $post_type );
  447.                 add_meta_box(
  448.                     'mycred_sell_content',
  449.                     $name,
  450.                     array( $this, 'metabox' ),
  451.                     $post_type,
  452.                     'side',
  453.                     'high'
  454.                 );
  455.             }
  456.         }
  457.  
  458.         /**
  459.          * Sale Preference
  460.          * Returns a given posts sale preferences. If none exists a new one is buildt and returned.
  461.          *
  462.          * @return (array) sales settings
  463.          * @since 0.1
  464.          * @version 1.0
  465.          */
  466.         public function get_sale_prefs( $post_id ) {
  467.             $sell_content = $this->sell_content;
  468.             if ( !isset( $sell_content['defaults']['expire'] ) )
  469.                 $sell_content['defaults']['expire'] = 0;
  470.  
  471.             $prefs = get_post_meta( $post_id, 'myCRED_sell_content', true );
  472.             if ( empty( $prefs ) ) {
  473.                 $sales_data = array(
  474.                     'status'       => 'disabled',
  475.                     'price'        => $sell_content['defaults']['price'],
  476.                     'button_label' => $sell_content['defaults']['button_label'],
  477.                     'expire'       => $sell_content['defaults']['expire']
  478.                 );
  479.             }
  480.             else {
  481.                 if ( !isset( $prefs['expire'] ) )
  482.                     $prefs['expire'] = $sell_content['defaults']['expire'];
  483.  
  484.                 $sales_data = $prefs;
  485.             }
  486.  
  487.             return $sales_data;
  488.         }
  489.  
  490.         /**
  491.          * Sell Meta Box
  492.          * @since 0.1
  493.          * @version 1.0
  494.          */
  495.         public function metabox( $post ) {
  496.             // Make sure add-on has been setup
  497.             if ( !$this->is_installed() ) {
  498.                 echo sprintf( __( '%s Sell Content needs to be setup before you can use this feature.', 'mycred' ), mycred_label() );
  499.                 // Settings Link
  500.                 if ( $this->core->can_edit_plugin( get_current_user_id() ) )
  501.                     echo ' <a href="' . admin_url( 'admin.php?page=myCRED_page_settings' ) . '" title="' . __( 'Setup add-on', 'mycred' ) . '">' . __( 'Lets do it', 'mycred' ) . '</a>';
  502.  
  503.                 return;
  504.             }
  505.             $admin = false;
  506.             $post_id = $post->ID;
  507.             $post_type = $post->post_type;
  508.  
  509.             $user_id = get_current_user_id();
  510.             $sell_content = $this->sell_content;
  511.             $sales_data = $this->get_sale_prefs( $post_id );
  512.  
  513.             // Mark admins
  514.             if ( $this->core->can_edit_plugin( $user_id ) )
  515.                 $admin = true;
  516.  
  517.             // Empty $sales_data means disabled same if the status is actually set to "disabled"
  518.             if ( empty( $sales_data ) || ( isset( $sales_data['status'] ) && $sales_data['status'] == 'disabled' ) ) {
  519.                 $style = 'display:none;';
  520.                 $status = 'disabled';
  521.             }
  522.             else {
  523.                 $style = 'display:block;';
  524.                 $status = 'enabled';
  525.             }
  526.  
  527.             $op = (bool) $sell_content['defaults']['overwrite_price'];
  528.             $ob = (bool) $sell_content['defaults']['overwrite_buttonlabel']; ?>
  529.  
  530.     <p><input type="checkbox" name="mycred_sell_this" id="mycred-sell-this"<?php checked( $status, 'enabled' ); ?> value="enabled" /><label for="mycred-sell-this"><?php echo __( 'Enable sale of this ', 'mycred' ) . $post_type . '.'; ?></label></p>
  531.     <div id="mycred-sale-settings" style="<?php echo $style; ?>">
  532.         <input type="hidden" name="mycred-sell-this-token" value="<?php echo wp_create_nonce( 'mycred-sell-this' ); ?>" />
  533.         <input type="hidden" name="mycred-sell-this-status" value="<?php echo $status; ?>" />
  534.         <ul>
  535.             <li>
  536.                 <label for="mycred-buy-prefs-"><?php _e( 'Price', 'mycred' ); ?></label>
  537.                 <div class="formated"><?php echo $this->core->before; ?> <input type="text" name="myCRED_sell_content[price]" id="mycred-buy-prefs-price" value="<?php echo $sales_data['price']; ?>" <?php if ( $op === false && !$admin ) echo 'disabled="disabled" class="disabled"'; ?> size="5" /> <?php echo $this->core->after; ?></div>
  538.             </li>
  539.             <li>
  540.                 <label for="mycred-buy-prefs-"><?php _e( 'Button Label', 'mycred' ); ?></label>
  541.                 <input type="text" name="myCRED_sell_content[button_label]" id="mycred-buy-prefs-" value="<?php echo $sales_data['button_label']; ?>" <?php if ( $ob === false && !$admin ) echo 'disabled="disabled" class="disabled"'; ?> />
  542.             </li>
  543.             <li>
  544.                 <label for="mycred-buy-prefs-"><?php _e( 'Purchase expires after', 'mycred' ); ?></label>
  545.                 <div class="formated"><input type="text" name="myCRED_sell_content[expire]" id="mycred-buy-prefs-expire" value="<?php echo $sales_data['expire']; ?>" <?php if ( $op === false && !$admin ) echo 'disabled="disabled" class="disabled"'; ?> size="5" /> <?php echo $this->exp_title; ?></div>
  546.             </li>
  547.         </ul>
  548.     </div>
  549.     <script type="text/javascript">//<![CDATA[
  550.         jQuery(function($) {
  551.             $('#mycred-sell-this').click(function(){
  552.                     $('#mycred-sale-settings').toggle();
  553.                 });
  554.         });//]]>
  555.     </script>
  556. <?php
  557.         }
  558.  
  559.         /**
  560.          * Save Sell Meta Box
  561.          * @since 0.1
  562.          * @version 1.0
  563.          */
  564.         public function save_metabox( $post_id ) {
  565.             // Make sure sale is enabled
  566.             if ( !isset( $_POST['mycred-sell-this-status'] ) || !isset( $_POST['mycred-sell-this-token'] ) ) return $post_id;
  567.  
  568.             // Verify token
  569.             if ( wp_verify_nonce( $_POST['mycred-sell-this-token'], 'mycred-sell-this' ) === false ) return $post_id;
  570.  
  571.             // Status
  572.             if ( !isset( $_POST['mycred_sell_this'] ) )
  573.                 $status = 'disabled';
  574.             else
  575.                 $status = 'enabled';
  576.  
  577.             $prefs = get_post_meta( $post_id, 'myCRED_sell_content', true );
  578.             // If sale has never been set and is not enabled bail
  579.             if ( empty( $prefs ) && $status == 'disabled' ) return $post_id;
  580.  
  581.             $sell_content = $this->sell_content;
  582.             $is_admin = $this->core->can_edit_plugin();
  583.  
  584.             // Status
  585.             $prefs['status'] = $status;
  586.  
  587.             // Prefs
  588.             $op = (bool) $sell_content['defaults']['overwrite_price'];
  589.             $prefs['price'] = ( $op === true || $is_admin === true ) ? $_POST['myCRED_sell_content']['price'] : $sell_content['defaults']['price'];
  590.  
  591.             $ob = (bool) $sell_content['defaults']['overwrite_buttonlabel'];
  592.             $prefs['button_label'] = ( $ob === true || $is_admin === true ) ? $_POST['myCRED_sell_content']['button_label'] : $sell_content['defaults']['button_label'];
  593.  
  594.             // Expiration
  595.             $prefs['expire'] = ( $is_admin === true ) ? abs( $_POST['myCRED_sell_content']['expire'] ) : $sell_content['defaults']['expire'];
  596.  
  597.             update_post_meta( $post_id, 'myCRED_sell_content', $prefs );
  598.         }
  599.  
  600.         /**
  601.          * Get the Post ID
  602.          * Added support for sale of bbPress items.
  603.          * @since 1.2
  604.          * @version 1.0
  605.          */
  606.         public function get_post_ID() {
  607.             $post_id = $bbp_topic_id = $bbp_reply_id = 0;
  608.             if ( function_exists( 'bbpress' ) ) {
  609.                 global $wp_query;
  610.  
  611.                 $bbp = bbpress();
  612.  
  613.                 // Currently inside a topic loop
  614.                 if ( ! empty( $bbp->topic_query->in_the_loop ) && isset( $bbp->topic_query->post->ID ) )
  615.                     $bbp_topic_id = $bbp->topic_query->post->ID;
  616.  
  617.                 // Currently inside a search loop
  618.                 elseif ( ! empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_topic( $bbp->search_query->post->ID ) )
  619.                     $bbp_topic_id = $bbp->search_query->post->ID;
  620.  
  621.                 // Currently viewing/editing a topic, likely alone
  622.                 elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && ! empty( $bbp->current_topic_id ) )
  623.                     $bbp_topic_id = $bbp->current_topic_id;
  624.  
  625.                 // Currently viewing/editing a topic, likely in a loop
  626.                 elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && isset( $wp_query->post->ID ) )
  627.                     $bbp_topic_id = $wp_query->post->ID;
  628.                
  629.                 // So far, no topic found, check if we are in a reply
  630.                 if ( $bbp_topic_id == 0 ) {
  631.  
  632.                     // Currently inside a replies loop
  633.                     if ( !empty( $bbp->reply_query->in_the_loop ) && isset( $bbp->reply_query->post->ID ) )
  634.                         $bbp_reply_id = $bbp->reply_query->post->ID;
  635.  
  636.                     // Currently inside a search loop
  637.                     elseif ( !empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_reply( $bbp->search_query->post->ID ) )
  638.                         $bbp_reply_id = $bbp->search_query->post->ID;
  639.  
  640.                     // Currently viewing a forum
  641.                     elseif ( ( bbp_is_single_reply() || bbp_is_reply_edit() ) && !empty( $bbp->current_reply_id ) )
  642.                         $bbp_reply_id = $bbp->current_reply_id;
  643.  
  644.                     // Currently viewing a reply
  645.                     elseif ( ( bbp_is_single_reply() || bbp_is_reply_edit() ) && isset( $wp_query->post->ID ) )
  646.                         $bbp_reply_id = $wp_query->post->ID;
  647.                
  648.                     if ( $bbp_reply_id != 0 )
  649.                         $post_id = $bbp_reply_id;
  650.  
  651.                 }
  652.                
  653.                 // Else we are in a topic
  654.                 else $post_id = $bbp_topic_id;
  655.  
  656.             }
  657.  
  658.             if ( $post_id == 0 )
  659.                 $post_id = $GLOBALS['post']->ID;
  660.  
  661.             return apply_filters( 'mycred_sell_this_get_post_ID', $post_id, $this );
  662.         }
  663.  
  664.         /**
  665.          * For Sale
  666.          * Checks if a given post is for sale.
  667.          *
  668.          * @param $post_id (int) required post id
  669.          * @returns (bool) true or false
  670.          * @since 0.1
  671.          * @version 1.0
  672.          */
  673.         public function for_sale( $post_id ) {
  674.             $prefs = get_post_meta( $post_id, 'myCRED_sell_content', true );
  675.             if ( !empty( $prefs ) && isset( $prefs['status'] ) && $prefs['status'] == 'enabled' ) return true;
  676.  
  677.             return false;
  678.         }
  679.  
  680.         /**
  681.          * User Paid
  682.          * Checks if a given user has paid for a specific post.
  683.          * Will return true if the user can edit this plugin or creds.
  684.          *
  685.          * @param $user_id (int) required user id
  686.          * @param $post_id (int) required post id
  687.          * @returns (bool) true or false
  688.          * @since 0.1
  689.          * @version 1.3
  690.          */
  691.         public function user_paid( $user_id, $post_id ) {
  692.             // Admins can view
  693.             if ( $this->core->can_edit_plugin( $user_id ) || $this->core->can_edit_creds( $user_id ) ) return true;
  694.             // Authors can view
  695.             $the_post = get_post( $post_id );
  696.             if ( ! isset( $the_post->post_author ) && $the_post->post_author == $user_id ) return true;
  697.  
  698.             global $wpdb;
  699.            
  700.             $sell_content = $this->sell_content;
  701.  
  702.             // Search for the latest purchase of this item.
  703.             $sql = "SELECT * FROM {$this->core->log_table} WHERE user_id = %d AND ref = %s AND ref_id = %d ORDER BY time DESC LIMIT 0,1;";
  704.             $purchases = $wpdb->get_results( $wpdb->prepare( $sql, $user_id, 'buy_content', $post_id ) );
  705.  
  706.             // We have found purchase records
  707.             if ( !empty( $purchases ) ) {
  708.                 // Since individual posts can override the default settings we need to check sales prefs
  709.                 $prefs = $this->get_sale_prefs( $post_id );
  710.                 if ( !isset( $prefs['expire'] ) )
  711.                     $prefs['expire'] = ( isset( $sell_content['defaults']['expire'] ) ) ? $sell_content['defaults']['expire'] : 0;
  712.  
  713.                 // If purchases never expire just return true here and now
  714.                 if ( $prefs['expire'] == 0 ) return true;
  715.  
  716.                 // Check if purchase has expired
  717.                 if ( !$this->purchase_has_expired( $purchases[0]->time, $prefs['expire'], $user_id, $post_id ) ) return true;
  718.             }
  719.  
  720.             // All else there are no purchases
  721.             return false;
  722.         }
  723.  
  724.         /**
  725.          * Purchase Has Expired
  726.          * Makes a time comparison to check if a given timestamp is in the future (not expired) or
  727.          * in the past (expired).
  728.          *
  729.          * @param $timestamp (int) The UNIX timestamp to wich we apply the expiration check
  730.          * @param $length (int) Length of expiration time to check by default this is the number of hours.
  731.          * @filter 'mycred_sell_expire_calc'
  732.          * @returns (bool) true or false
  733.          * @since 1.1
  734.          * @version 1.0
  735.          */
  736.         public function purchase_has_expired( $timestamp, $length = 0, $user_id, $post_id ) {
  737.             if ( $length == 0 ) return false;
  738.  
  739.             $expiration = apply_filters( 'mycred_sell_expire_calc', abs( $length*3600 ), $length, $user_id, $post_id );
  740.             $expiration = $expiration+$timestamp;
  741.  
  742.             if ( $expiration > date_i18n( 'U' ) ) return false;
  743.            
  744.             return true;
  745.         }
  746.  
  747.         /**
  748.          * User Can Buy
  749.          * Checks if a given user can afford the given price.
  750.          *
  751.          * @param $user_id (int) required user id
  752.          * @param $price (int|float) required price to check
  753.          * @returns (bool) true or false
  754.          * @since 0.1
  755.          * @version 1.0
  756.          */
  757.         public function user_can_buy( $user_id, $price ) {
  758.             $balance = $this->core->get_users_cred( $user_id );
  759.             if ( $balance-$price < 0 ) return false;
  760.             return true;
  761.         }
  762.  
  763.         /**
  764.          * Get Button
  765.          * Replaces the %buy_button% template tag with the submit button along
  766.          * with the set button label. If no template tag is found one is inserted in the end of the given string.
  767.          *
  768.          * @param $text (string) text to check for template tag.
  769.          * @param $post (object) optional post object to allow post template tags.
  770.          * @returns (string) formated string.
  771.          * @since 0.1
  772.          * @version 1.0
  773.          */
  774.         public function get_button( $text, $post ) {
  775.             $sell_content = $this->sell_content;
  776.             $prefs = $this->get_sale_prefs( $post->ID );
  777.  
  778.             // Button Label
  779.             if ( isset( $prefs['button_label'] ) )
  780.                 $button_text = $prefs['button_label'];
  781.             else
  782.                 $button_text = $sell_content['defaults']['button_label'];
  783.  
  784.             // Button element
  785.             $button = '<input type="submit" name="mycred-buy-button" value="' . $this->core->template_tags_post( $button_text, $post ) . '" class="button large" />';
  786.  
  787.             // Make sure there is a button
  788.             if ( !preg_match( '/%buy_button%/', $text ) )
  789.                 $text .= ' %buy_button% ';
  790.  
  791.             $content = str_replace( '%buy_button%', $button, $text );
  792.  
  793.             return $content;
  794.         }
  795.  
  796.         /**
  797.          * The Content Overwrite
  798.          * If the current post is set for sale we apply the appropirate template.
  799.          * Uses 3 different templates. a) Visitors Template b) Members Template and c) Cant Afford Template
  800.          *
  801.          * @returns (string) content
  802.          * @since 0.1
  803.          * @version 1.1
  804.          */
  805.         public function the_content( $content ) {
  806.             global $mycred_content_purchase;
  807.  
  808.             $post_id = $this->get_post_ID();
  809.             $the_post = get_post( $post_id );
  810.  
  811.             // If content is for sale
  812.             if ( $this->for_sale( $post_id ) ) {
  813.                 // Prep
  814.                 $user_id = get_current_user_id();
  815.                 $sell_content = $this->sell_content;
  816.                 $prefs = $this->get_sale_prefs( $post_id );
  817.  
  818.                 // Visitors
  819.                 if ( !is_user_logged_in() ) {
  820.                     $template = $sell_content['templates']['visitors'];
  821.                    
  822.                     $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  823.                     $template = $this->core->template_tags_general( $template );
  824.                     $template = $this->core->template_tags_post( $template, $the_post );
  825.                     return '<div class="mycred-content-forsale">' . $template . '</div>';
  826.                 }
  827.  
  828.                 // We are logged in, have not purchased this item and can make a purchase
  829.                 elseif ( is_user_logged_in() && !$this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
  830.                     $template = $sell_content['templates']['members'];
  831.  
  832.                     $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  833.                     $template = $this->core->template_tags_general( $template );
  834.                     $template = $this->core->template_tags_post( $template, $the_post );
  835.                     $template = $this->get_button( $template, $the_post );
  836.                     return '
  837. <form action="" method="post">
  838.     <input type="hidden" name="mycred_purchase[post_id]"   value="' . $post_id . '" />
  839.     <input type="hidden" name="mycred_purchase[post_type]" value="' . $the_post->post_type . '" />
  840.     <input type="hidden" name="mycred_purchase[user_id]"   value="' . get_current_user_id() . '" />
  841.     <input type="hidden" name="mycred_purchase[author]"    value="' . $the_post->post_author . '" />
  842.     <input type="hidden" name="mycred_purchase_token"      value="' . wp_create_nonce( 'buy-content' ) . '" />
  843.     <input type="hidden" name="mycred_purchase[action]"    value="buy" />
  844.     <div class="mycred-content-forsale">' . $template . '</div>
  845. </form>';
  846.                 }
  847.                 // We are logged in, have not purchased this item and can not afford to buy this
  848.                 elseif ( is_user_logged_in() && !$this->user_paid( $user_id, $post_id ) && !$this->user_can_buy( $user_id, $prefs['price'] ) ) {
  849.                     $template = $sell_content['templates']['cantafford'];
  850.  
  851.                     $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  852.                     $template = $this->core->template_tags_general( $template );
  853.                     $template = $this->core->template_tags_post( $template, $the_post );
  854.                     return '<div class="mycred-content-forsale">' . $template . '</div>';
  855.                 }
  856.             }
  857.  
  858.             // Mark purchases
  859.             if ( $mycred_content_purchase === true ) {
  860.                 $thank_you = __( 'Thank you for your purchase!', 'mycred' );
  861.                 $wrapper = '<div id="mycred-thank-you"><p>' . $thank_you . '</p></div>';
  862.                 $content = $wrapper . $content;
  863.             }
  864.  
  865.             return do_shortcode( $content );
  866.         }
  867.  
  868.         /**
  869.          * Render Shortcode
  870.          * Just as protecting the entire content, the mycred_sell_this shortcode protects
  871.          * parts of the content.
  872.          *
  873.          * @returns (string) content
  874.          * @since 0.1
  875.          * @version 1.3
  876.          */
  877.         public function render_shortcode( $atts, $content ) {
  878.             // The Post
  879.             $post_id = $this->get_post_ID();
  880.             $the_post = get_post( $post_id );
  881.  
  882.             // The User
  883.             $user_id = get_current_user_id();
  884.             $sell_content = $this->sell_content;
  885.  
  886.             $prefs = shortcode_atts( array(
  887.                 'price'        => $sell_content['defaults']['price'],
  888.                 'button_label' => $sell_content['defaults']['button_label'],
  889.                 'expire'       => $sell_content['defaults']['expire']
  890.             ), $atts );
  891.             $sales_prefs = $this->get_sale_prefs( $post_id );
  892.  
  893.             // If we are not using defaults save these settings.
  894.             if ( ( $sales_prefs['price'] != $prefs['price'] ) || ( $sales_prefs['button_label'] != $prefs['button_label'] ) || ( $sales_prefs['expire'] != $prefs['expire'] ) ) {
  895.                 update_post_meta( $post_id, 'myCRED_sell_content', array(
  896.                     'price'        => $prefs['price'],
  897.                     'status'       => $sales_prefs['status'],
  898.                     'button_label' => $prefs['button_label'],
  899.                     'expire'       => $prefs['expire']
  900.                 ) );
  901.             }
  902.  
  903.             // Not logged in
  904.             if ( !is_user_logged_in() ) {
  905.                 $template = $sell_content['templates']['visitors'];
  906.  
  907.                 $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  908.                 $template = $this->core->template_tags_general( $template );
  909.                 $template = $this->core->template_tags_post( $template, $the_post );
  910.                 unset( $content );
  911.                 return '<div class="mycred-content-forsale">' . $template . '</div>';
  912.             }
  913.  
  914.             // Can buy
  915.             elseif ( is_user_logged_in() && !$this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
  916.                 $template = $sell_content['templates']['members'];
  917.  
  918.                 $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  919.                 $template = $this->core->template_tags_general( $template );
  920.                 $template = $this->core->template_tags_post( $template, $the_post );
  921.                 $template = $this->get_button( $template, $the_post );
  922.                 unset( $content );
  923.                 return '
  924. <form action="" method="post">
  925.     <input type="hidden" name="mycred_purchase[post_id]"   value="' . $post_id . '" />
  926.     <input type="hidden" name="mycred_purchase[post_type]" value="' . $the_post->post_type . '" />
  927.     <input type="hidden" name="mycred_purchase[user_id]"   value="' . get_current_user_id() . '" />
  928.     <input type="hidden" name="mycred_purchase[author]"    value="' . $the_post->post_author . '" />
  929.     <input type="hidden" name="mycred_purchase_token"      value="' . wp_create_nonce( 'buy-content' ) . '" />
  930.     <input type="hidden" name="mycred_purchase[action]" value="buy" />
  931.     <div class="mycred-content-forsale">' . $template . '</div>
  932. </form>';
  933.             }
  934.  
  935.             // We are logged in, have not purchased this item and can not afford to buy this
  936.             elseif ( is_user_logged_in() && !$this->user_paid( $user_id, $post_id ) && !$this->user_can_buy( $user_id, $prefs['price'] ) ) {
  937.                 $template = $sell_content['templates']['cantafford'];
  938.                    
  939.                 $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  940.                 $template = $this->core->template_tags_general( $template );
  941.                 $template = $this->core->template_tags_post( $template, $the_post );
  942.                 unset( $content );
  943.                 return '<div class="mycred-content-forsale">' . $template . '</div>';
  944.             }
  945.  
  946.             // Admin and Author Wrapper for highlight of content set for sale
  947.             if ( mycred_is_admin() || $the_post->post_author == $user_id )
  948.                 $content = '<div class="mycred-mark-title">' . __( 'The following content is set for sale:', 'mycred' ) . '</div><div class="mycred-mark-content">' . $content . '</div>';
  949.  
  950.             return do_shortcode( $content );
  951.         }
  952.        
  953.         /**
  954.          * Render Shortcode AJAX
  955.          * Just as protecting the entire content, the mycred_sell_this_ajax shortcode protects
  956.          * parts of the content and uses AJAX to make the purchase
  957.          *
  958.          * @returns (string) content
  959.          * @since 0.1
  960.          * @version 1.0.1
  961.          */
  962.         public function render_ajax_shortcode( $atts, $content ) {
  963.             global $mycred_buy_content;
  964.  
  965.             // The Post
  966.             $post_id = $this->get_post_ID();
  967.             $the_post = get_post( $post_id );
  968.  
  969.             $user_id = get_current_user_id();
  970.             $sell_content = $this->sell_content;
  971.  
  972.             $prefs = shortcode_atts( array(
  973.                 'price'        => $sell_content['defaults']['price'],
  974.                 'button_label' => $sell_content['defaults']['button_label'],
  975.                 'expire'       => $sell_content['defaults']['expire']
  976.             ), $atts );
  977.             $sales_prefs = $this->get_sale_prefs( $post_id );
  978.  
  979.             // If we are not using defaults save these settings.
  980.             if ( ( $sales_prefs['price'] != $prefs['price'] ) || ( $sales_prefs['button_label'] != $prefs['button_label'] ) || ( $sales_prefs['expire'] != $prefs['expire'] ) ) {
  981.                 update_post_meta( $post_id, 'myCRED_sell_content', array(
  982.                     'price'        => $prefs['price'],
  983.                     'status'       => $sales_prefs['status'],
  984.                     'button_label' => $prefs['button_label'],
  985.                     'expire'       => $prefs['expire']
  986.                 ) );
  987.             }
  988.  
  989.             // Not logged in
  990.             if ( !is_user_logged_in() ) {
  991.                 $template = $sell_content['templates']['visitors'];
  992.  
  993.                 $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  994.                 $template = $this->core->template_tags_general( $template );
  995.                 $template = $this->core->template_tags_post( $template, $the_post );
  996.                 unset( $content );
  997.                 return '<div class="mycred-content-forsale">' . $template . '</div>';
  998.             }
  999.  
  1000.             // Can buy
  1001.             elseif ( is_user_logged_in() && !$this->user_paid( $user_id, $post_id ) && $this->user_can_buy( $user_id, $prefs['price'] ) ) {
  1002.                 $template = $sell_content['templates']['members'];
  1003.  
  1004.                 $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  1005.                 $template = $this->core->template_tags_general( $template );
  1006.                 $template = $this->core->template_tags_post( $template, $the_post );
  1007.  
  1008.                 if ( isset( $prefs['button_label'] ) )
  1009.                     $button_text = $prefs['button_label'];
  1010.                 else
  1011.                     $button_text = $sell_content['defaults']['button_label'];
  1012.  
  1013.                 $button = '<input type="button" data-id="' . $the_post->ID . '" name="mycred-buy-button" value="' . $this->core->template_tags_post( $button_text, $the_post ) . '" class="mycred-sell-this-button button large" />';
  1014.                 $template = str_replace( '%buy_button%', $button, $template );
  1015.                 unset( $content );
  1016.  
  1017.                 $mycred_buy_content = true;
  1018.                 return '<div class="mycred-content-forsale">' . $template . '</div>';
  1019.             }
  1020.  
  1021.             // We are logged in, have not purchased this item and can not afford to buy this
  1022.             elseif ( is_user_logged_in() && !$this->user_paid( $user_id, $post_id ) && !$this->user_can_buy( $user_id, $prefs['price'] ) ) {
  1023.                 $template = $sell_content['templates']['cantafford'];
  1024.                    
  1025.                 $template = str_replace( '%price%', $this->core->format_creds( $prefs['price'] ), $template );
  1026.                 $template = $this->core->template_tags_general( $template );
  1027.                 $template = $this->core->template_tags_post( $template, $the_post );
  1028.                 unset( $content );
  1029.                 return '<div class="mycred-content-forsale">' . $template . '</div>';
  1030.             }
  1031.  
  1032.             // Admin and Author Wrapper for highlight of content set for sale
  1033.             if ( mycred_is_admin() || $the_post->post_author == $user_id )
  1034.                 $content = '<div class="mycred-mark-title">' . __( 'The following content is set for sale:', 'mycred' ) . '</div><div class="mycred-mark-content">' . $content . '</div>';
  1035.  
  1036.             return do_shortcode( $content );
  1037.         }
  1038.        
  1039.         /**
  1040.          * Render Sales History Shortcode
  1041.          * @see http://mycred.me/shortcodes/mycred_sales_history/
  1042.          * @since 1.0.9
  1043.          * @version 1.0.1
  1044.          */
  1045.         public function render_sales_history( $atts ) {
  1046.             extract( shortcode_atts( array(
  1047.                 'login'        => NULL,
  1048.                 'title'        => '',
  1049.                 'title_el'     => 'h1',
  1050.                 'title_class'  => '',
  1051.                 'include_date' => true,
  1052.                 'no_result'    => __( 'No purchases found', 'mycred' )
  1053.             ), $atts ) );
  1054.            
  1055.             // Not logged in
  1056.             if ( !is_user_logged_in() ) {
  1057.                 if ( $login != NULL )
  1058.                     return '<div class="mycred-not-logged-in">' . $login . '</div>';
  1059.  
  1060.                 return;
  1061.             }
  1062.  
  1063.             // Prep
  1064.             $output = '<div class="mycred-sales-history-wrapper">';
  1065.             $user_id = get_current_user_id();
  1066.    
  1067.             global $wpdb;
  1068.    
  1069.             // Title
  1070.             if ( !empty( $title ) ) {
  1071.                 if ( !empty( $title_class ) )
  1072.                     $title_class = ' class="' . $title_class . '"';
  1073.                 $output .= '<' . $title_el . $title_class . '>' . $title . '</' . $title_el . '>';
  1074.             }
  1075.            
  1076.             // Query
  1077.             $sql = "SELECT * FROM {$this->core->log_table} WHERE user_id = %d AND ref = %s ORDER BY time;";
  1078.             $results = $wpdb->get_results( $wpdb->prepare( $sql, $user_id, 'buy_content' ) );
  1079.             $rows = array();
  1080.            
  1081.             // Results
  1082.             if ( $wpdb->num_rows > 0 ) {
  1083.                 foreach ( $results as $item ) {
  1084.                     // Row
  1085.                     $row = '<span class="item-link"><a href="' . get_permalink( $item->ref_id ) . '">' . get_the_title( $item->ref_id ) . '</a></span>';
  1086.  
  1087.                     // Add Date to row
  1088.                     if ( $include_date )
  1089.                         $row .= '<span class="purchased">' . __( 'Purchased', 'mycred' ) . ' ' . date_i18n( get_option( 'date_format' ), $item->time ) . '</span>';
  1090.  
  1091.                     // Construct row (and let others play)
  1092.                     $rows[] = apply_filters( 'mycred_sale_history_row', $row, $item );
  1093.                 }
  1094.             }
  1095.            
  1096.             // Implode rows if there are any
  1097.             if ( !empty( $rows ) ) {
  1098.                 $output .= '<ul class="mycred-purchase-history"><li>' . implode( '</li><li>', $rows ) . '</li></ul>';
  1099.             }
  1100.             // No results
  1101.             else {
  1102.                 if ( !empty( $no_result ) )
  1103.                     $output .= '<p>' . $no_result . '</p>';
  1104.             }
  1105.  
  1106.             $output .= '</div>';
  1107.  
  1108.             return $output;
  1109.         }
  1110.        
  1111.         /**
  1112.          * Support for Email Notices
  1113.          * @since 1.1
  1114.          * @version 1.0
  1115.          */
  1116.         public function email_notices( $data ) {
  1117.             if ( $data['request']['ref'] == 'buy_content' ) {
  1118.                 $message = $data['message'];
  1119.                 $data['message'] = $this->core->template_tags_post( $message, $data['request']['ref_id'] );
  1120.             }
  1121.             return $data;
  1122.         }
  1123.     }
  1124.     $sell_content = new myCRED_Sell_Content();
  1125.     $sell_content->load();
  1126. }
  1127. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement