Skip to content

Commit

Permalink
Updates to 6.0.27
Browse files Browse the repository at this point in the history
  • Loading branch information
Woo committed Jun 25, 2024
1 parent 46cb0e0 commit 4fbcfe2
Show file tree
Hide file tree
Showing 20 changed files with 431 additions and 327 deletions.
4 changes: 2 additions & 2 deletions automatewoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: AutomateWoo
* Plugin URI: https://automatewoo.com
* Description: Powerful marketing automation for your WooCommerce store.
* Version: 6.0.26
* Version: 6.0.27
* Author: WooCommerce
* Author URI: https://woocommerce.com
* License: GPLv3
Expand Down Expand Up @@ -37,7 +37,7 @@
defined( 'ABSPATH' ) || exit;

define( 'AUTOMATEWOO_SLUG', 'automatewoo' );
define( 'AUTOMATEWOO_VERSION', '6.0.26' ); // WRCS: DEFINED_VERSION.
define( 'AUTOMATEWOO_VERSION', '6.0.27' ); // WRCS: DEFINED_VERSION.
define( 'AUTOMATEWOO_FILE', __FILE__ );
define( 'AUTOMATEWOO_PATH', __DIR__ );
define( 'AUTOMATEWOO_MIN_PHP_VER', '7.4.0' );
Expand Down
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
*** AutomateWoo Changelog ***

2024-06-25 - version 6.0.27
* Dev - PHPCS for Memberships.
* Dev - PHPCS for Send Email actions.

