Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/wordads/php
File: class-wordads-api.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* The WordAds API.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
use Automattic\Jetpack\Connection\Client;
[7] Fix | Delete
use Automattic\Jetpack\Status;
[8] Fix | Delete
[9] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[10] Fix | Delete
exit( 0 );
[11] Fix | Delete
}
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Methods for accessing data through the WPCOM REST API
[15] Fix | Delete
*
[16] Fix | Delete
* @since 4.5.0
[17] Fix | Delete
*/
[18] Fix | Delete
class WordAds_API {
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Get the site's WordAds status
[22] Fix | Delete
*
[23] Fix | Delete
* @return array|WP_Error Array of site status values, or WP_Error if no response from the API.
[24] Fix | Delete
*
[25] Fix | Delete
* @since 4.5.0
[26] Fix | Delete
*/
[27] Fix | Delete
public static function get_wordads_status() {
[28] Fix | Delete
global $wordads_status_response;
[29] Fix | Delete
[30] Fix | Delete
// If the site is not connected, we can put it in a safe "house ad" mode.
[31] Fix | Delete
if ( ( new Status() )->is_offline_mode() ) {
[32] Fix | Delete
return array(
[33] Fix | Delete
'approved' => true,
[34] Fix | Delete
'active' => true,
[35] Fix | Delete
'house' => true,
[36] Fix | Delete
'unsafe' => false,
[37] Fix | Delete
);
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
// Fetch the status from WPCOM endpoint.
[41] Fix | Delete
$endpoint = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) );
[42] Fix | Delete
$response = Client::wpcom_json_api_request_as_blog( $endpoint );
[43] Fix | Delete
$wordads_status_response = $response;
[44] Fix | Delete
[45] Fix | Delete
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
[46] Fix | Delete
return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
$body = json_decode( wp_remote_retrieve_body( $response ) );
[50] Fix | Delete
[51] Fix | Delete
return array(
[52] Fix | Delete
'approved' => (bool) $body->approved,
[53] Fix | Delete
'active' => (bool) $body->active,
[54] Fix | Delete
'house' => (bool) $body->house,
[55] Fix | Delete
'unsafe' => (bool) $body->unsafe,
[56] Fix | Delete
);
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Grab WordAds status from WP.com API and store as option
[61] Fix | Delete
*
[62] Fix | Delete
* @since 4.5.0
[63] Fix | Delete
*/
[64] Fix | Delete
public static function update_wordads_status_from_api() {
[65] Fix | Delete
$status = self::get_wordads_status();
[66] Fix | Delete
[67] Fix | Delete
if ( ! is_wp_error( $status ) ) {
[68] Fix | Delete
[69] Fix | Delete
// Convert boolean options to string first to work around update_option not setting the option if the value is false.
[70] Fix | Delete
// This sets the option to either '1' if true or '' if false.
[71] Fix | Delete
update_option( 'wordads_approved', (string) $status['approved'], true );
[72] Fix | Delete
update_option( 'wordads_active', (string) $status['active'], true );
[73] Fix | Delete
update_option( 'wordads_house', (string) $status['house'], true );
[74] Fix | Delete
update_option( 'wordads_unsafe', (string) $status['unsafe'], true );
[75] Fix | Delete
}
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Returns the ads.txt content needed to run WordAds.
[80] Fix | Delete
*
[81] Fix | Delete
* @return array string contents of the ads.txt file.
[82] Fix | Delete
*
[83] Fix | Delete
* @since 6.1.0
[84] Fix | Delete
*/
[85] Fix | Delete
public static function get_wordads_ads_txt() {
[86] Fix | Delete
global $wordads_status_response;
[87] Fix | Delete
[88] Fix | Delete
$endpoint = sprintf( '/sites/%d/wordads/ads-txt', Jetpack::get_option( 'id' ) );
[89] Fix | Delete
$response = Client::wpcom_json_api_request_as_blog( $endpoint );
[90] Fix | Delete
$wordads_status_response = $response;
[91] Fix | Delete
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
[92] Fix | Delete
return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
$body = json_decode( wp_remote_retrieve_body( $response ) );
[96] Fix | Delete
$ads_txt = str_replace( '\\n', PHP_EOL, $body->adstxt );
[97] Fix | Delete
[98] Fix | Delete
return $ads_txt;
[99] Fix | Delete
}
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function