Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ In your project's `composer.json`, add the following:
],
"scripts": {
"post-install-cmd": [
"./10up-lib/wp-compat-validation-tool/replace-namespace.sh <New_Name_Space>"
"./10up-lib/wp-compat-validation-tool/replace-namespace.sh <New_Name_Space> <Translation-Domain>"
],
"post-update-cmd": [
"./10up-lib/wp-compat-validation-tool/replace-namespace.sh <New_Name_Space>"
"./10up-lib/wp-compat-validation-tool/replace-namespace.sh <New_Name_Space> <Translation-Domain>"
]
},
"extra": {
Expand All @@ -43,6 +43,9 @@ In your project's `composer.json`, add the following:
Replace `<New_Name_Space>` with a unique namespace specific to your project.
The `WP_Compat_Validation_Tools` namespace will be replaced by `<New_Name_Space>` to avoid namespace collisions in situations where multiple plugins use this package as their dependencies.

Replace `<Translation-Domain>` with a unique translation domain specific to your project.
This is usually the slug for your theme or plugin. The `wp-compat-validation-tool` translation domain will be replaced by `<Translation-Domain>`.

## Usage

```php
Expand Down
20 changes: 18 additions & 2 deletions replace-namespace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,35 @@
SCRIPT_DIR="$(dirname "$0")"
SCRIPT_NAME="$(basename "$0")"

# Check for the required argument
if [ "$#" -ne 1 ]; then
# Check for the required first argument
if [ -z "$1" ]; then
echo "Usage: $0 New_Namespace"
exit 1
fi

NEW_NAMESPACE="$1"

# Check for the optional translation domain argument and set it if provided
if [ -n "$2" ]; then
TRANSLATION_DOMAIN="$2"
else
echo "No translation domain provided. Skipping translation domain replacement."
fi

# Use find to get all files recursively from the script's directory, excluding the script itself
find "$SCRIPT_DIR" -type f \( -name "*.php" -o -name "*.json" \) ! -name "$SCRIPT_NAME" | while read -r file; do
echo $file
# Use perl for the replacement in each file
perl -pi -e "s/WP_Compat_Validation_Tool/$NEW_NAMESPACE/g" "$file"
done

# If a new translation domain was provided, replace the old one with the new one
if [ -n "$TRANSLATION_DOMAIN" ]; then
find "$SCRIPT_DIR" -type f \( -name "*.php" -o -name "*.json" \) ! -name "$SCRIPT_NAME" | while read -r file; do
echo $file
# Replace the exact string when surrounded by either single or double quotes
perl -pi -e "s/(['\"])wp-compat-validation-tool\\1/\\1$TRANSLATION_DOMAIN\\1/g" "$file"
done
fi

cd 10up-lib/wp-compat-validation-tool && rm -rf .git .github .gitignore composer.json composer.lock CHANGELOG.md CONTRIBUTING.md README.md LICENSE.md CODE_OF_CONDUCT.md CREDITS.md replace-namespace.sh
50 changes: 47 additions & 3 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
namespace WP_Compat_Validation_Tool;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

class Validator {
/**
* Array of checks.
Expand Down Expand Up @@ -104,6 +108,27 @@ public function set_wordpress_max_required_version( $value = '' ) {
return $this;
}

/**
* Returns the current WordPress version.
*
* Returns an unmodified value of `$wp_version`. Some plugins modify the global
* in an attempt to improve security through obscurity. This practice can cause
* errors in WordPress, so the ability to get an unmodified version is needed.
*
* Ports the WordPress function `wp_get_wp_version()` available in WordPress 6.7.0 and later.
*
* @return string The current WordPress version.
*/
public function wp_get_wp_version() {
static $wp_version;

if ( ! isset( $wp_version ) ) {
require ABSPATH . WPINC . '/version.php';
}

return $wp_version;
}

/**
* Returns true if the plugin meets all compatibility checks, false otherwise.
*
Expand All @@ -118,13 +143,29 @@ public function is_plugin_compatible() {
switch ( $item_name ) {
case 'php_min_required_version':
if ( ! empty( $item_details['value'] ) && version_compare( phpversion(), $item_details['value'], '<' ) ) {
$this->messages[] = sprintf( esc_html__( 'The minimum PHP version required is %s' ), $item_details['value'] );
// translators: %s: PHP version
$this->messages[] = sprintf( __( 'The minimum PHP version required is %s', 'wp-compat-validation-tool' ), $item_details['value'] );
}
break;

case 'php_max_required_version':
if ( ! empty( $item_details['value'] ) && version_compare( phpversion(), $item_details['value'], '>' ) ) {
$this->messages[] = sprintf( esc_html__( 'The maximum PHP version supported is %s' ), $item_details['value'] );
// translators: %s: PHP version
$this->messages[] = sprintf( __( 'The maximum PHP version supported is %s', 'wp-compat-validation-tool' ), $item_details['value'] );
}
break;

case 'wp_min_required_version':
if ( ! empty( $item_details['value'] ) && version_compare( $this->wp_get_wp_version(), $item_details['value'], '<' ) ) {
// translators: %s: WordPress version
$this->messages[] = sprintf( __( 'The minimum WordPress version required is %s', 'wp-compat-validation-tool' ), $item_details['value'] );
}
break;

case 'wp_max_required_version':
if ( ! empty( $item_details['value'] ) && version_compare( $this->wp_get_wp_version(), $item_details['value'], '>' ) ) {
// translators: %s: WordPress version
$this->messages[] = sprintf( __( 'The maximum WordPress version supported is %s', 'wp-compat-validation-tool' ), $item_details['value'] );
}
break;

Expand All @@ -149,7 +190,10 @@ public function render_php_compat_error() {
<div class="notice notice-error">
<p>
<strong>
<?php printf( esc_html__( '%s error:' ), $this->checklist['plugin_name']['value'] ); ?>
<?php
// translators: %s: Plugin name
printf( esc_html__( '%s error:', 'wp-compat-validation-tool' ), esc_html( $this->checklist['plugin_name']['value'] ) );
?>
</strong>
</p>
<?php if ( count( $this->messages ) > 1 ) : ?>
Expand Down
Loading