PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.21
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.21
1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 All 166 releases
← All changes | includes/Activator.php +48 -17 1.10.16 → 1.10.21 View file →
@@ -38,8 +38,11 @@
38 38 */
39 39 public function __construct() {
40 40 register_activation_hook( PLUGIN_FILE, array( $this, 'activate' ) );
41 41 add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
42 + // The staff-account rule (#1918, #2104) must hold even when WooCommerce is
43 + // inactive and Init never starts; it depends on nothing from WooCommerce.
44 + add_filter( 'map_meta_cap', array( Services\Permission_Rules::class, 'map_user_meta_caps' ), 10, 4 );
42 45 add_action( 'plugins_loaded', array( $this, 'init' ) );
43 46 }
44 47
45 48 /**
@@ -98,11 +101,28 @@
98 101 /**
99 102 * Fired when the plugin is activated.
100 103 *
101 104 * @param bool $install_sync_schema Whether to install the sync schema.
105 + * @param bool $full_role_sync Whether to repair all default role capabilities.
102 106 */
103 - public function single_activate( bool $install_sync_schema = true ): void {
104 - $role_capabilities = self::role_capability_definition();
107 + public function single_activate( bool $install_sync_schema = true, bool $full_role_sync = true ): void {
108 + $role_capabilities = self::role_capability_definition();
109 + $capability_names = $role_capabilities;
110 + $capability_names['cashier'] = array_merge( array( 'access_woocommerce_pos' ), array_keys( $role_capabilities['cashier'] ) );
111 + $synced = get_option( 'woocommerce_pos_role_caps_synced', false );
112 + if ( ! $full_role_sync && false === $synced && get_option( 'woocommerce_pos_role_caps_fingerprint' ) === $this->role_caps_fingerprint() ) {
113 + $synced = $capability_names;
114 + }
115 + // An upgrade grants only capabilities new to the definition since the
116 + // last sync, so a capability the merchant removed on the Access screen
117 + // stays removed. Explicit activation still repairs every default.
118 + $granted = $capability_names;
119 + if ( ! $full_role_sync && \is_array( $synced ) ) {
120 + foreach ( $granted as $slug => $capabilities ) {
121 + $already = isset( $synced[ $slug ] ) && \is_array( $synced[ $slug ] ) ? $synced[ $slug ] : array();
122 + $granted[ $slug ] = array_values( array_diff( $capabilities, $already ) );
123 + }
124 + }
105 125
106 126 // Reseed the default template terms on the next request: (re)activation
107 127 // is the repair a merchant reaches for after deleting a term by hand.
108 128 // This also runs once per upgrade (version_check re-activates to sync
@@ -115,15 +135,15 @@
115 135 self::autoload_request_latches();
116 136 Admin\Permalink::ensure_default();
117 137
118 138 // create POS specific roles.
119 - $this->create_pos_roles();
139 + $this->create_pos_roles( $granted['cashier'] );
120 140
121 141 // add pos capabilities to non POS roles.
122 142 $this->add_pos_capability(
123 143 array(
124 - 'administrator' => $role_capabilities['administrator'],
125 - 'shop_manager' => $role_capabilities['shop_manager'],
144 + 'administrator' => $granted['administrator'],
145 + 'shop_manager' => $granted['shop_manager'],
126 146 )
127 147 );
128 148
129 149 $stored_roles = get_option( wp_roles()->role_key, array() );
@@ -128,13 +148,14 @@
128 148
129 149 $stored_roles = get_option( wp_roles()->role_key, array() );
130 150 $roles_are_persisted = is_array( $stored_roles );
131 151 if ( $roles_are_persisted ) {
132 - foreach ( $role_capabilities as $slug => $capabilities ) {
133 - $required_capabilities = 'cashier' === $slug
134 - ? array_merge( array( 'access_woocommerce_pos' ), array_keys( $capabilities ) )
135 - : $capabilities;
136 - foreach ( $required_capabilities as $capability ) {
152 + foreach ( $granted as $slug => $capabilities ) {
153 + if ( ! isset( $stored_roles[ $slug ] ) ) {
154 + $roles_are_persisted = false;
155 + break;
156 + }
157 + foreach ( $capabilities as $capability ) {
137 158 if ( empty( $stored_roles[ $slug ]['capabilities'][ $capability ] ) ) {
138 159 $roles_are_persisted = false;
139 160 break 2;
140 161 }
@@ -143,8 +164,11 @@
143 164 }
144 165
145 166 $obsolete_customer_create_cap = isset( $role_capabilities['cashier']['create_customers'] ) ? 'promote_users' : 'create_customers';
146 167 if ( $roles_are_persisted && empty( $stored_roles['cashier']['capabilities'][ $obsolete_customer_create_cap ] ) ) {
168 + // Snapshot first: a fingerprint that advanced past a failed snapshot
169 + // write would never retry it.
170 + update_option( 'woocommerce_pos_role_caps_synced', $capability_names, true );
147 171 update_option( 'woocommerce_pos_role_caps_fingerprint', $this->role_caps_fingerprint(), true );
148 172 }
149 173
150 174 // Flag the consent pop-up for the next admin page load. Done here
@@ -312,9 +336,10 @@
312 336 $plugin_needs_upgrade = version_compare( $old, VERSION, '<' );
313 337 $sync_needs_upgrade = Sync_Api::SCHEMA_VERSION !== get_option( Sync_Api::SCHEMA_OPTION, null );
314 338
315 339 $role_caps_fingerprint = $this->role_caps_fingerprint();
316 - $role_caps_need_sync = get_option( 'woocommerce_pos_role_caps_fingerprint' ) !== $role_caps_fingerprint;
340 + $role_caps_need_sync = get_option( 'woocommerce_pos_role_caps_fingerprint' ) !== $role_caps_fingerprint
341 + || false === get_option( 'woocommerce_pos_role_caps_synced' );
317 342 if ( ! $plugin_needs_upgrade && ! $sync_needs_upgrade && ! $role_caps_need_sync ) {
318 343 return;
319 344 }
320 345
@@ -326,9 +351,10 @@
326 351 $locked_plugin_needs_upgrade = version_compare( $locked_old, VERSION, '<' );
327 352 $locked_sync_needs_upgrade = Sync_Api::SCHEMA_VERSION !== get_option( Sync_Api::SCHEMA_OPTION, null );
328 353
329 354 $locked_role_caps_fingerprint = $this->role_caps_fingerprint();
330 - $locked_role_caps_need_sync = get_option( 'woocommerce_pos_role_caps_fingerprint' ) !== $locked_role_caps_fingerprint;
355 + $locked_role_caps_need_sync = get_option( 'woocommerce_pos_role_caps_fingerprint' ) !== $locked_role_caps_fingerprint
356 + || false === get_option( 'woocommerce_pos_role_caps_synced' );
331 357 if ( ! $locked_plugin_needs_upgrade && ! $locked_sync_needs_upgrade && ! $locked_role_caps_need_sync ) {
332 358 $this->release_db_upgrade_lock();
333 359 return;
334 360 }
@@ -346,9 +372,9 @@
346 372 // requires translations to be loaded (WordPress 6.7+).
347 373 add_action(
348 374 'init',
349 375 function () {
350 - $this->single_activate( false );
376 + $this->single_activate( false, false );
351 377 }
352 378 );
353 379 }
354 380
@@ -513,10 +539,13 @@
513 539 }
514 540
515 541 /**
516 542 * Add POS specific roles.
543 + *
544 + * @param string[]|null $capabilities Capability names to sync onto an existing role, or null for
545 + * every default. A missing role is always created with the full set.
517 546 */
518 - private function create_pos_roles(): void {
547 + private function create_pos_roles( ?array $capabilities = null ): void {
519 548 $role_capabilities = self::role_capability_definition();
520 549 $cashier_capabilities = $role_capabilities['cashier'];
521 550
522 551 add_role(
@@ -522,9 +551,11 @@
522 551 add_role(
523 552 'cashier',
524 553 /* translators: Plugin activation notice label. */
525 554 __( 'Cashier', 'woocommerce-pos' ),
526 - $cashier_capabilities
555 + // A missing role is created whole, access gate included, whatever
556 + // subset an incremental upgrade asked to sync.
557 + array_merge( array( 'access_woocommerce_pos' => true ), $cashier_capabilities )
527 558 );
528 559
529 560 $obsolete_customer_create_cap = isset( $cashier_capabilities['create_customers'] ) ? 'promote_users' : 'create_customers';
530 561 $cashier = get_role( 'cashier' );
@@ -531,14 +562,14 @@
531 562 if ( $cashier ) {
532 563 $cashier->remove_cap( $obsolete_customer_create_cap );
533 564 }
534 565
535 - // Sync all capabilities to the existing role. add_role() is a no-op when
566 + // Sync the requested capabilities to the role. add_role() is a no-op when
536 567 // the role already exists, so capabilities added in newer versions would
537 568 // never reach existing installs without this.
538 569 $this->add_pos_capability(
539 570 array(
540 - 'cashier' => array_merge(
571 + 'cashier' => $capabilities ?? array_merge(
541 572 array( 'access_woocommerce_pos' ),
542 573 array_keys( $cashier_capabilities )
543 574 ),
544 575 )