Skip to content

Commit

Permalink
Merge 82a1fec into 9f9d51b
Browse files Browse the repository at this point in the history
  • Loading branch information
PatelUtkarsh committed Jun 16, 2022
2 parents 9f9d51b + 82a1fec commit 08932b3
Show file tree
Hide file tree
Showing 23 changed files with 721 additions and 143 deletions.
12 changes: 11 additions & 1 deletion plugin/assets/src/admin/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@
* @return {string|Object|undefined} Value of config.
*/
const getConfig = name => {
const configData = window.materialDesignWizard;
const configData = window?.materialDesignWizard;

if ( undefined === configData ) {
return undefined;
}

return configData[ name ];
};

export const getConfigTheme = name => {
const configData = window?.materialDesignWizardTheme;

if ( undefined === configData ) {
return undefined;
Expand Down
87 changes: 87 additions & 0 deletions plugin/assets/src/settings/components/integrations/fse-opt-in.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import _uniqueId from 'lodash/uniqueId';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState, useContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import { ACTIONS } from '../../constants';
import SettingsContext from '../../context';
import Switch from './switch';
import { toggleFseOptIn } from '../../utils';

/**
* @return {JSX.Element} JSX.
*/
const FseOptIn = () => {
const [ id ] = useState( _uniqueId( 'updater-' ) );
const { dispatch } = useContext( SettingsContext );
const type = 'OPT_IN';
const { state } = useContext( SettingsContext );

const checked = state.updaters.OPT_IN.autoUpdates;

const handleAutoUpdateToggle = () => {
dispatch( { type: ACTIONS.TOGGLE_UPDATES, payload: { type } } );

toggleFseOptIn( ! checked );
};

return (
<div className={ 'material-settings__updater no__last-update' }>
<div className="mdc-layout-grid">
<div className="mdc-layout-grid__inner">
<div className="mdc-layout-grid__cell mdc-layout-grid__cell--span-10 mdc-layout-grid__cell--align-middle">
<h3 className="mdc-typography--headline6">
{ __(
'Full Site Editing Opt In',
'material-design'
) }{ ' ' }
</h3>

<p className="mdc-typography--body1">
{ __(
'This setting won’t be available when Full Site Editing is out of Beta and fully supported by WordPress.',
'material-design'
) }
</p>
</div>
<div className="mdc-layout-grid__cell mdc-layout-grid__cell--span-2 mdc-layout-grid__cell--align-middle material-settings__cell--justify-end">
<Switch
checked={ checked }
onChange={ handleAutoUpdateToggle }
id={ id }
showLabel={ false }
/>
</div>
</div>
</div>
</div>
);
};

export default FseOptIn;
150 changes: 83 additions & 67 deletions plugin/assets/src/settings/components/integrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,89 +29,105 @@ import { useContext } from '@wordpress/element';
* Internal dependencies
*/
import SettingsContext from '../../context';
import { UPDATERS } from '../../constants';
import { isPluginActive, UPDATERS, isThemeActive } from '../../constants';
import Updater from './updater';
import Api from './api';
import FseOptIn from './fse-opt-in';

const Integrations = () => {
const { state } = useContext( SettingsContext );

return (
<div className="material-settings__integrations">
<h2 className="mdc-typography--headline6">
{ __( 'Integrations', 'material-design' ) }
</h2>
{ isPluginActive && (
<>
<h2 className="mdc-typography--headline6">
{ __( 'Integrations', 'material-design' ) }
</h2>

<p
className="mdc-typography--body1"
dangerouslySetInnerHTML={ {
__html: sprintf(
// translators: %1$s: google font anchor tag, %2$s material icon resources link.
__(
'Integrate %1$s and %1$s to get the most out of the Material Theme.',
'material-design'
),
`<a href="https://fonts.google.com/" target="_blank" rel="noopener noreferrer">${ __(
'Google Fonts',
'material-design'
) }</a>`,
`<a href="https://material.io/resources/icons/?style=baseline" target="_blank" rel="noopener noreferrer">${ __(
'Material icons',
'material-design'
) }</a>`
),
} }
></p>
<p
className="mdc-typography--body1"
dangerouslySetInnerHTML={ {
__html: sprintf(
// translators: %1$s: google font anchor tag, %2$s material icon resources link.
__(
'Integrate %1$s and %1$s to get the most out of the Material Theme.',
'material-design'
),
`<a href="https://fonts.google.com/" target="_blank" rel="noopener noreferrer">${ __(
'Google Fonts',
'material-design'
) }</a>`,
`<a href="https://material.io/resources/icons/?style=baseline" target="_blank" rel="noopener noreferrer">${ __(
'Material icons',
'material-design'
) }</a>`
),
} }
/>

<p className="mdc-typography--body1">
{ __(
'Turn on auto-updater or update your resources manually.',
'material-design'
) }
</p>
<p className="mdc-typography--body1">
{ __(
'Turn on auto-updater or update your resources manually.',
'material-design'
) }
</p>
</>
) }

<div className="material-settings__updates">
{ Object.keys( UPDATERS ).map( key => (
<Updater
key={ uniqueId( 'updater-' ) }
title={ UPDATERS[ key ].title }
needsKey={ UPDATERS[ key ].needsKey }
checked={ state.updaters[ key ].autoUpdates }
lastUpdated={ state.updaters[ key ].lastUpdated }
type={ UPDATERS[ key ].type }
displayUpdatedOn={ UPDATERS[ key ].displayUpdatedOn }
versionAvailable={ UPDATERS[ key ].versionAvailable }
apiStatus={ state.apiStatus }
updateAvailable={
state.updaters[ UPDATERS[ key ].type ]
.updateAvailable
}
/>
) ) }
{ Object.keys( UPDATERS ).map( key =>
key === 'OPT_IN' ? null : (
<Updater
key={ uniqueId( 'updater-' ) }
title={ UPDATERS[ key ].title }
needsKey={ UPDATERS[ key ].needsKey }
checked={ state.updaters[ key ].autoUpdates }
lastUpdated={ state.updaters[ key ].lastUpdated }
type={ UPDATERS[ key ].type }
displayUpdatedOn={
UPDATERS[ key ].displayUpdatedOn
}
versionAvailable={
UPDATERS[ key ].versionAvailable
}
apiStatus={ state.apiStatus }
updateAvailable={
state.updaters[ UPDATERS[ key ].type ]
.updateAvailable
}
/>
)
) }
{ isThemeActive && <FseOptIn /> }
</div>

<h2 className="mdc-typography--headline6">
{ __( 'Google API Key', 'material-design' ) }
</h2>
{ isPluginActive && (
<>
<h2 className="mdc-typography--headline6">
{ __( 'Google API Key', 'material-design' ) }
</h2>

<p
className="mdc-typography--body1"
dangerouslySetInnerHTML={ {
__html: sprintf(
// translators: %s google api key anchor tag.
__(
'To use Google Fonts in Material Theme, please activate your %s and enable updates',
'material-design'
),
`<a href="https://developers.google.com/fonts/docs/developer_api#APIKey" target="_blank" rel="noopener noreferrer">${ __(
'Google API Key',
'material-design'
) }</a>`
),
} }
></p>
<p
className="mdc-typography--body1"
dangerouslySetInnerHTML={ {
__html: sprintf(
// translators: %s google api key anchor tag.
__(
'To use Google Fonts in Material Theme, please activate your %s and enable updates',
'material-design'
),
`<a href="https://developers.google.com/fonts/docs/developer_api#APIKey" target="_blank" rel="noopener noreferrer">${ __(
'Google API Key',
'material-design'
) }</a>`
),
} }
/>

<Api apiStatus={ state.apiStatus } />
<Api apiStatus={ state.apiStatus } />
</>
) }
</div>
);
};
Expand Down
10 changes: 6 additions & 4 deletions plugin/assets/src/settings/components/integrations/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { MDCSwitch } from '@material/switch';
import { __ } from '@wordpress/i18n';
import { useEffect } from '@wordpress/element';

const Switch = ( { checked, id, onChange } ) => {
const Switch = ( { checked, id, onChange, showLabel = true } ) => {
useEffect( () => {
const mdcSwitches = document.querySelectorAll( '.mdc-switch' );

Expand Down Expand Up @@ -58,9 +58,11 @@ const Switch = ( { checked, id, onChange } ) => {
/>
</div>
</div>
<label id={ `label-${ id }` } htmlFor={ id }>
{ __( 'Auto-updates enabled', 'material-design' ) }
</label>
{ showLabel && (
<label id={ `label-${ id }` } htmlFor={ id }>
{ __( 'Auto-updates enabled', 'material-design' ) }
</label>
) }
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const Updater = ( {
) }</a>`
),
} }
></p>
/>
) }

{ ! isDisabled && false !== displayUpdatedOn && (
Expand Down
21 changes: 17 additions & 4 deletions plugin/assets/src/settings/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import getConfig from '../admin/get-config';
import getConfig, { getConfigTheme } from '../admin/get-config';

export const ACTIONS = {
ADD_ERROR: 'ADD_ERROR',
Expand All @@ -34,7 +34,7 @@ export const ACTIONS = {
SET_UPDATED: 'SET_UPDATED',
};

const ASSET_UPDATES = {
export const ASSET_UPDATES = {
FONTS: {
title: __( 'Google Fonts', 'material-design' ),
type: 'FONTS',
Expand Down Expand Up @@ -76,9 +76,22 @@ const CORE_UPDATES = {
},
};

const FSE = {
OPT_IN: {
title: __( 'Opt-in', 'material-design' ),
type: 'OPT_IN',
autoUpdates: parseInt( getConfigTheme( 'isOptIn' ) || 0, 10 ),
},
};
export const isPluginActive = !! window?.materialDesignWizard;
export const isThemeActive = !! window?.materialDesignWizardTheme;

export const UPDATERS = {
...ASSET_UPDATES,
...( getConfig( 'coreUpdatesEnabled' ) ? CORE_UPDATES : {} ),
...( isPluginActive ? ASSET_UPDATES : {} ),
...( isPluginActive && getConfig( 'coreUpdatesEnabled' )
? CORE_UPDATES
: {} ),
...( isThemeActive ? FSE : {} ),
};

export const KEY_PLACEHOLDER = '•••••••••••••••••••••••••••••';
20 changes: 19 additions & 1 deletion plugin/assets/src/settings/utils/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import getConfig from '../../admin/get-config';
import getConfig, { getConfigTheme } from '../../admin/get-config';
import { isPluginActive } from '../constants';

/**
* Update fonts.
Expand Down Expand Up @@ -125,3 +126,20 @@ export const toggleAutoUpdate = ( type, currentStatus ) => {
.catch( reject );
} );
};

export const toggleFseOptIn = checked => {
return new Promise( ( resolve, reject ) => {
apiFetch( {
path: isPluginActive
? getConfigTheme( 'restPath' )
: getConfigTheme( 'restUrl' ),
method: 'POST',
data: { checked },
headers: {
'X-WP-Nonce': getConfigTheme( 'nonce' ),
},
} )
.then( resolve )
.catch( reject );
} );
};
Loading

0 comments on commit 08932b3

Please sign in to comment.