| 1 |
export default { |
| 2 |
parseCSS(template) { |
| 3 |
let css = template.style.css; |
| 4 |
|
| 5 |
// Reconstruct CSS with new value and comments. |
| 6 |
for ( let property of Object.values(template.style.properties) ) { |
| 7 |
let fields = ''; |
| 8 |
Object.entries(property).forEach(([field, value]) => { |
| 9 |
if ( ! ['id', 'name', 'default', 'value', 'options'].includes( field ) ) { |
| 10 |
fields = ` ${field}=${value}`; |
| 11 |
} |
| 12 |
}); |
| 13 |
|
| 14 |
const replacement = `${property.value}; /*wpupg_${property.id}${fields}*/`; |
| 15 |
css = css.replace( new RegExp( `%wpupg_${property.id}%\s*;`, 'g' ), replacement ); |
| 16 |
} |
| 17 |
|
| 18 |
return css; |
| 19 |
}, |
| 20 |
getShortcodeName(id) { |
| 21 |
let name = id.replace('wpupg-', ''); |
| 22 |
name = name.replace(/-/g, ' '); |
| 23 |
name = name.toLowerCase().replace(/\b[a-z]/g, function(letter) { |
| 24 |
return letter.toUpperCase(); |
| 25 |
}); |
| 26 |
|
| 27 |
return name; |
| 28 |
}, |
| 29 |
getFullShortcode(shortcode) { |
| 30 |
let fullShortcode = '[' + shortcode.id; |
| 31 |
|
| 32 |
// Add shortcode attributes. |
| 33 |
for (let attribute in shortcode.attributes) { |
| 34 |
if ( shortcode.attributes.hasOwnProperty(attribute) ) { |
| 35 |
let value = shortcode.attributes[attribute]; |
| 36 |
|
| 37 |
// Replace " and ] with HTML entity to prevent breaking shortcode. |
| 38 |
value = value.replace(/"/gm, '"'); |
| 39 |
value = value.replace(/\]/gm, ']'); |
| 40 |
fullShortcode += ' ' + attribute + '="' + value + '"'; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Close shortcode. |
| 45 |
fullShortcode += ']'; |
| 46 |
|
| 47 |
return fullShortcode; |
| 48 |
}, |
| 49 |
dependencyMet(object, properties) { |
| 50 |
if (properties && object.hasOwnProperty('dependency')) { |
| 51 |
let dependencies = object.dependency; |
| 52 |
|
| 53 |
// Make sure dependencies is an array. |
| 54 |
if ( ! Array.isArray( dependencies ) ) { |
| 55 |
dependencies = [dependencies]; |
| 56 |
} |
| 57 |
|
| 58 |
// Check all dependencies. |
| 59 |
for ( let dependency of dependencies ) { |
| 60 |
if ( properties.hasOwnProperty(dependency.id) ) { |
| 61 |
const dependency_value = properties[dependency.id].value; |
| 62 |
|
| 63 |
if ( dependency.hasOwnProperty('type') && 'inverse' == dependency.type ) { |
| 64 |
if (dependency_value == dependency.value) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
} else { |
| 68 |
if (dependency_value != dependency.value) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return true; |
| 77 |
}, |
| 78 |
}; |
| 79 |
|