import { ConditionsConfig as ConditionsConfigCommand } from '../../data/commands'; export class ConditionsConfig { static instance; config = null; constructor( config ) { this.config = config; } /** * @return {Promise} Conditions config */ static create() { if ( ConditionsConfig.instance ) { return Promise.resolve( ConditionsConfig.instance ); } return $e.data.get( ConditionsConfigCommand.signature, {}, { refresh: true } ) .then( ( response ) => { ConditionsConfig.instance = new ConditionsConfig( response.data ); return ConditionsConfig.instance; } ); } /** * Get main options for condition name. * * @return {Array} Condition options */ getOptions() { return this.getSubOptions( 'general', true ) .map( ( { label, value } ) => { return { label, value, }; } ); } /** * Get the sub options for the select. * * @param {string} itemName * @param {boolean} isSubItem * @return {Array} Sub options */ getSubOptions( itemName, isSubItem = false ) { const config = this.config[ itemName ]; if ( ! config ) { return []; } return [ { label: config.all_label, value: isSubItem ? itemName : '' }, ...config.sub_conditions.map( ( subName ) => { const subConfig = this.config[ subName ]; return { label: subConfig.label, value: subName, children: subConfig.sub_conditions.length ? this.getSubOptions( subName, true ) : null, }; } ), ]; } /** * Get the autocomplete property from the conditions config * * @param {string} sub * @return {{}|any} Conditions autocomplete */ getSubIdAutocomplete( sub ) { const config = this.config[ sub ]; if ( ! config || ! ( 'object' === typeof ( config.controls ) ) ) { return {}; } const controls = Object.values( config.controls ); if ( ! controls?.[ 0 ]?.autocomplete ) { return {}; } return controls[ 0 ].autocomplete; } /** * Calculate instances from the conditions. * * @param {Array} conditions * @return {Object} Conditions Instances */ calculateInstances( conditions ) { let instances = conditions.reduce( ( current, condition ) => { if ( 'exclude' === condition.type ) { return current; } const key = condition.sub || condition.name, config = this.config[ key ]; if ( ! config ) { return current; } const instanceLabel = condition.subId ? `${ config.label } #${ condition.subId }` : config.all_label; return { ...current, [ key ]: instanceLabel, }; }, {} ); if ( 0 === Object.keys( instances ).length ) { instances = [ __( 'No instances', 'elementor-pro' ) ]; } return instances; } } export default ConditionsConfig; /* global _, jQuery, YoastSEO, YoastReplaceVarPlugin */ var config = require( "./config/config.js" ); var ReplaceVar = YoastReplaceVarPlugin.ReplaceVar; var supportedTypes = [ "email", "text", "textarea", "url", "wysiwyg", "block_preview" ]; var replaceVars = {}; var replaceVarPluginAvailable = function() { if ( typeof ReplaceVar === "undefined" ) { if ( config.debug ) { console.log( "Replacing ACF variables in the Snippet Window requires Yoast SEO >= 5.3." ); } return false; } return true; }; var updateReplaceVars = function( collect ) { if ( ! replaceVarPluginAvailable() ) { return; } var fieldData = _.filter( collect.getFieldData(), function( field ) { return _.contains( supportedTypes, field.type ); } ); _.each( fieldData, function( field ) { // Remove HTML tags using jQuery in case of a wysiwyg field. var content = ( field.type === "wysiwyg" ) ? jQuery( jQuery.parseHTML( field.content ) ).text() : field.content; if ( typeof replaceVars[ field.post_meta_key ] === "undefined" ) { replaceVars[ field.post_meta_key ] = new ReplaceVar( "%%cf_" + field.post_meta_key + "%%", content, { source: "direct" } ); YoastSEO.wp.replaceVarsPlugin.addReplacement( replaceVars[ field.post_meta_key ] ); if ( config.debug ) { console.log( "Created ReplaceVar for: ", field.post_meta_key, " with: ", content, replaceVars[ field.post_meta_key ] ); } } else { replaceVars[ field.post_meta_key ].replacement = content; if ( config.debug ) { console.log( "Updated ReplaceVar for: ", field.post_meta_key, " with: ", content, replaceVars[ field.post_meta_key ] ); } } } ); }; module.exports = { updateReplaceVars: updateReplaceVars, };