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 Image Widget to WordPress' Core Image Widget. |
||
| 4 | * |
||
| 5 | * @since 4.9 |
||
| 6 | * |
||
| 7 | * @package Jetpack |
||
| 8 | */ |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Migrates all active instances of Jetpack's image widget to Core's media image widget. |
||
| 12 | */ |
||
| 13 | function jetpack_migrate_image_widget() { |
||
| 14 | // Only trigger the migration from wp-admin |
||
| 15 | if ( ! is_admin() ) { |
||
| 16 | return; |
||
| 17 | } |
||
| 18 | // Only migrate if the new widget is available and we haven't yet migrated |
||
| 19 | if ( ! class_exists( 'WP_Widget_Media_Image' ) || Jetpack_Options::get_option( 'image_widget_migration' ) ) { |
||
| 20 | return; |
||
| 21 | } |
||
| 22 | |||
| 23 | $default_data = array( |
||
| 24 | 'attachment_id' => 0, |
||
| 25 | 'url' => '', |
||
| 26 | 'title' => '', |
||
| 27 | 'size' => 'full', |
||
| 28 | 'width' => 0, |
||
| 29 | 'height' => 0, |
||
| 30 | 'align' => 'none', |
||
| 31 | 'caption' => '', |
||
| 32 | 'alt' => '', |
||
| 33 | 'link_type' => '', |
||
| 34 | 'link_url' => '', |
||
| 35 | 'image_classes' => '', |
||
| 36 | 'link_classes' => '', |
||
| 37 | 'link_rel' => '', |
||
| 38 | 'image_title' => '', |
||
| 39 | 'link_target_blank' => false, |
||
| 40 | 'conditions' => null, |
||
| 41 | ); |
||
| 42 | |||
| 43 | $media_image = get_option( 'widget_media_image' ); |
||
| 44 | $sidebars_widgets = wp_get_sidebars_widgets(); |
||
| 45 | |||
| 46 | // Array to store legacy widget ids in to unregister on success. |
||
| 47 | $widgets_to_unregister = array(); |
||
| 48 | |||
| 49 | foreach ( get_option( 'widget_image', array() ) as $id => $widget ) { |
||
| 50 | if ( is_string( $id ) ) { |
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | |||
| 54 | // Can be caused by instanciating but not populating a widget in the Customizer. |
||
| 55 | if ( empty( $widget ) ) { |
||
| 56 | continue; |
||
| 57 | } |
||
| 58 | |||
| 59 | $media_image[ $id ] = array_merge( $default_data, array_intersect_key( $widget, $default_data ), array( |
||
| 60 | 'alt' => $widget['alt_text'], |
||
| 61 | 'height' => $widget['img_height'], |
||
| 62 | 'image_classes' => ! empty( $widget['align'] ) ? 'align' . $widget['align'] : '', |
||
| 63 | 'image_title' => $widget['img_title'], |
||
| 64 | 'link_url' => $widget['link'], |
||
| 65 | 'url' => $widget['img_url'], |
||
| 66 | 'width' => $widget['img_width'], |
||
| 67 | ) ); |
||
| 68 | |||
| 69 | // Check if the image is in the media library. |
||
| 70 | $image_basename = basename( $widget['img_url'] ); |
||
| 71 | |||
| 72 | if ( ! empty( $image_basename ) ) { |
||
| 73 | $attachment_ids = get_posts( array( |
||
| 74 | 'fields' => 'ids', |
||
| 75 | 'meta_query' => array( |
||
| 76 | array( |
||
| 77 | 'value' => basename( $image_basename ), |
||
| 78 | 'compare' => 'LIKE', |
||
| 79 | 'key' => '_wp_attachment_metadata', |
||
| 80 | ), |
||
| 81 | ), |
||
| 82 | 'post_status' => 'inherit', |
||
| 83 | 'post_type' => 'attachment', |
||
| 84 | ) ); |
||
| 85 | } |
||
| 86 | |||
| 87 | foreach ( (array) $attachment_ids as $attachment_id ) { |
||
|
0 ignored issues
–
show
|
|||
| 88 | $image_meta = wp_get_attachment_metadata( $attachment_id ); |
||
| 89 | |||
| 90 | // Is it a full size image? |
||
| 91 | $image_path_pieces = explode( '/', $image_meta['file'] ); |
||
| 92 | if ( $image_basename === array_pop( $image_path_pieces ) ) { |
||
| 93 | $media_image[ $id ]['attachment_id'] = $attachment_id; |
||
| 94 | $media_image[ $id ]['width'] = $image_meta['width']; |
||
| 95 | $media_image[ $id ]['height'] = $image_meta['height']; |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | |||
| 99 | // Is it a down-sized image? |
||
| 100 | foreach ( $image_meta['sizes'] as $size => $image ) { |
||
| 101 | if ( false !== array_search( $image_basename, $image ) ) { |
||
| 102 | $media_image[ $id ]['attachment_id'] = $attachment_id; |
||
| 103 | $media_image[ $id ]['size'] = $size; |
||
| 104 | $media_image[ $id ]['width'] = $image['width']; |
||
| 105 | $media_image[ $id ]['height'] = $image['height']; |
||
| 106 | break 2; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | if ( ! empty( $widget['link'] ) ) { |
||
| 112 | $media_image[ $id ]['link_type'] = $widget['link'] === $widget['img_url'] ? 'file' : 'custom'; |
||
| 113 | } |
||
| 114 | |||
| 115 | foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
||
| 116 | if ( |
||
| 117 | is_array( $widgets ) |
||
| 118 | && false !== ( $key = array_search( "image-{$id}", $widgets, true ) ) |
||
| 119 | ) { |
||
| 120 | $sidebars_widgets[ $sidebar ][ $key ] = "media_image-{$id}"; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $widgets_to_unregister[] = $id; |
||
| 125 | } |
||
| 126 | |||
| 127 | if ( update_option( 'widget_media_image', $media_image ) ) { |
||
| 128 | delete_option( 'widget_image' ); |
||
| 129 | |||
| 130 | // Now un-register old widgets and register new. |
||
| 131 | foreach ( $widgets_to_unregister as $id ) { |
||
| 132 | wp_unregister_sidebar_widget( "image-${id}" ); |
||
| 133 | |||
| 134 | // register new widget. |
||
| 135 | $media_image_widget = new WP_Widget_Media_Image(); |
||
| 136 | $media_image_widget->_set( $id ); |
||
| 137 | $media_image_widget->_register_one( $id ); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | wp_set_sidebars_widgets( $sidebars_widgets ); |
||
| 142 | |||
| 143 | Jetpack_Options::update_option( 'image_widget_migration', true ); |
||
| 144 | |||
| 145 | // We need to refresh on widgets page for changes to take effect. |
||
| 146 | add_action( 'current_screen', 'jetpack_refresh_on_widget_page' ); |
||
| 147 | } |
||
| 148 | add_action( 'widgets_init', 'jetpack_migrate_image_widget' ); |
||
| 149 | |||
| 150 | function jetpack_refresh_on_widget_page( $current ) { |
||
| 151 | if ( 'widgets' === $current->base ) { |
||
| 152 | wp_safe_redirect( admin_url( 'widgets.php' ) ); |
||
| 153 | } |
||
| 154 | } |
||
| 155 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: