| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.39 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2025 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class RESTAPI |
| 20 |
{ |
| 21 |
/** |
| 22 |
* REST Route namespace |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $namespace = 'fireplugins/firebox'; |
| 27 |
|
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
// set api call used by gutenberg block to fetch all boxes |
| 31 |
add_action('rest_api_init', [$this, 'init_block_api']); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Sets REST route used by Gutenberg block to fetch all boxes |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function init_block_api() |
| 40 |
{ |
| 41 |
register_rest_route( |
| 42 |
$this->namespace, |
| 43 |
'boxes', |
| 44 |
[ |
| 45 |
'methods' => 'GET', |
| 46 |
'callback' => [$this, 'get_boxes'], |
| 47 |
'permission_callback' => function () |
| 48 |
{ |
| 49 |
return current_user_can('manage_options'); |
| 50 |
} |
| 51 |
] |
| 52 |
); |
| 53 |
|
| 54 |
register_rest_route( |
| 55 |
'firebox', |
| 56 |
'embeds', |
| 57 |
[ |
| 58 |
'methods' => 'GET', |
| 59 |
'callback' => [$this, 'get_embeds'], |
| 60 |
'permission_callback' => function () |
| 61 |
{ |
| 62 |
return current_user_can('manage_options'); |
| 63 |
} |
| 64 |
] |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Finds all boxes and returns them |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public function get_boxes() |
| 74 |
{ |
| 75 |
$boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes(['publish']); |
| 76 |
$boxes = $boxes->posts; |
| 77 |
|
| 78 |
if (!count($boxes)) |
| 79 |
{ |
| 80 |
return []; |
| 81 |
} |
| 82 |
|
| 83 |
$data = []; |
| 84 |
|
| 85 |
foreach ($boxes as $box) |
| 86 |
{ |
| 87 |
$data[] = [ |
| 88 |
'id' => $box->ID, |
| 89 |
'title' => $box->post_title |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
return $data; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Finds all embeds |
| 98 |
* |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
public function get_embeds() |
| 102 |
{ |
| 103 |
$boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes(['publish']); |
| 104 |
$boxes = $boxes->posts; |
| 105 |
|
| 106 |
if (!count($boxes)) |
| 107 |
{ |
| 108 |
return []; |
| 109 |
} |
| 110 |
|
| 111 |
$data = []; |
| 112 |
|
| 113 |
foreach ($boxes as $box) |
| 114 |
{ |
| 115 |
$meta = get_post_meta($box->ID, 'fpframework_meta_settings', true); |
| 116 |
|
| 117 |
$data[] = [ |
| 118 |
'id' => $box->ID, |
| 119 |
'title' => $box->post_title |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
return $data; |
| 124 |
} |
| 125 |
} |