Skip to content

Commit b987a80

Browse files
committed
Add inputs validation function
1 parent 7b8f054 commit b987a80

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

README.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Donate link: http://1fix.io/
55
Tags: featured image, metabox
66
Requires at least: 3.5
77
Tested up to: 3.9.1
8-
Stable tag: 0.6.0
8+
Stable tag: 0.7.0
99
License: GPLv2 or later
1010
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1111

@@ -46,5 +46,5 @@ Custom the title, content and link / button text in the Featured Image metabox.
4646

4747
== Changelog ==
4848

49-
= 0.6.0 =
49+
= 0.7.0 =
5050
* The first version

admin/includes/settings.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ public function admin_init() {
6767
} // end if
6868

6969
$post_types = $this->supported_post_types();
70+
$options = get_option( $this->plugin_slug );
7071

7172
foreach ( $post_types as $pt ) {
7273
$post_object = get_post_type_object( $pt );
74+
$args = array( $pt, $options[$pt] );
7375

7476
add_settings_section(
7577
$pt,
@@ -84,7 +86,7 @@ public function admin_init() {
8486
array( $this, 'title_callback' ),
8587
$this->plugin_slug,
8688
$pt,
87-
array( $pt )
89+
$args
8890
);
8991

9092
add_settings_field(
@@ -93,7 +95,7 @@ public function admin_init() {
9395
array( $this, 'instruction_callback' ),
9496
$this->plugin_slug,
9597
$pt,
96-
array( $pt )
98+
$args
9799
);
98100

99101
add_settings_field(
@@ -102,7 +104,7 @@ public function admin_init() {
102104
array( $this, 'link_text_callback' ),
103105
$this->plugin_slug,
104106
$pt,
105-
array( $pt )
107+
$args
106108
);
107109

108110
add_settings_field(
@@ -111,13 +113,14 @@ public function admin_init() {
111113
array( $this, 'button_text_callback' ),
112114
$this->plugin_slug,
113115
$pt,
114-
array( $pt )
116+
$args
115117
);
116118
}
117119

118120
register_setting(
119121
$this->plugin_slug,
120-
$this->plugin_slug
122+
$this->plugin_slug,
123+
array( $this, 'validate_inputs' )
121124
);
122125

123126
} // end admin_init
@@ -170,8 +173,7 @@ public function supported_post_types() {
170173

171174
public function title_callback( $args ) {
172175

173-
$options = get_option( $this->plugin_slug );
174-
$value = isset( $options['title'] ) ? $options['title'] : '';
176+
$value = isset( $args[1]['title'] ) ? $args[1]['title'] : '';
175177

176178
$html = '<input type"text" id="title" name="' . $this->plugin_slug . '[' . $args[0] . '][title]" value="' . $value . '" class="regular-text" />';
177179
$html .= '<p class="description">' . __( 'Enter your custom title for Featured Image Metabox.', $this->plugin_slug ) . '</p>';
@@ -182,8 +184,7 @@ public function title_callback( $args ) {
182184

183185
public function instruction_callback( $args ) {
184186

185-
$options = get_option( $this->plugin_slug );
186-
$value = isset( $options['instruction'] ) ? $options['instruction'] : '';
187+
$value = isset( $args[1]['instruction'] ) ? $args[1]['instruction'] : '';
187188

188189
$html = '<input type"text" id="instruction" name="' . $this->plugin_slug . '[' . $args[0] . '][instruction]" value="' . $value . '" class="regular-text" />';
189190
$html .= '<p class="description">' . __( 'Enter the instruction for Featured Image, like image dimensions.', $this->plugin_slug ) . '</p>';
@@ -194,8 +195,7 @@ public function instruction_callback( $args ) {
194195

195196
public function link_text_callback( $args ) {
196197

197-
$options = get_option( $this->plugin_slug );
198-
$value = isset( $options['link_text'] ) ? $options['link_text'] : '';
198+
$value = isset( $args[1]['link_text'] ) ? $args[1]['link_text'] : '';
199199

200200
$html = '<input type"text" id="link_text" name="' . $this->plugin_slug . '[' . $args[0] . '][link_text]" value="' . $value . '" class="regular-text" />';
201201
$html .= '<p class="description">' . sprintf( __( 'Enter the custom link text to replace the "%s".', $this->plugin_slug ), __( 'Set featured image' ) ) . '</p>';
@@ -206,15 +206,40 @@ public function link_text_callback( $args ) {
206206

207207
public function button_text_callback( $args ) {
208208

209-
$options = get_option( $this->plugin_slug );
210-
$value = isset( $options['button_text'] ) ? $options['button_text'] : '';
209+
$value = isset( $args[1]['button_text'] ) ? $args[1]['button_text'] : '';
211210

212211
$html = '<input type"text" id="button_text" name="' . $this->plugin_slug . '[' . $args[0] . '][button_text]" value="' . $value . '" class="regular-text" />';
213212
$html .= '<p class="description">' . sprintf( __( 'Enter the custom button text to replace the "%s".', $this->plugin_slug ), __( 'Set featured image' ) ) . '</p>';
214213

215214
echo $html;
216215

217216
} // end button_text_callback
217+
218+
/**
219+
* Validate inputs
220+
*
221+
* @return array Sanitized data
222+
*
223+
* @since 0.7.0
224+
*/
225+
public function validate_inputs( $inputs ) {
226+
227+
$outputs = array();
228+
229+
foreach( $inputs as $key => $value ) {
230+
if ( is_array( $value ) ) {
231+
foreach ( $value as $k => $v ) {
232+
$outputs[$key][$k] = sanitize_text_field( $v );
233+
}
234+
} else {
235+
$outputs[$key] = sanitize_text_field( $value );
236+
}
237+
238+
}
239+
240+
return apply_filters( 'cfim_validate_inputs', $outputs, $inputs );
241+
242+
} // end validate_inputs
218243
}
219244

220245
Custom_Featured_Image_Metabox_Settings::get_instance();

custom-featured-image-metabox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Plugin Name: Custom Featured Image Metabox
1515
* Plugin URI: http://1fix.io
1616
* Description: Custom the title, content and link / button text in the Featured Image metabox.
17-
* Version: 0.6.0
17+
* Version: 0.7.0
1818
* Author: 1fixdotio
1919
* Author URI: http://1fix.io
2020
* Text Domain: cfim

public/class-custom-featured-image-metabox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Custom_Featured_Image_Metabox {
2828
*
2929
* @var string
3030
*/
31-
const VERSION = '0.6.0';
31+
const VERSION = '0.7.0';
3232

3333
/**
3434
* Unique identifier for your plugin.

0 commit comments

Comments
 (0)