| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.0 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2021 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\FB; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Libs\Registry; |
| 20 |
use FireBox\Core\Helpers\BoxHelper; |
| 21 |
|
| 22 |
class Boxes |
| 23 |
{ |
| 24 |
/** |
| 25 |
* A box instance |
| 26 |
* |
| 27 |
* @var Box |
| 28 |
*/ |
| 29 |
protected $box; |
| 30 |
|
| 31 |
/** |
| 32 |
* Factory |
| 33 |
* |
| 34 |
* @var Factory |
| 35 |
*/ |
| 36 |
protected $factory; |
| 37 |
|
| 38 |
public function __construct($box = null, $factory = null) |
| 39 |
{ |
| 40 |
if (!$factory) |
| 41 |
{ |
| 42 |
$factory = new \FPFramework\Base\Factory(); |
| 43 |
} |
| 44 |
$this->factory = $factory; |
| 45 |
|
| 46 |
if (!$box) |
| 47 |
{ |
| 48 |
$box = new \FireBox\Core\FB\Box(null, $this->factory); |
| 49 |
} |
| 50 |
$this->box = $box; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Render boxes |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function render() |
| 59 |
{ |
| 60 |
$boxes_query = BoxHelper::getAllBoxesWPQuery(); |
| 61 |
|
| 62 |
if (!method_exists($boxes_query, 'have_posts') || (method_exists($boxes_query, 'have_posts') && !$boxes_query->have_posts())) |
| 63 |
{ |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
return $this->renderAll($boxes_query); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Renders all boxes |
| 72 |
* |
| 73 |
* @param array $boxes |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function renderAll($boxes) |
| 78 |
{ |
| 79 |
$html = ''; |
| 80 |
|
| 81 |
// handle WP_Query boxes loop |
| 82 |
if (isset($boxes->posts)) |
| 83 |
{ |
| 84 |
foreach ($boxes->posts as $box) |
| 85 |
{ |
| 86 |
$boxes->the_post(); |
| 87 |
|
| 88 |
$this->prepare($box); |
| 89 |
|
| 90 |
$html .= $this->get_output($box); |
| 91 |
} |
| 92 |
wp_reset_query(); |
| 93 |
} |
| 94 |
else |
| 95 |
// handle boxes array |
| 96 |
{ |
| 97 |
foreach ($boxes as $box) |
| 98 |
{ |
| 99 |
$this->prepare($box); |
| 100 |
|
| 101 |
$html .= $this->get_output($box); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $html; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Prepares the box by setting its meta settings. |
| 110 |
* |
| 111 |
* @param object $box |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
private function prepare(&$box) |
| 116 |
{ |
| 117 |
// get meta options for box |
| 118 |
$meta = get_post_meta($box->ID, 'fpframework_meta_settings', true); |
| 119 |
$box->params = new Registry($meta); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the box output |
| 124 |
* |
| 125 |
* @param object $box |
| 126 |
* |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
private function get_output($box) |
| 130 |
{ |
| 131 |
$logged_and_admin = (is_user_logged_in() && current_user_can('manage_options')); |
| 132 |
|
| 133 |
$testmode = $box->params->get('testmode', false); |
| 134 |
|
| 135 |
// if box is in test mode only logged-in admin users can see it. |
| 136 |
if ($testmode == '1' && !$logged_and_admin) |
| 137 |
{ |
| 138 |
return ''; |
| 139 |
} |
| 140 |
|
| 141 |
return $this->box->render($box); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Return the box |
| 146 |
* |
| 147 |
* @return Box |
| 148 |
*/ |
| 149 |
public function getBox() |
| 150 |
{ |
| 151 |
return $this->box; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Set the box |
| 156 |
* |
| 157 |
* @param Box $box |
| 158 |
* |
| 159 |
* @return Box |
| 160 |
*/ |
| 161 |
public function setBox($box) |
| 162 |
{ |
| 163 |
if (!$box) |
| 164 |
{ |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$this->box = $box; |
| 169 |
} |
| 170 |
} |