Skip to content

Commit e934a73

Browse files
committed
Block self-pings and custom ping URLs
1 parent 567cf8d commit e934a73

File tree

5 files changed

+90
-6
lines changed

5 files changed

+90
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ _Auto-Close_ is a WordPress plugin that _automatically closes comments, pingback
3838
- _Automatically close_ comments, pingbacks, and trackbacks on posts, pages, and custom post types
3939
- _Open or close_ comments/pingbacks/trackbacks selectively by post ID
4040
- _Schedule_ automatic closing using WordPress cron
41+
- _Block self-pings and custom ping URLs_
4142
- _Delete or limit_ post revisions by type
4243
- _Manual tools_ for opening/closing and deleting
4344
- _Admin interface_ with settings and tools tabs

includes/admin/class-settings.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,35 +308,50 @@ public static function settings_comments() {
308308
*/
309309
public static function settings_pingtracks() {
310310
$settings = array(
311-
'close_pbtb' => array(
311+
'close_pbtb' => array(
312312
'id' => 'close_pbtb',
313313
'name' => esc_html__( 'Close Pingbacks/Trackbacks', 'autoclose' ),
314314
'desc' => esc_html__( 'Enable to close pingbacks and trackbacks - used for the automatic schedule as well as one time runs under the Tools tab.', 'autoclose' ),
315315
'type' => 'checkbox',
316316
'options' => false,
317317
),
318-
'pbtb_post_types' => array(
318+
'pbtb_post_types' => array(
319319
'id' => 'pbtb_post_types',
320320
'name' => esc_html__( 'Post types to include', 'autoclose' ),
321321
'desc' => esc_html__( 'At least one option should be selected above. Select which post types on which you want pingbacks/trackbacks closed.', 'autoclose' ),
322322
'type' => 'posttypes',
323323
'options' => 'post',
324324
),
325-
'pbtb_age' => array(
325+
'pbtb_age' => array(
326326
'id' => 'pbtb_age',
327327
'name' => esc_html__( 'Close pingbacks/trackbacks on posts/pages older than', 'autoclose' ),
328328
'desc' => esc_html__( 'Pingbacks/Trackbacks that are older than the above number, in days, will be closed automatically if the schedule is enabled', 'autoclose' ),
329329
'type' => 'number',
330330
'options' => '90',
331331
),
332-
'pbtb_pids' => array(
332+
'pbtb_pids' => array(
333333
'id' => 'pbtb_pids',
334334
'name' => esc_html__( 'Keep pingbacks/trackbacks on these posts/pages open', 'autoclose' ),
335335
'desc' => esc_html__( 'Comma-separated list of post, page or custom post type IDs. e.g. 188,320,500', 'autoclose' ),
336336
'type' => 'numbercsv',
337337
'options' => '',
338338
'size' => 'large',
339339
),
340+
'block_self_pings' => array(
341+
'id' => 'block_self_pings',
342+
'name' => esc_html__( 'Block Self-Pings', 'autoclose' ),
343+
'desc' => esc_html__( 'Enable to block self-pings (pings to your own site).', 'autoclose' ),
344+
'type' => 'checkbox',
345+
'options' => true,
346+
),
347+
'block_ping_urls' => array(
348+
'id' => 'block_ping_urls',
349+
'name' => esc_html__( 'Block Ping URLs', 'autoclose' ),
350+
'desc' => esc_html__( 'Enter one URL per line. Pings to any of these URLs will be blocked in addition to self-pings.', 'autoclose' ),
351+
'type' => 'textarea',
352+
'options' => '',
353+
'size' => 'large',
354+
),
340355
);
341356

342357
/**

includes/class-autoclose.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,19 @@ private function define_admin_hooks() {
114114
* @since 3.0.0
115115
*/
116116
private function define_feature_hooks() {
117-
$comments = new Features\Comments();
118-
$revisions = new Features\Revisions();
117+
$comments = new Features\Comments();
118+
$revisions = new Features\Revisions();
119+
$block_pings = new Features\Block_Pings();
119120

120121
// Register cron hooks.
121122
$this->loader->add_action( 'acc_cron_hook', $comments, 'process_comments' );
122123
$this->loader->add_action( 'acc_cron_hook', $revisions, 'process_revisions' );
123124

124125
// Register revisions hooks.
125126
$this->loader->add_filter( 'wp_revisions_to_keep', $revisions, 'revisions_to_keep', 999999, 2 );
127+
128+
// Register ping hooks.
129+
$this->loader->add_action( 'pre_ping', $block_pings, 'block_pings' );
126130
}
127131

128132
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Block Self-Pings and Custom Ping URLs
4+
*
5+
* @since 3.0.0
6+
* @package AutoClose
7+
*/
8+
9+
namespace WebberZone\AutoClose\Features;
10+
11+
use WebberZone\AutoClose\Utilities\Options;
12+
13+
if ( ! defined( 'WPINC' ) ) {
14+
die;
15+
}
16+
17+
/**
18+
* Class Block_Pings
19+
*
20+
* Blocks self-pings and user-defined URLs from receiving pings.
21+
*
22+
* @since 3.0.0
23+
*/
24+
class Block_Pings {
25+
/**
26+
* Constructor.
27+
*/
28+
public function __construct() {
29+
}
30+
31+
/**
32+
* Remove self-pings and user-defined URLs from ping list.
33+
*
34+
* @param array $links List of ping URLs (passed by reference).
35+
*
36+
* @since 3.0.0
37+
*/
38+
public function block_pings( &$links ) {
39+
$home = home_url();
40+
41+
// Retrieve user-defined blocked URLs and self-ping setting using the Options utility class.
42+
$block_self_pings = Options::get_option( 'block_self_pings', true );
43+
$extra_urls = Options::get_option( 'block_ping_urls', '' );
44+
45+
$url_array = array_filter( array_map( 'trim', explode( PHP_EOL, $extra_urls ) ) );
46+
47+
foreach ( $links as $l => $link ) {
48+
if ( $block_self_pings && 0 === strpos( $link, $home ) ) {
49+
unset( $links[ $l ] );
50+
continue;
51+
}
52+
foreach ( $url_array as $url ) {
53+
if ( '' !== $url && 0 === strpos( $link, $url ) ) {
54+
unset( $links[ $l ] );
55+
break;
56+
}
57+
}
58+
}
59+
}
60+
}

readme.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Found a bug or want to contribute? PRs and issues welcome on [GitHub](https://gi
2727
* Schedule a cron job to automatically close comments, pingbacks and trackbacks daily
2828
* Delete all post revisions or limit the number of revisions by post type
2929
* Exclude specific post IDs from auto-close
30+
* Block self-pings and custom ping URLs
3031

3132
== Screenshots ==
3233

@@ -72,6 +73,9 @@ Release post: https://webberzone.com/blog/auto-close-v3-0-0/
7273

7374
Completely rewritten the plugin to use autoloading, namespaces and classes.
7475

76+
* Features:
77+
* Added block ping URLs feature.
78+
7579
* Bug fixes:
7680
* Fixed PHP error/warnings about loading translations too early.
7781

0 commit comments

Comments
 (0)