Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creates a custom myCRED hook to award creds on Restrict content pro subscription renewal
<?php
add_filter( 'mycred_setup_hooks', 'custom_hook_renew_rcpro_subscription' );
function custom_hook_renew_rcpro_subscription( $installed )
{
$installed['hook_rcpro_renew_subs'] = array(
'title' => __( '%_plural% for RCP Subscription renewal', 'mycred' ),
'description' => __( 'Award %_plural% when RCP subscription is renewed', 'mycred' ),
'callback' => [ 'myCRED_Hook_Rcpro_Renew' ]
);
return $installed;
}
if ( !class_exists( 'myCRED_Hook_Rcpro_Renew' ) ) {
Class myCRED_Hook_Rcpro_Renew extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs, $type ) {
parent::__construct( array(
'id' => 'hook_rcpro_renew_subs',
'defaults' => array(
'creds' => 1,
'log' => '%plural% for RCP subscription renewal'
)
), $hook_prefs, $type );
}
/**
* Hook into WordPress
*/
public function run() {
add_action( 'rcp_membership_post_renew', array( $this, 'rcp_membership_renew' ), 10, 3 );
}
/**
* Check if the user qualifies for points
*/
public function rcp_membership_renew( $expiration, $membership_id, $membership ) {
$user_id = $membership->get_customer()->get_user_id();
// Check if user is excluded (required)
if ( $this->core->exclude_user( $user_id ) ) return;
// Make sure this is a unique event
if ( $this->has_entry( 'hook_rcpro_renew_subs', '', $user_id ) ) return;
// Execute
$this->core->add_creds(
'hook_rcpro_renew_subs',
$user_id,
$this->prefs['creds'],
$this->prefs['log'],
$membership_id,
[ 'ref_type' => 'rcp_membership' ],
$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 esc_attr( $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 esc_attr( $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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.