<?php
/**
* Listing items data manager
*
* @package package_name
* @author Cherry Team
* @license GPL-2.0+
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Jet_Engine_Listings_Data' ) ) {
/**
* Define Jet_Engine_Listings_Data class
*/
class Jet_Engine_Listings_Data {
/**
* Current listing object
*
* @var object
*/
private $current_object = null;
/**
* Current listing document
*
* @var array
*/
private $current_listing = false;
/**
* <ain listing document for current page
* @var null
*/
private $main_listing = null;
/**
* Default main object holder
*
* @var mixed
*/
private $default_object = null;
/**
* Default user object holder
*
* @var WP_User
*/
private $current_user = null;
/**
* Processed post object
*
* @var WP_Post
*/
private $current_post = null;
/**
* Processed term object
*
* @var WP_Term
*/
private $current_term = null;
/**
* Listing settings defaults
* @var array
*/
private $defaults = false;
/**
* Repeater iteration index
*
* @var integer
*/
public $repeater_index = 0;
public $user_fields = array();
/**
* Class constructor
*/
public function __construct() {
add_action( 'the_post', array( $this, 'maybe_set_current_object' ), 10, 2 );
}
/**
* Automatically setup current object for post loops started inside the page
*
* @param [type] $post [description]
* @param [type] $query [description]
* @return [type] [description]
*/
public function maybe_set_current_object( $post = false, $query = false ) {
if ( ! $post ) {
return;
}
if ( ! $query ) {
$this->set_current_object( $post );
return;
}
if ( $query->is_main_query() && $query->in_the_loop ) { // $query->in_the_loop is needed to correctly check $query->is_main_query()
$current_object = $this->get_current_object();
if ( $current_object && 'WP_Post' === get_class( $current_object ) ) {
$this->reset_current_object();
}
} else {
$this->set_current_object( $post );
}
}
/**
* Set current listing from outside
*
* @param void
*/
public function set_listing( $listing_doc = null ) {
if ( ! $listing_doc ) {
return;
}
if ( $listing_doc->get_settings( 'is_main' ) ) {
$this->main_listing = $listing_doc;
}
$this->current_listing = $listing_doc;
}
/**
* Reset current listing object
*
* @return void
*/
public function reset_listing() {
$this->current_listing = $this->main_listing;
$this->reset_current_object();
}
/**
* Returns current listing object
*
* @return [type] [description]
*/
public function get_listing() {
if ( ! $this->current_listing ) {
$default_settings = $this->setup_default_listing();
$this->current_listing = jet_engine()->listings->get_new_doc( $default_settings );
}
return $this->current_listing;
}
/**
* Retuns current object fields array
* @return [type] [description]
*/
public function get_object_fields( $where = 'elementor' ) {
switch ( $this->get_listing_source() ) {
case 'posts':
$fields = array(
'post_id' => __( 'Post ID', 'jet-engine' ),
'post_title' => __( 'Title', 'jet-engine' ),
'post_date' => __( 'Date', 'jet-engine' ),
'post_content' => __( 'Content', 'jet-engine' ),
'post_excerpt' => __( 'Excerpt', 'jet-engine' ),
'post_status' => __( 'Post Status', 'jet-engine' ),
);
break;
case 'terms':
$fields = array(
'name' => __( 'Term name', 'jet-engine' ),
'description' => __( 'Term description', 'jet-engine' ),
'count' => __( 'Posts count', 'jet-engine' ),
);
break;
case 'users':
$fields = array(
'ID' => __( 'ID', 'jet-engine' ),
'user_login' => __( 'Login', 'jet-engine' ),
'user_nicename' => __( 'Nickname', 'jet-engine' ),
'user_email' => __( 'E-mail', 'jet-engine' ),
'user_url' => __( 'URL', 'jet-engine' ),
'user_registered' => __( 'Registration Date', 'jet-engine' ),
'display_name' => __( 'Display Name', 'jet-engine' ),
);
break;
}
$groups = apply_filters( 'jet-engine/listing/data/object-fields-groups', array(
array(
'label' => __( 'Post', 'jet-engine' ),
'options' => array(
'post_id' => __( 'Post ID', 'jet-engine' ),
'post_title' => __( 'Title', 'jet-engine' ),
'post_date' => __( 'Date', 'jet-engine' ),
'post_content' => __( 'Content', 'jet-engine' ),
'post_excerpt' => __( 'Excerpt', 'jet-engine' ),
'post_status' => __( 'Post Status', 'jet-engine' ),
)
),
array(
'label' => __( 'Term', 'jet-engine' ),
'options' => array(
'name' => __( 'Term name', 'jet-engine' ),
'description' => __( 'Term description', 'jet-engine' ),
'count' => __( 'Posts count', 'jet-engine' ),
)
),
array(
'label' => __( 'User', 'jet-engine' ),
'options' => array(
'ID' => __( 'ID', 'jet-engine' ),
'user_login' => __( 'Login', 'jet-engine' ),
'user_nicename' => __( 'Nickname', 'jet-engine' ),
'user_email' => __( 'E-mail', 'jet-engine' ),
'user_url' => __( 'URL', 'jet-engine' ),
'user_registered' => __( 'Registration Date', 'jet-engine' ),
'display_name' => __( 'Display Name', 'jet-engine' ),
)
),
) );
if ( 'blocks' === $where ) {
$result = array();
foreach ( $groups as $group ) {
$values = array();
foreach ( $group['options'] as $key => $value ) {
$values[] = array(
'value' => $key,
'label' => $value,
);
}
$result[] = array(
'label' => $group['label'],
'values' => $values,
);
}
return $result;
} else {
return $groups;
}
}
/**
* Checkl if requested property is property of user object
*
* @param [type] $prop [description]
* @return boolean [description]
*/
public function is_user_prop( $prop ) {
return in_array(
$prop,
array(
'ID',
'user_login',
'user_nicename',
'user_email',
'user_url',
'user_registered',
'display_name',
)
);
}
/**
* Get listing default property
*
* @param string $prop [description]
* @return [type] [description]
*/
public function listing_defaults( $prop = 'listing_source' ) {
if ( ! empty( $this->defaults ) ) {
return isset( $this->defaults[ $prop ] ) ? $this->defaults[ $prop ] : false;
}
$listing = $this->get_listing();
return isset( $listing[ $prop ] ) ? $listing[ $prop ] : false;
}
/**
* Returns all listing settings
*
* @return [type] [description]
*/
public function get_listing_settings() {
$listing = $this->get_listing();
if ( ! $listing ) {
return $this->defaults;
} else {
return $listing->get_settings();
}
}
/**
* Setup default listing settings
*
* @return [type] [description]
*/
public function setup_default_listing() {
$default = array(
'listing_source' => 'posts',
'listing_post_type' => 'post',
'listing_tax' => 'category',
);
$default_object = $this->get_default_object();
Loading ...