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; } ?> } public function get_tracking( $id ) { global $wpdb; $result_obj = $wpdb->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 ) { ?>| ID | Description | Created | Edit | Enabled | Delete' ); }?>|
|---|---|---|---|---|---|
| edit | Delete Test |
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=]