/wp-content/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php

https://github.com/livinglab/openlab · PHP · 223 lines · 165 code · 31 blank · 27 comment · 24 complexity · 9a9d5c9645604051deab3173476bcd38 MD5 · raw file

  1. <?php
  2. /**
  3. * Migration from Jetpack's Image Widget to WordPress' Core Image Widget.
  4. *
  5. * @since 4.9
  6. *
  7. * @package automattic/jetpack
  8. */
  9. /**
  10. * Migrates all active instances of Jetpack's image widget to Core's media image widget.
  11. */
  12. function jetpack_migrate_image_widget() {
  13. // Only trigger the migration from wp-admin
  14. if ( ! is_admin() ) {
  15. return;
  16. }
  17. // Only migrate if the new widget is available and we haven't yet migrated
  18. if ( ! class_exists( 'WP_Widget_Media_Image' ) || Jetpack_Options::get_option( 'image_widget_migration' ) ) {
  19. return;
  20. }
  21. $default_data = array(
  22. 'attachment_id' => 0,
  23. 'url' => '',
  24. 'title' => '',
  25. 'size' => 'custom',
  26. 'width' => 0,
  27. 'height' => 0,
  28. 'align' => 'none',
  29. 'caption' => '',
  30. 'alt' => '',
  31. 'link_type' => '',
  32. 'link_url' => '',
  33. 'image_classes' => '',
  34. 'link_classes' => '',
  35. 'link_rel' => '',
  36. 'image_title' => '',
  37. 'link_target_blank' => false,
  38. 'conditions' => null,
  39. );
  40. $old_widgets = get_option( 'widget_image', array() );
  41. $media_image = get_option( 'widget_media_image', array() );
  42. $sidebars_widgets = wp_get_sidebars_widgets();
  43. // Persist old and current widgets in backup table.
  44. jetpack_store_migration_data( 'widget_image', maybe_serialize( $old_widgets ) );
  45. if ( jetpack_get_migration_data( 'widget_image' ) !== $old_widgets ) {
  46. return false;
  47. }
  48. jetpack_store_migration_data( 'sidebars_widgets', maybe_serialize( $sidebars_widgets ) );
  49. if ( jetpack_get_migration_data( 'sidebars_widgets' ) !== $sidebars_widgets ) {
  50. return false;
  51. }
  52. // Array to store legacy widget ids in to unregister on success.
  53. $widgets_to_unregister = array();
  54. foreach ( $old_widgets as $id => $widget ) {
  55. if ( is_string( $id ) ) {
  56. continue;
  57. }
  58. // Can be caused by instanciating but not populating a widget in the Customizer.
  59. if ( empty( $widget ) ) {
  60. continue;
  61. }
  62. // Ensure widget has no keys other than those expected.
  63. // Not all widgets have conditions, so lets add it in.
  64. $widget_copy = array_merge( array( 'conditions' => null ), $widget );
  65. $non_allowed_keys = array_diff_key(
  66. $widget_copy,
  67. array(
  68. 'title' => '',
  69. 'img_url' => '',
  70. 'alt_text' => '',
  71. 'img_title' => '',
  72. 'caption' => '',
  73. 'align' => '',
  74. 'img_width' => '',
  75. 'img_height' => '',
  76. 'link' => '',
  77. 'link_target_blank' => '',
  78. 'conditions' => '',
  79. )
  80. );
  81. if ( count( $non_allowed_keys ) > 0 ) {
  82. // skipping the widget in question
  83. continue;
  84. }
  85. $media_image[ $id ] = array_merge( $default_data, $widget, array(
  86. 'alt' => $widget['alt_text'],
  87. 'height' => $widget['img_height'],
  88. 'image_classes' => ! empty( $widget['align'] ) ? 'align' . $widget['align'] : '',
  89. 'image_title' => $widget['img_title'],
  90. 'link_url' => $widget['link'],
  91. 'url' => $widget['img_url'],
  92. 'width' => $widget['img_width'],
  93. ) );
  94. // Unsetting old widget fields
  95. $media_image[ $id ] = array_diff_key( $media_image[ $id ], array(
  96. 'align' => false,
  97. 'alt_text' => false,
  98. 'img_height' => false,
  99. 'img_title' => false,
  100. 'img_url' => false,
  101. 'img_width' => false,
  102. 'link' => false,
  103. ) );
  104. // Check if the image is in the media library.
  105. $image_basename = basename( $widget['img_url'] );
  106. if ( empty( $image_basename ) ) {
  107. continue;
  108. }
  109. $attachment_ids = get_posts( array(
  110. 'fields' => 'ids',
  111. 'meta_query' => array(
  112. array(
  113. 'value' => basename( $image_basename ),
  114. 'compare' => 'LIKE',
  115. 'key' => '_wp_attachment_metadata',
  116. ),
  117. ),
  118. 'post_status' => 'inherit',
  119. 'post_type' => 'attachment',
  120. 'suppress_filters' => false,
  121. ) );
  122. foreach ( $attachment_ids as $attachment_id ) {
  123. $image_meta = wp_get_attachment_metadata( $attachment_id );
  124. // Is it a full size image?
  125. $image_path_pieces = explode( '/', $image_meta['file'] );
  126. if ( $image_basename === array_pop( $image_path_pieces ) ) {
  127. $media_image[ $id ]['attachment_id'] = $attachment_id;
  128. // Set correct size if dimensions fit.
  129. if (
  130. $media_image[ $id ]['width'] == $image_meta['width'] ||
  131. $media_image[ $id ]['height'] == $image_meta['height']
  132. ) {
  133. $media_image[ $id ]['size'] = 'full';
  134. }
  135. break;
  136. }
  137. // Is it a down-sized image?
  138. foreach ( $image_meta['sizes'] as $size => $image ) {
  139. if ( false !== array_search( $image_basename, $image ) ) {
  140. $media_image[ $id ]['attachment_id'] = $attachment_id;
  141. // Set correct size if dimensions fit.
  142. if (
  143. $media_image[ $id ]['width'] == $image['width'] ||
  144. $media_image[ $id ]['height'] == $image['height']
  145. ) {
  146. $media_image[ $id ]['size'] = $size;
  147. }
  148. break 2;
  149. }
  150. }
  151. }
  152. if ( ! empty( $widget['link'] ) ) {
  153. $media_image[ $id ]['link_type'] = $widget['link'] === $widget['img_url'] ? 'file' : 'custom';
  154. }
  155. foreach ( $sidebars_widgets as $sidebar => $widgets ) {
  156. if (
  157. is_array( $widgets )
  158. && false !== ( $key = array_search( "image-{$id}", $widgets, true ) )
  159. ) {
  160. $sidebars_widgets[ $sidebar ][ $key ] = "media_image-{$id}";
  161. }
  162. }
  163. $widgets_to_unregister[] = $id;
  164. }
  165. if ( update_option( 'widget_media_image', $media_image ) ) {
  166. delete_option( 'widget_image' );
  167. // Now un-register old widgets and register new.
  168. foreach ( $widgets_to_unregister as $id ) {
  169. wp_unregister_sidebar_widget( "image-${id}" );
  170. // register new widget.
  171. $media_image_widget = new WP_Widget_Media_Image();
  172. $media_image_widget->_set( $id );
  173. $media_image_widget->_register_one( $id );
  174. }
  175. wp_set_sidebars_widgets( $sidebars_widgets );
  176. // We need to refresh on widgets page for changes to take effect.
  177. add_action( 'current_screen', 'jetpack_refresh_on_widget_page' );
  178. } else {
  179. $widget_media_image = get_option( 'widget_media_image' );
  180. if ( is_array( $widget_media_image ) ) {
  181. delete_option( 'widget_image' );
  182. }
  183. }
  184. Jetpack_Options::update_option( 'image_widget_migration', true );
  185. }
  186. add_action( 'widgets_init', 'jetpack_migrate_image_widget' );
  187. function jetpack_refresh_on_widget_page( $current ) {
  188. if ( 'widgets' === $current->base ) {
  189. wp_safe_redirect( admin_url( 'widgets.php' ) );
  190. exit;
  191. }
  192. }