| 1 |
<?php |
| 2 |
/** |
| 3 |
* The main loader class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Logger |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
if ( ! class_exists( 'ThemeIsle_SDK_Logger' ) ) : |
| 12 |
/** |
| 13 |
* Class ThemeIsle_SDK_Logger |
| 14 |
* |
| 15 |
* Send the statistics to the Themeisle Endpoint |
| 16 |
*/ |
| 17 |
/** |
| 18 |
* Class ThemeIsle_SDK_Logger |
| 19 |
*/ |
| 20 |
class ThemeIsle_SDK_Logger { |
| 21 |
|
| 22 |
/** |
| 23 |
* @var string $logging_url Url where to send the logs |
| 24 |
*/ |
| 25 |
private $logging_url = 'http://mirror.themeisle.com'; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var ThemeIsle_SDK_Product $product Themeisle Product. |
| 29 |
*/ |
| 30 |
private $product; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var string $product_cron Cron name handler |
| 34 |
*/ |
| 35 |
private $product_cron; |
| 36 |
|
| 37 |
/** |
| 38 |
* ThemeIsle_SDK_Logger constructor. |
| 39 |
* |
| 40 |
* @param ThemeIsle_SDK_Product $product_object Product Object. |
| 41 |
*/ |
| 42 |
public function __construct( $product_object ) { |
| 43 |
if ( $product_object instanceof ThemeIsle_SDK_Product ) { |
| 44 |
$this->product = $product_object; |
| 45 |
$this->product_cron = $product_object->get_key() . '_log_activity'; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Start the cron to send the log. It will randomize the interval in order to not send all the logs at the same time. |
| 52 |
*/ |
| 53 |
public function enable() { |
| 54 |
if ( ! wp_next_scheduled( $this->product_cron ) ) { |
| 55 |
wp_schedule_single_event( time() + ( rand( 15, 24 ) * 3600 ), $this->product_cron ); |
| 56 |
} |
| 57 |
add_action( $this->product_cron, array( $this, 'send_log' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Send the statistics to the api endpoint |
| 62 |
*/ |
| 63 |
public function send_log() { |
| 64 |
wp_remote_post( $this->logging_url, array( |
| 65 |
'method' => 'POST', |
| 66 |
'timeout' => 3, |
| 67 |
'redirection' => 5, |
| 68 |
'headers' => array( |
| 69 |
'X-ThemeIsle-Event' => 'log_site', |
| 70 |
), |
| 71 |
'body' => array( |
| 72 |
'site' => get_site_url(), |
| 73 |
'product' => $this->product->get_slug(), |
| 74 |
'version' => $this->product->get_version(), |
| 75 |
), |
| 76 |
) ); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
endif; |
| 81 |
|