class-capability-manager-integration.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Capabilities
  6. */
  7. /**
  8. * Integrates Yoast SEO capabilities with third party role manager plugins.
  9. *
  10. * Integrates with: Members
  11. * Integrates with: User Role Editor
  12. */
  13. class WPSEO_Capability_Manager_Integration implements WPSEO_WordPress_Integration {
  14. /**
  15. * Capability manager to use.
  16. *
  17. * @var WPSEO_Capability_Manager
  18. */
  19. public $manager;
  20. /**
  21. * WPSEO_Capability_Manager_Integration constructor.
  22. *
  23. * @param WPSEO_Capability_Manager $manager The capability manager to use.
  24. */
  25. public function __construct( WPSEO_Capability_Manager $manager ) {
  26. $this->manager = $manager;
  27. }
  28. /**
  29. * Registers the hooks.
  30. *
  31. * @return void
  32. */
  33. public function register_hooks() {
  34. add_filter( 'members_get_capabilities', [ $this, 'get_capabilities' ] );
  35. add_action( 'members_register_cap_groups', [ $this, 'action_members_register_cap_group' ] );
  36. add_filter( 'ure_capabilities_groups_tree', [ $this, 'filter_ure_capabilities_groups_tree' ] );
  37. add_filter( 'ure_custom_capability_groups', [ $this, 'filter_ure_custom_capability_groups' ], 10, 2 );
  38. }
  39. /**
  40. * Get the Yoast SEO capabilities.
  41. * Optionally append them to an existing array.
  42. *
  43. * @param array $caps Optional existing capability list.
  44. * @return array
  45. */
  46. public function get_capabilities( array $caps = [] ) {
  47. if ( ! did_action( 'wpseo_register_capabilities' ) ) {
  48. do_action( 'wpseo_register_capabilities' );
  49. }
  50. return array_merge( $caps, $this->manager->get_capabilities() );
  51. }
  52. /**
  53. * Add capabilities to its own group in the Members plugin.
  54. *
  55. * @see members_register_cap_group()
  56. */
  57. public function action_members_register_cap_group() {
  58. if ( ! function_exists( 'members_register_cap_group' ) ) {
  59. return;
  60. }
  61. // Register the yoast group.
  62. $args = [
  63. 'label' => esc_html__( 'Yoast SEO', 'wordpress-seo' ),
  64. 'caps' => $this->get_capabilities(),
  65. 'icon' => 'dashicons-admin-plugins',
  66. 'diff_added' => true,
  67. ];
  68. members_register_cap_group( 'wordpress-seo', $args );
  69. }
  70. /**
  71. * Adds Yoast SEO capability group in the User Role Editor plugin.
  72. *
  73. * @see URE_Capabilities_Groups_Manager::get_groups_tree()
  74. *
  75. * @param array $groups Current groups.
  76. *
  77. * @return array Filtered list of capabilty groups.
  78. */
  79. public function filter_ure_capabilities_groups_tree( $groups = [] ) {
  80. $groups = (array) $groups;
  81. $groups['wordpress-seo'] = [
  82. 'caption' => 'Yoast SEO',
  83. 'parent' => 'custom',
  84. 'level' => 3,
  85. ];
  86. return $groups;
  87. }
  88. /**
  89. * Adds capabilities to the Yoast SEO group in the User Role Editor plugin.
  90. *
  91. * @see URE_Capabilities_Groups_Manager::get_cap_groups()
  92. *
  93. * @param array $groups Current capability groups.
  94. * @param string $cap_id Capability identifier.
  95. *
  96. * @return array List of filtered groups.
  97. */
  98. public function filter_ure_custom_capability_groups( $groups = [], $cap_id = '' ) {
  99. if ( in_array( $cap_id, $this->get_capabilities(), true ) ) {
  100. $groups = (array) $groups;
  101. $groups[] = 'wordpress-seo';
  102. }
  103. return $groups;
  104. }
  105. }