ties as $capability ) { if ( $role->has_cap( $capability ) ) { $role->remove_cap( $capability ); } } $role->remove_cap( 'aioseo_manage_seo' ); } } /** * Checks if the current user has the capability. * * @since 4.0.0 * * @param string|array $capability The capability to check against. * @param string|null $checkRole A role to check against. * @return bool Whether or not the user has this capability. */ public function hasCapability( $capability, $checkRole = null ) { if ( $this->isAdmin( $checkRole ) ) { return true; } $canPublishOrEdit = $this->can( 'publish_posts', $checkRole ) || $this->can( 'edit_posts', $checkRole ); if ( ! $canPublishOrEdit ) { return false; } if ( is_array( $capability ) ) { foreach ( $capability as $cap ) { if ( false !== strpos( $cap, 'aioseo_page_' ) ) { return true; } } return false; } return false !== strpos( $capability, 'aioseo_page_' ); } /** * Gets all the capabilities for the current user. * * @since 4.0.0 * * @param string|null $role A role to check against. * @return array An array of capabilities. */ public function getAllCapabilities( $role = null ) { $capabilities = []; foreach ( $this->getCapabilityList() as $capability ) { $capabilities[ $capability ] = $this->hasCapability( $capability, $role ); } $capabilities['aioseo_admin'] = $this->isAdmin( $role ); $capabilities['aioseo_manage_seo'] = $this->isAdmin( $role ); $capabilities['aioseo_about_us_page'] = $this->canManage( $role ); return $capabilities; } /** * Returns the capability list. * * @return 4.1.3 * * @return array An array of capabilities. */ public function getCapabilityList() { return $this->capabilities; } /** * If the current user is an admin, or superadmin, they have access to all caps regardless. * * @since 4.0.0 * * @param string|null $role The role to check admin privileges if we have one. * @return bool Whether not the user/role is an admin. */ public function isAdmin( $role = null ) { if ( $role ) { if ( ( is_multisite() && 'superadmin' === $role ) || 'administrator' === $role ) { return true; } return false; } if ( ! function_exists( 'wp_get_current_user' ) ) { return false; } if ( ( is_multisite() && current_user_can( 'superadmin' ) ) || current_user_can( 'administrator' ) ) { return true; } return false; } /** * Check if the passed in role can publish posts. * * @since 4.0.9 * * @param string $capability The capability to check against. * @param string $role The role to check. * @return boolean True if the role can publish. */ protected function can( $capability, $role ) { if ( empty( $role ) ) { return current_user_can( $capability ); } $wpRoles = wp_roles(); $allRoles = $wpRoles->roles; foreach ( $allRoles as $key => $wpRole ) { if ( $key === $role ) { $r = get_role( $key ); if ( $r->has_cap( $capability ) ) { return true; } } } return false; } /** * Checks if the current user can manage AIOSEO. * * @since 4.0.0 * * @param string|null $checkRole A role to check against. * @return bool Whether or not the user can manage AIOSEO. */ public function canManage( $checkRole = null ) { return $this->isAdmin( $checkRole ); } /** * Gets all options that the user does not have access to manage. * * @since 4.1.3 * * @return array An array with the option names. */ public function getNotAllowedOptions() { return []; } /** * Gets all page fields that the user does not have access to manage. * * @since 4.1.3 * * @return array An array with the field names. */ public function getNotAllowedPageFields() { return []; } /** * Returns Roles. * * @since 4.0.17 * * @return array An array of role names. */ public function getRoles() { return $this->roles; } }