Created
March 28, 2014 14:27
-
-
Save gabrielmerovingi/9834118 to your computer and use it in GitHub Desktop.
Example of adding custom details to buyCRED purchases.
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
/** | |
* Step 1 - Create custom Shortcode | |
* Creates a custom buyCRED shortcode that includes | |
* the marker attribute that is passed on to the gateway. | |
* @version 1.0 | |
*/ | |
add_shortcode( 'mycred_custom_buy', 'mycred_render_custom_buy_shortcode' ); | |
function mycred_render_custom_buy_shortcode( $atts, $title = 'Buy Points' ) { | |
if ( ! function_exists( 'mycred' ) ) return 'myCRED is not installed'; | |
extract( shortcode_atts( array( | |
'gateway' => '', | |
'amount' => '', | |
'gift_to' => false, | |
'marker' => '', | |
'class' => 'mycred-buy-link button large custom', | |
'login' => 'Please login to buy points' | |
), $atts ) ); | |
// Load myCRED | |
$mycred = mycred(); | |
// If we are not logged in | |
if ( ! is_user_logged_in() ) return '<div class="mycred-buy login">' . $mycred->template_tags_general( $login ) . '</div>'; | |
$url = get_bloginfo( 'url' ) . '/'; | |
$args = array( | |
'mycred_buy' => $gateway, | |
'amount' => $mycred->number( $amount ), | |
'token' => wp_create_nonce( 'mycred-buy-creds' ), | |
'marker' => $marker | |
); | |
// Classes | |
$classes = explode( ' ', $class ); | |
if ( empty( $classes ) ) | |
$classes = array( 'mycred-buy-link', 'button large', 'custom' ); | |
$classes[] = $gateway; | |
$title = $mycred->template_tags_general( $title ); | |
// Element to return | |
$element = '<a href="' . add_query_arg( $args, $url ) . '" class="' . implode( ' ', $classes ) . '" title="' . $title . '">' . $title . '</a>'; | |
return $element; | |
} | |
/** | |
* Step 2 - Inject Marker | |
* Next we inject our marker into the data sent to the gateway. | |
* You would need to add a filter for each gateway you want this to support. | |
* filter: mycred_{gateway id}_extra. Replace gateway with the gateway name. | |
* Replace - with _ and remember to always return a result! | |
* @version 1.0 | |
*/ | |
add_filter( 'mycred_paypal_standard_extra', 'mycred_inject_marker_for_gateway', 10, 6 ); | |
function mycred_inject_marker_for_gateway( $extra, $cost, $from, $to, $gateway_prefs, $mycred ) { | |
// Only add marker if it is set and not empty | |
if ( isset( $_REQUEST['marker'] ) && ! empty( $_REQUEST['marker'] ) ) | |
return sanitize_text_field( $_REQUEST['marker'] ); | |
return $extra; | |
} | |
/** | |
* Step 3 - Insert a custom payment column | |
* Finally we will insert a custom column on the payment log to | |
* show if a marker has been used for this payment. | |
* @version 1.0 | |
*/ | |
add_filter( 'mycred_buycred_log_columns', 'mycred_adjust_purchase_log_columns' ); | |
function mycred_adjust_purchase_log_columns( $columns ) { | |
$columns['column-marker'] = 'Marker'; | |
return $columns; | |
} | |
add_action( 'mycred_payment_log_column-marker', 'mycred_payment_column_marker' ); | |
function mycred_payment_column_marker( $log_entry ) { | |
$data = maybe_unserialize( $log_entry->data ); | |
if ( isset( $data['sales_data'] ) ) { | |
list ( $buyer_id, $payer_id, $amount, $cost, $currency, $token, $other ) = explode( '|', $data['sales_data'] ); | |
// Marker is saved under "other" last in the array. | |
echo $other; | |
} | |
else { | |
echo 'n/a'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment