| 1 |
( function () { |
| 2 |
|
| 3 |
const { addFilter } = wp.hooks; |
| 4 |
const { createElement, Fragment } = wp.element; |
| 5 |
const { PanelRow, Spinner } = wp.components; |
| 6 |
const { dispatch, select } = wp.data; |
| 7 |
|
| 8 |
if ( typeof gamipress_default_badges === 'undefined' || |
| 9 |
! gamipress_default_badges.images || |
| 10 |
! gamipress_default_badges.allowed_post_types ) { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
function gamipress_set_featured_image( imageKey ) { |
| 15 |
|
| 16 |
const postId = select( 'core/editor' ).getCurrentPostId(); |
| 17 |
|
| 18 |
const data = new window.FormData(); |
| 19 |
data.append( 'action', 'gamipress_badge_builder_set_featured_image' ); |
| 20 |
data.append( 'nonce', gamipress_default_badges.nonce ); |
| 21 |
data.append( 'post_id', postId ); |
| 22 |
data.append( 'image', imageKey ); |
| 23 |
|
| 24 |
fetch( ajaxurl, { |
| 25 |
method: 'POST', |
| 26 |
credentials: 'same-origin', |
| 27 |
body: data |
| 28 |
} ) |
| 29 |
.then( res => res.json() ) |
| 30 |
.then( response => { |
| 31 |
|
| 32 |
if ( ! response.success ) { |
| 33 |
console.error( response ); |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
dispatch( 'core/editor' ).editPost( { |
| 38 |
featured_media: response.data |
| 39 |
} ); |
| 40 |
|
| 41 |
} ); |
| 42 |
} |
| 43 |
|
| 44 |
addFilter( |
| 45 |
'editor.PostFeaturedImage', |
| 46 |
'gamipress/default-badges', |
| 47 |
( OriginalComponent ) => ( props ) => { |
| 48 |
|
| 49 |
const currentPostType = select( 'core/editor' ).getCurrentPostType(); |
| 50 |
|
| 51 |
// Check the allowed post types |
| 52 |
if ( ! gamipress_default_badges.allowed_post_types.includes( currentPostType ) ) { |
| 53 |
return createElement( OriginalComponent, props ); |
| 54 |
} |
| 55 |
|
| 56 |
return createElement( |
| 57 |
Fragment, |
| 58 |
{}, |
| 59 |
createElement( OriginalComponent, props ), |
| 60 |
createElement( |
| 61 |
'div', |
| 62 |
{ className: 'gamipress-badge-builder-default-badges-description-top' }, |
| 63 |
gamipress_default_badges.text_top |
| 64 |
), |
| 65 |
createElement( |
| 66 |
PanelRow, |
| 67 |
{}, |
| 68 |
createElement( |
| 69 |
'div', |
| 70 |
{ className: 'gamipress-badge-builder-default-badges-list' }, |
| 71 |
Object.keys( gamipress_default_badges.images ).map( function ( key ) { |
| 72 |
|
| 73 |
const image = gamipress_default_badges.images[ key ]; |
| 74 |
|
| 75 |
return createElement( 'img', { |
| 76 |
key: key, |
| 77 |
src: image.url, |
| 78 |
className: 'gamipress-badge-builder-default-badge-thumb', |
| 79 |
|
| 80 |
onClick: function () { |
| 81 |
gamipress_set_featured_image( key ); |
| 82 |
} |
| 83 |
} ); |
| 84 |
} ) |
| 85 |
) |
| 86 |
), |
| 87 |
createElement( |
| 88 |
'div', |
| 89 |
{ className: 'gamipress-badge-builder-default-badges-description-bottom' }, |
| 90 |
createElement( |
| 91 |
'a', |
| 92 |
{ href: gamipress_default_badges.text_bottom_link, target: '_blank' }, |
| 93 |
gamipress_default_badges.text_bottom |
| 94 |
), |
| 95 |
), |
| 96 |
); |
| 97 |
} |
| 98 |
); |
| 99 |
|
| 100 |
} )(); |
| 101 |
|