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 PodsMeta 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 PodsMeta, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class PodsMeta { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @var PodsMeta |
||
| 9 | */ |
||
| 10 | static $instance = null; |
||
|
|
|||
| 11 | |||
| 12 | /** |
||
| 13 | * @var PodsAPI |
||
| 14 | */ |
||
| 15 | private $api; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var Pods |
||
| 19 | */ |
||
| 20 | private static $current_pod; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private static $current_pod_data; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Pods |
||
| 29 | */ |
||
| 30 | private static $current_field_pod; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | public static $object_identifier = -1; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | public static $advanced_content_types = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | public static $post_types = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | public static $taxonomies = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | public static $media = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | public static $user = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | public static $comment = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | public static $settings = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | public static $queue = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | public static $groups = array(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | public static $old_post_status = ''; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Singleton handling for a basic pods_meta() request |
||
| 89 | * |
||
| 90 | * @return \PodsMeta |
||
| 91 | * |
||
| 92 | * @since 2.3.5 |
||
| 93 | */ |
||
| 94 | public static function init () { |
||
| 95 | if ( !is_object( self::$instance ) ) |
||
| 96 | self::$instance = new PodsMeta(); |
||
| 97 | |||
| 98 | return self::$instance; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return \PodsMeta |
||
| 103 | * |
||
| 104 | * @since 2.0 |
||
| 105 | */ |
||
| 106 | function __construct () { |
||
| 107 | |||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return \PodsMeta |
||
| 112 | */ |
||
| 113 | public function core () { |
||
| 114 | self::$advanced_content_types = pods_api()->load_pods( array( 'type' => 'pod' ) ); |
||
| 115 | self::$post_types = pods_api()->load_pods( array( 'type' => 'post_type' ) ); |
||
| 116 | self::$taxonomies = pods_api()->load_pods( array( 'type' => 'taxonomy' ) ); |
||
| 117 | self::$media = pods_api()->load_pods( array( 'type' => 'media' ) ); |
||
| 118 | self::$user = pods_api()->load_pods( array( 'type' => 'user' ) ); |
||
| 119 | self::$comment = pods_api()->load_pods( array( 'type' => 'comment' ) ); |
||
| 120 | self::$settings = pods_api()->load_pods( array( 'type' => 'settings' ) ); |
||
| 121 | |||
| 122 | // Handle Post Type Editor (needed for Pods core) |
||
| 123 | |||
| 124 | // Loop through and add meta boxes for individual types (can't use this, Tabify doesn't pick it up) |
||
| 125 | /* |
||
| 126 | foreach ( self::$post_types as $post_type ) { |
||
| 127 | $post_type_name = $post_type[ 'name' ]; |
||
| 128 | |||
| 129 | if ( !empty( $post_type[ 'object' ] ) ) |
||
| 130 | $post_type_name = $post_type[ 'object' ]; |
||
| 131 | |||
| 132 | add_action( 'add_meta_boxes_' . $post_type_name, array( $this, 'meta_post_add' ) ); |
||
| 133 | } |
||
| 134 | */ |
||
| 135 | |||
| 136 | add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ); |
||
| 137 | add_action( 'transition_post_status', array( $this, 'save_post_detect_new' ), 10, 3 ); |
||
| 138 | add_action( 'save_post', array( $this, 'save_post' ), 10, 3 ); |
||
| 139 | |||
| 140 | View Code Duplication | if ( apply_filters( 'pods_meta_handler', true, 'post' ) ) { |
|
| 141 | // Handle *_post_meta |
||
| 142 | if ( apply_filters( 'pods_meta_handler_get', true, 'post' ) ) { |
||
| 143 | add_filter( 'get_post_metadata', array( $this, 'get_post_meta' ), 10, 4 ); |
||
| 144 | } |
||
| 145 | |||
| 146 | if ( !pods_tableless() ) { |
||
| 147 | add_filter( 'add_post_metadata', array( $this, 'add_post_meta' ), 10, 5 ); |
||
| 148 | add_filter( 'update_post_metadata', array( $this, 'update_post_meta' ), 10, 5 ); |
||
| 149 | add_filter( 'delete_post_metadata', array( $this, 'delete_post_meta' ), 10, 5 ); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | add_action( 'delete_post', array( $this, 'delete_post' ), 10, 1 ); |
||
| 154 | |||
| 155 | if ( !empty( self::$taxonomies ) ) { |
||
| 156 | $has_fields = false; |
||
| 157 | |||
| 158 | // Handle Taxonomy Editor |
||
| 159 | foreach ( self::$taxonomies as $taxonomy ) { |
||
| 160 | if ( empty( $taxonomy[ 'fields' ] ) ) { |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | |||
| 164 | $has_fields = true; |
||
| 165 | |||
| 166 | $taxonomy_name = $taxonomy[ 'name' ]; |
||
| 167 | |||
| 168 | if ( !empty( $taxonomy[ 'object' ] ) ) |
||
| 169 | $taxonomy_name = $taxonomy[ 'object' ]; |
||
| 170 | |||
| 171 | add_action( $taxonomy_name . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 ); |
||
| 172 | add_action( $taxonomy_name . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 ); |
||
| 173 | } |
||
| 174 | |||
| 175 | if ( $has_fields ) { |
||
| 176 | // Handle Term Editor |
||
| 177 | add_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 ); |
||
| 178 | add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 ); |
||
| 179 | |||
| 180 | if ( apply_filters( 'pods_meta_handler', true, 'term' ) ) { |
||
| 181 | // Handle *_term_meta |
||
| 182 | if ( apply_filters( 'pods_meta_handler_get', true, 'term' ) ) { |
||
| 183 | add_filter( 'get_term_metadata', array( $this, 'get_term_meta' ), 10, 4 ); |
||
| 184 | } |
||
| 185 | |||
| 186 | if ( !pods_tableless() ) { |
||
| 187 | add_filter( 'add_term_metadata', array( $this, 'add_term_meta' ), 10, 5 ); |
||
| 188 | add_filter( 'update_term_metadata', array( $this, 'update_term_meta' ), 10, 5 ); |
||
| 189 | add_filter( 'delete_term_metadata', array( $this, 'delete_term_meta' ), 10, 5 ); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Fires after a previously shared taxonomy term is split into two separate terms. |
||
| 197 | * |
||
| 198 | * @since 4.2.0 |
||
| 199 | * |
||
| 200 | * @param int $term_id ID of the formerly shared term. |
||
| 201 | * @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
||
| 202 | * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
||
| 203 | * @param string $taxonomy Taxonomy for the split term. |
||
| 204 | */ |
||
| 205 | add_action( 'split_shared_term', array( $this, 'split_shared_term' ), 10, 4 ); |
||
| 206 | |||
| 207 | // Handle Delete |
||
| 208 | add_action( 'delete_term_taxonomy', array( $this, 'delete_taxonomy' ), 10, 1 ); |
||
| 209 | |||
| 210 | if ( !empty( self::$media ) ) { |
||
| 211 | View Code Duplication | if ( pods_version_check( 'wp', '3.5' ) ) { |
|
| 212 | add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ); |
||
| 213 | add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 ); |
||
| 214 | } |
||
| 215 | |||
| 216 | add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 ); |
||
| 217 | |||
| 218 | add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 ); |
||
| 219 | add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 ); |
||
| 220 | |||
| 221 | if ( apply_filters( 'pods_meta_handler', true, 'post' ) ) { |
||
| 222 | // Handle *_post_meta |
||
| 223 | View Code Duplication | if ( !has_filter( 'get_post_metadata', array( $this, 'get_post_meta' ) ) ) { |
|
| 224 | if ( apply_filters( 'pods_meta_handler_get', true, 'post' ) ) { |
||
| 225 | add_filter( 'get_post_metadata', array( $this, 'get_post_meta' ), 10, 4 ); |
||
| 226 | } |
||
| 227 | |||
| 228 | if ( !pods_tableless() ) { |
||
| 229 | add_filter( 'add_post_metadata', array( $this, 'add_post_meta' ), 10, 5 ); |
||
| 230 | add_filter( 'update_post_metadata', array( $this, 'update_post_meta' ), 10, 5 ); |
||
| 231 | add_filter( 'delete_post_metadata', array( $this, 'delete_post_meta' ), 10, 5 ); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | // Handle Delete |
||
| 238 | add_action( 'delete_attachment', array( $this, 'delete_media' ), 10, 1 ); |
||
| 239 | |||
| 240 | if ( !empty( self::$user ) ) { |
||
| 241 | // Handle User Editor |
||
| 242 | add_action( 'show_user_profile', array( $this, 'meta_user' ) ); |
||
| 243 | add_action( 'edit_user_profile', array( $this, 'meta_user' ) ); |
||
| 244 | //add_action( 'user_register', array( $this, 'save_user' ) ); |
||
| 245 | add_action( 'profile_update', array( $this, 'save_user' ) ); |
||
| 246 | |||
| 247 | if ( apply_filters( 'pods_meta_handler', true, 'user' ) ) { |
||
| 248 | // Handle *_user_meta |
||
| 249 | if ( apply_filters( 'pods_meta_handler_get', true, 'user' ) ) { |
||
| 250 | add_filter( 'get_user_metadata', array( $this, 'get_user_meta' ), 10, 4 ); |
||
| 251 | } |
||
| 252 | |||
| 253 | if ( !pods_tableless() ) { |
||
| 254 | add_filter( 'add_user_metadata', array( $this, 'add_user_meta' ), 10, 5 ); |
||
| 255 | add_filter( 'update_user_metadata', array( $this, 'update_user_meta' ), 10, 5 ); |
||
| 256 | add_filter( 'delete_user_metadata', array( $this, 'delete_user_meta' ), 10, 5 ); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | // Handle Delete |
||
| 262 | add_action( 'delete_user', array( $this, 'delete_user' ), 10, 1 ); |
||
| 263 | |||
| 264 | if ( !empty( self::$comment ) ) { |
||
| 265 | // Handle Comment Form / Editor |
||
| 266 | add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 ); |
||
| 267 | add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) ); |
||
| 268 | add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) ); |
||
| 269 | add_filter( 'pre_comment_approved', array( $this, 'validate_comment' ), 10, 2 ); |
||
| 270 | add_action( 'comment_post', array( $this, 'save_comment' ) ); |
||
| 271 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
| 272 | |||
| 273 | if ( apply_filters( 'pods_meta_handler', true, 'comment' ) ) { |
||
| 274 | // Handle *_comment_meta |
||
| 275 | add_filter( 'get_comment_metadata', array( $this, 'get_comment_meta' ), 10, 4 ); |
||
| 276 | |||
| 277 | if ( !pods_tableless() ) { |
||
| 278 | add_filter( 'add_comment_metadata', array( $this, 'add_comment_meta' ), 10, 5 ); |
||
| 279 | add_filter( 'update_comment_metadata', array( $this, 'update_comment_meta' ), 10, 5 ); |
||
| 280 | add_filter( 'delete_comment_metadata', array( $this, 'delete_comment_meta' ), 10, 5 ); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | // Handle Delete |
||
| 286 | add_action( 'delete_comment', array( $this, 'delete_comment' ), 10, 1 ); |
||
| 287 | |||
| 288 | // @todo Patch core to provide $option back in filters, patch core to add filter pre_add_option to add_option |
||
| 289 | /*if ( !empty( self::$settings ) ) { |
||
| 290 | foreach ( self::$settings as $setting_pod ) { |
||
| 291 | foreach ( $setting_pod[ 'fields' ] as $option ) { |
||
| 292 | add_filter( 'pre_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'get_option' ), 10, 1 ); |
||
| 293 | add_action( 'add_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'add_option' ), 10, 2 ); |
||
| 294 | add_filter( 'pre_update_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'update_option' ), 10, 2 ); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | }*/ |
||
| 298 | |||
| 299 | if ( is_admin() ) |
||
| 300 | $this->integrations(); |
||
| 301 | |||
| 302 | add_action( 'init', array( $this, 'enqueue' ), 9 ); |
||
| 303 | |||
| 304 | if ( function_exists( 'pll_current_language' ) ) |
||
| 305 | add_action( 'init', array( $this, 'cache_pods' ), 101 ); |
||
| 306 | |||
| 307 | do_action( 'pods_meta_init' ); |
||
| 308 | |||
| 309 | return $this; |
||
| 310 | } |
||
| 311 | |||
| 312 | public static function enqueue () { |
||
| 313 | foreach ( self::$queue as $type => $objects ) { |
||
| 314 | foreach ( $objects as $pod_name => $pod ) { |
||
| 315 | pods_transient_set( 'pods_pod_' . $pod_name, $pod ); |
||
| 316 | } |
||
| 317 | |||
| 318 | self::$$type = array_merge( self::$$type, $objects ); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Go back through and cache the Pods now that Polylang has loaded |
||
| 324 | */ |
||
| 325 | public function cache_pods () { |
||
| 326 | self::$advanced_content_types = pods_api()->load_pods( array( 'type' => 'pod', 'refresh' => true ) ); |
||
| 327 | self::$post_types = pods_api()->load_pods( array( 'type' => 'post_type', 'refresh' => true ) ); |
||
| 328 | self::$taxonomies = pods_api()->load_pods( array( 'type' => 'taxonomy', 'refresh' => true ) ); |
||
| 329 | self::$media = pods_api()->load_pods( array( 'type' => 'media', 'refresh' => true ) ); |
||
| 330 | self::$user = pods_api()->load_pods( array( 'type' => 'user', 'refresh' => true ) ); |
||
| 331 | self::$comment = pods_api()->load_pods( array( 'type' => 'comment', 'refresh' => true ) ); |
||
| 332 | self::$settings = pods_api()->load_pods( array( 'type' => 'settings', 'refresh' => true ) ); |
||
| 333 | } |
||
| 334 | |||
| 335 | public function register ( $type, $pod ) { |
||
| 336 | $pod_type = $type; |
||
| 337 | |||
| 338 | if ( 'post_type' == $type ) |
||
| 339 | $type = 'post_types'; |
||
| 340 | elseif ( 'taxonomy' == $type ) |
||
| 341 | $type = 'taxonomies'; |
||
| 342 | elseif ( 'pod' == $type ) |
||
| 343 | $type = 'advanced_content_types'; |
||
| 344 | |||
| 345 | if ( !isset( self::$queue[ $type ] ) ) |
||
| 346 | self::$queue[ $type ] = array(); |
||
| 347 | |||
| 348 | View Code Duplication | if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) { |
|
| 349 | $data = array(); |
||
| 350 | |||
| 351 | foreach ( $pod as $p ) { |
||
| 352 | $data[] = $this->register( $type, $p ); |
||
| 353 | } |
||
| 354 | |||
| 355 | return $data; |
||
| 356 | } |
||
| 357 | |||
| 358 | $pod[ 'type' ] = $pod_type; |
||
| 359 | $pod = pods_api()->save_pod( $pod, false, false ); |
||
| 360 | |||
| 361 | if ( !empty( $pod ) ) { |
||
| 362 | self::$object_identifier--; |
||
| 363 | |||
| 364 | self::$queue[ $type ][ $pod[ 'name' ] ] = $pod; |
||
| 365 | |||
| 366 | return $pod; |
||
| 367 | } |
||
| 368 | |||
| 369 | return false; |
||
| 370 | } |
||
| 371 | |||
| 372 | public function register_field ( $pod, $field ) { |
||
| 373 | View Code Duplication | if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) { |
|
| 374 | $data = array(); |
||
| 375 | |||
| 376 | foreach ( $pod as $p ) { |
||
| 377 | $data[] = $this->register_field( $p, $field ); |
||
| 378 | } |
||
| 379 | |||
| 380 | return $data; |
||
| 381 | } |
||
| 382 | |||
| 383 | View Code Duplication | if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $pod ) |
|
| 384 | self::$current_pod_data = pods_api()->load_pod( array( 'name' => $pod ), false ); |
||
| 385 | |||
| 386 | $pod = self::$current_pod_data; |
||
| 387 | |||
| 388 | if ( !empty( $pod ) ) { |
||
| 389 | $type = $pod[ 'type' ]; |
||
| 390 | |||
| 391 | if ( 'post_type' == $pod[ 'type' ] ) |
||
| 392 | $type = 'post_types'; |
||
| 393 | elseif ( 'taxonomy' == $pod[ 'type' ] ) |
||
| 394 | $type = 'taxonomies'; |
||
| 395 | elseif ( 'pod' == $pod[ 'type' ] ) |
||
| 396 | $type = 'advanced_content_types'; |
||
| 397 | |||
| 398 | if ( !isset( self::$queue[ $pod[ 'type' ] ] ) ) |
||
| 399 | self::$queue[ $type ] = array(); |
||
| 400 | |||
| 401 | $field = pods_api()->save_field( $field, false, false, $pod[ 'id' ] ); |
||
| 402 | |||
| 403 | if ( !empty( $field ) ) { |
||
| 404 | $pod[ 'fields' ][ $field[ 'name' ] ] = $field; |
||
| 405 | |||
| 406 | self::$queue[ $type ][ $pod[ 'name' ] ] = $pod; |
||
| 407 | |||
| 408 | return $field; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | return false; |
||
| 413 | } |
||
| 414 | |||
| 415 | public function integrations () { |
||
| 416 | // Codepress Admin Columns 2.x |
||
| 417 | add_filter( 'cac/storage_model/meta_keys', array( $this, 'cpac_meta_keys' ), 10, 2 ); |
||
| 418 | add_filter( 'cac/post_types', array( $this, 'cpac_post_types' ), 10, 1 ); |
||
| 419 | add_filter( 'cac/column/meta/value', array( $this, 'cpac_meta_value' ), 10, 3 ); |
||
| 420 | } |
||
| 421 | |||
| 422 | |||
| 423 | public function cpac_meta_keys ( $meta_fields, $storage_model ) { |
||
| 424 | $object_type = 'post_type'; |
||
| 425 | $object = $storage_model->key; |
||
| 426 | |||
| 427 | if ( in_array( $storage_model->key, array( 'wp-links', 'link' ) ) ) { |
||
| 428 | $object_type = $object = 'link'; |
||
| 429 | } |
||
| 430 | elseif ( in_array( $storage_model->key, array( 'wp-media', 'media' ) ) ) { |
||
| 431 | $object_type = $object = 'media'; |
||
| 432 | } |
||
| 433 | elseif ( in_array( $storage_model->key, array( 'wp-users', 'user' ) ) ) { |
||
| 434 | $object_type = $object = 'user'; |
||
| 435 | } |
||
| 436 | elseif ( in_array( $storage_model->key, array( 'wp-comments', 'comment' ) ) ) { |
||
| 437 | $object_type = $object = 'comment'; |
||
| 438 | } |
||
| 439 | elseif ( 'taxonomy' === $storage_model->type ) { |
||
| 440 | $object_type = 'taxonomy'; |
||
| 441 | $object = $storage_model->taxonomy; |
||
| 442 | } |
||
| 443 | |||
| 444 | View Code Duplication | if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object ) |
|
| 445 | self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false ); |
||
| 446 | |||
| 447 | $pod = self::$current_pod_data; |
||
| 448 | |||
| 449 | // Add Pods fields |
||
| 450 | if ( !empty( $pod ) && $object_type == $pod[ 'type' ] ) { |
||
| 451 | foreach ( $pod[ 'fields' ] as $field => $field_data ) { |
||
| 452 | if ( !is_array( $meta_fields ) ) |
||
| 453 | $meta_fields = array(); |
||
| 454 | |||
| 455 | if ( !in_array( $field, $meta_fields ) ) |
||
| 456 | $meta_fields[] = $field; |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | // Remove internal Pods fields |
||
| 461 | if ( is_array( $meta_fields ) ) { |
||
| 462 | foreach ( $meta_fields as $k => $meta_field ) { |
||
| 463 | if ( 0 === strpos( $meta_field, '_pods_' ) ) |
||
| 464 | unset( $meta_fields[ $k ] ); |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | return $meta_fields; |
||
| 469 | } |
||
| 470 | |||
| 471 | public function cpac_post_types ( $post_types ) { |
||
| 472 | // Remove internal Pods post types |
||
| 473 | foreach ( $post_types as $post_type => $post_type_name ) { |
||
| 474 | if ( 0 === strpos( $post_type, '_pods_' ) || 0 === strpos( $post_type_name, '_pods_' ) ) |
||
| 475 | unset( $post_types[ $post_type ] ); |
||
| 476 | } |
||
| 477 | |||
| 478 | return $post_types; |
||
| 479 | } |
||
| 480 | |||
| 481 | public function cpac_meta_value ( $meta, $id, $obj ) { |
||
| 482 | $tableless_field_types = PodsForm::tableless_field_types(); |
||
| 483 | |||
| 484 | $object_type = 'post_type'; |
||
| 485 | $object = $obj->storage_model->key; |
||
| 486 | |||
| 487 | if ( in_array( $obj->storage_model->type, array( 'wp-links', 'link' ) ) ) { |
||
| 488 | $object_type = $object = 'link'; |
||
| 489 | } |
||
| 490 | elseif ( in_array( $obj->storage_model->type, array( 'wp-media', 'media' ) ) ) { |
||
| 491 | $object_type = $object = 'media'; |
||
| 492 | } |
||
| 493 | elseif ( in_array( $obj->storage_model->type, array( 'wp-users', 'user' ) ) ) { |
||
| 494 | $object_type = $object = 'user'; |
||
| 495 | } |
||
| 496 | elseif ( in_array( $obj->storage_model->type, array( 'wp-comments', 'comment' ) ) ) { |
||
| 497 | $object_type = $object = 'comment'; |
||
| 498 | } |
||
| 499 | elseif ( 'taxonomy' === $obj->storage_model->type ) { |
||
| 500 | $object_type = 'taxonomy'; |
||
| 501 | $object = $obj->storage_model->taxonomy; |
||
| 502 | } |
||
| 503 | |||
| 504 | $field = substr( $obj->options->field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden', '', $obj->options->field ) : $obj->options->field; |
||
| 505 | $field_type = $obj->options->field_type; |
||
| 506 | |||
| 507 | View Code Duplication | if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object ) |
|
| 508 | self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false ); |
||
| 509 | |||
| 510 | $pod = self::$current_pod_data; |
||
| 511 | |||
| 512 | // Add Pods fields |
||
| 513 | if ( !empty( $pod ) && isset( $pod[ 'fields' ][ $field ] ) ) { |
||
| 514 | if ( in_array( $pod[ 'type' ], array( 'post_type', 'media', 'taxonomy', 'user', 'comment', 'media' ) ) && ( !empty( $field_type ) || in_array( $pod[ 'fields' ][ $field ][ 'type' ], $tableless_field_types ) ) ) { |
||
| 515 | $metadata_type = $pod['type']; |
||
| 516 | |||
| 517 | if ( in_array( $metadata_type, array( 'post_type', 'media' ) ) ) { |
||
| 518 | $metadata_type = 'post'; |
||
| 519 | } elseif ( 'taxonomy' == $metadata_type ) { |
||
| 520 | $metadata_type = 'term'; |
||
| 521 | } |
||
| 522 | |||
| 523 | if ( 'term' == $metadata_type && ! function_exists( 'get_term_meta' ) ) { |
||
| 524 | $podterms = pods( $pod['name'], $id ); |
||
| 525 | |||
| 526 | $meta = $podterms->field( $field ); |
||
| 527 | } else { |
||
| 528 | $meta = get_metadata( $metadata_type, $id, $field, true ); |
||
| 529 | } |
||
| 530 | } |
||
| 531 | elseif ( 'taxonomy' == $pod['type'] ) { |
||
| 532 | $podterms = pods( $pod['name'], $id ); |
||
| 533 | |||
| 534 | $meta = $podterms->field( $field ); |
||
| 535 | } |
||
| 536 | |||
| 537 | $meta = PodsForm::field_method( $pod[ 'fields' ][ $field ][ 'type' ], 'ui', $id, $meta, $field, array_merge( $pod[ 'fields' ][ $field ], $pod[ 'fields' ][ $field ][ 'options' ] ), $pod[ 'fields' ], $pod ); |
||
| 538 | } |
||
| 539 | |||
| 540 | return $meta; |
||
| 541 | } |
||
| 542 | |||
| 543 | public function cpac_meta_values ( $meta, $field_type, $field, $type, $id ) { |
||
| 544 | $tableless_field_types = PodsForm::tableless_field_types(); |
||
| 545 | |||
| 546 | $object = $type; |
||
| 547 | |||
| 548 | if ( 'wp-media' == $type ) |
||
| 549 | $object = 'media'; |
||
| 550 | elseif ( 'wp-users' == $type ) |
||
| 551 | $object = 'user'; |
||
| 552 | elseif ( 'wp-comments' == $type ) |
||
| 553 | $object = 'comment'; |
||
| 554 | |||
| 555 | View Code Duplication | if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object ) |
|
| 556 | self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false ); |
||
| 557 | |||
| 558 | $pod = self::$current_pod_data; |
||
| 559 | |||
| 560 | // Add Pods fields |
||
| 561 | if ( !empty( $pod ) && isset( $pod[ 'fields' ][ $field ] ) ) { |
||
| 562 | if ( in_array( $pod[ 'type' ], array( 'post_type', 'user', 'taxonomy', 'comment', 'media' ) ) && ( !empty( $field_type ) || in_array( $pod[ 'fields' ][ $field ][ 'type' ], $tableless_field_types ) ) ) { |
||
| 563 | $metadata_type = $pod['type']; |
||
| 564 | |||
| 565 | if ( in_array( $metadata_type, array( 'post_type', 'media' ) ) ) { |
||
| 566 | $metadata_type = 'post'; |
||
| 567 | } elseif ( 'taxonomy' == $metadata_type ) { |
||
| 568 | $metadata_type = 'term'; |
||
| 569 | } |
||
| 570 | |||
| 571 | $meta = get_metadata( $metadata_type, $id, $field, true ); |
||
| 572 | } |
||
| 573 | |||
| 574 | $meta = PodsForm::field_method( $pod[ 'fields' ][ $field ][ 'type' ], 'ui', $id, $meta, $field, array_merge( $pod[ 'fields' ][ $field ], $pod[ 'fields' ][ $field ][ 'options' ] ), $pod[ 'fields' ], $pod ); |
||
| 575 | } |
||
| 576 | |||
| 577 | return $meta; |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Add a meta group of fields to add/edit forms |
||
| 582 | * |
||
| 583 | * @param string|array $pod The pod or type of element to attach the group to. |
||
| 584 | * @param string $label Title of the edit screen section, visible to user. |
||
| 585 | * @param string|array $fields Either a comma separated list of text fields or an associative array containing field infomration. |
||
| 586 | * @param string $context (optional) The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side'). |
||
| 587 | * @param string $priority (optional) The priority within the context where the boxes should show ('high', 'core', 'default' or 'low'). |
||
| 588 | * |
||
| 589 | * @since 2.0 |
||
| 590 | * |
||
| 591 | * @return mixed|void |
||
| 592 | */ |
||
| 593 | public function group_add ( $pod, $label, $fields, $context = 'normal', $priority = 'default' ) { |
||
| 594 | View Code Duplication | if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) { |
|
| 595 | foreach ( $pod as $p ) { |
||
| 596 | $this->group_add( $pod, $label, $fields, $context, $priority ); |
||
| 597 | } |
||
| 598 | |||
| 599 | return true; |
||
| 600 | } |
||
| 601 | |||
| 602 | if ( !is_array( $pod ) ) { |
||
| 603 | View Code Duplication | if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $pod ) |
|
| 604 | self::$current_pod_data = pods_api()->load_pod( array( 'name' => $pod ), false ); |
||
| 605 | |||
| 606 | if ( !empty( self::$current_pod_data ) ) |
||
| 607 | $pod = self::$current_pod_data; |
||
| 608 | else { |
||
| 609 | $type = 'post_type'; |
||
| 610 | |||
| 611 | if ( in_array( $pod, array( 'media', 'user', 'comment' ) ) ) |
||
| 612 | $type = $pod; |
||
| 613 | |||
| 614 | $pod = array( |
||
| 615 | 'name' => $pod, |
||
| 616 | 'type' => $type |
||
| 617 | ); |
||
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | if ( is_array( $pod ) && !isset( $pod[ 'id' ] ) ) { |
||
| 622 | $defaults = array( |
||
| 623 | 'name' => '', |
||
| 624 | 'type' => 'post_type' |
||
| 625 | ); |
||
| 626 | |||
| 627 | $pod = array_merge( $defaults, $pod ); |
||
| 628 | } |
||
| 629 | |||
| 630 | if ( 'post' == $pod[ 'type' ] ) |
||
| 631 | $pod[ 'type' ] = 'post_type'; |
||
| 632 | |||
| 633 | if ( empty( $pod[ 'name' ] ) && isset( $pod[ 'object' ] ) && !empty( $pod[ 'object' ] ) ) |
||
| 634 | $pod[ 'name' ] = $pod[ 'object' ]; |
||
| 635 | View Code Duplication | elseif ( !isset( $pod[ 'object' ] ) || empty( $pod[ 'object' ] ) ) |
|
| 636 | $pod[ 'object' ] = $pod[ 'name' ]; |
||
| 637 | |||
| 638 | if ( empty( $pod[ 'object' ] ) ) |
||
| 639 | return pods_error( __( 'Object required to add a Pods meta group', 'pods' ) ); |
||
| 640 | |||
| 641 | $object_name = $pod[ 'object' ]; |
||
| 642 | |||
| 643 | if ( 'pod' == $pod[ 'type' ] ) |
||
| 644 | $object_name = $pod[ 'name' ]; |
||
| 645 | |||
| 646 | View Code Duplication | if ( !isset( self::$groups[ $pod[ 'type' ] ] ) ) |
|
| 647 | self::$groups[ $pod[ 'type' ] ] = array(); |
||
| 648 | |||
| 649 | View Code Duplication | if ( !isset( self::$groups[ $pod[ 'type' ] ][ $object_name ] ) ) |
|
| 650 | self::$groups[ $pod[ 'type' ] ][ $object_name ] = array(); |
||
| 651 | |||
| 652 | $_fields = array(); |
||
| 653 | |||
| 654 | if ( !is_array( $fields ) ) |
||
| 655 | $fields = explode( ',', $fields ); |
||
| 656 | |||
| 657 | foreach ( $fields as $k => $field ) { |
||
| 658 | $name = $k; |
||
| 659 | |||
| 660 | $defaults = array( |
||
| 661 | 'name' => $name, |
||
| 662 | 'label' => $name, |
||
| 663 | 'type' => 'text' |
||
| 664 | ); |
||
| 665 | |||
| 666 | if ( !is_array( $field ) ) { |
||
| 667 | $name = trim( $field ); |
||
| 668 | |||
| 669 | $field = array( |
||
| 670 | 'name' => $name, |
||
| 671 | 'label' => $name |
||
| 672 | ); |
||
| 673 | } |
||
| 674 | |||
| 675 | $field = array_merge( $defaults, $field ); |
||
| 676 | |||
| 677 | $field[ 'name' ] = trim( $field[ 'name' ] ); |
||
| 678 | |||
| 679 | View Code Duplication | if ( isset( $pod[ 'fields' ] ) && isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) ) |
|
| 680 | $field = array_merge( $field, $pod[ 'fields' ][ $field[ 'name' ] ] ); |
||
| 681 | |||
| 682 | $_fields[ $k ] = $field; |
||
| 683 | } |
||
| 684 | |||
| 685 | // Setup field options |
||
| 686 | $fields = PodsForm::fields_setup( $_fields ); |
||
| 687 | |||
| 688 | $group = array( |
||
| 689 | 'pod' => $pod, |
||
| 690 | 'label' => $label, |
||
| 691 | 'fields' => $fields, |
||
| 692 | 'context' => $context, |
||
| 693 | 'priority' => $priority |
||
| 694 | ); |
||
| 695 | |||
| 696 | // Filter group data, pass vars separately for reference down the line (in case array changed by other filter) |
||
| 697 | $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ] . '_' . $object_name, $group, $pod, $label, $fields ); |
||
| 698 | $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ], $group, $pod, $label, $fields ); |
||
| 699 | $group = apply_filters( 'pods_meta_group_add', $group, $pod, $label, $fields ); |
||
| 700 | |||
| 701 | self::$groups[ $pod[ 'type' ] ][ $object_name ][] = $group; |
||
| 702 | |||
| 703 | // Hook it up! |
||
| 704 | if ( 'post_type' == $pod[ 'type' ] ) { |
||
| 705 | if ( !has_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ) ) |
||
| 706 | add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ); |
||
| 707 | |||
| 708 | /*if ( !has_action( 'save_post', array( $this, 'save_post' ), 10, 3 ) ) |
||
| 709 | add_action( 'save_post', array( $this, 'save_post' ), 10, 3 );*/ |
||
| 710 | } |
||
| 711 | elseif ( 'taxonomy' == $pod[ 'type' ] ) { |
||
| 712 | if ( !has_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 ) ) { |
||
| 713 | add_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 ); |
||
| 714 | add_action( $pod[ 'object' ] . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 ); |
||
| 715 | } |
||
| 716 | |||
| 717 | if ( !has_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 ) ) { |
||
| 718 | add_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 ); |
||
| 719 | add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 ); |
||
| 720 | } |
||
| 721 | } |
||
| 722 | elseif ( 'media' == $pod[ 'type' ] ) { |
||
| 723 | if ( !has_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 ) ) { |
||
| 724 | View Code Duplication | if ( pods_version_check( 'wp', '3.5' ) ) { |
|
| 725 | add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ); |
||
| 726 | add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 ); |
||
| 727 | } |
||
| 728 | |||
| 729 | add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 ); |
||
| 730 | |||
| 731 | add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 ); |
||
| 732 | add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 ); |
||
| 733 | } |
||
| 734 | } |
||
| 735 | elseif ( 'user' == $pod[ 'type' ] ) { |
||
| 736 | if ( !has_action( 'show_user_profile', array( $this, 'meta_user' ) ) ) { |
||
| 737 | add_action( 'show_user_profile', array( $this, 'meta_user' ) ); |
||
| 738 | add_action( 'edit_user_profile', array( $this, 'meta_user' ) ); |
||
| 739 | //add_action( 'user_register', array( $this, 'save_user' ) ); |
||
| 740 | add_action( 'profile_update', array( $this, 'save_user' ) ); |
||
| 741 | } |
||
| 742 | } |
||
| 743 | elseif ( 'comment' == $pod[ 'type' ] ) { |
||
| 744 | if ( !has_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 ) ) { |
||
| 745 | add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 ); |
||
| 746 | add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) ); |
||
| 747 | add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) ); |
||
| 748 | add_action( 'wp_insert_comment', array( $this, 'save_comment' ) ); |
||
| 749 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
| 750 | } |
||
| 751 | } |
||
| 752 | } |
||
| 753 | |||
| 754 | public function object_get ( $type, $name ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * @param $type |
||
| 803 | * @param $name |
||
| 804 | * @param $default_fields |
||
| 805 | * |
||
| 806 | * @return array |
||
| 807 | */ |
||
| 808 | public function groups_get ( $type, $name, $default_fields = null ) { |
||
| 903 | |||
| 904 | /** |
||
| 905 | * @param $post_type |
||
| 906 | * @param null $post |
||
| 907 | */ |
||
| 908 | public function meta_post_add ( $post_type, $post = null ) { |
||
| 909 | if ( 'comment' == $post_type ) |
||
| 910 | return; |
||
| 911 | |||
| 912 | if ( is_object( $post ) ) |
||
| 913 | $post_type = $post->post_type; |
||
| 914 | |||
| 915 | $groups = $this->groups_get( 'post_type', $post_type ); |
||
| 916 | $pods_field_found = false; |
||
| 917 | |||
| 918 | foreach ( $groups as $group ) { |
||
| 919 | if ( empty( $group[ 'fields' ] ) ) |
||
| 920 | continue; |
||
| 921 | |||
| 922 | $field_found = false; |
||
| 923 | $group_hidden = true; |
||
| 924 | |||
| 925 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 926 | if ( false !== PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ] ) ) { |
||
| 927 | $field_found = true; |
||
| 928 | } |
||
| 929 | if ( ! isset( $field['options']['hidden'] ) || 1 != (int) $field['options']['hidden'] ) { |
||
| 930 | $group_hidden = false; |
||
| 931 | } |
||
| 932 | } |
||
| 933 | |||
| 934 | if ( $group_hidden ) |
||
| 935 | continue; |
||
| 936 | |||
| 937 | if ( empty( $group[ 'label' ] ) ) |
||
| 938 | $group[ 'label' ] = get_post_type_object( $post_type )->labels->label; |
||
| 939 | |||
| 940 | View Code Duplication | if ( $field_found ) { |
|
| 941 | $pods_field_found = true; |
||
| 942 | add_meta_box( |
||
| 943 | 'pods-meta-' . sanitize_title( $group[ 'label' ] ), |
||
| 944 | $group[ 'label' ], |
||
| 945 | array( $this, 'meta_post' ), |
||
| 946 | $post_type, |
||
| 947 | $group[ 'context' ], |
||
| 948 | $group[ 'priority' ], |
||
| 949 | array( 'group' => $group ) |
||
| 950 | ); |
||
| 951 | |||
| 952 | } |
||
| 953 | } |
||
| 954 | |||
| 955 | if ( $pods_field_found ) { |
||
| 956 | // Only add the classes to forms that actually have pods fields |
||
| 957 | add_action( 'post_edit_form_tag', array( $this, 'add_class_submittable' ) ); |
||
| 958 | } |
||
| 959 | } |
||
| 960 | |||
| 961 | /** |
||
| 962 | * |
||
| 963 | * Called by 'post_edit_form_tag' action to include the classes in the <form> tag |
||
| 964 | * |
||
| 965 | */ |
||
| 966 | public function add_class_submittable () { |
||
| 967 | echo ' class="pods-submittable pods-form"'; |
||
| 968 | } |
||
| 969 | |||
| 970 | /** |
||
| 971 | * @param $post |
||
| 972 | * @param $metabox |
||
| 973 | */ |
||
| 974 | public function meta_post ( $post, $metabox ) { |
||
| 975 | wp_enqueue_style( 'pods-form' ); |
||
| 976 | wp_enqueue_script( 'pods' ); |
||
| 977 | |||
| 978 | $pod_type = 'post'; |
||
| 979 | |||
| 980 | if ( 'attachment' == $post->post_type ) { |
||
| 981 | $pod_type = 'media'; |
||
| 982 | } |
||
| 983 | |||
| 984 | do_action( 'pods_meta_' . __FUNCTION__, $post ); |
||
| 985 | |||
| 986 | $hidden_fields = array(); |
||
| 987 | |||
| 988 | $id = null; |
||
| 989 | |||
| 990 | if ( is_object( $post ) && false === strpos( $_SERVER[ 'REQUEST_URI' ], '/post-new.php' ) ) |
||
| 991 | $id = $post->ID; |
||
| 992 | |||
| 993 | View Code Duplication | if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod ) || self::$current_pod->pod != $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ] ) { |
|
| 994 | self::$current_pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true ); |
||
| 995 | } elseif ( self::$current_pod->id() != $id ) { |
||
| 996 | self::$current_pod->fetch( $id ); |
||
| 997 | } |
||
| 998 | |||
| 999 | $pod = self::$current_pod; |
||
| 1000 | |||
| 1001 | $fields = $metabox['args']['group']['fields']; |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Filter the fields used for the Pods metabox group |
||
| 1005 | * |
||
| 1006 | * @since 2.6.6 |
||
| 1007 | * |
||
| 1008 | * @param array $fields Fields from the current Pod metabox group |
||
| 1009 | * @param int $id Post ID |
||
| 1010 | * @param WP_Post $post Post object |
||
| 1011 | * @param array $metabox Metabox args from the current Pod metabox group |
||
| 1012 | * @param Pods $pod Pod object |
||
| 1013 | */ |
||
| 1014 | $fields = apply_filters( 'pods_meta_post_fields', $fields, $id, $post, $metabox, $pod ); |
||
| 1015 | |||
| 1016 | if ( empty( $fields ) ) { |
||
| 1017 | _e( 'There are no fields to display', 'pods' ); |
||
| 1018 | |||
| 1019 | return; |
||
| 1020 | } |
||
| 1021 | ?> |
||
| 1022 | <table class="form-table pods-metabox pods-admin pods-dependency"> |
||
| 1023 | <?php |
||
| 1024 | echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_' . $pod_type ), 'hidden' ); |
||
| 1025 | |||
| 1026 | foreach ( $fields as $field ) { |
||
| 1027 | View Code Duplication | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field[ 'options' ], $fields, $pod, $id ) ) { |
|
| 1028 | if ( pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1029 | $field[ 'type' ] = 'hidden'; |
||
| 1030 | else |
||
| 1031 | continue; |
||
| 1032 | } |
||
| 1033 | elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1034 | $field[ 'type' ] = 'hidden'; |
||
| 1035 | |||
| 1036 | $value = ''; |
||
| 1037 | |||
| 1038 | View Code Duplication | if ( !empty( $pod ) ) { |
|
| 1039 | pods_no_conflict_on( 'post' ); |
||
| 1040 | |||
| 1041 | $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) ); |
||
| 1042 | |||
| 1043 | pods_no_conflict_off( 'post' ); |
||
| 1044 | } |
||
| 1045 | elseif ( !empty( $id ) ) |
||
| 1046 | $value = get_post_meta( $id, $field[ 'name' ], true ); |
||
| 1047 | |||
| 1048 | if ( 'hidden' == $field[ 'type' ] ) { |
||
| 1049 | $hidden_fields[] = array( |
||
| 1050 | 'field' => $field, |
||
| 1051 | 'value' => $value |
||
| 1052 | ); |
||
| 1053 | } |
||
| 1054 | else { |
||
| 1055 | $depends = PodsForm::dependencies( $field, 'pods-meta-' ); |
||
| 1056 | |||
| 1057 | do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field[ 'name' ], $post, $field, $pod ); |
||
| 1058 | ?> |
||
| 1059 | <tr class="form-field pods-field pods-field-input <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?> <?php echo esc_attr( $depends ); ?>"> |
||
| 1060 | <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th> |
||
| 1061 | <td> |
||
| 1062 | <?php |
||
| 1063 | // Remove any extra ? help icons |
||
| 1064 | if ( isset( $field[ 'help' ] ) ) |
||
| 1065 | unset( $field[ 'help' ] ); |
||
| 1066 | ?> |
||
| 1067 | <div class="pods-submittable-fields"> |
||
| 1068 | <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?> |
||
| 1069 | <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?> |
||
| 1070 | </div> |
||
| 1071 | </td> |
||
| 1072 | </tr> |
||
| 1073 | <?php |
||
| 1074 | do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field[ 'name' ] . '_post', $post, $field, $pod ); |
||
| 1075 | } |
||
| 1076 | } |
||
| 1077 | ?> |
||
| 1078 | </table> |
||
| 1079 | |||
| 1080 | <?php |
||
| 1081 | do_action( 'pods_meta_' . __FUNCTION__ . '_post', $post ); |
||
| 1082 | |||
| 1083 | View Code Duplication | foreach ( $hidden_fields as $hidden_field ) { |
|
| 1084 | $field = $hidden_field[ 'field' ]; |
||
| 1085 | |||
| 1086 | echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' ); |
||
| 1087 | } |
||
| 1088 | ?> |
||
| 1089 | |||
| 1090 | <script type="text/javascript"> |
||
| 1091 | jQuery( function ( $ ) { |
||
| 1092 | $( document ).Pods( 'validate' ); |
||
| 1093 | $( document ).Pods( 'submit_meta' ); |
||
| 1094 | $( document ).Pods( 'dependency', true ); |
||
| 1095 | } ); |
||
| 1096 | </script> |
||
| 1097 | <?php |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * @param $new_status |
||
| 1102 | * @param $old_status |
||
| 1103 | * @param $post |
||
| 1104 | */ |
||
| 1105 | public function save_post_detect_new ( $new_status, $old_status, $post ) { |
||
| 1106 | self::$old_post_status = $old_status; |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * @param $post_id |
||
| 1111 | * @param $post |
||
| 1112 | * @param $update |
||
| 1113 | * |
||
| 1114 | * @return int Post ID |
||
| 1115 | */ |
||
| 1116 | public function save_post ( $post_id, $post, $update = null ) { |
||
| 1117 | $is_new_item = false; |
||
| 1118 | |||
| 1119 | if ( is_bool( $update ) ) |
||
| 1120 | $is_new_item = !$update; // false is new item |
||
| 1121 | elseif ( 'new' == self::$old_post_status ) { |
||
| 1122 | $is_new_item = true; |
||
| 1123 | } |
||
| 1124 | |||
| 1125 | View Code Duplication | if ( empty( $_POST ) ) { |
|
| 1126 | return $post_id; |
||
| 1127 | } |
||
| 1128 | elseif ( !$is_new_item && !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_post' ) ) { |
||
| 1129 | return $post_id; |
||
| 1130 | } |
||
| 1131 | |||
| 1132 | // Reset to avoid manual new post issues |
||
| 1133 | self::$old_post_status = ''; |
||
| 1134 | |||
| 1135 | $blacklisted_types = array( |
||
| 1136 | 'revision', |
||
| 1137 | '_pods_pod', |
||
| 1138 | '_pods_field' |
||
| 1139 | ); |
||
| 1140 | |||
| 1141 | $blacklisted_types = apply_filters( 'pods_meta_save_post_blacklist_types', $blacklisted_types, $post_id, $post ); |
||
| 1142 | |||
| 1143 | // @todo Figure out how to hook into autosave for saving meta |
||
| 1144 | |||
| 1145 | // Block Autosave and Revisions |
||
| 1146 | if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || in_array( $post->post_type, $blacklisted_types ) ) |
||
| 1147 | return $post_id; |
||
| 1148 | |||
| 1149 | // Block Quick Edits / Bulk Edits |
||
| 1150 | if ( 'edit.php' == pods_var( 'pagenow', 'global' ) && ( 'inline-save' == pods_var( 'action', 'post' ) || null != pods_var( 'bulk_edit', 'get' ) || is_array( pods_var( 'post', 'get' ) ) ) ) |
||
| 1151 | return $post_id; |
||
| 1152 | |||
| 1153 | // Block Trash |
||
| 1154 | if ( in_array( pods_var( 'action', 'get' ), array( 'untrash', 'trash' ) ) ) |
||
| 1155 | return $post_id; |
||
| 1156 | |||
| 1157 | // Block Auto-drafting and Trash (not via Admin action) |
||
| 1158 | $blacklisted_status = array( |
||
| 1159 | 'auto-draft', |
||
| 1160 | 'trash' |
||
| 1161 | ); |
||
| 1162 | |||
| 1163 | $blacklisted_status = apply_filters( 'pods_meta_save_post_blacklist_status', $blacklisted_status, $post_id, $post ); |
||
| 1164 | |||
| 1165 | if ( in_array( $post->post_status, $blacklisted_status ) ) |
||
| 1166 | return $post_id; |
||
| 1167 | |||
| 1168 | $groups = $this->groups_get( 'post_type', $post->post_type ); |
||
| 1169 | |||
| 1170 | if ( empty( $groups ) ) |
||
| 1171 | return $post_id; |
||
| 1172 | |||
| 1173 | $data = array(); |
||
| 1174 | |||
| 1175 | $id = $post_id; |
||
| 1176 | $pod = null; |
||
| 1177 | |||
| 1178 | View Code Duplication | foreach ( $groups as $group ) { |
|
| 1179 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1180 | continue; |
||
| 1181 | |||
| 1182 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1183 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) |
||
| 1184 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1185 | elseif ( self::$current_pod->id() != $id ) |
||
| 1186 | self::$current_pod->fetch( $id ); |
||
| 1187 | |||
| 1188 | $pod = self::$current_pod; |
||
| 1189 | } |
||
| 1190 | |||
| 1191 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1192 | |||
| 1193 | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
||
| 1194 | if ( !pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1195 | continue; |
||
| 1196 | } |
||
| 1197 | |||
| 1198 | $data[ $field[ 'name' ] ] = ''; |
||
| 1199 | |||
| 1200 | if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) ) |
||
| 1201 | $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ]; |
||
| 1202 | } |
||
| 1203 | } |
||
| 1204 | |||
| 1205 | View Code Duplication | if ( $is_new_item ) { |
|
| 1206 | do_action( 'pods_meta_create_pre_post', $data, $pod, $id, $groups, $post, $post->post_type ); |
||
| 1207 | do_action( "pods_meta_create_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post ); |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | do_action( 'pods_meta_save_pre_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item ); |
||
| 1211 | do_action( "pods_meta_save_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item ); |
||
| 1212 | |||
| 1213 | pods_no_conflict_on( 'post' ); |
||
| 1214 | |||
| 1215 | View Code Duplication | if ( !empty( $pod ) ) { |
|
| 1216 | // Fix for Pods doing it's own sanitization |
||
| 1217 | $data = pods_unslash( (array) $data ); |
||
| 1218 | |||
| 1219 | $pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) ); |
||
| 1220 | } |
||
| 1221 | elseif ( !empty( $id ) ) { |
||
| 1222 | foreach ( $data as $field => $value ) { |
||
| 1223 | update_post_meta( $id, $field, $value ); |
||
| 1224 | } |
||
| 1225 | } |
||
| 1226 | |||
| 1227 | pods_no_conflict_off( 'post' ); |
||
| 1228 | |||
| 1229 | View Code Duplication | if ( $is_new_item ) { |
|
| 1230 | do_action( 'pods_meta_create_post', $data, $pod, $id, $groups, $post, $post->post_type ); |
||
| 1231 | do_action( "pods_meta_create_post_{$post->post_type}", $data, $pod, $id, $groups, $post ); |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | do_action( 'pods_meta_save_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item ); |
||
| 1235 | do_action( "pods_meta_save_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item ); |
||
| 1236 | |||
| 1237 | return $post_id; |
||
| 1238 | |||
| 1239 | } |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * @param $form_fields |
||
| 1243 | * @param $post |
||
| 1244 | * |
||
| 1245 | * @return array |
||
| 1246 | */ |
||
| 1247 | public function meta_media ( $form_fields, $post ) { |
||
| 1248 | $groups = $this->groups_get( 'media', 'media' ); |
||
| 1249 | |||
| 1250 | if ( empty( $groups ) || 'attachment' == pods_var( 'typenow', 'global' ) ) |
||
| 1251 | return $form_fields; |
||
| 1252 | |||
| 1253 | wp_enqueue_style( 'pods-form' ); |
||
| 1254 | |||
| 1255 | $id = null; |
||
| 1256 | |||
| 1257 | if ( is_object( $post ) ) |
||
| 1258 | $id = $post->ID; |
||
| 1259 | |||
| 1260 | $pod = null; |
||
| 1261 | |||
| 1262 | $meta_nonce = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_media' ), 'hidden' ); |
||
| 1263 | |||
| 1264 | foreach ( $groups as $group ) { |
||
| 1265 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1266 | continue; |
||
| 1267 | |||
| 1268 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1269 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) |
||
| 1270 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1271 | elseif ( self::$current_pod->id() != $id ) |
||
| 1272 | self::$current_pod->fetch( $id ); |
||
| 1273 | |||
| 1274 | $pod = self::$current_pod; |
||
| 1275 | } |
||
| 1276 | |||
| 1277 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1278 | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
||
| 1279 | if ( !pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1280 | continue; |
||
| 1281 | } |
||
| 1282 | |||
| 1283 | $value = ''; |
||
| 1284 | |||
| 1285 | View Code Duplication | if ( !empty( $pod ) ) |
|
| 1286 | $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) ); |
||
| 1287 | elseif ( !empty( $id ) ) { |
||
| 1288 | pods_no_conflict_on( 'post' ); |
||
| 1289 | |||
| 1290 | $value = get_post_meta( $id, $field[ 'name' ], true ); |
||
| 1291 | |||
| 1292 | pods_no_conflict_off( 'post' ); |
||
| 1293 | } |
||
| 1294 | |||
| 1295 | $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = array( |
||
| 1296 | 'label' => $field[ 'label' ], |
||
| 1297 | 'input' => 'html', |
||
| 1298 | 'html' => PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ) . $meta_nonce, |
||
| 1299 | 'helps' => PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ) |
||
| 1300 | ); |
||
| 1301 | } |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | $form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields ); |
||
| 1305 | |||
| 1306 | return $form_fields; |
||
| 1307 | } |
||
| 1308 | |||
| 1309 | /** |
||
| 1310 | * @param $post |
||
| 1311 | * @param $attachment |
||
| 1312 | * |
||
| 1313 | * @return mixed |
||
| 1314 | */ |
||
| 1315 | public function save_media ( $post, $attachment ) { |
||
| 1316 | $groups = $this->groups_get( 'media', 'media' ); |
||
| 1317 | |||
| 1318 | if ( empty( $groups ) ) |
||
| 1319 | return $post; |
||
| 1320 | |||
| 1321 | $post_id = $attachment; |
||
| 1322 | |||
| 1323 | if ( empty( $_POST ) || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_media' ) ) { |
||
| 1324 | return $post; |
||
| 1325 | } |
||
| 1326 | |||
| 1327 | View Code Duplication | if ( is_array( $post ) && !empty( $post ) && isset( $post[ 'ID' ] ) && 'attachment' == $post[ 'post_type' ] ) |
|
| 1328 | $post_id = $post[ 'ID' ]; |
||
| 1329 | |||
| 1330 | if ( is_array( $post_id ) || empty( $post_id ) ) |
||
| 1331 | return $post; |
||
| 1332 | |||
| 1333 | $data = array(); |
||
| 1334 | |||
| 1335 | $id = $post_id; |
||
| 1336 | $pod = null; |
||
| 1337 | |||
| 1338 | View Code Duplication | foreach ( $groups as $group ) { |
|
| 1339 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1340 | continue; |
||
| 1341 | |||
| 1342 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1343 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) |
||
| 1344 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1345 | elseif ( self::$current_pod->id() != $id ) |
||
| 1346 | self::$current_pod->fetch( $id ); |
||
| 1347 | |||
| 1348 | $pod = self::$current_pod; |
||
| 1349 | } |
||
| 1350 | |||
| 1351 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1352 | |||
| 1353 | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
||
| 1354 | if ( !pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1355 | continue; |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | $data[ $field[ 'name' ] ] = ''; |
||
| 1359 | |||
| 1360 | if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) ) |
||
| 1361 | $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ]; |
||
| 1362 | } |
||
| 1363 | } |
||
| 1364 | |||
| 1365 | do_action( 'pods_meta_save_pre_media', $data, $pod, $id, $groups, $post, $attachment ); |
||
| 1366 | |||
| 1367 | View Code Duplication | if ( !empty( $pod ) ) { |
|
| 1368 | // Fix for Pods doing it's own sanitization |
||
| 1369 | $data = pods_unslash( (array) $data ); |
||
| 1370 | |||
| 1371 | $pod->save( $data ); |
||
| 1372 | } |
||
| 1373 | elseif ( !empty( $id ) ) { |
||
| 1374 | pods_no_conflict_on( 'post' ); |
||
| 1375 | |||
| 1376 | foreach ( $data as $field => $value ) { |
||
| 1377 | update_post_meta( $id, $field, $value ); |
||
| 1378 | } |
||
| 1379 | |||
| 1380 | pods_no_conflict_off( 'post' ); |
||
| 1381 | } |
||
| 1382 | |||
| 1383 | do_action( 'pods_meta_save_media', $data, $pod, $id, $groups, $post, $attachment ); |
||
| 1384 | |||
| 1385 | return $post; |
||
| 1386 | } |
||
| 1387 | |||
| 1388 | public function save_media_ajax () { |
||
| 1389 | if ( !isset( $_POST[ 'id' ] ) || empty( $_POST[ 'id' ] ) || absint( $_POST[ 'id' ] ) < 1 ) |
||
| 1390 | return; |
||
| 1391 | |||
| 1392 | $id = absint( $_POST[ 'id' ] ); |
||
| 1393 | |||
| 1394 | if ( !isset( $_POST[ 'nonce' ] ) || empty( $_POST[ 'nonce' ] ) ) |
||
| 1395 | return; |
||
| 1396 | |||
| 1397 | check_ajax_referer( 'update-post_' . $id, 'nonce' ); |
||
| 1398 | |||
| 1399 | if ( !current_user_can( 'edit_post', $id ) ) |
||
| 1400 | return; |
||
| 1401 | |||
| 1402 | $post = get_post( $id, ARRAY_A ); |
||
| 1403 | |||
| 1404 | if ( 'attachment' != $post[ 'post_type' ] ) |
||
| 1405 | return; |
||
| 1406 | |||
| 1407 | // fix ALL THE THINGS |
||
| 1408 | |||
| 1409 | if ( !isset( $_REQUEST[ 'attachments' ] ) ) |
||
| 1410 | $_REQUEST[ 'attachments' ] = array(); |
||
| 1411 | |||
| 1412 | if ( !isset( $_REQUEST[ 'attachments' ][ $id ] ) ) |
||
| 1413 | $_REQUEST[ 'attachments' ][ $id ] = array(); |
||
| 1414 | |||
| 1415 | if ( empty( $_REQUEST[ 'attachments' ][ $id ] ) ) |
||
| 1416 | $_REQUEST[ 'attachments' ][ $id ][ '_fix_wp' ] = 1; |
||
| 1417 | } |
||
| 1418 | |||
| 1419 | /** |
||
| 1420 | * @param $tag |
||
| 1421 | * @param null $taxonomy |
||
| 1422 | */ |
||
| 1423 | public function meta_taxonomy ( $tag, $taxonomy = null ) { |
||
| 1424 | wp_enqueue_style( 'pods-form' ); |
||
| 1425 | |||
| 1426 | do_action( 'pods_meta_' . __FUNCTION__, $tag, $taxonomy ); |
||
| 1427 | |||
| 1428 | $taxonomy_name = $taxonomy; |
||
| 1429 | |||
| 1430 | if ( !is_object( $tag ) ) |
||
| 1431 | $taxonomy_name = $tag; |
||
| 1432 | |||
| 1433 | $groups = $this->groups_get( 'taxonomy', $taxonomy_name ); |
||
| 1434 | |||
| 1435 | $id = null; |
||
| 1436 | |||
| 1437 | if ( is_object( $tag ) ) |
||
| 1438 | $id = $tag->term_id; |
||
| 1439 | |||
| 1440 | $pod = null; |
||
| 1441 | |||
| 1442 | echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_taxonomy' ), 'hidden' ); |
||
| 1443 | |||
| 1444 | foreach ( $groups as $group ) { |
||
| 1445 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1446 | continue; |
||
| 1447 | |||
| 1448 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1449 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) |
||
| 1450 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1451 | elseif ( self::$current_pod->id() != $id ) |
||
| 1452 | self::$current_pod->fetch( $id ); |
||
| 1453 | |||
| 1454 | $pod = self::$current_pod; |
||
| 1455 | } |
||
| 1456 | |||
| 1457 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1458 | View Code Duplication | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
|
| 1459 | if ( pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1460 | $field[ 'type' ] = 'hidden'; |
||
| 1461 | else |
||
| 1462 | continue; |
||
| 1463 | } |
||
| 1464 | elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1465 | $field[ 'type' ] = 'hidden'; |
||
| 1466 | |||
| 1467 | $value = ''; |
||
| 1468 | |||
| 1469 | View Code Duplication | if ( !empty( $pod ) ) |
|
| 1470 | $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) ); |
||
| 1471 | |||
| 1472 | if ( !is_object( $tag ) ) { |
||
| 1473 | ?> |
||
| 1474 | <div class="form-field pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>"> |
||
| 1475 | <?php |
||
| 1476 | echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); |
||
| 1477 | echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); |
||
| 1478 | echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); |
||
| 1479 | ?> |
||
| 1480 | </div> |
||
| 1481 | <?php |
||
| 1482 | } |
||
| 1483 | else { |
||
| 1484 | ?> |
||
| 1485 | <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>"> |
||
| 1486 | <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th> |
||
| 1487 | <td> |
||
| 1488 | <?php |
||
| 1489 | echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); |
||
| 1490 | echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); |
||
| 1491 | ?> |
||
| 1492 | </td> |
||
| 1493 | </tr> |
||
| 1494 | <?php |
||
| 1495 | } |
||
| 1496 | } |
||
| 1497 | } |
||
| 1498 | |||
| 1499 | do_action( 'pods_meta_' . __FUNCTION__ . '_post', $tag, $taxonomy ); |
||
| 1500 | } |
||
| 1501 | |||
| 1502 | /** |
||
| 1503 | * @param $term_id |
||
| 1504 | * @param $term_taxonomy_id |
||
| 1505 | * @param $taxonomy |
||
| 1506 | */ |
||
| 1507 | public function save_taxonomy ( $term_id, $term_taxonomy_id, $taxonomy ) { |
||
| 1508 | $is_new_item = false; |
||
| 1509 | |||
| 1510 | if ( 'create_term' == current_filter() ) |
||
| 1511 | $is_new_item = true; |
||
| 1512 | |||
| 1513 | if ( empty( $_POST ) || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_taxonomy' ) ) { |
||
| 1514 | return $term_id; |
||
| 1515 | } |
||
| 1516 | |||
| 1517 | // Block Quick Edits / Bulk Edits |
||
| 1518 | if ( 'inline-save-tax' == pods_var( 'action', 'post' ) || null != pods_var( 'delete_tags', 'post' ) ) { |
||
| 1519 | return $term_id; |
||
| 1520 | } |
||
| 1521 | |||
| 1522 | $groups = $this->groups_get( 'taxonomy', $taxonomy ); |
||
| 1523 | |||
| 1524 | if ( empty( $groups ) ) |
||
| 1525 | return $term_id; |
||
| 1526 | |||
| 1527 | $term = null; |
||
| 1528 | |||
| 1529 | $id = $term_id; |
||
| 1530 | $pod = null; |
||
| 1531 | |||
| 1532 | $has_fields = false; |
||
| 1533 | |||
| 1534 | foreach ( $groups as $group ) { |
||
| 1535 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1536 | continue; |
||
| 1537 | |||
| 1538 | if ( null === $term ) { |
||
| 1539 | $term = get_term( $term_id, $taxonomy ); |
||
| 1540 | |||
| 1541 | $data = array( |
||
| 1542 | 'name' => $term->name |
||
| 1543 | ); |
||
| 1544 | } |
||
| 1545 | |||
| 1546 | $has_fields = true; |
||
| 1547 | |||
| 1548 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1549 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) |
||
| 1550 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1551 | elseif ( self::$current_pod->id() != $id ) |
||
| 1552 | self::$current_pod->fetch( $id ); |
||
| 1553 | |||
| 1554 | $pod = self::$current_pod; |
||
| 1555 | } |
||
| 1556 | |||
| 1557 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1558 | |||
| 1559 | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
||
| 1560 | if ( !pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1561 | continue; |
||
| 1562 | } |
||
| 1563 | |||
| 1564 | $data[ $field[ 'name' ] ] = ''; |
||
| 1565 | |||
| 1566 | if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) ) |
||
| 1567 | $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ]; |
||
| 1568 | } |
||
| 1569 | } |
||
| 1570 | |||
| 1571 | if ( !$has_fields ) { |
||
| 1572 | return $term_id; |
||
| 1573 | } |
||
| 1574 | |||
| 1575 | View Code Duplication | if ( $is_new_item ) { |
|
| 1576 | do_action( 'pods_meta_create_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy ); |
||
| 1577 | do_action( "pods_meta_create_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy ); |
||
| 1578 | } |
||
| 1579 | |||
| 1580 | do_action( 'pods_meta_save_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item ); |
||
| 1581 | do_action( "pods_meta_save_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item ); |
||
| 1582 | |||
| 1583 | pods_no_conflict_on( 'taxonomy' ); |
||
| 1584 | |||
| 1585 | if ( !empty( $pod ) ) { |
||
| 1586 | // Fix for Pods doing it's own sanitization |
||
| 1587 | $data = pods_unslash( (array) $data ); |
||
| 1588 | |||
| 1589 | $pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) ); |
||
| 1590 | } |
||
| 1591 | |||
| 1592 | pods_no_conflict_off( 'taxonomy' ); |
||
| 1593 | |||
| 1594 | View Code Duplication | if ( $is_new_item ) { |
|
| 1595 | do_action( 'pods_meta_create_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy ); |
||
| 1596 | do_action( "pods_meta_create_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy ); |
||
| 1597 | } |
||
| 1598 | |||
| 1599 | do_action( 'pods_meta_save_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item ); |
||
| 1600 | do_action( "pods_meta_save_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item ); |
||
| 1601 | |||
| 1602 | return $term_id; |
||
| 1603 | } |
||
| 1604 | |||
| 1605 | /** |
||
| 1606 | * @param $user_id |
||
| 1607 | */ |
||
| 1608 | public function meta_user ( $user_id ) { |
||
| 1609 | wp_enqueue_style( 'pods-form' ); |
||
| 1610 | |||
| 1611 | do_action( 'pods_meta_' . __FUNCTION__, $user_id ); |
||
| 1612 | |||
| 1613 | $groups = $this->groups_get( 'user', 'user' ); |
||
| 1614 | |||
| 1615 | if ( is_object( $user_id ) ) |
||
| 1616 | $user_id = $user_id->ID; |
||
| 1617 | |||
| 1618 | $id = $user_id; |
||
| 1619 | $pod = null; |
||
| 1620 | |||
| 1621 | foreach ( $groups as $group ) { |
||
| 1622 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1623 | continue; |
||
| 1624 | |||
| 1625 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1626 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) |
||
| 1627 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1628 | elseif ( self::$current_pod->id() != $id ) |
||
| 1629 | self::$current_pod->fetch( $id ); |
||
| 1630 | |||
| 1631 | $pod = self::$current_pod; |
||
| 1632 | } |
||
| 1633 | |||
| 1634 | $hidden_fields = array(); |
||
| 1635 | ?> |
||
| 1636 | <h3><?php echo $group[ 'label' ]; ?></h3> |
||
| 1637 | |||
| 1638 | <?php echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_user' ), 'hidden' ); ?> |
||
| 1639 | |||
| 1640 | <table class="form-table pods-meta"> |
||
| 1641 | <tbody> |
||
| 1642 | <?php |
||
| 1643 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1644 | |||
| 1645 | View Code Duplication | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
|
| 1646 | if ( pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1647 | $field[ 'type' ] = 'hidden'; |
||
| 1648 | else |
||
| 1649 | continue; |
||
| 1650 | } |
||
| 1651 | elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1652 | $field[ 'type' ] = 'hidden'; |
||
| 1653 | |||
| 1654 | $value = ''; |
||
| 1655 | |||
| 1656 | View Code Duplication | if ( !empty( $pod ) ) |
|
| 1657 | $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) ); |
||
| 1658 | elseif ( !empty( $id ) ) { |
||
| 1659 | pods_no_conflict_on( 'user' ); |
||
| 1660 | |||
| 1661 | $value = get_user_meta( $id, $field[ 'name' ], true ); |
||
| 1662 | |||
| 1663 | pods_no_conflict_off( 'user' ); |
||
| 1664 | } |
||
| 1665 | |||
| 1666 | View Code Duplication | if ( 'hidden' == $field[ 'type' ] ) { |
|
| 1667 | $hidden_fields[] = array( |
||
| 1668 | 'field' => $field, |
||
| 1669 | 'value' => $value |
||
| 1670 | ); |
||
| 1671 | } |
||
| 1672 | else { |
||
| 1673 | ?> |
||
| 1674 | <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>"> |
||
| 1675 | <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th> |
||
| 1676 | <td> |
||
| 1677 | <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?> |
||
| 1678 | <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?> |
||
| 1679 | </td> |
||
| 1680 | </tr> |
||
| 1681 | <?php |
||
| 1682 | } |
||
| 1683 | } |
||
| 1684 | ?> |
||
| 1685 | </tbody> |
||
| 1686 | </table> |
||
| 1687 | <?php |
||
| 1688 | View Code Duplication | foreach ( $hidden_fields as $hidden_field ) { |
|
| 1689 | $field = $hidden_field[ 'field' ]; |
||
| 1690 | |||
| 1691 | echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' ); |
||
| 1692 | } |
||
| 1693 | } |
||
| 1694 | |||
| 1695 | do_action( 'pods_meta_' . __FUNCTION__ . '_post', $user_id ); |
||
| 1696 | } |
||
| 1697 | |||
| 1698 | /** |
||
| 1699 | * @param $user_id |
||
| 1700 | */ |
||
| 1701 | public function save_user ( $user_id ) { |
||
| 1702 | |||
| 1703 | $is_new_item = false; |
||
| 1704 | |||
| 1705 | if ( 'user_register' == current_filter() ) { |
||
| 1706 | $is_new_item = true; |
||
| 1707 | } |
||
| 1708 | |||
| 1709 | View Code Duplication | if ( empty( $_POST ) ) { |
|
| 1710 | return $user_id; |
||
| 1711 | } |
||
| 1712 | elseif ( $is_new_item || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_user' ) ) { |
||
| 1713 | return $user_id; |
||
| 1714 | } |
||
| 1715 | |||
| 1716 | $groups = $this->groups_get( 'user', 'user' ); |
||
| 1717 | |||
| 1718 | if ( empty( $groups ) ) { |
||
| 1719 | return $user_id; |
||
| 1720 | } |
||
| 1721 | |||
| 1722 | if ( is_object( $user_id ) ) { |
||
| 1723 | $user_id = $user_id->ID; |
||
| 1724 | } |
||
| 1725 | |||
| 1726 | $data = array(); |
||
| 1727 | |||
| 1728 | $id = $user_id; |
||
| 1729 | $pod = null; |
||
| 1730 | |||
| 1731 | View Code Duplication | foreach ( $groups as $group ) { |
|
| 1732 | if ( empty( $group[ 'fields' ] ) ) { |
||
| 1733 | continue; |
||
| 1734 | } |
||
| 1735 | |||
| 1736 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1737 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) { |
||
| 1738 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1739 | } |
||
| 1740 | elseif ( self::$current_pod->id() != $id ) { |
||
| 1741 | self::$current_pod->fetch( $id ); |
||
| 1742 | } |
||
| 1743 | |||
| 1744 | $pod = self::$current_pod; |
||
| 1745 | } |
||
| 1746 | |||
| 1747 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1748 | |||
| 1749 | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
||
| 1750 | if ( !pods_var( 'hidden', $field[ 'options' ], false ) ) { |
||
| 1751 | continue; |
||
| 1752 | } |
||
| 1753 | } |
||
| 1754 | |||
| 1755 | $data[ $field[ 'name' ] ] = ''; |
||
| 1756 | |||
| 1757 | if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) ) { |
||
| 1758 | $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ]; |
||
| 1759 | } |
||
| 1760 | } |
||
| 1761 | } |
||
| 1762 | |||
| 1763 | if ( $is_new_item ) { |
||
| 1764 | do_action( 'pods_meta_create_pre_user', $data, $pod, $id, $groups ); |
||
| 1765 | } |
||
| 1766 | |||
| 1767 | do_action( 'pods_meta_save_pre_user', $data, $pod, $id, $groups, $is_new_item ); |
||
| 1768 | |||
| 1769 | pods_no_conflict_on( 'user' ); |
||
| 1770 | |||
| 1771 | View Code Duplication | if ( !empty( $pod ) ) { |
|
| 1772 | // Fix for Pods doing it's own sanitization |
||
| 1773 | $data = pods_unslash( (array) $data ); |
||
| 1774 | |||
| 1775 | $pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) ); |
||
| 1776 | } |
||
| 1777 | elseif ( !empty( $id ) ) { |
||
| 1778 | foreach ( $data as $field => $value ) { |
||
| 1779 | update_user_meta( $id, $field, $value ); |
||
| 1780 | } |
||
| 1781 | } |
||
| 1782 | |||
| 1783 | pods_no_conflict_off( 'user' ); |
||
| 1784 | |||
| 1785 | if ( $is_new_item ) { |
||
| 1786 | do_action( 'pods_meta_create_user', $data, $pod, $id, $groups ); |
||
| 1787 | } |
||
| 1788 | |||
| 1789 | do_action( 'pods_meta_save_user', $data, $pod, $id, $groups, $is_new_item ); |
||
| 1790 | |||
| 1791 | return $user_id; |
||
| 1792 | |||
| 1793 | } |
||
| 1794 | |||
| 1795 | /** |
||
| 1796 | * @param $commenter |
||
| 1797 | * @param $user_identity |
||
| 1798 | */ |
||
| 1799 | public function meta_comment_new_logged_in ( $commenter, $user_identity ) { |
||
| 1800 | wp_enqueue_style( 'pods-form' ); |
||
| 1801 | |||
| 1802 | do_action( 'pods_meta_' . __FUNCTION__, $commenter, $user_identity ); |
||
| 1803 | |||
| 1804 | $groups = $this->groups_get( 'comment', 'comment' ); |
||
| 1805 | |||
| 1806 | $id = null; |
||
| 1807 | $pod = null; |
||
| 1808 | |||
| 1809 | echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' ); |
||
| 1810 | |||
| 1811 | foreach ( $groups as $group ) { |
||
| 1812 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1813 | continue; |
||
| 1814 | |||
| 1815 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1816 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) |
||
| 1817 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1818 | elseif ( self::$current_pod->id() != $id ) |
||
| 1819 | self::$current_pod->fetch( $id ); |
||
| 1820 | |||
| 1821 | $pod = self::$current_pod; |
||
| 1822 | } |
||
| 1823 | |||
| 1824 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1825 | View Code Duplication | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
|
| 1826 | if ( pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1827 | $field[ 'type' ] = 'hidden'; |
||
| 1828 | else |
||
| 1829 | continue; |
||
| 1830 | } |
||
| 1831 | elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1832 | $field[ 'type' ] = 'hidden'; |
||
| 1833 | |||
| 1834 | $value = ''; |
||
| 1835 | |||
| 1836 | View Code Duplication | if ( !empty( $pod ) ) |
|
| 1837 | $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) ); |
||
| 1838 | elseif ( !empty( $id ) ) { |
||
| 1839 | pods_no_conflict_on( 'comment' ); |
||
| 1840 | |||
| 1841 | $value = get_comment_meta( $id, $field[ 'name' ], true ); |
||
| 1842 | |||
| 1843 | pods_no_conflict_off( 'comment' ); |
||
| 1844 | } |
||
| 1845 | ?> |
||
| 1846 | <p class="comment-form-author comment-form-pods-meta-<?php echo esc_attr( $field[ 'name' ] ); ?> pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>"> |
||
| 1847 | <?php |
||
| 1848 | echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); |
||
| 1849 | echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); |
||
| 1850 | echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); |
||
| 1851 | ?> |
||
| 1852 | </p> |
||
| 1853 | <?php |
||
| 1854 | } |
||
| 1855 | } |
||
| 1856 | |||
| 1857 | do_action( 'pods_meta_' . __FUNCTION__ . '_post', $commenter, $user_identity ); |
||
| 1858 | } |
||
| 1859 | |||
| 1860 | /** |
||
| 1861 | * @param $form_fields |
||
| 1862 | * |
||
| 1863 | * @return array |
||
| 1864 | */ |
||
| 1865 | public function meta_comment_new ( $form_fields ) { |
||
| 1866 | wp_enqueue_style( 'pods-form' ); |
||
| 1867 | |||
| 1868 | $groups = $this->groups_get( 'comment', 'comment' ); |
||
| 1869 | |||
| 1870 | $id = null; |
||
| 1871 | $pod = null; |
||
| 1872 | |||
| 1873 | $form_fields[ 'pods_meta' ] = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' ); |
||
| 1874 | |||
| 1875 | foreach ( $groups as $group ) { |
||
| 1876 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1877 | continue; |
||
| 1878 | |||
| 1879 | if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) { |
||
| 1880 | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) |
||
| 1881 | self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true ); |
||
| 1882 | elseif ( self::$current_pod->id() != $id ) |
||
| 1883 | self::$current_pod->fetch( $id ); |
||
| 1884 | |||
| 1885 | $pod = self::$current_pod; |
||
| 1886 | } |
||
| 1887 | |||
| 1888 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1889 | |||
| 1890 | View Code Duplication | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) { |
|
| 1891 | if ( pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1892 | $field[ 'type' ] = 'hidden'; |
||
| 1893 | else |
||
| 1894 | continue; |
||
| 1895 | } |
||
| 1896 | elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 1897 | $field[ 'type' ] = 'hidden'; |
||
| 1898 | |||
| 1899 | $value = ''; |
||
| 1900 | |||
| 1901 | View Code Duplication | if ( !empty( $pod ) ) |
|
| 1902 | $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) ); |
||
| 1903 | elseif ( !empty( $id ) ) { |
||
| 1904 | pods_no_conflict_on( 'comment' ); |
||
| 1905 | |||
| 1906 | $value = get_comment_meta( $id, $field[ 'name' ], true ); |
||
| 1907 | |||
| 1908 | pods_no_conflict_off( 'comment' ); |
||
| 1909 | } |
||
| 1910 | |||
| 1911 | ob_start(); |
||
| 1912 | ?> |
||
| 1913 | <p class="comment-form-author comment-form-pods-meta-<?php echo esc_attr( $field[ 'name' ] ); ?> pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>"> |
||
| 1914 | <?php |
||
| 1915 | echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); |
||
| 1916 | echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); |
||
| 1917 | echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); |
||
| 1918 | ?> |
||
| 1919 | </p> |
||
| 1920 | <?php |
||
| 1921 | $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = ob_get_clean(); |
||
| 1922 | } |
||
| 1923 | } |
||
| 1924 | |||
| 1925 | $form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields ); |
||
| 1926 | |||
| 1927 | return $form_fields; |
||
| 1928 | } |
||
| 1929 | |||
| 1930 | /** |
||
| 1931 | * @param $comment_type |
||
| 1932 | * @param null $comment |
||
| 1933 | */ |
||
| 1934 | public function meta_comment_add ( $comment_type, $comment = null ) { |
||
| 1935 | if ( is_object( $comment ) && isset( $comment_type->comment_type ) ) |
||
| 1936 | $comment_type = $comment->comment_type; |
||
| 1937 | |||
| 1938 | if ( is_object( $comment_type ) && isset( $comment_type->comment_type ) ) { |
||
| 1939 | $comment = $comment_type; |
||
| 1940 | $comment_type = $comment_type->comment_type; |
||
| 1941 | } |
||
| 1942 | |||
| 1943 | if ( is_object( $comment_type ) ) |
||
| 1944 | return; |
||
| 1945 | elseif ( empty( $comment_type ) ) |
||
| 1946 | $comment_type = 'comment'; |
||
| 1947 | |||
| 1948 | $groups = $this->groups_get( 'comment', $comment_type ); |
||
| 1949 | |||
| 1950 | foreach ( $groups as $group ) { |
||
| 1951 | if ( empty( $group[ 'fields' ] ) ) |
||
| 1952 | continue; |
||
| 1953 | |||
| 1954 | $field_found = false; |
||
| 1955 | |||
| 1956 | foreach ( $group[ 'fields' ] as $field ) { |
||
| 1957 | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], null, null ) ) { |
||
| 1958 | if ( pods_var( 'hidden', $field[ 'options' ], false ) ) { |
||
| 1959 | $field_found = true; |
||
| 1960 | break; |
||
| 1961 | } |
||
| 1962 | else { |
||
| 1963 | continue; |
||
| 1964 | } |
||
| 1965 | } |
||
| 1966 | else { |
||
| 1967 | $field_found = true; |
||
| 1968 | break; |
||
| 1969 | } |
||
| 1970 | } |
||
| 1971 | |||
| 1972 | View Code Duplication | if ( $field_found ) { |
|
| 1973 | add_meta_box( |
||
| 1974 | 'pods-meta-' . sanitize_title( $group[ 'label' ] ), |
||
| 1975 | $group[ 'label' ], |
||
| 1976 | array( $this, 'meta_comment' ), |
||
| 1977 | $comment_type, |
||
| 1978 | $group[ 'context' ], |
||
| 1979 | $group[ 'priority' ], |
||
| 1980 | array( 'group' => $group ) |
||
| 1981 | ); |
||
| 1982 | } |
||
| 1983 | } |
||
| 1984 | } |
||
| 1985 | |||
| 1986 | /** |
||
| 1987 | * @param $comment |
||
| 1988 | * @param $metabox |
||
| 1989 | */ |
||
| 1990 | public function meta_comment ( $comment, $metabox ) { |
||
| 1991 | wp_enqueue_style( 'pods-form' ); |
||
| 1992 | |||
| 1993 | do_action( 'pods_meta_' . __FUNCTION__, $comment, $metabox ); |
||
| 1994 | |||
| 1995 | $hidden_fields = array(); |
||
| 1996 | |||
| 1997 | echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' ); |
||
| 1998 | ?> |
||
| 1999 | <table class="form-table editcomment pods-metabox"> |
||
| 2000 | <?php |
||
| 2001 | $id = null; |
||
| 2002 | |||
| 2003 | if ( is_object( $comment ) ) |
||
| 2004 | $id = $comment->comment_ID; |
||
| 2005 | |||
| 2006 | View Code Duplication | if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ] ) |
|
| 2007 | self::$current_pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true ); |
||
| 2008 | elseif ( self::$current_pod->id() != $id ) |
||
| 2009 | self::$current_pod->fetch( $id ); |
||
| 2010 | |||
| 2011 | $pod = self::$current_pod; |
||
| 2012 | |||
| 2013 | foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) { |
||
| 2014 | View Code Duplication | if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) ) { |
|
| 2015 | if ( pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 2016 | $field[ 'type' ] = 'hidden'; |
||
| 2017 | else |
||
| 2018 | continue; |
||
| 2019 | } |
||
| 2020 | elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) ) |
||
| 2021 | $field[ 'type' ] = 'hidden'; |
||
| 2022 | |||
| 2023 | $value = ''; |
||
| 2024 | |||
| 2025 | View Code Duplication | if ( !empty( $pod ) ) |
|
| 2026 | $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) ); |
||
| 2027 | |||
| 2028 | View Code Duplication | if ( 'hidden' == $field[ 'type' ] ) { |
|
| 2029 | $hidden_fields[] = array( |
||
| 2030 | 'field' => $field, |
||
| 2031 | 'value' => $value |
||
| 2032 | ); |
||
| 2033 | } |
||
| 2034 | else { |
||
| 2035 | ?> |
||
| 2036 | <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>"> |
||
| 2037 | <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th> |
||
| 2038 | <td> |
||
| 2039 | <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?> |
||
| 2040 | <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?> |
||
| 2041 | </td> |
||
| 2042 | </tr> |
||
| 2043 | <?php |
||
| 2044 | } |
||
| 2045 | } |
||
| 2046 | ?> |
||
| 2047 | </table> |
||
| 2048 | <?php |
||
| 2049 | View Code Duplication | foreach ( $hidden_fields as $hidden_field ) { |
|
| 2050 | $field = $hidden_field[ 'field' ]; |
||
| 2051 | |||
| 2052 | echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' ); |
||
| 2053 | } |
||
| 2054 | |||
| 2055 | do_action( 'pods_meta_' . __FUNCTION__ . '_post', $comment, $metabox ); |
||
| 2056 | } |
||
| 2057 | |||
| 2058 | /** |
||
| 2059 | * @param $approved |
||
| 2060 | * @param $commentdata |
||
| 2061 | */ |
||
| 2062 | public function validate_comment ( $approved, $commentdata ) { |
||
| 2110 | |||
| 2111 | /** |
||
| 2112 | * @param $comment_id |
||
| 2113 | */ |
||
| 2114 | public function save_comment ( $comment_id ) { |
||
| 2180 | |||
| 2181 | /** |
||
| 2182 | * All *_*_meta filter handler aliases |
||
| 2183 | * |
||
| 2184 | * @return mixed |
||
| 2185 | */ |
||
| 2186 | View Code Duplication | public function get_post_meta () { |
|
| 2198 | |||
| 2199 | /** |
||
| 2200 | * @return mixed |
||
| 2201 | */ |
||
| 2202 | View Code Duplication | public function get_user_meta () { |
|
| 2214 | |||
| 2215 | /** |
||
| 2216 | * @return mixed |
||
| 2217 | */ |
||
| 2218 | View Code Duplication | public function get_comment_meta () { |
|
| 2230 | |||
| 2231 | /** |
||
| 2232 | * @return mixed |
||
| 2233 | */ |
||
| 2234 | View Code Duplication | public function get_term_meta () { |
|
| 2246 | |||
| 2247 | /** |
||
| 2248 | * All *_*_meta filter handler aliases |
||
| 2249 | * |
||
| 2250 | * @return mixed |
||
| 2251 | */ |
||
| 2252 | View Code Duplication | public function get_option () { |
|
| 2264 | |||
| 2265 | /** |
||
| 2266 | * @return mixed |
||
| 2267 | */ |
||
| 2268 | View Code Duplication | public function add_post_meta () { |
|
| 2280 | |||
| 2281 | /** |
||
| 2282 | * @return mixed |
||
| 2283 | */ |
||
| 2284 | View Code Duplication | public function add_user_meta () { |
|
| 2296 | |||
| 2297 | /** |
||
| 2298 | * @return mixed |
||
| 2299 | */ |
||
| 2300 | View Code Duplication | public function add_comment_meta () { |
|
| 2312 | |||
| 2313 | /** |
||
| 2314 | * @return mixed |
||
| 2315 | */ |
||
| 2316 | View Code Duplication | public function add_term_meta () { |
|
| 2328 | |||
| 2329 | /** |
||
| 2330 | * @return mixed |
||
| 2331 | */ |
||
| 2332 | View Code Duplication | public function add_option () { |
|
| 2344 | |||
| 2345 | /** |
||
| 2346 | * @return mixed |
||
| 2347 | */ |
||
| 2348 | View Code Duplication | public function update_post_meta () { |
|
| 2360 | |||
| 2361 | /** |
||
| 2362 | * @return mixed |
||
| 2363 | */ |
||
| 2364 | View Code Duplication | public function update_user_meta () { |
|
| 2376 | |||
| 2377 | /** |
||
| 2378 | * @return mixed |
||
| 2379 | */ |
||
| 2380 | View Code Duplication | public function update_comment_meta () { |
|
| 2392 | |||
| 2393 | /** |
||
| 2394 | * @return mixed |
||
| 2395 | */ |
||
| 2396 | View Code Duplication | public function update_term_meta () { |
|
| 2408 | |||
| 2409 | /** |
||
| 2410 | * @return mixed |
||
| 2411 | */ |
||
| 2412 | View Code Duplication | public function update_option () { |
|
| 2424 | |||
| 2425 | /** |
||
| 2426 | * @return mixed |
||
| 2427 | */ |
||
| 2428 | View Code Duplication | public function delete_post_meta () { |
|
| 2440 | |||
| 2441 | /** |
||
| 2442 | * @return mixed |
||
| 2443 | */ |
||
| 2444 | View Code Duplication | public function delete_user_meta () { |
|
| 2456 | |||
| 2457 | /** |
||
| 2458 | * @return mixed |
||
| 2459 | */ |
||
| 2460 | View Code Duplication | public function delete_comment_meta () { |
|
| 2472 | |||
| 2473 | /** |
||
| 2474 | * @return mixed |
||
| 2475 | */ |
||
| 2476 | View Code Duplication | public function delete_term_meta () { |
|
| 2488 | |||
| 2489 | /** |
||
| 2490 | * @return mixed |
||
| 2491 | */ |
||
| 2492 | View Code Duplication | public function delete_option () { |
|
| 2504 | |||
| 2505 | /* |
||
| 2506 | * The real meta functions |
||
| 2507 | */ |
||
| 2508 | /** |
||
| 2509 | * @param $object_type |
||
| 2510 | * @param $object_id |
||
| 2511 | * @param string $aux |
||
| 2512 | * |
||
| 2513 | * @return bool|mixed |
||
| 2514 | */ |
||
| 2515 | public function get_object ( $object_type, $object_id, $aux = '' ) { |
||
| 2516 | |||
| 2517 | global $wpdb; |
||
| 2518 | |||
| 2519 | if ( 'term' == $object_type ) { |
||
| 2520 | $object_type = 'taxonomy'; |
||
| 2521 | } |
||
| 2522 | |||
| 2523 | if ( 'post_type' == $object_type ) |
||
| 2524 | $objects = self::$post_types; |
||
| 2525 | elseif ( 'taxonomy' == $object_type ) |
||
| 2526 | $objects = self::$taxonomies; |
||
| 2527 | elseif ( 'media' == $object_type ) |
||
| 2528 | $objects = self::$media; |
||
| 2529 | elseif ( 'user' == $object_type ) |
||
| 2530 | $objects = self::$user; |
||
| 2531 | elseif ( 'comment' == $object_type ) |
||
| 2532 | $objects = self::$comment; |
||
| 2533 | elseif ( 'settings' == $object_type ) |
||
| 2534 | $objects = self::$settings; |
||
| 2535 | else |
||
| 2536 | return false; |
||
| 2537 | |||
| 2538 | if ( empty( $objects ) || !is_array( $objects ) ) |
||
| 2539 | return false; |
||
| 2540 | |||
| 2541 | $object_name = null; |
||
| 2542 | |||
| 2543 | if ( 'media' == $object_type ) |
||
| 2544 | return @current( $objects ); |
||
| 2545 | elseif ( 'user' == $object_type ) |
||
| 2546 | return @current( $objects ); |
||
| 2547 | elseif ( 'comment' == $object_type ) |
||
| 2548 | return @current( $objects ); |
||
| 2549 | elseif ( 'post_type' == $object_type ) { |
||
| 2550 | $object = get_post( $object_id ); |
||
| 2551 | |||
| 2552 | if ( !is_object( $object ) || !isset( $object->post_type ) ) |
||
| 2553 | return false; |
||
| 2554 | |||
| 2555 | $object_name = $object->post_type; |
||
| 2556 | } |
||
| 2557 | elseif ( 'taxonomy' == $object_type ) { |
||
| 2558 | if ( pods_version_check( 'wp', '4.4' ) ) { |
||
| 2559 | $object = get_term( $object_id ); |
||
| 2560 | |||
| 2561 | if ( !is_object( $object ) || !isset( $object->taxonomy ) ) |
||
| 2562 | return false; |
||
| 2563 | |||
| 2564 | $object_name = $object->taxonomy; |
||
| 2565 | } elseif ( empty( $aux ) ) { |
||
| 2566 | $object_name = $wpdb->get_var( $wpdb->prepare( "SELECT `taxonomy` FROM `{$wpdb->term_taxonomy}` WHERE `term_id` = %d", $object_id ) ); |
||
| 2567 | } else { |
||
| 2568 | $object_name = $aux; |
||
| 2569 | } |
||
| 2570 | } |
||
| 2571 | elseif ( 'settings' == $object_type ) |
||
| 2572 | $object = $object_id; |
||
| 2573 | else |
||
| 2574 | return false; |
||
| 2575 | |||
| 2576 | $reserved_post_types = array( |
||
| 2577 | 'revision' |
||
| 2578 | ); |
||
| 2579 | |||
| 2580 | $reserved_post_types = apply_filters( 'pods_meta_reserved_post_types', $reserved_post_types, $object_type, $object_id, $object_name, $objects ); |
||
| 2581 | |||
| 2582 | if ( empty( $object_name ) || ( 'post_type' == $object_type && ( 0 === strpos( $object_name, '_pods_' ) ) || in_array( $object_name, $reserved_post_types ) ) ) { |
||
| 2583 | return false; |
||
| 2584 | } |
||
| 2585 | elseif ( 'attachment' == $object_name ) { |
||
| 2586 | return @current( self::$media ); |
||
| 2587 | } |
||
| 2588 | |||
| 2589 | $recheck = array(); |
||
| 2590 | |||
| 2591 | // Return first created by Pods, save extended for later |
||
| 2592 | foreach ( $objects as $pod ) { |
||
| 2593 | if ( $object_name == $pod[ 'object' ] ) |
||
| 2594 | $recheck[] = $pod; |
||
| 2595 | |||
| 2596 | if ( '' == $pod[ 'object' ] && $object_name == $pod[ 'name' ] ) |
||
| 2597 | return $pod; |
||
| 2598 | } |
||
| 2599 | |||
| 2600 | // If no objects created by Pods, return first extended |
||
| 2601 | foreach ( $recheck as $pod ) { |
||
| 2602 | return $pod; |
||
| 2603 | } |
||
| 2604 | |||
| 2605 | return false; |
||
| 2606 | } |
||
| 2607 | |||
| 2608 | /** |
||
| 2609 | * @param $object_type |
||
| 2610 | * @param null $_null |
||
| 2611 | * @param int $object_id |
||
| 2612 | * @param string $meta_key |
||
| 2613 | * @param bool $single |
||
| 2614 | * |
||
| 2615 | * @return array|bool|int|mixed|null|string|void |
||
| 2616 | */ |
||
| 2617 | public function get_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $single = false ) { |
||
| 2744 | |||
| 2745 | /** |
||
| 2746 | * @param $object_type |
||
| 2747 | * @param null $_null |
||
| 2748 | * @param int $object_id |
||
| 2749 | * @param string $meta_key |
||
| 2750 | * @param string $meta_value |
||
| 2751 | * @param bool $unique |
||
| 2752 | * |
||
| 2753 | * @return bool|int|null |
||
| 2754 | */ |
||
| 2755 | public function add_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $unique = false ) { |
||
| 2785 | |||
| 2786 | /** |
||
| 2787 | * @param $object_type |
||
| 2788 | * @param null $_null |
||
| 2789 | * @param int $object_id |
||
| 2790 | * @param string $meta_key |
||
| 2791 | * @param string $meta_value |
||
| 2792 | * @param string $prev_value |
||
| 2793 | * |
||
| 2794 | * @return bool|int|null |
||
| 2795 | */ |
||
| 2796 | public function update_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $prev_value = '' ) { |
||
| 2830 | |||
| 2831 | /** |
||
| 2832 | * @param $object_type |
||
| 2833 | * @param null $_null |
||
| 2834 | * @param int $object_id |
||
| 2835 | * @param string $meta_key |
||
| 2836 | * @param string $meta_value |
||
| 2837 | * @param bool $delete_all |
||
| 2838 | * |
||
| 2839 | * @return null |
||
| 2840 | */ |
||
| 2841 | public function delete_meta ( $object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $delete_all = false ) { |
||
| 2872 | |||
| 2873 | public function delete_post ( $id ) { |
||
| 2884 | |||
| 2885 | public function delete_taxonomy ( $id ) { |
||
| 2903 | |||
| 2904 | /** |
||
| 2905 | * Hook the split_shared_term action and point it to this method |
||
| 2906 | * |
||
| 2907 | * Fires after a previously shared taxonomy term is split into two separate terms. |
||
| 2908 | * |
||
| 2909 | * @param int $term_id ID of the formerly shared term. |
||
| 2910 | * @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
||
| 2911 | * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
||
| 2912 | * @param string $taxonomy Taxonomy for the split term. |
||
| 2913 | */ |
||
| 2914 | public static function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
||
| 2922 | |||
| 2923 | public function delete_user ( $id ) { |
||
| 2926 | |||
| 2927 | public function delete_comment ( $id ) { |
||
| 2930 | |||
| 2931 | public function delete_media ( $id ) { |
||
| 2934 | |||
| 2935 | public function delete_object ( $type, $id, $name = null ) { |
||
| 2953 | } |
||
| 2954 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.