Changeset 3234638
- Timestamp:
- 02/04/2025 11:34:51 AM (14 months ago)
- Location:
- dfx-woo-role-changer/trunk
- Files:
-
- 2 added
- 2 edited
-
assets (added)
-
assets/js (added)
-
dfx-woo-role-changer.php (modified) (16 diffs)
-
readme.txt (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
dfx-woo-role-changer/trunk/dfx-woo-role-changer.php
r3233947 r3234638 4 4 * Plugin Name: DFX Automatic Role Changer for WooCommerce 5 5 * Description: Allows the automatic assignation of roles to users on product purchases in WooCommerce 6 * Version: 2025020 36 * Version: 20250204 7 7 * Author: David Marín Carreño 8 8 * Author URI: https://davefx.com … … 24 24 * 25 25 * @package DFX-Woo-Role-Changer 26 * @version 2025020 326 * @version 20250204 27 27 * @author David Marín Carreño <davefx@davefx.com> 28 28 * @copyright Copyright (c) 2020-2025 David Marín Carreño … … 32 32 */ 33 33 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 34 const DFX_WOO_ROLE_CHANGER_VERSION = '20250204'; 34 35 if ( function_exists( 'dfx_woo_role_changer_fs' ) ) { 35 36 dfx_woo_role_changer_fs()->set_basename( false, __FILE__ ); … … 77 78 public $plugin_name = ''; 78 79 80 public const PLUGIN_DIR_PATH = __DIR__; 81 82 public $PLUGIN_DIR_URL; 83 79 84 /** 80 85 * Returns the instance. … … 97 102 */ 98 103 private function __construct() { 104 $this->PLUGIN_DIR_URL = plugin_dir_url( __FILE__ ); 99 105 // Set plugin file, removing the need to use __FILE__ 100 106 if ( strpos( __FILE__, WP_PLUGIN_DIR ) !== false ) { … … 263 269 } 264 270 } 265 woocommerce_wp_select( a rray(271 woocommerce_wp_select( apply_filters( 'dfx_wrc_role_selector_args', array( 266 272 'id' => 'dfxwcrc_role_assignment', 267 273 'value' => get_post_meta( get_the_ID(), '_dfxwcrc_role_assignment', true ) ?? 'none', … … 270 276 'description' => $description, 271 277 'options' => $options, 272 ) ); 278 'class' => 'wc-enhanced-select', 279 ), get_the_ID() ) ); 280 do_action( 'dfx_woo_role_changer_product_options' ); 273 281 echo '</div>'; 274 282 } … … 279 287 } 280 288 $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'] ) ); 282 290 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 ) ) { 284 297 update_post_meta( $id, '_dfxwcrc_role_assignment', $assignment ); 285 298 } … … 287 300 delete_post_meta( $id, '_dfxwcrc_role_assignment' ); 288 301 } 302 do_action( 'dfx_woo_role_changer_product_options_save', $id, $post ); 289 303 } 290 304 … … 292 306 $roles = []; 293 307 $order = wc_get_order( $order_id ); 294 $ product_items = $order->get_items();308 $order_product_items = $order->get_items(); 295 309 $user_id = $order->get_user_id(); 296 310 if ( !$user_id ) { … … 298 312 } 299 313 $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 ); 302 316 if ( empty( $role ) || empty( trim( $role ) ) ) { 303 317 continue; … … 310 324 } 311 325 } 326 $roles = apply_filters( 327 'dfx_woo_role_changer_roles_for_product', 328 $roles, 329 $order_product_item, 330 $order 331 ); 312 332 } 313 333 if ( count( $roles ) == 0 ) { … … 348 368 * @param WP_User $user 349 369 * @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 351 371 * @param string $note 352 372 * … … 359 379 $note = '' 360 380 ) { 381 $new_role = apply_filters( 382 'dfx_woo_role_changer_maybe_add_role_to_user', 383 $new_role, 384 $user, 385 $order 386 ); 361 387 if ( !in_array( $new_role, $user->roles ) ) { 362 388 if ( $this->get_current_mode() === 'replace_roles' ) { … … 383 409 } 384 410 } 411 do_action( 412 'dfx_woo_role_changer_role_maybe_assigned', 413 $user, 414 $new_role, 415 $order->get_id() 416 ); 385 417 } 386 418 … … 388 420 * @param WP_User $user 389 421 * @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 391 424 * 392 425 * @return void -
dfx-woo-role-changer/trunk/readme.txt
r3233947 r3234638 5 5 Requires at least: 3.1 6 6 Tested up to: 6.6 7 Stable tag: 2025020 37 Stable tag: 20250204 8 8 License: GPLv3 9 9 License URI: http://www.gnu.org/licenses/gpl-3.0.html … … 22 22 * Integrates seamlessly with WooCommerce. 23 23 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 24 26 * *Premium*: Supports role management tied to subscription products, supporting [WooCommerce Subscriptions](https://woocommerce.com/products/woocommerce-subscriptions/), 25 27 [YITH WooCommerce Subscription](https://wordpress.org/plugins/yith-woocommerce-subscription/) and 26 28 [WP Swings Subscriptions for WooCommerce](https://wordpress.org/plugins/subscriptions-for-woocommerce/). 29 30 * *Premium*: Allows defining multiple roles per product. 27 31 28 32 == Installation == … … 50 54 = Setting Up Role Assignments = 51 55 52 * Go to WooCommerce > Settings > Role Changer.56 * Edit a product from your catalog. 53 57 54 * Select a product from your catalog.58 * Choose the role (or several roles *PREMIUM*) to assign when the product is purchased. 55 59 56 * Choose the role to assign when the product is purchased.60 * *Premium*: Define a role validity period in days after the purchase. 57 61 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. 59 64 60 65 * Save changes. 61 66 62 67 == Premium Features == 68 69 = Define Role Validity Periods = 70 71 The 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. 63 72 64 73 = Manages roles following the lifecycle of subscription products = … … 95 104 * 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. 96 105 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? = 98 107 99 Yes, you can assign multiple roles. Users will gain all assigned roles upon purchase.108 In the Premium version, the validity period for a purchase is added to the previously existing one. 100 109 101 = 3. How do I upgrade to the premium version? = 110 = 3. Can I assign multiple roles to a single product? = 111 112 In 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? = 102 115 103 116 Visit 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. … … 134 147 135 148 == 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. 136 157 137 158 = 20250203 =
Note: See TracChangeset
for help on using the changeset viewer.