Skip to content

Commit

Permalink
Initial implementation of WooCommerce integration with Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityrail authored and dereksmart committed Jan 5, 2017
1 parent e93b7e2 commit 44c43c3
Show file tree
Hide file tree
Showing 4 changed files with 418 additions and 4 deletions.
16 changes: 16 additions & 0 deletions sync/class.jetpack-sync-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ static function initialize_sender() {
add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 6 );
}

static function initialize_woocommerce() {
if ( class_exists( 'WooCommerce' ) ) {
add_filter( 'jetpack_sync_modules', array( 'Jetpack_Sync_Actions', 'add_woocommerce_sync_module' ) );
}
}

static function add_woocommerce_sync_module( $sync_modules ) {
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-woocommerce.php';
$sync_modules[] = 'Jetpack_Sync_Module_WooCommerce';
return $sync_modules;
}

static function sanitize_filtered_sync_cron_schedule( $schedule ) {
$schedule = sanitize_key( $schedule );
$schedules = wp_get_schedules();
Expand Down Expand Up @@ -385,6 +397,10 @@ static function get_sync_status() {
*/
add_action( 'plugins_loaded', array( 'Jetpack_Sync_Actions', 'init' ), 90 );

// Check for WooCommerce support
add_action( 'plugins_loaded', array( 'Jetpack_Sync_Actions', 'initialize_woocommerce' ), 5 );

// We need to define this here so that it's hooked before `updating_jetpack_version` is called
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'do_initial_sync' ), 10, 2 );
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'cleanup_on_upgrade' ) );

115 changes: 115 additions & 0 deletions sync/class.jetpack-sync-module-woocommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-module.php';

class Jetpack_Sync_Module_WooCommerce extends Jetpack_Sync_Module {

private $meta_whitelist = array(
'_product_id',
'_variation_id',
'_qty',
'_tax_class',
'_line_subtotal',
'_line_subtotal_tax',
'_line_total',
'_line_tax',
'_line_tax_data',
);

private $order_item_table_name;

public function __construct() {
global $wpdb;
$this->order_item_table_name = $wpdb->prefix . 'woocommerce_order_items';
}

function name() {
return "woocommerce";
}

public function init_listeners( $callable ) {
// orders
add_action( 'woocommerce_new_order', $callable, 10, 1 );
add_action( 'woocommerce_order_status_changed', $callable, 10, 3 );
add_action( 'woocommerce_payment_complete', $callable, 10, 1 );

// order items
add_action( 'woocommerce_new_order_item', $callable, 10, 4 );
add_action( 'woocommerce_update_order_item', $callable, 10, 4 );
add_filter( 'jetpack_sync_before_enqueue_woocommerce_new_order_item', array( $this, 'filter_order_item' ) );
add_filter( 'jetpack_sync_before_enqueue_woocommerce_update_order_item', array( $this, 'filter_order_item' ) );

// order item meta
$this->init_listeners_for_meta_type( 'order_item', $callable );
}

public function init_full_sync_listeners( $callable ) {
add_action( 'jetpack_full_sync_woocommerce_order_items', $callable ); // also sends post meta
}

public function get_full_sync_actions() {
return array( 'jetpack_full_sync_woocommerce_order_items' );
}

public function init_before_send() {
// full sync
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_woocommerce_order_items', array( $this, 'expand_order_item_ids' ) );
}

public function filter_order_item( $args ) {
$args[1] = $this->build_order_item( $args[1] );
return $args;
}

public function expand_order_item_ids( $args ) {
$order_item_ids = $args[0];

global $wpdb;

$order_item_ids_sql = implode( ', ', array_map( 'intval', $order_item_ids ) );

$order_items = $wpdb->get_results(
"SELECT * FROM $this->order_item_table_name WHERE order_item_id IN ( $order_item_ids_sql )"
);

return array(
$order_items,
$this->get_metadata( $order_item_ids, 'order_item', $this->meta_whitelist )
);
}

public function build_order_item( $order_item ) {
if ( is_numeric( $order_item ) ) {
global $wpdb;
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->order_item_table_name WHERE order_item_id = %d", $order_item ) );
} elseif ( is_array( $order_item ) ) {
return $order_item;
} else {
return (object)array(
'order_item_id' => $order_item->get_id(),
'order_item_type' => $order_item->get_type(),
'order_item_name' => $order_item->get_name(),
'order_id' => $order_item->get_order_id(),
);
}
}

public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
global $wpdb;

return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_woocommerce_order_items', $this->order_item_table_name, 'order_item_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
}

public function estimate_full_sync_actions( $config ) {
global $wpdb;

$query = "SELECT count(*) FROM $this->order_item_table_name WHERE " . $this->get_where_sql( $config );
$count = $wpdb->get_var( $query );

return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
}

private function get_where_sql( $config ) {
return '1=1';
}
}
30 changes: 26 additions & 4 deletions tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,51 @@
echo "Disregard Core's -c tests/phpunit/multisite.xml notice below." . PHP_EOL;
}

if ( "1" != getenv( 'JETPACK_TEST_WOOCOMMERCE' ) ) {
echo "To run Jetpack woocommerce tests, prefix phpunit with JETPACK_TEST_WOOCOMMERCE=1" . PHP_EOL;
} else {
define( 'JETPACK_WOOCOMMERCE_INSTALL_DIR', dirname( __FILE__ ) . '/../../../woocommerce' );
}

require $test_root . '/includes/functions.php';

// Activates this plugin in WordPress so it can be tested.
function _manually_load_plugin() {
if ( "1" == getenv( 'JETPACK_TEST_WOOCOMMERCE' ) ) {
require JETPACK_WOOCOMMERCE_INSTALL_DIR . '/woocommerce.php';
}
require dirname( __FILE__ ) . '/../../jetpack.php';
}

function _manually_install_woocommerce() {
// clean existing install first
define( 'WP_UNINSTALL_PLUGIN', true );
define( 'WC_REMOVE_ALL_DATA', true );
include( JETPACK_WOOCOMMERCE_INSTALL_DIR . '/uninstall.php' );

WC_Install::install();

// reload capabilities after install, see https://core.trac.wordpress.org/ticket/28374
$GLOBALS['wp_roles']->reinit();

echo "Installing WooCommerce..." . PHP_EOL;
}

// If we are running the uninstall tests don't load jepack.
if ( ! ( in_running_uninstall_group() ) ) {
tests_add_filter( 'plugins_loaded', '_manually_load_plugin' );
if ( "1" == getenv( 'JETPACK_TEST_WOOCOMMERCE' ) ) {
tests_add_filter( 'setup_theme', '_manually_install_woocommerce' );
}
}


require $test_root . '/includes/bootstrap.php';


// Load the shortcodes module to test properly.
if ( ! function_exists( 'shortcode_new_to_old_params' ) && ! in_running_uninstall_group() ) {
require dirname( __FILE__ ) . '/../../modules/shortcodes.php';

}


// Load attachment helper methods.
require dirname( __FILE__ ) . '/attachment_test_case.php';

Expand Down
Loading

0 comments on commit 44c43c3

Please sign in to comment.