-
Notifications
You must be signed in to change notification settings - Fork 2
/
target.php
612 lines (545 loc) · 23.8 KB
/
target.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
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
global $_browsers;
global $_oses;
function toutrix_my_plugin_dir($file) {
$retour = "/wp-content/plugins/wp_toutrix/";
return $retour;
}
//if (is_admin()) {
//if (!is_admin()) {
wp_enqueue_script('jquery');
//}
$_browsers = array(
'ie' => 'Internet Explorer',
'firefox' => 'Firefox',
'chrome' => 'Chrome',
'opera' => 'Opera',
'safari' => 'Safari'
);
$_oses = array(
'Windows' => 'Windows',
'Linux' => 'Linux',
'Mac' => 'Mac',
);
function get_form_target() {
$fields = new stdclass();
if (!empty($_POST['id']))
$fields->id = intval($_POST['id']);
if (isset($_GET['flightId'])) {
$fields->flightId = intval($_GET['flightId']);
} else {
$fields->campaignId = intval($_GET['campaignId']);
}
$fields->isExcept = false;
if ($_POST['exception']=='on')
$fields->isExcept = true;
$fields->target_type = $_POST['target_type'];
if ($fields->target_type == 'country') {
$countries = array();
foreach ($_POST['country_code'] as $num => $country_code) {
$countries[] = $country_code;
}
$countries = json_encode($countries);
$fields->target_value = $countries;
} else if ($fields->target_type == 'language') {
$langs = array();
foreach ($_POST['language'] as $num => $lang) {
$langs[] = $lang;
}
$langs = json_encode($langs);
$fields->target_value = $langs;
} else if ($fields->target_type == 'browser') {
$langs = array();
foreach ($_POST['browser'] as $num => $lang) {
$langs[] = $lang;
}
$langs = json_encode($langs);
$fields->target_value = $langs;
} else if ($fields->target_type == 'os') {
$langs = array();
foreach ($_POST['os'] as $num => $lang) {
$langs[] = $lang;
}
$langs = json_encode($langs);
$fields->target_value = $langs;
} else if ($fields->target_type == 'channelId') {
foreach ($_POST['channel_ids'] as $num => $chanId) {
$channels[] = intval($chanId);
}
$channels = json_encode($channels);
$fields->target_value = $channels;
} else if ($fields->target_type == 'is_mobile') {
$fields->target_value = $_POST['true_false'];
} else if ($fields->target_type == 'is_carrier') {
$fields->target_value = $_POST['true_false'];
} else if ($fields->target_type == 'is_referer') {
$fields->target_value = $_POST['true_false'];
} else if ($fields->target_type == 'keywords') {
$arr = explode(",",$_POST['keywords']);
$fields->target_value = json_encode($arr);
} else if ($fields->target_type == 'domain') {
$arr = explode(",",$_POST['keywords']);
$fields->target_value = json_encode($arr);
} else {
$fields->target_value = $_POST['target_value'];
}
stripslashes_deep( $fields );
return $fields;
}
function toutrix_show_targets($targets) {
global $toutrix_adserver;
?>
<div id='wrap'>
<?
$targets_table = new targets_table();
$targets_table->set_datas($targets);
$targets_table->prepare_items();
$targets_table->display();
?>
</div>
<?php
//var_dump($targets);
}
function toutrix_show_target_line($target) {
echo " <tr><td>" . $target->target_type ."</td><td>";
if ($target->target_type == 'country') {
$first = true;
$values = json_decode($target->target_value, true);
foreach ($values as $country_code) {
if (!$first) echo "<br/>";
$lcountry = strtolower($country_code);
echo "<img src= '" . plugins_url( 'flags/' . $lcountry . '.png', __FILE__ ) . "'> " . $country_code;
$first = false;
}
} else {
echo $target->target_value;
}
echo "</td></tr>";
}
function toutrix_show_target_form($target) {
wp_enqueue_style( 'jquery-tag-it', toutrix_my_plugin_dir(__FILE__) . 'css/jquery.tagit.css' );
wp_enqueue_script( 'jquery-tag-it', toutrix_my_plugin_dir(__FILE__) . 'js/tag-it.js');
wp_enqueue_style( 'jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/flick/jquery-ui.css' );
wp_enqueue_script( 'jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js');
global $_countries;
global $_languages;
global $_browsers;
global $_oses;
?>
<form method='POST'>
<input type='hidden' name='target' value='yes'>
<?php if (!empty($target->id)) { ?>
<input type='hidden' name='id' value='<?php echo $target->id; ?>'>
Target type: <br/>
<?php echo $target->target_type . "<br/>"; ?>
<input type='hidden' name='target_type' id='target_type' value='<?php echo $target->target_type; ?>'>
<?php } else { ?>
Exception: <br/>
<input type='checkbox' name='exception'><br/>
Target type: <br/>
<select id='sel_target_type' name='target_type'>
<option value='choose'>Select a targeting type</option>
<option value='country' <?php if ($target->target_type == 'country') echo "selected"; ?>>Target country</option>
<!-- <option value='city' <?php if ($target->target_type == 'city') echo "selected"; ?>>Target city</option> -->
<option value='is_referer' <?php if ($target->target_type == 'is_referer') echo "selected"; ?>>Have referal</option>
<option value='is_mobile' <?php if ($target->target_type == 'is_mobile') echo "selected"; ?>>Is mobile</option>
<option value='is_carrier' <?php if ($target->target_type == 'is_carrier') echo "selected"; ?>>Is Carrier 3G</option>
<option value='channelId' <?php if ($target->target_type == 'channelId') echo "selected"; ?>>By channel</option>
<option value='keywords' <?php if ($target->target_type == 'keywords') echo "selected"; ?>>By keywords</option>
<option value='domain' <?php if ($target->target_type == 'domain') echo "selected"; ?>>By domains</option>
<option value='language' <?php if ($target->target_type == 'language') echo "selected"; ?>>By user language</option>
<option value='browser' <?php if ($target->target_type == 'browser') echo "selected"; ?>>By user browser</option>
<option value='os' <?php if ($target->target_type == 'os') echo "selected"; ?>>By user OS</option>
</select>
<br/>
<?php } ?>
<div id='target_table_value'>
Target value: <br/>
<input type='text' name='target_value' value="<?php echo $target->target_value; ?>"><br/>
</div>
<div id='target_keywords'>
Domains: <br/>
<input id='keywords' type='text' name='keywords' value=""><br/>
</div>
<div id='target_countries'>
<?php
foreach ($_countries as $country_code => $country_name) {
echo "<input type='checkbox' name='country_code[]' value='" . $country_code . "'> ";
echo "<img src= '" . plugins_url( 'flags/' . strtolower($country_code) . '.png', __FILE__ ) . "'> ";
echo $country_code . " " . $country_name . "<br/>";
}
?>
</div>
<div id='target_language'>
<?php
foreach ($_languages as $lang_code => $lang) {
echo "<input type='checkbox' name='language[]' value='" . $lang_code . "'> ";
echo $lang . "<br/>";
}
?>
</div>
<div id='target_browser'>
<?php
foreach ($_browsers as $browser_code => $browser) {
echo "<input type='checkbox' name='browser[]' value='" . $browser_code . "'> ";
echo $browser . "<br/>";
}
?>
</div>
<div id='target_os'>
<?php
foreach ($_oses as $os_code => $os) {
echo "<input type='checkbox' name='os[]' value='" . $os_code . "'> ";
echo $os . "<br/>";
}
?>
</div>
<div id='target_true_false'>
<select name='true_false'>
<option value='true'>True</option>
<option value='false'>False</option>
</select>
</div>
<div id='target_channels'>
<?php
$channels = toutrix_get_channels();
foreach ($channels as $channel) {
echo "<input type='checkbox' name='channel_ids[]' value='" . $channel->id . "'> ";
echo $channel->Title . "<br/>";
}
?>
</div>
<script type="text/javascript">
jQuery(document).ready( function () {
jQuery('#keywords').tagit();
updateForm();
jQuery('#sel_target_type').change(function() {
updateForm();
});
function updateForm() {
var target_type = jQuery('#sel_target_type').find(":selected").val();
jQuery("#target_table_value").hide();
jQuery("#target_channels").hide();
jQuery("#target_language").hide();
jQuery("#target_true_false").hide();
jQuery("#target_browser").hide();
jQuery("#target_os").hide();
jQuery("#target_keywords").hide();
jQuery("#target_countries").hide();
if (target_type == 'country') {
jQuery("#target_countries").show();
} else if (target_type == 'channelId') {
jQuery("#target_channels").show();
} else if (target_type == 'keywords') {
jQuery("#target_keywords").show();
} else if (target_type == 'domain') {
jQuery("#target_keywords").show();
} else if (target_type == 'language') {
jQuery("#target_language").show();
} else if (target_type == 'browser') {
jQuery("#target_browser").show();
} else if (target_type == 'os') {
jQuery("#target_os").show();
} else if (target_type == 'is_mobile' || target_type == 'is_carrier' || target_type == 'is_referer') {
jQuery("#target_true_false").show();
} else {
jQuery("#target_table_value").show();
}
}
});
</script>
<input type='submit' name='b' value='Save'>
</form>
<hr/>
It's easier to delete a target and create a new one. You can't update a target yet.<br/>
We are going to work on a better form very soon. We understand it's not easy.
<?php
}
class targets_table extends WP_List_Table {
var $datas;
/** ************************************************************************
* REQUIRED. Set up a constructor that references the parent constructor. We
* use the parent reference to set some default configs.
***************************************************************************/
function __construct(){
global $status, $page;
//Set parent defaults
parent::__construct( array(
'singular' => 'target', //singular name of the listed records
'plural' => 'targets', //plural name of the listed records
'ajax' => false //does this table support ajax?
) );
}
function set_datas($stats) {
$arr = array();
foreach ($stats as $target) {
$target_type = $target->target_type;
if ($target->isExcept==1)
$target_type = "Not " . $target_type;
$new_crea = array('id'=>$target->id, 'isExcept'=>$target->isExcept, 'target_type' => $target_type, 'target_value' => $target->target_value);
$arr[] = $new_crea;
}
$this->datas = $arr;
}
/** ************************************************************************
* Recommended. This method is called when the parent class can't find a method
* specifically build for a given column. Generally, it's recommended to include
* one method for each column you want to render, keeping your package class
* neat and organized. For example, if the class needs to process a column
* named 'title', it would first see if a method named $this->column_title()
* exists - if it does, that method will be used. If it doesn't, this one will
* be used. Generally, you should try to use custom column methods as much as
* possible.
*
* Since we have defined a column_title() method later on, this method doesn't
* need to concern itself with any column with a name of 'title'. Instead, it
* needs to handle everything else.
*
* For more detailed insight into how columns are handled, take a look at
* WP_List_Table::single_row_columns()
*
* @param array $item A singular item (one full row's worth of data)
* @param array $column_name The name/slug of the column to be processed
* @return string Text or HTML to be placed inside the column <td>
**************************************************************************/
function column_default($item, $column_name){
switch($column_name){
case 'isExcept':
if ($item[$column_name]==1) {
return "Yes";
} else {
return "No";
}
case 'target_type':
return $item[$column_name];
case 'target_value':
return $item[$column_name];
default:
return print_r($item,true); //Show the whole array for troubleshooting purposes
}
}
/** ************************************************************************
* Recommended. This is a custom column method and is responsible for what
* is rendered in any column with a name/slug of 'title'. Every time the class
* needs to render a column, it first looks for a method named
* column_{$column_title} - if it exists, that method is run. If it doesn't
* exist, column_default() is called instead.
*
* This example also illustrates how to implement rollover actions. Actions
* should be an associative array formatted as 'slug'=>'link html' - and you
* will need to generate the URLs yourself. You could even ensure the links
*
*
* @see WP_List_Table::::single_row_columns()
* @param array $item A singular item (one full row's worth of data)
* @return string Text to be placed inside the column <td> (movie title only)
**************************************************************************/
function column_target($item){
//Build row actions
if (isset($_GET['flightId'])) {
$actions = array(
'edit' => sprintf('<a href="?page=%s&action=%s&campaignId=%s&flightId=%s&tab=targets&id=%s">Edit</a>',$_REQUEST['page'],'edit',$_GET['campaignId'],$_GET['flightId'],$item['id']),
'delete' => sprintf('<a href="?page=%s&action=%s&campaignId=%s&flightId=%s&removetargetid=%s&tab=targets">Delete</a>',$_REQUEST['page'],'edit',$_GET['campaignId'],$_GET['flightId'],$item['id']),
/*
'stats' => sprintf('<a href="?page=%s&action=%s&campaignId=%s">Stats</a>',$_REQUEST['page'],'stats',$item['id']),
*/
);
} else {
$actions = array(
'edit' => sprintf('<a href="?page=%s&action=%s&campaignId=%s&tab=targets&id=%s">Edit</a>',$_REQUEST['page'],'edit',$_GET['campaignId'],$_GET['flightId'],$item['id']),
'delete' => sprintf('<a href="?page=%s&action=%s&campaignId=%s&removetargetid=%s&tab=targets">Delete</a>',$_REQUEST['page'],'edit',$_GET['campaignId'],$item['id']),
/*
'stats' => sprintf('<a href="?page=%s&action=%s&campaignId=%s">Stats</a>',$_REQUEST['page'],'stats',$item['id']),
*/
);
}
//Return the title contents
return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
/*$1%s*/ $item['name'],
/*$2%s*/ $item['id'],
/*$3%s*/ $this->row_actions($actions)
);
}
/** ************************************************************************
* REQUIRED if displaying checkboxes or using bulk actions! The 'cb' column
* is given special treatment when columns are processed. It ALWAYS needs to
* have it's own method.
*
* @see WP_List_Table::::single_row_columns()
* @param array $item A singular item (one full row's worth of data)
* @return string Text to be placed inside the column <td> (movie title only)
**************************************************************************/
function column_cb($item){
return sprintf(
'<input type="checkbox" name="%1$s[]" value="%2$s" />',
/*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("movie")
/*$2%s*/ $item['ID'] //The value of the checkbox should be the record's id
);
}
/** ************************************************************************
* REQUIRED! This method dictates the table's columns and titles. This should
* return an array where the key is the column slug (and class) and the value
* is the column's title text. If you need a checkbox for bulk actions, refer
* to the $columns array below.
*
* The 'cb' column is treated differently than the rest. If including a checkbox
* column in your table you must create a column_cb() method. If you don't need
* bulk actions or checkboxes, simply leave the 'cb' entry out of your array.
*
* @see WP_List_Table::::single_row_columns()
* @return array An associative array containing column information: 'slugs'=>'Visible Titles'
**************************************************************************/
function get_columns(){
$columns = array(
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
'target' => 'Target',
'target_type' => 'Target Type',
'target_value' => 'Target Value'
);
return $columns;
}
/** ************************************************************************
* Optional. If you want one or more columns to be sortable (ASC/DESC toggle),
* you will need to register it here. This should return an array where the
* key is the column that needs to be sortable, and the value is db column to
* sort by. Often, the key and value will be the same, but this is not always
* the case (as the value is a column name from the database, not the list table).
*
* This method merely defines which columns should be sortable and makes them
* clickable - it does not handle the actual sorting. You still need to detect
* the ORDERBY and ORDER querystring variables within prepare_items() and sort
* your data accordingly (usually by modifying your query).
*
* @return array An associative array containing all the columns that should be sortable: 'slugs'=>array('data_values',bool)
**************************************************************************/
function get_sortable_columns() {
$sortable_columns = array(
/*
'title' => array('title',false), //true means it's already sorted
'rating' => array('rating',false),
'director' => array('director',false)
*/
);
return $sortable_columns;
}
/** ************************************************************************
* Optional. If you need to include bulk actions in your list table, this is
* the place to define them. Bulk actions are an associative array in the format
* 'slug'=>'Visible Title'
*
* If this method returns an empty value, no bulk action will be rendered. If
* you specify any bulk actions, the bulk actions box will be rendered with
* the table automatically on display().
*
* Also note that list tables are not automatically wrapped in <form> elements,
* so you will need to create those manually in order for bulk actions to function.
*
* @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'
**************************************************************************/
function get_bulk_actions() {
$actions = array(
// 'delete' => 'Delete'
);
return $actions;
}
/** ************************************************************************
* Optional. You can handle your bulk actions anywhere or anyhow you prefer.
* For this example package, we will handle it in the class to keep things
* clean and organized.
*
* @see $this->prepare_items()
**************************************************************************/
function process_bulk_action() {
//Detect when a bulk action is being triggered...
if( 'delete'===$this->current_action() ) {
wp_die('Items deleted (or they would be if we had items to delete)!');
}
}
/** ************************************************************************
* REQUIRED! This is where you prepare your data for display. This method will
* usually be used to query the database, sort and filter the data, and generally
* get it ready to be displayed. At a minimum, we should set $this->items and
* $this->set_pagination_args(), although the following properties and methods
* are frequently interacted with here...
*
* @global WPDB $wpdb
* @uses $this->_column_headers
* @uses $this->items
* @uses $this->get_columns()
* @uses $this->get_sortable_columns()
* @uses $this->get_pagenum()
* @uses $this->set_pagination_args()
**************************************************************************/
function prepare_items() {
global $wpdb; //This is used only if making any database queries
/**
* First, lets decide how many records per page to show
*/
$per_page = 31;
/**
* REQUIRED. Now we need to define our column headers. This includes a complete
* array of columns to be displayed (slugs & titles), a list of columns
* to keep hidden, and a list of columns that are sortable. Each of these
* can be defined in another method (as we've done here) before being
* used to build the value for our _column_headers property.
*/
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
/**
* REQUIRED. Finally, we build an array to be used by the class for column
* headers. The $this->_column_headers property takes an array which contains
* 3 other arrays. One for all columns, one for hidden columns, and one
* for sortable columns.
*/
$this->_column_headers = array($columns, $hidden, $sortable);
/**
* Optional. You can handle your bulk actions however you see fit. In this
* case, we'll handle them within our package just to keep things clean.
*/
$this->process_bulk_action();
$data = $this->datas;
/**
* This checks for sorting input and sorts the data in our array accordingly.
*
* In a real-world situation involving a database, you would probably want
* to handle sorting by passing the 'orderby' and 'order' values directly
* to a custom query. The returned data will be pre-sorted, and this array
* sorting technique would be unnecessary.
*/
usort($data, 'usort_reorder');
/**
* REQUIRED for pagination. Let's figure out what page the user is currently
* looking at. We'll need this later, so you should always include it in
* your own package classes.
*/
$current_page = $this->get_pagenum();
/**
* REQUIRED for pagination. Let's check how many items are in our data array.
* In real-world use, this would be the total number of items in your database,
* without filtering. We'll need this later, so you should always include it
* in your own package classes.
*/
$total_items = count($data);
/**
* The WP_List_Table class does not handle pagination for us, so we need
* to ensure that the data is trimmed to only the current page. We can use
* array_slice() to
*/
$data = array_slice($data,(($current_page-1)*$per_page),$per_page);
/**
* REQUIRED. Now we can add our *sorted* data to the items property, where
* it can be used by the rest of the class.
*/
$this->items = $data;
/**
* REQUIRED. We also have to register our pagination options & calculations.
*/
$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
) );
}
}