Complex classes like FooGallery_Admin_Gallery_MetaBoxes 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FooGallery_Admin_Gallery_MetaBoxes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class FooGallery_Admin_Gallery_MetaBoxes { |
||
| 10 | |||
| 11 | private $_gallery; |
||
| 12 | |||
| 13 | public function __construct() { |
||
| 14 | //add our foogallery metaboxes |
||
| 15 | add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes_to_gallery' ) ); |
||
| 16 | |||
| 17 | //save extra post data for a gallery |
||
| 18 | add_action( 'save_post', array( $this, 'save_gallery' ) ); |
||
| 19 | |||
| 20 | //save custom field on a page or post |
||
| 21 | add_Action( 'save_post', array( $this, 'attach_gallery_to_post' ), 10, 2 ); |
||
| 22 | |||
| 23 | //whitelist metaboxes for our gallery postype |
||
| 24 | add_filter( 'foogallery_metabox_sanity', array( $this, 'whitelist_metaboxes' ) ); |
||
| 25 | |||
| 26 | //add scripts used by metaboxes |
||
| 27 | add_action( 'admin_enqueue_scripts', array( $this, 'include_required_scripts' ) ); |
||
| 28 | |||
| 29 | // Ajax calls for creating a page for the gallery |
||
| 30 | add_action( 'wp_ajax_foogallery_create_gallery_page', array( $this, 'ajax_create_gallery_page' ) ); |
||
| 31 | |||
| 32 | // Ajax call for clearing thumb cache for the gallery |
||
| 33 | add_action( 'wp_ajax_foogallery_clear_gallery_thumb_cache', array( $this, 'ajax_clear_gallery_thumb_cache' ) ); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function whitelist_metaboxes() { |
||
| 37 | return array( |
||
| 38 | FOOGALLERY_CPT_GALLERY => array( |
||
| 39 | 'whitelist' => apply_filters( 'foogallery_metabox_sanity_foogallery', |
||
| 40 | array( |
||
| 41 | 'submitdiv', |
||
| 42 | 'slugdiv', |
||
| 43 | 'postimagediv', |
||
| 44 | 'foogallery_items', |
||
| 45 | 'foogallery_settings', |
||
| 46 | 'foogallery_help', |
||
| 47 | 'foogallery_pages', |
||
| 48 | 'foogallery_customcss', |
||
| 49 | 'foogallery_sorting', |
||
| 50 | 'foogallery_thumb_cache' |
||
| 51 | ) ), |
||
| 52 | 'contexts' => array( 'normal', 'advanced', 'side', ), |
||
| 53 | 'priorities' => array( 'high', 'core', 'default', 'low', ), |
||
| 54 | ) |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function add_meta_boxes_to_gallery() { |
||
| 59 | global $post; |
||
| 60 | |||
| 61 | add_meta_box( |
||
| 62 | 'foogallery_items', |
||
| 63 | __( 'Gallery Items', 'foogallery' ), |
||
| 64 | array( $this, 'render_gallery_media_metabox' ), |
||
| 65 | FOOGALLERY_CPT_GALLERY, |
||
| 66 | 'normal', |
||
| 67 | 'high' |
||
| 68 | ); |
||
| 69 | |||
| 70 | add_meta_box( |
||
| 71 | 'foogallery_settings', |
||
| 72 | __( 'Gallery Settings', 'foogallery' ), |
||
| 73 | array( $this, 'render_gallery_settings_metabox' ), |
||
| 74 | FOOGALLERY_CPT_GALLERY, |
||
| 75 | 'normal', |
||
| 76 | 'high' |
||
| 77 | ); |
||
| 78 | |||
| 79 | add_meta_box( |
||
| 80 | 'foogallery_help', |
||
| 81 | __( 'Gallery Shortcode', 'foogallery' ), |
||
| 82 | array( $this, 'render_gallery_shortcode_metabox' ), |
||
| 83 | FOOGALLERY_CPT_GALLERY, |
||
| 84 | 'side', |
||
| 85 | 'default' |
||
| 86 | ); |
||
| 87 | |||
| 88 | if ( 'publish' == $post->post_status ) { |
||
| 89 | add_meta_box( 'foogallery_pages', |
||
| 90 | __( 'Gallery Usage', 'foogallery' ), |
||
| 91 | array( $this, 'render_gallery_usage_metabox' ), |
||
| 92 | FOOGALLERY_CPT_GALLERY, |
||
| 93 | 'side', |
||
| 94 | 'default' |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | add_meta_box( |
||
| 99 | 'foogallery_customcss', |
||
| 100 | __( 'Custom CSS', 'foogallery' ), |
||
| 101 | array( $this, 'render_customcss_metabox' ), |
||
| 102 | FOOGALLERY_CPT_GALLERY, |
||
| 103 | 'normal', |
||
| 104 | 'low' |
||
| 105 | ); |
||
| 106 | |||
| 107 | add_meta_box( |
||
| 108 | 'foogallery_sorting', |
||
| 109 | __( 'Gallery Sorting', 'foogallery' ), |
||
| 110 | array( $this, 'render_sorting_metabox' ), |
||
| 111 | FOOGALLERY_CPT_GALLERY, |
||
| 112 | 'side', |
||
| 113 | 'default' |
||
| 114 | ); |
||
| 115 | |||
| 116 | add_meta_box( |
||
| 117 | 'foogallery_thumb_cache', |
||
| 118 | __( 'Thumbnail Cache', 'foogallery' ), |
||
| 119 | array( $this, 'render_thumb_cache_metabox' ), |
||
| 120 | FOOGALLERY_CPT_GALLERY, |
||
| 121 | 'side', |
||
| 122 | 'default' |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function get_gallery( $post ) { |
||
| 136 | |||
| 137 | public function save_gallery( $post_id ) { |
||
| 175 | |||
| 176 | public function attach_gallery_to_post( $post_id, $post ) { |
||
| 206 | |||
| 207 | public function render_gallery_media_metabox( $post ) { |
||
| 244 | |||
| 245 | public function render_gallery_item( $attachment_post = false ) { |
||
| 274 | |||
| 275 | public function render_gallery_settings_metabox( $post ) { |
||
| 276 | //gallery settings including: |
||
| 277 | //gallery images link to image or attachment page |
||
| 278 | //default template to use |
||
| 279 | $gallery = $this->get_gallery( $post ); |
||
| 280 | $available_templates = foogallery_gallery_templates(); |
||
| 281 | |||
| 282 | //check if we have no templates |
||
| 283 | if ( 0 === count( $available_templates ) ) { |
||
| 284 | //force the default template to activate if there are no other gallery templates |
||
| 285 | foogallery_activate_default_templates_extension(); |
||
| 286 | $available_templates = foogallery_gallery_templates(); |
||
| 287 | } |
||
| 288 | |||
| 289 | $gallery_template = foogallery_default_gallery_template(); |
||
| 290 | if ( ! empty($gallery->gallery_template) ) { |
||
| 291 | $gallery_template = $gallery->gallery_template; |
||
| 292 | } |
||
| 293 | $hide_help = 'on' == foogallery_get_setting( 'hide_gallery_template_help' ); |
||
| 294 | ?> |
||
| 295 | <table class="foogallery-metabox-settings"> |
||
| 296 | <tbody> |
||
| 297 | <tr class="gallery_template_field gallery_template_field_selector"> |
||
| 298 | <th> |
||
| 299 | <label for="FooGallerySettings_GalleryTemplate"><?php _e( 'Gallery Template', 'foogallery' ); ?></label> |
||
| 300 | </th> |
||
| 301 | <td> |
||
| 302 | <select id="FooGallerySettings_GalleryTemplate" name="<?php echo FOOGALLERY_META_TEMPLATE; ?>"> |
||
| 303 | <?php |
||
| 304 | foreach ( $available_templates as $template ) { |
||
| 305 | $selected = ($gallery_template === $template['slug']) ? 'selected' : ''; |
||
| 306 | |||
| 307 | $preview_css = ''; |
||
| 308 | if ( isset( $template['preview_css'] ) ) { |
||
| 309 | if ( is_array( $template['preview_css'] ) ) { |
||
| 310 | //dealing with an array of css files to include |
||
| 311 | $preview_css = implode( ',', $template['preview_css'] ); |
||
| 312 | } else { |
||
| 313 | $preview_css = $template['preview_css']; |
||
| 314 | } |
||
| 315 | } |
||
| 316 | $preview_css = empty( $preview_css ) ? '' : ' data-preview-css="' . $preview_css . '" '; |
||
| 317 | |||
| 318 | echo "<option {$selected}{$preview_css} value=\"{$template['slug']}\">{$template['name']}</option>"; |
||
| 319 | } |
||
| 320 | ?> |
||
| 321 | </select> |
||
| 322 | <br /> |
||
| 323 | <small><?php _e( 'The gallery template that will be used when the gallery is output to the frontend.', 'foogallery' ); ?></small> |
||
| 324 | </td> |
||
| 325 | </tr> |
||
| 326 | <?php |
||
| 327 | foreach ( $available_templates as $template ) { |
||
| 328 | $field_visibility = ($gallery_template !== $template['slug']) ? 'style="display:none"' : ''; |
||
| 329 | |||
| 330 | //allow for extensions to override fields for every gallery template. |
||
| 331 | // Also passes the $template along so you can inspect and conditionally alter fields based on the template properties |
||
| 332 | $fields = apply_filters( 'foogallery_override_gallery_template_fields', $template['fields'], $template ); |
||
| 333 | |||
| 334 | $section = ''; |
||
| 335 | foreach ( $fields as $field ) { |
||
| 336 | |||
| 337 | //allow for the field to be altered by extensions. Also used by the build-in fields, e.g. lightbox |
||
| 338 | $field = apply_filters( 'foogallery_alter_gallery_template_field', $field, $gallery ); |
||
| 339 | |||
| 340 | $class = "gallery_template_field gallery_template_field-{$template['slug']} gallery_template_field-{$template['slug']}-{$field['id']}"; |
||
| 341 | |||
| 342 | if ( isset($field['section']) && $field['section'] !== $section ) { |
||
| 343 | $section = $field['section']; |
||
| 344 | ?> |
||
| 345 | <tr class="<?php echo $class; ?>" <?php echo $field_visibility; ?>> |
||
| 346 | <td colspan="2"><h4><?php echo $section; ?></h4></td> |
||
| 347 | </tr> |
||
| 348 | <?php } |
||
| 349 | if (isset($field['type']) && 'help' == $field['type'] && $hide_help) { |
||
| 350 | continue; //skip help if the 'hide help' setting is turned on |
||
| 351 | } |
||
| 352 | ?> |
||
| 353 | <tr class="<?php echo $class; ?>" <?php echo $field_visibility; ?>> |
||
| 354 | <?php if ( isset($field['type']) && 'help' == $field['type'] ) { ?> |
||
| 355 | <td colspan="2"> |
||
| 356 | <div class="foogallery-help"> |
||
| 357 | <?php echo $field['desc']; ?> |
||
| 358 | </div> |
||
| 359 | </td> |
||
| 360 | <?php } else { ?> |
||
| 361 | <th> |
||
| 362 | <label |
||
| 363 | for="FooGallerySettings_<?php echo $template['slug'] . '_' . $field['id']; ?>"><?php echo $field['title']; ?></label> |
||
| 364 | </th> |
||
| 365 | <td> |
||
| 366 | <?php do_action( 'foogallery_render_gallery_template_field', $field, $gallery, $template ); ?> |
||
| 367 | </td> |
||
| 368 | <?php } ?> |
||
| 369 | </tr> |
||
| 370 | <?php |
||
| 371 | } |
||
| 372 | } |
||
| 373 | ?> |
||
| 374 | </tbody> |
||
| 375 | </table> |
||
| 376 | <?php |
||
| 377 | } |
||
| 378 | |||
| 379 | public function render_gallery_shortcode_metabox( $post ) { |
||
| 380 | $gallery = $this->get_gallery( $post ); |
||
| 381 | $shortcode = $gallery->shortcode(); |
||
| 382 | ?> |
||
| 383 | <p class="foogallery-shortcode"> |
||
| 384 | <input type="text" id="foogallery-copy-shortcode" size="<?php echo strlen( $shortcode ); ?>" value="<?php echo htmlspecialchars( $shortcode ); ?>" readonly="readonly" /> |
||
| 385 | </p> |
||
| 386 | <p> |
||
| 387 | <?php _e( 'Paste the above shortcode into a post or page to show the gallery.', 'foogallery' ); ?> |
||
| 388 | </p> |
||
| 389 | <script> |
||
| 390 | jQuery(function($) { |
||
| 391 | var shortcodeInput = document.querySelector('#foogallery-copy-shortcode'); |
||
| 392 | shortcodeInput.addEventListener('click', function () { |
||
| 393 | try { |
||
| 394 | // select the contents |
||
| 395 | shortcodeInput.select(); |
||
| 396 | //copy the selection |
||
| 397 | document.execCommand('copy'); |
||
| 398 | //show the copied message |
||
| 399 | $('.foogallery-shortcode-message').remove(); |
||
| 400 | $(shortcodeInput).after('<p class="foogallery-shortcode-message"><?php _e( 'Shortcode copied to clipboard :)','foogallery' ); ?></p>'); |
||
| 401 | } catch(err) { |
||
| 402 | console.log('Oops, unable to copy!'); |
||
| 403 | } |
||
| 404 | }, false); |
||
| 405 | }); |
||
| 406 | </script> |
||
| 407 | <?php |
||
| 408 | } |
||
| 409 | |||
| 410 | public function render_gallery_usage_metabox( $post ) { |
||
| 439 | |||
| 440 | public function render_sorting_metabox( $post ) { |
||
| 458 | |||
| 459 | public function render_thumb_cache_metabox( $post ) { |
||
| 460 | ?> |
||
| 461 | <p> |
||
| 462 | <?php _e( 'Clear all the previously cached thumbnails that have been generated for this gallery.', 'foogallery' ); ?> |
||
| 463 | </p> |
||
| 464 | <div class="foogallery_metabox_actions"> |
||
| 465 | <button class="button button-primary button-large" id="foogallery_clear_thumb_cache"><?php _e( 'Clear Thumbnail Cache', 'foogallery' ); ?></button> |
||
| 466 | <span id="foogallery_clear_thumb_cache_spinner" class="spinner"></span> |
||
| 467 | <?php wp_nonce_field( 'foogallery_clear_gallery_thumb_cache', 'foogallery_clear_gallery_thumb_cache_nonce', false ); ?> |
||
| 468 | </div> |
||
| 469 | <?php |
||
| 470 | } |
||
| 471 | |||
| 472 | public function include_required_scripts() { |
||
| 473 | $screen_id = foo_current_screen_id(); |
||
| 474 | |||
| 475 | //only include scripts if we on the foogallery add/edit page |
||
| 476 | if ( FOOGALLERY_CPT_GALLERY === $screen_id || |
||
| 477 | 'edit-' . FOOGALLERY_CPT_GALLERY === $screen_id ) { |
||
| 478 | |||
| 479 | //spectrum needed for the colorpicker field |
||
| 480 | $url = FOOGALLERY_URL . 'lib/spectrum/spectrum.js'; |
||
| 481 | wp_enqueue_script( 'foogallery-spectrum', $url, array('jquery'), FOOGALLERY_VERSION ); |
||
| 482 | $url = FOOGALLERY_URL . 'lib/spectrum/spectrum.css'; |
||
| 483 | wp_enqueue_style( 'foogallery-spectrum', $url, array(), FOOGALLERY_VERSION ); |
||
| 484 | |||
| 485 | //include any admin js required for the templates |
||
| 486 | foreach ( foogallery_gallery_templates() as $template ) { |
||
| 487 | $admin_js = foo_safe_get( $template, 'admin_js' ); |
||
| 488 | if ( is_array( $admin_js ) ) { |
||
| 489 | //dealing with an array of js files to include |
||
| 490 | foreach( $admin_js as $admin_js_key => $admin_js_src ) { |
||
| 491 | wp_enqueue_script( 'foogallery-gallery-admin-' . $template['slug'] . '-' . $admin_js_key, $admin_js_src, array('jquery', 'media-upload', 'jquery-ui-sortable'), FOOGALLERY_VERSION ); |
||
| 492 | } |
||
| 493 | } else { |
||
| 494 | //dealing with a single js file to include |
||
| 495 | wp_enqueue_script( 'foogallery-gallery-admin-' . $template['slug'], $admin_js, array('jquery', 'media-upload', 'jquery-ui-sortable'), FOOGALLERY_VERSION ); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | public function render_customcss_metabox( $post ) { |
||
| 520 | |||
| 521 | public function ajax_create_gallery_page() { |
||
| 539 | |||
| 540 | public function ajax_clear_gallery_thumb_cache() { |
||
| 541 | if ( check_admin_referer( 'foogallery_clear_gallery_thumb_cache', 'foogallery_clear_gallery_thumb_cache_nonce' ) ) { |
||
| 542 | |||
| 543 | $foogallery_id = $_POST['foogallery_id']; |
||
| 544 | |||
| 545 | $foogallery = FooGallery::get_by_id( $foogallery_id ); |
||
| 565 | } |
||
| 566 | } |
||
| 567 |