PluginProbe ʕ •ᴥ•ʔ
External Links – nofollow, noopener & new window / 1.60
External Links – nofollow, noopener & new window v1.60
0.31 0.32 0.33 0.34 0.35 1.01 1.02 1.03 1.10 1.20 1.21 1.30 1.31 1.40 1.41 1.50 1.51 1.52 1.53 1.54 1.55 1.56 1.60 1.61 1.62 1.70 1.80 1.81 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 2.3 2.32 2.35 2.40 2.42 2.43 2.45 2.46 2.47 2.48 2.50 2.51 2.55 2.56 2.57 2.58 2.59 2.60 2.61 2.62 2.63 2.64 2.65 trunk 0.10 0.11 0.12 0.20 0.21 0.30
wp-external-links / includes / wp-plugin-dev-classes / class-wp-option-forms.php
wp-external-links / includes / wp-plugin-dev-classes Last commit date
class-wp-meta-box-page.php 11 years ago class-wp-option-forms.php 11 years ago
class-wp-option-forms.php
361 lines
1 <?php defined( 'ABSPATH' ) OR die( 'No direct access.' );
2 if ( ! class_exists( 'WP_Option_Forms_01' ) ):
3 /**
4 * WP_Option_Forms_01 Class
5 *
6 * Simple class for creating option forms of a the same option_group.
7 * Also with Ajax save support.
8 *
9 * Requires WordPress 3.0+ and PHP 5.2+
10 *
11 * @version 0.1
12 * @author Victor Villaverde Laan
13 * @link http://www.freelancephp.net/
14 * @license Dual licensed under the MIT and GPL licenses
15 */
16 class WP_Option_Forms_01 {
17
18 /**
19 * Name used as prefix for saving option names
20 * @var string
21 */
22 protected $name = NULL;
23
24 /**
25 * Option names and values
26 * @var string
27 */
28 protected $options = array();
29
30 /**
31 * Current option name
32 * @var string
33 */
34 protected $current_option = NULL;
35
36
37 /**
38 * Constructor
39 * @param array $name
40 * @param array $options Optional
41 */
42 public function __construct( $name, $options = array() ) {
43 $this->name = sanitize_title_with_dashes( $name );
44
45 // set option names
46 foreach ( $options AS $option_name => $values ) {
47 $this->add_option( $option_name, $values );
48 }
49
50 // actions
51 add_action( 'wp_ajax_wpof_update_options', array( $this, 'call_wp_ajax' ) );
52 add_action( 'admin_menu', array( $this, 'call_admin_menu' ) );
53 }
54
55 /**
56 * Admin menu callback
57 */
58 public function call_admin_menu() {
59 // Register settings
60 foreach ( $this->options AS $option_name => $values ) {
61 register_setting( $option_name, $option_name );
62 }
63
64 // script
65 // wp_enqueue_script( 'option-forms', plugins_url( 'js/wp-option-forms.js', WP_EXTERNAL_LINKS_FILE ), array( 'jquery' ), '1.0' );
66 }
67
68 /**
69 * Ajax call for saving option values
70 */
71 public function call_wp_ajax() {
72 check_ajax_referer( 'wpof_update_options', 'wpof-nonce' );
73
74 $option_name = $_POST[ 'ajax_option_name' ];
75 $value = NULL;
76
77 if ( isset( $_POST[ $option_name ] ) )
78 $value = $_POST[ $option_name ];
79
80 if ( ! is_array( $value ) )
81 $value = trim( $value );
82
83 $value = stripslashes_deep( $value );
84
85 update_option( $option_name, $value );
86
87 die( '1' );
88 }
89
90 /**
91 * Add option (or reset option when already exists)
92 * @param string $option_name
93 * @param array $default_values Optional
94 * @return this
95 */
96 public function add_option( $option_name, $default_values = array() ) {
97 // set values
98 $saved_values = get_option( $this->name .'-'. $option_name );
99
100 if ( empty( $saved_values ) ) {
101 foreach ( $default_values AS $key => $value )
102 $values[ $key ] = $value;
103 } else {
104 foreach ( $default_values AS $key => $value )
105 $values[ $key ] = '';
106
107 foreach ( $saved_values AS $key => $value )
108 $values[ $key ] = $value;
109 }
110
111 // option and values
112 $this->options[ $this->name .'-'. $option_name ] = $values;
113 return $this;
114 }
115
116 /**
117 * Set current option to use
118 * @param string $option_name
119 * @return this
120 */
121 public function set_current_option( $option_name ) {
122 $this->current_option = $this->name .'-'. $option_name;
123 return $this;
124 }
125
126 /**
127 * Get opening form with all nescessary WP fields
128 * @param boolean $ajaxSave Optional
129 * @param array $attrs Optional
130 * @return string
131 */
132 public function open_form( $ajaxSave = TRUE, $attrs = array() ) {
133 // set class for ajax or non-ajax form
134 $attrs[ 'class' ] = ( ( $ajaxSave ) ? 'ajax-form' : 'no-ajax-form' )
135 . ( ( key_exists( 'class', $attrs ) ) ? ' '. $attrs[ 'class' ] : '' );
136
137 // show start form
138 $html = '';
139 $html .= '<form method="post" action="options.php" '. $this->attrs( $attrs ) .'>';
140
141 if ( $ajaxSave ) {
142 $html .= wp_nonce_field( 'wpof_update_options', 'wpof-nonce', FALSE, FALSE );
143 $html .= '<input type="hidden" name="action" value="wpof_update_options" />';
144 $html .= '<input type="hidden" name="ajax_option_name" value="'. $this->current_option .'" />';
145
146 // instead of using settings_fields();
147 $html .= '<input type="hidden" name="option_page" value="' . esc_attr( $this->current_option ) . '" />';
148 $html .= wp_nonce_field( $this->current_option . '-options', '_wpnonce', TRUE, FALSE );
149 } else {
150 // instead of using settings_fields();
151 $html .= '<input type="hidden" name="option_page" value="' . esc_attr( $this->current_option ) . '" />';
152 $html .= '<input type="hidden" name="action" value="update" />';
153 $html .= wp_nonce_field( $this->current_option . '-options', '_wpnonce', TRUE, FALSE );
154 }
155
156 return $html;
157 }
158
159 /**
160 * Get script for saving screen option
161 * @param string $option_name
162 * @param string $key
163 * @return string
164 */
165 public function open_screen_option( $option_name, $key ) {
166 $this->set_current_option( $option_name );
167
168 $html = '';
169 $html .= '<script type="text/javascript">' . "\n";
170 $html .= '//<![CDATA[' . "\n";
171 $html .= 'jQuery( document ).ready( function( $ ){' . "\n";
172 $html .= "\t" . '// save screen option' . "\n";
173 $html .= "\t" . '$( "#screen-meta #'. $key .'" )' . "\n";
174 $html .= "\t\t" . '.change(function(){' . "\n";
175 $html .= "\t\t\t" . 'var self = this;' . "\n";
176 $html .= "\t\t\t" . '$.post( ajaxurl, {' . "\n";
177 $html .= "\t\t\t\t" . 'action: "wpof_update_options",' . "\n";
178 $html .= "\t\t\t\t" . '"wpof-nonce": "'. wp_create_nonce( 'wpof_update_options' ) .'",' . "\n";
179 $html .= "\t\t\t\t" . 'ajax_option_name: "'. $this->current_option .'",' . "\n";
180 $html .= "\t\t\t\t" . '"'. $this->field_name( $key ) .'": $( this ).val()' . "\n";
181 $html .= "\t\t\t" . '}, function () {' . "\n";
182 $html .= "\t\t\t\t" . '$( self ).trigger( "ajax_updated" );' . "\n";
183 $html .= "\t\t\t" . '});' . "\n";
184 $html .= "\t\t" . '});' . "\n";
185 $html .= '});' . "\n";
186 $html .= '//]]>' . "\n";
187 $html .= '</script>' . "\n";
188
189 return $html;
190 }
191
192 /**
193 * Get closing form
194 * @return string
195 */
196 public function close_form() {
197 return '</form>';
198 }
199
200 /**
201 * Text field
202 * @param string $key
203 * @param array $attrs Optional
204 * @return string
205 */
206 public function text( $key, $attrs = array() ) {
207 if ( ! key_exists( 'class', $attrs ) )
208 $attrs[ 'class' ] = 'regular-text';
209
210 return '<input type="text" '. $this->attrs( $attrs, $key, $this->value( $key ) ) .' />';
211 }
212
213 /**
214 * Text field
215 * @param string $key
216 * @param array $attrs Optional
217 * @return string
218 */
219 public function textarea( $key, $attrs = array() ) {
220 if ( ! key_exists( 'class', $attrs ) )
221 $attrs[ 'class' ] = 'large-text';
222
223 return '<textarea '. $this->attrs( $attrs, $key ) .'>'. $this->value( $key ) .'</textarea>';
224 }
225
226 /**
227 * Radio field
228 * @param string $key
229 * @param mixed $value
230 * @param array $attrs Optional
231 * @return string
232 */
233 public function radio( $key, $value, $attrs = array() ) {
234 $checked = ( $value == $this->value( $key ) ) ? ' checked="checked"' : '';
235 return '<input type="radio" '. $this->attrs( $attrs, $key, $value )
236 . $checked . ' />';
237 }
238
239 /**
240 * Checkbox field
241 * @param string $key
242 * @param mixed $value
243 * @param array $attrs Optional
244 * @return string
245 */
246 public function checkbox( $key, $value, $attrs = array() ) {
247 $checked = ( $value == $this->value( $key ) ) ? ' checked="checked"' : '';
248 return '<input type="checkbox" '. $this->attrs( $attrs, $key, $value )
249 . $checked . ' />';
250 }
251
252 /**
253 * Select field
254 * @param string $key
255 * @param array $options Optional
256 * @param array $attrs Optional
257 * @return string
258 */
259 public function select( $key, $options = array(), $attrs = array() ) {
260 $html = '<select '. $this->attrs( $attrs, $key ) .'>';
261
262 foreach ( $options AS $value => $label ) {
263 $selected = ( $value == $this->value( $key ) ) ? ' selected="selected"' : '';
264 $html .= '<option value="'. $value .'"'. $selected .'>'. $label .'</option>';
265 }
266
267 $html .= '</select>';
268 return $html;
269 }
270
271 /**
272 * Submit button
273 * @param array $attrs Optional
274 * @return string
275 */
276 public function submit( $attrs = array() ) {
277 // set class attr
278 $attrs[ 'class' ] = 'button-primary'. ( ( key_exists( 'class', $attrs ) ) ? ' '. $attrs[ 'class' ] : '' );
279
280 // show submit
281 $html = '';
282 $html .= '<p class="button-controls" style="text-align:right;">';
283 $html .= '<img alt="" title="" class="ajax-feedback" src="'. get_bloginfo( 'url' ) .'/wp-admin/images/wpspin_light.gif" style="visibility: hidden;" />';
284 $html .= '<input type="submit" '. $this->attrs( $attrs, '', __( 'Save Changes' ) ) .' />';
285 $html .= '</p>';
286 return $html;
287 }
288
289 /**
290 * Get field name of given key
291 * @param string $key
292 * @return string
293 */
294 public function field_name( $key ) {
295 return $this->current_option . '[' . $key . ']';
296 }
297
298 /**
299 * Get value of given option key
300 * @param string $key
301 * @param mixed $default_value Optional
302 * @param boolean $option_name Optional, search in given option_name instead of the current option
303 * @return mixed
304 */
305 public function value( $key, $default_value = NULL, $option_name = NULL ) {
306 if ( $option_name === NULL ) {
307 $option = $this->current_option;
308 } else {
309 $option = $this->name . '-' . $option_name;
310 }
311
312 if (!isset($this->options[ $option ])) {
313 return $default_value;
314 }
315
316 $values = $this->options[ $option ];
317
318 return ( is_array( $values ) AND key_exists( $key, $values ) AND $values[ $key ] !== NULL ) ? $values[ $key ] : $default_value;
319 }
320
321 /**
322 * Delete and unregister option
323 */
324 public function delete_options() {
325 foreach ( $this->options AS $option_name => $values ) {
326 delete_option( $option_name );
327 }
328 }
329
330 /**
331 * Get string of given attributes
332 * @param array $attrs
333 * @param string $key Optional
334 * @param mixed $value Optional
335 * @return string
336 */
337 protected function attrs( $attrs, $key = NULL, $value = NULL ) {
338 $str = '';
339
340 // set name, id, value attr
341 if ( $key !== NULL ) {
342 $str .= 'name="' . $this->field_name( $key ) .'" ';
343 if ( ! key_exists( 'id', $attrs ) )
344 $str .= 'id="' . $key .'" ';
345 }
346
347 if ( $value !== NULL )
348 $str .= 'value="' . $value .'" ';
349
350 foreach ( $attrs AS $attr => $value )
351 $str .= $attr .'="'. $value .'" ';
352
353 return $str;
354 }
355
356 } // End WP_Option_Forms_01
357
358 endif;
359
360 /* ommit PHP closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */
361