通过修改一些插件代码,我成功地获得了我想要的行为。然而,我想使用提供的钩子将我的修改移到插件代码之外,但我似乎无法使其工作。挂钩使用do\\u action\\u ref\\u数组。
我能找到的访问php变量的唯一方法是向调试程序发送错误消息,这一事实无助于这种情况。日志文件。
(像这样):
function ra_func1($args) {
$str = print_r($args, true);
error_log($str);
}
add_action ( \'bookly_validate_custom_field\', \'ra_func1\', 10, 3);
问题是对象和数组混合在一起,我现在真的被卡住了,我花了很多时间试图让它工作。如果有人能提供一个符合我要求的工作回调,我将不胜感激。
原始插件代码:
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 ] ) );
}
}
}
}
代码中包含我要通过外部回调添加的功能的开头:
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\' );
}
}
}
}
}