first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,198 @@
<?php
/**
* Migration from Jetpack's Gallery Widget to WordPress' Core Gallery Widget.
*
* @since 5.5
*
* @package Jetpack
*/
/**
* Migrates all active instances of Jetpack's Gallery widget to Core's Media Gallery widget.
*/
function jetpack_migrate_gallery_widget() {
// Only trigger the migration from wp-admin and outside unit tests
if ( ! is_admin() || defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) {
return;
}
// Only migrate if the new widget is available and we haven't yet migrated
if ( ! class_exists( 'WP_Widget_Media_Gallery' ) || Jetpack_Options::get_option( 'gallery_widget_migration' ) ) {
return;
}
$old_widgets = get_option( 'widget_gallery', array() );
$media_gallery = get_option( 'widget_media_gallery', array() );
$sidebars_widgets = wp_get_sidebars_widgets();
// Array to store legacy widget ids in to unregister on success.
$widgets_to_unregister = array();
$old_widgets = array_filter( $old_widgets, 'jetpack_migrate_gallery_widget_is_importable' );
foreach ( $old_widgets as $id => $widget ) {
$new_id = $id;
// Try to get an unique id for the new type of widget.
// It may be the case that the user has already created a core Gallery Widget
// before the migration begins. (Maybe Jetpack was deactivated during core's upgrade).
for( $i = 0; $i < 10 && in_array( $new_id, array_keys( $media_gallery ) ); $i++, $new_id++ );
$widget_copy = jetpack_migrate_gallery_widget_upgrade_widget( $widget );
if ( null === $widget_copy ) {
jetpack_migrate_gallery_widget_bump_stats( 'gallery-widget-skipped' );
continue;
}
$media_gallery[ $new_id ] = $widget_copy;
$sidebars_widgets = jetpack_migrate_gallery_widget_update_sidebars( $sidebars_widgets, $id, $new_id );
$widgets_to_unregister[ $id ] = $new_id;
}
if ( update_option( 'widget_media_gallery', $media_gallery ) ) {
// Now un-register old widgets and register new.
foreach ( $widgets_to_unregister as $id => $new_id ) {
wp_unregister_sidebar_widget( "gallery-${id}" );
// register new widget.
$media_gallery_widget = new WP_Widget_Media_Gallery();
$media_gallery_widget->_set( $new_id );
$media_gallery_widget->_register_one( $new_id );
}
wp_set_sidebars_widgets( $sidebars_widgets );
// Log if we migrated all, or some for this site.
foreach ( $widgets_to_unregister as $w ) {
jetpack_migrate_gallery_widget_bump_stats( 'gallery-widget-migrated' );
}
// We need to refresh on widgets page for changes to take effect.
// The jetpack_refresh_on_widget_page function is already defined in migrate-to-core/image-widget.php
add_action( 'current_screen', 'jetpack_refresh_on_widget_page' );
}
Jetpack_Options::update_option( 'gallery_widget_migration', true );
}
function jetpack_migrate_gallery_widget_is_importable( $widget ) {
// Can be caused by instantiating but not populating a widget in the Customizer.
if ( empty( $widget ) ) {
return false;
}
// The array as stored in the option constains two keys and one
// is a string `_multiwidget` which does not represent a widget, so we skip it
if ( ! is_array( $widget ) ) {
return false;
}
return true;
}
/**
* Returns a transformed version of the Gallery Widget.
* Will return null if the widget is either empty, is not an array or has more keys than expected
*
* @param $widget One of the Jetpack Gallery widgets to be transformed into a new Core Media Gallery Widget
*
* @return array|null
*/
function jetpack_migrate_gallery_widget_upgrade_widget( $widget ) {
$whitelisted_keys = array(
'ids' => '',
'link' => '',
'title' => '',
'type' => '',
'random' => '',
'conditions' => '',
);
$default_data = array(
'columns' => 3,
'ids' => array(),
'link_type' => '',
'orderby_random' => false,
'size' => 'thumbnail',
'title' => '',
'type' => '',
);
if ( ! jetpack_migrate_gallery_widget_is_importable( $widget ) ) {
return null;
}
// 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, $whitelisted_keys );
if ( count( $non_whitelisted_keys ) > 0 ) {
jetpack_migrate_gallery_widget_bump_stats( 'extra-key' );
// Log the names of the keys not in our whitelist.
foreach ( $non_whitelisted_keys as $key => $value ) {
jetpack_migrate_gallery_widget_bump_stats( "extra-key-$key", "migration-extra-key" );
}
}
$widget_copy = array_merge( $default_data, $widget, array(
// ids in Jetpack's Gallery are a string of comma-separated values.
// Core's Media Gallery Widget stores ids in an array
'ids' => explode( ',', $widget['ids'] ),
'link_type' => $widget['link'],
'orderby_random' => isset( $widget['random'] ) && $widget['random'] === 'on',
) );
// Unsetting old widget fields
$widget_copy = array_diff_key( $widget_copy, array(
'link' => false,
'random' => false,
) );
return $widget_copy;
}
/**
* Replaces the references to Jetpack Gallery Widget in the sidebars for references to the new version of the widget
*
* @param $sidebars_widgets The sidebar widgets array to update
* @param $id Old id of the widget (basically its index in the array )
* @param $new_id New id that will be using on the sidebar as a new widget
*
* @return mixed Updated sidebar widgets array
*/
function jetpack_migrate_gallery_widget_update_sidebars( $sidebars_widgets, $id, $new_id ) {
foreach ( $sidebars_widgets as $sidebar => $widgets ) {
if (
is_array( $widgets )
&& false !== ( $key = array_search( "gallery-{$id}", $widgets, true ) )
) {
$sidebars_widgets[ $sidebar ][ $key ] = "media_gallery-{$new_id}";
// Check if the inactive widgets sidebar exists
// Related: https://core.trac.wordpress.org/ticket/14893
if ( ! isset( $sidebars_widgets['wp_inactive_widgets'] ) || ! is_array( $sidebars_widgets['wp_inactive_widgets'] ) ) {
$sidebars_widgets['wp_inactive_widgets'] = array();
}
$sidebars_widgets['wp_inactive_widgets'][ $key ] = "gallery-{$id}";
}
}
return $sidebars_widgets;
}
/**
* Will bump stat in jetpack_gallery_widget_migration group.
*
* @param string $bin The bin to log into.
* @param string $group The group name. Defaults to "widget-migration".
*/
function jetpack_migrate_gallery_widget_bump_stats( $bin, $group = 'widget-migration' ) {
// If this is being run on .com bumps_stats_extra exists, but using the filter looks more elegant.
if ( function_exists( 'bump_stats_extras' ) ) {
$group = "jetpack-$group";
do_action( 'jetpack_bump_stats_extra', $group, $bin );
} else {
// $group is prepended with 'jetpack-'
$jetpack = Jetpack::init();
$jetpack->stat( $group, $bin ) ;
}
}
add_action( 'widgets_init', 'jetpack_migrate_gallery_widget' );

