class.akismet-rest-api.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. class Akismet_REST_API {
  3. /**
  4. * Register the REST API routes.
  5. */
  6. public static function init() {
  7. if ( ! function_exists( 'register_rest_route' ) ) {
  8. // The REST API wasn't integrated into core until 4.4, and we support 4.0+ (for now).
  9. return false;
  10. }
  11. register_rest_route( 'akismet/v1', '/key', array(
  12. array(
  13. 'methods' => WP_REST_Server::READABLE,
  14. 'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
  15. 'callback' => array( 'Akismet_REST_API', 'get_key' ),
  16. ), array(
  17. 'methods' => WP_REST_Server::EDITABLE,
  18. 'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
  19. 'callback' => array( 'Akismet_REST_API', 'set_key' ),
  20. 'args' => array(
  21. 'key' => array(
  22. 'required' => true,
  23. 'type' => 'string',
  24. 'sanitize_callback' => array( 'Akismet_REST_API', 'sanitize_key' ),
  25. 'description' => __( 'A 12-character Akismet API key. Available at akismet.com/get/', 'akismet' ),
  26. ),
  27. ),
  28. ), array(
  29. 'methods' => WP_REST_Server::DELETABLE,
  30. 'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
  31. 'callback' => array( 'Akismet_REST_API', 'delete_key' ),
  32. )
  33. ) );
  34. register_rest_route( 'akismet/v1', '/settings/', array(
  35. array(
  36. 'methods' => WP_REST_Server::READABLE,
  37. 'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
  38. 'callback' => array( 'Akismet_REST_API', 'get_settings' ),
  39. ),
  40. array(
  41. 'methods' => WP_REST_Server::EDITABLE,
  42. 'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
  43. 'callback' => array( 'Akismet_REST_API', 'set_boolean_settings' ),
  44. 'args' => array(
  45. 'akismet_strictness' => array(
  46. 'required' => false,
  47. 'type' => 'boolean',
  48. 'description' => __( 'If true, Akismet will automatically discard the worst spam automatically rather than putting it in the spam folder.', 'akismet' ),
  49. ),
  50. 'akismet_show_user_comments_approved' => array(
  51. 'required' => false,
  52. 'type' => 'boolean',
  53. 'description' => __( 'If true, show the number of approved comments beside each comment author in the comments list page.', 'akismet' ),
  54. ),
  55. ),
  56. )
  57. ) );
  58. register_rest_route( 'akismet/v1', '/stats', array(
  59. 'methods' => WP_REST_Server::READABLE,
  60. 'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
  61. 'callback' => array( 'Akismet_REST_API', 'get_stats' ),
  62. 'args' => array(
  63. 'interval' => array(
  64. 'required' => false,
  65. 'type' => 'string',
  66. 'sanitize_callback' => array( 'Akismet_REST_API', 'sanitize_interval' ),
  67. 'description' => __( 'The time period for which to retrieve stats. Options: 60-days, 6-months, all', 'akismet' ),
  68. 'default' => 'all',
  69. ),
  70. ),
  71. ) );
  72. register_rest_route( 'akismet/v1', '/stats/(?P<interval>[\w+])', array(
  73. 'args' => array(
  74. 'interval' => array(
  75. 'description' => __( 'The time period for which to retrieve stats. Options: 60-days, 6-months, all', 'akismet' ),
  76. 'type' => 'string',
  77. ),
  78. ),
  79. array(
  80. 'methods' => WP_REST_Server::READABLE,
  81. 'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
  82. 'callback' => array( 'Akismet_REST_API', 'get_stats' ),
  83. )
  84. ) );
  85. }
  86. /**
  87. * Get the current Akismet API key.
  88. *
  89. * @param WP_REST_Request $request
  90. * @return WP_Error|WP_REST_Response
  91. */
  92. public static function get_key( $request = null ) {
  93. return rest_ensure_response( Akismet::get_api_key() );
  94. }
  95. /**
  96. * Set the API key, if possible.
  97. *
  98. * @param WP_REST_Request $request
  99. * @return WP_Error|WP_REST_Response
  100. */
  101. public static function set_key( $request ) {
  102. if ( defined( 'WPCOM_API_KEY' ) ) {
  103. return rest_ensure_response( new WP_Error( 'hardcoded_key', __( 'This site\'s API key is hardcoded and cannot be changed via the API.', 'akismet' ), array( 'status'=> 409 ) ) );
  104. }
  105. $new_api_key = $request->get_param( 'key' );
  106. if ( ! self::key_is_valid( $new_api_key ) ) {
  107. return rest_ensure_response( new WP_Error( 'invalid_key', __( 'The value provided is not a valid and registered API key.', 'akismet' ), array( 'status' => 400 ) ) );
  108. }
  109. update_option( 'wordpress_api_key', $new_api_key );
  110. return self::get_key();
  111. }
  112. /**
  113. * Unset the API key, if possible.
  114. *
  115. * @param WP_REST_Request $request
  116. * @return WP_Error|WP_REST_Response
  117. */
  118. public static function delete_key( $request ) {
  119. if ( defined( 'WPCOM_API_KEY' ) ) {
  120. return rest_ensure_response( new WP_Error( 'hardcoded_key', __( 'This site\'s API key is hardcoded and cannot be deleted.', 'akismet' ), array( 'status'=> 409 ) ) );
  121. }
  122. delete_option( 'wordpress_api_key' );
  123. return rest_ensure_response( true );
  124. }
  125. /**
  126. * Get the Akismet settings.
  127. *
  128. * @param WP_REST_Request $request
  129. * @return WP_Error|WP_REST_Response
  130. */
  131. public static function get_settings( $request = null ) {
  132. return rest_ensure_response( array(
  133. 'akismet_strictness' => ( get_option( 'akismet_strictness', '1' ) === '1' ),
  134. 'akismet_show_user_comments_approved' => ( get_option( 'akismet_show_user_comments_approved', '1' ) === '1' ),
  135. ) );
  136. }
  137. /**
  138. * Update the Akismet settings.
  139. *
  140. * @param WP_REST_Request $request
  141. * @return WP_Error|WP_REST_Response
  142. */
  143. public static function set_boolean_settings( $request ) {
  144. foreach ( array(
  145. 'akismet_strictness',
  146. 'akismet_show_user_comments_approved',
  147. ) as $setting_key ) {
  148. $setting_value = $request->get_param( $setting_key );
  149. if ( is_null( $setting_value ) ) {
  150. // This setting was not specified.
  151. continue;
  152. }
  153. // From 4.7+, WP core will ensure that these are always boolean
  154. // values because they are registered with 'type' => 'boolean',
  155. // but we need to do this ourselves for prior versions.
  156. $setting_value = Akismet_REST_API::parse_boolean( $setting_value );
  157. update_option( $setting_key, $setting_value ? '1' : '0' );
  158. }
  159. return self::get_settings();
  160. }
  161. /**
  162. * Parse a numeric or string boolean value into a boolean.
  163. *
  164. * @param mixed $value The value to convert into a boolean.
  165. * @return bool The converted value.
  166. */
  167. public static function parse_boolean( $value ) {
  168. switch ( $value ) {
  169. case true:
  170. case 'true':
  171. case '1':
  172. case 1:
  173. return true;
  174. case false:
  175. case 'false':
  176. case '0':
  177. case 0:
  178. return false;
  179. default:
  180. return (bool) $value;
  181. }
  182. }
  183. /**
  184. * Get the Akismet stats for a given time period.
  185. *
  186. * Possible `interval` values:
  187. * - all
  188. * - 60-days
  189. * - 6-months
  190. *
  191. * @param WP_REST_Request $request
  192. * @return WP_Error|WP_REST_Response
  193. */
  194. public static function get_stats( $request ) {
  195. $api_key = Akismet::get_api_key();
  196. $interval = $request->get_param( 'interval' );
  197. $stat_totals = array();
  198. $response = Akismet::http_post( Akismet::build_query( array( 'blog' => get_option( 'home' ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
  199. if ( ! empty( $response[1] ) ) {
  200. $stat_totals[$interval] = json_decode( $response[1] );
  201. }
  202. return rest_ensure_response( $stat_totals );
  203. }
  204. private static function key_is_valid( $key ) {
  205. $response = Akismet::http_post(
  206. Akismet::build_query(
  207. array(
  208. 'key' => $key,
  209. 'blog' => get_option( 'home' )
  210. )
  211. ),
  212. 'verify-key'
  213. );
  214. if ( $response[1] == 'valid' ) {
  215. return true;
  216. }
  217. return false;
  218. }
  219. public static function privileged_permission_callback() {
  220. return current_user_can( 'manage_options' );
  221. }
  222. public static function sanitize_interval( $interval, $request, $param ) {
  223. $interval = trim( $interval );
  224. $valid_intervals = array( '60-days', '6-months', 'all', );
  225. if ( ! in_array( $interval, $valid_intervals ) ) {
  226. $interval = 'all';
  227. }
  228. return $interval;
  229. }
  230. public static function sanitize_key( $key, $request, $param ) {
  231. return trim( $key );
  232. }
  233. }