Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created September 26, 2018 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/bafee02e2366df11adf061e822e79bd2 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/bafee02e2366df11adf061e822e79bd2 to your computer and use it in GitHub Desktop.
Adds an extra page to manually add a referred user to an affiliate.
<?php
/**
* Plugin Name: Affiliate - Manual Add
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds an extra page to manually add a referred user to an affiliate.
* Version: 1.0.0
* Author: Konstantinos Xenos @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Affiliate_Manually_Add' ) ) {
class Affiliate_Manually_Add {
/**
* Constructor.
*/
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
/**
* Initialization.
*/
public function init() {
add_action( 'admin_menu', array( $this, 'create_menus' ) );
add_action( 'network_admin_menu', array( $this, 'create_menus' ) );
add_action( 'wp_ajax_affiliate-manaff-adref', array( $this, 'save_ref' ), 10, 3 );
}
/**
* Create the menus
*/
public function create_menus() {
if ( function_exists( 'is_network_admin' ) && is_network_admin() ) {
$capabilty = 'manage_network_options';
} else {
$capabilty = 'manage_options';
}
add_submenu_page(
'affiliatesadmin',
__( 'Manually Add', 'affiliate' ),
__( 'Manually Add', 'affiliate' ),
$capabilty,
'affiliatesadminmanuallyadd',
array( $this, 'create_page' )
);
}
/**
* Create the Page
*/
public function create_page() {
global $wpdb;
?>
<div class="wrap">
<h2><?php esc_html_e( 'Manually Add Referrer', 'affiliate' ); ?></h2>
<form class="manuallreferrer" name="manuallreferrer" method="POST">
<table class="form-table">
<tr>
<th><label for="affid"><?php esc_html_e( 'Affiliate', 'affiliate' ); ?></label></th>
<td>
<select name="affid" id="affid">
<?php
$affs = get_users(
array(
'meta_key' => 'enable_affiliate',
'meta_value' => 'yes',
)
);
foreach ( $affs as $aff ) {
echo '<option value="' . $aff->ID . '">' . esc_html( $aff->user_login ) . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<th><label for="refid"><?php esc_html_e( 'Referred', 'affiliate' ); ?></label></th>
<td>
<select name="refid" id="refid">
<?php
$refs = get_users();
foreach ( $refs as $ref ) {
echo '<option value="' . $ref->ID . '">' . esc_html( $ref->user_login ) . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td colspan="2" style="padding-left:0;">
<input type="hidden" name="nonce" id="nonce" value="<?php echo wp_create_nonce( 'ref' . get_current_user_id() ); ?> ">
<button type="submit" class="button button-primary"><?php esc_html_e( 'Submit', 'affiliate' ); ?></button>
</td>
</tr>
</table>
</form>
</div>
<script>
( function ( $ ) {
$( document ).ready(function() {
$( '.manuallreferrer' ).on( 'submit', function( e ) {
var affid = $( '#affid' ).val(),
refid = $( '#refid' ).val(),
nonce = $( '#nonce' ).val();
e.preventDefault();
data = {
'action': 'affiliate-manaff-adref',
'affid': affid,
'refid': refid,
'nonce': nonce
};
$.post( ajaxurl, data, function( response ) {
if ( response.success ) {
window.location.href = window.location.href;
} else {
console.log( 'invalid nonce' );
}
} );
} );
} );
} ( jQuery ) );
</script>
<?php
}
/**
* Ajax Call
*/
public function save_ref() {
$affid = intval( $_POST['affid'] );
$refid = intval( $_POST['refid'] );
$nonce = sanitize_text_field( $_POST['nonce'] );
if ( ! wp_verify_nonce( $nonce, 'ref' . get_current_user_id() ) ) {
wp_send_json_error();
}
update_user_meta( $refid, 'affiliate_referred_by', $affid );
$aff = new affiliate();
$rec = $aff->record_signup(
$affiliate_user_id = $affid,
$amount = false,
$area = 'signup:user',
$area_id = $refid,
$note = 'User',
$meta = false
);
wp_send_json_success();
}
}
new Affiliate_Manually_Add();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment