Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Award MyCRED points for members who sign up for Membership Level 1.
<?php
/*
Use this recipe in combination with MyCRED to award points to members when signing up
for Level 1 membership. This code gist could be customized to give points for another
membership level ID, award recurring points for each subscription payment, and more.
MyCRED can be downloaded/configured here: https://wordpress.org/plugins/mycred/
*/
// Register Hook for PMPro Membership Points at Signup
function pmpromyCRED_register_my_custom_hook( $installed )
{
$installed['pmpromyCRED_membership_level_1_checkout'] = array(
'title' => __( '%plural% for Membership Signup', 'pmpromycred' ),
'description' => __( 'This hook awards points for users who sign up for Membership Level 1.', 'pmpromycred' ),
'callback' => array( 'pmpromyCRED_hook_class' )
);
return $installed;
}
add_filter( 'mycred_setup_hooks', 'pmpromyCRED_register_my_custom_hook' );
// setup myCRED PMPro Membership Hook Class on init
function pmpromyCRED_init() {
//make sure myCRED is loaded
if(!class_exists('myCRED_Hook'))
return;
class pmpromyCRED_hook_class extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs, $type = 'mycred_default' ) {
parent::__construct( array(
'id' => 'pmpromyCRED_membership_level_1_checkout',
'defaults' => array(
'creds' => 10,
'log' => '%plural% for signing up for Membership Level 1'
)
), $hook_prefs, $type );
}
/**
* Hook into WordPress
*/
public function run() {
add_action( 'pmpro_after_checkout', array( $this, 'pmpromyCRED_membership_level_1_checkout' ) );
}
/**
* Check if the user qualifies for points
*/
public function pmpromyCRED_membership_level_1_checkout( $user_id ) {
// Check if user is excluded (required)
if ( $this->core->exclude_user( $user_id ) ) return;
// Check to see if user has signed up for Membership Level 1
if ( !pmpro_hasMembershipLevel('1', $user_id) ) return;
// Make sure this is a unique event
if ( $this->has_entry( 'pmpromyCRED_membership_level_1_checkout_complete', '', $user_id ) ) return;
// Execute
$this->core->add_creds(
'pmpromyCRED_membership_level_1_checkout_complete',
$user_id,
$this->prefs['creds'],
$this->prefs['log'],
'',
'',
$this->mycred_type
);
}
/**
* Add Settings
*/
public function preferences() {
// Our settings are available under $this->prefs
$prefs = $this->prefs; ?>
<!-- First we set the amount -->
<label class="subheader"><?php echo $this->core->plural(); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'creds' ); ?>" id="<?php echo $this->field_id( 'creds' ); ?>" value="<?php echo $this->core->format_number( $prefs['creds'] ); ?>" size="8" /></div>
</li>
</ol>
<!-- Then the log template -->
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo $prefs['log']; ?>" class="long" /></div>
</li>
</ol>
<?php
}
/**
* Sanitize Preferences
*/
public function sanitise_preferences( $data ) {
$new_data = $data;
// Apply defaults if any field is left empty
$new_data['creds'] = ( !empty( $data['creds'] ) ) ? $data['creds'] : $this->defaults['creds'];
$new_data['log'] = ( !empty( $data['log'] ) ) ? sanitize_text_field( $data['log'] ) : $this->defaults['log'];
return $new_data;
}
}
}
add_action('init', 'pmpromyCRED_init');
// Update the Confirmation Message with MyCRED Balance
function pmpromyCRED_pmpro_confirmation_message($message)
{
global $current_user;
if ( mycred_count_ref_id_instances( 'pmpromyCRED_membership_level_1_checkout_complete', 'mycred-hook-pmpromyCRED_membership_level_1_checkout', $current_user->ID ) > 0 )
{
$message .= '<hr /><h3>Points Awarded</h3>' . do_shortcode( '[mycred_history time="today" user_id="current"]' ) . '<hr />';
}
return $message;
}
add_filter("pmpro_confirmation_message", "pmpromyCRED_pmpro_confirmation_message");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment