qa-basic-adsense.php 2.74 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 27 28
class qa_basic_adsense
{
	public function allow_template($template)
	{
		return ($template!='admin');
	}
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 39
	public function admin_form(&$qa_content)
	{
		$saved=false;
Scott committed
40

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

Scott committed
48 49
		return array(
			'ok' => $saved ? 'AdSense settings saved' : null,
Scott committed
50

Scott committed
51 52 53 54 55 56
			'fields' => array(
				array(
					'label' => 'AdSense Publisher ID:',
					'value' => qa_html(qa_opt('adsense_publisher_id')),
					'tags' => 'name="adsense_publisher_id_field"',
					'note' => 'Example: <i>pub-1234567890123456</i>',
Scott committed
57
				),
Amiya committed
58 59 60 61 62 63
				array(
					'label' => 'AdSense Ad Unit ID:',
					'value' => qa_html(qa_opt('adsense_adunit_id')),
					'tags' => 'name="adsense_adunit_id_field"',
					'note' => 'Example: <i>8XXXXX1</i>',
				),
Scott committed
64
			),
Scott committed
65

Scott committed
66 67 68 69
			'buttons' => array(
				array(
					'label' => 'Save Changes',
					'tags' => 'name="adsense_save_button"',
Scott committed
70
				),
Scott committed
71 72 73
			),
		);
	}
Scott committed
74 75


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

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

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

Amiya committed
91 92 93 94 95 96 97 98 99 100 101 102
		?>
		<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
103
	}
Scott committed
104
}