* @link https://www.fireplugins.com * @copyright Copyright © 2021 FirePlugins All Rights Reserved * @license GNU GPLv3 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; if (!$box) { $box = new \FireBox\Core\FB\Box(null, $this->factory); } $this->box = $box; } /** * Render boxes * * @return string */ public function render() { $boxes_query = BoxHelper::getAllBoxesWPQuery(); 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 $this->box->render($box); } /** * Return the box * * @return Box */ public function getBox() { return $this->box; } /** * Set the box * * @param Box $box * * @return Box */ public function setBox($box) { if (!$box) { return; } $this->box = $box; } }