first commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
const TokenList = wp.tokenList;
|
||||
|
||||
/**
|
||||
* Returns the active style from the given className.
|
||||
*
|
||||
* @param {string} className Class name
|
||||
* @param {string} namespace The replacing class namespace.
|
||||
*
|
||||
* @return {string} The active style.
|
||||
*/
|
||||
export function getActiveClass( className, namespace ) {
|
||||
const list = new TokenList( className );
|
||||
|
||||
for ( const activeClass of list.values() ) {
|
||||
if ( activeClass.indexOf( `${ namespace }-` ) === -1 ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return activeClass;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the active class with namespace in the className.
|
||||
*
|
||||
* @param {string} className Class name.
|
||||
* @param {string} namespace The replacing class namespace.
|
||||
* @param {string} newClass The replacing class.
|
||||
*
|
||||
* @return {string} The updated className.
|
||||
*/
|
||||
export function replaceClass( className, namespace, newClass ) {
|
||||
const list = new TokenList( className );
|
||||
const namespaceRegExp = new RegExp( `${ namespace }-` );
|
||||
|
||||
// remove classes with the same namespace.
|
||||
for ( const activeClass of list.values() ) {
|
||||
if ( namespaceRegExp.test( activeClass ) ) {
|
||||
list.remove( activeClass );
|
||||
}
|
||||
}
|
||||
|
||||
// add new class.
|
||||
if ( newClass ) {
|
||||
list.add( `${ namespace }-${ newClass }` );
|
||||
}
|
||||
|
||||
return list.value;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Generating dynamic styles by key.
|
||||
*
|
||||
* @param {string} suffix suffix
|
||||
* @param {string} key key
|
||||
* @param {string} styles styles
|
||||
*/
|
||||
export default function dynamicStylesBYkey( suffix, key, styles ) {
|
||||
if ( ! key || ! styles || 'undefined' === typeof( key ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
var styleID = `dynamic-css-${suffix}-${key}`;
|
||||
|
||||
if ( document.getElementById( styleID ) ) {
|
||||
|
||||
document.getElementById( styleID ).innerHTML = styles;
|
||||
} else {
|
||||
// Get the head element.
|
||||
var head = document.head || document.getElementsByTagName('head')[0];
|
||||
|
||||
// Create a new style node.
|
||||
var style = document.createElement( 'style' );
|
||||
|
||||
style.id = styleID;
|
||||
|
||||
// Set type attribute for the style node.
|
||||
style.type = 'text/css';
|
||||
|
||||
// Append the css rules to the style node.
|
||||
style.appendChild( document.createTextNode( styles ) );
|
||||
|
||||
// Append the style node to the head of the page.
|
||||
head.appendChild(style);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Get parent block data.
|
||||
*
|
||||
* @param {Object} rootBlock root block data
|
||||
* @param {Object} currentBlock current block data
|
||||
* @return {Object}
|
||||
*/
|
||||
export default function getParentBlock(rootBlock, currentBlock) {
|
||||
if (rootBlock && rootBlock.clientId === currentBlock.clientId) {
|
||||
return rootBlock;
|
||||
}
|
||||
|
||||
let result = false;
|
||||
|
||||
// Check all inner blocks to find parent block.
|
||||
if (rootBlock && rootBlock.innerBlocks && rootBlock.innerBlocks.length) {
|
||||
rootBlock.innerBlocks.forEach((innerBlockData) => {
|
||||
const innerParent = getParentBlock(innerBlockData, currentBlock);
|
||||
|
||||
if (!result && innerParent) {
|
||||
result = innerParent.clientId === currentBlock.clientId ? rootBlock : innerParent;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Check if core block support extensions.
|
||||
*
|
||||
* @param {string} name block name
|
||||
*
|
||||
* @return {boolean}
|
||||
*/
|
||||
export default function isCoreBlockWithExt(name) {
|
||||
return name &&
|
||||
/^core/.test(name) &&
|
||||
! /^core\/block$/.test(name) &&
|
||||
! /^core\/tag-cloud/.test(name) &&
|
||||
! /^core\/calendar/.test(name) &&
|
||||
! /^core\/latest-comments/.test(name) &&
|
||||
! /^core\/rss/.test(name) &&
|
||||
! /^core\/archives/.test(name) &&
|
||||
! /^core\/template/.test(name);
|
||||
}
|
||||
@@ -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