PluginProbe ʕ •ᴥ•ʔ
Akismet Anti-spam: Spam Protection / 5.7
Akismet Anti-spam: Spam Protection v5.7
5.7 3.0.4 3.0.5 3.1 3.1.1 3.1.10 3.1.11 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2 3.3 3.3.1 3.3.2 3.3.3 3.3.4 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1 4.1.1 4.1.10 4.1.11 4.1.12 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 5.0 5.0.1 5.0.2 5.1 5.2 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.4 5.5 5.6 trunk 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.4.0 2.4.1 2.5.0 2.5.1 2.5.10 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 3.0.0 3.0.0-RC1 3.0.1 3.0.2 3.0.3
akismet / class-akismet-connector.php
akismet Last commit date
_inc 1 month ago abilities 1 month ago views 1 month ago .htaccess 1 month ago LICENSE.txt 10 years ago akismet.php 1 month ago changelog.txt 1 year ago class-akismet-abilities.php 1 month ago class-akismet-compatible-plugins.php 9 months ago class-akismet-connector.php 1 month ago class.akismet-admin.php 1 month ago class.akismet-cli.php 1 year ago class.akismet-rest-api.php 2 months ago class.akismet-widget.php 7 months ago class.akismet.php 1 month ago index.php 1 year ago readme.txt 1 month ago wrapper.php 1 year ago
class-akismet-connector.php
148 lines
1 <?php
2 /**
3 * Akismet Connector integration.
4 *
5 * @package Akismet
6 */
7
8 declare( strict_types = 1 );
9
10 /**
11 * Integrates Akismet with the WordPress Connectors framework,
12 * handling API key validation and connection status reporting.
13 */
14 class Akismet_Connector {
15
16 /**
17 * Register hooks for the WordPress Connectors integration.
18 */
19 public static function init() {
20 add_action( 'wp_connectors_init', array( 'Akismet_Connector', 'register_connector' ) );
21
22 // Priority 9 so we validate the real key before core's connector masking filter at priority 10.
23 add_filter( 'rest_post_dispatch', array( 'Akismet_Connector', 'validate_api_key' ), 9, 3 );
24 add_filter( 'script_module_data_options-connectors-wp-admin', array( 'Akismet_Connector', 'set_connected_status' ), 11 );
25
26 // Invalidate the connector key status cache on any key change.
27 foreach ( array( 'add', 'update', 'delete' ) as $action ) {
28 add_action( "{$action}_option_wordpress_api_key", array( 'Akismet_Connector', 'invalidate_key_status_cache' ) );
29 }
30 }
31
32 /**
33 * Validate the Akismet API key when saved via the connectors REST settings endpoint.
34 * If the key is invalid, revert it to an empty string.
35 *
36 * @param WP_REST_Response $response The response object.
37 * @param WP_REST_Server $server The server instance.
38 * @param WP_REST_Request $request The request object.
39 * @return WP_REST_Response
40 */
41 public static function validate_api_key( $response, $server, $request ) {
42 if ( '/wp/v2/settings' !== $request->get_route() ) {
43 return $response;
44 }
45
46 if ( 'POST' !== $request->get_method() && 'PUT' !== $request->get_method() ) {
47 return $response;
48 }
49
50 $data = $response->get_data();
51 if ( ! is_array( $data ) || ! array_key_exists( 'wordpress_api_key', $data ) ) {
52 return $response;
53 }
54
55 $key = $data['wordpress_api_key'];
56 if ( ! is_string( $key ) || '' === $key ) {
57 return $response;
58 }
59
60 if ( Akismet::KEY_STATUS_INVALID === Akismet::verify_key( $key ) ) {
61 update_option( 'wordpress_api_key', '' );
62 $data['wordpress_api_key'] = '';
63 $response->set_data( $data );
64 }
65
66 return $response;
67 }
68
69 /**
70 * Set the isConnected status for the Akismet connector based on actual key validity.
71 *
72 * @param array $data Script module data.
73 * @return array
74 */
75 public static function set_connected_status( $data ) {
76 if ( ! isset( $data['connectors']['akismet']['authentication'] ) ) {
77 return $data;
78 }
79
80 $key = Akismet::get_api_key();
81
82 if ( empty( $key ) ) {
83 $data['connectors']['akismet']['authentication']['isConnected'] = false;
84 return $data;
85 }
86
87 $is_connected = get_transient( 'akismet_connector_key_status' );
88
89 if ( false === $is_connected ) {
90 $is_connected = Akismet::verify_key( $key );
91
92 // Don't cache failures (e.g. network timeouts) so we retry on the next page load.
93 if ( Akismet::KEY_STATUS_FAILED !== $is_connected ) {
94 set_transient( 'akismet_connector_key_status', $is_connected, DAY_IN_SECONDS );
95 }
96 }
97
98 $data['connectors']['akismet']['authentication']['isConnected'] = ( Akismet::KEY_STATUS_VALID === $is_connected );
99
100 return $data;
101 }
102
103 /**
104 * Clear the connector key status cache so it doesn't serve stale data.
105 */
106 public static function invalidate_key_status_cache() {
107 delete_transient( 'akismet_connector_key_status' );
108 }
109
110 /**
111 * Register the Akismet connector with an is_active callback so the
112 * connectors page can detect Akismet as active when installed as a mu-plugin.
113 *
114 * We re-register the full connector rather than patching the core one
115 * so that Akismet still has a connector even if core removes its own.
116 *
117 * @see https://github.com/WordPress/gutenberg/pull/76994
118 *
119 * @param WP_Connector_Registry $registry Connector registry instance.
120 */
121 public static function register_connector( $registry ) {
122 if ( method_exists( $registry, 'is_registered' ) && $registry->is_registered( 'akismet' ) ) {
123 $registry->unregister( 'akismet' );
124 }
125
126 $registry->register(
127 'akismet',
128 array(
129 'name' => __( 'Akismet Anti-spam', 'akismet' ),
130 'description' => __( 'Protect your site from spam.', 'akismet' ),
131 'type' => 'spam_filtering',
132 'plugin' => array(
133 'file' => 'akismet/akismet.php',
134 'is_active' => function () {
135 return defined( 'AKISMET_VERSION' );
136 },
137 ),
138 'authentication' => array(
139 'method' => 'api_key',
140 'credentials_url' => 'https://akismet.com/get/',
141 'setting_name' => 'wordpress_api_key',
142 'constant_name' => 'WPCOM_API_KEY',
143 ),
144 )
145 );
146 }
147 }
148