Skip to content

Instantly share code, notes, and snippets.

@arraypress
Created May 5, 2021 18:39
Show Gist options
  • Save arraypress/ca0248bc51208bb3499442e958fa3ce5 to your computer and use it in GitHub Desktop.
Save arraypress/ca0248bc51208bb3499442e958fa3ce5 to your computer and use it in GitHub Desktop.
Reward new user registrations with store credit.
<?php
/**
* Plugin Name: ArrayPress - Easy Digital Downloads Sign Up Bonus
* Plugin URI: https://arraypress.com
* Description: Reward new user registrations with store credit.
* Author: David Sherlock
* Author URI: https://arraypress.com
* Text Domain: edd-sign-up-bonus
* Domain Path: /languages/
* Requires PHP: 5.6.20
* Requires at least: 5.7.1
* Version: 1.0.0
*/
namespace ArrayPress\Easy_Digital_Downloads\Sign_Up_Bonus;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Internationalization
*
* @return void
* @since 1.0.0
*/
function load_textdomain() {
load_plugin_textdomain( 'edd-sign-up-bonus', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'init', __NAMESPACE__ . '\\load_textdomain' );
/**
* Registers the "Sign Up Bonus" extension subsection for EDD Settings
*
* @param array $sections The base sections
*
* @return array $sections with our new section added
* @since 1.0.0
*/
function register_settings_section( $sections ) {
$sections[ 'sign_up_bonus' ] = __( 'Sign Up Bonus', 'edd-sign-up-bonus' );
return $sections;
}
add_filter( 'edd_settings_sections_extensions', __NAMESPACE__ . '\\register_settings_section' );
/**
* Registers the new "Sign Up Bonus" options in Extensions
*
* @param array $settings The existing plugin settings
*
* @return array $settings The new EDD settings array with "Sign Up Bonus" added
* @since 1.0.0
*/
function register_settings( $settings ) {
$new_settings = array(
array(
'id' => 'edd_sign_up_bonus_header',
'name' => '<strong>' . __( 'Sign Up Bonus Settings', 'edd-sign-up-bonus' ) . '</strong>',
'desc' => '',
'type' => 'header',
'size' => 'regular',
),
array(
'id' => 'edd_sign_up_bonus_amount',
'name' => __( 'Amount', 'edd-sign-up-bonus' ),
'desc' => __( 'Enter the amount new user\'s should receive when registering an account.', 'edd-sign-up-bonus' ),
'type' => 'text',
'size' => 'small',
),
);
if ( version_compare( EDD_VERSION, 2.5, '>=' ) ) {
$new_settings = array( 'sign_up_bonus' => $new_settings );
}
return array_merge( $settings, $new_settings );
}
add_filter( 'edd_settings_extensions', __NAMESPACE__ . '\\register_settings' );
/**
* Maybe deposit funds to user's wallet on new user registration
*
* @param int $user_id The ID of the newly registered user
* @param array $user_data Array of user data
*
* @return void
* @since 1.0.0
*/
function maybe_process_sign_up_bonus( $user_id, $user_data ) {
if ( ! class_exists( 'EDD_Wallet' ) || ! function_exists( 'EDD' ) || empty( $user_id ) ) {
return;
}
// Get the sign up amount from the settings
$amount = edd_get_option( 'edd_sign_up_bonus_amount', false );
// Bail early if amount empty
if ( empty( $amount ) ) {
return;
}
// Bail if the user does not exist
$user = get_user_by( 'id', $user_id );
if ( ! $user ) {
return;
}
// Lets create a customer record if one does not already exist
$customer = EDD()->customers->get_customer_by( 'email', $user->user_email );
if ( ! $customer ) {
$customer_id = EDD()->customers->add( array(
'user_id' => $user->ID,
'email' => $user->user_email,
'name' => ( ! empty( $user->display_name ) ? $user->display_name : $user->user_nicename )
) );
}
// Let's get the customer ID, either the newly added customer or existing
$customer_id = ! empty( $customer_id ) ? $customer_id : $customer->id;
// Instantiate customer object
$customer = new \EDD_Customer( $customer_id );
// Deposit the funds to the user wallet
edd_wallet()->wallet->deposit( $user->ID, floatval( $amount ), 'deposit' );
// Lets record a customer note so we know the sign up bonus has been rewarded to this customer
$customer_note = sprintf(
__( 'Sign up bonus of %s awarded for new user registration', 'edd-sign-up-bonus' ),
edd_currency_filter( edd_format_amount( $amount ) )
);
$customer->add_note( $customer_note );
}
add_action( 'edd_insert_user', __NAMESPACE__ . '\\maybe_process_sign_up_bonus', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment