affiliates-admin-affiliates-add.php
7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* affiliates-admin-affiliates-add.php
*
* Copyright (c) 2010, 2011 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package affiliates
* @since affiliates 1.1.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Show add affiliate form.
*/
function affiliates_admin_affiliates_add() {
if ( !current_user_can( AFFILIATES_ADMINISTER_AFFILIATES ) ) {
wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) );
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'paged', $current_url );
$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'] : '';
$email = isset( $_POST['email-field'] ) ? $_POST['email-field'] : '';
$user_login = isset( $_POST['user-field'] ) ? $_POST['user-field'] : '';
$from_date = isset( $_POST['from-date-field'] ) ? $_POST['from-date-field'] : '';
$thru_date = isset( $_POST['thru-date-field'] ) ? $_POST['thru-date-field'] : '';
$output =
'<div class="manage-affiliates">' .
'<div>' .
'<h1>' .
__( 'Add a new affiliate', AFFILIATES_PLUGIN_DOMAIN ) .
'</h1>' .
'</div>' .
'<form id="add-affiliate" action="' . esc_url( $current_url ) . '" method="post">' .
'<div class="affiliate new">' .
'<div class="field">' .
'<label class="field-label first required">' .
'<span class="label">' .
__( 'Name', AFFILIATES_PLUGIN_DOMAIN ) .
'</span>' .
' ' .
'<input id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr( stripslashes( $name ) ) . '"/>' .
'</label>' .
'</div>' .
'<div class="field">' .
'<label class="field-label">' .
'<span class="label">' .
__( 'Email', AFFILIATES_PLUGIN_DOMAIN ) .
'</span>' .
' ' .
'<input id="email-field" name="email-field" class="emailfield" type="text" value="' . esc_attr( $email ) . '"/>' .
'</label>' .
'<span class="description">' .
__( "If a valid <strong>Username</strong> is specified and no email is given, the user's email address will be used automatically.", AFFILIATES_PLUGIN_DOMAIN ) .
'</span>' .
'</div>' .
'<div class="field">' .
'<label class="field-label">' .
'<span class="label">' .
__( 'Username', AFFILIATES_PLUGIN_DOMAIN ) .
'</span>' .
' ' .
'<input id="user-field" name="user-field" class="userfield" type="text" value="' . esc_attr( stripslashes( $user_login ) ) . '"/>' .
'</label>' .
'</div>' .
'<div class="field">' .
'<label class="field-label">' .
'<span class="label">' .
__( 'From', AFFILIATES_PLUGIN_DOMAIN ) .
'</span>' .
' ' .
'<input id="from-date-field" name="from-date-field" class="datefield" type="text" value="' . esc_attr( $from_date ) . '"/>' .
'</label>' .
'</div>' .
'<div class="field">' .
'<label class="field-label">' .
'<span class="label">' .
__( 'Until', AFFILIATES_PLUGIN_DOMAIN ) .
'</span>' .
' ' .
'<input id="thru-date-field" name="thru-date-field" class="datefield" type="text" value="' . esc_attr( $thru_date ) . '"/>' .
'</label>' .
'</div>' .
'<div class="field">' .
wp_nonce_field( 'affiliates-add', AFFILIATES_ADMIN_AFFILIATES_NONCE, true, false ) .
'<input class="button button-primary" type="submit" value="' . __( 'Add', AFFILIATES_PLUGIN_DOMAIN ) . '"/>' .
'<input type="hidden" value="add" name="action"/>' .
' ' .
'<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', AFFILIATES_PLUGIN_DOMAIN ) . '</a>' .
'</div>' .
'</div>' . // .affiliate.new
'</form>' .
'</div>'; // .manage-affiliates
echo $output;
affiliates_footer();
} // function affiliates_admin_affiliates_add
/**
* Handle add affiliate form submission.
*/
function affiliates_admin_affiliates_add_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], 'affiliates-add' ) ) {
wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) );
}
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : null;
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'; // (*)
}
$data_ = array();
$formats_ = array();
foreach( $data as $key => $value ) { // (*)
if ( $value ) {
$data_[$key] = $value;
}
}
foreach( $formats as $format ) { // (*)
if ( $format != "NULL" ) {
$formats_[] = $format;
}
}
if ( $wpdb->insert( $affiliates_table, $data_, $formats_ ) ) {
$affiliate_id = $wpdb->get_var( "SELECT LAST_INSERT_ID()" );
}
// user association
$new_associated_user_login = trim( $_POST['user-field'] );
// new association
if ( !empty( $affiliate_id ) && !empty( $new_associated_user_login ) ) {
$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 ) );
}
}
}
}
// hook
if ( !empty( $affiliate_id ) ) {
do_action( 'affiliates_added_affiliate', intval( $affiliate_id ) );
}
} else {
$result = false;
}
return $result;
} // function affiliates_admin_affiliates_add_submit