2024-06-18 - version 6.0.26
* Dev - fix unit tests for protecting admin roles.
* Tweak - Replace woo.com references with woocommerce.com.
Expand Down
11 changes: 7 additions & 4 deletions includes/Actions/Memberships_Abstract.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* @class Action_Memberships_Abstract
* @since 2.8
*/
abstract class Action_Memberships_Abstract extends Action {

function load_admin_details() {
/**
* Method to set the action's admin props.
*/
public function load_admin_details() {
$this->group = __( 'Membership', 'automatewoo' );
}

}
96 changes: 59 additions & 37 deletions includes/Actions/Memberships_Change_Plan.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* @class Action_Memberships_Change_Plan
* @since 2.8
*/
class Action_Memberships_Change_Plan extends Action_Memberships_Abstract {

/**
* The data items required by the action.
*
* @var array
*/
public $required_data_items = [ 'customer' ];


function load_admin_details() {
/**
* Method to set the action's admin props.
*/
public function load_admin_details() {
parent::load_admin_details();
$this->title = __( "Create / Change Membership Plan For User", 'automatewoo' );
$this->title = __( 'Create / Change Membership Plan For User', 'automatewoo' );
$this->description = __( "Changes the plan of a user's active membership. If no active membership exists a new membership can optionally be created.", 'automatewoo' );
}


function load_fields() {
/**
* Method to load the action's fields.
**/
public function load_fields() {

$plans = Memberships_Helper::get_membership_plans();

Expand All @@ -46,17 +56,19 @@ function load_fields() {
$this->add_field( $from_plan );
$this->add_field( $to_plan );
$this->add_field( $allow_create );

}


function run() {
/**
* Runs the action
*/
public function run() {

$customer = $this->workflow->data_layer()->get_customer();
$customer = $this->workflow->data_layer()->get_customer();
$existing_plan_id = absint( $this->get_option( 'from_plan' ) );
$new_plan_id = absint( $this->get_option( 'to_plan' ) );
$allow_create = $this->get_option( 'allow_create' );
$new_plan = $new_plan_id ? wc_memberships_get_membership_plan( $new_plan_id ) : false;
$new_plan_id = absint( $this->get_option( 'to_plan' ) );
$allow_create = $this->get_option( 'allow_create' );
$new_plan = $new_plan_id ? wc_memberships_get_membership_plan( $new_plan_id ) : false;

if ( ! $customer->is_registered() || ! $new_plan ) {
return;
Expand All @@ -73,65 +85,75 @@ function run() {
$membership = $existing_plan_id ? wc_memberships_get_user_membership( $customer->get_user_id(), $existing_plan_id ) : false;

if ( $membership ) {
$this->change_membership_plan( $membership, $new_plan );
}
elseif ( $allow_create ) {
$this->change_membership_plan( $membership, $new_plan );
} elseif ( $allow_create ) {
// if no existing plan and allow create is checked, create a new plan for the user
$this->create_membership( $customer, $new_plan );
}

}


/**
* @param Customer $customer
* @param \WC_Memberships_Membership_Plan $new_plan
*
* @throws \Exception
* @throws \Exception If an error happens creating the membership.
*/
function create_membership( $customer, $new_plan ) {
public function create_membership( $customer, $new_plan ) {

$membership = wc_memberships_create_user_membership([
'user_id' => $customer->get_user_id(),
'plan_id' => $new_plan->get_id(),
]);
$membership = wc_memberships_create_user_membership(
[
'user_id' => $customer->get_user_id(),
'plan_id' => $new_plan->get_id(),
]
);

if ( is_wp_error( $membership ) ) {
throw new \Exception( $membership->get_error_message() );
throw new \Exception( esc_attr( $membership->get_error_message() ) );
}

$membership->add_note(
sprintf(
__( 'Membership created by AutomateWoo workflow #%s', 'automatewoo' ),
$this->workflow->get_id()
esc_textarea(
sprintf(
// translators: The Workflow ID
__( 'Membership created by AutomateWoo workflow #%s', 'automatewoo' ),
$this->workflow->get_id()
)
)
);

$this->workflow->log_action_note( $this, sprintf(
__( 'Membership #%s successfully created.', 'automatewoo' ),
$membership->get_id()
));
$this->workflow->log_action_note(
$this,
sprintf(
// translators: The Membership ID
__( 'Membership #%s successfully created.', 'automatewoo' ),
$membership->get_id()
)
);
}


/**
* @param \WC_Memberships_User_Membership $membership
* @param \WC_Memberships_Membership_Plan $new_plan
*/
function change_membership_plan( $membership, $new_plan ) {
public function change_membership_plan( $membership, $new_plan ) {

$membership->add_note(
sprintf(
__( 'Membership plan changed from %s to %s by AutomateWoo workflow:#%s', 'automatewoo' ),
// translators: %1$s The Membership plan name, %2$s the new plan name, %3$s The Workflow ID
__( 'Membership plan changed from %1$s to %2$s by AutomateWoo workflow:#%3$s', 'automatewoo' ),
$membership->get_plan()->get_name(),
$new_plan->get_name(),
$this->workflow->get_id()
)
);

wp_update_post([
'ID' => $membership->get_id(),
'post_parent' => $new_plan->get_id()
]);
wp_update_post(
[
'ID' => $membership->get_id(),
'post_parent' => $new_plan->get_id(),
]
);
}
}
43 changes: 28 additions & 15 deletions includes/Actions/Memberships_Delete_User_Membership.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* @class Action_Memberships_Delete_User_Membership
* @since 2.9
*/
class Action_Memberships_Delete_User_Membership extends Action_Memberships_Abstract {

/**
* The data items required by the action.
*
* @var array
*/
public $required_data_items = [ 'customer' ];


function load_admin_details() {
/**
* Method to set the action's admin props.
*
* Admin props include: title, group and description.
*/
public function load_admin_details() {
parent::load_admin_details();
$this->title = __( "Delete Membership For User", 'automatewoo' );
$this->title = __( 'Delete Membership For User', 'automatewoo' );
}


function load_fields() {
/**
* Method to load the action's fields.
*/
public function load_fields() {

$plans = Memberships_Helper::get_membership_plans();

Expand All @@ -31,17 +43,18 @@ function load_fields() {
->set_required();

$this->add_field( $plan );

}


/**
* @throws \Exception
* Run the action
*
* @throws \Exception When an error happens.
*/
function run() {
public function run() {

$customer = $this->workflow->data_layer()->get_customer();
$plan_id = absint( $this->get_option( 'plan' ) );
$plan_id = absint( $this->get_option( 'plan' ) );

if ( ! $customer->is_registered() || ! $plan_id ) {
return;
Expand All @@ -59,11 +72,11 @@ function run() {
$success = wp_delete_post( $membership_id, true );

if ( $success ) {
// translators: The Membership ID
$this->workflow->log_action_note( $this, sprintf( __( 'Deleted membership #%s', 'automatewoo' ), $membership_id ) );
}
else {
throw new \Exception( sprintf( __( 'Failed deleting membership #%s', 'automatewoo' ), $membership_id ) );
} else {
// translators: The Membership ID
throw new \Exception( esc_textarea( sprintf( __( 'Failed deleting membership #%s', 'automatewoo' ), $membership_id ) ) );
}
}

}
Loading

0 comments on commit 4fbcfe2

Please sign in to comment.