Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Migration from Jetpack's Gallery Widget to WordPress' Core Gallery Widget. |
||
| 4 | * |
||
| 5 | * @since 5.5 |
||
| 6 | * |
||
| 7 | * @package Jetpack |
||
| 8 | */ |
||
| 9 | /** |
||
| 10 | * Migrates all active instances of Jetpack's Gallery widget to Core's Media Gallery widget. |
||
| 11 | */ |
||
| 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++ ); |
||
|
0 ignored issues
–
show
|
|||
| 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 | |||
| 78 | function jetpack_migrate_gallery_widget_is_importable( $widget ) { |
||
| 79 | // Can be caused by instantiating but not populating a widget in the Customizer. |
||
| 80 | if ( empty( $widget ) ) { |
||
| 81 | return false; |
||
| 82 | } |
||
| 83 | // The array as stored in the option constains two keys and one |
||
| 84 | // is a string `_multiwidget` which does not represent a widget, so we skip it |
||
| 85 | if ( ! is_array( $widget ) ) { |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns a transformed version of the Gallery Widget. |
||
| 93 | * Will return null if the widget is either empty, is not an array or has more keys than expected |
||
| 94 | * |
||
| 95 | * @param $widget One of the Jetpack Gallery widgets to be transformed into a new Core Media Gallery Widget |
||
| 96 | * |
||
| 97 | * @return array|null |
||
| 98 | */ |
||
| 99 | function jetpack_migrate_gallery_widget_upgrade_widget( $widget ) { |
||
| 100 | $whitelisted_keys = array( |
||
| 101 | 'ids' => '', |
||
| 102 | 'link' => '', |
||
| 103 | 'title' => '', |
||
| 104 | 'type' => '', |
||
| 105 | 'random' => '', |
||
| 106 | 'conditions' => '', |
||
| 107 | ); |
||
| 108 | |||
| 109 | $default_data = array( |
||
| 110 | 'columns' => 3, |
||
| 111 | 'ids' => array(), |
||
| 112 | 'link_type' => '', |
||
| 113 | 'orderby_random' => false, |
||
| 114 | 'size' => 'thumbnail', |
||
| 115 | 'title' => '', |
||
| 116 | 'type' => '', |
||
| 117 | ); |
||
| 118 | |||
| 119 | if ( ! jetpack_migrate_gallery_widget_is_importable( $widget ) ) { |
||
| 120 | return null; |
||
| 121 | } |
||
| 122 | // Ensure widget has no keys other than those expected. |
||
| 123 | // Not all widgets have conditions, so lets add it in. |
||
| 124 | $widget_copy = array_merge( array( 'conditions' => null ), $widget ); |
||
| 125 | $non_whitelisted_keys = array_diff_key( $widget_copy, $whitelisted_keys ); |
||
| 126 | if ( count( $non_whitelisted_keys ) > 0 ) { |
||
| 127 | jetpack_migrate_gallery_widget_bump_stats( 'extra-key' ); |
||
| 128 | |||
| 129 | // Log the names of the keys not in our whitelist. |
||
| 130 | foreach ( $non_whitelisted_keys as $key => $value ) { |
||
| 131 | jetpack_migrate_gallery_widget_bump_stats( "extra-key-$key", "migration-extra-key" ); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | $widget_copy = array_merge( $default_data, $widget, array( |
||
| 136 | // ids in Jetpack's Gallery are a string of comma-separated values. |
||
| 137 | // Core's Media Gallery Widget stores ids in an array |
||
| 138 | 'ids' => explode( ',', $widget['ids'] ), |
||
| 139 | 'link_type' => $widget['link'], |
||
| 140 | 'orderby_random' => isset( $widget['random'] ) && $widget['random'] === 'on', |
||
| 141 | ) ); |
||
| 142 | |||
| 143 | // Unsetting old widget fields |
||
| 144 | $widget_copy = array_diff_key( $widget_copy, array( |
||
| 145 | 'link' => false, |
||
| 146 | 'random' => false, |
||
| 147 | ) ); |
||
| 148 | |||
| 149 | return $widget_copy; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Replaces the references to Jetpack Gallery Widget in the sidebars for references to the new version of the widget |
||
| 154 | * |
||
| 155 | * @param $sidebars_widgets The sidebar widgets array to update |
||
| 156 | * @param $id Old id of the widget (basically its index in the array ) |
||
| 157 | * @param $new_id New id that will be using on the sidebar as a new widget |
||
| 158 | * |
||
| 159 | * @return mixed Updated sidebar widgets array |
||
| 160 | */ |
||
| 161 | function jetpack_migrate_gallery_widget_update_sidebars( $sidebars_widgets, $id, $new_id ) { |
||
| 162 | View Code Duplication | foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
|
| 163 | if ( |
||
| 164 | is_array( $widgets ) |
||
| 165 | && false !== ( $key = array_search( "gallery-{$id}", $widgets, true ) ) |
||
| 166 | ) { |
||
| 167 | $sidebars_widgets[ $sidebar ][ $key ] = "media_gallery-{$new_id}"; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | return $sidebars_widgets; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Will bump stat in jetpack_gallery_widget_migration group. |
||
| 175 | * |
||
| 176 | * @param string $bin The bin to log into. |
||
| 177 | * @param string $group The group name. Defaults to "widget-migration". |
||
| 178 | */ |
||
| 179 | function jetpack_migrate_gallery_widget_bump_stats( $bin, $group = 'widget-migration' ) { |
||
| 180 | // If this is being run on .com bumps_stats_extra exists, but using the filter looks more elegant. |
||
| 181 | if ( function_exists( 'bump_stats_extras' ) ) { |
||
| 182 | $group = "jetpack-$group"; |
||
| 183 | do_action( 'jetpack_bump_stats_extra', $group, $bin ); |
||
| 184 | } else { |
||
| 185 | // $group is prepended with 'jetpack-' |
||
| 186 | $jetpack = Jetpack::init(); |
||
| 187 | $jetpack->stat( $group, $bin ) ; |
||
| 188 | } |
||
| 189 | |||
| 190 | } |
||
| 191 | |||
| 192 | add_action( 'widgets_init', 'jetpack_migrate_gallery_widget' ); |
||
| 193 |
Let’s take a look at an example: