/index.php

  1. <?php 
  2. /** 
  3. Plugin Name: Google Adsense for Responsive Design - GARD 
  4. Plugin URI: http://thedigitalhippies.com/gard 
  5. Description: Allows you to use shortcode to display responsive adsense ads throughout your responsive theme. 
  6. Version: 2.23 
  7. Author: The Plugin Factory, The Digital Hippies 
  8. Author URI: http://thedigitalhippies.com 
  9. */ 
  10.  
  11.  
  12.  
  13. if( is_admin() ) { 
  14.  
  15. function start_gard_now() { 
  16.  
  17. include('adsizes.php'); 
  18.  
  19. define('GARDPLUGINOPTIONS_VER', '2.23'); 
  20. define('GARDPLUGINOPTIONS_ID', 'GARD-plugin-options'); 
  21. define('GARDPLUGINOPTIONS_NICK', 'Google Adsense for Responsive Design'); 
  22. define('GARD_FOLDER', dirname(__FILE__) ); 
  23. define('GARD_PRO_LINK', 'http://thepluginfactory.co/warehouse/gard-pro/'); 
  24. define('GARD_PLUGIN_SUPPORT_URL', 'http://thepluginfactory.co/community/forum/plugin-specific/gard-google-adsense-for-responsive-design/'); 
  25. define('GARD_PLUGIN_SUPPORT_LINK', '<a href="http://thepluginfactory.co/community/forum/plugin-specific/gard-google-adsense-for-responsive-design/" title="GARD Support Forum" target="_blank">GARD Support Forum</a>'); 
  26.  
  27.  
  28. function gard_register() { 
  29. include(dirname(__FILE__) . '/register.php'); 
  30. }  
  31.  
  32. function gard_options_page() {  
  33. include(dirname(__FILE__) . '/options.php'); 
  34.  
  35. function gard_menu() { 
  36. include(dirname(__FILE__) . '/menu.php'); 
  37.  
  38. function gard_help() {  
  39. include(dirname(__FILE__) . '/help.php'); 
  40.  
  41. function gard_pro_settings() { 
  42. include(dirname(__FILE__) . '/pro.php'); 
  43.  
  44. function GARD_ENQUEUE() { 
  45. wp_register_script( 'spectrum_js', plugins_url( '/js/spectrum/spectrum.js', __FILE__ ) ); 
  46. wp_enqueue_script( 'spectrum_js' ); 
  47.  
  48. wp_register_style( 'spectrum_css', plugins_url( '/js/spectrum/spectrum.css', __FILE__ ) ); 
  49. wp_enqueue_style( 'spectrum_css' ); 
  50.  
  51. wp_enqueue_script( 'jquery' ); 
  52.  
  53. function GARD_PRO_plugin_links($links, $file) { 
  54. if ($file == 'google-adsense-for-responsive-design-gard/index.php' ) { 
  55. $links[] = '<a target="_blank" style="color: #cc0000; font-weight: bold;" href="http://thepluginfactory.co/warehouse/gard-pro/?so=gard_manage_plugins">Upgrade to GARD Pro</a>'; 
  56. return $links; 
  57.  
  58. add_action( 'admin_init', 'gard_register' ); 
  59. add_action( 'admin_menu', 'gard_menu' ); 
  60. add_action( 'admin_init', 'GARD_ENQUEUE' ); 
  61. add_filter( 'plugin_row_meta', 'GARD_PRO_plugin_links', 10, 2 ); 
  62.  
  63.  
  64. function gard_restrict_admin() { 
  65.  
  66. if ( ! current_user_can( 'manage_options' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { 
  67. return; 
  68. }  
  69.  
  70. add_action( 'admin_init', 'gard_restrict_admin', 1 ); 
  71. start_gard_now(); 
  72.  
  73. } else { 
  74.  
  75. include('adsizes.php'); 
  76. function gard_ismobile() { 
  77. # Source http://detectmobilebrowsers.com/ 
  78. $useragent=$_SERVER['HTTP_USER_AGENT']; 
  79. if(preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', substr($useragent, 0, 4))) { 
  80. return TRUE; 
  81. } else { 
  82. return FALSE; 
  83.  
  84. function _GARD( $atts = '' ) { 
  85. extract( shortcode_atts( array( 
  86. 'align' => '',  
  87. 'async' => 'false' 
  88. ), $atts ) ); 
  89.  
  90.  
  91. if($align == 'center') { 
  92. $align = 'margin: 0 auto;float: none;display: block;text-align:center;'; 
  93. $float = 'none'; 
  94. $margin = '0 auto'; 
  95. $display = 'block'; 
  96. $text = 'center'; 
  97. } elseif($align == 'left') { 
  98. $align = 'margin: 0 10px 10px 0;float: left;display: block;'; 
  99. $float = 'left'; 
  100. $margin = '0 10px 10px 0'; 
  101. $text = 'initial'; 
  102. $display = 'block'; 
  103. } elseif($align == 'right') { 
  104. $align = 'margin: 0 0 10px 10px;float: right;display: block;'; 
  105. $float = 'right'; 
  106. $margin = '0 0 10px 10px'; 
  107. $text = 'initial'; 
  108. $display = 'block'; 
  109. } elseif ($align == '') { 
  110. $float = ''; 
  111. $margin = ''; 
  112. $display = ''; 
  113. $text = '';  
  114.  
  115. $id = "ca-pub-".get_option("GARD_ID"); 
  116. $num = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4); 
  117.  
  118. $mode = get_option('GARD_ADVANCED_MODE', '1') ; 
  119.  
  120. $async_mode = get_option("GARD_ASYNC"); 
  121.  
  122. if( $mode == 1 && $async_mode !== '1' ) { 
  123.  
  124. # ADVANCED MODE 
  125.  
  126. $link_units = array( 
  127. '728x15' => '728x15',  
  128. '468x15' => '468x15',  
  129. '200x90' => '200x90',  
  130. '180x90' => '180x90',  
  131. '160x90' => '160x90',  
  132. '120x90' => '120x90',  
  133. ); 
  134.  
  135.  
  136. $adsense = '  
  137. <div class="GARD gard_advanced_mode" id="google-ads-'.$num.'"><script> 
  138. adUnit = document.getElementById("google-ads-'.$num.'"); 
  139. google_ad_client = "'.$id.'"; 
  140. adUnit = document.getElementById("google-ads-'.$num.'"); 
  141. adWidth = adUnit.offsetWidth;  
  142. if ( adWidth >= 999999 ) { 
  143. /* GETTING THE FIRST IF OUT OF THE WAY */  
  144. }'; 
  145.  
  146. global $adsizes; 
  147.  
  148. $link_units = array( 
  149. '728x15' => '728x15',  
  150. '468x15' => '468x15',  
  151. '200x90' => '200x90',  
  152. '180x90' => '180x90',  
  153. '160x90' => '160x90',  
  154. '120x90' => '120x90',  
  155. ); 
  156.  
  157. foreach($adsizes as $size => $description) { 
  158. $type = 'ad_unit'; 
  159. if ( in_array($size, $link_units) ) { 
  160. $type = 'link_unit'; 
  161. } elseif ($size == '300x600') { 
  162. $type = 'large_skyscraper'; 
  163.  
  164. $tag = $type."_".$size; 
  165. $code = get_option("GARD_".$size); 
  166. $original_size = $size; 
  167. $size = explode('x', $size); 
  168. $size1 = $size[0]; 
  169. $size2 = $size[1]; 
  170.  
  171. $mobile = get_option('GARD_MOBILE_ADVANCED_'.$original_size); 
  172.  
  173. if( $mobile == 1 && gard_ismobile() === FALSE ) { 
  174. // If ad is set to show to mobile only, and this is not a mobile device, skip the output of this ad 
  175. continue; 
  176. } elseif( $mobile == 2 && gard_ismobile() === TRUE ) { 
  177. // If ad is set to show to non-mobile only, and this is a mobile device, skip the output of this ad 
  178. continue; 
  179.  
  180. if ( strlen($code) == '10' && is_numeric($code)) {  
  181. $adsense .= ' else if ( adWidth >= '.$size1.' ) { 
  182. if (document.querySelectorAll(".'.$type.'").length > 3 ) {  
  183. google_ad_slot = "0"; 
  184. adUnit.style.display = "none"; 
  185. } else { 
  186. google_ad_slot = "'.$code.'"; 
  187. google_ad_width = '.$size1.'; 
  188. google_ad_height = '.$size2.'; 
  189. adUnit.style.cssFloat = "'.$float.'"; 
  190. adUnit.style.styleFloat = "'.$float.'"; 
  191. adUnit.style.margin = "'.$margin.'"; 
  192. adUnit.style.textAlign = "'.$text.'"; 
  193. adcount = document.querySelectorAll(".'.$type.'").length; 
  194. tag = "'.$tag.'_"+adcount; 
  195. adUnit.className = adUnit.className + " '.$type.' " + tag; 
  196. }'; 
  197.  
  198. $adsense .= ' else { 
  199. google_ad_slot = "0"; 
  200. adUnit.style.display = "none"; 
  201. }</script> 
  202. <script src="//pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>'; 
  203.  
  204. } else if ( $mode == 1 && $async_mode == 1 ) { 
  205.  
  206. # ASYNC MODE 
  207.  
  208. $adsense = '<div class="GARD gard_async_mode" id="google-ads-'.$num.'" ><script async="async" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><div id="GARDasync_'.$num.'"></div><script >(adsbygoogle = window.adsbygoogle || []).push({});</script></div>'; 
  209.  
  210. # determine size and insert correct ad. 
  211. $adsense .= '<script> 
  212. adUnit = document.getElementById("google-ads-'.$num.'"); 
  213. adWidth = adUnit.offsetWidth;  
  214. if ( adWidth >= 999999 ) { 
  215. /* GETTING THE FIRST IF OUT OF THE WAY */  
  216. }'; 
  217.  
  218. global $adsizes; 
  219. foreach($adsizes as $size => $description) { 
  220. $code = get_option("GARD_".$size); 
  221. $original_size = $size; 
  222. $size = explode('x', $size); 
  223. $size1 = $size[0]; 
  224. $size2 = $size[1]; 
  225.  
  226. $mobile = get_option('GARD_MOBILE_ADVANCED_'.$original_size); 
  227.  
  228. if( $mobile == 1 && gard_ismobile() === FALSE ) { 
  229. // If ad is set to show to mobile only, and this is not a mobile device, skip the output of this ad 
  230. continue; 
  231. } elseif( $mobile == 2 && gard_ismobile() === TRUE ) { 
  232. // If ad is set to show to non-mobile only, and this is a mobile device, skip the output of this ad 
  233. continue; 
  234.  
  235. if ( strlen($code) == '10' && is_numeric($code)) {  
  236. $adsense .= ' else if ( adWidth >= '.$size1.' ) { 
  237. document.getElementById("GARDasync_'.$num. 
  238. '").innerHTML = "<ins class=\"adsbygoogle\" style=\"'.$align.'width:'.$size1.'px;height:'.$size2. 
  239. 'px;display:block;\" data-ad-client=\"'.$id.'\" data-ad-slot=\"'.$code.'\"></ins>"; 
  240. }'; 
  241. $adsense .= '</script>'; 
  242.  
  243. } else { 
  244.  
  245. # BASIC MODE 
  246.  
  247. $link_units = array( 
  248. '728x15' => '728x15',  
  249. '468x15' => '468x15',  
  250. '200x90' => '200x90',  
  251. '180x90' => '180x90',  
  252. '160x90' => '160x90',  
  253. '120x90' => '120x90',  
  254. ); 
  255.  
  256. $google_ad_type = get_option('GARD_AD_TYPE', 'text_image' ); 
  257.  
  258. if ($google_ad_type == 'link') { 
  259. $adsizes = $link_units; 
  260. } else { 
  261. global $adsizes; 
  262.  
  263. $google_color_border = str_replace("#", '', get_option( 'GARD_AD_BORDER', '#FFFFFF' ) ); 
  264. $google_color_bg = str_replace("#", '', get_option( 'GARD_AD_BACKGROUND', '#FFFFFF' ) ); 
  265. $google_color_link = str_replace("#", '', get_option( 'GARD_AD_TITLE', '#0000FF' ) ); 
  266. $google_color_url = str_replace("#", '', get_option( 'GARD_AD_URL', '#008000' ) ); 
  267. $google_color_text = str_replace("#", '', get_option( 'GARD_AD_TEXT', '#000000' ) ); 
  268. $google_ui_features = get_option('GARD_AD_RADIUS', '0'); 
  269.  
  270. $format_extension = '_as'; 
  271.  
  272. if ($google_ad_type == 'link') { 
  273. $format_extension = '_0ads_al'; 
  274. $google_ad_type = 'google_ad_type = "";'; 
  275. } else { 
  276. $google_ad_type = 'google_ad_type = "text";'; 
  277. // $google_ad_type = 'google_ad_type = "'.$google_ad_type.'";'; 
  278.  
  279.  
  280. $adsense = '  
  281. <div class="GARD gard_basic_mode" id="google-ads-'.$num.'"><script > 
  282. adUnit = document.getElementById("google-ads-'.$num.'"); 
  283. adWidth = adUnit.offsetWidth;  
  284.  
  285. if ( adWidth >= 999999 ) { 
  286. /* GETTING THE FIRST IF OUT OF THE WAY */  
  287. }'; 
  288.  
  289. foreach($adsizes as $size => $description) { 
  290.  
  291. $code = get_option('GARD_'.$size.'_BASIC'); 
  292. $mobile = get_option('GARD_MOBILE_BASIC'); 
  293.  
  294. $type = 'ad_unit'; 
  295. if ( get_option('GARD_AD_TYPE', 'text_image' ) == 'link' ) { 
  296. $type = 'link_unit'; 
  297. } elseif ($size == '300x600') { 
  298. $type = 'large_skyscraper'; 
  299.  
  300. $tag = $type."_".$size; 
  301. $original_size = $size; 
  302. $size = explode('x', $size); 
  303. $size1 = $size[0]; 
  304. $size2 = $size[1]; 
  305.  
  306. if ($code == '') { 
  307. continue; 
  308.  
  309. $mobile = get_option('GARD_MOBILE_BASIC_'.$original_size); 
  310.  
  311. if( $mobile == 1 && gard_ismobile() === FALSE ) { 
  312. // If ad is set to show to mobile only, and this is not a mobile device, skip the output of this ad 
  313. continue; 
  314. } elseif( $mobile == 2 && gard_ismobile() === TRUE ) { 
  315. // If ad is set to show to non-mobile only, and this is a mobile device, skip the output of this ad 
  316. continue; 
  317.  
  318. $adsense .= ' else if ( adWidth >= '.$size1.' ) { 
  319. if (document.querySelectorAll(".'.$type.'").length > 2 ) { 
  320. google_ad_slot = "0"; 
  321. adUnit.style.display = "none"; 
  322. } else { 
  323. adcount = document.querySelectorAll(".'.$type.'").length; 
  324. tag = "'.$tag.'_"+adcount; 
  325. google_ad_width = "'.$size1.'"; 
  326. google_ad_height = "'.$size2.'"; 
  327. google_ad_format = "'.$size1.'x'.$size2.$format_extension.'"; 
  328. '.$google_ad_type.' 
  329. google_ad_channel = ""; 
  330. }'; 
  331.  
  332.  
  333. $adsense .= ' else { 
  334. google_ad_slot = "0"; 
  335. adUnit.style.display = "none"; 
  336. adUnit.className = adUnit.className + " '.$type.' " + tag; 
  337. google_ad_client = "'.$id.'"; 
  338. adUnit.style.cssFloat = "'.$float.'"; 
  339. adUnit.style.styleFloat = "'.$float.'"; 
  340. adUnit.style.margin = "'.$margin.'"; 
  341. adUnit.style.textAlign = "'.$text.'"; 
  342. google_color_border = "'.$google_color_border.'"; 
  343. google_color_bg = "'.$google_color_bg.'"; 
  344. google_color_link = "'.$google_color_link.'"; 
  345. google_color_url = "'.$google_color_url.'"; 
  346. google_color_text = "'.$google_color_text.'"; 
  347. google_ui_features = "rc:'.$google_ui_features.'"; 
  348. </script> 
  349. <script src="//pagead2.googlesyndication.com/pagead/show_ads.js"></script> 
  350. </div>'; 
  351.  
  352.  
  353. # SET UP STYLE 
  354. if ( file_exists( dirname(__FILE__) .'gard_custom.css') ) {  
  355. wp_register_style( 'gard_ad_style', plugins_url( '/gard_custom.css', __FILE__ ) ); 
  356. wp_enqueue_style( 'gard_ad_style' ); 
  357.  
  358. if (!isset($_GET['GARD_DEBUG']))  
  359. $adsense = str_replace(array("\n", "\t"), "", $adsense); 
  360.  
  361. return $adsense; 
  362.  
  363. add_shortcode( 'GARD', '_GARD' ); 
  364. add_shortcode( 'gard', '_GARD' ); 
  365. ?> 
.