Ad Injection - Version 0.8.3

Version Description

  • First public release

=

Download this release

Release Info

Developer reviewmylife
Plugin Icon wp plugin Ad Injection
Version 0.8.3
Comparing to
See all releases

Version 0.8.3

ad-injection-admin.php ADDED
@@ -0,0 +1,859 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Part of the Ad Injection plugin for WordPress
4
+ http://www.reviewmylife.co.uk/blog/
5
+ */
6
+
7
+ if (!is_admin()) return;
8
+
9
+ if (is_admin() && !function_exists('check_admin_referer')){
10
+ //TODO check_admin_referer doesn't exist on post. Why?
11
+ require_once( ABSPATH . WPINC . '/pluggable.php' );
12
+ }
13
+
14
+ function adinj_checkNonce(){
15
+ if (empty($_POST) || !check_admin_referer('_adinj_form', '_adinj_nonce')){
16
+ echo 'form error';
17
+ exit();
18
+ }
19
+ }
20
+
21
+ // TODO investigate register_settings for a future release
22
+ if (isset($_POST['adinj_action'])){
23
+ switch($_POST['adinj_action']){
24
+ case 'Save all settings':
25
+ adinj_checkNonce();
26
+
27
+ // Extract all know options
28
+ $default_options = adinj_default_options();
29
+ foreach ($default_options as $key => $value){
30
+ if (isset($_POST[$key])){
31
+ $options[$key] = $_POST[$key];
32
+ } else {
33
+ $options[$key] = "";
34
+ }
35
+ }
36
+
37
+ if (!file_exists(ADINJ_AD_PATH)){
38
+ mkdir(ADINJ_AD_PATH, 0750) //TODO is this the right permission
39
+ or print("Error: could not create dir: ADINJ_AD_PATH. Please create it manually and try again.");
40
+
41
+ }
42
+
43
+ $raw_ad_code_random = stripslashes($_POST['ad_code_random_1']);
44
+ write_ad_to_file($raw_ad_code_random, ADINJ_AD_PATH.'/'.ADINJ_AD_RANDOM_FILE);
45
+
46
+ $raw_ad_code_top = stripslashes($_POST['ad_code_top_1']);
47
+ write_ad_to_file($raw_ad_code_top, ADINJ_AD_PATH.'/'.ADINJ_AD_TOP_FILE);
48
+
49
+ $raw_ad_code_bottom = stripslashes($_POST['ad_code_bottom_1']);
50
+ write_ad_to_file($raw_ad_code_bottom, ADINJ_AD_PATH.'/'.ADINJ_AD_BOTTOM_FILE);
51
+
52
+ $ad_referrers = stripslashes($_POST['ad_referrers']); // TODO do i need strip slashes?
53
+ $options['ad_referrers'] = $ad_referrers;
54
+
55
+ $blocked_ips = stripslashes($_POST['blocked_ips']);
56
+ $options['blocked_ips'] = $blocked_ips;
57
+
58
+ update_option('adinj_options', $options);
59
+
60
+ adinj_write_config_file();
61
+
62
+ break;
63
+
64
+ case 'Reset to Default':
65
+ adinj_checkNonce();
66
+ update_option('adinj_options', adinj_default_options());
67
+ break;
68
+
69
+ case 'Delete settings from DB':
70
+ adinj_checkNonce();
71
+ delete_option('adinj_options');
72
+ break;
73
+ }
74
+ }
75
+
76
+ // Test reminder: These generated functions won't exist until the first save
77
+ // so be careful if adding any new ones - they might not exist yet, but could
78
+ // still be referenced by adshow.
79
+ function adinj_write_config_file(){
80
+ $handle = fopen(ADINJ_CONFIG_FILE, "w") or die("could not open config file: $ad_path");
81
+
82
+ $referrer_list = adinj_quote_list('ad_referrers');
83
+ $ip_list = adinj_quote_list('blocked_ips');
84
+ $sevisitors_only = adinj_ticked('sevisitors_only')?'true':'false';
85
+ $debug_mode = adinj_ticked('debug_mode')?'true':'false';
86
+
87
+ $rnd_func = adinj_add_tags(NULL, 'rnd_', 'adinj_config_add_tags_rnd');
88
+ $top_func = adinj_add_tags(NULL, 'top_', 'adinj_config_add_tags_top');
89
+ $bottom_func = adinj_add_tags(NULL, 'bottom_', 'adinj_config_add_tags_bottom');
90
+
91
+ $config = <<<CONFIG
92
+ <?php
93
+ /*
94
+ Ad Injection config file
95
+ DO NOT EDIT MANUALLY
96
+ This file is generated by Ad Injection
97
+ */
98
+
99
+ function adinj_config_sevisitors_only() { return $sevisitors_only; }
100
+ function adinj_config_search_engine_referrers() { return array($referrer_list); }
101
+ function adinj_config_blocked_ips() { return array($ip_list); }
102
+ function adinj_config_debug_mode() { return $debug_mode; }
103
+ $rnd_func
104
+ $top_func
105
+ $bottom_func
106
+
107
+ ?>
108
+ CONFIG;
109
+
110
+ fwrite($handle, $config) or die("could not write to config file: $config_file");
111
+ fclose($handle);
112
+ chmod(ADINJ_CONFIG_FILE, 0640) or die("chmod on config file failed: $config_file");
113
+
114
+ }
115
+
116
+ function adinj_get_logo(){
117
+ return '<a href="http://www.reviewmylife.co.uk/blog/" target="_new"><img src="'. WP_PLUGIN_URL . '/ad-injection/rml-micro-logo.png" width="16" height="16" border="0" alt="reviewmylife" /></a>';
118
+ }
119
+
120
+ function adinj_options_page(){
121
+ $options = adinj_options();
122
+ if ($options === false || adinj_options_need_upgrading($options)){
123
+ // Upgraded via FTP without being deactivated/reactivated
124
+ adinj_activate_hook();
125
+ $options = adinj_options(1);
126
+ }
127
+
128
+ echo '<div class="wrap">';
129
+
130
+ if (adinj_problem_with_wpminify_check()){
131
+ echo '<div id="ad-injection-warning" class="error"><p><strong>';
132
+ echo adinj_get_problem_with_wpminify_message();
133
+ echo '</strong></p></div>';
134
+ }
135
+
136
+ echo '<div id="icon-options-general" class="icon32"></div><h2>Ad Injection ' . adinj_get_version() . adinj_get_logo() . '</h2>';
137
+
138
+ ?>
139
+
140
+ <div class="postbox-container" style="float:right; width:258px;">
141
+ <div class="metabox-holder">
142
+ <div class="meta-box-sortables">
143
+ <div id="toc" class="postbox">
144
+ <h3 class="hndle"><span><?php echo adinj_get_logo(); ?> More info...</span></h3>
145
+ <div class="inside" style="margin:5px;">
146
+ <h4>More Ad Injection information</h4>
147
+ <ul>
148
+ <li><a href="http://www.reviewmylife.co.uk/blog/2010/12/06/ad-injection-plugin-wordpress/" target="_new">Ad Injection Home</a></li>
149
+ <li><a href="https://spreadsheets.google.com/viewform?formkey=dFUwZzBYcG1HNzNKMmJZdWFDdFhkY0E6MQ" target="_new">Report a bug or give feedback!</a></li>
150
+ </ul>
151
+ <h4>More by this author</h4>
152
+ <ul>
153
+ <li><a href="http://www.reviewmylife.co.uk/blog/" target="_new">www.reviewmylife.co.uk</a></li>
154
+ <li><a href="http://www.advancedhtml.co.uk/" target="_new">www.advancedhtml.co.uk</a></li>
155
+ </ul>
156
+
157
+ <h4><font color="red">Be careful!</font></h4>
158
+ <p>Make sure that the ad settings and positioning you define are in compliance with your ad provider's terms of service!</p>
159
+ </div>
160
+ </div>
161
+ </div>
162
+ </div>
163
+ </div>
164
+
165
+ <p><a href="#random">Random ads</a> | <a href="#topad">Top ad</a> | <a href="#bottomad">Bottom ad</a> | <a href="#restrictions">Ad insertion mode/dynamic restrictions</a> | <a href="#debugging">Debugging</a> | <a href="#docs">Quick Start</a> | <a href="#testads">Test ads</a></p>
166
+
167
+ <form name="adinjform" method="post" action="">
168
+ <?php wp_nonce_field('_adinj_form', '_adinj_nonce'); ?>
169
+
170
+ <p><input type="checkbox" name="ads_enabled" <?php echo adinj_ticked('ads_enabled'); ?> /><b><?php _e('Ads enabled', 'adinj') ?></b> - Tick this to turn your ads on!<br />
171
+ <?php _e("Only show ads on pages older than ", 'adinj') ?>
172
+
173
+ <select name='ads_on_page_older_than'>
174
+ <?php
175
+ $older_than_days = array(0, 1, 2, 3, 5, 7, 10, 14, 21, 28, 40, 50);
176
+ for ($value=0; $value<sizeof($older_than_days); ++$value){
177
+ echo "<option value=\"$older_than_days[$value]\" ";
178
+ if($options['ads_on_page_older_than'] == $older_than_days[$value]) echo 'selected="selected"';
179
+ echo ">$older_than_days[$value]</option>";
180
+ }
181
+ ?>
182
+ </select><?php _e(" (days)", 'adinj') ?></p>
183
+
184
+
185
+ <?php adinj_postbox_start(__("Randomly Injected ad code", 'adinj'), 'random'); ?>
186
+
187
+ <table border="0" cellspacing="5">
188
+ <tr><td style="vertical-align: top">
189
+ <textarea name="ad_code_random_1" rows="10" cols="60"><?php echo read_ad_from_file(ADINJ_AD_PATH.'/'.ADINJ_AD_RANDOM_FILE); ?></textarea>
190
+ </td><td style="vertical-align: top">
191
+ <?php adinj_add_alignment_options('rnd_'); ?>
192
+ </td></tr>
193
+ </table>
194
+
195
+
196
+ <p><b>Docs:</b> On individual posts or pages this advert is inserted between randomly selected paragraphs. On a multi-post page (e.g. home page), one ad is inserted into each post. Try a <a href="#468x60">468x60</a> or <a href="#728x90">728x90</a> banner.</p>
197
+ <p>Be especially careful if you decide to use the 'float' layout options. Make sure that you don't have adverts floated over the top of other page elements, or vice-versa.</p>
198
+ </div>
199
+
200
+ <input type="submit" style="float:right" name="adinj_action" value="<?php _e('Save all settings', 'adinj') ?>" />
201
+ <h3><?php _e("Single posts and pages: Randomly Injected ad settings", 'adinj') ?></h3>
202
+ <div class="inside" style="margin:10px">
203
+
204
+ <table border="0">
205
+
206
+ <tr><td>
207
+ <?php _e("Always put the first ad after the first paragraph.", 'adinj') ?>
208
+ </td><td>
209
+ <input type="checkbox" name="first_paragraph_ad" <?php echo adinj_ticked('first_paragraph_ad'); ?> />
210
+ </td></tr>
211
+
212
+ <tr><td>
213
+ <?php _e("Allow multiple ads to be injected at the same positions.", 'adinj') ?>
214
+ </td><td>
215
+ <input type="checkbox" name="multiple_ads_at_same_position" <?php echo adinj_ticked('multiple_ads_at_same_position'); ?> /> (The default behaviour is to inject ads at unique positions)
216
+ </td></tr>
217
+
218
+ <tr><td>
219
+ <p>Don't show ads on these page types: </p>
220
+ </td><td>
221
+ <input type="checkbox" name="exclude_page" <?php echo adinj_ticked('exclude_page'); ?> />page (<?php echo wp_count_posts('page', 'readable')->publish; ?> page(s))<br />
222
+ <input type="checkbox" name="exclude_single" <?php echo adinj_ticked('exclude_single'); ?> />single (<?php echo wp_count_posts('post', 'readable')->publish; ?> individual blog post(s))<br />
223
+ </td></tr>
224
+
225
+ <tr><td>
226
+ <?php _e("Maximum number of randomly injected ads: ", 'adinj') ?></td><td>
227
+ <select name='max_num_of_ads'>
228
+ <?php
229
+ for ($value=0; $value<=6; ++$value){
230
+ echo "<option value=\"$value\" ";
231
+ if($options['max_num_of_ads'] == $value) echo 'selected="selected"';
232
+ echo ">$value</option>";
233
+ }
234
+ ?>
235
+ </select> <?php echo adinj_getdefault('max_num_of_ads'); ?><br />
236
+ </td></tr>
237
+
238
+ <tr><td>
239
+ <?php
240
+ _e("No random ads if page shorter than: ", 'adinj');
241
+ echo '</td><td>';
242
+ adinj_selection_box("no_random_ads_if_shorter_than",
243
+ array(ADINJ_RULE_DISABLED, 100, 200, 300, 500, 1000, 1500, 2000, 2500, 3000));
244
+ echo adinj_getdefault('no_random_ads_if_shorter_than');
245
+ ?>
246
+ </td></tr>
247
+
248
+ <tr><td>
249
+ <?php
250
+ _e("Limit of 1 ad if page shorter than: ", 'adinj');
251
+ echo '</td><td>';
252
+ adinj_selection_box("one_ad_if_shorter_than",
253
+ array(ADINJ_RULE_DISABLED, 100, 200, 300, 500, 1000, 1500, 2000, 2500, 3000));
254
+ echo adinj_getdefault('one_ad_if_shorter_than');
255
+ ?>
256
+ </td></tr>
257
+
258
+ <tr><td>
259
+ <?php
260
+ _e("Limit of 2 ads if page shorter than: ", 'adinj');
261
+ echo '</td><td>';
262
+ adinj_selection_box("two_ads_if_shorter_than",
263
+ array(ADINJ_RULE_DISABLED, 100, 200, 300, 500, 1000, 1500, 2000, 2500, 3000, 5000, 10000));
264
+ echo adinj_getdefault('two_ads_if_shorter_than');
265
+ ?>
266
+ </td></tr>
267
+
268
+ <tr><td>
269
+ <?php
270
+ _e("Limit of 3 ads if page shorter than: ", 'adinj');
271
+ echo '</td><td>';
272
+ adinj_selection_box("three_ads_if_shorter_than",
273
+ array(ADINJ_RULE_DISABLED, 100, 200, 300, 500, 1000, 1500, 2000, 2500, 3000, 5000, 10000, 20000));
274
+ echo adinj_getdefault('three_ads_if_shorter_than');
275
+ ?>
276
+ </td></tr>
277
+ </table>
278
+
279
+ <br clear="all" />
280
+ <p><b>Docs:</b> The above directives are processed in order from top to bottom.</p>
281
+
282
+
283
+ <p></p>
284
+
285
+ </div>
286
+
287
+ <input type="submit" style="float:right" name="adinj_action" value="<?php _e('Save all settings', 'adinj') ?>" />
288
+ <h3><?php _e("Home page: Randomly Injected ad settings", 'adinj') ?> (<?php echo get_bloginfo('url'); ?>)</h3>
289
+ <div class="inside" style="margin:10px">
290
+
291
+
292
+ <?php _e("Maximum number of injected ads: ", 'adinj') ?>
293
+ <select name='max_num_of_ads_home_page'>
294
+ <?php
295
+ for ($value=0; $value<=6; ++$value){
296
+ echo "<option value=\"$value\" ";
297
+ if($options['max_num_of_ads_home_page'] == $value) echo 'selected="selected"';
298
+ echo ">$value</option>";
299
+ }
300
+ ?>
301
+ </select> <?php echo adinj_getdefault('max_num_of_ads_home_page'); ?><br />
302
+
303
+ <p><b>Docs:</b> On multi-post pages such as the home page, one randomly positioned advert will be inserted into each page, up to the maximum number specified here.</p>
304
+
305
+ <?php adinj_postbox_end(); ?>
306
+
307
+
308
+
309
+ <?php adinj_postbox_start(__("Optional top advert (single posts and pages only)", 'adinj'), 'topad'); ?>
310
+
311
+ <?php
312
+ _e("Only show top ad on pages longer than: ", 'adinj');
313
+ adinj_selection_box("top_ad_if_longer_than",
314
+ array(ADINJ_RULE_DISABLED, 100, 200, 300, 500, 1000, 1500, 2000, 2500, 3000, 5000, 10000, 20000, ADINJ_ALWAYS_SHOW));
315
+
316
+ ?>
317
+
318
+ <br clear="all" />
319
+
320
+ <table border="0">
321
+ <tr><td>
322
+ <textarea name="ad_code_top_1" rows="10" cols="60"><?php echo read_ad_from_file(ADINJ_AD_PATH.'/'.ADINJ_AD_TOP_FILE); ?></textarea>
323
+ </td><td style="vertical-align: top">
324
+ <?php adinj_add_alignment_options('top_'); ?>
325
+ </td></tr>
326
+ </table>
327
+
328
+ <p><font color="red">Important: Make sure that the total number of ads does not exceed the amount allowed in your ad provider's TOS!</font> The top ad is in addition to the quantity of random ads selected.</p>
329
+
330
+ <p><b>Docs:</b> The top ad will only appear on single posts and pages. It will not appear on multi-post pages. Try a <a href="#468x15">468x15</a> or <a href="#336x280">336x280</a> advert.</p>
331
+
332
+ <?php adinj_postbox_end(); ?>
333
+
334
+
335
+ <?php adinj_postbox_start(__("Optional bottom advert (single posts and pages only)", 'adinj'), 'bottomad'); ?>
336
+
337
+ <?php
338
+ _e("Only show bottom ad on pages longer than: ", 'adinj');
339
+ adinj_selection_box("bottom_ad_if_longer_than",
340
+ array(ADINJ_RULE_DISABLED, 100, 200, 300, 500, 1000, 1500, 2000, 2500, 3000, 5000, 10000, 20000, ADINJ_ALWAYS_SHOW));
341
+ ?>
342
+
343
+ <br clear="all"/>
344
+
345
+ <table border="0">
346
+ <tr><td>
347
+ <textarea name="ad_code_bottom_1" rows="10" cols="60"><?php echo read_ad_from_file(ADINJ_AD_PATH.'/'.ADINJ_AD_BOTTOM_FILE); ?></textarea>
348
+ </td><td style="vertical-align: top">
349
+ <?php adinj_add_alignment_options('bottom_'); ?>
350
+ </td></tr>
351
+ </table>
352
+
353
+ <p><font color="red">Important: Make sure that the total number of ads does not exceed the amount allowed in your ad provider's TOS!</font> The top ad is in addition to the quantity of random ads selected.</p>
354
+
355
+ <p><b>Docs:</b> The bottom ad will only appear on single posts and pages. It will not appear on multi-post pages. Try a <a href="#336x280">336x280</a> advert.</p>
356
+
357
+
358
+ <?php adinj_postbox_end(); ?>
359
+
360
+
361
+ <?php adinj_postbox_start(__("Ad insertion mode and dynamic ad display restrictions", 'adinj'), 'restrictions'); ?>
362
+
363
+ <h4>Ad insertion mode</h4>
364
+
365
+ <blockquote>
366
+ <p><input type="radio" name="ad_insertion_mode" value="mfunc" <?php if ($options['ad_insertion_mode']=='mfunc') echo 'checked="checked"'; ?> /> <b>Use mfunc tags for dynamic features</b> - Dynamic features will work with WP Super Cache in legacy mode (or with a caching program that is compatible with WP Super Cache's mfunc tags). Dynamic features will also work if you don't use a caching program, although if you don't use a caching program 'direct' insertion will be more efficient.</p>
367
+ <p><input type="radio" name="ad_insertion_mode" value="direct_dynamic" <?php if ($options['ad_insertion_mode']=='direct_dynamic') echo 'checked="checked"'; ?> /> <b>Direct ad insertion with dynamic features</b> - Dynamic features will work if no caching is used. Only select this if you are not using any caching plugin.</p>
368
+ <p><input type="radio" name="ad_insertion_mode" value="direct_static" <?php if ($options['ad_insertion_mode']=='direct_static') echo 'checked="checked"'; ?> /> <b>Direct static ad insertion</b> - No dynamic feature support. Select this if you are using a caching plugin which is not compatible with WP Super Cache's mfunc tags.</p>
369
+ </blockquote>
370
+ </div>
371
+
372
+
373
+ <div class="inside" style="margin:10px">
374
+ <h4>Show ads only to search engine visitors (dynamic feature)</h4>
375
+
376
+ <blockquote>
377
+ <input type="checkbox" name="sevisitors_only" <?php echo adinj_ticked('sevisitors_only'); ?> /><?php _e("Only show ads to search engine visitors (customise search engine referrers below if necessary).", 'adinj') ?><br />
378
+ <textarea name="ad_referrers" rows="2" cols="80"><?php echo $options['ad_referrers']; ?></textarea>
379
+ <p>Comma separated list e.g.: <br /><code>.google., .bing., .yahoo., .ask., search?, search., /search/</code></p>
380
+ </blockquote>
381
+
382
+ <h4>Blocked IP addresses (dynamic feature)</h4>
383
+
384
+ <blockquote>
385
+ <textarea name="blocked_ips" rows="4" cols="80"><?php echo $options['blocked_ips']; ?></textarea>
386
+ <p>Comma separated list e.g.: <br /><code>0.0.0.1, 0.0.0.2</code></p>
387
+ <p>Or you can list one IP per line with optional comments e.g.</p>
388
+ <code style="padding:0px 0px">192.168.0.1<br />0.0.0.2<br /><?php echo $_SERVER['REMOTE_ADDR'] ?> //my ip<br />0.0.0.3</code>
389
+
390
+ <p>For reference your current IP address is <code><?php echo $_SERVER['REMOTE_ADDR'] ?></code></p>
391
+ </blockquote>
392
+
393
+ </div>
394
+
395
+ <h3>Recommended WP Super Cache settings:</h3>
396
+ <div class="inside" style="margin:10px">
397
+
398
+ <blockquote>
399
+ <ul>
400
+ <li>Cache hits to this website for quick access.</li>
401
+ <li>Legacy page caching.</li>
402
+ <li>Compress pages so they&#x2019;re served more quickly to visitors.</li>
403
+ <li>Expire time: 36000 (10 hours). Or 3600 (1 hour) for very busy site.</li>
404
+ </ul>
405
+ </blockquote>
406
+
407
+ <p>
408
+ <?php if (is_plugin_active('wp-super-cache/wp-cache.php')) {
409
+ echo "Go to the WP Super Cache <a href='options-general.php?page=wpsupercache&amp;tab=settings'>advanced settings page</a> (where you can set the caching mode to Legacy).";
410
+ } else {
411
+ echo "Note: WP Super Cache does not appear to be active.";
412
+ } ?>
413
+ </p>
414
+
415
+ <?php adinj_postbox_end(); ?>
416
+
417
+
418
+ <?php adinj_postbox_start(__("Debugging", 'adinj'), 'debugging'); ?>
419
+
420
+ <input type="checkbox" name="debug_mode" <?php echo adinj_ticked('debug_mode'); ?> />Enable debug mode
421
+
422
+ <p>If you are not sure why ads aren't appearing, or why they are appearing, enable debug mode and look at the debug information (search for 'ADINJ DEBUG') in the HTML of your content pages.</p>
423
+
424
+ <?php
425
+
426
+ if (adinj_problem_with_wpminify_check()){
427
+ echo adinj_get_problem_with_wpminify_message();
428
+ }
429
+
430
+ adinj_debug_information();
431
+ ?>
432
+
433
+ <p></p>
434
+
435
+ <p>If you want to restore all settings (excluding the ad contents) to their default values use this button.</p>
436
+
437
+ <input type="submit" name="adinj_action" value="<?php _e('Reset to Default', 'adinj') ?>" />
438
+
439
+ <p>You can delete the database settings if you are going to uninstall Ad Injection.</p>
440
+
441
+ <input type="submit" name="adinj_action" value="<?php _e('Delete settings from DB', 'adinj') ?>" />
442
+
443
+ <?php adinj_postbox_end(); ?>
444
+
445
+
446
+ <br clear="all" />
447
+
448
+ <?php
449
+
450
+ adinj_docs();
451
+
452
+ echo '</form>';
453
+ echo '</div> <!--wrap-->';
454
+ }
455
+
456
+ function adinj_postbox_start($title, $anchor, $width='650px'){
457
+ $options = adinj_options();
458
+ ?>
459
+ <div class='postbox-container' style='width:<?php echo $width; ?>;clear:left'>
460
+ <div class="metabox-holder">
461
+ <div class="postbox">
462
+ <input type="submit" style="float:right" class="button-primary" name="adinj_action" value="<?php _e('Save all settings', 'adinj') ?>" />
463
+ <div class="<?php echo $anchor; ?>-button"></div>
464
+ <script type="text/javascript">
465
+ jQuery(document).ready(function(){
466
+ if (jQuery('#ui_<?php echo $anchor; ?>_hide').val() == 'true'){
467
+ jQuery('.<?php echo $anchor; ?>-box').hide();
468
+ }
469
+ jQuery('.<?php echo $anchor; ?>-button').show().before('<a href="#" style="float:right" id="toggle-<?php echo $anchor; ?>" class="button">Show/Hide</a>');
470
+ jQuery('a#toggle-<?php echo $anchor; ?>').click(function() {
471
+ jQuery('#ui_<?php echo $anchor; ?>_hide').val(!jQuery('.<?php echo $anchor; ?>-box').is(":hidden"));
472
+ jQuery('.<?php echo $anchor; ?>-box').slideToggle(1000);
473
+ return false;
474
+ });
475
+ });
476
+ </script>
477
+ <input type='hidden' id='ui_<?php echo $anchor; ?>_hide' name='ui_<?php echo $anchor; ?>_hide' value='<?php echo $options['ui_'.$anchor.'_hide']; ?>' />
478
+ <h3><a name="<?php echo $anchor ?>"></a><?php echo adinj_get_logo() . ' ' . $title; ?></h3>
479
+ <div class="<?php echo $anchor; ?>-box">
480
+ <div class="inside" style="margin:10px">
481
+ <?php
482
+ }
483
+
484
+ function adinj_postbox_end(){
485
+ echo '
486
+ </div> <!--inside-->
487
+ </div> <!--anchor-box-->
488
+ </div> <!--postbox-->
489
+ </div> <!--metabox-holder-->
490
+ </div> <!--postbox-container-->';
491
+ }
492
+
493
+ function adinj_selection_box($name, $values, $type=NULL){
494
+ echo "<select name='$name'>";
495
+ $options = adinj_options();
496
+ $selected_value = $options[$name];
497
+ foreach ($values as $value){
498
+ echo "<option value=\"$value\" ";
499
+ if($selected_value == "$value") echo 'selected="selected"';
500
+ if (empty($type) && is_numeric($value)){
501
+ $typetxt = "(chars)";
502
+ } else {
503
+ $typetxt = $type;
504
+ }
505
+ if ($value === ADINJ_RULE_DISABLED) $typetxt = "";
506
+ echo ">$value $typetxt</option>";
507
+ }
508
+ echo "</select>";
509
+ }
510
+
511
+ function adinj_add_alignment_options($prefix){
512
+ _e("Alignment", 'adinj');
513
+ echo "<br />";
514
+ adinj_selection_box($prefix.'align',
515
+ array(ADINJ_RULE_DISABLED, 'left', 'center', 'right', 'float left', 'float right'));
516
+
517
+ echo "<br />";
518
+
519
+ _e("Margin top", 'adinj');
520
+ echo "<br />";
521
+ adinj_selection_box($prefix.'margin_top',
522
+ array(ADINJ_RULE_DISABLED, 0, 1, 2, 3, 4, 5, 7, 10, 15, 20, 30), "(px)");
523
+
524
+ echo "<br />";
525
+
526
+ _e("Margin bottom", 'adinj');
527
+ echo "<br />";
528
+ adinj_selection_box($prefix.'margin_bottom',
529
+ array(ADINJ_RULE_DISABLED, 0, 1, 2, 3, 4, 5, 7, 10, 15, 20, 30), "(px)");
530
+ }
531
+
532
+ function adinj_debug_information(){
533
+ $stored_options = adinj_options();
534
+ $default_options = adinj_default_options();
535
+ ?>
536
+ <h4>Settings dump from database (all in 'adinj_options' option)</h4><blockquote>
537
+ <table border="1">
538
+ <tr><td><b>Name</b></td><td><b>Stored</b></td><td><b>Default</b></td></tr>
539
+ <?php
540
+ if ($stored_options !== false){
541
+ $count = 0;
542
+ foreach ($stored_options as $key => $value){
543
+ if ($count % 2 == 0){
544
+ echo '<tr style="background-color:#cccccc"><td>';
545
+ } else {
546
+ echo '<tr><td>';
547
+ }
548
+ echo "$key";
549
+ echo "</td><td>";
550
+ echo "$value";
551
+ echo "</td><td>";
552
+ echo $default_options[$key];
553
+ echo "</td></tr>";
554
+ $count++;
555
+ }
556
+ } else {
557
+ echo "<br />No options in database!";
558
+ }
559
+ echo '</table>';
560
+ echo '</blockquote>';
561
+
562
+ echo '<h4>Other settings</h4><blockquote>';
563
+
564
+ echo 'ADINJ_PATH='.ADINJ_PATH.'<br />';
565
+ echo 'ADINJ_CONFIG_FILE='.ADINJ_CONFIG_FILE.'<br />';
566
+ echo 'ADINJ_AD_PATH='.ADINJ_AD_PATH.'<br />';
567
+
568
+ echo 'Plugin version='.adinj_get_version();
569
+ echo '</blockquote>';
570
+
571
+
572
+ }
573
+
574
+ function adinj_get_version(){
575
+ $plugin_data=get_plugin_data(ADINJ_PATH . '/ad-injection.php');
576
+ return $plugin_data['Version'];
577
+ }
578
+
579
+ function adinj_problem_with_wpminify_check(){
580
+ $wpm_options = get_option('wp_minify');
581
+ if ($wpm_options['enable_html'] == true && is_plugin_active('wp-super-cache/wp-cache.php')){
582
+ return true;
583
+ }
584
+ return false;
585
+ }
586
+
587
+ function adinj_get_problem_with_wpminify_message(){
588
+ return '<font color="red">Problem: Ad Injection will not work if WP Minify is set to minify HTML whilst WP Super Cache is enabled.</font><br />
589
+ Solution: Go to <a href="options-general.php?page=wp-minify">the WP Minify settings page</a> and untick "Enable HTML Minification".';
590
+ }
591
+
592
+ function adinj_init_warning() {
593
+ if (stripos($_SERVER["PHP_SELF"], 'plugins.php') !== false){
594
+ echo '<div id="ad-injection-warning" class="updated fade"><p><strong>
595
+ Ad Injection needs configuring:
596
+ Go to <a href="options-general.php?page=ad-injection.php">the settings page</a> to configure and enable your ads.
597
+ </strong></p></div>
598
+ ';
599
+ }
600
+ // TODO add option to disable message without having to go to settings.
601
+ }
602
+
603
+ function adinj_compatibility_checks() {
604
+ if (stripos($_SERVER["PHP_SELF"], 'ad-injection') !== false){
605
+ $wpm_options = get_option('wp_minify');
606
+ if ($wpm_options['enable_html'] == true){
607
+ echo '<div id="ad-injection-error" class="error"><p><strong>';
608
+ echo adinj_get_problem_with_wpminify_message();
609
+ echo '</strong></p></div>';
610
+ }
611
+ }
612
+ }
613
+
614
+ function adinj_activate_hook() {
615
+ $stored_options = adinj_options();
616
+ $default_options = adinj_default_options();
617
+ if($stored_options === false){
618
+ // Save defaults to DB below.
619
+ if (!is_plugin_active('wp-super-cache/wp-cache.php')){
620
+ $stored_options['ad_insertion_mode'] = 'direct_dynamic';
621
+ }
622
+ } else {
623
+ // Upgrade options if necessary. Use default as a baseline,
624
+ // and then overwrite default with the saved ones.
625
+ foreach ($default_options as $key => $value){
626
+ if (array_key_exists($key, $stored_options)){
627
+ $default_options[$key] = $stored_options[$key];
628
+ }
629
+ }
630
+ }
631
+ update_option('adinj_options', $default_options);
632
+ }
633
+
634
+ // If the options in the database are out of sync with our default options
635
+ // then the database options will need upgrading
636
+ function adinj_options_need_upgrading($stored_options){
637
+ $default_options = adinj_default_options();
638
+ foreach ($default_options as $key => $value){
639
+ if (!array_key_exists($key, $stored_options)){
640
+ return true;
641
+ }
642
+ }
643
+ foreach ($stored_options as $key => $value){
644
+ if (!array_key_exists($key, $default_options)){
645
+ return true;
646
+ }
647
+ }
648
+ return false;
649
+ }
650
+
651
+ function adinj_default_options(){
652
+ return array(
653
+ 'ads_enabled' => '',
654
+ 'max_num_of_ads' => '2', // single posts and pages
655
+ 'max_num_of_ads_home_page' => '3',
656
+ 'no_random_ads_if_shorter_than' => '100',
657
+ 'one_ad_if_shorter_than' => '500',
658
+ 'two_ads_if_shorter_than' => '1000',
659
+ 'three_ads_if_shorter_than' => ADINJ_RULE_DISABLED,
660
+ 'top_ad_if_longer_than' => ADINJ_RULE_DISABLED,
661
+ 'bottom_ad_if_longer_than' => ADINJ_RULE_DISABLED,
662
+ 'rnd_align' => ADINJ_RULE_DISABLED,
663
+ 'rnd_margin_top' => '3',
664
+ 'rnd_margin_bottom' => '3',
665
+ 'top_align' => ADINJ_RULE_DISABLED,
666
+ 'top_margin_top' => '3',
667
+ 'top_margin_bottom' => '3',
668
+ 'bottom_align' => ADINJ_RULE_DISABLED,
669
+ 'bottom_margin_top' => '3',
670
+ 'bottom_margin_bottom' => '3',
671
+ 'ads_on_page_older_than' => '10',
672
+ 'exclude_page' => '',
673
+ 'exclude_single' => '',
674
+ 'first_paragraph_ad' => '',
675
+ 'multiple_ads_at_same_position' => '',
676
+ 'sevisitors_only' => '',
677
+ 'ad_referrers' => '.google., .bing., .yahoo., .ask., search?, search., /search/',
678
+ 'blocked_ips' => '',
679
+ 'ad_insertion_mode' => 'mfunc',
680
+ 'ui_random_hide' => 'false',
681
+ 'ui_topad_hide' => 'false',
682
+ 'ui_bottomad_hide' => 'false',
683
+ 'ui_restrictions_hide' => 'false',
684
+ 'ui_debugging_hide' => 'true',
685
+ 'ui_docs_hide' => 'false',
686
+ 'ui_testads_hide' => 'false',
687
+ 'debug_mode' => ''
688
+ );
689
+ }
690
+
691
+ function adinj_getdefault($option){
692
+ $default_options = adinj_default_options();
693
+ return "(default: " . $default_options[$option] . ")";
694
+ }
695
+
696
+ // TODO is the chmod'ing here a bit extreme?
697
+ function write_ad_to_file($ad, $ad_path){
698
+ chmod(ADINJ_AD_PATH, 0750) or die("chmod failed: " . ADINJ_AD_PATH);
699
+ if (file_exists($ad_path)){
700
+ chmod($ad_path, 0640) or die("chmod failed: $ad_path");
701
+ }
702
+ $handle = fopen($ad_path, "w") or die("could not open ad file: $ad_path");
703
+ if (strlen($ad) >0){
704
+ fwrite($handle, $ad) or die("could not write to ad file: $ad_path");
705
+ }
706
+ fclose($handle);
707
+ chmod($ad_path, 0640) or die("chmod failed: $ad_path");
708
+ }
709
+
710
+ function adinj_docs(){
711
+ ?>
712
+ <hr />
713
+
714
+ <?php adinj_postbox_start(__("Quick Start", 'adinj'), "docs", '95%'); ?>
715
+
716
+ <p>1. Copy and paste your ad code into the ad code boxes.</p>
717
+
718
+ <p>2. Choose how many ads of each type you want displayed.</p>
719
+
720
+ <p>3. Configure any ad positioning (optional).</p>
721
+
722
+ <p>4. Check the ad insertion mode (in the Insertion mode and ad display restriction section).</p>
723
+
724
+ <p>5. If you are using a compatible ad insertion mode you may configure dynamic ad display restrictions. i.e. showing ads only to certain people (e.g. search engine visitors), or blocking ads from specific IPs.</p>
725
+
726
+ <p>6. Enable your ads (tick box at the very top).</p>
727
+ </div>
728
+
729
+ <h3><?php echo adinj_get_logo(); ?> Supported in-page tags</h3>
730
+ <div class="inside" style="margin:10px">
731
+
732
+ <p>These tags can be inserted into the page source to override the configured behaviour on individual pages. Because sometimes specific pages need to be treated differently.</p>
733
+
734
+ <ul>
735
+ <li><code>&lt;!--noadsense--&gt;</code> OR <code>&lt;!-no-adsense--&gt;</code> OR <code>&lt;!--NoAds--&gt;</code> OR <code>&lt;!--OffAds--&gt;</code> - disables all ads on this page. These tags are here to make this plugin compatible with the tags from Adsense Injection, Whydowork Adsense and Quick Adsense.</li>
736
+ </ul>
737
+
738
+ <p></p>
739
+
740
+ <ul>
741
+ <li><code>&lt;!--adsandwich--&gt;</code> - Inserts the top and bottom ad but no random ads. Disables all other ads.</li>
742
+ <li><code>&lt;!--adfooter--&gt;</code> - Insert a single ad at the very bottom. Disables all other ads.</li>
743
+ </ul>
744
+
745
+ <p></p>
746
+
747
+ <ol>
748
+ <li><code>&lt;!--adsensestart--&gt;</code> - Random ads will start from this point*. For compatibility with Adsense Injection.</li>
749
+ <li><code>&lt;!--adsenseend--&gt;</code> - Random ads will not be inserted after this point*. New tag but I've kept the Adsense Injection naming convention to make it fit with the above tag.</li>
750
+ <li><code>&lt;!--adstart--&gt;</code> - Random ads will start from this point*.</li>
751
+ <li><code>&lt;!--adend--&gt;</code> - Random ads will not be inserted after this point*.</li>
752
+ </ol>
753
+
754
+ <p>These four tags will not affect the top and bottom ad.</p>
755
+
756
+ <h4>Custom field for disabling adverts</h4>
757
+
758
+ <p>To disable all adverts on the page you can also set the custom <code>disable_adverts</code> field to '1' from the WordPress post editor.</p>
759
+
760
+
761
+ <?php adinj_postbox_end(); ?>
762
+
763
+
764
+ <?php adinj_postbox_start(__("Test Adverts", 'adinj'), "testads", '95%'); ?>
765
+
766
+ <p>You can copy and paste these adverts into the boxes above to test your ad setup before switching to your real ads.</p>
767
+
768
+ <h4><a name="468x60"></a>468x60 banner</h4>
769
+
770
+ <p><textarea onclick="javascript:this.focus();this.select();" style="min-height:50px;" cols="80" rows="4">&lt;div style=&quot;background-color:#ff9999; width:468px; height:60px;&quot;&gt;
771
+ &lt;h5&gt;TEST ADVERT 468x60 - &lt;a href=&quot;http://www.reviewmylife.co.uk/&quot;&gt;www.reviewmylife.co.uk&lt;/a&gt;&lt;/h5&gt;
772
+ &lt;/div&gt;</textarea></p>
773
+
774
+ <div style="background-color:#99ffff; width:468px; height:60px;">
775
+ <h5>TEST ADVERT 468x60 - <a href="http://www.reviewmylife.co.uk/">www.reviewmylife.co.uk</a></h5>
776
+ </div><p></p>
777
+
778
+ <h4><a name="728x90"></a>728x90 banner</h4>
779
+
780
+ <p><textarea onclick="javascript:this.focus();this.select();" style="min-height:50px;" cols="80" rows="4">&lt;div style=&quot;background-color:#ff9999; width:728px; height:90px;&quot;&gt;
781
+ &lt;h5&gt;TEST ADVERT 728x90&lt;/h5&gt;
782
+ &lt;a href=&quot;http://www.reviewmylife.co.uk/&quot;&gt;www.reviewmylife.co.uk&lt;/a&gt;&lt;br /&gt;
783
+ &lt;a href=&quot;http://www.advancedhtml.co.uk/&quot;&gt;www.advancedhtml.co.uk&lt;/a&gt;
784
+ &lt;/div&gt;</textarea></p>
785
+
786
+ <div style="background-color:#ff9999; width:728px; height:90px;">
787
+ <h5>TEST ADVERT 728x90</h5>
788
+ <a href="http://www.reviewmylife.co.uk/">www.reviewmylife.co.uk</a><br />
789
+ <a href="http://www.advancedhtml.co.uk/">www.advancedhtml.co.uk</a>
790
+ </div><p></p>
791
+
792
+ <h4>160x90 link unit</h4>
793
+
794
+ <p><textarea onclick="javascript:this.focus();this.select();" style="min-height:50px;" cols="80" rows="4">&lt;div style=&quot;background-color:#ccff99; width:160px; height:90px;&quot;&gt;
795
+ &lt;h5&gt;TEST ADVERT 160x90&lt;/h5&gt;
796
+ &lt;a href=&quot;http://www.reviewmylife.co.uk/&quot;&gt;reviewmylife.co.uk&lt;/a&gt;&lt;br /&gt;
797
+ &lt;a href=&quot;http://www.advancedhtml.co.uk/&quot;&gt;advancedhtml.co.uk&lt;/a&gt;
798
+ &lt;/div&gt;</textarea></p>
799
+
800
+ <div style="background-color:#ccff99; width:160px; height:90px;">
801
+ <h5>TEST ADVERT 160x90</h5>
802
+ <a href="http://www.reviewmylife.co.uk/">reviewmylife.co.uk</a>
803
+ <a href="http://www.advancedhtml.co.uk/">advancedhtml.co.uk</a><br />
804
+ </div><p></p>
805
+
806
+ <h4><a name="468x15"></a>468x15 link unit</h4>
807
+
808
+ <p><textarea onclick="javascript:this.focus();this.select();" style="min-height:50px;" cols="80" rows="4">&lt;div style=&quot;background-color:#cccc99; width:468px; height:15px;&quot;&gt;
809
+ &lt;font size=&quot;-2&quot;&gt;&lt;b&gt;TEST ADVERT 160x90&lt;/b&gt; &lt;a href=&quot;http://www.reviewmylife.co.uk/&quot;&gt;reviewmylife.co.uk&lt;/a&gt;&lt;/font&gt;
810
+ &lt;/div&gt;</textarea></p>
811
+
812
+ <div style="background-color:#cccc99; width:468px; height:15px;">
813
+ <font size="-2"><b>TEST ADVERT 160x90</b> <a href="http://www.reviewmylife.co.uk/">reviewmylife.co.uk</a></font>
814
+ </div><p></p>
815
+
816
+ <h4><a name="336x280"></a>336x280 large rectangle</h4>
817
+
818
+ <p><textarea onclick="javascript:this.focus();this.select();" style="min-height:50px;" cols="80" rows="4">&lt;div style=&quot;background-color:#ccccff; width:336px; height:280px;&quot;&gt;
819
+ &lt;h5&gt;TEST ADVERT 336x280 - &lt;a href=&quot;http://www.reviewmylife.co.uk/&quot;&gt;www.reviewmylife.co.uk&lt;/a&gt;&lt;&lt;/h5&gt;
820
+ &lt;/div&gt;</textarea></p>
821
+
822
+ <div style="background-color:#ccccff; width:336px; height:280px;">
823
+ <h5>TEST ADVERT 336x280 - <a href="http://www.reviewmylife.co.uk/">www.reviewmylife.co.uk</a></h5>
824
+ </div><p></p>
825
+
826
+ <h4>468x60 banner with dynamic PHP</h4>
827
+
828
+ <p>The PHP will execute if 1) WP Super Cache is turned on in legacy mode, and 2) if WP Super Cache is not installed/disabled.</p>
829
+
830
+ <p><textarea onclick="javascript:this.focus();this.select();" style="min-height:50px;" cols="80" rows="5">&lt;div style=&quot;background-color:#ffff99; width:468px; height:60px;&quot;&gt;
831
+ &lt;b&gt;TEST ADVERT 468x60 with date() and rand()&lt;/b&gt;&lt;br /&gt;
832
+ &lt;?php echo &quot;date=&quot;.date(&quot;Y-m-d H:i:s&quot;) .&quot; rand=&quot;.rand(); ?&gt;&lt;br /&gt;
833
+ &lt;a href=&quot;http://www.advancedhtml.co.uk/&quot;&gt;www.advancedhtml.co.uk&lt;/a&gt;
834
+ &lt;/div&gt;</textarea></p>
835
+
836
+ <div style="background-color:#ffff99; width:468px; height:60px;">
837
+ <b>TEST ADVERT 468x60 with date() and rand()</b><br />
838
+ <?php echo "date=".date("Y-m-d H:i:s") ." rand=".rand(); ?><br />
839
+ <a href="http://www.advancedhtml.co.uk/">www.advancedhtml.co.uk</a>
840
+ </div><p></p>
841
+
842
+ <?php adinj_postbox_end();
843
+
844
+ }
845
+
846
+ function adinj_init_hook() {
847
+ if (!adinj_ticked('ads_enabled')) {
848
+ add_action('admin_notices', 'adinj_init_warning');
849
+ }
850
+ }
851
+
852
+ add_action('init', 'adinj_init_hook');
853
+ // Install / activate
854
+ register_activation_hook( __FILE__, 'adinj_activate_hook' );
855
+ // Config links
856
+ add_action('admin_menu', 'adinj_admin_menu_hook');
857
+ add_filter('plugin_action_links', 'adinj_options_link_hook', 10, 2); // digits = priority, num args
858
+
859
+ ?>
ad-injection.php ADDED
@@ -0,0 +1,567 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Ad Injection
4
+ Plugin URI: http://www.reviewmylife.co.uk/blog/2010/12/06/ad-injection-plugin-wordpress/
5
+ Description: Inserts any advert into your blog. Options to exclude by post age, visitor IP, and visitor referrer. Works with WP Super Cache.
6
+ Version: 0.8.3
7
+ Author: reviewmylife
8
+ Author URI: http://www.reviewmylife.co.uk/blog/
9
+ License: GPLv2
10
+ */
11
+
12
+ /* Copyright 2010 reviewmylife (contact : http://www.reviewmylife.co.uk/)
13
+
14
+ This program is free software; you can redistribute it and/or modify
15
+ it under the terms of the GNU General Public License, version 2, as
16
+ published by the Free Software Foundation.
17
+
18
+ This program is distributed in the hope that it will be useful,
19
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ GNU General Public License for more details.
22
+
23
+ You should have received a copy of the GNU General Public License
24
+ along with this program; if not, write to the Free Software
25
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
+ */
27
+
28
+
29
+ //error_reporting(E_ALL ^ E_STRICT);
30
+
31
+ // Files
32
+ define('ADINJ_PATH', WP_PLUGIN_DIR.'/ad-injection');
33
+ define('ADINJ_CONFIG_FILE', ADINJ_PATH . '/ad-injection-config.php');
34
+ define('ADINJ_AD_PATH', ADINJ_PATH . '/ads');
35
+ define('ADINJ_AD_RANDOM_FILE', 'ad_random_1.txt');
36
+ define('ADINJ_AD_TOP_FILE', 'ad_top_1.txt');
37
+ define('ADINJ_AD_BOTTOM_FILE', 'ad_bottom_1.txt');
38
+
39
+ // Constants
40
+ define('ADINJ_RULE_DISABLED', 'Rule Disabled');
41
+ define('ADINJ_ALWAYS_SHOW', 'Always show');
42
+
43
+ // Global variables
44
+ $adinj_total_rand_ads_used = 0;
45
+ $adinj_total_all_ads_used = 0;
46
+ $adinj_data = false;
47
+
48
+ if (is_admin()){
49
+ require_once(ADINJ_PATH . '/ad-injection-admin.php');
50
+ }
51
+
52
+ function adinj_admin_menu_hook(){
53
+ add_options_page('Ad Injection', 'Ad Injection', 'manage_options', basename(__FILE__), 'adinj_options_page');
54
+ }
55
+
56
+ function adinj_options_link_hook($links, $file) {
57
+ static $this_plugin;
58
+ if (!$this_plugin) $this_plugin = plugin_basename(__FILE__);
59
+ if ($file == $this_plugin){
60
+ $link = "<a href='options-general.php?page=ad-injection.php'>" . __("Settings") . "</a>";
61
+ array_unshift($links, $link);
62
+ }
63
+ return $links;
64
+ }
65
+
66
+ function adinj_options($reset=false){
67
+ global $adinj_data;
68
+ if ($adinj_data === false || $reset !== false){
69
+ $adinj_data = get_option('adinj_options');
70
+ }
71
+ return $adinj_data;
72
+ }
73
+
74
+ function adinj_option($option){
75
+ $options = adinj_options();
76
+ return $options[$option];
77
+ }
78
+
79
+ // TODO make the cookie domain from wp-config.php accessible to script
80
+ //$cookie_domain = COOKIE_DOMAIN; // TODO test
81
+ //var adinj_cookie_domain = "$cookie_domain"; //JS line. TODO test
82
+ function adinj_print_referrers_hook(){
83
+ if (adinj_ads_completely_disabled_from_page()) return;
84
+ if (adinj_ticked('sevisitors_only')){
85
+ $referrer_list = adinj_quote_list('ad_referrers');
86
+ echo <<<SCRIPT
87
+ <script type="text/javascript">
88
+ var adinj_referrers = new Array($referrer_list);
89
+ adinj_searchenginevisitor();
90
+ </script>
91
+
92
+ SCRIPT;
93
+ // TODO add comment to script // Ad Injection plugin
94
+ }
95
+ }
96
+
97
+ function adinj_quote_list($option){
98
+ $options = adinj_options();
99
+ $list = $options[$option];
100
+
101
+ // I'm sure this whole thing could be done with a much simpler single
102
+ // line of PHP - but right now my brain isn't up to thinking about it!
103
+ $lines = explode("\n", $list);
104
+ foreach ($lines as $line){
105
+ $stripped_lines[] = preg_replace("/\/\/.*/", "", $line);
106
+ }
107
+ $list = implode(" ", $stripped_lines);
108
+
109
+ $list = preg_replace("/'/", "", $list);
110
+ $referrers = preg_split("/[\s,]+/", $list);
111
+ foreach ($referrers as $referrer){
112
+ $newlist[] = "'" . $referrer . "'";
113
+ }
114
+ return implode(", ", $newlist);
115
+ }
116
+
117
+ function adinj_addsevjs_hook(){
118
+ if (adinj_ads_completely_disabled_from_page()) return;
119
+ // Put the search engine detection / cookie setting script in the footer
120
+ wp_enqueue_script('adinj_sev', WP_PLUGIN_URL.'/ad-injection/adinj-sev.js', NULL, NULL, true);
121
+ }
122
+
123
+ function adinj_ad_code_eval($adfile){
124
+ $options = adinj_options();
125
+ if ($options['ad_insertion_mode'] == 'mfunc'){
126
+ $adcode = "\n
127
+ <!--mfunc adshow_display_ad_file('$adfile') -->
128
+ <?php adshow_display_ad_file('$adfile'); ?>
129
+ <!--/mfunc-->
130
+ ";
131
+ } else {
132
+ $adcode = read_ad_from_file(ADINJ_AD_PATH.'/'.$adfile);
133
+ $prefix = 'rnd_';
134
+ if ($adfile == ADINJ_AD_TOP_FILE) $prefix = 'top_';
135
+ if ($adfile == ADINJ_AD_BOTTOM_FILE) $prefix = 'bottom_';
136
+ $adcode = adinj_add_tags($adcode, $prefix);
137
+ }
138
+ if (stripos($adcode, '<?php') !== false){
139
+ return adinj_eval_php($adcode);
140
+ }
141
+ return $adcode;
142
+ }
143
+
144
+ function adinj_ad_code_include(){
145
+ $plugin_dir = ADINJ_PATH;
146
+ $options = adinj_options();
147
+ $adcode = "";
148
+ if ($options['ad_insertion_mode'] == 'mfunc'){
149
+ // WP Super Cache's support for mclude assumes that we will be including
150
+ // files from within ABSPATH. To remove this limitation we do the include
151
+ // using mfunc instead.
152
+ $adcode = "\n
153
+ <!--mfunc include_once('$plugin_dir/adshow.php') -->
154
+ <?php include_once('$plugin_dir/adshow.php'); ?>
155
+ <!--/mfunc-->
156
+ ";
157
+ }
158
+ return adinj_eval_php($adcode);
159
+ }
160
+
161
+ function adinj_add_tags($adcode, $prefix, $func=NULL){
162
+ $options = adinj_options();
163
+ if ($options[$prefix . 'align'] !== ADINJ_RULE_DISABLED ||
164
+ $options[$prefix . 'margin_top'] !== ADINJ_RULE_DISABLED ||
165
+ $options[$prefix . 'margin_bottom'] !== ADINJ_RULE_DISABLED) {
166
+ $top = "";
167
+ $bottom = "";
168
+ if ($options[$prefix . 'margin_top'] !== ADINJ_RULE_DISABLED) $top="margin-top:" . $options[$prefix . 'margin_top'] . "px;";
169
+ if ($options[$prefix . 'margin_bottom'] !== ADINJ_RULE_DISABLED) $bottom="margin-bottom:" . $options[$prefix . 'margin_bottom'] . "px;";
170
+
171
+ if ($options[$prefix . 'align'] == 'left'){
172
+ $div = "<div style='float:left;" . $top . $bottom . "'>ADCODE</div><br clear='all' />";
173
+ } else if ($options[$prefix . 'align'] == 'center'){
174
+ $div = "<div style='" . $top . $bottom . "'><center>ADCODE</center></div>";
175
+ } else if ($options[$prefix . 'align'] == 'right'){
176
+ $div = "<div style='float:right;" . $top . $bottom . "'>ADCODE</div><br clear='all' />";
177
+ } else if ($options[$prefix . 'align'] == 'float left'){
178
+ $div = "<div style='float:left;" . $top . $bottom . "margin-right:5px;'>ADCODE</div>";
179
+ } else if ($options[$prefix . 'align'] == 'float right'){
180
+ $div = "<div style='float:right;" . $top . $bottom . "margin-left:5px;'>ADCODE</div>";
181
+ } else {
182
+ $div = "<div style='" . $top . $bottom . "'>ADCODE</div>";
183
+ }
184
+ if (empty($func)){
185
+ return str_replace("ADCODE", $adcode, $div);
186
+ } else {
187
+ $ad = str_replace("ADCODE", "\$adcode", $div);
188
+ return "function $func(\$adcode) { return \"$ad\"; }";
189
+ }
190
+ }
191
+ if (!empty($func)){
192
+ return "function $func(\$adcode){return \$adcode;}";
193
+ }
194
+ return $adcode;
195
+ }
196
+
197
+ function adinj_ad_code_random(){
198
+ return adinj_ad_code_eval(ADINJ_AD_RANDOM_FILE);
199
+ }
200
+
201
+ function adinj_ad_code_top(){
202
+ global $adinj_total_all_ads_used;
203
+ ++$adinj_total_all_ads_used;
204
+ return adinj_ad_code_eval(ADINJ_AD_TOP_FILE);
205
+ }
206
+
207
+ function adinj_ad_code_bottom(){
208
+ global $adinj_total_all_ads_used;
209
+ ++$adinj_total_all_ads_used;
210
+ return adinj_ad_code_eval(ADINJ_AD_BOTTOM_FILE);
211
+ }
212
+
213
+ function read_ad_from_file($ad_path){
214
+ $contents = "";
215
+ if (file_exists($ad_path)){
216
+ $contents = file_get_contents($ad_path);
217
+ if ($contents === false) die("can't read from file: $ad_path");
218
+ }
219
+ return $contents;
220
+ }
221
+
222
+ // Based on: http://www.wprecipes.com/wordpress-hack-how-to-display-ads-on-old-posts-only
223
+ // Only use for pages and posts. Not for archives, categories, home page, etc.
224
+ function adinj_is_old_post(){
225
+ $options = adinj_options();
226
+ $days = $options['ads_on_page_older_than'];
227
+ if ($days == 0) return true;
228
+ if(is_single() || is_page()) {
229
+ $current_date = time();
230
+ $offset = $days * 60*60*24;
231
+ $post_date = get_the_time('U');
232
+ if(($post_date + $offset) > $current_date){
233
+ return false;
234
+ } else {
235
+ return true;
236
+ }
237
+ }
238
+ return false;
239
+ }
240
+
241
+ function adinj_adverts_disabled_flag(){
242
+ $custom_fields = get_post_custom();
243
+ if (isset($custom_fields['disable_adverts'])){
244
+ $disable_adverts = $custom_fields['disable_adverts'];
245
+ return ($disable_adverts[0] == 1);
246
+ }
247
+ return 0;
248
+ }
249
+
250
+ //////////For runtime ads - i.e. when caching is off and config.php not loaded
251
+ function adinj_search_engine_referrers(){
252
+ $list = adinj_quote_list('ad_referrers');
253
+ return preg_split("/[,'\s*]/", $list);
254
+ }
255
+ function adinj_blocked_ips(){
256
+ $list = adinj_quote_list('blocked_ips');
257
+ return preg_split("/[,'\s*]/", $list);
258
+ }
259
+ function adinj_fromasearchengine(){
260
+ $referrer = $_SERVER['HTTP_REFERER'];
261
+ $searchengines = adinj_search_engine_referrers();
262
+ foreach ($searchengines as $se) {
263
+ if (stripos($referrer, $se) !== false) {
264
+ return true;
265
+ }
266
+ }
267
+ // Also return true if the visitor has recently come from a search engine
268
+ // and has the adinj cookie set.
269
+ return ($_COOKIE["adinj"]==1);
270
+ }
271
+ function adinj_blocked_ip(){
272
+ $visitorIP = $_SERVER['REMOTE_ADDR'];
273
+ return in_array($visitorIP, adinj_blocked_ips());
274
+ }
275
+ function adinj_show_adverts(){
276
+ if (adinj_blocked_ip()){
277
+ return "blockedip";
278
+ }
279
+ if (adinj_ticked('sevisitors_only')){
280
+ if (!adinj_fromasearchengine()){
281
+ return "blockedreferrer";
282
+ }
283
+ }
284
+ return true;
285
+ }
286
+ // From: Exec-PHP plugin
287
+ function adinj_eval_php($code) {
288
+ if (strlen($code) == 0) return $code;
289
+ ob_start();
290
+ eval("?>$code<?php ");
291
+ $output = ob_get_contents();
292
+ ob_end_clean();
293
+ return $output;
294
+ }
295
+ //////////For runtime ads
296
+
297
+ function adinj($content, $message){
298
+ if (!adinj_ticked('debug_mode')) return $content;
299
+ global $adinj_total_rand_ads_used, $adinj_total_all_ads_used;
300
+ $path = ADINJ_AD_PATH;
301
+ $options = adinj_options();
302
+ $mode = $options['ad_insertion_mode'];
303
+ return $content."
304
+ <!--
305
+ [ADINJ DEBUG]
306
+ $message
307
+ content length=".strlen($content)."
308
+ \$adinj_total_rand_ads_used=$adinj_total_rand_ads_used
309
+ \$adinj_total_all_ads_used=$adinj_total_all_ads_used
310
+ injection mode=$mode
311
+ ADINJ_AD_PATH=$path
312
+ //-->\n";
313
+ }
314
+
315
+ function adinj_ads_completely_disabled_from_page($content=NULL){
316
+ if (!adinj_ticked('ads_enabled')){
317
+ return "NOADS: ads_enabled not ticked";
318
+ }
319
+
320
+ // check for ads on certain page types being disabled
321
+ if ((is_home() && adinj_ticked('exclude_home')) ||
322
+ (is_page() && adinj_ticked('exclude_page')) ||
323
+ (is_single() && adinj_ticked('exclude_single'))){
324
+ return "NOADS: excluded from this page type";
325
+ }
326
+
327
+ // if disable_adverts==1
328
+ if (adinj_adverts_disabled_flag()) return "NOADS: adverts_disabled_flag";
329
+
330
+ // no ads on old posts/pages if rule is enabled
331
+ if((is_page() || is_single()) && !adinj_is_old_post()) return "NOADS: !is_old_post";
332
+
333
+ if ($content == NULL){
334
+ return false;
335
+ }
336
+
337
+ // manual ad disabling tags
338
+ if (strpos($content, "<!--noadsense-->") !== false) return "NOADS: noadsense tag"; // 'Adsense Injection' tag
339
+ if (strpos($content, "<!-no-adsense-->") !== false) return "NOADS: no-adsense tag"; // 'Whydowork Adsense' tag
340
+ if (stripos($content,'<!--NoAds-->') !== false) return "NOADS: NoAds tag"; // 'Quick Adsense' tag
341
+ if (stripos($content,'<!--OffAds-->') !== false) return "NOADS: OffAds tag"; // 'Quick Adsense' tag
342
+
343
+ return false;
344
+ }
345
+
346
+ function adinj_inject_hook($content){
347
+ if (is_feed()) return $content;
348
+
349
+ $reason = adinj_ads_completely_disabled_from_page($content);
350
+ if ($reason !== false){
351
+ return adinj($content, $reason);
352
+ }
353
+
354
+ $options = adinj_options();
355
+
356
+ if ($options['ad_insertion_mode'] == 'direct_dynamic'){
357
+ $showads = adinj_show_adverts();
358
+ if ($showads !== true){
359
+ return adinj($content, "NOADS: ad blocked at run time reason=$showads");
360
+ }
361
+ }
362
+
363
+ $adcode_include = adinj_ad_code_include();
364
+
365
+ # Ad sandwich mode
366
+ if(is_page() || is_single()){
367
+ if(stripos($content, "<!--adsandwich-->") !== false) return adinj($adcode_include.adinj_ad_code_top().$content.adinj_ad_code_bottom(), "Ads=adsandwich");
368
+ if(stripos($content, "<!--adfooter-->") !== false) return adinj($content.$adcode_include.adinj_ad_code_bottom(), "Ads=adfooter");
369
+ }
370
+
371
+ # Insert top and bottom ads if necesary
372
+ $length = strlen($content);
373
+ if(is_page() || is_single()){
374
+ if (adinj_do_rule_if($options['top_ad_if_longer_than'], '<', $length)){
375
+ $content = $adcode_include.adinj_ad_code_top().$content;
376
+ $adcode_include = false;
377
+ }
378
+ if (adinj_do_rule_if($options['bottom_ad_if_longer_than'], '<', $length)){
379
+ $content = $content.adinj_ad_code_bottom();
380
+ }
381
+ }
382
+
383
+ $num_rand_ads_to_insert = adinj_num_rand_ads_to_insert($length);
384
+ if ($num_rand_ads_to_insert <= 0) return adinj($content, "all ads used up");
385
+
386
+ if ($adcode_include !== false) $content = $adcode_include.$content;
387
+
388
+ $debug_on = $options['debug_mode'];
389
+ if (!$debug_on) $debugtags=false;
390
+
391
+ $content_adfree_header = "";
392
+ $content_adfree_footer = "";
393
+
394
+ // TODO add docs explaining the significance of leaving blank lines
395
+ // before or after these tags
396
+ # 'Adsense Injection' tag compatibility
397
+ $split = adinj_split_by_tag($content, "<!--adsensestart-->", $debugtags);
398
+ if (count($split) == 2){
399
+ $content_adfree_header = $split[0];
400
+ $content = $split[1];
401
+ }
402
+
403
+ # Use the same naming convention for the end tag
404
+ $split = adinj_split_by_tag($content, "<!--adsenseend-->", $debugtags);
405
+ if (count($split) == 2){
406
+ $content = $split[0];
407
+ $content_adfree_footer = $split[1];
408
+ }
409
+
410
+ $split = adinj_split_by_tag($content, "<!--adstart-->", $debugtags);
411
+ if (count($split) == 2){
412
+ $content_adfree_header = $split[0];
413
+ $content = $split[1];
414
+ }
415
+
416
+ $split = adinj_split_by_tag($content, "<!--adend-->", $debugtags);
417
+ if (count($split) == 2){
418
+ $content = $split[0];
419
+ $content_adfree_footer = $split[1];
420
+ }
421
+
422
+ # Move onto random ad insertions
423
+ $paragraphmarker = "</p>";
424
+ if(stripos($content, $paragraphmarker) === false) return adinj($content, "no &lt;/p&gt; tags");
425
+
426
+ if ($debug_on) $debug = "\nTags=". htmlentities($debugtags);
427
+
428
+ // Generate a list of all potential injection points
429
+ if ($debug_on) $debug .= "\nPotential positions: ";
430
+ $potential_inj_positions = array();
431
+ $prevpos = -1;
432
+ while(($prevpos = stripos($content, $paragraphmarker, $prevpos+1)) !== false){
433
+ $potential_inj_positions[] = $prevpos + strlen($paragraphmarker);
434
+ if ($debug_on) $debug .= $prevpos.", ";
435
+ }
436
+
437
+ if ($debug_on) $debug .= "\npotential_inj_positions:".sizeof($potential_inj_positions);
438
+
439
+ if (sizeof($potential_inj_positions) == 0){
440
+ return adinj($content, "Error: no potential inj positions");
441
+ }
442
+
443
+ $inj_positions = array();
444
+
445
+ if (adinj_ticked('first_paragraph_ad')){
446
+ $inj_positions[] = array_shift($potential_inj_positions);
447
+ --$num_rand_ads_to_insert;
448
+ }
449
+
450
+ // Pick the correct number of random injection points
451
+ if (sizeof($potential_inj_positions) > 0 && $num_rand_ads_to_insert > 0){
452
+ if (!adinj_ticked('multiple_ads_at_same_position')){
453
+ // Each ad is inserted into a unique position
454
+ if (sizeof($potential_inj_positions) < $num_rand_ads_to_insert){
455
+ $debug .= "\nnum_rand_ads_to_insert requested=$num_rand_ads_to_insert. But restricted to ". sizeof($potential_inj_positions) . " due to limited injection points.";
456
+ $num_rand_ads_to_insert = sizeof($potential_inj_positions);
457
+ }
458
+ $rand_positions = array_rand(array_flip($potential_inj_positions), $num_rand_ads_to_insert);
459
+ if ($num_rand_ads_to_insert == 1){
460
+ // Convert it back into an array
461
+ $inj_positions[] = $rand_positions;
462
+ } else {
463
+ $inj_positions = array_merge($inj_positions, $rand_positions);
464
+ }
465
+ foreach($inj_positions as $pos){
466
+ if ($debug_on) $debug = $pos . ", " . $debug;
467
+ }
468
+ } else {
469
+ // Multiple ads may be inserted at the same position
470
+ $injections = 0;
471
+ while($injections++ < $num_rand_ads_to_insert){
472
+ $rnd = array_rand($potential_inj_positions);
473
+ if ($debug_on) $debug = $potential_inj_positions[$rnd] . ", " . $debug;
474
+ $inj_positions[] = $potential_inj_positions[$rnd];
475
+ }
476
+ }
477
+ }
478
+
479
+ if (sizeof($inj_positions) == 0){
480
+ return adinj($content_adfree_header.$content.$content_adfree_footer, "Error: No ad injection positions: " . $debug);
481
+ }
482
+
483
+ // Sort positions
484
+ sort($inj_positions);
485
+
486
+ // Insert ads in reverse order
487
+ $adcode = adinj_ad_code_random();
488
+ global $adinj_total_rand_ads_used, $adinj_total_all_ads_used;
489
+ for ($adnum=sizeof($inj_positions)-1; $adnum>=0; $adnum--){
490
+ $content = substr_replace($content, $adcode, $inj_positions[$adnum], 0);
491
+ ++$adinj_total_rand_ads_used;
492
+ ++$adinj_total_all_ads_used;
493
+ }
494
+
495
+ return adinj($content_adfree_header.$content.$content_adfree_footer, "Ads injected: " . $debug);
496
+ }
497
+
498
+ function adinj_split_by_tag($content, $tag, &$debugtags=false){
499
+ $ret = array();
500
+ if (strpos($content, $tag) !== false){
501
+ if ($debugtags !== false) $debugtags .= "$tag, ";
502
+ $content_split = explode($tag, $content, 2);
503
+ $ret[] = $content_split[0];
504
+ if (count($content_split) == 2){
505
+ $ret[] = $content_split[1];
506
+ }
507
+ }
508
+ return $ret;
509
+ }
510
+
511
+ function adinj_num_rand_ads_to_insert($content_length){
512
+ global $adinj_total_rand_ads_used; // a page can be more than one post
513
+ $options = adinj_options();
514
+ if (is_single() || is_page()){
515
+ $max_num_rand_ads_to_insert = $options['max_num_of_ads'] - $adinj_total_rand_ads_used;
516
+ } else if (is_home()){
517
+ $max_num_rand_ads_to_insert = $options['max_num_of_ads_home_page'] - $adinj_total_rand_ads_used;
518
+ } else {
519
+ die('Something badly wrong in num_rand_ads_to_insert');
520
+ }
521
+ if ($max_num_rand_ads_to_insert <= 0) {
522
+ return 0;
523
+ }
524
+ if(!is_single() && !is_page()) {
525
+ // If there are multiple posts on page only show one ad per post
526
+ // This rule from 'Adsense Injection'.
527
+ return 1;
528
+ }
529
+ $length = $content_length;
530
+ if (adinj_do_rule_if($options['no_random_ads_if_shorter_than'], '>', $length)){
531
+ return 0;
532
+ }
533
+ if (adinj_do_rule_if($options['one_ad_if_shorter_than'], '>', $length)){
534
+ return 1;
535
+ }
536
+ if (adinj_do_rule_if($options['two_ads_if_shorter_than'], '>', $length)){
537
+ return min(2, $max_num_rand_ads_to_insert);
538
+ }
539
+ if (adinj_do_rule_if($options['three_ads_if_shorter_than'], '>', $length)){
540
+ return min(3, $max_num_rand_ads_to_insert);
541
+ }
542
+ return $max_num_rand_ads_to_insert;
543
+ }
544
+
545
+ function adinj_do_rule_if($rule_value, $condition, $content_length){
546
+ if ($rule_value == ADINJ_ALWAYS_SHOW) return true;
547
+ if ($condition == '>'){
548
+ return ($rule_value != ADINJ_RULE_DISABLED && $rule_value > $content_length);
549
+ } else if ($condition == '<'){
550
+ return ($rule_value != ADINJ_RULE_DISABLED && $rule_value < $content_length);
551
+ } else {
552
+ die("adinj_do_rule_if bad condition: $condition");
553
+ }
554
+ }
555
+
556
+ function adinj_ticked($option){
557
+ $options = adinj_options();
558
+ if (!empty($options[$option])) return 'checked="checked"';
559
+ return false;
560
+ }
561
+
562
+ // Content injection
563
+ add_action('wp_enqueue_scripts', 'adinj_addsevjs_hook');
564
+ add_action('wp_footer', 'adinj_print_referrers_hook');
565
+ add_filter('the_content', 'adinj_inject_hook');
566
+
567
+ ?>
adinj-sev.js ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ Part of the Ad Injection plugin for WordPress
3
+ http://www.reviewmylife.co.uk/blog/
4
+ */
5
+
6
+ // Based on code from
7
+ // http://www.scratch99.com/2008/09/avoid-smart-pricing-show-adsense-only-to-search-engine-visitors/
8
+ function adinj_searchenginevisitor(){
9
+ if (adinj_searchEngineCookieSet()){
10
+ return true;
11
+ }
12
+ var searchengines = adinj_searchengines();
13
+ for (var i = 0; i <= searchengines.length-1; i++) {
14
+ if (document.referrer.indexOf(searchengines[i])!== -1) {
15
+ var expiry = new Date();
16
+ expiry.setTime(expiry.getTime() + 1000*60*60); // 1 hour
17
+ document.cookie = "adinj=1; expires=" + expiry.toGMTString() + "; path=/; ";
18
+ return true;
19
+ }
20
+ }
21
+ return false;
22
+ }
23
+
24
+ function adinj_searchengines(){
25
+ if (typeof adinj_referrers != 'undefined'){
26
+ return adinj_referrers;
27
+ } else {
28
+ document.write("<!--ADINJ DEBUG: couldn't find adinj_referrers value. Using defaults.//-->");
29
+ return new Array('.google.', '.bing.', '.yahoo.', '.ask.', 'search?', 'search.', '/search/');
30
+ }
31
+ }
32
+
33
+ function adinj_searchEngineCookieSet() {
34
+ var results = document.cookie.match('(^|;) ?adinj=([^;]*)(;|$)');
35
+ if(results){
36
+ return true;
37
+ }
38
+ return false;
39
+ }
40
+
adshow.php ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Part of the Ad Injection plugin for WordPress
4
+ http://www.reviewmylife.co.uk/blog/
5
+ */
6
+
7
+ include_once('ad-injection-config.php');
8
+
9
+ //////////////////////////////////////////////////////////////////////////////
10
+
11
+ if (!function_exists('adshow_display_ad_file')){
12
+ function adshow_display_ad_file($adfile){
13
+ $ad_path = dirname(__FILE__).'/ads/'.$adfile;
14
+ adshow_display_ad_full_path($ad_path);
15
+ }
16
+ }
17
+
18
+ if (!function_exists('adshow_display_ad_full_path')){
19
+ function adshow_display_ad_full_path($ad_path){
20
+ $showads = adshow_show_adverts();
21
+ if ($showads !== true){
22
+ if (adinj_config_debug_mode()){
23
+ echo "<!--ADINJ DEBUG: ad blocked at run time reason=$showads-->";
24
+ }
25
+ return;
26
+ }
27
+ if (file_exists($ad_path)){
28
+ $ad = file_get_contents($ad_path);
29
+ if ($ad === false) die("could not read from file: $ad_path");
30
+ if (stripos($ad_path, 'random_1.txt') > 0){ // TODO something better than this
31
+ echo adinj_config_add_tags_rnd(adshow_eval_php($ad));
32
+ } else if (stripos($ad_path, 'top_1.txt') > 0){
33
+ echo adinj_config_add_tags_top(adshow_eval_php($ad));
34
+ } else if (stripos($ad_path, 'bottom_1.txt') > 0){
35
+ echo adinj_config_add_tags_bottom(adshow_eval_php($ad));
36
+ } else {
37
+ echo adshow_eval_php($ad);
38
+ }
39
+ } else {
40
+ echo "ADINJ WARNING: file does not exist: $ad_path";
41
+ }
42
+ }
43
+ }
44
+
45
+ //////////////////////////////////////////////////////////////////////////////
46
+
47
+ if (!function_exists('adshow_fromasearchengine')){
48
+ function adshow_fromasearchengine(){
49
+ $referrer = $_SERVER['HTTP_REFERER'];
50
+ $searchengines = adinj_config_search_engine_referrers();
51
+ foreach ($searchengines as $se) {
52
+ if (stripos($referrer, $se) !== false) {
53
+ return true;
54
+ }
55
+ }
56
+ // Also return true if the visitor has recently come from a search engine
57
+ // and has the adinj cookie set.
58
+ return ($_COOKIE["adinj"]==1);
59
+ }
60
+ }
61
+
62
+ if (!function_exists('adshow_blocked_ip')){
63
+ function adshow_blocked_ip(){
64
+ $visitorIP = $_SERVER['REMOTE_ADDR'];
65
+ return in_array($visitorIP, adinj_config_blocked_ips());
66
+ }
67
+ }
68
+
69
+ if (!function_exists('adshow_show_adverts')){
70
+ function adshow_show_adverts(){
71
+ if (adshow_blocked_ip()) return "blockedip";
72
+ if (adinj_config_sevisitors_only()){
73
+ if (!adshow_fromasearchengine()) return "referrer";
74
+ }
75
+ return true;
76
+ }
77
+ }
78
+
79
+ // From: Exec-PHP plugin
80
+ if (!function_exists('adshow_eval_php')){
81
+ function adshow_eval_php($code) {
82
+ ob_start();
83
+ eval("?>$code<?php ");
84
+ $output = ob_get_contents();
85
+ ob_end_clean();
86
+ return $output;
87
+ }
88
+ }
89
+
90
+ //////////////////////////////////////////////////////////////////////////////
91
+
92
+ ?>
readme.txt ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Ad Injection ===
2
+ Contributors: reviewmylife
3
+ Donate link: http://www.reviewmylife.co.uk/blog/2010/12/06/ad-injection-plugin-wordpress/
4
+ Tags: ad injection, advert injection, advert, ad, injection, adsense, advertising, affiliate, inject, injection, insert, monetize, monetise, banner, Amazon, ClickBank, TradeDoubler, Google, adBrite, post, WordPress, automatically, plugin, free
5
+ Requires at least: 3.0.0
6
+ Tested up to: 3.0.2
7
+ Stable tag: 0.8.3
8
+
9
+ Injects any kind of advert into existing WordPress posts. Can restrict who sees the ads by post age, visitor referrer, IP address. Cache compatible.
10
+
11
+ == Description ==
12
+
13
+ It injects any kind of advert (e.g. Google AdSense, Amazon Associates, ClickBank, TradeDoubler, etc) into the existing content of your WordPress posts and pages. You can control the number of adverts based on the post length, and it can restrict who sees adverts by post age, visitor referrer and IP address. And the dynamic restrictions (by IP and referrer) work with WP Super Cache!
14
+
15
+ Note: This version (0.8.3) has a bug whereby when you upgrade to the next version your ad code will be lost. Please back up your ad code before you upgrade. I am working on a fix, but unfortunately the data loss will occur if you have already installed 0.8.3. I hope to have submitted a fix later today (7th December 2010).
16
+
17
+ = Automatic advert injection =
18
+
19
+ The ads can be injected into existing posts without requiring any modification of the post. The injection can be done randomly between paragraphs, and there is an option to always inject the first advert after the first paragraph. Two separate adverts can be defined for the top and bottom of the content. Randomly positioning the adverts helps to reduce 'ad blindness'.
20
+
21
+ = Ad quantity by post length =
22
+
23
+ The number of adverts can be set based on the length of the post. It is a good idea for longer posts to have more adverts than shorter posts for example. Adverts can also be turned off for very short posts.
24
+
25
+ = Search engines only =
26
+
27
+ You can specify that ads should only be shown to search engine visitors (or from any other referring websites) so that your regular visitors (who are unlikely to click your ads) get a better experience of your site. You can define which search engines or any other referring sites see your adverts. A visitor who enters the site by a search engine will see ads for the next hour.
28
+
29
+ = Ads on old posts only =
30
+
31
+ Adverts can be restricted to posts that are more than a defined numbers of days old. This prevents your regular visitors from having to see your ads.
32
+
33
+ = Block ads from IP addresses =
34
+
35
+ IP addresses of people who shouldn't see your ads can be defined. These could be the IP addresses of your friends, family, or even yourself.
36
+
37
+ = Not tied to any ad provider =
38
+
39
+ The advert code can be copied and pasted directly from your ad provider (Google AdSense, adBrite, ClickBank, etc) which will help you to comply with any terms of service (TOS) that state their ad code may not be modified.
40
+
41
+ = Flexible ad positioning =
42
+
43
+ Easy positioning options are provided for left, right, center, float left, and float right. Extra spacing can be set above and below the ad. Or if that isn't flexible enough, you can write your own positioning code using HTML and CSS.
44
+
45
+ = Works with WP Super Cache =
46
+
47
+ The dynamic features that require code to be executed for each page view (i.e. search engine visitors only, and ad blocking based on IP address) work with WP Super Cache! This plugin will automatically use the dynamic mfunc tag to ensure that the dynamic ad features still work when caching is on. Displaying the adverts (even with the dynamic restrictions) whilst caching with WP Super Cache requires no MySQL database access. Note: WP Super Cache must be configured in 'Legacy' mode for the dynamic features to work.
48
+
49
+ = Inject PHP and JavaScript =
50
+
51
+ As the plugin will inject whatever content you like into the page you can write your own ad rotation or a/b split testing code for the ads you inject. PHP code can be automatically executed, even when using WP Super Cache.
52
+
53
+ = Hide UI panels that you don't need =
54
+
55
+ If there are any panels on the admin screen that you don't need, you can click on the show/hide button to hide them until you need them.
56
+
57
+ == Installation ==
58
+
59
+ This section describes how to install the plugin and get it working.
60
+
61
+ 1. Upload the ad-injection folder to the '/wp-content/plugins/' directory. The plugin must be in a folder called 'ad-injection'. So the main plugin file will be at /wp-content/plugins/ad-injection/ad-injection.php
62
+ 2. Activate the plugin through the 'Plugins' menu in WordPress.
63
+ 3. Configure your ads. Make sure you select the option to say which ad injection mode to use. You need to say whether you are using WP Super Cache (or compatible) or not. Dynamic features (referrer and IP ad filtering) will only work with either 1) WP Super Cache (or compatible) or 2) no caching plugin.
64
+ 4. Tick the box right at the top to enable your ads.
65
+ 5. If you are using a caching plugin you may need to clear the cache to see your ads immediately.
66
+
67
+ == Frequently Asked Questions ==
68
+
69
+ = Why was this plugin created? =
70
+
71
+ I used to use the excellent Adsense Injection by Dax Herrera, but found I needed more features and flexibility. His plugin inspired this one.
72
+
73
+ = How is this plugin different to Adsense Injection by Dax Herrera? =
74
+
75
+ One a basic level it can do the same job as Dax's excellent Adsense Injection. If you want it can just inject Adsense like his plugin does. I used to use his plugin, but found that I wanted a lot more features. Here is a quick list of the extra features.
76
+
77
+ * Inject any type of advert from any ad provider.
78
+ * Restrict ad display by referrer (e.g. can restrict display to search engine visitors).
79
+ * Can prevent specific IP addresses from seeing adverts.
80
+ * Can define randomly positioned adverts, and adverts at the top and bottom of the posts.
81
+ * Vary number of adverts based on post length.
82
+ * You can inject raw JavaScript and PHP.
83
+ * The dynamic features (restricting ads by referrer and IP) work with WP Super Cache.
84
+ * Compatible with the <!--noadsense--> <!--adsensestart--> in-page tags from Adsense Injection to make migration easy.
85
+ * Compatible with in-page tags from Whydowork Adsense and Quick Adsense.
86
+ * Extra positioning options - for example you can force the first advert to be right after the first paragraph so that it will be 'above the fold'.
87
+
88
+ Thanks to Dax by the way for providing the inspiration for this plugin!
89
+
90
+ = Do I need to have WP Super Cache installed? =
91
+
92
+ No! All the features of this plugin will work with no caching plugin installed. But if you do have WP Super Cache the dynamic features (enabling ads based on IP address and referrer) will still work. And your blog will run a lot faster than with no caching plugin. Usually a caching plugin would prevent dynamic plugin features from working. Just make sure you have WP Super Cache configured in 'Legacy' mode and that you choose the option on Ad Injection to say that you are using WP Super Cache.
93
+
94
+ = Will the dynamic features work with other caching plugins? =
95
+
96
+ Potentially - if they are compatible with WP Super Cache's mfunc tags. I'm guessing they will work with the original WP Cache as I think the mfunc tags in WP Super Cache were inherited from here. However I haven't yet tested this. W3 Total Cache does have support for some kind of mfunc tags, but I haven't tested to see if they are compatible with the WP Super Cache ones. Do let me know!
97
+
98
+ = What if I am using an incompatible caching plugin? =
99
+
100
+ Don't worry - everything will still work except for:
101
+
102
+ 1. Filtering ads based on the IP address of the visitor.
103
+ 2. Filtering the ads based on the HTTP referrer of the visitor.
104
+
105
+ If you aren't interested in these features then it doesn't matter! Just make sure you tick the box to say that you to use 'Direct static ad insertion' on the Ad Injection settings screen.
106
+
107
+ = Are there any known plugin conflicts? =
108
+
109
+ If you use WP Minify and WP Super Cache in combination with this plugin, you'll need to turn off the HTML minification in WP Minify. This is because HTML minification strips out the mfunc tags that Ad Injection uses. You can leave the CSS and JavaScript minification on if you already use them.
110
+
111
+ = Some technical details =
112
+
113
+ * Plugin stores all its settings in a single option (adinj_options).
114
+ * Uninstall support is provided to delete this option if you uninstall the plugin.
115
+ * Admin code is separated into a separate file so it is not loaded when your visitors view your pages.
116
+ * When used with WP Super Cache the plugin loads its dynamic settings from a static PHP file so no MySQL database queries are required.
117
+ * The ads are saved as text files into the plugin folder. The plugin will therefore need write access to its own folder.
118
+ * The JavaScript for setting the referrer cookie is inserted using wp_enqueue_scripts.
119
+ * If there is anything I can do better please let me know - this is my first plugin so I still have a lot to learn!
120
+
121
+ == Troubleshooting ==
122
+
123
+ Here are some things to check if the ads are not appearing, or are appearing when you think they shouldn't.
124
+
125
+ 1. Have you clicked the box to enable your ads?
126
+ 2. Would the options you have selected allow the ads to appear on that page?
127
+ 3. Have you cleared your cache (if you are using a caching plugin) to make sure that the page has the ads injected into it?
128
+ 4. If you still aren't sure why the ads aren't there (or why they are), click 'Enable debug mode'. Make sure the page gets regenerated (either by reloading, or by clearing the cache and reloading). The search the source code of the page (the HTML) for 'ADINJ DEBUG' tags. This will give you information about the decisions that the plugin made.
129
+ 5. Have you selected the correct insertion mode in the 'Ad insertion mode' section?
130
+ 6. The plugin inserts adverts after the closing HTML paragraph tag </p>. If the ads aren't appearing where you expect, check where your </p> tags are.
131
+
132
+ If you are using WP Super Cache.
133
+
134
+ 1. Have you enabled the WP Super Cache 'mfunc' mode? (in the Ad insertion mode and dynamic ad display restrictions pane)
135
+ 2. Are your WP Super Cache settings correct? It must be in 'Legacy' mode.
136
+ 3. If you are using WP Minify as well then turn off the HTML minification as this strips out the mfunc tags that Ad Injection uses to check if the adverts should be inserted.
137
+
138
+ If you are getting errors when using the plugin check the following.
139
+
140
+ 1. Is there an 'ads' directory in the plugin directory? The path will probably be: '/wp-content/plugins/ad-injection/ads/'. If not create the ads directory and make sure it is writeable by the plugin (chmod 0755 will do, chmod 0750 is better).
141
+ 2. Are there text files in the ads directory? The ad code that you enter into the ad boxes should get saved in text files in the ads directory.
142
+ 3. Has the config file been created? It should be at '/wp-content/plugins/ad-injection/ad-injection-config.php'. If not make sure the '/wp-content/plugins/ad-injection/' directory is writeable (chmod 0750 is best, chmod 0755 will do).
143
+
144
+ If you do get any errors please use the 'Report a bug or give feedback' link on the plugin to send me the error details. If things go so badly wrong that you can't even get to the settings page please send me an email via [this contact form](http://www.reviewmylife.co.uk/blog/contact-us/ "contact form").
145
+
146
+ For more information visit [reviewmylife](http://www.reviewmylife.co.uk/blog/2010/12/06/ad-injection-plugin-wordpress/ "reviewmylife blog").
147
+
148
+ == Screenshots ==
149
+
150
+ 1. Easy to use interface which allows you to copy and paste your ad code directly from your ad provider. Options are provided to control when and where your ads appear.
151
+ 2. The ads are automatically injected into the pages of your blog.
152
+ 3. Can choose to show the ads only to search engine visitors, or define IP addresses that ads aren't shown to.
153
+
154
+ == Changelog ==
155
+
156
+ = 0.8.3 =
157
+ * First public release
158
+
159
+ == Upgrade Notice ==
160
+
161
+ = 0.8.3 =
162
+ First public release.
163
+
rml-micro-logo.png ADDED
Binary file
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
screenshot-3.png ADDED
Binary file
uninstall.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Part of the Ad Injection plugin for WordPress
4
+ http://www.reviewmylife.co.uk/blog/
5
+ */
6
+
7
+ if( !defined( 'ABSPATH') && !defined('WP_UNINSTALL_PLUGIN') )
8
+ exit();
9
+
10
+ delete_option('adinj_options');
11
+
12
+ ?>