Skip to content

Commit 3005a03

Browse files
committed
feat: integrate v2 settings to new settings
1 parent ebe913b commit 3005a03

File tree

7 files changed

+102
-43
lines changed

7 files changed

+102
-43
lines changed

src/components/admin-base-setting/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let i = 1
55

66
const AdminBaseSetting = props => {
77
const [ uid ] = useState( `ugb-admin-setting-${ i++ }` )
8-
const isSearched = props.searchedSettings.includes( props.label )
8+
const isSearched = props.searchedSettings ? props.searchedSettings.includes( props.label ) : true
99
const mainClasses = classnames( [
1010
'ugb-admin-setting',
1111
props.className,

src/deprecated/v2/admin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function __construct() {
2020
add_action( 'stackable_settings_admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
2121

2222
// Add the optimization setting.
23-
add_action( 'stackable_settings_page_mid', array( $this, 'add_optimization_settings' ), 11 );
23+
// add_action( 'stackable_settings_page_mid', array( $this, 'add_block_settings' ), 11 );
2424
}
2525
}
2626

@@ -33,7 +33,7 @@ public function admin_enqueue_scripts() {
3333
*
3434
* @return void
3535
*/
36-
public function add_optimization_settings() {
36+
public function add_block_settings() {
3737
?>
3838
<article class="s-box" id="block-settings-v2">
3939
<h2><?php _e( '🎛 Enable & Disable Blocks', STACKABLE_I18N ) ?> (V2)</h2>

src/deprecated/v2/optimization-settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function __construct() {
3535
}
3636

3737
// Add the optimization setting.
38-
add_action( 'stackable_settings_page_mid', array( $this, 'add_optimization_settings' ) );
38+
// add_action( 'stackable_settings_page_mid', array( $this, 'add_optimization_settings' ) );
3939
}
4040
}
4141

src/deprecated/v2/welcome/admin.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* Internal dependencies
3-
*/
4-
import blockData from './blocks'
5-
61
/**
72
* WordPress dependencies
83
*/
@@ -11,22 +6,19 @@ import {
116
Component, useEffect, useState, Fragment,
127
} from '@wordpress/element'
138
import { send as ajaxSend } from '@wordpress/ajax'
14-
import domReady from '@wordpress/dom-ready'
159
import { Spinner } from '@wordpress/components'
1610
import { loadPromise, models } from '@wordpress/api'
1711

1812
/**
1913
* External dependencies
2014
*/
2115
import {
22-
v2disabledBlocks as disabledBlocks,
2316
i18n,
2417
v2nonce as nonce,
2518
} from 'stackable'
2619
import AdminToggleSetting from '~stackable/components/admin-toggle-setting'
27-
import { createRoot } from '~stackable/util/element'
2820

29-
class BlockToggler extends Component {
21+
export class BlockToggler extends Component {
3022
constructor() {
3123
super( ...arguments )
3224
this.toggleBlock = this.toggleBlock.bind( this )
@@ -81,7 +73,7 @@ class BlockToggler extends Component {
8173
}
8274

8375
render() {
84-
const { blocks: blockData } = this.props
76+
const { blocks: blockData, searchedSettings: searchedSettings } = this.props
8577

8678
return (
8779
<div>
@@ -105,6 +97,7 @@ class BlockToggler extends Component {
10597
<AdminToggleSetting
10698
key={ i }
10799
label={ __( block.title, i18n ) } /* eslint-disable-line @wordpress/i18n-no-variables */
100+
searchedSettings={ searchedSettings }
108101
value={ ! isDisabled }
109102
onChange={ () => this.toggleBlock( blockName ) }
110103
size="small"
@@ -119,7 +112,7 @@ class BlockToggler extends Component {
119112
}
120113
}
121114

122-
const OptimizationSettings = () => {
115+
export const OptimizationSettings = ( { searchSettings } ) => {
123116
const [ optimizeScriptLoad, setOptimizeScriptLoad ] = useState( false )
124117

125118
useEffect( () => {
@@ -140,25 +133,11 @@ const OptimizationSettings = () => {
140133
return <Fragment>
141134
<AdminToggleSetting
142135
label={ __( 'Frontend JS & CSS Files', i18n ) }
136+
searchSettings={ searchSettings }
143137
value={ optimizeScriptLoad }
144138
onChange={ updateOptimizeScriptLoad }
145139
disabled={ __( 'Load across entire site', i18n ) }
146140
enabled={ __( 'Load only in posts with Stackable blocks', i18n ) }
147141
/>
148142
</Fragment>
149143
}
150-
151-
// Load all the options into the UI.
152-
domReady( () => {
153-
if ( document.querySelector( '.s-settings-wrapper-v2' ) ) {
154-
createRoot( document.querySelector( '.s-settings-wrapper-v2' ) ).render(
155-
<BlockToggler blocks={ blockData } disabledBlocks={ disabledBlocks } />
156-
)
157-
}
158-
159-
if ( document.querySelector( '.s-optimization-settings' ) ) {
160-
createRoot( document.querySelector( '.s-optimization-settings' ) ).render(
161-
<OptimizationSettings />
162-
)
163-
}
164-
} )

src/welcome/admin.js

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
i18n,
2626
showProNoticesOption,
2727
isPro,
28+
v2disabledBlocks,
2829
} from 'stackable'
2930
import classnames from 'classnames'
3031
import { importBlocks } from '~stackable/util/admin'
@@ -35,6 +36,8 @@ import AdminTextSetting from '~stackable/components/admin-text-setting'
3536
import AdminToolbarSetting from '~stackable/components/admin-toolbar-setting'
3637
import { GettingStarted } from './getting-started'
3738
import { BLOCK_STATE } from '~stackable/util/blocks'
39+
import { BlockToggler, OptimizationSettings } from '~stackable/deprecated/v2/welcome/admin'
40+
import blockData from '~stackable/deprecated/v2/welcome/blocks'
3841

3942
const FREE_BLOCKS = importBlocks( require.context( '../block', true, /block\.json$/ ) )
4043

@@ -222,6 +225,22 @@ const SEARCH_TREE = [
222225
},
223226
],
224227
},
228+
{
229+
id: 'v2-settings',
230+
label: __( 'V2 Settings', i18n ),
231+
groups: [
232+
{
233+
id: 'optimizations',
234+
children: [
235+
__( 'Frontend JS & CSS Files' ),
236+
],
237+
},
238+
{
239+
id: 'blocks',
240+
children: Object.values( blockData ).map( block => block.title ),
241+
},
242+
],
243+
},
225244
]
226245

227246
const BLOCK_DEPENDENCIES = {
@@ -402,6 +421,7 @@ const Sidenav = ( {
402421
currentSearch,
403422
filteredSearchTree,
404423
isSaving,
424+
hasV2Tab,
405425
} ) => {
406426
const saveButtonClasses = classnames( [
407427
's-save-changes',
@@ -424,6 +444,11 @@ const Sidenav = ( {
424444
{ 's-sidenav-item-highlight': isSearched },
425445
{ 's-active': currentTab === id },
426446
] )
447+
448+
if ( id === 'v2-settings' && ! hasV2Tab ) {
449+
return null
450+
}
451+
427452
return ( <button
428453
key={ id }
429454
className={ tabClasses }
@@ -437,12 +462,14 @@ const Sidenav = ( {
437462
)
438463
} ) }
439464
<div className="s-save-changes-wrapper">
440-
<button
441-
className={ saveButtonClasses }
442-
onClick={ handleSettingsSave }
443-
>
444-
{ isSaving ? <Spinner /> : __( 'Save Changes', i18n ) }
445-
</button>
465+
{ currentTab !== 'v2-settings' &&
466+
<button
467+
className={ saveButtonClasses }
468+
onClick={ handleSettingsSave }
469+
>
470+
{ isSaving ? <Spinner /> : __( 'Save Changes', i18n ) }
471+
</button>
472+
}
446473
</div>
447474
</div>
448475
</nav>
@@ -474,6 +501,13 @@ const Settings = () => {
474501
const [ currentTab, setCurrentTab ] = useState( 'editor-settings' )
475502
const [ currentSearch, setCurrentSearch ] = useState( '' )
476503
const [ isSaving, setIsSaving ] = useState( false )
504+
const [ hasV2Tab, setHasV2Tab ] = useState( false )
505+
506+
const hasV2Compatibility = currentSettings => {
507+
return currentSettings.stackable_v2_frontend_compatibility === '1' ||
508+
currentSettings.stackable_v2_editor_compatibility === '1' ||
509+
currentSettings.stackable_v2_editor_compatibility_usage === '1'
510+
}
477511

478512
const handleSettingsChange = useCallback( newSettings => {
479513
setSettings( prev => ( { ...prev, ...newSettings } ) )
@@ -500,10 +534,19 @@ const Settings = () => {
500534
const settings = new models.Settings()
501535
settings.fetch().then( response => {
502536
setSettings( response )
537+
// Should only be set initially since we have to reload after setting for it to work with the backend
538+
setHasV2Tab( hasV2Compatibility( response ) )
503539
} )
504540
} )
505541
}, [] )
506542

543+
// However, when disabling V2 blocks, update the settings page to disallow further configuration
544+
useEffect( () => {
545+
if ( ! hasV2Compatibility( settings ) ) {
546+
setHasV2Tab( false )
547+
}
548+
}, [ settings ] )
549+
507550
const hasUnsavedChanges = useMemo( () => Object.keys( unsavedChanges ).length > 0, [ unsavedChanges ] )
508551
const filteredSearchTree = useMemo( () => {
509552
if ( ! currentSearch ) {
@@ -526,6 +569,7 @@ const Settings = () => {
526569
settings,
527570
handleSettingsChange,
528571
filteredSearchTree,
572+
currentTab,
529573
}
530574

531575
return <>
@@ -537,6 +581,7 @@ const Settings = () => {
537581
currentSearch={ currentSearch }
538582
filteredSearchTree={ filteredSearchTree }
539583
isSaving={ isSaving }
584+
hasV2Tab={ hasV2Tab }
540585
/>
541586
<article className="s-box" id={ currentTab }>
542587
<Searchbar currentSearch={ currentSearch } handleSearchChange={ setCurrentSearch } />
@@ -549,6 +594,8 @@ const Settings = () => {
549594
{ currentTab === 'custom-fields-settings' && <CustomFields { ...props } /> }
550595
{ currentTab === 'integrations' && <Integrations { ...props } /> }
551596
{ currentTab === 'other-settings' && <AdditionalOptions { ...props } /> }
597+
{ /* Render the V2 settings and show/hide via CSS */ }
598+
<V2Settings { ...props } />
552599
</article>
553600
</>
554601
}
@@ -1262,6 +1309,7 @@ const AdditionalOptions = props => {
12621309
{ migrationSettings.children.length > 0 &&
12631310
<div className="s-setting-group">
12641311
<h3>{ __( 'Migration Settings', i18n ) }</h3>
1312+
<p>{ __( 'After enabling the version 2 blocks, please refresh the page to re-fetch the blocks from the server.', i18n ) }</p>
12651313
<p>
12661314
{ __( 'Migrating from version 2 to version 3?', i18n ) }
12671315
&nbsp;
@@ -1308,8 +1356,39 @@ const AdditionalOptions = props => {
13081356
)
13091357
}
13101358

1311-
AdditionalOptions.defaultProps = {
1312-
showProNoticesOption: false,
1359+
const V2Settings = props => {
1360+
const groups = props.filteredSearchTree.find( tab => tab.id === 'v2-settings' ).groups
1361+
const optimizations = groups.find( group => group.id === 'optimizations' )
1362+
const blocks = groups.find( group => group.id === 'blocks' )
1363+
1364+
const classes = classnames( [
1365+
's-v2-settings',
1366+
{ 's-settings-hide': props.currentTab !== 'v2-settings' },
1367+
] )
1368+
1369+
return (
1370+
<div className={ classes }>
1371+
{ optimizations.children.length > 0 &&
1372+
<div className="s-setting-group">
1373+
<h2>{ __( '🏃‍♂️ Optimization Settings', i18n ) } (V2)</h2>
1374+
<p className="s-settings-subtitle">
1375+
{ __( 'Here are some settings that you can tweak to optimize Stackable.', i18n ) }
1376+
<a href="https://docs.wpstackable.com/article/460-how-to-use-optimization-settings?utm_source=wp-settings-global-settings&utm_campaign=learnmore&utm_medium=wp-dashboard" target="_docs">{ __( 'Learn more.', i18n ) } </a>
1377+
<br />
1378+
<strong>{ __( 'This only works for version 2 blocks.', i18n ) }</strong>
1379+
</p>
1380+
<OptimizationSettings searchedSettings={ optimizations.children } />
1381+
</div>
1382+
}
1383+
{ blocks.children.length > 0 &&
1384+
<div className="s-setting-group">
1385+
<h2>{ __( 'Enable & Disable Blocks', i18n ) } (V2)</h2>
1386+
<strong>{ __( 'This only works for version 2 blocks.', i18n ) }</strong>
1387+
<BlockToggler blocks={ blockData } disabledBlocks={ v2disabledBlocks } searchedSettings={ blocks.children } />
1388+
</div>
1389+
}
1390+
</div>
1391+
)
13131392
}
13141393

13151394
// Load all the options into the UI.

src/welcome/admin.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@ body.toplevel_page_stk-custom-fields {
673673
}
674674
}
675675

676+
.s-v2-settings {
677+
&.s-settings-hide {
678+
display: none;
679+
}
680+
}
681+
676682
.s-side {
677683
h2,
678684
h3 {

src/welcome/index.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,6 @@ public function stackable_settings_content() {
192192

193193
<div class="s-content" id="settings-content"></div>
194194
<?php do_action( 'stackable_settings_page_mid' ); ?>
195-
<!-- We put all the other options here. -->
196-
<article class="s-box s-box-hidden" id="other-settings">
197-
<h2><?php _e( '🔩 Other Settings', STACKABLE_I18N ) ?></h2>
198-
<aside class="s-other-options-wrapper"></aside>
199-
</article>
200195
</div>
201196
<!-- <div class="s-side">
202197
<?php if ( ! sugb_fs()->can_use_premium_code() ) : ?>

0 commit comments

Comments
 (0)