| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Jul 3 08:20:12 2026 | Cross-referenced by PHPXref |