Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PodsField_Pick often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PodsField_Pick, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 5 | class PodsField_Pick extends PodsField { | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Field Type Group | ||
| 9 | * | ||
| 10 | * @var string | ||
| 11 | * @since 2.0 | ||
| 12 | */ | ||
| 13 | public static $group = 'Relationships / Media'; | ||
| 14 | |||
| 15 | /** | ||
| 16 | * Field Type Identifier | ||
| 17 | * | ||
| 18 | * @var string | ||
| 19 | * @since 2.0 | ||
| 20 | */ | ||
| 21 | public static $type = 'pick'; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Field Type Label | ||
| 25 | * | ||
| 26 | * @var string | ||
| 27 | * @since 2.0 | ||
| 28 | */ | ||
| 29 | public static $label = 'Relationship'; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Available Related Objects | ||
| 33 | * | ||
| 34 | * @var array | ||
| 35 | * @since 2.3 | ||
| 36 | */ | ||
| 37 | public static $related_objects = array(); | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Custom Related Objects | ||
| 41 | * | ||
| 42 | * @var array | ||
| 43 | * @since 2.3 | ||
| 44 | */ | ||
| 45 | public static $custom_related_objects = array(); | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Data used during validate / save to avoid extra queries | ||
| 49 | * | ||
| 50 | * @var array | ||
| 51 | * @since 2.3 | ||
| 52 | */ | ||
| 53 | public static $related_data = array(); | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Data used during input method (mainly for autocomplete) | ||
| 57 | * | ||
| 58 | * @var array | ||
| 59 | * @since 2.3 | ||
| 60 | */ | ||
| 61 | public static $field_data = array(); | ||
| 62 | |||
| 63 | /** | ||
| 64 | * API caching for fields that need it during validate/save | ||
| 65 | * | ||
| 66 | * @var \PodsAPI | ||
| 67 | * @since 2.3 | ||
| 68 | */ | ||
| 69 | protected static $api = false; | ||
| 70 | |||
| 71 | /** | ||
| 72 | * Saved array of simple relationship names | ||
| 73 | * | ||
| 74 | * @var array | ||
| 75 | * @since 2.5 | ||
| 76 | */ | ||
| 77 | private static $names_simple = null; | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Saved array of relationship names | ||
| 81 | * | ||
| 82 | * @var array | ||
| 83 | * @since 2.5 | ||
| 84 | */ | ||
| 85 | private static $names_related = null; | ||
| 86 | |||
| 87 | /** | ||
| 88 | * Saved array of bidirectional relationship names | ||
| 89 | * | ||
| 90 | * @var array | ||
| 91 | * @since 2.5 | ||
| 92 | */ | ||
| 93 | private static $names_bidirectional = null; | ||
| 94 | |||
| 95 | /** | ||
| 96 | * Setup related objects list | ||
| 97 | * | ||
| 98 | * @since 2.0 | ||
| 99 | */ | ||
| 100 |     public function __construct () { | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Add admin_init actions | ||
| 106 | * | ||
| 107 | * @since 2.3 | ||
| 108 | */ | ||
| 109 |     public function admin_init () { | ||
| 110 | //--!! Prototype testing only | ||
| 111 | add_action( 'wp_ajax_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) ); | ||
| 112 | add_action( 'wp_ajax_nopriv_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) ); | ||
| 113 | |||
| 114 | // AJAX for Relationship lookups | ||
| 115 | add_action( 'wp_ajax_pods_relationship', array( $this, 'admin_ajax_relationship' ) ); | ||
| 116 | add_action( 'wp_ajax_nopriv_pods_relationship', array( $this, 'admin_ajax_relationship' ) ); | ||
| 117 | } | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Add options and set defaults to | ||
| 121 | * | ||
| 122 | * @return array | ||
| 123 | * | ||
| 124 | * @since 2.0 | ||
| 125 | */ | ||
| 126 |     public function options () { | ||
| 127 | $options = array( | ||
| 128 | self::$type . '_format_type' => array( | ||
| 129 | 'label' => __( 'Selection Type', 'pods' ), | ||
| 130 | 'help' => __( 'help', 'pods' ), | ||
| 131 | 'default' => 'single', | ||
| 132 | 'type' => 'pick', | ||
| 133 | 'data' => array( | ||
| 134 | 'single' => __( 'Single Select', 'pods' ), | ||
| 135 | 'multi' => __( 'Multiple Select', 'pods' ) | ||
| 136 | ), | ||
| 137 | 'dependency' => true | ||
| 138 | ), | ||
| 139 | self::$type . '_format_single' => array( | ||
| 140 | 'label' => __( 'Format', 'pods' ), | ||
| 141 | 'help' => __( 'help', 'pods' ), | ||
| 142 | 'depends-on' => array( self::$type . '_format_type' => 'single' ), | ||
| 143 | 'default' => 'dropdown', | ||
| 144 | 'type' => 'pick', | ||
| 145 | 'data' => apply_filters( | ||
| 146 | 'pods_form_ui_field_pick_format_single_options', | ||
| 147 | array( | ||
| 148 | 'dropdown' => __( 'Drop Down', 'pods' ), | ||
| 149 | 'radio' => __( 'Radio Buttons', 'pods' ), | ||
| 150 | 'autocomplete' => __( 'Autocomplete', 'pods' ), | ||
| 151 | 'flexible' => __( 'Flexible', 'pods' ), | ||
| 152 | ) | ||
| 153 | ), | ||
| 154 | 'dependency' => true | ||
| 155 | ), | ||
| 156 | self::$type . '_format_multi' => array( | ||
| 157 | 'label' => __( 'Format', 'pods' ), | ||
| 158 | 'help' => __( 'help', 'pods' ), | ||
| 159 | 'depends-on' => array( self::$type . '_format_type' => 'multi' ), | ||
| 160 | 'default' => 'checkbox', | ||
| 161 | 'type' => 'pick', | ||
| 162 | 'data' => apply_filters( | ||
| 163 | 'pods_form_ui_field_pick_format_multi_options', | ||
| 164 | array( | ||
| 165 | 'checkbox' => __( 'Checkboxes', 'pods' ), | ||
| 166 | 'multiselect' => __( 'Multi Select', 'pods' ), | ||
| 167 | 'autocomplete' => __( 'Autocomplete', 'pods' ), | ||
| 168 | 'flexible' => __( 'Flexible', 'pods' ), | ||
| 169 | ) | ||
| 170 | ), | ||
| 171 | 'dependency' => true | ||
| 172 | ), | ||
| 173 | self::$type . '_taggable' => array( | ||
| 174 | 'label' => __( 'Taggable', 'pods' ), | ||
| 175 | 'help' => __( 'Allow new values to be inserted when using an Autocomplete field', 'pods' ), | ||
| 176 | 'excludes-on' => array( | ||
| 177 | self::$type . '_format_single' => array( 'dropdown', 'radio' ), | ||
| 178 | self::$type . '_format_multi' => array( 'checkbox', 'multiselect' ), | ||
| 179 | self::$type . '_object' => array_merge( | ||
| 180 | array( 'site', 'network' ), | ||
| 181 | self::simple_objects() | ||
| 182 | ) | ||
| 183 | ), | ||
| 184 | 'type' => 'boolean', | ||
| 185 | 'default' => 0 | ||
| 186 | ), | ||
| 187 | self::$type . '_show_icon' => array( | ||
| 188 | 'label' => __( 'Show Icons', 'pods' ), | ||
| 189 | 'excludes-on' => array( | ||
| 190 | self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ), | ||
| 191 | self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ), | ||
| 192 | self::$type . '_object' => array_merge( | ||
| 193 | array( 'site', 'network' ), | ||
| 194 | self::simple_objects() | ||
| 195 | ) | ||
| 196 | ), | ||
| 197 | 'type' => 'boolean', | ||
| 198 | 'default' => 1 | ||
| 199 | ), | ||
| 200 | self::$type . '_show_edit_link' => array( | ||
| 201 | 'label' => __( 'Show Edit Links', 'pods' ), | ||
| 202 | 'excludes-on' => array( | ||
| 203 | self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ), | ||
| 204 | self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ), | ||
| 205 | self::$type . '_object' => array_merge( | ||
| 206 | array( 'site', 'network' ), | ||
| 207 | self::simple_objects() | ||
| 208 | ) | ||
| 209 | ), | ||
| 210 | 'type' => 'boolean', | ||
| 211 | 'default' => 1 | ||
| 212 | ), | ||
| 213 | self::$type . '_show_view_link' => array( | ||
| 214 | 'label' => __( 'Show View Links', 'pods' ), | ||
| 215 | 'excludes-on' => array( | ||
| 216 | self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ), | ||
| 217 | self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ), | ||
| 218 | self::$type . '_object' => array_merge( | ||
| 219 | array( 'site', 'network' ), | ||
| 220 | self::simple_objects() | ||
| 221 | ) | ||
| 222 | ), | ||
| 223 | 'type' => 'boolean', | ||
| 224 | 'default' => 1 | ||
| 225 | ), | ||
| 226 | self::$type . '_select_text' => array( | ||
| 227 | 'label' => __( 'Default Select Text', 'pods' ), | ||
| 228 | 'help' => __( 'This is the text use for the default "no selection" dropdown item, if empty, it will default to "-- Select One --"', 'pods' ), | ||
| 229 | 'depends-on' => array( | ||
| 230 | self::$type . '_format_type' => 'single', | ||
| 231 | self::$type . '_format_single' => 'dropdown' | ||
| 232 | ), | ||
| 233 | 'default' => '', | ||
| 234 | 'type' => 'text' | ||
| 235 | ), | ||
| 236 | self::$type . '_limit' => array( | ||
| 237 | 'label' => __( 'Selection Limit', 'pods' ), | ||
| 238 | 'help' => __( 'help', 'pods' ), | ||
| 239 | 'depends-on' => array( self::$type . '_format_type' => 'multi' ), | ||
| 240 | 'default' => 0, | ||
| 241 | 'type' => 'number' | ||
| 242 | ), | ||
| 243 | self::$type . '_table_id' => array( | ||
| 244 | 'label' => __( 'Table ID Column', 'pods' ), | ||
| 245 | 'help' => __( 'You must provide the ID column name for the table, this will be used to keep track of the relationship', 'pods' ), | ||
| 246 | 'depends-on' => array( self::$type . '_object' => 'table' ), | ||
| 247 | 'required' => 1, | ||
| 248 | 'default' => '', | ||
| 249 | 'type' => 'text' | ||
| 250 | ), | ||
| 251 | self::$type . '_table_index' => array( | ||
| 252 | 'label' => __( 'Table Index Column', 'pods' ), | ||
| 253 | 'help' => __( 'You must provide the index column name for the table, this may optionally also be the ID column name', 'pods' ), | ||
| 254 | 'depends-on' => array( self::$type . '_object' => 'table' ), | ||
| 255 | 'required' => 1, | ||
| 256 | 'default' => '', | ||
| 257 | 'type' => 'text' | ||
| 258 | ), | ||
| 259 | self::$type . '_display' => array( | ||
| 260 | 'label' => __( 'Display Field in Selection List', 'pods' ), | ||
| 261 |                 'help' => __( 'Provide the name of a field on the related object to reference, example: {@post_title}', 'pods' ), | ||
| 262 | 'excludes-on' => array( | ||
| 263 | self::$type . '_object' => array_merge( | ||
| 264 | array( 'site', 'network' ), | ||
| 265 | self::simple_objects() | ||
| 266 | ) | ||
| 267 | ), | ||
| 268 | 'default' => '', | ||
| 269 | 'type' => 'text' | ||
| 270 | ), | ||
| 271 | self::$type . '_user_role' => array( | ||
| 272 | 'label' => __( 'Limit list to Role(s)', 'pods' ), | ||
| 273 | 'help' => __( 'help', 'pods' ), | ||
| 274 | 'depends-on' => array( self::$type . '_object' => 'user' ), | ||
| 275 | 'default' => '', | ||
| 276 | 'type' => 'pick', | ||
| 277 | 'pick_object' => 'role', | ||
| 278 | 'pick_format_type' => 'multi' | ||
| 279 | ),/* | ||
|  | |||
| 280 | self::$type . '_user_site' => array( | ||
| 281 | 'label' => __( 'Limit list to Site(s)', 'pods' ), | ||
| 282 | 'help' => __( 'help', 'pods' ), | ||
| 283 | 'depends-on' => array( self::$type . '_object' => 'user' ), | ||
| 284 | 'default' => '', | ||
| 285 | 'type' => 'pick', | ||
| 286 | 'pick_object' => 'site', | ||
| 287 | 'pick_format_type' => 'multi' | ||
| 288 | ),*/ | ||
| 289 | self::$type . '_where' => array( | ||
| 290 | 'label' => __( 'Customized <em>WHERE</em>', 'pods' ), | ||
| 291 | 'help' => __( 'help', 'pods' ), | ||
| 292 | 'excludes-on' => array( | ||
| 293 | self::$type . '_object' => array_merge( | ||
| 294 | array( 'site', 'network' ), | ||
| 295 | self::simple_objects() | ||
| 296 | ) | ||
| 297 | ), | ||
| 298 | 'default' => '', | ||
| 299 | 'type' => 'text' | ||
| 300 | ), | ||
| 301 | self::$type . '_orderby' => array( | ||
| 302 | 'label' => __( 'Customized <em>ORDER BY</em>', 'pods' ), | ||
| 303 | 'help' => __( 'help', 'pods' ), | ||
| 304 | 'excludes-on' => array( | ||
| 305 | self::$type . '_object' => array_merge( | ||
| 306 | array( 'site', 'network' ), | ||
| 307 | self::simple_objects() | ||
| 308 | ) | ||
| 309 | ), | ||
| 310 | 'default' => '', | ||
| 311 | 'type' => 'text' | ||
| 312 | ), | ||
| 313 | self::$type . '_groupby' => array( | ||
| 314 | 'label' => __( 'Customized <em>GROUP BY</em>', 'pods' ), | ||
| 315 | 'help' => __( 'help', 'pods' ), | ||
| 316 | 'excludes-on' => array( | ||
| 317 | self::$type . '_object' => array_merge( | ||
| 318 | array( 'site', 'network' ), | ||
| 319 | self::simple_objects() | ||
| 320 | ) | ||
| 321 | ), | ||
| 322 | 'default' => '', | ||
| 323 | 'type' => 'text' | ||
| 324 | ) | ||
| 325 | /*, | ||
| 326 | self::$type . '_size' => array( | ||
| 327 | 'label' => __( 'Field Size', 'pods' ), | ||
| 328 | 'default' => 'medium', | ||
| 329 | 'type' => 'pick', | ||
| 330 | 'data' => array( | ||
| 331 | 'small' => __( 'Small', 'pods' ), | ||
| 332 | 'medium' => __( 'Medium', 'pods' ), | ||
| 333 | 'large' => __( 'Large', 'pods' ) | ||
| 334 | ) | ||
| 335 | )*/ | ||
| 336 | ); | ||
| 337 | |||
| 338 | /*if ( !is_multisite() ) | ||
| 339 | unset( $options[ self::$type . '_user_site' ] );*/ | ||
| 340 | |||
| 341 | return $options; | ||
| 342 | } | ||
| 343 | |||
| 344 | /** | ||
| 345 | * Register a related object | ||
| 346 | * | ||
| 347 | * @param string $name Object name | ||
| 348 | * @param string $label Object label | ||
| 349 | * @param array $options Object options | ||
| 350 | * | ||
| 351 | * @return array|boolean Object array or false if unsuccessful | ||
| 352 | * @since 2.3 | ||
| 353 | */ | ||
| 354 |     public function register_related_object ( $name, $label, $options = null ) { | ||
| 355 | if ( empty( $name ) || empty( $label ) ) | ||
| 356 | return false; | ||
| 357 | |||
| 358 | $related_object = array( | ||
| 359 | 'label' => $label, | ||
| 360 | 'group' => 'Custom Relationships', | ||
| 361 | 'simple' => true, | ||
| 362 | 'bidirectional' => false, | ||
| 363 | 'data' => array(), | ||
| 364 | 'data_callback' => null | ||
| 365 | ); | ||
| 366 | |||
| 367 | $related_object = array_merge( $related_object, $options ); | ||
| 368 | |||
| 369 | self::$custom_related_objects[ $name ] = $related_object; | ||
| 370 | |||
| 371 | return true; | ||
| 372 | } | ||
| 373 | |||
| 374 | /** | ||
| 375 | * Setup related objects | ||
| 376 | * | ||
| 377 | * @param boolean $force Whether to force refresh of related objects | ||
| 378 | * @return bool True when data has been loaded | ||
| 379 | * @since 2.3 | ||
| 380 | */ | ||
| 381 | 	public function setup_related_objects( $force = false ) { | ||
| 382 | |||
| 383 | $new_data_loaded = false; | ||
| 384 | |||
| 385 | 		if ( ! $force && empty( self::$related_objects ) ) { | ||
| 386 | // Only load transient if we aren't forcing a refresh | ||
| 387 | self::$related_objects = pods_transient_get( 'pods_related_objects' ); | ||
| 388 | |||
| 389 | 			if ( false !== self::$related_objects ) { | ||
| 390 | $new_data_loaded = true; | ||
| 391 | } | ||
| 392 | 		} elseif ( $force ) { | ||
| 393 | // If we are rebuilding, make sure we start with a clean slate | ||
| 394 | self::$related_objects = array(); | ||
| 395 | } | ||
| 396 | |||
| 397 | 		if ( empty( self::$related_objects ) ) { | ||
| 398 | // Do a complete build of related_objects | ||
| 399 | $new_data_loaded = true; | ||
| 400 | |||
| 401 | // Custom | ||
| 402 | self::$related_objects['custom-simple'] = array( | ||
| 403 | 'label' => __( 'Simple (custom defined list)', 'pods' ), | ||
| 404 | 'group' => __( 'Custom', 'pods' ), | ||
| 405 | 'simple' => true | ||
| 406 | ); | ||
| 407 | |||
| 408 | // Pods | ||
| 409 | $pod_options = array(); | ||
| 410 | |||
| 411 | // Include PodsMeta if not already included | ||
| 412 | pods_meta(); | ||
| 413 | |||
| 414 | // Advanced Content Types | ||
| 415 | $_pods = PodsMeta::$advanced_content_types; | ||
| 416 | |||
| 417 | View Code Duplication | 			foreach ( $_pods as $pod ) { | |
| 418 | 				$pod_options[ $pod['name'] ] = $pod['label'] . ' (' . $pod['name'] . ')'; | ||
| 419 | } | ||
| 420 | |||
| 421 | // Settings | ||
| 422 | $_pods = PodsMeta::$settings; | ||
| 423 | |||
| 424 | View Code Duplication | 			foreach ( $_pods as $pod ) { | |
| 425 | 				$pod_options[ $pod['name'] ] = $pod['label'] . ' (' . $pod['name'] . ')'; | ||
| 426 | } | ||
| 427 | |||
| 428 | asort( $pod_options ); | ||
| 429 | |||
| 430 | 			foreach ( $pod_options as $pod => $label ) { | ||
| 431 | self::$related_objects[ 'pod-' . $pod ] = array( | ||
| 432 | 'label' => $label, | ||
| 433 | 'group' => __( 'Pods', 'pods' ), | ||
| 434 | 'bidirectional' => true | ||
| 435 | ); | ||
| 436 | } | ||
| 437 | |||
| 438 | // Post Types | ||
| 439 | $post_types = get_post_types(); | ||
| 440 | asort( $post_types ); | ||
| 441 | |||
| 442 | $ignore = array( 'attachment', 'revision', 'nav_menu_item' ); | ||
| 443 | |||
| 444 | 			foreach ( $post_types as $post_type => $label ) { | ||
| 445 | 				if ( in_array( $post_type, $ignore ) || empty( $post_type ) ) { | ||
| 446 | unset( $post_types[ $post_type ] ); | ||
| 447 | |||
| 448 | continue; | ||
| 449 | 				} elseif ( 0 === strpos( $post_type, '_pods_' ) && apply_filters( 'pods_pick_ignore_internal', true ) ) { | ||
| 450 | unset( $post_types[ $post_type ] ); | ||
| 451 | |||
| 452 | continue; | ||
| 453 | } | ||
| 454 | |||
| 455 | $post_type = get_post_type_object( $post_type ); | ||
| 456 | |||
| 457 | self::$related_objects[ 'post_type-' . $post_type->name ] = array( | ||
| 458 | 					'label'         => $post_type->label . ' (' . $post_type->name . ')', | ||
| 459 | 'group' => __( 'Post Types', 'pods' ), | ||
| 460 | 'bidirectional' => true | ||
| 461 | ); | ||
| 462 | } | ||
| 463 | |||
| 464 | // Taxonomies | ||
| 465 | $taxonomies = get_taxonomies(); | ||
| 466 | asort( $taxonomies ); | ||
| 467 | |||
| 468 | $ignore = array( 'nav_menu', 'post_format' ); | ||
| 469 | |||
| 470 | 			foreach ( $taxonomies as $taxonomy => $label ) { | ||
| 471 | /** | ||
| 472 | * Prevent ability to extend core Pods content types. | ||
| 473 | * | ||
| 474 | * @param bool Default is true, when set to false Pods internal content types can not be extended. | ||
| 475 | * | ||
| 476 | * @since 2.3.19 | ||
| 477 | */ | ||
| 478 | $ignore_internal = apply_filters( 'pods_pick_ignore_internal', true ); | ||
| 479 | |||
| 480 | 				if ( in_array( $taxonomy, $ignore ) || empty( $taxonomy ) ) { | ||
| 481 | unset( $taxonomies[ $taxonomy ] ); | ||
| 482 | |||
| 483 | continue; | ||
| 484 | 				} elseif ( 0 === strpos( $taxonomy, '_pods_' ) && $ignore_internal ) { | ||
| 485 | unset( $taxonomies[ $taxonomy ] ); | ||
| 486 | |||
| 487 | continue; | ||
| 488 | } | ||
| 489 | |||
| 490 | $taxonomy = get_taxonomy( $taxonomy ); | ||
| 491 | |||
| 492 | self::$related_objects[ 'taxonomy-' . $taxonomy->name ] = array( | ||
| 493 | 					'label'         => $taxonomy->label . ' (' . $taxonomy->name . ')', | ||
| 494 | 'group' => __( 'Taxonomies', 'pods' ), | ||
| 495 | 'bidirectional' => true | ||
| 496 | ); | ||
| 497 | } | ||
| 498 | |||
| 499 | // Other WP Objects | ||
| 500 | self::$related_objects['user'] = array( | ||
| 501 | 'label' => __( 'Users', 'pods' ), | ||
| 502 | 'group' => __( 'Other WP Objects', 'pods' ), | ||
| 503 | 'bidirectional' => true | ||
| 504 | ); | ||
| 505 | |||
| 506 | self::$related_objects['role'] = array( | ||
| 507 | 'label' => __( 'User Roles', 'pods' ), | ||
| 508 | 'group' => __( 'Other WP Objects', 'pods' ), | ||
| 509 | 'simple' => true, | ||
| 510 | 'data_callback' => array( $this, 'data_roles' ) | ||
| 511 | ); | ||
| 512 | |||
| 513 | self::$related_objects['capability'] = array( | ||
| 514 | 'label' => __( 'User Capabilities', 'pods' ), | ||
| 515 | 'group' => __( 'Other WP Objects', 'pods' ), | ||
| 516 | 'simple' => true, | ||
| 517 | 'data_callback' => array( $this, 'data_capabilities' ) | ||
| 518 | ); | ||
| 519 | |||
| 520 | self::$related_objects['media'] = array( | ||
| 521 | 'label' => __( 'Media', 'pods' ), | ||
| 522 | 'group' => __( 'Other WP Objects', 'pods' ), | ||
| 523 | 'bidirectional' => true | ||
| 524 | ); | ||
| 525 | |||
| 526 | self::$related_objects['comment'] = array( | ||
| 527 | 'label' => __( 'Comments', 'pods' ), | ||
| 528 | 'group' => __( 'Other WP Objects', 'pods' ), | ||
| 529 | 'bidirectional' => true | ||
| 530 | ); | ||
| 531 | |||
| 532 | self::$related_objects['image-size'] = array( | ||
| 533 | 'label' => __( 'Image Sizes', 'pods' ), | ||
| 534 | 'group' => __( 'Other WP Objects', 'pods' ), | ||
| 535 | 'simple' => true, | ||
| 536 | 'data_callback' => array( $this, 'data_image_sizes' ) | ||
| 537 | ); | ||
| 538 | |||
| 539 | self::$related_objects['nav_menu'] = array( | ||
| 540 | 'label' => __( 'Navigation Menus', 'pods' ), | ||
| 541 | 'group' => __( 'Other WP Objects', 'pods' ) | ||
| 542 | ); | ||
| 543 | |||
| 544 | self::$related_objects['post_format'] = array( | ||
| 545 | 'label' => __( 'Post Formats', 'pods' ), | ||
| 546 | 'group' => __( 'Other WP Objects', 'pods' ) | ||
| 547 | ); | ||
| 548 | |||
| 549 | self::$related_objects['post-status'] = array( | ||
| 550 | 'label' => __( 'Post Status', 'pods' ), | ||
| 551 | 'group' => __( 'Other WP Objects', 'pods' ), | ||
| 552 | 'simple' => true, | ||
| 553 | 'data_callback' => array( $this, 'data_post_stati' ) | ||
| 554 | ); | ||
| 555 | |||
| 556 | do_action( 'pods_form_ui_field_pick_related_objects_other' ); | ||
| 557 | |||
| 558 | self::$related_objects['country'] = array( | ||
| 559 | 'label' => __( 'Countries', 'pods' ), | ||
| 560 | 'group' => __( 'Predefined Lists', 'pods' ), | ||
| 561 | 'simple' => true, | ||
| 562 | 'data_callback' => array( $this, 'data_countries' ) | ||
| 563 | ); | ||
| 564 | |||
| 565 | self::$related_objects['us_state'] = array( | ||
| 566 | 'label' => __( 'US States', 'pods' ), | ||
| 567 | 'group' => __( 'Predefined Lists', 'pods' ), | ||
| 568 | 'simple' => true, | ||
| 569 | 'data_callback' => array( $this, 'data_us_states' ) | ||
| 570 | ); | ||
| 571 | |||
| 572 | self::$related_objects['days_of_week'] = array( | ||
| 573 | 'label' => __( 'Calendar - Days of Week', 'pods' ), | ||
| 574 | 'group' => __( 'Predefined Lists', 'pods' ), | ||
| 575 | 'simple' => true, | ||
| 576 | 'data_callback' => array( $this, 'data_days_of_week' ) | ||
| 577 | ); | ||
| 578 | |||
| 579 | self::$related_objects['months_of_year'] = array( | ||
| 580 | 'label' => __( 'Calendar - Months of Year', 'pods' ), | ||
| 581 | 'group' => __( 'Predefined Lists', 'pods' ), | ||
| 582 | 'simple' => true, | ||
| 583 | 'data_callback' => array( $this, 'data_months_of_year' ) | ||
| 584 | ); | ||
| 585 | |||
| 586 | do_action( 'pods_form_ui_field_pick_related_objects_predefined' ); | ||
| 587 | |||
| 588 | 			if ( did_action( 'init' ) ) { | ||
| 589 | pods_transient_set( 'pods_related_objects', self::$related_objects ); | ||
| 590 | } | ||
| 591 | } | ||
| 592 | |||
| 593 | /** | ||
| 594 | * Allow custom related objects to be defined | ||
| 595 | */ | ||
| 596 | do_action( 'pods_form_ui_field_pick_related_objects_custom' ); | ||
| 597 | |||
| 598 | 		foreach ( self::$custom_related_objects as $object => $related_object ) { | ||
| 599 | 			if ( ! isset( self::$related_objects[ $object ] ) ) { | ||
| 600 | $new_data_loaded = true; | ||
| 601 | |||
| 602 | self::$related_objects[ $object ] = $related_object; | ||
| 603 | } | ||
| 604 | } | ||
| 605 | |||
| 606 | return $new_data_loaded; | ||
| 607 | |||
| 608 | } | ||
| 609 | |||
| 610 | /** | ||
| 611 | * Return available related objects | ||
| 612 | * | ||
| 613 | * @param boolean $force Whether to force refresh of related objects | ||
| 614 | * | ||
| 615 | * @return array Field selection array | ||
| 616 | * @since 2.3 | ||
| 617 | */ | ||
| 618 |     public function related_objects ( $force = false ) { | ||
| 619 |         if ( $this->setup_related_objects( $force ) || null === self::$names_related ) { | ||
| 620 | $related_objects = array(); | ||
| 621 | |||
| 622 | 	        foreach ( self::$related_objects as $related_object_name => $related_object ) { | ||
| 623 | 		        if ( ! isset( $related_objects[ $related_object[ 'group' ] ] ) ) { | ||
| 624 | $related_objects[ $related_object[ 'group' ] ] = array(); | ||
| 625 | } | ||
| 626 | |||
| 627 | $related_objects[ $related_object[ 'group' ] ][ $related_object_name ] = $related_object[ 'label' ]; | ||
| 628 | } | ||
| 629 | |||
| 630 | self::$names_related = (array) apply_filters( 'pods_form_ui_field_pick_related_objects', $related_objects ); | ||
| 631 | } | ||
| 632 | |||
| 633 | return self::$names_related; | ||
| 634 | } | ||
| 635 | |||
| 636 | /** | ||
| 637 | * Return available simple object names | ||
| 638 | * | ||
| 639 | * @return array Simple object names | ||
| 640 | * @since 2.3 | ||
| 641 | */ | ||
| 642 | View Code Duplication |     public function simple_objects () { | |
| 643 | 		if ( $this->setup_related_objects() || null === self::$names_simple ) { | ||
| 644 | $simple_objects = array(); | ||
| 645 | |||
| 646 | 			foreach ( self::$related_objects as $object => $related_object ) { | ||
| 647 | if ( !isset( $related_object[ 'simple' ] ) || !$related_object[ 'simple' ] ) | ||
| 648 | continue; | ||
| 649 | |||
| 650 | $simple_objects[] = $object; | ||
| 651 | } | ||
| 652 | |||
| 653 | self::$names_simple = (array) apply_filters( 'pods_form_ui_field_pick_simple_objects', $simple_objects ); | ||
| 654 | } | ||
| 655 | |||
| 656 | return self::$names_simple; | ||
| 657 | } | ||
| 658 | |||
| 659 | /** | ||
| 660 | * Return available bidirectional object names | ||
| 661 | * | ||
| 662 | * @return array Bidirectional object names | ||
| 663 | * @since 2.3.4 | ||
| 664 | */ | ||
| 665 | View Code Duplication |     public function bidirectional_objects () { | |
| 666 |         if ( $this->setup_related_objects() || null === self::$names_bidirectional ) { | ||
| 667 | $bidirectional_objects = array(); | ||
| 668 | |||
| 669 | 	        foreach ( self::$related_objects as $object => $related_object ) { | ||
| 670 | if ( !isset( $related_object[ 'bidirectional' ] ) || !$related_object[ 'bidirectional' ] ) | ||
| 671 | continue; | ||
| 672 | |||
| 673 | $bidirectional_objects[] = $object; | ||
| 674 | } | ||
| 675 | |||
| 676 | self::$names_bidirectional = (array) apply_filters( 'pods_form_ui_field_pick_bidirectional_objects', $bidirectional_objects ); | ||
| 677 | } | ||
| 678 | |||
| 679 | return self::$names_bidirectional; | ||
| 680 | } | ||
| 681 | |||
| 682 | /** | ||
| 683 | * Define the current field's schema for DB table storage | ||
| 684 | * | ||
| 685 | * @param array $options | ||
| 686 | * | ||
| 687 | * @return array | ||
| 688 | * @since 2.0 | ||
| 689 | */ | ||
| 690 |     public function schema ( $options = null ) { | ||
| 691 | $schema = false; | ||
| 692 | |||
| 693 | $simple_tableless_objects = $this->simple_objects(); | ||
| 694 | |||
| 695 | if ( in_array( pods_var( self::$type . '_object', $options ), $simple_tableless_objects ) ) | ||
| 696 | $schema = 'LONGTEXT'; | ||
| 697 | |||
| 698 | return $schema; | ||
| 699 | } | ||
| 700 | |||
| 701 | /** | ||
| 702 | * Change the way the value of the field is displayed with Pods::get | ||
| 703 | * | ||
| 704 | * @param mixed $value | ||
| 705 | * @param string $name | ||
| 706 | * @param array $options | ||
| 707 | * @param array $fields | ||
| 708 | * @param array $pod | ||
| 709 | * @param int $id | ||
| 710 | * | ||
| 711 | * @since 2.0 | ||
| 712 | */ | ||
| 713 |     public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { | ||
| 714 | $fields = null; | ||
| 715 | |||
| 716 |         if ( is_object( $pod ) && isset( $pod->fields ) ) { | ||
| 717 | $fields = $pod->fields; | ||
| 718 | |||
| 719 | 	        if ( ! empty( $pod->pod_data[ 'object_fields' ] ) ) { | ||
| 720 | $fields = array_merge( $fields, $pod->pod_data[ 'object_fields' ] ); | ||
| 721 | } | ||
| 722 | View Code Duplication |         } elseif ( is_array( $pod ) && isset( $pod[ 'fields' ] ) ) { | |
| 723 | $fields = $pod[ 'fields' ]; | ||
| 724 | |||
| 725 | 	        if ( ! empty( $pod[ 'object_fields' ] ) ) { | ||
| 726 | $fields = array_merge( $fields, $pod[ 'object_fields' ] ); | ||
| 727 | } | ||
| 728 | } | ||
| 729 | |||
| 730 | return pods_serial_comma( $value, array( 'field' => $name, 'fields' => $fields ) ); | ||
| 731 | } | ||
| 732 | |||
| 733 | /** | ||
| 734 | * Customize output of the form field | ||
| 735 | * | ||
| 736 | * @param string $name | ||
| 737 | * @param mixed $value | ||
| 738 | * @param array $options | ||
| 739 | * @param array $pod | ||
| 740 | * @param int $id | ||
| 741 | * | ||
| 742 | * @since 2.0 | ||
| 743 | */ | ||
| 744 |     public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 745 | global $wpdb; | ||
| 746 | |||
| 747 | $options = (array) $options; | ||
| 748 | $form_field_type = PodsForm::$field_type; | ||
| 749 | |||
| 750 | $options[ 'grouped' ] = 1; | ||
| 751 | |||
| 752 | $options[ 'table_info' ] = array(); | ||
| 753 | |||
| 754 | $custom = pods_var_raw( self::$type . '_custom', $options, false ); | ||
| 755 | |||
| 756 | $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id ); | ||
| 757 | |||
| 758 | $ajax = false; | ||
| 759 | |||
| 760 | if ( ( 'custom-simple' != pods_var( self::$type . '_object', $options ) || empty( $custom ) ) && '' != pods_var( self::$type . '_object', $options, '', null, true ) ) | ||
| 761 | $ajax = true; | ||
| 762 | |||
| 763 |         if ( !empty( self::$field_data ) && self::$field_data[ 'id' ] == $options[ 'id' ] ) { | ||
| 764 | $ajax = (boolean) self::$field_data[ 'autocomplete' ]; | ||
| 765 | } | ||
| 766 | |||
| 767 | $ajax = apply_filters( 'pods_form_ui_field_pick_ajax', $ajax, $name, $value, $options, $pod, $id ); | ||
| 768 | |||
| 769 | if ( 0 == pods_var( self::$type . '_ajax', $options, 1 ) ) | ||
| 770 | $ajax = false; | ||
| 771 | |||
| 772 | $format_type = pods_v( self::$type . '_format_type', $options, 'single' ); | ||
| 773 | |||
| 774 |         if ( 'single' == $format_type ) { | ||
| 775 | View Code Duplication | if ( 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) | |
| 776 | $field_type = 'select'; | ||
| 777 | elseif ( 'radio' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) | ||
| 778 | $field_type = 'radio'; | ||
| 779 | elseif ( 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) | ||
| 780 | $field_type = 'select2'; | ||
| 781 | elseif ( 'flexible' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) | ||
| 782 | $field_type = 'flexible'; | ||
| 783 |             else { | ||
| 784 | // Support custom integration | ||
| 785 | do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_single', $options, 'dropdown' ), $name, $value, $options, $pod, $id ); | ||
| 786 | do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id ); | ||
| 787 | return; | ||
| 788 | } | ||
| 789 | } | ||
| 790 |         elseif ( 'multi' == $format_type ) { | ||
| 791 | if ( !empty( $value ) && !is_array( $value ) ) | ||
| 792 | $value = explode( ',', $value ); | ||
| 793 | |||
| 794 | View Code Duplication | if ( 'checkbox' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) | |
| 795 | $field_type = 'checkbox'; | ||
| 796 | elseif ( 'multiselect' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) | ||
| 797 | $field_type = 'select'; | ||
| 798 | elseif ( 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) | ||
| 799 | $field_type = 'select2'; | ||
| 800 | elseif ( 'flexible' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) | ||
| 801 | $field_type = 'flexible'; | ||
| 802 |             else { | ||
| 803 | // Support custom integration | ||
| 804 | do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_multi', $options, 'checkbox' ), $name, $value, $options, $pod, $id ); | ||
| 805 | do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id ); | ||
| 806 | return; | ||
| 807 | } | ||
| 808 | } | ||
| 809 |         else { | ||
| 810 | // Support custom integration | ||
| 811 | do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id ); | ||
| 812 | return; | ||
| 813 | } | ||
| 814 | |||
| 815 | // Flexible relationships only support certain related objects | ||
| 816 | 	    if ( 'flexible' == $field_type || ( 'select2' == $field_type && 1 == pods_v( self::$type . '_taggable', $options, 0 ) ) ) { | ||
| 817 | $pick_object = pods_v( 'pick_object', $options ); | ||
| 818 | |||
| 819 | 		    if ( ! in_array( $pick_object, array( 'post_type', 'taxonomy', 'user', 'pod' ) ) ) { | ||
| 820 | // Default all others to Select2 | ||
| 821 | $field_type = 'select2'; | ||
| 822 | 		    } else { | ||
| 823 | // @todo: Fix location when merging | ||
| 824 | $field_type = 'pick'; | ||
| 825 | pods_view( PODS_DIR . 'ui/fields-mv/pick.php', compact( array_keys( get_defined_vars() ) ) ); | ||
| 826 | return; | ||
| 827 | } | ||
| 828 | } | ||
| 829 | |||
| 830 | pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); | ||
| 831 | } | ||
| 832 | |||
| 833 | /** | ||
| 834 | * Validate a value before it's saved | ||
| 835 | * | ||
| 836 | * @param mixed $value | ||
| 837 | * @param string $name | ||
| 838 | * @param array $options | ||
| 839 | * @param array $fields | ||
| 840 | * @param array $pod | ||
| 841 | * @param int $id | ||
| 842 | * | ||
| 843 | * @param null $params | ||
| 844 | * @return array|bool | ||
| 845 | * @since 2.0 | ||
| 846 | */ | ||
| 847 |     public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { | ||
| 848 | if ( empty( self::$api ) ) | ||
| 849 | self::$api = pods_api(); | ||
| 850 | |||
| 851 | $simple_tableless_objects = $this->simple_objects(); | ||
| 852 | |||
| 853 | $related_pick_limit = 0; | ||
| 854 | $related_field = $related_pod = $current_related_ids = false; | ||
| 855 | |||
| 856 | // Bidirectional relationship requirement checks | ||
| 857 | $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc.. | ||
| 858 | $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc.. | ||
| 859 |         if ( empty( $related_val ) ) { | ||
| 860 | $related_val = $related_object; | ||
| 861 | } | ||
| 862 | |||
| 863 | $related_sister_id = (int) pods_var( 'sister_id', $options, 0 ); | ||
| 864 | |||
| 865 | $options[ 'id' ] = (int) $options[ 'id' ]; | ||
| 866 | |||
| 867 | if ( !isset( self::$related_data[ $options[ 'id' ] ] ) || empty( self::$related_data[ $options[ 'id' ] ] ) ) | ||
| 868 | self::$related_data[ $options[ 'id' ] ] = array(); | ||
| 869 | |||
| 870 |         if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) { | ||
| 871 | $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false ); | ||
| 872 | |||
| 873 |             if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) { | ||
| 874 | $related_field = false; | ||
| 875 | |||
| 876 | // Ensure sister_id exists on related Pod | ||
| 877 | View Code Duplication |                 foreach ( $related_pod[ 'fields' ] as $related_pod_field ) { | |
| 878 |                     if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) { | ||
| 879 | $related_field = $related_pod_field; | ||
| 880 | |||
| 881 | break; | ||
| 882 | } | ||
| 883 | } | ||
| 884 | |||
| 885 |                 if ( !empty( $related_field ) ) { | ||
| 886 | $current_ids = self::$api->lookup_related_items( $fields[ $name ][ 'id' ], $pod[ 'id' ], $id, $fields[ $name ], $pod ); | ||
| 887 | |||
| 888 | self::$related_data[ $options[ 'id' ] ][ 'current_ids' ] = $current_ids; | ||
| 889 | |||
| 890 | $value_ids = $value; | ||
| 891 | |||
| 892 | // Convert values from a comma-separated string into an array | ||
| 893 | if ( !is_array( $value_ids ) ) | ||
| 894 | $value_ids = explode( ',', $value_ids ); | ||
| 895 | |||
| 896 | $value_ids = array_unique( array_filter( $value_ids ) ); | ||
| 897 | |||
| 898 | // Get ids to remove | ||
| 899 | $remove_ids = array_diff( $current_ids, $value_ids ); | ||
| 900 | |||
| 901 | $related_required = (boolean) pods_var( 'required', $related_field[ 'options' ], 0 ); | ||
| 902 | $related_pick_limit = (int) pods_var( self::$type . '_limit', $related_field[ 'options' ], 0 ); | ||
| 903 | |||
| 904 | if ( 'single' == pods_var_raw( self::$type . '_format_type', $related_field[ 'options' ] ) ) | ||
| 905 | $related_pick_limit = 1; | ||
| 906 | |||
| 907 | // Validate Required | ||
| 908 |                     if ( $related_required && !empty( $remove_ids ) ) { | ||
| 909 |                         foreach ( $remove_ids as $related_id ) { | ||
| 910 | $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod ); | ||
| 911 | |||
| 912 | self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] = $bidirectional_ids; | ||
| 913 | |||
| 914 | if ( empty( $bidirectional_ids ) || ( in_array( $id, $bidirectional_ids ) && 1 == count( $bidirectional_ids ) ) ) | ||
| 915 | return sprintf( __( 'The %s field is required and cannot be removed by the %s field', 'pods' ), $related_field[ 'label' ], $options[ 'label' ] ); | ||
| 916 | } | ||
| 917 | } | ||
| 918 | } | ||
| 919 | else | ||
| 920 | $related_pod = false; | ||
| 921 | } | ||
| 922 | else | ||
| 923 | $related_pod = false; | ||
| 924 | } | ||
| 925 | |||
| 926 | if ( empty( self::$related_data[ $options[ 'id' ] ] ) ) | ||
| 927 | unset( self::$related_data[ $options[ 'id' ] ] ); | ||
| 928 |         else { | ||
| 929 | self::$related_data[ $options[ 'id' ] ][ 'related_pod' ] = $related_pod; | ||
| 930 | self::$related_data[ $options[ 'id' ] ][ 'related_field' ] = $related_field; | ||
| 931 | self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ] = $related_pick_limit; | ||
| 932 | |||
| 933 | $pick_limit = (int) pods_var( self::$type . '_limit', $options[ 'options' ], 0 ); | ||
| 934 | |||
| 935 | if ( 'single' == pods_var_raw( self::$type . '_format_type', $options[ 'options' ] ) ) | ||
| 936 | $pick_limit = 1; | ||
| 937 | |||
| 938 | $related_field[ 'id' ] = (int) $related_field[ 'id' ]; | ||
| 939 | |||
| 940 |             if ( !isset( self::$related_data[ $related_field[ 'id' ] ] ) || empty( self::$related_data[ $related_field[ 'id' ] ] ) ) { | ||
| 941 | self::$related_data[ $related_field[ 'id' ] ] = array( | ||
| 942 | 'related_pod' => $pod, | ||
| 943 | 'related_field' => $options, | ||
| 944 | 'related_pick_limit' => $pick_limit | ||
| 945 | ); | ||
| 946 | } | ||
| 947 | } | ||
| 948 | |||
| 949 | return true; | ||
| 950 | } | ||
| 951 | |||
| 952 | /** | ||
| 953 | * Save the value to the DB | ||
| 954 | * | ||
| 955 | * @param mixed $value | ||
| 956 | * @param int $id | ||
| 957 | * @param string $name | ||
| 958 | * @param array $options | ||
| 959 | * @param array $fields | ||
| 960 | * @param array $pod | ||
| 961 | * @param object $params | ||
| 962 | * | ||
| 963 | * @since 2.3 | ||
| 964 | */ | ||
| 965 |     public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { | ||
| 966 | if ( empty( self::$api ) ) | ||
| 967 | self::$api = pods_api(); | ||
| 968 | |||
| 969 | $options[ 'id' ] = (int) $options[ 'id' ]; | ||
| 970 | |||
| 971 | if ( !isset( self::$related_data[ $options[ 'id' ] ] ) ) | ||
| 972 | return; | ||
| 973 | |||
| 974 | $related_pod = self::$related_data[ $options[ 'id' ] ][ 'related_pod' ]; | ||
| 975 | $related_field = self::$related_data[ $options[ 'id' ] ][ 'related_field' ]; | ||
| 976 | $related_pick_limit = self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ]; | ||
| 977 | |||
| 978 | // Bidirectional relationship updates | ||
| 979 |         if ( !empty( $related_field ) ) { | ||
| 980 | // Don't use no conflict mode unless this isn't the current pod type | ||
| 981 | $no_conflict = true; | ||
| 982 | |||
| 983 | if ( $related_pod[ 'type' ] != $pod[ 'type' ] ) | ||
| 984 | $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] ); | ||
| 985 | |||
| 986 | if ( !$no_conflict ) | ||
| 987 | pods_no_conflict_on( $related_pod[ 'type' ] ); | ||
| 988 | |||
| 989 | $value = array_filter( $value ); | ||
| 990 | |||
| 991 |             foreach ( $value as $related_id ) { | ||
| 992 | if ( isset( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) && !empty( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) ) | ||
| 993 | $bidirectional_ids = self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ]; | ||
| 994 | else | ||
| 995 | $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod ); | ||
| 996 | |||
| 997 | $bidirectional_ids = array_filter( $bidirectional_ids ); | ||
| 998 | |||
| 999 | if ( empty( $bidirectional_ids ) ) | ||
| 1000 | $bidirectional_ids = array(); | ||
| 1001 | |||
| 1002 | $remove_ids = array(); | ||
| 1003 | |||
| 1004 |                 if ( 0 < $related_pick_limit && !empty( $bidirectional_ids ) && !in_array( $id, $bidirectional_ids ) ) { | ||
| 1005 |                     while ( $related_pick_limit <= count( $bidirectional_ids ) ) { | ||
| 1006 | $remove_ids[] = (int) array_pop( $bidirectional_ids ); | ||
| 1007 | } | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | // Remove this item from related items no longer related to | ||
| 1011 | $remove_ids = array_unique( array_filter( $remove_ids ) ); | ||
| 1012 | |||
| 1013 | // Add to related items | ||
| 1014 | if ( !in_array( $id, $bidirectional_ids ) ) | ||
| 1015 | $bidirectional_ids[] = $id; | ||
| 1016 | // Nothing to change | ||
| 1017 | elseif ( empty( $remove_ids ) ) | ||
| 1018 | continue; | ||
| 1019 | |||
| 1020 | self::$api->save_relationships( $related_id, $bidirectional_ids, $related_pod, $related_field ); | ||
| 1021 | |||
| 1022 | if ( !empty( $remove_ids ) ) | ||
| 1023 | self::$api->delete_relationships( $remove_ids, $related_id, $pod, $options ); | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | if ( !$no_conflict ) | ||
| 1027 | pods_no_conflict_off( $related_pod[ 'type' ] ); | ||
| 1028 | } | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | /** | ||
| 1032 | * Delete the value from the DB | ||
| 1033 | * | ||
| 1034 | * @param int $id | ||
| 1035 | * @param string $name | ||
| 1036 | * @param array $options | ||
| 1037 | * @param array $pod | ||
| 1038 | * | ||
| 1039 | * @since 2.3 | ||
| 1040 | */ | ||
| 1041 |     public function delete ( $id = null, $name = null, $options = null, $pod = null ) { | ||
| 1042 | if ( empty( self::$api ) ) | ||
| 1043 | self::$api = pods_api(); | ||
| 1044 | |||
| 1045 | $simple_tableless_objects = $this->simple_objects(); | ||
| 1046 | |||
| 1047 | // Bidirectional relationship requirement checks | ||
| 1048 | $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc.. | ||
| 1049 | $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc.. | ||
| 1050 | $related_sister_id = (int) pods_var( 'sister_id', $options, 0 ); | ||
| 1051 | |||
| 1052 |         if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) { | ||
| 1053 | $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false ); | ||
| 1054 | |||
| 1055 |             if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) { | ||
| 1056 | $related_field = false; | ||
| 1057 | |||
| 1058 | // Ensure sister_id exists on related Pod | ||
| 1059 | View Code Duplication |                 foreach ( $related_pod[ 'fields' ] as $related_pod_field ) { | |
| 1060 |                     if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) { | ||
| 1061 | $related_field = $related_pod_field; | ||
| 1062 | |||
| 1063 | break; | ||
| 1064 | } | ||
| 1065 | } | ||
| 1066 | |||
| 1067 |                 if ( !empty( $related_field ) ) { | ||
| 1068 | $values = self::$api->lookup_related_items( $options[ 'id' ], $pod[ 'id' ], $id, $options, $pod ); | ||
| 1069 | |||
| 1070 |                     if ( !empty( $values ) ) { | ||
| 1071 | $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] ); | ||
| 1072 | |||
| 1073 | if ( !$no_conflict ) | ||
| 1074 | pods_no_conflict_on( $related_pod[ 'type' ] ); | ||
| 1075 | |||
| 1076 | self::$api->delete_relationships( $values, $id, $related_pod, $related_field ); | ||
| 1077 | |||
| 1078 | if ( !$no_conflict ) | ||
| 1079 | pods_no_conflict_off( $related_pod[ 'type' ] ); | ||
| 1080 | } | ||
| 1081 | } | ||
| 1082 | } | ||
| 1083 | } | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | /** | ||
| 1087 | * Customize the Pods UI manage table column output | ||
| 1088 | * | ||
| 1089 | * @param int $id | ||
| 1090 | * @param mixed $value | ||
| 1091 | * @param string $name | ||
| 1092 | * @param array $options | ||
| 1093 | * @param array $fields | ||
| 1094 | * @param array $pod | ||
| 1095 | * | ||
| 1096 | * @since 2.0 | ||
| 1097 | */ | ||
| 1098 |     public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { | ||
| 1099 | $value = $this->simple_value( $name, $value, $options, $pod, $id ); | ||
| 1100 | |||
| 1101 | return $this->display( $value, $name, $options, $pod, $id ); | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | /** | ||
| 1105 | * Get the data from the field | ||
| 1106 | * | ||
| 1107 | * @param string $name The name of the field | ||
| 1108 | * @param string|array $value The value of the field | ||
| 1109 | * @param array $options Field options | ||
| 1110 | * @param array $pod Pod data | ||
| 1111 | * @param int $id Item ID | ||
| 1112 | * @param boolean $in_form | ||
| 1113 | * | ||
| 1114 | * @return array Array of possible field data | ||
| 1115 | * | ||
| 1116 | * @since 2.0 | ||
| 1117 | */ | ||
| 1118 |     public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) { | ||
| 1119 | View Code Duplication |         if ( isset( $options[ 'options' ] ) ) { | |
| 1120 | $options = array_merge( $options, $options[ 'options' ] ); | ||
| 1121 | |||
| 1122 | unset( $options[ 'options' ] ); | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | $data = pods_var_raw( 'data', $options, null, null, true ); | ||
| 1126 | |||
| 1127 | $object_params = array( | ||
| 1128 | 'name' => $name, // The name of the field | ||
| 1129 | 'value' => $value, // The value of the field | ||
| 1130 | 'options' => $options, // Field options | ||
| 1131 | 'pod' => $pod, // Pod data | ||
| 1132 | 'id' => $id, // Item ID | ||
| 1133 | 'context' => 'data', // Data context | ||
| 1134 | ); | ||
| 1135 | |||
| 1136 | if ( null !== $data ) | ||
| 1137 | $data = (array) $data; | ||
| 1138 | else | ||
| 1139 | $data = $this->get_object_data( $object_params ); | ||
| 1140 | |||
| 1141 | if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) | ||
| 1142 | $data = array( '' => pods_var_raw( self::$type . '_select_text', $options, __( '-- Select One --', 'pods' ), null, true ) ) + $data; | ||
| 1143 | |||
| 1144 | $data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id ); | ||
| 1145 | |||
| 1146 | return $data; | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | /** | ||
| 1150 | * Convert a simple value to the correct value | ||
| 1151 | * | ||
| 1152 | * @param string $name The name of the field | ||
| 1153 | * @param string|array $value The value of the field | ||
| 1154 | * @param array $options Field options | ||
| 1155 | * @param array $pod Pod data | ||
| 1156 | * @param int $id Item ID | ||
| 1157 | * @param boolean $raw Whether to return the raw list of keys (true) or convert to key=>value (false) | ||
| 1158 | * | ||
| 1159 | * @return mixed Corrected value | ||
| 1160 | */ | ||
| 1161 |     public function simple_value ( $name, $value = null, $options = null, $pod = null, $id = null, $raw = false ) { | ||
| 1162 |         if ( in_array( pods_var( self::$type . '_object', $options ), self::simple_objects() ) ) { | ||
| 1163 | View Code Duplication |             if ( isset( $options[ 'options' ] ) ) { | |
| 1164 | $options = array_merge( $options, $options[ 'options' ] ); | ||
| 1165 | |||
| 1166 | unset( $options[ 'options' ] ); | ||
| 1167 | } | ||
| 1168 | |||
| 1169 |             if ( !is_array( $value ) && 0 < strlen( $value ) ) { | ||
| 1170 | $simple = @json_decode( $value, true ); | ||
| 1171 | |||
| 1172 | if ( is_array( $simple ) ) | ||
| 1173 | $value = $simple; | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | $data = pods_var_raw( 'data', $options, null, null, true ); | ||
| 1177 | |||
| 1178 | $object_params = array( | ||
| 1179 | 'name' => $name, // The name of the field | ||
| 1180 | 'value' => $value, // The value of the field | ||
| 1181 | 'options' => $options, // Field options | ||
| 1182 | 'pod' => $pod, // Pod data | ||
| 1183 | 'id' => $id, // Item ID | ||
| 1184 | 'context' => 'simple_value', // Data context | ||
| 1185 | ); | ||
| 1186 | |||
| 1187 | if ( null === $data ) | ||
| 1188 | $data = $this->get_object_data( $object_params ); | ||
| 1189 | |||
| 1190 | $data = (array) $data; | ||
| 1191 | |||
| 1192 | $key = 0; | ||
| 1193 | |||
| 1194 |             if ( is_array( $value ) ) { | ||
| 1195 |                 if ( !empty( $data ) ) { | ||
| 1196 | $val = array(); | ||
| 1197 | |||
| 1198 |                     foreach ( $value as $k => $v ) { | ||
| 1199 |                         if ( isset( $data[ $v ] ) ) { | ||
| 1200 |                             if ( false === $raw ) { | ||
| 1201 | $k = $v; | ||
| 1202 | $v = $data[ $v ]; | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | $val[ $k ] = $v; | ||
| 1206 | } | ||
| 1207 | } | ||
| 1208 | |||
| 1209 | $value = $val; | ||
| 1210 | } | ||
| 1211 | } | ||
| 1212 |             elseif ( isset( $data[ $value ] ) && false === $raw ) { | ||
| 1213 | $key = $value; | ||
| 1214 | $value = $data[ $value ]; | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | $single_multi = pods_var( self::$type . '_format_type', $options, 'single' ); | ||
| 1218 | |||
| 1219 | if ( 'multi' == $single_multi ) | ||
| 1220 | $limit = (int) pods_var( self::$type . '_limit', $options, 0 ); | ||
| 1221 | else | ||
| 1222 | $limit = 1; | ||
| 1223 | |||
| 1224 |             if ( is_array( $value ) && 0 < $limit ) { | ||
| 1225 | if ( 1 == $limit ) | ||
| 1226 | $value = current( $value ); | ||
| 1227 | else | ||
| 1228 | $value = array_slice( $value, 0, $limit, true ); | ||
| 1229 | } | ||
| 1230 |             elseif ( !is_array( $value ) && null !== $value && 0 < strlen( $value ) ) { | ||
| 1231 |                 if ( 1 != $limit || ( true === $raw && 'multi' == $single_multi ) ) { | ||
| 1232 | $value = array( | ||
| 1233 | $key => $value | ||
| 1234 | ); | ||
| 1235 | } | ||
| 1236 | } | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | return $value; | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | /** | ||
| 1243 | * Get the label from a pick value | ||
| 1244 | * | ||
| 1245 | * @param string $name The name of the field | ||
| 1246 | * @param string|array $value The value of the field | ||
| 1247 | * @param array $options Field options | ||
| 1248 | * @param array $pod Pod data | ||
| 1249 | * @param int $id Item ID | ||
| 1250 | * | ||
| 1251 | * @return string | ||
| 1252 | * | ||
| 1253 | * @since 2.2 | ||
| 1254 | */ | ||
| 1255 |     public function value_to_label ( $name, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 1256 | View Code Duplication |         if ( isset( $options[ 'options' ] ) ) { | |
| 1257 | $options = array_merge( $options, $options[ 'options' ] ); | ||
| 1258 | |||
| 1259 | unset( $options[ 'options' ] ); | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | $data = pods_var_raw( 'data', $options, null, null, true ); | ||
| 1263 | |||
| 1264 | $object_params = array( | ||
| 1265 | 'name' => $name, // The name of the field | ||
| 1266 | 'value' => $value, // The value of the field | ||
| 1267 | 'options' => $options, // Field options | ||
| 1268 | 'pod' => $pod, // Pod data | ||
| 1269 | 'id' => $id, // Item ID | ||
| 1270 | 'context' => 'value_to_label', // Data context | ||
| 1271 | ); | ||
| 1272 | |||
| 1273 | if ( null !== $data ) | ||
| 1274 | $data = (array) $data; | ||
| 1275 | else | ||
| 1276 | $data = $this->get_object_data( $object_params ); | ||
| 1277 | |||
| 1278 | $labels = array(); | ||
| 1279 | |||
| 1280 |         foreach ( $data as $v => $l ) { | ||
| 1281 | if ( !in_array( $l, $labels ) && ( $value == $v || ( is_array( $value ) && in_array( $v, $value ) ) ) ) | ||
| 1282 | $labels[] = $l; | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | $labels = apply_filters( 'pods_field_pick_value_to_label', $labels, $name, $value, $options, $pod, $id ); | ||
| 1286 | |||
| 1287 | $labels = pods_serial_comma( $labels ); | ||
| 1288 | |||
| 1289 | return $labels; | ||
| 1290 | } | ||
| 1291 | |||
| 1292 | /** | ||
| 1293 | * Get available items from a relationship field | ||
| 1294 | * | ||
| 1295 | * @param array|string $field Field array or field name | ||
| 1296 | * @param array $options [optional] Field options array overrides | ||
| 1297 | * @param array $object_params [optional] Additional get_object_data options | ||
| 1298 | * | ||
| 1299 | * @return array An array of available items from a relationship field | ||
| 1300 | */ | ||
| 1301 | 	public function get_field_data( $field, $options = array(), $object_params = array() ) { | ||
| 1302 | |||
| 1303 | // Handle field array overrides | ||
| 1304 | 		if ( is_array( $field ) ) { | ||
| 1305 | $options = array_merge( $field, $options ); | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | // Get field name from array | ||
| 1309 | $field = pods_var_raw( 'name', $options, $field, null, true ); | ||
| 1310 | |||
| 1311 | // Field name or options not set | ||
| 1312 | 		if ( empty( $field ) || empty( $options ) ) { | ||
| 1313 | return array(); | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | // Options normalization | ||
| 1317 | $options = array_merge( $options, pods_var_raw( 'options', $options, array(), null, true ) ); | ||
| 1318 | |||
| 1319 | // Setup object params | ||
| 1320 | $object_params = array_merge( | ||
| 1321 | array( | ||
| 1322 | 'name' => $field, // The name of the field | ||
| 1323 | 'options' => $options, // Field options | ||
| 1324 | ), | ||
| 1325 | $object_params | ||
| 1326 | ); | ||
| 1327 | |||
| 1328 | // Get data override | ||
| 1329 | $data = pods_var_raw( 'data', $options, null, null, true ); | ||
| 1330 | |||
| 1331 | // Return data override | ||
| 1332 |         if ( null !== $data ) { | ||
| 1333 | $data = (array) $data; | ||
| 1334 | } | ||
| 1335 | // Get object data | ||
| 1336 |         else { | ||
| 1337 | $data = $this->get_object_data( $object_params ); | ||
| 1338 | } | ||
| 1339 | |||
| 1340 | return $data; | ||
| 1341 | |||
| 1342 | } | ||
| 1343 | |||
| 1344 | /** | ||
| 1345 | * Get data from relationship objects | ||
| 1346 | * | ||
| 1347 | * @param array $object_params Object data parameters | ||
| 1348 | * | ||
| 1349 | * @return array|bool Object data | ||
| 1350 | */ | ||
| 1351 |     public function get_object_data ( $object_params = null ) { | ||
| 1352 | |||
| 1353 | /** | ||
| 1354 | * @var $wpdb wpdb | ||
| 1355 | */ | ||
| 1356 | global $wpdb; | ||
| 1357 | |||
| 1358 | $object_params = array_merge( | ||
| 1359 | array( | ||
| 1360 | 'name' => '', // The name of the field | ||
| 1361 | 'value' => '', // The value of the field | ||
| 1362 | 'options' => array(), // Field options | ||
| 1363 | 'pod' => '', // Pod data | ||
| 1364 | 'id' => '', // Item ID | ||
| 1365 | 'context' => '', // Data context | ||
| 1366 | 'data_params' => array( | ||
| 1367 | 'query' => '' // Query being searched | ||
| 1368 | ), | ||
| 1369 | 'page' => 1, // Page number of results to get | ||
| 1370 | 'limit' => 0 // How many data items to limit to (autocomplete defaults to 30, set to -1 or 1+ to override) | ||
| 1371 | ), | ||
| 1372 | $object_params | ||
| 1373 | ); | ||
| 1374 | |||
| 1375 | $name = $object_params[ 'name' ]; | ||
| 1376 | $value = $object_params[ 'value' ]; | ||
| 1377 | $options = $object_params[ 'options' ] = (array) $object_params[ 'options' ]; | ||
| 1378 | $pod = $object_params[ 'pod' ]; | ||
| 1379 | $id = $object_params[ 'id' ]; | ||
| 1380 | $context = $object_params[ 'context' ]; | ||
| 1381 | $data_params = $object_params[ 'data_params' ] = (array) $object_params[ 'data_params' ]; | ||
| 1382 | $page = min( 1, (int) $object_params[ 'page' ] ); | ||
| 1383 | $limit = (int) $object_params[ 'limit' ]; | ||
| 1384 | |||
| 1385 | View Code Duplication |         if ( isset( $options[ 'options' ] ) ) { | |
| 1386 | $options = array_merge( $options, $options[ 'options' ] ); | ||
| 1387 | |||
| 1388 | unset( $options[ 'options' ] ); | ||
| 1389 | } | ||
| 1390 | |||
| 1391 | $data = apply_filters( 'pods_field_pick_object_data', null, $name, $value, $options, $pod, $id, $object_params ); | ||
| 1392 | $items = array(); | ||
| 1393 | |||
| 1394 | if ( !isset( $options[ self::$type . '_object' ] ) ) | ||
| 1395 | $data = pods_var_raw( 'data', $options, array(), null, true ); | ||
| 1396 | |||
| 1397 | $simple = false; | ||
| 1398 | |||
| 1399 |         if ( null === $data ) { | ||
| 1400 | $data = array(); | ||
| 1401 | |||
| 1402 |             if ( 'custom-simple' == $options[ self::$type . '_object' ] ) { | ||
| 1403 | $custom = pods_var_raw( self::$type . '_custom', $options, '' ); | ||
| 1404 | |||
| 1405 | $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id, $object_params ); | ||
| 1406 | |||
| 1407 |                 if ( !empty( $custom ) ) { | ||
| 1408 |                     if ( !is_array( $custom ) ) { | ||
| 1409 | $data = array(); | ||
| 1410 | |||
| 1411 | $custom = explode( "\n", trim( $custom ) ); | ||
| 1412 | |||
| 1413 |                         foreach ( $custom as $custom_value ) { | ||
| 1414 | $custom_label = explode( '|', $custom_value ); | ||
| 1415 | |||
| 1416 | if ( empty( $custom_label ) ) | ||
| 1417 | continue; | ||
| 1418 | |||
| 1419 | if ( 1 == count( $custom_label ) ) | ||
| 1420 | $custom_label = $custom_value; | ||
| 1421 |                             else { | ||
| 1422 | $custom_value = $custom_label[ 0 ]; | ||
| 1423 | $custom_label = $custom_label[ 1 ]; | ||
| 1424 | } | ||
| 1425 | |||
| 1426 | $custom_value = trim( (string) $custom_value ); | ||
| 1427 | $custom_label = trim( (string) $custom_label ); | ||
| 1428 | |||
| 1429 | $data[ $custom_value ] = $custom_label; | ||
| 1430 | } | ||
| 1431 | } | ||
| 1432 | else | ||
| 1433 | $data = $custom; | ||
| 1434 | |||
| 1435 | $simple = true; | ||
| 1436 | } | ||
| 1437 | } | ||
| 1438 |             elseif ( isset( self::$related_objects[ $options[ self::$type . '_object' ] ] ) && isset( self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] ) && !empty( self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] ) ) { | ||
| 1439 | $data = self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ]; | ||
| 1440 | |||
| 1441 | $simple = true; | ||
| 1442 | } | ||
| 1443 |             elseif ( isset( self::$related_objects[ $options[ self::$type . '_object' ] ] ) && isset( self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ] ) && is_callable( self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ] ) ) { | ||
| 1444 | $data = call_user_func_array( | ||
| 1445 | self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ], | ||
| 1446 | array( $name, $value, $options, $pod, $id ) | ||
| 1447 | ); | ||
| 1448 | |||
| 1449 | $simple = true; | ||
| 1450 | |||
| 1451 | // Cache data from callback | ||
| 1452 | if ( !empty( $data ) ) | ||
| 1453 | self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] = $data; | ||
| 1454 | } | ||
| 1455 |             elseif ( 'simple_value' != $context ) { | ||
| 1456 | $pick_val = pods_var( self::$type . '_val', $options ); | ||
| 1457 | |||
| 1458 | if ( 'table' == pods_var( self::$type . '_object', $options ) ) | ||
| 1459 | $pick_val = pods_var( self::$type . '_table', $options, $pick_val, null, true ); | ||
| 1460 | |||
| 1461 | View Code Duplication |                 if ( '__current__' == $pick_val ) { | |
| 1462 | if ( is_object( $pod ) ) | ||
| 1463 | $pick_val = $pod->pod; | ||
| 1464 | elseif ( is_array( $pod ) ) | ||
| 1465 | $pick_val = $pod[ 'name' ]; | ||
| 1466 | elseif ( 0 < strlen( $pod ) ) | ||
| 1467 | $pick_val = $pod; | ||
| 1468 | } | ||
| 1469 | |||
| 1470 | $options[ 'table_info' ] = pods_api()->get_table_info( pods_var( self::$type . '_object', $options ), $pick_val, null, null, $options ); | ||
| 1471 | |||
| 1472 | $search_data = pods_data(); | ||
| 1473 | $search_data->table( $options[ 'table_info' ] ); | ||
| 1474 | |||
| 1475 |                 if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'name' ] ) ) { | ||
| 1476 | $search_data->pod = $options[ 'table_info' ][ 'pod' ][ 'name' ]; | ||
| 1477 | $search_data->fields = $options[ 'table_info' ][ 'pod' ][ 'fields' ]; | ||
| 1478 | } | ||
| 1479 | |||
| 1480 | $params = array( | ||
| 1481 |                     'select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`", | ||
| 1482 | 'table' => $search_data->table, | ||
| 1483 | 'where' => pods_var_raw( self::$type . '_where', $options, (array) $options[ 'table_info' ][ 'where_default' ], null, true ), | ||
| 1484 | 'orderby' => pods_var_raw( self::$type . '_orderby', $options, null, null, true ), | ||
| 1485 | 'groupby' => pods_var_raw( self::$type . '_groupby', $options, null, null, true ), | ||
| 1486 | //'having' => pods_var_raw( self::$type . '_having', $options, null, null, true ), | ||
| 1487 | 'pagination' => false, | ||
| 1488 | 'search' => false | ||
| 1489 | ); | ||
| 1490 | |||
| 1491 | if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) ) | ||
| 1492 | $params[ 'select' ] .= ', `t`.`path`'; | ||
| 1493 | |||
| 1494 | if ( !empty( $params[ 'where' ] ) && (array) $options[ 'table_info' ][ 'where_default' ] != $params[ 'where' ] ) | ||
| 1495 | $params[ 'where' ] = pods_evaluate_tags( $params[ 'where' ], true ); | ||
| 1496 | |||
| 1497 | if ( empty( $params[ 'where' ] ) || ( !is_array( $params[ 'where' ] ) && strlen( trim( $params[ 'where' ] ) ) < 1 ) ) | ||
| 1498 | $params[ 'where' ] = array(); | ||
| 1499 | elseif ( !is_array( $params[ 'where' ] ) ) | ||
| 1500 | $params[ 'where' ] = (array) $params[ 'where' ]; | ||
| 1501 | |||
| 1502 | if ( 'value_to_label' == $context ) | ||
| 1503 |                     $params[ 'where' ][] = "`t`.`{$search_data->field_id}` = " . number_format( $value, 0, '', '' ); | ||
| 1504 | |||
| 1505 | /* not needed yet | ||
| 1506 | if ( !empty( $params[ 'orderby' ] ) ) | ||
| 1507 | $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true ); | ||
| 1508 | |||
| 1509 | if ( !empty( $params[ 'groupby' ] ) ) | ||
| 1510 | $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/ | ||
| 1511 | |||
| 1512 |                 $display = trim( pods_var( self::$type . '_display', $options ), ' {@}' ); | ||
| 1513 | |||
| 1514 |                 if ( 0 < strlen( $display ) ) { | ||
| 1515 |                     if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) ) { | ||
| 1516 |                         if ( isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ][ $display ] ) ) { | ||
| 1517 | $search_data->field_index = $display; | ||
| 1518 | |||
| 1519 |                             $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`"; | ||
| 1520 | } | ||
| 1521 |                         elseif ( isset( $options[ 'table_info' ][ 'pod' ][ 'fields' ][ $display ] ) ) { | ||
| 1522 | $search_data->field_index = $display; | ||
| 1523 | |||
| 1524 | if ( 'table' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] && !in_array( $options[ 'table_info' ][ 'pod' ][ 'type' ], array( 'pod', 'table' ) ) ) | ||
| 1525 |                                 $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`"; | ||
| 1526 | elseif ( 'meta' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] ) | ||
| 1527 |                                 $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `{$search_data->field_index}`.`meta_value` AS {$search_data->field_index}"; | ||
| 1528 | else | ||
| 1529 |                                 $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`"; | ||
| 1530 | } | ||
| 1531 | } | ||
| 1532 |                     elseif ( isset( $options[ 'table_info' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'object_fields' ][ $display ] ) ) { | ||
| 1533 | $search_data->field_index = $display; | ||
| 1534 | |||
| 1535 |                         $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`"; | ||
| 1536 | } | ||
| 1537 | } | ||
| 1538 | |||
| 1539 | $autocomplete = false; | ||
| 1540 | |||
| 1541 | if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) | ||
| 1542 | $autocomplete = true; | ||
| 1543 | View Code Duplication | elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) | |
| 1544 | $autocomplete = true; | ||
| 1545 | |||
| 1546 | $hierarchy = false; | ||
| 1547 | |||
| 1548 |                 if ( 'data' == $context && !$autocomplete ) { | ||
| 1549 | if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && in_array( pods_var( self::$type . '_format_single', $options, 'dropdown' ), array( 'dropdown', 'radio' ) ) ) | ||
| 1550 | $hierarchy = true; | ||
| 1551 | View Code Duplication | elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) && in_array( pods_var( self::$type . '_format_multi', $options, 'checkbox' ), array( 'multiselect', 'checkbox' ) ) ) | |
| 1552 | $hierarchy = true; | ||
| 1553 | } | ||
| 1554 | |||
| 1555 | if ( $hierarchy && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) ) | ||
| 1556 | $params[ 'select' ] .= ', ' . $options[ 'table_info' ][ 'field_parent_select' ]; | ||
| 1557 | |||
| 1558 |                 if ( $autocomplete ) { | ||
| 1559 | 					if ( 0 == $limit ) { | ||
| 1560 | $limit = 30; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | $params[ 'limit' ] = apply_filters( 'pods_form_ui_field_pick_autocomplete_limit', $limit, $name, $value, $options, $pod, $id, $object_params ); | ||
| 1564 | |||
| 1565 | 					if ( is_array( $value ) && $params[ 'limit' ] < count( $value ) ) { | ||
| 1566 | $params[ 'limit' ] = count( $value ); | ||
| 1567 | } | ||
| 1568 | |||
| 1569 | $params[ 'page' ] = $page; | ||
| 1570 | |||
| 1571 |                     if ( 'admin_ajax_relationship' == $context ) { | ||
| 1572 | $lookup_where = array( | ||
| 1573 |                             $search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'" | ||
| 1574 | ); | ||
| 1575 | |||
| 1576 | // @todo Hook into WPML for each table | ||
| 1577 |                         if ( $wpdb->users == $search_data->table ) { | ||
| 1578 | $lookup_where[ 'display_name' ] = "`t`.`display_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1579 | $lookup_where[ 'user_login' ] = "`t`.`user_login` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1580 | $lookup_where[ 'user_email' ] = "`t`.`user_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1581 | } | ||
| 1582 |                         elseif ( $wpdb->posts == $search_data->table ) { | ||
| 1583 | $lookup_where[ 'post_title' ] = "`t`.`post_title` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1584 | $lookup_where[ 'post_name' ] = "`t`.`post_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1585 | $lookup_where[ 'post_content' ] = "`t`.`post_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1586 | $lookup_where[ 'post_excerpt' ] = "`t`.`post_excerpt` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1587 | } | ||
| 1588 |                         elseif ( $wpdb->terms == $search_data->table ) { | ||
| 1589 | $lookup_where[ 'name' ] = "`t`.`name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1590 | $lookup_where[ 'slug' ] = "`t`.`slug` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1591 | } | ||
| 1592 |                         elseif ( $wpdb->comments == $search_data->table ) { | ||
| 1593 | $lookup_where[ 'comment_content' ] = "`t`.`comment_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1594 | $lookup_where[ 'comment_author' ] = "`t`.`comment_author` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1595 | $lookup_where[ 'comment_author_email' ] = "`t`.`comment_author_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"; | ||
| 1596 | } | ||
| 1597 | |||
| 1598 | $lookup_where = apply_filters( 'pods_form_ui_field_pick_autocomplete_lookup', $lookup_where, $data_params[ 'query' ], $name, $value, $options, $pod, $id, $object_params, $search_data ); | ||
| 1599 | |||
| 1600 | if ( !empty( $lookup_where ) ) | ||
| 1601 | $params[ 'where' ][] = implode( ' OR ', $lookup_where ); | ||
| 1602 | |||
| 1603 | $orderby = array(); | ||
| 1604 |                         $orderby[] = "(`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%' ) DESC"; | ||
| 1605 | |||
| 1606 | $pick_orderby = pods_var_raw( self::$type . '_orderby', $options, null, null, true ); | ||
| 1607 | |||
| 1608 | if ( 0 < strlen( $pick_orderby ) ) | ||
| 1609 | $orderby[] = $pick_orderby; | ||
| 1610 | |||
| 1611 |                         $orderby[] = "`t`.`{$search_data->field_index}`"; | ||
| 1612 |                         $orderby[] = "`t`.`{$search_data->field_id}`"; | ||
| 1613 | |||
| 1614 | $params[ 'orderby' ] = $orderby; | ||
| 1615 | } | ||
| 1616 | } | ||
| 1617 | 				elseif ( 0 < $limit ) { | ||
| 1618 | $params[ 'limit' ] = $limit; | ||
| 1619 | $params[ 'page' ] = $page; | ||
| 1620 | } | ||
| 1621 | |||
| 1622 | $extra = ''; | ||
| 1623 | |||
| 1624 | if ( $wpdb->posts == $search_data->table ) | ||
| 1625 | $extra = ', `t`.`post_type`'; | ||
| 1626 | elseif ( $wpdb->terms == $search_data->table ) | ||
| 1627 | $extra = ', `tt`.`taxonomy`'; | ||
| 1628 | elseif ( $wpdb->comments == $search_data->table ) | ||
| 1629 | $extra = ', `t`.`comment_type`'; | ||
| 1630 | |||
| 1631 | $params[ 'select' ] .= $extra; | ||
| 1632 | |||
| 1633 |                 if ( 'user' == pods_var( self::$type . '_object', $options ) ) { | ||
| 1634 | $roles = pods_var( self::$type . '_user_role', $options ); | ||
| 1635 | |||
| 1636 |                     if ( !empty( $roles ) ) { | ||
| 1637 | $where = array(); | ||
| 1638 | |||
| 1639 |                         foreach ( (array) $roles as $role ) { | ||
| 1640 | if ( empty( $role ) || ( pods_clean_name( $role ) != $role && sanitize_title( $role ) != $role ) ) | ||
| 1641 | continue; | ||
| 1642 | |||
| 1643 | $where[] = $wpdb->base_prefix . ( ( is_multisite() && !is_main_site() ) ? get_current_blog_id() . '_' : '' ) . 'capabilities.meta_value LIKE "%\"' . pods_sanitize_like( $role ) . '\"%"'; | ||
| 1644 | } | ||
| 1645 | |||
| 1646 |                         if ( !empty( $where ) ) { | ||
| 1647 | $params[ 'where' ][] = implode( ' OR ', $where ); | ||
| 1648 | } | ||
| 1649 | } | ||
| 1650 | } | ||
| 1651 | |||
| 1652 | $results = $search_data->select( $params ); | ||
| 1653 | |||
| 1654 |                 if ( $autocomplete && $params[ 'limit' ] < $search_data->total_found() ) { | ||
| 1655 |                     if ( !empty( $value ) ) { | ||
| 1656 | $ids = $value; | ||
| 1657 | |||
| 1658 | 						if ( is_array( $ids ) && isset( $ids[ 0 ] ) && is_array( $ids[ 0 ] ) ) { | ||
| 1659 | $ids = wp_list_pluck( $ids, $search_data->field_id ); | ||
| 1660 | } | ||
| 1661 | |||
| 1662 | if ( is_array( $ids ) ) | ||
| 1663 | $ids = implode( ', ', $ids ); | ||
| 1664 | |||
| 1665 | if ( is_array( $params[ 'where' ] ) ) | ||
| 1666 | $params[ 'where' ] = implode( ' AND ', $params[ 'where' ] ); | ||
| 1667 | if ( !empty( $params[ 'where' ] ) ) | ||
| 1668 | $params[ 'where' ] .= ' AND '; | ||
| 1669 | |||
| 1670 |                         $params[ 'where' ] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )"; | ||
| 1671 | |||
| 1672 | $results = $search_data->select( $params ); | ||
| 1673 | } | ||
| 1674 | } | ||
| 1675 | else | ||
| 1676 | $autocomplete = false; | ||
| 1677 | |||
| 1678 |                 if ( 'data' == $context ) { | ||
| 1679 | self::$field_data = array( | ||
| 1680 | 'field' => $name, | ||
| 1681 | 'id' => $options[ 'id' ], | ||
| 1682 | 'autocomplete' => $autocomplete | ||
| 1683 | ); | ||
| 1684 | } | ||
| 1685 | |||
| 1686 |                 if ( $hierarchy && !$autocomplete && !empty( $results ) && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) ) { | ||
| 1687 | $args = array( | ||
| 1688 | 'id' => $options[ 'table_info' ][ 'field_id' ], | ||
| 1689 | 'index' => $options[ 'table_info' ][ 'field_index' ], | ||
| 1690 | 'parent' => $options[ 'table_info' ][ 'field_parent' ], | ||
| 1691 | ); | ||
| 1692 | |||
| 1693 | $results = pods_hierarchical_select( $results, $args ); | ||
| 1694 | } | ||
| 1695 | |||
| 1696 | $ids = array(); | ||
| 1697 | |||
| 1698 |                 if ( !empty( $results ) ) { | ||
| 1699 | $display_filter = pods_var( 'display_filter', pods_var_raw( 'options', pods_var_raw( $search_data->field_index, $search_data->pod_data[ 'object_fields' ] ) ) ); | ||
| 1700 | |||
| 1701 |                     foreach ( $results as $result ) { | ||
| 1702 | $result = get_object_vars( $result ); | ||
| 1703 | |||
| 1704 | if ( !isset( $result[ $search_data->field_id ] ) || !isset( $result[ $search_data->field_index ] ) ) | ||
| 1705 | continue; | ||
| 1706 | |||
| 1707 | $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] ); | ||
| 1708 | |||
| 1709 | $object = $object_type = ''; | ||
| 1710 | |||
| 1711 |                         if ( $wpdb->posts == $search_data->table && isset( $result[ 'post_type' ] ) ) { | ||
| 1712 | $object = $result[ 'post_type' ]; | ||
| 1713 | $object_type = 'post_type'; | ||
| 1714 | } | ||
| 1715 |                         elseif ( $wpdb->terms == $search_data->table && isset( $result[ 'taxonomy' ] ) ) { | ||
| 1716 | $object = $result[ 'taxonomy' ]; | ||
| 1717 | $object_type = 'taxonomy'; | ||
| 1718 | } | ||
| 1719 | |||
| 1720 |                         if ( 0 < strlen( $display_filter ) ) { | ||
| 1721 | $display_filter_args = pods_var( 'display_filter_args', pods_var_raw( 'options', pods_var_raw( $search_data->field_index, $search_data->pod_data[ 'object_fields' ] ) ) ); | ||
| 1722 | |||
| 1723 | $args = array( | ||
| 1724 | $display_filter, | ||
| 1725 | $result[ $search_data->field_index ] | ||
| 1726 | ); | ||
| 1727 | |||
| 1728 |                             if ( !empty( $display_filter_args ) ) { | ||
| 1729 |                                 foreach ( (array) $display_filter_args as $display_filter_arg ) { | ||
| 1730 | if ( isset( $result[ $display_filter_arg ] ) ) | ||
| 1731 | $args[] = $result[ $display_filter_arg ]; | ||
| 1732 | } | ||
| 1733 | } | ||
| 1734 | |||
| 1735 | $result[ $search_data->field_index ] = call_user_func_array( 'apply_filters', $args ); | ||
| 1736 | } | ||
| 1737 | |||
| 1738 | if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) ) | ||
| 1739 | $result[ $search_data->field_index ] = $result[ $search_data->field_index ] . $result[ 'path' ]; | ||
| 1740 | elseif ( strlen( $result[ $search_data->field_index ] ) < 1 ) | ||
| 1741 | $result[ $search_data->field_index ] = '(No Title)'; | ||
| 1742 | |||
| 1743 |                         if ( 'admin_ajax_relationship' == $context ) { | ||
| 1744 | $items[] = array( | ||
| 1745 | 'id' => $result[ $search_data->field_id ], | ||
| 1746 | 'text' => $result[ $search_data->field_index ], | ||
| 1747 | 'image' => '' | ||
| 1748 | ); | ||
| 1749 | } | ||
| 1750 | else | ||
| 1751 | $data[ $result[ $search_data->field_id ] ] = $result[ $search_data->field_index ]; | ||
| 1752 | |||
| 1753 | $ids[] = $result[ $search_data->field_id ]; | ||
| 1754 | } | ||
| 1755 | } | ||
| 1756 | } | ||
| 1757 | |||
| 1758 | 			if ( $simple && 'admin_ajax_relationship' == $context ) { | ||
| 1759 | $found_data = array(); | ||
| 1760 | |||
| 1761 | 				foreach ( $data as $k => $v ) { | ||
| 1762 | 					if ( false !== stripos( $v, $data_params[ 'query' ] ) || false !== stripos( $k, $data_params[ 'query' ] ) ) { | ||
| 1763 | $found_data[ $k ] = $v; | ||
| 1764 | } | ||
| 1765 | } | ||
| 1766 | |||
| 1767 | $data = $found_data; | ||
| 1768 | } | ||
| 1769 | } | ||
| 1770 | |||
| 1771 |         if ( 'admin_ajax_relationship' == $context ) { | ||
| 1772 |             if ( empty( $items ) && !empty( $data ) ) { | ||
| 1773 |                 foreach ( $data as $k => $v ) { | ||
| 1774 | $items[] = array( | ||
| 1775 | 'id' => $k, | ||
| 1776 | 'text' => $v, | ||
| 1777 | 'image' => '' | ||
| 1778 | ); | ||
| 1779 | } | ||
| 1780 | } | ||
| 1781 | |||
| 1782 | return $items; | ||
| 1783 | } | ||
| 1784 | |||
| 1785 | return $data; | ||
| 1786 | } | ||
| 1787 | |||
| 1788 | /** | ||
| 1789 | * AJAX call to refresh relationship field markup (supports adding new records modally) | ||
| 1790 | * | ||
| 1791 | * @since 2.7 | ||
| 1792 | */ | ||
| 1793 |     public function admin_ajax_relationship_popup () { | ||
| 1822 | |||
| 1823 | /** | ||
| 1824 | * Handle autocomplete AJAX | ||
| 1825 | * | ||
| 1826 | * @since 2.3 | ||
| 1827 | */ | ||
| 1828 |     public function admin_ajax_relationship () { | ||
| 1829 | pods_session_start(); | ||
| 1830 | |||
| 1831 | // Sanitize input | ||
| 1832 | $params = pods_unslash( (array) $_POST ); | ||
| 1833 | |||
| 1834 | View Code Duplication |         foreach ( $params as $key => $value ) { | |
| 1835 | if ( 'action' == $key ) | ||
| 1836 | continue; | ||
| 1837 | |||
| 1838 | unset( $params[ $key ] ); | ||
| 1839 | |||
| 1840 | $params[ str_replace( '_podsfix_', '', $key ) ] = $value; | ||
| 1841 | } | ||
| 1925 | |||
| 1926 | /** | ||
| 1927 | * Data callback for Post Stati | ||
| 1928 | * | ||
| 1929 | * @param string $name The name of the field | ||
| 1930 | * @param string|array $value The value of the field | ||
| 1931 | * @param array $options Field options | ||
| 1932 | * @param array $pod Pod data | ||
| 1933 | * @param int $id Item ID | ||
| 1934 | * | ||
| 1935 | * @return array | ||
| 1936 | * | ||
| 1937 | * @since 2.3 | ||
| 1938 | */ | ||
| 1939 |     public function data_post_stati ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 1950 | |||
| 1951 | /** | ||
| 1952 | * Data callback for User Roles | ||
| 1953 | * | ||
| 1954 | * @param string $name The name of the field | ||
| 1955 | * @param string|array $value The value of the field | ||
| 1956 | * @param array $options Field options | ||
| 1957 | * @param array $pod Pod data | ||
| 1958 | * @param int $id Item ID | ||
| 1959 | * | ||
| 1960 | * @return array | ||
| 1961 | * | ||
| 1962 | * @since 2.3 | ||
| 1963 | */ | ||
| 1964 |     public function data_roles ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 1975 | |||
| 1976 | /** | ||
| 1977 | * Data callback for User Capabilities | ||
| 1978 | * | ||
| 1979 | * @param string $name The name of the field | ||
| 1980 | * @param string|array $value The value of the field | ||
| 1981 | * @param array $options Field options | ||
| 1982 | * @param array $pod Pod data | ||
| 1983 | * @param int $id Item ID | ||
| 1984 | * | ||
| 1985 | * @return array | ||
| 1986 | * | ||
| 1987 | * @since 2.3 | ||
| 1988 | */ | ||
| 1989 |     public function data_capabilities ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 2077 | |||
| 2078 | /** | ||
| 2079 | * Data callback for Image Sizes | ||
| 2080 | * | ||
| 2081 | * @param string $name The name of the field | ||
| 2082 | * @param string|array $value The value of the field | ||
| 2083 | * @param array $options Field options | ||
| 2084 | * @param array $pod Pod data | ||
| 2085 | * @param int $id Item ID | ||
| 2086 | * | ||
| 2087 | * @return array | ||
| 2088 | * | ||
| 2089 | * @since 2.3 | ||
| 2090 | */ | ||
| 2091 |     public function data_image_sizes ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 2102 | |||
| 2103 | /** | ||
| 2104 | * Data callback for Countries | ||
| 2105 | * | ||
| 2106 | * @param string $name The name of the field | ||
| 2107 | * @param string|array $value The value of the field | ||
| 2108 | * @param array $options Field options | ||
| 2109 | * @param array $pod Pod data | ||
| 2110 | * @param int $id Item ID | ||
| 2111 | * | ||
| 2112 | * @return array | ||
| 2113 | * | ||
| 2114 | * @since 2.3 | ||
| 2115 | */ | ||
| 2116 |     public function data_countries ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 2386 | |||
| 2387 | /** | ||
| 2388 | * Data callback for US States | ||
| 2389 | * | ||
| 2390 | * @param string $name The name of the field | ||
| 2391 | * @param string|array $value The value of the field | ||
| 2392 | * @param array $options Field options | ||
| 2393 | * @param array $pod Pod data | ||
| 2394 | * @param int $id Item ID | ||
| 2395 | * | ||
| 2396 | * @return array | ||
| 2397 | * | ||
| 2398 | * @since 2.3 | ||
| 2399 | */ | ||
| 2400 |     public function data_us_states ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 2457 | |||
| 2458 | /** | ||
| 2459 | * Data callback for US States | ||
| 2460 | * | ||
| 2461 | * @param string $name The name of the field | ||
| 2462 | * @param string|array $value The value of the field | ||
| 2463 | * @param array $options Field options | ||
| 2464 | * @param array $pod Pod data | ||
| 2465 | * @param int $id Item ID | ||
| 2466 | * | ||
| 2467 | * @return array | ||
| 2468 | * | ||
| 2469 | * @since 2.3 | ||
| 2470 | */ | ||
| 2471 |     public function data_days_of_week ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 2481 | |||
| 2482 | /** | ||
| 2483 | * Data callback for US States | ||
| 2484 | * | ||
| 2485 | * @param string $name The name of the field | ||
| 2486 | * @param string|array $value The value of the field | ||
| 2487 | * @param array $options Field options | ||
| 2488 | * @param array $pod Pod data | ||
| 2489 | * @param int $id Item ID | ||
| 2490 | * | ||
| 2491 | * @return array | ||
| 2492 | * | ||
| 2493 | * @since 2.3 | ||
| 2494 | */ | ||
| 2495 |     public function data_months_of_year ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { | ||
| 2505 | } | ||
| 2506 | 
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.