PluginProbe
Quick Adsense / 1.3
Quick Adsense v1.3
2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1 2.9.2 2.9.4 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 1.6 1.7 1.8 1.8.1 1.8.2 All 41 releases
quick-adsense / quick-adsense.php

quick-adsense.php in Quick Adsense 1.3, at quick-adsense.php

348 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Plugin Name: Quick Adsense
5 Plugin URI: http://techmilieu.com/quick-adsense
6 Description: Quick Adsense offers a quicker & flexible way to insert Google Adsense or any Ads code into a blog post.
7 Author: Philip Ze
8 Version: 1.3
9 Author URI: http://techmilieu.com/
10 */
11
12 /* Copyright 2009 Philip Ze [ http://techmilieu.com/quick-adsense ]
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 as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29
30
31
32 global $QData;
33
34 $QData['AdsWid'] = 3; /* Ads on Widget */
35 $QData['Ads'] = 10; /* Ads on Post body */
36 $QData['Name'] = 'Quick Adsense';
37 $QData['Version'] = '1.3';
38 $QData['URI'] = 'http://techmilieu.com/quick-adsense';
39 $QData['AdsWidName'] = 'AdsWidget%d (Quick Adsense)';
40 $QData['Default'] = array(
41 'AdsDisp'=>'3',
42 'BegnAds'=>true,'BegnRnd'=>'1','EndiAds'=>true,'EndiRnd'=>'0',
43 'AppPost'=>true,'AppPage'=>true,'AppHome'=>false,'AppCate'=>false,'AppArch'=>false,'AppTags'=>false,'AppMaxA'=>false,
44 'QckTags'=>true
45 );
46 $QData['DefaultAdsOpt'] = array(
47 'AdsMargin'=>'10','AdsAlign'=>'2'
48 );
49 $QData['DefaultAdsName'] = array();
50 for ($i=1;$i<=$QData['Ads'];$i++) {
51 array_push($QData['DefaultAdsName'], 'AdsCode'.$i );
52 array_push($QData['DefaultAdsName'], 'AdsAlign'.$i );
53 array_push($QData['DefaultAdsName'], 'AdsMargin'.$i );
54 };
55 for ($i=1;$i<=$QData['AdsWid'];$i++) {
56 array_push($QData['DefaultAdsName'], 'WidCode'.$i );
57 };
58
59 function ads_admin_page_inc() {
60 include('quick-adsense-admin.php');
61 }
62 function ads_admin_page() {
63 add_options_page("Quick Adsense Options", "Quick Adsense", 1, basename(__FILE__), "ads_admin_page_inc");
64 }
65 function register_ads_settings() {
66 global $QData;
67 foreach ($QData['Default'] as $key => $value) {
68 register_setting( 'qa-options', $key);
69 }
70 foreach ($QData['DefaultAdsName'] as $key => $value) {
71 register_setting( 'qa-options', $value);
72 }
73 }
74 if(is_admin()) {
75 add_action('admin_menu', 'ads_admin_page');
76 add_action('admin_init', 'register_ads_settings');
77 }
78
79
80 function ads_plugin_links($links,$file) {
81 if($file==plugin_basename(__FILE__)) {
82 array_unshift($links,'<a href="options-general.php?page='.basename(__FILE__).'">'.__('Setting').'</a>');
83 }
84 return $links;
85 }
86 add_filter('plugin_action_links','ads_plugin_links',10,2);
87
88
89 function plugin_activated() {
90 global $QData;
91 $isold = get_option('AdsDisp');
92 if ( !$isold && is_bool($isold) ) {
93 foreach ($QData['Default'] as $key => $value) {
94 update_option($key , $value);
95 }
96 for ($i=1;$i<=$QData['Ads'];$i++) {
97 update_option('AdsMargin'.$i, $QData['DefaultAdsOpt']['AdsMargin']);
98 update_option('AdsAlign'.$i, $QData['DefaultAdsOpt']['AdsAlign']);
99 }
100 }
101 }
102 register_activation_hook( __FILE__, 'plugin_activated' );
103
104
105 function ads_head_java() {
106 global $QData;
107 $tp = get_option('QckTags');
108 if ($tp) { ?>
109 <script type="text/javascript">
110 if(typeof(edButtons)!='undefined') {
111 for(i=1;i<=<?php echo($QData['Ads']) ?>;i++) {
112 edButtons[edButtons.length]=new edButton("ads"+i.toString(),"Ads"+i.toString(),"\n<!--Ads"+i.toString()+"-->\n","","",-1);
113 }
114 edButtons[edButtons.length]=new edButton("random_ads","RndAds","\n<!--RndAds-->\n","","",-1);
115 edButtons[edButtons.length]=new edButton("no_ads","NoAds","\n<!--NoAds-->\n","","",-1);
116 edButtons[edButtons.length]=new edButton("off_ads","OffAds","\n<!--OffAds-->\n","","",-1);
117 };
118 </script>
119 <?php }
120 }
121 add_action('admin_head', 'ads_head_java');
122
123
124 $ShownAds = 0;
125 $AdsId = array();
126 $beginend = 0;
127
128 function process_content($content)
129 {
130 global $QData;
131 global $ShownAds;
132 global $AdsId;
133 global $beginend;
134
135 /* verifying */
136 if( (strpos($content,'<!--NoAds-->')) ||
137 (strpos($content,'<!--OffAds-->')) ||
138 (is_single() && !(get_option('AppPost'))) ||
139 (is_page() && !(get_option('AppPage'))) ||
140 (is_home() && !(get_option('AppHome'))) ||
141 (is_category() && !(get_option('AppCate'))) ||
142 (is_archive() && !(get_option('AppArch'))) ||
143 (is_tag() && !(get_option('AppTags'))) ) {
144 $content = clean_tags($content); return $content;
145 }
146
147 $AdsToShow = get_option('AdsDisp');
148 for($i=1;$i<=$QData['AdsWid'];$i++) {
149 $wadsid = sanitize_title(sprintf($QData['AdsWidName'],$i));
150 $AdsToShow -= (is_active_widget(true, $wadsid)) ? 1 : 0 ;
151 }
152 if( $ShownAds >= $AdsToShow ) { $content = clean_tags($content); return $content; };
153
154 if( !count($AdsId) ) {
155 for($i=1;$i<=$QData['Ads'];$i++) {
156 $tmp = trim(get_option('AdsCode'.$i));
157 if( !empty($tmp) ) {
158 array_push($AdsId, $i);
159 }
160 }
161 }
162 if( !count($AdsId) ) { $content = clean_tags($content); return $content; };
163
164 /* ... Tidy up content ... */
165 $AdsIdCus = array();
166 $cusads = 'CusAds'; $cusrnd = 'CusRnd';
167 $begn1 = get_option('BegnAds'); $begn2 = get_option('BegnRnd');
168 $endi1 = get_option('EndiAds'); $endi2 = get_option('EndiRnd');
169 if ( $begn2 == 0 ) { $b1 = $cusrnd; } else { $b1 = $cusads.$begn2; array_push($AdsIdCus, $begn2); };
170 if ( $endi2 == 0 ) { $b2 = $cusrnd; } else { $b2 = $cusads.$endi2; array_push($AdsIdCus, $endi2); };
171 if( $begn1 && $endi1 ) {
172 $content = '<!--'.$b1.'-->'.$content.'<!--'.$b2.'-->' ;
173 } else if( $begn1 ) {
174 $content = '<!--'.$b1.'-->'.$content;
175 } else if( $endi1 ) {
176 $content = $content.'<!--'.$b1.'-->';
177 }
178 $content = '<!--EmptyClear-->'.$content.PHP_EOL.'<div style="font-size:0px;height:0px;line-height:0px;margin:0;padding:0;clear:both"></div>';
179 $content = clean_tags($content, true);
180 $ismany = (!is_single() && !is_page());
181 $showall = get_option('AppMaxA');
182
183 /* ... Replace Beginning/End Ads1-10 ... */
184 for( $i=1; $i<=count($AdsIdCus); $i++ ) {
185 if( $showall || !$ismany || $beginend != $i ) {
186 if( strpos($content, '<!--'.$cusads.$AdsIdCus[$i-1].'-->') && in_array($AdsIdCus[$i-1], $AdsId)) {
187 $content = replace_ads( $content, $cusads.$AdsIdCus[$i-1], $AdsIdCus[$i-1] ); $AdsId = del_element($AdsId, array_search($AdsIdCus[$i-1], $AdsId)) ;
188 $ShownAds += 1; if( $ShownAds >= $AdsToShow || !count($AdsId) ){ $content = clean_tags($content); return $content; };
189 $beginend = $i; if(!$showall && $ismany){break;}
190 }
191 }
192 }
193 /* ... Replace Ads1 to Ads10 ... */
194 if( $showall || !$ismany ) {
195 $tcn = count($AdsId); $tt = 0;
196 for( $i=1; $i<=$tcn; $i++ ) {
197 if( strpos($content, '<!--Ads'.$AdsId[$tt].'-->') ) {
198 $content = replace_ads( $content, 'Ads'.$AdsId[$tt], $AdsId[$tt] ); $AdsId = del_element($AdsId, $tt) ;
199 $ShownAds += 1; if( $ShownAds >= $AdsToShow || !count($AdsId) ){ $content = clean_tags($content); return $content; };
200 } else {
201 $tt += 1;
202 }
203 }
204 }
205 /* ... Replace Beginning/End random Ads ... */
206 if( strpos($content, '<!--'.$cusrnd.'-->') && ($showall || !$ismany) ) {
207 $tcx = count($AdsId);
208 $tcy = substr_count($content, '<!--'.$cusrnd.'-->');
209 for( $i=$tcx; $i<=$tcy-1; $i++ ) {
210 array_push($AdsId, -1);
211 }
212 shuffle($AdsId);
213 for( $i=1; $i<=$tcy; $i++ ) {
214 $content = replace_ads( $content, $cusrnd, $AdsId[0] ); $AdsId = del_element($AdsId, 0) ;
215 $ShownAds += 1; if( $ShownAds >= $AdsToShow || !count($AdsId) ){ $content = clean_tags($content); return $content; };
216 }
217 }
218 /* ... Replace RndAds ... */
219 if( strpos($content, '<!--RndAds-->') && ($showall || !$ismany) ) {
220 $AdsIdTmp = array();
221 shuffle($AdsId);
222 for( $i=1; $i<=$AdsToShow-$ShownAds; $i++ ) {
223 if( $i <= count($AdsId) ) {
224 array_push($AdsIdTmp, $AdsId[$i-1]);
225 }
226 }
227 $tcx = count($AdsIdTmp);
228 $tcy = substr_count($content, '<!--RndAds-->');
229 for( $i=$tcx; $i<=$tcy-1; $i++ ) {
230 array_push($AdsIdTmp, -1);
231 }
232 shuffle($AdsIdTmp);
233 for( $i=1; $i<=$tcy; $i++ ) {
234 $tmp = $AdsIdTmp[0];
235 $content = replace_ads( $content, 'RndAds', $AdsIdTmp[0] ); $AdsIdTmp = del_element($AdsIdTmp, 0) ;
236 if($tmp != -1){$ShownAds += 1;}; if( $ShownAds >= $AdsToShow || !count($AdsIdTmp) ){ $content = clean_tags($content); return $content; };
237 }
238 }
239
240 /* ... That's it. DONE :) ... */
241 $content = clean_tags($content); return $content;
242 }
243 function clean_tags($content, $trimonly = false) {
244 global $QData;
245 $tagnames = array('EmptyClear','RndAds','NoAds','OffAds','CusRnd');
246 for($i=1;$i<=$QData['Ads'];$i++) { array_push($tagnames, 'CusAds'.$i); array_push($tagnames, 'Ads'.$i); };
247 foreach ($tagnames as $tgn) {
248 if(strpos($content,'<!--'.$tgn.'-->') || $tgn=='EmptyClear') {
249 if($trimonly) {
250 $content = str_replace('<p><!--'.$tgn.'--></p>', '<!--'.$tgn.'-->', $content);
251 }else{
252 $content = str_replace(array('<p><!--'.$tgn.'--></p>','<!--'.$tgn.'-->'), '', $content);
253 }
254 }
255 }
256 return $content;
257 }
258 function replace_ads($content, $nme, $adn) {
259 if( !strpos($content,'<!--'.$nme.'-->') ) { return $content; }
260 global $QData;
261 if ($adn != -1) {
262 $arr = array('',
263 'float:left;margin:%1$dpx %1$dpx %1$dpx 0;',
264 'float:none;margin:%1$dpx 0 %1$dpx 0;text-align:center;',
265 'float:right;margin:%1$dpx 0 %1$dpx %1$dpx;',
266 'float:none;margin:0px;');
267 $adsalign = get_option('AdsAlign'.$adn);
268 $adsmargin = get_option('AdsMargin'.$adn);
269 $style = sprintf($arr[(int)$adsalign], $adsmargin);
270 $adscode = get_option('AdsCode'.$adn);
271 $adscode =
272 PHP_EOL.'<!-- '.$QData['Name'].' Wordpress Plugin: '.$QData['URI'].' -->'.PHP_EOL.
273 '<div style="'.$style.'">'.PHP_EOL.
274 $adscode.PHP_EOL.
275 '</div>'.PHP_EOL;
276 } else {
277 $adscode ='';
278 }
279 $cont = explode('<!--'.$nme.'-->', $content, 2);
280 return $cont[0].$adscode.$cont[1];
281 }
282 function del_element($array, $idx) {
283 $copy = array();
284 for( $i=0; $i<count($array) ;$i++) {
285 if ( $idx != $i ) {
286 array_push($copy, $array[$i]);
287 }
288 }
289 return $copy;
290 }
291 add_filter('the_content', 'process_content');
292
293
294 function ads_widget_register() {
295 global $QData;
296 if (!function_exists('register_sidebar_widget') || !function_exists('register_widget_control')) { return; };
297 for($i=1;$i<=$QData['AdsWid'];$i++) {
298 $displaystr =
299 '$cont = get_the_content("",0,"");'.
300 'if(!strpos($cont,"<!--OffAds-->")) {'.
301 'extract($args);'.
302 '$title = get_option("WidCode-title-'.$i.'");'.
303 '$codetxt = get_option("WidCode'.$i.'");'.
304 'echo $before_widget;'.
305 'if (!empty($title)) { echo $before_title.$title.$after_title; };'.
306 'echo $codetxt;'.
307 'echo $after_widget;'.
308 '}';
309 $controlstr =
310 'if ($_POST["WidCodeSubmit'.$i.'"]) {'.
311 ' update_option_en("WidCode-title-'.$i.'", $_POST["WidCode-title-'.$i.'"], "strip_tags");'.
312 ' update_option_en("WidCode'.$i.'", $_POST["WidCode'.$i.'"]);'.
313 '}'.
314 '$title = get_option_en("WidCode-title-'.$i.'");'.
315 '$codetxt = get_option_en("WidCode'.$i.'");'.
316 'echo "<p><label>'.__('Title:').'</label>";'.
317 'echo "<input style=\"width:100%;\" id=\"WidCode-title-'.$i.'\" name=\"WidCode-title-'.$i.'\" type=\"text\" value=\"".$title."\" />";'.
318 'echo "<p><textarea style=\"width:100%;height:150px;\" id=\"WidCode'.$i.'\" name=\"WidCode'.$i.'\">".$codetxt."</textarea><br\>";'.
319 'echo "<a style=\"font-size:8pt;text-decoration:none\" href=\"options-general.php?page='.basename(__FILE__).'\">'.__('Quick Adsense Setting').'</a></p>";'.
320 'echo "<input type=\"hidden\" id=\"WidCodeSubmit'.$i.'\" name=\"WidCodeSubmit'.$i.'\" value=\"true\" />";';
321 $displaycall[$i] = create_function('$args', $displaystr);
322 $controlcall[$i] = create_function('', $controlstr);
323 }
324 for($i=1;$i<=$QData['AdsWid'];$i++) {
325 $wadsid = sprintf($QData['AdsWidName'],$i);
326 register_sidebar_widget($wadsid, $displaycall[$i]);
327 register_widget_control($wadsid, $controlcall[$i]);
328 }
329 }
330 function get_option_en($nameid)
331 {
332 $txt = get_option($nameid);
333 $txt = htmlspecialchars($txt, ENT_QUOTES);
334 if(!empty($txt)) { return $txt; }else{ return ""; };
335 }
336 function update_option_en($nameid, $text, $opt='')
337 {
338 $txt = stripslashes($text);
339 if ($opt=='strip_tags') { $txt = strip_tags($txt); };
340 update_option($nameid, $txt);
341 if(!empty($txt)) { return $txt; }else{ return ""; };
342 }
343 add_action('init', ads_widget_register);
344
345
346
347 ?>
348