| Conditions | 12 |
| Paths | 12 |
| Total Lines | 65 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 12 | function jetpack_migrate_gallery_widget() { |
||
| 13 | // Only trigger the migration from wp-admin and outside unit tests |
||
| 14 | if ( ! is_admin() || defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) { |
||
| 15 | return; |
||
| 16 | } |
||
| 17 | |||
| 18 | // Only migrate if the new widget is available and we haven't yet migrated |
||
| 19 | if ( ! class_exists( 'WP_Widget_Media_Gallery' ) || Jetpack_Options::get_option( 'gallery_widget_migration' ) ) { |
||
| 20 | return; |
||
| 21 | } |
||
| 22 | |||
| 23 | $old_widgets = get_option( 'widget_gallery', array() ); |
||
| 24 | $media_gallery = get_option( 'widget_media_gallery', array() ); |
||
| 25 | $sidebars_widgets = wp_get_sidebars_widgets(); |
||
| 26 | |||
| 27 | // Array to store legacy widget ids in to unregister on success. |
||
| 28 | $widgets_to_unregister = array(); |
||
| 29 | |||
| 30 | $old_widgets = array_filter( $old_widgets, 'jetpack_migrate_gallery_widget_is_importable' ); |
||
| 31 | foreach ( $old_widgets as $id => $widget ) { |
||
| 32 | $new_id = $id; |
||
| 33 | // Try to get an unique id for the new type of widget. |
||
| 34 | // It may be the case that the user has already created a core Gallery Widget |
||
| 35 | // before the migration begins. (Maybe Jetpack was deactivated during core's upgrade). |
||
| 36 | for( $i = 0; $i < 10 && in_array( $new_id, array_keys( $media_gallery ) ); $i++, $new_id++ ); |
||
|
|
|||
| 37 | |||
| 38 | $widget_copy = jetpack_migrate_gallery_widget_upgrade_widget( $widget ); |
||
| 39 | |||
| 40 | if ( null === $widget_copy ) { |
||
| 41 | jetpack_migrate_gallery_widget_bump_stats( 'gallery-widget-skipped' ); |
||
| 42 | continue; |
||
| 43 | } |
||
| 44 | |||
| 45 | $media_gallery[ $new_id ] = $widget_copy; |
||
| 46 | |||
| 47 | $sidebars_widgets = jetpack_migrate_gallery_widget_update_sidebars( $sidebars_widgets, $id, $new_id ); |
||
| 48 | |||
| 49 | $widgets_to_unregister[ $id ] = $new_id; |
||
| 50 | } |
||
| 51 | |||
| 52 | if ( update_option( 'widget_media_gallery', $media_gallery ) ) { |
||
| 53 | |||
| 54 | // Now un-register old widgets and register new. |
||
| 55 | foreach ( $widgets_to_unregister as $id => $new_id ) { |
||
| 56 | wp_unregister_sidebar_widget( "gallery-${id}" ); |
||
| 57 | |||
| 58 | // register new widget. |
||
| 59 | $media_gallery_widget = new WP_Widget_Media_Gallery(); |
||
| 60 | $media_gallery_widget->_set( $new_id ); |
||
| 61 | $media_gallery_widget->_register_one( $new_id ); |
||
| 62 | } |
||
| 63 | |||
| 64 | wp_set_sidebars_widgets( $sidebars_widgets ); |
||
| 65 | |||
| 66 | // Log if we migrated all, or some for this site. |
||
| 67 | foreach ( $widgets_to_unregister as $w ) { |
||
| 68 | jetpack_migrate_gallery_widget_bump_stats( 'gallery-widget-migrated' ); |
||
| 69 | } |
||
| 70 | |||
| 71 | // We need to refresh on widgets page for changes to take effect. |
||
| 72 | // The jetpack_refresh_on_widget_page function is already defined in migrate-to-core/image-widget.php |
||
| 73 | add_action( 'current_screen', 'jetpack_refresh_on_widget_page' ); |
||
| 74 | } |
||
| 75 | Jetpack_Options::update_option( 'gallery_widget_migration', true ); |
||
| 76 | } |
||
| 77 | |||
| 193 |
Let’s take a look at an example: