PageRenderTime 21ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/php/context/class-fieldmanager-context-submenu.php

https://github.com/Automattic/wordpress-fieldmanager
PHP | 131 lines | 59 code | 14 blank | 58 comment | 7 complexity | 944089676a6a9fa832c5dd2e6ad1f368 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * @package Fieldmanager_Context
  4. */
  5. /**
  6. * Use fieldmanager to create meta boxes on
  7. * @package Fieldmanager_Context
  8. */
  9. class Fieldmanager_Context_Submenu extends Fieldmanager_Context {
  10. /**
  11. * @var string
  12. * Parent of this submenu page
  13. */
  14. public $parent_slug;
  15. /**
  16. * @var string
  17. * Title of the page
  18. */
  19. public $page_title;
  20. /**
  21. * @var string
  22. * Menu title
  23. */
  24. public $menu_title;
  25. /**
  26. * @var string
  27. * Capability required
  28. */
  29. public $capability;
  30. /**
  31. * @var string
  32. * Menu slug
  33. */
  34. public $menu_slug;
  35. /**
  36. * @var string|Null
  37. * Only used for options pages
  38. */
  39. public $submit_button_label = Null;
  40. /**
  41. * @var string
  42. * For submenu pages, set autoload to true or false
  43. */
  44. public $wp_option_autoload = False;
  45. /**
  46. * Create a submenu page out of a field
  47. * @param string $parent_slug
  48. * @param string $page_title
  49. * @param string $menu_title
  50. * @param string $capability
  51. * @param string $menu_slug
  52. * @param Fieldmanager_Field $fm
  53. */
  54. public function __construct( $parent_slug, $page_title, $menu_title = Null, $capability = 'manage_options', $menu_slug = Null, $fm ) {
  55. $this->fm = $fm;
  56. $this->menu_slug = $menu_slug ?: $this->fm->name;
  57. $this->menu_title = $menu_title ?: $page_title;
  58. $this->parent_slug = $parent_slug;
  59. $this->page_title = $page_title;
  60. $this->capability = $capability;
  61. $this->uniqid = $this->fm->get_element_id() . '_form';
  62. add_action( 'admin_menu', array( $this, 'register_submenu_page' ) );
  63. add_action( 'admin_init', array( $this, 'handle_submenu_save' ) );
  64. }
  65. /**
  66. * Register a submenu page with WordPress
  67. * @return void
  68. */
  69. public function register_submenu_page() {
  70. add_submenu_page( $this->parent_slug, $this->page_title, $this->menu_title, $this->capability, $this->menu_slug, array( $this, 'render_submenu_page' ) );
  71. }
  72. /**
  73. * Helper to attach element_markup() to add_meta_box(). Prints markup for options page.
  74. * @return void.
  75. */
  76. public function render_submenu_page() {
  77. $values = get_option( $this->fm->name, Null );
  78. echo '<div class="wrap">';
  79. screen_icon();
  80. printf( '<h2>%s</h2>', $this->page_title );
  81. echo '<form method="POST" id="' . esc_attr( $this->uniqid ) . '">';
  82. echo '<div class="fm-submenu-form-wrapper">';
  83. printf( '<input type="hidden" name="fm-options-action" value="%s" />', sanitize_title( $this->fm->name ) );
  84. wp_nonce_field( 'fieldmanager-save-' . $this->fm->name, 'fieldmanager-' . $this->fm->name . '-nonce' );
  85. echo $this->fm->element_markup( $values );
  86. echo '</div>';
  87. printf( '<input type="submit" name="fm-submit" class="button-primary" value="%s" />', esc_attr( $this->submit_button_label ) ?: __( 'Save Options' ) );
  88. echo '</form>';
  89. echo '</div>';
  90. // Check if any validation is required
  91. $fm_validation = Fieldmanager_Util_Validation( $this->uniqid, 'submenu' );
  92. $fm_validation->add_field( $this->fm );
  93. }
  94. /**
  95. * Save a submenu page
  96. * @return void
  97. */
  98. public function handle_submenu_save() {
  99. if ( ! empty( $_POST ) && $_GET['page'] == $this->fm->name && current_user_can( $this->capability ) ) {
  100. // Make sure that our nonce field arrived intact
  101. if( !wp_verify_nonce( $_POST['fieldmanager-' . $this->fm->name . '-nonce'], 'fieldmanager-save-' . $this->fm->name ) ) {
  102. $this->_unauthorized_access( 'Nonce validation failed' );
  103. }
  104. $this->fm->data_id = $this->fm->name;
  105. $this->fm->data_type = 'options';
  106. $current = get_option( $this->fm->name );
  107. $value = isset( $_POST[ $this->fm->name ] ) ? $_POST[ $this->fm->name ] : "";
  108. $data = $this->fm->presave_all( $value, $current );
  109. $data = apply_filters( 'fm_submenu_presave_data', $data, $this );
  110. if ( get_option( $this->fm->name ) ) {
  111. update_option( $this->fm->name, $data );
  112. } else {
  113. add_option( $this->fm->name, $data, ' ', $this->wp_option_autoload ? 'yes' : 'no' );
  114. }
  115. }
  116. }
  117. }