/classes/PodsAPI.php
PHP | 6128 lines | 3893 code | 1196 blank | 1039 comment | 1248 complexity | ec4c5a41cc728c74d964fb5f059538e3 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 PodsAPI {
- /**
- * @var bool
- */
- public $display_errors = false;
- /**
- * @var array|bool|mixed|null|void
- */
- public $pod_data;
- /**
- * @var
- */
- public $pod;
- /**
- * @var
- */
- public $pod_id;
- /**
- * @var
- */
- public $fields;
- /**
- * @var
- * @deprecated 2.0.0
- */
- public $format = null;
- /**
- * @var
- */
- private $deprecated;
- /**
- * Store and retrieve data programatically
- *
- * @param string $pod (optional) The pod name
- * @param string $format (deprecated) Format for import/export, "php" or "csv"
- *
- * @license http://www.gnu.org/licenses/gpl-2.0.html
- * @since 1.7.1
- */
- public function __construct ( $pod = null, $format = null ) {
- if ( null !== $pod && 0 < strlen( (string) $pod ) ) {
- if ( null !== $format ) {
- $this->format = $format;
- pods_deprecated( 'pods_api( $pod, $format )', '2.0.0', 'pods_api( $pod )' );
- }
- $pod = pods_clean_name( $pod );
- $pod = $this->load_pod( array( 'name' => $pod ), false );
- if ( !empty( $pod ) ) {
- $this->pod_data = $pod;
- $this->pod = $pod[ 'name' ];
- $this->pod_id = $pod[ 'id' ];
- $this->fields = $pod[ 'fields' ];
- }
- }
- }
- /**
- * Save a WP object and its meta
- *
- * @param string $object_type Object type: post|user|comment
- * @param array $data All post data to be saved
- * @param array $meta (optional) Associative array of meta keys and values
- * @param bool $strict (optional) Decides whether the previous saved meta should be deleted or not
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
- *
- * @return bool|mixed
- *
- * @since 2.0.0
- */
- public function save_wp_object ( $object_type, $data, $meta = array(), $strict = false, $sanitized = false ) {
- if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
- $object_type = 'post';
- if ( $sanitized ) {
- $data = pods_unsanitize( $data );
- $meta = pods_unsanitize( $meta );
- }
- if ( in_array( $object_type, array( 'post', 'user', 'comment' ) ) )
- return call_user_func( array( $this, 'save_' . $object_type ), $data, $meta, $strict, false );
- return false;
- }
- /**
- * Delete a WP object
- *
- * @param string $object_type Object type: post|user|comment
- * @param int $id Object ID
- * @param bool $force_delete (optional) Force deletion instead of trashing (post types only)
- *
- * @return bool|mixed
- *
- * @since 2.0.0
- */
- public function delete_wp_object ( $object_type, $id, $force_delete = true ) {
- if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
- $object_type = 'post';
- if ( empty( $id ) )
- return false;
- if ( in_array( $object_type, array( 'post' ) ) )
- return wp_delete_post( $id, $force_delete );
- if ( function_exists( 'wp_delete_' . $object_type ) )
- return call_user_func( 'wp_delete_' . $object_type, $id );
- return false;
- }
- /**
- * Save a post and it's meta
- *
- * @param array $post_data All post data to be saved (using wp_insert_post / wp_update_post)
- * @param array $post_meta (optional) All meta to be saved (set value to null to delete)
- * @param bool $strict (optional) Whether to delete previously saved meta not in $post_meta
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
- * @return mixed|void
- */
- public function save_post ( $post_data, $post_meta = null, $strict = false, $sanitized = false ) {
- pods_no_conflict_on( 'post' );
- if ( !is_array( $post_data ) || empty( $post_data ) )
- $post_data = array( 'post_title' => '' );
- if ( !is_array( $post_meta ) )
- $post_meta = array();
- if ( $sanitized ) {
- $post_data = pods_unsanitize( $post_data );
- $post_meta = pods_unsanitize( $post_meta );
- }
- if ( !isset( $post_data[ 'ID' ] ) || empty( $post_data[ 'ID' ] ) )
- $post_data[ 'ID' ] = wp_insert_post( $post_data, true );
- elseif ( 2 < count( $post_data ) || !isset( $post_data[ 'post_type' ] ) )
- wp_update_post( $post_data );
- if ( is_wp_error( $post_data[ 'ID' ] ) ) {
- pods_no_conflict_off( 'post' );
- /**
- * @var $post_error WP_Error
- */
- $post_error = $post_data[ 'ID' ];
- return pods_error( $post_error->get_error_message(), $this );
- }
- $this->save_post_meta( $post_data[ 'ID' ], $post_meta, $strict );
- pods_no_conflict_off( 'post' );
- return $post_data[ 'ID' ];
- }
- /**
- * Save a post's meta
- *
- * @param int $id Post ID
- * @param array $post_meta All meta to be saved (set value to null to delete)
- * @param bool $strict Whether to delete previously saved meta not in $post_meta
- *
- * @return int Id of the post with the meta
- *
- * @since 2.0.0
- */
- public function save_post_meta ( $id, $post_meta = null, $strict = false ) {
- pods_no_conflict_on( 'post' );
- if ( !is_array( $post_meta ) )
- $post_meta = array();
- $id = (int) $id;
- $meta = get_post_meta( $id );
- foreach ( $meta as &$value ) {
- if ( is_array( $value ) && 1 == count( $value ) && isset( $value[ 0 ] ) )
- $value = $value[ 0 ];
- }
- foreach ( $post_meta as $meta_key => $meta_value ) {
- if ( null === $meta_value ) {
- $old_meta_value = '';
- if ( isset( $meta[ $meta_key ] ) )
- $old_meta_value = $meta[ $meta_key ];
- delete_post_meta( $id, $meta_key, $old_meta_value );
- }
- else
- update_post_meta( $id, $meta_key, $meta_value );
- }
- if ( $strict ) {
- foreach ( $meta as $meta_key => $meta_value ) {
- if ( !isset( $post_meta[ $meta_key ] ) )
- delete_post_meta( $id, $meta_key, $meta_value );
- }
- }
- pods_no_conflict_off( 'post' );
- return $id;
- }
- /**
- * Save a user and it's meta
- *
- * @param array $user_data All user data to be saved (using wp_insert_user / wp_update_user)
- * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
- * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
- *
- * @return int Returns user id on success
- *
- * @since 2.0.0
- */
- public function save_user ( $user_data, $user_meta = null, $strict = false, $sanitized = false ) {
- if ( !is_array( $user_data ) || empty( $user_data ) )
- return pods_error( __( 'User data is required but is either invalid or empty', 'pods' ), $this );
- pods_no_conflict_on( 'user' );
- if ( !is_array( $user_meta ) )
- $user_meta = array();
- if ( $sanitized ) {
- $user_data = pods_unsanitize( $user_data );
- $user_meta = pods_unsanitize( $user_meta );
- }
- if ( !isset( $user_data[ 'ID' ] ) || empty( $user_data[ 'ID' ] ) )
- $user_data[ 'ID' ] = wp_insert_user( $user_data );
- else
- wp_update_user( $user_data );
- if ( is_wp_error( $user_data[ 'ID' ] ) ) {
- pods_no_conflict_off( 'user' );
- /**
- * @var $user_error WP_Error
- */
- $user_error = $user_data[ 'ID' ];
- return pods_error( $user_error->get_error_message(), $this );
- }
- $this->save_user_meta( $user_data[ 'ID' ], $user_meta, $strict );
- pods_no_conflict_off( 'user' );
- return $user_data[ 'ID' ];
- }
- /**
- * Save a user meta
- *
- * @param int $id User ID
- * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
- * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
- *
- * @return int User ID
- *
- * @since 2.0.0
- *
- */
- public function save_user_meta ( $id, $user_meta = null, $strict = false ) {
- pods_no_conflict_on( 'user' );
- if ( !is_array( $user_meta ) )
- $user_meta = array();
- $id = (int) $id;
- $meta = get_user_meta( $id );
- foreach ( $user_meta as $meta_key => $meta_value ) {
- if ( null === $meta_value ) {
- $old_meta_value = '';
- if ( isset( $meta[ $meta_key ] ) )
- $old_meta_value = $meta[ $meta_key ];
- delete_user_meta( $id, $meta_key, $old_meta_value );
- }
- else
- update_user_meta( $id, $meta_key, $meta_value );
- }
- if ( $strict ) {
- foreach ( $meta as $meta_key => $meta_value ) {
- if ( !isset( $user_meta[ $meta_key ] ) )
- delete_user_meta( $id, $meta_key, $user_meta[ $meta_key ] );
- }
- }
- pods_no_conflict_off( 'user' );
- return $id;
- }
- /**
- * Save a comment and it's meta
- *
- * @param array $comment_data All comment data to be saved (using wp_insert_comment / wp_update_comment)
- * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
- * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
- *
- * @return int Comment ID
- *
- * @since 2.0.0
- */
- public function save_comment ( $comment_data, $comment_meta = null, $strict = false, $sanitized = false ) {
- if ( !is_array( $comment_data ) || empty( $comment_data ) )
- return pods_error( __( 'Comment data is required but is either invalid or empty', 'pods' ), $this );
- pods_no_conflict_on( 'comment' );
- if ( !is_array( $comment_meta ) )
- $comment_meta = array();
- if ( $sanitized ) {
- $comment_data = pods_unsanitize( $comment_data );
- $comment_meta = pods_unsanitize( $comment_meta );
- }
- if ( !isset( $comment_data[ 'comment_ID' ] ) || empty( $comment_data[ 'comment_ID' ] ) )
- $comment_data[ 'comment_ID' ] = wp_insert_comment( $comment_data );
- else
- wp_update_comment( $comment_data );
- if ( is_wp_error( $comment_data[ 'comment_ID' ] ) ) {
- pods_no_conflict_off( 'comment' );
- /**
- * @var $comment_error WP_Error
- */
- $comment_error = $comment_data[ 'comment_ID' ];
- return pods_error( $comment_error->get_error_message(), $this );
- }
- $this->save_comment_meta( $comment_data[ 'comment_ID' ], $comment_meta, $strict );
- pods_no_conflict_off( 'comment' );
- return $comment_data[ 'comment_ID' ];
- }
- /**
- * Save a comment meta
- *
- * @param int $id Comment ID
- * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
- * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
- *
- * @return int Comment ID
- *
- * @since 2.0.0
- */
- public function save_comment_meta ( $id, $comment_meta = null, $strict = false ) {
- pods_no_conflict_on( 'comment' );
- if ( !is_array( $comment_meta ) )
- $comment_meta = array();
- $id = (int) $id;
- $meta = get_comment_meta( $id );
- foreach ( $comment_meta as $meta_key => $meta_value ) {
- if ( null === $meta_value ) {
- $old_meta_value = '';
- if ( isset( $meta[ $meta_key ] ) )
- $old_meta_value = $meta[ $meta_key ];
- delete_comment_meta( $id, $meta_key, $old_meta_value );
- }
- else
- update_comment_meta( $id, $meta_key, $meta_value );
- }
- if ( $strict ) {
- foreach ( $meta as $meta_key => $meta_value ) {
- if ( !isset( $comment_meta[ $meta_key ] ) )
- delete_comment_meta( (int) $id, $meta_key, $comment_meta[ $meta_key ] );
- }
- }
- pods_no_conflict_off( 'comment' );
- return $id;
- }
- /**
- * Save a taxonomy's term
- *
- * @param int $term_ID Term ID, leave empty to add
- * @param string $term Term name
- * @param string $taxonomy Taxonomy name
- * @param array $term_data All term data to be saved (using wp_insert_term / wp_update_term)
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
- *
- * @return int Term ID
- *
- * @since 2.0.0
- */
- public function save_term ( $term_ID, $term, $taxonomy, $term_data, $sanitized = false ) {
- pods_no_conflict_on( 'taxonomy' );
- if ( !is_array( $term_data ) )
- $term_data = array();
- $term_ID = (int) $term_ID;
- if ( $sanitized ) {
- $term = pods_unsanitize( $term );
- $taxonomy = pods_unsanitize( $taxonomy );
- $term_data = pods_unsanitize( $term_data );
- }
- if ( empty( $term_ID ) )
- $term_ID = wp_insert_term( $term, $taxonomy, $term_data );
- else {
- if ( 0 < strlen( $term ) )
- $term_data[ 'term' ] = $term;
- if ( empty( $term_data ) ) {
- pods_no_conflict_off( 'taxonomy' );
- return pods_error( __( 'Taxonomy term data is required but is either invalid or empty', 'pods' ), $this );
- }
- wp_update_term( $term_ID, $taxonomy, $term_data );
- }
- if ( is_wp_error( $term_ID ) ) {
- pods_no_conflict_off( 'taxonomy' );
- return pods_error( $term_ID->get_error_message(), $this );
- }
- elseif ( is_array( $term_ID ) )
- $term_ID = $term_ID[ 'term_id' ];
- pods_no_conflict_off( 'taxonomy' );
- return $term_ID;
- }
- /**
- * Rename a WP object's type
- *
- * @param string $object_type Object type: post|taxonomy|comment
- * @param string $old_name The old name
- * @param string $new_name The new name
- *
- * @return bool
- *
- * @since 2.0.0
- */
- public function rename_wp_object_type ( $object_type, $old_name, $new_name ) {
- /**
- * @var $wpdb wpdb
- */
- global $wpdb;
- if ( 'post_type' == $object_type )
- $object_type = 'post';
- if ( 'post' == $object_type ) {
- pods_query( "UPDATE `{$wpdb->posts}` SET `post_type` = %s WHERE `post_type` = %s", array(
- $new_name,
- $old_name
- ) );
- }
- elseif ( 'taxonomy' == $object_type ) {
- pods_query( "UPDATE `{$wpdb->term_taxonomy}` SET `taxonomy` = %s WHERE `taxonomy` = %s", array(
- $new_name,
- $old_name
- ) );
- }
- elseif ( 'comment' == $object_type ) {
- pods_query( "UPDATE `{$wpdb->comments}` SET `comment_type` = %s WHERE `comment_type` = %s", array(
- $new_name,
- $old_name
- ) );
- }
- return true;
- }
- /**
- * Get a list of core WP object fields for a specific object
- *
- * @param string $object The post type to look for, possible values: post_type, user, comment, taxonomy
- * @param array $pod Array of Pod data
- * @param boolean $refresh Whether to force refresh the information
- *
- * @return array Array of fields
- */
- public function get_wp_object_fields ( $object = 'post_type', $pod = null, $refresh = false ) {
- $pod_name = pods_var_raw( 'name', $pod );
- $fields = pods_transient_get( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ) );
- if ( false !== $fields && !$refresh )
- return $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
- $fields = array();
- if ( 'post_type' == $object ) {
- $fields = array(
- 'post_title' => array(
- 'name' => 'post_title',
- 'label' => 'Title',
- 'type' => 'text',
- 'alias' => array( 'title', 'name' )
- ),
- 'post_content' => array(
- 'name' => 'post_content',
- 'label' => 'Content',
- 'type' => 'wysiwyg',
- 'alias' => array( 'content' ),
- 'options' => array(
- 'wysiwyg_allow_html' => 1,
- 'wysiwyg_allowed_html_tags' => '',
- 'display_filter' => 'the_content',
- 'pre_save' => 0
- )
- ),
- 'post_excerpt' => array(
- 'name' => 'post_excerpt',
- 'label' => 'Excerpt',
- 'type' => 'paragraph',
- 'alias' => array( 'excerpt' ),
- 'options' => array(
- 'paragraph_allow_html' => 1,
- 'paragraph_allowed_html_tags' => '',
- 'display_filter' => 'the_excerpt',
- 'pre_save' => 0
- )
- ),
- 'post_author' => array(
- 'name' => 'post_author',
- 'label' => 'Author',
- 'type' => 'pick',
- 'alias' => array( 'author' ),
- 'pick_object' => 'user',
- 'options' => array(
- 'pick_format_type' => 'single',
- 'pick_format_single' => 'autocomplete',
- 'default_value' => '{@user.ID}'
- )
- ),
- 'post_date' => array(
- 'name' => 'post_date',
- 'label' => 'Publish Date',
- 'type' => 'datetime',
- 'alias' => array( 'created', 'date' )
- ),
- 'post_date_gmt' => array(
- 'name' => 'post_date_gmt',
- 'label' => 'Publish Date (GMT)',
- 'type' => 'datetime',
- 'alias' => array(),
- 'hidden' => true
- ),
- 'post_status' => array(
- 'name' => 'post_status',
- 'label' => 'Status',
- 'type' => 'pick',
- 'pick_object' => 'post-status',
- 'default' => $this->do_hook( 'default_status_' . pods_var_raw( 'name', $pod, $object, null, true ), pods_var( 'default_status', pods_var_raw( 'options', $pod ), 'draft', null, true ), $pod ),
- 'alias' => array( 'status' )
- ),
- 'comment_status' => array(
- 'name' => 'comment_status',
- 'label' => 'Comment Status',
- 'type' => 'text',
- 'default' => get_option( 'default_comment_status', 'open' ),
- 'alias' => array(),
- 'data' => array(
- 'open' => __( 'Open', 'pods' ),
- 'closed' => __( 'Closed', 'pods' )
- )
- ),
- 'ping_status' => array(
- 'name' => 'ping_status',
- 'label' => 'Ping Status',
- 'default' => get_option( 'default_ping_status', 'open' ),
- 'type' => 'text',
- 'alias' => array(),
- 'data' => array(
- 'open' => __( 'Open', 'pods' ),
- 'closed' => __( 'Closed', 'pods' )
- )
- ),
- 'post_password' => array(
- 'name' => 'post_password',
- 'label' => 'Password',
- 'type' => 'text',
- 'alias' => array()
- ),
- 'post_name' => array(
- 'name' => 'post_name',
- 'label' => 'Permalink',
- 'type' => 'slug',
- 'alias' => array( 'slug', 'permalink' )
- ),
- 'to_ping' => array(
- 'name' => 'to_ping',
- 'label' => 'To Ping',
- 'type' => 'text',
- 'alias' => array(),
- 'hidden' => true
- ),
- 'pinged' => array(
- 'name' => 'pinged',
- 'label' => 'Pinged',
- 'type' => 'text',
- 'alias' => array(),
- 'hidden' => true
- ),
- 'post_modified' => array(
- 'name' => 'post_modified',
- 'label' => 'Last Modified Date',
- 'type' => 'datetime',
- 'alias' => array( 'modified' ),
- 'hidden' => true
- ),
- 'post_modified_gmt' => array(
- 'name' => 'post_modified_gmt',
- 'label' => 'Last Modified Date (GMT)',
- 'type' => 'datetime',
- 'alias' => array(),
- 'hidden' => true
- ),
- 'post_content_filtered' => array(
- 'name' => 'post_content_filtered',
- 'label' => 'Content (filtered)',
- 'type' => 'paragraph',
- 'alias' => array(),
- 'hidden' => true,
- 'options' => array(
- 'paragraph_allow_html' => 1,
- 'paragraph_oembed' => 1,
- 'paragraph_wptexturize' => 1,
- 'paragraph_convert_chars' => 1,
- 'paragraph_wpautop' => 1,
- 'paragraph_allow_shortcode' => 1,
- 'paragraph_allowed_html_tags' => ''
- )
- ),
- 'post_parent' => array(
- 'name' => 'post_parent',
- 'label' => 'Parent',
- 'type' => 'pick',
- 'pick_object' => 'post_type',
- 'alias' => array( 'parent' ),
- 'data' => array(),
- 'hidden' => true
- ),
- 'guid' => array(
- 'name' => 'guid',
- 'label' => 'GUID',
- 'type' => 'text',
- 'alias' => array(),
- 'hidden' => true
- ),
- 'menu_order' => array(
- 'name' => 'menu_order',
- 'label' => 'Menu Order',
- 'type' => 'number',
- 'alias' => array()
- ),
- 'post_type' => array(
- 'name' => 'post_type',
- 'label' => 'Type',
- 'type' => 'text',
- 'alias' => array( 'type' ),
- 'hidden' => true
- ),
- 'post_mime_type' => array(
- 'name' => 'post_mime_type',
- 'label' => 'Mime Type',
- 'type' => 'text',
- 'alias' => array(),
- 'hidden' => true
- ),
- 'comment_count' => array(
- 'name' => 'comment_count',
- 'label' => 'Comment Count',
- 'type' => 'number',
- 'alias' => array(),
- 'hidden' => true
- )
- );
- if ( !empty( $pod ) ) {
- $taxonomies = get_object_taxonomies( pods_var_raw( 'name', $pod ), 'objects' );
- foreach ( $taxonomies as $taxonomy ) {
- $fields[ $taxonomy->name ] = array(
- 'name' => $taxonomy->name,
- 'label' => $taxonomy->labels->name,
- 'type' => 'taxonomy',
- 'alias' => array(),
- 'hidden' => true
- );
- }
- }
- }
- elseif ( 'user' == $object ) {
- $fields = array(
- 'user_login' => array(
- 'name' => 'user_login',
- 'label' => 'Title',
- 'type' => 'text',
- 'alias' => array( 'login' ),
- 'options' => array(
- 'required' => 1
- )
- ),
- 'user_nicename' => array(
- 'name' => 'user_nicename',
- 'label' => 'Permalink',
- 'type' => 'slug',
- 'alias' => array( 'nicename', 'slug', 'permalink' )
- ),
- 'display_name' => array(
- 'name' => 'display_name',
- 'label' => 'Display Name',
- 'type' => 'text',
- 'alias' => array( 'title', 'name' )
- ),
- 'user_pass' => array(
- 'name' => 'user_pass',
- 'label' => 'Password',
- 'type' => 'text',
- 'alias' => array( 'password', 'pass' ),
- 'options' => array(
- 'required' => 1,
- 'text_format_type' => 'password'
- )
- ),
- 'user_email' => array(
- 'name' => 'user_email',
- 'label' => 'E-mail',
- 'type' => 'text',
- 'alias' => array( 'email' ),
- 'options' => array(
- 'required' => 1,
- 'text_format_type' => 'email'
- )
- ),
- 'user_url' => array(
- 'name' => 'user_url',
- 'label' => 'URL',
- 'type' => 'text',
- 'alias' => array( 'url', 'website' ),
- 'options' => array(
- 'required' => 1,
- 'text_format_type' => 'website',
- 'text_format_website' => 'normal'
- )
- ),
- 'user_registered' => array(
- 'name' => 'user_registered',
- 'label' => 'Registration Date',
- 'type' => 'date',
- 'alias' => array( 'created', 'date', 'registered' ),
- 'options' => array(
- 'date_format_type' => 'datetime'
- )
- )
- );
- }
- elseif ( 'comment' == $object ) {
- $fields = array(
- 'comment_content' => array(
- 'name' => 'comment_content',
- 'label' => 'Content',
- 'type' => 'wysiwyg',
- 'alias' => array( 'content' )
- ),
- 'comment_approved' => array(
- 'name' => 'comment_approved',
- 'label' => 'Approved',
- 'type' => 'number',
- 'alias' => array( 'approved' )
- ),
- 'comment_post_ID' => array(
- 'name' => 'comment_post_ID',
- 'label' => 'Post',
- 'type' => 'pick',
- 'alias' => array( 'post', 'post_id' ),
- 'data' => array()
- ),
- 'user_id' => array(
- 'name' => 'user_id',
- 'label' => 'Author',
- 'type' => 'pick',
- 'alias' => array( 'author' ),
- 'pick_object' => 'user',
- 'data' => array()
- ),
- 'comment_date' => array(
- 'name' => 'comment_date',
- 'label' => 'Date',
- 'type' => 'date',
- 'alias' => array( 'created', 'date' ),
- 'options' => array(
- 'date_format_type' => 'datetime'
- )
- )
- );
- }
- elseif ( 'taxonomy' == $object ) {
- $fields = array(
- 'name' => array(
- 'name' => 'name',
- 'label' => 'Title',
- 'type' => 'text',
- 'alias' => array( 'title' )
- ),
- 'slug' => array(
- 'name' => 'slug',
- 'label' => 'Permalink',
- 'type' => 'slug',
- 'alias' => array( 'permalink' )
- ),
- 'description' => array(
- 'name' => 'description',
- 'label' => 'Description',
- 'type' => 'wysiwyg',
- 'alias' => array( 'content' )
- ),
- 'taxonomy' => array(
- 'name' => 'taxonomy',
- 'label' => 'Taxonomy',
- 'type' => 'pick',
- 'alias' => array()
- )
- );
- }
- $fields = $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
- foreach ( $fields as $field => $options ) {
- if ( !isset( $options[ 'alias' ] ) )
- $options[ 'alias' ] = array();
- else
- $options[ 'alias' ] = (array) $options[ 'alias' ];
- if ( !isset( $options[ 'name' ] ) )
- $options[ 'name' ] = $field;
- $fields[ $field ] = $options;
- }
- $fields = PodsForm::fields_setup( $fields );
- if ( did_action( 'init' ) )
- pods_transient_set( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ), $fields );
- return $fields;
- }
- /**
- *
- * @see PodsAPI::save_pod
- *
- * Add a Pod via the Wizard
- *
- * $params['create_extend'] string Create or Extend a Content Type
- * $params['create_pod_type'] string Pod Type (for Creating)
- * $params['create_name'] string Pod Name (for Creating)
- * $params['create_label_plural'] string Plural Label (for Creating)
- * $params['create_label_singular'] string Singular Label (for Creating)
- * $params['create_storage'] string Storage Type (for Creating Post Types)
- * $params['create_storage_taxonomy'] string Storage Type (for Creating Taxonomies)
- * $params['extend_pod_type'] string Pod Type (for Extending)
- * $params['extend_post_type'] string Post Type (for Extending Post Types)
- * $params['extend_taxonomy'] string Taxonomy (for Extending Taxonomies)
- * $params['extend_storage'] string Storage Type (for Extending Post Types / Users / Comments)
- *
- * @param array $params An associative array of parameters
- *
- * @return bool|int Pod ID
- * @since 2.0.0
- */
- public function add_pod ( $params ) {
- $defaults = array(
- 'create_extend' => 'create',
- 'create_pod_type' => 'post_type',
- 'create_name' => '',
- 'create_label_plural' => '',
- 'create_label_singular' => '',
- 'create_storage' => 'meta',
- 'create_storage_taxonomy' => 'none',
- 'extend_pod_type' => 'post_type',
- 'extend_post_type' => 'post',
- 'extend_taxonomy' => 'category',
- 'extend_storage_taxonomy' => 'table',
- 'extend_storage' => 'meta'
- );
- $params = (object) array_merge( $defaults, (array) $params );
- if ( empty( $params->create_extend ) || !in_array( $params->create_extend, array( 'create', 'extend' ) ) )
- return pods_error( __( 'Please choose whether to Create or Extend a Content Type', $this ) );
- $pod_params = array(
- 'name' => '',
- 'label' => '',
- 'type' => '',
- 'storage' => 'table',
- 'object' => '',
- 'options' => array()
- );
- if ( 'create' == $params->create_extend ) {
- if ( empty( $params->create_name ) )
- return pods_error( 'Please enter a Name for this Pod', $this );
- $pod_params[ 'name' ] = $params->create_name;
- $pod_params[ 'label' ] = ( !empty( $params->create_label_plural ) ? $params->create_label_plural : ucwords( str_replace( '_', ' ', $params->create_name ) ) );
- $pod_params[ 'type' ] = $params->create_pod_type;
- $pod_params[ 'options' ] = array(
- 'label_singular' => ( !empty( $params->create_label_singular ) ? $params->create_label_singular : ucwords( str_replace( '_', ' ', $params->create_name ) ) ),
- 'public' => 1,
- 'show_ui' => 1
- );
- if ( 'post_type' == $pod_params[ 'type' ] ) {
- $pod_params[ 'storage' ] = $params->create_storage;
- if ( defined( 'PODS_TABLELESS' ) && PODS_TABLELESS )
- $pod_params[ 'storage' ] = 'meta';
- }
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
- $pod_params[ 'storage' ] = $params->create_storage_taxonomy;
- if ( defined( 'PODS_TABLELESS' ) && PODS_TABLELESS )
- $pod_params[ 'storage' ] = 'none';
- }
- elseif ( defined( 'PODS_TABLELESS' ) && PODS_TABLELESS ) {
- $pod_params[ 'type' ] = 'post_type';
- $pod_params[ 'storage' ] = 'meta';
- }
- }
- elseif ( 'extend' == $params->create_extend ) {
- $pod_params[ 'type' ] = $params->extend_pod_type;
- if ( 'post_type' == $pod_params[ 'type' ] ) {
- $pod_params[ 'storage' ] = $params->extend_storage;
- if ( defined( 'PODS_TABLELESS' ) && PODS_TABLELESS )
- $pod_params[ 'storage' ] = 'meta';
- $pod_params[ 'name' ] = $params->extend_post_type;
- }
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
- $pod_params[ 'storage' ] = $params->extend_storage_taxonomy;
- if ( defined( 'PODS_TABLELESS' ) && PODS_TABLELESS )
- $pod_params[ 'storage' ] = 'none';
- $pod_params[ 'name' ] = $params->extend_taxonomy;
- }
- else {
- $pod_params[ 'storage' ] = $params->extend_storage;
- if ( defined( 'PODS_TABLELESS' ) && PODS_TABLELESS )
- $pod_params[ 'storage' ] = 'meta';
- $pod_params[ 'name' ] = $params->extend_pod_type;
- }
- $pod_params[ 'label' ] = ucwords( str_replace( '_', ' ', $pod_params[ 'name' ] ) );
- $pod_params[ 'object' ] = $pod_params[ 'name' ];
- }
- if ( empty( $pod_params[ 'object' ] ) ) {
- if ( 'post_type' == $pod_params[ 'type' ] ) {
- $check = get_post_type_object( $pod_params[ 'name' ] );
- if ( !empty( $check ) )
- return pods_error( sprintf( __( 'Post Type %s already exists, try extending it instead', 'pods' ), $pod_params[ 'name' ] ), $this );
- $pod_params[ 'options' ][ 'supports_title' ] = 1;
- $pod_params[ 'options' ][ 'supports_editor' ] = 1;
- }
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
- $check = get_taxonomy( $pod_params[ 'name' ] );
- if ( !empty( $check ) )
- return pods_error( sprintf( __( 'Taxonomy %s already exists, try extending it instead', 'pods' ), $pod_params[ 'name' ] ), $this );
- }
- }
- if ( !empty( $pod_params ) )
- return $this->save_pod( $pod_params );
- return false;
- }
- /**
- * Add or edit a Pod
- *
- * $params['id'] int The Pod ID
- * $params['name'] string The Pod name
- * $params['label'] string The Pod label
- * $params['type'] string The Pod type
- * $params['storage'] string The Pod storage
- * $params['options'] array Options
- *
- * @param array $params An associative array of parameters
- * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed, will sanitize them if false.
- * @param bool|int $db (optional) Whether to save into the DB or just return Pod array.
- *
- * @return int Pod ID
- * @since 1.7.9
- */
- public function save_pod ( $params, $sanitized = false, $db = true ) {
- $tableless_field_types = apply_filters( 'pods_tableless_field_types', array( 'pick', 'file', 'avatar', 'taxonomy' ) );
- $load_params = (object) $params;
- if ( isset( $load_params->id ) && isset( $load_params->name ) )
- unset( $load_params->name );
- if ( isset( $load_params->old_name ) )
- $load_params->name = $load_params->old_name;
- $pod = $this->load_pod( $load_params, false );
- $params = (object) $params;
- if ( false === $sanitized )
- $params = pods_sanitize( $params );
- $old_id = $old_name = $old_storage = null;
- $old_fields = $old_options = array();
- if ( isset( $params->name ) )
- $params->name = pods_clean_name( $params->name );
- if ( !empty( $pod ) ) {
- if ( isset( $params->id ) && 0 < $params->id )
- $old_id = $params->id;
- $params->id = $pod[ 'id' ];
- $old_name = $pod[ 'name' ];
- $old_storage = $pod[ 'storage' ];
- $old_fields = $pod[ 'fields' ];
- $old_options = $pod[ 'options' ];
- if ( !isset( $params->name ) && empty( $params->name ) )
- $params->name = $pod[ 'name' ];
- if ( $old_name != $params->name && false !== $this->pod_exists( array( 'name' => $params->name ) ) )
- return pods_error( sprintf( __( 'Pod %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
- if ( $old_name != $params->name && in_array( $pod[ 'type' ], array( 'user', 'comment', 'media' ) ) && in_array( $pod[ 'object' ], array( 'user', 'comment', 'media' ) ) )
- return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
- if ( $old_name != $params->name && in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && !empty( $pod[ 'object' ] ) && $pod[ 'object' ] != $old_name )
- return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
- if ( $old_id != $params->id ) {
- if ( $params->type == $pod[ 'type' ] && isset( $params->object ) && $params->object == $pod[ 'object' ] )
- return pods_error( sprintf( __( 'Pod using %s already exists, you can not reuse an object across multiple pods', 'pods' ), $params->object ), $this );
- else
- return pods_error( sprintf( __( 'Pod %s already exists', 'pods' ), $params->name ), $this );
- }
- }
- else {
- $pod = array(
- 'id' => 0,
- 'name' => $params->name,
- 'label' => $params->name,
- 'description' => '',
- 'type' => 'pod',
- 'storage' => 'table',
- 'object' => '',
- 'alias' => '',
- 'options' => array(),
- 'fields' => array()
- );
- }
- // Blank out fields and options for AJAX calls (everything should be sent to it for a full overwrite)
- if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
- $pod[ 'fields' ] = array();
- $pod[ 'options' ] = array();
- }
- // Setup options
- $options = get_object_vars( $params );
- if ( isset( $options[ 'method' ] ) )
- unset( $options[ 'method' ] );
- $exclude = array(
- 'id',
- 'name',
- 'label',
- 'description',
- 'type',
- 'storage',
- 'object',
- 'alias',
- 'options',
- 'fields'
- );
- foreach ( $exclude as $k => $exclude_field ) {
- $aliases = array( $exclude_field );
- if ( is_array( $exclude_field ) ) {
- $aliases = array_merge( array( $k ), $exclude_field );
- $exclude_field = $k;
- }
- foreach ( $aliases as $alias ) {
- if ( isset( $options[ $alias ] ) ) {
- $pod[ $exclude_field ] = pods_trim( $options[ $alias ] );
- unset( $options[ $alias ] );
- }
- }
- }
- if ( defined( 'PODS_TABLELESS' ) && PODS_TABLELESS ) {
- if ( 'pod' == $pod[ 'type' ] )
- $pod[ 'type' ] = 'post_type';
- if ( 'table' == $pod[ 'storage' ] ) {
- if ( 'taxonomy' == $pod[ 'type' ] )
- $pod[ 'storage' ] = 'none';
- else
- $pod[ 'storage' ] = 'meta';
- }
- }
- $pod[ 'options' ][ 'type' ] = $pod[ 'type' ];
- $pod[ 'options' ][ 'storage' ] = $pod[ 'storage' ];
- $pod[ 'options' ][ 'object' ] = $pod[ 'object' ];
- $pod[ 'options' ][ 'alias' ] = $pod[ 'alias' ];
- $pod[ 'options' ] = array_merge( $pod[ 'options' ], $options );
- if ( strlen( $pod[ 'label' ] ) < 1 )
- $pod[ 'label' ] = $pod[ 'name' ];
- if ( 'post_type' == $pod[ 'type' ] ) {
- // Max length for post types are 20 characters
- $pod[ 'name' ] = substr( $pod[ 'name' ], 0, 20 );
- }
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
- // Max length for taxonomies are 32 characters
- $pod[ 'name' ] = substr( $pod[ 'name' ], 0, 32 );
- }
- $params->id = $pod[ 'id' ];
- $params->name = $pod[ 'name' ];
- if ( null !== $old_name && $old_name != $params->name && empty( $pod[ 'object' ] ) ) {
- if ( 'post_type' == $pod[ 'type' ] ) {
- $check = get_post_type_object( $params->name );
- if ( !empty( $check ) )
- return pods_error( sprintf( __( 'Post Type %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
- }
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
- $check = get_taxonomy( $params->name );
- if ( !empty( $check ) )
- return pods_error( sprintf( __( 'Taxonomy %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
- }
- }
- $field_table_operation = true;
- // Add new pod
- if ( empty( $params->id ) ) {
- if ( strlen( $params->name ) < 1 )
- return pods_error( __( 'Pod name cannot be empty', 'pods' ), $this );
- $post_data = array(
- 'post_name' => $pod[ 'name' ],
- 'post_title' => $pod[ 'label' ],
- 'post_content' => $pod[ 'description' ],
- 'post_type' => '_pods_pod',
- 'post_status' => 'publish'
- );
- if ( 'pod' == $pod[ 'type' ] && ( !is_array( $pod[ 'fields' ] ) || empty( $pod[ 'fields' ] ) ) ) {
- $pod[ 'fields' ] = array();
- $pod[ 'fields' ][] = array(
- 'name' => 'name',
- 'label' => 'Name',
- 'type' => 'text',
- 'options' => array(
- 'required' => '1'
- )
- );
- $pod[ 'fields' ][] = array(
- 'name' => 'created',
- 'label' => 'Date Created',
- 'type' => 'datetime',
- 'options' => array(
- 'datetime_format' => 'ymd_slash',
- 'datetime_time_type' => '12',
- 'datetime_time_format' => 'h_mm_ss_A'
- )
- );
- $pod[ 'fields' ][] = array(
- 'name' => 'modified',
- 'label' => 'Date Modified',
- 'type' => 'datetime',
- 'options' => array(
- 'datetime_format' => 'ymd_slash',
- 'datetime_time_type' => '12',
- 'datetime_time_format' => 'h_mm_ss_A'
- )
- );
- $pod[ 'fields' ][] = array(
- 'name' => 'author',
- 'label' => 'Author',
- 'type' => 'pick',
- 'pick_object' => 'user',
- 'options' => array(
- 'pick_format_type' => 'single',
- 'pick_format_single' => 'autocomplete',
- 'default_value' => '{@user.ID}'
- )
- );
- $pod[ 'fields' ][] = array(
- 'name' => 'permalink',
- 'label' => 'Permalink',
- 'type' => 'slug',
- 'description' => 'Leave blank to auto-generate from Name'
- );
- if ( !isset( $pod[ 'options' ][ 'pod_index' ] ) )
- $pod[ 'options' ][ 'pod_index' ] = 'name';
- }
- $field_table_operation = false;
- }
- else {
- $post_data = array(
- 'ID' => $pod[ 'id' ],
- 'post_name' => $pod[ 'name' ],
- 'post_title' => $pod[ 'label' ],
- 'post_content' => $pod[ 'description' ],
- 'post_status' => 'publish'
- );
- }
- if ( true === $db ) {
- $params->id = $this->save_post( $post_data, $pod[ 'options' ], true, true );
- if ( false === $params->id )
- return pods_error( __( 'Cannot save Pod', 'pods' ), $this );
- }
- elseif ( empty( $params->id ) )
- $params->id = (int) $db;
- $pod[ 'id' ] = $params->id;
- // Setup / update tables
- if ( 'table' == $pod[ 'storage' ] && $old_storage != $pod[ 'storage' ] && $db ) {
- $definitions = array( "`id` BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY" );
- foreach ( $pod[ 'fields' ] as $field ) {
- if ( !in_array( $field[ 'type' ], $tableless_field_types ) )
- $definitions[] = "`{$field['name']}` " . $this->get_field_definition( $field[ 'type' ], $field[ 'options' ] );
- }
- pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`" );
- $result = pods_query( "CREATE TABLE `@wp_pods_{$params->name}` (" . implode( ', ', $definitions ) . ") DEFAULT CHARSET utf8", $this );
- if ( empty( $result ) )
- return pods_error( __( 'Cannot add Database Table for Pod', 'pods' ), $this );
- }
- elseif ( 'table' == $pod[ 'storage' ] && $pod[ 'storage' ] == $old_storage && null !== $old_name && $old_name != $params->name && $db ) {
- $result = pods_query( "ALTER TABLE `@wp_pods_{$old_name}` RENAME `@wp_pods_{$params->name}`", $this );
- if ( empty( $result ) )
- return pods_error( __( 'Cannot update Database Table for Pod', 'pods' ), $this );
- }
- /**
- * @var $wpdb wpdb
- */
- global $wpdb;
- if ( 'post_type' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) && null !== $old_name && $old_name != $params->name && $db )
- $this->rename_wp_object_type( 'post', $old_name, $params->name );
- elseif ( 'taxonomy' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) && null !== $old_name && $old_name != $params->name && $db )
- $this->rename_wp_object_type( 'taxonomy', $old_name, $params->name );
- elseif ( 'comment' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) && null !== $old_name && $old_name != $params->name && $db )
- $this->rename_wp_object_type( 'comment', $old_name, $params->name );
- // Sync any related fields if the name has changed
- if ( null !== $old_name && $old_name != $params->name && $db ) {
- $fields = pods_query( "
- SELECT `p`.`ID`
- FROM `{$wpdb->posts}` AS `p`
- LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
- LEFT JOIN `{$wpdb->postmeta}` AS `pm2` ON `pm2`.`post_id` = `p`.`ID`
- WHERE
- `p`.`post_type` = '_pods_field'
- AND `pm`.`meta_key` = 'pick_object'
- AND `pm`.`meta_value` = 'pod'
- AND `pm2`.`meta_key` = 'pick_val'
- AND `pm2`.`meta_value` = '{$old_name}'
- " );
- if ( !empty( $fields ) ) {
- foreach ( $fields as $field ) {
- …
Large files files are truncated, but you can click here to view the full file