访问WordPress

时间:2017-07-11 09:55:45

标签: php wordpress reference

我设法通过修改一些插件代码来获得我想要的行为。但是,我想使用提供的钩子在插件代码之外移动我的修改,但我似乎无法使其工作。钩子使用WP函数do_action_ref_array(https://developer.wordpress.org/reference/functions/do_action_ref_array/)。

我能找到访问php变量的唯一方法是通过向debug.log文件发送错误消息,这种情况没有帮助。

(如此):

function ra_func1($args) {
    $str = print_r($args, true);
    error_log($str);
}
add_action ( 'bookly_validate_custom_field', 'ra_func1', 10, 3);

麻烦的是混合了物体和阵列,我现在真的陷入了困境,花了很多时间试图让它发挥作用。如果有人能提供一个可以做我想做的工作回调,我将非常感激。

问题的关键似乎是"如何通过do_action_ref_array访问和修改传递给回调的值?他们似乎已经失去了他们的名字而且属于不同的类型。"

原始插件代码:

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' );                      
                                         }
                }
            }
        }
    }

0 个答案:

没有答案
相关问题