class-rcp-easy-digital-downloads.php 4.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
<?php
/**
 * Easy Digital Downloads Integration
 *
 * @package     Restrict Content Pro
 * @subpackage  Integrations/EDD
 * @copyright   Copyright (c) 2017, Restrict Content Pro
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       2.7
 */

class RCP_EDD {

	private $user;
	private $member;

	/**
	 * RCP_EDD constructor.
	 *
	 * @return void
	 */
	public function __construct() {
		$this->user = wp_get_current_user();
		$this->member = new RCP_Member( $this->user->ID );

		add_filter( 'edd_can_purchase_download', array( $this, 'can_purchase' ), 10, 2 );
		add_filter( 'edd_purchase_download_form', array( $this, 'download_form' ), 10, 2 );
		add_filter( 'edd_file_download_has_access', array( $this, 'file_download_has_access' ), 10, 3 );
		add_filter( 'edd_downloads_query', array( $this, 'edd_downloads_query' ), 10, 2 );
		add_filter( 'edd_downloads_excerpt', array( $this, 'edd_downloads_excerpt' ) );
	}

	/**
	 * Restricts the ability to purchase a product if the user doesn't have access to it.
	 *
	 * @param bool         $can_purchase
	 * @param EDD_Download $download
	 *
	 * @access public
	 * @since  2.7
	 * @return bool
	 */
	public function can_purchase( $can_purchase, $download ) {

		if ( ! $can_purchase || ! $this->member->can_access( $download->ID ) ) {
			$can_purchase = false;
		}

		return $can_purchase;
	}

	/**
	 * Overrides the purchase form if the user doesn't have access to the product.
	 *
	 * @param string $purchase_form Purchase form HTML.
	 * @param array  $args          Array of arguments for display.
	 *
	 * @access public
	 * @since  2.7
	 * @return string
	 */
	public function download_form( $purchase_form, $args ) {

		if ( ! $this->member->can_access( $args['download_id'] ) ) {
			return '';
		}

		return $purchase_form;
	}

	/**
	 * Prevents downloading files if the member doesn't have access.
	 *
	 * @param bool  $has_access Whether or not the member has access.
	 * @param int   $payment_id ID of the payment to check.
	 * @param array $args       Array of arguments.
	 *
	 * @access public
	 * @since  2.7
	 * @return bool
	 */
	public function file_download_has_access( $has_access, $payment_id, $args ) {

		if ( ! $this->member->can_access( $args['download'] ) ) {
			$has_access = false;
		}

		return $has_access;
	}

	/**
	 * Removes restricted downloads from the [downloads] shortcode query.
	 *
	 * @param array $query Query arguments.
	 * @param array $atts  Shortcode attributes.
	 *
	 * @access public
	 * @since 2.7
	 * @return array
	 */
	public function edd_downloads_query( $query, $atts ) {

		global $rcp_options;

		if ( isset( $rcp_options['hide_premium'] ) && ! rcp_is_active( get_current_user_id() ) ) {
			$premium_ids              = rcp_get_restricted_post_ids();
			$term_restricted_post_ids = rcp_get_post_ids_assigned_to_restricted_terms();
			$post_ids                 = array_unique( array_merge( $premium_ids, $term_restricted_post_ids ) );
			if ( ! empty( $post_ids ) ) {
				$query['post__not_in'] = $post_ids;
			}
		}

		return $query;
	}

	/**
	 * Filters the excerpt in the [downloads] shortcode if the member doesn't have access.
	 *
	 * @param string $excerpt
	 *
	 * @access public
	 * @since  2.7
	 * @return string
	 */
	public function edd_downloads_excerpt( $excerpt ) {

		global $rcp_options;

		$post_id = get_the_ID();

		if ( $this->member->can_access( $post_id ) || get_post_meta( $post_id, 'rcp_show_excerpt', true ) ) {
			return $excerpt;
		}

		if ( rcp_is_paid_content( $post_id ) ) {
			$excerpt = ! empty( $rcp_options['paid_message'] ) ? $rcp_options['paid_message'] : false;
		} else {
			$excerpt = ! empty( $rcp_options['free_message'] ) ? $rcp_options['free_message'] : false;
		}

		if( empty( $excerpt ) ) {
			$excerpt = __( 'This content is restricted to subscribers', 'rcp' );
		}

		return $excerpt;
	}
}

/**
 * Initialize the EDD integration if the plugin is activated.
 *
 * @since 2.7
 * @return void
 */
function rcp_edd_init() {

	if ( ! class_exists( 'Easy_Digital_Downloads' ) ) {
		return;
	}
	new RCP_EDD;
}
add_action( 'init', 'rcp_edd_init' );