Complex classes like FooGallery_NextGen_Helper 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_NextGen_Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class FooGallery_NextGen_Helper { |
||
| 6 | |||
| 7 | const NEXTGEN_TABLE_GALLERY = 'ngg_gallery'; |
||
| 8 | const NEXTGEN_TABLE_PICTURES = 'ngg_pictures'; |
||
| 9 | const NEXTGEN_TABLE_ALBUMS = 'ngg_album'; |
||
| 10 | |||
| 11 | const NEXTGEN_OPTION_IMPORT_CURRENT = 'foogallery_nextgen_import-current'; |
||
| 12 | const NEXTGEN_OPTION_IMPORT_PROGRESS = 'foogallery_nextgen_import-progress'; |
||
| 13 | const NEXTGEN_OPTION_IMPORT_IN_PROGRESS = 'foogallery_nextgen_import-importing'; |
||
| 14 | |||
| 15 | const NEXTGEN_OPTION_IMPORT_CURRENT_ALBUM = 'foogallery_nextgen_import-current-album'; |
||
| 16 | const NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM = 'foogallery_nextgen_import-progress-album'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @TODO |
||
| 20 | */ |
||
| 21 | function is_nextgen_installed() { |
||
| 24 | |||
| 25 | function get_galleries() { |
||
| 26 | global $wpdb; |
||
| 27 | $gallery_table = $wpdb->prefix . self::NEXTGEN_TABLE_GALLERY; |
||
| 28 | $picture_table = $wpdb->prefix . self::NEXTGEN_TABLE_PICTURES; |
||
| 29 | |||
| 30 | return $wpdb->get_results( "select gid, name, title, galdesc, |
||
| 31 | (select count(*) from {$picture_table} where galleryid = gid) 'image_count' |
||
| 32 | from {$gallery_table}" ); |
||
| 33 | } |
||
| 34 | |||
| 35 | function get_albums() { |
||
| 36 | global $wpdb; |
||
| 37 | $album_table = $wpdb->prefix . self::NEXTGEN_TABLE_ALBUMS; |
||
| 38 | return $wpdb->get_results(" select * from {$album_table}"); |
||
| 39 | } |
||
| 40 | |||
| 41 | function get_album( $id ) { |
||
| 42 | global $wpdb; |
||
| 43 | $album_table = $wpdb->prefix . self::NEXTGEN_TABLE_ALBUMS; |
||
| 44 | |||
| 45 | return $wpdb->get_row( $wpdb->prepare( "select * from {$album_table} where id = %d", $id ) ); |
||
| 46 | } |
||
| 47 | |||
| 48 | function get_gallery( $id ) { |
||
| 49 | global $wpdb; |
||
| 50 | $gallery_table = $wpdb->prefix . self::NEXTGEN_TABLE_GALLERY; |
||
| 51 | $picture_table = $wpdb->prefix . self::NEXTGEN_TABLE_PICTURES; |
||
| 52 | |||
| 53 | return $wpdb->get_row( $wpdb->prepare( "select gid, name, title, galdesc, path, author, |
||
| 54 | (select count(*) from {$picture_table} where galleryid = gid) 'image_count' |
||
| 55 | from {$gallery_table} |
||
| 56 | where gid = %d", $id ) ); |
||
| 57 | } |
||
| 58 | |||
| 59 | function get_gallery_images( $id ) { |
||
| 60 | global $wpdb; |
||
| 61 | $picture_table = $wpdb->prefix . self::NEXTGEN_TABLE_PICTURES; |
||
| 62 | |||
| 63 | return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$picture_table} WHERE galleryid = %d", $id ) ); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * |
||
| 68 | * @return FooGallery_NextGen_Import_Progress |
||
| 69 | */ |
||
| 70 | function get_import_progress( $nextgen_gallery_id ) { |
||
| 80 | |||
| 81 | function set_import_progress( $nextgen_gallery_id, FooGallery_NextGen_Import_Progress $progress ) { |
||
| 82 | $all_progress = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, array() ); |
||
| 83 | $all_progress[ $nextgen_gallery_id ] = $progress; |
||
| 84 | update_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, $all_progress ); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param int $nextgen_gallery_id |
||
| 89 | * @param string $foogallery_title |
||
| 90 | */ |
||
| 91 | function init_import_progress( $nextgen_gallery_id, $foogallery_title ) { |
||
| 92 | $progress = new FooGallery_NextGen_Import_Progress(); |
||
| 93 | $progress->init( $nextgen_gallery_id, $foogallery_title ); |
||
| 94 | $this->set_import_progress( $nextgen_gallery_id, $progress ); |
||
| 95 | } |
||
| 96 | |||
| 97 | function start_import() { |
||
| 101 | |||
| 102 | function cancel_import() { |
||
| 106 | |||
| 107 | function reset_import() { |
||
| 108 | delete_option( self::NEXTGEN_OPTION_IMPORT_CURRENT ); |
||
| 109 | delete_option( self::NEXTGEN_OPTION_IMPORT_IN_PROGRESS ); |
||
| 110 | delete_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS ); |
||
| 111 | } |
||
| 112 | |||
| 113 | function import_in_progress() { |
||
| 116 | |||
| 117 | function continue_import() { |
||
| 118 | //get the current gallery being imported |
||
| 119 | $current_nextgen_id = get_option( self::NEXTGEN_OPTION_IMPORT_CURRENT, 0 ); |
||
| 120 | |||
| 121 | if ( 0 === $current_nextgen_id ) { |
||
| 122 | //try and get the next gallery to import |
||
| 123 | $current_nextgen_id = $this->get_next_gallery_to_import(); |
||
| 124 | |||
| 125 | //if we still have no current then do nothing |
||
| 126 | if ( 0 === $current_nextgen_id ) { |
||
| 127 | $this->cancel_import(); |
||
| 128 | return; |
||
| 129 | } else { |
||
| 130 | update_option( self::NEXTGEN_OPTION_IMPORT_CURRENT, $current_nextgen_id ); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | $progress = $this->get_import_progress( $current_nextgen_id ); |
||
| 135 | |||
| 136 | if ( ! $progress->has_started() ) { |
||
| 137 | $progress->start(); |
||
| 138 | } |
||
| 139 | |||
| 140 | //import the next picture |
||
| 141 | $progress->import_next_picture(); |
||
| 142 | |||
| 143 | //update our progress |
||
| 144 | $this->set_import_progress( $current_nextgen_id, $progress ); |
||
| 145 | |||
| 146 | //if the percentage complete is 100 then clear the current gallery |
||
| 147 | if ( $progress->is_completed() ) { |
||
| 148 | delete_option( self::NEXTGEN_OPTION_IMPORT_CURRENT ); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | function get_overall_progress() { |
||
| 167 | |||
| 168 | function get_next_gallery_to_import() { |
||
| 169 | $all_progress = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, array() ); |
||
| 170 | |||
| 171 | foreach ( $all_progress as $id => $progress ) { |
||
| 172 | if ( $progress->can_import() ) { |
||
| 173 | return $id; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | return 0; |
||
| 178 | } |
||
| 179 | |||
| 180 | function ignore_previously_imported_galleries() { |
||
| 181 | $all_progress = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, array() ); |
||
| 182 | foreach ( $all_progress as $id => $progress ) { |
||
| 183 | if ( $progress->is_completed() ) { |
||
| 184 | $progress->is_part_of_current_import = false; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | update_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, $all_progress ); |
||
| 188 | } |
||
| 189 | |||
| 190 | function import_picture( $nextgen_gallery_path, $picture ) { |
||
| 191 | $picture_url = trailingslashit( site_url() ) . trailingslashit( $nextgen_gallery_path ) . $picture->filename; |
||
| 192 | |||
| 193 | // Get the contents of the picture |
||
| 194 | $response = wp_remote_get( $picture_url ); |
||
| 195 | $contents = wp_remote_retrieve_body( $response ); |
||
| 196 | |||
| 197 | // Upload and get file data |
||
| 198 | $upload = wp_upload_bits( basename( $picture_url ), null, $contents ); |
||
| 199 | $guid = $upload['url']; |
||
| 200 | $file = $upload['file']; |
||
| 201 | $file_type = wp_check_filetype( basename( $file ), null ); |
||
| 202 | |||
| 203 | // Create attachment |
||
| 204 | $attachment = array( |
||
| 205 | 'ID' => 0, |
||
| 206 | 'guid' => $guid, |
||
| 207 | 'post_title' => $picture->alttext != '' ? $picture->alttext : $picture->image_slug, |
||
| 208 | 'post_excerpt' => $picture->description, |
||
| 209 | 'post_content' => $picture->description, |
||
| 210 | 'post_date' => '', |
||
| 211 | 'post_mime_type' => $file_type['type'], |
||
| 212 | ); |
||
| 213 | |||
| 214 | // Include image.php so we can call wp_generate_attachment_metadata() |
||
| 215 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
||
| 216 | |||
| 217 | // Insert the attachment |
||
| 218 | $attachment_id = wp_insert_attachment( $attachment, $file, 0 ); |
||
| 219 | $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file ); |
||
| 220 | wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
||
| 221 | |||
| 222 | // Save alt text in the post meta |
||
| 223 | update_post_meta( $attachment_id, '_wp_attachment_image_alt', $picture->alttext ); |
||
| 224 | |||
| 225 | return $attachment_id; |
||
| 226 | } |
||
| 227 | |||
| 228 | function render_import_form( $galleries = false ) { |
||
| 344 | |||
| 345 | function render_album_import_form( $albums = false ) { |
||
| 346 | if ( false === $albums ) { |
||
| 347 | $albums = $this->get_albums(); |
||
| 348 | } |
||
| 349 | $has_imports = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM ); |
||
| 350 | ?> |
||
| 351 | <table class="wp-list-table widefat" cellspacing="0"> |
||
| 352 | <thead> |
||
| 353 | <tr> |
||
| 354 | <th scope="col" class="manage-column"> |
||
| 355 | <span><?php _e( 'NextGen Album', 'foogallery' ); ?></span> |
||
| 356 | </th> |
||
| 357 | <th scope="col" class="manage-column"> |
||
| 358 | <span><?php _e( 'Album Name', 'foogallery' ); ?></span> |
||
| 359 | </th> |
||
| 360 | <th scope="col" class="manage-column"> |
||
| 361 | <span><?php _e( 'NextGen Galleries', 'foogallery' ); ?></span> |
||
| 362 | </th> |
||
| 363 | <th scope="col" class="manage-column"> |
||
| 364 | <span><?php _e( 'Import Options', 'foogallery' ); ?></span> |
||
| 365 | </th> |
||
| 366 | </tr> |
||
| 367 | </thead> |
||
| 368 | <tbody> |
||
| 369 | <?php |
||
| 370 | $counter = 0; |
||
| 371 | foreach ( $albums as $album ) { |
||
| 372 | $counter++; |
||
| 373 | $progress = $this->get_import_progress_for_album( $album->id ); |
||
| 374 | $done = $progress->is_completed(); |
||
| 375 | $edit_link = ''; |
||
| 376 | $galleries = $this->nextgen_unserialize( $album->sortorder ); |
||
| 377 | $foogallery_album = false; |
||
| 378 | if ( $progress->foogallery_album_id > 0 ) { |
||
| 379 | $foogallery_album = FooGalleryAlbum::get_by_id( $progress->foogallery_album_id ); |
||
| 380 | if ( $foogallery_album ) { |
||
| 381 | $edit_link = '<a href="' . admin_url( 'post.php?post=' . $progress->foogallery_album_id . '&action=edit' ) . '">' . $foogallery_album->name . '</a>'; |
||
| 382 | } else { |
||
| 383 | $done = false; |
||
| 384 | } |
||
| 385 | } ?> |
||
| 386 | <tr class="<?php echo ($counter % 2 === 0) ? 'alternate' : ''; ?>"> |
||
| 387 | <td> |
||
| 388 | <?php echo $album->name; ?> |
||
| 389 | <input type="hidden" class="foogallery-album-id" value="<?php echo $album->id; ?>"> |
||
| 390 | </td> |
||
| 391 | <td> |
||
| 392 | <?php if ( $foogallery_album ) { |
||
| 393 | echo $edit_link; |
||
| 394 | } else { ?> |
||
| 395 | <input class="foogallery-album-name" value="<?php echo $album->name; ?>"> |
||
| 396 | <?php } ?> |
||
| 397 | </td> |
||
| 398 | <td> |
||
| 399 | <ul class="ul-disc" style="margin: 0 0 0 20px;"> |
||
| 400 | <?php |
||
| 401 | $import_gallery_count = 0; |
||
| 402 | foreach ( $galleries as $gallery_id ) { |
||
| 403 | if ( 'a' === substr( $gallery_id, 0, 1 ) ) { |
||
| 404 | //we are dealing with an album inside the album |
||
| 405 | $nested_album = $this->get_album( substr( $gallery_id, 1 ) ); |
||
| 406 | if ( $nested_album ) { |
||
| 407 | echo '<li>'; |
||
| 408 | echo __('[Album] ', 'foogallery'); |
||
| 409 | echo ' <span style="text-decoration:line-through">'; |
||
| 410 | echo $nested_album->name; |
||
| 411 | echo '</span>'; |
||
| 412 | echo ' (<span class="nextgen-import-progress-' . FooGallery_NextGen_Import_Progress::PROGRESS_ERROR . '">'; |
||
| 413 | echo __( 'nested albums not supported', 'foogallery' ); |
||
| 414 | echo '</span>)</li>'; |
||
| 415 | } |
||
| 416 | } else { |
||
| 417 | $nextgen_gallery = $this->get_gallery( $gallery_id ); |
||
| 418 | echo '<li>'; |
||
| 419 | $gallery_progress = $this->get_import_progress( $gallery_id ); |
||
| 420 | $gallery_completed = $gallery_progress->is_completed(); |
||
| 421 | if ( $gallery_completed ) { |
||
| 422 | $import_gallery_count ++; |
||
| 423 | } |
||
| 424 | echo $gallery_completed ? '' : '<span style="text-decoration:line-through">'; |
||
| 425 | echo $nextgen_gallery->title; |
||
| 426 | echo $gallery_completed ? '' : '</span>'; |
||
| 427 | echo ' (<span class="nextgen-import-progress-' . $gallery_progress->status . '">'; |
||
| 428 | echo $gallery_completed ? __( 'imported', 'foogallery' ) : __( 'not imported', 'foogallery' ); |
||
| 429 | echo '</span>)</li>'; |
||
| 430 | } |
||
| 431 | } |
||
| 432 | ?> |
||
| 433 | </ul> |
||
| 434 | </td> |
||
| 435 | <td> |
||
| 436 | <span class="nextgen-import-progress nextgen-import-progress-<?php echo $progress->status; ?>"> |
||
| 437 | <?php echo $progress->message(); ?> |
||
| 438 | </span> |
||
| 439 | <?php |
||
| 440 | |||
| 441 | echo '<br />'; |
||
| 442 | if ( !$progress->is_completed() ) { |
||
| 443 | if ( $import_gallery_count > 0 ) { |
||
| 444 | echo '<input type="submit" class="button button-primary start_album_import" value="Import Album">'; |
||
| 445 | echo '<div class="inline" style="width:20px"><span class="spinner"></span></div>'; |
||
| 446 | echo '<br />'; |
||
| 447 | if ( $import_gallery_count === count( $galleries ) ) { |
||
| 448 | _e( 'All galleries will be linked', 'foogallery' ); |
||
| 449 | } else { |
||
| 450 | echo sprintf( __( '%d/%d galleries will be linked', 'foogallery' ), $import_gallery_count, count( $galleries ) ); |
||
| 451 | echo '<br />'; |
||
| 452 | _e ( '(Only previously imported galleries can be linked)', 'foogallery' ); |
||
| 453 | } |
||
| 454 | } else { |
||
| 455 | _e( 'No galleries imported yet!!', 'foogallery' ); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | ?> |
||
| 460 | </td> |
||
| 461 | </tr> |
||
| 462 | <?php |
||
| 463 | } |
||
| 464 | ?> |
||
| 465 | </tbody> |
||
| 466 | </table> |
||
| 467 | <?php |
||
| 468 | wp_nonce_field( 'foogallery_nextgen_album_reset', 'foogallery_nextgen_album_reset', false ); |
||
| 469 | wp_nonce_field( 'foogallery_nextgen_album_import', 'foogallery_nextgen_album_import', false ); |
||
| 470 | |||
| 471 | if ( $has_imports ) { ?> |
||
| 472 | <br /> |
||
| 473 | <input type="submit" name="foogallery_nextgen_reset_album" class="button reset_album_import" value="<?php _e( 'Reset All Album Imports', 'foogallery' ); ?>"> |
||
| 474 | <?php } |
||
| 475 | } |
||
| 476 | |||
| 477 | function get_import_progress_for_album( $nextgen_gallery_album_id ) { |
||
| 487 | |||
| 488 | function import_album( $nextgen_gallery_album_id, $foogallery_album_name ) { |
||
| 489 | $progress = new FooGallery_NextGen_Import_Progress_Album(); |
||
| 490 | $progress->nextgen_album_id = $nextgen_gallery_album_id; |
||
| 491 | $progress->foogallery_album_title = $foogallery_album_name; |
||
| 492 | |||
| 493 | //create a new foogallery album |
||
| 494 | $progress->import(); |
||
| 495 | |||
| 496 | $overall_progress = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM ); |
||
| 497 | $overall_progress[ $nextgen_gallery_album_id ] = $progress; |
||
| 498 | update_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM, $overall_progress ); |
||
| 499 | } |
||
| 500 | |||
| 501 | function reset_album_import() { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Unserialize NextGEN data |
||
| 507 | */ |
||
| 508 | function nextgen_unserialize($value) { |
||
| 509 | $retval = NULL; |
||
| 510 | |||
| 511 | if ( is_string( $value ) ) { |
||
| 512 | $retval = stripcslashes( $value ); |
||
| 513 | |||
| 514 | if ( strlen( $value ) > 1 ) { |
||
| 515 | // We can't always rely on base64_decode() or json_decode() to return FALSE as their documentation |
||
| 516 | // claims so check if $retval begins with a: as that indicates we have a serialized PHP object. |
||
| 517 | if ( strpos( $retval, 'a:' ) === 0 ) { |
||
| 518 | $er = error_reporting(0); |
||
| 530 | |||
| 531 | } |
||
| 532 | |||
| 534 |