Guest User

Untitled

a guest
Sep 12th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.70 KB | None | 0 0
  1. <?php
  2. class td_ad_box extends td_block {
  3. function __construct() {
  4. $this->block_id = 'td_ad_box';
  5. add_shortcode('td_ad_box', array($this, 'render'));
  6. }
  7.  
  8.  
  9. /**
  10. * renders the ads
  11. * @param $atts
  12. * @return string
  13. */
  14. function render( $atts ) {
  15. extract(shortcode_atts(
  16. array(
  17. 'spot_id' => '', //header / sidebar etc
  18. 'align' => '', //align left or right in inline content
  19. ), $atts));
  20.  
  21.  
  22. if (empty($spot_id)) {
  23. return;
  24. }
  25.  
  26. $ad_array = td_util::get_td_ads($spot_id);
  27.  
  28. // return if the ad for a specific spot id is empty
  29. if (empty($ad_array[$spot_id]['ad_code'])) {
  30. return;
  31. }
  32.  
  33.  
  34. $buffy = '';
  35.  
  36. if (!empty($ad_array[$spot_id]['current_ad_type'])) {
  37.  
  38.  
  39. switch ($ad_array[$spot_id]['current_ad_type']) {
  40.  
  41. case 'other':
  42. //render the normal ads
  43. $buffy .= $this->render_ads($ad_array[$spot_id], $atts);
  44. break;
  45.  
  46. case 'google':
  47. //render the magic google ads :)
  48. $buffy .= $this->render_google_ads($ad_array[$spot_id], $atts);
  49. break;
  50.  
  51. }
  52. }
  53.  
  54.  
  55. //print_r($ad_array);
  56.  
  57. return $buffy;
  58.  
  59. }
  60.  
  61.  
  62. /**
  63. * This function renders and returns a google ad.
  64. * @param $ad_array - uses an ad array of the form:
  65. - current_ad_type - google or other
  66. - ad_code - the full ad code as entered by the user
  67. - disable_m - disable on monitor
  68. - disable_tl - disable on tablet landscape
  69. - disable_tp - disable on tablet p
  70. - disable_p - disable on phones
  71. - g_data_ad_client - the google ad client id (ca-pub-etc)
  72. - g_data_ad_slot - the google ad slot id
  73. * 'm_w' => '', // big monitor - width
  74. 'm_h' => '', // big monitor - height
  75. 'tl_w' => '', // tablet_landscape width
  76. 'tl_h' => '', // tablet_landscape height
  77. 'tp_w' => '', // tablet_portrait width
  78. 'tp_h' => '', // tablet_portrait height
  79. 'p_w' => '', // phone width
  80. 'p_h' => '' // phone height
  81. * @param $atts test
  82. * @return the full rendered ad
  83. */
  84. // tagDiv google responsive renderer
  85. // copyright 2014 tagDiv
  86. function render_google_ads($ad_array, $atts) {
  87.  
  88.  
  89. $spot_id = ''; //the spot id header / sidebar etc we read it from shortcode
  90.  
  91. extract(shortcode_atts(
  92. array(
  93. 'spot_id' => '', //header / sidebar etc
  94. 'align' => '', //align left or right in inline content
  95. ), $atts));
  96.  
  97.  
  98. //echo ($p_w);
  99.  
  100. //print_r($ad_array);
  101.  
  102. $default_ad_sizes = array (
  103. 'header' => array (
  104. 'm_w' => '728', // big monitor - width
  105. 'm_h' => '90', // big monitor - height
  106.  
  107. 'tl_w' => '468', // tablet_landscape width
  108. 'tl_h' => '60', // tablet_landscape height
  109.  
  110. 'tp_w' => '468', // tablet_portrait width
  111. 'tp_h' => '60', // tablet_portrait height
  112.  
  113. 'p_w' => '320', // phone width
  114. 'p_h' => '50' // phone height
  115. ),
  116. 'sidebar' => array (
  117. 'm_w' => '300', // big monitor - width
  118. 'm_h' => '250', // big monitor - height
  119.  
  120. 'tl_w' => '250', // tablet_landscape width
  121. 'tl_h' => '250', // tablet_landscape height
  122.  
  123. 'tp_w' => '200', // tablet_portrait width
  124. 'tp_h' => '200', // tablet_portrait height
  125.  
  126. 'p_w' => '300', // phone width
  127. 'p_h' => '250' // phone height
  128. ),
  129.  
  130.  
  131. 'content_inline' => array (
  132. 'm_w' => '728', // big monitor - width
  133. 'm_h' => '90', // big monitor - height
  134.  
  135. 'tl_w' => '468', // tablet_landscape width
  136. 'tl_h' => '60', // tablet_landscape height
  137.  
  138. 'tp_w' => '468', // tablet_portrait width
  139. 'tp_h' => '60', // tablet_portrait height
  140.  
  141. 'p_w' => '320', // phone width
  142. 'p_h' => '50' // phone height
  143. ),
  144.  
  145. 'content_top' => array (
  146. 'm_w' => '728', // big monitor - width
  147. 'm_h' => '90', // big monitor - height
  148.  
  149. 'tl_w' => '468', // tablet_landscape width
  150. 'tl_h' => '60', // tablet_landscape height
  151.  
  152. 'tp_w' => '468', // tablet_portrait width
  153. 'tp_h' => '60', // tablet_portrait height
  154.  
  155. 'p_w' => '300', // phone width
  156. 'p_h' => '250' // phone height
  157. ),
  158.  
  159. 'content_bottom' => array (
  160. 'm_w' => '728', // big monitor - width
  161. 'm_h' => '90', // big monitor - height
  162.  
  163. 'tl_w' => '468', // tablet_landscape width
  164. 'tl_h' => '60', // tablet_landscape height
  165.  
  166. 'tp_w' => '468', // tablet_portrait width
  167. 'tp_h' => '60', // tablet_portrait height
  168.  
  169. 'p_w' => '300', // phone width
  170. 'p_h' => '250' // phone height
  171. ),
  172.  
  173. 'custom_ad_1' => array (
  174. 'm_w' => '300', // big monitor - width
  175. 'm_h' => '250', // big monitor - height
  176.  
  177. 'tl_w' => '250', // tablet_landscape width
  178. 'tl_h' => '250', // tablet_landscape height
  179.  
  180. 'tp_w' => '200', // tablet_portrait width
  181. 'tp_h' => '200', // tablet_portrait height
  182.  
  183. 'p_w' => '300', // phone width
  184. 'p_h' => '250' // phone height
  185. ),
  186.  
  187. 'custom_ad_2' => array (
  188. 'm_w' => '300', // big monitor - width
  189. 'm_h' => '250', // big monitor - height
  190.  
  191. 'tl_w' => '250', // tablet_landscape width
  192. 'tl_h' => '250', // tablet_landscape height
  193.  
  194. 'tp_w' => '200', // tablet_portrait width
  195. 'tp_h' => '200', // tablet_portrait height
  196.  
  197. 'p_w' => '300', // phone width
  198. 'p_h' => '250' // phone height
  199. ),
  200.  
  201. 'custom_ad_3' => array (
  202. 'm_w' => '300', // big monitor - width
  203. 'm_h' => '250', // big monitor - height
  204.  
  205. 'tl_w' => '250', // tablet_landscape width
  206. 'tl_h' => '250', // tablet_landscape height
  207.  
  208. 'tp_w' => '200', // tablet_portrait width
  209. 'tp_h' => '200', // tablet_portrait height
  210.  
  211. 'p_w' => '300', // phone width
  212. 'p_h' => '250' // phone height
  213. ),
  214.  
  215. 'custom_ad_4' => array (
  216. 'm_w' => '300', // big monitor - width
  217. 'm_h' => '250', // big monitor - height
  218.  
  219. 'tl_w' => '250', // tablet_landscape width
  220. 'tl_h' => '250', // tablet_landscape height
  221.  
  222. 'tp_w' => '200', // tablet_portrait width
  223. 'tp_h' => '200', // tablet_portrait height
  224.  
  225. 'p_w' => '300', // phone width
  226. 'p_h' => '250' // phone height
  227. ),
  228.  
  229. 'custom_ad_5' => array (
  230. 'm_w' => '300', // big monitor - width
  231. 'm_h' => '250', // big monitor - height
  232.  
  233. 'tl_w' => '250', // tablet_landscape width
  234. 'tl_h' => '250', // tablet_landscape height
  235.  
  236. 'tp_w' => '200', // tablet_portrait width
  237. 'tp_h' => '200', // tablet_portrait height
  238.  
  239. 'p_w' => '300', // phone width
  240. 'p_h' => '250' // phone height
  241. ),
  242.  
  243. 'custom_ad_6' => array (
  244. 'm_w' => '300', // big monitor - width
  245. 'm_h' => '250', // big monitor - height
  246.  
  247. 'tl_w' => '250', // tablet_landscape width
  248. 'tl_h' => '250', // tablet_landscape height
  249.  
  250. 'tp_w' => '200', // tablet_portrait width
  251. 'tp_h' => '200', // tablet_portrait height
  252.  
  253. 'p_w' => '300', // phone width
  254. 'p_h' => '250' // phone height
  255. ),
  256.  
  257. 'custom_ad_7' => array (
  258. 'm_w' => '300', // big monitor - width
  259. 'm_h' => '250', // big monitor - height
  260.  
  261. 'tl_w' => '250', // tablet_landscape width
  262. 'tl_h' => '250', // tablet_landscape height
  263.  
  264. 'tp_w' => '200', // tablet_portrait width
  265. 'tp_h' => '200', // tablet_portrait height
  266.  
  267. 'p_w' => '300', // phone width
  268. 'p_h' => '250' // phone height
  269. ),
  270.  
  271. 'custom_ad_8' => array (
  272. 'm_w' => '300', // big monitor - width
  273. 'm_h' => '250', // big monitor - height
  274.  
  275. 'tl_w' => '250', // tablet_landscape width
  276. 'tl_h' => '250', // tablet_landscape height
  277.  
  278. 'tp_w' => '200', // tablet_portrait width
  279. 'tp_h' => '200', // tablet_portrait height
  280.  
  281. 'p_w' => '300', // phone width
  282. 'p_h' => '250' // phone height
  283. ),
  284.  
  285. 'custom_ad_9' => array (
  286. 'm_w' => '300', // big monitor - width
  287. 'm_h' => '250', // big monitor - height
  288.  
  289. 'tl_w' => '250', // tablet_landscape width
  290. 'tl_h' => '250', // tablet_landscape height
  291.  
  292. 'tp_w' => '200', // tablet_portrait width
  293. 'tp_h' => '200', // tablet_portrait height
  294.  
  295. 'p_w' => '300', // phone width
  296. 'p_h' => '250' // phone height
  297. )
  298.  
  299. );
  300.  
  301.  
  302. if ($align == 'left') {
  303. $default_ad_sizes['content_inline'] = array (
  304. 'm_w' => '300', // big monitor - width
  305. 'm_h' => '250', // big monitor - height
  306.  
  307. 'tl_w' => '250', // tablet_landscape width
  308. 'tl_h' => '250', // tablet_landscape height
  309.  
  310. 'tp_w' => '200', // tablet_portrait width
  311. 'tp_h' => '200', // tablet_portrait height
  312.  
  313. 'p_w' => '320', // phone width
  314. 'p_h' => '50' // phone height
  315. );
  316. }
  317. elseif ($align == 'right') {
  318. $default_ad_sizes['content_inline'] = array (
  319. 'm_w' => '300', // big monitor - width
  320. 'm_h' => '250', // big monitor - height
  321.  
  322. 'tl_w' => '250', // tablet_landscape width
  323. 'tl_h' => '250', // tablet_landscape height
  324.  
  325. 'tp_w' => '200', // tablet_portrait width
  326. 'tp_h' => '200', // tablet_portrait height
  327.  
  328. 'p_w' => '320', // phone width
  329. 'p_h' => '50' // phone height
  330. );
  331. }
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339. //overwrite the default values if we have some
  340.  
  341. //monitor big ad
  342. if (!empty($ad_array['m_size'])) {
  343. $ad_size_parts = explode(' x ', $ad_array['m_size']);
  344. $default_ad_sizes[$spot_id]['m_w'] = $ad_size_parts[0];
  345. $default_ad_sizes[$spot_id]['m_h'] = $ad_size_parts[1];
  346. }
  347.  
  348.  
  349. //tablet landscape
  350. if (!empty($ad_array['tl_size'])) {
  351. $ad_size_parts = explode(' x ', $ad_array['tl_size']);
  352. $default_ad_sizes[$spot_id]['tl_w'] = $ad_size_parts[0];
  353. $default_ad_sizes[$spot_id]['tl_h'] = $ad_size_parts[1];
  354. }
  355.  
  356.  
  357. //tablet portrait
  358. if (!empty($ad_array['tp_size'])) {
  359. $ad_size_parts = explode(' x ', $ad_array['tp_size']);
  360. $default_ad_sizes[$spot_id]['tp_w'] = $ad_size_parts[0];
  361. $default_ad_sizes[$spot_id]['tp_h'] = $ad_size_parts[1];
  362. }
  363.  
  364.  
  365. //phone
  366. if (!empty($ad_array['p_size'])) {
  367. $ad_size_parts = explode(' x ', $ad_array['p_size']);
  368. $default_ad_sizes[$spot_id]['p_w'] = $ad_size_parts[0];
  369. $default_ad_sizes[$spot_id]['p_h'] = $ad_size_parts[1];
  370. }
  371.  
  372.  
  373.  
  374.  
  375.  
  376. //init the disable variables
  377. if (!empty($ad_array['disable_m']) and $ad_array['disable_m'] == 'yes') {
  378. $default_ad_sizes[$spot_id]['disable_m'] = true;
  379. } else {
  380. $default_ad_sizes[$spot_id]['disable_m'] = false;
  381. }
  382.  
  383. if (!empty($ad_array['disable_tl']) and $ad_array['disable_tl'] == 'yes') {
  384. $default_ad_sizes[$spot_id]['disable_tl'] = true;
  385. } else {
  386. $default_ad_sizes[$spot_id]['disable_tl'] = false;
  387. }
  388.  
  389. if (!empty($ad_array['disable_tp']) and $ad_array['disable_tp'] == 'yes') {
  390. $default_ad_sizes[$spot_id]['disable_tp'] = true;
  391. } else {
  392. $default_ad_sizes[$spot_id]['disable_tp'] = false;
  393. }
  394.  
  395. if (!empty($ad_array['disable_p']) and $ad_array['disable_p'] == 'yes') {
  396. $default_ad_sizes[$spot_id]['disable_p'] = true;
  397. } else {
  398. $default_ad_sizes[$spot_id]['disable_p'] = false;
  399. }
  400.  
  401.  
  402.  
  403.  
  404. $buffy = "\n <!-- A generated by theme --> \n\n";
  405.  
  406. //google async script
  407. $buffy .= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>';
  408.  
  409. $buffy .= '<div class="td-g-rec td-g-rec-id-' . $spot_id . $align . '">' . "\n";
  410. $buffy .= '<script type="text/javascript">' . "\n";
  411.  
  412.  
  413. //$buffy .= 'var td_a_g_custom_size = ' . json_encode($default_ad_sizes[$spot_id]) . ';' . "\n";
  414.  
  415.  
  416. $buffy .= 'var td_screen_width = document.body.clientWidth;' . "\n";
  417.  
  418. //print_r($ad_array);
  419.  
  420. if ($default_ad_sizes[$spot_id]['disable_m'] == false and !empty($default_ad_sizes[$spot_id]['m_w']) and !empty($default_ad_sizes[$spot_id]['m_h'])) {
  421. $buffy .= '
  422. if ( td_screen_width >= 1200 ) {
  423. /* large monitors */
  424. document.write(\'<ins class="adsbygoogle" style="display:inline-block;width:' . $default_ad_sizes[$spot_id]['m_w'] . 'px;height:' . $default_ad_sizes[$spot_id]['m_h'] . 'px" data-ad-client="' . $ad_array['g_data_ad_client'] . '" data-ad-slot="' . $ad_array['g_data_ad_slot'] . '"></ins>\');
  425. (adsbygoogle = window.adsbygoogle || []).push({});
  426. }
  427. ';
  428. }
  429.  
  430. if ($default_ad_sizes[$spot_id]['disable_tl'] == false and !empty($default_ad_sizes[$spot_id]['tl_w']) and !empty($default_ad_sizes[$spot_id]['tl_h'])) {
  431. $buffy .= '
  432. if ( td_screen_width >= 1019 && td_screen_width < 1200 ) {
  433. /* landscape tablets */
  434. document.write(\'<ins class="adsbygoogle" style="display:inline-block;width:' . $default_ad_sizes[$spot_id]['tl_w'] . 'px;height:' . $default_ad_sizes[$spot_id]['tl_h'] . 'px" data-ad-client="' . $ad_array['g_data_ad_client'] . '" data-ad-slot="' . $ad_array['g_data_ad_slot'] . '"></ins>\');
  435. (adsbygoogle = window.adsbygoogle || []).push({});
  436. }
  437. ';
  438. }
  439.  
  440.  
  441. if ($default_ad_sizes[$spot_id]['disable_tp'] == false and !empty($default_ad_sizes[$spot_id]['tp_w']) and !empty($default_ad_sizes[$spot_id]['tp_h'])) {
  442. $buffy .= '
  443. if ( td_screen_width >= 768 && td_screen_width < 1019 ) {
  444. /* portrait tablets */
  445. document.write(\'<ins class="adsbygoogle" style="display:inline-block;width:' . $default_ad_sizes[$spot_id]['tp_w'] . 'px;height:' . $default_ad_sizes[$spot_id]['tp_h'] . 'px" data-ad-client="' . $ad_array['g_data_ad_client'] . '" data-ad-slot="' . $ad_array['g_data_ad_slot'] . '"></ins>\');
  446. (adsbygoogle = window.adsbygoogle || []).push({});
  447. }
  448. ';
  449. }
  450.  
  451. if ($default_ad_sizes[$spot_id]['disable_p'] == false and !empty($default_ad_sizes[$spot_id]['p_w']) and !empty($default_ad_sizes[$spot_id]['p_h'])) {
  452. $buffy .= '
  453. if ( td_screen_width < 768 ) {
  454. /* Phones */
  455. document.write(\'<ins class="adsbygoogle" style="display:inline-block;width:' . $default_ad_sizes[$spot_id]['p_w'] . 'px;height:' . $default_ad_sizes[$spot_id]['p_h'] . 'px" data-ad-client="' . $ad_array['g_data_ad_client'] . '" data-ad-slot="' . $ad_array['g_data_ad_slot'] . '"></ins>\');
  456. (adsbygoogle = window.adsbygoogle || []).push({});
  457. }
  458. ';
  459. }
  460.  
  461.  
  462. //$buffy .= 'console.log(td_a_g_custom_size)';
  463.  
  464. $buffy .= '</script>' . "\n";
  465.  
  466. $buffy .= '</div>' . "\n";
  467. $buffy .= "\n <!-- end A --> \n\n";
  468. return $buffy;
  469. }
  470.  
  471.  
  472. /**
  473. * This function renders and returns a normal ad.
  474. * @param $ad_array - uses an ad array of the form:
  475. - current_ad_type - google or other
  476. - ad_code - the full ad code as entered by the user
  477. - disable_m - disable on monitor
  478. - disable_tl - disable on tablet landscape
  479. - disable_tp - disable on tablet p
  480. - disable_p - disable on phones
  481. - g_data_ad_client - the google ad client id (ca-pub-etc)
  482. - g_data_ad_slot - the google ad slot id
  483. *
  484. * @return the full rendered ad
  485. */
  486. function render_ads($ad_array, $atts) {
  487.  
  488. $spot_id = ''; //the spot id header / sidebar etc we read it from shortcode
  489.  
  490. extract(shortcode_atts(
  491. array(
  492. 'spot_id' => '', //header / sidebar etc
  493. 'align' => '', //align left or right in inline content
  494.  
  495. ), $atts));
  496.  
  497.  
  498. $buffy = '';
  499.  
  500.  
  501.  
  502.  
  503. $buffy .= '<div class="td-a-rec td-a-rec-id-' . $spot_id . $align . ' '
  504. . ((!empty($ad_array['disable_m'])) ? ' td-rec-hide-on-m' : '')
  505. . ((!empty($ad_array['disable_tl'])) ? ' td-rec-hide-on-tl' : '')
  506. . ((!empty($ad_array['disable_tp'])) ? ' td-rec-hide-on-tp' : '')
  507. . ((!empty($ad_array['disable_p'])) ? ' td-rec-hide-on-p' : '')
  508. . '">';
  509. $buffy .= do_shortcode(stripslashes($ad_array['ad_code']));
  510. $buffy .= '</div>';
  511.  
  512.  
  513. //print_r($ad_array);
  514. return $buffy;
  515.  
  516. }
  517.  
  518.  
  519.  
  520.  
  521.  
  522. function get_map() {
  523. //read the adspots
  524. $td_ad_spots = td_util::get_option('td_ad_spots');
  525. $td_pb_ad_spots = array();
  526. if (!empty($td_ad_spots)) {
  527. foreach ($td_ad_spots as $td_ad_spot) {
  528. $td_pb_ad_spots['Ad spot -- ' . $td_ad_spot['name']] = 'Ad spot -- ' . $td_ad_spot['name'];
  529. }
  530. }
  531.  
  532. //read the google adspots
  533. $td_adsense_spots = td_util::get_option('td_adsense_spots');
  534. if (!empty($td_adsense_spots)) {
  535. foreach ($td_adsense_spots as $td_ad_spot) {
  536. $td_pb_ad_spots['Adsense spot -- ' . $td_ad_spot['name']] = 'Adsense spot -- ' . $td_ad_spot['name'];
  537. }
  538. }
  539.  
  540. return array(
  541. "name" => __("Ad box", TD_THEME_NAME),
  542. "base" => "td_ad_box",
  543. "class" => "",
  544. "controls" => "full",
  545. "category" => __('Content', TD_THEME_NAME),
  546. 'icon' => 'icon-pagebuilder-ads',
  547. "params" => array(
  548. array(
  549. "param_name" => "spot_id",
  550. "type" => "dropdown",
  551. "value" => array(
  552. 'sidebar' => 'sidebar',
  553. 'content_inline' => 'content_inline',
  554. 'content_top' => 'content_top',
  555. 'content_bottom' => 'content_bottom',
  556. 'header' => 'header',
  557. 'custom_ad_1' => 'custom_ad_1',
  558. 'custom_ad_2' => 'custom_ad_2',
  559. 'custom_ad_3' => 'custom_ad_3',
  560. 'custom_ad_4' => 'custom_ad_4',
  561. 'custom_ad_5' => 'custom_ad_5',
  562. 'custom_ad_6' => 'custom_ad_6',
  563. 'custom_ad_7' => 'custom_ad_7',
  564. 'custom_ad_8' => 'custom_ad_8',
  565. 'custom_ad_9' => 'custom_ad_9'
  566. ),
  567. "heading" => __("Use adspot :", TD_THEME_NAME),
  568. "description" => "",
  569. "holder" => "div",
  570. "class" => ""
  571. )
  572. )
  573. );
  574. }
  575. }
  576.  
  577. td_global_blocks::add_instance('Ad box', new td_ad_box());
Advertisement
Add Comment
Please, Sign In to add comment