source: trunk/www.guidonia.net/wp/wp-content/plugins/google-website-optimizer-for-wordpress/gwo4wp.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 8.0 KB
Line 
1<?php
2
3/*
4Plugin Name: Google Website Optimizer for WordPress
5Plugin URI: http://www.masteringlandingpages.com/gwo4wp
6Description: Test your WordPress Landing Pages with Google Website Optimizer for WordPress
7Version: 1.2
8Author: Filippo Toso
9Author URI: http://www.masteringlandingpages.com/
10*/
11
12/* Copyright 2009 Filippo Toso (email : info@masteringlandingpages.com)
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27*/
28
29define('GWO_META_NAME', 'gwo4wp');
30define('GWO_CONTROL_IN_HEAD', true);
31
32class GW04WP {
33
34 var $meta = 'gwo4wp';
35 var $control_in_head = true;
36 var $control_script = '';
37 var $tracking_script = '';
38 var $conversion_script = '';
39
40 function GW04WP($meta = 'gwo4wp', $control_in_head = true) {
41 $this->__construct($meta, $control_in_head);
42 }
43
44 function __construct($meta = 'gwo4wp', $control_in_head = true) {
45 $this->meta = $meta;
46 $this->control_in_head = $control_in_head;
47 add_action('plugins_loaded', array(&$this, 'action_plugins_loaded'));
48 }
49
50 function action_plugins_loaded() {
51 if (is_admin()) {
52 require_once(ABSPATH . 'wp-admin/includes/template.php'); // Needed for add_meta_box()
53 add_meta_box('gwo_section', 'Google Website Optimizer', array(&$this, 'meta_box_post'), 'post', 'normal', 'high');
54 add_meta_box('gwo_section', 'Google Website Optimizer', array(&$this, 'meta_box_post'), 'page', 'normal', 'high');
55 add_action('save_post', array(&$this, 'action_save_post'), 1, 2);
56 } else {
57 add_action('template_redirect', array(&$this, 'action_template_redirect'));
58 }
59 }
60
61 function action_wp_head() {
62 echo('<meta name="generator" content="Google Website Optimizer for WordPress - http://www.masteringlandingpages.com/gwo4wp" />');
63 if ($this->control_in_head) {
64 echo($this->control_script);
65 }
66 }
67
68 function action_template_redirect() {
69
70 if (is_single() || is_page()) {
71 global $post;
72 $options = get_post_meta($post->ID, $this->meta, true);
73 if (isset($options['enabled']) && $options['enabled']) {
74
75 add_action('wp_head', array(&$this, 'action_wp_head'));
76
77 $this->control_script = isset($options['control_script']) ? trim($options['control_script']) : '';
78 $this->tracking_script = isset($options['tracking_script']) ? trim($options['tracking_script']) : '';
79 $this->conversion_script = isset($options['conversion_script']) ? trim($options['conversion_script']) : '';
80
81 ob_start(array(&$this, 'ob_start_callback'));
82
83 }
84 }
85
86 }
87
88 function ob_start_callback($content) {
89 if (!$this->control_in_head) {
90 $html = $this->control_script;
91 if ($html != '') {
92 $content = preg_replace('#<body[^>]*>#si', "$0\r\n{$html}\r\n", $content);
93 }
94 }
95 $html = trim($this->tracking_script . "\r\n" . $this->conversion_script);
96 if ($html != '') {
97 $content = preg_replace('#</body[^>]*>#si', "\r\n{$html}\r\n$0", $content);
98 }
99 return $content;
100 }
101
102 function action_save_post($post_id, $post) {
103 if ($post->post_type != 'revision') {
104 $options = array();
105 $options['enabled'] = isset($_POST['enable_gwo']) && ($_POST['enable_gwo'] == '1');
106 $options['control_script'] = isset($_POST['control_script']) ? trim($_POST['control_script']) : '';
107 $options['tracking_script'] = isset($_POST['tracking_script']) ? trim($_POST['tracking_script']) : '';
108 $options['conversion_script'] = isset($_POST['conversion_script']) ? trim($_POST['conversion_script']) : '';
109 if (!update_post_meta($post->ID, $this->meta, $options)) {
110 add_post_meta($post->ID, $this->meta, $options);
111 }
112 }
113 }
114
115 function meta_box_post() {
116
117 global $post;
118 $options = get_post_meta($post->ID, $this->meta, true);
119
120 if (is_array($options)) {
121 $options['enabled'] = isset($options['enabled']) ? (bool)$options['enabled'] : false;
122 $options['control_script'] = isset($options['control_script']) ? trim($options['control_script']) : '';
123 $options['tracking_script'] = isset($options['tracking_script']) ? trim($options['tracking_script']) : '';
124 $options['conversion_script'] = isset($options['conversion_script']) ? trim($options['conversion_script']) : '';
125 } else {
126 $options['enabled'] = false;
127 $options['control_script'] = '';
128 $options['tracking_script'] = '';
129 $options['conversion_script'] = '';
130 }
131
132 $url = get_bloginfo('wpurl') . '/' . PLUGINDIR . '/' . dirname(plugin_basename(__FILE__));
133
134?>
135<table border="0" width="100%">
136 <tr>
137 <td colspan="2">To use this plugin you must register a <a href="http://www.google.com/websiteoptimizer" target="_blank">Google Website Optimizer</a> account. It's free.</td>
138 </tr>
139 <tr>
140 <td colspan="2">&nbsp;</td>
141 </tr>
142 <tr>
143 <td colspan="2"><input type="checkbox" name="enable_gwo" value="1" <?php if ($options['enabled']) { echo('checked="checked"'); } ?> /> <label for="enable_gwo">Enable the Google Website Optimizer support for this page/post.</label></td>
144 </tr>
145 <tr>
146 <td colspan="2">&nbsp;</td>
147 </tr>
148 <tr>
149 <td>&nbsp;</td>
150 <td><label for="control_script" style="color: #CC6600; font-weight: bold;">Control Script</label></td>
151 </tr>
152 <tr>
153 <td width="35" valign="top"><img src="<?php echo($url); ?>/control_script.gif" align="Control Script" width="30" height="37" /></td>
154 <td><textarea rows="2" cols="40" name="control_script" tabindex="5" id="control_script" style="width: 98%"><?php echo(htmlentities($options['control_script'])); ?></textarea>
155 <br/>
156 <span style="font-size: 11px">Insert here the <strong style="color: #CC6600;">Control Script</strong> provided by the Google Website Optimizer. <br/>
157 You must fill this field only if this page/post is your <strong>Original page</strong>.</span> </td>
158 </tr>
159 <tr>
160 <td colspan="2">&nbsp;</td>
161 </tr>
162 <tr>
163 <td>&nbsp;</td>
164 <td><label for="tracking_script" style="color: #006600; font-weight: bold;">Tracking Script</label></td>
165 </tr>
166 <tr>
167 <td width="35" valign="top"><img src="<?php echo($url); ?>/tracking_script.gif" align="Tracking Script" width="30" height="37" /></td>
168 <td><textarea rows="2" cols="40" name="tracking_script" tabindex="5" id="tracking_script" style="width: 98%"><?php echo(htmlentities($options['tracking_script'])); ?></textarea>
169 <br/>
170 <span style="font-size: 11px">Insert here the <strong style="color: #006600;">Tracking Script</strong> provided by the Google Website Optimizer. <br/>
171 You must fill this field only if this page/post is your <strong>Original page</strong> or a <strong>Variation page</strong>.</span> </td>
172 </tr>
173 <tr>
174 <td colspan="2">&nbsp;</td>
175 </tr>
176 <tr>
177 <td>&nbsp;</td>
178 <td><label for="conversion_script" style="color: #660000; font-weight: bold;">Conversion Script</label></td>
179 </tr>
180 <tr>
181 <td width="35" valign="top"><img src="<?php echo($url); ?>/conversion_script.gif" align="Conversion Script" width="30" height="37" /></td>
182 <td><textarea rows="2" cols="40" name="conversion_script" tabindex="5" id="conversion_script" style="width: 98%"><?php echo(htmlentities($options['conversion_script'])); ?></textarea>
183 <br/>
184 <span style="font-size: 11px">Insert here the <strong style="color: #660000;">Conversion Script</strong> provided by the Google Website Optimizer. <br/>
185 You must fill this field only if this page/post is your <strong>Conversion page</strong>.</span> </td>
186 </tr>
187</table>
188<?php
189 }
190
191}
192
193$gwo = new GW04WP(GWO_META_NAME, GWO_CONTROL_IN_HEAD);
194
195?>
Note: See TracBrowser for help on using the repository browser.