Completed
Push — 2.x ( 4ed99b...cdb245 )
by Scott Kingsley
06:09
created

functions-pod_reference.php ➔ pq_loadpod()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 9
eloc 25
nc 24
nop 1
dl 0
loc 34
rs 4.909
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(!empty($_POST['pod_reference']['pod'])){
7
		$podname = $_POST['pod_reference']['pod'];
8
	}
9
	if(!empty($_POST['pod'])){
10
		$podname = $_POST['pod'];
11
12
	}
13
	$fields = array('No reference Pod selected');
14
	if(!empty($podname)){
15
		$pod = pods( $podname );
16
		$fields = array();
17
		foreach( $pod->pod_data['object_fields'] as $name=>$field ){
18
			$fields[] = $name;
19
		}
20
		$pod_fields = $pod->fields();
21
		if( post_type_supports( $podname, 'thumbnail' ) ){
22
			$fields[] = 'post_thumbnail';
23
			$fields[] = 'post_thumbnail_url';
24
			$sizes = get_intermediate_image_sizes();
25
			foreach( $sizes as &$size){
26
				$fields[] = 'post_thumbnail.'.$size;
27
				$fields[] = 'post_thumbnail_url.'.$size;
28
			}
29
		}
30
		$fields = array_merge( $fields, pq_tunnel_pod_field( $pod_fields ) );
31
	}
32
	if(!empty($_POST['pod_reference']['pod']) || !empty($_POST['pod'])){
33
		header("Content-Type:application/json");
34
		echo json_encode($fields);
35
		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...
36
	}
37
	return $fields;
38
}
39
40
function pq_tunnel_pod_field( $fields, $prefix = null ){
41
42
	$out = array();
43
	// return out if fields are empty
44
	if(empty($fields)){
45
		return $out;
46
	}
47
	foreach($fields as $name=>$field){
48
		$out[] = $prefix . $name;
49
		if($field['type'] === 'file' && $field['options']['file_uploader'] == 'attachment'){
50
51
		$out[] = $prefix . $name .'._src';
52
		$out[] = $prefix . $name .'._img';
53
54
			$sizes = get_intermediate_image_sizes();
55
			foreach( $sizes as &$size){
56
				$out[] = $prefix . $name . '._src.'.$size;
57
			}
58
			if( 'multi' != $field['options']['file_format_type']){
59
				foreach( $sizes as &$size){
60
					$out[] = $prefix . $name . '._src_relative.'.$size;
61
				}
62
				foreach( $sizes as &$size){
63
					$out[] = $prefix . $name . '._src_schemeless.'.$size;
64
				}
65
			}
66
			foreach( $sizes as &$size){
67
				$out[] = $prefix . $name . '._img.'.$size;
68
			}
69
		}
70
		if( !empty( $field['table_info'] ) ){
71
			if( !empty( $field['table_info']['pod'] ) ){
72
				if( false === strpos( $prefix, $name . '.' ) ){
73
74
					$pod = pods( $field['table_info']['pod']['name'] );
75
					// only tunnel in if there are object fields
76 View Code Duplication
					if(!empty($field['table_info']['object_fields'])){
77
						$out = array_merge( $out, pq_tunnel_pod_field( $field['table_info']['object_fields'], $prefix . $name . '.' ) );
78
					}
79
					if( post_type_supports( $field['table_info']['pod']['name'], 'thumbnail' ) ){
80
						$out[] = 'post_thumbnail';
81
						$out[] = 'post_thumbnail_url';
82
						$sizes = get_intermediate_image_sizes();
83
						foreach( $sizes as &$size){
84
							$out[] = 'post_thumbnail.'.$size;
85
							$out[] = 'post_thumbnail_url.'.$size;
86
						}
87
					}
88
					$pod_fields = $pod->fields();
89
					$out = array_merge( $out, pq_tunnel_pod_field( $pod_fields, $prefix . $name . '.') );
90
				}
91 View Code Duplication
			}else{
92
				
93
				if(!empty($field['table_info']['object_fields'])){
94
				
95
					$out = array_merge( $out, pq_tunnel_pod_field( $field['table_info']['object_fields'], $prefix . $name . '.') );
96
				
97
				}
98
99
			}
100
		}
101
	}
102
103
	return $out;
104
}
105
106