Skip to content

Instantly share code, notes, and snippets.

@webzunft
Last active December 10, 2019 10:06
Show Gist options
  • Save webzunft/07e473d3e946e316d987044ff9eae260 to your computer and use it in GitHub Desktop.
Save webzunft/07e473d3e946e316d987044ff9eae260 to your computer and use it in GitHub Desktop.
Extend edd_aa_all_access() function in EDD All Access to work for multiple All Access passes
<?php
/**
* Shortcode which can be used to easily give a user the option to log in or purchase an All Access Pass.
* If the user is already logged in but does not have a valid All Access Pass, they will see a buy button.
* If the user is both logged in and has a valid All Access Pass, they will be redirected to the page defined by the shortcode args.
* Can also be used to restrict content.
*
* @since 1.0.0
* @param array $atts Shortcode attributes
* @param string $content
* @return string Shortcode Output
*/
public function edd_aa_all_access( $atts, $content = null ) {
global $post;
$post_id = is_object( $post ) ? $post->ID : 0;
$atts = shortcode_atts( array(
'id' => $post_id,
'price_id' => isset( $atts['price_id'] ) ? $atts['price_id'] : false,
'sku' => '',
'price' => true,
'direct' => '0',
'text' => '',
'style' => edd_get_option( 'button_style', 'button' ),
'color' => edd_get_option( 'checkout_color', 'blue' ),
'class' => 'edd-submit',
'form_id' => '',
'popup_login' => true,
'buy_instructions' => '',
'login_instructions' => '',
'login_btn_style' => 'text',
'preview_image' => '',
'success_redirect_url' => '',
'success_text' => ''
),
$atts, 'all_access' );
/**
* $atts[id] can be comma-separated. edd_all_access_user_has_pass() can handle that
*/
$customer_has_all_access_pass = edd_all_access_user_has_pass( get_current_user_id(), $atts['id'], $atts['price_id'] );
/**
* Get possible All Access download IDs if separated by comma
* the first of them is used to create the buy button later
*/
$download_ids = explode( ',', $atts['id'] );
$main_download_id = isset( $download_ids[ 0 ] ) ? $download_ids[ 0 ] : 0;
$preview_area_html = '';
$login_purchase_area = '';
$success_html = '';
// If the customer does not have access and a preview image has been provided
if ( ! $customer_has_all_access_pass && !empty( $atts['preview_image'] ) ){
$preview_area_html .= '<div class="edd-aa-preview-area"><img class="edd-aa-preview-img" src="' . esc_url( $atts['preview_image'] ) . '" /></div>';
}
// If this customer has this All Access Pass and it is valid
if ( $customer_has_all_access_pass ){
// If success content has been passed in use that
if ( !empty( $content ) ){
error_log( print_r( 'has content', true ) );
$success_html .= do_shortcode( $content );
}else{
// Set up success text if it exists
$success_html .= empty( $atts['success_text'] ) ? __( 'You have an All Access Pass for', 'edd-all-access' ) . ' ' . get_the_title( $customer_has_all_access_pass->download_id ) : $atts['success_text'];
}
// Redirect the user if shortcode has it set.
if ( !empty( $atts['success_redirect_url'] ) ){
// Prevent redirect loops
if ( ! isset( $_GET['redirect-from-aa'] ) ){
// Redirect the user to the redirection page provided by the shortcode args.
$success_html .= '<script type="text/javascript">window.location.replace("' . esc_url( add_query_arg( array( 'redirect-from-aa' => true ), $atts['success_redirect_url'] ) ) . '");</script>';
}
}
}else{
$all_access_buy_or_login_atts = array(
'all_access_download_id' => $main_download_id, // the first All Access download ID, if multiple are given
'all_access_price_id' => $atts['price_id'],
'all_access_sku' => $atts['sku'],
'all_access_price' => $atts['price'],
'all_access_direct' => $atts['direct'],
'all_access_btn_text' => $atts['text'],
'all_access_btn_style' => $atts['style'],
'all_access_btn_color' => $atts['color'],
'all_access_btn_class' => $atts['class'],
'all_access_form_id' => $atts['form_id'],
'class' => 'edd-aa-login-purchase-aa-only-mode',
'popup_login' => $atts['popup_login'],
'buy_instructions' => $atts['buy_instructions'],
'login_instructions' => $atts['login_instructions'],
'login_btn_style' => $atts['login_btn_style'],
'preview_image' => $atts['preview_image'],
);
// Customer does not have All Access Pass. Output buy / login form.
$login_purchase_area = edd_all_access_buy_or_login_form( $all_access_buy_or_login_atts );
}
$output_array = apply_filters( 'edd_all_access_shortcode_outputs', array(
'preview_area' => $preview_area_html,
'login_purchase_area' => $login_purchase_area,
'success_output' => $success_html
), $atts );
// Set html output wrapper
$html_output = '<div class="edd-aa-wrapper">';
foreach( $output_array as $chunk_name => $output_chunk ){
// Make sure success_output is only shown if the customer has access
if ( $chunk_name == 'success_output' && ! $customer_has_all_access_pass ){
continue;
}
$html_output .= $output_chunk;
}
$html_output .= '</div>';
return $html_output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment