-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-mint-ab-testing-admin.php
374 lines (297 loc) · 14.1 KB
/
class-mint-ab-testing-admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Handles admin pages
*
* @since 0.9.0.3
* @version 0.9.0.6
*/
class Mint_AB_Testing_Admin
{
/**
* Hook into actions and filters here, along with any other global setup
* that needs to run when this plugin is invoked
*
* @since 0.9.0.3
* @version 0.9.0.6
*/
public function __construct() {
add_action( 'admin_menu', array( &$this,'admin_menu' ) );
add_action( 'admin_init', array( &$this,'register_settings' ) );
}
/**
* Add the admin menu page
*
* @since 0.9.0.3
* @version 0.9.0.6
*/
public function admin_menu() {
add_theme_page( __( 'A/B Testing Configuration', 'mint-ab-testing' ), __( 'A/B Testing', 'mint-ab-testing' ), 'manage_options', Mint_AB_Testing_Options::plugin_id, array( &$this, 'settings_page' ) );
}
/**
* Register the plugin settings with the WordPress settings API
*
* @since 0.9.0.3
* @version 0.9.0.7
*/
public function register_settings() {
register_setting( Mint_AB_Testing_Options::option_group, Mint_AB_Testing_Options::option_name, array( &$this, 'settings_section_validate_main' ) );
add_settings_section( Mint_AB_Testing_Options::plugin_id . '-main', __( 'Main Settings', 'mint-ab-testing' ), array( &$this, 'settings_section_description_main' ), Mint_AB_Testing_Options::plugin_id );
add_settings_field( Mint_AB_Testing_Options::option_group . '-endpoint', __( '"B" Theme URL Endpoint', 'mint-ab-testing' ), array( &$this, 'settings_field_endpoint' ), Mint_AB_Testing_Options::plugin_id, Mint_AB_Testing_Options::plugin_id . '-main' );
add_settings_field( Mint_AB_Testing_Options::option_group . '-alternate_theme', __( 'Theme Select', 'mint-ab-testing' ), array( &$this, 'settings_field_alternate_theme' ), Mint_AB_Testing_Options::plugin_id, Mint_AB_Testing_Options::plugin_id . '-main' );
add_settings_field( Mint_AB_Testing_Options::option_group . '-ratio', __( 'Ratio', 'mint-ab-testing' ), array( &$this, 'settings_field_ratio' ), Mint_AB_Testing_Options::plugin_id, Mint_AB_Testing_Options::plugin_id . '-main' );
add_settings_field( Mint_AB_Testing_Options::option_group . '-cookie_ttl', __( '"B" Theme TTL', 'mint-ab-testing' ), array( &$this, 'settings_field_cookie_ttl' ), Mint_AB_Testing_Options::plugin_id, Mint_AB_Testing_Options::plugin_id . '-main' );
add_settings_field( Mint_AB_Testing_Options::option_group . '-javascript_redirect', __( 'Javascript Redirect', 'mint-ab-testing' ), array( &$this, 'settings_field_javascript_redirect' ), Mint_AB_Testing_Options::plugin_id, Mint_AB_Testing_Options::plugin_id . '-main' );
add_settings_field( Mint_AB_Testing_Options::option_group . '-entrypoints', __( 'Entry Points', 'mint-ab-testing' ), array( &$this, 'settings_field_entrypoints' ), Mint_AB_Testing_Options::plugin_id, Mint_AB_Testing_Options::plugin_id . '-main' );
add_settings_field( Mint_AB_Testing_Options::option_group . '-enable', __( 'Enable A/B Testing', 'mint-ab-testing' ), array( &$this, 'settings_field_enable' ), Mint_AB_Testing_Options::plugin_id, Mint_AB_Testing_Options::plugin_id . '-main' );
}
/**
* Output the description HTML for the "Main" settings section
*
* @since 0.9.0.3
* @version 0.9.0.3
*/
public function settings_section_description_main() {
// Doesn't do anything, I just don't want to forget it's here
}
/**
* Output enable/disable settings field(s)
*
* @since 0.9.0.3
* @version 0.9.0.6
*/
public function settings_field_enable() {
$options = Mint_AB_Testing_Options::instance();
$settings_field_name = 'enable';
$id = $options::option_group . '-' . $settings_field_name;
$enable = $options->get_option( $settings_field_name );
echo '<label><input name="' . $options::option_name . '[' . $settings_field_name . ']" id="' . $id . '" type="radio" value="yes" ' . checked( ( 'yes' === $enable ), true, false ) . '/> ' . __( 'On', 'mint-ab-testing' ) . '</label><br />';
echo '<label><input name="' . $options::option_name . '[' . $settings_field_name . ']" id="' . $id . '" type="radio" value="no" ' . checked( ( 'no' === $enable ), true, false ) . '/> ' . __( 'Off', 'mint-ab-testing' ) . '</label><br />';
}
/**
* Output alternate theme select settings field
*
* @since 0.9.0.3
* @version 0.9.0.6
*/
public function settings_field_alternate_theme() {
$options = Mint_AB_Testing_Options::instance();
$settings_field_name = 'alternate_theme';
$id = $options::option_group . '-' . $settings_field_name;
$alternate_theme = $options->get_option( $settings_field_name );
$current_theme = get_current_theme();
$themes = get_allowed_themes();
unset( $themes[$current_theme] );
printf( __( '"A" Theme: %s<br />', 'mint-ab-testing' ), $current_theme );
_e( '"B" Theme: ', 'mint-ab-testing' );
if ( empty( $themes ) ) {
_e( 'You have no other themes installed!', 'mint-ab-testing' );
echo '<input name="' . $options::option_name . '[' . $settings_field_name . ']" id="' . $id . '" type="hidden" value="" />';
} else {
echo '<select name="' . $options::option_name . '[' . $settings_field_name . ']" id="' . $id . '">';
echo '<option value="">' . __( 'Select one...', 'mint-ab-testing' ) . '</option>';
foreach ( $themes as $name => $data ) {
echo '<option value="' . $name . '"' . selected( ( $alternate_theme === $name ), true, false ) . '>' . $name . '</option>';
}
echo '</select>';
}
}
/**
* Output A/B ratio settings field
*
* @since 0.9.0.3
* @version 0.9.0.6
*
* @todo Use jQuery UI slider
*/
public function settings_field_ratio() {
$options = Mint_AB_Testing_Options::instance();
$settings_field_name = 'ratio';
$ratio = $options->get_option( $settings_field_name );
$id = $options::option_group . '-' . $settings_field_name;
$field_output = '<input name="' . $options::option_name . '[' . $settings_field_name . ']" id="' . $id . '" type="text" size="4" value="' . $ratio . '" />';
printf( __( 'Show the "B" Theme %s%% of the time', 'mint-ab-testing' ), $field_output );
echo '<br /><span class="description">' . __( 'Visitors will not be redirected from the "B" theme back to the "A" theme. If a visitor has an "A" theme cookie and lands on a "B" theme URL (e.g., by following a link), they will stay on the "B" theme. If they later return to the site via the "A" theme they will stay on the "A" theme because they still have their "A" theme cookie.', 'mint-ab-testing' ) . '</span>';
}
/**
* Output cookie TTL settings field
*
* @since 0.9.0.3
* @version 0.9.0.7
*/
public function settings_field_cookie_ttl() {
$options = Mint_AB_Testing_Options::instance();
$settings_field_name = 'cookie_ttl';
$cookie_ttl = $options->get_option( $settings_field_name );
$id = $options::option_group . '-' . $settings_field_name;
$field_output = '<input name="' . $options::option_name . '[' . $settings_field_name . ']" id="' . $id . '" type="text" size="3" value="' . $cookie_ttl . '" />';
printf( __( 'Visitors who see the "B" Theme will see it for %s days', 'mint-ab-testing' ), $field_output );
echo '<br /><span class="description">' . __( 'Set to "0" to expire at the end of the browser session.', 'mint-ab-testing' ) . '</span>';
}
/**
* Output javascript redirect settings field(s)
*
* @since 0.9.0.7
* @version 0.9.0.7
*/
public function settings_field_javascript_redirect() {
$options = Mint_AB_Testing_Options::instance();
$settings_field_name = 'javascript_redirect';
$id = $options::option_group . '-' . $settings_field_name;
$javascript_redirect = $options->get_option( $settings_field_name );
echo '<label><input name="' . $options::option_name . '[' . $settings_field_name . ']" id="' . $id . '" type="checkbox" value="1" ' . checked( ( 1 == $javascript_redirect ), true, false ) . '/> ' . __( 'Use Javascript to redirect to "B" theme', 'mint-ab-testing' ) . '</label><br />';
echo '<span class="description">' . __( 'If your page requests usually get returned from a cache (proxy caching or full page caching) you should enable this.', 'mint-ab-testing' ) . '</span>';
// Additional help text for handling analytics
if ( class_exists('Pmc_Google_Analytics') ) {
$additional_help_text = __( 'It looks like you are using the PMC Google Analytics plugin. Referrer tracking will be handled automatically.', 'mint-ab-testing' );
} elseif ( class_exists('Yoast_GA_Plugin_Admin') ) {
$additional_help_text = __( 'It looks like you are using the Google Analytics for WordPress plugin. Referrer tracking will be handled automatically.', 'mint-ab-testing' );
} else {
$additional_help_text = __( '<br />This plugin will handle this automatically if you are using the <strong>PMC Google Analytics</strong> or <strong>Google Analytics for WordPress</strong> plugin, otherwise you will need to implement this yourself.', 'mint-ab-testing' );
}
echo '<br /><span class="description">' . sprintf( __( 'Using javascript redirects with an analytics package like Google Analytics or Omniture requires some additional work to properly track referrers. %s', 'mint-ab-testing' ), $additional_help_text ) . '</span>';
}
/**
* Output entry point settings field(s)
*
* @since 0.9.0.7
* @version 0.9.0.7
*/
public function settings_field_entrypoints() {
$options = Mint_AB_Testing_Options::instance();
$settings_field_name = 'entrypoints';
$id = $options::option_group . '-' . $settings_field_name;
$entrypoints = $options->get_option( $settings_field_name );
echo '<div id="' . $id . '">';
foreach ( $entrypoints as $entrypoint => $enabled ) {
switch ( $entrypoint ) {
case 'home':
$label = __( 'Home', 'mint-ab-testing' );
break;
case 'singular':
$label = __( 'Single pages: Post, Page, Attachment, and single custom post type pages', 'mint-ab-testing' );
break;
case 'archive':
$label = __( 'Archive pages: Tag, Category, custom taxonomy, date-based archives, and custom post type archives', 'mint-ab-testing' );
break;
case 'search':
$label = __( 'Search results', 'mint-ab-testing' );
break;
case '404':
$label = __( '404 Not Found error pages', 'mint-ab-testing' );
break;
default:
// No default, has to be specified above
$label = '';
break;
}
echo '<label><input name="' . $options::option_name . '[' . $settings_field_name . '][' . $entrypoint . ']" id="' . $id . '-' . $entrypoint . '" type="checkbox" value="1" ' . checked( ( 1 == $enabled ), true, false ) . '/> ' .$label . '</label><br />';
}
echo '</div>';
echo '<a style="cursor: pointer;" onclick="jQuery(\'#' . $id . ' input\').each(function(){ jQuery(this).attr(\'checked\', true); });">' . __( 'Select All', 'mint-ab-testing' ) . '</a> / <a style="cursor: pointer;" onclick="jQuery(\'#' . $id . ' input\').each(function(){ jQuery(this).attr(\'checked\', false); });">' . __( 'Select None', 'mint-ab-testing' ) . '</a><br />';
echo '<span class="description">' . __( 'Only run the A/B test when landing on one of the page types above.', 'mint-ab-testing' ) . '</span>';
}
/**
* Output the endpoint settings field
*
* @since 0.9.0.3
* @version 0.9.0.6
*/
public function settings_field_endpoint() {
$options = Mint_AB_Testing_Options::instance();
$settings_field_name = 'endpoint';
$endpoint = $options->get_option( $settings_field_name );
$id = $options::option_group . '-' . $settings_field_name;
echo home_url() . '/<input name="' . $options::option_name . '[' . $settings_field_name . ']" id="' . $id . '" type="text" size="4" value="' . $endpoint . '" />/';
echo '<br /><span class="description">' . __( 'This identifies the alternate theme ("B" theme). Users who visit a URL with this at the end will see the "B" theme.', 'mint-ab-testing' ) . '</span>';
}
/**
* Validate the options being saved in the "Main" settings section
*
* @since 0.9.0.3
* @version 0.9.0.7
*
* @todo Error messages / warnings
* @todo Don't make this an all-in-one function
*/
public function settings_section_validate_main( $saved_options ) {
$options = Mint_AB_Testing_Options::instance();
foreach ( $saved_options as $key => &$value ) {
switch ( $key ) {
case 'alternate_theme':
// Prevent invalid or nonexistent theme names from being saved
$themes = get_allowed_themes();
if ( ! isset( $themes[$value] ) ) {
$value = '';
}
break;
case 'ratio':
// Make sure ratio is an integer
$value = intval( $value );
if ( $value > 100 ) {
$value = 100;
} elseif ( $value < 0 ) {
$value = 0;
}
break;
case 'enable':
// Make sure "enable" is one of our valid values
$value = ( in_array( $value, array( 'yes', 'no', 'preview' ) ) ) ? $value : 'no';
break;
case 'cookie_ttl':
// Make sure ratio is an integer
$value = intval( $value );
if ( $value < 1 ) {
$value = 0;
} else {
$value = ( 60 * 60 * 24 * 1 );
}
break;
case 'used_endpoints':
$value = explode( ',', $value );
break;
case 'javascript_redirect':
$value = intval( $value );
break;
case 'entrypoints':
// Only checked (true) settings get POSTed. Any false/unset entrypoints are not set. So we'll take the defaults, and if it hasn't been POSTed we can safely assume it's false. Side benefit: we don't have to worry about invalid/unaccounted keys.
$entrypoint_defaults = $options->get_option_default( 'entrypoints' );
foreach ( $entrypoint_defaults as $entrypoint => &$enabled ) {
if ( ! isset( $value[$entrypoint] ) ) {
$enabled = false;
}
}
$value = $entrypoint_defaults;
break;
default:
// Do nothing
break;
}
// Reset the options in the current instance
$options->set_option( $key, $value );
}
return $saved_options;
}
/**
* Output the settings page
*
* @since 0.9.0.3
* @version 0.9.0.6
*/
public function settings_page() {
?>
<div class="wrap">
<h2><?php _e( 'Mint A/B Testing', 'mint-ab-testing' ); ?></h2>
<form method="post" action="options.php">
<?php
settings_fields( Mint_AB_Testing_Options::option_group );
do_settings_sections( Mint_AB_Testing_Options::plugin_id );
?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'mint-ab-testing' ) ?>" />
</p>
</form>
</div>
<?php
}
}
// EOF