Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed Oct 5, 2023
1 parent 8038129 commit c43ee3c
Showing 1 changed file with 109 additions and 48 deletions.
157 changes: 109 additions & 48 deletions includes/Checker/Checks/Trademarks_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace WordPress\Plugin_Check\Checker\Checks;

use Exception;
use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Traits\Stable_Check;
Expand Down Expand Up @@ -210,12 +211,12 @@ protected function check_files( Check_Result $result, array $files ) {

// Check the trademarks in readme file plugin name.
if ( $this->flags & self::TYPE_README ) {
$this->check_for_readme( $result, $files );
$this->check_for_name_in_readme( $result, $files );
}

// Check the trademarks in plugin name.
if ( $this->flags & self::TYPE_NAME ) {
$this->check_for_name( $result );
$this->check_for_name_in_main_file( $result );
}

// Check the trademarks in plugin slug.
Expand All @@ -232,7 +233,7 @@ protected function check_files( Check_Result $result, array $files ) {
* @param Check_Result $result The Check Result to amend.
* @param array $files Array of plugin files.
*/
private function check_for_readme( Check_Result $result, array $files ) {
private function check_for_name_in_readme( Check_Result $result, array $files ) {
$plugin_relative_path = $result->plugin()->path();

// Find the readme file.
Expand Down Expand Up @@ -283,12 +284,11 @@ function ( $file ) {

$name = isset( $matches[1] ) ? $matches[1] : '';

$this->look_for_trademark(
$result,
str_replace( $result->plugin()->path(), '', $file ),
$name,
__( 'The plugin name includes a restricted term.', 'plugin-check' )
);
try {
$this->validate_name_has_no_trademarks( $name );
} catch ( Exception $e ) {
$this->add_result_error_for_file( $result, $file, $e->getMessage() );
}
}

/**
Expand All @@ -298,7 +298,7 @@ function ( $file ) {
*
* @param Check_Result $result The Check Result to amend.
*/
private function check_for_name( Check_Result $result ) {
private function check_for_name_in_main_file( Check_Result $result ) {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
Expand All @@ -307,12 +307,11 @@ private function check_for_name( Check_Result $result ) {
$plugin_header = get_plugin_data( $plugin_main_file );

if ( ! empty( $plugin_header['Name'] ) ) {
$this->look_for_trademark(
$result,
$plugin_main_file,
$plugin_header['Name'],
__( 'The plugin name includes a restricted term.', 'plugin-check' )
);
try {
$this->validate_name_has_no_trademarks( $plugin_header['Name'] );
} catch ( Exception $e ) {
$this->add_result_error_for_file( $result, $plugin_main_file, $e->getMessage() );
}
}
}

Expand All @@ -326,30 +325,28 @@ private function check_for_name( Check_Result $result ) {
private function check_for_slug( Check_Result $result ) {
$plugin_slug = basename( $result->plugin()->path() );

$this->look_for_trademark(
$result,
WP_PLUGIN_DIR . '/' . $result->plugin()->basename(),
$plugin_slug,
__( 'The plugin slug includes a restricted term.', 'plugin-check' )
);
try {
$this->validate_slug_has_no_trademarks( $plugin_slug );
} catch ( Exception $e ) {
$this->add_result_error_for_file( $result, WP_PLUGIN_DIR . '/' . $result->plugin()->basename(), $e->getMessage() );
}
}

/**
* Determines if we find a trademarked term.
* Determines if we find a trademarked term in plugin name.
*
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param string $file The plugin file.
* @param string $input The plugin name or slug.
* @param string $preamble The preamble message.
* @param string $plugin_name The plugin name.
*
* @throws Exception Thrown if we found trademarked term in plugin name.
*/
private function look_for_trademark( $result, $file, $input, $preamble ) {
if ( empty( $input ) ) {
private function validate_name_has_no_trademarks( $plugin_name ) {
if ( empty( $plugin_name ) ) {
return;
}

$check = $this->has_trademarked_slug( $input );
$check = $this->has_trademarked_slug( $plugin_name );
if ( ! $check ) {
return;
}
Expand All @@ -360,37 +357,81 @@ private function look_for_trademark( $result, $file, $input, $preamble ) {
) {
// Trademarks that do NOT end in "-", but are within the FOR_USE_EXCEPTIONS array can be used, but only if it ends with 'for x'.
$message = sprintf(
/* translators: 1: plugin name or slug, 2: found trademarked term */
__( 'Your chosen plugin name - %1$s - contains the restricted term "%2$s" which cannot be used within in your plugin name, unless your plugin name ends with "for %2$s". The term must still not appear anywhere else in your name.', 'plugin-check' ),
'<code>' . esc_html( $input ) . '</code>',
/* translators: 1: plugin name, 2: found trademarked term */
__( 'The plugin name includes a restricted term. Your chosen plugin name - %1$s - contains the restricted term "%2$s" which cannot be used within in your plugin name, unless your plugin name ends with "for %2$s". The term must still not appear anywhere else in your name.', 'plugin-check' ),
'<code>' . esc_html( $plugin_name ) . '</code>',
esc_html( trim( $check, '-' ) )
);
} elseif ( trim( $check, '-' ) === $check ) {
// Trademarks that do NOT end in "-" indicate slug cannot contain term at all.
$message = sprintf(
/* translators: 1: plugin name or slug, 2: found trademarked term */
__( 'Your chosen plugin name - %1$s - contains the restricted term "%2$s" which cannot be used at all in your plugin name.', 'plugin-check' ),
'<code>' . esc_html( $input ) . '</code>',
/* translators: 1: plugin name, 2: found trademarked term */
__( 'The plugin name includes a restricted term. Your chosen plugin name - %1$s - contains the restricted term "%2$s" which cannot be used at all in your plugin name.', 'plugin-check' ),
'<code>' . esc_html( $plugin_name ) . '</code>',
esc_html( trim( $check, '-' ) )
);
} else {
// Trademarks ending in "-" indicate slug cannot BEGIN with that term.
$message = sprintf(
/* translators: 1: plugin name or slug, 2: found trademarked term */
__( 'Your chosen plugin name - %1$s - contains the restricted term "%2$s" and cannot be used to begin your plugin name. We disallow the use of certain terms in ways that are abused, or potentially infringe on and/or are misleading with regards to trademarks. You may use the term "%2$s" elsewhere in your plugin name, such as "... for %2$s".', 'plugin-check' ),
'<code>' . esc_html( $input ) . '</code>',
/* translators: 1: plugin name, 2: found trademarked term */
__( 'The plugin name includes a restricted term. Your chosen plugin name - %1$s - contains the restricted term "%2$s" and cannot be used to begin your plugin name. We disallow the use of certain terms in ways that are abused, or potentially infringe on and/or are misleading with regards to trademarks. You may use the term "%2$s" elsewhere in your plugin name, such as "... for %2$s".', 'plugin-check' ),
'<code>' . esc_html( $plugin_name ) . '</code>',
esc_html( trim( $check, '-' ) )
);
}

$result->add_message(
true,
$preamble . ' ' . $message,
array(
'code' => 'trademarked_term',
'file' => $file,
)
);
throw new Exception( $message );
}

/**
* Determines if we find a trademarked term in plugin slug.
*
* @since n.e.x.t
*
* @param string $plugin_slug The plugin slug.
*
* @throws Exception Thrown if we found trademarked term in plugin slug.
*/
private function validate_slug_has_no_trademarks( $plugin_slug ) {
if ( empty( $plugin_slug ) ) {
return;
}

$check = $this->has_trademarked_slug( $plugin_slug );
if ( ! $check ) {
return;
}

if (
trim( $check, '-' ) === $check
&& in_array( $check, self::FOR_USE_EXCEPTIONS, true )
) {
// Trademarks that do NOT end in "-", but are within the FOR_USE_EXCEPTIONS array can be used, but only if it ends with 'for x'.
$message = sprintf(
/* translators: 1: plugin slug, 2: found trademarked term */
__( 'The plugin slug includes a restricted term. Your plugin slug - %1$s - contains the restricted term "%2$s" which cannot be used within in your plugin slug, unless your plugin slug ends with "for %2$s". The term must still not appear anywhere else in your plugin slug.', 'plugin-check' ),
'<code>' . esc_html( $plugin_slug ) . '</code>',
esc_html( trim( $check, '-' ) )
);
} elseif ( trim( $check, '-' ) === $check ) {
// Trademarks that do NOT end in "-" indicate slug cannot contain term at all.
$message = sprintf(
/* translators: 1: plugin slug, 2: found trademarked term */
__( 'The plugin slug includes a restricted term. Your plugin slug - %1$s - contains the restricted term "%2$s" which cannot be used at all in your plugin slug.', 'plugin-check' ),
'<code>' . esc_html( $plugin_slug ) . '</code>',
esc_html( trim( $check, '-' ) )
);
} else {
// Trademarks ending in "-" indicate slug cannot BEGIN with that term.
$message = sprintf(
/* translators: 1: plugin slug, 2: found trademarked term */
__( 'The plugin slug includes a restricted term. Your plugin slug - %1$s - contains the restricted term "%2$s" and cannot be used to begin your plugin slug. We disallow the use of certain terms in ways that are abused, or potentially infringe on and/or are misleading with regards to trademarks. You may use the term "%2$s" elsewhere in your plugin slug, such as "... for %2$s".', 'plugin-check' ),
'<code>' . esc_html( $plugin_slug ) . '</code>',
esc_html( trim( $check, '-' ) )
);
}

throw new Exception( $message );
}

/**
Expand Down Expand Up @@ -446,7 +487,7 @@ private function has_trademarked_slug( $slug ) {
*
* @since n.e.x.t
*
* @param string $slug The plugin slug.
* @param string $slug The plugin slug.
* @param string $trademark The trademark term.
* @return bool True if the trademark is valid with a for-use exception, false otherwise.
*/
Expand Down Expand Up @@ -479,4 +520,24 @@ private function is_valid_for_use_exception( $slug, $trademark ) {
// If the trademark still doesn't exist in the slug, it's OK.
return false === strpos( $short_slug, $trademark );
}

/**
* Amends the given result with an error for the given file, code, and message.
*
* @since n.e.x.t
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @param string $file Absolute path to the file found.
* @param string $message Error message.
*/
private function add_result_error_for_file( Check_Result $result, $file, $message ) {
$result->add_message(
true,
$message,
array(
'code' => 'trademarked_term',
'file' => str_replace( $result->plugin()->path(), '', $file ),
)
);
}
}

0 comments on commit c43ee3c

Please sign in to comment.