Completed
Push — update/open-graph-fallbacks ( 560614...bcfa67 )
by Jeremy
12:56
created

image-widget.php ➔ jetpack_migrate_image_widget()   D

Complexity

Conditions 28
Paths 145

Size

Total Lines 199
Code Lines 121

Duplication

Lines 24
Ratio 12.06 %

Importance

Changes 0
Metric Value
cc 28
eloc 121
nc 145
nop 0
dl 24
loc 199
rs 4.248
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
19
	// Only migrate if the new widget is available and we haven't yet migrated
20
	if ( ! class_exists( 'WP_Widget_Media_Image' ) || Jetpack_Options::get_option( 'image_widget_migration' ) ) {
21
		return;
22
	}
23
24
	$default_data = array(
25
		'attachment_id' => 0,
26
		'url' => '',
27
		'title' => '',
28
		'size' => 'custom',
29
		'width' => 0,
30
		'height' => 0,
31
		'align' => 'none',
32
		'caption' => '',
33
		'alt' => '',
34
		'link_type' => '',
35
		'link_url' => '',
36
		'image_classes' => '',
37
		'link_classes' => '',
38
		'link_rel' => '',
39
		'image_title' => '',
40
		'link_target_blank' => false,
41
		'conditions' => null,
42
	);
43
44
	$old_widgets = get_option( 'widget_image', array() );
45
	$media_image = get_option( 'widget_media_image', array() );
46
	$sidebars_widgets = wp_get_sidebars_widgets();
47
48
	// Persist old and current widgets in backup table.
49
	jetpack_store_migration_data( 'widget_image', maybe_serialize( $old_widgets ) );
50
	if ( jetpack_get_migration_data( 'widget_image' ) !== $old_widgets ) {
51
		return false;
52
	}
53
54
	jetpack_store_migration_data( 'sidebars_widgets', maybe_serialize( $sidebars_widgets ) );
55
	if ( jetpack_get_migration_data( 'sidebars_widgets' ) !== $sidebars_widgets ) {
56
		return false;
57
	}
58
59
	// Array to store legacy widget ids in to unregister on success.
60
	$widgets_to_unregister = array();
