/** * Retrieve referral data * * @since 1.1 */ public function get_data() { $dates = affwp_get_report_dates(); $start = $dates['year'] . '-' . $dates['m_start'] . '-' . $dates['day'] . ' 00:00:00'; $end = $dates['year_end'] . '-' . $dates['m_end'] . '-' . $dates['day_end'] . ' 23:59:59'; $date = array('start' => $start, 'end' => $end); $affiliates = affiliate_wp()->affiliates->get_affiliates(array('orderby' => 'date_registered', 'order' => 'ASC', 'number' => -1, 'date' => $date)); $affiliate_data = array(); $affiliate_data[] = array(strtotime($start) * 1000); $affiliate_data[] = array(strtotime($end) * 1000); if ($affiliates) { foreach ($affiliates as $affiliate) { if ('today' == $dates['range'] || 'yesterday' == $dates['range']) { $point = strtotime($affiliate->date_registered) * 1000; $affiliate_data[$point] = array($point, 1); } else { $time = date('Y-n-d', strtotime($affiliate->date_registered)); $timestamp = strtotime($time) * 1000; if (array_key_exists($time, $affiliate_data) && isset($affiliate_data[$time][1])) { $count = $affiliate_data[$time][1] += 1; $affiliate_data[$time] = array($timestamp, $count); } else { $affiliate_data[$time] = array($timestamp, 1); } } } } $data = array(__('Affiliate Registrations', 'affiliate-wp') => $affiliate_data); return $data; }
public function do_affiliates($step = 1) { global $wpdb; $offset = ($step - 1) * 100; $affiliates = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}affiliates_tbl LIMIT {$offset}, 100;"); $to_delete = array(); if ($affiliates) { foreach ($affiliates as $affiliate) { if (empty($affiliate->email)) { continue; } $user = get_user_by('email', $affiliate->email); if (is_wp_error($user) || !$user) { $user_id = wp_insert_user(array('user_email' => $affiliate->email, 'first_name' => $affiliate->firstname, 'last_name' => $affiliate->lastname, 'user_url' => $affiliate->website, 'user_pass' => '', 'user_login' => $affiliate->email)); } else { $user_id = $user->ID; } $payment_email = !empty($affiliate->paypalemail) ? $affiliate->paypalemail : $affiliate->email; $status = 'approved' == $affiliate->account_status ? 'active' : 'pending'; $args = array('date_registered' => date('Y-n-d H:i:s', strtotime($affiliate->date)), 'user_id' => $user_id, 'payment_email' => $payment_email, 'rate' => $affiliate->commissionlevel, 'status' => $status); // Try to get an existing affiliate based on the user_id $existing_affiliate = affiliate_wp()->affiliates->get_by('user_id', $user_id); if ($existing_affiliate) { continue; } // Insert a new affiliate - we need to always insert to make sure the affiliate_ids will match $id = affiliate_wp()->affiliates->insert($args, 'affiliate'); } return true; } else { // No affiliates found, so all done return false; } }
/** * Reject referrals when the transaction is refunded * * @access public * @since 1.5 */ public function revoke_referral_on_refund($txn) { if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } $this->reject_referral($txn->id); }
/** * Get allowed products * * @since 1.0 */ function affwp_allowed_products_get_products() { $products = affiliate_wp()->settings->get('allowed_products'); $products = explode(',', $products); $products = array_filter(array_map('trim', $products)); return $products; }
public function revoke_referral_on_delete($order_id = 0, $order) { if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } $this->reject_referral($order_id); }
public function revoke_referral_on_refund($data) { if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } $reference = $data['member_id'] . '|' . $data['order_number'] . '-' . $data['order_transaction_id']; $this->reject_referral($reference); }
function setUp() { parent::setUp(); $args = array('user_id' => 1); $this->_affiliate_id = affiliate_wp()->affiliates->add($args); $args = array('affiliate_id' => $this->_affiliate_id, 'amount' => 10, 'status' => 'pending', 'context' => 'tests', 'custom' => 4, 'reference' => 5); $this->_referral_id = affiliate_wp()->referrals->add($args); }
public function revoke_referral_on_refund($order_id = 0, $current_status, $previous_status, $order) { if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } if ($order->is_refunded() || $order->is_payment_declined()) { $this->reject_referral($order_id); } }
function test_add_affiliate() { $args = array('user_id' => 1); $affiliate_id = affiliate_wp()->affiliates->add($args); $this->assertFalse($affiliate_id); $args = array('user_id' => 2); $this->_affiliate_id2 = affiliate_wp()->affiliates->add($args); $this->assertGreaterThan(0, $this->_affiliate_id2); }
/** * Revokes a referral when the payment is deleted * * @access public * @since 1.0 */ public function revoke_referral_on_delete($payment_id = 0) { if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } $payments = new RCP_Payments(); $payment = $payments->get_payment($payment_id); $this->reject_referral($payment->subscription_key); }
/** * Handle refunds * @param $old_invoice * @param $new_post * @since 1.7.5 */ public function track_refund($old_invoice, $new_post) { if ($new_post['post_status'] !== 'refund') { return; } if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } $this->reject_referral($old_invoice['invoice_id']); }
/** * Revoke a referral when a submission is deleted or trashed * * @access private * @since 1.6 */ public function revoke_referral_on_delete($sub_id = 0) { if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } if ('nf_sub' != get_post_type($sub_id)) { return; } $this->reject_referral($sub_id); }
/** * Plugin Name: AffiliateWP - Append Affiliate ID To Jetpack Sharing Links * Plugin URI: http://affiliatewp.com * Description: Automatically appends an affiliate's ID to Jetpack sharing links. * Author: Andrew Munro, Sumobi * Author URI: http://sumobi.com * Version: 1.0 */ function affwp_custom_append_affiliate_id_to_jetpack_sharing_links($permalink, $post_id, $id) { // return if non-affiliate if (!(is_user_logged_in() && affwp_is_affiliate())) { return $permalink; } // append referral variable and affiliate ID to sharing links in Jetpack $permalink = add_query_arg(affiliate_wp()->tracking->get_referral_var(), affwp_get_affiliate_id(), $permalink); return $permalink; }
function affwp_custom_affiliate_area_shortcode($atts, $content = null) { ob_start(); if (is_user_logged_in() && affwp_is_affiliate()) { // show the affiliate dashboard for logged in affiliates affiliate_wp()->templates->get_template_part('dashboard'); } elseif (!is_user_logged_in()) { affiliate_wp()->templates->get_template_part('login'); } return ob_get_clean(); }
/** * Example Two * * Give the affiliate a $10 commission for each product, but only for their very first referral */ function affwp_modify_first_referral_flat_amount($referral_amount, $affiliate_id, $amount, $reference, $product_id) { // get the affiliate's referrals $referrals = affiliate_wp()->referrals->get_referrals(array('affiliate_id' => $affiliate_id)); // if they haven't made a referral before, give them $10.00 on each product if (empty($referrals)) { $referral_amount = 10.0; // $10.00 } return $referral_amount; }
/** * Restrict commission to purchased products in Easy Digital Downloads * * Make sure to enable "Ignore Zero Referrals?" from Affiliates -> Settings -> Misc * If you would like 0.00 referrals to be ignored. */ function affwp_edd_restrict_commission_to_purchased_products($referral_amount, $affiliate_id, $amount, $reference, $product_id) { // get referring affiliate ID $affiliate_id = affiliate_wp()->tracking->get_affiliate_id(); // get user ID of referring affiliate $user_id = affwp_get_affiliate_user_id($affiliate_id); if (!(function_exists('edd_has_user_purchased') && edd_has_user_purchased($user_id, array($product_id)))) { $referral_amount = 0.0; } return $referral_amount; }
function affwp_eawrp_affiliate_referral_paid_email_body($affiliate_id, $amount, $date) { $referrals_page = add_query_arg('tab', 'referrals', affiliate_wp()->login->get_login_url()); $message = sprintf("Hi %s,", affiliate_wp()->affiliates->get_affiliate_name($affiliate_id)) . "\n\n"; $message .= sprintf("We wanted to let you know that your referral for %s from %s has just been paid!", $amount, $date) . "\n\n"; $message .= sprintf("You can view your referrals anytime from your affiliate dashboard: %s", $referrals_page) . "\n\n"; $message .= "Keep it up!" . "\n\n"; $message .= get_bloginfo('name') . "\n"; $message .= home_url(); return $message; }
/** * [affiliate_area_order_details] shortcode */ function affwp_aas_order_details_shortcode($atts, $content = null) { if (!(is_user_logged_in() && affwp_is_affiliate())) { return $content; } ob_start(); echo '<div id="affwp-affiliate-dashboard">'; affiliate_wp()->templates->get_template_part('dashboard-tab', 'order-details'); echo '</div>'; $content = ob_get_clean(); return $content; }
/** * Plugin Name: AffiliateWP - Add Log out Link To Affiliate Area * Plugin URI: http://affiliatewp.com * Description: Adds a log out link to the top of the affiliate area * Author: Andrew Munro, Sumobi * Author URI: http://sumobi.com * Version: 1.0 */ function affwp_custom_affiliate_dashboard_logout() { $redirect = function_exists('affiliate_wp') ? affiliate_wp()->login->get_login_url() : ''; ?> <p> <a href="<?php echo wp_logout_url($redirect); ?> " title="Logout">Logout</a> </p> <?php }
/** * Add the conversion tracking script to the thanks/success page * * @since 1.0 */ function affwp_cf7_conversion_script() { // return if not thanks page if (!is_page(affwp_cf7_get_success_page_id())) { return; } // get the amount $amount = isset($_GET['amount']) ? $_GET['amount'] : ''; // referral arguments $args = array('status' => 'unpaid', 'amount' => $amount); // add the conversion script to the page affiliate_wp()->tracking->conversion_script($args); }
/** * Sanitize visit URLs * * @since 1.7.5 * @param string $url The URL to sanitize * @return string $url The sanitized URL */ function affwp_sanitize_visit_url($url) { $original_url = $url; $referral_var = affiliate_wp()->tracking->get_referral_var(); // Remove the referral var $url = remove_query_arg($referral_var, $url); // Fallback for pretty permalinks if ($original_url === $url) { if (strpos($url, $referral_var)) { $url = preg_replace('/(\\/' . $referral_var . ')[\\/](\\w*)/', '', $url); } } return $url; }
/** * Plugin Name: AffiliateWP - Hide Affiliate Area From Non Affiliates * Plugin URI: http://affiliatewp.com * Description: Hides the Affiliate Area from appearing in the site navigation unless the user is an affiliate * Author: Andrew Munro, Sumobi * Author URI: http://sumobi.com * Version: 1.0 */ function affwp_custom_hide_affiliate_area_from_non_affiliates($items, $menu, $args) { // grab the page ID of the affiliate area // Alternatively you can just set a page ID here // For example: $affiliate_area_page_id = 5; $affiliate_area_page_id = affiliate_wp()->settings->get('affiliates_page'); // loop through and remove the page from the menu if user is not an affiliate foreach ($items as $key => $item) { if ($item->object_id == $affiliate_area_page_id && !affwp_is_affiliate()) { unset($items[$key]); } } return $items; }
/** * Get the data being exported * * @access public * @since 1.0 * @return array $data Data for Export */ public function get_data() { $args = array('status' => 'unpaid', 'date' => !empty($this->date) ? $this->date : '', 'number' => -1); // Final data to be exported $data = array(); // The affiliates that have earnings to be paid $affiliates = array(); // The list of referrals that are possibly getting marked as paid $to_maybe_pay = array(); // Retrieve the referrals from the database $referrals = affiliate_wp()->referrals->get_referrals($args); // The minimum payout amount $minimum = !empty($_POST['minimum']) ? sanitize_text_field(affwp_sanitize_amount($_POST['minimum'])) : 0; if ($referrals) { foreach ($referrals as $referral) { if (in_array($referral->affiliate_id, $affiliates)) { // Add the amount to an affiliate that already has a referral in the export $amount = $data[$referral->affiliate_id]['amount'] + $referral->amount; $data[$referral->affiliate_id]['amount'] = $amount; } else { $email = affwp_get_affiliate_email($referral->affiliate_id); $data[$referral->affiliate_id] = array('email' => $email, 'amount' => $referral->amount, 'currency' => !empty($referral->currency) ? $referral->currency : affwp_get_currency()); $affiliates[] = $referral->affiliate_id; } // Add the referral to the list of referrals to maybe payout if (!array_key_exists($referral->affiliate_id, $to_maybe_pay)) { $to_maybe_pay[$referral->affiliate_id] = array(); } $to_maybe_pay[$referral->affiliate_id][] = $referral->referral_id; } // Now determine which affiliates are above the minimum payout amount if ($minimum > 0) { foreach ($data as $affiliate_id => $payout) { if ($payout['amount'] < $minimum) { unset($data[$affiliate_id]); unset($to_maybe_pay[$affiliate_id]); } } } // We now know which referrals should be marked as paid foreach ($to_maybe_pay as $referral_list) { foreach ($referral_list as $referral_id) { affwp_set_referral_status($referral_id, 'paid'); } } } $data = apply_filters('affwp_export_get_data', $data); $data = apply_filters('affwp_export_get_data_' . $this->export_type, $data); return $data; }
/** * Get the data being exported * * @access public * @since 1.3 * @return array $data Data for Export */ public function get_data() { $args = array('status' => $this->status, 'number' => -1); $data = array(); $affiliates = affiliate_wp()->affiliates->get_affiliates($args); if ($affiliates) { foreach ($affiliates as $affiliate) { $data[] = array('affiliate_id' => $affiliate->affiliate_id, 'email' => affwp_get_affiliate_email($affiliate->affiliate_id), 'payment_email' => affwp_get_affiliate_payment_email($affiliate->affiliate_id), 'username' => affwp_get_affiliate_login($affiliate->affiliate_id), 'rate' => affwp_get_affiliate_rate($affiliate->affiliate_id), 'rate_type' => affwp_get_affiliate_rate_type($affiliate->affiliate_id), 'earnings' => $affiliate->earnings, 'referrals' => $affiliate->referrals, 'visits' => $affiliate->visits, 'status' => $affiliate->status, 'date_registered' => $affiliate->date_registered); } } $data = apply_filters('affwp_export_get_data', $data); $data = apply_filters('affwp_export_get_data_' . $this->export_type, $data); return $data; }
/** * Revoke a referral when a payment is refunded * * @access public * @since 1.6 */ public function revoke_referral_on_refund($payment_id = 0) { if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } $this->reject_referral($payment_id); $referral = affiliate_wp()->referrals->get_by('reference', $payment_id, $this->context); $amount = affwp_currency_filter(affwp_format_amount($referral->amount)); $name = affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id); $note = sprintf(__('Referral #%d for %s for %s rejected', 'affiliate-wp'), $referral->referral_id, $amount, $name); $payment = SI_Payment::get_instance($payment_id); $new_data = wp_parse_args($payment->get_data(), array('affwp_notes' => $note)); $payment->set_data($new_data); }
/** * Load the frontend scripts and styles * * @since 1.0 * @return void */ function affwp_frontend_scripts_and_styles() { global $post; if (!is_object($post)) { return; } if (has_shortcode($post->post_content, 'affiliate_area') || apply_filters('affwp_force_frontend_scripts', false)) { $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; wp_enqueue_script('affwp-frontend', AFFILIATEWP_PLUGIN_URL . 'assets/js/frontend' . $suffix . '.js', array('jquery'), AFFILIATEWP_VERSION); wp_localize_script('affwp-frontend', 'affwp_vars', array('affwp_version' => AFFILIATEWP_VERSION, 'permalinks' => get_option('permalink_structure'), 'pretty_affiliate_urls' => affwp_is_pretty_referral_urls(), 'currency_sign' => affwp_currency_filter(''), 'currency_pos' => affiliate_wp()->settings->get('currency_position', 'before'))); wp_enqueue_style('affwp-forms', AFFILIATEWP_PLUGIN_URL . 'assets/css/forms' . $suffix . '.css', AFFILIATEWP_VERSION); wp_enqueue_style('dashicons'); } }
/** * Show a different image/banner/logo based on which affiliate is being tracked * * @since 1.0 * @return string Image banner of affiliate */ function affwp_affiliate_banners_get_banner() { // get the affiliate ID from query string $ref_var = isset($_GET[affiliate_wp()->tracking->get_referral_var()]) ? $_GET[affiliate_wp()->tracking->get_referral_var()] : ''; // if affiliate ID is set in query string make sure they're actually an affiliate if (isset($ref_var) && affwp_is_affiliate(affwp_get_affiliate_user_id($ref_var))) { $affiliate_id = $ref_var; } elseif (affiliate_wp()->tracking->get_affiliate_id()) { $affiliate_id = affiliate_wp()->tracking->get_affiliate_id(); } else { $affiliate_id = ''; } // built out the image based on the image path of the affiliate return '<img src="' . affwp_affiliate_banners_get_image($affiliate_id) . '" />'; }
/** * Make coupon invalid if customer is not currently tracking an affiliate (can be any affiliate) */ function affwp_custom_block_coupon_if_no_referrer($return, $coupon) { // make sure AffiliateWP is installed if (!function_exists('affiliate_wp')) { return $return; } // check to see if the coupon is linked to an affiliate $is_tracked_coupon = get_post_meta($coupon->id, 'affwp_discount_affiliate', true); // make sure an affiliate is being tracked if (!affiliate_wp()->tracking->was_referred() && $is_tracked_coupon) { $return = false; // disallow the coupon } return $return; }
function affwp_search_users() { if (empty($_POST['search'])) { die('-1'); } if (!current_user_can('manage_affiliates')) { die('-1'); } $search_query = htmlentities2(trim($_POST['search'])); do_action('affwp_pre_search_users', $search_query); $args = array(); if (isset($_POST['status'])) { $status = mb_strtolower(htmlentities2(trim($_POST['status']))); switch ($status) { case 'none': $affiliates = affiliate_wp()->affiliates->get_affiliates(array('number' => 9999)); $args = array('exclude' => array_map('absint', wp_list_pluck($affiliates, 'user_id'))); break; case 'any': $affiliates = affiliate_wp()->affiliates->get_affiliates(array('number' => 9999)); $args = array('include' => array_map('absint', wp_list_pluck($affiliates, 'user_id'))); break; default: $affiliates = affiliate_wp()->affiliates->get_affiliates(array('number' => 9999, 'status' => $status)); $args = array('include' => array_map('absint', wp_list_pluck($affiliates, 'user_id'))); } } //make sure we filter the search columns so they only include the columns we want to search //this filter was exposed by WordPress in WP 3.6.0 add_filter('user_search_columns', function ($search_columns, $search, WP_User_Query $WP_User_Query) { return array('user_login', 'display_name', 'user_email'); }, 10, 3); //add search string to args $args['search'] = mb_strtolower(htmlentities2(trim($_POST['search']))); //get users matching search $found_users = get_users($args); if ($found_users) { $user_list = '<ul>'; foreach ($found_users as $user) { $user_list .= '<li><a href="#" data-id="' . esc_attr($user->ID) . '" data-login="******">' . esc_html($user->user_login) . '</a></li>'; } $user_list .= '</ul>'; echo json_encode(array('results' => $user_list, 'id' => 'found')); } else { echo json_encode(array('results' => '<p>' . __('No users found', 'affiliate-wp') . '</p>', 'id' => 'fail')); } die; }
/** * Get the data being exported * * @access public * @since 1.0 * @return array $data Data for Export */ public function get_data() { $args = array('status' => $this->status, 'date' => !empty($this->date) ? $this->date : '', 'affiliate_id' => $this->affiliate, 'number' => -1); $data = array(); $affiliates = array(); $referral_ids = array(); $referrals = affiliate_wp()->referrals->get_referrals($args); if ($referrals) { foreach ($referrals as $referral) { $data[] = array('affiliate_id' => $referral->affiliate_id, 'email' => affwp_get_affiliate_email($referral->affiliate_id), 'payment_email' => affwp_get_affiliate_payment_email($referral->affiliate_id), 'amount' => $referral->amount, 'currency' => $referral->currency, 'campaign' => $referral->campaign, 'reference' => $referral->reference, 'context' => $referral->context, 'status' => $referral->status, 'date' => $referral->date); } } $data = apply_filters('affwp_export_get_data', $data); $data = apply_filters('affwp_export_get_data_' . $this->export_type, $data); return $data; }