migrate.gateways.settings.js
115 lines
| 1 | const getMeta = () => { |
| 2 | const { select } = wp.data; |
| 3 | return select( 'core/editor' ).getEditedPostAttribute( 'meta' ); |
| 4 | }; |
| 5 | |
| 6 | const editMeta = ( name, objectValue ) => { |
| 7 | const { dispatch } = wp.data; |
| 8 | |
| 9 | const { editPost } = dispatch( 'core/editor' ); |
| 10 | const formBuilderMeta = getMeta(); |
| 11 | |
| 12 | editPost( { |
| 13 | meta: ( |
| 14 | { |
| 15 | ...formBuilderMeta, |
| 16 | [ name ]: JSON.stringify( objectValue ), |
| 17 | } |
| 18 | ), |
| 19 | } ); |
| 20 | }; |
| 21 | |
| 22 | const getActiveActions = actions => { |
| 23 | const response = []; |
| 24 | |
| 25 | for ( const [ action_id, { active = false } ] of Object.entries( actions ) ) { |
| 26 | if ( ! active ) { |
| 27 | continue; |
| 28 | } |
| 29 | response.push( + action_id ); |
| 30 | } |
| 31 | |
| 32 | return response; |
| 33 | }; |
| 34 | |
| 35 | const getGatewaysActions = () => { |
| 36 | const formBuilderMeta = getMeta(); |
| 37 | |
| 38 | let gateways = {}, |
| 39 | actions = []; |
| 40 | |
| 41 | try { |
| 42 | gateways = JSON.parse( formBuilderMeta[ '_jf_gateways' ] ); |
| 43 | } catch ( error ) { |
| 44 | return []; |
| 45 | } |
| 46 | |
| 47 | if ( 1 === gateways.last_migrate ) { |
| 48 | throw 'migrated'; |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | actions = JSON.parse( formBuilderMeta[ '_jf_actions' ] ); |
| 53 | } catch ( error ) { |
| 54 | return [ gateways ]; |
| 55 | } |
| 56 | |
| 57 | return [ gateways, actions ]; |
| 58 | }; |
| 59 | |
| 60 | const migrate = ( gateways, actions ) => { |
| 61 | const on_success = getActiveActions( gateways[ 'notifications_success' ] ?? {} ); |
| 62 | const on_failed = getActiveActions( gateways[ 'notifications_failed' ] ?? {} ); |
| 63 | const on_before = getActiveActions( gateways[ 'notifications_before' ] ?? {} ); |
| 64 | const use_redirect = gateways[ 'use_success_redirect' ] ?? false; |
| 65 | |
| 66 | let has_redirect = false; |
| 67 | |
| 68 | if ( ! on_success.length && ! on_failed.length && ! on_before.length && ! use_redirect ) { |
| 69 | throw 'nothing_to_migrate'; |
| 70 | } |
| 71 | |
| 72 | return actions.map( action => { |
| 73 | action.events = action.events ?? []; |
| 74 | |
| 75 | if ( on_success.includes( action.id ) ) { |
| 76 | action.events.push( 'GATEWAY.SUCCESS' ); |
| 77 | } |
| 78 | if ( on_failed.includes( action.id ) ) { |
| 79 | action.events.push( 'GATEWAY.FAILED' ); |
| 80 | } |
| 81 | if ( on_before.includes( action.id ) ) { |
| 82 | action.events.push( 'DEFAULT.PROCESS' ); |
| 83 | } |
| 84 | if ( use_redirect && ! has_redirect && 'redirect_to_page' === action.type ) { |
| 85 | action.events.push( 'GATEWAY.SUCCESS' ); |
| 86 | has_redirect = true; |
| 87 | } |
| 88 | |
| 89 | return action; |
| 90 | } ); |
| 91 | }; |
| 92 | |
| 93 | const runEvent = () => { |
| 94 | let gateways = {}, actions = []; |
| 95 | |
| 96 | try { |
| 97 | [ gateways = {}, actions = [] ] = getGatewaysActions(); |
| 98 | } catch ( error ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | gateways.last_migrate = 1; |
| 103 | editMeta( '_jf_gateways', gateways ); |
| 104 | |
| 105 | const withConditions = []; |
| 106 | try { |
| 107 | withConditions.push( ...migrate( gateways, actions ) ); |
| 108 | } catch ( error ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | editMeta( '_jf_actions', withConditions ); |
| 113 | }; |
| 114 | |
| 115 | wp.domReady( () => setTimeout( runEvent, 0 ) ); |