Last active
June 13, 2022 12:31
-
-
Save adamziel/d707d589d3da72f71a2ae47328c7abb4 to your computer and use it in GitHub Desktop.
useEntityRecord research
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
if ( ! wp.data.select( 'core' ).getEntityRecord( 'postType', 'attachment', id ) ) { | |
return el( BlockEdit, props ); | |
} | |
var imageMeta = wp.data.select( 'core' ).getEntityRecord( 'postType', 'attachment', id ).meta; | |
props.attributes.isc_image_source = imageMeta.isc_image_source; | |
props.attributes.isc_image_source_own = imageMeta.isc_image_source_own; | |
props.attributes.isc_image_source_url = imageMeta.isc_image_source_url; | |
props.attributes.isc_image_licence = imageMeta.isc_image_licence; | |
var panelFields = [el(wp.components.TextControl, { | |
label: __('Image Source', 'image-source-control-isc'), | |
value: props.attributes.isc_image_source, | |
key: 'advadsTextImageSource', | |
help: __('Include the image source here.', 'image-source-control-isc'), | |
onChange: function onChange(newValue) { | |
imageMeta.isc_image_source = newValue | |
wp.data.dispatch( 'core' ).editEntityRecord( 'postType', 'attachment', id, { meta: imageMeta } ); | |
wp.data.dispatch( 'core' ).saveEditedEntityRecord( 'postType', 'attachment', id ); | |
props.setAttributes({ | |
isc_image_source: newValue, | |
}); | |
}, | |
}), el(wp.components.CheckboxControl, { | |
label: __('Use standard source', 'image-source-control-isc'), | |
checked: props.attributes.isc_image_source_own, | |
key: 'advadsCheckboxImageOwn', | |
onChange: function (newValue) { | |
imageMeta.isc_image_source_own = newValue | |
wp.data.dispatch( 'core' ).editEntityRecord( 'postType', 'attachment', id, { meta: imageMeta } ); | |
wp.data.dispatch( 'core' ).saveEditedEntityRecord( 'postType', 'attachment', id ); | |
props.setAttributes({ | |
isc_image_source_own: newValue, | |
}); | |
} | |
}), el(wp.components.TextControl, { | |
label: __('Image Source URL', 'image-source-control-isc'), | |
value: props.attributes.isc_image_source_url, | |
key: 'advadsTextSourceUrl', | |
help: __('URL to link the source text to.', 'image-source-control-isc'), | |
onChange: function onChange(newValue) { | |
imageMeta.isc_image_source_url = newValue | |
wp.data.dispatch( 'core' ).editEntityRecord( 'postType', 'attachment', id, { meta: imageMeta } ); | |
wp.data.dispatch( 'core' ).saveEditedEntityRecord( 'postType', 'attachment', id ); | |
props.setAttributes({ | |
isc_image_source_url: newValue, | |
}); | |
}, | |
})]; |
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
// From Stripe Payment Forms by WP Simple Pay - Best Stripe Payments Plugin for WordPress | |
/** | |
* WordPress dependencies | |
*/ | |
import { useSelect, useDispatch } from '@wordpress/data'; | |
import { __ } from '@wordpress/i18n'; | |
const DEFAULT_SETTINGS = { | |
simpay_settings: [], | |
}; | |
/** | |
* Returns helpers to interact with WP Simple Pay settings. | |
* | |
* @return {Object} Settings helpers. | |
*/ | |
export function useSettings() { | |
const shape = [ 'root', 'site', undefined ]; | |
const { settings, rawSettings } = useSelect( | |
( select ) => { | |
const { getEditedEntityRecord, getRawEntityRecord } = select( | |
'core' | |
); | |
return { | |
settings: getEditedEntityRecord( ...shape ) || DEFAULT_SETTINGS, | |
rawSettings: getRawEntityRecord( ...shape ) || DEFAULT_SETTINGS, | |
}; | |
}, | |
[ shape ] | |
); | |
const { editEntityRecord, saveEditedEntityRecord } = useDispatch( 'core' ); | |
const { createSuccessNotice } = useDispatch( 'core/notices' ); | |
/** | |
* Edits settings. | |
* | |
* @param {Object} changedSettings simpay_settings to change. | |
*/ | |
function editSettings( changedSettings ) { | |
editEntityRecord( ...shape, { | |
simpay_settings: { | |
...settings.simpay_settings, | |
...changedSettings, | |
}, | |
} ); | |
} | |
/** | |
* Saves the edited settings and shows a success notice. | |
*/ | |
function saveSettings() { | |
saveEditedEntityRecord( ...shape, settings ); | |
createSuccessNotice( __( 'Settings saved', 'simple-pay' ), { | |
type: 'snackbar', | |
} ); | |
} | |
/** | |
* Discards edits. | |
*/ | |
function discardChanges() { | |
editSettings( rawSettings.simpay_settings ); | |
} | |
return { | |
settings: settings.simpay_settings, | |
rawSettings: rawSettings.simpay_settings, | |
discardChanges, | |
editSettings, | |
saveSettings, | |
}; | |
} |
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
a | |
.editEntityRecord( | |
"postType", | |
"attachment", | |
c, { | |
meta: { | |
wubtitle_status: n | |
} | |
} | |
), | |
a | |
.saveEditedEntityRecord( | |
"postType", | |
"attachment", | |
c | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment