first
This commit is contained in:
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Get control value.
|
||||
* Supported array names like `images[3].format`
|
||||
*
|
||||
* @param {string} name - control name.
|
||||
* @param {Object} attributes - block attributes.
|
||||
*
|
||||
* @return {Mixed} value.
|
||||
*/
|
||||
export default function controlGetValue(name, attributes) {
|
||||
let val = attributes[name];
|
||||
|
||||
// Parse arrays and objects.
|
||||
// Example `images[3].format`.
|
||||
if (typeof val === 'undefined' && /[\[\.]/g.test(name)) {
|
||||
// Find parts, used for objects.
|
||||
// Example `images.format`
|
||||
const valObjectParts = name.split('.');
|
||||
const valParts = [];
|
||||
|
||||
if (valObjectParts && valObjectParts.length) {
|
||||
// Find parts, used for arrays.
|
||||
// Example `images[3]`
|
||||
valObjectParts.forEach((objPart) => {
|
||||
if (/[\[]/g.test(objPart)) {
|
||||
const valArrayParts = objPart.split(/[\[\]]/g);
|
||||
|
||||
if (valArrayParts && valArrayParts.length) {
|
||||
valArrayParts.forEach((arrPart) => {
|
||||
if (arrPart !== '') {
|
||||
if (`${parseInt(arrPart, 10)}` === arrPart) {
|
||||
valParts.push(parseInt(arrPart, 10));
|
||||
} else {
|
||||
valParts.push(arrPart);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
valParts.push(objPart);
|
||||
}
|
||||
});
|
||||
|
||||
// Try to find value in attributes.
|
||||
if (valParts.length) {
|
||||
let currentVal = attributes;
|
||||
|
||||
valParts.forEach((partName) => {
|
||||
if (
|
||||
currentVal &&
|
||||
typeof currentVal[partName] !== 'undefined'
|
||||
) {
|
||||
currentVal = currentVal[partName];
|
||||
} else {
|
||||
currentVal = undefined;
|
||||
}
|
||||
});
|
||||
|
||||
val = currentVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Control condition check.
|
||||
*
|
||||
* @package visual-portfolio
|
||||
*/
|
||||
|
||||
/**
|
||||
* Visual_Portfolio_Control_Get_Value
|
||||
*/
|
||||
class Visual_Portfolio_Control_Get_Value {
|
||||
/**
|
||||
* Get control value.
|
||||
* Supported array names like `images[3].format`
|
||||
*
|
||||
* @param string $name - control name.
|
||||
* @param array $attributes - block attributes.
|
||||
*
|
||||
* @return mixed value.
|
||||
*/
|
||||
public static function get( $name, $attributes ) {
|
||||
$val = isset( $attributes[ $name ] ) ? $attributes[ $name ] : null;
|
||||
|
||||
// Parse arrays and objects.
|
||||
// Example `images[3].format`.
|
||||
if ( null !== $val && preg_match( '/[\[\.]/', $name ) ) {
|
||||
// Find parts, used for objects.
|
||||
// Example `images.format`.
|
||||
$val_object_parts = explode( '.', $name );
|
||||
$val_parts = array();
|
||||
|
||||
if ( $val_object_parts && ! empty( $val_object_parts ) ) {
|
||||
// Find parts, used for arrays.
|
||||
// Example `images[3]`.
|
||||
foreach ( $val_object_parts as $obj_part ) {
|
||||
if ( preg_match( '/[\[]/', $obj_part ) ) {
|
||||
$val_array_parts = preg_split( '/[\[\]]/', $obj_part );
|
||||
|
||||
if ( $val_array_parts && ! empty( $val_array_parts ) ) {
|
||||
foreach ( $val_array_parts as $arr_part ) {
|
||||
if ( '' !== $arr_part ) {
|
||||
$arr_part_int = (int) $arr_part;
|
||||
if ( "$arr_part_int" === $arr_part ) {
|
||||
$val_parts[] = $arr_part_int;
|
||||
} else {
|
||||
$val_parts[] = $arr_part;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$val_parts[] = $obj_part;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to find value in attributes.
|
||||
if ( ! empty( $val_parts ) ) {
|
||||
$current_val = $attributes;
|
||||
|
||||
foreach ( $val_parts as $part_name ) {
|
||||
if ( $current_val && isset( $current_val[ $part_name ] ) ) {
|
||||
$current_val = $current_val[ $part_name ];
|
||||
} else {
|
||||
$current_val = null;
|
||||
}
|
||||
}
|
||||
|
||||
$val = $current_val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $val;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user