class-base-menu.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Menu
  6. */
  7. /**
  8. * Admin menu base class.
  9. */
  10. abstract class WPSEO_Base_Menu implements WPSEO_WordPress_Integration {
  11. /**
  12. * A menu.
  13. *
  14. * @var WPSEO_Menu
  15. */
  16. protected $menu;
  17. /**
  18. * Constructs the Admin Menu.
  19. *
  20. * @param WPSEO_Menu $menu Menu to use.
  21. */
  22. public function __construct( WPSEO_Menu $menu ) {
  23. $this->menu = $menu;
  24. }
  25. /**
  26. * Returns the list of registered submenu pages.
  27. *
  28. * @return array List of registered submenu pages.
  29. */
  30. abstract public function get_submenu_pages();
  31. /**
  32. * Creates a submenu formatted array.
  33. *
  34. * @param string $page_title Page title to use.
  35. * @param string $page_slug Page slug to use.
  36. * @param callable $callback Optional. Callback which handles the page request.
  37. * @param callable[] $hook Optional. Hook to trigger when the page is registered.
  38. *
  39. * @return array Formatted submenu.
  40. */
  41. protected function get_submenu_page( $page_title, $page_slug, $callback = null, $hook = null ) {
  42. if ( $callback === null ) {
  43. $callback = $this->get_admin_page_callback();
  44. }
  45. return [
  46. $this->get_page_identifier(),
  47. '',
  48. $page_title,
  49. $this->get_manage_capability(),
  50. $page_slug,
  51. $callback,
  52. $hook,
  53. ];
  54. }
  55. /**
  56. * Registers submenu pages as menu pages.
  57. *
  58. * This method should only be used if the user does not have the required capabilities
  59. * to access the parent menu page.
  60. *
  61. * @param array $submenu_pages List of submenu pages to register.
  62. *
  63. * @return void
  64. */
  65. protected function register_menu_pages( $submenu_pages ) {
  66. if ( ! is_array( $submenu_pages ) || empty( $submenu_pages ) ) {
  67. return;
  68. }
  69. // Loop through submenu pages and add them.
  70. array_walk( $submenu_pages, [ $this, 'register_menu_page' ] );
  71. }
  72. /**
  73. * Registers submenu pages.
  74. *
  75. * @param array $submenu_pages List of submenu pages to register.
  76. *
  77. * @return void
  78. */
  79. protected function register_submenu_pages( $submenu_pages ) {
  80. if ( ! is_array( $submenu_pages ) || empty( $submenu_pages ) ) {
  81. return;
  82. }
  83. // Loop through submenu pages and add them.
  84. array_walk( $submenu_pages, [ $this, 'register_submenu_page' ] );
  85. // Set the first submenu title to the title of the first submenu page.
  86. global $submenu;
  87. if ( isset( $submenu[ $this->get_page_identifier() ] ) && $this->check_manage_capability() ) {
  88. $submenu[ $this->get_page_identifier() ][0][0] = $submenu_pages[0][2];
  89. }
  90. }
  91. /**
  92. * Registers a submenu page as a menu page.
  93. *
  94. * This method should only be used if the user does not have the required capabilities
  95. * to access the parent menu page.
  96. *
  97. * @param array $submenu_page {
  98. * Submenu page definition.
  99. *
  100. * @type string $0 Parent menu page slug.
  101. * @type string $1 Page title, currently unused.
  102. * @type string $2 Title to display in the menu.
  103. * @type string $3 Required capability to access the page.
  104. * @type string $4 Page slug.
  105. * @type callable $5 Callback to run when the page is rendered.
  106. * @type array $6 Optional. List of callbacks to run when the page is loaded.
  107. * }
  108. *
  109. * @return void
  110. */
  111. protected function register_menu_page( $submenu_page ) {
  112. // If the submenu page requires the general manage capability, it must be added as an actual submenu page.
  113. if ( $submenu_page[3] === $this->get_manage_capability() ) {
  114. return;
  115. }
  116. $page_title = 'Yoast SEO: ' . $submenu_page[2];
  117. // Register submenu page as menu page.
  118. $hook_suffix = add_menu_page(
  119. $page_title,
  120. $submenu_page[2],
  121. $submenu_page[3],
  122. $submenu_page[4],
  123. $submenu_page[5],
  124. WPSEO_Utils::get_icon_svg(),
  125. '99.31337'
  126. );
  127. // If necessary, add hooks for the submenu page.
  128. if ( isset( $submenu_page[6] ) && ( is_array( $submenu_page[6] ) ) ) {
  129. $this->add_page_hooks( $hook_suffix, $submenu_page[6] );
  130. }
  131. }
  132. /**
  133. * Registers a submenu page.
  134. *
  135. * This method will override the capability of the page to automatically use the
  136. * general manage capability. Use the `register_menu_page()` method if the submenu
  137. * page should actually use a different capability.
  138. *
  139. * @param array $submenu_page {
  140. * Submenu page definition.
  141. *
  142. * @type string $0 Parent menu page slug.
  143. * @type string $1 Page title, currently unused.
  144. * @type string $2 Title to display in the menu.
  145. * @type string $3 Required capability to access the page.
  146. * @type string $4 Page slug.
  147. * @type callable $5 Callback to run when the page is rendered.
  148. * @type array $6 Optional. List of callbacks to run when the page is loaded.
  149. * }
  150. *
  151. * @return void
  152. */
  153. protected function register_submenu_page( $submenu_page ) {
  154. $page_title = $submenu_page[2];
  155. // We cannot use $submenu_page[1] because add-ons define that, so hard-code this value.
  156. if ( $submenu_page[4] === 'wpseo_licenses' ) {
  157. $page_title = $this->get_license_page_title();
  158. }
  159. $page_title .= ' - Yoast SEO';
  160. // Force the general manage capability to be used.
  161. $submenu_page[3] = $this->get_manage_capability();
  162. // Register submenu page.
  163. $hook_suffix = add_submenu_page(
  164. $submenu_page[0],
  165. $page_title,
  166. $submenu_page[2],
  167. $submenu_page[3],
  168. $submenu_page[4],
  169. $submenu_page[5]
  170. );
  171. // If necessary, add hooks for the submenu page.
  172. if ( isset( $submenu_page[6] ) && ( is_array( $submenu_page[6] ) ) ) {
  173. $this->add_page_hooks( $hook_suffix, $submenu_page[6] );
  174. }
  175. }
  176. /**
  177. * Adds hook callbacks for a given admin page hook suffix.
  178. *
  179. * @param string $hook_suffix Admin page hook suffix, as returned by `add_menu_page()`
  180. * or `add_submenu_page()`.
  181. * @param array $callbacks Callbacks to add.
  182. *
  183. * @return void
  184. */
  185. protected function add_page_hooks( $hook_suffix, array $callbacks ) {
  186. foreach ( $callbacks as $callback ) {
  187. add_action( 'load-' . $hook_suffix, $callback );
  188. }
  189. }
  190. /**
  191. * Gets the main admin page identifier.
  192. *
  193. * @return string Admin page identifier.
  194. */
  195. protected function get_page_identifier() {
  196. return $this->menu->get_page_identifier();
  197. }
  198. /**
  199. * Checks whether the current user has capabilities to manage all options.
  200. *
  201. * @return bool True if capabilities are sufficient, false otherwise.
  202. */
  203. protected function check_manage_capability() {
  204. return WPSEO_Capability_Utils::current_user_can( $this->get_manage_capability() );
  205. }
  206. /**
  207. * Returns the capability that is required to manage all options.
  208. *
  209. * @return string Capability to check against.
  210. */
  211. abstract protected function get_manage_capability();
  212. /**
  213. * Returns the page handler callback.
  214. *
  215. * @return array Callback page handler.
  216. */
  217. protected function get_admin_page_callback() {
  218. return [ $this->menu, 'load_page' ];
  219. }
  220. /**
  221. * Returns the page title to use for the licenses page.
  222. *
  223. * @return string The title for the license page.
  224. */
  225. protected function get_license_page_title() {
  226. static $title = null;
  227. if ( $title === null ) {
  228. $title = __( 'Premium', 'wordpress-seo' );
  229. }
  230. return $title;
  231. }
  232. }