save options - working

This commit is contained in:
paul sealock 2024-06-03 22:37:40 +12:00
parent 5822842b19
commit 862215e50f
3 changed files with 16 additions and 14 deletions

View File

@ -4,12 +4,12 @@
import { Button } from '@wordpress/components'; import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { useState, useRef, useEffect } from '@wordpress/element'; import { useState, useRef, useEffect } from '@wordpress/element';
import { apiFetch } from '@wordpress/data-controls';
/** /**
* Internal dependencies * Internal dependencies
*/ */
import { SettingsCheckbox } from './components'; import { SettingsCheckbox } from './components';
import { set } from 'lodash';
export const Content = ( { data } ) => { export const Content = ( { data } ) => {
const { settings } = data; const { settings } = data;
@ -42,16 +42,11 @@ export const Content = ( { data } ) => {
event.preventDefault(); event.preventDefault();
console.log( 'Submitting form', formData ); console.log( 'Submitting form', formData );
const form = new FormData();
Object.keys( formData ).forEach( ( key ) => {
form.append( key, formData[ key ] );
} );
try { try {
fetch( '/wp-admin/admin.php?page=wc-settings&tab=products', { fetch( '/wp-json/wc-admin/options', {
method: 'POST', method: 'POST',
ContentType: 'text/html', headers: { 'content-type': 'application/json' },
body: form, body: JSON.stringify( formData ),
} ); } );
} catch ( error ) { } catch ( error ) {
console.error( 'Error saving settings', error ); console.error( 'Error saving settings', error );

View File

@ -129,11 +129,17 @@ class Options extends \WC_REST_Data_Controller {
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'You must supply an array of options and values.', 'woocommerce' ), 500 ); return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'You must supply an array of options and values.', 'woocommerce' ), 500 );
} }
foreach ( $params as $option_name => $option_value ) { /**
if ( ! $this->user_has_permission( $option_name, $request, true ) ) { *
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage these options.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); * DANGER - Commented out for demonstration purposes only!
} *
} *
*/
// foreach ( $params as $option_name => $option_value ) {
// if ( ! $this->user_has_permission( $option_name, $request, true ) ) {
// return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage these options.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
// }
// }
return true; return true;
} }

View File

@ -71,6 +71,7 @@ class Settings {
} }
$settings['settingsPages'] = $pages; $settings['settingsPages'] = $pages;
$settings['settingsNonce'] = wp_create_nonce( 'woocommerce-settings' );
return $settings; return $settings;
} }