Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors: plausible, DaanvandenBergh
Donate link: https://plausible.io/
Tags: analytics, google analytics, web analytics, stats, privacy
Requires at least: 5.3
Requires at least: 5.9
Tested up to: 6.6
Requires PHP: 7.0
Stable tag: 2.2.0
Expand Down
78 changes: 1 addition & 77 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,47 +219,6 @@ public function create_goals( $goals ) {
}
}

/**
* @param $old_settings
* @param $settings
*
* @return void
* @codeCoverageIgnore Because we don't want to test the API.
*/
public function maybe_create_woocommerce_funnel( $old_settings, $settings ) {
if ( ! Helpers::is_enhanced_measurement_enabled( 'revenue', $settings[ 'enhanced_measurements' ] ) || ! Integrations::is_wc_active() ) {
return; // @codeCoverageIgnore
}

$goals = [];
$woocommerce = new WooCommerce( false );

foreach ( $woocommerce->event_goals as $event_key => $event_goal ) {
// Don't add this goal to the funnel. Create it separately instead.
if ( $event_key === 'remove-from-cart' ) {
$this->create_goals( [ $this->create_goal_request( $event_goal ) ] );

continue;
}

if ( $event_key === 'purchase' ) {
$goals[] = $this->create_goal_request( $event_goal, 'Revenue', get_woocommerce_currency() );

continue;
}

if ( $event_key === 'view-product' ) {
$goals[] = $this->create_goal_request( $event_goal, 'Pageview', null, '/product*' );

continue;
}

$goals[] = $this->create_goal_request( $event_goal );
}

$this->create_funnel( __( 'Woo Purchase Funnel', 'plausible-analytics' ), $goals );
}

/**
* Creates a funnel and creates goals if they don't exist.
*
Expand Down Expand Up @@ -336,41 +295,6 @@ public function maybe_delete_goals( $old_settings, $settings ) {
update_option( 'plausible_analytics_enhanced_measurements_goal_ids', $goals );
}

/**
* Delete all custom WooCommerce event goals if Revenue setting is disabled. The funnel is deleted when the minimum
* required no. of goals is no longer met.
*
* @param $old_settings
* @param $settings
*
* @return void
* @codeCoverageIgnore Because we don't want to test if the API is working.
*/
public function maybe_delete_woocommerce_goals( $old_settings, $settings ) {
$enhanced_measurements = array_filter( $settings[ 'enhanced_measurements' ] );

// Setting is enabled, no need to continue.
if ( Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) ) {
return;
}

$goals = get_option( 'plausible_analytics_enhanced_measurements_goal_ids', [] );
$woo_integration = new WooCommerce( false );

foreach ( $goals as $id => $name ) {
$key = $this->array_search_contains( $name, $woo_integration->event_goals );

if ( $key ) {
$this->client->delete_goal( $id );

unset( $goals[ $id ] );
}
}

// Refresh the stored IDs in the DB.
update_option( 'plausible_analytics_enhanced_measurements_goal_ids', $goals );
}

/**
* Searches an array for the presence of $string within each element's value. Strips currencies using a regex, e.g.
* (USD), because these are added to revenue goals by Plausible.
Expand All @@ -387,7 +311,7 @@ public function array_search_contains( $string, $haystack ) {
}

foreach ( $haystack as $key => $value ) {
if ( strpos( $value, $string ) !== false ) {
if ( str_contains( $value, $string ) ) {
return $key;
}
}
Expand Down
31 changes: 30 additions & 1 deletion src/Admin/Upgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Plausible\Analytics\WP\Admin;

use Exception;
use Plausible\Analytics\WP\Admin\Provisioning\Integrations;
use Plausible\Analytics\WP\Helpers;

/**
Expand Down Expand Up @@ -77,6 +78,10 @@ public function run() {
$this->upgrade_to_210();
}

if ( version_compare( $plausible_analytics_version, '2.3.0', '<' ) ) {
$this->upgrade_to_230();
}

// Add required upgrade routines for future versions here.
}

Expand Down Expand Up @@ -254,9 +259,10 @@ private function upgrade_to_203() {
}

/**
* v2.0.8 and older contained a bug that
* v2.0.8 and older contained a bug that caused the Enhanced Measurement option to not be an array in some cases.
*
* @return void
* @codeCoverageIgnore
*/
public function upgrade_to_210() {
$settings = Helpers::get_settings();
Expand All @@ -269,4 +275,27 @@ public function upgrade_to_210() {

update_option( 'plausible_analytics_version', '2.1.0' );
}

/**
* If EDD is active and Ecommerce is enabled, create goals after updating the plugin.
*
* @since v2.3.0
*
* @return void
*
* @codeCoverageIgnore because all we'd be doing is testing the Plugins API.
*/
public function upgrade_to_230() {
$settings = Helpers::get_settings();

if ( Helpers::is_enhanced_measurement_enabled( 'revenue' ) ) {
$edd_provisioning = new Provisioning\Integrations\EDD( new Integrations() );
$provisioning = new Provisioning();

$provisioning->maybe_create_custom_properties( [], $settings );
$edd_provisioning->maybe_create_edd_funnel( [], $settings );
}

update_option( 'plausible_analytics_version', '2.3.0' );
}
}