如何使用do_action_ref_array?

时间:2017-02-03 作者:rtpHarry

我有一个插件,里面有这个方法,应该有一个用于自定义验证的挂钩。我有两个问题:

我不知道该如何调用它(已在下面发布了我的尝试)

  • 插件中似乎有一个bug,因为我无法使用$cart\\u键
  • 带挂钩的插件代码:

    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 \\stdClass
                     */
                    do_action_ref_array( \'bookly_validate_custom_field\', array( $field, &$this->errors, $fields[ $field->id ] ) );
                }
            }
        }
    }
    
    函数中的我的代码。php:

    /**
     * Adds the custom validator for postcodes
     */
    function postcode_custom_validator_action($field, $errors, $fieldId) {
        if ( $fieldId == 16020 ) {
            $postcodesCoveredSetting = get_theme_mod( \'postcode_textbox\' );
            if(!isValidPostcode($field->value, $postcodesCoveredSetting)) {
                // Validator.php says: "if the value is not valid then please add an error message like in the above example."
                // which looks like this:
                // $this->errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( \'Required\', \'bookly\' );
                // but $cart_key not passed through it seems?
                $errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( "Sorry, we only cover " . $postcodesCoveredSetting . " postcode areas at the moment", \'bookly\' );
            }
        }
    }
    add_action ( \'bookly_validate_custom_field\', \'postcode_custom_validator_action\', 10, 3 ); // 10 is priority, 3 is number of params in array?
    

    1 个回复
    SO网友:rtpHarry

    我尚未对此进行测试,但有人建议:

    add_action( \'bookly_validate_custom_field\', function ( \\stdClass $field, &$errors, $cart_key, \\stdClass $field_info ) {
        // Validation by custom_field id
        switch ( $field->id ) {
            case \'id_value\':
                if ( /*$invalid == */ true ) {
                    $errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( \'Invalid\', \'bookly\' );
                }
                break;
        }
    }, 10, 4 );
    

    相关推荐

    使用快速编辑更新POST时,POST_ROW_ACTIONS过滤器不工作

    我使用post\\u row\\u操作创建了两个链接,页面加载时显示良好。但使用快速编辑更新帖子后,链接将消失。是WordPress的bug还是我错过了什么。我的代码如下所示。add_filter(\'post_row_actions\', \'add_new_link\', 10, 2); public function add_new_link($actions, $page_object) { $actions[\'new_action\'] = \'<