View File

@@ -0,0 +1,220 @@
<?php
/**
* Migration from Jetpack's Image Widget to WordPress' Core Image Widget.
*
* @since 4.9
*
* @package Jetpack
*/
/**
* Migrates all active instances of Jetpack's image widget to Core's media image widget.
*/
function jetpack_migrate_image_widget() {
// Only trigger the migration from wp-admin
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;
}
$default_data = array(
'attachment_id' => 0,
'url' => '',
'title' => '',
'size' => 'custom',
'width' => 0,
'height' => 0,
'align' => 'none',
'caption' => '',
'alt' => '',
'link_type' => '',
'link_url' => '',
'image_classes' => '',
'link_classes' => '',
'link_rel' => '',
'image_title' => '',
'link_target_blank' => false,
'conditions' => null,
);
$old_widgets = get_option( 'widget_image', array() );
$media_image = get_option( 'widget_media_image', array() );
$sidebars_widgets = wp_get_sidebars_widgets();
// 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;
}
// 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'] : '',
'image_title' => $widget['img_title'],
'link_url' => $widget['link'],
'url' => $widget['img_url'],
'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(
array(
'value' => basename( $image_basename ),
'compare' => 'LIKE',
'key' => '_wp_attachment_metadata',
),
),
'post_status' => 'inherit',
'post_type' => 'attachment',
'suppress_filters' => false,
) );
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;
// 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;
}
// Is it a down-sized image?
foreach ( $image_meta['sizes'] as $size => $image ) {
if ( false !== array_search( $image_basename, $image ) ) {
$media_image[ $id ]['attachment_id'] = $attachment_id;
// 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;
}
}
}
if ( ! empty( $widget['link'] ) ) {
$media_image[ $id ]['link_type'] = $widget['link'] === $widget['img_url'] ? 'file' : 'custom';
}
foreach ( $sidebars_widgets as $sidebar => $widgets ) {
if (
is_array( $widgets )
&& false !== ( $key = array_search( "image-{$id}", $widgets, true ) )
) {
$sidebars_widgets[ $sidebar ][ $key ] = "media_image-{$id}";
}
}
$widgets_to_unregister[] = $id;
}
if ( update_option( 'widget_media_image', $media_image ) ) {
delete_option( 'widget_image' );
// 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 );
}
wp_set_sidebars_widgets( $sidebars_widgets );
// We need to refresh on widgets page for changes to take effect.
add_action( 'current_screen', 'jetpack_refresh_on_widget_page' );
} else {
$widget_media_image = get_option( 'widget_media_image' );
if ( is_array( $widget_media_image ) ) {
delete_option( 'widget_image' );
}
}
Jetpack_Options::update_option( 'image_widget_migration', true );
}
add_action( 'widgets_init', 'jetpack_migrate_image_widget' );
function jetpack_refresh_on_widget_page( $current ) {
if ( 'widgets' === $current->base ) {
wp_safe_redirect( admin_url( 'widgets.php' ) );
exit;
}
}