class-base-menu.php 6.9 KB

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