Compare commits
32 Commits
Author | SHA1 | Date | |
---|---|---|---|
ae232eceb5
|
|||
93b4dd744d
|
|||
268e3933c8
|
|||
5d7a75ed31
|
|||
aaf054fb5b
|
|||
17c27a7b7c
|
|||
7757142237
|
|||
40347b0f50
|
|||
2ea3d66677
|
|||
7ae0ffcb94
|
|||
a533a494fa
|
|||
7ffc8074b4
|
|||
8267e408f0
|
|||
10cc310e48
|
|||
91fff8a32c
|
|||
7104188718
|
|||
e0a6b498af
|
|||
e449fe02b0
|
|||
24c32327aa | |||
2bf0bcb0fd | |||
eb49dbc7db | |||
be173c6428 | |||
6ffa650254 | |||
6fdaa8bd5a | |||
e64122a5c6 | |||
5981971ddb | |||
eee20e4d05 | |||
0a3102ee38 | |||
5f3fd8b554 | |||
2f4428c0e4 | |||
e794758d77 | |||
91b612425b |
16
.distignore
Normal file
16
.distignore
Normal file
@ -0,0 +1,16 @@
|
||||
/.git
|
||||
/.github
|
||||
/assets
|
||||
/test
|
||||
/.distignore
|
||||
/.editorconfig
|
||||
/.gitattributes
|
||||
/.gitignore
|
||||
/.travis.yml
|
||||
/composer.json
|
||||
/composer.lock
|
||||
/CONTRIBUTING.md
|
||||
/package.json
|
||||
/package-lock.json
|
||||
/phpcs.xml
|
||||
/phpunit.xml
|
21
.editorconfig
Normal file
21
.editorconfig
Normal file
@ -0,0 +1,21 @@
|
||||
# This file is for unifying the coding style for different editors and IDEs
|
||||
# editorconfig.org
|
||||
|
||||
# WordPress Coding Standards
|
||||
# https://make.wordpress.org/core/handbook/coding-standards/
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
indent_style = tab
|
||||
|
||||
[{.jshintrc,*.json,*.yml,*.feature}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[{*.txt,wp-config-sample.php}]
|
||||
end_of_line = crlf
|
3
.gitattributes
vendored
3
.gitattributes
vendored
@ -1,5 +1,7 @@
|
||||
/.github export-ignore
|
||||
/assets export-ignore
|
||||
/test export-ignore
|
||||
.distignore export-ignore
|
||||
.gitattributes export-ignore
|
||||
.gitignore export-ignore
|
||||
.travis.yml export-ignore
|
||||
@ -9,4 +11,3 @@ CONTRIBUTING.md export-ignore
|
||||
package.json export-ignore
|
||||
phpcs.xml export-ignore
|
||||
phpunit.xml export-ignore
|
||||
RoboFile.php export-ignore
|
||||
|
54
.github/workflows/test.yml
vendored
Normal file
54
.github/workflows/test.yml
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
name: CI
|
||||
on: [ push, pull_request ]
|
||||
jobs:
|
||||
unit-test:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
php: [ '5.6', '7.4', '8.0', '8.1', '8.2' ]
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php }}
|
||||
tools: composer
|
||||
- name: Install
|
||||
run: composer install --no-interaction
|
||||
- name: Unit tests
|
||||
run: |
|
||||
composer test
|
||||
sed -i "s#<file name=\"${GITHUB_WORKSPACE}#<file name=\"/github/workspace#g" tests-clover.xml
|
||||
- name: Analyze with SonarCloud
|
||||
if: matrix.php == '8.0'
|
||||
uses: sonarsource/sonarcloud-github-action@master
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
with:
|
||||
args: >
|
||||
-Dsonar.organization=stklcode-github
|
||||
-Dsonar.projectKey=stklcode:statify-blacklist
|
||||
-Dsonar.sources=inc,views,statify-blacklist.php
|
||||
-Dsonar.tests=test
|
||||
-Dsonar.php.tests.reportPath=tests-junit.xml
|
||||
-Dsonar.php.coverage.reportPaths=tests-clover.xml
|
||||
-Dsonar.coverage.exclusions=test/**/*.php
|
||||
|
||||
quality:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: '8.0'
|
||||
tools: composer
|
||||
- name: Install
|
||||
run: composer install --no-interaction
|
||||
- name: Code style checks for PHP
|
||||
run: composer test-cs
|
20
.github/workflows/wordpress-plugin-asset-update.yml
vendored
Normal file
20
.github/workflows/wordpress-plugin-asset-update.yml
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
name: Plugin asset/readme update
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- stable
|
||||
jobs:
|
||||
master:
|
||||
name: Push to stable
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Clean README.md
|
||||
run: tail -n +6 README.md > README.md.tmp && mv README.md.tmp README.md
|
||||
- name: WordPress.org plugin asset/readme update
|
||||
uses: 10up/action-wordpress-plugin-asset-update@stable
|
||||
env:
|
||||
ASSETS_DIR: assets
|
||||
README_NAME: README.md
|
||||
SVN_PASSWORD: ${{ secrets.WP_SVN_PASSWORD }}
|
||||
SVN_USERNAME: ${{ secrets.WP_SVN_USERNAME }}
|
20
.github/workflows/wordpress-plugin-deploy.yml
vendored
Normal file
20
.github/workflows/wordpress-plugin-deploy.yml
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
name: Deploy to WordPress.org
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
- "!v*-*"
|
||||
jobs:
|
||||
tag:
|
||||
name: New tag
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Clean README.md
|
||||
run: tail -n +6 README.md > README.md.tmp && mv README.md.tmp README.md
|
||||
- name: WordPress Plugin Deploy
|
||||
uses: 10up/action-wordpress-plugin-deploy@stable
|
||||
env:
|
||||
ASSETS_DIR: assets
|
||||
SVN_PASSWORD: ${{ secrets.WP_SVN_PASSWORD }}
|
||||
SVN_USERNAME: ${{ secrets.WP_SVN_USERNAME }}
|
13
.travis.yml
13
.travis.yml
@ -1,13 +0,0 @@
|
||||
language: php
|
||||
php:
|
||||
- '5.6'
|
||||
- '7.3'
|
||||
- '7.4'
|
||||
- '8.0'
|
||||
before_script:
|
||||
- composer install
|
||||
script:
|
||||
- composer test-all
|
||||
notifications:
|
||||
slack:
|
||||
secure: "ScXTSMO65veI1jA6TBHGDUtvDqEMkqJykaNf7vLLbb7YIxPIHHNBiX/wcjOHVFfQXZCV3qxQrflB7Lbm9qVUsAv861jTO9x/ZkECl5QhRoc0DIznejwZoypx0HJ9tBZFYT6qNUkViXRKZ/ILAiBLU9Yw52WACtQB9hu3FNFZwmKsjipvV8Sne1qEyTkLYLaMphsbC5mtXYdKMHvdt39jsYsk91UWGeYbXQ37LkMbsaG/8YHXF724d5JO7BRGoThw6p5knKAO5fk29V7GfNqg2h+hnGyNIUOcmxujgMDMFLyFCGMZpPoBa+3jyWWgq4PgpQt0F5VZtJFGoXCGcoMQm5IbVfqkSKJ4jYhqiSIrqSebLmzoPHepWX3yn8tpfOiBWjC6K9w9esp6vcZf26rnAJcjcGkA01rMrHRwR+UEMCLvj7q0DR0qzi/AFeED6gtpODzUf93Rp42Tz1iGvWIbgeCtkCWjfPO6XLuNiqGVPEVaT5BDKqlqbijdKxxp7yh1fdt8s0fInWdIsgoWTbU9DC1W4ZiqtQW7oYO+QtFZMaD6kZWpSqJUwB3kW5JL3odAUEm8bLbRWBvK5ZjGdaGqSbOs6f9gAKcf86iQQhwzCJSOgFlLlKFv9smicjPC+BGOxgx32pgseHNPWn6tmEo/ihmmr/NbbqoOusUKX9gQbA4="
|
37
README.md
37
README.md
@ -1,19 +1,19 @@
|
||||
[](https://travis-ci.com/stklcode/statify-blacklist)
|
||||
[](https://sonarcloud.io/dashboard?id=de.stklcode.web.wordpress.plugins%3Astatify-blacklist)
|
||||
[](https://github.com/stklcode/statify-blacklist/actions/workflows/test.yml?query=branch%3Astable)
|
||||
[](https://sonarcloud.io/dashboard?id=stklcode%3Astatify-blacklist)
|
||||
[](https://packagist.org/packages/stklcode/statify-blacklist)
|
||||
[](https://github.com/stklcode/statify-blacklist/blob/master/LICENSE.md)
|
||||
[](https://github.com/stklcode/statify-blacklist/blob/stable/LICENSE.md)
|
||||
|
||||
# Statify Filter #
|
||||
* Contributors: Stefan Kalscheuer
|
||||
* Requires at least: 4.7
|
||||
* Tested up to: 5.6
|
||||
* Tested up to: 6.3
|
||||
* Requires PHP: 5.5
|
||||
* Stable tag: 1.6.0
|
||||
* Stable tag: 1.6.3
|
||||
* License: GPLv2 or later
|
||||
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
||||
|
||||
## Description ##
|
||||
A filter extension for the famous [Statify](https://wordpress.org/plugins/statify/) Wordpress plugin.
|
||||
A filter extension for the famous [Statify](https://wordpress.org/plugins/statify/) WordPress plugin.
|
||||
|
||||
This plugin adds customizable filters to Statify to allow blocking of referer spam or internal interactions.
|
||||
|
||||
@ -62,7 +62,7 @@ The plugin is capable of handling multisite installations.
|
||||
### What is blocked by default? ###
|
||||
Nothing. By default, all filters are empty and disabled. They can and have to be filled by the blog administrator.
|
||||
|
||||
A default filter is not provided, as the plugin itself is totally neutral. If you want to filter out referer spam,
|
||||
A default filter is not provided, as the plugin itself is totally neutral. If you want to filter out referer spam,
|
||||
visitors from search engines, just "false" referrers from 301 redirects or you own IP address used for testing only depends on you.
|
||||
|
||||
### Does the filter effect user experience? ###
|
||||
@ -71,7 +71,7 @@ No. It only prevents _Statify_ from tracking, nothing more or less.
|
||||
### Does live filtering impact performance? ###
|
||||
Yes, but probably not noticeable. Checking a single referer string against a (usually small) list should be negligible compared to the total loading procedure.
|
||||
If this still is an issue for you, consider deactivating the filter and only run the one-time-cleanup or activate the cron job.
|
||||
|
||||
|
||||
### Is any personal data collected? ###
|
||||
No. The privacy policy of _Statify_ is untouched. Data is only processed, not stored or exposed to anyone.
|
||||
|
||||
@ -93,6 +93,15 @@ Same for IPv6 prefixes like _2001:db8:a0b:12f0::/64_.
|
||||
|
||||
## Upgrade Notice ##
|
||||
|
||||
### 1.6.3 ###
|
||||
This is a service release with minor internal corrections and WP 6.3 compatibility.
|
||||
|
||||
### 1.6.2 ###
|
||||
This is a service release with minor internal corrections and PHP 8.2 compatibility.
|
||||
|
||||
### 1.6.1 ###
|
||||
This is a bugfix release that corrects storage and evaluation of the user agent filter list.
|
||||
|
||||
### 1.6.0 ###
|
||||
The plugin has been renamed from _Statify Blacklist_ to _Statify Filter_.
|
||||
This does not imply any changes in functionality, rather than using a better wording.
|
||||
@ -103,6 +112,18 @@ This version should be compatible with latest WordPress 5.6.
|
||||
|
||||
## Changelog ##
|
||||
|
||||
### 1.6.3 / 14.08.2023 ###
|
||||
* Minor internal code cleanup
|
||||
* Declared compatibility with WordPress 6.3
|
||||
|
||||
### 1.6.2 / 25.02.2023 ###
|
||||
* Always process IPv6 addresses lowercase
|
||||
* Optimize internally used regular expression
|
||||
* Minor adjustments to prevent warnings during user agent filtering with PHP 8.2
|
||||
|
||||
### 1.6.1 / 28.05.2021 ###
|
||||
* Fix storage of user agent filter list (#28, props @BananaSquishee)
|
||||
|
||||
### 1.6.0 / 09.12.2020 ###
|
||||
|
||||
Plugin renamed to _Statify Filter_.
|
||||
|
396
RoboFile.php
396
RoboFile.php
@ -1,396 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Statify Filter Robo build script.
|
||||
*
|
||||
* This file contains the Robo tasks for building a distributable plugin package.
|
||||
* Should not be included in final package.
|
||||
*
|
||||
* @author Stefan Kalscheuer <stefan@stklcode.de>
|
||||
*
|
||||
* @package Statify_Blacklist
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
use Robo\Exception\TaskException;
|
||||
use Robo\Tasks;
|
||||
|
||||
/**
|
||||
* Class RoboFile
|
||||
*/
|
||||
class RoboFile extends Tasks {
|
||||
const PROJECT_NAME = 'statify-blacklist';
|
||||
const SVN_URL = 'https://plugins.svn.wordpress.org/statify-blacklist';
|
||||
|
||||
const OPT_TARGET = 'target';
|
||||
const OPT_SKIPTEST = 'skipTests';
|
||||
const OPT_SKIPSTYLE = 'skipStyle';
|
||||
|
||||
/**
|
||||
* Version tag (read from composer.json).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $version;
|
||||
|
||||
/**
|
||||
* Target directory path.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $target_dir;
|
||||
|
||||
/**
|
||||
* Final package name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $final_name;
|
||||
|
||||
/**
|
||||
* RoboFile constructor
|
||||
*
|
||||
* @param array $opts Options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $opts = [ self::OPT_TARGET => 'dist' ] ) {
|
||||
// Read composer configuration and extract version number..
|
||||
$composer = json_decode( file_get_contents( __DIR__ . '/composer.json' ) );
|
||||
// Extract parameter from options.
|
||||
$this->version = $composer->version;
|
||||
$this->target_dir = $opts[ self::OPT_TARGET ];
|
||||
$this->final_name = self::PROJECT_NAME . '.' . $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up target directory
|
||||
*
|
||||
* @param array $opts Options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clean( $opts = [ self::OPT_TARGET => 'dist' ] ) {
|
||||
$this->say( 'Cleaning target directory...' );
|
||||
if ( is_dir( $this->target_dir . '/' . $this->final_name ) ) {
|
||||
$this->_deleteDir( [ $this->target_dir . '/' . $this->final_name ] );
|
||||
}
|
||||
if ( is_file( $this->target_dir . '/' . $this->final_name . '.zip' ) ) {
|
||||
$this->_remove( $this->target_dir . '/' . $this->final_name . '.zip' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run PHPUnit tests
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test() {
|
||||
$this->say( 'Executing PHPUnit tests...' );
|
||||
$this->taskPhpUnit()->configFile( __DIR__ . '/phpunit.xml' )->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run code style tests
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCS() {
|
||||
$this->say( 'Executing PHPCS tests...' );
|
||||
$this->_exec( __DIR__ . '/vendor/bin/phpcs --standard=phpcs.xml -s' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a distributable bundle.
|
||||
*
|
||||
* @param array $opts Options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function build(
|
||||
$opts = [
|
||||
self::OPT_TARGET => 'dist',
|
||||
self::OPT_SKIPTEST => false,
|
||||
self::OPT_SKIPSTYLE => false,
|
||||
]
|
||||
) {
|
||||
$this->clean( $opts );
|
||||
if ( isset( $opts[ self::OPT_SKIPTEST ] ) && true === $opts[ self::OPT_SKIPTEST ] ) {
|
||||
$this->say( 'Tests skipped' );
|
||||
} else {
|
||||
$this->test();
|
||||
}
|
||||
if ( isset( $opts[ self::OPT_SKIPSTYLE ] ) && true === $opts[ self::OPT_SKIPSTYLE ] ) {
|
||||
$this->say( 'Style checks skipped' );
|
||||
} else {
|
||||
$this->testCS();
|
||||
}
|
||||
$this->bundle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bundle global resources.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function bundle() {
|
||||
$this->say( 'Bundling resources...' );
|
||||
$this->taskCopyDir(
|
||||
[
|
||||
'inc' => $this->target_dir . '/' . $this->final_name . '/inc',
|
||||
'views' => $this->target_dir . '/' . $this->final_name . '/views',
|
||||
]
|
||||
)->run();
|
||||
$this->_copy( 'statify-blacklist.php', $this->target_dir . '/' . $this->final_name . '/statify-blacklist.php' );
|
||||
$this->_copy( 'LICENSE.md', $this->target_dir . '/' . $this->final_name . '/LICENSE.md' );
|
||||
$this->_copy( 'README.md', $this->target_dir . '/' . $this->final_name . '/README.md' );
|
||||
|
||||
// Remove content before title (e.g. badges) from README file.
|
||||
$this->taskReplaceInFile( $this->target_dir . '/' . $this->final_name . '/README.md' )
|
||||
->regex( '/^[^\\#]*/' )
|
||||
->to( '' )
|
||||
->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create ZIP package from distribution bundle.
|
||||
*
|
||||
* @param array $opts Options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function package(
|
||||
$opts = [
|
||||
self::OPT_TARGET => 'dist',
|
||||
self::OPT_SKIPTEST => false,
|
||||
self::OPT_SKIPSTYLE => false,
|
||||
]
|
||||
) {
|
||||
$this->build( $opts );
|
||||
$this->say( 'Packaging...' );
|
||||
$this->taskPack( $this->target_dir . '/' . $this->final_name . '.zip' )
|
||||
->addDir( '', $this->target_dir . '/' . $this->final_name )
|
||||
->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy development version (trunk).
|
||||
*
|
||||
* @param array $opts Options.
|
||||
*
|
||||
* @return void
|
||||
* @throws TaskException On errors.
|
||||
*/
|
||||
public function deployTrunk(
|
||||
$opts = [
|
||||
self::OPT_TARGET => 'dist',
|
||||
self::OPT_SKIPTEST => false,
|
||||
self::OPT_SKIPSTYLE => false,
|
||||
]
|
||||
) {
|
||||
// First execute build job.
|
||||
$this->build( $opts );
|
||||
|
||||
// Prepare VCS, either checkout or update local copy.
|
||||
$this->prepareVCS();
|
||||
|
||||
$this->say( 'Preparing deployment directory...' );
|
||||
$this->updateVCStrunk();
|
||||
|
||||
// Update remote repository.
|
||||
$this->say( 'Deploying...' );
|
||||
$this->commitVCS(
|
||||
'--force trunk/*',
|
||||
'Updated ' . self::PROJECT_NAME . ' trunk'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy current version tag.
|
||||
*
|
||||
* @param array $opts Options.
|
||||
*
|
||||
* @return void
|
||||
* @throws TaskException On errors.
|
||||
*/
|
||||
public function deployTag(
|
||||
$opts = [
|
||||
self::OPT_TARGET => 'dist',
|
||||
self::OPT_SKIPTEST => false,
|
||||
self::OPT_SKIPSTYLE => false,
|
||||
]
|
||||
) {
|
||||
// First execute build job.
|
||||
$this->build( $opts );
|
||||
|
||||
// Prepare VCS, either checkout or update local copy.
|
||||
$this->prepareVCS();
|
||||
|
||||
$this->say( 'Preparing deployment directory...' );
|
||||
$this->updateVCStag();
|
||||
|
||||
// Update remote repository.
|
||||
$this->say( 'Deploying...' );
|
||||
$this->commitVCS(
|
||||
'tags/' . $this->version,
|
||||
'Updated ' . self::PROJECT_NAME . ' v' . $this->version
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy current version tag.
|
||||
*
|
||||
* @param array $opts Options.
|
||||
*
|
||||
* @return void
|
||||
* @throws TaskException On errors.
|
||||
*/
|
||||
public function deployReadme(
|
||||
$opts = [
|
||||
self::OPT_TARGET => 'dist',
|
||||
self::OPT_SKIPTEST => false,
|
||||
self::OPT_SKIPSTYLE => false,
|
||||
]
|
||||
) {
|
||||
// First execute build job.
|
||||
$this->build( $opts );
|
||||
|
||||
// Prepare VCS, either checkout or update local copy.
|
||||
$this->prepareVCS();
|
||||
|
||||
$this->updateVCSreadme();
|
||||
|
||||
// Update remote repository.
|
||||
$this->say( 'Deploying...' );
|
||||
$this->commitVCS(
|
||||
'--force trunk/README.md',
|
||||
'Updated ' . self::PROJECT_NAME . ' ReadMe'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy current version tag and trunk.
|
||||
*
|
||||
* @param array $opts Options.
|
||||
*
|
||||
* @return void
|
||||
* @throws TaskException On errors.
|
||||
*/
|
||||
public function deployAll(
|
||||
$opts = [
|
||||
self::OPT_TARGET => 'dist',
|
||||
self::OPT_SKIPTEST => false,
|
||||
self::OPT_SKIPSTYLE => false,
|
||||
]
|
||||
) {
|
||||
// First execute build job.
|
||||
$this->build( $opts );
|
||||
|
||||
// Prepare VCS, either checkout or update local copy.
|
||||
$this->prepareVCS();
|
||||
|
||||
$this->say( 'Preparing deployment directory...' );
|
||||
$this->updateVCStrunk();
|
||||
$this->updateVCStag();
|
||||
|
||||
// Update remote repository.
|
||||
$this->say( 'Deploying...' );
|
||||
$this->commitVCS(
|
||||
[
|
||||
'--force trunk/*',
|
||||
'--force tags/' . $this->version,
|
||||
],
|
||||
'Updated ' . self::PROJECT_NAME . ' v' . $this->version
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare VCS direcory.
|
||||
*
|
||||
* Checkout or update local copy of SVN repository.
|
||||
*
|
||||
* @return void
|
||||
* @throws TaskException On errors.
|
||||
*/
|
||||
private function prepareVCS() {
|
||||
if ( is_dir( $this->target_dir . '/svn' ) ) {
|
||||
$this->taskSvnStack()
|
||||
->stopOnFail()
|
||||
->dir( $this->target_dir . '/svn/statify-blacklist' )
|
||||
->update()
|
||||
->run();
|
||||
} else {
|
||||
$this->_mkdir( $this->target_dir . '/svn' );
|
||||
$this->taskSvnStack()
|
||||
->dir( $this->target_dir . '/svn' )
|
||||
->checkout( self::SVN_URL )
|
||||
->run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit VCS changes
|
||||
*
|
||||
* @param string|array $to_add Files to add.
|
||||
* @param string $msg Commit message.
|
||||
*
|
||||
* @return void
|
||||
* @throws TaskException On errors.
|
||||
*/
|
||||
private function commitVCS( $to_add, $msg ) {
|
||||
$task = $this->taskSvnStack()
|
||||
->stopOnFail()
|
||||
->dir( $this->target_dir . '/svn/statify-blacklist' );
|
||||
|
||||
if ( is_array( $to_add ) ) {
|
||||
foreach ( $to_add as $ta ) {
|
||||
$task = $task->add( $ta );
|
||||
}
|
||||
} else {
|
||||
$task = $task->add( $to_add );
|
||||
}
|
||||
|
||||
$task->commit( $msg )->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update SVN readme file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function updateVCSreadme() {
|
||||
$trunk_dir = $this->target_dir . '/svn/statify-blacklist/trunk';
|
||||
$this->_copy( $this->target_dir . '/' . $this->final_name . '/README.md', $trunk_dir . '/README.md' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update SVN development version (trunk).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function updateVCStrunk() {
|
||||
// Clean trunk directory.
|
||||
$trunk_dir = $this->target_dir . '/svn/statify-blacklist/trunk';
|
||||
$this->taskCleanDir( $trunk_dir )->run();
|
||||
|
||||
// Copy built bundle to trunk.
|
||||
$this->taskCopyDir( [ $this->target_dir . '/' . $this->final_name => $trunk_dir ] )->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update current SVN version tag.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function updateVCStag() {
|
||||
// Clean tag directory if it exists.
|
||||
$tag_dir = $this->target_dir . '/svn/statify-blacklist/tags/' . $this->version;
|
||||
if ( is_dir( $tag_dir ) ) {
|
||||
$this->taskCleanDir( $this->target_dir . '/svn/statify-blacklist/tags/' . $this->version )->run();
|
||||
} else {
|
||||
$this->_mkdir( $tag_dir );
|
||||
}
|
||||
|
||||
// Copy built bundle to trunk.
|
||||
$this->taskCopyDir( [ $this->target_dir . '/' . $this->final_name => $tag_dir ] )->run();
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stklcode/statify-blacklist",
|
||||
"version": "1.6.0",
|
||||
"version": "1.6.3",
|
||||
"description": "A filter extension for the famous Statify WordPress plugin",
|
||||
"keywords": [
|
||||
"wordpress",
|
||||
@ -19,28 +19,18 @@
|
||||
"type": "wordpress-plugin",
|
||||
"require": {
|
||||
"php": ">=5.5",
|
||||
"composer/installers": "~1.9"
|
||||
"composer/installers": "~v1.12|~v2.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
|
||||
"consolidation/robo": "^1.4",
|
||||
"phpunit/phpunit": "*",
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^v1.0",
|
||||
"phpunit/phpunit": "^5|^6|^7|^8|^9",
|
||||
"phpunit/php-code-coverage": "*",
|
||||
"slowprog/composer-copy-file": "~0.3",
|
||||
"squizlabs/php_codesniffer": "^3.5",
|
||||
"squizlabs/php_codesniffer": "^3.7",
|
||||
"phpcompatibility/phpcompatibility-wp": "^2.1",
|
||||
"wp-coding-standards/wpcs": "^2.3"
|
||||
},
|
||||
"scripts": {
|
||||
"build": [
|
||||
"robo build"
|
||||
],
|
||||
"package": [
|
||||
"robo package"
|
||||
],
|
||||
"deploy": [
|
||||
"robo deploy:all"
|
||||
],
|
||||
"test-all": [
|
||||
"@test",
|
||||
"@test-cs"
|
||||
@ -54,5 +44,11 @@
|
||||
"fix-cs": [
|
||||
"phpcbf --standard=phpcs.xml"
|
||||
]
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"composer/installers": true,
|
||||
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -163,7 +163,6 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -329,14 +328,20 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
*/
|
||||
private static function sanitize_ips( $ips ) {
|
||||
return array_filter(
|
||||
$ips,
|
||||
array_map( 'strtolower', $ips ),
|
||||
function ( $ip ) {
|
||||
return preg_match(
|
||||
'/^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\/([0-9]|[1-2][0-9]|3[0-2]))?$/',
|
||||
'/^((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])(\/([0-9]|[1-2][0-9]|3[0-2]))?$/',
|
||||
$ip
|
||||
) ||
|
||||
preg_match(
|
||||
'/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/',
|
||||
'/^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|([0-9a-f]{1,4}:){1,7}:|([0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}' .
|
||||
'|([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}|([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}' .
|
||||
'|([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}|([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}' .
|
||||
'|[0-9a-f]{1,4}:((:[0-9a-f]{1,4}){1,6})|:((:[0-9a-f]{1,4}){1,7}|:)' .
|
||||
'|fe80:(:[0-9a-f]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]' .
|
||||
'|1?[0-9])?[0-9])|([0-9a-f]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))' .
|
||||
'(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/i',
|
||||
$ip
|
||||
);
|
||||
}
|
||||
|
@ -178,6 +178,30 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
self::update_options();
|
||||
}
|
||||
|
||||
// Version older than 1.6.
|
||||
if ( self::$options['version'] < 1.6 ) {
|
||||
$options = self::$options;
|
||||
if ( ! isset( $options['ua'] ) ) {
|
||||
$options['ua'] = array(
|
||||
'active' => 0,
|
||||
'regexp' => 0,
|
||||
'blacklist' => array(),
|
||||
);
|
||||
} elseif ( ! isset( $options['ua']['blacklist'] ) ) {
|
||||
$options['ua']['blacklist'] = array();
|
||||
} elseif ( isset( $options['ua'] ) ) {
|
||||
// User agent strings got stored incorrectly in 1.6.0 - luckily the version was not updated, either.
|
||||
$options['ua']['blacklist'] = array_flip( $options['ua']['blacklist'] );
|
||||
}
|
||||
$options['version'] = 1.6;
|
||||
if ( self::$multisite ) {
|
||||
update_site_option( 'statify-blacklist', $options );
|
||||
} else {
|
||||
update_option( 'statify-blacklist', $options );
|
||||
}
|
||||
self::update_options();
|
||||
}
|
||||
|
||||
// Version older than current major release.
|
||||
if ( self::VERSION_MAIN > self::$options['version'] ) {
|
||||
// Merge default options with current config, assuming only additive changes.
|
||||
|
@ -24,7 +24,7 @@ class StatifyBlacklist {
|
||||
* @since 1.4.0
|
||||
* @var int VERSION_MAIN
|
||||
*/
|
||||
const VERSION_MAIN = 1.4;
|
||||
const VERSION_MAIN = 1.6;
|
||||
|
||||
/**
|
||||
* Operation mode "normal".
|
||||
@ -80,7 +80,7 @@ class StatifyBlacklist {
|
||||
*/
|
||||
public static function init() {
|
||||
// Skip on autosave.
|
||||
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
|
||||
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -388,7 +388,7 @@ class StatifyBlacklist {
|
||||
*/
|
||||
private static function get_user_agent() {
|
||||
if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
|
||||
$user_agent = filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), FILTER_SANITIZE_STRING );
|
||||
$user_agent = filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
|
||||
if ( $user_agent ) {
|
||||
return $user_agent;
|
||||
}
|
||||
@ -436,7 +436,7 @@ class StatifyBlacklist {
|
||||
}
|
||||
|
||||
$ceil = ceil( $mask / 16 );
|
||||
for ( $i = 1; $i <= $ceil; ++ $i ) {
|
||||
for ( $i = 1; $i <= $ceil; ++$i ) {
|
||||
$left = $mask - 16 * ( $i - 1 );
|
||||
$left = ( $left <= 16 ) ? $left : 16;
|
||||
$mask_b = ~( 0xffff >> $left ) & 0xffff;
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "statify-blacklist",
|
||||
"version": "1.6.0",
|
||||
"version": "1.6.3",
|
||||
"description": "A filter extension for the famous Statify WordPress plugin",
|
||||
"author": "Stefan Kalscheuer",
|
||||
"license": "GPL-2.0+"
|
||||
|
@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<phpunit bootstrap="./vendor/autoload.php">
|
||||
<phpunit bootstrap="test/bootstrap.php">
|
||||
<testsuites>
|
||||
<testsuite name="Statify Blacklist TestSuite">
|
||||
<directory suffix="_Test.php">./test/</directory>
|
||||
|
@ -10,7 +10,7 @@
|
||||
* Plugin Name: Statify Filter
|
||||
* Plugin URI: https://wordpress.org/plugins/statify-blacklist/
|
||||
* Description: Extension for the Statify plugin to add customizable filters. (formerly "Statify Blacklist)
|
||||
* Version: 1.6.0
|
||||
* Version: 1.6.3
|
||||
* Author: Stefan Kalscheuer (@stklcode)
|
||||
* Author URI: https://www.stklcode.de
|
||||
* Text Domain: statify-blacklist
|
||||
@ -37,7 +37,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
|
||||
// Constants.
|
||||
define( 'STATIFYBLACKLIST_FILE', __FILE__ );
|
||||
define( 'STATIFYBLACKLIST_DIR', dirname( __FILE__ ) );
|
||||
define( 'STATIFYBLACKLIST_DIR', __DIR__ );
|
||||
define( 'STATIFYBLACKLIST_BASE', plugin_basename( __FILE__ ) );
|
||||
|
||||
// Check for compatibility.
|
||||
|
67
test/StatifyBlacklist_Admin_Test.php
Normal file
67
test/StatifyBlacklist_Admin_Test.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Statify Filter: Unit Test
|
||||
*
|
||||
* This is a PHPunit test class for the plugin's functionality
|
||||
*
|
||||
* @package Statify_Blacklist
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class StatifyBlacklist_Admin_Test.
|
||||
*
|
||||
* PHPUnit test class for StatifyBlacklist_Admin.
|
||||
*/
|
||||
class StatifyBlacklist_Admin_Test extends PHPUnit\Framework\TestCase {
|
||||
|
||||
|
||||
/**
|
||||
* Test sanitization of IP addresses.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_ips() {
|
||||
// IPv4 tests.
|
||||
$valid = array( '192.0.2.123', '192.0.2.123/32', '192.0.2.0/24', '192.0.2.128/25' );
|
||||
$invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' );
|
||||
$result = invoke_static( StatifyBlacklist_Admin::class, 'sanitize_ips', array( array_merge( $valid, $invalid ) ) );
|
||||
$this->assertNotFalse( $result );
|
||||
|
||||
/*
|
||||
* Unfortunately this is necessary as long as we run PHP 5 tests, because "assertInternalType" is deprecated
|
||||
* as of PHPUnit 8, but "assertIsArray" has been introduces in PHPUnit 7.5 which requires PHP >= 7.1.
|
||||
*/
|
||||
if ( method_exists( $this, 'assertIsArray' ) ) {
|
||||
$this->assertIsArray( $result );
|
||||
} else {
|
||||
$this->assertInternalType( 'array', $result );
|
||||
}
|
||||
$this->assertEquals( $valid, $result );
|
||||
|
||||
// IPv6 tests.
|
||||
$valid = array(
|
||||
'2001:db8:a0b:12f0::',
|
||||
'2001:db8:a0b:12f0::1',
|
||||
'2001:db8:a0b:12f0::1/128',
|
||||
'2001:DB8:A0B:12F0::/64',
|
||||
'fe80::7645:6de2:ff:1',
|
||||
'::ffff:192.0.2.123',
|
||||
);
|
||||
$invalid = array(
|
||||
'2001:db8:a0b:12f0::x',
|
||||
'2001:db8:a0b:12f0:::',
|
||||
'2001:fffff:a0b:12f0::1',
|
||||
'2001:DB8:A0B:12F0::/129',
|
||||
'1:2:3:4:5:6:7:8:9',
|
||||
'::ffff:12.34.56.789',
|
||||
);
|
||||
$result = invoke_static( StatifyBlacklist_Admin::class, 'sanitize_ips', array( array_merge( $valid, $invalid ) ) );
|
||||
$this->assertNotFalse( $result );
|
||||
if ( method_exists( $this, 'assertIsArray' ) ) {
|
||||
$this->assertIsArray( $result );
|
||||
} else {
|
||||
$this->assertInternalType( 'array', $result );
|
||||
}
|
||||
$this->assertEquals( array_map( 'strtolower', $valid ), $result );
|
||||
}
|
||||
}
|
92
test/StatifyBlacklist_System_Test.php
Normal file
92
test/StatifyBlacklist_System_Test.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Statify Filter: Unit Test
|
||||
*
|
||||
* This is a PHPunit test class for the plugin's functionality
|
||||
*
|
||||
* @package Statify_Blacklist
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class StatifyBlacklist_System_Test.
|
||||
*
|
||||
* PHPUnit test class for StatifyBlacklist_System.
|
||||
*/
|
||||
class StatifyBlacklist_System_Test extends PHPUnit\Framework\TestCase {
|
||||
|
||||
/**
|
||||
* Test the upgrade methodology for configuration options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_upgrade() {
|
||||
// Create configuration of version 1.3.
|
||||
$options13 = array(
|
||||
'active_referer' => 1,
|
||||
'cron_referer' => 0,
|
||||
'referer' => array(
|
||||
'example.net' => 0,
|
||||
'example.com' => 1,
|
||||
),
|
||||
'referer_regexp' => 0,
|
||||
'version' => 1.3,
|
||||
);
|
||||
|
||||
// Set options in mock.
|
||||
update_option( 'statify-blacklist', $options13 );
|
||||
|
||||
// Execute upgrade.
|
||||
StatifyBlacklist_System::upgrade();
|
||||
|
||||
// Retrieve updated options.
|
||||
$options_updated = get_option( 'statify-blacklist' );
|
||||
|
||||
// Verify size against default options (no junk left).
|
||||
$this->assertEquals( 5, count( $options_updated ) );
|
||||
$this->assertEquals( 4, count( $options_updated['referer'] ) );
|
||||
$this->assertEquals( 4, count( $options_updated['target'] ) );
|
||||
$this->assertEquals( 2, count( $options_updated['ip'] ) );
|
||||
$this->assertEquals( 3, count( $options_updated['ua'] ) );
|
||||
$this->assertEquals( 1.6, $options_updated['version'] );
|
||||
|
||||
// Verify that original attributes are unchanged.
|
||||
$this->assertEquals( $options13['active_referer'], $options_updated['referer']['active'] );
|
||||
$this->assertEquals( $options13['cron_referer'], $options_updated['referer']['cron'] );
|
||||
$this->assertEquals( $options13['referer'], $options_updated['referer']['blacklist'] );
|
||||
$this->assertEquals( $options13['referer_regexp'], $options_updated['referer']['regexp'] );
|
||||
|
||||
// Verify that new attributes are present in config and filled with default values (disabled, empty).
|
||||
$this->assertEquals( 0, $options_updated['target']['active'] );
|
||||
$this->assertEquals( 0, $options_updated['target']['cron'] );
|
||||
$this->assertEquals( 0, $options_updated['target']['regexp'] );
|
||||
$this->assertEquals( array(), $options_updated['target']['blacklist'] );
|
||||
$this->assertEquals( 0, $options_updated['ip']['active'] );
|
||||
$this->assertEquals( array(), $options_updated['ip']['blacklist'] );
|
||||
$this->assertEquals( 0, $options_updated['ua']['active'] );
|
||||
$this->assertEquals( 0, $options_updated['ua']['regexp'] );
|
||||
$this->assertEquals( array(), $options_updated['ua']['blacklist'] );
|
||||
|
||||
// Verify that version number has changed to current release.
|
||||
$this->assertEquals( StatifyBlacklist::VERSION_MAIN, $options_updated['version'] );
|
||||
|
||||
// Test upgrade of incorrectly stored user agent list in 1.6.
|
||||
$options_updated['version'] = 1.4;
|
||||
$options_updated['ua']['blacklist'] = array( 'user agent 1', 'user agent 2' );
|
||||
update_option( 'statify-blacklist', $options_updated );
|
||||
|
||||
// Execute upgrade.
|
||||
StatifyBlacklist_System::upgrade();
|
||||
|
||||
// Retrieve updated options.
|
||||
$options_updated = get_option( 'statify-blacklist' );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'user agent 1' => 0,
|
||||
'user agent 2' => 1,
|
||||
),
|
||||
$options_updated['ua']['blacklist']
|
||||
);
|
||||
$this->assertEquals( 1.6, $options_updated['version'] );
|
||||
$this->assertEquals( StatifyBlacklist::VERSION_MAIN, $options_updated['version'] );
|
||||
}
|
||||
}
|
@ -4,34 +4,10 @@
|
||||
*
|
||||
* This is a PHPunit test class for the plugin's functionality
|
||||
*
|
||||
* @package Statify_Blacklist
|
||||
* @subpackage Admin
|
||||
* @since 1.3.0
|
||||
* @package Statify_Blacklist
|
||||
* @since 1.3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simulating the ABSPATH constant.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @var bool ABSPATH
|
||||
*/
|
||||
const ABSPATH = false;
|
||||
|
||||
/**
|
||||
* The StatifyBlacklist base class.
|
||||
*/
|
||||
require_once __DIR__ . '/../inc/class-statifyblacklist.php';
|
||||
|
||||
/**
|
||||
* The StatifyBlacklist system class.
|
||||
*/
|
||||
require_once __DIR__ . '/../inc/class-statifyblacklist-system.php';
|
||||
|
||||
/**
|
||||
* The StatifyBlacklist admin class.
|
||||
*/
|
||||
require_once __DIR__ . '/../inc/class-statifyblacklist-admin.php';
|
||||
|
||||
/**
|
||||
* Class StatifyBlacklistTest.
|
||||
*
|
||||
@ -228,57 +204,6 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
||||
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the upgrade methodology for configuration options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_upgrade() {
|
||||
// Create configuration of version 1.3.
|
||||
$options13 = array(
|
||||
'active_referer' => 1,
|
||||
'cron_referer' => 0,
|
||||
'referer' => array(
|
||||
'example.net' => 0,
|
||||
'example.com' => 1,
|
||||
),
|
||||
'referer_regexp' => 0,
|
||||
'version' => 1.3,
|
||||
);
|
||||
|
||||
// Set options in mock.
|
||||
update_option( 'statify-blacklist', $options13 );
|
||||
|
||||
// Execute upgrade.
|
||||
StatifyBlacklist_System::upgrade();
|
||||
|
||||
// Retrieve updated options.
|
||||
$options_updated = get_option( 'statify-blacklist' );
|
||||
|
||||
// Verify size against default options (no junk left).
|
||||
$this->assertEquals( 4, count( $options_updated ) );
|
||||
$this->assertEquals( 4, count( $options_updated['referer'] ) );
|
||||
$this->assertEquals( 4, count( $options_updated['target'] ) );
|
||||
$this->assertEquals( 2, count( $options_updated['ip'] ) );
|
||||
|
||||
// Verify that original attributes are unchanged.
|
||||
$this->assertEquals( $options13['active_referer'], $options_updated['referer']['active'] );
|
||||
$this->assertEquals( $options13['cron_referer'], $options_updated['referer']['cron'] );
|
||||
$this->assertEquals( $options13['referer'], $options_updated['referer']['blacklist'] );
|
||||
$this->assertEquals( $options13['referer_regexp'], $options_updated['referer']['regexp'] );
|
||||
|
||||
// Verify that new attributes are present in config and filled with default values (disabled, empty).
|
||||
$this->assertEquals( 0, $options_updated['target']['active'] );
|
||||
$this->assertEquals( 0, $options_updated['target']['cron'] );
|
||||
$this->assertEquals( 0, $options_updated['target']['regexp'] );
|
||||
$this->assertEquals( array(), $options_updated['target']['blacklist'] );
|
||||
$this->assertEquals( 0, $options_updated['ip']['active'] );
|
||||
$this->assertEquals( array(), $options_updated['ip']['blacklist'] );
|
||||
|
||||
// Verify that version number has changed to current release.
|
||||
$this->assertEquals( StatifyBlacklist::VERSION_MAIN, $options_updated['version'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test CIDR address matching for IP filter (#7).
|
||||
*
|
||||
@ -358,6 +283,20 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
||||
array( '2001:db8:a0b:12f0::123:456', '2001:db8:a0b:12f0::1/96 ' )
|
||||
)
|
||||
);
|
||||
$this->assertTrue(
|
||||
invoke_static(
|
||||
StatifyBlacklist::class,
|
||||
'cidr_match',
|
||||
array( '2001:DB8:A0B:12F0::123:456', '2001:db8:a0b:12f0::1/96 ' )
|
||||
)
|
||||
);
|
||||
$this->assertTrue(
|
||||
invoke_static(
|
||||
StatifyBlacklist::class,
|
||||
'cidr_match',
|
||||
array( '2001:db8:a0b:12f0::123:456', '2001:DB8:A0B:12F0::1/96 ' )
|
||||
)
|
||||
);
|
||||
$this->assertFalse(
|
||||
invoke_static(
|
||||
StatifyBlacklist::class,
|
||||
@ -367,53 +306,6 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sanitization of IP addresses.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_ips() {
|
||||
// IPv4 tests.
|
||||
$valid = array( '192.0.2.123', '192.0.2.123/32', '192.0.2.0/24', '192.0.2.128/25' );
|
||||
$invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' );
|
||||
$result = invoke_static( StatifyBlacklist_Admin::class, 'sanitize_ips', array( array_merge( $valid, $invalid ) ) );
|
||||
$this->assertNotFalse( $result );
|
||||
|
||||
/*
|
||||
* Unfortunately this is necessary as long as we run PHP 5 tests, because "assertInternalType" is deprecated
|
||||
* as of PHPUnit 8, but "assertIsArray" has been introduces in PHPUnit 7.5 which requires PHP >= 7.1.
|
||||
*/
|
||||
if ( method_exists( $this, 'assertIsArray' ) ) {
|
||||
$this->assertIsArray( $result );
|
||||
} else {
|
||||
$this->assertInternalType( 'array', $result );
|
||||
}
|
||||
$this->assertEquals( $valid, $result );
|
||||
|
||||
// IPv6 tests.
|
||||
$valid = array(
|
||||
'2001:db8:a0b:12f0::',
|
||||
'2001:db8:a0b:12f0::1',
|
||||
'2001:db8:a0b:12f0::1/128',
|
||||
'2001:db8:a0b:12f0::/64',
|
||||
);
|
||||
$invalid = array(
|
||||
'2001:db8:a0b:12f0::x',
|
||||
'2001:db8:a0b:12f0:::',
|
||||
'2001:fffff:a0b:12f0::1',
|
||||
'2001:db8:a0b:12f0::/129',
|
||||
'1:2:3:4:5:6:7:8:9',
|
||||
);
|
||||
$result = invoke_static( StatifyBlacklist_Admin::class, 'sanitize_ips', array( array_merge( $valid, $invalid ) ) );
|
||||
$this->assertNotFalse( $result );
|
||||
if ( method_exists( $this, 'assertIsArray' ) ) {
|
||||
$this->assertIsArray( $result );
|
||||
} else {
|
||||
$this->assertInternalType( 'array', $result );
|
||||
}
|
||||
$this->assertEquals( $valid, $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test IP filter (#7).
|
||||
*
|
||||
@ -619,11 +511,11 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
||||
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
|
||||
// Keyword matching.
|
||||
StatifyBlacklist::$options['ua']['blacklist'] = array( 'TestBot' => 0 );
|
||||
StatifyBlacklist::$options['ua']['regexp'] = StatifyBlacklist::MODE_KEYWORD;
|
||||
StatifyBlacklist::$options['ua']['regexp'] = StatifyBlacklist::MODE_KEYWORD;
|
||||
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
|
||||
// RegEx.
|
||||
StatifyBlacklist::$options['ua']['blacklist'] = array( 'T[a-z]+B[a-z]+' => 0 );
|
||||
StatifyBlacklist::$options['ua']['regexp'] = StatifyBlacklist::MODE_REGEX;
|
||||
StatifyBlacklist::$options['ua']['regexp'] = StatifyBlacklist::MODE_REGEX;
|
||||
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
|
||||
StatifyBlacklist::$options['ua']['blacklist'] = array( 't[a-z]+' => 0 );
|
||||
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
|
||||
@ -721,69 +613,3 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @ignore */
|
||||
function invoke_static( $class, $method_name, $parameters = array() ) {
|
||||
$reflection = new \ReflectionClass( $class );
|
||||
$method = $reflection->getMethod( $method_name );
|
||||
$method->setAccessible( true );
|
||||
|
||||
return $method->invokeArgs( null, $parameters );
|
||||
}
|
||||
|
||||
|
||||
// Some mocked WP functions.
|
||||
$mock_options = array();
|
||||
$mock_multisite = false;
|
||||
|
||||
/** @ignore */
|
||||
function is_multisite() {
|
||||
global $mock_multisite;
|
||||
|
||||
return $mock_multisite;
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function wp_parse_args( $args, $defaults = '' ) {
|
||||
if ( is_object( $args ) ) {
|
||||
$r = get_object_vars( $args );
|
||||
} elseif ( is_array( $args ) ) {
|
||||
$r =& $args;
|
||||
} else {
|
||||
parse_str( $args, $r );
|
||||
}
|
||||
|
||||
if ( is_array( $defaults ) ) {
|
||||
return array_merge( $defaults, $r );
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function get_option( $option, $default = false ) {
|
||||
global $mock_options;
|
||||
|
||||
return isset( $mock_options[ $option ] ) ? $mock_options[ $option ] : $default;
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function update_option( $option, $value, $autoload = null ) {
|
||||
global $mock_options;
|
||||
$mock_options[ $option ] = $value;
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function wp_get_raw_referer() {
|
||||
return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
|
||||
}
|
||||
|
||||
function wp_parse_url( $value ) {
|
||||
return parse_url( $value );
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function wp_unslash( $value ) {
|
||||
return is_string( $value ) ? stripslashes( $value ) : $value;
|
||||
}
|
||||
|
89
test/bootstrap.php
Normal file
89
test/bootstrap.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPUnit bootstrap file
|
||||
*
|
||||
* @package Statify_Blacklist
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simulating the ABSPATH constant.
|
||||
*
|
||||
* @var boolean ABSPATH
|
||||
*/
|
||||
const ABSPATH = false;
|
||||
|
||||
/*
|
||||
* Include class files.
|
||||
*/
|
||||
require_once __DIR__ . '/../inc/class-statifyblacklist.php';
|
||||
require_once __DIR__ . '/../inc/class-statifyblacklist-admin.php';
|
||||
require_once __DIR__ . '/../inc/class-statifyblacklist-system.php';
|
||||
|
||||
// Include Composer autoloader.
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
|
||||
|
||||
/** @ignore */
|
||||
function invoke_static( $class, $method_name, $parameters = array() ) {
|
||||
$reflection = new \ReflectionClass( $class );
|
||||
$method = $reflection->getMethod( $method_name );
|
||||
$method->setAccessible( true );
|
||||
|
||||
return $method->invokeArgs( null, $parameters );
|
||||
}
|
||||
|
||||
// Some mocked WP functions.
|
||||
$mock_options = array();
|
||||
$mock_multisite = false;
|
||||
|
||||
/** @ignore */
|
||||
function is_multisite() {
|
||||
global $mock_multisite;
|
||||
|
||||
return $mock_multisite;
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function wp_parse_args( $args, $defaults = '' ) {
|
||||
if ( is_object( $args ) ) {
|
||||
$r = get_object_vars( $args );
|
||||
} elseif ( is_array( $args ) ) {
|
||||
$r =& $args;
|
||||
} else {
|
||||
parse_str( $args, $r );
|
||||
}
|
||||
|
||||
if ( is_array( $defaults ) ) {
|
||||
return array_merge( $defaults, $r );
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function get_option( $option, $default = false ) {
|
||||
global $mock_options;
|
||||
|
||||
return isset( $mock_options[ $option ] ) ? $mock_options[ $option ] : $default;
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function update_option( $option, $value, $autoload = null ) {
|
||||
global $mock_options;
|
||||
$mock_options[ $option ] = $value;
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function wp_get_raw_referer() {
|
||||
return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
|
||||
}
|
||||
|
||||
function wp_parse_url( $value ) {
|
||||
return parse_url( $value );
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
function wp_unslash( $value ) {
|
||||
return is_string( $value ) ? stripslashes( $value ) : $value;
|
||||
}
|
@ -88,7 +88,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: Extract user agent array.
|
||||
// Extract user agent array.
|
||||
if ( isset( $_POST['statifyblacklist']['ua']['blacklist'] ) ) {
|
||||
$ua_string = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['ua']['blacklist'] ) );
|
||||
}
|
||||
@ -139,7 +139,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
? (int) $_POST['statifyblacklist']['ua']['active'] : 0,
|
||||
'regexp' => isset( $_POST['statifyblacklist']['ua']['regexp'] )
|
||||
? (int) $_POST['statifyblacklist']['ua']['regexp'] : 0,
|
||||
'blacklist' => $ua,
|
||||
'blacklist' => array_flip( $ua ),
|
||||
),
|
||||
'version' => StatifyBlacklist::VERSION_MAIN,
|
||||
)
|
||||
@ -457,9 +457,9 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
<td>
|
||||
<textarea cols="40" rows="5" name="statifyblacklist[ua][blacklist]" id="statify-blacklist_ua"><?php
|
||||
if ( empty( $statifyblacklist_update_result['ua'] ) ) {
|
||||
print esc_html( implode( "\r\n", StatifyBlacklist::$options['ua']['blacklist'] ) );
|
||||
print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$options['ua']['blacklist'] ) ) );
|
||||
} else {
|
||||
print esc_html( implode( "\r\n", $statifyblacklist_update_result['ua']['sanitized'] ) );
|
||||
print esc_html( implode( "\r\n", array_keys( $statifyblacklist_update_result['ua']['sanitized'] ) ) );
|
||||
}
|
||||
?></textarea>
|
||||
|
||||
|
Reference in New Issue
Block a user