| 1 |
<?php |
| 2 |
|
| 3 |
namespace ContentEgg\application; |
| 4 |
|
| 5 |
use ContentEgg\application\admin\GeneralConfig; |
| 6 |
use ContentEgg\application\components\ModuleManager; |
| 7 |
use ContentEgg\application\ModuleViewer; |
| 8 |
use ContentEgg\application\helpers\TextHelper; |
| 9 |
use ContentEgg\application\WooIntegrator; |
| 10 |
|
| 11 |
defined('\ABSPATH') || exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* DataRestController class file |
| 15 |
* |
| 16 |
* @author keywordrush.com <support@keywordrush.com> |
| 17 |
* @link https://www.keywordrush.com |
| 18 |
* @copyright Copyright © 2026 keywordrush.com |
| 19 |
*/ |
| 20 |
class DataRestController extends \WP_REST_Controller |
| 21 |
{ |
| 22 |
protected $namespace = 'cegg/v1/data'; |
| 23 |
|
| 24 |
const VERSION = 1; |
| 25 |
const BASE = 'data'; |
| 26 |
|
| 27 |
private static $instance = null; |
| 28 |
|
| 29 |
public static function getInstance() |
| 30 |
{ |
| 31 |
if (self::$instance == null) |
| 32 |
self::$instance = new self; |
| 33 |
|
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
private function __construct() |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
public function init() |
| 42 |
{ |
| 43 |
\add_action('rest_api_init', array($this, 'register_routes')); |
| 44 |
} |
| 45 |
|
| 46 |
public function register_routes() |
| 47 |
{ |
| 48 |
\register_rest_route( |
| 49 |
$this->namespace, |
| 50 |
'/' . $this->rest_base . '/(?P<post_id>[\d]+)', |
| 51 |
array( |
| 52 |
array( |
| 53 |
'methods' => \WP_REST_Server::READABLE, |
| 54 |
'callback' => array($this, 'get_items'), |
| 55 |
'permission_callback' => '__return_true', |
| 56 |
'args' => array( |
| 57 |
'module_id' => array( |
| 58 |
'description' => __('Module ID.', 'woocommerce'), |
| 59 |
'type' => 'string', |
| 60 |
), |
| 61 |
'module_type' => array( |
| 62 |
'description' => __('Module type.', 'woocommerce'), |
| 63 |
'type' => 'string', |
| 64 |
), |
| 65 |
'extra' => array( |
| 66 |
'description' => __('Return extra.', 'woocommerce'), |
| 67 |
'type' => 'boolean', |
| 68 |
), |
| 69 |
'synced' => array( |
| 70 |
'description' => __('Return a synced product only.', 'woocommerce'), |
| 71 |
'type' => 'boolean', |
| 72 |
), |
| 73 |
), |
| 74 |
), |
| 75 |
'schema' => array($this, 'get_public_item_schema'), |
| 76 |
) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
public function get_items($request, $module_type = '') |
| 81 |
{ |
| 82 |
$post_id = (int) $request['post_id']; |
| 83 |
if (!isset($request['extra'])) |
| 84 |
$return_extra = false; |
| 85 |
else |
| 86 |
$return_extra = (bool) $request['extra']; |
| 87 |
|
| 88 |
if (!isset($request['synced'])) |
| 89 |
$return_synced = false; |
| 90 |
else |
| 91 |
$return_synced = (bool) $request['synced']; |
| 92 |
|
| 93 |
if (\get_post_status($post_id) !== 'publish') |
| 94 |
return new \WP_Error('cegg_rest_post_invalid_id', 'Invalid post ID.', array('status' => 404)); |
| 95 |
|
| 96 |
if (!in_array(\get_post_type($post_id), GeneralConfig::getInstance()->option('post_types'))) |
| 97 |
return new \WP_Error('cegg_rest_post_invalid_id', 'Invalid post type.', array('status' => 404)); |
| 98 |
|
| 99 |
$data = array(); |
| 100 |
|
| 101 |
$module_ids = ModuleManager::getInstance()->getParserModulesIdList(true); |
| 102 |
|
| 103 |
if ($return_synced) |
| 104 |
{ |
| 105 |
$mdata = WooIntegrator::getSyncItem($post_id); |
| 106 |
|
| 107 |
if (!$mdata || !isset($mdata['module_id']) || !in_array($mdata['module_id'], $module_ids)) |
| 108 |
return array(); |
| 109 |
|
| 110 |
$data[$mdata['module_id']] = array($mdata['unique_id'] => $mdata); |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
if (!empty($request['module_id'])) |
| 115 |
$module_ids = array_intersect($module_ids, TextHelper::getArrayFromCommaList($request['module_id'])); |
| 116 |
|
| 117 |
if (!empty($request['module_type'])) |
| 118 |
$module_type = TextHelper::getArrayFromCommaList(strtoupper($request['module_type'])); |
| 119 |
else |
| 120 |
$module_type = array(); |
| 121 |
|
| 122 |
if ($module_type) |
| 123 |
$module_ids = array_intersect($module_ids, ModuleManager::getInstance()->getParserModuleIdsByTypes($module_type, true)); |
| 124 |
|
| 125 |
foreach ($module_ids as $module_id) |
| 126 |
{ |
| 127 |
if ($mdata = ModuleViewer::getInstance()->getData($module_id, $post_id)) |
| 128 |
$data[$module_id] = $mdata; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if (!$return_extra) |
| 133 |
{ |
| 134 |
foreach ($data as $module_id => $mdata) |
| 135 |
{ |
| 136 |
foreach ($mdata as $unique_id => $d) |
| 137 |
{ |
| 138 |
$d['extra'] = array(); |
| 139 |
$data[$module_id][$unique_id] = $d; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return \rest_ensure_response($data); |
| 145 |
} |
| 146 |
} |
| 147 |
|