-
Notifications
You must be signed in to change notification settings - Fork 0
/
second-chance.php
466 lines (412 loc) · 21.4 KB
/
second-chance.php
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
/*
Plugin Name: Second Chance
Plugin URI: http://www.hanneswidrig.com
Description: This Plugin provides magical traits
Version: 1.1.2
Author: Hannes Widrig
Author URI: http://www.hanneswidrig.com
License: A "Slug" license name e.g. GPL2
*/
add_action('admin_menu', 'register_second_chance_menu');
add_action('admin_enqueue_scripts', 'register_scripts');
add_action('wp_ajax_bid_now_live_me', 'SecondChance_insert_sc_bid');
add_action('wp_ajax_nopriv_bid_now_live_me', 'SecondChance_insert_sc_bid');
add_action('template_redirect', 'SecondChance_get_top_bidders');
add_action('template_redirect', 'SecondChance_payment');
update_option('SecondChance_subject', 'A second chance to purchase the item you bid on!');
update_option('SecondChance_message', 'Hello ##username##,'.PHP_EOL.PHP_EOL.
'You did not win the auction, but you we can offer you the opportunity to purchase this item at a discounted price.'.PHP_EOL.
'The ##item_name## will cost $##item_price##. ##item_link##'.PHP_EOL.PHP_EOL.
'Thank you,'.PHP_EOL.
'##your_site_name## Team');
register_activation_hook(__FILE__, 'SecondChance_first_run');
include 'functions.php';
include 'accept-offer.php';
function SecondChance_first_run() {
$path = WP_PLUGIN_DIR . "/second-chance/config.txt";
$fileContents = file_get_contents($path);
$decoded = json_decode($fileContents, true);
if($decoded["first_run"] === 0) {
SecondChance_create_offer_page();
SecondChance_create_table();
$array = array(
'first_run' => 1,
'last_post_before_install' => 0,
'last_post_checked' => 0
);
$encodedString = json_encode($array);
file_put_contents( $path, $encodedString );
}
}
function register_second_chance_menu() {
add_menu_page(
'Second Chance',
'Second Chance',
'manage_options',
'second-chance',
'SecondChance_home_page',
'dashicons-smiley');
}
function register_scripts() {
wp_register_style('styles', plugins_url('css/styles.css', __FILE__) );
wp_enqueue_style('styles');
wp_register_script('scripts', plugins_url('js/scripts.js', __FILE__) );
wp_enqueue_script('scripts');
}
function SecondChance_home_page() {
global $wpdb;
$posts_db = $wpdb->posts;
$second_chance_db = $wpdb->prefix.'penny_second_chance';
$postsPerPage = 8;
if(isset($_GET['pj'])) {$page = $_GET['pj'];}
else {$page = 1;}
$query_for_all_pages = "
SELECT distinct posts.ID, sc.pid, sc.uid, sc.total_bids, sc.winner, sc.offered, sc.status
FROM $posts_db AS posts, $second_chance_db AS sc
WHERE posts.ID = sc.pid AND sc.offered = 1 AND sc.winner <> 1";
$query_for_page = "
SELECT distinct posts.ID, posts.post_date, sc.pid, sc.uid, sc.total_bids, sc.winner, sc.offered, sc.status
FROM $posts_db AS posts, $second_chance_db AS sc
WHERE posts.ID = sc.pid AND sc.offered = 1 AND sc.winner <> 1 AND sc.status <= 1
ORDER BY posts.post_date DESC LIMIT ".($postsPerPage * ($page - 1) ).",".$postsPerPage;
$query_for_orders = "
SELECT distinct posts.ID, posts.post_date, sc.pid, sc.uid, sc.total_bids, sc.winner, sc.offered, sc.status
FROM $posts_db AS posts, $second_chance_db AS sc
WHERE posts.ID = sc.pid AND sc.offered = 1 AND sc.winner <> 1 AND sc.status >= 2
ORDER BY posts.post_date DESC LIMIT ".($postsPerPage * ($page - 1) ).",".$postsPerPage;
$total_num_offered = count($wpdb->get_results($query_for_all_pages));
$my_page = $page;
$current_page = $page;
$totalPages = ($total_num_offered > 0 ? ceil($total_num_offered / $postsPerPage) : 0);
$pages_s = $totalPages;
$page_posts = $wpdb->get_results($query_for_page);
$page_orders = $wpdb->get_results($query_for_orders);
?>
<div class="sc_wrapper">
<header>
<p id="sc_title">Second Chances Plugin</p>
</header>
<main>
<div class="sc_navi">
<ul>
<li><a href="#1" class="sc_link">Users Offered</a></li><li><a href="#2" class="sc_link">Paid Orders</a></li><li><a href="#3" class="sc_link">Settings</a></li>
</ul>
</div>
<div class="sc_body_main">
<div id="1" class="sc_tabs" style="display:block;">
<table class="widefat post fixed">
<thead>
<tr>
<th>Auction Title</th>
<th>Buyer</th>
<th>Total Bids</th>
<th>Cost</th>
<th>Discount</th>
<th>Shipping</th>
<th>Total</th>
<th>Date</th>
<th>Status</th>
<th style="display: none;">UID</th>
<th style="display: none;">PID</th>
</tr>
</thead>
<tbody>
<?php $i = 0; if ($page_posts): ?>
<?php global $post; ?>
<?php foreach ($page_posts as $post): ?>
<?php setup_postdata($post); ?>
<?php
$title = get_the_title(get_the_ID());
$uid = $post->uid;
$buyer = get_user_by('ID',$post->uid)->user_nicename;
$total_bids = $post->total_bids;
$msrp = get_post_meta(get_the_ID(),'retail_price',true);
$total_wo = SecondChance_calculate_discounted_price(get_the_ID(),$total_bids);
$discount = $msrp - $total_wo;
$shp = get_post_meta(get_the_ID(),'shipping',true); if(empty($shp)) {$shp = 0;}
$total = $total_wo + $shp;
$msrp = SecondChance_money_formatter($msrp);
$discount = SecondChance_money_formatter($discount);
$shp = SecondChance_money_formatter($shp);
$total = SecondChance_money_formatter($total);
$time = get_post_meta(get_the_ID(), 'ending', true);
$date_choosen = date("M j, Y, g:i a", $time);
$status = $post->status;
switch($status) {
case 0:
$status = "Not";
break;
case 1:
$status = "Clicked";
break;
case 2:
$status = "Paid";
break;
case 3:
$status = "Shipped";
break;
}
?>
<tr class="sc-tr">
<td title="<?php echo get_the_ID() ?>"><?php echo $title ?></td>
<td title="<?php echo $uid ?>"><?php echo $buyer ?></td>
<td><?php echo $total_bids ?></td>
<td><?php echo $msrp ?></td>
<td><?php echo $discount ?></td>
<td><?php echo $shp ?></td>
<td><a href="<?php echo get_site_url().'/my-account/accept-offer/?pid='.get_the_ID().'&user='.$uid ?>"><?php echo $total ?></a></td>
<td><?php echo $date_choosen ?></td>
<td><select class="status" name="selector">
<option hidden><?php echo $status ?></option>
<option value="0">Not Accepted</option>
<option value="1">Accepted</option>
<option value="2">Clicked</option>
<option value="3">Shipped</option>
</select></td>
<td class="nr1" style="display: none;"><?php echo $uid ?></td>
<td class="nr2" style="display: none;"><?php echo get_the_ID() ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<div class="sc_nav">
<?php
$batch = 10;
$end = $batch * $postsPerPage;
if ($end > $pages_s) {$end = $pages_s;}
$start = $end - $postsPerPage + 1;
if($start < 1) {$start = 1;}
$links = '';
$report = ceil($my_page/$batch) - 1; if ($report < 0) {$report = 0;}
$start = $report * $batch + 1;
$end = $start + $batch - 1;
$end_me = $end + 1;
$start_me = $start - 1;
if($end > $totalPages) {$end = $totalPages;}
if($end_me > $totalPages) {$end_me = $totalPages;}
if($start_me <= 0) {$start_me = 1;}
$previous_pg = $page - 1;
if($previous_pg <= 0) {$previous_pg = 1;}
$next_pg = $current_page + 1;
if($next_pg > $totalPages) {$next_pg = 1;}
if($my_page > 1)
{
echo '<a href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj='.$previous_pg.'"><< '.__('Previous','PennyTheme').'</a>';
echo '<a href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj=' .$start_me.'"><<</a>';
}
for($i = $start; $i <= $end; $i ++) {
if ($i == $current_page) {
echo '<a class="activee" href="#">'.$i.'</a>';
} else {
echo '<a href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj=' . $i.'" > '.$i.'</a>';
}
}
/*if($totalPages > $my_page)
{echo '<a href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj=' . $end_me.'">>></a>';}*/
if($page < $totalPages)
{echo '<a href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj=' . $next_pg.'"> '.__('Next','PennyTheme').' >></a>';}
?>
</div>
</div>
<div id="2" class="sc_tabs">
<table class="widefat post fixed">
<thead>
<tr>
<th>Auction Title</th>
<th>Buyer</th>
<th>Total Bids</th>
<th>Cost</th>
<th>Discount</th>
<th>Shipping</th>
<th>Total</th>
<th>Purchase Date</th>
<th>Status</th>
<th style="display: none;">UID</th>
<th style="display: none;">PID</th>
</tr>
</thead>
<tbody>
<?php $i = 0; if ($page_orders): ?>
<?php global $post; ?>
<?php foreach ($page_orders as $post): ?>
<?php setup_postdata($post); ?>
<?php
$title = get_the_title(get_the_ID());
$uid = $post->uid;
$buyer = get_user_by('ID',$post->uid)->user_nicename;
$total_bids = $post->total_bids;
$msrp = get_post_meta(get_the_ID(),'retail_price',true);
$total_wo = SecondChance_calculate_discounted_price(get_the_ID(),$total_bids);
$discount = $msrp - $total_wo;
$shp = get_post_meta(get_the_ID(),'shipping',true); if(empty($shp)) {$shp = 0;}
$total = $total_wo + $shp;
$msrp = SecondChance_money_formatter($msrp);
$discount = SecondChance_money_formatter($discount);
$shp = SecondChance_money_formatter($shp);
$total = SecondChance_money_formatter($total);
$date_choosen = $post->post_date;
$status = $post->status;
switch($status) {
case 0:
$status = "Not";
break;
case 1:
$status = "Accepted";
break;
case 2:
$status = "Paid";
break;
case 3:
$status = "Shipped";
break;
}
?>
<tr class="sc-tr">
<th><?php echo $title ?></th>
<th><?php echo $buyer ?></th>
<th><?php echo $total_bids ?></th>
<th><?php echo $msrp ?></th>
<th><?php echo $discount ?></th>
<th><?php echo $shp ?></th>
<th><?php echo $total ?></th>
<th><?php echo $date_choosen ?></th>
<td><select class="status" name="selector">
<option hidden><?php echo $status ?></option>
<option value="0">Not Accepted</option>
<option value="1">Accepted</option>
<option value="2">Paid</option>
<option value="3">Shipped</option>
</select></td>
<td class="nr1" style="display: none;"><?php echo $uid ?></td>
<td class="nr2" style="display: none;"><?php echo get_the_ID() ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<div class="sc_nav">
<?php
$batch = 10;
$end = $batch * $postsPerPage;
if ($end > $pages_s) {$end = $pages_s;}
$start = $end - $postsPerPage + 1;
if($start < 1) {$start = 1;}
$links = '';
$report = ceil($my_page/$batch) - 1; if ($report < 0) {$report = 0;}
$start = $report * $batch + 1;
$end = $start + $batch - 1;
$end_me = $end + 1;
$start_me = $start - 1;
if($end > $totalPages) {$end = $totalPages;}
if($end_me > $totalPages) {$end_me = $totalPages;}
if($start_me <= 0) {$start_me = 1;}
$previous_pg = $page - 1;
if($previous_pg <= 0) {$previous_pg = 1;}
$next_pg = $current_page + 1;
if($next_pg > $totalPages) {$next_pg = 1;}
// if($my_page > 1)
// {
// echo '<a href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj='.$previous_pg.'"><< '.__('Previous','PennyTheme').'</a>';
// echo '<a href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj=' .$start_me.'"><<</a>';
// }
for($i = $start; $i <= $end; $i ++) {
if ($i == $current_page) {
echo '<a class="activee" href="#">'.$i.'</a>';
} else {
echo '<a style="margin: 0 4px;" href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj=' . $i.'" > '.$i.'</a>';
}
}
/*if($totalPages > $my_page)
{echo '<a style="margin: 0 4px;" href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj=' . $end_me.'">>></a>';}*/
if($page < $totalPages)
{echo '<a style="margin: 0 4px;" href="'.get_bloginfo('siteurl').'/wp-admin/admin.php?page=second-chance&pj=' . $next_pg.'">'.__('Next','PennyTheme').' >></a>';}
?>
</div>
</div>
<div id="3" class="sc_tabs">
<p style="padding-left: 10px;">Version 1.0</p>
<div id="new_user_email">
<div><?php _e('This email will be received by you and when a user is offered a discounted rate.','PennyTheme'); ?> </div>
<form method="post" action="<?php bloginfo('siteurl');?>/wp-admin/admin.php?page=second-chance#3">
<table width="100%">
<tr>
<td width="160"><?php _e('Email Subject:','PennyTheme'); ?></td>
<td><input type="text" size="90" name="SecondChance_subject" value="<?php echo stripslashes(get_option('SecondChance_subject')); ?>"/></td>
</tr>
<tr>
<td valign=top ><?php _e('Email Content:','PennyTheme'); ?></td>
<td><textarea cols="92" rows="10" name="SecondChance_message"><?php echo stripslashes(get_option('SecondChance_message')); ?></textarea></td>
</tr>
<tr>
<td valign=top ></td>
<td><div class="spntxt_bo2">
<?php _e('Here is a list of tags you can use in this email:','PennyTheme'); ?><br/><br/>
<strong>##username##</strong> - <?php _e("your new username",'PennyTheme'); ?><br/>
<strong>##username_email##</strong> - <?php _e("your new user's email",'PennyTheme'); ?><br/>
<strong>##your_site_name##</strong> - <?php _e("your website's name","PennyTheme"); ?><br/>
<strong>##item_name##</strong> - <?php _e("auction item name","PennyTheme"); ?><br/>
<strong>##item_link##</strong> - <?php _e("Provides the link for user to accept offer.","PennyTheme"); ?><br/>
<strong>##item_price##</strong> - <?php _e("Total price after discount and shipping.",'PennyTheme'); ?>
</div></td>
</tr>
<tr>
<td ></td>
<td><input type="submit" name="SecondChance-save-email" value="<?php _e('Save Options','PennyTheme'); ?>"/></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</main>
<footer><h4>Created by Hannes Widrig</h4></footer>
</div>
<?php
}
function SecondChance_create_offer_page() {
global $wpdb;
$doesExist = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type = 'page' AND
post_content = '[penny_theme_my_account_accept-offer]'");
if(count($doesExist) === 0) {
$post = array(
'post_title' => 'Accept Offer',
'post_content' => '[penny_theme_my_account_accept-offer]',
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'ping_status' => 'closed',
'post_parent' => get_option('PennyTheme_my_account_page_id'));
$post_id = wp_insert_post($post);
update_post_meta($post_id, '_wp_page_template', 'penny-special-page-template.php');
}
}
function SecondChance_payment() {
global $wp_query, $wp;
$wp->add_query_var('pay_second');
$a_action = $wp_query->query_vars['a_action'];
if($a_action == "pay_second") {
// include dirname(__FILE__).'/pay.php';
include 'pay.php';
exit;
}
}
if(isset($_POST['SecondChance-save-email'])) {
update_option('SecondChance_subject', trim($_POST['SecondChance_subject']));
update_option('SecondChance_message', trim($_POST['SecondChance_message']));
echo '<script language="javascript">';
echo 'alert("Email updated!")';
echo '</script>';
}
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'update-row':
$status = $_POST['status'];
$uid = $_POST['uid'];
$pid = $_POST['pid'];
SecondChance_update_row($status, $uid, $pid);
break;
}
}