$field Field (`payment` bool, `targets` Lookup source, `targets_label`).
* @param mixed $value Saved rules.
* @param string $id Element ID.
* @return void
*/
public static function render( array $field, $value, $id ) {
$payment = ! empty( $field['payment'] );
$fields = Rules::fields( $payment );
$kinds = array_map( static fn( $f ) => $f['kind'], $fields );
$ops = array(
'number' => Rules::operators( 'number' ),
'set' => Rules::operators( 'set' ),
);
$rules = Rules::well_formed( $value );
?>
$rule ) : ?>
>
$def ) : ?>
$rule Rule (empty for the template).
* @param array $field Field.
* @param array> $fields Condition fields.
* @return void
*/
private static function rule( $uid, array $rule, array $field, array $fields ) {
$conditions = (array) ( $rule['conditions'] ?? array( array() ) );
?>
'apwc_rt_' . $uid,
'type' => 'search',
'source' => (string) $field['targets'],
'label' => (string) $field['targets_label'],
'placeholder' => (string) $field['targets_label'],
),
(array) ( $rule['targets'] ?? array() )
);
?>
>
$rule Rule.
* @return bool
*/
private static function complete( array $rule ) {
if ( empty( $rule['targets'] ) || empty( $rule['conditions'] ) ) {
return false;
}
foreach ( (array) $rule['conditions'] as $c ) {
if ( ! is_array( $c ) || ! isset( $c['value'] ) || array() === $c['value'] || '' === $c['value'] ) {
return false;
}
}
return true;
}
/**
* One condition row.
*
* @param string $uid Unique key.
* @param array $condition Condition (empty for the template).
* @param array> $fields Fields.
* @return void
*/
private static function condition( $uid, array $condition, array $fields ) {
$field = is_string( $condition['field'] ?? null ) ? $condition['field'] : '';
$key = isset( $fields[ $field ] ) ? $field : (string) array_key_first( $fields );
$def = $fields[ $key ];
$op = is_string( $condition['op'] ?? null ) ? $condition['op'] : '';
?>
$def Field definition.
* @param mixed $value Value (number, or a list of IDs).
* @return void
*/
private static function value( $uid, $key, array $def, $value ) {
if ( 'number' === $def['kind'] ) {
$value = is_scalar( $value ) ? $value : null;
printf(
'',
esc_attr( $uid ),
esc_html( $def['label'] ),
esc_attr( null === $value ? '' : (string) $value )
);
return;
}
Fields::render(
'rules',
array(
'id' => 'apwc_rc_' . $uid,
'type' => 'search',
'source' => (string) $def['source'],
'label' => $def['label'],
'placeholder' => __( 'Search…', 'admin-powerups-for-woocommerce' ),
),
array_values( array_filter( (array) $value, 'is_scalar' ) )
);
}
}