embeds-block.js
6 years ago
embeds-block.min.js
3 years ago
popup-trigger-block.js
6 years ago
popup-trigger-block.min.js
3 years ago
slidein-trigger-block.js
6 years ago
slidein-trigger-block.min.js
3 years ago
social-share-block.js
5 years ago
social-share-block.min.js
3 years ago
unsubscribe-block.js
3 years ago
unsubscribe-block.min.js
3 years ago
embeds-block.js
421 lines
| 1 | const { registerBlockType, createBlock } = wp.blocks, |
| 2 | { createElement: el, Component, RawHTML } = wp.element, |
| 3 | { string: shortcodeToString, next } = wp.shortcode, |
| 4 | |
| 5 | hustleEmbedIconEl = el( |
| 6 | 'svg', { |
| 7 | class: 'dashicon', viewBox: '0 0 24 24', width: 20, height: 20, xmlns: 'http://www.w3.org/2000/svg', preserveAspectRatio: 'xMidYMid meet', 'aria-hidden': 'true', role: 'img' |
| 8 | }, |
| 9 | el( |
| 10 | 'path', { |
| 11 | d: 'M.857 20.571h22.286c.232 0 .433.085.603.255.17.17.254.37.254.603v1.714a.824.824 0 0 1-.254.603.824.824 0 0 1-.603.254H.857a.824.824 0 0 1-.603-.254.824.824 0 0 1-.254-.603v-1.714c0-.233.085-.433.254-.603.17-.17.371-.255.603-.255zM1.714 6.857h20.572c.482 0 .888.165 1.218.496.33.33.496.736.496 1.218v6.858c0 .482-.165.888-.496 1.218-.33.33-.736.496-1.218.496H1.714c-.482 0-.888-.165-1.218-.496A1.657 1.657 0 0 1 0 15.43V8.57c0-.482.165-.888.496-1.218.33-.33.736-.496 1.218-.496zm19.429 2.286c-.482 0-.889.165-1.219.495-.33.33-.495.737-.495 1.22 0 .481.165.888.495 1.218.33.33.737.495 1.219.495.482 0 .888-.165 1.219-.495.33-.33.495-.737.495-1.219 0-.482-.165-.888-.495-1.219a1.657 1.657 0 0 0-1.22-.495zM.857 0h22.286c.232 0 .433.085.603.254.17.17.254.371.254.603v1.714a.824.824 0 0 1-.254.603.824.824 0 0 1-.603.255H.857a.824.824 0 0 1-.603-.255A.824.824 0 0 1 0 2.571V.857C0 .625.085.424.254.254.424.084.625 0 .857 0z' |
| 12 | } |
| 13 | ) |
| 14 | ); |
| 15 | |
| 16 | /** |
| 17 | * Block edit class |
| 18 | */ |
| 19 | class Hustle_Embed_BlockEdit extends Component { |
| 20 | |
| 21 | /** |
| 22 | * Class constructor |
| 23 | */ |
| 24 | constructor() { |
| 25 | super( ...arguments ); |
| 26 | |
| 27 | this.update_id = this.update_id.bind( this ); |
| 28 | this.update_css_class = this.update_css_class.bind( this ); |
| 29 | this.preview = this.preview.bind( this ); |
| 30 | |
| 31 | this.state = { |
| 32 | loading: false, // Set to true while loading preview markup |
| 33 | markup: '' // Preview markup |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Update shortcode id |
| 39 | */ |
| 40 | update_id( id ) { |
| 41 | this.props.setAttributes( { id } ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Update module id |
| 46 | */ |
| 47 | update_module_id( module_id ) { |
| 48 | this.props.setAttributes( { module_id } ); |
| 49 | } |
| 50 | |
| 51 | update_css_class( css_class ) { |
| 52 | this.props.setAttributes( { css_class } ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Preview module |
| 57 | */ |
| 58 | preview( attributes ) { |
| 59 | const { id, type } = attributes; |
| 60 | |
| 61 | // Check if we already process ajax request |
| 62 | if ( this.state.loading ) { |
| 63 | |
| 64 | // Ajax request in process, skip |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // Set loading to true |
| 69 | this.setState({ loading: true }); |
| 70 | |
| 71 | let ajax_url = ''; |
| 72 | |
| 73 | if ( id ) { |
| 74 | let module_type_query = 'undefined' === typeof type ? '' : '&type=' + type; |
| 75 | ajax_url = ajaxurl + '?action=hustle_render_module&_wpnonce=' + hustle_embed_data.nonce + '&shortcode_id=' + id + module_type_query; |
| 76 | |
| 77 | } else { |
| 78 | return; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | window.fetch( ajax_url ) |
| 83 | .then( response => response.json() ) |
| 84 | .then( data => { |
| 85 | |
| 86 | if ( data.success ) { |
| 87 | |
| 88 | const html = data.data.html, |
| 89 | style = data.data.style, |
| 90 | moduleData = data.data.data; |
| 91 | |
| 92 | this.setState({ |
| 93 | markup: html + style, |
| 94 | loading: false |
| 95 | }); |
| 96 | |
| 97 | const $module = jQuery( '.hustle_module_id_' + moduleData.module_id ); |
| 98 | HUI.maybeRenderRecaptcha( $module ); |
| 99 | HUI.nonSharingSimulation( $module ); |
| 100 | |
| 101 | // Update the shortcode id |
| 102 | if ( ! id ) { |
| 103 | this.update_id( moduleData.shortcode_id ); |
| 104 | } |
| 105 | |
| 106 | // Update the module id |
| 107 | this.update_module_id( moduleData.module_id ); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | }) |
| 112 | .catch( error => { |
| 113 | console.log( error ); |
| 114 | }) |
| 115 | |
| 116 | ; |
| 117 | |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * React method called when block is initialized |
| 122 | */ |
| 123 | componentDidMount() { |
| 124 | const { attributes } = this.props; |
| 125 | |
| 126 | let { id } = attributes; |
| 127 | // Check if module ID set |
| 128 | if ( ! id ) { |
| 129 | // Fallback |
| 130 | return; |
| 131 | |
| 132 | } else { |
| 133 | // Load preview |
| 134 | this.preview( attributes ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * React method called when block is updated |
| 140 | */ |
| 141 | componentDidUpdate( prevProps ) { |
| 142 | const { attributes } = this.props; |
| 143 | let { id } = attributes; |
| 144 | |
| 145 | if ( prevProps.attributes.id === id ) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if ( ! id ) { |
| 150 | |
| 151 | // Clear the preview markup |
| 152 | this.setState({ markup: '' }); |
| 153 | |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | // Load preview |
| 158 | this.preview( attributes ); |
| 159 | } |
| 160 | |
| 161 | open_settings( module_id ) { |
| 162 | |
| 163 | let url = hustle_embed_data.admin_url + '?page=hustle_embedded&id=' + module_id; |
| 164 | window.open( url ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Render |
| 169 | */ |
| 170 | render() { |
| 171 | |
| 172 | const |
| 173 | { loading, markup } = this.state, |
| 174 | { attributes, isSelected } = this.props, |
| 175 | { module_id, id, css_class } = attributes, |
| 176 | update_id = ( e ) => this.update_id( e.target.value ), |
| 177 | open_settings = ( e ) => this.open_settings( module_id ); |
| 178 | |
| 179 | let options = hustle_embed_data.modules; |
| 180 | |
| 181 | const controls = [ isSelected && el ( |
| 182 | wp.editor.InspectorControls, |
| 183 | { key: 'inspector' }, |
| 184 | |
| 185 | el ( |
| 186 | wp.components.PanelBody, |
| 187 | { |
| 188 | title: hustle_embed_data.l10n.module, |
| 189 | initialOpen: true |
| 190 | }, |
| 191 | el ( |
| 192 | wp.components.PanelRow, |
| 193 | null, |
| 194 | el ( wp.components.SelectControl, { |
| 195 | label: hustle_embed_data.l10n.name, |
| 196 | value: id, |
| 197 | options: options, |
| 198 | onChange: this.update_id |
| 199 | }) |
| 200 | ) |
| 201 | ), |
| 202 | el ( |
| 203 | wp.components.PanelBody, |
| 204 | { |
| 205 | title: hustle_embed_data.l10n.advanced, |
| 206 | initialOpen: true |
| 207 | }, |
| 208 | el ( |
| 209 | wp.components.PanelRow, |
| 210 | null, |
| 211 | el ( wp.components.TextControl, { |
| 212 | label: hustle_embed_data.l10n.additional_css_classes, |
| 213 | value: css_class, |
| 214 | onChange: this.update_css_class |
| 215 | }) |
| 216 | ) |
| 217 | ) |
| 218 | ), el ( wp.editor.BlockControls, |
| 219 | null, |
| 220 | !! id && el ( wp.components.Toolbar, |
| 221 | null, |
| 222 | el ( wp.components.IconButton, { |
| 223 | className: 'components-toolbar__control', |
| 224 | label: hustle_embed_data.l10n.customize_module, |
| 225 | icon: 'edit', |
| 226 | onClick: open_settings |
| 227 | }) |
| 228 | ) |
| 229 | )]; |
| 230 | |
| 231 | // If preview is being loaded, show spinner |
| 232 | if( loading ) { |
| 233 | return [ controls, el ( 'div', |
| 234 | { key: 'loading', className: 'wp-block-embed is-loading' }, |
| 235 | el ( wp.components.Spinner, null ), |
| 236 | el ( 'span', |
| 237 | null, |
| 238 | hustle_embed_data.l10n.rendering |
| 239 | ) |
| 240 | )]; |
| 241 | } |
| 242 | |
| 243 | // If we have preview markup display it |
| 244 | if( markup ) { |
| 245 | return [ |
| 246 | controls, el ( RawHTML, |
| 247 | null, |
| 248 | markup |
| 249 | )]; |
| 250 | } |
| 251 | |
| 252 | // Fallback, display the select |
| 253 | return [ controls, el ( |
| 254 | wp.components.Placeholder, |
| 255 | { |
| 256 | key: 'placeholder', |
| 257 | className: 'wp-block-embed', |
| 258 | instructions: hustle_embed_data.l10n.block_description, |
| 259 | icon: hustleEmbedIconEl, |
| 260 | label: hustle_embed_data.l10n.block_name |
| 261 | }, |
| 262 | el( |
| 263 | 'form', |
| 264 | null, |
| 265 | el( |
| 266 | 'select', |
| 267 | { value: id, onChange: update_id }, |
| 268 | options.map(row => el( |
| 269 | 'option', |
| 270 | { key: row.value, value: row.value }, |
| 271 | row.label |
| 272 | )) |
| 273 | ) |
| 274 | ) |
| 275 | )]; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | registerBlockType( 'hustle/embedded', { |
| 280 | title: hustle_embed_data.l10n.block_name, |
| 281 | description: hustle_embed_data.l10n.block_description, |
| 282 | icon: hustleEmbedIconEl, |
| 283 | category: 'hustle', |
| 284 | keywords: [ 'Hustle', hustle_embed_data.l10n.block_name ], |
| 285 | attributes: { |
| 286 | module_id: { |
| 287 | type: 'string' |
| 288 | }, |
| 289 | // 'shortcode_id' as identifier. |
| 290 | id: { |
| 291 | type: 'string' |
| 292 | }, |
| 293 | type: { |
| 294 | type: 'string' |
| 295 | }, |
| 296 | css_class: { |
| 297 | type: 'string' |
| 298 | } |
| 299 | }, |
| 300 | supports: { |
| 301 | customClassName: false, |
| 302 | className: false, |
| 303 | html: false, |
| 304 | }, |
| 305 | transforms: { |
| 306 | to: [ |
| 307 | { |
| 308 | type: 'block', |
| 309 | blocks: [ 'core/shortcode' ], |
| 310 | transform: ( { id, css_class } ) => { |
| 311 | let options = { |
| 312 | tag: hustle_popup_trigger_data.shortcode_tag, |
| 313 | attrs: { |
| 314 | id: ( id || '' ), |
| 315 | type: 'embedded', |
| 316 | css_class: ( css_class || '' ) |
| 317 | }, |
| 318 | type: 'single' |
| 319 | }, |
| 320 | text = shortcodeToString( options ); |
| 321 | |
| 322 | return createBlock( 'core/shortcode', { |
| 323 | text, |
| 324 | } ); |
| 325 | }, |
| 326 | }, |
| 327 | ], |
| 328 | |
| 329 | from: [ |
| 330 | //{ |
| 331 | // type: 'shortcode', |
| 332 | // // Use "isMatch" here to differentiate social shares from embeds by the shortcode's attribute. |
| 333 | // // Not possible atm as it's an open issue. https://github.com/WordPress/gutenberg/issues/10674 |
| 334 | // tag: [ hustle_embed_data.shortcode_tag ], |
| 335 | // attributes: { |
| 336 | // id: { |
| 337 | // type: 'string', |
| 338 | // shortcode: ( { named: { id } } ) => { |
| 339 | // return id; |
| 340 | // } |
| 341 | // }, |
| 342 | // type: { |
| 343 | // type: 'string', |
| 344 | // shortcode: ( { named: { type } } ) => { |
| 345 | // return type; |
| 346 | // } |
| 347 | // }, |
| 348 | // } |
| 349 | //}, |
| 350 | |
| 351 | // Legacy shortcode. |
| 352 | { |
| 353 | type: 'shortcode', |
| 354 | tag: [ 'wd_hustle_cc' ], |
| 355 | attributes: { |
| 356 | id: { |
| 357 | type: 'string', |
| 358 | shortcode: ( { named: { id } } ) => { |
| 359 | return id; |
| 360 | } |
| 361 | }, |
| 362 | type: { |
| 363 | type: 'string', |
| 364 | shortcode: ( { named: { type } } ) => { |
| 365 | return type; |
| 366 | } |
| 367 | }, |
| 368 | } |
| 369 | }, |
| 370 | |
| 371 | { |
| 372 | type: 'block', |
| 373 | blocks: [ 'core/shortcode' ], |
| 374 | isMatch( { text } ) { |
| 375 | |
| 376 | let shortcode_tag = next( 'wd_hustle', text ); |
| 377 | |
| 378 | // If it's not a Hustle shortcode, abort. |
| 379 | if ( 'undefined' === typeof shortcode_tag ) { |
| 380 | |
| 381 | shortcode_tag = next( 'wd_hustle_cc', text ); |
| 382 | if ( 'undefined' === typeof shortcode_tag ) { |
| 383 | return false; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | let { shortcode } = shortcode_tag; |
| 388 | if ( 'embedded' !== shortcode.attrs.named.type ) { |
| 389 | // The old shortcode didn't have the same attributes. Prevent a false positive. |
| 390 | if ( 'wd_hustle_cc' !== shortcode.attrs.tag ) { |
| 391 | return false; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | }, |
| 397 | transform( { text } ) { |
| 398 | |
| 399 | let { shortcode } = next( 'wd_hustle', text ); |
| 400 | if ( 'undefined' === shortcode ) { |
| 401 | shortcode = next( 'wd_hustle_cc', text ); |
| 402 | } |
| 403 | let { attrs: { named: { id, type, css_class } } } = shortcode; |
| 404 | |
| 405 | return createBlock( 'hustle/embedded', { |
| 406 | id, |
| 407 | css_class, |
| 408 | type |
| 409 | } ); |
| 410 | }, |
| 411 | }, |
| 412 | ] |
| 413 | }, |
| 414 | edit: Hustle_Embed_BlockEdit, |
| 415 | |
| 416 | save() { |
| 417 | |
| 418 | return null; |
| 419 | }, |
| 420 | |
| 421 | } ); |