qa-basic-adsense.php 2.89 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-plugin/basic-adsense/qa-basic-adsense.php
	Description: Widget module class for AdSense widget plugin


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
23 24 25 26
class qa_basic_adsense
{
	public function allow_template($template)
	{
Scott committed
27
		return $template != 'admin';
Scott committed
28
	}
Scott committed
29 30


Scott committed
31 32 33 34
	public function allow_region($region)
	{
		return in_array($region, array('main', 'side', 'full'));
	}
Scott committed
35 36


Scott committed
37 38
	public function admin_form(&$qa_content)
	{
Scott committed
39
		$saved = false;
Scott committed
40

Scott committed
41
		if (qa_clicked('adsense_save_button')) {
Scott committed
42 43
			// prevent common errors by copying and pasting from Javascript
			$trimchars = "=;\"\' \t\r\n";
Scott committed
44
			qa_opt('adsense_publisher_id', trim(qa_post_text('adsense_publisher_id_field'), $trimchars));
Amiya committed
45
			qa_opt('adsense_adunit_id', trim(qa_post_text('adsense_adunit_id_field'), $trimchars));
Scott committed
46 47

			$saved = true;
Scott committed
48
		}
Scott committed
49

Scott committed
50
		return array(
51
			'ok' => $saved ? qa_lang_html('admin/options_saved') : null,
Scott committed
52

Scott committed
53 54
			'fields' => array(
				array(
55
					'label' => qa_lang_html('basic_adsense/publisher_id'),
Scott committed
56 57
					'value' => qa_html(qa_opt('adsense_publisher_id')),
					'tags' => 'name="adsense_publisher_id_field"',
58
					'note' => qa_lang_html_sub('basic_adsense/example_x', '<i>pub-1234567890123456</i>'),
Scott committed
59
				),
Amiya committed
60
				array(
61
					'label' => qa_lang_html('basic_adsense/ad_unit_id'),
Amiya committed
62 63
					'value' => qa_html(qa_opt('adsense_adunit_id')),
					'tags' => 'name="adsense_adunit_id_field"',
64
					'note' => qa_lang_html_sub('basic_adsense/example_x', '<i>8XXXXX1</i>'),
Amiya committed
65
				),
Scott committed
66
			),
Scott committed
67

Scott committed
68 69
			'buttons' => array(
				array(
70
					'label' => qa_lang_html('main/save_button'),
Scott committed
71
					'tags' => 'name="adsense_save_button"',
Scott committed
72
				),
Scott committed
73 74 75
			),
		);
	}
Scott committed
76 77


Scott committed
78 79
	public function output_widget($region, $place, $themeobject, $template, $request, $qa_content)
	{
Amiya committed
80
		$format = 'auto';
Scott committed
81

Scott committed
82
		switch ($region) {
Amiya committed
83 84
			case 'full':
			case 'main':
Scott committed
85
				$format = 'horizontal';
Scott committed
86
				break;
Scott committed
87

Amiya committed
88
			case 'side':
Scott committed
89
				$format = 'vertical';
Scott committed
90 91
				break;
		}
Scott committed
92

Amiya committed
93 94 95 96 97 98 99 100 101 102 103 104
		?>
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<ins class="adsbygoogle <?php echo qa_html($region) ?>"
			style="display:block; margin:.5em auto"
			data-ad-client="<?php echo qa_html(qa_opt('adsense_publisher_id')) ?>"
			data-ad-slot="<?php echo qa_html(qa_opt('adsense_adunit_id')) ?>"
			data-ad-format="<?php echo qa_html($format) ?>">
		</ins>
		<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		<?php
Scott committed
105
	}
Scott committed
106
}