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/functions/replacement.js
const replacement = ( str, data, postDynamicData = {} ) => {
	if ( ! str || ! data ) {
		return str;
	}
	const chunks = variables( str );

	const replacementArray = [];
	chunks.forEach( ( chunk ) => {
		if ( ! replacementArray[ chunk ] ) {
			// Chunk will be replaced dynamic keys like title, content, excerpt so we need to check if it is available in postDynamicData.
			const dynamicKeys = [ 'title', 'excerpt', 'content' ];

			// Verify if chunk is available in dynamicKeys and postDynamicData is not empty.
			if ( dynamicKeys.includes( chunk ) && postDynamicData?.[ chunk ] ) {
				replacementArray[ chunk ] = postDynamicData[ chunk ];
			} else {
				replacementArray[ chunk ] = data[ chunk ] || '';
			}
		}
	} );

	return str.replace( /%([^%\s]+)%/g, ( match, value ) => {
		return replacementArray[ value ] || '';
	} );
};

const variables = ( str ) => {
	const regex = /%([^%\s]+)%/g;
	const matches = str.match( regex );
	if ( ! matches ) {
		return [];
	}
	return matches.map( ( match ) => match.replace( /%/g, '' ) );
};

export default replacement;