Skip to content

Commit

Permalink
Merge pull request #890 from takayukister/dev/5.6
Browse files Browse the repository at this point in the history
Update config-validator.php
  • Loading branch information
takayukister authored Jul 18, 2022
2 parents 3d25477 + 26ffe18 commit ff0b449
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions includes/config-validator.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php

/**
* Configuration validator.
*
* @link https://contactform7.com/configuration-errors/
*/
class WPCF7_ConfigValidator {

/**
* The plugin version in which important updates happened last time.
*/
const last_important_update = '5.6.1';

const error = 100;
Expand All @@ -20,6 +28,10 @@ class WPCF7_ConfigValidator {
const error_dots_in_names = 113;
const error_colons_in_names = 114;


/**
* Returns a URL linking to the documentation page for the error type.
*/
public static function get_doc_link( $error_code = '' ) {
$url = __( 'https://contactform7.com/configuration-errors/',
'contact-form-7'
Expand All @@ -34,21 +46,34 @@ public static function get_doc_link( $error_code = '' ) {
return esc_url( $url );
}


private $contact_form;
private $errors = array();

public function __construct( WPCF7_ContactForm $contact_form ) {
$this->contact_form = $contact_form;
}


/**
* Returns the contact form object that is tied to this validator.
*/
public function contact_form() {
return $this->contact_form;
}


/**
* Returns true if no error has been detected.
*/
public function is_valid() {
return ! $this->count_errors();
}


/**
* Counts detected errors.
*/
public function count_errors( $args = '' ) {
$args = wp_parse_args( $args, array(
'section' => '',
Expand Down Expand Up @@ -84,6 +109,10 @@ public function count_errors( $args = '' ) {
return $count;
}


/**
* Collects messages for detected errors.
*/
public function collect_error_messages() {
$error_messages = array();

Expand Down Expand Up @@ -117,6 +146,10 @@ public function collect_error_messages() {
return $error_messages;
}


/**
* Builds an error message by replacing placeholders.
*/
public function build_message( $message, $params = '' ) {
$params = wp_parse_args( $params, array() );

Expand All @@ -135,6 +168,11 @@ public function build_message( $message, $params = '' ) {
return $message;
}


/**
* Returns a default message that is used when the message for the error
* is not specified.
*/
public function get_default_message( $code ) {
switch ( $code ) {
case self::error_maybe_empty:
Expand All @@ -156,6 +194,15 @@ public function get_default_message( $code ) {
}
}


/**
* Adds a validation error.
*
* @param string $section The section where the error detected.
* @param int $code The unique code of the error.
* This must be one of the class constants.
* @param string|array $args Optional options for the error.
*/
public function add_error( $section, $code, $args = '' ) {
$args = wp_parse_args( $args, array(
'message' => '',
Expand All @@ -171,6 +218,10 @@ public function add_error( $section, $code, $args = '' ) {
return true;
}


/**
* Removes an error.
*/
public function remove_error( $section, $code ) {
if ( empty( $this->errors[$section] ) ) {
return;
Expand All @@ -188,6 +239,12 @@ public function remove_error( $section, $code ) {
}
}


/**
* The main validation runner.
*
* @return bool True if there is no error detected.
*/
public function validate() {
$this->errors = array();

Expand Down

0 comments on commit ff0b449

Please sign in to comment.