-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsam-pro-admin-actions.php
1239 lines (1171 loc) · 50.8 KB
/
sam-pro-admin-actions.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Created by PhpStorm.
* Author: minimus
* Date: 03.11.2016
* Time: 15:05
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'SamProSizesLite' ) ) {
class SamProSizesLite {
public $size;
public $name;
public $width;
public $height;
private $aSizes;
public function __construct( $value = '', $width = null, $height = null ) {
$this->aSizes = array(
'800x90' => sprintf( '%1$s x %2$s %3$s', 800, 90, 'Large Leaderboard' ),
'728x90' => sprintf( '%1$s x %2$s %3$s', 728, 90, 'Leaderboard' ),
'600x90' => sprintf( '%1$s x %2$s %3$s', 600, 90, 'Small Leaderboard' ),
'550x250' => sprintf( '%1$s x %2$s %3$s', 550, 250, 'Mega Unit' ),
'550x120' => sprintf( '%1$s x %2$s %3$s', 550, 120, 'Small Leaderboard' ),
'550x90' => sprintf( '%1$s x %2$s %3$s', 550, 90, 'Small Leaderboard' ),
'468x180' => sprintf( '%1$s x %2$s %3$s', 468, 180, 'Tall Banner' ),
'468x120' => sprintf( '%1$s x %2$s %3$s', 468, 120, 'Tall Banner' ),
'468x90' => sprintf( '%1$s x %2$s %3$s', 468, 90, 'Tall Banner' ),
'468x60' => sprintf( '%1$s x %2$s %3$s', 468, 60, 'Banner' ),
'450x90' => sprintf( '%1$s x %2$s %3$s', 450, 90, 'Tall Banner' ),
'430x90' => sprintf( '%1$s x %2$s %3$s', 430, 90, 'Tall Banner' ),
'400x90' => sprintf( '%1$s x %2$s %3$s', 400, 90, 'Tall Banner' ),
'234x60' => sprintf( '%1$s x %2$s %3$s', 234, 60, 'Half Banner' ),
'200x90' => sprintf( '%1$s x %2$s %3$s', 200, 90, 'Tall Half Banner' ),
'150x50' => sprintf( '%1$s x %2$s %3$s', 150, 50, 'Half Banner' ),
'120x90' => sprintf( '%1$s x %2$s %3$s', 120, 90, 'Button' ),
'120x60' => sprintf( '%1$s x %2$s %3$s', 120, 60, 'Button' ),
'83x31' => sprintf( '%1$s x %2$s %3$s', 83, 31, 'Micro Bar' ),
'728x15x4' => sprintf( '%1$s x %2$s %3$s', 728, 15, 'Thin Banner 4 Links' ),
'728x15x5' => sprintf( '%1$s x %2$s %3$s', 728, 15, 'Thin Banner 5 Links' ),
'468x15x4' => sprintf( '%1$s x %2$s %3$s', 468, 15, 'Thin Banner 4 Links' ),
'468x15x5' => sprintf( '%1$s x %2$s %3$s', 468, 15, 'Thin Banner 5 Links' ),
'160x600' => sprintf( '%1$s x %2$s %3$s', 160, 600, 'Wide Skyscraper' ),
'120x600' => sprintf( '%1$s x %2$s %3$s', 120, 600, 'Skyscraper' ),
'200x360' => sprintf( '%1$s x %2$s %3$s', 200, 360, 'Wide Half Banner' ),
'240x400' => sprintf( '%1$s x %2$s %3$s', 240, 400, 'Vertical Rectangle' ),
'180x300' => sprintf( '%1$s x %2$s %3$s', 180, 300, 'Tall Rectangle' ),
'200x270' => sprintf( '%1$s x %2$s %3$s', 200, 270, 'Tall Rectangle' ),
'120x240' => sprintf( '%1$s x %2$s %3$s', 120, 240, 'Vertical Banner' ),
'336x280' => sprintf( '%1$s x %2$s %3$s', 336, 280, 'Large Rectangle' ),
'336x160' => sprintf( '%1$s x %2$s %3$s', 336, 160, 'Wide Rectangle' ),
'334x100' => sprintf( '%1$s x %2$s %3$s', 334, 100, 'Wide Rectangle' ),
'300x250' => sprintf( '%1$s x %2$s %3$s', 300, 250, 'Medium Rectangle' ),
'300x150' => sprintf( '%1$s x %2$s %3$s', 300, 150, 'Small Wide Rectangle' ),
'300x125' => sprintf( '%1$s x %2$s %3$s', 300, 125, 'Small Wide Rectangle' ),
'300x70' => sprintf( '%1$s x %2$s %3$s', 300, 70, 'Mini Wide Rectangle' ),
'250x250' => sprintf( '%1$s x %2$s %3$s', 250, 250, 'Square' ),
'200x200' => sprintf( '%1$s x %2$s %3$s', 200, 200, 'Small Square' ),
'200x180' => sprintf( '%1$s x %2$s %3$s', 200, 180, 'Small Rectangle' ),
'180x150' => sprintf( '%1$s x %2$s %3$s', 180, 150, 'Small Rectangle' ),
'160x160' => sprintf( '%1$s x %2$s %3$s', 160, 160, 'Small Square' ),
'125x125' => sprintf( '%1$s x %2$s %3$s', 125, 125, 'Button' ),
'200x90x4' => sprintf( '%1$s x %2$s %3$s', 200, 90, 'Tall Half Banner 4 Links' ),
'200x90x5' => sprintf( '%1$s x %2$s %3$s', 200, 90, 'Tall Half Banner 5 Links' ),
'180x90x4' => sprintf( '%1$s x %2$s %3$s', 180, 90, 'Half Banner 4 Links' ),
'180x90x5' => sprintf( '%1$s x %2$s %3$s', 180, 90, 'Half Banner 5 Links' ),
'160x90x4' => sprintf( '%1$s x %2$s %3$s', 160, 90, 'Tall Button 4 Links' ),
'160x90x5' => sprintf( '%1$s x %2$s %3$s', 160, 90, 'Tall Button 5 Links' ),
'120x90x4' => sprintf( '%1$s x %2$s %3$s', 120, 90, 'Button 4 Links' ),
'120x90x5' => sprintf( '%1$s x %2$s %3$s', 120, 90, 'Button 5 Links' )
);
self::setSize( $value, $width, $height );
}
public function setSize( $value = '', $width = null, $height = null ) {
if ( $value == '' ) {
$this->size = '468x60';
$this->name = 'Banner';
$this->width = 468;
$this->height = 60;
} elseif ( $value == 'custom' ) {
$this->size = $value;
$this->name = 'Custom size';
$this->width = $width;
$this->height = $height;
} else {
$aSize = explode( "x", $value );
$this->size = $value;
$this->name = $this->aSizes[ $value ];
$this->width = $aSize[0];
$this->height = $aSize[1];
}
}
}
}
if ( ! class_exists( 'SamProAdminActions' ) ) {
class SamProAdminActions {
private $action;
private $params;
public function __construct( $action, $params ) {
$this->action = $action;
$this->params = $params;
}
private function getLimit( $params ) {
$out = '';
$skip = ( ( isset( $params['skip'] ) ) ? (int) $params['skip'] : null );
$take = ( ( isset( $params['take'] ) ) ? (int) $params['take'] : null );
if ( $take != null ) {
$out = " LIMIT " . $skip . "," . $take;
}
return $out;
}
private function getSorted( $params ) {
$sorted = ( isset( $params['sorted'] ) ) ? $params['sorted'] : null;
$sortedArray = array();
if ( $sorted != null ) {
$columncount = count( $sorted );
for ( $i = $columncount - 1; $i >= 0; $i -- ) {
$svalue = $sorted[ $i ]['name'];
$firstLetter = substr( $sorted[ $i ]['direction'], 0, 1 );
if ( $firstLetter == 'a' ) {
$direction = substr( $sorted[ $i ]['direction'], 0, 3 );
} else {
$direction = substr( $sorted[ $i ]['direction'], 0, 4 );
}
$tempQuery = $svalue . " " . $direction;
array_push( $sortedArray, $tempQuery );
}
$out = " ORDER BY " . join( ",", $sortedArray );
} else {
$out = '';
}
return $out;
}
private function getUpdateData( $params, $table ) {
$value = ( ( isset( $params['value'] ) ) ? $params['value'] : null );
$pid = (int) $value['pid'];
$aid = (int) $value['aid'];
$weight = (int) $value['weight'];
return array(
'table' => $table,
'data' => array( 'weight' => $weight ),
'where' => array( 'pid' => $pid, 'aid' => $aid ),
'format' => array( '%d' ),
'where_format' => array( '%d', '%d' )
);
}
private function getWhere( $input, $prefix = '', $complex = false, $str = false ) {
$op = '';
$intFields = array( 'pid', 'aid', 'zid', 'bid' );
$postfix = '';
$res = '';
$data = ( $input[0]['isComplex'] ) ? $input[0]['predicates'] : $input;
foreach ( $data as $in ) {
$str = ( ( ! $str ) ? ! in_array( $in['field'], $intFields ) : $str );
$res .= ( ( empty( $res ) ) ? '' : ' AND ' );
$res .= $prefix . $in['field'];
switch ( $in['operator'] ) {
case 'equal':
$op = ' = ';
break;
case 'notequal':
$op = ' != ';
break;
case 'startswith':
$op = " LIKE '";
$postfix = "%'";
$str = false;
break;
case 'endswith':
$op = " LIKE '%";
$postfix = "'";
$str = false;
break;
case 'contains':
$op = " LIKE '%";
$postfix = "%'";
$str = false;
break;
}
$res .= $op;
$res .= ( $str ) ? "\"{$in['value']}\"" : $in['value'];
$res .= $postfix;
}
$res = ( ( $complex ) ? ' AND ' : ' WHERE ' ) . $res;
return $res;
}
public function doAction() {
if ( is_null( $this->action ) ) {
die();
}
$params = $this->params;
$charset = get_option( 'blog_charset' );
header( "Content-Type: application/json; charset={$charset}" );
global $wpdb;
$pTable = $wpdb->prefix . 'sampro_places';
$paTable = $wpdb->prefix . 'sampro_places_ads';
$aTable = $wpdb->prefix . 'sampro_ads';
$zTable = $wpdb->prefix . 'sampro_zones';
$zrTable = $wpdb->prefix . 'sampro_zones_rules';
$bTable = $wpdb->prefix . 'sampro_blocks';
$eTable = $wpdb->prefix . 'sampro_errors';
$sTable = $wpdb->prefix . 'sampro_stats';
$rTable = $wpdb->prefix . 'sampro_regions';
$postTable = $wpdb->prefix . 'posts';
$tTable = $wpdb->prefix . "terms";
$ttTable = $wpdb->prefix . "term_taxonomy";
$uTable = $wpdb->base_prefix . "users";
$umTable = $wpdb->base_prefix . "usermeta";
$userLevel = $wpdb->base_prefix . 'user_level';
$options = get_option( 'samProOptions' );
$where = ( isset( $params['where'] ) ? $params['where'] : false );
$limit = self::getLimit( $params );
$sort = self::getSorted( $params );
$action = 'sam_pro_ajax_' . $this->action;
switch ( $action ) {
case 'sam_pro_ajax_load_places':
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'pt.' ) : '' );
$concat = "IF(pt.trash, \"<i class='icon-trash'></i>\", \"<i class='icon-leaf'></i>\") AS stat";
$sql = "SELECT pt.pid, pt.title, pt.description, pt.asize AS psize, {$concat}, trash FROM {$pTable} pt{$sqlWhere}{$sort}{$limit};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
$apSize = new SamProSizesLite( '', 0, 0 );
foreach ( $rows as $key => $value ) {
$apSize->setSize( $value['psize'], 0, 0 );
$rows[ $key ]['psize'] = $apSize->name;
}
$sql = "SELECT COUNT(*) FROM {$pTable} pt{$sqlWhere};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
break;
case 'sam_pro_ajax_load_places_ads':
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'wspa.' ) : '' );
$concat = "CONCAT(IF((wsa.trash OR (wsa.moderated = 0) OR (wspa.weight = 0)), \"<i class='icon-eye-off'></i>\", \"<i class='icon-eye'></i>\"),
IF(wsa.moderated = 0, \"<i class='icon-traffic-cone'></i>\", \"<i class='icon-ok'></i>\"),
IF(wsa.trash, \"<i class='icon-trash'></i>\", \"<i class='icon-leaf'></i>\")) AS trash";
$sql = "SELECT wspa.pid, wspa.aid, wsa.title, wsa.description, wspa.weight, {$concat} FROM {$paTable} wspa INNER JOIN {$aTable} wsa ON wspa.aid = wsa.aid {$sqlWhere}{$sort}{$limit};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
$out = array( 'count' => count( $rows ), 'result' => $rows );
break;
case 'sam_pro_ajax_load_places_ads_item':
$pid = ( isset( $_GET['pid'] ) ) ? (integer) $_GET['pid'] : 0;
if ( $pid > 0 ) {
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'wspa.', true ) : '' );
$concat = "CONCAT(IF((wsa.trash OR (wsa.moderated = 0) OR (wspa.weight = 0)), \"<i class='icon-eye-off'></i>\", \"<i class='icon-eye'></i>\"),
IF(wsa.moderated = 0, \"<i class='icon-traffic-cone'></i>\", \"<i class='icon-ok'></i>\"),
IF(wsa.trash, \"<i class='icon-trash'></i>\", \"<i class='icon-leaf'></i>\")) AS trash";
$sql = "SELECT wspa.pid, wspa.aid, wsa.title, wsa.description, wspa.weight, {$concat} FROM {$paTable} wspa INNER JOIN {$aTable} wsa ON wspa.aid = wsa.aid WHERE wspa.pid = {$pid}{$sqlWhere}{$sort}{$limit};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$paTable} wspa WHERE wspa.pid = {$pid};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
} else {
$out = array( 'count' => 0, 'result' => array() );
}
break;
case 'sam_pro_ajax_load_ads':
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'wat.' ) : '' );
$concat = "CONCAT(IF((wat.trash OR (wat.moderated = 0)), \"<i class='icon-eye-off'></i>\", \"<i class='icon-eye'></i>\"),
IF(wat.moderated = 0, \"<i class='icon-traffic-cone'></i>\", \"<i class='icon-ok'></i>\"),
IF(wat.trash, \"<i class='icon-trash'></i>\", \"<i class='icon-leaf'></i>\")) AS stat";
$sql = "SELECT wat.aid, wat.title, wat.description, wat.asize, wat.moderated, {$concat}, wat.trash FROM {$aTable} wat{$sqlWhere}{$sort}{$limit};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
foreach ( $rows as $key => $value ) {
$rows[ $key ]['moderated'] = (boolean) $rows[ $key ]['moderated'];
}
$sql = "SELECT COUNT(*) FROM {$aTable} wat{$sqlWhere};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
break;
case 'sam_pro_ajax_load_zones':
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'zt.' ) : '' );
$concat = "IF(zt.trash, \"<i class='icon-trash'></i>\", \"<i class='icon-leaf'></i>\") AS stat";
$sql = "SELECT zt.zid, zt.title, zt.description, {$concat}, zt.trash FROM {$zTable} zt{$sqlWhere}{$sort}{$limit};";
$sss = $sql;
$rows = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$zTable} zt{$sqlWhere};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
break;
case 'sam_pro_ajax_load_blocks':
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'bt.' ) : '' );
$concat = "IF(bt.b_rows IS NOT NULL AND bt.b_columns IS NOT NULL, CONCAT(bt.b_rows, \" x \", bt.b_columns), '') AS size,";
$concat .= "IF(bt.trash, \"<i class='icon-trash'></i>\", \"<i class='icon-leaf'></i>\") AS stat";
$sql = "SELECT bt.bid, bt.title, bt.description, {$concat}, bt.trash FROM {$bTable} bt{$sqlWhere}{$sort}{$limit};";
$sss = $sql;
$rows = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$bTable} bt{$sqlWhere};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
break;
case 'sam_pro_ajax_load_error_log':
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'et.' ) : '' );
$concat = "IF(et.etype = 0, IF(et.solved = 0, \"<i class='icon-warning-empty'></i>\", \"<i class='icon-shield'></i>\"), \"<i class='icon-ok'></i>\") AS stype";
$sql = "SELECT et.eid, et.edate, et.tname, et.emsg, et.etype, {$concat}, et.esql, et.solved FROM {$eTable} et{$sqlWhere}{$sort}{$limit};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$eTable} et{$sqlWhere};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
break;
case 'sam_pro_ajax_lookup_ads':
$sql = "SELECT aid, title FROM {$aTable};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
$out = array( 'count' => count( $rows ), 'result' => $rows );
break;
case 'sam_pro_ajax_update_place_ad':
$update = self::getUpdateData( $params, $paTable );
$res = $wpdb->update( $update['table'], $update['data'], $update['where'], $update['format'], $update['where_format'] );
$out = array( "Updated" => array( "weight" => $update['data']['weight'] ) );
break;
case 'sam_pro_ajax_trash_place':
if ( isset( $_POST['pid'] ) && isset( $_POST['trash'] ) ) {
$pid = (int) $_POST['pid'];
$trash = (int) $_POST['trash'];
$res = $wpdb->update(
$pTable,
array( 'trash' => $trash ),
array( 'pid' => $pid ),
array( '%d' ),
array( '%d' )
);
$out = array( 'updated' => array( 'pid' => $pid ) );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_trash_ad':
if ( isset( $_POST['aid'] ) && isset( $_POST['trash'] ) ) {
$aid = (int) $_POST['aid'];
$trash = (int) $_POST['trash'];
$res = $wpdb->update(
$aTable,
array( 'trash' => $trash ),
array( 'aid' => $aid ),
array( '%d' ),
array( '%d' )
);
$out = array( 'updated' => array( 'aid' => $aid ) );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_trash_zone':
if ( isset( $_POST['zid'] ) && isset( $_POST['trash'] ) ) {
$zid = (int) $_POST['zid'];
$trash = (int) $_POST['trash'];
$res = $wpdb->update(
$zTable,
array( 'trash' => $trash ),
array( 'zid' => $zid ),
array( '%d' ),
array( '%d' )
);
$out = array( 'updated' => array( 'zid' => $zid ) );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_trash_block':
if ( isset( $_POST['bid'] ) && isset( $_POST['trash'] ) ) {
$bid = (int) $_POST['bid'];
$trash = (int) $_POST['trash'];
$res = $wpdb->update(
$bTable,
array( 'trash' => $trash ),
array( 'bid' => $bid ),
array( '%d' ),
array( '%d' )
);
$out = array( 'updated' => array( 'bid' => $bid ) );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_moderate_ad':
if ( isset( $_POST['aid'] ) && isset( $_POST['mod'] ) ) {
$aid = (int) $_POST['aid'];
$mod = (int) $_POST['mod'];
$res = $wpdb->update(
$aTable,
array( 'moderated' => $mod ),
array( 'aid' => $aid ),
array( '%d' ),
array( '%d' )
);
$out = array( 'updated' => array( 'aid' => $aid ) );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_trash_place_ad':
if ( isset( $_POST['pid'] ) && isset( $_POST['trash'] ) && isset( $_POST['aid'] ) ) {
$pid = (int) $_POST['pid'];
$aid = (int) $_POST['aid'];
$trash = (int) $_POST['trash'];
$res = $wpdb->update(
$paTable,
array( 'trash' => $trash ),
array( 'pid' => $pid, 'aid' => $aid ),
array( '%d' ),
array( '%d', '%d' )
);
$out = array( 'updated' => array( 'pid' => $pid, 'aid' => $aid ) );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_remove_place':
$act = $params['action'];
if ( $act == 'remove' ) {
$value = $params['key'];
$col = $params['keyColumn'];
$sql = "DELETE FROM {$pTable} WHERE {$col} = {$value};";
$wpdb->query( $sql );
$out = array( "Deleted" => $value );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_remove_ad':
$act = $params['action'];
if ( $act == 'remove' ) {
$value = $params['key'];
$col = $params['keyColumn'];
$sql = "DELETE FROM {$aTable} WHERE {$col} = {$value};";
$wpdb->query( $sql );
$sql = "DELETE FROM {$paTable} WHERE {$col} = {$value};";
$wpdb->query( $sql );
$out = array( "Deleted" => $value );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_remove_zone':
$act = $params['action'];
if ( $act == 'remove' ) {
$value = $params['key'];
$col = $params['keyColumn'];
$sql = "DELETE FROM {$zTable} WHERE {$col} = {$value};";
$wpdb->query( $sql );
$out = array( "Deleted" => $value );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_remove_block':
$act = $params['action'];
if ( $act == 'remove' ) {
$value = $params['key'];
$col = $params['keyColumn'];
$sql = "DELETE FROM {$bTable} WHERE {$col} = {$value};";
$wpdb->query( $sql );
$out = array( "Deleted" => $value );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_remove_error':
$act = $params['action'];
if ( $act == 'remove' ) {
$value = $params['key'];
$col = $params['keyColumn'];
$sql = "DELETE FROM {$eTable} WHERE {$col} = {$value};";
$out = array( "Deleted" => $value );
$wpdb->query( $sql );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_load_posts':
$custs = ( isset( $_REQUEST['cstr'] ) ) ? $_REQUEST['cstr'] : '';
$sPost = ( isset( $_REQUEST['sp'] ) ) ? urldecode( $_REQUEST['sp'] ) : 'Post';
$sPage = ( isset( $_REQUEST['spg'] ) ) ? urldecode( $_REQUEST['spg'] ) : 'Page';
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'wp.', true ) : '' );
$sql = "SELECT
wp.id,
wp.post_title AS title,
wp.post_type AS type
FROM
$postTable wp
WHERE
wp.post_status = 'publish' AND
FIND_IN_SET(wp.post_type, 'post,page{$custs}')
{$sqlWhere}{$sort}{$limit};";
$posts = $wpdb->get_results( $sql, ARRAY_A );
$k = 0;
foreach ( $posts as &$val ) {
switch ( $val['type'] ) {
case 'post':
$val['type'] = $sPost;
break;
case 'page':
$val['type'] = $sPage;
break;
default:
$val['type'] = $sPost . ': ' . $val['type'];
break;
}
}
$sql = "SELECT COUNT(*) FROM {$postTable} wp WHERE wp.post_status = 'publish' AND FIND_IN_SET(wp.post_type, 'post,page{$custs}');";
$count = $wpdb->get_var( $sql );
$out = array(
'count' => $count,
'result' => $posts
);
break;
case 'sam_pro_ajax_load_cats':
$sql = "SELECT wt.term_id AS id, wt.name AS title, wt.slug
FROM $tTable wt
INNER JOIN $ttTable wtt
ON wt.term_id = wtt.term_id
WHERE wtt.taxonomy = 'category'{$sort}{$limit};";
$cats = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$tTable} wt INNER JOIN {$ttTable} wtt ON wt.term_id = wtt.term_id WHERE wtt.taxonomy = 'category';";
$count = $wpdb->get_var( $sql );
$out = array(
'count' => $count,
'result' => $cats
);
break;
case 'sam_pro_ajax_load_tags':
$sql = "SELECT wt.term_id AS id, wt.name AS title, wt.slug
FROM $tTable wt
INNER JOIN $ttTable wtt
ON wt.term_id = wtt.term_id
WHERE wtt.taxonomy = 'post_tag'{$sort}{$limit};";
$tags = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$tTable} wt INNER JOIN {$ttTable} wtt ON wt.term_id = wtt.term_id WHERE wtt.taxonomy = 'post_tag';";
$count = $wpdb->get_var( $sql );
$out = array(
'count' => $count,
'result' => $tags
);
break;
case 'sam_pro_ajax_load_authors':
$sql = "SELECT wu.id, wu.display_name AS title, wu.user_nicename AS slug
FROM {$uTable} wu
INNER JOIN {$umTable} wum ON wu.id = wum.user_id
WHERE wum.meta_key = '{$userLevel}' AND wum.meta_value > 1{$sort}{$limit};";
$auth = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$uTable} wu INNER JOIN {$umTable} wum ON wu.id = wum.user_id WHERE wum.meta_key = '{$userLevel}' AND wum.meta_value > 1;";
$count = $wpdb->get_var( $sql );
$out = array(
'count' => $count,
'result' => $auth
);
break;
case 'sam_pro_ajax_load_taxes':
global $wp_taxonomies;
$sql = "SELECT wt.term_id AS id, wt.name AS title, wt.slug, wtt.taxonomy
FROM $tTable wt
INNER JOIN $ttTable wtt
ON wt.term_id = wtt.term_id
WHERE NOT FIND_IN_SET(wtt.taxonomy, 'category,post_tag,nav_menu,link_category,post_format'){$sort}{$limit};";
$cTax = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM $tTable wt INNER JOIN $ttTable wtt ON wt.term_id = wtt.term_id WHERE NOT FIND_IN_SET(wtt.taxonomy, 'category,post_tag,nav_menu,link_category,post_format');";
$count = $wpdb->get_var( $sql );
foreach ( $cTax as &$val ) {
if ( isset( $wp_taxonomies[ $val['taxonomy'] ] ) ) {
$val['ctax_name'] = urldecode( $wp_taxonomies[ $val['taxonomy'] ]->labels->name );
} else {
$val['ctax_name'] = '';
}
}
$out = array(
'count' => $count,
'result' => $cTax
);
break;
case 'sam_pro_ajax_load_types':
$args = array( 'public' => true, '_builtin' => false );
$output = 'objects';
$operator = 'and';
$post_types = get_post_types( $args, $output, $operator );
$customs = array();
foreach ( $post_types as $post_type ) {
array_push( $customs, array( 'title' => $post_type->labels->name, 'slug' => $post_type->name ) );
}
$out = array(
'count' => count( $customs ),
'result' => $customs
);
break;
case 'sam_pro_ajax_load_users':
$roleSubscriber = ( isset( $_REQUEST['subscriber'] ) ) ? urldecode( $_REQUEST['subscriber'] ) : 'Subscriber';
$roleContributor = ( isset( $_REQUEST['contributor'] ) ) ? urldecode( $_REQUEST['contributor'] ) : 'Contributor';
$roleAuthor = ( isset( $_REQUEST['author'] ) ) ? urldecode( $_REQUEST['author'] ) : 'Author';
$roleEditor = ( isset( $_REQUEST['editor'] ) ) ? urldecode( $_REQUEST['editor'] ) : 'Editor';
$roleAdministrator = ( isset( $_REQUEST["admin"] ) ) ? urldecode( $_REQUEST["admin"] ) : 'Administrator';
$roleSuperAdmin = ( isset( $_REQUEST['sadmin'] ) ) ? urldecode( $_REQUEST['sadmin'] ) : 'Super Admin';
$sql = "SELECT
wu.id,
wu.display_name AS title,
wu.user_nicename AS slug,
(CASE wum.meta_value
WHEN 0 THEN '$roleSubscriber'
WHEN 1 THEN '$roleContributor'
WHEN 2 THEN '$roleAuthor'
ELSE
IF(wum.meta_value > 2 AND wum.meta_value <= 7, '$roleEditor',
IF(wum.meta_value > 7 AND wum.meta_value <= 10, '$roleAdministrator',
IF(wum.meta_value > 10, '$roleSuperAdmin', NULL)
)
)
END) AS role
FROM $uTable wu
INNER JOIN $umTable wum
ON wu.id = wum.user_id AND wum.meta_key = '$userLevel'
{$sort}{$limit};";
$users = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM $uTable wu INNER JOIN $umTable wum ON wu.id = wum.user_id AND wum.meta_key = '$userLevel';";
$count = $wpdb->get_var( $sql );
$out = array(
'count' => $count,
'result' => $users
);
break;
case 'sam_pro_ajax_list_ads':
$pid = ( isset( $_REQUEST['pid'] ) ) ? (int) $_REQUEST['pid'] : - 1;
if ( $pid != - 1 ) {
$sql = "SELECT aid, title, description, asize FROM {$aTable} WHERE aid NOT IN(SELECT aid FROM {$paTable} WHERE pid = {$pid}){$sort}{$limit};";
$ads = $wpdb->get_results( $sql );
$sql = "SELECT COUNT(*) FROM {$aTable} WHERE aid NOT IN(SELECT aid FROM {$paTable} WHERE pid = {$pid});";
$count = $wpdb->get_var( $sql );
$out = array(
'count' => $count,
'result' => $ads,
);
} else {
$out = array( 'error' => true );
}
break;
case 'sam_pro_ajax_link_ads':
$sid = ( isset( $_REQUEST['sid'] ) ) ? $_REQUEST['sid'] : '';
$pid = ( isset( $_REQUEST['pid'] ) ) ? (integer) $_REQUEST['pid'] : 0;
$values = '';
if ( $sid != '' && $pid != 0 ) {
$aValues = explode( ',', $sid );
foreach ( $aValues as $val ) {
if ( ! empty( $val ) ) {
$values .= ( ( ( $values == '' ) ? '' : ',' ) . "({$pid},{$val})" );
}
}
$sql = "INSERT INTO {$paTable} (pid, aid) VALUES {$values};";
$affected = $wpdb->query( $sql );
$out = array(
'sid' => $sid,
'done' => $affected,
'sql' => $sql
);
$sql = "UPDATE {$paTable} spa SET spa.hits = 0 WHERE spa.pid = {$pid};";
$wpdb->query( $sql );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_unlink_ad':
$aid = ( isset( $_REQUEST['aid'] ) ) ? (integer) $_REQUEST['aid'] : 0;
$pid = ( isset( $_REQUEST['pid'] ) ) ? (integer) $_REQUEST['pid'] : 0;
if ( $aid > 0 && $pid > 0 ) {
$sql = "DELETE FROM {$paTable} WHERE pid = {$pid} AND aid = {$aid};";
$count = $wpdb->query( $sql );
$out = array(
'success' => true,
'count' => $count
);
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_unlink_ad_item':
//$aid = (isset($_REQUEST['aid'])) ? (integer)$_REQUEST['aid'] : 0;
$pid = ( isset( $_REQUEST['pid'] ) ) ? (integer) $_REQUEST['pid'] : 0;
$act = $params['action'];
if ( $act == 'remove' && $pid > 0 ) {
$value = $params['key'];
$col = $params['keyColumn'];
$sql = "DELETE FROM {$paTable} WHERE pid = {$pid} AND {$col} = {$value};";
$wpdb->query( $sql );
$out = array( "Deleted" => $value );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_load_ad_objects':
$strPlaces = ( isset( $_REQUEST['pl'] ) ) ? $_REQUEST['pl'] : 'Places';
$strAds = ( isset( $_REQUEST['ads'] ) ) ? $_REQUEST['ads'] : 'Single Ads';
$sql = "(SELECT '{$strPlaces}' AS cat, CONCAT(0, '_', pid) AS ival, title FROM {$pTable}) UNION (SELECT '{$strAds}' AS cat, CONCAT(1, '_', aid) AS ival, title FROM {$aTable});";
$ads = $wpdb->get_results( $sql );
$count = count( $ads );
$out = $ads;
break;
case 'sam_pro_ajax_clear_error_log':
$sql = "DELETE FROM {$eTable};";
$res = $wpdb->query( $sql );
if ( $res === false ) {
$out = array( 'success' => false );
} else {
$out = array( 'success' => true, 'deleted' => $res );
}
break;
case 'sam_pro_ajax_solve_error':
if ( isset( $_POST['eid'] ) && is_numeric( $_POST['eid'] ) ) {
$eid = (integer) $_POST['eid'];
$res = $wpdb->update(
$eTable,
array( 'solved' => 1 ),
array( 'eid' => $eid ),
array( '%d' ),
array( '%d' )
);
$out = array( 'updated' => array( 'eid' => $eid ), 'success' => true );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_load_zone_rules':
$zid = ( isset( $_GET['item'] ) ) ? (int) $_GET['item'] : 0;
if ( $zid > 0 ) {
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'szr.', true ) : '' );
$sql = "SELECT szr.zid, szr.slug, szr.single, szr.tax, szr.name, szr.term_slug, szr.pid, szr.priority
FROM {$zrTable} szr WHERE szr.single = 0 AND szr.zid = {$zid}{$sqlWhere}{$sort}{$limit};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$zrTable} szr WHERE szr.single = 0 AND szr.zid = {$zid};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_update_zone_rule':
$zid = ( isset( $_GET['item'] ) ) ? (int) $_GET['item'] : 0;
if ( $zid > 0 ) {
$value = ( ( isset( $params['value'] ) ) ? $params['value'] : null );
if ( ! is_null( $value ) ) {
$slug = $value['slug'];
$priority = (int) $value['priority'];
$pid = (int) $value['pid'];
$wpdb->update(
$zrTable,
array( 'priority' => $priority, 'pid' => $pid ),
array( 'zid' => $zid, 'slug' => $slug, 'single' => 0 ),
array( '%d', '%d' ),
array( '%d', '%s', '%d' )
);
$out = array( 'Updated' => array( 'pid' => $pid, 'priority' => $priority ) );
} else {
$out = array( 'success' => false );
}
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_remove_zone_rule':
$zid = ( isset( $_GET['item'] ) ) ? (int) $_GET['item'] : 0;
$act = $params['action'];
if ( $act == 'remove' && $zid > 0 ) {
$value = $params['key'];
$col = $params['keyColumn'];
$sql = "DELETE FROM {$zrTable} WHERE zid = {$zid} AND {$col} = '{$value}' AND single = 0;";
$wpdb->query( $sql );
$out = array( "Deleted" => $value );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_add_zone_rules':
$zid = ( isset( $_REQUEST['zid'] ) ) ? (int) $_REQUEST['zid'] : 0;
$rules = ( isset( $_REQUEST['rules'] ) ) ? $_REQUEST['rules'] : null;
$values = '';
if ( ! is_null( $rules ) && is_array( $rules ) && $zid > 0 ) {
foreach ( $rules as $val ) {
$values .= ( ( ! empty( $values ) ) ? ', ' : '' ) . "({$zid}, '{$val['slug']}', 0, '{$val['tax']}', '{$val['name']}', '{$val['term_slug']}', {$val['priority']})";
}
$sql = "INSERT IGNORE INTO {$zrTable} (zid, slug, single, tax, name, term_slug, priority) VALUES {$values};";
$rows = $wpdb->query( $sql );
$out = array( 'rows' => $rows );
} else {
$out = array( 'success' => false, 'zid' => $zid, );
}
break;
case 'sam_pro_ajax_load_zone_single_rules':
$zid = ( isset( $_GET['item'] ) ) ? (int) $_GET['item'] : 0;
if ( $zid > 0 ) {
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'szr.', true ) : '' );
$sql = "SELECT szr.zid, szr.slug, szr.single, szr.tax, szr.name, szr.term_slug, szr.pid, szr.priority
FROM {$zrTable} szr WHERE szr.single = 1 AND szr.zid = {$zid}{$sqlWhere}{$sort}{$limit};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$zrTable} szr WHERE szr.single = 1 AND szr.zid = {$zid};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_update_zone_single_rule':
$zid = ( isset( $_GET['item'] ) ) ? (int) $_GET['item'] : 0;
if ( $zid > 0 ) {
$value = ( ( isset( $params['value'] ) ) ? $params['value'] : null );
if ( ! is_null( $value ) ) {
$slug = $value['slug'];
$priority = (int) $value['priority'];
$pid = (int) $value['pid'];
$wpdb->update(
$zrTable,
array( 'priority' => $priority, 'pid' => $pid ),
array( 'zid' => $zid, 'slug' => $slug, 'single' => 1 ),
array( '%d', '%d' ),
array( '%d', '%s', '%d' )
);
$out = array( 'Updated' => array( 'pid' => $pid, 'priority' => $priority ) );
} else {
$out = array( 'success' => false );
}
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_remove_zone_single_rule':
$zid = ( isset( $_GET['item'] ) ) ? (int) $_GET['item'] : 0;
$act = $params['action'];
if ( $act == 'remove' && $zid > 0 ) {
$value = $params['key'];
$col = $params['keyColumn'];
$sql = "DELETE FROM {$zrTable} WHERE zid = {$zid} AND {$col} = '{$value}' AND single = 1;";
$wpdb->query( $sql );
$out = array( "Deleted" => $value );
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_add_zone_single_rules':
$zid = ( isset( $_REQUEST['zid'] ) ) ? (int) $_REQUEST['zid'] : 0;
$rules = ( isset( $_REQUEST['rules'] ) ) ? $_REQUEST['rules'] : null;
$values = '';
if ( ! is_null( $rules ) && is_array( $rules ) && $zid > 0 ) {
foreach ( $rules as $val ) {
$values .= ( ( ! empty( $values ) ) ? ', ' : '' ) . "({$zid}, '{$val['slug']}', 1, '{$val['tax']}', '{$val['name']}', '{$val['term_slug']}', {$val['priority']})";
}
$sql = "INSERT IGNORE INTO {$zrTable} (zid, slug, single, tax, name, term_slug, priority) VALUES {$values};";
$rows = $wpdb->query( $sql );
$out = array( 'rows' => $rows );
} else {
$out = array( 'success' => false, 'zid' => $zid, );
}
break;
case 'sam_pro_ajax_load_regions':
if ( isset( $params['co'] ) ) {
$valid = true;
$aco = explode( ',', $params['co'] );
foreach ( $aco as $iso ) {
$valid &= ( strlen( $iso ) == 2 );
}
if ( $valid ) {
$sqlWhere = ( ( $where ) ? self::getWhere( $where, 'sr.', true ) : '' );
$sql = "SELECT sr.iso_1, sr.country, sr.region_name, CONCAT(sr.iso_1, '-', sr.iso_2) AS iso_2, sr.region_type
FROM {$rTable} sr WHERE FIND_IN_SET(sr.iso_1, '{$params['co']}'){$sqlWhere}{$limit};";
$rows = $wpdb->get_results( $sql, ARRAY_A );
$sql = "SELECT COUNT(*) FROM {$rTable} sr WHERE FIND_IN_SET(sr.iso_1, '{$params['co']}') {$sqlWhere};";
$count = $wpdb->get_var( $sql );
$out = array( 'count' => $count, 'result' => $rows );
} else {
$out = array( 'success' => false );
}
} else {
$out = array( 'success' => false );
}
break;
case 'sam_pro_ajax_load_ad_chart_data':
if ( isset( $_REQUEST['start'] ) && isset( $_REQUEST['end'] ) && isset( $_REQUEST['aid'] ) && isset( $_REQUEST['pid'] ) && isset( $_REQUEST['period'] ) ) {
$start = $_REQUEST['start'];
$end = $_REQUEST['end'];
$aid = (int) $_REQUEST['aid'];
$pid = (int) $_REQUEST['pid'];
$period = (int) $_REQUEST['period'];
if ( $period < 20 && $period >= 0 ) {
$sql = "SELECT DATE_FORMAT(ss.edate, '%Y-%m-%dT00:00:01') AS mdate, SUM(ss.hits) AS hits, SUM(ss.clicks) AS clicks
FROM {$sTable} ss
WHERE ss.edate BETWEEN '{$start}' AND '{$end}' AND ss.pid = {$pid} AND ss.aid = {$aid}
GROUP BY mdate;";
} elseif ( $period == 20 || $period == 21 ) {
$sql = "SELECT DATE_FORMAT(ss.edate, '%Y-%m-01T00:00:01') AS mdate, SUM(ss.hits) AS hits, SUM(ss.clicks) AS clicks
FROM {$sTable} ss
WHERE ss.edate BETWEEN '{$start}' AND '{$end}' AND ss.pid = {$pid} AND ss.aid = {$aid}
GROUP BY mdate;";
} else {
$sql = '';
}
if ( ! empty( $sql ) ) {
$rows = $wpdb->get_results( $sql, ARRAY_A );
} else {
$rows = array();
}
$out = $rows;
} else {
$out = array();
}
break;
case 'sam_pro_ajax_load_chart_data':
if ( isset( $params['start'] ) && isset( $params['end'] ) && isset( $params['owner'] ) && isset( $params['item'] ) && isset( $params['period'] ) ) {
$start = $params['start'];
$end = $params['end'];
$owner = $params['owner'];
$item = (int) $params['item'];
$period = (int) $params['period'];
$ownerData = ( $owner === 'all' ) ? '' : " INNER JOIN {$aTable} sa ON ss.aid = sa.aid";
$ownerClause = ( $owner === 'all' ) ? '' : " AND sa.owner = '{$owner}'";
if ( $period < 20 && $period >= 0 ) {
$sql = "SELECT DATE_FORMAT(ss.edate, '%Y-%m-%dT00:00:01') AS mdate, SUM(ss.hits) AS hits, SUM(ss.clicks) AS clicks
FROM {$sTable} ss
INNER JOIN {$pTable} sp ON ss.pid = sp.pid{$ownerData}
WHERE ss.edate BETWEEN '{$start}' AND '{$end}' AND sp.sale{$ownerClause}
GROUP BY mdate;";
} elseif ( $period == 20 || $period == 21 ) {
$sql = "SELECT DATE_FORMAT(ss.edate, '%Y-%m-01T00:00:01') AS mdate, SUM(ss.hits) AS hits, SUM(ss.clicks) AS clicks
FROM {$sTable} ss
INNER JOIN {$pTable} sp ON ss.pid = sp.pid{$ownerData}
WHERE ss.edate BETWEEN '{$start}' AND '{$end}' AND sp.sale{$ownerClause}
GROUP BY mdate;";
} else {
$sql = '';
}
if ( ! empty( $sql ) ) {
$rows = $wpdb->get_results( $sql, ARRAY_A );
} else {
$rows = array();
}
$out = $rows;
} else {
$out = array();
}
break;
case 'sam_pro_ajax_load_chart_data_all':
if ( isset( $params['start'] ) && isset( $params['end'] ) && isset( $params['owner'] ) && isset( $params['item'] ) && isset( $params['period'] ) ) {
$start = $params['start'];
$end = $params['end'];
$owner = $params['owner'];
$item = (int) $params['item'];
$period = (int) $params['period'];
if ( $period < 20 && $period >= 0 ) {
$sql = "SELECT DATE_FORMAT(ss.edate, '%Y-%m-%dT00:00:01') AS mdate, SUM(ss.hits) AS hits, SUM(ss.clicks) AS clicks
FROM {$sTable} ss
WHERE ss.edate BETWEEN '{$start}' AND '{$end}'
GROUP BY mdate;";
} elseif ( $period == 20 || $period == 21 ) {
$sql = "SELECT DATE_FORMAT(ss.edate, '%Y-%m-01T00:00:01') AS mdate, SUM(ss.hits) AS hits, SUM(ss.clicks) AS clicks
FROM {$sTable} ss
WHERE ss.edate BETWEEN '{$start}' AND '{$end}'