/classes/Pods.php
PHP | 2144 lines | 1218 code | 424 blank | 502 comment | 346 complexity | 90263c31ee632c475bd9879b94480c5b 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 Pods {
- /**
- * @var PodsAPI
- */
- public $api;
- /**
- * @var PodsData
- */
- public $data;
- /**
- * @var Array of pod item arrays
- */
- public $rows;
- /**
- * @var Current pod item array
- */
- public $row;
- /**
- * @var bool
- */
- public $display_errors = false;
- /**
- * @var array|bool|mixed|null|void
- */
- public $pod_data;
- /**
- * @var array
- */
- public $params = array();
- /**
- * @var
- */
- public $pod;
- /**
- * @var
- */
- public $pod_id;
- /**
- * @var
- */
- public $fields;
- /**
- * @var
- */
- public $detail_page;
- /**
- * @var int
- */
- public $id;
- /**
- * @var int
- */
- public $limit = 15;
- /**
- * @var string
- */
- public $page_var = 'pg';
- /**
- * @var int|mixed
- */
- public $page = 1;
- /**
- * @var bool
- */
- public $pagination = true;
- /**
- * @var bool
- */
- public $search = true;
- /**
- * @var string
- */
- public $search_var = 'search';
- /**
- * @var string
- */
- public $search_mode = 'int'; // int | text | text_like
- /**
- * @var int
- */
- public $total = 0;
- /**
- * @var int
- */
- public $total_found = 0;
- /**
- * @var
- */
- public $ui = array();
- /**
- * @var
- */
- public $deprecated;
- public $datatype;
- public $datatype_id;
- public $page_template;
- public $body_classes;
- public $meta = array();
- public $meta_properties = array();
- public $meta_extra = '';
- public $sql;
- /**
- * Constructor - Pods Framework core
- *
- * @param string $pod The pod name
- * @param mixed $id (optional) The ID or slug, to load a single record; Provide array of $params to run 'find'
- *
- * @license http://www.gnu.org/licenses/gpl-2.0.html
- * @since 1.0.0
- * @link http://podsframework.org/docs/pods/
- */
- public function __construct ( $pod = null, $id = null ) {
- if ( null === $pod ) {
- $pod = get_post_type();
- if ( null === $id )
- $id = get_the_ID();
- }
- $this->api = pods_api( $pod );
- $this->api->display_errors =& $this->display_errors;
- $this->data = pods_data( $this->api, $id, false );
- PodsData::$display_errors =& $this->display_errors;
- // Set up page variable
- if ( defined( 'PODS_STRICT_MODE' ) && PODS_STRICT_MODE ) {
- $this->page = 1;
- $this->pagination = false;
- $this->search = false;
- }
- else {
- // Get the page variable
- $this->page = pods_var( $this->page_var, 'get' );
- $this->page = ( empty( $this->page ) ? 1 : max( pods_absint( $this->page ), 1 ) );
- }
- // Set default pagination handling to on/off
- if ( defined( 'PODS_GLOBAL_POD_PAGINATION' ) ) {
- if ( !PODS_GLOBAL_POD_PAGINATION ) {
- $this->page = 1;
- $this->pagination = false;
- }
- else
- $this->pagination = true;
- }
- // Set default search to on/off
- if ( defined( 'PODS_GLOBAL_POD_SEARCH' ) ) {
- if ( PODS_GLOBAL_POD_SEARCH )
- $this->search = true;
- else
- $this->search = false;
- }
- // Set default search mode
- $allowed_search_modes = array( 'int', 'text', 'text_like' );
- if ( defined( 'PODS_GLOBAL_POD_SEARCH_MODE' ) && in_array( PODS_GLOBAL_POD_SEARCH_MODE, $allowed_search_modes ) )
- $this->search_mode = PODS_GLOBAL_POD_SEARCH_MODE;
- // Sync Settings
- $this->data->page =& $this->page;
- $this->data->limit =& $this->limit;
- $this->data->pagination =& $this->pagination;
- $this->data->search =& $this->search;
- $this->data->search_mode =& $this->search_mode;
- // Sync Pod Data
- $this->api->pod_data =& $this->data->pod_data;
- $this->pod_data =& $this->api->pod_data;
- $this->api->pod_id =& $this->data->pod_id;
- $this->pod_id =& $this->api->pod_id;
- $this->datatype_id =& $this->pod_id;
- $this->api->pod =& $this->data->pod;
- $this->pod =& $this->api->pod;
- $this->datatype =& $this->pod;
- $this->api->fields =& $this->data->fields;
- $this->fields =& $this->api->fields;
- $this->detail_page =& $this->data->detail_page;
- $this->id =& $this->data->id;
- $this->row =& $this->data->row;
- $this->rows =& $this->data->data;
- if ( is_array( $id ) || is_object( $id ) )
- $this->find( $id );
- }
- /**
- * Whether this Pod object is valid or not
- *
- * @return bool
- *
- * @since 2.0.0
- */
- public function valid () {
- if ( empty( $this->pod_id ) )
- return false;
- return true;
- }
- /**
- * Whether a Pod item exists or not when using fetch() or construct with an ID or slug
- *
- * @return bool
- *
- * @since 2.0.0
- */
- public function exists () {
- if ( empty( $this->row ) )
- return false;
- return true;
- }
- /**
- * Return an array of all rows returned from a find() call.
- *
- * Most of the time, you will want to loop through data using fetch()
- * instead of using this function.
- *
- * @return array|bool An array of all rows returned from a find() call, or false if no items returned
- *
- * @since 2.0.0
- * @link http://podsframework.org/docs/data/
- */
- public function data () {
- $this->do_hook( 'data' );
- if ( empty( $this->rows ) )
- return false;
- return (array) $this->rows;
- }
- /**
- * Return field array from a Pod
- *
- * @return array
- *
- * @since 2.0.0
- */
- public function fields () {
- $this->do_hook( 'fields' );
- if ( empty( $this->fields ) )
- return false;
- return (array) $this->fields;
- }
- /**
- * Return row array for an item
- *
- * @return array
- *
- * @since 2.0.0
- */
- public function row () {
- $this->do_hook( 'row' );
- if ( empty( $this->row ) )
- return false;
- return (array) $this->row;
- }
- /**
- * Return the output for a field. If you want the raw value for use in PHP for custom manipulation,
- * you will want to use field() instead. This function will automatically convert arrays into a
- * list of text such as "Rick, John, and Gary"
- *
- * @param string|array $name The field name, or an associative array of parameters
- * @param boolean $single (optional) For tableless fields, to return an array or the first
- *
- * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned for tableless fields
- * @since 2.0.0
- * @link http://podsframework.org/docs/display/
- */
- public function display ( $name, $single = null ) {
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
- $defaults = array(
- 'name' => $name,
- 'orderby' => null,
- 'single' => $single,
- 'args' => array(),
- 'in_form' => false
- );
- if ( is_array( $name ) || is_object( $name ) )
- $params = (object) array_merge( $defaults, (array) $name );
- else
- $params = (object) $defaults;
- if ( is_array( $params->single ) ) {
- $params->args = $params->single;
- $params->single = null;
- }
- $value = $this->field( $params );
- if ( false === $params->in_form && isset( $this->fields[ $params->name ] ) ) {
- if ( 'pick' == $this->fields[ $params->name ][ 'type' ] && in_array( $this->fields[ $params->name ][ 'pick_object' ], $simple_tableless_objects ) )
- $value = PodsForm::field_method( 'pick', 'simple_value', $value, $this->fields[ $params->name ] );
- if ( 0 < strlen( pods_var( 'display_filter', $this->fields[ $params->name ] ) ) )
- $value = apply_filters( pods_var( 'display_filter', $this->fields[ $params->name ] ), $value );
- else {
- $value = PodsForm::display(
- $this->fields[ $params->name ][ 'type' ],
- $value,
- $params->name,
- array_merge( $this->fields[ $params->name ][ 'options' ], $this->fields[ $params->name ] ),
- $this->pod_data,
- $this->id(),
- $params
- );
- }
- }
- if ( is_array( $value ) )
- $value = pods_serial_comma( $value, $params->name, $this->fields );
- return $value;
- }
- /**
- * Return the raw output for a field If you want the raw value for use in PHP for custom manipulation,
- * you will want to use field() instead. This function will automatically convert arrays into a
- * list of text such as "Rick, John, and Gary"
- *
- * @param string|array $name The field name, or an associative array of parameters
- * @param boolean $single (optional) For tableless fields, to return an array or the first
- *
- * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned for tableless fields
- * @since 2.0.0
- * @link http://podsframework.org/docs/display/
- */
- public function raw ( $name, $single = null ) {
- $defaults = array(
- 'name' => $name,
- 'orderby' => null,
- 'single' => $single,
- 'in_form' => false,
- 'raw' => true
- );
- if ( is_array( $name ) || is_object( $name ) )
- $params = (object) array_merge( $defaults, (array) $name );
- else
- $params = (object) $defaults;
- $value = $this->field( $params );
- return $value;
- }
- /**
- * Return the value for a field.
- *
- * If you are getting a field for output in a theme, most of the time you will want to use display() instead.
- *
- * This function will return arrays for relationship and file fields.
- *
- * @param string|array $name The field name, or an associative array of parameters
- * @param boolean $single (optional) For tableless fields, to return the whole array or the just the first item
- * @param boolean $raw (optional) Whether to return the raw value, or to run through the field type's display method
- *
- * @return mixed|null Value returned depends on the field type, null if the field doesn't exist, false if no value returned for tableless fields
- * @since 2.0.0
- * @link http://podsframework.org/docs/field/
- */
- public function field ( $name, $single = null, $raw = false ) {
- $defaults = array(
- 'name' => $name,
- 'orderby' => null,
- 'single' => $single,
- 'in_form' => false,
- 'raw' => $raw,
- 'deprecated' => false
- );
- if ( is_array( $name ) || is_object( $name ) )
- $params = (object) array_merge( $defaults, (array) $name );
- else
- $params = (object) $defaults;
- // Support old $orderby variable
- if ( null !== $params->single && !is_bool( $params->single ) && empty( $params->orderby ) ) {
- pods_deprecated( 'Pods::field', '2.0.0', 'Use $params[ \'orderby\' ] instead' );
- $params->orderby = $params->single;
- $params->single = false;
- }
- if ( null !== $params->single )
- $params->single = (boolean) $params->single;
- $single = $params->single;
- if ( is_array( $params->name ) || strlen( $params->name ) < 1 )
- return null;
- if ( false === $this->row() ) {
- if ( false !== $this->data() )
- $this->fetch();
- else
- return null;
- }
- if ( $this->data->field_id == $params->name ) {
- if ( isset( $this->row[ $params->name ] ) )
- return $this->row[ $params->name ];
- return 0;
- }
- $value = null;
- $tableless_field_types = apply_filters( 'pods_tableless_field_types', array( 'pick', 'file', 'avatar', 'taxonomy' ) );
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
- $params->traverse = array();
- if ( 'detail_url' == $params->name || ( in_array( $params->name, array( 'permalink', 'the_permalink' ) ) && in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) ) {
- if ( 0 < strlen( $this->detail_page ) )
- $value = get_bloginfo( 'url' ) . '/' . $this->do_magic_tags( $this->detail_page );
- elseif ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) )
- $value = get_permalink( $this->id() );
- elseif ( 'taxonomy' == $this->pod_data[ 'type' ] )
- $value = get_term_link( $this->id(), $this->pod_data[ 'name' ] );
- elseif ( 'user' == $this->pod_data[ 'type' ] )
- $value = get_author_posts_url( $this->id() );
- elseif ( 'comment' == $this->pod_data[ 'type' ] )
- $value = get_comment_link( $this->id() );
- }
- if ( empty( $value ) && isset( $this->row[ $params->name ] ) ) {
- if ( !isset( $this->fields[ $params->name ] ) || in_array( $this->fields[ $params->name ][ 'type' ], array( 'boolean', 'number', 'currency' ) ) || in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) )
- $params->raw = true;
- $value = $this->row[ $params->name ];
- if ( !is_array( $value ) && isset( $this->fields[ $params->name ] ) && 'pick' == $this->fields[ $params->name ][ 'type' ] && in_array( $this->fields[ $params->name ][ 'pick_object' ], $simple_tableless_objects ) )
- $value = PodsForm::field_method( 'pick', 'simple_value', $value, $this->fields[ $params->name ], true );
- }
- elseif ( empty( $value ) ) {
- $object_field_found = false;
- $first_field = explode( '.', $params->name );
- $first_field = $first_field[ 0 ];
- // @todo Handle Author WP object fields like they are pick fields
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
- if ( $object_field == $first_field || in_array( $first_field, $object_field_opt[ 'alias' ] ) ) {
- if ( isset( $this->row[ $object_field ] ) ) {
- $value = $this->row[ $object_field ];
- $object_field_found = true;
- }
- elseif ( in_array( $object_field_opt[ 'type' ], $tableless_field_types ) )
- $this->fields[ $object_field ] = $object_field_opt;
- else
- return null;
- }
- }
- if ( 'post_type' == $this->pod_data[ 'type' ] ) {
- if ( 'post_thumbnail' == $params->name || 0 === strpos( $params->name, 'post_thumbnail.' ) ) {
- $size = 'thumbnail';
- if ( 0 === strpos( $params->name, 'post_thumbnail.' ) ) {
- $field_names = explode( '.', $params->name );
- if ( isset( $field_names[ 1 ] ) )
- $size = $field_names[ 1 ];
- }
- // Pods will auto-get the thumbnail ID if this isn't an attachment
- $value = pods_image( $this->id(), $size );
- $object_field_found = true;
- }
- elseif ( 'post_thumbnail_url' == $params->name || 0 === strpos( $params->name, 'post_thumbnail_url.' ) ) {
- $size = 'thumbnail';
- if ( 0 === strpos( $params->name, 'post_thumbnail_url.' ) ) {
- $field_names = explode( '.', $params->name );
- if ( isset( $field_names[ 1 ] ) )
- $size = $field_names[ 1 ];
- }
- // Pods will auto-get the thumbnail ID if this isn't an attachment
- $value = pods_image_url( $this->id(), $size );
- $object_field_found = true;
- }
- }
- elseif ( 'image_attachment' == $params->name || 0 === strpos( $params->name, 'image_attachment.' ) ) {
- $size = 'thumbnail';
- $image_id = 0;
- if ( 0 === strpos( $params->name, 'image_attachment.' ) ) {
- $field_names = explode( '.', $params->name );
- if ( isset( $field_names[ 1 ] ) )
- $image_id = $field_names[ 1 ];
- if ( isset( $field_names[ 2 ] ) )
- $size = $field_names[ 2 ];
- }
- if ( !empty( $image_id ) )
- $value = pods_image_url( $image_id, $size );
- $object_field_found = true;
- }
- elseif ( 'image_attachment_url' == $params->name || 0 === strpos( $params->name, 'image_attachment_url.' ) ) {
- $size = 'thumbnail';
- $image_id = 0;
- if ( 0 === strpos( $params->name, 'image_attachment_url.' ) ) {
- $field_names = explode( '.', $params->name );
- if ( isset( $field_names[ 1 ] ) )
- $image_id = $field_names[ 1 ];
- if ( isset( $field_names[ 2 ] ) )
- $size = $field_names[ 2 ];
- }
- if ( !empty( $image_id ) )
- $value = pods_image_url( $image_id, $size );
- $object_field_found = true;
- }
- if ( false === $object_field_found ) {
- $params->traverse = array( $params->name );
- if ( false !== strpos( $params->name, '.' ) ) {
- $params->traverse = explode( '.', $params->name );
- $params->name = $params->traverse[ 0 ];
- }
- if ( isset( $this->fields[ $params->name ] ) && isset( $this->fields[ $params->name ][ 'type' ] ) ) {
- $v = $this->do_hook( 'field_' . $this->fields[ $params->name ][ 'type' ], null, $this->fields[ $params->name ], $this->row, $params );
- if ( null !== $v )
- return $v;
- }
- $simple = false;
- $simple_data = array();
- if ( isset( $this->fields[ $params->name ] ) ) {
- if ( 'meta' == $this->pod_data[ 'storage' ] ) {
- if ( !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) )
- $simple = true;
- }
- if ( in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) ) {
- $params->raw = true;
- if ( in_array( $this->fields[ $params->name ][ 'pick_object' ], $simple_tableless_objects ) ) {
- $simple = true;
- $params->single = true;
- }
- }
- elseif ( in_array( $this->fields[ $params->name ][ 'type' ], array( 'boolean', 'number', 'currency' ) ) )
- $params->raw = true;
- }
- if ( !isset( $this->fields[ $params->name ] ) || !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) || $simple ) {
- if ( null === $params->single ) {
- if ( isset( $this->fields[ $params->name ] ) && !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) )
- $params->single = true;
- else
- $params->single = false;
- }
- $no_conflict = pods_no_conflict_check( $this->pod_data[ 'type' ] );
- if ( !$no_conflict )
- pods_no_conflict_on( $this->pod_data[ 'type' ] );
- if ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) {
- $id = $this->id();
- if ( function_exists( 'icl_get_languages' ) ) {
- $master_post_id = (int) get_post_meta( $id, '_icl_lang_duplicate_of', true );
- if ( 0 < $master_post_id )
- $id = $master_post_id;
- }
- $value = get_post_meta( $id, $params->name, $params->single );
- }
- elseif ( 'user' == $this->pod_data[ 'type' ] )
- $value = get_user_meta( $this->id(), $params->name, $params->single );
- elseif ( 'comment' == $this->pod_data[ 'type' ] )
- $value = get_comment_meta( $this->id(), $params->name, $params->single );
- // Handle Simple Relationships
- if ( $simple ) {
- if ( null === $single )
- $params->single = false;
- $value = PodsForm::field_method( 'pick', 'simple_value', $value, $this->fields[ $params->name ], true );
- }
- if ( !$no_conflict )
- pods_no_conflict_off( $this->pod_data[ 'type' ] );
- }
- else {
- // Dot-traversal
- $pod = $this->pod;
- $ids = array( $this->id() );
- $all_fields = array(
- $this->pod => $this->fields
- );
- $lookup = $params->traverse;
- if ( !empty( $lookup ) )
- unset( $lookup[ 0 ] );
- // Get fields matching traversal names
- if ( !empty( $lookup ) ) {
- $fields = $this->api->load_fields( array(
- 'name' => $lookup,
- 'type' => $tableless_field_types
- ) );
- if ( !empty( $fields ) ) {
- foreach ( $fields as $field ) {
- if ( !empty( $field ) ) {
- if ( !isset( $all_fields[ $field[ 'pod' ] ] ) )
- $all_fields[ $field[ 'pod' ] ] = array();
- $all_fields[ $field[ 'pod' ] ][ $field[ 'name' ] ] = $field;
- }
- }
- }
- }
- $last_type = $last_object = $last_pick_val = '';
- $single_multi = pods_var( $this->fields[ $params->name ][ 'type' ] . '_format_type', $this->fields[ $params->name ][ 'options' ], 'single' );
- if ( 'multi' == $single_multi )
- $limit = (int) pods_var( $this->fields[ $params->name ][ 'type' ] . '_limit', $this->fields[ $params->name ][ 'options' ], 0 );
- else
- $limit = 1;
- $last_limit = 0;
- // Loop through each traversal level
- foreach ( $params->traverse as $key => $field ) {
- $last_loop = false;
- if ( count( $params->traverse ) <= ( $key + 1 ) )
- $last_loop = true;
- $field_exists = isset( $all_fields[ $pod ][ $field ] );
- $simple = false;
- $simple_options = array();
- if ( $field_exists && 'pick' == $all_fields[ $pod ][ $field ][ 'type' ] && in_array( $all_fields[ $pod ][ $field ][ 'pick_object' ], $simple_tableless_objects ) ) {
- $simple = true;
- $simple_options = $all_fields[ $pod ][ $field ];
- }
- // Tableless handler
- if ( $field_exists && ( 'pick' != $all_fields[ $pod ][ $field ][ 'type' ] || !$simple ) ) {
- $type = $all_fields[ $pod ][ $field ][ 'type' ];
- $pick_object = $all_fields[ $pod ][ $field ][ 'pick_object' ];
- $pick_val = $all_fields[ $pod ][ $field ][ 'pick_val' ];
- $last_limit = 0;
- if ( in_array( $type, $tableless_field_types ) ) {
- $single_multi = pods_var( "{$type}_format_type", $all_fields[ $pod ][ $field ][ 'options' ], 'single' );
- if ( 'multi' == $single_multi )
- $last_limit = (int) pods_var( "{$type}_limit", $all_fields[ $pod ][ $field ][ 'options' ], 0 );
- else
- $last_limit = 1;
- }
- $last_type = $type;
- $last_object = $pick_object;
- $last_pick_val = $pick_val;
- $last_options = $all_fields[ $pod ][ $field ];
- // Get related IDs
- $ids = $this->api->lookup_related_items(
- $all_fields[ $pod ][ $field ][ 'id' ],
- $all_fields[ $pod ][ $field ][ 'pod_id' ],
- $ids,
- $all_fields[ $pod ][ $field ]
- );
- // No items found
- if ( empty( $ids ) )
- return false;
- elseif ( 0 < $last_limit )
- $ids = array_slice( $ids, 0, $last_limit );
- // Get $pod if related to a Pod
- if ( !empty( $pick_object ) && !empty( $pick_val ) ) {
- if ( 'pod' == $pick_object )
- $pod = $pick_val;
- else {
- $check = $this->api->get_table_info( $pick_object, $pick_val );
- if ( !empty( $check ) && !empty( $check[ 'pod' ] ) )
- $pod = $check[ 'pod' ][ 'name' ];
- }
- }
- }
- // Assume last iteration
- else {
- // Invalid field
- if ( 0 == $key )
- return false;
- $last_loop = true;
- }
- if ( $last_loop ) {
- $object_type = $last_object;
- $object = $last_pick_val;
- if ( in_array( $last_type, apply_filters( 'pods_file_field_types', array( 'file', 'avatar' ) ) ) ) {
- $object_type = 'media';
- $object = 'attachment';
- }
- $data = array();
- $table = $this->api->get_table_info( $object_type, $object );
- $join = $where = array();
- if ( !empty( $table[ 'join' ] ) )
- $join = (array) $table[ 'join' ];
- if ( !empty( $table[ 'where' ] ) || !empty( $ids ) ) {
- foreach ( $ids as $id ) {
- $where[ $id ] = '`t`.`' . $table[ 'field_id' ] . '` = ' . (int) $id;
- }
- if ( !empty( $where ) )
- $where = array( '( ' . implode( ' OR ', $where ) . ' )' );
- if ( !empty( $table[ 'where' ] ) )
- $where = array_merge( $where, array_values( (array) $table[ 'where' ] ) );
- }
- if ( !empty( $table[ 'table' ] ) ) {
- $sql = array(
- 'select' => '*, `t`.`' . $table[ 'field_id' ] . '` AS `pod_item_id`',
- 'table' => $table[ 'table' ],
- 'join' => $join,
- 'where' => $where,
- 'orderby' => $params->orderby
- );
- $item_data = pods_data()->select( $sql );
- $items = array();
- foreach ( $item_data as $item ) {
- if ( empty( $item->pod_item_id ) )
- continue;
- // Get Item ID
- $item_id = $item->pod_item_id;
- // Cleanup
- unset( $item->pod_item_id );
- // Pass item data into $data
- $items[ $item_id ] = $item;
- }
- // Cleanup
- unset( $item_data );
- // Return all of the data in the order expected
- if ( empty( $params->orderby ) ) {
- foreach ( $ids as $id ) {
- if ( isset( $items[ $id ] ) )
- $data[ $id ] = $items[ $id ];
- }
- }
- }
- if ( in_array( $last_type, $tableless_field_types ) || in_array( $last_type, array( 'boolean', 'number', 'currency' ) ) )
- $params->raw = true;
- if ( empty( $data ) )
- $value = false;
- else {
- foreach ( $data as &$item_value ) {
- $item_value = get_object_vars( (object) $item_value );
- }
- $object_type = $table[ 'type' ];
- if ( in_array( $table[ 'type' ], array( 'post_type', 'attachment' ) ) )
- $object_type = 'post';
- if ( in_array( $object_type, array( 'post', 'user', 'comment' ) ) ) {
- $no_conflict = pods_no_conflict_check( $object_type );
- if ( !$no_conflict )
- pods_no_conflict_on( $object_type );
- }
- // Return entire array
- if ( false === $params->in_form && false !== $field_exists && in_array( $last_type, $tableless_field_types ) )
- $value = $data;
- // Return an array of single column values
- else {
- $value = array();
- if ( false !== $params->in_form )
- $field = $table[ 'field_id' ];
- foreach ( $data as $item_id => $item ) {
- if ( isset( $item[ $field ] ) )
- $value[] = $item[ $field ];
- elseif ( in_array( $object_type, array( 'post', 'user', 'comment' ) ) )
- $value[] = get_metadata( $object_type, $item_id, $field, true );
- }
- }
- if ( in_array( $object_type, array( 'post', 'user', 'comment' ) ) && !$no_conflict )
- pods_no_conflict_off( $object_type );
- // Handle Simple Relationships
- if ( $simple ) {
- if ( null === $single )
- $params->single = false;
- $value = PodsForm::field_method( 'pick', 'simple_value', $value, $simple_options, true );
- }
- // Return a single column value
- if ( false === $params->in_form && 1 == $limit && !empty( $value ) && is_array( $value ) && isset( $value[ 0 ] ) )
- $value = $value[ 0 ];
- }
- break;
- }
- }
- }
- }
- }
- if ( !empty( $params->traverse ) && 1 < count( $params->traverse ) ) {
- $field_names = implode( '.', $params->traverse );
- $this->row[ $field_names ] = $value;
- }
- else
- $this->row[ $params->name ] = $value;
- if ( true === $params->single && is_array( $value ) && isset( $value[ 0 ] ) )
- $value = $value[ 0 ];
- // @todo Expand this into traversed fields too
- if ( false === $params->raw && false === $params->in_form && isset( $this->fields[ $params->name ] ) ) {
- if ( 0 < strlen( pods_var( 'display_filter', $this->fields[ $params->name ] ) ) )
- $value = apply_filters( pods_var( 'display_filter', $this->fields[ $params->name ] ), $value );
- else {
- $value = PodsForm::display(
- $this->fields[ $params->name ][ 'type' ],
- $value,
- $params->name,
- array_merge( $this->fields[ $params->name ][ 'options' ], $this->fields[ $params->name ] ),
- $this->pod_data,
- $this->id()
- );
- }
- }
- $value = $this->do_hook( 'field', $value, $this->row, $params );
- return $value;
- }
- /**
- * Return the item ID
- *
- * @return int
- * @since 2.0.0
- */
- public function id () {
- return $this->field( $this->data->field_id );
- }
- /**
- * Return the previous item ID, loops at the last id to return the first
- *
- * @param int $id
- * @param array $params_override
- *
- * @return int
- * @since 2.0.0
- */
- public function prev_id ( $id = null, $params_override = null ) {
- if ( null === $id )
- $id = $this->field( 'id' );
- $id = (int) $id;
- $params = array(
- 'select' => "`t`.{$this->data->field_id}`",
- 'where' => "`t`.{$this->data->field_id}` < {$id}",
- 'orderby' => "`t`.{$this->data->field_id}` DESC",
- 'limit' => 1
- );
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
- if ( !empty( $params_override ) )
- $params = $params_override;
- elseif ( !empty( $this->params ) )
- $params = $this->params;
- if ( 0 < $id )
- $params[ 'where' ] = "`t`.{$this->data->field_id}` < {$id}";
- elseif ( isset( $params[ 'offset' ] ) && 0 < $params[ 'offset' ] )
- $params[ 'offset' ] -= 1;
- elseif ( !isset( $params[ 'offset' ] ) && !empty( $this->params ) && 0 < $this->row )
- $params[ 'offset' ] = $this->row - 1;
- else
- return 0;
- $params[ 'select' ] = "`t`.{$this->data->field_id}`";
- $params[ 'limit' ] = 1;
- }
- $pod = pods( $this->pod, $params );
- if ( $pod->fetch() )
- return $pod->id();
- return 0;
- }
- /**
- * Return the next item ID, loops at the first id to return the last
- *
- * @param int $id
- * @param array $find_params
- *
- * @return int
- * @since 2.0.0
- */
- public function next_id ( $id = null, $params_override = null ) {
- if ( null === $id )
- $id = $this->field( 'id' );
- $id = (int) $id;
- $params = array(
- 'select' => "`t`.{$this->data->field_id}`",
- 'where' => "{$id} < `t`.{$this->data->field_id}`",
- 'orderby' => "`t`.{$this->data->field_id}` ASC",
- 'limit' => 1
- );
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
- if ( !empty( $params_override ) )
- $params = $params_override;
- elseif ( !empty( $this->params ) )
- $params = $this->params;
- if ( 0 < $id )
- $params[ 'where' ] = "{$id} < `t`.{$this->data->field_id}`";
- elseif ( !isset( $params[ 'offset' ] ) ) {
- if ( !empty( $this->params ) && -1 < $this->row )
- $params[ 'offset' ] = $this->row + 1;
- else
- $params[ 'offset' ] = 1;
- }
- else
- $params[ 'offset' ] += 1;
- $params[ 'select' ] = "`t`.{$this->data->field_id}`";
- $params[ 'limit' ] = 1;
- }
- $pod = pods( $this->pod, $params );
- if ( $pod->fetch() )
- return $pod->id();
- return 0;
- }
- /**
- * Return the first item ID
- *
- * @param array $params_override
- *
- * @return int
- * @since 2.3.0
- */
- public function first_id ( $params_override = null ) {
- $params = array(
- 'select' => "`t`.{$this->data->field_id}`",
- 'orderby' => "`t`.{$this->data->field_id}` ASC",
- 'limit' => 1
- );
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
- if ( !empty( $params_override ) )
- $params = $params_override;
- elseif ( !empty( $this->params ) )
- $params = $this->params;
- $params[ 'select' ] = "`t`.{$this->data->field_id}`";
- $params[ 'offset' ] = 0;
- $params[ 'limit' ] = 1;
- }
- $pod = pods( $this->pod, $params );
- if ( $pod->fetch() )
- return $pod->id();
- return 0;
- }
- /**
- * Return the last item ID
- *
- * @param array $params_override
- *
- * @return int
- * @since 2.3.0
- */
- public function last_id ( $params_override = null ) {
- $params = array(
- 'select' => "`t`.{$this->data->field_id}`",
- 'orderby' => "`t`.{$this->data->field_id}` DESC",
- 'limit' => 1
- );
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
- if ( !empty( $params_override ) )
- $params = $params_override;
- elseif ( !empty( $this->params ) )
- $params = $this->params;
- if ( isset( $params[ 'total_found' ] ) )
- $params[ 'offset' ] = $params[ 'total_found' ] - 1;
- else
- $params[ 'offset' ] = $this->total_found() - 1;
- $params[ 'select' ] = "`t`.{$this->data->field_id}`";
- $params[ 'limit' ] = 1;
- }
- $pod = pods( $this->pod, $params );
- if ( $pod->fetch() )
- return $pod->id();
- return 0;
- }
- /**
- * Return the item name
- *
- * @return string
- * @since 2.0.0
- */
- public function index () {
- return $this->field( $this->data->field_index );
- }
- /**
- * Find items of a pod, much like WP_Query, but with advanced table handling.
- *
- * @param array $params An associative array of parameters
- * @param int $limit (optional) (deprecated) Limit the number of items to find, use -1 to return all items with no limit
- * @param string $where (optional) (deprecated) SQL WHERE declaration to use
- * @param string $sql (optional) (deprecated) For advanced use, a custom SQL query to run
- *
- * @return \Pods The pod object
- * @since 2.0.0
- * @link http://podsframework.org/docs/find/
- */
- public function find ( $params = null, $limit = 15, $where = null, $sql = null ) {
- $tableless_field_types = apply_filters( 'pods_tableless_field_types', array( 'pick', 'file', 'avatar', 'taxonomy' ) );
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
- $select = '`t`.*';
- $pod_table_prefix = 't';
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) && 'table' == $this->pod_data[ 'storage' ] ) {
- $select .= ', `d`.*';
- $pod_table_prefix = 'd';
- }
- if ( empty( $this->data->table ) )
- return $this;
- $defaults = array(
- 'table' => $this->data->table,
- 'select' => $select,
- 'join' => null,
- 'where' => $where,
- 'groupby' => null,
- 'having' => null,
- 'orderby' => null,
- 'limit' => (int) $limit,
- 'offset' => null,
- 'page' => (int) $this->page,
- 'page_var' => $this->page_var,
- 'pagination' => (boolean) $this->pagination,
- 'search' => (boolean) $this->search,
- 'search_var' => $this->search_var,
- 'search_query' => null,
- 'search_mode' => $this->search_mode,
- 'search_across' => false,
- 'search_across_picks' => false,
- 'search_across_files' => false,
- 'fields' => $this->fields,
- 'sql' => $sql,
- 'expires' => null,
- 'cache_mode' => 'cache'
- );
- if ( is_array( $params ) )
- $params = (object) array_merge( $defaults, $params );
- if ( is_object( $params ) )
- $params = (object) array_merge( $defaults, get_object_vars( $params ) );
- else {
- $defaults[ 'orderby' ] = $params;
- $params = (object) $defaults;
- }
- $params = $this->do_hook( 'find', $params );
- $params->limit = (int) $params->limit;
- if ( 0 == $params->limit )
- $params->limit = -1;
- $this->limit = $params->limit;
- $this->page = (int) $params->page;
- $this->page_var = $params->page_var;
- $this->pagination = (boolean) $params->pagination;
- $this->search = (boolean) $params->search;
- $this->search_var = $params->search_var;
- $params->join = (array) $params->join;
- if ( empty( $params->search_query ) )
- $params->search_query = pods_var( $this->search_var, 'get', '' );
- // Allow orderby array ( 'field' => 'asc|desc' )
- if ( !empty( $params->orderby ) && is_array( $params->orderby ) ) {
- foreach ( $params->orderby as $k => &$orderby ) {
- if ( !is_numeric( $k ) ) {
- $key = '';
- $order = 'ASC';
- if ( 'DESC' == strtoupper( $orderby ) )
- $order = 'DESC';
- if ( isset( $this->fields[ $k ] ) && in_array( $this->fields[ $k ][ 'type' ], $tableless_field_types ) ) {
- if ( in_array( $this->fields[ $k ][ 'pick_object' ], $simple_tableless_objects ) ) {
- if ( 'table' == $this->pod_data[ 'storage' ] )
- $key = "`t`.`{$k}`";
- else
- $key = "`{$k}`.`meta_value`";
- }
- else {
- $table = $this->api->get_table_info( $this->fields[ $k ][ 'pick_object' ], $this->fields[ $k ][ 'pick_val' ] );
- if ( !empty( $table ) )
- $key = "`{$k}`.`" . $table[ 'field_index' ] . '`';
- }
- }
- if ( empty( $key ) ) {
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) ) {
- if ( isset( $this->pod_data[ 'object_fields' ][ $k ] ) )
- $key = "`t`.`{$k}`";
- elseif ( isset( $this->fields[ $k ] ) ) {
- if ( 'table' == $this->pod_data[ 'storage' ] )
- $key = "`d`.`{$k}`";
- else
- $key = "`{$k}`.`meta_value`";
- }
- else {
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
- if ( $object_field == $k || in_array( $k, $object_field_opt[ 'alias' ] ) )
- $key = "`t`.`{$object_field}`";
- }
- }
- }
- elseif ( isset( $this->fields[ $k ] ) ) {
- if ( 'table' == $this->pod_data[ 'storage' ] )
- $key = "`t`.`{$k}`";
- else
- $key = "`{$k}`.`meta_value`";
- }
- if ( empty( $key ) ) {
- $key = $k;
- if ( false === strpos( $key, ' ' ) && false === strpos( $key, '`' ) )
- $key = '`' . str_replace( '.', '`.`', $key ) . '`';
- }
- }
- $orderby = $key;
- if ( false === strpos( $orderby, ' ' ) )
- $orderby .= ' ' . $order;
- }
- }
- }
- // Add prefix to $params->orderby if needed
- if ( !empty( $params->orderby ) ) {
- if ( !is_array( $params->orderby ) )
- $params->orderby = array( $params->orderby );
- foreach ( $params->orderby as &$prefix_orderby ) {
- if ( false === strpos( $prefix_orderby, ',' ) && false === strpos( $prefix_orderby, '(' ) && false === stripos( $prefix_orderby, ' AS ' ) && false === strpos( $prefix_orderby, '`' ) && false === strpos( $prefix_orderby, '.' ) ) {
- if ( false !== stripos( $prefix_orderby, ' ASC' ) ) {
- $k = trim( str_ireplace( array( '`', ' ASC' ), '', $prefix_orderby ) );
- $dir = 'ASC';
- }
- else {
- $k = trim( str_ireplace( array( '`', ' DESC' ), '', $prefix_orderby ) );
- $dir = 'DESC';
- }
- $key = $k;
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) ) {
- if ( isset( $this->pod_data[ 'object_fields' ][ $k ] ) )
- $key = "`t`.`{$k}`";
- elseif ( isset( $this->fields[ $k ] ) ) {
- if ( 'table' == $this->pod_data[ 'storage' ] )
- $key = "`d`.`{$k}`";
- else
- $key = "`{$k}`.`meta_value`";
- }
- else {
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
- if ( $object_field == $k || in_array( $k, $object_field_opt[ 'alias' ] ) )
- $key = "`t`.`{$object_field}`";
- }
- }
- }
- elseif ( isset( $this->fields[ $k ] ) ) {
- if ( 'table' == $this->pod_data[ 'storage' ] )
- $key = "`t`.`{$k}`";
- else
- $key = "`{$k}`.`meta_value`";
- }
- $prefix_orderby = "{$key} {$dir}";
- }
- }
- }
- $this->params = $params;
- $this->data->select( $params );
- $this->sql = $this->data->sql;
- return $this;
- }
- /**
- * Fetch an item from a Pod. If $id is null, it will return the next item in the list after running find().
- * You can rewind the list back to the start by using reset().
- *
- * Providing an $id will fetch a specific item from a Pod, much like a call to pods(), and can handle either an id or slug.
- *
- * @see PodsData::fetch
- *
- * @param int $id ID or slug of the item to fetch
- *
- * @return array An array of fields from the row
- *
- * @since 2.0.0
- * @link http://podsframework.org/docs/fetch/
- */
- public function fetch ( $id = null ) {
- $this->do_hook( 'fetch', $id );
- if ( !empty( $id ) )
- $this->params = array();
- $this->data->fetch( $id );
- $this->sql = $this->data->sql;
- return $this->row;
- }
- /**
- * (Re)set the MySQL result pointer
- *
- * @see PodsData::reset
- *
- * @param int $row ID of the row to reset to
- *
- * @retur…
Large files files are truncated, but you can click here to view the full file