| Conditions | 27 |
| Paths | 533 |
| Total Lines | 192 |
| Code Lines | 115 |
| Lines | 24 |
| Ratio | 12.5 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | function jetpack_migrate_image_widget() { |
||
| 52 | // Only trigger the migration from wp-admin |
||
| 53 | if ( ! is_admin() ) { |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | // Only migrate if the new widget is available and we haven't yet migrated |
||
| 58 | if ( ! class_exists( 'WP_Widget_Media_Image' ) || Jetpack_Options::get_option( 'image_widget_migration' ) ) { |
||
|
|
|||
| 59 | // return; |
||
| 60 | } |
||
| 61 | |||
| 62 | $default_data = array( |
||
| 63 | 'attachment_id' => 0, |
||
| 64 | 'url' => '', |
||
| 65 | 'title' => '', |
||
| 66 | 'size' => 'custom', |
||
| 67 | 'width' => 0, |
||
| 68 | 'height' => 0, |
||
| 69 | 'align' => 'none', |
||
| 70 | 'caption' => '', |
||
| 71 | 'alt' => '', |
||
| 72 | 'link_type' => '', |
||
| 73 | 'link_url' => '', |
||
| 74 | 'image_classes' => '', |
||
| 75 | 'link_classes' => '', |
||
| 76 | 'link_rel' => '', |
||
| 77 | 'image_title' => '', |
||
| 78 | 'link_target_blank' => false, |
||
| 79 | 'conditions' => null, |
||
| 80 | ); |
||
| 81 | |||
| 82 | $old_widgets = get_option( 'widget_image', array() ); |
||
| 83 | $media_image = get_option( 'widget_media_image' ); |
||
| 84 | $sidebars_widgets = wp_get_sidebars_widgets(); |
||
| 85 | |||
| 86 | // Persist old and current widgets in backup table. |
||
| 87 | jetpack_store_legacy_image_widget_options( 'widget_image', serialize( $old_widgets ) ); |
||
| 88 | if ( jetpack_get_legacy_image_widget_option( 'widget_image' ) !== $old_widgets ) { |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | jetpack_store_legacy_image_widget_options( 'sidebars_widgets', serialize( $sidebars_widgets ) ); |
||
| 93 | if ( jetpack_get_legacy_image_widget_option( 'sidebars_widgets' ) !== $sidebars_widgets ) { |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | // Array to store legacy widget ids in to unregister on success. |
||
| 98 | $widgets_to_unregister = array(); |
||
| 99 | |||
| 100 | foreach ( $old_widgets as $id => $widget ) { |
||
| 101 | if ( is_string( $id ) ) { |
||
| 102 | continue; |
||
| 103 | } |
||
| 104 | |||
| 105 | // Can be caused by instanciating but not populating a widget in the Customizer. |
||
| 106 | if ( empty( $widget ) ) { |
||
| 107 | continue; |
||
| 108 | } |
||
| 109 | |||
| 110 | // Ensure widget has no keys other than those expected. |
||
| 111 | // Not all widgets have conditions, so lets add it in. |
||
| 112 | $widget_copy = array_merge( array( 'conditions' => null ), $widget ); |
||
| 113 | $non_whitelisted_keys = array_diff_key( $widget_copy, array( |
||
| 114 | 'title' => '', |
||
| 115 | 'img_url' => '', |
||
| 116 | 'alt_text' => '', |
||
| 117 | 'img_title' => '', |
||
| 118 | 'caption' => '', |
||
| 119 | 'align' => '', |
||
| 120 | 'img_width' => '', |
||
| 121 | 'img_height' => '', |
||
| 122 | 'link' => '', |
||
| 123 | 'link_target_blank' => '', |
||
| 124 | 'conditions' => '', |
||
| 125 | ) ); |
||
| 126 | |||
| 127 | if ( count( $non_whitelisted_keys ) > 0 ) { |
||
| 128 | // skipping the widget in question |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | $media_image[ $id ] = array_merge( $default_data, $widget, array( |
||
| 133 | 'alt' => $widget['alt_text'], |
||
| 134 | 'height' => $widget['img_height'], |
||
| 135 | 'image_classes' => ! empty( $widget['align'] ) ? 'align' . $widget['align'] : '', |
||
| 136 | 'image_title' => $widget['img_title'], |
||
| 137 | 'link_url' => $widget['link'], |
||
| 138 | 'url' => $widget['img_url'], |
||
| 139 | 'width' => $widget['img_width'], |
||
| 140 | ) ); |
||
| 141 | |||
| 142 | // Unsetting old widget fields |
||
| 143 | $media_image[ $id ] = array_diff_key( $media_image[ $id ], array( |
||
| 144 | 'align' => false, |
||
| 145 | 'alt_text' => false, |
||
| 146 | 'img_height' => false, |
||
| 147 | 'img_title' => false, |
||
| 148 | 'img_url' => false, |
||
| 149 | 'img_width' => false, |
||
| 150 | 'link' => false, |
||
| 151 | ) ); |
||
| 152 | |||
| 153 | // Check if the image is in the media library. |
||
| 154 | $image_basename = basename( $widget['img_url'] ); |
||
| 155 | |||
| 156 | if ( ! empty( $image_basename ) ) { |
||
| 157 | $attachment_ids = get_posts( array( |
||
| 158 | 'fields' => 'ids', |
||
| 159 | 'meta_query' => array( |
||
| 160 | array( |
||
| 161 | 'value' => basename( $image_basename ), |
||
| 162 | 'compare' => 'LIKE', |
||
| 163 | 'key' => '_wp_attachment_metadata', |
||
| 164 | ), |
||
| 165 | ), |
||
| 166 | 'post_status' => 'inherit', |
||
| 167 | 'post_type' => 'attachment', |
||
| 168 | ) ); |
||
| 169 | } |
||
| 170 | |||
| 171 | foreach ( (array) $attachment_ids as $attachment_id ) { |
||
| 172 | $image_meta = wp_get_attachment_metadata( $attachment_id ); |
||
| 173 | |||
| 174 | // Is it a full size image? |
||
| 175 | $image_path_pieces = explode( '/', $image_meta['file'] ); |
||
| 176 | View Code Duplication | if ( $image_basename === array_pop( $image_path_pieces ) ) { |
|
| 177 | $media_image[ $id ]['attachment_id'] = $attachment_id; |
||
| 178 | |||
| 179 | // Set correct size if dimensions fit. |
||
| 180 | if ( |
||
| 181 | $media_image[ $id ]['width'] == $image_meta['width'] || |
||
| 182 | $media_image[ $id ]['height'] == $image_meta['height'] |
||
| 183 | ) { |
||
| 184 | $media_image[ $id ]['size'] = 'full'; |
||
| 185 | } |
||
| 186 | break; |
||
| 187 | } |
||
| 188 | |||
| 189 | // Is it a down-sized image? |
||
| 190 | foreach ( $image_meta['sizes'] as $size => $image ) { |
||
| 191 | View Code Duplication | if ( false !== array_search( $image_basename, $image ) ) { |
|
| 192 | $media_image[ $id ]['attachment_id'] = $attachment_id; |
||
| 193 | |||
| 194 | // Set correct size if dimensions fit. |
||
| 195 | if ( |
||
| 196 | $media_image[ $id ]['width'] == $image['width'] || |
||
| 197 | $media_image[ $id ]['height'] == $image['height'] |
||
| 198 | ) { |
||
| 199 | $media_image[ $id ]['size'] = $size; |
||
| 200 | } |
||
| 201 | break 2; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | if ( ! empty( $widget['link'] ) ) { |
||
| 207 | $media_image[ $id ]['link_type'] = $widget['link'] === $widget['img_url'] ? 'file' : 'custom'; |
||
| 208 | } |
||
| 209 | |||
| 210 | foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
||
| 211 | if ( |
||
| 212 | is_array( $widgets ) |
||
| 213 | && false !== ( $key = array_search( "image-{$id}", $widgets, true ) ) |
||
| 214 | ) { |
||
| 215 | $sidebars_widgets[ $sidebar ][ $key ] = "media_image-{$id}"; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | $widgets_to_unregister[] = $id; |
||
| 220 | } |
||
| 221 | |||
| 222 | if ( update_option( 'widget_media_image', $media_image ) ) { |
||
| 223 | delete_option( 'widget_image' ); |
||
| 224 | |||
| 225 | // Now un-register old widgets and register new. |
||
| 226 | foreach ( $widgets_to_unregister as $id ) { |
||
| 227 | wp_unregister_sidebar_widget( "image-${id}" ); |
||
| 228 | |||
| 229 | // register new widget. |
||
| 230 | $media_image_widget = new WP_Widget_Media_Image(); |
||
| 231 | $media_image_widget->_set( $id ); |
||
| 232 | $media_image_widget->_register_one( $id ); |
||
| 233 | } |
||
| 234 | |||
| 235 | wp_set_sidebars_widgets( $sidebars_widgets ); |
||
| 236 | |||
| 237 | Jetpack_Options::update_option( 'image_widget_migration', true ); |
||
| 238 | |||
| 239 | // We need to refresh on widgets page for changes to take effect. |
||
| 240 | add_action( 'current_screen', 'jetpack_refresh_on_widget_page' ); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | add_action( 'widgets_init', 'jetpack_migrate_image_widget' ); |
||
| 250 |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.