-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract meta custom sources to a separate file
- Loading branch information
1 parent
dd59572
commit 987a544
Showing
6 changed files
with
253 additions
and
206 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* Meta Custom Source | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
return array( | ||
'name' => 'meta', | ||
'apply_source' => function ( $block_type, $block_content, $block_instance, $attribute_source, $attribute_config ) { | ||
$meta_field = $attribute_source['name']; | ||
$meta_value = get_post_meta( $block_instance->context['postId'], $meta_field, true ); | ||
$p = new WP_HTML_Tag_Processor( $block_content ); | ||
$found = $p->next_tag( | ||
array( | ||
// TODO: build the query from CSS selector. | ||
'tag_name' => $attribute_config['selector'], | ||
) | ||
); | ||
if ( ! $found ) { | ||
return $block_content; | ||
} | ||
$tag_name = $p->get_tag(); | ||
$markup = "<$tag_name>$meta_value</$tag_name>"; | ||
$p2 = new WP_HTML_Tag_Processor( $markup ); | ||
$p2->next_tag(); | ||
$names = $p->get_attribute_names_with_prefix( '' ); | ||
foreach ( $names as $name ) { | ||
$p2->set_attribute( $name, $p->get_attribute( $name ) ); | ||
} | ||
|
||
return $p2 . ''; | ||
}, | ||
); |
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,110 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getBlockType } from '@wordpress/blocks'; | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
import { useMemo, useCallback } from '@wordpress/element'; | ||
|
||
export default { | ||
name: 'meta', | ||
|
||
useSource( { | ||
name, | ||
context: { postType, postId }, | ||
attributes, | ||
setAttributes, | ||
} ) { | ||
const [ meta, setMeta ] = useEntityProp( | ||
'postType', | ||
postType, | ||
'meta', | ||
postId | ||
); | ||
|
||
const blockType = getBlockType( name ); | ||
|
||
const attributesWithSourcedAttributes = useMemo( () => { | ||
if ( ! blockType.supports?.customSources ) { | ||
return attributes; | ||
} | ||
return { | ||
...attributes, | ||
...Object.fromEntries( | ||
Object.keys( blockType.supports.customSources ).map( | ||
( attributeName ) => { | ||
if ( | ||
attributes.source?.[ attributeName ]?.type === | ||
'meta' | ||
) { | ||
return [ | ||
attributeName, | ||
meta?.[ | ||
attributes.source?.[ attributeName ] | ||
?.name | ||
], | ||
]; | ||
} | ||
return [ | ||
attributeName, | ||
attributes[ attributeName ], | ||
]; | ||
} | ||
) | ||
), | ||
}; | ||
}, [ blockType.supports?.customSources, attributes, meta ] ); | ||
|
||
const updatedSetAttributes = useCallback( | ||
( nextAttributes ) => { | ||
const nextMeta = Object.fromEntries( | ||
Object.entries( nextAttributes ?? {} ) | ||
.filter( | ||
// Filter to intersection of keys between the updated | ||
// attributes and those with an associated meta key. | ||
( [ key ] ) => | ||
blockType.supports?.customSources && | ||
key in blockType.supports?.customSources && | ||
attributes.source?.[ key ]?.type === 'meta' | ||
) | ||
.map( ( [ attributeKey, value ] ) => [ | ||
// Rename the keys to the expected meta key name. | ||
attributes.source?.[ attributeKey ]?.name, | ||
value, | ||
] ) | ||
); | ||
|
||
const updatedAttributes = Object.entries( nextMeta ).length | ||
? Object.fromEntries( | ||
Object.entries( nextAttributes ).filter( | ||
( [ key ] ) => | ||
! ( | ||
blockType.supports?.customSources && | ||
key in | ||
blockType.supports?.customSources && | ||
attributes.source?.[ key ]?.type === | ||
'meta' | ||
) | ||
) | ||
) | ||
: nextAttributes; | ||
|
||
if ( Object.entries( nextMeta ).length ) { | ||
setMeta( nextMeta ); | ||
} | ||
|
||
setAttributes( updatedAttributes ); | ||
}, | ||
[ | ||
setAttributes, | ||
attributes.source, | ||
blockType.supports?.customSources, | ||
setMeta, | ||
] | ||
); | ||
|
||
return { | ||
setAttributes: updatedSetAttributes, | ||
attributes: attributesWithSourcedAttributes, | ||
}; | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.