Skip to content

Commit

Permalink
Merge pull request #9 from Emilia-Capital/develop
Browse files Browse the repository at this point in the history
Merge version 1.3.1
  • Loading branch information
jdevalk authored Dec 13, 2023
2 parents 9ab24b1 + 5fc65cb commit 83e5a67
Show file tree
Hide file tree
Showing 11 changed files with 535 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
/coverage/
.phpunit.result.cache
10 changes: 7 additions & 3 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<!-- Exclude the Node Modules directory. -->
<exclude-pattern>/node_modules/*</exclude-pattern>

<!-- Exclude the Coverage output directory. -->
<exclude-pattern>/coverage/*</exclude-pattern>

<!-- Exclude minified Javascript files. -->
<exclude-pattern>*.min.js</exclude-pattern>

Expand Down Expand Up @@ -106,6 +109,7 @@
<properties>
<property name="prefixes" type="array">
<element value="FewerTags"/>
<element value="fewer_tags"/>
</property>
</properties>
</rule>
Expand Down Expand Up @@ -142,12 +146,12 @@
For more information on ruleset configuration optiones, check out the PHPCS wiki:
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset
-->
<rule ref="WordPress.WP.GlobalVariablesOverride">
<exclude-pattern>/path/to/Tests/*Test\.php</exclude-pattern>
</rule>
<rule ref="WordPress.Files.FileName">
<exclude-pattern>fewer-tags.php</exclude-pattern>
<exclude-pattern>fewer-tags-playground.php</exclude-pattern>
</rule>

<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<exclude-pattern>/tests/bootstrap\.php$</exclude-pattern>
</rule>
</ruleset>
13 changes: 7 additions & 6 deletions fewer-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* Plugin that redirects tag pages to the home page if they contain fewer than a specified number of posts.
*
* @package FewerTags
* @version 1.3
* @version 1.3.1
*
* Plugin Name: Fewer Tags
* Plugin URI: https://joost.blog/plugins/fewer-tags/
* Description: Redirects tag pages to the home page if they contain fewer than a specified number of posts, defaults to 10. Change under Settings > Reading. Results in fewer useFewer tags, which is good for SEO.
* Requires at least: 6.2
* Requires PHP: 7.4
* Version: 1.3
* Version: 1.3.1
* Author: Joost de Valk
* Author URI: https://joost.blog
* License: GPL-3.0+
Expand All @@ -35,17 +35,17 @@ class FewerTags {
public static $min_posts_count;

/**
* Constructor method
* Register plugin hooks.
*/
public function __construct() {
public function register_hooks() {
add_action( 'init', [ $this, 'init' ] );
}

/**
* Initialize the plugin and register hooks.
*/
public function init() {
self::$min_posts_count = get_option( 'joost_min_posts_count', 10 );
self::$min_posts_count = (int) get_option( 'joost_min_posts_count', 10 );

require __DIR__ . '/vendor/autoload.php';

Expand All @@ -60,4 +60,5 @@ public function init() {
}

// Instantiate the plugin class.
new FewerTags();
$fewer_tags = new FewerTags();
$fewer_tags->register_hooks();
6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
<file>fewer-tags.php</file>
</include>
</coverage>
</phpunit>
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: tag, tags, seo
Requires at least: 6.2
Tested up to: 6.4
Requires PHP: 7.4
Stable tag: 1.3
Stable tag: 1.3.1
License: GPL3+
License URI: https://www.gnu.org/licenses/gpl-3.0.en.html

Expand Down Expand Up @@ -46,6 +46,10 @@ You can report security bugs through the Patchstack Vulnerability Disclosure Pro

== Changelog ==

= 1.3.1 =

* Fix fatal error caused by not loading the autoload file.

= 1.3 =

* Make sure the output of the Gutenberg terms block is filtered to when it's showing tags.
Expand Down
26 changes: 2 additions & 24 deletions src/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function redirect_tag_pages() {
$tag = get_queried_object();
if ( $tag && $tag->count < \FewerTags::$min_posts_count ) {
wp_safe_redirect( home_url(), 301 );
// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
}
}
}
Expand Down Expand Up @@ -82,30 +84,6 @@ public function filter_get_the_tags( $tags ) {
return $tags;
}

/**
* Filters the list of terms (including tags) retrieved to exclude tags with fewer than the specified minimum number of posts.
*
* @param array $terms The list of terms retrieved.
* @param string[] $taxonomies The taxonomies for which terms were retrieved.
*
* @return array The filtered list of terms.
*/
public function filter_get_terms( $terms, $taxonomies ) {
if ( is_admin() ) {
return $terms;
}

if ( in_array( 'post_tag', $taxonomies, true ) && is_array( $terms ) ) {
foreach ( $terms as $key => $term ) {
if ( is_object( $term ) && $term->count < \FewerTags::$min_posts_count ) {
unset( $terms[ $key ] );
}
}
}

return $terms;
}

/**
* Excludes tags with fewer than the minimum number of posts from the core sitemap.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* PHPUnit bootstrap file.
*
* @package Accessibility_Checker
* @package FewerTags
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );
Expand All @@ -29,7 +29,7 @@
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( __DIR__ ) . '/accessibility-checker.php';
require dirname( __DIR__ ) . '/fewer-tags.php';
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
Expand Down
39 changes: 0 additions & 39 deletions tests/phpunit/helper-functions/test-class-admin.php

This file was deleted.

174 changes: 174 additions & 0 deletions tests/phpunit/test-class-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php
/**
* Class Admin_Test
*
* @package FewerTags
*/

namespace FewerTags\Tests;

use FewerTags;
use FewerTags\Admin;

/**
* Sample test case.
*/
class Admin_Test extends \WP_UnitTestCase {

/**
* Instance of the class being tested.
*
* @var FewerTags\Admin
*/
private static $class_instance;

/**
* Live tag.
*
* @var array
*/
private static $live_tag;

/**
* Not live tag.
*
* @var array
*/
private static $not_live_tag;

/**
* Set up the class instance to be tested.
*/
public static function set_up_before_class() {
parent::set_up_before_class();
self::$class_instance = new Admin();

FewerTags::$min_posts_count = 5;

self::$live_tag = wp_insert_term( 'Live tag', 'post_tag' );
self::$not_live_tag = wp_insert_term( 'Not alive', 'post_tag' );

$i = 0;
while ( $i < 11 ) {
wp_insert_post(
[
'post_title' => 'Post ' . $i,
'post_status' => 'publish',
'tags_input' => 'Live tag',
]
);
++$i;
}
}

/**
* Tests hooks registration.
*
* @covers FewerTags\Admin::register_hooks
*/
public function test_register_hooks() {
self::$class_instance->register_hooks();

$this->assertSame( 10, has_action( 'admin_init', [ self::$class_instance, 'register_settings' ] ) );
$this->assertSame( 10, has_action( 'manage_edit-post_tag_columns', [ self::$class_instance, 'add_tag_columns' ] ) );
$this->assertSame( 10, has_action( 'manage_post_tag_custom_column', [ self::$class_instance, 'manage_tag_columns' ] ), 10, 3 );
$this->assertSame( 10, has_action( 'tag_row_actions', [ self::$class_instance, 'remove_view_action' ] ), 10, 2 );
}

/**
* Tests settings registration.
*
* @covers FewerTags\Admin::register_settings
*/
public function test_register_settings() {
self::$class_instance->register_settings();

global $wp_settings_sections, $wp_settings_fields;

$this->assertArrayHasKey( 'fewer_tags_section', $wp_settings_sections['reading'] );
$this->assertArrayHasKey( 'joost_min_posts_count', $wp_settings_fields['reading']['fewer_tags_section'] );
}

/**
* Tests display section.
*
* @covers FewerTags\Admin::display_section
*/
public function test_display_section() {
ob_start();
self::$class_instance->display_section();
$output = ob_get_clean();

$this->assertSame( 'Set the minimum number of posts a tag should have to become live on the site and not be redirected to the homepage.', $output );
}

/**
* Tests display setting.
*
* @covers FewerTags\Admin::display_setting
*/
public function test_display_setting() {
ob_start();
self::$class_instance->display_setting();
$output = ob_get_clean();

$this->assertStringContainsString( 'name="joost_min_posts_count"', $output );
$this->assertStringContainsString( 'id="joost_min_posts_count"', $output );
$this->assertStringContainsString( 'type="number"', $output );
$this->assertStringContainsString( 'min="1"', $output );
$this->assertStringContainsString( 'value="5"', $output ); // This is tied to the value of FewerTags::$min_posts_count.
$this->assertStringContainsString( 'class="small-text"', $output );
$this->assertStringContainsString( 'posts before being live on the site.', $output );
}

/**
* Tests add tag columns.
*
* @covers FewerTags\Admin::add_tag_columns
*/
public function test_add_tag_columns() {
$columns = self::$class_instance->add_tag_columns( [] );

$this->assertArrayHasKey( 'active', $columns );
$this->assertSame( 'Live on site', $columns['active'] );
}

/**
* Tests manage tag columns.
*
* @covers FewerTags\Admin::manage_tag_columns
*/
public function test_manage_tag_columns() {
$out = self::$class_instance->manage_tag_columns( '', 'active', self::$not_live_tag['term_id'] );
$this->assertMatchesRegularExpression( '/Not live/', $out );

$out = self::$class_instance->manage_tag_columns( '', 'active', self::$live_tag['term_id'] );
$this->assertSame( 'Live', $out );
}

/**
* Tests remove view action.
*
* @covers FewerTags\Admin::remove_view_action
*/
public function test_remove_view_action() {
$actions = self::$class_instance->remove_view_action( $this->get_term_row_actions(), get_term( self::$not_live_tag['term_id'] ) );
$this->assertArrayNotHasKey( 'view', $actions );

$actions = self::$class_instance->remove_view_action( $this->get_term_row_actions(), get_term( self::$live_tag['term_id'] ) );
$this->assertArrayHasKey( 'view', $actions );
}

/**
* Returns the term row actions.
*
* @return array
*/
private function get_term_row_actions() {
return [
'edit' => 'Edit',
'delete' => 'Delete',
'view' => 'View',
];
}
}
Loading

0 comments on commit 83e5a67

Please sign in to comment.