Completed
Pull Request — 2.x (#3999)
by
unknown
05:26
created

functions-pod_reference.php ➔ pq_recurse_pod_fields()   C

Complexity

Conditions 15
Paths 41

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 36
nc 41
nop 3
dl 0
loc 55
rs 6.7239
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
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 5 and the first side effect is on line 3.

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
add_action( 'wp_ajax_pq_loadpod', 'pq_loadpod' );
4
5
function pq_loadpod($podname = false) {
6
	if ( !pods_is_admin() ) {
7
		pods_error( __( 'Unauthorized request', 'pods' ) );
8
	}
9
	if(!empty($_POST['pod_reference']['pod'])){
10
		$podname = $_POST['pod_reference']['pod'];
11
	}
12
	if(!empty($_POST['pod'])){
13
		$podname = $_POST['pod'];
14
	}
15
	$fields = array( __( 'No reference Pod selected', 'pods' ) );
16
17
	if(!empty($podname)){
18
		$fields = pq_recurse_pod_fields( $podname );
19
	}
20
	if(!empty($_POST['pod_reference']['pod']) || !empty($_POST['pod'])){
21
		header("Content-Type:application/json");
22
		echo json_encode($fields);
23
		die;
0 ignored issues
show
Coding Style Compatibility introduced by
The function pq_loadpod() 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...
24
	}
25
	return $fields;
26
}
27
28
function pq_recurse_pod_fields( $pod_name, $prefix = '', &$pods_visited = array() ) {
29
	$fields = array();
30
	if ( empty( $pod_name ) ) {
31
		return $fields;
32
	}
33
	$pod = pods( $pod_name );
34
	$recurse_queue = array();
35
36
	foreach( $pod->pod_data['object_fields'] as $name => $field ){
37
		// Add WordPress object fields
38
		$fields[] = $prefix . $name;
39
	}
40
	if ( post_type_supports( $pod_name, 'thumbnail' ) ) {
41
		$fields[] = '{$prefix}post_thumbnail';
42
		$fields[] = '{$prefix}post_thumbnail_url';
43
		$sizes = get_intermediate_image_sizes();
44
		foreach ( $sizes as $size ) {
45
			$fields[] = "{$prefix}post_thumbnail.{$size}";
46
			$fields[] = "{$prefix}post_thumbnail_url.{$size}";
47
		}
48
	}
49
	$pod_fields = $pod->fields();
50
	foreach ( $pod_fields as $name => $field ) {
51
		// Add base field name
52
		$fields[] = $prefix . $name;
53
54
		// Field type specific handling
55
		if ( 'file' === $field['type'] && 'attachment' === $field['options']['file_uploader'] ) {
56
			$fields[] = $prefix . $name . '._src';
57
			$fields[] = $prefix . $name . '._img';
58
59
			$sizes = get_intermediate_image_sizes();
60
			foreach ( $sizes as $size ) {
61
				$fields[] = "{$prefix}{$name}._src.{$size}";
62
				if ( 'multi' != $field['options']['file_format_type'] ) {
63
					$fields[] = "{$prefix}{$name}._src_relative.{$size}";
64
					$fields[] = "{$prefix}{$name}._src_schemeless.{$size}";
65
				}
66
				$fields[] = "{$prefix}{$name}._img.{$size}";
67
			}
68
69
		} elseif ( ! empty( $field['table_info'] ) && ! empty( $field['table_info']['pod'] ) ) {
70
			$linked_pod = $field['table_info']['pod']['name'];
71
			if (  !isset( $pods_visited[ $linked_pod ] ) || !in_array( $name, $pods_visited[ $linked_pod ] ) ) {
72
				$pods_visited[ $linked_pod ][] = $name;
73
				$recurse_queue[ $linked_pod ] = "{$prefix}{$name}.";
74
			}
75
76
		}
77
	}
78
	foreach ( $recurse_queue as $recurse_name => $recurse_prefix ) {
79
		$fields = array_merge( $fields, pq_recurse_pod_fields( $recurse_name, $recurse_prefix, $pods_visited ) );
80
	}
81
	return $fields;
82
}
83
84