| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 4 |
var __ = wp.i18n.__; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 7 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 8 |
var PanelBody = wp.components.PanelBody; |
| 9 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 10 |
var ToggleControl = wp.components.ToggleControl; |
| 11 |
var RangeControl = wp.components.RangeControl; |
| 12 |
var SelectControl = wp.components.SelectControl; |
| 13 |
var TextControl = wp.components.TextControl; |
| 14 |
var TextareaControl = wp.components.TextareaControl; |
| 15 |
|
| 16 |
var _ccnTC, _ccnTV; |
| 17 |
function _tc() { return _ccnTC || (_ccnTC = window.bkbgTypographyControl); } |
| 18 |
function _tv(obj, prefix) { var fn = _ccnTV || (_ccnTV = window.bkbgTypoCssVars); return fn ? fn(obj, prefix) : {}; } |
| 19 |
|
| 20 |
var positionOptions = [ |
| 21 |
{ label: __('Bottom Bar', 'blockenberg'), value: 'bottom' }, |
| 22 |
{ label: __('Top Bar', 'blockenberg'), value: 'top' }, |
| 23 |
{ label: __('Bottom-Left Corner', 'blockenberg'), value: 'bottom-left' }, |
| 24 |
{ label: __('Bottom-Right Corner', 'blockenberg'), value: 'bottom-right' } |
| 25 |
]; |
| 26 |
|
| 27 |
var layoutOptions = [ |
| 28 |
{ label: __('Bar (inline)', 'blockenberg'), value: 'bar' }, |
| 29 |
{ label: __('Popup (stacked)', 'blockenberg'), value: 'popup' } |
| 30 |
]; |
| 31 |
|
| 32 |
var animationOptions = [ |
| 33 |
{ label: __('Slide', 'blockenberg'), value: 'slide' }, |
| 34 |
{ label: __('Fade', 'blockenberg'), value: 'fade' }, |
| 35 |
{ label: __('None', 'blockenberg'), value: 'none' } |
| 36 |
]; |
| 37 |
|
| 38 |
registerBlockType('blockenberg/cookie-consent', { |
| 39 |
edit: function (props) { |
| 40 |
var a = props.attributes; |
| 41 |
var setAttributes = props.setAttributes; |
| 42 |
|
| 43 |
var wrapStyle = Object.assign({ |
| 44 |
'--bkbg-cc-bg': a.bgColor, |
| 45 |
'--bkbg-cc-text': a.textColor, |
| 46 |
'--bkbg-cc-accept-bg': a.acceptBg, |
| 47 |
'--bkbg-cc-accept-color': a.acceptColor, |
| 48 |
'--bkbg-cc-decline-bg': a.declineBg, |
| 49 |
'--bkbg-cc-decline-color': a.declineColor, |
| 50 |
'--bkbg-cc-decline-border': a.declineBorderColor, |
| 51 |
'--bkbg-cc-link-color': a.linkColor, |
| 52 |
'--bkbg-cc-radius': a.borderRadius + 'px', |
| 53 |
'--bkbg-cc-padding': a.padding + 'px', |
| 54 |
'--bkbg-cc-font-size': a.fontSize + 'px', |
| 55 |
'--bkbg-cc-max-width': a.maxWidth + 'px' |
| 56 |
}, _tv(a.typoMessage, '--bkbg-ccn-msg-')); |
| 57 |
|
| 58 |
var blockProps = useBlockProps({ |
| 59 |
className: 'bkbg-cc-preview-wrap' |
| 60 |
}); |
| 61 |
|
| 62 |
var inspector = el(InspectorControls, {}, |
| 63 |
|
| 64 |
el(PanelBody, { title: __('Message & Buttons', 'blockenberg'), initialOpen: true }, |
| 65 |
el(TextareaControl, { |
| 66 |
label: __('Consent Message', 'blockenberg'), |
| 67 |
value: a.message, |
| 68 |
rows: 4, |
| 69 |
onChange: function (v) { setAttributes({ message: v }); } |
| 70 |
}), |
| 71 |
el(TextControl, { |
| 72 |
label: __('Accept Button Text', 'blockenberg'), |
| 73 |
value: a.acceptLabel, |
| 74 |
onChange: function (v) { setAttributes({ acceptLabel: v }); } |
| 75 |
}), |
| 76 |
el(ToggleControl, { |
| 77 |
label: __('Show Decline Button', 'blockenberg'), |
| 78 |
checked: a.showDecline, |
| 79 |
__nextHasNoMarginBottom: true, |
| 80 |
onChange: function (v) { setAttributes({ showDecline: v }); } |
| 81 |
}), |
| 82 |
a.showDecline && el(TextControl, { |
| 83 |
label: __('Decline Button Text', 'blockenberg'), |
| 84 |
value: a.declineLabel, |
| 85 |
onChange: function (v) { setAttributes({ declineLabel: v }); } |
| 86 |
}), |
| 87 |
el(ToggleControl, { |
| 88 |
label: __('Show Customize Button', 'blockenberg'), |
| 89 |
help: __('Expands cookie category toggles.', 'blockenberg'), |
| 90 |
checked: a.showCustomize, |
| 91 |
__nextHasNoMarginBottom: true, |
| 92 |
onChange: function (v) { setAttributes({ showCustomize: v }); } |
| 93 |
}), |
| 94 |
a.showCustomize && el(TextControl, { |
| 95 |
label: __('Customize Button Text', 'blockenberg'), |
| 96 |
value: a.customizeLabel, |
| 97 |
onChange: function (v) { setAttributes({ customizeLabel: v }); } |
| 98 |
}), |
| 99 |
el('hr', {}), |
| 100 |
el(TextControl, { |
| 101 |
label: __('Privacy Policy Link Text', 'blockenberg'), |
| 102 |
value: a.privacyLabel, |
| 103 |
onChange: function (v) { setAttributes({ privacyLabel: v }); } |
| 104 |
}), |
| 105 |
el(TextControl, { |
| 106 |
label: __('Privacy Policy URL', 'blockenberg'), |
| 107 |
value: a.privacyUrl, |
| 108 |
type: 'url', |
| 109 |
onChange: function (v) { setAttributes({ privacyUrl: v }); } |
| 110 |
}) |
| 111 |
), |
| 112 |
|
| 113 |
el(PanelBody, { title: __('Cookie Categories', 'blockenberg'), initialOpen: false }, |
| 114 |
el('p', { style: { fontSize: '12px', color: '#6b7280', marginBottom: '12px' } }, |
| 115 |
__('These appear in the customize panel when enabled.', 'blockenberg') |
| 116 |
), |
| 117 |
el(ToggleControl, { |
| 118 |
label: __('Analytics Cookies', 'blockenberg'), |
| 119 |
checked: a.showAnalytics, |
| 120 |
__nextHasNoMarginBottom: true, |
| 121 |
onChange: function (v) { setAttributes({ showAnalytics: v }); } |
| 122 |
}), |
| 123 |
a.showAnalytics && el(TextControl, { |
| 124 |
label: __('Analytics Label', 'blockenberg'), |
| 125 |
value: a.analyticsLabel, |
| 126 |
onChange: function (v) { setAttributes({ analyticsLabel: v }); } |
| 127 |
}), |
| 128 |
el(ToggleControl, { |
| 129 |
label: __('Marketing Cookies', 'blockenberg'), |
| 130 |
checked: a.showMarketing, |
| 131 |
__nextHasNoMarginBottom: true, |
| 132 |
onChange: function (v) { setAttributes({ showMarketing: v }); } |
| 133 |
}), |
| 134 |
a.showMarketing && el(TextControl, { |
| 135 |
label: __('Marketing Label', 'blockenberg'), |
| 136 |
value: a.marketingLabel, |
| 137 |
onChange: function (v) { setAttributes({ marketingLabel: v }); } |
| 138 |
}), |
| 139 |
el(ToggleControl, { |
| 140 |
label: __('Functional Cookies', 'blockenberg'), |
| 141 |
checked: a.showFunctional, |
| 142 |
__nextHasNoMarginBottom: true, |
| 143 |
onChange: function (v) { setAttributes({ showFunctional: v }); } |
| 144 |
}), |
| 145 |
a.showFunctional && el(TextControl, { |
| 146 |
label: __('Functional Label', 'blockenberg'), |
| 147 |
value: a.functionalLabel, |
| 148 |
onChange: function (v) { setAttributes({ functionalLabel: v }); } |
| 149 |
}) |
| 150 |
), |
| 151 |
|
| 152 |
el(PanelBody, { title: __('Position & Layout', 'blockenberg'), initialOpen: false }, |
| 153 |
el(SelectControl, { |
| 154 |
label: __('Banner Position', 'blockenberg'), |
| 155 |
value: a.position, |
| 156 |
options: positionOptions, |
| 157 |
onChange: function (v) { setAttributes({ position: v }); } |
| 158 |
}), |
| 159 |
el(SelectControl, { |
| 160 |
label: __('Layout Style', 'blockenberg'), |
| 161 |
value: a.layout, |
| 162 |
options: layoutOptions, |
| 163 |
onChange: function (v) { setAttributes({ layout: v }); } |
| 164 |
}), |
| 165 |
el(SelectControl, { |
| 166 |
label: __('Entry Animation', 'blockenberg'), |
| 167 |
value: a.animation, |
| 168 |
options: animationOptions, |
| 169 |
onChange: function (v) { setAttributes({ animation: v }); } |
| 170 |
}), |
| 171 |
el(RangeControl, { |
| 172 |
label: __('Max Width (px)', 'blockenberg'), |
| 173 |
value: a.maxWidth, min: 320, max: 1400, |
| 174 |
onChange: function (v) { setAttributes({ maxWidth: v }); } |
| 175 |
}), |
| 176 |
el(RangeControl, { |
| 177 |
label: __('Border Radius (px)', 'blockenberg'), |
| 178 |
value: a.borderRadius, min: 0, max: 24, |
| 179 |
onChange: function (v) { setAttributes({ borderRadius: v }); } |
| 180 |
}), |
| 181 |
el(RangeControl, { |
| 182 |
label: __('Padding (px)', 'blockenberg'), |
| 183 |
value: a.padding, min: 8, max: 40, |
| 184 |
onChange: function (v) { setAttributes({ padding: v }); } |
| 185 |
}), |
| 186 |
), |
| 187 |
|
| 188 |
el(PanelBody, { title: __('Cookie Settings', 'blockenberg'), initialOpen: false }, |
| 189 |
el(TextControl, { |
| 190 |
label: __('Cookie Name', 'blockenberg'), |
| 191 |
help: __('Key used to store consent in localStorage.', 'blockenberg'), |
| 192 |
value: a.cookieName, |
| 193 |
onChange: function (v) { setAttributes({ cookieName: v }); } |
| 194 |
}), |
| 195 |
el(RangeControl, { |
| 196 |
label: __('Remember Consent (days)', 'blockenberg'), |
| 197 |
value: a.cookieDays, min: 1, max: 730, |
| 198 |
onChange: function (v) { setAttributes({ cookieDays: v }); } |
| 199 |
}) |
| 200 |
), |
| 201 |
|
| 202 |
|
| 203 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 204 |
_tc() && el(_tc(), { label: __('Message', 'blockenberg'), value: a.typoMessage, onChange: function (v) { setAttributes({ typoMessage: v }); } }) |
| 205 |
), |
| 206 |
el(PanelBody, { title: __('Colors', 'blockenberg'), initialOpen: false }, |
| 207 |
el(PanelColorSettings, { |
| 208 |
title: __('Banner Colors', 'blockenberg'), |
| 209 |
initialOpen: false, |
| 210 |
colorSettings: [ |
| 211 |
{ value: a.bgColor, onChange: function (c) { setAttributes({ bgColor: c || '#1f2937' }); }, label: __('Background', 'blockenberg') }, |
| 212 |
{ value: a.textColor, onChange: function (c) { setAttributes({ textColor: c || '#f9fafb' }); }, label: __('Text', 'blockenberg') }, |
| 213 |
{ value: a.linkColor, onChange: function (c) { setAttributes({ linkColor: c || '#93c5fd' }); }, label: __('Link', 'blockenberg') } |
| 214 |
] |
| 215 |
}), |
| 216 |
el(PanelColorSettings, { |
| 217 |
title: __('Button Colors', 'blockenberg'), |
| 218 |
initialOpen: false, |
| 219 |
colorSettings: [ |
| 220 |
{ value: a.acceptBg, onChange: function (c) { setAttributes({ acceptBg: c || '#2563eb' }); }, label: __('Accept Background', 'blockenberg') }, |
| 221 |
{ value: a.acceptColor, onChange: function (c) { setAttributes({ acceptColor: c || '#ffffff' }); }, label: __('Accept Text', 'blockenberg') }, |
| 222 |
{ value: a.declineColor, onChange: function (c) { setAttributes({ declineColor: c || '#9ca3af' }); }, label: __('Decline Text', 'blockenberg') }, |
| 223 |
{ value: a.declineBorderColor, onChange: function (c) { setAttributes({ declineBorderColor: c || '#4b5563' }); }, label: __('Decline Border', 'blockenberg') } |
| 224 |
] |
| 225 |
}) |
| 226 |
) |
| 227 |
); |
| 228 |
|
| 229 |
// Editor preview — shows a compact inline preview |
| 230 |
return el(Fragment, {}, |
| 231 |
inspector, |
| 232 |
el('div', blockProps, |
| 233 |
el('div', { |
| 234 |
className: 'bkbg-cc-editor-notice', |
| 235 |
style: { |
| 236 |
background: '#fef3c7', |
| 237 |
border: '1px solid #fcd34d', |
| 238 |
borderRadius: '6px', |
| 239 |
padding: '10px 14px', |
| 240 |
fontSize: '13px', |
| 241 |
color: '#92400e' |
| 242 |
} |
| 243 |
}, |
| 244 |
el('strong', {}, __('Cookie Consent Banner', 'blockenberg')), |
| 245 |
el('br', {}), |
| 246 |
__('Position: ', 'blockenberg'), el('code', {}, a.position), ' · ', |
| 247 |
__('Layout: ', 'blockenberg'), el('code', {}, a.layout), |
| 248 |
el('br', {}), |
| 249 |
__('This banner renders as a fixed overlay on the live site.', 'blockenberg') |
| 250 |
), |
| 251 |
// Live preview |
| 252 |
el('div', { |
| 253 |
className: 'bkbg-cc-banner bkbg-cc-preview', |
| 254 |
style: wrapStyle, |
| 255 |
'data-layout': a.layout, |
| 256 |
'data-position': a.position |
| 257 |
}, |
| 258 |
el('div', { className: 'bkbg-cc-inner' }, |
| 259 |
el('div', { className: 'bkbg-cc-text' }, |
| 260 |
el('p', { className: 'bkbg-cc-message' }, a.message), |
| 261 |
a.privacyUrl && el('a', { |
| 262 |
href: '#', |
| 263 |
className: 'bkbg-cc-privacy-link' |
| 264 |
}, a.privacyLabel) |
| 265 |
), |
| 266 |
el('div', { className: 'bkbg-cc-actions' }, |
| 267 |
el('button', { className: 'bkbg-cc-btn bkbg-cc-accept-btn' }, a.acceptLabel), |
| 268 |
a.showDecline && el('button', { className: 'bkbg-cc-btn bkbg-cc-decline-btn' }, a.declineLabel), |
| 269 |
a.showCustomize && el('button', { className: 'bkbg-cc-btn bkbg-cc-customize-btn' }, a.customizeLabel) |
| 270 |
) |
| 271 |
) |
| 272 |
) |
| 273 |
) |
| 274 |
); |
| 275 |
}, |
| 276 |
|
| 277 |
save: function (props) { |
| 278 |
var a = props.attributes; |
| 279 |
|
| 280 |
var typoVars = _tv(a.typoMessage, '--bkbg-ccn-msg-'); |
| 281 |
var typoEntries = Object.keys(typoVars).map(function(k) { return k + ':' + typoVars[k]; }); |
| 282 |
|
| 283 |
var wrapStyle = [ |
| 284 |
'--bkbg-cc-bg:' + a.bgColor, |
| 285 |
'--bkbg-cc-text:' + a.textColor, |
| 286 |
'--bkbg-cc-accept-bg:' + a.acceptBg, |
| 287 |
'--bkbg-cc-accept-color:' + a.acceptColor, |
| 288 |
'--bkbg-cc-decline-bg:' + a.declineBg, |
| 289 |
'--bkbg-cc-decline-color:' + a.declineColor, |
| 290 |
'--bkbg-cc-decline-border:' + a.declineBorderColor, |
| 291 |
'--bkbg-cc-link-color:' + a.linkColor, |
| 292 |
'--bkbg-cc-radius:' + a.borderRadius + 'px', |
| 293 |
'--bkbg-cc-padding:' + a.padding + 'px', |
| 294 |
'--bkbg-cc-font-size:' + a.fontSize + 'px', |
| 295 |
'--bkbg-cc-max-width:' + a.maxWidth + 'px' |
| 296 |
].concat(typoEntries).join(';'); |
| 297 |
|
| 298 |
var blockProps = wp.blockEditor.useBlockProps.save({ |
| 299 |
className: 'bkbg-cc-banner', |
| 300 |
style: wrapStyle, |
| 301 |
'data-position': a.position, |
| 302 |
'data-layout': a.layout, |
| 303 |
'data-animation': a.animation, |
| 304 |
'data-cookie-name': a.cookieName, |
| 305 |
'data-cookie-days': a.cookieDays, |
| 306 |
'data-show-analytics': a.showAnalytics ? '1' : '0', |
| 307 |
'data-analytics-label': a.analyticsLabel, |
| 308 |
'data-show-marketing': a.showMarketing ? '1' : '0', |
| 309 |
'data-marketing-label': a.marketingLabel, |
| 310 |
'data-show-functional': a.showFunctional ? '1' : '0', |
| 311 |
'data-functional-label': a.functionalLabel |
| 312 |
}); |
| 313 |
|
| 314 |
return wp.element.createElement('div', blockProps, |
| 315 |
wp.element.createElement('div', { className: 'bkbg-cc-inner' }, |
| 316 |
wp.element.createElement('div', { className: 'bkbg-cc-text' }, |
| 317 |
wp.element.createElement('p', { className: 'bkbg-cc-message' }, a.message), |
| 318 |
a.privacyUrl && wp.element.createElement('a', { |
| 319 |
href: a.privacyUrl, |
| 320 |
className: 'bkbg-cc-privacy-link', |
| 321 |
target: '_blank', |
| 322 |
rel: 'noopener noreferrer' |
| 323 |
}, a.privacyLabel) |
| 324 |
), |
| 325 |
wp.element.createElement('div', { className: 'bkbg-cc-actions' }, |
| 326 |
wp.element.createElement('button', { |
| 327 |
className: 'bkbg-cc-btn bkbg-cc-accept-btn', |
| 328 |
'data-bkbg-cc-accept': 'true', |
| 329 |
type: 'button' |
| 330 |
}, a.acceptLabel), |
| 331 |
a.showDecline && wp.element.createElement('button', { |
| 332 |
className: 'bkbg-cc-btn bkbg-cc-decline-btn', |
| 333 |
'data-bkbg-cc-decline': 'true', |
| 334 |
type: 'button' |
| 335 |
}, a.declineLabel), |
| 336 |
a.showCustomize && wp.element.createElement('button', { |
| 337 |
className: 'bkbg-cc-btn bkbg-cc-customize-btn', |
| 338 |
'data-bkbg-cc-customize': 'true', |
| 339 |
type: 'button' |
| 340 |
}, a.customizeLabel) |
| 341 |
), |
| 342 |
// Categories panel (hidden until customize clicked) |
| 343 |
a.showCustomize && wp.element.createElement('div', { |
| 344 |
className: 'bkbg-cc-categories', |
| 345 |
hidden: true |
| 346 |
}, |
| 347 |
a.showAnalytics && wp.element.createElement('label', { className: 'bkbg-cc-category' }, |
| 348 |
wp.element.createElement('input', { type: 'checkbox', 'data-bkbg-cc-category': 'analytics' }), |
| 349 |
a.analyticsLabel |
| 350 |
), |
| 351 |
a.showMarketing && wp.element.createElement('label', { className: 'bkbg-cc-category' }, |
| 352 |
wp.element.createElement('input', { type: 'checkbox', 'data-bkbg-cc-category': 'marketing' }), |
| 353 |
a.marketingLabel |
| 354 |
), |
| 355 |
a.showFunctional && wp.element.createElement('label', { className: 'bkbg-cc-category' }, |
| 356 |
wp.element.createElement('input', { type: 'checkbox', 'data-bkbg-cc-category': 'functional' }), |
| 357 |
a.functionalLabel |
| 358 |
), |
| 359 |
wp.element.createElement('button', { |
| 360 |
className: 'bkbg-cc-btn bkbg-cc-save-btn', |
| 361 |
'data-bkbg-cc-save': 'true', |
| 362 |
type: 'button' |
| 363 |
}, __('Save Preferences', 'blockenberg')) |
| 364 |
) |
| 365 |
) |
| 366 |
); |
| 367 |
} |
| 368 |
}); |
| 369 |
}() ); |
| 370 |
|