first commit
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
const {
|
||||
applyFilters,
|
||||
} = wp.hooks;
|
||||
|
||||
/**
|
||||
* Check active_callback in custom controls.
|
||||
*
|
||||
* @param {Object} fieldData field data.
|
||||
* @param {Object} attributes block attributes.
|
||||
* @param {Object} allFields all fields.
|
||||
*
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export default function isFieldVisible( fieldData, attributes, allFields ) {
|
||||
let result = true;
|
||||
|
||||
const filteredAttributes = applyFilters( 'canvas.block.prepareServerRenderAttributes', attributes, {
|
||||
fields: allFields,
|
||||
} );
|
||||
|
||||
if ( fieldData.active_callback && fieldData.active_callback.length ) {
|
||||
result = checkCondition( fieldData.active_callback, filteredAttributes, 'AND' );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare 2 values
|
||||
*
|
||||
* @param {mixed} lval Left value.
|
||||
* @param {string} operator Operator.
|
||||
* @param {mixed} rval Right value.
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function compare( lval, operator, rval ) {
|
||||
let checkResult = true;
|
||||
|
||||
switch ( operator ) {
|
||||
case '==':
|
||||
checkResult = lval == rval;
|
||||
break;
|
||||
case '===':
|
||||
checkResult = lval === rval;
|
||||
break;
|
||||
case '!=':
|
||||
checkResult = lval != rval;
|
||||
break;
|
||||
case '!==':
|
||||
checkResult = lval !== rval;
|
||||
break;
|
||||
case '>=':
|
||||
checkResult = lval >= rval;
|
||||
break;
|
||||
case '<=':
|
||||
checkResult = lval <= rval;
|
||||
break;
|
||||
case '>':
|
||||
checkResult = lval > rval;
|
||||
break;
|
||||
case '<':
|
||||
checkResult = lval < rval;
|
||||
break;
|
||||
case 'AND':
|
||||
checkResult = lval && rval;
|
||||
break;
|
||||
case 'OR':
|
||||
checkResult = lval || rval;
|
||||
break;
|
||||
default:
|
||||
checkResult = lval;
|
||||
break;
|
||||
}
|
||||
|
||||
return checkResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check condition
|
||||
*
|
||||
* @param {Object} conditions - Conditions array.
|
||||
* @param {Object} attributes - Available block attributes.
|
||||
* @param {string} relation - Can be one of 'AND' or 'OR'.
|
||||
*
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function checkCondition( conditions, attributes, relation ) {
|
||||
const childRelation = ( 'AND' === relation ) ? 'OR' : 'AND';
|
||||
|
||||
// by default result will be TRUE for relation AND
|
||||
// and FALSE for relation OR.
|
||||
let result = relation === 'AND';
|
||||
|
||||
conditions.forEach( ( data ) => {
|
||||
if ( Array.isArray( data ) ) {
|
||||
result = compare( result, relation, checkCondition( data, attributes, childRelation ) );
|
||||
} else if ( data.field ) {
|
||||
const splitValName = data.field.split( '.' );
|
||||
let fieldVal = undefined;
|
||||
|
||||
// check for array values like:
|
||||
// toggleListName['option1']
|
||||
if ( 2 === splitValName.length && typeof attributes[ splitValName[ 0 ] ] !== 'undefined' && typeof attributes[ splitValName[ 0 ] ][ splitValName[ 1 ] ] !== 'undefined' ) {
|
||||
fieldVal = attributes[ splitValName[ 0 ] ][ splitValName[ 1 ] ];
|
||||
}
|
||||
|
||||
// check for normal values
|
||||
if ( typeof fieldVal === 'undefined' ) {
|
||||
fieldVal = attributes[ data.field ];
|
||||
}
|
||||
|
||||
// check count
|
||||
if ( typeof data.count !== 'undefined' ) {
|
||||
const count = fieldVal.split( data['count'] );
|
||||
fieldVal = count.length - 1;
|
||||
}
|
||||
|
||||
if ( typeof fieldVal !== 'undefined' ) {
|
||||
result = compare( result, relation, compare( fieldVal, data.operator, data.value ) );
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* Check if field visible.
|
||||
*
|
||||
* @package Canvas
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'CNVS_Gutenberg_Utils_Is_Field_Visible' ) ) {
|
||||
/**
|
||||
* Class Gutenberg Utils Is Field Visible.
|
||||
*/
|
||||
class CNVS_Gutenberg_Utils_Is_Field_Visible {
|
||||
/**
|
||||
* Check active_callback in custom controls.
|
||||
*
|
||||
* @param array $fieldData field data.
|
||||
* @param array $attributes block attributes.
|
||||
* @param array $allFields all fields.
|
||||
* @param array $prepareAttrs prepare attributes for server render.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function check( $fieldData, $attributes, $allFields, $prepareAttrs = true ) {
|
||||
$result = true;
|
||||
|
||||
// if ( $prepareAttrs ) {
|
||||
// $attributes = apply_filters( 'canvas_block_prepare_server_render_attributes', $attributes, array( 'fields' => $allFields ) );
|
||||
// }
|
||||
|
||||
if ( isset( $fieldData['active_callback'] ) ) {
|
||||
$result = self::checkCondition( $fieldData['active_callback'], $attributes, 'AND' );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare 2 values
|
||||
*
|
||||
* @param mixed $lval Left value.
|
||||
* @param string $operator Operator.
|
||||
* @param mixed $rval Right value.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function compare( $lval, $operator, $rval ) {
|
||||
$check_result = true;
|
||||
|
||||
switch ( $operator ) {
|
||||
case '==':
|
||||
$check_result = $lval == $rval;
|
||||
break;
|
||||
case '===':
|
||||
$check_result = $lval === $rval;
|
||||
break;
|
||||
case '!=':
|
||||
$check_result = $lval != $rval;
|
||||
break;
|
||||
case '!==':
|
||||
$check_result = $lval !== $rval;
|
||||
break;
|
||||
case '>=':
|
||||
$check_result = $lval >= $rval;
|
||||
break;
|
||||
case '<=':
|
||||
$check_result = $lval <= $rval;
|
||||
break;
|
||||
case '>':
|
||||
$check_result = $lval > $rval;
|
||||
break;
|
||||
case '<':
|
||||
$check_result = $lval < $rval;
|
||||
break;
|
||||
case 'AND':
|
||||
$check_result = $lval && $rval;
|
||||
break;
|
||||
case 'OR':
|
||||
$check_result = $lval || $rval;
|
||||
break;
|
||||
default:
|
||||
$check_result = $lval;
|
||||
break;
|
||||
}
|
||||
|
||||
return $check_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check condition
|
||||
*
|
||||
* @param array $conditions - Conditions array.
|
||||
* @param array $attributes - Available block attributes.
|
||||
* @param string $relation - Can be one of 'AND' or 'OR'.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function checkCondition( $conditions, $attributes, $relation ) {
|
||||
$childRelation = ( 'AND' === $relation ) ? 'OR' : 'AND';
|
||||
|
||||
// By default result will be TRUE for relation AND and FALSE for relation OR.
|
||||
$result = $relation === 'AND';
|
||||
|
||||
foreach ( $conditions as $data ) {
|
||||
if ( is_array( $data ) && ! isset( $data['field'] ) ) {
|
||||
$result = self::compare( $result, $relation, self::checkCondition( $data, $attributes, $childRelation ) );
|
||||
} elseif ( isset( $data['field'] ) ) {
|
||||
$splitValName = explode( '.', $data['field'] );
|
||||
$fieldVal = null;
|
||||
|
||||
// Check for array values like: toggleListName['option1'].
|
||||
if ( 2 === count( $splitValName ) && isset( $attributes[ $splitValName[0] ] ) && isset( $attributes[ $splitValName[0] ][ $splitValName[1] ] ) ) {
|
||||
$fieldVal = $attributes[ $splitValName[0] ][ $splitValName[1] ];
|
||||
}
|
||||
|
||||
// Check for normal values.
|
||||
if ( null === $fieldVal && isset( $attributes[ $data['field'] ] ) ) {
|
||||
$fieldVal = $attributes[ $data['field'] ];
|
||||
}
|
||||
|
||||
// Check count.
|
||||
if ( isset( $data['count'] ) ) {
|
||||
$count = explode( $data['count'], $fieldVal );
|
||||
$fieldVal = count( $count ) - 1;
|
||||
}
|
||||
|
||||
if ( null !== $fieldVal ) {
|
||||
$result = self::compare( $result, $relation, self::compare( $fieldVal, isset( $data['operator'] ) ? $data['operator'] : '===', isset( $data['value'] ) ? $data['value'] : true ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user