Plugin Directory

Changeset 3234638


Ignore:
Timestamp:
02/04/2025 11:34:51 AM (14 months ago)
Author:
DaveFX
Message:

Deploy from Git

Location:
dfx-woo-role-changer/trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • dfx-woo-role-changer/trunk/dfx-woo-role-changer.php

    r3233947 r3234638  
    44 * Plugin Name: DFX Automatic Role Changer for WooCommerce
    55 * Description: Allows the automatic assignation of roles to users on product purchases in WooCommerce
    6  * Version:     20250203
     6 * Version:     20250204
    77 * Author:      David Marín Carreño
    88 * Author URI:  https://davefx.com
     
    2424 *
    2525 * @package   DFX-Woo-Role-Changer
    26  * @version   20250203
     26 * @version   20250204
    2727 * @author    David Marín Carreño <davefx@davefx.com>
    2828 * @copyright Copyright (c) 2020-2025 David Marín Carreño
     
    3232 */
    3333defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
     34const DFX_WOO_ROLE_CHANGER_VERSION = '20250204';
    3435if ( function_exists( 'dfx_woo_role_changer_fs' ) ) {
    3536    dfx_woo_role_changer_fs()->set_basename( false, __FILE__ );
     
    7778        public $plugin_name = '';
    7879
     80        public const PLUGIN_DIR_PATH = __DIR__;
     81
     82        public $PLUGIN_DIR_URL;
     83
    7984        /**
    8085         * Returns the instance.
     
    97102         */
    98103        private function __construct() {
     104            $this->PLUGIN_DIR_URL = plugin_dir_url( __FILE__ );
    99105            // Set plugin file, removing the need to use __FILE__
    100106            if ( strpos( __FILE__, WP_PLUGIN_DIR ) !== false ) {
     
    263269                }
    264270            }
    265             woocommerce_wp_select( array(
     271            woocommerce_wp_select( apply_filters( 'dfx_wrc_role_selector_args', array(
    266272                'id'          => 'dfxwcrc_role_assignment',
    267273                'value'       => get_post_meta( get_the_ID(), '_dfxwcrc_role_assignment', true ) ?? 'none',
     
    270276                'description' => $description,
    271277                'options'     => $options,
    272             ) );
     278                'class'       => 'wc-enhanced-select',
     279            ), get_the_ID() ) );
     280            do_action( 'dfx_woo_role_changer_product_options' );
    273281            echo '</div>';
    274282        }
     
    279287            }
    280288            $available_roles = wp_roles()->get_names();
    281             $assignment = sanitize_text_field( $_POST['dfxwcrc_role_assignment'] );
     289            $assignment = apply_filters( 'dfx_wrc_product_new_role', sanitize_text_field( $_POST['dfxwcrc_role_assignment'] ) );
    282290            if ( $assignment !== 'none' ) {
    283                 if ( array_key_exists( $assignment, $available_roles ) ) {
     291                if ( apply_filters(
     292                    'dfx_wrc_is_valid_role_value',
     293                    array_key_exists( $assignment, $available_roles ),
     294                    $assignment,
     295                    $available_roles
     296                ) ) {
    284297                    update_post_meta( $id, '_dfxwcrc_role_assignment', $assignment );
    285298                }
     
    287300                delete_post_meta( $id, '_dfxwcrc_role_assignment' );
    288301            }
     302            do_action( 'dfx_woo_role_changer_product_options_save', $id, $post );
    289303        }
    290304
     
    292306            $roles = [];
    293307            $order = wc_get_order( $order_id );
    294             $product_items = $order->get_items();
     308            $order_product_items = $order->get_items();
    295309            $user_id = $order->get_user_id();
    296310            if ( !$user_id ) {
     
    298312            }
    299313            $user = new WP_User($user_id);
    300             foreach ( $product_items as $product_item ) {
    301                 $role = get_post_meta( $product_item->get_product_id(), '_dfxwcrc_role_assignment', true );
     314            foreach ( $order_product_items as $order_product_item ) {
     315                $role = get_post_meta( $order_product_item->get_product_id(), '_dfxwcrc_role_assignment', true );
    302316                if ( empty( $role ) || empty( trim( $role ) ) ) {
    303317                    continue;
     
    310324                    }
    311325                }
     326                $roles = apply_filters(
     327                    'dfx_woo_role_changer_roles_for_product',
     328                    $roles,
     329                    $order_product_item,
     330                    $order
     331                );
    312332            }
    313333            if ( count( $roles ) == 0 ) {
     
    348368         * @param WP_User $user
    349369         * @param string $new_role
    350          * @param WC_Order | null $order
     370         * @param WC_Order | null $order Order object where the role change is being notified
    351371         * @param string $note
    352372         *
     
    359379            $note = ''
    360380        ) {
     381            $new_role = apply_filters(
     382                'dfx_woo_role_changer_maybe_add_role_to_user',
     383                $new_role,
     384                $user,
     385                $order
     386            );
    361387            if ( !in_array( $new_role, $user->roles ) ) {
    362388                if ( $this->get_current_mode() === 'replace_roles' ) {
     
    383409                }
    384410            }
     411            do_action(
     412                'dfx_woo_role_changer_role_maybe_assigned',
     413                $user,
     414                $new_role,
     415                $order->get_id()
     416            );
    385417        }
    386418
     
    388420         * @param WP_User $user
    389421         * @param string $new_role
    390          * @param WC_Order | null $order
     422         * @param WC_Order | null $order Order object where the role change is being notified
     423         * @param string $note
    391424         *
    392425         * @return void
  • dfx-woo-role-changer/trunk/readme.txt

    r3233947 r3234638  
    55Requires at least: 3.1
    66Tested up to: 6.6
    7 Stable tag: 20250203
     7Stable tag: 20250204
    88License: GPLv3
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    2222* Integrates seamlessly with WooCommerce.
    2323
     24* *Premium*: Allows defining, per product, a role validity period in days after the purchase, so the role granted in the purchase will be automatically removed after that period.
     25
    2426* *Premium*: Supports role management tied to subscription products, supporting [WooCommerce Subscriptions](https://woocommerce.com/products/woocommerce-subscriptions/),
    2527  [YITH WooCommerce Subscription](https://wordpress.org/plugins/yith-woocommerce-subscription/) and
    2628  [WP Swings Subscriptions for WooCommerce](https://wordpress.org/plugins/subscriptions-for-woocommerce/).
     29
     30* *Premium*: Allows defining multiple roles per product.
    2731
    2832== Installation ==
     
    5054= Setting Up Role Assignments =
    5155
    52 * Go to WooCommerce > Settings > Role Changer.
     56* Edit a product from your catalog.
    5357
    54 * Select a product from your catalog.
     58* Choose the role (or several roles *PREMIUM*) to assign when the product is purchased.
    5559
    56 * Choose the role to assign when the product is purchased.
     60* *Premium*: Define a role validity period in days after the purchase.
    5761
    58 * (Optional) Configure advanced settings to fine-tune role assignments.
     62* In WooCommerce > Settings > Role Changer you can configure advanced settings to fine-tune role assignments, such as defining if the new role must be added to the user,
     63  or if the new role should just replace the previous one.
    5964
    6065* Save changes.
    6166
    6267== Premium Features ==
     68
     69= Define Role Validity Periods =
     70
     71The premium version allows defining a role validity period in days after the purchase. The role granted in the purchase will be automatically removed after that period.
    6372
    6473= Manages roles following the lifecycle of subscription products =
     
    95104* If the plugin is configured to add roles, all the roles will be assigned to the user. We recommend using a plugin like "Members" to manage multiple roles per user.
    96105
    97 = 2. Can I assign multiple roles to a single product? =
     106= 2. What happens if a user purchases a product granting a role several times? =
    98107
    99 Yes, you can assign multiple roles. Users will gain all assigned roles upon purchase.
     108In the Premium version, the validity period for a purchase is added to the previously existing one.
    100109
    101 = 3. How do I upgrade to the premium version? =
     110= 3. Can I assign multiple roles to a single product? =
     111
     112In the Premium version you can assign multiple roles. Users will gain all assigned roles upon purchase.
     113
     114= 4. How do I upgrade to the premium version? =
    102115
    103116Visit the [official plugin page](https://davefx.com/en/dfx-automatic-role-changer-for-woocommerce/) to purchase the premium version. After purchasing, install and activate it like the free version.
     
    134147
    135148== Changelog ==
     149
     150= 20250204 =
     151
     152* The role selector is now rendered as a Select2 dropdown, allowing an easier selection of roles.
     153
     154* *Premium*: Added support for roles with a validity period. The role granted in the purchase will be automatically removed after that period.
     155
     156* *Premium*: Added support for multiple roles per product.
    136157
    137158= 20250203 =
Note: See TracChangeset for help on using the changeset viewer.