plugins - Adding an item to an anonymous array inside a filter?

I'm trying to add a user_role inside the ATUM stock management plugin. Currently it's only visible to admins,

I'm trying to add a user_role inside the ATUM stock management plugin. Currently it's only visible to admins, while I also want to add shop_managers. But I just can't succeed in adding it successfully -except if I add it directly in the plugin.

This is where the filter is placed inside the ATUM plugin.

namespace Atum\Components;
class AtumCapabilities {
   private function __construct(){
      $admin_roles = (array) apply_filters( 'atum/capabilities/admin_roles', [ get_role( 'administrator' ) ] );  
  
      foreach ( $admin_roles as $admin_role ) {

            if ( $admin_role instanceof \WP_Role ) {
                foreach ( $this->capabilities as $cap ) {
                    $admin_role->add_cap( $cap );
                }
            }

        }
   }
}

And this is the code I have been using. I also tried variations of this, changing the priority or including this in the "init" hook.

add_filter('atum/capabilities/admin_roles', function($roles) {
  $roles[] = get_role('shop_manager');
  return $roles;
});

Does anyone know how I can add get_role('shop_manager') to the array inside the atum/capabilities/admin_roles filter?

Thanks

I'm trying to add a user_role inside the ATUM stock management plugin. Currently it's only visible to admins, while I also want to add shop_managers. But I just can't succeed in adding it successfully -except if I add it directly in the plugin.

This is where the filter is placed inside the ATUM plugin.

namespace Atum\Components;
class AtumCapabilities {
   private function __construct(){
      $admin_roles = (array) apply_filters( 'atum/capabilities/admin_roles', [ get_role( 'administrator' ) ] );  
  
      foreach ( $admin_roles as $admin_role ) {

            if ( $admin_role instanceof \WP_Role ) {
                foreach ( $this->capabilities as $cap ) {
                    $admin_role->add_cap( $cap );
                }
            }

        }
   }
}

And this is the code I have been using. I also tried variations of this, changing the priority or including this in the "init" hook.

add_filter('atum/capabilities/admin_roles', function($roles) {
  $roles[] = get_role('shop_manager');
  return $roles;
});

Does anyone know how I can add get_role('shop_manager') to the array inside the atum/capabilities/admin_roles filter?

Thanks

Share Improve this question asked Jul 7, 2020 at 15:08 photogenicphotogenic 211 silver badge7 bronze badges 4
  • Your code looks fine to me. The issue is likely that the original code is only run during plugin activation, because add_cap() should only be run once. Perhaps try deactivating and reactivating ATUM while your filter code is active. – Jacob Peattie Commented Jul 7, 2020 at 15:13
  • @JacobPeattie Just tried it. Unfortunately nothing. When I added the role shop_manager inside the plugin it immediately changes the capabilities, thus is the filter not only run during plugin activation -my guess. – photogenic Commented Jul 7, 2020 at 15:37
  • You may need to contact the plugin author for assistance, as it's not apparent what the issue is from the information included in the question. My only other thought is that your filter is running too late. Where have you added your code? – Jacob Peattie Commented Jul 7, 2020 at 15:43
  • @JacobPeattie I already wrote the author and they tried to push another plugin on me link. This is what they wrote: "...please make sure you are adding it after ATUM registers its ‘atum/capabilities/admin_roles’ filter or won’t work. So, if this is the problem, you can try to delay it through any other WP hook." – photogenic Commented Jul 7, 2020 at 15:53
Add a comment  | 

1 Answer 1

Reset to default 0

I added the capabilities now through this code. I'm not happy with my solution and it would be great if someone could find a proper way to add to the filter. I would be honestly interested to know how to make it properly work.

add_action( 'admin_init', 'jp_add_atum_caps_to_shop_manager');
function jp_add_atum_caps_to_shop_manager() {
    if(!user_can( 217 , 'atum_read_inbound_stock')) {
        $capabilities = array(

                // Purchase price caps.
                'edit_purchase_price',
                'view_purchase_price',

                // Inbound Stock caps.
                'read_inbound_stock',

                // Out Stock Threshold caps.
                'edit_out_stock_threshold',

                // Settings caps.
                'manage_settings',
                'edit_visual_settings',

                // ATUM menus caps.
                'view_admin_menu',
                'view_admin_bar_menu',

                // Other caps.
                'export_data',
                'view_statistics',
            ); 

        // Add the ATUM prefix to all the capabilities.
        $capabilities = preg_filter( '/^/', 'atum_' , $capabilities );

        $admin_role = get_role( 'shop_manager' );

        foreach ( $capabilities as $cap ) {
            $admin_role->add_cap( $cap );
        }  
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742279698a4414257.html

相关推荐

  • plugins - Adding an item to an anonymous array inside a filter?

    I'm trying to add a user_role inside the ATUM stock management plugin. Currently it's only visible to admins,

    21小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信