policy = $policy; } /** * Hooks into WordPress. * * @return void */ public function register() { add_filter( 'map_meta_cap', array( $this, 'map' ), 10, 4 ); } /** * The primitive capability that lets a user manage the plugin. * * Defaults to manage_options, which a Multisite Super Admin always has. * * @return string */ public static function manage_primitive() { /** * Filters the capability required to manage TurtleReport. * * Only administrators should hold it: managers can read every site's * report and add Google credentials. * * @param string $capability Default 'manage_options'. */ return (string) apply_filters( 'turtlereport_manage_capability', 'manage_options' ); } /** * Resolves the plugin's meta capabilities. * * @param string[] $caps Primitive capabilities WordPress will check. * @param string $cap Capability being checked. * @param int $user_id User ID. * @param array $args Extra arguments, e.g. the site ID. * @return string[] */ public function map( $caps, $cap, $user_id, $args ) { switch ( $cap ) { case self::MANAGE: return array( self::manage_primitive() ); case self::VIEW_REPORTS: return $this->policy->has_any_access( (int) $user_id ) ? array( 'read' ) : array( 'do_not_allow' ); case self::VIEW_SITE: $site_id = isset( $args[0] ) ? (string) $args[0] : ''; return '' !== $site_id && $this->policy->can_view( (int) $user_id, $site_id ) ? array( 'read' ) : array( 'do_not_allow' ); } return $caps; } }