/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.21 
  7. Author: The Plugin Factory, The Digital Hippies 
  8. Author URI: http://thedigitalhippies.com 
  9. */ 
  10.  
  11.  
  12. if (!function_exists('wp_get_current_user') ) { 
  13. include_once( ABSPATH . '/wp-includes/pluggable.php' ); 
  14.  
  15. function GARD_PERMISSION_CHECK() { 
  16. if (!current_user_can('manage_options')) { 
  17. return FALSE; 
  18. //wp_die( __('You do not have sufficient permissions to access this page.') ); 
  19. } else { 
  20. return TRUE; 
  21.  
  22. if( is_admin() && GARD_PERMISSION_CHECK() ) { 
  23.  
  24. include('adsizes.php'); 
  25.  
  26. define('GARDPLUGINOPTIONS_VER', '2.21'); 
  27. define('GARDPLUGINOPTIONS_ID', 'GARD-plugin-options'); 
  28. define('GARDPLUGINOPTIONS_NICK', 'Google Adsense for Responsive Design'); 
  29. define('GARD_FOLDER', dirname(__FILE__) ); 
  30. define('GARD_PRO_LINK', 'http://thepluginfactory.co/warehouse/gard-pro/'); 
  31. define('GARD_PLUGIN_SUPPORT_URL', 'http://thepluginfactory.co/community/forum/plugin-specific/gard-google-adsense-for-responsive-design/'); 
  32. 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>'); 
  33.  
  34.  
  35. function gard_register() { 
  36. include(dirname(__FILE__) . '/register.php'); 
  37. }  
  38.  
  39. function gard_options_page() {  
  40. include(dirname(__FILE__) . '/options.php'); 
  41.  
  42. function gard_menu() { 
  43. include(dirname(__FILE__) . '/menu.php'); 
  44.  
  45. function gard_help() {  
  46. include(dirname(__FILE__) . '/help.php'); 
  47.  
  48. function gard_pro_settings() { 
  49. include(dirname(__FILE__) . '/pro.php'); 
  50.  
  51. function GARD_ENQUEUE() { 
  52. wp_register_script( 'spectrum_js', plugins_url( '/js/spectrum/spectrum.js', __FILE__ ) ); 
  53. wp_enqueue_script( 'spectrum_js' ); 
  54.  
  55. wp_register_style( 'spectrum_css', plugins_url( '/js/spectrum/spectrum.css', __FILE__ ) ); 
  56. wp_enqueue_style( 'spectrum_css' ); 
  57.  
  58. wp_enqueue_script( 'jquery' ); 
  59.  
  60. function GARD_PRO_plugin_links($links, $file) { 
  61. if ($file == 'google-adsense-for-responsive-design-gard/index.php' ) { 
  62. $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>'; 
  63. return $links; 
  64.  
  65. add_action( 'admin_init', 'gard_register' ); 
  66. add_action( 'admin_menu', 'gard_menu' ); 
  67. add_action( 'admin_init', 'GARD_ENQUEUE' ); 
  68. add_filter( 'plugin_row_meta', 'GARD_PRO_plugin_links', 10, 2 ); 
  69.  
  70. } else { 
  71.  
  72. include('adsizes.php'); 
  73. function gard_ismobile() { 
  74. # Source http://detectmobilebrowsers.com/ 
  75. $useragent=$_SERVER['HTTP_USER_AGENT']; 
  76. 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))) { 
  77. return TRUE; 
  78. } else { 
  79. return FALSE; 
  80.  
  81. function _GARD( $atts = '' ) { 
  82. extract( shortcode_atts( array( 
  83. 'align' => '',  
  84. 'async' => 'false' 
  85. ), $atts ) ); 
  86.  
  87.  
  88. if($align == 'center') { 
  89. $align = 'margin: 0 auto;float: none;display: block;text-align:center;'; 
  90. $float = 'none'; 
  91. $margin = '0 auto'; 
  92. $display = 'block'; 
  93. $text = 'center'; 
  94. } elseif($align == 'left') { 
  95. $align = 'margin: 0 10px 10px 0;float: left;display: block;'; 
  96. $float = 'left'; 
  97. $margin = '0 10px 10px 0'; 
  98. $text = 'initial'; 
  99. $display = 'block'; 
  100. } elseif($align == 'right') { 
  101. $align = 'margin: 0 0 10px 10px;float: right;display: block;'; 
  102. $float = 'right'; 
  103. $margin = '0 0 10px 10px'; 
  104. $text = 'initial'; 
  105. $display = 'block'; 
  106. } elseif ($align == '') { 
  107. $float = ''; 
  108. $margin = ''; 
  109. $display = ''; 
  110. $text = '';  
  111.  
  112. $id = "ca-pub-".get_option("GARD_ID"); 
  113. $num = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4); 
  114.  
  115. $mode = get_option('GARD_ADVANCED_MODE', '1') ; 
  116.  
  117. $async_mode = get_option("GARD_ASYNC"); 
  118.  
  119. if( $mode == 1 && $async_mode !== '1' ) { 
  120.  
  121. # ADVANCED MODE 
  122.  
  123. $link_units = array( 
  124. '728x15' => '728x15',  
  125. '468x15' => '468x15',  
  126. '200x90' => '200x90',  
  127. '180x90' => '180x90',  
  128. '160x90' => '160x90',  
  129. '120x90' => '120x90',  
  130. ); 
  131.  
  132.  
  133. $adsense = '  
  134. <div class="GARD gard_advanced_mode" id="google-ads-'.$num.'"><script> 
  135. adUnit = document.getElementById("google-ads-'.$num.'"); 
  136. google_ad_client = "'.$id.'"; 
  137. adUnit = document.getElementById("google-ads-'.$num.'"); 
  138. adWidth = adUnit.offsetWidth;  
  139. if ( adWidth >= 999999 ) { 
  140. /* GETTING THE FIRST IF OUT OF THE WAY */  
  141. }'; 
  142.  
  143. global $adsizes; 
  144.  
  145. $link_units = array( 
  146. '728x15' => '728x15',  
  147. '468x15' => '468x15',  
  148. '200x90' => '200x90',  
  149. '180x90' => '180x90',  
  150. '160x90' => '160x90',  
  151. '120x90' => '120x90',  
  152. ); 
  153.  
  154. foreach($adsizes as $size => $description) { 
  155. $type = 'ad_unit'; 
  156. if ( in_array($size, $link_units) ) { 
  157. $type = 'link_unit'; 
  158. } elseif ($size == '300x600') { 
  159. $type = 'large_skyscraper'; 
  160.  
  161. $tag = $type."_".$size; 
  162. $code = get_option("GARD_".$size); 
  163. $original_size = $size; 
  164. $size = explode('x', $size); 
  165. $size1 = $size[0]; 
  166. $size2 = $size[1]; 
  167.  
  168. $mobile = get_option('GARD_MOBILE_ADVANCED_'.$original_size); 
  169.  
  170. if( $mobile == 1 && gard_ismobile() === FALSE ) { 
  171. // If ad is set to show to mobile only, and this is not a mobile device, skip the output of this ad 
  172. continue; 
  173. } elseif( $mobile == 2 && gard_ismobile() === TRUE ) { 
  174. // If ad is set to show to non-mobile only, and this is a mobile device, skip the output of this ad 
  175. continue; 
  176.  
  177. if ( strlen($code) == '10' && is_numeric($code)) {  
  178. $adsense .= ' else if ( adWidth >= '.$size1.' ) { 
  179. if (document.querySelectorAll(".'.$type.'").length > 3 ) {  
  180. google_ad_slot = "0"; 
  181. adUnit.style.display = "none"; 
  182. } else { 
  183. google_ad_slot = "'.$code.'"; 
  184. google_ad_width = '.$size1.'; 
  185. google_ad_height = '.$size2.'; 
  186. adUnit.style.cssFloat = "'.$float.'"; 
  187. adUnit.style.styleFloat = "'.$float.'"; 
  188. adUnit.style.margin = "'.$margin.'"; 
  189. adUnit.style.textAlign = "'.$text.'"; 
  190. adcount = document.querySelectorAll(".'.$type.'").length; 
  191. tag = "'.$tag.'_"+adcount; 
  192. adUnit.className = adUnit.className + " '.$type.' " + tag; 
  193. }'; 
  194.  
  195. $adsense .= ' else { 
  196. google_ad_slot = "0"; 
  197. adUnit.style.display = "none"; 
  198. }</script> 
  199. <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>'; 
  200.  
  201. } else if ( $mode == 1 && $async_mode == 1 ) { 
  202.  
  203. # ASYNC MODE 
  204.  
  205. $adsense = '<div class="GARD gard_async_mode" id="google-ads-'.$num.'" ><script async="async" src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><div id="GARDasync_'.$num.'"></div><script >(adsbygoogle = window.adsbygoogle || []).push({});</script></div>'; 
  206.  
  207. # determine size and insert correct ad. 
  208. $adsense .= '<script> 
  209. adUnit = document.getElementById("google-ads-'.$num.'"); 
  210. adWidth = adUnit.offsetWidth;  
  211. if ( adWidth >= 999999 ) { 
  212. /* GETTING THE FIRST IF OUT OF THE WAY */  
  213. }'; 
  214.  
  215. global $adsizes; 
  216. foreach($adsizes as $size => $description) { 
  217. $code = get_option("GARD_".$size); 
  218. $original_size = $size; 
  219. $size = explode('x', $size); 
  220. $size1 = $size[0]; 
  221. $size2 = $size[1]; 
  222.  
  223. $mobile = get_option('GARD_MOBILE_ADVANCED_'.$original_size); 
  224.  
  225. if( $mobile == 1 && gard_ismobile() === FALSE ) { 
  226. // If ad is set to show to mobile only, and this is not a mobile device, skip the output of this ad 
  227. continue; 
  228. } elseif( $mobile == 2 && gard_ismobile() === TRUE ) { 
  229. // If ad is set to show to non-mobile only, and this is a mobile device, skip the output of this ad 
  230. continue; 
  231.  
  232. if ( strlen($code) == '10' && is_numeric($code)) {  
  233. $adsense .= ' else if ( adWidth >= '.$size1.' ) { 
  234. document.getElementById("GARDasync_'.$num. 
  235. '").innerHTML = "<ins class=\"adsbygoogle\" style=\"'.$align.'width:'.$size1.'px;height:'.$size2. 
  236. 'px;display:block;\" data-ad-client=\"'.$id.'\" data-ad-slot=\"'.$code.'\"></ins>"; 
  237. }'; 
  238. $adsense .= '</script>'; 
  239.  
  240. } else { 
  241.  
  242. # BASIC MODE 
  243.  
  244. $link_units = array( 
  245. '728x15' => '728x15',  
  246. '468x15' => '468x15',  
  247. '200x90' => '200x90',  
  248. '180x90' => '180x90',  
  249. '160x90' => '160x90',  
  250. '120x90' => '120x90',  
  251. ); 
  252.  
  253. $google_ad_type = get_option('GARD_AD_TYPE', 'text_image' ); 
  254.  
  255. if ($google_ad_type == 'link') { 
  256. $adsizes = $link_units; 
  257. } else { 
  258. global $adsizes; 
  259.  
  260. $google_color_border = str_replace("#", '', get_option( 'GARD_AD_BORDER', '#FFFFFF' ) ); 
  261. $google_color_bg = str_replace("#", '', get_option( 'GARD_AD_BACKGROUND', '#FFFFFF' ) ); 
  262. $google_color_link = str_replace("#", '', get_option( 'GARD_AD_TITLE', '#0000FF' ) ); 
  263. $google_color_url = str_replace("#", '', get_option( 'GARD_AD_URL', '#008000' ) ); 
  264. $google_color_text = str_replace("#", '', get_option( 'GARD_AD_TEXT', '#000000' ) ); 
  265. $google_ui_features = get_option('GARD_AD_RADIUS', '0'); 
  266.  
  267. $format_extension = '_as'; 
  268.  
  269. if ($google_ad_type == 'link') { 
  270. $format_extension = '_0ads_al'; 
  271. $google_ad_type = 'google_ad_type = "";'; 
  272. } else { 
  273. $google_ad_type = 'google_ad_type = "text";'; 
  274. // $google_ad_type = 'google_ad_type = "'.$google_ad_type.'";'; 
  275.  
  276.  
  277. $adsense = '  
  278. <div class="GARD gard_basic_mode" id="google-ads-'.$num.'"><script > 
  279. adUnit = document.getElementById("google-ads-'.$num.'"); 
  280. adWidth = adUnit.offsetWidth;  
  281.  
  282. if ( adWidth >= 999999 ) { 
  283. /* GETTING THE FIRST IF OUT OF THE WAY */  
  284. }'; 
  285.  
  286. foreach($adsizes as $size => $description) { 
  287.  
  288. $code = get_option('GARD_'.$size.'_BASIC'); 
  289. $mobile = get_option('GARD_MOBILE_BASIC'); 
  290.  
  291. $type = 'ad_unit'; 
  292. if ( get_option('GARD_AD_TYPE', 'text_image' ) == 'link' ) { 
  293. $type = 'link_unit'; 
  294. } elseif ($size == '300x600') { 
  295. $type = 'large_skyscraper'; 
  296.  
  297. $tag = $type."_".$size; 
  298. $original_size = $size; 
  299. $size = explode('x', $size); 
  300. $size1 = $size[0]; 
  301. $size2 = $size[1]; 
  302.  
  303. if ($code == '') { 
  304. continue; 
  305.  
  306. $mobile = get_option('GARD_MOBILE_BASIC_'.$original_size); 
  307.  
  308. if( $mobile == 1 && gard_ismobile() === FALSE ) { 
  309. // If ad is set to show to mobile only, and this is not a mobile device, skip the output of this ad 
  310. continue; 
  311. } elseif( $mobile == 2 && gard_ismobile() === TRUE ) { 
  312. // If ad is set to show to non-mobile only, and this is a mobile device, skip the output of this ad 
  313. continue; 
  314.  
  315. $adsense .= ' else if ( adWidth >= '.$size1.' ) { 
  316. if (document.querySelectorAll(".'.$type.'").length > 2 ) { 
  317. google_ad_slot = "0"; 
  318. adUnit.style.display = "none"; 
  319. } else { 
  320. adcount = document.querySelectorAll(".'.$type.'").length; 
  321. tag = "'.$tag.'_"+adcount; 
  322. google_ad_width = "'.$size1.'"; 
  323. google_ad_height = "'.$size2.'"; 
  324. google_ad_format = "'.$size1.'x'.$size2.$format_extension.'"; 
  325. '.$google_ad_type.' 
  326. google_ad_channel = ""; 
  327. }'; 
  328.  
  329.  
  330. $adsense .= ' else { 
  331. google_ad_slot = "0"; 
  332. adUnit.style.display = "none"; 
  333. adUnit.className = adUnit.className + " '.$type.' " + tag; 
  334. google_ad_client = "'.$id.'"; 
  335. adUnit.style.cssFloat = "'.$float.'"; 
  336. adUnit.style.styleFloat = "'.$float.'"; 
  337. adUnit.style.margin = "'.$margin.'"; 
  338. adUnit.style.textAlign = "'.$text.'"; 
  339. google_color_border = "'.$google_color_border.'"; 
  340. google_color_bg = "'.$google_color_bg.'"; 
  341. google_color_link = "'.$google_color_link.'"; 
  342. google_color_url = "'.$google_color_url.'"; 
  343. google_color_text = "'.$google_color_text.'"; 
  344. google_ui_features = "rc:'.$google_ui_features.'"; 
  345. </script> 
  346. <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 
  347. </div>'; 
  348.  
  349.  
  350. # SET UP STYLE 
  351. if ( file_exists( dirname(__FILE__) .'gard_custom.css') ) {  
  352. wp_register_style( 'gard_ad_style', plugins_url( '/gard_custom.css', __FILE__ ) ); 
  353. wp_enqueue_style( 'gard_ad_style' ); 
  354.  
  355. if (!isset($_GET['GARD_DEBUG']))  
  356. $adsense = str_replace(array("\n", "\t"), "", $adsense); 
  357.  
  358. return $adsense; 
  359.  
  360. add_shortcode( 'GARD', '_GARD' ); 
  361. add_shortcode( 'gard', '_GARD' ); 
  362. ?> 
.