sidebar.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * "Post Meta" block-editor sidebar — no build step, uses the global wp.* runtime.
  3. * Binds three post-meta fields edited via core/editor: media (og:video),
  4. * featured_image_url (og:image), and meta_desc (meta description).
  5. */
  6. ( function ( wp ) {
  7. var el = wp.element.createElement;
  8. var Fragment = wp.element.Fragment;
  9. var __ = wp.i18n.__;
  10. var PluginSidebar = wp.editor.PluginSidebar;
  11. var PluginSidebarMoreMenuItem = wp.editor.PluginSidebarMoreMenuItem;
  12. var PanelBody = wp.components.PanelBody;
  13. var TextControl = wp.components.TextControl;
  14. var TextareaControl = wp.components.TextareaControl;
  15. var useSelect = wp.data.useSelect;
  16. var useDispatch = wp.data.useDispatch;
  17. function MetaField( props ) {
  18. var meta = useSelect( function ( select ) {
  19. return select( 'core/editor' ).getEditedPostAttribute( 'meta' );
  20. }, [] );
  21. var editPost = useDispatch( 'core/editor' ).editPost;
  22. var Control = props.multiline ? TextareaControl : TextControl;
  23. return el(
  24. PanelBody,
  25. { title: props.title, initialOpen: true },
  26. el( Control, {
  27. label: props.label,
  28. help: props.help,
  29. value: ( meta && meta[ props.metaKey ] ) || '',
  30. onChange: function ( value ) {
  31. var next = {};
  32. next[ props.metaKey ] = value;
  33. editPost( { meta: next } );
  34. },
  35. __nextHasNoMarginBottom: true,
  36. } )
  37. );
  38. }
  39. wp.plugins.registerPlugin( 'dw-sidebar', {
  40. icon: 'welcome-widgets-menus',
  41. render: function () {
  42. return el(
  43. Fragment,
  44. {},
  45. el(
  46. PluginSidebarMoreMenuItem,
  47. { target: 'dw-sidebar' },
  48. __( 'Meta Options', 'dw-guten' )
  49. ),
  50. el(
  51. PluginSidebar,
  52. { name: 'dw-sidebar', title: __( 'Post Meta', 'dw-guten' ) },
  53. el( MetaField, {
  54. metaKey: 'media',
  55. title: __( 'Media URL (og:video)', 'dw-guten' ),
  56. label: __( 'og:video', 'dw-guten' ),
  57. help: 'postmeta:media — absolute URL to an mp4 for OpenGraph og:video',
  58. } ),
  59. el( MetaField, {
  60. metaKey: 'featured_image_url',
  61. title: __( 'Image URL (og:image)', 'dw-guten' ),
  62. label: __( 'og:image', 'dw-guten' ),
  63. help: 'postmeta:featured_image_url — absolute URL to an image',
  64. } ),
  65. el( MetaField, {
  66. metaKey: 'meta_desc',
  67. title: __( 'Meta Excerpt', 'dw-guten' ),
  68. label: __( 'meta description', 'dw-guten' ),
  69. help: 'postmeta:meta_desc — alternative to the post excerpt',
  70. multiline: true,
  71. } )
  72. )
  73. );
  74. },
  75. } );
  76. } )( window.wp );