Completed
Push — add/core-image-widget-compat ( 7ce3d6 )
by
unknown
09:45
created

image-widget.php ➔ jetpack_migrate_image_widget()   D

Complexity

Conditions 14
Paths 34

Size

Total Lines 106
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 71
nc 34
nop 0
dl 0
loc 106
rs 4.9516
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

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
	);
41
42
	$media_image      = get_option( 'widget_media_image' );
43
	$sidebars_widgets = wp_get_sidebars_widgets();
44
45
	foreach ( get_option( 'widget_image' ) as $id => $widget ) {
46
		if ( is_string( $id ) ) {
47
			continue;
48
		}
49
50
		$media_image[ $id ] = array_merge( $default_data, array_intersect_key( $widget, $default_data ), array(
51
			'alt'         => $widget['alt_text'],
52
			'height'      => $widget['img_height'],
53
			'image_title' => $widget['img_title'],
54
			'link_url'    => $widget['link'],
55
			'url'         => $widget['img_url'],
56
			'width'       => $widget['img_width'],
57
		) );
58
59
		// Check if the image is in the media library.
60
		$image_basename = basename( $widget['img_url'] );
61
		$attachment_ids = get_posts( array(
62
			'fields'      => 'ids',
63
			'meta_query'  => array(
64
				array(
65
					'value'   => basename( $image_basename ),
66
					'compare' => 'LIKE',
67
					'key'     => '_wp_attachment_metadata',
68
				),
69
			),
70
			'post_status' => 'inherit',
71
			'post_type'   => 'attachment',
72
		) );
73
74
		foreach ( (array) $attachment_ids as $attachment_id ) {
75
			$image_meta = wp_get_attachment_metadata( $attachment_id );
76
77
			// Is it a full size image?
78
			if ( $image_basename === array_pop( explode( '/', $image_meta['file'] ) ) ) {
0 ignored issues
show
Bug introduced by
explode('/', $image_meta['file']) cannot be passed to array_pop() as the parameter $array expects a reference.
Loading history...
79
				$media_image[ $id ]['attachment_id'] = $attachment_id;
80
				$media_image[ $id ]['width']         = $image_meta['width'];
81
				$media_image[ $id ]['height']        = $image_meta['height'];
82
				break;
83
			}
84
85
			// Is it a down-sized image?
86
			foreach ( $image_meta['sizes'] as $size => $image ) {
87
				if ( false !== array_search( $image_basename, $image ) ) {
88
					$media_image[ $id ]['attachment_id'] = $attachment_id;
89
					$media_image[ $id ]['size']          = $size;
90
					$media_image[ $id ]['width']         = $image['width'];
91
					$media_image[ $id ]['height']        = $image['height'];
92
					break 2;
93
				}
94
			}
95
		}
96
97
		if ( ! empty( $widget['link'] ) ) {
98
			$media_image[ $id ]['link_type'] = $widget['link'] === $widget['img_url'] ? 'file' : 'custom';
99
		}
100
101
		foreach ( $sidebars_widgets as $sidebar => $widgets ) {
102
			if ( false !== ( $key = array_search( "image-{$id}", $widgets, true ) ) ) {
103
				$sidebars_widgets[ $sidebar ][ $key ] = "media_image-{$id}";
104
			}
105
		}
106
107
		wp_unregister_sidebar_widget( "image-{$id}" );
108
		$media_image_widget = new WP_Widget_Media_Image();
109
		$media_image_widget->_set( $id );
110
		$media_image_widget->_register_one( $id );
111
	}
112
113
	update_option( 'widget_media_image', $media_image );
114
	delete_option( 'widget_image' );
115
	wp_set_sidebars_widgets( $sidebars_widgets );
116
117
	Jetpack_Options::update_option( 'image_widget_migration', true );
118
}
119
add_action( 'widgets_init', 'jetpack_migrate_image_widget' );
120