diff options
Diffstat (limited to 'plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php')
-rw-r--r-- | plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php | 125 |
1 files changed, 104 insertions, 21 deletions
diff --git a/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php b/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php index a441608b..08a36181 100644 --- a/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php +++ b/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php @@ -15,6 +15,7 @@ function jetpack_migrate_image_widget() { if ( ! is_admin() ) { return; } + // Only migrate if the new widget is available and we haven't yet migrated if ( ! class_exists( 'WP_Widget_Media_Image' ) || Jetpack_Options::get_option( 'image_widget_migration' ) ) { return; @@ -24,7 +25,7 @@ function jetpack_migrate_image_widget() { 'attachment_id' => 0, 'url' => '', 'title' => '', - 'size' => 'full', + 'size' => 'custom', 'width' => 0, 'height' => 0, 'align' => 'none', @@ -37,17 +38,60 @@ function jetpack_migrate_image_widget() { 'link_rel' => '', 'image_title' => '', 'link_target_blank' => false, + 'conditions' => null, ); - $media_image = get_option( 'widget_media_image' ); + $old_widgets = get_option( 'widget_image', array() ); + $media_image = get_option( 'widget_media_image', array() ); $sidebars_widgets = wp_get_sidebars_widgets(); - foreach ( get_option( 'widget_image', array() ) as $id => $widget ) { + // Persist old and current widgets in backup table. + jetpack_store_migration_data( 'widget_image', maybe_serialize( $old_widgets ) ); + if ( jetpack_get_migration_data( 'widget_image' ) !== $old_widgets ) { + return false; + } + + jetpack_store_migration_data( 'sidebars_widgets', maybe_serialize( $sidebars_widgets ) ); + if ( jetpack_get_migration_data( 'sidebars_widgets' ) !== $sidebars_widgets ) { + return false; + } + + // Array to store legacy widget ids in to unregister on success. + $widgets_to_unregister = array(); + + foreach ( $old_widgets as $id => $widget ) { if ( is_string( $id ) ) { continue; } - $media_image[ $id ] = array_merge( $default_data, array_intersect_key( $widget, $default_data ), array( + // Can be caused by instanciating but not populating a widget in the Customizer. + if ( empty( $widget ) ) { + continue; + } + + // Ensure widget has no keys other than those expected. + // Not all widgets have conditions, so lets add it in. + $widget_copy = array_merge( array( 'conditions' => null ), $widget ); + $non_whitelisted_keys = array_diff_key( $widget_copy, array( + 'title' => '', + 'img_url' => '', + 'alt_text' => '', + 'img_title' => '', + 'caption' => '', + 'align' => '', + 'img_width' => '', + 'img_height' => '', + 'link' => '', + 'link_target_blank' => '', + 'conditions' => '', + ) ); + + if ( count( $non_whitelisted_keys ) > 0 ) { + // skipping the widget in question + continue; + } + + $media_image[ $id ] = array_merge( $default_data, $widget, array( 'alt' => $widget['alt_text'], 'height' => $widget['img_height'], 'image_classes' => ! empty( $widget['align'] ) ? 'align' . $widget['align'] : '', @@ -57,8 +101,24 @@ function jetpack_migrate_image_widget() { 'width' => $widget['img_width'], ) ); + // Unsetting old widget fields + $media_image[ $id ] = array_diff_key( $media_image[ $id ], array( + 'align' => false, + 'alt_text' => false, + 'img_height' => false, + 'img_title' => false, + 'img_url' => false, + 'img_width' => false, + 'link' => false, + ) ); + // Check if the image is in the media library. $image_basename = basename( $widget['img_url'] ); + + if ( empty( $image_basename ) ) { + continue; + } + $attachment_ids = get_posts( array( 'fields' => 'ids', 'meta_query' => array( @@ -72,15 +132,21 @@ function jetpack_migrate_image_widget() { 'post_type' => 'attachment', ) ); - foreach ( (array) $attachment_ids as $attachment_id ) { + foreach ( $attachment_ids as $attachment_id ) { $image_meta = wp_get_attachment_metadata( $attachment_id ); // Is it a full size image? $image_path_pieces = explode( '/', $image_meta['file'] ); if ( $image_basename === array_pop( $image_path_pieces ) ) { $media_image[ $id ]['attachment_id'] = $attachment_id; - $media_image[ $id ]['width'] = $image_meta['width']; - $media_image[ $id ]['height'] = $image_meta['height']; + + // Set correct size if dimensions fit. + if ( + $media_image[ $id ]['width'] == $image_meta['width'] || + $media_image[ $id ]['height'] == $image_meta['height'] + ) { + $media_image[ $id ]['size'] = 'full'; + } break; } @@ -88,9 +154,14 @@ function jetpack_migrate_image_widget() { foreach ( $image_meta['sizes'] as $size => $image ) { if ( false !== array_search( $image_basename, $image ) ) { $media_image[ $id ]['attachment_id'] = $attachment_id; - $media_image[ $id ]['size'] = $size; - $media_image[ $id ]['width'] = $image['width']; - $media_image[ $id ]['height'] = $image['height']; + + // Set correct size if dimensions fit. + if ( + $media_image[ $id ]['width'] == $image['width'] || + $media_image[ $id ]['height'] == $image['height'] + ) { + $media_image[ $id ]['size'] = $size; + } break 2; } } @@ -101,25 +172,37 @@ function jetpack_migrate_image_widget() { } foreach ( $sidebars_widgets as $sidebar => $widgets ) { - if ( false !== ( $key = array_search( "image-{$id}", $widgets, true ) ) ) { + if ( + is_array( $widgets ) + && false !== ( $key = array_search( "image-{$id}", $widgets, true ) ) + ) { $sidebars_widgets[ $sidebar ][ $key ] = "media_image-{$id}"; } } - wp_unregister_sidebar_widget( "image-{$id}" ); - $media_image_widget = new WP_Widget_Media_Image(); - $media_image_widget->_set( $id ); - $media_image_widget->_register_one( $id ); + $widgets_to_unregister[] = $id; } - update_option( 'widget_media_image', $media_image ); - delete_option( 'widget_image' ); - wp_set_sidebars_widgets( $sidebars_widgets ); + if ( update_option( 'widget_media_image', $media_image ) ) { + delete_option( 'widget_image' ); - Jetpack_Options::update_option( 'image_widget_migration', true ); + // Now un-register old widgets and register new. + foreach ( $widgets_to_unregister as $id ) { + wp_unregister_sidebar_widget( "image-${id}" ); + + // register new widget. + $media_image_widget = new WP_Widget_Media_Image(); + $media_image_widget->_set( $id ); + $media_image_widget->_register_one( $id ); + } - // We need to refresh on widgets page for changes to take effect. - add_action( 'current_screen', 'jetpack_refresh_on_widget_page' ); + wp_set_sidebars_widgets( $sidebars_widgets ); + + Jetpack_Options::update_option( 'image_widget_migration', true ); + + // We need to refresh on widgets page for changes to take effect. + add_action( 'current_screen', 'jetpack_refresh_on_widget_page' ); + } } add_action( 'widgets_init', 'jetpack_migrate_image_widget' ); |