# firebox/1.0.7/Inc/Core/FB/Boxes.php

FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin &amp; Cart Abandonment, version 1.0.7. 140 lines.

- Page: https://pluginprobe.com/plugins/firebox/1.0.7/code/Inc/Core/FB/Boxes.php
- Raw: https://pluginprobe.com/plugins/firebox/1.0.7/raw/Inc/Core/FB/Boxes.php
- Modified: 2021-11-25T12:05:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/firebox/1.0.7/code/Inc/Core/FB/Boxes.php#L10-L20`.

```php
<?php
/**
 * @package         FireBox
 * @version         1.0.7 Free
 * 
 * @author          FirePlugins <info@fireplugins.com>
 * @link            https://www.fireplugins.com
 * @copyright       Copyright © 2021 FirePlugins All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/

namespace FireBox\Core\FB;

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

use FPFramework\Libs\Registry;
use FireBox\Core\Helpers\BoxHelper;

class Boxes
{
	/**
	 * A box instance
	 * 
	 * @var  Box
	 */
	protected $box;

	/**
	 * Factory
	 * 
	 * @var  Factory
	 */
	protected $factory;
	
	public function __construct($box = null, $factory = null)
	{
		if (!$factory)
		{
			$factory = new \FPFramework\Base\Factory();
		}
		$this->factory = $factory;
	}

	/**
	 * Render boxes
	 * 
	 * @return  string
	 */
	public function render()
	{
		if (!$boxes_query = BoxHelper::getAllBoxesWPQuery())
		{
			return;
		}

		if (!method_exists($boxes_query, 'have_posts') || (method_exists($boxes_query, 'have_posts') && !$boxes_query->have_posts()))
		{
			return;
        }

		return $this->renderAll($boxes_query);
	}
	
	/**
	 * Renders all boxes
	 * 
	 * @param   array  $boxes
	 * 
	 * @return  string
	 */
	public function renderAll($boxes)
	{
		$html = '';
		
		// handle WP_Query boxes loop
		if (isset($boxes->posts))
		{
			foreach ($boxes->posts as $box)
			{
				$boxes->the_post();

				$this->prepare($box);

				$html .= $this->get_output($box);
			}
			wp_reset_query();
		}
		else
		// handle boxes array
		{
			foreach ($boxes as $box)
			{
				$this->prepare($box);

				$html .= $this->get_output($box);
			}
		}

		return $html;
	}

	/**
	 * Prepares the box by setting its meta settings.
	 * 
	 * @param   object  $box
	 * 
	 * @return  void
	 */
	private function prepare(&$box)
	{
		// get meta options for box
		$meta = get_post_meta($box->ID, 'fpframework_meta_settings', true);
		$box->params = new Registry($meta);
	}

	/**
	 * Returns the box output
	 * 
	 * @param   object  $box
	 * 
	 * @return  string
	 */
	private function get_output($box)
	{
		$logged_and_admin = (is_user_logged_in() && current_user_can('manage_options'));

		$testmode = $box->params->get('testmode', false);
		
		// if box is in test mode only logged-in admin users can see it.
		if ($testmode == '1' && !$logged_and_admin)
		{
			return '';
		}
		
		return firebox()->box->render($box);
	}
}
```
