21
21
*/
22
22
class Custom_Featured_Image_Metabox_Admin {
23
23
24
+ /**
25
+ * Unique identifier for your plugin.
26
+ *
27
+ *
28
+ * Call $plugin_slug from public plugin class later.
29
+ *
30
+ * @since 0.8.0
31
+ *
32
+ * @var string
33
+ */
34
+ protected $ plugin_slug = null ;
35
+
36
+ protected $ plugin_options = null ;
37
+
24
38
/**
25
39
* Instance of this class.
26
40
*
@@ -53,6 +67,7 @@ private function __construct() {
53
67
*/
54
68
$ plugin = Custom_Featured_Image_Metabox::get_instance ();
55
69
$ this ->plugin_slug = $ plugin ->get_plugin_slug ();
70
+ $ this ->plugin_options = $ plugin ->get_plugin_options ();
56
71
57
72
// Add the options page and menu item.
58
73
require_once ( plugin_dir_path ( __FILE__ ) . 'includes/settings.php ' );
@@ -64,14 +79,9 @@ private function __construct() {
64
79
$ plugin_basename = plugin_basename ( plugin_dir_path ( realpath ( dirname ( __FILE__ ) ) ) . 'custom-featured-image-metabox.php ' );
65
80
add_filter ( 'plugin_action_links_ ' . $ plugin_basename , array ( $ this , 'add_action_links ' ) );
66
81
67
- /*
68
- * Define custom functionality.
69
- *
70
- * Read more about actions and filters:
71
- * http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
72
- */
73
- // add_action( '@TODO', array( $this, 'action_method_name' ) );
74
- // add_filter( '@TODO', array( $this, 'filter_method_name' ) );
82
+ add_action ( 'add_meta_boxes ' , array ( $ this , 'change_metabox_title ' ) );
83
+ add_filter ( 'admin_post_thumbnail_html ' , array ( $ this , 'change_metabox_content ' ) );
84
+ add_filter ( 'media_view_strings ' , array ( $ this , 'change_media_strings ' ), 10 , 2 );
75
85
76
86
}
77
87
@@ -143,29 +153,86 @@ public function add_action_links( $links ) {
143
153
}
144
154
145
155
/**
146
- * NOTE: Actions are points in the execution of a page or process
147
- * lifecycle that WordPress fires.
156
+ * Change the title of Featured Image Metabox
148
157
*
149
- * Actions: http://codex.wordpress.org/Plugin_API#Actions
150
- * Reference: http://codex.wordpress.org/Plugin_API/Action_Reference
158
+ * @return null
151
159
*
152
- * @since 0.1 .0
160
+ * @since 0.8 .0
153
161
*/
154
- public function action_method_name () {
155
- // @TODO: Define your action hook callback here
156
- }
162
+ public function change_metabox_title () {
163
+
164
+ $ options = $ this ->plugin_options ;
165
+
166
+ $ screen = get_current_screen ();
167
+ $ post_type = $ screen ->id ;
168
+
169
+ if ( isset ( $ options [$ post_type ] ) && isset ( $ options [$ post_type ]['title ' ] ) && ! empty ( $ options [$ post_type ]['title ' ] ) ) {
170
+ //remove original featured image metabox
171
+ remove_meta_box ( 'postimagediv ' , $ post_type , 'side ' );
172
+
173
+ //add our customized metabox
174
+ add_meta_box ( 'postimagediv ' , $ options [$ post_type ]['title ' ], 'post_thumbnail_meta_box ' , $ post_type , 'side ' , 'low ' );
175
+ }
176
+
177
+ } // end change_metabox_title
157
178
158
179
/**
159
- * NOTE: Filters are points of execution in which WordPress modifies data
160
- * before saving it or sending it to the browser.
180
+ * Change metabox content
161
181
*
162
- * Filters: http://codex.wordpress.org/Plugin_API#Filters
163
- * Reference: http://codex.wordpress.org/Plugin_API/Filter_Reference
182
+ * @param string $content HTML string
183
+ * @return string Modified content
164
184
*
165
- * @since 0.1 .0
185
+ * @since 0.8 .0
166
186
*/
167
- public function filter_method_name () {
168
- // @TODO: Define your filter hook callback here
169
- }
187
+ public function change_metabox_content ( $ content ) {
188
+
189
+ $ options = $ this ->plugin_options ;
190
+
191
+ $ screen = get_current_screen ();
192
+ $ post_type = $ screen ->id ;
193
+
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> ' ;
196
+
197
+ $ content = $ instruction . $ content ;
198
+ }
199
+
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 );
202
+ }
203
+
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 );
206
+ }
207
+
208
+ return $ content ;
209
+
210
+ } // end change_metabox_content
211
+
212
+ /**
213
+ * Change the strings in media manager
214
+ *
215
+ * @param array $strings Strings array
216
+ * @param object $post Post object
217
+ * @return array Modified strings array
218
+ *
219
+ * @since 0.8.0
220
+ */
221
+ public function change_media_strings ( $ strings , $ post ) {
222
+
223
+ $ options = $ this ->plugin_options ;
224
+ $ post_type = $ post ->post_type ;
225
+
226
+ 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 ' ];
230
+ }
231
+
232
+ }
233
+
234
+ return $ strings ;
235
+
236
+ } // end change_media_strings
170
237
171
238
}
0 commit comments