1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Name: Advanced Relationships |
4
|
|
|
* |
5
|
|
|
* Description: Add advanced relationship objects for relating to including Database Tables, Multisite Networks, |
6
|
|
|
* Multisite Sites, Themes, Page Templates, Sidebars, Post Type Objects, and Taxonomy Objects |
7
|
|
|
* |
8
|
|
|
* Version: 2.3 |
9
|
|
|
* |
10
|
|
|
* Category: Advanced |
11
|
|
|
* |
12
|
|
|
* Tableless Mode: No |
13
|
|
|
* |
14
|
|
|
* @package Pods\Components |
15
|
|
|
* @subpackage Advanced Relationships |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
if ( class_exists( 'Pods_Advanced_Relationships' ) ) { |
19
|
|
|
return; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Pods_Advanced_Relationships |
24
|
|
|
*/ |
25
|
|
|
class Pods_Advanced_Relationships extends PodsComponent { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function init() { |
31
|
|
|
|
32
|
|
|
add_action( 'pods_form_ui_field_pick_related_objects_other', array( $this, 'add_related_objects' ) ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add Advanced Related Objects |
37
|
|
|
* |
38
|
|
|
* @since 2.3 |
39
|
|
|
*/ |
40
|
|
|
public function add_related_objects() { |
41
|
|
|
|
42
|
|
|
PodsField_Pick::$related_objects['table'] = array( |
43
|
|
|
'label' => __( 'Database Tables', 'pods' ), |
44
|
|
|
'group' => __( 'Advanced Objects', 'pods' ), |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if ( is_multisite() ) { |
48
|
|
|
PodsField_Pick::$related_objects['site'] = array( |
49
|
|
|
'label' => __( 'Multisite Sites', 'pods' ), |
50
|
|
|
'group' => __( 'Advanced Objects', 'pods' ), |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
PodsField_Pick::$related_objects['network'] = array( |
54
|
|
|
'label' => __( 'Multisite Networks', 'pods' ), |
55
|
|
|
'group' => __( 'Advanced Objects', 'pods' ), |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
PodsField_Pick::$related_objects['theme'] = array( |
60
|
|
|
'label' => __( 'Themes', 'pods' ), |
61
|
|
|
'group' => __( 'Advanced Objects', 'pods' ), |
62
|
|
|
'simple' => true, |
63
|
|
|
'data_callback' => array( $this, 'data_themes' ), |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
PodsField_Pick::$related_objects['page-template'] = array( |
67
|
|
|
'label' => __( 'Page Templates', 'pods' ), |
68
|
|
|
'group' => __( 'Advanced Objects', 'pods' ), |
69
|
|
|
'simple' => true, |
70
|
|
|
'data_callback' => array( $this, 'data_page_templates' ), |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
PodsField_Pick::$related_objects['sidebar'] = array( |
74
|
|
|
'label' => __( 'Sidebars', 'pods' ), |
75
|
|
|
'group' => __( 'Advanced Objects', 'pods' ), |
76
|
|
|
'simple' => true, |
77
|
|
|
'data_callback' => array( $this, 'data_sidebars' ), |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
PodsField_Pick::$related_objects['post-types'] = array( |
81
|
|
|
'label' => __( 'Post Type Objects', 'pods' ), |
82
|
|
|
'group' => __( 'Advanced Objects', 'pods' ), |
83
|
|
|
'simple' => true, |
84
|
|
|
'data_callback' => array( $this, 'data_post_types' ), |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
PodsField_Pick::$related_objects['taxonomies'] = array( |
88
|
|
|
'label' => __( 'Taxonomy Objects', 'pods' ), |
89
|
|
|
'group' => __( 'Advanced Objects', 'pods' ), |
90
|
|
|
'simple' => true, |
91
|
|
|
'data_callback' => array( $this, 'data_taxonomies' ), |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Data callback for Themes |
97
|
|
|
* |
98
|
|
|
* @param string $name The name of the field |
|
|
|
|
99
|
|
|
* @param string|array $value The value of the field |
|
|
|
|
100
|
|
|
* @param array $options Field options |
|
|
|
|
101
|
|
|
* @param array $pod Pod data |
|
|
|
|
102
|
|
|
* @param int $id Item ID |
|
|
|
|
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
* |
106
|
|
|
* @since 2.3 |
107
|
|
|
*/ |
108
|
|
|
public function data_themes( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
109
|
|
|
|
110
|
|
|
$data = array(); |
111
|
|
|
|
112
|
|
|
$themes = wp_get_themes( array( 'allowed' => true ) ); |
113
|
|
|
|
114
|
|
|
foreach ( $themes as $theme ) { |
115
|
|
|
$data[ $theme->Template ] = $theme->Name; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Data callback for Page Templates |
123
|
|
|
* |
124
|
|
|
* @param string $name The name of the field |
|
|
|
|
125
|
|
|
* @param string|array $value The value of the field |
|
|
|
|
126
|
|
|
* @param array $options Field options |
|
|
|
|
127
|
|
|
* @param array $pod Pod data |
|
|
|
|
128
|
|
|
* @param int $id Item ID |
|
|
|
|
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
* |
132
|
|
|
* @since 2.3 |
133
|
|
|
*/ |
134
|
|
|
public function data_page_templates( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
135
|
|
|
|
136
|
|
|
$data = array(); |
137
|
|
|
|
138
|
|
|
if ( ! function_exists( 'get_page_templates' ) ) { |
139
|
|
|
include_once ABSPATH . 'wp-admin/includes/theme.php'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$page_templates = apply_filters( 'pods_page_templates', get_page_templates() ); |
143
|
|
|
|
144
|
|
|
if ( ! in_array( 'page.php', $page_templates, true ) && locate_template( array( 'page.php', false ) ) ) { |
145
|
|
|
$page_templates['Page (WP Default)'] = 'page.php'; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ( ! in_array( 'index.php', $page_templates, true ) && locate_template( array( 'index.php', false ) ) ) { |
149
|
|
|
$page_templates['Index (WP Fallback)'] = 'index.php'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
ksort( $page_templates ); |
153
|
|
|
|
154
|
|
|
$page_templates = array_flip( $page_templates ); |
155
|
|
|
|
156
|
|
|
foreach ( $page_templates as $page_template_file => $page_template ) { |
157
|
|
|
$data[ $page_template_file ] = $page_template; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Data callback for Sidebars |
165
|
|
|
* |
166
|
|
|
* @param string $name The name of the field |
|
|
|
|
167
|
|
|
* @param string|array $value The value of the field |
|
|
|
|
168
|
|
|
* @param array $options Field options |
|
|
|
|
169
|
|
|
* @param array $pod Pod data |
|
|
|
|
170
|
|
|
* @param int $id Item ID |
|
|
|
|
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
* |
174
|
|
|
* @since 2.3 |
175
|
|
|
*/ |
176
|
|
|
public function data_sidebars( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
177
|
|
|
|
178
|
|
|
$data = array(); |
179
|
|
|
|
180
|
|
|
global $wp_registered_sidebars; |
|
|
|
|
181
|
|
|
|
182
|
|
|
if ( ! empty( $wp_registered_sidebars ) ) { |
183
|
|
|
foreach ( $wp_registered_sidebars as $sidebar ) { |
184
|
|
|
$data[ $sidebar['id'] ] = $sidebar['name']; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Data callback for Post Types |
193
|
|
|
* |
194
|
|
|
* @param string $name The name of the field |
|
|
|
|
195
|
|
|
* @param string|array $value The value of the field |
|
|
|
|
196
|
|
|
* @param array $options Field options |
|
|
|
|
197
|
|
|
* @param array $pod Pod data |
|
|
|
|
198
|
|
|
* @param int $id Item ID |
|
|
|
|
199
|
|
|
* |
200
|
|
|
* @return array |
201
|
|
|
* |
202
|
|
|
* @since 2.3 |
203
|
|
|
*/ |
204
|
|
|
public function data_post_types( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
205
|
|
|
|
206
|
|
|
$data = array(); |
207
|
|
|
|
208
|
|
|
$post_types = get_post_types( array(), 'objects' ); |
209
|
|
|
|
210
|
|
|
$ignore = array( 'revision', 'nav_menu_item' ); |
211
|
|
|
|
212
|
|
|
foreach ( $post_types as $post_type ) { |
213
|
|
|
if ( in_array( $post_type->name, $ignore, true ) || 0 === strpos( $post_type->name, '_pods_' ) ) { |
214
|
|
|
continue; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$data[ $post_type->name ] = $post_type->label; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Data callback for Taxonomies |
225
|
|
|
* |
226
|
|
|
* @param string $name The name of the field |
|
|
|
|
227
|
|
|
* @param string|array $value The value of the field |
|
|
|
|
228
|
|
|
* @param array $options Field options |
|
|
|
|
229
|
|
|
* @param array $pod Pod data |
|
|
|
|
230
|
|
|
* @param int $id Item ID |
|
|
|
|
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
* |
234
|
|
|
* @since 2.3 |
235
|
|
|
*/ |
236
|
|
|
public function data_taxonomies( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
237
|
|
|
|
238
|
|
|
$data = array(); |
239
|
|
|
|
240
|
|
|
$taxonomies = get_taxonomies( array(), 'objects' ); |
241
|
|
|
|
242
|
|
|
$ignore = array( 'nav_menu', 'post_format' ); |
243
|
|
|
|
244
|
|
|
foreach ( $taxonomies as $taxonomy ) { |
245
|
|
|
if ( in_array( $taxonomy->name, $ignore, true ) ) { |
246
|
|
|
continue; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$data[ $taxonomy->name ] = $taxonomy->label; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id ); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
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.