| 1 |
<?php |
| 2 |
|
| 3 |
namespace ContentEgg\application; |
| 4 |
|
| 5 |
defined('\ABSPATH') || exit; |
| 6 |
|
| 7 |
use ContentEgg\application\components\ModuleManager; |
| 8 |
use ContentEgg\application\components\BlockTemplateManager; |
| 9 |
use ContentEgg\application\components\ShortcodeAtts; |
| 10 |
|
| 11 |
use function ContentEgg\prn; |
| 12 |
use function ContentEgg\prnx;; |
| 13 |
|
| 14 |
/** |
| 15 |
* BlockShortcode class file |
| 16 |
* |
| 17 |
* @author keywordrush.com <support@keywordrush.com> |
| 18 |
* @link https://www.keywordrush.com |
| 19 |
* @copyright Copyright © 2026 keywordrush.com |
| 20 |
*/ |
| 21 |
class BlockShortcode extends EggShortcode |
| 22 |
{ |
| 23 |
const shortcode = 'content-egg-block'; |
| 24 |
|
| 25 |
private static $instance = null; |
| 26 |
|
| 27 |
public static function getInstance() |
| 28 |
{ |
| 29 |
if (self::$instance == null) |
| 30 |
self::$instance = new self; |
| 31 |
return self::$instance; |
| 32 |
} |
| 33 |
|
| 34 |
private function __construct() |
| 35 |
{ |
| 36 |
\add_shortcode(self::shortcode, array($this, 'viewDataShortcode')); |
| 37 |
} |
| 38 |
|
| 39 |
public function viewDataShortcode($atts, $content = '') |
| 40 |
{ |
| 41 |
return $this->viewData($atts, $content); |
| 42 |
} |
| 43 |
|
| 44 |
protected function renderAsyncPlaceholder($post_id, array $a, $content = '') |
| 45 |
{ |
| 46 |
$html = $this->buildAsyncPlaceholderHtml( |
| 47 |
'cegg-block-', |
| 48 |
'cegg-block', |
| 49 |
'block', |
| 50 |
$post_id, |
| 51 |
$a, |
| 52 |
$content |
| 53 |
); |
| 54 |
|
| 55 |
return $this->getAsyncInlineCssOnce() . $html; |
| 56 |
} |
| 57 |
|
| 58 |
public function viewData($atts, $content = '', $only_return_data = false) |
| 59 |
{ |
| 60 |
$a = ShortcodeAtts::prepare($atts); |
| 61 |
|
| 62 |
if (empty($a['post_id'])) |
| 63 |
{ |
| 64 |
global $post; |
| 65 |
if (empty($post)) |
| 66 |
return ''; |
| 67 |
|
| 68 |
$post_id = $post->ID; |
| 69 |
} |
| 70 |
else |
| 71 |
$post_id = $a['post_id']; |
| 72 |
|
| 73 |
if (empty($a['template'])) |
| 74 |
return ''; |
| 75 |
|
| 76 |
if ($a['template'] != 'block_greenshift') |
| 77 |
{ |
| 78 |
$tpl_manager = BlockTemplateManager::getInstance(); |
| 79 |
|
| 80 |
if (!$tpl_manager->isTemplateExists($a['template'])) |
| 81 |
return ''; |
| 82 |
|
| 83 |
$template_file = $tpl_manager->getViewPath($a['template']); |
| 84 |
if (!$template_file) |
| 85 |
return ''; |
| 86 |
|
| 87 |
// Get supported modules for this tpl |
| 88 |
$headers = \get_file_data($template_file, array( |
| 89 |
'module_ids' => 'Modules', |
| 90 |
'module_types' => 'Module Types', |
| 91 |
'shortcoded' => 'Shortcoded' |
| 92 |
)); |
| 93 |
|
| 94 |
$supported_module_ids = array(); |
| 95 |
|
| 96 |
if ($headers && !empty($headers['module_ids'])) |
| 97 |
{ |
| 98 |
$supported_module_ids = explode(',', $headers['module_ids']); |
| 99 |
$supported_module_ids = array_map('trim', $supported_module_ids); |
| 100 |
} |
| 101 |
elseif ($headers && !empty($headers['module_types'])) |
| 102 |
{ |
| 103 |
$module_types = explode(',', $headers['module_types']); |
| 104 |
$module_types = array_map('trim', $module_types); |
| 105 |
$supported_module_ids = ModuleManager::getInstance()->getParserModuleIdsByTypes($module_types, true); |
| 106 |
} |
| 107 |
elseif (!$headers || empty($headers['module_types'])) |
| 108 |
$module_types = 'PRODUCT'; |
| 109 |
|
| 110 |
if ($headers && !empty($headers['shortcoded'])) |
| 111 |
$a['shortcoded'] = filter_var($headers['shortcoded'], FILTER_VALIDATE_BOOLEAN); |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
$a['shortcoded'] = true; |
| 116 |
$supported_module_ids = ModuleManager::getInstance()->getParserModuleIdsByTypes('PRODUCT', true); |
| 117 |
} |
| 118 |
|
| 119 |
if (!$supported_module_ids) |
| 120 |
return ''; |
| 121 |
|
| 122 |
/** |
| 123 |
* Async mode (frontend only) |
| 124 |
* - Do NOT use for editor preview or internal data-only calls. |
| 125 |
*/ |
| 126 |
if (!$this->isEditorRenderRequest() && !$only_return_data && !empty($a['async'])) |
| 127 |
{ |
| 128 |
return $this->renderAsyncPlaceholder($post_id, $a, $content); |
| 129 |
} |
| 130 |
|
| 131 |
if ($a['modules']) |
| 132 |
$module_ids = $a['modules']; |
| 133 |
else |
| 134 |
$module_ids = ModuleManager::getInstance()->getParserModulesIdList(true); |
| 135 |
|
| 136 |
$module_ids = array_intersect($module_ids, $supported_module_ids); |
| 137 |
|
| 138 |
if ($a['exclude_modules']) |
| 139 |
$module_ids = array_diff($module_ids, $a['exclude_modules']); |
| 140 |
|
| 141 |
return ModuleViewer::getInstance()->viewBlockData($module_ids, $post_id, $a, $content, $only_return_data); |
| 142 |
} |
| 143 |
} |
| 144 |
|