Edit File by line
/home/barbar84/www/wp-conte.../plugins/akismet
File: class.akismet-rest-api.php
<?php
[0] Fix | Delete
[1] Fix | Delete
class Akismet_REST_API {
[2] Fix | Delete
/**
[3] Fix | Delete
* Register the REST API routes.
[4] Fix | Delete
*/
[5] Fix | Delete
public static function init() {
[6] Fix | Delete
if ( ! function_exists( 'register_rest_route' ) ) {
[7] Fix | Delete
// The REST API wasn't integrated into core until 4.4, and we support 4.0+ (for now).
[8] Fix | Delete
return false;
[9] Fix | Delete
}
[10] Fix | Delete
[11] Fix | Delete
register_rest_route( 'akismet/v1', '/key', array(
[12] Fix | Delete
array(
[13] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[14] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
[15] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'get_key' ),
[16] Fix | Delete
), array(
[17] Fix | Delete
'methods' => WP_REST_Server::EDITABLE,
[18] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
[19] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'set_key' ),
[20] Fix | Delete
'args' => array(
[21] Fix | Delete
'key' => array(
[22] Fix | Delete
'required' => true,
[23] Fix | Delete
'type' => 'string',
[24] Fix | Delete
'sanitize_callback' => array( 'Akismet_REST_API', 'sanitize_key' ),
[25] Fix | Delete
'description' => __( 'A 12-character Akismet API key. Available at akismet.com/get/', 'akismet' ),
[26] Fix | Delete
),
[27] Fix | Delete
),
[28] Fix | Delete
), array(
[29] Fix | Delete
'methods' => WP_REST_Server::DELETABLE,
[30] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
[31] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'delete_key' ),
[32] Fix | Delete
)
[33] Fix | Delete
) );
[34] Fix | Delete
[35] Fix | Delete
register_rest_route( 'akismet/v1', '/settings/', array(
[36] Fix | Delete
array(
[37] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[38] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
[39] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'get_settings' ),
[40] Fix | Delete
),
[41] Fix | Delete
array(
[42] Fix | Delete
'methods' => WP_REST_Server::EDITABLE,
[43] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
[44] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'set_boolean_settings' ),
[45] Fix | Delete
'args' => array(
[46] Fix | Delete
'akismet_strictness' => array(
[47] Fix | Delete
'required' => false,
[48] Fix | Delete
'type' => 'boolean',
[49] Fix | Delete
'description' => __( 'If true, Akismet will automatically discard the worst spam automatically rather than putting it in the spam folder.', 'akismet' ),
[50] Fix | Delete
),
[51] Fix | Delete
'akismet_show_user_comments_approved' => array(
[52] Fix | Delete
'required' => false,
[53] Fix | Delete
'type' => 'boolean',
[54] Fix | Delete
'description' => __( 'If true, show the number of approved comments beside each comment author in the comments list page.', 'akismet' ),
[55] Fix | Delete
),
[56] Fix | Delete
),
[57] Fix | Delete
)
[58] Fix | Delete
) );
[59] Fix | Delete
[60] Fix | Delete
register_rest_route( 'akismet/v1', '/stats', array(
[61] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[62] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
[63] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'get_stats' ),
[64] Fix | Delete
'args' => array(
[65] Fix | Delete
'interval' => array(
[66] Fix | Delete
'required' => false,
[67] Fix | Delete
'type' => 'string',
[68] Fix | Delete
'sanitize_callback' => array( 'Akismet_REST_API', 'sanitize_interval' ),
[69] Fix | Delete
'description' => __( 'The time period for which to retrieve stats. Options: 60-days, 6-months, all', 'akismet' ),
[70] Fix | Delete
'default' => 'all',
[71] Fix | Delete
),
[72] Fix | Delete
),
[73] Fix | Delete
) );
[74] Fix | Delete
[75] Fix | Delete
register_rest_route( 'akismet/v1', '/stats/(?P<interval>[\w+])', array(
[76] Fix | Delete
'args' => array(
[77] Fix | Delete
'interval' => array(
[78] Fix | Delete
'description' => __( 'The time period for which to retrieve stats. Options: 60-days, 6-months, all', 'akismet' ),
[79] Fix | Delete
'type' => 'string',
[80] Fix | Delete
),
[81] Fix | Delete
),
[82] Fix | Delete
array(
[83] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[84] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'privileged_permission_callback' ),
[85] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'get_stats' ),
[86] Fix | Delete
)
[87] Fix | Delete
) );
[88] Fix | Delete
[89] Fix | Delete
register_rest_route( 'akismet/v1', '/alert', array(
[90] Fix | Delete
array(
[91] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[92] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'remote_call_permission_callback' ),
[93] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'get_alert' ),
[94] Fix | Delete
'args' => array(
[95] Fix | Delete
'key' => array(
[96] Fix | Delete
'required' => false,
[97] Fix | Delete
'type' => 'string',
[98] Fix | Delete
'sanitize_callback' => array( 'Akismet_REST_API', 'sanitize_key' ),
[99] Fix | Delete
'description' => __( 'A 12-character Akismet API key. Available at akismet.com/get/', 'akismet' ),
[100] Fix | Delete
),
[101] Fix | Delete
),
[102] Fix | Delete
),
[103] Fix | Delete
array(
[104] Fix | Delete
'methods' => WP_REST_Server::EDITABLE,
[105] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'remote_call_permission_callback' ),
[106] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'set_alert' ),
[107] Fix | Delete
'args' => array(
[108] Fix | Delete
'key' => array(
[109] Fix | Delete
'required' => false,
[110] Fix | Delete
'type' => 'string',
[111] Fix | Delete
'sanitize_callback' => array( 'Akismet_REST_API', 'sanitize_key' ),
[112] Fix | Delete
'description' => __( 'A 12-character Akismet API key. Available at akismet.com/get/', 'akismet' ),
[113] Fix | Delete
),
[114] Fix | Delete
),
[115] Fix | Delete
),
[116] Fix | Delete
array(
[117] Fix | Delete
'methods' => WP_REST_Server::DELETABLE,
[118] Fix | Delete
'permission_callback' => array( 'Akismet_REST_API', 'remote_call_permission_callback' ),
[119] Fix | Delete
'callback' => array( 'Akismet_REST_API', 'delete_alert' ),
[120] Fix | Delete
'args' => array(
[121] Fix | Delete
'key' => array(
[122] Fix | Delete
'required' => false,
[123] Fix | Delete
'type' => 'string',
[124] Fix | Delete
'sanitize_callback' => array( 'Akismet_REST_API', 'sanitize_key' ),
[125] Fix | Delete
'description' => __( 'A 12-character Akismet API key. Available at akismet.com/get/', 'akismet' ),
[126] Fix | Delete
),
[127] Fix | Delete
),
[128] Fix | Delete
)
[129] Fix | Delete
) );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Get the current Akismet API key.
[134] Fix | Delete
*
[135] Fix | Delete
* @param WP_REST_Request $request
[136] Fix | Delete
* @return WP_Error|WP_REST_Response
[137] Fix | Delete
*/
[138] Fix | Delete
public static function get_key( $request = null ) {
[139] Fix | Delete
return rest_ensure_response( Akismet::get_api_key() );
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Set the API key, if possible.
[144] Fix | Delete
*
[145] Fix | Delete
* @param WP_REST_Request $request
[146] Fix | Delete
* @return WP_Error|WP_REST_Response
[147] Fix | Delete
*/
[148] Fix | Delete
public static function set_key( $request ) {
[149] Fix | Delete
if ( defined( 'WPCOM_API_KEY' ) ) {
[150] Fix | Delete
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 ) ) );
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
$new_api_key = $request->get_param( 'key' );
[154] Fix | Delete
[155] Fix | Delete
if ( ! self::key_is_valid( $new_api_key ) ) {
[156] Fix | Delete
return rest_ensure_response( new WP_Error( 'invalid_key', __( 'The value provided is not a valid and registered API key.', 'akismet' ), array( 'status' => 400 ) ) );
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
update_option( 'wordpress_api_key', $new_api_key );
[160] Fix | Delete
[161] Fix | Delete
return self::get_key();
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Unset the API key, if possible.
[166] Fix | Delete
*
[167] Fix | Delete
* @param WP_REST_Request $request
[168] Fix | Delete
* @return WP_Error|WP_REST_Response
[169] Fix | Delete
*/
[170] Fix | Delete
public static function delete_key( $request ) {
[171] Fix | Delete
if ( defined( 'WPCOM_API_KEY' ) ) {
[172] Fix | Delete
return rest_ensure_response( new WP_Error( 'hardcoded_key', __( 'This site\'s API key is hardcoded and cannot be deleted.', 'akismet' ), array( 'status'=> 409 ) ) );
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
delete_option( 'wordpress_api_key' );
[176] Fix | Delete
[177] Fix | Delete
return rest_ensure_response( true );
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
/**
[181] Fix | Delete
* Get the Akismet settings.
[182] Fix | Delete
*
[183] Fix | Delete
* @param WP_REST_Request $request
[184] Fix | Delete
* @return WP_Error|WP_REST_Response
[185] Fix | Delete
*/
[186] Fix | Delete
public static function get_settings( $request = null ) {
[187] Fix | Delete
return rest_ensure_response( array(
[188] Fix | Delete
'akismet_strictness' => ( get_option( 'akismet_strictness', '1' ) === '1' ),
[189] Fix | Delete
'akismet_show_user_comments_approved' => ( get_option( 'akismet_show_user_comments_approved', '1' ) === '1' ),
[190] Fix | Delete
) );
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* Update the Akismet settings.
[195] Fix | Delete
*
[196] Fix | Delete
* @param WP_REST_Request $request
[197] Fix | Delete
* @return WP_Error|WP_REST_Response
[198] Fix | Delete
*/
[199] Fix | Delete
public static function set_boolean_settings( $request ) {
[200] Fix | Delete
foreach ( array(
[201] Fix | Delete
'akismet_strictness',
[202] Fix | Delete
'akismet_show_user_comments_approved',
[203] Fix | Delete
) as $setting_key ) {
[204] Fix | Delete
[205] Fix | Delete
$setting_value = $request->get_param( $setting_key );
[206] Fix | Delete
if ( is_null( $setting_value ) ) {
[207] Fix | Delete
// This setting was not specified.
[208] Fix | Delete
continue;
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
// From 4.7+, WP core will ensure that these are always boolean
[212] Fix | Delete
// values because they are registered with 'type' => 'boolean',
[213] Fix | Delete
// but we need to do this ourselves for prior versions.
[214] Fix | Delete
$setting_value = Akismet_REST_API::parse_boolean( $setting_value );
[215] Fix | Delete
[216] Fix | Delete
update_option( $setting_key, $setting_value ? '1' : '0' );
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
return self::get_settings();
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
/**
[223] Fix | Delete
* Parse a numeric or string boolean value into a boolean.
[224] Fix | Delete
*
[225] Fix | Delete
* @param mixed $value The value to convert into a boolean.
[226] Fix | Delete
* @return bool The converted value.
[227] Fix | Delete
*/
[228] Fix | Delete
public static function parse_boolean( $value ) {
[229] Fix | Delete
switch ( $value ) {
[230] Fix | Delete
case true:
[231] Fix | Delete
case 'true':
[232] Fix | Delete
case '1':
[233] Fix | Delete
case 1:
[234] Fix | Delete
return true;
[235] Fix | Delete
[236] Fix | Delete
case false:
[237] Fix | Delete
case 'false':
[238] Fix | Delete
case '0':
[239] Fix | Delete
case 0:
[240] Fix | Delete
return false;
[241] Fix | Delete
[242] Fix | Delete
default:
[243] Fix | Delete
return (bool) $value;
[244] Fix | Delete
}
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
/**
[248] Fix | Delete
* Get the Akismet stats for a given time period.
[249] Fix | Delete
*
[250] Fix | Delete
* Possible `interval` values:
[251] Fix | Delete
* - all
[252] Fix | Delete
* - 60-days
[253] Fix | Delete
* - 6-months
[254] Fix | Delete
*
[255] Fix | Delete
* @param WP_REST_Request $request
[256] Fix | Delete
* @return WP_Error|WP_REST_Response
[257] Fix | Delete
*/
[258] Fix | Delete
public static function get_stats( $request ) {
[259] Fix | Delete
$api_key = Akismet::get_api_key();
[260] Fix | Delete
[261] Fix | Delete
$interval = $request->get_param( 'interval' );
[262] Fix | Delete
[263] Fix | Delete
$stat_totals = array();
[264] Fix | Delete
[265] Fix | Delete
$response = Akismet::http_post( Akismet::build_query( array( 'blog' => get_option( 'home' ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
[266] Fix | Delete
[267] Fix | Delete
if ( ! empty( $response[1] ) ) {
[268] Fix | Delete
$stat_totals[$interval] = json_decode( $response[1] );
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
return rest_ensure_response( $stat_totals );
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
/**
[275] Fix | Delete
* Get the current alert code and message. Alert codes are used to notify the site owner
[276] Fix | Delete
* if there's a problem, like a connection issue between their site and the Akismet API,
[277] Fix | Delete
* invalid requests being sent, etc.
[278] Fix | Delete
*
[279] Fix | Delete
* @param WP_REST_Request $request
[280] Fix | Delete
* @return WP_Error|WP_REST_Response
[281] Fix | Delete
*/
[282] Fix | Delete
public static function get_alert( $request ) {
[283] Fix | Delete
return rest_ensure_response( array(
[284] Fix | Delete
'code' => get_option( 'akismet_alert_code' ),
[285] Fix | Delete
'message' => get_option( 'akismet_alert_msg' ),
[286] Fix | Delete
) );
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
/**
[290] Fix | Delete
* Update the current alert code and message by triggering a call to the Akismet server.
[291] Fix | Delete
*
[292] Fix | Delete
* @param WP_REST_Request $request
[293] Fix | Delete
* @return WP_Error|WP_REST_Response
[294] Fix | Delete
*/
[295] Fix | Delete
public static function set_alert( $request ) {
[296] Fix | Delete
delete_option( 'akismet_alert_code' );
[297] Fix | Delete
delete_option( 'akismet_alert_msg' );
[298] Fix | Delete
[299] Fix | Delete
// Make a request so the most recent alert code and message are retrieved.
[300] Fix | Delete
Akismet::verify_key( Akismet::get_api_key() );
[301] Fix | Delete
[302] Fix | Delete
return self::get_alert( $request );
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
/**
[306] Fix | Delete
* Clear the current alert code and message.
[307] Fix | Delete
*
[308] Fix | Delete
* @param WP_REST_Request $request
[309] Fix | Delete
* @return WP_Error|WP_REST_Response
[310] Fix | Delete
*/
[311] Fix | Delete
public static function delete_alert( $request ) {
[312] Fix | Delete
delete_option( 'akismet_alert_code' );
[313] Fix | Delete
delete_option( 'akismet_alert_msg' );
[314] Fix | Delete
[315] Fix | Delete
return self::get_alert( $request );
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
private static function key_is_valid( $key ) {
[319] Fix | Delete
$response = Akismet::http_post(
[320] Fix | Delete
Akismet::build_query(
[321] Fix | Delete
array(
[322] Fix | Delete
'key' => $key,
[323] Fix | Delete
'blog' => get_option( 'home' )
[324] Fix | Delete
)
[325] Fix | Delete
),
[326] Fix | Delete
'verify-key'
[327] Fix | Delete
);
[328] Fix | Delete
[329] Fix | Delete
if ( $response[1] == 'valid' ) {
[330] Fix | Delete
return true;
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
return false;
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
public static function privileged_permission_callback() {
[337] Fix | Delete
return current_user_can( 'manage_options' );
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
/**
[341] Fix | Delete
* For calls that Akismet.com makes to the site to clear outdated alert codes, use the API key for authorization.
[342] Fix | Delete
*/
[343] Fix | Delete
public static function remote_call_permission_callback( $request ) {
[344] Fix | Delete
$local_key = Akismet::get_api_key();
[345] Fix | Delete
[346] Fix | Delete
return $local_key && ( strtolower( $request->get_param( 'key' ) ) === strtolower( $local_key ) );
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
public static function sanitize_interval( $interval, $request, $param ) {
[350] Fix | Delete
$interval = trim( $interval );
[351] Fix | Delete
[352] Fix | Delete
$valid_intervals = array( '60-days', '6-months', 'all', );
[353] Fix | Delete
[354] Fix | Delete
if ( ! in_array( $interval, $valid_intervals ) ) {
[355] Fix | Delete
$interval = 'all';
[356] Fix | Delete
}
[357] Fix | Delete
[358] Fix | Delete
return $interval;
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
public static function sanitize_key( $key, $request, $param ) {
[362] Fix | Delete
return trim( $key );
[363] Fix | Delete
}
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function