I've managed to get the behaviour I want by modifying some plugin code. However, I would like to move my modifications outside of the plugin code using the provided hook, but I can't seem make it work. The hook uses do_action_ref_array.
The situation is not helped by the fact that the only way I can find to access the php vars is by sending error messages to the debug.log file.
(like so):
function ra_func1($args) {
$str = print_r($args, true);
error_log($str);
}
add_action ( 'bookly_validate_custom_field', 'ra_func1', 10, 3);
Trouble is there's a mixture of objects and arrays and I'm now really stuck, having spent a lot of time trying to get this working. If anyone could provide a working callback that does what I want I would be very grateful.
Original plugin code:
public function validateCustomFields( $value, $form_id, $cart_key )
{
$decoded_value = json_decode( $value );
$fields = array();
foreach ( json_decode( get_option( 'bookly_custom_fields' ) ) as $field ) {
$fields[ $field->id ] = $field;
}
foreach ( $decoded_value as $field ) {
if ( isset( $fields[ $field->id ] ) ) {
if ( ( $fields[ $field->id ]->type == 'captcha' ) && ! Captcha\Captcha::validate( $form_id, $field->value ) ) {
$this->errors['custom_fields'][ $cart_key ][ $field->id ] = __( 'Incorrect code', 'bookly' );
} elseif ( $fields[ $field->id ]->required && empty ( $field->value ) && $field->value != '0' ) {
$this->errors['custom_fields'][ $cart_key ][ $field->id ] = __( 'Required', 'bookly' );
} else {
/**
* Custom field validation for a third party,
* if the value is not valid then please add an error message like in the above example.
*
* @param \stdClass
* @param ref array
* @param string
* @param \stdClass
*/
do_action_ref_array( 'bookly_validate_custom_field', array( $field, &$this->errors, $cart_key, $fields[ $field->id ] ) );
}
}
}
}
Code with the beginnings of the functionality I want to add via an external callback:
public function validateCustomFields( $value, $form_id, $cart_key )
{
$decoded_value = json_decode( $value );
$fields = array();
foreach ( json_decode( get_option( 'bookly_custom_fields' ) ) as $field ) {
$fields[ $field->id ] = $field;
}
foreach ( $decoded_value as $field ) {
if ( isset( $fields[ $field->id ] ) ) {
if ( ( $fields[ $field->id ]->type == 'captcha' ) && ! Captcha\Captcha::validate( $form_id, $field->value ) ) {
$this->errors['custom_fields'][ $cart_key ][ $field->id ] = __( 'Incorrect code', 'bookly' );
} elseif ( $fields[ $field->id ]->required && empty ( $field->value ) && $field->value != '0' ) {
$this->errors['custom_fields'][ $cart_key ][ $field->id ] = __( 'Required', 'bookly' );
} else {
/**
* Custom field validation for a third party,
* if the value is not valid then please add an error message like in the above example.
*
* @param \stdClass
* @param ref array
* @param string
* @param \stdClass
*/
//
if ($fields[$field->id]->id == 12372){
$this->errors['custom_fields'][ $cart_key ][ $field->id ] = __( 'Post Code Error', 'bookly' );
}
}
}
}
}