Completed
Push — master ( af0f5c...e208fb )
by Md. Mozahidur
03:34
created

5.0.0.php ➔ _migrate_field_500()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 116
Code Lines 33

Duplication

Lines 16
Ratio 13.79 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 33
c 1
b 0
f 0
nc 10
nop 1
dl 16
loc 116
rs 4.8196

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 
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 79 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
*  Upgrade to version 5.0.0
5
*
6
*  @type	upgrade
7
*  @date	20/02/2014
8
*  @since	5.0.0
9
*
10
*  @param	n/a
11
*  @return	n/a
12
*/
13
14
// Exit if accessed directly
15
if( !defined('ABSPATH') ) exit;
16
17
18
// global
19
global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
20
21
22
// migrate field groups
23
$ofgs = get_posts(array(
24
	'numberposts' 		=> -1,
25
	'post_type' 		=> 'acf',
26
	'orderby' 			=> 'menu_order title',
27
	'order' 			=> 'asc',
28
	'suppress_filters'	=> true,
29
));
30
31
32
// populate acfs
33
if( $ofgs ){ foreach( $ofgs as $ofg ){
34
	
35
	// migrate field group
36
	$nfg = _migrate_field_group_500( $ofg );
37
	
38
	
39
	// get field from postmeta
40
	$rows = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key LIKE %s", $ofg->ID, 'field_%'), ARRAY_A);
41
	
42
	
43
	if( $rows )
44
	{
45
		$nfg['fields'] = array();
46
		
47
		foreach( $rows as $row )
48
		{
49
			$field = $row['meta_value'];
50
			$field = maybe_unserialize( $field );
51
			$field = maybe_unserialize( $field ); // run again for WPML
52
			
53
			
54
			// add parent
55
			$field['parent'] = $nfg['ID'];
56
			
57
			
58
			// migrate field
59
			$field = _migrate_field_500( $field );
60
		}
61
 	}
62
 		
63
}}
64
65
66
/*
67
*  _migrate_field_group_500
68
*
69
*  description
70
*
71
*  @type	function
72
*  @date	20/02/2014
73
*  @since	5.0.0
74
*
75
*  @param	$post_id (int)
76
*  @return	$post_id (int)
77
*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
78
79
function _migrate_field_group_500( $ofg ) {
80
	
81
	// global
82
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
83
	
84
	
85
	// get post status
86
	$post_status = $ofg->post_status;
87
	
88
	
89
	// create new field group
90
	$nfg = array(
91
		'ID'			=> 0,
92
		'title'			=> $ofg->post_title,
93
		'menu_order'	=> $ofg->menu_order,
94
	);
95
	
96
	
97
	// location rules
98
	$groups = array();
99
	
100
	
101
	// get all rules
102
 	$rules = get_post_meta($ofg->ID, 'rule', false);
103
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
104
 	if( is_array($rules) ) {
105
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
106
 		$group_no = 0;
107
 		
108
	 	foreach( $rules as $rule ) {
109
	 		
110
	 		// if field group was duplicated, it may now be a serialized string!
111
	 		$rule = maybe_unserialize($rule);
112
	 		
113
	 		
114
		 	// does this rule have a group?
115
		 	// + groups were added in 4.0.4
116
		 	if( !isset($rule['group_no']) ) {
117
		 	
118
			 	$rule['group_no'] = $group_no;
119
			 	
120
			 	// sperate groups?
121
			 	if( get_post_meta($ofg->ID, 'allorany', true) == 'any' ) {
122
			 	
123
				 	$group_no++;
124
				 	
125
			 	}
126
			 	
127
		 	}
128
		 	
129
		 	
130
		 	// extract vars
131
		 	$group = acf_extract_var( $rule, 'group_no' );
132
		 	$order = acf_extract_var( $rule, 'order_no' );
133
		 	
134
		 	
135
		 	// add to group
136
		 	$groups[ $group ][ $order ] = $rule;
137
		 	
138
		 	
139
		 	// sort rules
140
		 	ksort( $groups[ $group ] );
141
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
142
	 	}
143
	 	
144
	 	// sort groups
145
		ksort( $groups );
146
 	}
147
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
148
 	$nfg['location'] = $groups;
149
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
150
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
151
	// settings
152
 	if( $position = get_post_meta($ofg->ID, 'position', true) ) {
153
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
154
		$nfg['position'] = $position;
155
		
156
	}
157
	
158
 	if( $layout = get_post_meta($ofg->ID, 'layout', true) ) {
159
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
160
		$nfg['layout'] = $layout;
161
		
162
	}
163
	
164
 	if( $hide_on_screen = get_post_meta($ofg->ID, 'hide_on_screen', true) ) {
165
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
166
		$nfg['hide_on_screen'] = maybe_unserialize($hide_on_screen);
167
		
168
	}
169
	
170
	
171
	// Note: acf_update_field_group will call the acf_get_valid_field_group function and apply 'compatibility' changes
172
	
173
	
174
	// add old ID reference
175
	$nfg['old_ID'] = $ofg->ID;
176
	
177
	
178
	// save field group
179
	$nfg = acf_update_field_group( $nfg );
180
	
181
	
182
	// trash?
183
	if( $post_status == 'trash' ) {
184
		
185
		acf_trash_field_group( $nfg['ID'] );
186
		
187
	}
188
	
189
	
190
	// return
191
	return $nfg;
192
}
193
194
195
/*
196
*  _migrate_field_500
197
*
198
*  description
199
*
200
*  @type	function
201
*  @date	20/02/2014
202
*  @since	5.0.0
203
*
204
*  @param	$post_id (int)
205
*  @return	$post_id (int)
206
*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
207
208
function _migrate_field_500( $field ) {
209
	
210
	// orig
211
	$orig = $field;
212
	
213
	
214
	// order_no is now menu_order
215
	$field['menu_order'] = acf_extract_var( $field, 'order_no' );
216
	
217
	
218
	// correct very old field keys
219
	if( substr($field['key'], 0, 6) !== 'field_' ) {
220
	
221
		$field['key'] = 'field_' . str_replace('field', '', $field['key']);
222
		
223
	}
224
	
225
	
226
	// get valid field
227
	$field = acf_get_valid_field( $field );
0 ignored issues
show
Documentation introduced by
$field is of type array<string,*>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
228
	
229
	
230
	// save field
231
	$field = acf_update_field( $field );
0 ignored issues
show
Documentation introduced by
$field is of type array<string,?,{"_valid":"?"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
232
	
233
	
234
	// sub fields
235
	if( $field['type'] == 'repeater' ) {
236
		
237
		// get sub fields
238
		$sub_fields = acf_extract_var( $orig, 'sub_fields' );
239
		
240
		
241
		// save sub fields
242
		if( !empty($sub_fields) ) {
243
			
244
			$keys = array_keys($sub_fields);
245
		
246
			foreach( $keys as $key ) {
247
			
248
				$sub_field = acf_extract_var($sub_fields, $key);
249
				$sub_field['parent'] = $field['ID'];
250
				
251
				_migrate_field_500( $sub_field );
252
				
253
			}
254
			
255
		}
256
		
257
	
258
	} elseif( $field['type'] == 'flexible_content' ) {
259
		
260
		// get layouts
261
		$layouts = acf_extract_var( $orig, 'layouts' );
262
		
263
		
264
		// update layouts
265
		$field['layouts'] = array();
266
		
267
		
268
		// save sub fields
269
		if( !empty($layouts) ) {
270
			
271
			foreach( $layouts as $layout ) {
272
				
273
				// vars
274
				$layout_key = uniqid();
275
				
276
				
277
				// append layotu key
278
				$layout['key'] = $layout_key;
279
				
280
				
281
				// extract sub fields
282
				$sub_fields = acf_extract_var($layout, 'sub_fields');
283
				
284
				
285
				// save sub fields
286 View Code Duplication
				if( !empty($sub_fields) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
					
288
					$keys = array_keys($sub_fields);
289
					
290
					foreach( $keys as $key ) {
291
					
292
						$sub_field = acf_extract_var($sub_fields, $key);
293
						$sub_field['parent'] = $field['ID'];
294
						$sub_field['parent_layout'] = $layout_key;
295
						
296
						_migrate_field_500( $sub_field );
297
						
298
					}
299
					// foreach
300
					
301
				}
302
				// if
303
				
304
				
305
				// append layout
306
				$field['layouts'][] = $layout;
307
			
308
			}
309
			// foreach
310
			
311
		}
312
		// if
313
		
314
		
315
		// save field again with less sub field data
316
		$field = acf_update_field( $field );
0 ignored issues
show
Documentation introduced by
$field is of type array<string,array,{"layouts":"array"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
317
		
318
	}
319
	
320
	
321
	// return
322
	return $field;
323
}
324
325
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
326