PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/php/context/class-fieldmanager-context-page.php

https://github.com/Automattic/wordpress-fieldmanager
PHP | 108 lines | 51 code | 12 blank | 45 comment | 7 complexity | 68b6204865f2be649b312647a71ec2b4 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * @package Fieldmanager_Context
  4. */
  5. /**
  6. * Use fieldmanager on the public-facing theme.
  7. * @package Fieldmanager_Datasource
  8. */
  9. class Fieldmanager_Context_Page extends Fieldmanager_Context {
  10. /**
  11. * @var Fieldmanager_Context_Page[]
  12. * Array of contexts for rendering forms
  13. */
  14. public static $forms = array();
  15. /**
  16. * @var boolean
  17. * Was the form saved?
  18. */
  19. public $did_save = False;
  20. /**
  21. * Create page context handler.
  22. * @param string unique form ID
  23. * @param Fieldmanager_Field $fm
  24. */
  25. public function __construct( $uniqid, $fm ) {
  26. $this->fm = $fm;
  27. self::$forms[$uniqid] = $this;
  28. $this->uniqid = $uniqid;
  29. // since this should be set up in init, check for submit now
  30. if ( !empty( $_POST ) && ! empty( $_POST['fm-page-action'] ) && esc_html( $_POST['fm-page-action'] ) == $uniqid ) {
  31. $this->save_page_form();
  32. }
  33. }
  34. /**
  35. * Action to save the form
  36. * @return void
  37. */
  38. public function save_page_form() {
  39. if( !wp_verify_nonce( $_POST['fieldmanager-' . $this->fm->name . '-nonce'], 'fieldmanager-save-' . $this->fm->name ) ) {
  40. $this->fm->_unauthorized_access( 'Nonce validation failed' );
  41. }
  42. $this->fm->data_id = $user_id;
  43. $value = isset( $_POST[ $this->fm->name ] ) ? $_POST[ $this->fm->name ] : "";
  44. if ( empty( $this->fm->data_type ) ) $this->fm->data_type = 'page';
  45. if ( empty( $this->fm->data_id ) ) $this->fm->data_id = $this->uniqid;
  46. $current = apply_filters( 'fm_' . $this->uniqid . '_load', array(), $this->fm );
  47. $data = apply_filters( 'fm_' . $this->uniqid . '_presave', $value, $this->fm );
  48. $data = $this->fm->presave_all( $data, $current );
  49. $data = apply_filters( 'fm_presave_data', $data, $this->fm );
  50. do_action( 'fm_' . $this->uniqid . '_save', $data, $current, $this->fm );
  51. $this->did_save = True;
  52. }
  53. /**
  54. * Output HTML for the form
  55. * @return void
  56. */
  57. public function render_page_form() {
  58. $current = apply_filters( 'fm_' . $this->uniqid . '_load', array(), $this->fm );
  59. echo '<form method="POST" id="' . esc_attr( $this->uniqid ) . '">';
  60. echo '<div class="fm-page-form-wrapper">';
  61. printf( '<input type="hidden" name="fm-page-action" value="%s" />', sanitize_title( $this->uniqid ) );
  62. wp_nonce_field( 'fieldmanager-save-' . $this->fm->name, 'fieldmanager-' . $this->fm->name . '-nonce' );
  63. echo $this->fm->element_markup( $current );
  64. echo '</div>';
  65. printf( '<input type="submit" name="fm-submit" class="button-primary" value="%s" />', esc_attr( $this->fm->submit_button_label ) ?: __( 'Save Options' ) );
  66. echo '</form>';
  67. echo '</div>';
  68. // Check if any validation is required
  69. $fm_validation = Fieldmanager_Util_Validation( $this->uniqid, 'page' );
  70. $fm_validation->add_field( $this->fm );
  71. }
  72. /**
  73. * Get a form by ID, used for rendering
  74. * @param string $uniqid
  75. * @return Fieldmanager_Context_Page
  76. */
  77. public static function get_form( $uniqid ) {
  78. return self::$forms[$uniqid];
  79. }
  80. }
  81. /**
  82. * Template tag to output a form by unique ID
  83. * @param string $uniqid
  84. * @return void
  85. */
  86. function fm_the_page_form( $uniqid ) {
  87. Fieldmanager_Context_Page::get_form( $uniqid )->render_page_form();
  88. }
  89. /**
  90. * Check to see if the form saved (useful for error messages)
  91. * @param string $uniqid
  92. * @return void
  93. */
  94. function fm_page_form_did_save( $uniqid ) {
  95. return Fieldmanager_Context_Page::get_form( $uniqid )->did_save;
  96. }