-
Notifications
You must be signed in to change notification settings - Fork 0
/
adgear-ad-manager.php
545 lines (472 loc) · 18.8 KB
/
adgear-ad-manager.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
<?php
/*
* Plugin Name: AdGear Ad Manager
* Plugin URI: http://github.com/bloom/adgear-ad-manager
* Description: Serve AdGear ads through your blog
* Version: 1.1.2
* Author: Bloom Digital Platforms
* Author URI: http://github.com/bloom/adgear-ad-mananger
* License: GPL2
* */
?>
<?php
/* Copyright 2010 Bloom Digital Platforms ( [email protected] )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
?>
<?php
error_reporting( E_ALL );
adgear_init();
function adgear_init() {
define( 'ADGEAR_PATH', dirname( __FILE__ ) );
define( 'ADGEAR_URL', get_bloginfo( 'wpurl' ) . '/wp-content/plugins/adgear-wp-plugin' );
/* Same callback because we clean the DB of everything that could have existed prior to this install. */
register_activation_hook( 'adgear-ad-manager', 'adgear_deactivate' );
register_deactivation_hook( 'adgear-ad-manager', 'adgear_deactivate' );
if ( function_exists('register_uninstall_hook') ) {
register_uninstall_hook(__FILE__, 'adgear_uninstall');
}
if ( is_admin() ) {
require_once(dirname(__FILE__) . "/adgear-ad-manager/admin.php");
} else {
add_action('wp_head', 'adgear_output_site_embed_tag');
}
}
/* Fully remove all adgear settings from the options DB. */
function adgear_uninstall() {
require_once( dirname(__FILE__).'/uninstall.php' );
}
function adgear_output_site_embed_tag() {
$embed_code = get_option('adgear_site_embed_code');
if ( !$embed_code ) return;
echo "<!-- adgear site embed tag -->\n";
echo $embed_code;
}
function adgear_warning_css_properties() {
return "background:#c33;margin:inherited 2em;padding:.5em;color:#000;";
}
function adgear_ad_internal() {
echo "<!-- adgear adspot embed tag -->\n";
$embed_code = "";
if ( adgear_is_dynamic_site() ) {
$format = func_get_arg(0);
$path = func_get_arg(1);
// Switch on $format
$embed_code = get_option( 'adgear_site_universal_embed_code' );
$embed_code = str_replace( "__CHIP_KEY__", get_option( 'adgear_site_chip_key' ), $embed_code );
$embed_code = str_replace( "__FORMAT_ID__", $format, $embed_code );
if ( $path ) {
$embed_code = preg_replace( '/"path"\s*:\s*\[.*\]/', '"path":'.json_encode( array_values( $path ) ), $embed_code );
} else {
// We might be called with only a single arg, and func_get_arg() returns FALSE in that case
$embed_code = preg_replace( '/"path"\s*:\s*\[.*\]/', '"path":'.json_encode( array() ), $embed_code );
}
} else {
$id = func_get_arg(0);
$key = 'adgear_adspot_embed_code_'.$id;
$embed_code = get_option( $key, "<p style='".adgear_warning_css_properties()."'><strong>WARNING</strong>: AdSpot $id is unknown and cannot be served.</p>" );
}
return $embed_code;
}
function adgear_is_dynamic_site() {
return get_option( 'adgear_site_is_dynamic', FALSE );
}
/* Sorts based on format name */
function adgear_cmp($a, $b) {
$an = $a["name"];
preg_match( "/^(\\d)+x/", $an, $awidth );
$acount = count( $awidth );
$bn = $b["name"];
preg_match( "/^(\\d)+x/", $bn, $bwidth );
$bcount = count( $bwidth );
if ( $acount == 0 && $bcount == 0 ) {
/* 2 names -- sort alphabetically */
return strcmp( $an, $bn );
} else if ( $acount > 0 && $bcount > 0 ) {
/* 2 formats with width/height - sort in ascending width */
return strcmp( $awidth[0], $bwidth[0] );
} else if ( $acount > 0 ) {
/* a format with width/height with a name: name goes first */
return 1;
} else {
/* a name and a format with width/height with a name: name goes first */
return -1;
}
}
function adgear_formats() {
$csv = get_option("adgear_formats_csv");
if ( $csv == "" ) return array();
$formats = array();
foreach( explode( "\n", $csv ) as $line ) {
$row = explode( ",", $line );
if ( count( $row ) > 0 ) {
$formats[] = array( "id" => $row[1], "name" => $row[0], "width" => $row[2], "height" => $row[3] );
}
}
$temp = array();
$output = array();
foreach( $formats as $format ) {
if ( $format["width"] == "" ) {
$output[] = $format;
} else {
$temp[] = $format;
}
}
usort( $temp, "adgear_cmp" );
foreach( $temp as $format ) {
$output[] = $format;
}
return $output;
}
function adgear_ad_spots() {
$csv = get_option("adgear_ad_spots_csv");
if ( $csv == "" ) return array();
$formats = adgear_formats();
$adspots = array();
foreach( explode( "\n", $csv ) as $line ) {
$row = explode( ",", $line );
if ( count( $row ) > 0 ) {
$width = NULL;
$height = NULL;
foreach( $formats as $format ) {
if ( $format["id"] == $row[2] ) {
$width = $format["width"];
$height = $format["height"];
break;
}
}
$adspots[] = array( "id" => $row[1], "name" => $row[0], "width" => $width, "height" => $height );
}
}
return $adspots;
}
function adgear_ad($atts) {
extract(shortcode_atts(array(
"id" => "",
"format" => "",
"path" => "",
"slugify" => "",
"single" => "",
"path_pre" => "",
"path_middle" => "",
"path_post" => "",
), $atts));
switch($single) {
case 'yes':
if (!is_single()) return "";
break;
case 'no':
if (is_single()) return "";
break;
case 'front':
if (!is_front_page()) return "";
break;
case 'home':
if (!is_home()) return "";
break;
case 'all':
/* NOP: we always succeed */
break;
default:
return "<p style='".adgear_warning_css_properties()."'><strong>WARNING</strong>: AdGear Ad Manager did not understand the "single" option. This parameter accepts one of five values: yes, no, all, home or front; not <strong>$single</strong>.</p>";
}
// If this tag should render only on single posts page, and we're not on a single post, abort
if ($single == 'yes' && !is_single()) return "";
// If this tag should render only on listing pages, and we're on a single post, abort
if ($single == 'no' && is_single()) return "";
// echo "<pre><code>";
// var_dump(array(
// "id" => $id,
// "format" => $format,
// "path" => $path,
// "path_pre" => $path_pre,
// "path_middle" => $path_middle,
// "path_post" => $path_post,
// "slugify" => $slugify,
// "single" => $single,
// "site_is_dynamic" => adgear_is_dynamic_site(),
// ));
// echo "</code></pre>";
if ( !adgear_is_dynamic_site() && $id ) {
return adgear_ad_internal( $id );
} else if ( adgear_is_dynamic_site() && $format && $path ) {
$pathname = explode( ',', $path_pre);
switch( $path ) {
case "by_categories":
global $post;
$postcats = get_the_category($post->ID);
$cats = array();
if ( $postcats ) {
foreach( $postcats as $cat ) {
$cats[] = $cat->cat_name;
}
}
sort( $cats );
$pathname = array_merge( $pathname, $cats );
break;
case "by_tags":
global $post;
$posttags = get_the_tags($post->ID);
$tags = array();
if ( $posttags ) {
foreach( $posttags as $tag ) {
$tags[] = $tag->name;
}
}
sort( $tags );
$pathname = array_merge( $pathname, $tags );
break;
default:
$pathname = array_merge( $pathname, explode( ',', $path ) );
break;
}
if ( $slugify == "1" || $slugify == "yes" ) {
$post = get_post( get_the_ID() );
$pathname[] = $post->post_name;
}
$pathname = array_merge( $pathname, explode( ',', $path_post ) );
// Remove empty strings from the Array
return adgear_ad_internal( $format, array_filter( $pathname ) );
} else if ( adgear_is_dynamic_site() && $format ) {
return adgear_ad_internal( $format, array() );
} else {
return "<p style='".adgear_warning_css_properties()."'><strong>WARNING</strong>: AdGear Ad Manager did not understand the embed code. This would be because you used a dynamic embed code on a dynamic site, or the reverse.</p>";
}
}
add_shortcode('adgear_ad', 'adgear_ad');
function adgear_adspot_selector_ui($args) {
extract($args);
?>
<select class="adgear_adspot_selector" id="<?php echo $id; ?>" name="<?php echo $name; ?>" >
<?php
foreach( adgear_ad_spots() as $adspot ) {
?>
<option <?php if ( $selected == $adspot["id"] ) { echo "selected"; } ?> value="<?php echo $adspot["id"] ?>"><?php echo $adspot["name"]; ?><?php if ( $adspot["width"] ) { ?> (<?php echo $adspot["width"]; ?>×<?php echo $adspot["height"]; ?>)<?php } ?></option>
<?php
}
?>
</select>
<?php
}
function adgear_single_selector_ui($args) {
extract($args);
?>
<select class="adgear_single_selector" id="<?php echo $id; ?>" name="<?php echo $name; ?>">
<option <?php if ( $selected == 'all' ) { echo "selected"; } ?> value="all">On all pages</option>
<option <?php if ( $selected == 'front' ) { echo "selected"; } ?> value="front">On the front page</option>
<option <?php if ( $selected == 'home' ) { echo "selected"; } ?> value="home">On the home page</option>
<option <?php if ( $selected == 'yes' ) { echo "selected"; } ?> value="yes">On pages with a single post</option>
<option <?php if ( $selected == 'no' ) { echo "selected"; } ?> value="no">On archive pages</option>
</select>
<?php
}
function adgear_slugify_selector_ui($args) {
extract($args);
?>
<select class="adgear_slugify_selector" id="<?php echo $id; ?>" name="<?php echo $name; ?>">
<option <?php if ($selected == "yes") { echo "selected"; } ?> value="yes">Yes</option>
<option <?php if ($selected == "no") { echo "selected"; } ?> value="no">No</option>
</select>
<?php
}
function adgear_path_type_selector_ui($args) {
extract($args);
?>
<select class="adgear_path_type_selector" name="<?php echo $name; ?>" id="<?php echo $id; ?>">
<option <?php if ($selected == "categories") { echo "selected"; } ?> value="categories">Using the post's categories</option>
<option <?php if ($selected == "tags") { echo "selected"; } ?> value="tags">Using the post's tags</option>
<option <?php if ($selected == "path") { echo "selected"; } ?> value="path">Using a static path:</option>
</select>
<br/>
<input class="adgear_path" name="<?php echo $path_name; ?>" id="<?php echo $path_id; ?>" type="text" size="40" style="width:95%<?php if ( $selected != "path" ) { echo ';display:none'; } ?>" value="<?php echo $path_selected; ?>"/>
<?php
}
function adgear_toggle_advanced_ui($args) {
echo '<input type="checkbox" class="adgear_toggle_advanced"';
if ( true == $args[ 'selected' ] ) {
echo ' checked';
}
echo '/>';
}
function adgear_example_ui($args) {
if ( $args[ 'selected' ] ) {
$style = "";
} else {
$style = "display:none";
}
echo '<div style="'.$style.'" class="adgear_example">Write manual paths (before, middle, after and manual path) using no quotes and separate segments using commas, like this: <tt>advanced, tutorial</tt>.</div>';
}
function adgear_path_pre_ui($args) {
extract($args);
?>
<input class="adgear_path_pre" type="text" name="<?php echo $name; ?>" id="<?php echo $id; ?>" value="<?php echo $value; ?>"/>
<?php
}
function adgear_path_middle_ui($args) {
extract($args);
?>
<input class="adgear_path_middle" type="text" name="<?php echo $name; ?>" id="<?php echo $id; ?>" value="<?php echo $value; ?>"/>
<?php
}
function adgear_path_post_ui($args) {
extract($args);
?>
<input class="adgear_path_post" type="text" name="<?php echo $name; ?>" id="<?php echo $id; ?>" value="<?php echo $value; ?>"/>
<?php
}
function adgear_format_selector_ui($args) {
extract($args);
?>
<select class="adgear_format_selector" name="<?php echo $name; ?>" id="<?php echo $id; ?>">
<?php if ( $include_blank ) { ?>
<option value="">Choose an Ad format…</option>
<?php
}
foreach( adgear_formats() as $format ) {
?>
<option <?php if ( $selected == $format['id'] ) { echo "selected"; } ?> value="<?php echo $format["id"]; ?>"><?php echo $format["name"]; ?><?php if ( $format["width"] ) { ?> (<?php echo $format["width"]; ?>×<?php echo $format["height"]; ?>)<?php } ?></option>
<?php
}
?>
</select>
<?php
}
/* Sidebar Widget */
class AdGearAdWidget extends WP_Widget {
function AdGearAdWidget() {
$widget_ops = array(
'classname' => 'adgear_ad',
'description' => 'Display your AdGear Ads in your sidebars' );
$this->WP_Widget('adgear_ad', 'AdGear Ad', $widget_ops);
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
if ( adgear_is_dynamic_site() ) {
switch( $instance['path_type'] ) {
case "categories":
case "tags":
$path_value = "by_".$instance[ 'path_type' ];
break;
default:
$path_value = $instance[ 'path' ];
}
$embed_code = adgear_ad( array(
"format" => $instance[ 'format_id' ],
"path" => $path_value,
"path_pre" => $instance[ 'path_pre' ],
"path_middle" => $instance[ 'path_middle' ],
"path_post" => $instance[ 'path_post' ],
"slugify" => $instance[ 'slugify' ],
"single" => $instance[ 'single' ] ) );
} else {
$embed_code = adgear_ad( array(
"id" => $instance[ 'adspot_id' ],
"single" => $instance[ 'single' ] ) );
}
if ( $embed_code <> "" ) {
echo $before_widget;
echo $embed_code;
echo $after_widget;
}
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
if ( adgear_is_dynamic_site() ) {
$keys = array( 'format_id', 'path_type', 'path_pre', 'path', 'path_middle', 'slugify', 'path_post', 'single' );
} else {
$keys = array( 'adspot_id', 'single' );
}
foreach( $keys as $key ) {
$instance[$key] = strip_tags( $new_instance[$key] );
}
return $instance;
}
function form($instance) {
if ( adgear_is_dynamic_site() ) {
$options = array( 'format_id' => '', 'path_type' => 'categories', 'path' => '', 'slugify' => 'yes', 'single' => 'all' );
} else {
$options = array( 'adspot_id' => '', 'single' => 'all' );
}
$instance = wp_parse_args( (array) $instance, $options );
$single = strip_tags($instance['single']);
echo '<div class="adgear-meta" style="margin:0;padding:0">';
if ( adgear_is_dynamic_site() ) {
$format_id = strip_tags($instance['format_id']);
$path_type = strip_tags($instance['path_type']);
$path = strip_tags($instance['path']);
$slugify = strip_tags($instance['slugify']);
/* Backwards compatibility: don't show ugly error messages when the keys don't exist */
if ( array_key_exists( 'path_pre', $instance ) ) {
$path_pre = strip_tags($instance['path_pre']);
$path_middle = strip_tags($instance['path_middle']);
$path_post = strip_tags($instance['path_post']);
} else {
$path_pre = $path_middle = $path_post = "";
}
if ( $path_pre == "" && $path_middle == "" && $path_post == "" ) {
$show_advanced = false;
} else {
$show_advanced = true;
}
?>
<p>
<label for="<?php echo $this->get_field_id('format_id'); ?>"><?php _e('Ad Format:'); ?></label>
<?php adgear_format_selector_ui( array( 'id' => $this->get_field_id('format_id'), 'name' => $this->get_field_name('format_id'), 'selected' => $format_id, 'include_blank' => true )); ?>
</p>
<p class="adgear_advanced" style="<?php if (!$show_advanced) { echo "display:none"; } ?>">
<label for="<?php echo $this->get_field_id('path_pre'); ?>"><?php _e('Path before:'); ?></label>
<?php adgear_path_pre_ui( array( 'id' => $this->get_field_id('path_pre'), 'name' => $this->get_field_name('path_pre'), 'value' => $path_pre ) ); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('path_type'); ?>"><?php _e('Path type:'); ?></label>
<?php adgear_path_type_selector_ui( array( 'id' => $this->get_field_id('path_type'), 'name' => $this->get_field_name('path_type'), 'selected' => $path_type, 'path_id' => $this->get_field_id('path'), 'path_name' => $this->get_field_name('path'), 'path_selected' => $path )); ?>
</p>
<p class="adgear_advanced" style="<?php if (!$show_advanced) { echo "display:none"; } ?>">
<label for="<?php echo $this->get_field_id('path_middle'); ?>"><?php _e('Path middle:'); ?></label>
<?php adgear_path_middle_ui( array( 'id' => $this->get_field_id('path_middle'), 'name' => $this->get_field_name('path_middle'), 'value' => $path_middle ) ); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('slugify'); ?>"><?php _e('Use post\'s slug in path:'); ?></label>
<?php adgear_slugify_selector_ui( array( 'id' => $this->get_field_id('slugify'), 'name' => $this->get_field_name('slugify'), 'selected' => $slugify )); ?>
</p>
<p class="adgear_advanced" style="<?php if (!$show_advanced) { echo "display:none"; } ?>">
<label for="<?php echo $this->get_field_id('path_post'); ?>"><?php _e('Path after:'); ?></label>
<?php adgear_path_post_ui( array( 'id' => $this->get_field_id('path_post'), 'name' => $this->get_field_name('path_post'), 'value' => $path_post ) ); ?>
</p>
<p>
<label><?php _e('Show advanced path options'); ?> <?php adgear_toggle_advanced_ui( array( 'selected' => $show_advanced ) ); ?></label>
<?php adgear_example_ui( array( 'selected' => $show_advanced ) ); ?>
</p>
<?php
} else {
$adspot_id = strip_tags($instance['adspot_id']);
?>
<p>
<label for="<?php echo $this->get_field_id('adspot_id'); ?>"><?php _e('Ad spot:'); ?></label>
<?php adgear_adspot_selector_ui( array( 'id' => $this->get_field_id('adspot_id'), 'name' => $this->get_field_name('adspot_id'), 'selected' => $adspot_id )); ?>
</p>
<?php
}
?>
<p>
<label for="<?php $this->get_field_id('single'); ?>"><?php _e('When to show this ad:'); ?></label>
<?php adgear_single_selector_ui( array( 'id' => $this->get_field_id('single'), 'name' => $this->get_field_name('single'), 'selected' => $single)) ?>
</p>
<div style="display:none;margin:0;padding:0">
<input type="hidden" value="<?php echo adgear_is_dynamic_site(); ?>" id="adgear_site_is_dynamic"/>
</div>
</div>
<?php
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("AdGearAdWidget");') );
?>