| 1 |
<?php |
| 2 |
/** |
| 3 |
* Client Service Provider Class |
| 4 |
* |
| 5 |
* @package Easy_Invoice |
| 6 |
* @subpackage Providers |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Providers; |
| 10 |
|
| 11 |
use EasyInvoice\Interfaces\ClientRepositoryInterface; |
| 12 |
use EasyInvoice\Repositories\ClientRepository; |
| 13 |
|
| 14 |
/** |
| 15 |
* ClientServiceProvider Class |
| 16 |
* |
| 17 |
* Handles registration of client-related services. |
| 18 |
*/ |
| 19 |
class ClientServiceProvider { |
| 20 |
/** |
| 21 |
* The repository instance |
| 22 |
* |
| 23 |
* @var ClientRepositoryInterface |
| 24 |
*/ |
| 25 |
protected static $repository = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Register client services |
| 29 |
*/ |
| 30 |
public function register() { |
| 31 |
// Register the `customer` role. |
| 32 |
// |
| 33 |
// This provider is constructed from easy_invoice_init(), which |
| 34 |
// itself runs on init:10 — so hooking `after_setup_theme` (the |
| 35 |
// original code) or `init` at priority 1 (the first fix) both |
| 36 |
// registered a callback for a point WordPress had already passed, |
| 37 |
// and the role was never created. On sites without WooCommerce |
| 38 |
// (which also registers `customer`) the role stayed unregistered: |
| 39 |
// WP_User::roles filtered it out as unknown, the Clients page fell |
| 40 |
// back to a "Client" label, and Pro's Client Portal — which |
| 41 |
// recognises a client by `in_array('customer', $user->roles)` — |
| 42 |
// could not see the client at all. Register it now when `init` |
| 43 |
// is already in progress, otherwise on init:1 as before. |
| 44 |
if (did_action('init')) { |
| 45 |
$this->registerClientRole(); |
| 46 |
} else { |
| 47 |
add_action('init', [$this, 'registerClientRole'], 1); |
| 48 |
} |
| 49 |
|
| 50 |
// Register client repository |
| 51 |
$this->registerClientRepository(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Register client role if it doesn't exist. |
| 56 |
* |
| 57 |
* Idempotent — short-circuits when WooCommerce (or anything else) |
| 58 |
* has already registered the role. Two implications worth knowing: |
| 59 |
* |
| 60 |
* 1. We register WITHOUT the `read` capability. On a site that |
| 61 |
* never had WooCommerce, existing 'customer'-role users had no |
| 62 |
* effective `read` cap because the role wasn't registered at |
| 63 |
* all (and `WP_User::has_cap` couldn't pull `read` from a role |
| 64 |
* that didn't exist). Registering with `['read' => true]` here |
| 65 |
* would silently UPGRADE those users — granting wp-admin access |
| 66 |
* they never had. We pass `[]` so the registration only fixes |
| 67 |
* the display label, not the cap surface. |
| 68 |
* |
| 69 |
* 2. On a site WITH WooCommerce, WC has already registered |
| 70 |
* 'customer' with its full cap set (including `read`). Our |
| 71 |
* add_role() is a no-op there — get_role() returns truthy and |
| 72 |
* we bail. WC customers retain WC's caps unchanged. |
| 73 |
*/ |
| 74 |
public function registerClientRole() { |
| 75 |
if (get_role('customer')) { |
| 76 |
return; |
| 77 |
} |
| 78 |
add_role( |
| 79 |
'customer', |
| 80 |
__('Customer', 'easy-invoice'), |
| 81 |
[] |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Register client repository |
| 87 |
*/ |
| 88 |
protected function registerClientRepository() { |
| 89 |
// Create and store the repository instance |
| 90 |
self::$repository = new ClientRepository(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the client repository |
| 95 |
* |
| 96 |
* @return ClientRepositoryInterface The client repository |
| 97 |
*/ |
| 98 |
public static function getClientRepository() { |
| 99 |
// Create the repository if it doesn't exist |
| 100 |
if (self::$repository === null) { |
| 101 |
self::$repository = new ClientRepository(); |
| 102 |
} |
| 103 |
|
| 104 |
return self::$repository; |
| 105 |
} |
| 106 |
} |