-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#84] Parts Kit Integration #101
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
933a664
[#84] Parts Kit (to be continued...)
bd-viget 0df4305
[#84] WIP Parts Kit Plugin using Standalone Editor
bd-viget dd191e7
[#84] Reverted back to rendering the blocks
bd-viget d9413eb
[#84] Probably as close as it's going to get.
bd-viget f89cf81
[#84] Disable links in Block rendered area.
bd-viget File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous iteration of
craft-viget
base` has some nice helpers for generating random text for the parts kit that might be cool to check out.https://github.com/vigetlabs/craft-viget-base/blob/8def1735a0b3b8ef62661562dd01ee9eaf6fbdb1/src/services/PartsKit.php#L192