Menu.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * WordPress Menu Page Base Class
  4. */
  5. abstract class NF_Abstracts_Menu
  6. {
  7. /**
  8. * (required) The text to be displayed in the title tags of the page when the menu is selected
  9. *
  10. * @var string
  11. */
  12. public $page_title = '';
  13. /**
  14. * (required) The on-screen name text for the menu
  15. *
  16. * @var string
  17. */
  18. public $menu_title = '';
  19. /**
  20. * (required) The capability required for this menu to be displayed to the user.
  21. *
  22. * @var string
  23. */
  24. public $capability = 'manage_options';
  25. /**
  26. * (required) The slug name to refer to this menu by (should be unique for this menu).
  27. *
  28. * @var string
  29. */
  30. public $menu_slug = '';
  31. /**
  32. * (optional) The function that displays the page content for the menu page.
  33. *
  34. * @var string
  35. */
  36. public $function = 'display';
  37. /**
  38. * (optional) The icon for this menu.
  39. *
  40. * @var string
  41. */
  42. public $icon_url = '';
  43. /**
  44. * (optional) The position in the menu order this menu should appear.
  45. *
  46. * @var string
  47. */
  48. public $position = NULL;
  49. /**
  50. * Constructor
  51. *
  52. * Translate text and add the 'admin_menu' action.
  53. */
  54. public function __construct()
  55. {
  56. add_action( 'admin_menu', array( $this, 'register' ) );
  57. }
  58. /**
  59. * Register the menu page.
  60. */
  61. public function register()
  62. {
  63. add_menu_page(
  64. $this->get_page_title(),
  65. $this->get_menu_title(),
  66. apply_filters( 'ninja_forms_menu_' . $this->get_menu_slug() . '_capability', $this->get_capability() ),
  67. $this->menu_slug,
  68. array( $this, $this->function ),
  69. $this->icon_url,
  70. $this->position
  71. );
  72. }
  73. public function get_page_title()
  74. {
  75. return $this->page_title;
  76. }
  77. public function get_menu_title()
  78. {
  79. return ( $this->menu_title ) ? $this->menu_title : $this->get_page_title();
  80. }
  81. public function get_menu_slug()
  82. {
  83. return ( $this->menu_slug ) ? $this->menu_slug : 'nf-' . strtolower( preg_replace( '/[^A-Za-z0-9-]+/', '-', $this->get_menu_title() ) );
  84. }
  85. public function get_capability()
  86. {
  87. return $this->capability;
  88. }
  89. /**
  90. * Display the menu page.
  91. */
  92. public abstract function display();
  93. }