/classes/PodsMeta.php
PHP | 1946 lines | 1216 code | 465 blank | 265 comment | 327 complexity | 19272ecb9c715423b5e56fe23c9136e5 MD5 | raw file
Possible License(s): AGPL-1.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * @package Pods
- */
- class PodsMeta {
- /**
- * @var PodsAPI
- */
- private $api;
- /**
- * @var int
- */
- public static $object_identifier = -1;
- /**
- * @var array
- */
- public static $advanced_content_types = array();
- /**
- * @var array
- */
- public static $post_types = array();
- /**
- * @var array
- */
- public static $taxonomies = array();
- /**
- * @var array
- */
- public static $media = array();
- /**
- * @var array
- */
- public static $user = array();
- /**
- * @var array
- */
- public static $comment = array();
- /**
- * @var array
- */
- public static $queue = array();
- /**
- * @var array
- */
- public static $groups = array();
- /**
- *
- */
- function __construct () {
- $this->api = pods_api();
- }
- /**
- * @return PodsMeta
- */
- public function init () {
- self::$advanced_content_types = $this->api->load_pods( array( 'type' => 'pod' ) );
- self::$post_types = $this->api->load_pods( array( 'type' => 'post_type' ) );
- self::$taxonomies = $this->api->load_pods( array( 'type' => 'taxonomy' ) );
- self::$media = $this->api->load_pods( array( 'type' => 'media' ) );
- self::$user = $this->api->load_pods( array( 'type' => 'user' ) );
- self::$comment = $this->api->load_pods( array( 'type' => 'comment' ) );
- // Handle Post Type Editor (needed for Pods core)
- // Loop through and add meta boxes for individual types (can't use this, Tabify doesn't pick it up)
- /*
- foreach ( self::$post_types as $post_type ) {
- $post_type_name = $post_type[ 'name' ];
- if ( !empty( $post_type[ 'object' ] ) )
- $post_type_name = $post_type[ 'object' ];
- add_action( 'add_meta_boxes_' . $post_type_name, array( $this, 'meta_post_add' ) );
- }
- */
- add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
- add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
- // Handle *_post_meta
- add_filter( 'get_post_metadata', array( $this, 'get_post_meta' ), 10, 4 );
- add_filter( 'add_post_metadata', array( $this, 'add_post_meta' ), 10, 5 );
- add_filter( 'update_post_metadata', array( $this, 'update_post_meta' ), 10, 5 );
- add_filter( 'delete_post_metadata', array( $this, 'delete_post_meta' ), 10, 5 );
- add_action( 'delete_post', array( $this, 'delete_post' ), 10, 1 );
- if ( !empty( self::$taxonomies ) ) {
- // Handle Taxonomy Editor
- foreach ( self::$taxonomies as $taxonomy ) {
- $taxonomy_name = $taxonomy[ 'name' ];
- if ( !empty( $taxonomy[ 'object' ] ) )
- $taxonomy_name = $taxonomy[ 'object' ];
- add_action( $taxonomy_name . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
- add_action( $taxonomy_name . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
- }
- // Handle Term Editor
- add_action( 'edit_term', array( $this, 'save_taxonomy' ), 10, 3 );
- add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
- // Handle *_term_meta, LOL just kidding
- /*
- add_filter( 'get_term_metadata', array( $this, 'get_term_meta' ), 10, 4 );
- add_filter( 'add_term_metadata', array( $this, 'add_term_meta' ), 10, 5 );
- add_filter( 'update_term_metadata', array( $this, 'update_term_meta' ), 10, 5 );
- add_filter( 'delete_term_metadata', array( $this, 'delete_term_meta' ), 10, 5 );
- */
- // Handle Delete
- add_action( 'delete_term_taxonomy', array( $this, 'delete_taxonomy' ), 10, 1 );
- }
- if ( !empty( self::$media ) ) {
- if ( pods_wp_version( '3.5' ) ) {
- add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
- add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
- }
- add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
- add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
- add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
- // Handle Delete
- add_action( 'delete_attachment', array( $this, 'delete_media' ), 10, 1 );
- }
- if ( !empty( self::$user ) ) {
- // Handle User Editor
- add_action( 'show_user_profile', array( $this, 'meta_user' ) );
- add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
- add_action( 'personal_options_update', array( $this, 'save_user' ) );
- add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
- // Handle *_user_meta
- add_filter( 'get_user_metadata', array( $this, 'get_user_meta' ), 10, 4 );
- add_filter( 'add_user_metadata', array( $this, 'add_user_meta' ), 10, 5 );
- add_filter( 'update_user_metadata', array( $this, 'update_user_meta' ), 10, 5 );
- add_filter( 'delete_user_metadata', array( $this, 'delete_user_meta' ), 10, 5 );
- // Handle Delete
- add_action( 'delete_user', array( $this, 'delete_user' ), 10, 1 );
- }
- if ( !empty( self::$comment ) ) {
- // Handle Comment Form / Editor
- add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
- add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
- add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
- add_filter( 'pre_comment_approved', array( $this, 'validate_comment' ), 10, 2 );
- add_action( 'comment_post', array( $this, 'save_comment' ) );
- add_action( 'edit_comment', array( $this, 'save_comment' ) );
- // Handle *_comment_meta
- add_filter( 'get_comment_metadata', array( $this, 'get_comment_meta' ), 10, 4 );
- add_filter( 'add_comment_metadata', array( $this, 'add_comment_meta' ), 10, 5 );
- add_filter( 'update_comment_metadata', array( $this, 'update_comment_meta' ), 10, 5 );
- add_filter( 'delete_comment_metadata', array( $this, 'delete_comment_meta' ), 10, 5 );
- // Handle Delete
- add_action( 'delete_comment', array( $this, 'delete_comment' ), 10, 1 );
- }
- if ( is_admin() )
- $this->integrations();
- add_action( 'init', array( $this, 'enqueue' ), 9 );
- do_action( 'pods_meta_init' );
- return $this;
- }
- public static function enqueue () {
- foreach ( self::$queue as $type => $objects ) {
- foreach ( $objects as $pod_name => $pod ) {
- pods_transient_set( 'pods_pod_' . $pod_name, $pod );
- }
- self::$$type = array_merge( self::$$type, $objects );
- }
- }
- public function register ( $type, $pod ) {
- $pod_type = $type;
- if ( 'post_type' == $type )
- $type = 'post_types';
- elseif ( 'taxonomy' == $type )
- $type = 'taxonomies';
- elseif ( 'pod' == $type )
- $type = 'advanced_content_types';
- if ( !isset( self::$queue[ $type ] ) )
- self::$queue[ $type ] = array();
- $pod[ 'type' ] = $pod_type;
- $pod = $this->api->save_pod( $pod, false, self::$object_identifier );
- if ( !empty( $pod ) ) {
- self::$object_identifier--;
- self::$queue[ $type ][ $pod[ 'name' ] ] = $pod;
- return $pod;
- }
- return false;
- }
- public function register_field ( $pod, $field ) {
- $pod = $this->api->load_pod( array( 'name' => $pod ), false );
- if ( !empty( $pod ) ) {
- $type = $pod[ 'type' ];
- if ( 'post_type' == $pod[ 'type' ] )
- $type = 'post_types';
- elseif ( 'taxonomy' == $pod[ 'type' ] )
- $type = 'taxonomies';
- elseif ( 'pod' == $pod[ 'type' ] )
- $type = 'advanced_content_types';
- if ( !isset( self::$queue[ $pod[ 'type' ] ] ) )
- self::$queue[ $type ] = array();
- $field = $this->api->save_field( $field, false, false, $pod[ 'id' ] );
- if ( !empty( $field ) ) {
- $pod[ 'fields' ][ $field[ 'name' ] ] = $field;
- self::$queue[ $type ][ $pod[ 'name' ] ] = $pod;
- return $field;
- }
- }
- return false;
- }
- public function integrations () {
- add_filter( 'cpac-get-meta-by-type', array( $this, 'cpac_meta_keys' ), 10, 2 );
- add_filter( 'cpac-get-post-types', array( $this, 'cpac_post_types' ), 10, 1 );
- add_filter( 'cpac_get_column_value_custom_field', array( $this, 'cpac_meta_values' ), 10, 5 );
- }
- public function cpac_meta_keys ( $meta_fields, $type ) {
- $object_type = 'post_type';
- $object = $type;
- if ( 'wp-media' == $type )
- $object_type = $object = 'media';
- elseif ( 'wp-users' == $type )
- $object_type = $object = 'user';
- elseif ( 'wp-comments' == $type )
- $object_type = $object = 'comment';
- $pod = $this->api->load_pod( array( 'name' => $object ), false );
- // Add Pods fields
- if ( !empty( $pod ) && $object_type == $pod[ 'type' ] ) {
- foreach ( $pod[ 'fields' ] as $field => $field_data ) {
- if ( !is_array( $meta_fields ) )
- $meta_fields = array();
- if ( !in_array( $field, $meta_fields ) )
- $meta_fields[] = $field;
- }
- }
- // Remove internal Pods fields
- if ( is_array( $meta_fields ) ) {
- foreach ( $meta_fields as $meta_field ) {
- if ( 0 === strpos( $meta_field, '_pods_' ) )
- unset( $meta_fields[ $meta_field ] );
- }
- }
- return $meta_fields;
- }
- public function cpac_post_types ( $post_types ) {
- // Remove internal Pods post types
- foreach ( $post_types as $post_type => $post_type_name ) {
- if ( 0 === strpos( $post_type, '_pods_' ) || 0 === strpos( $post_type_name, '_pods_' ) )
- unset( $post_types[ $post_type ] );
- }
- return $post_types;
- }
- public function cpac_meta_values ( $meta, $fieldtype, $field, $type, $object_id ) {
- $tableless_field_types = apply_filters( 'pods_tableless_field_types', array( 'pick', 'file', 'avatar' ) );
- $object = $type;
- if ( 'wp-media' == $type )
- $object = 'media';
- elseif ( 'wp-users' == $type )
- $object = 'user';
- elseif ( 'wp-comments' == $type )
- $object = 'comment';
- $pod = $this->api->load_pod( array( 'name' => $object ), false );
- // Add Pods fields
- if ( !empty( $pod ) && isset( $pod[ 'fields' ][ $field ] ) ) {
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'user', 'comment', 'media' ) ) && ( !empty( $fieldtype ) || in_array( $pod[ 'fields' ][ $field ][ 'type' ], $tableless_field_types ) ) )
- $meta = get_metadata( ( 'post_type' == $pod[ 'type' ] ? 'post' : $pod[ 'type' ] ), $object_id, $field, true );
- $meta = PodsForm::field_method( $pod[ 'fields' ][ $field ][ 'type' ], 'ui', $object_id, $meta, $field, array_merge( $pod[ 'fields' ][ $field ][ 'options' ], $pod[ 'fields' ][ $field ] ), $pod[ 'fields' ], $pod[ 'name' ] );
- }
- return $meta;
- }
- /**
- * Add a meta group of fields to add/edit forms
- *
- * @param string|array $pod The pod or type of element to attach the group to.
- * @param string $label Title of the edit screen section, visible to user.
- * @param string|array $fields Either a comma separated list of text fields or an associative array containing field infomration.
- * @param string $context (optional) The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side').
- * @param string $priority (optional) The priority within the context where the boxes should show ('high', 'core', 'default' or 'low').
- *
- * @since 2.0.0
- *
- * @return mixed|void
- */
- public function group_add ( $pod, $label, $fields, $context = 'normal', $priority = 'default' ) {
- if ( !is_array( $pod ) ) {
- $_pod = $this->api->load_pod( array( 'name' => $pod ), false );
- if ( !empty( $_pod ) )
- $pod = $_pod;
- else {
- $type = 'post_type';
- if ( in_array( $pod, array( 'media', 'user', 'comment' ) ) )
- $type = $pod;
- $pod = array(
- 'name' => $pod,
- 'type' => $type
- );
- }
- }
- if ( is_array( $pod ) && !isset( $pod[ 'id' ] ) ) {
- $defaults = array(
- 'name' => '',
- 'type' => 'post_type'
- );
- $pod = array_merge( $defaults, $pod );
- }
- if ( 'post' == $pod[ 'type' ] )
- $pod[ 'type' ] = 'post_type';
- if ( empty( $pod[ 'name' ] ) && isset( $pod[ 'object' ] ) && !empty( $pod[ 'object' ] ) )
- $pod[ 'name' ] = $pod[ 'object' ];
- elseif ( !isset( $pod[ 'object' ] ) || empty( $pod[ 'object' ] ) )
- $pod[ 'object' ] = $pod[ 'name' ];
- if ( empty( $pod[ 'object' ] ) )
- return pods_error( __( 'Object required to add a Pods meta group', 'pods' ) );
- $object_name = $pod[ 'object' ];
- if ( 'pod' == $pod[ 'type' ] )
- $object_name = $pod[ 'name' ];
- if ( !isset( self::$groups[ $pod[ 'type' ] ] ) )
- self::$groups[ $pod[ 'type' ] ] = array();
- if ( !isset( self::$groups[ $pod[ 'type' ] ][ $object_name ] ) )
- self::$groups[ $pod[ 'type' ] ][ $object_name ] = array();
- $_fields = array();
- if ( !is_array( $fields ) )
- $fields = explode( ',', $fields );
- foreach ( $fields as $k => $field ) {
- $name = $k;
- $defaults = array(
- 'name' => $name,
- 'label' => $name,
- 'type' => 'text'
- );
- if ( !is_array( $field ) ) {
- $name = trim( $field );
- $field = array(
- 'name' => $name,
- 'label' => $name
- );
- }
- $field = array_merge( $defaults, $field );
- $field[ 'name' ] = trim( $field[ 'name' ] );
- if ( isset( $pod[ 'fields' ] ) && isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
- $field = array_merge( $field, $pod[ 'fields' ][ $field[ 'name' ] ] );
- $_fields[ $k ] = $field;
- }
- // Setup field options
- $fields = PodsForm::fields_setup( $_fields );
- $group = array(
- 'pod' => $pod,
- 'label' => $label,
- 'fields' => $fields,
- 'context' => $context,
- 'priority' => $priority
- );
- // Filter group data, pass vars separately for reference down the line (in case array changed by other filter)
- $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ] . '_' . $object_name, $group, $pod, $label, $fields );
- $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ], $group, $pod, $label, $fields );
- $group = apply_filters( 'pods_meta_group_add', $group, $pod, $label, $fields );
- self::$groups[ $pod[ 'type' ] ][ $object_name ][] = $group;
- // Hook it up!
- if ( 'post_type' == $pod[ 'type' ] ) {
- if ( !has_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ) )
- add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
- /*if ( !has_action( 'save_post', array( $this, 'save_post' ), 10, 2 ) )
- add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );*/
- }
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
- if ( !has_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 ) ) {
- add_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
- add_action( $pod[ 'object' ] . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
- }
- if ( !has_action( 'edit_term', array( $this, 'save_taxonomy' ), 10, 3 ) ) {
- add_action( 'edit_term', array( $this, 'save_taxonomy' ), 10, 3 );
- add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
- }
- }
- elseif ( 'media' == $pod[ 'type' ] ) {
- if ( !has_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 ) ) {
- if ( pods_wp_version( '3.5' ) ) {
- add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
- add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
- }
- add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
- add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
- add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
- }
- }
- elseif ( 'user' == $pod[ 'type' ] ) {
- if ( !has_action( 'show_user_profile', array( $this, 'meta_user' ) ) ) {
- add_action( 'show_user_profile', array( $this, 'meta_user' ) );
- add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
- add_action( 'personal_options_update', array( $this, 'save_user' ) );
- add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
- }
- }
- elseif ( 'comment' == $pod[ 'type' ] ) {
- if ( !has_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 ) ) {
- add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
- add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
- add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
- add_action( 'wp_insert_comment', array( $this, 'save_comment' ) );
- add_action( 'edit_comment', array( $this, 'save_comment' ) );
- }
- }
- }
- public function object_get ( $type, $name ) {
- $object = self::$post_types;
- if ( 'taxonomy' == $type )
- $object = self::$taxonomies;
- elseif ( 'media' == $type )
- $object = self::$media;
- elseif ( 'user' == $type )
- $object = self::$user;
- elseif ( 'comment' == $type )
- $object = self::$comment;
- if ( 'pod' != $type && !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
- $pod = $object[ $name ];
- else
- $pod = $this->api->load_pod( array( 'name' => $name ), false );
- if ( empty( $pod ) )
- return array();
- $defaults = array(
- 'name' => 'post',
- 'object' => 'post',
- 'type' => 'post_type'
- );
- $pod = array_merge( $defaults, (array) $pod );
- if ( empty( $pod[ 'name' ] ) )
- $pod[ 'name' ] = $pod[ 'object' ];
- elseif ( empty( $pod[ 'object' ] ) )
- $pod[ 'object' ] = $pod[ 'name' ];
- if ( $pod[ 'type' ] != $type )
- return array();
- return $pod;
- }
- /**
- * @param $type
- * @param $name
- *
- * @return array
- */
- public function groups_get ( $type, $name ) {
- if ( 'post_type' == $type && 'attachment' == $name ) {
- $type = 'media';
- $name = 'media';
- }
- do_action( 'pods_meta_groups', $type, $name );
- $pod = array();
- $fields = array();
- $object = self::$post_types;
- if ( 'taxonomy' == $type )
- $object = self::$taxonomies;
- elseif ( 'media' == $type )
- $object = self::$media;
- elseif ( 'user' == $type )
- $object = self::$user;
- elseif ( 'comment' == $type )
- $object = self::$comment;
- if ( 'pod' != $type && !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
- $fields = $object[ $name ][ 'fields' ];
- else {
- $pod = $this->api->load_pod( array( 'name' => $name ), false );
- if ( !empty( $pod ) )
- $fields = $pod[ 'fields' ];
- }
- $defaults = array(
- 'name' => 'post',
- 'object' => 'post',
- 'type' => 'post_type'
- );
- $pod = array_merge( $defaults, (array) $pod );
- if ( empty( $pod[ 'name' ] ) )
- $pod[ 'name' ] = $pod[ 'object' ];
- elseif ( empty( $pod[ 'object' ] ) )
- $pod[ 'object' ] = $pod[ 'name' ];
- if ( $pod[ 'type' ] != $type )
- return array();
- $groups = array(
- array(
- 'pod' => $pod,
- 'label' => apply_filters( 'pods_meta_default_box_title', __( 'More Fields', 'pods' ), $pod, $fields, $type, $name ),
- 'fields' => $fields,
- 'context' => 'normal',
- 'priority' => 'default'
- )
- );
- if ( isset( self::$groups[ $type ] ) && isset( self::$groups[ $type ][ $name ] ) )
- $groups = self::$groups[ $type ][ $name ];
- return $groups;
- }
- /**
- * @param $post_type
- * @param null $post
- */
- public function meta_post_add ( $post_type, $post = null ) {
- if ( 'comment' == $post_type )
- return;
- if ( is_object( $post ) )
- $post_type = $post->post_type;
- $groups = $this->groups_get( 'post_type', $post_type );
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( empty( $group[ 'label' ] ) )
- $group[ 'label' ] = get_post_type_object( $post_type )->labels->label;
- add_meta_box(
- $post_type . '-pods-meta-' . sanitize_title( $group[ 'label' ] ),
- $group[ 'label' ],
- array( $this, 'meta_post' ),
- $post_type,
- $group[ 'context' ],
- $group[ 'priority' ],
- array( 'group' => $group )
- );
- }
- }
- /**
- * @param $post
- * @param $metabox
- */
- public function meta_post ( $post, $metabox ) {
- wp_enqueue_style( 'pods-form' );
- wp_enqueue_script( 'pods' );
- ?>
- <table class="form-table pods-metabox pods-admin pods-dependency">
- <?php
- $id = null;
- if ( is_object( $post ) && false === strpos( $_SERVER[ 'REQUEST_URI' ], '/post-new.php?' ) )
- $id = $post->ID;
- $pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
- foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) )
- continue;
- $value = '';
- if ( !empty( $pod ) ) {
- pods_no_conflict_on( 'post' );
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
- pods_no_conflict_off( 'post' );
- }
- elseif ( !empty( $id ) )
- $value = get_post_meta( $id, $field[ 'name' ], true );
- $depends = PodsForm::dependencies( $field, 'pods-meta-' );
- ?>
- <tr class="form-field pods-field <?php echo $depends; ?>">
- <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], pods_var_raw( 'label_options', $field ) ); ?></th>
- <td>
- <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
- <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
- </td>
- </tr>
- <?php
- }
- ?>
- </table>
- <input type="hidden" name="pods_metasave" value="1" />
- <script type="text/javascript">
- jQuery( function ( $ ) {
- $( document ).Pods( 'dependency', true );
- } );
- </script>
- <?php
- }
- /**
- * @param $post_id
- * @param $post
- *
- * @return mixed
- */
- public function save_post ( $post_id, $post ) {
- $blacklisted_types = array(
- 'revision',
- '_pods_pod',
- '_pods_field'
- );
- $blacklisted_types = apply_filters( 'pods_meta_save_post_blacklist_types', $blacklisted_types, $post_id, $post );
- if ( empty( $_POST ) || 1 != pods_var( 'pods_metasave', 'post' ) )
- return $post_id;
- // @todo Figure out how to hook into autosave for saving meta
- // Block Autosave and Revisions
- if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || in_array( $post->post_type, $blacklisted_types ) )
- return $post_id;
- // Block Quick Edits / Bulk Edits
- 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' ) ) ) )
- return $post_id;
- // Block Trash
- if ( in_array( pods_var( 'action', 'get' ), array( 'untrash', 'trash' ) ) )
- return $post_id;
- // Block Auto-drafting and Trash (not via Admin action)
- $blacklisted_status = array(
- 'auto-draft',
- 'trash'
- );
- $blacklisted_status = apply_filters( 'pods_meta_save_post_blacklist_status', $blacklisted_status, $post_id, $post );
- if ( in_array( $post->post_status, $blacklisted_status ) )
- return $post_id;
- $groups = $this->groups_get( 'post_type', $post->post_type );
- if ( empty( $groups ) )
- return $post_id;
- $data = array();
- $id = $post_id;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $data[ $field[ 'name' ] ] = '';
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
- }
- }
- do_action( 'pods_meta_save_pre_post', $data, $pod, $id, $groups, $post, $post->post_type );
- do_action( "pods_meta_save_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
- pods_no_conflict_on( 'post' );
- if ( !empty( $pod ) ) {
- // Fix for Pods doing it's own sanitization
- $data = stripslashes_deep( $data );
- $pod->save( $data );
- }
- elseif ( !empty( $id ) ) {
- foreach ( $data as $field => $value ) {
- update_post_meta( $id, $field, $value );
- }
- }
- pods_no_conflict_off( 'post' );
- do_action( 'pods_meta_save_post', $data, $pod, $id, $groups, $post, $post->post_type );
- do_action( "pods_meta_save_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
- return $post_id;
- }
- /**
- * @param $form_fields
- * @param $post
- *
- * @return array
- */
- public function meta_media ( $form_fields, $post ) {
- $groups = $this->groups_get( 'media', 'media' );
- if ( empty( $groups ) || 'attachment' == pods_var( 'typenow', 'global' ) )
- return $form_fields;
- wp_enqueue_style( 'pods-form' );
- $id = null;
- if ( is_object( $post ) )
- $id = $post->ID;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $value = '';
- if ( !empty( $pod ) )
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
- elseif ( !empty( $id ) ) {
- pods_no_conflict_on( 'post' );
- $value = get_post_meta( $id, $field[ 'name' ], true );
- pods_no_conflict_off( 'post' );
- }
- $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = array(
- 'label' => $field[ 'label' ],
- 'input' => 'html',
- 'html' => PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ) . PodsForm::field( 'pods_metasave', 1, 'hidden' ),
- 'helps' => PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field )
- );
- }
- }
- return $form_fields;
- }
- /**
- * @param $post
- * @param $attachment
- *
- * @return mixed
- */
- public function save_media ( $post, $attachment ) {
- $groups = $this->groups_get( 'media', 'media' );
- if ( empty( $groups ) )
- return $post;
- $post_id = $attachment;
- if ( empty( $_POST ) || 1 != pods_var( 'pods_metasave', 'post' ) )
- return $post;
- if ( is_array( $post ) && !empty( $post ) && isset( $post[ 'ID' ] ) && 'attachment' == $post[ 'post_type' ] )
- $post_id = $post[ 'ID' ];
- if ( is_array( $post_id ) || empty( $post_id ) )
- return $post;
- $data = array();
- $id = $post_id;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $data[ $field[ 'name' ] ] = '';
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
- }
- }
- do_action( 'pods_meta_save_pre_media', $data, $pod, $id, $groups, $post, $attachment );
- if ( !empty( $pod ) ) {
- // Fix for Pods doing it's own sanitization
- $data = stripslashes_deep( $data );
- $pod->save( $data );
- }
- elseif ( !empty( $id ) ) {
- pods_no_conflict_on( 'post' );
- foreach ( $data as $field => $value ) {
- update_post_meta( $id, $field, $value );
- }
- pods_no_conflict_off( 'post' );
- }
- do_action( 'pods_meta_save_media', $data, $pod, $id, $groups, $post, $attachment );
- return $post;
- }
- public function save_media_ajax () {
- if ( !isset( $_POST[ 'id' ] ) || empty( $_POST[ 'id' ] ) || absint( $_POST[ 'id' ] ) < 1 )
- return;
- $id = absint( $_POST[ 'id' ] );
- if ( !isset( $_POST[ 'nonce' ] ) || empty( $_POST[ 'nonce' ] ) )
- return;
- check_ajax_referer( 'update-post_' . $id, 'nonce' );
- if ( !current_user_can( 'edit_post', $id ) )
- return;
- $post = get_post( $id, ARRAY_A );
- if ( 'attachment' != $post[ 'post_type' ] )
- return;
- // fix ALL THE THINGS
- if ( !isset( $_REQUEST[ 'attachments' ] ) )
- $_REQUEST[ 'attachments' ] = array();
- if ( !isset( $_REQUEST[ 'attachments' ][ $id ] ) )
- $_REQUEST[ 'attachments' ][ $id ] = array();
- if ( empty( $_REQUEST[ 'attachments' ][ $id ] ) )
- $_REQUEST[ 'attachments' ][ $id ][ '_fix_wp' ] = 1;
- }
- /**
- * @param $tag
- * @param null $taxonomy
- */
- public function meta_taxonomy ( $tag, $taxonomy = null ) {
- wp_enqueue_style( 'pods-form' );
- $taxonomy_name = $taxonomy;
- if ( !is_object( $tag ) )
- $taxonomy_name = $tag;
- $groups = $this->groups_get( 'taxonomy', $taxonomy_name );
- $id = null;
- if ( is_object( $tag ) )
- $id = $tag->term_id;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $value = '';
- if ( !empty( $pod ) )
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
- if ( !is_object( $tag ) ) {
- ?>
- <div class="form-field pods-field">
- <?php
- echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] );
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
- echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
- ?>
- </div>
- <?php
- }
- else {
- ?>
- <tr class="form-field pods-field">
- <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] ); ?></th>
- <td>
- <?php
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
- echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
- ?>
- </td>
- </tr>
- <?php
- }
- }
- }
- }
- /**
- * @param $term_id
- * @param $term_taxonomy_id
- * @param $taxonomy
- */
- public function save_taxonomy ( $term_id, $term_taxonomy_id, $taxonomy ) {
- $groups = $this->groups_get( 'taxonomy', $taxonomy );
- if ( empty( $groups ) )
- return;
- $term = get_term( $term_id, $taxonomy );
- $data = array(
- 'name' => $term->name
- );
- $id = $term_id;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $data[ $field[ 'name' ] ] = '';
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
- }
- }
- do_action( 'pods_meta_save_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
- do_action( "pods_meta_save_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
- if ( !empty( $pod ) ) {
- // Fix for Pods doing it's own sanitization
- $data = stripslashes_deep( $data );
- $pod->save( $data );
- }
- do_action( 'pods_meta_save_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
- do_action( "pods_meta_save_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
- }
- /**
- * @param $user_id
- */
- public function meta_user ( $user_id ) {
- wp_enqueue_style( 'pods-form' );
- if ( is_object( $user_id ) )
- $user_id = $user_id->ID;
- $groups = $this->groups_get( 'user', 'user' );
- if ( is_object( $user_id ) )
- $user_id = $user_id->ID;
- $id = $user_id;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- ?>
- <h3><?php echo $group[ 'label' ]; ?></h3>
- <table class="form-table pods-meta">
- <tbody>
- <?php
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $value = '';
- if ( !empty( $pod ) )
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
- elseif ( !empty( $id ) ) {
- pods_no_conflict_on( 'user' );
- $value = get_user_meta( $id, $field[ 'name' ], true );
- pods_no_conflict_off( 'user' );
- }
- ?>
- <tr class="form-field pods-field">
- <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] ); ?></th>
- <td>
- <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
- <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
- </td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- <?php
- }
- }
- /**
- * @param $user_id
- */
- public function save_user ( $user_id ) {
- $groups = $this->groups_get( 'user', 'user' );
- if ( empty( $groups ) )
- return;
- if ( is_object( $user_id ) )
- $user_id = $user_id->ID;
- $data = array();
- $id = $user_id;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $data[ $field[ 'name' ] ] = '';
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
- }
- }
- if ( !empty( $pod ) ) {
- // Fix for Pods doing it's own sanitization
- $data = stripslashes_deep( $data );
- $pod->save( $data );
- }
- elseif ( !empty( $id ) ) {
- pods_no_conflict_on( 'user' );
- foreach ( $data as $field => $value ) {
- update_user_meta( $id, $field, $value );
- }
- pods_no_conflict_off( 'user' );
- }
- do_action( 'pods_meta_save_user', $data, $pod, $id, $groups );
- }
- /**
- * @param $commenter
- * @param $user_identity
- */
- public function meta_comment_new_logged_in ( $commenter, $user_identity ) {
- wp_enqueue_style( 'pods-form' );
- $groups = $this->groups_get( 'comment', 'comment' );
- $id = null;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $value = '';
- if ( !empty( $pod ) )
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
- elseif ( !empty( $id ) ) {
- pods_no_conflict_on( 'comment' );
- $value = get_comment_meta( $id, $field[ 'name' ], true );
- pods_no_conflict_off( 'comment' );
- }
- ?>
- <p class="comment-form-author comment-form-pods-meta-<?php echo $field[ 'name' ]; ?> pods-field">
- <?php
- echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] );
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
- echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
- ?>
- </p>
- <?php
- }
- }
- }
- /**
- * @param $form_fields
- *
- * @return array
- */
- public function meta_comment_new ( $form_fields ) {
- wp_enqueue_style( 'pods-form' );
- $groups = $this->groups_get( 'comment', 'comment' );
- $id = null;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $value = '';
- if ( !empty( $pod ) )
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
- elseif ( !empty( $id ) ) {
- pods_no_conflict_on( 'comment' );
- $value = get_comment_meta( $id, $field[ 'name' ], true );
- pods_no_conflict_off( 'comment' );
- }
- ob_start();
- ?>
- <p class="comment-form-author comment-form-pods-meta-<?php echo $field[ 'name' ]; ?> pods-field">
- <?php
- echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] );
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
- echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
- ?>
- </p>
- <?php
- $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = ob_get_clean();
- }
- }
- return $form_fields;
- }
- /**
- * @param $comment_type
- * @param null $comment
- */
- public function meta_comment_add ( $comment_type, $comment = null ) {
- if ( is_object( $comment ) && isset( $comment_type->comment_type ) )
- $comment_type = $comment->comment_type;
- if ( is_object( $comment_type ) && isset( $comment_type->comment_type ) ) {
- $comment = $comment_type;
- $comment_type = $comment_type->comment_type;
- }
- if ( is_object( $comment_type ) )
- return;
- elseif ( empty( $comment_type ) )
- $comment_type = 'comment';
- $groups = $this->groups_get( 'comment', $comment_type );
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- add_meta_box(
- $comment_type . '-pods-meta-' . sanitize_title( $group[ 'label' ] ),
- $group[ 'label' ],
- array( $this, 'meta_comment' ),
- $comment_type,
- $group[ 'context' ],
- $group[ 'priority' ],
- array( 'group' => $group )
- );
- }
- }
- /**
- * @param $comment
- * @param $metabox
- */
- public function meta_comment ( $comment, $metabox ) {
- wp_enqueue_style( 'pods-form' );
- ?>
- <table class="form-table editcomment pods-metabox">
- <?php
- $id = null;
- if ( is_object( $comment ) )
- $id = $comment->comment_ID;
- $pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
- foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) )
- continue;
- $value = '';
- if ( !empty( $pod ) )
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
- ?>
- <tr class="form-field pods-field">
- <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] ); ?></th>
- <td>
- <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
- <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
- </td>
- </tr>
- <?php
- }
- ?>
- </table>
- <?php
- }
- /**
- * @param $approved
- * @param $commentdata
- */
- public function validate_comment ( $approved, $commentdata ) {
- $groups = $this->groups_get( 'comment', 'comment' );
- if ( empty( $groups ) )
- return $approved;
- $data = array();
- $pod = null;
- $id = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $data[ $field[ 'name' ] ] = '';
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
- $validate = $this->api->handle_field_validation( $data[ $field[ 'name' ] ], $field[ 'name' ], $this->api->get_wp_object_fields( 'comment' ), $pod->fields(), $pod, array() );
- if ( false === $validate )
- $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field[ 'label' ] );
- if ( !is_bool( $validate ) && !empty( $validate ) )
- return pods_error( $validate, $this );
- }
- }
- return $approved;
- }
- /**
- * @param $comment_id
- */
- public function save_comment ( $comment_id ) {
- $groups = $this->groups_get( 'comment', 'comment' );
- if ( empty( $groups ) )
- return $comment_id;
- $data = array();
- $id = $comment_id;
- $pod = null;
- foreach ( $groups as $group ) {
- if ( empty( $group[ 'fields' ] ) )
- continue;
- if ( null === $pod )
- $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
- foreach ( $group[ 'fields' ] as $field ) {
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
- continue;
- $data[ $field[ 'name' ] ] = '';
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
- }
- }
- do_action( 'pods_meta_save_pre_comment', $data, $pod, $id, $groups );
- if ( !empty( $pod ) ) {
- // Fix for Pods doing it's own sanitization
- $data = stripslashes_deep( $data );
- $pod->save( $data );
- }
- elseif ( !empty( $id ) ) {
- pods_no_conflict_on( 'comment' );
- foreach ( $data…
Large files files are truncated, but you can click here to view the full file