61
62
	foreach ( $old_widgets as $id => $widget ) {
63
		if ( is_string( $id ) ) {
64
			continue;
65
		}
66
67
		// Can be caused by instanciating but not populating a widget in the Customizer.
68
		if ( empty( $widget ) ) {
69
			continue;
70
		}
71
72
		// Ensure widget has no keys other than those expected.
73
		// Not all widgets have conditions, so lets add it in.
74
		$widget_copy = array_merge( array( 'conditions' => null ), $widget );
75
		$non_whitelisted_keys = array_diff_key( $widget_copy, array(
76
			'title' => '',
77
			'img_url' => '',
78
			'alt_text' => '',
79
			'img_title' => '',
80
			'caption' => '',
81
			'align' => '',
82
			'img_width' => '',
83
			'img_height' => '',
84
			'link' => '',
85
			'link_target_blank' => '',
86
			'conditions' => '',
87
		) );
88
89
		if ( count( $non_whitelisted_keys ) > 0 ) {
90
			// skipping the widget in question
91
			continue;
92
		}
93
94
		$media_image[ $id ] = array_merge( $default_data, $widget, array(
95
			'alt'         => $widget['alt_text'],
96
			'height'      => $widget['img_height'],
97
			'image_classes' => ! empty( $widget['align'] ) ? 'align' . $widget['align'] : '',
98
			'image_title' => $widget['img_title'],
99
			'link_url'    => $widget['link'],
100
			'url'         => $widget['img_url'],
101
			'width'       => $widget['img_width'],
102
		) );
103
104
		// Unsetting old widget fields
105
		$media_image[ $id ] = array_diff_key( $media_image[ $id ], array(
106
			'align' => false,
107
			'alt_text' => false,
108
			'img_height' => false,
109
			'img_title' => false,
110
			'img_url' => false,
111
			'img_width' => false,
112
			'link' => false,
113
		) );
114
115
		// Check if the image is in the media library.
116
		$image_basename = basename( $widget['img_url'] );
117
118
		if ( empty( $image_basename ) ) {
119
			continue;
120
		}
121
122
		$attachment_ids = get_posts( array(
123
			'fields'      => 'ids',
124
			'meta_query'  => array(
125
				array(
126
					'value'   => basename( $image_basename ),
127
					'compare' => 'LIKE',
128
					'key'     => '_wp_attachment_metadata',
129
				),
130
			),
131
			'post_status' => 'inherit',
132
			'post_type'   => 'attachment',
133
		) );
134
135
		foreach ( $attachment_ids as $attachment_id ) {
136
			$image_meta = wp_get_attachment_metadata( $attachment_id );
137
138
			// Is it a full size image?
139
			$image_path_pieces = explode( '/', $image_meta['file'] );
140 View Code Duplication
			if ( $image_basename === array_pop( $image_path_pieces ) ) {
141
				$media_image[ $id ]['attachment_id'] = $attachment_id;
142
143
				// Set correct size if dimensions fit.
144
				if (
145
					$media_image[ $id ]['width']  == $image_meta['width'] ||
146
					$media_image[ $id ]['height'] == $image_meta['height']
147
				) {
148
					$media_image[ $id ]['size'] = 'full';
149
				}
150
				break;
151
			}
152
153
			// Is it a down-sized image?
154
			foreach ( $image_meta['sizes'] as $size => $image ) {
155 View Code Duplication
				if ( false !== array_search( $image_basename, $image ) ) {
156
					$media_image[ $id ]['attachment_id'] = $attachment_id;
157
158
					// Set correct size if dimensions fit.
159
					if (
160
						$media_image[ $id ]['width']  == $image['width'] ||
161
						$media_image[ $id ]['height'] == $image['height']
162
					) {
163
						$media_image[ $id ]['size'] = $size;
164
					}
165
					break 2;
166
				}
167
			}
168
		}
169
170
		if ( ! empty( $widget['link'] ) ) {
171
			$media_image[ $id ]['link_type'] = $widget['link'] === $widget['img_url'] ? 'file' : 'custom';
172
		}
173
174
		foreach ( $sidebars_widgets as $sidebar => $widgets ) {
175
			if (
176
				is_array( $widgets )
177
				&& false !== ( $key = array_search( "image-{$id}", $widgets, true ) )
178
			) {
179
				$sidebars_widgets[ $sidebar ][ $key ] = "media_image-{$id}";
180
			}
181
		}
182
183
		$widgets_to_unregister[] = $id;
184
	}
185
186
	if ( update_option( 'widget_media_image', $media_image ) ) {
187
		delete_option( 'widget_image' );
188
189
		// Now un-register old widgets and register new.
190
		foreach ( $widgets_to_unregister as $id ) {
191
			wp_unregister_sidebar_widget( "image-${id}" );
192
193
			// register new widget.
194
			$media_image_widget = new WP_Widget_Media_Image();
195
			$media_image_widget->_set( $id );
196
			$media_image_widget->_register_one( $id );
197
		}
198
199
		wp_set_sidebars_widgets( $sidebars_widgets );
200
201
		// We need to refresh on widgets page for changes to take effect.
202
		add_action( 'current_screen', 'jetpack_refresh_on_widget_page' );
203
	} else {
204
		$widget_media_image = get_option( 'widget_media_image' );
205
		if ( is_array( $widget_media_image ) ) {
206
			delete_option( 'widget_image' );
207
		}
208
	}
209
	
210
	Jetpack_Options::update_option( 'image_widget_migration', true );
211
}
212
add_action( 'widgets_init', 'jetpack_migrate_image_widget' );
213
214
function jetpack_refresh_on_widget_page( $current ) {
215
	if ( 'widgets' === $current->base ) {
216
		wp_safe_redirect( admin_url( 'widgets.php' ) );
217
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function jetpack_refresh_on_widget_page() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
218
	}
219
}
220