<?php
namespace Jet_Engine_Divi_Views\Components\Jet_Engine;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

trait Current_Object_Trait {

	protected static $prev_object = null;

	/**
	 * Get current object
	 *
	 * @return bool
	 */
	public static function set_current_object( $attrs = [] ) {

		$was_set = false;

		if ( empty( $attrs['jet_loop_object'] ) ) {
			return $was_set;
		}

		$loop_object = maybe_unserialize( $attrs['jet_loop_object'] );

		if ( is_object( $loop_object ) ) {
			self::$prev_object = jet_engine()->listings->data->get_current_object();
			jet_engine()->listings->data->set_current_object( $loop_object );
			$was_set = true;
		}

		return $was_set;
	}

	public static function reset_current_object() {
		if ( self::$prev_object ) {
			jet_engine()->listings->data->set_current_object( self::$prev_object );
			self::$prev_object = null;
		}
	}

}