table_name = $wpdb->prefix . 'ab_see'; $this->table_tracking_name = $wpdb->prefix . 'ab_see_tracking'; register_activation_hook( __FILE__, array( $this, 'install' ) ); add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array( $this, 'add_action_links' ) ); add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_shortcode( 'ab-see', array( $this, 'shortcode_absee' ) ); add_shortcode( 'ab-convert', array( $this, 'shortcode_abconvert' ) ); } public static function get_instance() { return self::$instance; } public function install() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE " . $this->table_name . " ( id VARCHAR(32) NOT NULL, description TEXT, created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, enabled BOOLEAN DEFAULT false, option_a TEXT, option_b TEXT, conversion_id TINYTEXT, UNIQUE KEY id (id) ) $charset_collate; CREATE TABLE " . $this->table_tracking_name . " ( id VARCHAR(32) NOT NULL, user_id VARCHAR(64) NOT NULL, user_group TINYINT, created datetime NOT NULL, converted datetime NOT NULL, UNIQUE KEY `id` (`id`,`user_id`,`user_group`) ) $charset_collate"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } public function add_action_links( $links ) { $new_links = array( 'Settings', ); return array_merge( $links, $new_links ); } /** * Add a link to a settings page. */ public function admin_menu() { add_menu_page( 'A/B See', 'A/B See', 'manage_options', self::DOMAIN . 'admin', array( $this, 'admin_page' ) ); } public function update_tracking( $test_id, $user_id, $user_group ) { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM `' . $this->table_tracking_name . '` WHERE id=%s AND user_id=%s', $test_id, $user_id ), ARRAY_A ); if ( FALSE == $result ) { $wpdb->insert( $this->table_tracking_name, array( 'id' => $test_id, 'user_id' => $user_id, 'user_group' => $user_group, 'created' => current_time( 'mysql' ), ), array( '%s', '%s', '%d', '%s' ) ); } } public function get_tests_with_conversion( $conversion_id ) { global $wpdb; $result_obj = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM `' . $this->table_name . '` WHERE conversion_id=%s', $conversion_id ), ARRAY_A ); return $result_obj; } public function update_conversion( $test_id, $user_id ) { global $wpdb; $wpdb->update( $this->table_tracking_name, array( 'converted' => current_time( 'mysql' ), ), array( 'id' => $test_id, 'user_id' => $user_id, ), array( '%s', '%s' ) ); } public function create_test( $test_id ) { global $wpdb; $wpdb->insert( $this->table_name, array( 'id' => $test_id, 'created' => current_time( 'mysql' ), ) ); } public function get_all_tests() { global $wpdb; $result_obj = $wpdb->get_results( 'SELECT * FROM `' . $this->table_name . '`', ARRAY_A ); foreach ( array_keys( $result_obj ) as $k ) { $result_obj[ $k ][ 'description' ] = stripslashes( $result_obj[ $k ][ 'description' ] ); $result_obj[ $k ][ 'option_a' ] = stripslashes( $result_obj[ $k ][ 'option_a' ] ); $result_obj[ $k ][ 'option_b' ] = stripslashes( $result_obj[ $k ][ 'option_b' ] ); } return $result_obj; } public function get_test( $test_id ) { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM `' . $this->table_name . '` WHERE id=%s', $test_id ), ARRAY_A ); if ( $result == null ) { return array(); } $result[ 'description' ] = stripslashes( $result[ 'description' ] ); $result[ 'option_a' ] = stripslashes( $result[ 'option_a' ] ); $result[ 'option_b' ] = stripslashes( $result[ 'option_b' ] ); return $result; } public function update_test( $args ) { global $wpdb; $required = array( 'id', 'description', 'option_a', 'option_b', 'conversion_id' ); foreach ( $required as $x ) { if ( ! isset( $args[ $x ] ) ) { return FALSE; } } $wpdb->update( $this->table_name, array( 'id' => $args[ 'id' ], 'description' => $args[ 'description' ], 'option_a' => $args[ 'option_a' ], 'option_b' => $args[ 'option_b' ], 'conversion_id' => $args[ 'conversion_id' ], ), array( 'id' => $args[ 'id' ], ) ); return TRUE; } public function toggle_test( $test_id ) { global $wpdb; $test = $this->get_test( $test_id ); if ( FALSE == $test ) { return FALSE; } $enabled = $test[ 'enabled' ] == TRUE ? FALSE : TRUE; $wpdb->update( $this->table_name, array( 'enabled' => $enabled, ), array( 'id' => $test_id, ) ); } public function delete_test( $test_id ) { global $wpdb; $test = $this->get_test( $test_id ); if ( FALSE == $test ) { return FALSE; } if ( ! isset( $_GET[ 'nonce' ] ) ) { ?>

