Repository navigation
Gutenberg: Simple payment read/save/update - #28065
Conversation
| status: 'publish', | ||
| content: description, | ||
| featured_media: '', | ||
| // @FIXME: meta isn't saving. Tried { spay_price: … } simple object literal and it didn't work either :( |
There was a problem hiding this comment.
We need to use register_meta to whitelist these meta fields in the API for reading+writing (PR Automattic/jetpack#10402).
https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
| title, | ||
| status: 'publish', | ||
| content: description, | ||
| featured_media: '', |
There was a problem hiding this comment.
This has to default to 0, otherwise, we'll get a rest_invalid_param error Invalid parameter(s): featured_media
| const { id } = await apiFetch( { | ||
| path, | ||
| method: 'POST', | ||
| body: JSON.stringify( attributesToPost( attributes ) ), |
There was a problem hiding this comment.
This is sending the wrong content type. We need to send application/json, so this line has to be:
data: attributesToPost( attributes ),| content: description, | ||
| featured_media: '', | ||
| // @FIXME: meta isn't saving. Tried { spay_price: … } simple object literal and it didn't work either :( | ||
| meta: [ |
There was a problem hiding this comment.
The backend expects the meta with a different format. We need to send:
meta: {
'spay_price': price,
'spay_currency': 'USD',
'spay_multiple': 0,
'spay_email': email,
'spay_formatted_price': '$' + price.toFixed( 2 ),
},|
I can confirm that, once applied the changes from Automattic/jetpack#10402 on WordPress.com, that it works as expected in Calypso for sites with a business plan. I think we can safely add |
tyxla
left a comment
There was a problem hiding this comment.
This is great work 👏
The PR is testing well for me! I realize the UI is still a WIP, so I've refrained from any feedback on that bit.
The only thing I've noticed is when saving and then refreshing the post that has a payment button, I get the following:
I've also left some comments, mostly minor stuff.
Nicely done! 💯
| /** | ||
| * External dependencies | ||
| */ | ||
| import get from 'lodash/get'; |
| * External dependencies | ||
| */ | ||
| import get from 'lodash/get'; | ||
| import { __ } from '@wordpress/i18n'; |
There was a problem hiding this comment.
Noting that once #28088 lands, we'll have to use our own i18n wrappers.
| this.props.setAttributes( { paymentId } ); | ||
| constructor() { | ||
| super( ...arguments ); | ||
| this.state = { |
There was a problem hiding this comment.
If we alter:
this.state = {
savingProduct: false,
};
to a class instance property, defined outside of constructor():
state = {
savingProduct: false,
};
we could totally ditch the constructor() method.
|
|
||
| if ( ! prevProps.simplePayment && simplePayment ) { | ||
| setAttributes( { | ||
| description: get( simplePayment, 'content.raw', attributes.description ), |
There was a problem hiding this comment.
We could save _.get() some cycles if we use arrays instead of strings in all these calls, e.g. [ 'content', 'raw' ] instead of 'content.raw'
|
|
||
| // @TODO field validation | ||
|
|
||
| const path = `/wp/v2/${ SIMPLE_PAYMENTS_PRODUCT_POST_TYPE }/${ paymentId ? paymentId : '' }`; |
There was a problem hiding this comment.
Ditch the trailing / if we don't have an ID yet?
| */ | ||
| import { SIMPLE_PAYMENTS_PRODUCT_POST_TYPE } from 'lib/simple-payments/constants'; | ||
|
|
||
| class Edit extends Component { |
There was a problem hiding this comment.
Perhaps a little less-broad name for this component?
|
|
||
| if ( ! isSelected ) { | ||
| // @TODO component | ||
| return ( |
There was a problem hiding this comment.
I'm expecting this is a WIP at this time? Would be even better if we split it off in a separate component before merging, to make it easier for iteration with less chance for conflicts.
Essentially, the more we componentize here, the better it becomes for iteration and reusability.
| category: 'jetpack', | ||
|
|
||
| keywords: [ 'simple payments', 'PayPal' ], | ||
| keywords: [ __( 'simple payments', 'jetpack' ), __( 'PayPal', 'jetpack' ) ], |
There was a problem hiding this comment.
Noting that after #28088 is merged, we won't need the textdomains.
| // @TODO move everything else except `paymentId` to state instead? | ||
| currency: { | ||
| type: 'string', | ||
| default: 'USD', |
There was a problem hiding this comment.
FWIW, any block instances that use the default values are removed from the serialized representation of the block, so we should either be sure that we're using the right default values, or try to rely as little as possible on default attribute values at block registration level.
| const titleId = `${ baseId }__title`; | ||
|
|
||
| if ( ! isSelected ) { | ||
| // @TODO component |
There was a problem hiding this comment.
I'm assuming that all TODOs and FIXMEs will be handled in subsequent PRs?
382bc16 to
d49d93c
Compare
Yap, known issue independent from the block Automattic/jetpack#10481 |
As per Gutenberg docs: _Note:_ While it is possible to return a string value from `save`, it _will be escaped_. If the string includes HTML markup, the markup will be shown on the front of the site verbatim, not as the equivalent HTML node content. If you must return raw HTML from `save`, use `wp.element.RawHTML`. As the name implies, this is prone to [cross-site scripting](https://en.wikipedia.org/wiki/Cross-site_scripting) and therefore is discouraged in favor of a WordPress Element hierarchy whenever possible. Kudos @sirreal
506b6eb to
b924b01
Compare
|
@tyxla I made some changes you suggested, left a few for the next PR (#28169) and rebased. Since I removed this from jetpack preset again and put it behind feature flag in Calypso, to test you can now just:
Saving some fields depends on D19907-code |
tyxla
left a comment
There was a problem hiding this comment.
This looks good and works well 👍
Let's
considering that:
- This has dependencies that haven't been merged yet
- There are many bits that are a WIP and will be iterated on in following PRs.
|
@tyxla Automattic/jetpack#10531 should fix the PayPal execution error. |

Changes proposed in this Pull Request
gutenberg/block/simple-paymentsfeature flag 👍Testing instructions
Test in Jetpack
Test in Calypso
Manual checking
Apply these patches and cross-test the block in different editors:
Checklist:
attributes: you can test this by removing or mocking attributes in the editor's code-editor, saving the post while in code-editor and reloading the page again.