Plugin Directory

Changeset 3476671


Ignore:
Timestamp:
03/06/2026 07:55:37 PM (3 weeks ago)
Author:
srdjan121
Message:

Added the '/bccentralconnector/eligible-roles' edpoint to update eligible user roles.

Location:
businesscentralconnector/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • businesscentralconnector/trunk/businesscentralconnector.php

    r3476596 r3476671  
    44 * Plugin URI: https://help.synfynal.com/articles/index.html
    55 * Description: Extends WooCommerce API for Business Central integration via Synfynal Connector. Requires Business Central/WooCommerce Connector from AppSource.
    6  * Version: 1.1.5
     6 * Version: 1.1.6
    77 * Author: Synfynal
    88 * Author URI: https://www.synfynal.com/
     
    119119// Option name ELEX uses to store its eligible users list.
    120120define('BCCENTRALCONNECTOR_ELEX_OPTION', 'eh_pricing_discount_product_on_users');
     121define('BCCENTRALCONNECTOR_ELEX_ROLE_OPTION', 'eh_pricing_discount_product_price_user_role');
    121122
    122123/**
     
    227228add_action('rest_api_init', 'bccentralconnector_register_user_roles_endpoint');
    228229add_action('rest_api_init', 'bccentralconnector_register_ping_endpoint');
     230add_action('rest_api_init', 'bccentralconnector_register_eligible_roles_endpoint');
    229231
    230232function bccentralconnector_register_user_roles_endpoint() {
     
    326328/**
    327329 * =====================================================================
     330 * Custom REST endpoint: POST /wp-json/wc/v3/bccentralconnector/eligible-roles
     331 *
     332 * Adds the supplied role slugs to the ELEX eligible roles list without
     333 * removing any roles that are already stored. Unknown slugs (not
     334 * registered in this WordPress installation) are silently discarded.
     335 * Resending an already-stored slug has no effect (deduplication).
     336 *
     337 * Request body (JSON):
     338 *   { "roles": ["Company_D", "Company_E"] }
     339 *
     340 * Response:
     341 *   { "eligible_roles": ["Company_A", "Company_B", "Company_D", "Company_E"] }
     342 * =====================================================================
     343 */
     344function bccentralconnector_register_eligible_roles_endpoint() {
     345    register_rest_route(
     346        'wc/v3',
     347        '/bccentralconnector/eligible-roles',
     348        array(
     349            'methods'             => WP_REST_Server::CREATABLE,
     350            'callback'            => 'bccentralconnector_set_eligible_roles',
     351            'permission_callback' => 'bccentralconnector_user_roles_permission_check',
     352            'args'                => array(
     353                'roles' => array(
     354                    'required'          => true,
     355                    'type'              => 'array',
     356                    'items'             => array('type' => 'string'),
     357                    'sanitize_callback' => function($value) {
     358                        return array_map('sanitize_text_field', (array) $value);
     359                    },
     360                    'description'       => __('Array of role slugs to add to the ELEX discount-eligible list.', 'businesscentralconnector'),
     361                ),
     362            ),
     363        )
     364    );
     365}
     366
     367/**
     368 * Callback for POST /wp-json/wc/v3/bccentralconnector/eligible-roles.
     369 *
     370 * Validates each incoming slug against the registered WordPress roles,
     371 * then merges them into the existing `eh_pricing_discount_product_price_user_role`
     372 * option — existing roles are never removed.
     373 *
     374 * @param WP_REST_Request $request
     375 * @return WP_REST_Response
     376 */
     377function bccentralconnector_set_eligible_roles(WP_REST_Request $request) {
     378    $allowed_roles = array_keys(wp_roles()->roles);
     379    $requested     = (array) $request->get_param('roles');
     380
     381    // Keep only slugs that actually exist in this WordPress installation.
     382    $valid = array_filter($requested, function($slug) use ($allowed_roles) {
     383        return in_array($slug, $allowed_roles, true);
     384    });
     385
     386    // Merge with the existing list — do not remove roles that are already stored.
     387    $existing = get_option(BCCENTRALCONNECTOR_ELEX_ROLE_OPTION, array());
     388    if (!is_array($existing)) {
     389        $existing = array();
     390    }
     391    $merged = array_values(array_unique(array_merge($existing, $valid)));
     392
     393    update_option(BCCENTRALCONNECTOR_ELEX_ROLE_OPTION, $merged);
     394
     395    return rest_ensure_response(array(
     396        'eligible_roles' => $merged,
     397    ));
     398}
     399
     400/**
     401 * =====================================================================
    328402 * Add a "WooCommerce Connector" download link in the Plugins list
    329403 * =====================================================================
  • businesscentralconnector/trunk/readme.txt

    r3476596 r3476671  
    55Requires PHP: 7.0
    66Tested up to: 6.8
    7 Stable tag: 1.1.5
     7Stable tag: 1.1.6
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    5252
    5353== Changelog ==
     54= 1.1.6 =
     55* Added the '/bccentralconnector/eligible-roles' edpoint to update eligible user roles.
     56
    5457= 1.1.5 =
    5558* Added the '/bccentralconnector/user-roles' endpoint to return existing user roles from the webstore.
     
    7174
    7275== Upgrade Notice ==
     76= 1.1.6 =
     77This release introduces the ability to update eligible user roles for discounts via the API.
     78
    7379= 1.1.5 =
    7480This release introduces the ability to retrieve existing user roles from the webstore via the API.
Note: See TracChangeset for help on using the changeset viewer.