| 1 |
<?php |
| 2 |
|
| 3 |
namespace Vimeography; |
| 4 |
|
| 5 |
class Logger |
| 6 |
{ |
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
add_action( |
| 10 |
"vimeography.request.response", |
| 11 |
array($this, "maybe_log_response"), |
| 12 |
10, |
| 13 |
6 |
| 14 |
); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Maybe log response |
| 19 |
*/ |
| 20 |
public function maybe_log_response( |
| 21 |
$response, |
| 22 |
$gallery_id, |
| 23 |
$gallery_settings, |
| 24 |
$endpoint, |
| 25 |
$params, |
| 26 |
$headers |
| 27 |
) { |
| 28 |
$enabled = apply_filters('vimeography.logs.enabled', false, $gallery_id); |
| 29 |
|
| 30 |
if (!$enabled) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
global $post; |
| 35 |
|
| 36 |
// Only log front-end request |
| 37 |
if (!is_a($post, 'WP_Post')) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$response_copy = unserialize(serialize($response)); |
| 42 |
$response_code = $response_copy['status']; |
| 43 |
$response_headers = $response_copy['headers']; |
| 44 |
|
| 45 |
foreach ($response['body']->data as $index => $video) { |
| 46 |
$omitted = "[OMITTED BY \Vimeography\Logger]"; |
| 47 |
|
| 48 |
unset($response_copy['body']->data[$index]->embed); |
| 49 |
unset($response_copy['body']->data[$index]->badges); |
| 50 |
unset($response_copy['body']->data[$index]->description); |
| 51 |
unset($response_copy['body']->data[$index]->pictures); |
| 52 |
unset($response_copy['body']->data[$index]->metadata); |
| 53 |
unset($response_copy['body']->data[$index]->user); |
| 54 |
unset($response_copy['body']->data[$index]->tags); |
| 55 |
unset($response_copy['body']->data[$index]->stats); |
| 56 |
unset($response_copy['body']->data[$index]->duration); |
| 57 |
unset($response_copy['body']->data[$index]->width); |
| 58 |
unset($response_copy['body']->data[$index]->height); |
| 59 |
|
| 60 |
$response_copy['body']->data[$index]->fields_omitted_by_logger = |
| 61 |
"embed,badges,description,pictures,metadata,user,tags,stats,duration,width,height"; |
| 62 |
} |
| 63 |
|
| 64 |
$payload = array( |
| 65 |
"generated_at" => date("Y-m-d H:i:s"), |
| 66 |
"post_id" => $post->ID, |
| 67 |
"post_title" => $post->post_title, |
| 68 |
"post_permalink" => get_permalink($post->ID), |
| 69 |
"gallery_id" => $gallery_id, |
| 70 |
"gallery_settings" => $gallery_settings, |
| 71 |
"request_endpoint" => $endpoint, |
| 72 |
"request_params" => $params, |
| 73 |
"request_headers" => $headers, |
| 74 |
"response_code" => $response_code, |
| 75 |
"response_headers" => $response_headers, |
| 76 |
"response_body" => $response_copy['body'] |
| 77 |
); |
| 78 |
|
| 79 |
// expires in 15 minutes |
| 80 |
set_site_transient( |
| 81 |
"vimeography_gallery_" . $gallery_id . "_response", |
| 82 |
$payload, |
| 83 |
900 |
| 84 |
); |
| 85 |
} |
| 86 |
} |
| 87 |
|