| 1 |
/** |
| 2 |
* WordPress Dependencies |
| 3 |
*/ |
| 4 |
const { __ } = wp.i18n; |
| 5 |
const { addFilter } = wp.hooks; |
| 6 |
const { Fragment } = wp.element; |
| 7 |
const { compose, createHigherOrderComponent } = wp.compose; |
| 8 |
|
| 9 |
function addAttributes(settings) { |
| 10 |
// Add custom selector/id |
| 11 |
if (typeof settings.attributes !== "undefined") { |
| 12 |
settings.attributes = { |
| 13 |
...settings.attributes, |
| 14 |
eb: { type: "object" } |
| 15 |
}; |
| 16 |
} |
| 17 |
|
| 18 |
return settings; |
| 19 |
} |
| 20 |
|
| 21 |
const withAttributes = createHigherOrderComponent(BlockEdit => { |
| 22 |
return props => { |
| 23 |
const { attributes, setAttributes } = props; |
| 24 |
|
| 25 |
if (typeof attributes.eb === "undefined") { |
| 26 |
attributes.eb = []; |
| 27 |
} |
| 28 |
|
| 29 |
//add unique selector |
| 30 |
if (typeof attributes.eb.id === "undefined") { |
| 31 |
let d = new Date(); |
| 32 |
|
| 33 |
if ( |
| 34 |
typeof attributes.eb !== "undefined" && |
| 35 |
typeof attributes.eb.id !== "undefined" |
| 36 |
) { |
| 37 |
delete attributes.eb.id; |
| 38 |
} |
| 39 |
|
| 40 |
const eb = Object.assign( |
| 41 |
{ |
| 42 |
id: |
| 43 |
"" + |
| 44 |
d.getMonth() + |
| 45 |
d.getDate() + |
| 46 |
d.getHours() + |
| 47 |
d.getMinutes() + |
| 48 |
d.getSeconds() + |
| 49 |
d.getMilliseconds() |
| 50 |
}, |
| 51 |
attributes.eb |
| 52 |
); |
| 53 |
setAttributes({ eb }); |
| 54 |
} |
| 55 |
|
| 56 |
return ( |
| 57 |
<Fragment> |
| 58 |
<BlockEdit {...props} /> |
| 59 |
</Fragment> |
| 60 |
); |
| 61 |
}; |
| 62 |
}, "withAttributes"); |
| 63 |
|
| 64 |
addFilter("blocks.registerBlockType", "eb/button", addAttributes); |
| 65 |
|
| 66 |
addFilter("editor.BlockEdit", "eb/button", withAttributes); |
| 67 |
|