Skip to content

Commit 6ed3ba5

Browse files
committed
Update plugin and theme auto-update settings
1 parent 3f475cc commit 6ed3ba5

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

plugin/assets/src/settings/components/integrations/updater.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const Updater = ( {
8585

8686
const handleAutoUpdateToggle = () => {
8787
dispatch( { type: ACTIONS.TOGGLE_UPDATES, payload: { type } } );
88-
toggleAutoUpdate( type );
88+
toggleAutoUpdate( type, checked );
8989
};
9090

9191
return (

plugin/assets/src/settings/constants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const UPDATERS = {
5858
needsKey: false,
5959
updateAvailable: '0' !== String( getConfig( 'pluginUpdateStatus' ) ),
6060
versionAvailable: getConfig( 'pluginUpdateStatus' ),
61-
autoUpdates: parseInt( getConfig( 'pluginAutoUpdate' ), 10 ),
61+
autoUpdates: parseInt( getConfig( 'pluginAutoUpdate' ) || 0, 10 ),
6262
displayUpdatedOn: false,
6363
},
6464
THEME: {
@@ -68,7 +68,7 @@ export const UPDATERS = {
6868
needsKey: false,
6969
updateAvailable: '0' !== String( getConfig( 'themeUpdateStatus' ) ),
7070
versionAvailable: getConfig( 'themeUpdateStatus' ),
71-
autoUpdates: parseInt( getConfig( 'themeAutoUpdate' ), 10 ),
71+
autoUpdates: parseInt( getConfig( 'themeAutoUpdate' ) || 0, 10 ),
7272
displayUpdatedOn: false,
7373
},
7474
};

plugin/assets/src/settings/utils.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export const update = type => {
3838
if ( type === UPDATERS.ICONS.type ) {
3939
return updateIcons();
4040
}
41+
42+
if ( isCoreUpdate( type ) ) {
43+
return new Promise( () => {
44+
window.location.href = getConfig( 'coreUpdateUrl' );
45+
} );
46+
}
4147
};
4248

4349
/**
@@ -74,6 +80,9 @@ const updateIcons = () => {
7480
} );
7581
};
7682

83+
const isCoreUpdate = type =>
84+
[ UPDATERS.PLUGIN.type, UPDATERS.THEME.type ].includes( type );
85+
7786
/**
7887
* Save API Key in database
7988
*
@@ -99,10 +108,31 @@ export const setApiKey = key => {
99108
* Turn auto updates on/off
100109
*
101110
* @param {string} type Item to toggle updates
111+
* @param {boolean} currentStatus Current status of the toggle.
102112
* @return {Promise} Request response
103113
*/
104-
export const toggleAutoUpdate = type => {
114+
export const toggleAutoUpdate = ( type, currentStatus ) => {
105115
return new Promise( ( resolve, reject ) => {
116+
if ( isCoreUpdate( type ) ) {
117+
// eslint-disable-next-line no-undef
118+
const body = new FormData();
119+
body.append( 'action', 'toggle-auto-updates' );
120+
body.append( '_ajax_nonce', getConfig( 'autoUpdateNonce' ) );
121+
body.append( 'state', currentStatus ? 'disable' : 'enable' );
122+
body.append( 'type', type.toLowerCase() );
123+
body.append( 'asset', getConfig( `${ type.toLowerCase() }AssetName` ) );
124+
125+
apiFetch( {
126+
url: getConfig( 'autoUpdateUrl' ),
127+
method: 'POST',
128+
body,
129+
} )
130+
.then( resolve )
131+
.catch( reject );
132+
133+
return;
134+
}
135+
106136
apiFetch( {
107137
path: getConfig( 'assetsRestPath' ) + 'toggle-auto-updates',
108138
method: 'POST',

plugin/php/class-admin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ public function enqueue_assets() {
316316
'themeAutoUpdate' => in_array( Plugin::THEME_SLUG, get_site_option( 'auto_update_themes', [] ), true ),
317317
'pluginUpdateStatus' => $plugin_status,
318318
'themeUpdateStatus' => $theme_status,
319+
'coreUpdateUrl' => admin_url( 'update-core.php' ),
320+
'autoUpdateNonce' => wp_create_nonce( 'updates' ),
321+
'autoUpdateUrl' => admin_url( 'admin-ajax.php' ),
322+
'pluginAssetName' => $plugin_file,
323+
'themeAssetName' => Plugin::THEME_SLUG,
319324
]
320325
);
321326
}

0 commit comments

Comments
 (0)