Fieldmanager
  • Package
  • Function
  • Tree
  • Todo

Packages

  • Fieldmanager
    • Context
    • Datasource
    • Field
    • Util
  • None

Functions

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