Complex classes like Black_Studio_TinyMCE_Admin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Black_Studio_TinyMCE_Admin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | final class Black_Studio_TinyMCE_Admin { |
||
22 | |||
23 | /** |
||
24 | * The single instance of the class |
||
25 | * |
||
26 | * @var object |
||
27 | * @since 2.0.0 |
||
28 | */ |
||
29 | protected static $_instance = null; |
||
30 | |||
31 | /** |
||
32 | * Array containing the plugin links |
||
33 | * |
||
34 | * @var array |
||
35 | * @since 2.0.0 |
||
36 | */ |
||
37 | protected $links; |
||
38 | |||
39 | /** |
||
40 | * Return the single class instance |
||
41 | * |
||
42 | * @return object |
||
43 | * @since 2.0.0 |
||
44 | */ |
||
45 | public static function instance() { |
||
51 | |||
52 | /** |
||
53 | * Class constructor |
||
54 | * |
||
55 | * @uses add_action() |
||
56 | * @uses add_filter() |
||
57 | * @uses get_option() |
||
58 | * @uses get_bloginfo() |
||
59 | * |
||
60 | * @global object $wp_embed |
||
61 | * @since 2.0.0 |
||
62 | */ |
||
63 | protected function __construct() { |
||
64 | // Register action and filter hooks. |
||
65 | add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); |
||
66 | add_action( 'admin_init', array( $this, 'admin_init' ), 20 ); |
||
67 | add_action( 'init', array( $this, 'register_dummy_post_type' ) ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Prevent the class from being cloned |
||
72 | * |
||
73 | * @return void |
||
74 | * @since 2.0.0 |
||
75 | */ |
||
76 | protected function __clone() { |
||
77 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ uh?' ), '2.0' ); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Load language files |
||
82 | * |
||
83 | * @uses load_plugin_textdomain() |
||
84 | * |
||
85 | * @return void |
||
86 | * @since 2.0.0 |
||
87 | */ |
||
88 | public function load_textdomain() { |
||
89 | load_plugin_textdomain( 'black-studio-tinymce-widget', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' ); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Checks if the plugin admin code should be loaded |
||
94 | * |
||
95 | * @uses apply_filters() |
||
96 | * |
||
97 | * @global string $pagenow |
||
98 | * @return boolean |
||
99 | * @since 2.0.0 |
||
100 | */ |
||
101 | public function enabled() { |
||
102 | global $pagenow; |
||
103 | $enabled_pages = apply_filters( 'black_studio_tinymce_enable_pages', array( 'widgets.php', 'customize.php', 'admin-ajax.php' ) ); |
||
104 | return apply_filters( 'black_studio_tinymce_enable', in_array( $pagenow, $enabled_pages, true ) ); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Add actions and filters (only in widgets admin page) |
||
109 | * |
||
110 | * @uses add_action() |
||
111 | * @uses add_filter() |
||
112 | * @uses do_action() |
||
113 | * |
||
114 | * @return void |
||
115 | * @since 2.0.0 |
||
116 | */ |
||
117 | public function admin_init() { |
||
118 | $this->init_links(); |
||
119 | add_action( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); |
||
120 | if ( $this->enabled() ) { |
||
121 | add_action( 'admin_head', array( $this, 'enqueue_media' ) ); |
||
122 | add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ) ); |
||
123 | add_action( 'admin_print_styles', array( $this, 'admin_print_styles' ) ); |
||
124 | add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ) ); |
||
125 | add_action( 'black_studio_tinymce_before_editor', array( $this, 'display_links' ) ); |
||
126 | add_action( 'black_studio_tinymce_editor', array( $this, 'editor' ), 10, 4 ); |
||
127 | add_action( 'black_studio_tinymce_after_editor', array( $this, 'fix_the_editor_content_filter' ) ); |
||
128 | add_action( 'wp_tiny_mce_init', array( $this, 'wp_tiny_mce_init' ) ); |
||
129 | add_filter( 'wp_editor_settings', array( $this, 'editor_settings' ), 5, 2 ); |
||
130 | add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_fix_rtl' ), 10 ); |
||
131 | add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_fullscreen' ), 10, 2 ); |
||
132 | add_filter( 'quicktags_settings', array( $this, 'quicktags_fullscreen' ), 10, 2 ); |
||
133 | if ( ! user_can_richedit() ) { |
||
134 | add_action( 'admin_notices', array( $this, 'visual_editor_disabled_notice' ) ); |
||
135 | } |
||
136 | add_action( 'wp_ajax_bstw_visual_editor_disabled_dismiss_notice', array( $this, 'visual_editor_disabled_dismiss_notice' ) ); |
||
137 | do_action( 'black_studio_tinymce_load' ); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Instantiate tinyMCE editor |
||
143 | * |
||
144 | * @uses add_thickbox() |
||
145 | * @uses wp_enqueue_media() |
||
146 | * |
||
147 | * @return void |
||
148 | * @since 2.0.0 |
||
149 | */ |
||
150 | public function enqueue_media() { |
||
151 | // Add support for thickbox media dialog. |
||
152 | add_thickbox(); |
||
153 | // New media modal dialog (WP 3.5+). |
||
154 | if ( function_exists( 'wp_enqueue_media' ) ) { |
||
155 | wp_enqueue_media(); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Enqueue styles |
||
161 | * |
||
162 | * @uses wp_enqueue_style() |
||
163 | * |
||
164 | * @return void |
||
165 | * @since 2.0.0 |
||
166 | */ |
||
167 | public function admin_print_styles() { |
||
172 | |||
173 | /** |
||
174 | * Helper function to enqueue style |
||
175 | * |
||
176 | * @uses apply_filters() |
||
177 | * @uses wp_enqueue_style() |
||
178 | * @uses plugins_url() |
||
179 | * @uses SCRIPT_DEBUG |
||
180 | * |
||
181 | * @return void |
||
182 | * @since 2.0.0 |
||
183 | */ |
||
184 | public function enqueue_style() { |
||
195 | |||
196 | /** |
||
197 | * Enqueue header scripts |
||
198 | * |
||
199 | * @uses wp_enqueue_script() |
||
200 | * @uses do_action() |
||
201 | * |
||
202 | * @return void |
||
203 | * @since 2.0.0 |
||
204 | */ |
||
205 | public function admin_print_scripts() { |
||
213 | |||
214 | /** |
||
215 | * Helper function to enqueue script |
||
216 | * |
||
217 | * @uses apply_filters() |
||
218 | * @uses wp_enqueue_script() |
||
219 | * @uses plugins_url() |
||
220 | * @uses SCRIPT_DEBUG |
||
221 | * |
||
222 | * @return void |
||
223 | * @since 2.0.0 |
||
224 | */ |
||
225 | public function enqueue_script() { |
||
237 | |||
238 | /** |
||
239 | * Helper function to enqueue localized script |
||
240 | * |
||
241 | * @uses apply_filters() |
||
242 | * @uses wp_localize_script() |
||
243 | * |
||
244 | * @return void |
||
245 | * @since 2.0.0 |
||
246 | */ |
||
247 | public function localize_script() { |
||
261 | |||
262 | /** |
||
263 | * Enqueue footer scripts |
||
264 | * |
||
265 | * @return void |
||
266 | * @since 2.0.0 |
||
267 | */ |
||
268 | public function admin_print_footer_scripts() { |
||
271 | |||
272 | /** |
||
273 | * Output the visual editor |
||
274 | * |
||
275 | * @uses wp_editor() |
||
276 | * |
||
277 | * @param string $text Text inside the editor. |
||
278 | * @param string $editor_id Editor instance ID. |
||
279 | * @param string $name Editor instance name. |
||
280 | * @param string $type Editor instance type. |
||
281 | * @return void |
||
282 | * @since 2.0.0 |
||
283 | */ |
||
284 | public function editor( $text, $editor_id, $name = '', $type = 'visual' ) { |
||
290 | |||
291 | /** |
||
292 | * Remove editor content filters for multiple editor instances |
||
293 | * Workaround for WordPress Core bug #28403 https://core.trac.wordpress.org/ticket/28403 |
||
294 | * |
||
295 | * @uses remove_filter |
||
296 | * |
||
297 | * @return void |
||
298 | * @since 2.1.7 |
||
299 | */ |
||
300 | public function fix_the_editor_content_filter() { |
||
304 | |||
305 | /** |
||
306 | * Setup editor instance for event handling |
||
307 | * |
||
308 | * @uses SCRIPT_DEBUG |
||
309 | * |
||
310 | * @return void |
||
311 | * @since 2.2.1 |
||
312 | */ |
||
313 | public function wp_tiny_mce_init() { |
||
318 | |||
319 | /** |
||
320 | * Set editor settings |
||
321 | * |
||
322 | * @param mixed[] $settings Array of settings. |
||
323 | * @param string $editor_id Editor instance ID. |
||
324 | * @return mixed[] |
||
325 | * @since 2.0.0 |
||
326 | */ |
||
327 | public function editor_settings( $settings, $editor_id ) { |
||
340 | |||
341 | /** |
||
342 | * Initialize plugin links |
||
343 | * |
||
344 | * @return void |
||
345 | * @since 2.0.0 |
||
346 | */ |
||
347 | public function init_links() { |
||
363 | |||
364 | /** |
||
365 | * Display plugin links |
||
366 | * |
||
367 | * @return void |
||
368 | * @since 2.0.0 |
||
369 | */ |
||
370 | public function display_links() { |
||
383 | |||
384 | /** |
||
385 | * Show row meta on the plugin screen |
||
386 | * |
||
387 | * @uses esc_html() |
||
388 | * @uses esc_url() |
||
389 | * |
||
390 | * @param string[] $links Array of links. |
||
391 | * @param string $file Plugin's filename. |
||
392 | * @return string[] |
||
393 | * @since 2.0.0 |
||
394 | */ |
||
395 | public function plugin_row_meta( $links, $file ) { |
||
403 | |||
404 | /** |
||
405 | * Fix for rtl languages |
||
406 | * |
||
407 | * @param mixed[] $settings Array of settings. |
||
408 | * @return mixed[] |
||
409 | * @since 2.1.0 |
||
410 | */ |
||
411 | public function tinymce_fix_rtl( $settings ) { |
||
418 | |||
419 | /** |
||
420 | * Apply TinyMCE default fullscreen |
||
421 | * |
||
422 | * @param mixed[] $settings Array of settings. |
||
423 | * @param string|null $editor_id Editor ID. |
||
424 | * @return mixed[] |
||
425 | * @since 2.1.2 |
||
426 | */ |
||
427 | public function tinymce_fullscreen( $settings, $editor_id = null ) { |
||
438 | |||
439 | /** |
||
440 | * Disable Quicktags default fullscreen |
||
441 | * |
||
442 | * @param mixed[] $settings Array of settings. |
||
443 | * @param string $editor_id Editor ID. |
||
444 | * @return mixed[] |
||
445 | * @since 2.1.2 |
||
446 | */ |
||
447 | public function quicktags_fullscreen( $settings, $editor_id ) { |
||
453 | |||
454 | /** |
||
455 | * Show admin notice when visual editor is disabled in current user's profile settings |
||
456 | * |
||
457 | * @uses get_user_meta() |
||
458 | * @uses get_current_user_id() |
||
459 | * |
||
460 | * @return void |
||
461 | * @since 2.4.0 |
||
462 | */ |
||
463 | public function visual_editor_disabled_notice() { |
||
476 | |||
477 | /** |
||
478 | * Store dismission of the "Visual Editor disabled" notice for the current user |
||
479 | * |
||
480 | * @uses add_user_meta() |
||
481 | * @uses get_current_user_id() |
||
482 | * |
||
483 | * @return void |
||
484 | * @since 2.4.0 |
||
485 | */ |
||
486 | public function visual_editor_disabled_dismiss_notice() { |
||
491 | |||
492 | /** |
||
493 | * Register a private custom post type to be used for link embed previews |
||
494 | * |
||
495 | * @uses register_post_type() |
||
496 | * |
||
497 | * @return void |
||
498 | * @since 3.0.0 |
||
499 | */ |
||
500 | public function register_dummy_post_type() { |
||
515 | |||
516 | /** |
||
517 | * Get dummy post ID for link embed previews |
||
518 | * |
||
519 | * @uses WP_Query() |
||
520 | * @uses wp_insert_post() |
||
521 | * @uses update_option() |
||
522 | * @uses get_option() |
||
523 | * |
||
524 | * @return int |
||
525 | * @since 3.0.0 |
||
526 | */ |
||
527 | public function get_dummy_post_id() { |
||
536 | |||
537 | } // END class Black_Studio_TinyMCE_Admin |
||
538 | |||
540 |