Last active
February 26, 2021 18:35
-
-
Save alexstandiford/c0392f710b610f6703f92dc5cb829f77 to your computer and use it in GitHub Desktop.
Example Plugin Affiliate Dashboard
This file contains hidden or 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
<?php | |
/** | |
* Plugin Name: Affiliate Order Detail Notes | |
* Text Domain: affwp_od | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
use \AffiliateWP_Affiliate_Portal\Core; | |
/** | |
* Registers the controls for the order details screen. | |
* | |
* @since 1.0.0 | |
* | |
* @param Core\Controls_Registry $controls_registry The controls registry instance. | |
*/ | |
function affwp_od_register_controls( $controls_registry ) { | |
/** | |
* Create controls. | |
* A control is what determines where, when, and how HTML is rendered in Affiliate Portal. | |
**/ | |
// Instantiate a textarea control. This instructs Affiliate Portal on how to create the order notes text field. | |
$textarea_control = new Core\Components\Controls\Textarea_Control( array( | |
'id' => 'affiliate_wp_order_notes', // Unique ID, specific to this control. | |
'view_id' => 'order-details', // The view in which this control should display. | |
'section' => 'order-details-single', // The section in which this control should display. | |
'args' => array( | |
'label' => __( 'Order Notes', 'affwp_od' ), // The input label. | |
'get_callback' => 'affwp_od_get_order_notes', // The callback used to display this input. | |
'save_callback' => 'affwp_od_update_order_notes', // The callback used when saving this input. | |
), | |
) ); | |
// Instantiate a checkbox control. This instructs Affiliate Portal on how to create the checkbox that controls | |
// if this field should be shared with the affiliate manager. | |
$share_content_control = new Core\Components\Controls\Checkbox_Control( array( | |
'id' => 'affiliate_wp_order_share', | |
'view_id' => 'order-details', | |
'section' => 'order-details-single', | |
'args' => array( | |
'label' => __( 'Share Details With Manager?', 'affwp_od' ), // The input label. | |
'desc' => __( 'Check this box to share these details with the affiliate manager.', 'affwp_od' ), // The description to display under the checkbox. | |
'get_callback' => '__return_false', // The callback used when getting this input. | |
'save_callback' => 'affwp_od_update_order_shared' // The callback used when saving this input. | |
), | |
'atts' => array( | |
'checked' => false, //TODO: Make this checkbox checked status update appropriately. See AD issue #383 | |
), | |
) ); | |
// Create a hidden control. This instructs Affiliate Portal on how to create a hidden input that stores the referral ID. | |
// We will use this referral ID when saving the form later. | |
$hidden_control = new Core\Components\Controls\Text_Input_Control( array( | |
'id' => 'affiliate_wp_order_notes_order_id', // Unique ID, specific to this control. | |
'view_id' => 'order-details', // The view in which this control should display. | |
'section' => 'order-detail-notes', // The section in which this control should display. | |
'args' => array( | |
'get_callback' => 'affwp_od_get_current_order_id', // The callback used to display this input. | |
'save_callback' => '__return_false', // The callback used when saving this input. | |
), | |
'atts' => array( | |
'readonly' => true, // Set this field as readonly. | |
'type' => 'number', // Set the field type as a number | |
'class' => array( 'hidden' ), // Hide the field. | |
), | |
) ); | |
/** | |
* Register our controls. | |
* This instructs Affiliate Portal to use these controls when rendering the page. | |
**/ | |
// Register the textarea | |
$controls_registry->add_control( $textarea_control ); | |
// Register the checkbox field | |
$controls_registry->add_control( $share_content_control ); | |
// Register the hidden field. | |
$controls_registry->add_control( $hidden_control ); | |
} | |
function affwp_od_get_order_shared( $referral_id ) { | |
return (bool) affwp_get_referral_meta( $referral_id, 'affwp_od_shared', true ); | |
} | |
function affwp_od_update_order_shared( $value ) { | |
$shared = $value === 'on'; | |
$referral_id = (int) $_POST['affiliate_wp_order_notes_order_id']; | |
return affwp_update_referral_meta( $referral_id, 'affwp_od_shared', $shared ); | |
} | |
/** | |
* Retrieves the order notes. | |
* | |
* @since 1.0.0 | |
* | |
* @return string the custom order notes for the current screen. | |
*/ | |
function affwp_od_get_order_notes() { | |
return affwp_od_get_details( affwp_od_get_current_order_id() ); | |
} | |
/** | |
* Updates the order notes on-submit. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $value The value to set. | |
*/ | |
function affwp_od_update_order_notes( $value ) { | |
// Grab the referral ID from POST data. | |
$referral_id = (int) $_POST['affiliate_wp_order_notes_order_id']; | |
// Get the referral from the referral ID. We'll use this to validate the submission. | |
$referral = affwp_get_referral( $referral_id ); | |
// Validate the referral before updating. | |
// If the referral exists and the referral affiliate is the same as the affiliate who initiated this request... | |
if ( false !== $referral && $referral->affiliate_id === (int) affwp_get_affiliate_id() ) { | |
//...Then update the referral meta with the submitted value. | |
affwp_update_referral_meta( $referral_id, 'custom_order_notes', strip_tags( $value ) ); | |
} | |
} | |
/** | |
* Retrieves the order details from the current screen. | |
* | |
* @since 1.0.0 | |
* | |
* @return int the order ID. | |
*/ | |
function affwp_od_get_current_order_id() { | |
// The referral ID is stored as a query var, so let's fetch it from there. | |
return (int) get_query_var( 'affwp_odfa_order' ); | |
} | |
/** | |
* Fetch the order details for the specified referral ID | |
* | |
* @param int $referral_id The referral ID | |
* | |
* @return string the referral details. | |
*/ | |
function affwp_od_get_details( $referral_id ) { | |
return affwp_get_referral_meta( $referral_id, 'custom_order_notes', true ); | |
} | |
/** | |
* Echos the output for the edit referrals screen. | |
* | |
* @since 1.0.0 | |
* | |
* @param AffWP\Referral $referral Referral object | |
*/ | |
function affwp_od_display_order_details( $referral ) { | |
// If this referral is not shared with the affiliate manager, bail. | |
if ( ! affwp_od_get_order_shared( $referral->referral_id ) ) { | |
return ''; | |
} | |
$output = ' | |
<table class="form-table"><tr class="row"> | |
<th scope="row"> | |
<label for="status">%s</label> | |
</th> | |
<td> | |
%s | |
</td> | |
</tr></table> | |
'; | |
echo sprintf( $output, __( 'Order Details', 'affwp_od' ), affwp_od_get_details( $referral->referral_id ) ); | |
} | |
// Add the description to the edit referral screen. | |
add_action( 'affwp_edit_referral_bottom', 'affwp_od_display_order_details' ); | |
// Register controls. | |
add_action( 'affwp_portal_controls_registry_init', 'affwp_od_register_controls' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment