Skip to content

Commit 310bc10

Browse files
committed
Change settings into section groups
1 parent d1140c3 commit 310bc10

File tree

7 files changed

+100
-126
lines changed

7 files changed

+100
-126
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.8.0
8+
Stable tag: 0.9.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.8.0 =
49+
= 0.9.0 =
5050
* The first version

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class Custom_Featured_Image_Metabox_Admin {
3333
*/
3434
protected $plugin_slug = null;
3535

36-
protected $plugin_options = null;
37-
3836
/**
3937
* Instance of this class.
4038
*
@@ -67,7 +65,6 @@ private function __construct() {
6765
*/
6866
$plugin = Custom_Featured_Image_Metabox::get_instance();
6967
$this->plugin_slug = $plugin->get_plugin_slug();
70-
$this->plugin_options = $plugin->get_plugin_options();
7168

7269
// Add the options page and menu item.
7370
require_once( plugin_dir_path( __FILE__ ) . 'includes/settings.php' );
@@ -161,17 +158,16 @@ public function add_action_links( $links ) {
161158
*/
162159
public function change_metabox_title() {
163160

164-
$options = $this->plugin_options;
165-
166161
$screen = get_current_screen();
167162
$post_type = $screen->id;
163+
$options = get_option( $this->plugin_slug . '_' . $post_type );
168164

169-
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['title'] ) && ! empty( $options[$post_type]['title'] ) ) {
165+
if ( isset( $options['title'] ) && ! empty( $options['title'] ) ) {
170166
//remove original featured image metabox
171167
remove_meta_box( 'postimagediv', $post_type, 'side' );
172168

173169
//add our customized metabox
174-
add_meta_box( 'postimagediv', $options[$post_type]['title'], 'post_thumbnail_meta_box', $post_type, 'side', 'low' );
170+
add_meta_box( 'postimagediv', $options['title'], 'post_thumbnail_meta_box', $post_type, 'side', 'low' );
175171
}
176172

177173
} // end change_metabox_title
@@ -186,23 +182,22 @@ public function change_metabox_title() {
186182
*/
187183
public function change_metabox_content( $content ) {
188184

189-
$options = $this->plugin_options;
190-
191185
$screen = get_current_screen();
192186
$post_type = $screen->id;
187+
$options = get_option( $this->plugin_slug . '_' . $post_type );
193188

194-
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['instruction'] ) && ! empty( $options[$post_type]['instruction'] ) ) {
195-
$instruction = '<p class="cfim-instruction">' . $options[$post_type]['instruction'] . '</p>';
189+
if ( isset( $options['instruction'] ) && ! empty( $options['instruction'] ) ) {
190+
$instruction = '<p class="cfim-instruction">' . $options['instruction'] . '</p>';
196191

197192
$content = $instruction . $content;
198193
}
199194

200-
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['set_text'] ) && ! empty( $options[$post_type]['set_text'] ) ) {
201-
$content = str_replace( __( 'Set featured image' ), $options[$post_type]['set_text'], $content );
195+
if ( isset( $options['set_text'] ) && ! empty( $options['set_text'] ) ) {
196+
$content = str_replace( __( 'Set featured image' ), $options['set_text'], $content );
202197
}
203198

204-
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['remove_text'] ) && ! empty( $options[$post_type]['remove_text'] ) ) {
205-
$content = str_replace( __( 'Remove featured image' ), $options[$post_type]['remove_text'], $content );
199+
if ( isset( $options['remove_text'] ) && ! empty( $options['remove_text'] ) ) {
200+
$content = str_replace( __( 'Remove featured image' ), $options['remove_text'], $content );
206201
}
207202

208203
return $content;
@@ -220,13 +215,13 @@ public function change_metabox_content( $content ) {
220215
*/
221216
public function change_media_strings( $strings, $post ) {
222217

223-
$options = $this->plugin_options;
224218
$post_type = $post->post_type;
219+
$options = get_option( $this->plugin_slug . '_' . $post_type );
225220

226221
if ( ! empty( $post ) ) {
227-
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['set_text'] ) && ! empty( $options[$post_type]['set_text'] ) ) {
228-
$strings['setFeaturedImage'] = $options[$post_type]['set_text'];
229-
$strings['setFeaturedImageTitle'] = $options[$post_type]['set_text'];
222+
if ( isset( $options['set_text'] ) && ! empty( $options['set_text'] ) ) {
223+
$strings['setFeaturedImage'] = $options['set_text'];
224+
$strings['setFeaturedImageTitle'] = $options['set_text'];
230225
}
231226

232227
}

admin/includes/settings.php

Lines changed: 32 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Custom_Featured_Image_Metabox_Settings {
1414
*/
1515
protected $plugin_slug = null;
1616

17-
protected $plugin_options = null;
17+
protected $supported_post_types = null;
1818

1919
/**
2020
* Instance of this class.
@@ -35,11 +35,7 @@ private function __construct() {
3535

3636
$plugin = Custom_Featured_Image_Metabox::get_instance();
3737
$this->plugin_slug = $plugin->get_plugin_slug();
38-
39-
if ( false == get_option( $this->plugin_slug ) ) {
40-
add_option( $this->plugin_slug, $this->default_settings() );
41-
}
42-
$this->plugin_options = $plugin->get_plugin_options();
38+
$this->supported_post_types = $plugin->supported_post_types();
4339

4440
// Add settings page
4541
add_action( 'admin_init', array( $this, 'admin_init' ) );
@@ -70,25 +66,36 @@ public static function get_instance() {
7066
*/
7167
public function admin_init() {
7268

73-
$post_types = $this->supported_post_types();
74-
$options = $this->plugin_options;
69+
$post_types = $this->supported_post_types;
70+
$defaults = array(
71+
'title' => '',
72+
'instruction' => '',
73+
'set_text' => '',
74+
'remove_text' => '',
75+
);
7576

7677
foreach ( $post_types as $pt ) {
7778
$post_object = get_post_type_object( $pt );
78-
$args = array( $pt, $options[$pt] );
79+
$section = $this->plugin_slug . '_' . $pt;
80+
81+
if ( false == get_option( $section ) ) {
82+
add_option( $section, apply_filters( $section . '_default_settings', $defaults ) );
83+
}
84+
85+
$args = array( $section, get_option( $section ) );
7986

8087
add_settings_section(
8188
$pt,
8289
sprintf( __( 'Featured Image Metabox in %s', $this->plugin_slug ), $post_object->labels->name ),
8390
'',
84-
$this->plugin_slug
91+
$section
8592
);
8693

8794
add_settings_field(
8895
'title',
8996
__( 'Title Text', $this->plugin_slug ),
9097
array( $this, 'title_callback' ),
91-
$this->plugin_slug,
98+
$section,
9299
$pt,
93100
$args
94101
);
@@ -97,7 +104,7 @@ public function admin_init() {
97104
'instruction',
98105
__( 'Instruction', $this->plugin_slug ),
99106
array( $this, 'instruction_callback' ),
100-
$this->plugin_slug,
107+
$section,
101108
$pt,
102109
$args
103110
);
@@ -106,7 +113,7 @@ public function admin_init() {
106113
'set_text',
107114
__( 'Set Text', $this->plugin_slug ),
108115
array( $this, 'set_text_callback' ),
109-
$this->plugin_slug,
116+
$section,
110117
$pt,
111118
$args
112119
);
@@ -115,71 +122,25 @@ public function admin_init() {
115122
'remove_text',
116123
__( 'Remove Text', $this->plugin_slug ),
117124
array( $this, 'remove_text_callback' ),
118-
$this->plugin_slug,
125+
$section,
119126
$pt,
120127
$args
121128
);
122-
}
123-
124-
register_setting(
125-
$this->plugin_slug,
126-
$this->plugin_slug,
127-
array( $this, 'validate_inputs' )
128-
);
129-
130-
} // end admin_init
131-
132-
/**
133-
* Provides default values for the plugin settings.
134-
*
135-
* @return array<string> Default settings
136-
*/
137-
public function default_settings() {
138129

139-
$post_types = $this->supported_post_types();
140-
$keys = array(
141-
'title' => '',
142-
'instruction' => '',
143-
'set_text' => '',
144-
'remove_text' => '',
130+
register_setting(
131+
$section,
132+
$section,
133+
array( $this, 'validate_inputs' )
145134
);
146-
$defaults = array();
147-
148-
foreach ( $post_types as $pt ) {
149-
$defaults[$pt] = $keys;
150-
}
151-
152-
return apply_filters( 'cfim_default_settings', $defaults );
153-
154-
} // end default_settings
155-
156-
/**
157-
* Get post types with thumbnail support
158-
*
159-
* @return array supported post types
160-
*
161-
* @since 0.6.0
162-
*/
163-
public function supported_post_types() {
164-
165-
$post_types = get_post_types();
166-
$results = array();
167-
168-
foreach ( $post_types as $pt ) {
169-
if ( post_type_supports( $pt, 'thumbnail' ) ) {
170-
$results[] = $pt;
171-
}
172135
}
173136

174-
return $results;
175-
176-
} // end supported_post_types
137+
} // end admin_init
177138

178139
public function title_callback( $args ) {
179140

180141
$value = isset( $args[1]['title'] ) ? $args[1]['title'] : '';
181142

182-
$html = '<input type"text" id="title" name="' . $this->plugin_slug . '[' . $args[0] . '][title]" value="' . $value . '" class="regular-text" />';
143+
$html = '<input type"text" id="title" name="' . $args[0] . '[title]" value="' . $value . '" class="regular-text" />';
183144
$html .= '<p class="description">' . __( 'Enter your custom title for Featured Image Metabox.', $this->plugin_slug ) . '</p>';
184145

185146
echo $html;
@@ -190,7 +151,7 @@ public function instruction_callback( $args ) {
190151

191152
$value = isset( $args[1]['instruction'] ) ? $args[1]['instruction'] : '';
192153

193-
$html = '<input type"text" id="instruction" name="' . $this->plugin_slug . '[' . $args[0] . '][instruction]" value="' . $value . '" class="regular-text" />';
154+
$html = '<input type"text" id="instruction" name="' . $args[0] . '[instruction]" value="' . $value . '" class="regular-text" />';
194155
$html .= '<p class="description">' . __( 'Enter the instruction for Featured Image, like image dimensions.', $this->plugin_slug ) . '</p>';
195156

196157
echo $html;
@@ -201,7 +162,7 @@ public function set_text_callback( $args ) {
201162

202163
$value = isset( $args[1]['set_text'] ) ? $args[1]['set_text'] : '';
203164

204-
$html = '<input type"text" id="set_text" name="' . $this->plugin_slug . '[' . $args[0] . '][set_text]" value="' . $value . '" class="regular-text" />';
165+
$html = '<input type"text" id="set_text" name="' . $args[0] . '[set_text]" value="' . $value . '" class="regular-text" />';
205166
$html .= '<p class="description">' . sprintf( __( 'Enter the custom text to replace the default "%s".', $this->plugin_slug ), __( 'Set featured image' ) ) . '</p>';
206167

207168
echo $html;
@@ -212,7 +173,7 @@ public function remove_text_callback( $args ) {
212173

213174
$value = isset( $args[1]['remove_text'] ) ? $args[1]['remove_text'] : '';
214175

215-
$html = '<input type"text" id="remove_text" name="' . $this->plugin_slug . '[' . $args[0] . '][remove_text]" value="' . $value . '" class="regular-text" />';
176+
$html = '<input type"text" id="remove_text" name="' . $args[0] . '[remove_text]" value="' . $value . '" class="regular-text" />';
216177
$html .= '<p class="description">' . sprintf( __( 'Enter the custom text to replace the default "%s".', $this->plugin_slug ), __( 'Remove featured image' ) ) . '</p>';
217178

218179
echo $html;
@@ -231,20 +192,12 @@ public function validate_inputs( $inputs ) {
231192
$outputs = array();
232193

233194
foreach( $inputs as $key => $value ) {
234-
if ( is_array( $value ) ) {
235-
foreach ( $value as $k => $v ) {
236-
$outputs[$key][$k] = sanitize_text_field( $v );
237-
}
238-
} else {
239-
$outputs[$key] = sanitize_text_field( $value );
240-
}
241-
195+
$outputs[$key] = sanitize_text_field( $value );
242196
}
243197

244198
return apply_filters( 'cfim_validate_inputs', $outputs, $inputs );
245199

246200
} // end validate_inputs
247201
}
248202

249-
Custom_Featured_Image_Metabox_Settings::get_instance();
250-
?>
203+
Custom_Featured_Image_Metabox_Settings::get_instance();

admin/views/admin.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,31 @@
2020
<h2><?php echo esc_html( get_admin_page_title() ) . ' Settings'; ?></h2>
2121
<?php // settings_errors(); ?>
2222

23+
<?php
24+
$plugin = Custom_Featured_Image_Metabox::get_instance();
25+
$post_types = $plugin->supported_post_types();
26+
27+
if ( isset( $_GET['tab'] ) ) {
28+
$active_tab = $_GET['tab'];
29+
} else {
30+
$active_tab = ( isset( $post_types[0] ) ) ? $post_types[0] : '';
31+
}
32+
?>
33+
34+
<h2 class="nav-tab-wrapper">
35+
<?php foreach ( $post_types as $pt ) { ?>
36+
<a href="?page=<?php echo $plugin->get_plugin_slug(); ?>&tab=<?php echo $pt; ?>" class="nav-tab <?php echo ( $pt == $active_tab ) ? 'nav-tab-active' : ''; ?>"><?php $post_type_object = get_post_type_object( $pt ); echo $post_type_object->labels->name; ?></a>
37+
<?php } ?>
38+
</h2>
39+
2340
<form method="post" action="options.php">
2441
<?php
25-
$plugin = Custom_Featured_Image_Metabox::get_instance();
42+
$section = $plugin->get_plugin_slug() . '_' . $active_tab;
2643

27-
settings_fields( $plugin->get_plugin_slug() );
28-
do_settings_sections( $plugin->get_plugin_slug() );
44+
settings_fields( $section );
45+
do_settings_sections( $section );
2946

3047
submit_button();
31-
3248
?>
3349
</form>
3450

custom-featured-image-metabox.php

Lines changed: 1 addition & 13 deletions
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.8.0
17+
* Version: 0.9.0
1818
* Author: 1fixdotio
1919
* Author URI: http://1fix.io
2020
* Text Domain: cfim
@@ -50,18 +50,6 @@
5050
*----------------------------------------------------------------------------*/
5151

5252
/*
53-
* @TODO:
54-
*
55-
* - replace `class-custom-featured-image-metabox-admin.php` with the name of the plugin's admin file
56-
* - replace Custom_Featured_Image_Metabox_Admin with the name of the class defined in
57-
* `class-custom-featured-image-metabox-admin.php`
58-
*
59-
* If you want to include Ajax within the dashboard, change the following
60-
* conditional to:
61-
*
62-
* if ( is_admin() ) {
63-
* ...
64-
* }
6553
*
6654
* The code below is intended to to give the lightest footprint possible.
6755
*/

0 commit comments

Comments
 (0)