class-capability-manager-integration.php 3.1 KB

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