get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
))) {
$output .= affiliates_encode_affiliate_id( $affiliate_id );
}
}
return $output;
}
/**
* Referrer ID shortcode.
* Renders the referring affiliate's id.
* Pages using this shortcode should NOT be cached.
*
* @param array $atts attributes
* @param string $content not used
*/
public static function referrer_id( $atts, $content = null ) {
$options = shortcode_atts(
array(
'direct' => false
),
$atts
);
extract( $options );
$output = "";
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
if ( $affiliate_id ) {
if ( $direct || $affiliate_id !== affiliates_get_direct_id() ) {
$output .= affiliates_encode_affiliate_id( $affiliate_id );
}
}
return $output;
}
/**
* Renders the referrer's username and other user details.
* Pages using this shortcode should NOT be cached.
*
* Supported values for the display attribute are: user_login, user_nicename, user_email, user_url and display_name.
*
* @param array $atts
* @param string $content not used
* @return string
*/
public static function referrer_user( $atts, $content = null ) {
$options = shortcode_atts(
array(
'direct' => false,
'display' => 'user_login'
),
$atts
);
extract( $options );
$output = '';
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
if ( $affiliate_id ) {
if ( $direct || $affiliate_id !== affiliates_get_direct_id() ) {
if ( $user_id = affiliates_get_affiliate_user( $affiliate_id ) ) {
if ( $user = get_user_by( 'id', $user_id ) ) {
switch( $display ) {
case 'user_login' :
$output .= $user->user_login;
break;
case 'user_nicename' :
$output .= $user->user_nicename;
break;
case 'user_email' :
$output .= $user->user_email;
break;
case 'user_url' :
$output .= $user->user_url;
break;
case 'display_name' :
$output .= $user->display_name;
break;
default :
$output .= $user->user_login;
}
$output = wp_strip_all_tags( $output );
}
}
}
}
return $output;
}
/**
* Renders information about the referring affiliate.
* Pages using this shortcode should NOT be cached.
*
* Supported values for the display attribute are:
* - name, id, email taken from the affiliate entry
* - user_id, user_login, user_nicename, user_email, user_url and display_name from the user
* - the field name of any enabled affiliate registration field
*
* If the display attribute is omitted, the user_login of the referrer is displayed.
*
* @param array $atts
* @param string $content not used
* @return string
*/
public static function referrer( $atts, $content = null ) {
$options = shortcode_atts(
array(
'direct' => false,
'display' => 'user_login'
),
$atts
);
extract( $options );
$output = '';
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
if ( $affiliate_id ) {
if ( $direct || $affiliate_id !== affiliates_get_direct_id() ) {
if ( affiliates_check_affiliate_id( $affiliate_id ) ) {
$affiliate = affiliates_get_affiliate( $affiliate_id );
switch( $display ) {
case 'name' :
$output = $affiliate['name'];
break;
case 'id' :
$output = $affiliate['affiliate_id'];
break;
case 'email' :
$output = $affiliate['email'];
break;
default :
if ( $user_id = affiliates_get_affiliate_user( $affiliate_id ) ) {
if ( $user = get_user_by( 'id', $user_id ) ) {
switch( $display ) {
case 'user_id' :
$output = $user->ID;
break;
case 'user_login' :
$output = $user->user_login;
break;
case 'user_nicename' :
$output = $user->user_nicename;
break;
case 'user_email' :
$output = $user->user_email;
break;
case 'user_url' :
$output = $user->user_url;
break;
case 'display_name' :
$output = $user->display_name;
break;
default :
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings.php';
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings-registration.php';
$registration_fields = Affiliates_Settings_Registration::get_fields();
unset( $registration_fields['password'] );
if ( !empty( $registration_fields ) && isset( $registration_fields[$display] ) ) {
$output = stripslashes( get_user_meta( $user_id, $display , true ) );
}
}
}
}
}
}
}
}
$output = wp_strip_all_tags( $output );
return $output;
}
/**
* Affiliate content shortcode.
* Renders the content if the current user is an affiliate.
*
* @param array $atts attributes (none used)
* @param string $content this is rendered for affiliates
*/
public static function affiliates_is_affiliate( $atts, $content = null ) {
remove_shortcode( 'affiliates_is_affiliate' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_is_affiliate', array( __CLASS__, 'affiliates_is_affiliate' ) );
$output = "";
if ( affiliates_user_is_affiliate( get_current_user_id() ) ) {
$output .= $content;
}
return $output;
}
/**
* Non-Affiliate content shortcode.
*
* @param array $atts attributes
* @param string $content this is rendered for non-affiliates
*/
public static function affiliates_is_not_affiliate( $atts, $content = null ) {
remove_shortcode( 'affiliates_is_not_affiliate' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_is_not_affiliate', array( __CLASS__, 'affiliates_is_not_affiliate' ) );
$output = "";
if ( !affiliates_user_is_affiliate( get_current_user_id() ) ) {
$output .= $content;
}
return $output;
}
/**
* Render content if visitor was referred. Pages using this shortcode should NOT be cached.
*
* @param array $atts
* @param string $content
* @return string $content is rendered if referred
*/
public static function affiliates_is_referred( $atts, $content = null ) {
remove_shortcode( 'affiliates_is_referred' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_is_referred', array( __CLASS__, 'affiliates_is_referred' ) );
$output = '';
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
if ( $affiliate_id ) {
if ( $affiliate_id !== affiliates_get_direct_id() ) {
if ( affiliates_check_affiliate_id( $affiliate_id ) ) {
$output .= $content;
}
}
}
return $output;
}
/**
* Render content if visitor was not referred. Pages using this shortcode should NOT be cached.
*
* @param array $atts
* @param string $content
* @return string $content is rendered if not referred
*/
public static function affiliates_is_not_referred( $atts, $content = null ) {
remove_shortcode( 'affiliates_is_not_referred' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_is_not_referred', array( __CLASS__, 'affiliates_is_not_referred' ) );
$output = '';
require_once( 'class-affiliates-service.php' );
$affiliate_id = Affiliates_Service::get_referrer_id();
// it can not be an affiliate and direct doesn't count
if ( ( !$affiliate_id ) || ( $affiliate_id === affiliates_get_direct_id() ) ) {
$output .= $content;
}
return $output;
}
/**
* Adjust from und until dates from UTZ to STZ and take into account the
* for option which will adjust the from date to that of the current
* day, the start of the week or the month, leaving the until date
* set to null.
*
* @param string $for "day", "week" or "month"
* @param string $from date/datetime
* @param string $until date/datetime
*/
private static function for_from_until( $for, &$from, &$until ) {
require_once AFFILIATES_CORE_LIB . '/class-affiliates-date-helper.php';
if ( $for === null ) {
if ( $from !== null ) {
$from = date( 'Y-m-d H:i:s', strtotime( DateHelper::u2s( $from ) ) );
}
if ( $until !== null ) {
$until = date( 'Y-m-d H:i:s', strtotime( DateHelper::u2s( $until ) ) );
}
} else {
$user_now = strtotime( DateHelper::s2u( date( 'Y-m-d H:i:s', time() ) ) );
$user_now_datetime = date( 'Y-m-d H:i:s', $user_now );
$user_daystart_datetime = date( 'Y-m-d', $user_now ) . ' 00:00:00';
$server_now_datetime = DateHelper::u2s( $user_now_datetime );
$server_user_daystart_datetime = DateHelper::u2s( $user_daystart_datetime );
$until = null;
switch ( strtolower( $for ) ) {
case 'day' :
$from = date( 'Y-m-d H:i:s', strtotime( $server_user_daystart_datetime ) );
break;
case 'week' :
$fdow = intval( get_option( 'start_of_week' ) );
$dow = intval( date( 'w', strtotime( $server_user_daystart_datetime ) ) );
$d = $dow - $fdow;
$from = date( 'Y-m-d H:i:s', mktime( 0, 0, 0, date( 'm', strtotime( $server_user_daystart_datetime ) ) , date( 'd', strtotime( $server_user_daystart_datetime ) )- $d, date( 'Y', strtotime( $server_user_daystart_datetime ) ) ) );
break;
case 'month' :
$from = date( 'Y-m', strtotime( $server_user_daystart_datetime ) ) . '-01 00:00:00';
break;
default :
$from = null;
}
}
}
/**
* Hits shortcode - renders the number of hits.
*
* @param array $atts attributes
* @param string $content not used
*/
public static function affiliates_hits( $atts, $content = null ) {
global $wpdb;
require_once AFFILIATES_CORE_LIB . '/class-affiliates-date-helper.php';
remove_shortcode( 'affiliates_hits' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_hits', array( __CLASS__, 'affiliates_hits' ) );
$output = "";
$options = shortcode_atts(
array(
'from' => null,
'until' => null,
'for' => null
),
$atts
);
extract( $options );
self::for_from_until( $for, $from, $until );
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
))) {
$output .= affiliates_get_affiliate_hits( $affiliate_id, $from, $until, true );
}
}
return $output;
}
/**
* Visits shortcode - renders the number of visits.
*
* @param array $atts attributes
* @param string $content not used
*/
public static function affiliates_visits( $atts, $content = null ) {
global $wpdb;
remove_shortcode( 'affiliates_visits' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_visits', array( __CLASS__, 'affiliates_visits' ) );
$output = "";
$options = shortcode_atts(
array(
'from' => null,
'until' => null,
'for' => null
),
$atts
);
extract( $options );
self::for_from_until( $for, $from, $until );
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
) ) ) {
$output .= affiliates_get_affiliate_visits( $affiliate_id, $from, $until, true );
}
}
return $output;
}
/**
* Referrals shortcode - renders referral information.
*
* @param array $atts attributes
* @param string $content not used
*/
public static function affiliates_referrals( $atts, $content = null ) {
global $wpdb;
remove_shortcode( 'affiliates_referrals' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_referrals', array( __CLASS__, 'affiliates_referrals' ) );
$output = "";
$options = shortcode_atts(
array(
'status' => null,
'from' => null,
'until' => null,
'show' => 'count',
'currency' => null,
'for' => null,
'if_empty' => null
),
$atts
);
extract( $options );
self::for_from_until( $for, $from, $until );
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
))) {
switch ( $show ) {
case 'count' :
switch ( $status ) {
case null :
case AFFILIATES_REFERRAL_STATUS_ACCEPTED :
case AFFILIATES_REFERRAL_STATUS_CLOSED :
case AFFILIATES_REFERRAL_STATUS_PENDING :
case AFFILIATES_REFERRAL_STATUS_REJECTED :
$referrals = affiliates_get_affiliate_referrals( $affiliate_id, $from, $until, $status );
break;
default :
$referrals = "";
}
$output .= $referrals;
break;
case 'total' :
if ( $totals = self::get_total( $affiliate_id, $from, $until, $status ) ) {
if ( count( $totals ) > 0 ) {
$output .= '
';
foreach ( $totals as $currency_id => $total ) {
$output .= '- ';
$output .= apply_filters( 'affiliates_referrals_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_referrals_display_total', number_format_i18n( $total, apply_filters( 'affiliates_referrals_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
$output .= '
';
}
$output .= '
';
}
}
if ( !$totals || count( $totals ) === 0 ) {
if ( $if_empty !== null ) {
$output .= '';
$output .= '- ';
$output .= apply_filters( 'affiliates_referrals_display_total_none', wp_filter_nohtml_kses( $if_empty ) );
$output .= '
';
$output .= '
';
}
}
break;
}
}
}
return $output;
}
/**
* Shows monthly earnings.
*
* Note that we don't do any s2u or u2s date adjustments here.
*
* @param array $atts options
* - show_paid : true or false (default), if true, also shows paid earnings
* - per_page : results per page, 10 by default
* - page : page to show by default
* - order : desc (default) or asc
* - orderby : date (default with no other options supported at current)
*
* @param string $content not used
*/
public static function affiliates_earnings( $atts, $content = null ) {
global $wpdb;
$output = '';
$atts = shortcode_atts( array( 'show_paid' => false, 'per_page' => 12, 'page' => 1, 'order' => 'desc', 'orderby' => 'date' ), $atts );
if ( isset( $_REQUEST['earnings-page'] ) ) {
$atts['page'] = intval( $_REQUEST['earnings-page'] );
}
if ( is_string( $atts['show_paid'] ) ) {
$atts['show_paid'] = strtolower( $atts['show_paid'] );
}
switch ( $atts['show_paid'] ) {
case true :
case 'true' :
case 'yes ':
$atts['show_paid'] = true;
break;
case false :
case 'false' :
case 'no' :
$atts['show_paid'] = false;
break;
default :
$atts['show_paid'] = false;
}
$atts['per_page'] = max( intval( $atts['per_page'] ), 1 );
$atts['page'] = max( intval( $atts['page'] ), 1 );
$atts['order'] = strtolower( $atts['order'] );
switch( $atts['order'] ) {
case 'asc' :
case 'desc' :
break;
default :
$atts['order'] = 'desc';
}
$atts['orderby'] = 'date';
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
if ( $affiliate_ids = affiliates_get_user_affiliate( $user_id ) ) {
$cols = 2;
$output .= '';
$output .= '';
$output .= '';
$output .= '| ';
$output .= __( 'Month', 'affiliates' );
$output .= ' | ';
$output .= '';
$output .= __( 'Earnings', 'affiliates' );
$output .= ' | ';
if ( $atts['show_paid'] ) {
$cols++;
$output .= '';
$output .= __( 'Paid', 'affiliates' );
$output .= ' | ';
}
$output .= '
';
$output .= '';
$output .= '';
$rows = array();
$referrals_table = _affiliates_get_tablename( 'referrals' );
$range = $wpdb->get_row(
"SELECT MIN(datetime) from_datetime, MAX(datetime) thru_datetime FROM $referrals_table WHERE affiliate_id IN (" . implode( ',', $affiliate_ids ) . ") "
);
if ( $range ) {
if ( !empty( $range->from_datetime ) ) { // Covers for NULL when no referrals recorded yet, too.
$t = strtotime( date( 'Y-m-01 00:00:00', strtotime( $range->from_datetime ) ) );
$eom = strtotime( date( 'Y-m-t 23:59:59', time() ) );
while ( $t < $eom ) {
$from = date( 'Y-m', $t ) . '-01 00:00:00';
$thru = date( 'Y-m-t', strtotime( $from ) );
$sums = array();
$sums_paid = array();
foreach( $affiliate_ids as $affiliate_id ) {
// accepted and closed
if ( $totals = self::get_total( $affiliate_id, $from, $thru ) ) {
if ( count( $totals ) > 0 ) {
foreach ( $totals as $currency_id => $total ) {
if ( function_exists( 'bcadd' ) ) {
$sums[$currency_id] = isset( $sums[$currency_id] ) ? bcadd( $sums[$currency_id], $total, affiliates_get_referral_amount_decimals() ) : $total;
} else {
$sums[$currency_id] = isset( $sums[$currency_id] ) ? $sums[$currency_id] + $total : $total;
}
}
}
}
// closed
if ( $totals_paid = self::get_total( $affiliate_id, $from, $thru, AFFILIATES_REFERRAL_STATUS_CLOSED ) ) {
if ( count( $totals_paid ) > 0 ) {
foreach ( $totals_paid as $currency_id => $total ) {
if ( function_exists( 'bcadd' ) ) {
$sums_paid[$currency_id] = isset( $sums[$currency_id] ) ? bcadd( $sums[$currency_id], $total, affiliates_get_referral_amount_decimals() ) : $total;
} else {
$sums_paid[$currency_id] = isset( $sums[$currency_id] ) ? $sums[$currency_id] + $total : $total;
}
}
}
}
}
$rows[$t] = array( 'from' => $from, 'sums' => $sums, 'sums_paid' => $sums_paid );
$t = strtotime( '+1 month', $t );
}
}
}
// sort rows
if ( $atts['order'] === 'desc' ) {
$rows = array_reverse( $rows );
}
$pages = count( $rows ) / $atts['per_page'];
$offset = $atts['per_page'] * ( $atts['page'] - 1 );
$rows = array_splice( $rows, $offset, $atts['per_page'] );
if ( count( $rows ) > 0 ) {
foreach ( $rows as $t => $row ) {
$from = $row['from'];
$sums = $row['sums'];
$sums_paid = $row['sums_paid'];
$output .= '';
// month & year
$output .= '| ';
$output .= date_i18n( __( 'F Y', 'affiliates' ), strtotime( $from ) ); // translators: date format; month and year for earnings display
$output .= ' | ';
// earnings
$output .= '';
if ( count( $sums ) > 1 ) {
$output .= '';
foreach ( $sums as $currency_id => $total ) {
$output .= '- ';
$output .= apply_filters( 'affiliates_earnings_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_earnings_display_total', number_format_i18n( $total, apply_filters( 'affiliates_earnings_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
$output .= '
';
}
$output .= ' ';
} else if ( count( $sums ) > 0 ) {
foreach ( $sums as $currency_id => $total ) {
$output .= apply_filters( 'affiliates_earnings_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_earnings_display_total', number_format_i18n( $total, apply_filters( 'affiliates_earnings_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
}
} else {
$output .= apply_filters( 'affiliates_earnings_display_total_none', __( 'None', 'affiliates' ) );
}
$output .= ' | ';
// paid
if ( $atts['show_paid'] ) {
$output .= '';
if ( count( $sums_paid ) > 1 ) {
$output .= '';
foreach ( $sums_paid as $currency_id => $total ) {
$output .= '- ';
$output .= apply_filters( 'affiliates_earnings_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_earnings_display_total', number_format_i18n( $total, apply_filters( 'affiliates_earnings_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
$output .= '
';
}
$output .= ' ';
} else if ( count( $sums_paid ) > 0 ) {
foreach ( $sums as $currency_id => $total ) {
$output .= apply_filters( 'affiliates_earnings_display_currency', $currency_id );
$output .= ' ';
$output .= apply_filters( 'affiliates_earnings_display_total', number_format_i18n( $total, apply_filters( 'affiliates_earnings_decimals', affiliates_get_referral_amount_decimals( 'display' ) ) ), $total, $currency_id );
}
} else {
$output .= apply_filters( 'affiliates_earnings_display_total_none', __( 'None', 'affiliates' ) );
}
$output .= ' | ';
}
$output .= '
';
}
} else {
$output .= sprintf( '', $cols );
$output .= apply_filters( 'affiliates_earnings_display_total_no_earnings', __( 'There are no earnings yet.', 'affiliates' ) );
$output .= ' | ';
}
$output .= '';
$output .= '
';
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = remove_query_arg( 'earnings-page', $current_url );
if ( count( $rows ) > 0 ) {
if ( $atts['page'] > 1 ) {
$output .= sprintf( '%s', esc_url( add_query_arg( 'earnings-page', $atts['page'] - 1, $url ) ), esc_html_x( 'Previous', 'Label used to show previous page of affiliate earnings results', 'affiliates' ) );
}
if ( $atts['page'] < $pages ) {
$output .= sprintf( '%s', esc_url( add_query_arg( 'earnings-page', $atts['page'] + 1, $url ) ), esc_html_x( 'Next', 'Label used to show next page of affiliate earnings results', 'affiliates' ) );
}
}
}
}
return $output;
}
/**
* Retrieve totals for an affiliate.
*
* @param int $affiliate_id
* @param string $from_date
* @param string $thru_date
* @param string $status
* @return array of totals indexed by currency_id or false on error
*/
public static function get_total( $affiliate_id, $from_date = null , $thru_date = null, $status = null ) {
global $wpdb;
$referrals_table = _affiliates_get_tablename( 'referrals' );
$where = " WHERE affiliate_id = %d";
$values = array( $affiliate_id );
if ( $from_date ) {
$from_date = date( 'Y-m-d', strtotime( $from_date ) );
}
if ( $thru_date ) {
$thru_date = date( 'Y-m-d', strtotime( $thru_date ) + 24*3600 );
}
if ( $from_date && $thru_date ) {
$where .= " AND datetime >= %s AND datetime < %s ";
$values[] = $from_date;
$values[] = $thru_date;
} else if ( $from_date ) {
$where .= " AND datetime >= %s ";
$values[] = $from_date;
} else if ( $thru_date ) {
$where .= " AND datetime < %s ";
$values[] = $thru_date;
}
if ( !empty( $status ) ) {
$where .= " AND status = %s ";
$values[] = $status;
} else {
$where .= " AND status IN ( %s, %s ) ";
$values[] = AFFILIATES_REFERRAL_STATUS_ACCEPTED;
$values[] = AFFILIATES_REFERRAL_STATUS_CLOSED;
}
$totals = $wpdb->get_results( $wpdb->prepare(
"SELECT SUM(amount) total, currency_id FROM $referrals_table
$where
GROUP BY currency_id
",
$values
) );
if ( $totals ) {
$result = array();
foreach( $totals as $total ) {
if ( ( $total->currency_id !== null ) && ( $total->total !== null ) ) {
$result[$total->currency_id] = $total->total;
}
}
return $result;
} else {
return false;
}
}
/**
* URL shortcode - renders the affiliate url.
*
* @param array $atts attributes
* @param string $content (is not used)
*/
public static function affiliates_url( $atts, $content = null ) {
global $wpdb, $wp;
$options = shortcode_atts(
array(
'url' => ''
),
$atts
);
extract( $options );
switch( $url ) {
case '' :
$url = home_url();
break;
case 'current' :
$pname = get_option( 'aff_pname', AFFILIATES_PNAME );
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = remove_query_arg( $pname, $current_url );
break;
case 'permalink' :
$url = get_permalink();
break;
default :
}
remove_shortcode( 'affiliates_url' );
$content = do_shortcode( $content );
add_shortcode( 'affiliates_url', array( __CLASS__, 'affiliates_url' ) );
$output = '';
$user_id = get_current_user_id();
if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
if ( $affiliate_id = $wpdb->get_var( $wpdb->prepare(
"SELECT $affiliates_users_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status = 'active'",
intval( $user_id )
))) {
$content = trim( $content );
if ( strlen( $content ) > 0 ) {
// wp_texturize() has already been applied to $content and
// it indiscriminately replaces ampersands with the HTML
// entity & - we need to undo this so path separators
// are not messed up. Note that it does that even though we
// have indicated to exclude affiliates_url via the
// no_texturize_shortcodes filter.
$url = trim( $content );
$url = str_replace( '&', '&', $url );
$url = strip_tags( $url );
$url = preg_replace('/\r|\n/', '', $url );
$url = trim( $url );
}
$output .= affiliates_get_affiliate_url( $url, $affiliate_id );
}
}
return $output;
}
/**
* Exclude the affiliates_url shortcode.
*
* @param array $shortcodes
* @return array
*/
public static function no_texturize_shortcodes( $shortcodes ) {
if ( !in_array( 'affiliates_url', $shortcodes ) ) {
$shortcodes[] = 'affiliates_url';
}
return $shortcodes;
}
/**
* Renders a login form that can redirect to a url or the current page.
*
* @param array $atts
* @param string $content
* @return string rendered form
*/
public static function affiliates_login_redirect( $atts, $content = null ) {
extract( shortcode_atts( array( 'redirect_url' => '' ), $atts ) );
$form = '';
if ( !is_user_logged_in() ) {
if ( empty( $redirect_url ) ) {
$redirect_url = get_permalink();
}
$form = wp_login_form( array( 'echo' => false, 'redirect' => $redirect_url ) );
}
return $form;
}
/**
* Renders a link to log out.
*
* @param array $atts
* @param string $content not used
* @return string rendered logout link or empty if not logged in
*/
public static function affiliates_logout( $atts, $content = null ) {
if ( is_user_logged_in() ) {
return '' . __( 'Log out', 'affiliates' ) . '';
} else {
return '';
}
}
/**
* Affiliate field info.
*
* user_id - print for ... requires AFFILIATES_ADMIN...
* name - field name or names, empty includes all by default
* edit - yes or no
* load_styles - yes or no
*
* @param array $atts
* @param string $content
* @return string
*/
public static function affiliates_fields( $atts, $content = null ) {
global $wpdb;
$output = '';
if ( is_user_logged_in() ) {
$atts = shortcode_atts(
array(
'edit' => 'yes',
'load_styles' => 'yes',
'name' => '',
'user_id' => null
),
$atts
);
$atts['load_styles'] = strtolower( trim( $atts['load_styles' ] ) );
if ( $atts['load_styles'] == 'yes' ) {
wp_enqueue_style( 'affiliates-fields' );
}
$atts['edit'] = strtolower( trim( $atts['edit' ] ) );
$fields = null;
if ( !empty( $atts['name'] ) ) {
$fields = array_map( 'strtolower', array_map( 'trim', explode( ',', $atts['name'] ) ) );
}
if ( current_user_can( AFFILIATES_ADMINISTER_AFFILIATES ) && !empty( $atts['user_id'] ) ) {
$user_id = intval( trim( $atts['user_id'] ) );
} else {
$user_id = get_current_user_id();
}
$user = get_user_by( 'id', $user_id );
if ( affiliates_user_is_affiliate( $user_id ) ) {
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings.php';
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings-registration.php';
$registration_fields = Affiliates_Settings_Registration::get_fields();
if ( $atts['edit'] != 'yes' ) {
unset( $registration_fields['password'] );
}
if ( !empty( $fields ) ) {
$_registration_fields = array();
foreach( $fields as $name ) {
if ( isset( $registration_fields[$name] ) ) {
$_registration_fields[$name] = $registration_fields[$name];
}
}
$registration_fields = $_registration_fields;
}
// handle form submission
if ( $atts['edit'] === 'yes' ) {
if ( !empty( $_POST['affiliate-nonce'] ) && wp_verify_nonce( $_POST['affiliate-nonce'], 'save' ) ) {
if ( !empty( $registration_fields ) ) {
$error = false;
// gather field values
foreach( $registration_fields as $name => $field ) {
if ( $field['enabled'] ) {
$value = isset( $_POST[$name] ) ? $_POST[$name] : '';
$value = Affiliates_Utility::filter( $value );
if ( $field['required'] && empty( $value ) && !( is_user_logged_in() && isset( $field['type'] ) && $field['type'] == 'password' ) ) {
$error = true;
$output .= '';
$output .= __( 'ERROR', 'affiliates' );
$output .= ' : ';
$output .= sprintf( __( 'Please fill out the field %s.', 'affiliates' ), $field['label'] );
$output .= '
';
}
$registration_fields[$name]['value'] = $value;
// password check
$type = isset( $field['type'] ) ? $field['type'] : 'text';
if ( $type == 'password' ) {
if ( !empty( $value ) ) {
$value2 = isset( $_POST[$name . '2'] ) ? $_POST[$name . '2'] : '';
$value2 = Affiliates_Utility::filter( $value2 );
if ( $value !== $value2 ) {
$error = true;
$output .= '';
$output .= __( 'ERROR', 'affiliates' );
$output .= ' : ';
$output .= sprintf( __( 'The passwords for the field %s do not match.', 'affiliates' ), $field['label'] );
$output .= '
';
}
}
}
}
}
$userdata = array();
foreach( $registration_fields as $name => $field ) {
if ( $registration_fields[$name]['enabled'] ) {
$userdata[$name] = $registration_fields[$name]['value'];
}
}
if ( !$error ) {
$updated_user_id = Affiliates_Registration::update_affiliate_user( $user_id, $userdata );
if ( is_wp_error( $updated_user_id ) ) {
$error_messages = implode( '
', $updated_user_id->get_error_messages() );
if ( !empty( $error_messages ) ) {
$output .= '';
$output .= $error_messages;
$output .= '
';
}
} else {
$output .= '';
$output .= __( 'Saved', 'affiliates' );
$output .= '
';
}
}
}
}
}
// get user again in case anything changed as we're using it below
$user = get_user_by( 'id', $user_id );
// show form
$n = 0;
if ( !empty( $registration_fields ) ) {
if ( $atts['edit'] === 'yes' ) {
$output .= '