Skip to content

Commit

Permalink
adding event better and faster caching for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Apr 30, 2024
1 parent 2e6cc1c commit 88e5dfe
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 178 deletions.
175 changes: 29 additions & 146 deletions src/Blocks/AbstractBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
use EightshiftLibs\Cache\AbstractManifestCache;
use EightshiftLibs\Cache\ManifestCacheInterface;
use EightshiftLibs\Exception\InvalidBlock;
use EightshiftLibs\Exception\InvalidManifest;
use EightshiftLibs\Exception\InvalidPath;
use EightshiftLibs\Helpers\Components;
use EightshiftLibs\Helpers\Helpers;
use EightshiftLibs\Services\ServiceInterface;
use WP_Block_Editor_Context;
use WP_Post;
Expand All @@ -26,64 +25,6 @@
*/
abstract class AbstractBlocks implements ServiceInterface, RenderableBlockInterface
{
/**
* Blocks builder array.
*
* @var array<string, array<string, mixed>>
*/
private const BLOCKS_BUILDER = [
AbstractManifestCache::BLOCKS_KEY => [
'multiple' => true,
'validation' => [
'blockName',
'namespace',
'blockFullName',
'keywords',
'icon',
'category',
'description',
'title',
'$schema',
],
],
AbstractManifestCache::COMPONENTS_KEY => [
'multiple' => true,
'validation' => [
'componentName',
'$schema',
'title',
],
],
AbstractManifestCache::VARIATIONS_KEY => [
'multiple' => true,
'validation' => [
'name',
'$schema',
'parentName',
'title',
'icon',
'description',
],
],
AbstractManifestCache::WRAPPER_KEY => [
'multiple' => false,
'validation' => [
'$schema',
'title'
],
],
AbstractManifestCache::SETTINGS_KEY => [
'multiple' => false,
'validation' => [
'$schema',
'namespace',
'background',
'foreground',
'blockClassPrefix',
],
],
];

/**
* Instance variable for manifest cache.
*
Expand All @@ -110,7 +51,7 @@ public function __construct(ManifestCacheInterface $manifestCache)
public function changeEditorColorPalette(): void
{
// Unable to use state due to this method is used in JS and store is not registered there.
$colors = $this->getManifest(AbstractManifestCache::SETTINGS_KEY)['globalVariables']['colors'] ?? [];
$colors = $this->manifestCache->getManifestCacheTopItem(AbstractManifestCache::SETTINGS_KEY)['globalVariables']['colors'] ?? [];

if ($colors) {
\add_theme_support('editor-color-palette', $colors);
Expand All @@ -137,24 +78,24 @@ public function addThemeSupport(): void
public function getBlocksDataFullRaw(): void
{
// Register store and set all the data.
Components::setStore();
Components::setSettings($this->getManifest(AbstractManifestCache::SETTINGS_KEY));
Components::setConfigFlags();
Helpers::setStore();
Helpers::setSettings($this->manifestCache->getManifestCacheTopItem(AbstractManifestCache::SETTINGS_KEY));
Helpers::setConfigFlags();

if (Components::getConfigUseBlocks()) {
Components::setBlocks($this->getManifest(AbstractManifestCache::BLOCKS_KEY));
if (Helpers::getConfigUseBlocks()) {
Helpers::setBlocks($this->manifestCache->getManifestCacheTopItem(AbstractManifestCache::BLOCKS_KEY));
}

if (Components::getConfigUseComponents()) {
Components::setComponents($this->getManifest(AbstractManifestCache::COMPONENTS_KEY));
if (Helpers::getConfigUseComponents()) {
Helpers::setComponents($this->manifestCache->getManifestCacheTopItem(AbstractManifestCache::COMPONENTS_KEY));
}

if (Components::getConfigUseVariations()) {
Components::setVariations($this->getManifest(AbstractManifestCache::VARIATIONS_KEY));
if (Helpers::getConfigUseVariations()) {
Helpers::setVariations($this->manifestCache->getManifestCacheTopItem(AbstractManifestCache::VARIATIONS_KEY));
}

if (Components::getConfigUseWrapper()) {
Components::setWrapper($this->getManifest(AbstractManifestCache::WRAPPER_KEY));
if (Helpers::getConfigUseWrapper()) {
Helpers::setWrapper($this->manifestCache->getManifestCacheTopItem(AbstractManifestCache::WRAPPER_KEY));
}
}

Expand Down Expand Up @@ -187,7 +128,7 @@ public function getAllAllowedBlocksList($allowedBlockTypes, WP_Block_Editor_Cont
return $allowedBlockTypes;
}

if (Components::getConfigUseBlocks()) {
if (Helpers::getConfigUseBlocks()) {
$allowedBlockTypes = \array_merge(
\array_map(
function ($block) {
Expand Down Expand Up @@ -233,7 +174,7 @@ public function getAllBlocksList($allowedBlockTypes, WP_Block_Editor_Context $bl
*/
public function registerBlocks(): void
{
if (!Components::getConfigUseBlocks()) {
if (!Helpers::getConfigUseBlocks()) {
return;
}

Expand All @@ -259,11 +200,11 @@ public function render(array $attributes, string $innerBlockContent): string

// Get block view path.
$sep = \DIRECTORY_SEPARATOR;
$templatePath = Components::getProjectPaths('blocksDestinationCustom', "{$blockName}{$sep}{$blockName}.php");
$templatePath = Helpers::getProjectPaths('blocksDestinationCustom', "{$blockName}{$sep}{$blockName}.php");

// Get block wrapper view path.
if (Components::getConfigUseWrapper()) {
$wrapperPath = Components::getProjectPaths('blocksDestinationWrapper', 'wrapper.php');
if (Helpers::getConfigUseWrapper()) {
$wrapperPath = Helpers::getProjectPaths('blocksDestinationWrapper', 'wrapper.php');

// Check if wrapper component exists.
if (!\file_exists($wrapperPath)) {
Expand Down Expand Up @@ -352,7 +293,7 @@ public function renderWrapperView(string $src, array $attributes, ?string $inner
*/
public function filterBlocksContent(array $parsedBlock, array $sourceBlock): array
{
$namespace = Components::getSettingsNamespace();
$namespace = Helpers::getSettingsNamespace();

if ($parsedBlock['blockName'] === "{$namespace}/paragraph") {
$content = $parsedBlock['attrs']['paragraphParagraphContent'] ?? '';
Expand All @@ -373,7 +314,7 @@ public function filterBlocksContent(array $parsedBlock, array $sourceBlock): arr
*/
public function outputCssVariablesInline(): void
{
echo Components::outputCssVariablesInline(); // phpcs:ignore
echo Helpers::outputCssVariablesInline(); // phpcs:ignore
}

/**
Expand All @@ -396,64 +337,6 @@ private function registerBlock(array $blockDetails): void
);
}

/**
* Get manifest data for the provided type.
*
* @param string $type Type of the manifest.
*
* @throws InvalidManifest If the manifest is missing or has an error.
*
* @return array<string, mixed>
*/
private function getManifest(string $type): array
{
$multiple = self::BLOCKS_BUILDER[$type]['multiple'] ?? false;

return $multiple ? $this->getManifestItems($type) : $this->getManifestItem($type);
}

/**
* Get multiple items from the manifest.
*
* @param string $type Type of the manifest.
*
* @throws InvalidManifest If the manifest is missing or has an error.
*
* @return array<array<string, mixed>>
*/
private function getManifestItems(string $type): array
{
$data = $this->manifestCache->getManifestCacheTopItem($type);

foreach ($data as $path => $item) {
$validation = self::BLOCKS_BUILDER[$type]['validation'] ?? [];

if (!$validation) {
continue;
}

foreach ($validation as $key) {
if (!isset($item[$key])) {
throw InvalidManifest::missingManifestKeyException($key, $path);
}
}
}

return \array_values($data);
}

/**
* Get single item from the manifest.
*
* @param string $type Type of the manifest.
*
* @return array<string, mixed>
*/
private function getManifestItem(string $type): array
{
return $this->manifestCache->getManifestCacheTopItem($type);
}

/**
* Prepare all blocks attributes.
*
Expand All @@ -469,12 +352,12 @@ private function getManifestItem(string $type): array
private function getAttributes(array $blockDetails): array
{
$blockName = $blockDetails['blockName'];
$blockClassPrefix = Components::getSettingsBlockClassPrefix();
$blockClassPrefix = Helpers::getSettingsBlockClassPrefix();

$wrapperAttributes = [];

if (Components::getConfigUseWrapper()) {
$wrapperAttributes = Components::getWrapperAttributes();
if (Helpers::getConfigUseWrapper()) {
$wrapperAttributes = Helpers::getWrapperAttributes();
}

return \array_merge(
Expand All @@ -489,7 +372,7 @@ private function getAttributes(array $blockDetails): array
],
'blockTopLevelId' => [
'type' => 'string',
'default' => Components::getUnique(),
'default' => Helpers::getUnique(),
],
'blockFullName' => [
'type' => 'string',
Expand All @@ -508,7 +391,7 @@ private function getAttributes(array $blockDetails): array
'default' => false,
],
],
Components::getSettingsAttributes(),
Helpers::getSettingsAttributes(),
$wrapperAttributes,
$this->prepareComponentAttributes($blockDetails)
);
Expand Down Expand Up @@ -538,7 +421,7 @@ private function prepareComponentAttribute(array $manifest, string $newName, str
}

// Make sure the case is always correct for parent.
$newParent = Components::kebabToCamelCase($parent);
$newParent = Helpers::kebabToCamelCase($parent);

// Iterate each attribute and attach parent prefixes.
$componentAttributeKeys = \array_keys($componentAttributes);
Expand All @@ -552,7 +435,7 @@ private function prepareComponentAttribute(array $manifest, string $newName, str

// Check if current attribute is used strip component prefix from attribute and replace it with parent prefix.
if ($currentAttributes) {
$attribute = \str_replace(\lcfirst(Components::kebabToCamelCase($realName)), '', (string) $componentAttribute);
$attribute = \str_replace(\lcfirst(Helpers::kebabToCamelCase($realName)), '', (string) $componentAttribute);
}

// Determine if parent is empty and if parent name is the same as component/block name and skip wrapper attributes.
Expand Down Expand Up @@ -594,7 +477,7 @@ private function prepareComponentAttributes(array $manifest, string $parent = ''
// Iterate over components key in manifest recursively and check component names.
foreach ($components as $newComponentName => $realComponentName) {
// Filter components real name.
$component = Components::getComponent(Components::camelToKebabCase($realComponentName));
$component = Helpers::getComponent(Helpers::camelToKebabCase($realComponentName));

// Bailout if component doesn't exist.
if (!$component) {
Expand All @@ -603,7 +486,7 @@ private function prepareComponentAttributes(array $manifest, string $parent = ''

// If component has more components do recursive loop.
if (isset($component['components'])) {
$outputAttributes = $this->prepareComponentAttributes($component, $newParent . \ucfirst(Components::camelToKebabCase($newComponentName)));
$outputAttributes = $this->prepareComponentAttributes($component, $newParent . \ucfirst(Helpers::camelToKebabCase($newComponentName)));
} else {
// Output the component attributes if there is no nesting left, and append the parent prefixes.
$outputAttributes = $this->prepareComponentAttribute($component, $newComponentName, $realComponentName, $newParent);
Expand Down
Loading

0 comments on commit 88e5dfe

Please sign in to comment.