Are you sure you want to delete the test ? (Yes, really delete the test!)

delete( $this->table_name, array( 'id' => $test_id, ) ); $wpdb->delete( $this->table_tracking_name, array( 'id' => $test_id, ) ); ?>

Test deleted.

get_test( $id ); if ( $test == FALSE ) { return; } ?>

Edit Test

[ab-see id=your_id]
[ab-convert id=your_conversion_id]
 
get_results( $wpdb->prepare( 'SELECT * FROM `' . $this->table_tracking_name . '` WHERE id=%s', $id ), ARRAY_A ); return $result_obj; } public function get_conversion_rate( $yes, $no ) { $total = $yes + $no; if ( $total > 0 ) { return 100 * $yes / ( $yes + $no ); } else { return 0; } } public function render_test_table( $test_obj, $enabled ) { ?> ' . __( 'Delete', self::DOMAIN ) . '' ); }?>
get_test( $id ); if ( $test == FALSE ) { return; } ?>

To use this test, add the following shortcode to the place you want to show your content:
[ab-see id=]

To register a conversion, add the following shortcode to the final page:
[ab-convert id=]

get_tracking( $id ); $group_obj = array( 1 => array( 'yes' => array(), 'no' => array() ), 2 => array( 'yes' => array(), 'no' => array() ), ); foreach ( $tracking_obj as $track ) { if ( strtotime( $track[ 'converted' ] ) > 0 ) { array_push( $group_obj[ $track[ 'user_group' ] ][ 'yes' ], $track[ 'converted' ] ); } else { array_push( $group_obj[ $track[ 'user_group' ] ][ 'no' ], $track[ 'converted' ] ); } } $group_a = round( $this->get_conversion_rate( count( $group_obj[ 1 ][ 'yes' ] ), count( $group_obj[ 1 ][ 'no' ] ) ), 2 ); $group_b = round( $this->get_conversion_rate( count( $group_obj[ 2 ][ 'yes' ] ), count( $group_obj[ 2 ][ 'no' ] ) ), 2 ); ?>

Group 1 conversions: % (/)

Group 2 conversions: % (/)

A/B See

Simple split testing for WordPress

create_test( $_POST[ 'create_id' ] ); } else if ( isset( $_POST[ 'update' ] ) ) { $this->update_test( $_POST ); } else if ( isset( $_GET[ 'toggle' ] ) ) { $this->toggle_test( $_GET[ 'toggle' ] ); } else if ( isset( $_GET[ 'edit_id' ] ) ) { $this->show_edit_page( $_GET[ 'edit_id' ] ); } else if ( isset( $_GET[ 'view_id' ] ) ) { $this->show_view_page( $_GET[ 'view_id' ] ); } else if ( isset( $_GET[ 'delete' ] ) ) { $this->delete_test( $_GET[ 'delete' ] ); } $test_obj = $this->get_all_tests(); echo( '

Active Tests

' ); $this->render_test_table( $test_obj, true ); echo( '

Inactive Tests

' ); $this->render_test_table( $test_obj, false ); ?>

Create a New Test

Test ID (unique, no spaces!):

get_test( $test_id ); if ( FALSE == $test || ! $test[ 'enabled' ] ) { return ''; } $user_id = $this->get_user_id(); $group = $this->get_group( $test_id, $user_id, 2 ); $this->update_tracking( $test_id, $user_id, $group ); if ( isset( $_GET[ 'group_override' ] ) ) { $group = intval( $_GET[ 'group_override' ] ); } $result = ''; if ( $group == 1 ) { $result = $test[ 'option_a' ]; } else if ( $group == 2 ) { $result = $test[ 'option_b' ]; } return do_shortcode( $result ); } public function shortcode_abconvert( $args ) { if ( ! isset( $args[ 'id' ] ) ) { return ''; } $conversion_id = $args[ 'id' ]; $user_id = $this->get_user_id(); $test_obj = $this->get_tests_with_conversion( $conversion_id ); foreach ( $test_obj as $test ) { if ( ! $test[ 'enabled' ] ) { continue; } $this->update_conversion( $test[ 'id' ], $user_id ); } return ''; } } $wp_ab_see = new WP_AB_See();