Skip to content

Commit

Permalink
Fix: no warning for valid records
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrox committed Mar 1, 2024
1 parent c314ad9 commit 2e41904
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 15 deletions.
54 changes: 39 additions & 15 deletions inc/save.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ function save() {
$ays = isset( $_post['adstxt_ays'] ) ? $_post['adstxt_ays'] : null;

// Different browsers use different line endings.
$lines = preg_split( '/\r\n|\r|\n/', $_post['adstxt'] );
$sanitized = array();
$errors = array();
$warnings = array();
$response = array();
$has_placeholder_record = false;
$lines = preg_split( '/\r\n|\r|\n/', $_post['adstxt'] );
$sanitized = array();
$errors = array();
$warnings = array();
$response = array();
$has_only_placeholder_records = null;

foreach ( $lines as $i => $line ) {
$line_number = $i + 1;
$result = validate_line( $line, $line_number, $has_placeholder_record );
$result = validate_line( $line, $line_number, $has_only_placeholder_records );

$sanitized[] = $result['sanitized'];
if ( ! empty( $result['errors'] ) ) {
Expand All @@ -46,7 +46,24 @@ function save() {
}

if ( ! empty( $result['is_placeholder_record'] ) ) {
$has_placeholder_record = true;
if ( is_null( $has_only_placeholder_records ) ) {
$has_only_placeholder_records = true;
}
}

list( 'errors' => $errors_data, 'warnings' => $warnings_data, 'is_placeholder_record' => $is_placeholder, 'is_empty_record' => $is_empty_line, 'is_comment' => $is_comment ) = $result;

// Check if the line is valid, then set $has_only_placeholder_records to false.
if ( empty( $is_placeholder ) && empty( $errors_data ) && empty( $warnings_data ) && ( ! $is_comment && ! $is_empty_line ) ) {
$has_only_placeholder_records = false;
}
}

// If $has_only_placeholder_records is false, remove no_authorized_seller warning.
if ( false === $has_only_placeholder_records ) {
$key = array_search( 'no_authorized_seller', array_column( $warnings, 'type' ) );
if ( false !== $key ) {
unset( $warnings[ $key ] );
}
}

Expand Down Expand Up @@ -98,27 +115,32 @@ function save() {
/**
* Validate a single line.
*
* @param string $line The line to validate.
* @param string $line_number The line number being evaluated.
* @param string $has_placeholder_record Flag for presence of placeholder record.
* @param string $line The line to validate.
* @param string $line_number The line number being evaluated.
* @param string $has_only_placeholder_records Flag for presence of placeholder record.
*
* @return array {
* @type string $sanitized Sanitized version of the original line.
* @type array $errors Array of errors associated with the line.
* }
*/
function validate_line( $line, $line_number, $has_placeholder_record = false ) {
function validate_line( $line, $line_number, $has_only_placeholder_records = null ) {
static $record_lines = 0;
$is_placeholder_record = false;
$is_empty_record = false;
$is_comment = false;

// Only to count for records, not comments/variables.
$domain_regex = '/^((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}$/i';
$errors = array();
$warnings = array();

if ( empty( $line ) ) {
$sanitized = '';
$sanitized = '';
$is_empty_record = true;
} elseif ( 0 === strpos( $line, '#' ) ) { // This is a full-line comment.
$sanitized = wp_strip_all_tags( $line );
$sanitized = wp_strip_all_tags( $line );
$is_comment = true;
} elseif ( 1 < strpos( $line, '=' ) ) { // This is a variable declaration.
// The spec currently supports CONTACT, INVENTORYPARTNERDOMAIN, SUBDOMAIN, OWNERDOMAIN and MANAGERDOMAIN.
if ( ! preg_match( '/^(CONTACT|SUBDOMAIN|INVENTORYPARTNERDOMAIN|OWNERDOMAIN|MANAGERDOMAIN)=/i', $line ) ) {
Expand Down Expand Up @@ -164,7 +186,7 @@ function validate_line( $line, $line_number, $has_placeholder_record = false ) {
$is_placeholder_record = is_placeholder_record( $exchange, $pub_id, $account_type, $fields[3] ? trim( $fields[3] ) : null );

// If the file contains placeholder record and no placeholder was already present, set variable.
if ( $is_placeholder_record && ! $has_placeholder_record ) {
if ( $is_placeholder_record && is_null( $has_only_placeholder_records ) ) {
$warnings[] = array(
'type' => 'no_authorized_seller',
'message' => __( 'Your ads.txt indicates no authorized advertising sellers.', 'ads-txt' ),
Expand Down Expand Up @@ -223,6 +245,8 @@ function validate_line( $line, $line_number, $has_placeholder_record = false ) {
'errors' => $errors,
'warnings' => $warnings,
'is_placeholder_record' => $is_placeholder_record,
'is_empty_record' => $is_empty_record,
'is_comment' => $is_comment,
);
}

Expand Down
45 changes: 45 additions & 0 deletions tests/unit/test-save.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function data_provider_for_validate_line() {
'errors' => array(),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => true,
'is_comment' => false,
),
),
'Validate comment' => array(
Expand All @@ -53,6 +55,8 @@ public function data_provider_for_validate_line() {
'errors' => array(),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => true,
),
),
'Validate CONTACT var' => array(
Expand All @@ -63,6 +67,8 @@ public function data_provider_for_validate_line() {
'errors' => array(),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Validate SUBDOMAIN var' => array(
Expand All @@ -73,6 +79,8 @@ public function data_provider_for_validate_line() {
'errors' => array(),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Validate INVENTORYPARTNERDOMAIN var' => array(
Expand All @@ -83,6 +91,8 @@ public function data_provider_for_validate_line() {
'errors' => array(),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Invalid var' => array(
Expand All @@ -98,6 +108,8 @@ public function data_provider_for_validate_line() {
),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Invalid SUBDOMAIN var' => array(
Expand All @@ -114,6 +126,8 @@ public function data_provider_for_validate_line() {
),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Validate reseller record' => array(
Expand All @@ -124,6 +138,8 @@ public function data_provider_for_validate_line() {
'errors' => array(),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Validate direct record' => array(
Expand All @@ -134,6 +150,8 @@ public function data_provider_for_validate_line() {
'errors' => array(),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Validate commented record' => array(
Expand All @@ -144,6 +162,8 @@ public function data_provider_for_validate_line() {
'errors' => array(),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Invalid exchange' => array(
Expand All @@ -160,6 +180,8 @@ public function data_provider_for_validate_line() {
),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Invalid account type' => array(
Expand All @@ -175,6 +197,8 @@ public function data_provider_for_validate_line() {
),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Invalid record' => array(
Expand All @@ -190,6 +214,8 @@ public function data_provider_for_validate_line() {
),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'Multiple errors' => array(
Expand All @@ -210,6 +236,25 @@ public function data_provider_for_validate_line() {
),
'warnings' => array(),
'is_placeholder_record' => false,
'is_empty_record' => false,
'is_comment' => false,
),
),
'No authorized advertising sellers' => array(
'line' => 'placeholder.example.com,placeholder,DIRECT,placeholder',
'line_number' => 132,
'expected' => array(
'sanitized' => 'placeholder.example.com,placeholder,DIRECT,placeholder',
'errors' => array(),
'warnings' => array(
array(
'type' => 'no_authorized_seller',
'message' => 'Your ads.txt indicates no authorized advertising sellers.',
),
),
'is_placeholder_record' => true,
'is_empty_record' => false,
'is_comment' => false,
),
),
);
Expand Down

0 comments on commit 2e41904

Please sign in to comment.