1 <?php
2
3 4 5 6 7 8
9 class extends Fieldmanager_Context_Storable {
10
11 12 13 14
15 public $parent_slug;
16
17 18 19 20
21 public $page_title;
22
23 24 25 26
27 public ;
28
29 30 31 32
33 public $capability;
34
35 36 37 38
39 public ;
40
41 42 43 44
45 public $submit_button_label = Null;
46
47 48 49 50 51 52
53 public $updated_message = null;
54
55 56 57 58
59 public $wp_option_autoload = False;
60
61 62 63 64 65 66 67 68 69
70 public function __construct( $parent_slug, $page_title, $menu_title = Null, $capability = 'manage_options', $menu_slug = Null, $fm = Null, $already_registered = False ) {
71 $this->fm = $fm;
72 $this->menu_slug = $menu_slug ?: $this->fm->name;
73 $this->menu_title = $menu_title ?: $page_title;
74 $this->parent_slug = $parent_slug;
75 $this->page_title = $page_title;
76 $this->capability = $capability;
77 $this->updated_message = __( 'Options updated', 'fieldmanager' );
78 $this->uniqid = $this->fm->get_element_id() . '_form';
79 if ( ! $already_registered ) {
80 add_action( 'admin_menu', array( $this, 'register_submenu_page' ) );
81 }
82 add_action( 'admin_init', array( $this, 'handle_submenu_save' ) );
83 }
84
85 86 87 88
89 public function () {
90 add_submenu_page( $this->parent_slug, $this->page_title, $this->menu_title, $this->capability, $this->menu_slug, array( $this, 'render_submenu_page' ) );
91 }
92
93 94 95 96
97 public function () {
98 $values = get_option( $this->fm->name, null );
99 ?>
100 <div class="wrap">
101 <?php if ( ! empty( $_GET['msg'] ) && 'success' == $_GET['msg'] ) : ?>
102 <div class="updated success"><p><?php echo esc_html( $this->updated_message ); ?></p></div>
103 <?php endif ?>
104
105 <h1><?php echo esc_html( $this->page_title ) ?></h1>
106
107 <form method="POST" id="<?php echo esc_attr( $this->uniqid ) ?>">
108 <div class="fm-submenu-form-wrapper">
109 <input type="hidden" name="fm-options-action" value="<?php echo sanitize_title( $this->fm->name ) ?>" />
110 <?php $this->render_field( array( 'data' => $values ) ); ?>
111 </div>
112 <?php submit_button( $this->submit_button_label, 'primary', 'fm-submit' ) ?>
113 </form>
114 </div>
115 <?php
116
117
118 $fm_validation = Fieldmanager_Util_Validation( $this->uniqid, 'submenu' );
119 $fm_validation->add_field( $this->fm );
120 }
121
122 123 124 125
126 public function handle_submenu_save() {
127 if ( empty( $_GET['page'] ) || $_GET['page'] != $this->menu_slug ) {
128 return;
129 }
130
131
132 if ( ! $this->is_valid_nonce() ) {
133 return;
134 }
135
136 if ( ! current_user_can( $this->capability ) ) {
137 $this->fm->_unauthorized_access( __( 'Current user cannot edit this page', 'fieldmanager' ) );
138 return;
139 }
140
141 if ( $this->save_submenu_data() ) {
142 wp_redirect( esc_url_raw( add_query_arg( array( 'msg' => 'success' ), $this->url() ) ) );
143 exit;
144 }
145 }
146
147 public function ( $data = null ) {
148 $this->fm->data_id = $this->fm->name;
149 $this->fm->data_type = 'options';
150 $current = get_option( $this->fm->name, null );
151 $data = $this->prepare_data( $current, $data );
152 $data = apply_filters( 'fm_submenu_presave_data', $data, $this );
153 if ( $this->fm->skip_save ) {
154 return true;
155 }
156
157 if ( isset( $current ) ) {
158 update_option( $this->fm->name, $data );
159 } else {
160 add_option( $this->fm->name, $data, '', $this->wp_option_autoload ? 'yes' : 'no' );
161 }
162
163 return true;
164 }
165
166 167 168 169 170 171
172 public function url() {
173 if ( $this->parent_slug && ! isset( $GLOBALS['_parent_pages'][ $this->parent_slug ] ) ) {
174 return admin_url( add_query_arg( 'page', $this->menu_slug, $this->parent_slug ) );
175 } else {
176 return admin_url( 'admin.php?page=' . $this->menu_slug );
177 }
178 }
179
180 181 182 183 184
185 protected function get_data( $data_id, $option_name, $single = false ) {
186 return get_option( $option_name, null );
187 }
188
189 190 191 192 193
194 protected function add_data( $data_id, $option_name, $option_value, $unique = false ) {
195 return add_option( $option_name, $option_value, '', $this->wp_option_autoload ? 'yes' : 'no' );
196 }
197
198 199 200 201 202
203 protected function update_data( $data_id, $option_name, $option_value, $option_prev_value = '' ) {
204 return update_option( $option_name, $option_value );
205 }
206
207 208 209 210 211
212 protected function delete_data( $data_id, $option_name, $option_value = '' ) {
213 return delete_option( $option_name, $option_value );
214 }
215 }
216