| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.6 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2023 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 |
* Render boxes |
| 26 |
* |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
public function render() |
| 30 |
{ |
| 31 |
if (!$boxes = BoxHelper::getAllBoxes()) |
| 32 |
{ |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
return $this->renderAll($boxes); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Renders all boxes |
| 41 |
* |
| 42 |
* @param array $boxes |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function renderAll($boxes) |
| 47 |
{ |
| 48 |
$html = ''; |
| 49 |
|
| 50 |
if ($boxes instanceof \WP_Query) |
| 51 |
{ |
| 52 |
if (!$boxes->have_posts()) |
| 53 |
{ |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
while ($boxes->have_posts()) |
| 58 |
{ |
| 59 |
$boxes->the_post(); |
| 60 |
|
| 61 |
global $post; |
| 62 |
|
| 63 |
$this->prepare($post); |
| 64 |
|
| 65 |
$html .= $this->get_output($post); |
| 66 |
} |
| 67 |
|
| 68 |
wp_reset_postdata(); |
| 69 |
} |
| 70 |
else if (is_array($boxes)) |
| 71 |
{ |
| 72 |
foreach ($boxes as $box) |
| 73 |
{ |
| 74 |
$this->prepare($box); |
| 75 |
|
| 76 |
$html .= $this->get_output($box); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return $html; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Prepares the box by setting its meta settings. |
| 85 |
* |
| 86 |
* @param object $box |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
private function prepare(&$box) |
| 91 |
{ |
| 92 |
// get meta options for box |
| 93 |
$meta = get_post_meta($box->ID, 'fpframework_meta_settings', true); |
| 94 |
$box->params = new Registry($meta); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the box output |
| 99 |
* |
| 100 |
* @param object $box |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
private function get_output($box) |
| 105 |
{ |
| 106 |
$logged_and_admin = (is_user_logged_in() && current_user_can('manage_options')); |
| 107 |
|
| 108 |
$testmode = $box->params->get('testmode', false); |
| 109 |
|
| 110 |
// if box is in test mode only logged-in admin users can see it. |
| 111 |
if ($testmode == '1' && !$logged_and_admin) |
| 112 |
{ |
| 113 |
return ''; |
| 114 |
} |
| 115 |
|
| 116 |
return firebox()->box->setBox($box)->render(); |
| 117 |
} |
| 118 |
} |