settings = Plugin::get_settings( 'security' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !empty( $this->settings['disable_pingbacks'] ) ) { add_filter( 'pre_option_default_ping_status', [ $this, 'close_default_ping_status' ] ); add_filter( 'pre_option_default_pingback_flag', [ $this, 'close_default_pingback_flag' ] ); add_filter( 'xmlrpc_methods', [ $this, 'disable_xmlrpc_pingback' ] ); add_filter( 'wp_headers', [ $this, 'remove_pingback_header' ] ); add_filter( 'pings_open', '__return_false', 20 ); add_action( 'pre_ping', [ $this, 'clear_ping_links' ] ); add_filter( 'get_to_ping', '__return_empty_array' ); } elseif( !empty( $this->settings['disable_self_pingbacks'] ) ) { add_action( 'pre_ping', [ $this, 'disable_internal_pingbacks' ] ); } } /** * Modifies and extends the settings fields array with additional security options. * * @param array $fields An array of existing settings fields. This array will be extended * to include new fields for administrating security-related options. * The new field added provides an option to disable pingbacks entirely. * * @return array Updated settings fields array including the new field for disabling pingbacks. */ public function adminease_settings_fields( array $fields ): array { $fields['security']['fields'][] = [ 'type' => 'switch', 'id' => 'disable-pingbacks', 'name' => 'adminease[security][disable_pingbacks]', 'value' => $this->settings['disable_pingbacks'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Disable pingbacks', 'adminease' ), 'description' => __( 'Pingbacks allow other WordPress websites to automatically leave comments under your posts when these websites link to these posts. Pingbacks can be abused to use your website for DDoS attacks on other sites. This security option closes pingbacks and trackbacks on every post, including older ones, stops your site from sending them, and removes the XML-RPC pingback methods and the X-Pingback header. New posts also start with pingbacks switched off. Your Discussion settings are not changed.', 'adminease' ), ]; $fields['security']['fields'][] = [ 'type' => 'switch', 'id' => 'disable-self-pingbacks', 'name' => 'adminease[security][disable_self_pingbacks]', 'value' => $this->settings['disable_self_pingbacks'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Disable self pingbacks', 'adminease' ), 'description' => __( 'Stops your site from sending a pingback to itself when a post links to another page on your own site, so you no longer get a comment for every internal link. Not needed when "Disable pingbacks" is on.', 'adminease' ), ]; return $fields; } /** * Answers the "Allow link notifications from other sites" default as closed. * Nothing is written to the database, so turning the switch off gives the site * its own setting back. * * @param mixed $pre The value that short-circuits get_option(), false to read the database. * * @return mixed */ public function close_default_ping_status( $pre ) { return $this->is_discussion_settings_screen() ? $pre : 'closed'; } /** * Answers the "Attempt to notify any blogs linked to from the post" default as off. * * @param mixed $pre The value that short-circuits get_option(), false to read the database. * * @return mixed */ public function close_default_pingback_flag( $pre ) { return $this->is_discussion_settings_screen() ? $pre : '0'; } /** * The Discussion settings screen and the options save handler keep showing and * saving the real stored values. * @return bool */ private function is_discussion_settings_screen(): bool { global $pagenow; return is_admin() && in_array( $pagenow, [ 'options-discussion.php', 'options.php' ], true ); } /** * Disables XML-RPC pingback methods to improve security by preventing * potential abuse of the pingback feature in XML-RPC. * * @param array $methods An array of existing XML-RPC methods. * * @return array Modified array of XML-RPC methods with pingback methods removed. */ public function disable_xmlrpc_pingback( array $methods ): array { unset( $methods['pingback.ping'] ); unset( $methods['pingback.extensions.getPingbacks'] ); return $methods; } /** * Removes the X-Pingback header from the provided headers array. * * @param array $headers An associative array of HTTP headers. * * @return array The modified headers array with the X-Pingback header removed. */ public function remove_pingback_header( array $headers ): array { unset( $headers['X-Pingback'] ); return $headers; } /** * Stops the site from sending pingbacks and trackbacks by emptying the link list. * * @param array &$post_links The links found in the post. Changed in place. * * @return void */ public function clear_ping_links( array &$post_links ): void { $post_links = []; } /** * Removes links to this same site from the list of pingbacks to send. * * @param array &$post_links The links found in the post. Changed in place. * * @return void */ public function disable_internal_pingbacks( array &$post_links ): void { $home_host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); foreach( $post_links as $key => $link ) { $host = strtolower( (string) wp_parse_url( $link, PHP_URL_HOST ) ); if( '' !== $home_host && $home_host === $host ) { unset( $post_links[ $key ] ); } } } }