| 1 |
( function( editor, components, i18n, element, $ ) { |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
const __ = i18n.__; |
| 5 |
const el = element.createElement; |
| 6 |
const compose = wp.compose.compose; |
| 7 |
const registerPlugin = wp.plugins.registerPlugin; |
| 8 |
|
| 9 |
const { |
| 10 |
Fragment, |
| 11 |
Component |
| 12 |
} = element; |
| 13 |
|
| 14 |
|
| 15 |
const { |
| 16 |
TextareaControl, |
| 17 |
PanelBody |
| 18 |
} = components; |
| 19 |
|
| 20 |
const { |
| 21 |
dispatch, |
| 22 |
withSelect, |
| 23 |
withDispatch |
| 24 |
} = wp.data; |
| 25 |
|
| 26 |
const { |
| 27 |
PluginSidebar, |
| 28 |
PluginSidebarMoreMenuItem |
| 29 |
} = wp.editPost; |
| 30 |
|
| 31 |
const Icon = el( 'svg', { |
| 32 |
height: '20px', |
| 33 |
width: '20px', |
| 34 |
viewBox: '0 0 17.39 17.39' |
| 35 |
}, el ( 'polygon', { |
| 36 |
points: '14.77 11.19 17.3 8.65 14.77 6.12 14.77 2.53 11.19 2.53 8.65 0 6.12 2.53 2.53 2.53 2.53 6.12 0 8.65 2.53 11.19 2.53 14.77 6.12 14.77 8.65 17.3 11.19 14.77 14.77 14.77 14.77 11.19' |
| 37 |
} ) |
| 38 |
); |
| 39 |
|
| 40 |
class LoftLoaderPlugin extends Component { |
| 41 |
constructor() { |
| 42 |
super( ...arguments ); |
| 43 |
|
| 44 |
this.state = this.props.meta; |
| 45 |
} |
| 46 |
onSaveMeta( newValue ) { |
| 47 |
dispatch( 'core/editor' ).editPost( { meta: { 'loftload-saving': 1 } } ); |
| 48 |
this.setState( newValue ); |
| 49 |
} |
| 50 |
static getDerivedStateFromProps( nextProps, state ) { |
| 51 |
if ( ( nextProps.isPublishing || nextProps.isSaving ) && ! nextProps.isAutoSaving ) { |
| 52 |
wp.apiRequest( { path: '/loftloader/v2/update-meta?id=' + nextProps.postId, method: 'POST', data: state } ).then( |
| 53 |
( data ) => { |
| 54 |
return data; |
| 55 |
}, ( err ) => { |
| 56 |
return err; |
| 57 |
} |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
render() { |
| 62 |
return el( Fragment, {}, |
| 63 |
el( PluginSidebarMoreMenuItem, { target: 'loftloader-any-page' }, __( 'LoftLoader Any Page Shortcode' ) ), |
| 64 |
el( PluginSidebar, { name: 'loftloader-any-page', title: __( 'LoftLoader Any Page Shortcode' ) }, |
| 65 |
el( PanelBody, { |
| 66 |
className: 'loftloader-any-page-sidebar', |
| 67 |
initialOpen: true |
| 68 |
}, |
| 69 |
el( TextareaControl, { |
| 70 |
label: __( 'Paste LoftLoader shortcode into the box below' ), |
| 71 |
value: this.state.loftloader_page_shortcode, |
| 72 |
onChange: ( value ) => { |
| 73 |
this.onSaveMeta( { loftloader_page_shortcode: value } ); |
| 74 |
} |
| 75 |
} ) |
| 76 |
), |
| 77 |
el( 'input', { |
| 78 |
type: 'hidden', |
| 79 |
name: 'loftloader_gutenberg_enabled', |
| 80 |
value: 'on' |
| 81 |
} ) |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Fetch the post meta. |
| 88 |
const applyWithSelect = withSelect( ( select, { forceIsSaving } ) => { |
| 89 |
const { |
| 90 |
getEditedPostAttribute, |
| 91 |
getCurrentPostId, |
| 92 |
isSavingPost, |
| 93 |
isPublishingPost, |
| 94 |
isAutosavingPost, |
| 95 |
} = select( 'core/editor' ); |
| 96 |
|
| 97 |
return { |
| 98 |
meta: getEditedPostAttribute( 'meta' ), |
| 99 |
postId: getCurrentPostId(), |
| 100 |
isSaving: forceIsSaving || isSavingPost(), |
| 101 |
isAutoSaving: isAutosavingPost(), |
| 102 |
isPublishing: isPublishingPost(), |
| 103 |
}; |
| 104 |
} ); |
| 105 |
|
| 106 |
const render = compose( [ |
| 107 |
applyWithSelect |
| 108 |
] )( LoftLoaderPlugin ); |
| 109 |
|
| 110 |
registerPlugin( 'loftloader-any-page', { |
| 111 |
icon: Icon, |
| 112 |
render |
| 113 |
} ); |
| 114 |
} )( |
| 115 |
window.wp.editor, |
| 116 |
window.wp.components, |
| 117 |
window.wp.i18n, |
| 118 |
window.wp.element, |
| 119 |
jQuery |
| 120 |
); |