Created
September 14, 2017 14:17
-
-
Save sareiodata/2d6215eca33fb7a44ced43e315a76f0e to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Plugin Name: Easy Digital Downloads - TranslatePress Customizations | |
* Plugin URI: http://translatepress.com | |
* Description: TranslatePress customizations for edd | |
* Version: 1.0.0 | |
* Author: Madalin Ungureanu | |
*/ | |
add_filter( 'edd_sl_check_item_name', 'edd_trp_cust_sl_name_matches', 10, 4 ); | |
add_filter( 'edd_sl_id_license_match', 'edd_trp_cust_sl_id_matches', 10, 4 ); | |
add_filter( 'edd_sl_force_check_by_name', 'edd_trp_cust_sl_force_check_by_name' ); | |
// Exit if accessed directly | |
if( !defined( 'ABSPATH' ) ) exit; | |
function edd_trp_cust_add_meta_box() { | |
if( current_user_can( 'manage_shop_settings' ) ) { | |
add_meta_box( 'edd_downloads_trp_cust', __( 'Master Licence', 'edd-trp-cust' ), 'edd_trp_cust_render_trp_cust_meta_box', 'download', 'normal', 'high' ); | |
} | |
} | |
add_action( 'add_meta_boxes', 'edd_trp_cust_add_meta_box', 100 ); | |
// render the download information meta box | |
function edd_trp_cust_render_trp_cust_meta_box() { | |
global $post; | |
// Use nonce for verification | |
echo '<input type="hidden" name="edd_download_trp_cust_meta_box_nonce" value="', wp_create_nonce( basename( __FILE__ ) ), '" />'; | |
echo '<table class="form-table">'; | |
// Main Options | |
$enabled = get_post_meta( $post->ID, '_edd_trp_cust_enabled', true ) ? true : false; | |
$meta = get_post_meta( $post->ID, '_edd_trp_cust_settings', true ); | |
$trp_cust_categories = isset( $meta['trp_cust_categories'] ) ? $meta['trp_cust_categories'] : array(); | |
echo '<tr>'; | |
echo '<td class="edd_field_type_text" colspan="2">'; | |
echo '<p><strong>' . __( 'Convert this product into an "Master Licence" product?', 'edd-trp-cust' ) . '</strong></p>'; | |
echo '<input type="checkbox" name="edd_trp_cust_enabled" id="edd_trp_cust_enabled" value="1" ' . esc_attr( checked( true, $enabled, false ) ) . '/> '; | |
echo '<label for="edd_trp_cust_enabled">' . __( 'Yes', 'edd-trp-cust' ) . '</label>'; | |
echo '<td>'; | |
echo '</tr>'; | |
//All Access Categories | |
echo '<tr class="edd_trp_cust_categories_row edd_trp_cust_row">'; | |
echo '<td class="edd_field_type_text" colspan="2">'; | |
echo '<p><strong>' . __( 'Master Licence for:', 'edd-trp-cust' ); | |
echo '<span alt="f223" class="edd-help-tip dashicons dashicons-editor-help" title="' . __( '<strong>Limit by category</strong>: ', 'edd-trp-cust' ) . '"></span>'; | |
echo '</strong></p>'; | |
echo '<label for="edd_trp_cust_meta_trp_cust_categories">' . __( 'To which product categories does the customer get a "Master Licence"', 'edd-trp-cust' ) .'</label><br />'; | |
$categories = get_terms( 'download_category', apply_filters( 'edd_category_dropdown', array() ) ); | |
$options = array(); | |
foreach ( $categories as $category ) { | |
$options[ absint( $category->term_id ) ] = esc_html( $category->name ); | |
} | |
// Default to all categories included | |
if( empty( $trp_cust_categories ) ){ | |
$trp_cust_categories['all'] = __( 'All Products', 'edd-trp-cust' ); | |
} | |
echo EDD()->html->select( array( | |
'options' => $options, | |
'name' => 'edd_trp_cust_meta[trp_cust_categories][]', | |
'selected' => $trp_cust_categories, | |
'id' => 'edd_trp_cust_meta_trp_cust_categories', | |
'class' => 'edd_trp_cust_meta_trp_cust_categories', | |
'chosen' => true, | |
'placeholder' => __( 'Type to search Categories', 'edd-trp-cust' ), | |
'multiple' => true, | |
'show_option_all' => __( 'All Products', 'edd-trp-cust' ), | |
'show_option_none' => false, | |
'data' => array( 'search-type' => 'no_ajax' ) | |
) ); | |
echo '<td>'; | |
echo '</tr>'; | |
echo '</table>'; | |
} | |
/** | |
* Save data from the All Access metabox | |
* | |
* @access public | |
* @since 1.0.0 | |
* @param string $post_id The ID of the post being saved. | |
* @return void | |
*/ | |
function edd_trp_cust_download_meta_box_save( $post_id ) { | |
global $post; | |
// verify nonce | |
if ( ! isset( $_POST['edd_download_trp_cust_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['edd_download_trp_cust_meta_box_nonce'], basename( __FILE__ ) ) ) { | |
return $post_id; | |
} | |
// Check for auto save / bulk edit | |
if ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) { | |
return $post_id; | |
} | |
if ( ( isset( $_POST['post_type'] ) && 'download' != $_POST['post_type'] ) || get_post_type( $post_id ) != 'download' ) { | |
return $post_id; | |
} | |
if ( ! current_user_can( 'edit_product', $post_id ) ) { | |
return $post_id; | |
} | |
if ( false !== wp_is_post_revision( $post_id ) ){ | |
return; | |
} | |
if ( isset( $_POST['edd_trp_cust_enabled'] ) ) { | |
update_post_meta( $post_id, '_edd_trp_cust_enabled', true ); | |
// Also add this post to the list of All Access posts saved into the options table | |
$edd_trp_cust_products = get_option( 'edd_trp_cust_products' ); | |
if ( ! is_array( $edd_trp_cust_products ) ){ | |
$edd_trp_cust_products = array(); | |
} | |
array_push( $edd_trp_cust_products, $post_id ); | |
update_option( 'edd_trp_cust_products', array_unique( $edd_trp_cust_products ) ); | |
$new_trp_cust_meta = isset( $_POST['edd_trp_cust_meta'] ) ? $_POST['edd_trp_cust_meta'] : false; | |
if ( $new_trp_cust_meta ) { | |
$sanitized_values = array(); | |
//Sanitize Values | |
foreach( $_POST['edd_trp_cust_meta'] as $meta_key => $meta_value ){ | |
switch( $meta_key ){ | |
case 'trp_cust_categories': | |
$trp_cust_categories = array(); | |
foreach( $meta_value as $trp_cust_category ){ | |
if( is_numeric( $trp_cust_category ) || $trp_cust_category == 'all' ){ | |
$trp_cust_categories[] = $trp_cust_category; | |
} | |
} | |
$sanitized_values['trp_cust_categories'] = $trp_cust_categories; | |
break; | |
} | |
} | |
update_post_meta( $post_id, '_edd_trp_cust_settings', $sanitized_values ); | |
} | |
} else { | |
delete_post_meta( $post_id, '_edd_trp_cust_enabled' ); | |
// Also remove this post from the list of All Access posts saved into the options table | |
$edd_trp_cust_products = get_option( 'edd_trp_cust_products' ); | |
if( !empty( $edd_trp_cust_products ) ){ | |
foreach( $edd_trp_cust_products as $key => $edd_trp_cust_product ){ | |
if( $edd_trp_cust_product == $post_id ){ | |
unset($edd_trp_cust_products[$key]); | |
break; | |
} | |
} | |
} | |
update_option( 'edd_trp_cust_products', $edd_trp_cust_products ); | |
} | |
} | |
add_action( 'save_post', 'edd_trp_cust_download_meta_box_save' ); | |
/** | |
* When Software Licensing is checking if the passed-in title matches the title attached to the passed-in license in our Store, | |
* Check if the license's product-title is an All Access product which includes the product trying to be accessed/downloaded. If it does, tell | |
* Software Licensing the titles match up. In this way, we can "trick" Software Licensing into accepting a license other than one for the actual product. | |
* In this case, it allows for a "master" license key to be used for products it isn't actually for (outside of the All Access extension). | |
* | |
* @since 1.0.0 | |
* @param bool $item_name_matches | |
* @param int $download_id | |
* @param string $item_name | |
* @param object $license | |
* @return bool $item_name_matches | |
*/ | |
function edd_trp_cust_sl_name_matches( $item_name_matches, $download_id, $item_name, $license ){ | |
// If no license was passed, they might be running an older version of Software Licensing. | |
if ( ! isset( $license ) || empty( $license ) ) { | |
return $item_name_matches; | |
} | |
if( is_numeric($item_name) ){ | |
$update_id = $item_name; | |
} | |
else{ | |
// Decode the item name since it came from a URL | |
$item_name_decode = urldecode( $item_name ); | |
// Get a WP Post object using that decoded title | |
$post_with_title_passed_in = get_page_by_title( $item_name_decode, 'OBJECT', 'download' ); | |
// If we didn't find a post using the decoded title, double check it for special characters not covered by urldecode. | |
if ( ! $post_with_title_passed_in ){ | |
//Do a double check using rawurldecode in case the plugn author used a + sign in the Download's title and used rawurlencode to send the title to us here. | |
$item_name_raw_decode = rawurldecode( $item_name ); | |
$post_with_title_passed_in = get_page_by_title( $item_name_raw_decode, 'OBJECT', 'download' ); | |
// If no WP post objects were found using the passed-in item name, return the incoming value which we got from the filter. | |
if ( ! $post_with_title_passed_in ){ | |
return $item_name_matches; | |
} | |
} | |
$update_id = $post_with_title_passed_in->ID; | |
} | |
if(is_object($license) ){ | |
$trp_cust_check = edd_trp_cust_check( array( | |
'download_id' => $update_id, | |
'licence_download_id' => $license->download_id | |
) ); | |
} | |
// If the customer attached to the license has an All Access pass which includes the desired product | |
if ( isset( $trp_cust_check['success'] ) && $trp_cust_check['success'] ){ | |
return true; | |
} | |
// Otherwise, return the unchanged value that we got from the filter hook | |
return $item_name_matches; | |
} | |
/** | |
* When Software Licensing is checking if the passed-in ID matches the ID attached to the passed-in license in our Store, | |
* Check if the license's ID is an All Access product which includes the product trying to be accessed/downloaded. If it does, tell | |
* Software Licensing the IDs match up. In this way, we can "trick" Software Licensing into accepting a license other than one for the actual product. | |
* In this case, it allows for a "master" license key to be used for products it isn't actually for (outside of the All Access extension). | |
* | |
* @since 1.0.0 | |
* @param bool $license_match | |
* @param int $download_id | |
* @param int $license_download | |
* @param string $license_key | |
* @return bool $license_match | |
*/ | |
function edd_trp_cust_sl_id_matches( $license_match, $download_id, $license_download, $license_key ){ | |
// Check if the license is an All Access license which includes the desired product | |
// Get the customer attached to the license | |
$license = edd_software_licensing()->get_license( $license_key, true ); | |
$trp_cust_check = edd_trp_cust_check( array( | |
'download_id' => $download_id, | |
'licence_download_id' => $license->download_id | |
) ); | |
// If the customer attached to the license has an All Access pass which includes the desired product | |
if ( isset( $trp_cust_check['success'] ) && $trp_cust_check['success'] ){ | |
return true; | |
} | |
// Otherwise, return the unchanged value that we got from the filter hook | |
return $license_match; | |
} | |
/** | |
* Tell Software Licensing if we should check for new version updates using the passed-in license or the passed-in name. | |
* Because the All Access license isn't the product we are hoping to check for updates, we want to force Software Licensing to check using the name. | |
* | |
* @since 1.0.0 | |
* @param bool $check_by_name_first Whether we should fetch update data using the passed-in name or using the passed-in license. | |
* @return bool $check_by_name_first We want to fetch update data using the passed-in name. | |
*/ | |
function edd_trp_cust_sl_force_check_by_name( $check_by_name_first ){ | |
return true; | |
} | |
function edd_trp_cust_check( $args = array() ){ | |
$default_args = array( | |
'download_id' => false, | |
'licence_download_id' => false, | |
); | |
$args = wp_parse_args( $args, $default_args ); | |
//Set up the default return data | |
$return_data = array( | |
'success' => false, | |
'failure_id' => 'failure_by_default', | |
'failure_message' => __( 'For some unknown reason, this user does not have Master Licence for this product.', 'edd-trp-cust' ), | |
); | |
/* check to see if this is a Master Licence */ | |
$edd_trp_cust_products = get_option( 'edd_trp_cust_products' ); | |
if( !in_array( $args['licence_download_id'], $edd_trp_cust_products ) ){ | |
$return_data = array( | |
'success' => false, | |
'failure_id' => 'not_a_master_licence', | |
'failure_message' => __( 'This is not a Master Licence', 'edd-trp-cust' ), | |
); | |
return $return_data; | |
} | |
$master_licence_settings = get_post_meta( $args['licence_download_id'], '_edd_trp_cust_settings', true ); | |
if( empty( $master_licence_settings ) ){ | |
$return_data = array( | |
'success' => false, | |
'failure_id' => 'no_master_licence_settings', | |
'failure_message' => __( 'This Master Licence has no settings', 'edd-trp-cust' ), | |
); | |
return $return_data; | |
} | |
else{ | |
if( is_array( $master_licence_settings['trp_cust_categories'] ) ){ | |
if( in_array( 'all', $master_licence_settings['trp_cust_categories'] ) ){ | |
$return_data = array( | |
'success' => true | |
); | |
return $return_data; | |
} | |
else{//check if the download_id is in the Master Licence Category | |
// Get the product-being-downloaded's information | |
$download_categories = wp_get_post_terms( $args['download_id'], 'download_category' ); | |
$has_required_category = false; | |
// Loop through each acceptable/included download category | |
foreach( $master_licence_settings['trp_cust_categories'] as $included_category ){ | |
foreach( $download_categories as $download_category ){ | |
if ( $included_category == $download_category->term_id ){ | |
$has_required_category = true; | |
break; | |
} | |
} | |
} | |
if( $has_required_category ){ | |
$return_data = array( | |
'success' => true | |
); | |
return $return_data; | |
} | |
else{ | |
$return_data = array( | |
'success' => false, | |
'failure_id' => 'not_in_the_master_licence_category', | |
'failure_message' => __( 'This product is not in the Master Licences category', 'edd-trp-cust' ), | |
); | |
return $return_data; | |
} | |
} | |
} | |
return $return_data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment