settings = $settings; } public function hooks() { add_filter( 'user_has_cap', array( $this, 'admin_caps' ), 10, 1 ); add_action( 'admin_init', array( __CLASS__, 'maybe_install' ) ); add_action( 'show_user_profile', array( $this, 'profile_field' ) ); add_action( 'edit_user_profile', array( $this, 'profile_field' ) ); add_action( 'personal_options_update', array( $this, 'profile_save' ) ); add_action( 'edit_user_profile_update', array( $this, 'profile_save' ) ); } public static function maybe_install() { if ( self::VERSION !== get_option( self::OPTION ) ) { self::install(); } } /** Create / refresh the role and grant the caps to WooCommerce's shop_manager (idempotent). */ public static function install() { $caps = array( 'read' => true ); $editor = get_role( 'editor' ); if ( $editor ) { $caps = array_merge( $caps, array_filter( (array) $editor->capabilities ) ); } $caps[ self::CAP_ORDERS ] = true; $caps[ self::CAP_PRODUCTS ] = true; $role = get_role( self::ROLE ); if ( ! $role ) { add_role( self::ROLE, __( 'Shop manager (AOO Shop)', 'aoo-shop' ), $caps ); } else { foreach ( $caps as $cap => $grant ) { if ( empty( $role->capabilities[ $cap ] ) ) { $role->add_cap( $cap ); } } } $woo = get_role( 'shop_manager' ); if ( $woo ) { $woo->add_cap( self::CAP_ORDERS ); $woo->add_cap( self::CAP_PRODUCTS ); } update_option( self::OPTION, self::VERSION, false ); } /** Uninstall: users of our role become Editors, then the role and every grant of the caps go away. */ public static function uninstall() { self::uninstall_role( self::ROLE, array( self::CAP_ORDERS, self::CAP_PRODUCTS ) ); // 0.7.0 before the prefix change, in case the upgrade never ran. self::uninstall_role( 'aoo_shop_manager', array( 'aoo_shop_view_orders', 'aoo_shop_view_products' ) ); } /** * Remove one role and its caps everywhere. * * @param string $role_name Role. * @param string[] $caps Capabilities granted by it. */ private static function uninstall_role( $role_name, array $caps ) { if ( get_role( $role_name ) ) { foreach ( get_users( array( 'role' => $role_name, 'fields' => 'ID' ) ) as $id ) { $user = new WP_User( (int) $id ); $user->remove_role( $role_name ); if ( empty( $user->roles ) ) { $user->add_role( get_role( 'editor' ) ? 'editor' : 'subscriber' ); } } remove_role( $role_name ); } foreach ( wp_roles()->role_objects as $role ) { foreach ( $caps as $cap ) { $role->remove_cap( $cap ); } } foreach ( $caps as $cap ) { foreach ( get_users( array( 'capability__in' => array( $cap ), 'fields' => 'ID' ) ) as $id ) { ( new WP_User( (int) $id ) )->remove_cap( $cap ); } } delete_option( self::OPTION ); } /** Orders screen capability (filter `aooshop_admin_capability` from 0.4.0/0.4.1 still overrides it). */ public static function orders_cap() { $cap = apply_filters( 'aooshop_admin_capability', self::CAP_ORDERS ); return is_string( $cap ) && '' !== $cap ? $cap : self::CAP_ORDERS; } /** Products screen capability (filter `aooshop_products_capability`). */ public static function products_cap() { $cap = apply_filters( 'aooshop_products_capability', self::CAP_PRODUCTS ); return is_string( $cap ) && '' !== $cap ? $cap : self::CAP_PRODUCTS; } /** * Administrators: products always · orders while `admins_see_orders` is on. * * @param array $allcaps Capabilities of the user being checked. */ public function admin_caps( $allcaps ) { if ( empty( $allcaps['manage_options'] ) ) { return $allcaps; } $allcaps[ self::CAP_PRODUCTS ] = true; if ( (int) $this->settings->get( 'admins_see_orders' ) ) { $allcaps[ self::CAP_ORDERS ] = true; } return $allcaps; } /** Profile checkbox — only administrators edit it, only on administrator accounts while the shared switch is off. */ public function profile_field( $user ) { if ( ! current_user_can( 'manage_options' ) || ! ( $user instanceof WP_User ) || ! user_can( $user, 'manage_options' ) ) { return; } $all = (int) $this->settings->get( 'admins_see_orders' ); $own = ! empty( $user->caps[ self::CAP_ORDERS ] ); $link = AooShop_Admin::url( AooShop_Admin::SETTINGS ) . '#aoo-access'; wp_nonce_field( 'aooshop_profile_' . $user->ID, '_aooshop_profile' ); ?>

settings->get( 'admins_see_orders' ) ) { return; // Checkbox disabled → not posted; keep whatever was set before. } $user = new WP_User( $user_id ); if ( ! $user->exists() || ! user_can( $user, 'manage_options' ) ) { return; } if ( ! empty( $_POST['aooshop_view_orders'] ) ) { $user->add_cap( self::CAP_ORDERS ); } else { $user->remove_cap( self::CAP_ORDERS ); } } }