HEX
Server: LiteSpeed
System: Linux premium235.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: beaupptk (733)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: /home/beaupptk/abdulfashion.shop/wp-content/plugins/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;