1 <?php
2
3 4 5 6 7
8 class Fieldmanager_Context_Page extends Fieldmanager_Context {
9
10 11 12 13
14 public static $forms = array();
15
16 17 18 19
20 public $did_save = False;
21
22 23 24 25 26
27 public function __construct( $uniqid, $fm ) {
28 $this->fm = $fm;
29 self::$forms[$uniqid] = $this;
30 $this->uniqid = $uniqid;
31
32
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 40 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 60 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
75 $fm_validation = Fieldmanager_Util_Validation( $this->uniqid, 'page' );
76 $fm_validation->add_field( $this->fm );
77 }
78
79 80 81 82 83
84 public static function get_form( $uniqid ) {
85 return self::$forms[$uniqid];
86 }
87
88 }
89
90 91 92 93 94
95 function fm_the_page_form( $uniqid ) {
96 Fieldmanager_Context_Page::get_form( $uniqid )->render_page_form();
97 }
98
99 100 101 102 103
104 function fm_page_form_did_save( $uniqid ) {
105 return Fieldmanager_Context_Page::get_form( $uniqid )->did_save;
106 }
107