Skip to content

Commit 082e987

Browse files
committed
Disable pings/trackbacks for local, development, and staging environments
When WP_ENVIRONMENT_TYPE is not `production`, disable pingbacks and trackbacks. Otherwise, when `WP_ENVIRONMENT_TYPE` is `local`, `development`, or `staging`, pingbacks and trackbacks are sent when posts are published. This creates confusion on the receiving end and is unnecessary for testing workflows. Props arcangelini, cagrimmett, ramonopoly, tyxla. Fixes #64837. Built from https://develop.svn.wordpress.org/trunk@62231 git-svn-id: http://core.svn.wordpress.org/trunk@61511 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 543046e commit 082e987

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

wp-includes/comment.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,6 +3163,84 @@ function generic_ping( $post_id = 0 ) {
31633163
return $post_id;
31643164
}
31653165

3166+
/**
3167+
* Determines whether pings should be disabled for the current environment.
3168+
*
3169+
* By default, all pings (outgoing pingbacks, trackbacks, and ping service
3170+
* notifications, as well as incoming pingbacks and trackbacks) are disabled
3171+
* for non-production environments ('local', 'development', 'staging').
3172+
*
3173+
* @since 7.1.0
3174+
*
3175+
* @return bool True if pings should be disabled, false otherwise.
3176+
*/
3177+
function wp_should_disable_pings_for_environment() {
3178+
$environment_type = wp_get_environment_type();
3179+
$should_disable = 'production' !== $environment_type;
3180+
3181+
/**
3182+
* Filters whether pings should be disabled for the current environment.
3183+
*
3184+
* Returning false re-enables pings in non-production environments.
3185+
* Returning true disables pings even in production.
3186+
*
3187+
* @since 7.1.0
3188+
*
3189+
* @param bool $should_disable Whether pings should be disabled. Default true
3190+
* for non-production environments, false for production.
3191+
* @param string $environment_type The current environment type as returned by
3192+
* wp_get_environment_type().
3193+
*/
3194+
return apply_filters( 'wp_should_disable_pings_for_environment', $should_disable, $environment_type );
3195+
}
3196+
3197+
/**
3198+
* Removes outgoing ping callbacks in non-production environments.
3199+
*
3200+
* Hooked to `do_all_pings` at priority 1 so it runs before the default
3201+
* priority 10 callbacks. Does not remove `do_all_enclosures`.
3202+
*
3203+
* @since 7.1.0
3204+
*/
3205+
function wp_maybe_disable_outgoing_pings_for_environment() {
3206+
if ( wp_should_disable_pings_for_environment() ) {
3207+
remove_action( 'do_all_pings', 'do_all_pingbacks' );
3208+
remove_action( 'do_all_pings', 'do_all_trackbacks' );
3209+
remove_action( 'do_all_pings', 'generic_ping' );
3210+
}
3211+
}
3212+
3213+
/**
3214+
* Rejects incoming trackbacks in non-production environments.
3215+
*
3216+
* Hooked to `pre_trackback_post` which fires in `wp-trackback.php` before the
3217+
* trackback is processed. Calls `trackback_response()` which sends an XML error
3218+
* response and terminates the request.
3219+
*
3220+
* @since 7.1.0
3221+
*/
3222+
function wp_maybe_disable_trackback_for_environment() {
3223+
if ( wp_should_disable_pings_for_environment() ) {
3224+
trackback_response( 1, __( 'Trackbacks are disabled in non-production environments.' ) );
3225+
}
3226+
}
3227+
3228+
/**
3229+
* Removes the pingback XML-RPC method in non-production environments.
3230+
*
3231+
* @since 7.1.0
3232+
*
3233+
* @param string[] $methods An array of XML-RPC methods, keyed by their methodName.
3234+
* @return string[] Modified array of XML-RPC methods.
3235+
*/
3236+
function wp_maybe_disable_xmlrpc_pingback_for_environment( $methods ) {
3237+
if ( wp_should_disable_pings_for_environment() ) {
3238+
unset( $methods['pingback.ping'] );
3239+
}
3240+
3241+
return $methods;
3242+
}
3243+
31663244
/**
31673245
* Pings back the links found in a post.
31683246
*

wp-includes/default-filters.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@
421421
add_action( 'do_all_pings', 'do_all_enclosures', 10, 0 );
422422
add_action( 'do_all_pings', 'do_all_trackbacks', 10, 0 );
423423
add_action( 'do_all_pings', 'generic_ping', 10, 0 );
424+
425+
// Disable pings (pingbacks, trackbacks, and ping service notifications) in non-production environments.
426+
add_action( 'do_all_pings', 'wp_maybe_disable_outgoing_pings_for_environment', 1, 0 );
427+
add_action( 'pre_trackback_post', 'wp_maybe_disable_trackback_for_environment', 10, 0 );
428+
add_filter( 'xmlrpc_methods', 'wp_maybe_disable_xmlrpc_pingback_for_environment' );
429+
424430
add_action( 'do_robots', 'do_robots' );
425431
add_action( 'do_favicon', 'do_favicon' );
426432
add_action( 'wp_before_include_template', 'wp_start_template_enhancement_output_buffer', 1000 ); // Late priority to let `wp_template_enhancement_output_buffer` filters and `wp_finalized_template_enhancement_output_buffer` actions be registered.

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '7.1-alpha-62230';
19+
$wp_version = '7.1-alpha-62231';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)