/** * External dependencies */ import React from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; /** * WordPress dependencies */ import domReady from '@wordpress/dom-ready'; const { __, sprintf } = wp.i18n; /** * Internal dependencies */ import HBAPIFetch from '../../api'; import BorderFrame from '../../components/border-frame'; import Box from '../../components/sui-box'; import Button from '../../components/sui-button'; import Checkbox from '../../components/sui-checkbox'; import Icon from '../../components/sui-icon'; import Notice from '../../components/sui-notice'; import Select from '../../components/sui-select'; import SettingsRow from '../../components/sui-box-settings/row'; import Toggle from '../../components/sui-toggle'; import Tooltip from '../../components/sui-tooltip'; import Tag from '../../components/sui-tag'; import {createInterpolateElement} from "@wordpress/element"; /** * Integrations component. * * @since 3.0.0 */ class Integrations extends React.Component { /** * Component constructor. * * @param {Object} props */ constructor( props ) { super( props ); this.state = { api: new HBAPIFetch(), allowModify: this.props.wphbData.modify, links: this.props.wphbData.links, loading: true, cf: this.props.wphbData.module.cloudflare, apo: this.props.wphbData.module.apo, zones: [], selectedZone: false, // Track zone select. }; this.disconnectCloudflare = this.disconnectCloudflare.bind( this ); this.reCheckStatus = this.reCheckStatus.bind( this ); this.reCheckAPOStatus = this.reCheckAPOStatus.bind( this ); this.clearCache = this.clearCache.bind( this ); this.handleAPOToggleChange = this.handleAPOToggleChange.bind( this ); this.handleDevChkbxChange = this.handleDevChkbxChange.bind( this ); this.handleZoneChange = this.handleZoneChange.bind( this ); this.saveSelectedZone = this.saveSelectedZone.bind( this ); } /** * Invoked immediately after a component is mounted. */ componentDidMount() { this.state.api .post( 'cloudflare_status' ) .then( ( response ) => { this.setState( { loading: false, cf: response.cloudflare, apo: response.apo, } ); } ) .catch( ( error ) => window.console.log( error ) ); } /** * Open the Connect Cloudflare modal. */ openConnectModal() { window.SUI.openModal( 'cloudflare-connect', 'wphb-box-integrations', 'cloudflare-email', false, false ); } /** * Re-check status button click. */ reCheckStatus() { this.setState( { loading: true } ); this.state.api .post( 'cloudflare_zones' ) .then( ( response ) => { // Error. if ( 'undefined' !== typeof response.message ) { window.WPHB_Admin.notices.show( response.message, 'error' ); } // Got a list of zones. let zones = []; if ( 'undefined' !== typeof response.zones ) { zones = response.zones; } this.setState( { loading: false, zones, } ); } ) .catch( ( error ) => window.console.log( error ) ); } /** * Re-check APO status (used to verify payment). */ reCheckAPOStatus() { this.setState( { loading: true } ); this.state.api .post( 'cloudflare_apo_status' ) .then( ( response ) => { this.setState( { loading: false, cf: response.cloudflare, apo: response.apo, } ); } ) .catch( ( error ) => window.console.log( error ) ); } /** * Disconnect Cloudflare. */ disconnectCloudflare() { this.setState( { loading: true } ); this.state.api .post( 'cloudflare_disconnect' ) .then( ( response ) => { this.setState( { loading: false, cf: response.cloudflare, apo: response.apo, } ); window.wphbMixPanel.disableFeature( 'Cloudflare_integration' ); window.WPHB_Admin.notices.show( __( 'Cloudflare was disconnected successfully.', 'wphb' ) ); } ) .catch( ( error ) => window.console.log( error ) ); } /** * Clear Cloudflare cache. */ clearCache() { this.setState( { loading: true } ); this.state.api .post( 'cloudflare_clear_cache' ) .then( () => { this.setState( { loading: false, } ); window.WPHB_Admin.notices.show( __( 'Cloudflare cache successfully purged. Please wait 30 seconds for the purge to complete.', 'wphb' ) ); } ) .catch( ( error ) => window.console.log( error ) ); } /** * Handle toggling APO. * * @param {Object} e */ handleAPOToggleChange( e ) { this.setState( { loading: true } ); let msg = __( 'Automatic Platform Optimization is disabled.', 'wphb' ); if ( e.target.checked ) { msg = __( 'Automatic Platform Optimization is enabled.', 'wphb' ); } this.state.api .post( 'cloudflare_toggle_apo', e.target.checked ) .then( ( response ) => { this.setState( { loading: false, apo: response, } ); window.WPHB_Admin.notices.show( msg ); } ) .catch( ( error ) => window.console.log( error ) ); } /** * Handle toggling APO cache by device type. * * @param {Object} e */ handleDevChkbxChange( e ) { this.setState( { loading: true } ); this.state.api .post( 'cloudflare_toggle_device_cache', e.target.checked ) .then( ( response ) => { this.setState( { loading: false, apo: response, } ); window.WPHB_Admin.notices.show( __( 'Settings updated.', 'wphb' ) ); } ) .catch( ( error ) => window.console.log( error ) ); } /** * Handle zone select. */ handleZoneChange() { this.setState( { selectedZone: jQuery( '#cloudflare-zone-select' ) .find( ':selected' ) .text(), } ); } /** * Save selected zone. */ saveSelectedZone() { if ( false === this.state.selectedZone ) { return; } this.setState( { loading: true } ); this.state.api .post( 'cloudflare_save_zone', this.state.selectedZone ) .then( ( response ) => { this.setState( { loading: false, cf: response.cloudflare, apo: response.apo, zones: [], selectedZone: false, } ); window.WPHB_Admin.notices.show( __( 'Cloudflare was connected successfully.', 'wphb' ) ); } ) .catch( ( error ) => window.console.log( error ) ); } /** * Render accordion header title. * * @return {JSX.Element} Title element. */ renderHeaderTitle() { const images = this.state.links.wphbDirUrl + 'admin/assets/image/integrations/'; const tooltip = ( ); return (
{ { __( 'Cloudflare', 'wphb' ) } { this.state.cf.connected && this.state.cf.zone && tooltip }
); } /** * Render status notice. * * @return {JSX.Element} Notice. */ renderStatusNotice() { let statusNotice; if ( this.state.cf.connected && this.state.cf.zone ) { statusNotice = ( ); } else if ( this.state.cf.connected && ! this.state.cf.zone ) { statusNotice = ( ); } else if ( this.state.cf.dnsSet ) { statusNotice = ( ); } else { statusNotice = ( ); } return statusNotice; } /** * Render zone dropdown. * * @return {JSX.Element} Zone dropdown. */ renderZoneDropdown() { const zones = this.state.zones.map( ( el ) => { return [ el.value, el.label ]; } ); // Fix for not working placeholder (pre-selected first value). zones.unshift( [ '', '' ] ); return (

{ __( 'If the zone is not auto detected, try selecting one of the available zones from the list below:', 'wphb' ) }