Prevent showing WC Customers UI to new WC website

[MAILPOET-2206]
This commit is contained in:
Pavel Dohnal
2019-08-21 13:05:31 +02:00
committed by M. Shull
parent 25ddc80a9a
commit 69e9b099d8
8 changed files with 27 additions and 31 deletions

View File

@@ -106,7 +106,7 @@ class Changelog {
!in_array($_GET['page'], ['mailpoet-revenue-tracking-permission', 'mailpoet-woocommerce-list-import', 'mailpoet-welcome-wizard', 'mailpoet-migration'])
&& !$this->settings->get('woocommerce_import_screen_displayed')
&& $this->wooCommerceHelper->isWooCommerceActive()
&& $this->wooCommerceHelper->getOrdersCount() >= 1
&& $this->wooCommerceHelper->getOrdersCountCreatedBefore($this->settings->get('installed_at')) > 0
&& $this->wp->currentUserCan('administrator')
) {
$this->url_helper->redirectTo($this->wp->adminUrl('admin.php?page=mailpoet-woocommerce-list-import'));

View File

@@ -39,7 +39,7 @@ class PrivacyPolicy {
WPFunctions::get()->__('No identifiable information is otherwise tracked outside this website except for the email address.', 'mailpoet') .
'</p>'
);
$helper = new WooCommerceHelper(new WPFunctions);
$helper = new WooCommerceHelper();
if ($helper->isWooCommerceActive()) {
$content .= (
'<p> ' .

View File

@@ -16,7 +16,7 @@ class Analytics extends AbstractExtension {
public function getFunctions() {
$settings = new SettingsController();
$analytics = new AnalyticsGenerator(
new Reporter($settings, new WooCommerceHelper(new WPFunctions())),
new Reporter($settings, new WooCommerceHelper()),
$settings
);
return [

View File

@@ -568,12 +568,4 @@ class Functions {
}
}
/**
* @param string|null $type
* @param string|null $permission
* @return object
*/
function wpCountPosts($type = null, $permission = null) {
return wp_count_posts($type, $permission);
}
}

View File

@@ -1,20 +1,8 @@
<?php
namespace MailPoet\WooCommerce;
use MailPoet\WP\Functions;
class Helper {
/** @var Functions */
private $wp;
function __construct(Functions $wp = null) {
if (!$wp) {
$wp = Functions::get();
}
$this->wp = $wp;
}
function isWooCommerceActive() {
return class_exists('WooCommerce');
}
@@ -47,11 +35,13 @@ class Helper {
return get_woocommerce_currency();
}
function getOrdersCount() {
$counts = $this->wp->wpCountPosts('shop_order');
return array_reduce((array)$counts, function($sum, $count_for_state) {
return $sum + (int)$count_for_state;
});
function getOrdersCountCreatedBefore($date_time) {
global $wpdb;
$result = $wpdb->get_var( "
SELECT DISTINCT count(p.ID) FROM {$wpdb->prefix}posts as p
WHERE p.post_type = 'shop_order' AND p.post_date < '{$date_time}'
" );
return (int)$result;
}
function getRawPrice($price, array $args = []) {

View File

@@ -42,6 +42,10 @@ class WooCommerceOrder {
return $this->update(['status' => $status]);
}
function withDateCreated($date) {
return $this->update(['date_created' => $date]);
}
/**
* @param array $customer_data Customer created via WooCommerceCustomer factory
* @return $this
@@ -88,7 +92,15 @@ class WooCommerceOrder {
}
$create_output = $this->tester->cliToArray($cmd);
$order_out = $this->tester->cliToArray("wc shop_order get $create_output[0] --format=json --allow-root --user=admin");
return json_decode($order_out[0], true);
$order = json_decode($order_out[0], true);
if (isset($this->data['date_created'])) {
wp_update_post([
'ID' => $order['id'],
'post_date' => $this->data['date_created'],
'post_date_gmt' => get_gmt_from_date( $this->data['date_created'] ),
]);
}
return $order;
}
/**

View File

@@ -84,7 +84,9 @@ class WooCommerceListImportPageCest {
function importListPageRedirectionTest(\AcceptanceTester $I) {
$settings_factory = new Settings();
$settings_factory->withWooCommerceListImportPageDisplayed(false);
$order = $this->order_factory->create();
$order = $this->order_factory
->withDateCreated('2001-08-22T11:11:56') // any time in the past. Must be before the plugin activation
->create();
$I->login();
$I->amOnMailpoetPage('Emails');
$I->seeInCurrentUrl('wp-admin/admin.php?page=mailpoet-woocommerce-list-import');

View File

@@ -23,7 +23,7 @@ class AnalyticsTest extends \MailPoetTest {
parent::_before();
$this->settings = new SettingsController();
$this->analytics = new Analytics(
new Reporter($this->settings, new WooCommerceHelper(new WPFunctions)),
new Reporter($this->settings, new WooCommerceHelper()),
$this->settings
);
// Remove premium plugin hooks so that tests pass also with premium active