Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules
File: stats.php
</button>
[1000] Fix | Delete
<div id="dashboard_stats" class="is-loading">
[1001] Fix | Delete
<div class="inside">
[1002] Fix | Delete
<div style="height: 250px;"></div>
[1003] Fix | Delete
</div>
[1004] Fix | Delete
</div>
[1005] Fix | Delete
<?php
[1006] Fix | Delete
}
[1007] Fix | Delete
[1008] Fix | Delete
/**
[1009] Fix | Delete
* Stats Dashboard Widget Content.
[1010] Fix | Delete
*
[1011] Fix | Delete
* TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
[1012] Fix | Delete
*
[1013] Fix | Delete
* @access public
[1014] Fix | Delete
* @return never
[1015] Fix | Delete
*/
[1016] Fix | Delete
function stats_dashboard_widget_content() {
[1017] Fix | Delete
$width = isset( $_GET['width'] ) ? intval( $_GET['width'] ) / 2 : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[1018] Fix | Delete
$height = isset( $_GET['height'] ) ? intval( $_GET['height'] ) - 36 : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[1019] Fix | Delete
if ( ! $width || $width < 250 ) {
[1020] Fix | Delete
$width = 370;
[1021] Fix | Delete
}
[1022] Fix | Delete
if ( ! $height || $height < 230 ) {
[1023] Fix | Delete
$height = 180;
[1024] Fix | Delete
}
[1025] Fix | Delete
[1026] Fix | Delete
$_width = $width - 5;
[1027] Fix | Delete
$_height = $height - 5;
[1028] Fix | Delete
[1029] Fix | Delete
$options = stats_dashboard_widget_options();
[1030] Fix | Delete
$blog_id = Jetpack_Options::get_option( 'id' );
[1031] Fix | Delete
[1032] Fix | Delete
$q = array(
[1033] Fix | Delete
'noheader' => 'true',
[1034] Fix | Delete
'proxy' => '',
[1035] Fix | Delete
'blog' => $blog_id,
[1036] Fix | Delete
'page' => 'stats',
[1037] Fix | Delete
'chart' => '',
[1038] Fix | Delete
'unit' => $options['chart'],
[1039] Fix | Delete
'color' => get_user_option( 'admin_color' ),
[1040] Fix | Delete
'width' => $_width,
[1041] Fix | Delete
'height' => $_height,
[1042] Fix | Delete
'ssl' => is_ssl(),
[1043] Fix | Delete
'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ),
[1044] Fix | Delete
);
[1045] Fix | Delete
[1046] Fix | Delete
$url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
[1047] Fix | Delete
[1048] Fix | Delete
$url = add_query_arg( $q, $url );
[1049] Fix | Delete
$method = 'GET';
[1050] Fix | Delete
$timeout = 90;
[1051] Fix | Delete
$user_id = 0; // Means use the blog token.
[1052] Fix | Delete
[1053] Fix | Delete
$get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
[1054] Fix | Delete
$get_code = wp_remote_retrieve_response_code( $get );
[1055] Fix | Delete
if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
[1056] Fix | Delete
stats_print_wp_remote_error( $get, $url );
[1057] Fix | Delete
} else {
[1058] Fix | Delete
$body = stats_convert_post_titles( $get['body'] );
[1059] Fix | Delete
$body = stats_convert_chart_urls( $body );
[1060] Fix | Delete
$body = stats_convert_image_urls( $body );
[1061] Fix | Delete
echo $body; // phpcs:ignore WordPress.Security.EscapeOutput
[1062] Fix | Delete
}
[1063] Fix | Delete
[1064] Fix | Delete
$post_ids = array();
[1065] Fix | Delete
[1066] Fix | Delete
$csv_end_date = current_time( 'Y-m-d' );
[1067] Fix | Delete
$csv_args = array(
[1068] Fix | Delete
'top' => "&limit=6&end=$csv_end_date",
[1069] Fix | Delete
'search' => "&limit=5&end=$csv_end_date",
[1070] Fix | Delete
);
[1071] Fix | Delete
[1072] Fix | Delete
$top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" );
[1073] Fix | Delete
foreach ( $top_posts as $i => $post ) {
[1074] Fix | Delete
if ( 0 === $post['post_id'] ) {
[1075] Fix | Delete
unset( $top_posts[ $i ] );
[1076] Fix | Delete
continue;
[1077] Fix | Delete
}
[1078] Fix | Delete
$post_ids[] = $post['post_id'];
[1079] Fix | Delete
}
[1080] Fix | Delete
[1081] Fix | Delete
// Cache.
[1082] Fix | Delete
get_posts( array( 'include' => implode( ',', array_unique( $post_ids ) ) ) );
[1083] Fix | Delete
[1084] Fix | Delete
$searches = array();
[1085] Fix | Delete
$search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" );
[1086] Fix | Delete
foreach ( $search_terms as $search_term ) {
[1087] Fix | Delete
if ( 'encrypted_search_terms' === $search_term['searchterm'] ) {
[1088] Fix | Delete
continue;
[1089] Fix | Delete
}
[1090] Fix | Delete
$searches[] = esc_html( $search_term['searchterm'] );
[1091] Fix | Delete
}
[1092] Fix | Delete
[1093] Fix | Delete
?>
[1094] Fix | Delete
<div id="stats-info">
[1095] Fix | Delete
<div id="stats-info-container">
[1096] Fix | Delete
<div class="stats-info-header">
[1097] Fix | Delete
<h2><?php esc_html_e( 'Highlights', 'jetpack' ); ?></h2>
[1098] Fix | Delete
<div class="stats-info-header-right">
[1099] Fix | Delete
<a href="<?php echo esc_url( admin_url( 'admin.php?page=stats' ) ); ?>" class="button button-primary">
[1100] Fix | Delete
<?php esc_html_e( 'View detailed stats', 'jetpack' ); ?>
[1101] Fix | Delete
</a>
[1102] Fix | Delete
</div>
[1103] Fix | Delete
</div>
[1104] Fix | Delete
<div class="stats-info-content">
[1105] Fix | Delete
<div id="top-posts" class="stats-section">
[1106] Fix | Delete
<div class="stats-section-inner">
[1107] Fix | Delete
<h3 class="heading"><?php esc_html_e( 'Top Posts', 'jetpack' ); ?></h3>
[1108] Fix | Delete
<?php
[1109] Fix | Delete
if ( empty( $top_posts ) ) {
[1110] Fix | Delete
?>
[1111] Fix | Delete
<p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
[1112] Fix | Delete
<?php
[1113] Fix | Delete
} else {
[1114] Fix | Delete
foreach ( $top_posts as $post ) {
[1115] Fix | Delete
if ( ! get_post( $post['post_id'] ) ) {
[1116] Fix | Delete
continue;
[1117] Fix | Delete
}
[1118] Fix | Delete
?>
[1119] Fix | Delete
<p>
[1120] Fix | Delete
<?php
[1121] Fix | Delete
printf(
[1122] Fix | Delete
esc_html(
[1123] Fix | Delete
/* Translators: Stats dashboard widget Post list with view count: "Post Title 1 View (or Views if plural)". */
[1124] Fix | Delete
_n( '%1$s %2$s View', '%1$s %2$s Views', $post['views'], 'jetpack' )
[1125] Fix | Delete
),
[1126] Fix | Delete
'<a href="' . esc_url( get_permalink( $post['post_id'] ) ) . '">' . esc_html( get_the_title( $post['post_id'] ) ) . '</a>',
[1127] Fix | Delete
esc_html( number_format_i18n( $post['views'] ) )
[1128] Fix | Delete
);
[1129] Fix | Delete
?>
[1130] Fix | Delete
</p>
[1131] Fix | Delete
<?php
[1132] Fix | Delete
}
[1133] Fix | Delete
}
[1134] Fix | Delete
?>
[1135] Fix | Delete
</div>
[1136] Fix | Delete
</div>
[1137] Fix | Delete
<div id="top-search" class="stats-section">
[1138] Fix | Delete
<div class="stats-section-inner">
[1139] Fix | Delete
<h3 class="heading"><?php esc_html_e( 'Top Searches', 'jetpack' ); ?></h3>
[1140] Fix | Delete
<?php
[1141] Fix | Delete
if ( empty( $searches ) ) {
[1142] Fix | Delete
?>
[1143] Fix | Delete
<p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
[1144] Fix | Delete
<?php
[1145] Fix | Delete
} else {
[1146] Fix | Delete
foreach ( $searches as $search_term_item ) {
[1147] Fix | Delete
printf(
[1148] Fix | Delete
'<p>%s</p>',
[1149] Fix | Delete
esc_html( $search_term_item )
[1150] Fix | Delete
);
[1151] Fix | Delete
}
[1152] Fix | Delete
}
[1153] Fix | Delete
?>
[1154] Fix | Delete
</div>
[1155] Fix | Delete
</div>
[1156] Fix | Delete
</div>
[1157] Fix | Delete
</div>
[1158] Fix | Delete
</div>
[1159] Fix | Delete
<?php
[1160] Fix | Delete
exit( 0 );
[1161] Fix | Delete
}
[1162] Fix | Delete
[1163] Fix | Delete
/**
[1164] Fix | Delete
* Stats Print WP Remote Error.
[1165] Fix | Delete
*
[1166] Fix | Delete
* @access public
[1167] Fix | Delete
* @param mixed $get Get.
[1168] Fix | Delete
* @param mixed $url URL.
[1169] Fix | Delete
* @return void
[1170] Fix | Delete
*/
[1171] Fix | Delete
function stats_print_wp_remote_error( $get, $url ) {
[1172] Fix | Delete
$state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 );
[1173] Fix | Delete
$previous_error = Jetpack::state( $state_name );
[1174] Fix | Delete
$error = md5( wp_json_encode( compact( 'get', 'url' ), JSON_UNESCAPED_SLASHES ) );
[1175] Fix | Delete
Jetpack::state( $state_name, $error );
[1176] Fix | Delete
if ( $error !== $previous_error ) {
[1177] Fix | Delete
?>
[1178] Fix | Delete
<div class="wrap">
[1179] Fix | Delete
<p><?php esc_html_e( 'We were unable to get your stats just now. Please reload this page to try again.', 'jetpack' ); ?></p>
[1180] Fix | Delete
</div>
[1181] Fix | Delete
<?php
[1182] Fix | Delete
return;
[1183] Fix | Delete
}
[1184] Fix | Delete
?>
[1185] Fix | Delete
<div class="wrap">
[1186] Fix | Delete
<p>
[1187] Fix | Delete
<?php
[1188] Fix | Delete
printf(
[1189] Fix | Delete
/* translators: placeholder is an a href for a support site. */
[1190] Fix | Delete
esc_html__( 'We were unable to get your stats just now. Please reload this page to try again. If this error persists, please contact %1$s. In your report, please include the information below.', 'jetpack' ),
[1191] Fix | Delete
sprintf(
[1192] Fix | Delete
'<a href="https://support.wordpress.com/contact/?jetpack=needs-service">%s</a>',
[1193] Fix | Delete
esc_html__( 'Jetpack Support', 'jetpack' )
[1194] Fix | Delete
)
[1195] Fix | Delete
);
[1196] Fix | Delete
?>
[1197] Fix | Delete
</p>
[1198] Fix | Delete
<pre class="stats-widget-error">
[1199] Fix | Delete
User Agent: "<?php echo isset( $_SERVER['HTTP_USER_AGENT'] ) ? esc_html( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized ?>"
[1200] Fix | Delete
Page URL: "http<?php echo ( is_ssl() ? 's' : '' ) . '://' . esc_html( ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized ?>"
[1201] Fix | Delete
API URL: "<?php echo esc_url( $url ); ?>"
[1202] Fix | Delete
<?php
[1203] Fix | Delete
if ( is_wp_error( $get ) ) {
[1204] Fix | Delete
foreach ( $get->get_error_codes() as $code ) {
[1205] Fix | Delete
foreach ( $get->get_error_messages( $code ) as $message ) {
[1206] Fix | Delete
print esc_html( $code ) . ': "' . esc_html( $message ) . '"';
[1207] Fix | Delete
}
[1208] Fix | Delete
}
[1209] Fix | Delete
} else {
[1210] Fix | Delete
$get_code = wp_remote_retrieve_response_code( $get );
[1211] Fix | Delete
$content_length = strlen( wp_remote_retrieve_body( $get ) );
[1212] Fix | Delete
?>
[1213] Fix | Delete
Response code: "<?php print esc_html( $get_code ); ?>"
[1214] Fix | Delete
Content length: "<?php print esc_html( $content_length ); ?>"
[1215] Fix | Delete
<?php
[1216] Fix | Delete
}
[1217] Fix | Delete
?>
[1218] Fix | Delete
</pre>
[1219] Fix | Delete
</div>
[1220] Fix | Delete
<?php
[1221] Fix | Delete
}
[1222] Fix | Delete
[1223] Fix | Delete
/**
[1224] Fix | Delete
* Get stats from WordPress.com
[1225] Fix | Delete
*
[1226] Fix | Delete
* @param string $table The stats which you want to retrieve: postviews, or searchterms.
[1227] Fix | Delete
* @param array $args {
[1228] Fix | Delete
* An associative array of arguments.
[1229] Fix | Delete
*
[1230] Fix | Delete
* @type bool $end The last day of the desired time frame. Format is 'Y-m-d' (e.g. 2007-05-01)
[1231] Fix | Delete
* and default timezone is UTC date. Default value is Now.
[1232] Fix | Delete
* @type string $days The length of the desired time frame. Default is 30. Maximum 90 days.
[1233] Fix | Delete
* @type int $limit The maximum number of records to return. Default is 10. Maximum 100.
[1234] Fix | Delete
* @type int $post_id The ID of the post to retrieve stats data for
[1235] Fix | Delete
* @type string $summarize If present, summarizes all matching records. Default Null.
[1236] Fix | Delete
* @type int $blog_id The WordPress.com blog ID to retrieve stats data for. Default is the current blog.
[1237] Fix | Delete
*
[1238] Fix | Delete
* }
[1239] Fix | Delete
*
[1240] Fix | Delete
* @return array {
[1241] Fix | Delete
* An array of post view data, each post as an array
[1242] Fix | Delete
*
[1243] Fix | Delete
* array {
[1244] Fix | Delete
* The post view data for a single post
[1245] Fix | Delete
*
[1246] Fix | Delete
* @type string $post_id The ID of the post
[1247] Fix | Delete
* @type string $post_title The title of the post
[1248] Fix | Delete
* @type string $post_permalink The permalink for the post
[1249] Fix | Delete
* @type string $views The number of views for the post within the $num_days specified
[1250] Fix | Delete
* }
[1251] Fix | Delete
* }
[1252] Fix | Delete
*/
[1253] Fix | Delete
function stats_get_csv( $table, $args = null ) {
[1254] Fix | Delete
$defaults = array(
[1255] Fix | Delete
'end' => false,
[1256] Fix | Delete
'days' => false,
[1257] Fix | Delete
'limit' => 3,
[1258] Fix | Delete
'post_id' => false,
[1259] Fix | Delete
'summarize' => '',
[1260] Fix | Delete
'blog_id' => Jetpack_Options::get_option( 'id' ),
[1261] Fix | Delete
);
[1262] Fix | Delete
[1263] Fix | Delete
$args = wp_parse_args( $args, $defaults );
[1264] Fix | Delete
$args['table'] = $table;
[1265] Fix | Delete
[1266] Fix | Delete
$stats_csv_url = add_query_arg( $args, 'https://stats.wordpress.com/csv.php' );
[1267] Fix | Delete
[1268] Fix | Delete
$key = md5( $stats_csv_url );
[1269] Fix | Delete
[1270] Fix | Delete
// Get cache.
[1271] Fix | Delete
$stats_cache = get_option( 'stats_cache' );
[1272] Fix | Delete
if ( ! $stats_cache || ! is_array( $stats_cache ) ) {
[1273] Fix | Delete
$stats_cache = array();
[1274] Fix | Delete
}
[1275] Fix | Delete
[1276] Fix | Delete
// Return or expire this key.
[1277] Fix | Delete
if ( isset( $stats_cache[ $key ] ) ) {
[1278] Fix | Delete
$time = key( $stats_cache[ $key ] );
[1279] Fix | Delete
if ( time() - $time < 300 ) {
[1280] Fix | Delete
return $stats_cache[ $key ][ $time ];
[1281] Fix | Delete
}
[1282] Fix | Delete
unset( $stats_cache[ $key ] );
[1283] Fix | Delete
}
[1284] Fix | Delete
[1285] Fix | Delete
$stats_rows = array();
[1286] Fix | Delete
do {
[1287] Fix | Delete
$stats = stats_get_remote_csv( $stats_csv_url );
[1288] Fix | Delete
if ( ! $stats ) {
[1289] Fix | Delete
break;
[1290] Fix | Delete
}
[1291] Fix | Delete
[1292] Fix | Delete
$labels = array_shift( $stats );
[1293] Fix | Delete
[1294] Fix | Delete
if ( 0 === stripos( $labels[0], 'error' ) ) {
[1295] Fix | Delete
break;
[1296] Fix | Delete
}
[1297] Fix | Delete
[1298] Fix | Delete
$stats_rows = array();
[1299] Fix | Delete
for ( $s = 0; isset( $stats[ $s ] ); $s++ ) {
[1300] Fix | Delete
$row = array();
[1301] Fix | Delete
foreach ( $labels as $col => $label ) {
[1302] Fix | Delete
$row[ $label ] = $stats[ $s ][ $col ];
[1303] Fix | Delete
}
[1304] Fix | Delete
$stats_rows[] = $row;
[1305] Fix | Delete
}
[1306] Fix | Delete
} while ( 0 );
[1307] Fix | Delete
[1308] Fix | Delete
// Expire old keys.
[1309] Fix | Delete
foreach ( $stats_cache as $k => $cache ) {
[1310] Fix | Delete
if ( ! is_array( $cache ) || 300 < time() - key( $cache ) ) {
[1311] Fix | Delete
unset( $stats_cache[ $k ] );
[1312] Fix | Delete
}
[1313] Fix | Delete
}
[1314] Fix | Delete
[1315] Fix | Delete
// Set cache.
[1316] Fix | Delete
$stats_cache[ $key ] = array( time() => $stats_rows );
[1317] Fix | Delete
update_option( 'stats_cache', $stats_cache );
[1318] Fix | Delete
[1319] Fix | Delete
return $stats_rows;
[1320] Fix | Delete
}
[1321] Fix | Delete
[1322] Fix | Delete
/**
[1323] Fix | Delete
* Stats get remote CSV.
[1324] Fix | Delete
*
[1325] Fix | Delete
* @access public
[1326] Fix | Delete
* @param mixed $url URL.
[1327] Fix | Delete
* @return array
[1328] Fix | Delete
*/
[1329] Fix | Delete
function stats_get_remote_csv( $url ) {
[1330] Fix | Delete
$method = 'GET';
[1331] Fix | Delete
$timeout = 90;
[1332] Fix | Delete
$user_id = 0; // Blog token.
[1333] Fix | Delete
[1334] Fix | Delete
$get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
[1335] Fix | Delete
$get_code = wp_remote_retrieve_response_code( $get );
[1336] Fix | Delete
if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
[1337] Fix | Delete
return array(); // @todo: return an error?
[1338] Fix | Delete
} else {
[1339] Fix | Delete
return stats_str_getcsv( $get['body'] );
[1340] Fix | Delete
}
[1341] Fix | Delete
}
[1342] Fix | Delete
[1343] Fix | Delete
/**
[1344] Fix | Delete
* Recursively run str_getcsv on the stats csv.
[1345] Fix | Delete
*
[1346] Fix | Delete
* @since 9.7.0 Remove custom handling since str_getcsv is available on all servers running this now.
[1347] Fix | Delete
*
[1348] Fix | Delete
* @param mixed $csv CSV.
[1349] Fix | Delete
* @return array
[1350] Fix | Delete
*/
[1351] Fix | Delete
function stats_str_getcsv( $csv ) {
[1352] Fix | Delete
// @todo Correctly handle embedded newlines. Note, despite claims online, `str_getcsv( $csv, "\n" )` does not actually work.
[1353] Fix | Delete
$lines = explode( "\n", rtrim( $csv, "\n" ) );
[1354] Fix | Delete
return array_map(
[1355] Fix | Delete
function ( $line ) {
[1356] Fix | Delete
// @todo When we drop support for PHP <7.4, consider passing empty-string for `$escape` here for better spec compatibility.
[1357] Fix | Delete
return str_getcsv( $line, ',', '"', '\\' );
[1358] Fix | Delete
},
[1359] Fix | Delete
$lines
[1360] Fix | Delete
);
[1361] Fix | Delete
}
[1362] Fix | Delete
[1363] Fix | Delete
/**
[1364] Fix | Delete
* Abstract out building the rest api stats path.
[1365] Fix | Delete
*
[1366] Fix | Delete
* @param string $resource Resource.
[1367] Fix | Delete
* @return string
[1368] Fix | Delete
*/
[1369] Fix | Delete
function jetpack_stats_api_path( $resource = '' ) {
[1370] Fix | Delete
$resource = ltrim( $resource, '/' );
[1371] Fix | Delete
return sprintf( '/sites/%d/stats/%s', Stats_Options::get_option( 'blog_id' ), $resource );
[1372] Fix | Delete
}
[1373] Fix | Delete
[1374] Fix | Delete
/**
[1375] Fix | Delete
* Fetches stats data from the REST API. Caches locally for 5 minutes.
[1376] Fix | Delete
*
[1377] Fix | Delete
* @link: https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/
[1378] Fix | Delete
* @access public
[1379] Fix | Delete
* @deprecated 11.5 Use WPCOM_Stats available methodsinstead.
[1380] Fix | Delete
* @param array $args (default: array()) The args that are passed to the endpoint.
[1381] Fix | Delete
* @param string $resource (default: '') Optional sub-endpoint following /stats/.
[1382] Fix | Delete
* @return array|WP_Error
[1383] Fix | Delete
*/
[1384] Fix | Delete
function stats_get_from_restapi( $args = array(), $resource = '' ) {
[1385] Fix | Delete
_deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
[1386] Fix | Delete
$endpoint = jetpack_stats_api_path( $resource );
[1387] Fix | Delete
$api_version = '1.1';
[1388] Fix | Delete
$args = wp_parse_args( $args, array() );
[1389] Fix | Delete
$cache_key = md5( implode( '|', array( $endpoint, $api_version, wp_json_encode( $args, JSON_UNESCAPED_SLASHES ) ) ) );
[1390] Fix | Delete
[1391] Fix | Delete
$transient_name = "jetpack_restapi_stats_cache_{$cache_key}";
[1392] Fix | Delete
[1393] Fix | Delete
$stats_cache = get_transient( $transient_name );
[1394] Fix | Delete
[1395] Fix | Delete
// Return or expire this key.
[1396] Fix | Delete
if ( $stats_cache ) {
[1397] Fix | Delete
$time = key( $stats_cache );
[1398] Fix | Delete
$data = $stats_cache[ $time ]; // WP_Error or string (JSON encoded object).
[1399] Fix | Delete
[1400] Fix | Delete
if ( is_wp_error( $data ) ) {
[1401] Fix | Delete
return $data;
[1402] Fix | Delete
}
[1403] Fix | Delete
[1404] Fix | Delete
return (object) array_merge( array( 'cached_at' => $time ), (array) json_decode( $data ) );
[1405] Fix | Delete
}
[1406] Fix | Delete
[1407] Fix | Delete
// Do the dirty work.
[1408] Fix | Delete
$response = Client::wpcom_json_api_request_as_blog( $endpoint, $api_version, $args );
[1409] Fix | Delete
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
[1410] Fix | Delete
// WP_Error.
[1411] Fix | Delete
$data = is_wp_error( $response ) ? $response : new WP_Error( 'stats_error' );
[1412] Fix | Delete
// WP_Error.
[1413] Fix | Delete
$return = $data;
[1414] Fix | Delete
} else {
[1415] Fix | Delete
// string (JSON encoded object).
[1416] Fix | Delete
$data = wp_remote_retrieve_body( $response );
[1417] Fix | Delete
// object (rare: null on JSON failure).
[1418] Fix | Delete
$return = json_decode( $data );
[1419] Fix | Delete
}
[1420] Fix | Delete
[1421] Fix | Delete
// To reduce size in storage: store with time as key, store JSON encoded data (unless error).
[1422] Fix | Delete
set_transient( $transient_name, array( time() => $data ), 5 * MINUTE_IN_SECONDS );
[1423] Fix | Delete
[1424] Fix | Delete
return $return;
[1425] Fix | Delete
}
[1426] Fix | Delete
[1427] Fix | Delete
/**
[1428] Fix | Delete
* Add the Jetpack plugin version to the stats tracking data.
[1429] Fix | Delete
*
[1430] Fix | Delete
* @param array $kvs The stats array in key values.
[1431] Fix | Delete
* @return array
[1432] Fix | Delete
*/
[1433] Fix | Delete
function filter_stats_array_add_jp_version( $kvs ) {
[1434] Fix | Delete
$kvs['j'] = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
[1435] Fix | Delete
[1436] Fix | Delete
return $kvs;
[1437] Fix | Delete
}
[1438] Fix | Delete
[1439] Fix | Delete
/**
[1440] Fix | Delete
* Convert stats array to object after sanity checking the array is valid.
[1441] Fix | Delete
*
[1442] Fix | Delete
* @param array $stats_array The stats array.
[1443] Fix | Delete
* @return WP_Error|Object|null
[1444] Fix | Delete
*/
[1445] Fix | Delete
function convert_stats_array_to_object( $stats_array ) {
[1446] Fix | Delete
_deprecated_function( __FUNCTION__, 'jetpack-13.2', 'Automattic\Jetpack\Stats\WPCOM_Stats->convert_stats_array_to_object' );
[1447] Fix | Delete
[1448] Fix | Delete
return ( new WPCOM_Stats() )->convert_stats_array_to_object( $stats_array );
[1449] Fix | Delete
}
[1450] Fix | Delete
[1451] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function