-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCron.php
More file actions
100 lines (86 loc) · 1.93 KB
/
Cron.php
File metadata and controls
100 lines (86 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Plausible Analytics | Cron.
*
* @since 1.3.0
* @package WordPress
* @subpackage Plausible Analytics
*/
namespace Plausible\Analytics\WP;
use WpOrg\Requests\Exception\InvalidArgument;
use Exception;
class Cron {
/**
* Cron job handle
*
* @var string
*/
const TASK_NAME = 'plausible_analytics_update_js';
/**
* Build class
*
* @return void
* @throws InvalidArgument
* @throws Exception
*/
public function __construct() {
$this->init();
}
/**
* Run
*
* @return void
* @throws InvalidArgument
* @throws Exception
*/
private function init() {
$this->maybe_download();
}
/**
* Download the plausible.js file if the Proxy is enabled and downloads it to the uploads directory with an alias.
*
* @return bool
* @throws InvalidArgument
* @throws Exception
*/
private function maybe_download() {
if ( ! Helpers::proxy_enabled() ) {
return false;
}
$remote = Helpers::get_js_url();
$local = Helpers::get_js_path();
if ( ! $remote || ! $local ) {
return false;
}
return $this->download_file( $remote, $local );
}
/**
* Downloads a remote file to this server.
*
* @since 1.3.0
*
* @param string $remote_file Full URL to file to download.
*
* @param string $local_file Absolute path to where to store the $remote_file.
*
* @return bool True when successful. False if it fails.
* @throws InvalidArgument
*
* @throws Exception
*/
private function download_file( $remote_file, $local_file ) {
$file_contents = wp_remote_get( $remote_file );
if ( is_wp_error( $file_contents ) ) {
// TODO: add error handling?
return false; // @codeCoverageIgnore
}
/**
* Some servers don't do a full overwrite if file already exists, so we delete it first.
*/
if ( file_exists( $local_file ) ) {
unlink( $local_file );
}
$write = file_put_contents( $local_file, wp_remote_retrieve_body( $file_contents ) );
return $write > 0;
}
}