wordpress-seo
/
src
/
dashboard
/
user-interface
/
configuration
/
site-kit-capabilities-integration.php
site-kit-capabilities-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at src/dashboard/user-interface/configuration/site-kit-capabilities-integration.php
| 1 | <?php |
| 2 | |
| 3 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 | namespace Yoast\WP\SEO\Dashboard\User_Interface\Configuration; |
| 5 | |
| 6 | use Google\Site_Kit\Core\Permissions\Permissions; |
| 7 | use Yoast\WP\SEO\Conditionals\Third_Party\Site_Kit_Conditional; |
| 8 | use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 9 | |
| 10 | /** |
| 11 | * Enables the needed Site Kit capabilities for the SEO manager role. |
| 12 | */ |
| 13 | class Site_Kit_Capabilities_Integration implements Integration_Interface { |
| 14 | |
| 15 | /** |
| 16 | * Registers needed filters. |
| 17 | * |
| 18 | * @return void |
| 19 | */ |
| 20 | public function register_hooks() { |
| 21 | \add_filter( 'user_has_cap', [ $this, 'enable_site_kit_capabilities' ], 10, 2 ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * The needed conditionals. |
| 26 | * |
| 27 | * @return array<string> |
| 28 | */ |
| 29 | public static function get_conditionals() { |
| 30 | // This cannot have the Admin Conditional since it also needs to run in Rest requests. |
| 31 | return [ Site_Kit_Conditional::class ]; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Checks if the Site Kit capabilities need to be enabled for a manager. |
| 36 | * |
| 37 | * @param array<string> $all_caps All the current capabilities of the current user. |
| 38 | * @param array<string> $cap_to_check The capability to check against. |
| 39 | * |
| 40 | * @return array<string> |
| 41 | */ |
| 42 | public function enable_site_kit_capabilities( $all_caps, $cap_to_check ) { |
| 43 | if ( ! isset( $cap_to_check[0] ) || ! \class_exists( Permissions::class ) ) { |
| 44 | return $all_caps; |
| 45 | } |
| 46 | $user = \wp_get_current_user(); |
| 47 | $caps_to_check = [ |
| 48 | Permissions::SETUP, |
| 49 | Permissions::VIEW_DASHBOARD, |
| 50 | ]; |
| 51 | if ( \in_array( $cap_to_check[0], $caps_to_check, true ) && \in_array( 'wpseo_manager', $user->roles, true ) ) { |
| 52 | $all_caps[ $cap_to_check[0] ] = true; |
| 53 | } |
| 54 | |
| 55 | return $all_caps; |
| 56 | } |
| 57 | } |
| 58 |