' . '

' . __( 'Affiliates', AFFILIATES_PLUGIN_DOMAIN ) . '

' . ''; if ( !$wp_rewrite->using_permalinks() ) { echo '

' . sprintf( __( 'Your site is not using permalinks. You will only be able to use affiliate links but not affiliate permalinks, unless you change your permalink settings.', AFFILIATES_PLUGIN_DOMAIN ), get_admin_url( null, 'options-permalink.php') ) . '

'; } // // handle affiliates form submission // $affiliates_table = _affiliates_get_tablename( 'affiliates' ); if ( wp_verify_nonce( $_POST[AFFILIATES_ADMIN_AFFILIATES_NONCE], plugin_basename( __FILE__ ) ) ) { // mark deleted affiliates as deleted $i = 0; $affiliatecount = intval( $_POST['affiliatecount'] ); while ( ( $_POST['delete-affiliate-id-' . $i] != NULL ) || ( $i <= $affiliatecount ) ) { $affiliate_id = $_POST['delete-affiliate-id-' . $i]; $affiliate_exists = FALSE; // do not mark the pseudo-affiliate as deleted: type != ... $check = $wpdb->prepare( "SELECT affiliate_id FROM $affiliates_table WHERE affiliate_id = %d AND (type IS NULL OR type != '" . AFFILIATES_DIRECT_TYPE . "')", intval( $affiliate_id ) ); if ( $wpdb->query( $check ) ) { $affiliate_exists = TRUE; } if ( $affiliate_exists ) { $wpdb->query( $query = $wpdb->prepare( "UPDATE $affiliates_table SET status = 'deleted' WHERE affiliate_id = %d", intval( $affiliate_id ) ) ); } $i++; } // add or modify affiliate entries $i = 0; $affiliatecount = intval( $_POST['affiliatecount'] ); while ( ( $_POST['name-field-' . $i] != NULL ) || ( $i <= $affiliatecount ) ) { $affiliate_id = $_POST['affiliate-id-' . $i]; $affiliate_exists = false; $is_direct = false; if ( $the_affiliate = $wpdb->get_row( $wpdb->prepare( "SELECT affiliate_id FROM $affiliates_table WHERE affiliate_id = %d", intval( $affiliate_id ) ) ) ) { $affiliate_exists = true; $is_direct = $the_affiliate->type == AFFILIATES_DIRECT_TYPE; } $name = $_POST['name-field-' . $i]; // don't change the name of the pseudo-affiliate if ( $is_direct ) { $name = AFFILIATES_DIRECT_NAME; } if ( !empty( $name ) ) { // Note the trickery (*) that has to be used because wpdb::prepare() is not // able to handle null values. // @see http://core.trac.wordpress.org/ticket/11622 // @see http://core.trac.wordpress.org/ticket/12819 $data = array( 'name' => $name ); $formats = array( '%s' ); $email = trim( $_POST['email-field-' . $i] ); if ( is_email( $email ) ) { $data['email'] = $email; $formats[] = '%s'; } else { $data['email'] = null; // (*) $formats[] = 'NULL'; // (*) } $from_date = $_POST['from-date-field-' . $i]; if ( empty( $from_date ) ) { $from_date = date( 'Y-m-d', time() ); } else { $from_date = date( 'Y-m-d', strtotime( $from_date ) ); } $data['from_date'] = $from_date; $formats[] = '%s'; $thru_date = $_POST['thru-date-field-' . $i]; if ( !empty( $thru_date ) && strtotime( $thru_date ) < strtotime( $from_date ) ) { // thru_date is before from_date => set to null $thru_date = null; } if ( !empty( $thru_date ) ) { $thru_date = date( 'Y-m-d', strtotime( $thru_date ) ); $data['thru_date'] = $thru_date; $formats[] = '%s'; } else { $data['thru_date'] = null; // (*) $formats[] = 'NULL'; // (*) } if ( $affiliate_exists ) { $sets = array(); $values = array(); $j = 0; foreach( $data as $key => $value ) { $sets[] = $key . ' = ' . $formats[$j]; if ( $value ) { // (*) $values[] = $value; } $j++; } if ( !empty( $sets ) ) { $sets = implode( ', ', $sets ); $values[] = intval( $affiliate_id ); $query = $wpdb->prepare( "UPDATE $affiliates_table SET $sets WHERE affiliate_id = %d", $values ); $wpdb->query( $query ); } } else { $data_ = array(); $formats_ = array(); foreach( $data as $key => $value ) { // (*) if ( $value ) { $data_[$key] = $value; } } foreach( $formats as $format ) { // (*) if ( $format != "NULL" ) { $formats_[] = $format; } } $wpdb->insert( $affiliates_table, $data_, $formats_ ); } } // if $i++; } // while } // if (nonce) // // print the Affiliates form // echo '

' . __( 'Your current affiliates', AFFILIATES_PLUGIN_DOMAIN ) . '

'; echo '

' . __( 'There are two types of links your affiliates may use to link to your site:', AFFILIATES_PLUGIN_DOMAIN ) . '

' . '' . '

' . __( 'Once a visitor has landed on your site through an affiliate link, referrals may be recorded and attributed to the affiliate.', AFFILIATES_PLUGIN_DOMAIN ) . '

'; echo '
'; echo '
'; $affiliates_table = _affiliates_get_tablename( 'affiliates' ); $query = "SELECT * FROM " . $affiliates_table . " WHERE status = 'active' ORDER BY name"; $affiliates = $wpdb->get_results( $query, OBJECT ); $i = 0; foreach ( $affiliates as $affiliate ) { $is_direct = $affiliate->type == AFFILIATES_DIRECT_TYPE; echo '
'; $affiliate_id = $affiliate->affiliate_id; echo ''; echo '

'; if ( !$is_direct ) { $name = $affiliate->name; echo ''; echo ''; } else { echo ''; echo ''; echo ''; } $email = $affiliate->email; echo ''; echo ''; echo '

'; echo '

'; $from_date = $affiliate->from_date; if ( !empty( $from_date ) ) { $from_date = date( 'Y-m-d', strtotime( $from_date ) ); } $thru_date = $affiliate->thru_date; if ( !empty( $thru_date ) ) { $thru_date = date( 'Y-m-d', strtotime( $thru_date ) ); } echo ''; if ( !empty( $from_date ) ) { echo ''; } else { echo '/'; } echo ''; if ( !empty( $thru_date ) ) { echo ''; } else { echo ''; } echo '
'; echo '' . __( 'Affiliates are inoperative outside these dates. Hits, visits and referrals will only be stored for operative affiliates.', AFFILIATES_PLUGIN_DOMAIN ) . ''; echo '

'; $encoded_id = affiliates_encode_affiliate_id( $affiliate->affiliate_id ); echo ''; echo ''; if ( $affiliate->type != AFFILIATES_DIRECT_TYPE ) { echo '' . __( 'Remove', AFFILIATES_PLUGIN_DOMAIN ) . ''; } else { echo '

' . __( 'This is the pseudo-affiliate that represents you or your organization. Referrals that are not attributable to affiliates will be attributed to this one.', AFFILIATES_PLUGIN_DOMAIN ) . '

' . '

' . __( 'This is useful to keep track of all your transactions, including those that have not been initiated via an affiliate.', AFFILIATES_PLUGIN_DOMAIN ) . '

' ; } echo '
'; // affiliate-$i $i++; } echo '
'; echo '
'; echo '
'; echo '
'; echo ''; echo '

'; echo __('To create a new affiliate press', AFFILIATES_PLUGIN_DOMAIN ); echo '' . __( 'Add', AFFILIATES_PLUGIN_DOMAIN ) . ''; echo '

'; echo '

'; echo __('Use the Remove button to eliminate an affiliate. Your changes will be applied after you press Submit changes.', AFFILIATES_PLUGIN_DOMAIN ); echo '

'; wp_nonce_field( plugin_basename( __FILE__ ), AFFILIATES_ADMIN_AFFILIATES_NONCE ); echo ''; echo '
'; echo '
'; affiliates_footer(); } ?>