-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [#84] Parts Kit (to be continued...) * [#84] WIP Parts Kit Plugin using Standalone Editor * [#84] Reverted back to rendering the blocks * [#84] Probably as close as it's going to get. * [#84] Disable links in Block rendered area.
- Loading branch information
Showing
35 changed files
with
328,402 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
wp-content/plugins/acf-blocks-toolkit/includes/parts-kit.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
<?php | ||
/** | ||
* Add ACF Blocks to Parts Kit | ||
* | ||
* @package ACFBlocksToolkit | ||
*/ | ||
|
||
use Viget\ACFBlocksToolkit\Block_Registration; | ||
|
||
add_filter( | ||
'viget_parts_kit_block_%', | ||
function ( string $output, string $block_name ): string { | ||
$block = Block_Registration::get_block( $block_name ); | ||
|
||
if ( ! $block ) { | ||
return $output; | ||
} | ||
|
||
if ( ! empty( $block['path'] ) ) { | ||
$include = $block['path'] . '/block.php'; | ||
|
||
// Autoload block.php within block directory | ||
if ( file_exists( $include ) ) { | ||
require_once $include; | ||
} | ||
} | ||
|
||
return acfbt_parse_inner_blocks( $output ); | ||
}, | ||
10, | ||
2 | ||
); | ||
|
||
/** | ||
* Parse InnerBlocks Template | ||
* | ||
* @param string $output | ||
* | ||
* @return string | ||
*/ | ||
function acfbt_parse_inner_blocks( string $output ): string { | ||
// Check if InnerBlocks tag has a template attribute with regular expression | ||
if ( ! preg_match( '/<InnerBlocks[^>]*template="([^"]*)"[^>]*>/i', $output, $matches ) ) { | ||
return $output; | ||
} | ||
|
||
// Get the template attribute value | ||
$template = $matches[1]; | ||
$template = htmlspecialchars_decode( $template ); | ||
$template = json_decode( $template, true ); | ||
|
||
$content = ''; | ||
|
||
foreach ( $template as $block_array ) { | ||
$block = [ | ||
'blockName' => $block_array[0], | ||
'attrs' => $block_array[1] ?? [], | ||
'innerBlocks' => $block_array[2] ?? [], | ||
]; | ||
|
||
$block = acfbt_add_sample_data( $block ); | ||
$content .= apply_filters( 'the_content', trim( render_block( $block ) ) ); | ||
$content = acfbt_parse_inner_blocks( $content ); | ||
$content = acfbt_fill_empty_tags( $content ); | ||
} | ||
|
||
$content = str_replace( '$', '\$', $content ); | ||
|
||
// Replace the InnerBlocks tag with the parsed content | ||
return preg_replace( '/<InnerBlocks[^>]*\/>/i', $content, $output ); | ||
} | ||
|
||
/** | ||
* Get Sample Block properties | ||
* | ||
* @param array $block | ||
* | ||
* @return array | ||
*/ | ||
function acfbt_get_sample_props( array $block ): array { | ||
$props = []; | ||
$fields = get_fields( $block['name'] ); | ||
|
||
if ( ! is_array( $fields ) ) { | ||
return $props; | ||
} | ||
|
||
foreach ( $fields as $field ) { | ||
$props[ $field['name'] ] = acfbt_get_sample_data( $field ); | ||
} | ||
|
||
return $props; | ||
} | ||
|
||
/** | ||
* Return Sample Parts Kit Data | ||
* | ||
* @param array $field | ||
* | ||
* @return string|array | ||
*/ | ||
function acfbt_get_sample_data( array $field ): string|array { | ||
if ( in_array( $field['type'], [ 'text', 'select' ], true ) ) { | ||
return 'Lorem Ipsum'; | ||
} | ||
|
||
if ( 'image' === $field['type'] ) { | ||
return [ | ||
'ID' => 000, | ||
'id' => 000, | ||
'title' => 'placeholder', | ||
'filename' => '600x400.svg', | ||
'filesize' => 3270, | ||
'url' => 'https://placehold.co/600x400/EEE/31343C', | ||
'link' => 'https://placehold.co/600x400/EEE/31343C', | ||
'alt' => '600x400 Placeholder', | ||
'author' => 1, | ||
'description' => 'A 600x400 placeholder image.', | ||
'caption' => 'This is a placeholder', | ||
'name' => '600x400', | ||
'status' => 'inherit', | ||
'uploaded_to' => 0, | ||
'date' => '2023-09-24 13:12:00', | ||
'modified' => '2023-09-24 13:12:00', | ||
'menu_order' => 0, | ||
'mime_type' => 'image/svg+xml', | ||
'type' => 'image', | ||
'subtype' => 'svg', | ||
'icon' => 'https://viget-wp-boilerplate.vgt.site/wp-includes/images/media/default.png', | ||
'width' => 600, | ||
'height' => 400, | ||
'sizes' => [ | ||
'thumbnail' => 'https://placehold.co/150x150/EEE/31343C', | ||
'thumbnail-width' => 150, | ||
'thumbnail-height' => 150, | ||
'medium' => 'https://placehold.co/226x300/EEE/31343C', | ||
'medium-width' => 226, | ||
'medium-height' => 300, | ||
'large' => 'https://placehold.co/771x1024/EEE/31343C', | ||
'large-width' => 771, | ||
'large-height' => 1024, | ||
], | ||
]; | ||
} | ||
|
||
if ( 'wysiwyg' === $field['type'] ) { | ||
return '<p>Lorem Ipsum</p>'; | ||
} | ||
|
||
return 'Unsupported.'; | ||
} | ||
|
||
/** | ||
* Add Sample Data to Block | ||
* | ||
* @param array $block | ||
* | ||
* @return array | ||
*/ | ||
function acfbt_add_sample_data( array $block ): array { | ||
$supported = [ | ||
'core/image' => [ | ||
'url' => acfbt_get_sample_data( [ 'type' => 'image' ] )[ 'url' ], | ||
'width' => acfbt_get_sample_data( [ 'type' => 'image' ] )[ 'width' ], | ||
'height' => acfbt_get_sample_data( [ 'type' => 'image' ] )[ 'height' ], | ||
], | ||
'core/heading' => [ | ||
'content' => acfbt_get_sample_data( [ 'type' => 'text' ] ), | ||
], | ||
'core/paragraph' => [ | ||
'content' => acfbt_get_sample_data( [ 'type' => 'text' ] ), | ||
], | ||
'core/button' => [ | ||
'text' => acfbt_get_sample_data( [ 'type' => 'text' ] ), | ||
'url' => '#', | ||
], | ||
'core/details' => [ | ||
'summary' => acfbt_get_sample_data( [ 'type' => 'text' ] ), | ||
], | ||
]; | ||
|
||
if ( ! array_key_exists( $block['blockName'], $supported ) ) { | ||
return $block; | ||
} | ||
|
||
foreach ( $supported[ $block['blockName'] ] as $attr => $value ) { | ||
if ( empty( $block['attrs'][ $attr ] ) ) { | ||
$block['attrs'][ $attr ] = $value; | ||
} | ||
} | ||
|
||
return $block; | ||
} | ||
|
||
/** | ||
* Add sample content to empty paragraph, heading, summary, etc. | ||
* | ||
* @param string $content | ||
* | ||
* @return string | ||
*/ | ||
function acfbt_fill_empty_tags( string $content ): string { | ||
$sample_text = acfbt_get_sample_data( [ 'type' => 'text' ] ); | ||
|
||
$patterns = [ | ||
'/<(p[^>]*)><\/(p)>/i', | ||
'/<(h[1-6][^>]*)><\/(h[1-6])>/i', | ||
]; | ||
|
||
foreach ( $patterns as $pattern ) { | ||
$content = preg_replace( $pattern, '<$1>' . $sample_text . '</$2>', $content ); | ||
} | ||
|
||
return $content; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# This file is for unifying the coding style for different editors and IDEs | ||
# It is based on https://core.trac.wordpress.org/browser/trunk/.editorconfig | ||
# See https://editorconfig.org for more information about the standard. | ||
# WordPress VIP documentation: https://docs.wpvip.com/technical-references/vip-codebase/editorconfig/ | ||
|
||
# WordPress Coding Standards | ||
# https://make.wordpress.org/core/handbook/coding-standards/ | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = tab | ||
indent_size = 4 | ||
|
||
[{*.yml,*.json}] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.txt] | ||
end_of_line = crlf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": [ | ||
"plugin:@wordpress/eslint-plugin/recommended" | ||
] | ||
} |
Oops, something went wrong.