get_var( $wpdb->prepare( "SELECT user_id FROM $affiliates_users_table WHERE affiliate_id = %d", intval( $affiliate_id ) ) ); if ( $affiliate_user_id !== null ) { $affiliate_user = get_user_by( 'id', intval( $affiliate_user_id ) ); if ( $affiliate_user ) { if ( current_user_can( 'edit_user', $affiliate_user->ID ) ) { $affiliate_user_edit = sprintf( __( 'Edit %s', AFFILIATES_PLUGIN_DOMAIN ) , 'ID" ) . '">' . $affiliate_user->user_login . '' ); } } } $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $current_url = remove_query_arg( 'action', $current_url ); $current_url = remove_query_arg( 'affiliate_id', $current_url ); $name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : $affiliate['name']; $email = isset( $_POST['email-field'] ) ? $_POST['email-field'] : $affiliate['email']; $user_login = isset( $_POST['user-field'] ) ? $_POST['user-field'] : ( $affiliate_user != null ? $affiliate_user->user_login : '' ); $from_date = isset( $_POST['from-date-field'] ) ? $_POST['from-date-field'] : $affiliate['from_date']; $thru_date = isset( $_POST['thru-date-field'] ) ? $_POST['thru-date-field'] : $affiliate['thru_date']; $output = '
' . '
' . '

' . __( 'Edit an affiliate', AFFILIATES_PLUGIN_DOMAIN ) . '

' . '
' . '
' . '
' . '' . '
' . '' . '' . '
' . '
' . '' . '' . '' . __( "If a valid Username is specified and no email is given, the user's email address will be used automatically.", AFFILIATES_PLUGIN_DOMAIN ) . '' . '
' . '
' . '' . '' . $affiliate_user_edit . '
' . '
' . '' . '' . '
' . '
' . '' . '' . '
' . '
' . wp_nonce_field( plugin_basename( __FILE__ ), AFFILIATES_ADMIN_AFFILIATES_NONCE, true, false ) . '' . '' . '' . __( 'Cancel', AFFILIATES_PLUGIN_DOMAIN ) . '' . '
' . '
' . // .affiliate.edit '
' . '
'; // .manage-affiliates echo $output; affiliates_footer(); } // function affiliates_admin_affiliates_edit /** * Handle edit form submission. */ function affiliates_admin_affiliates_edit_submit() { global $wpdb; $result = true; if ( !current_user_can( AFFILIATES_ADMINISTER_AFFILIATES ) ) { wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) ); } if ( !wp_verify_nonce( $_POST[AFFILIATES_ADMIN_AFFILIATES_NONCE], plugin_basename( __FILE__ ) ) ) { wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) ); } $affiliates_table = _affiliates_get_tablename( 'affiliates' ); $affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' ); $affiliate_id = isset( $_POST['affiliate-id-field'] ) ? $_POST['affiliate-id-field'] : null; $is_direct = false; $affiliate = null; if ( $affiliate = $wpdb->get_row( $wpdb->prepare( "SELECT affiliate_id FROM $affiliates_table WHERE affiliate_id = %d", intval( $affiliate_id ) ) ) ) { $is_direct = isset( $affiliate->type ) && ( $affiliate->type == AFFILIATES_DIRECT_TYPE ); } if ( empty( $affiliate ) ) { wp_die( __( 'No such affiliate.', AFFILIATES_PLUGIN_DOMAIN ) ); } $name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : null; // 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'] ); if ( is_email( $email ) ) { $data['email'] = $email; $formats[] = '%s'; } else { $data['email'] = null; // (*) $formats[] = 'NULL'; // (*) } $from_date = $_POST['from-date-field']; 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']; 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'; // (*) } $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 ); } // user association // delete old association if necessary $current_associated_user = $wpdb->get_row( $wpdb->prepare(" SELECT affiliate_id, user_id, user_login FROM $affiliates_users_table LEFT JOIN $wpdb->users ON $affiliates_users_table.user_id = $wpdb->users.ID WHERE affiliate_id = %d", intval( $affiliate_id ) ) ); $new_associated_user_login = trim( $_POST['user-field'] ); if ( ( empty( $new_associated_user_login ) && !empty( $current_associated_user ) ) || ( !empty( $current_associated_user ) && ( strcmp( $current_associated_user->user_login, $new_associated_user_login ) !== 0 ) ) ) { $wpdb->query( $wpdb->prepare( "DELETE FROM $affiliates_users_table WHERE affiliate_id = %d", intval( $affiliate_id ) ) ); } // new association if ( !empty( $affiliate_id ) && !empty( $new_associated_user_login ) && ( empty( $current_associated_user ) || ( !empty( $current_associated_user ) && ( strcmp( $current_associated_user->user_login, $new_associated_user_login ) !== 0 ) ) ) ) { $new_associated_user = get_user_by( 'login', $new_associated_user_login ); if ( !empty( $new_associated_user ) ) { if ( $wpdb->query( $wpdb->prepare( "INSERT INTO $affiliates_users_table SET affiliate_id = %d, user_id = %d", intval( $affiliate_id ), intval( $new_associated_user->ID ) ) ) ) { if ( empty( $email ) && !empty( $new_associated_user->user_email ) ) { $wpdb->query( $wpdb->prepare( "UPDATE $affiliates_table SET email = %s WHERE affiliate_id = %d", $new_associated_user->user_email, $affiliate_id ) ); } } } } } else { $result = false; } return $result; } // function affiliates_admin_affiliates_edit_submit ?>