-
Notifications
You must be signed in to change notification settings - Fork 38
Code patterns: Initial experiment. #173
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
Draft
cbravobernal
wants to merge
20
commits into
trunk
Choose a base branch
from
experiment/code-patterns
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
18542b5
First commit
cbravobernal fe4afa5
Experimenting, images still not working
cbravobernal 81b614c
Some cleaning, image still failing
cbravobernal 5edb9b1
Back to something working again
cbravobernal 37d3297
Image fixed
cbravobernal 080f447
Simplify pattern creation with just PHP functions
cbravobernal 2b15a95
Enable as an experiment
cbravobernal 64cde78
Fix images
cbravobernal d6bd7d8
Group it if not before
cbravobernal d7cef4a
Now is working on the editor
cbravobernal 6e60b6b
Update and edit values
cbravobernal 4668dda
Now saves correctly
cbravobernal 99fcf7d
Not ready, update metaboxes
cbravobernal 41b2bed
A slighlty better way to handle the post-meta update
cbravobernal bc491b9
Clean custom-sources file
cbravobernal 136e564
Add usercanedit depending on role
cbravobernal 77b88cc
Now works with empty posts
cbravobernal d8a99f5
Fix working with type number
cbravobernal 845bf93
Remove clogs
cbravobernal fe360db
Remove not needed code
cbravobernal 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 hidden or 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 hidden or 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,116 @@ | ||
| /** | ||
| * WordPress dependencies. | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { registerBlockBindingsSource } from '@wordpress/blocks'; | ||
| import { store as coreDataStore } from '@wordpress/core-data'; | ||
|
|
||
| /** | ||
| * Get the value of a specific field from the ACF fields. | ||
| * | ||
| * @param {Object} fields The ACF fields object. | ||
| * @param {string} fieldName The name of the field to retrieve. | ||
| * @returns {string} The value of the specified field, or undefined if not found. | ||
| */ | ||
| const getFieldValue = ( fields, fieldName ) => fields?.acf?.[ fieldName ]; | ||
|
|
||
| const resolveImageAttribute = ( imageObj, attribute ) => { | ||
| if ( ! imageObj ) return ''; | ||
| switch ( attribute ) { | ||
| case 'id': | ||
| return imageObj.id; | ||
| case 'url': | ||
| case 'content': | ||
| return imageObj.source_url; | ||
| case 'alt': | ||
| return imageObj.alt_text || ''; | ||
| case 'title': | ||
| return imageObj.title?.rendered || ''; | ||
| default: | ||
| return ''; | ||
| } | ||
| }; | ||
|
|
||
| registerBlockBindingsSource( { | ||
| name: 'scf/experimental-field', | ||
| label: 'SCF Custom Fields', | ||
| getValues( { context, bindings, select } ) { | ||
| const { getEditedEntityRecord, getMedia } = select( coreDataStore ); | ||
| let fields = | ||
| context?.postType && context?.postId | ||
| ? getEditedEntityRecord( | ||
| 'postType', | ||
| context.postType, | ||
| context.postId | ||
| ) | ||
| : undefined; | ||
| const result = {}; | ||
|
|
||
| Object.entries( bindings ).forEach( | ||
| ( [ attribute, { args } = {} ] ) => { | ||
| const fieldName = args?.field; | ||
| const fieldValue = getFieldValue( fields, fieldName ); | ||
|
|
||
| if ( typeof fieldValue === 'object' && fieldValue !== null ) { | ||
| result[ attribute ] = | ||
| ( fieldValue[ attribute ] ?? | ||
| ( attribute === 'content' && fieldValue.url ) ) || | ||
| ''; | ||
| } else if ( typeof fieldValue === 'number' ) { | ||
| const imageObj = getMedia( fieldValue ); | ||
| result[ attribute ] = resolveImageAttribute( | ||
| imageObj, | ||
| attribute | ||
| ); | ||
| } else { | ||
| result[ attribute ] = fieldValue || ''; | ||
| } | ||
| } | ||
| ); | ||
| return result; | ||
| }, | ||
| async setValues( { context, bindings, dispatch, select } ) { | ||
| const { getEditedEntityRecord } = select( coreDataStore ); | ||
| if ( ! bindings || ! context?.postType || ! context?.postId ) return; | ||
|
|
||
| const postType = context.postType; | ||
| const postId = context.postId; | ||
| const currentPost = getEditedEntityRecord( | ||
| 'postType', | ||
| postType, | ||
| postId | ||
| ); | ||
| const currentAcfData = currentPost?.acf || {}; | ||
| const fieldsToUpdate = {}; | ||
|
|
||
| for ( const [ attribute, binding ] of Object.entries( bindings ) ) { | ||
| const fieldName = binding?.args?.field; | ||
| const newValue = binding?.newValue; | ||
| if ( ! fieldName || newValue === undefined ) continue; | ||
| if ( ! fieldsToUpdate[ fieldName ] ) { | ||
| fieldsToUpdate[ fieldName ] = newValue; | ||
| } else if ( | ||
| attribute === 'url' && | ||
| typeof fieldsToUpdate[ fieldName ] === 'object' | ||
| ) { | ||
| fieldsToUpdate[ fieldName ] = { | ||
| ...fieldsToUpdate[ fieldName ], | ||
| url: newValue, | ||
| }; | ||
| } else if ( attribute === 'id' && typeof newValue === 'number' ) { | ||
| fieldsToUpdate[ fieldName ] = newValue; | ||
| } | ||
| } | ||
|
|
||
| dispatch( coreDataStore ).editEntityRecord( | ||
| 'postType', | ||
| postType, | ||
| postId, | ||
| { | ||
| acf: { ...currentAcfData, ...fieldsToUpdate }, | ||
| meta: { _acf_changed: 1 }, | ||
| } | ||
| ); | ||
| }, | ||
| canUserEditValue: () => true, | ||
| } ); | ||
This file contains hidden or 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 @@ | ||
| import './custom-sources.js'; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
This should check permissions.