HEX
Server: Apache
System: Linux cp4.skywebbox.com 5.14.0-503.15.1.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Nov 28 07:25:19 EST 2024 x86_64
User: alfouzantranspor (1054)
PHP: 8.3.23
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/alfouzantranspor/public_html/wp-content/plugins_backup/surerank/src/store/setInitialState.js
import apiFetch from '@wordpress/api-fetch';
import { dispatch } from '@wordpress/data';
import { addQueryArgs } from '@wordpress/url';
import { store } from './store';
import { EDITOR_URL } from '@Global/constants/api';

const setInitialState = () => {
	const queryParams = {};
	// If post page.
	if ( window?.surerank_globals?.post_id ) {
		queryParams.post_id = window?.surerank_globals.post_id;
	}
	// If term page.
	if ( window?.surerank_globals?.term_id ) {
		queryParams.term_id = window?.surerank_globals.term_id;
	}

	if ( window?.surerank_seo_popup?.post_id ) {
		queryParams.post_id = window?.surerank_seo_popup.post_id;
	}
	// If term page.
	if ( window?.surerank_seo_popup?.term_id ) {
		queryParams.term_id = window?.surerank_seo_popup.term_id;
	}

	apiFetch( {
		path: addQueryArgs( EDITOR_URL, queryParams ),
	} ).then( ( response ) => {
		if ( response?.success ) {
			let initialState = {
				variables: response.variables,
			};

			if ( response.other ) {
				initialState = { ...initialState, ...response.other };
			}

			dispatch( store ).updateInitialState( initialState );
		}
	} );
};

export default setInitialState;