| 1 |
<?php |
| 2 |
/** |
| 3 |
* The logger model class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Modules |
| 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 |
|
| 12 |
namespace ThemeisleSDK\Modules; |
| 13 |
|
| 14 |
use ThemeisleSDK\Common\Abstract_Module; |
| 15 |
use ThemeisleSDK\Loader; |
| 16 |
use ThemeisleSDK\Product; |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Logger module for ThemeIsle SDK. |
| 25 |
*/ |
| 26 |
class Logger extends Abstract_Module { |
| 27 |
/** |
| 28 |
* Endpoint where to collect logs. |
| 29 |
*/ |
| 30 |
const TRACKING_ENDPOINT = 'https://api.themeisle.com/tracking/log'; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Check if we should load the module for this product. |
| 35 |
* |
| 36 |
* @param Product $product Product to load the module for. |
| 37 |
* |
| 38 |
* @return bool Should we load ? |
| 39 |
*/ |
| 40 |
public function can_load( $product ) { |
| 41 |
|
| 42 |
return apply_filters( $product->get_slug() . '_sdk_enable_logger', true ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Load module logic. |
| 47 |
* |
| 48 |
* @param Product $product Product to load. |
| 49 |
* |
| 50 |
* @return Logger Module object. |
| 51 |
*/ |
| 52 |
public function load( $product ) { |
| 53 |
$this->product = $product; |
| 54 |
$this->setup_notification(); |
| 55 |
$this->setup_actions(); |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Setup notification on admin. |
| 62 |
*/ |
| 63 |
public function setup_notification() { |
| 64 |
if ( ! $this->product->is_wordpress_available() ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
add_filter( 'themeisle_sdk_registered_notifications', [ $this, 'add_notification' ] ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Setup tracking actions. |
| 74 |
*/ |
| 75 |
public function setup_actions() { |
| 76 |
if ( ! $this->is_logger_active() ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
$action_key = $this->product->get_key() . '_log_activity'; |
| 80 |
if ( ! wp_next_scheduled( $action_key ) ) { |
| 81 |
wp_schedule_single_event( time() + ( rand( 1, 24 ) * 3600 ), $action_key ); |
| 82 |
} |
| 83 |
add_action( $action_key, array( $this, 'send_log' ) ); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if the logger is active. |
| 89 |
* |
| 90 |
* @return bool Is logger active? |
| 91 |
*/ |
| 92 |
private function is_logger_active() { |
| 93 |
$default = 'no'; |
| 94 |
|
| 95 |
if ( ! $this->product->is_wordpress_available() ) { |
| 96 |
$default = 'yes'; |
| 97 |
} else { |
| 98 |
$pro_slug = $this->product->get_pro_slug(); |
| 99 |
|
| 100 |
if ( ! empty( $pro_slug ) ) { |
| 101 |
$all_products = Loader::get_products(); |
| 102 |
if ( isset( $all_products[ $pro_slug ] ) ) { |
| 103 |
$default = 'yes'; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return ( get_option( $this->product->get_key() . '_logger_flag', $default ) === 'yes' ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Add notification to queue. |
| 113 |
* |
| 114 |
* @param array $all_notifications Previous notification. |
| 115 |
* |
| 116 |
* @return array All notifications. |
| 117 |
*/ |
| 118 |
public function add_notification( $all_notifications ) { |
| 119 |
|
| 120 |
$message = apply_filters( $this->product->get_key() . '_logger_heading', 'Do you enjoy <b>{product}</b>? Become a contributor by opting in to our anonymous data tracking. We guarantee no sensitive data is collected.' ); |
| 121 |
|
| 122 |
$message = str_replace( |
| 123 |
array( '{product}' ), |
| 124 |
$this->product->get_friendly_name(), |
| 125 |
$message |
| 126 |
); |
| 127 |
$button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', 'Sure, I would love to help.' ); |
| 128 |
$button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', 'No, thanks.' ); |
| 129 |
|
| 130 |
$all_notifications[] = [ |
| 131 |
'id' => $this->product->get_key() . '_logger_flag', |
| 132 |
'message' => $message, |
| 133 |
'ctas' => [ |
| 134 |
'confirm' => [ |
| 135 |
'link' => '#', |
| 136 |
'text' => $button_submit, |
| 137 |
], |
| 138 |
'cancel' => [ |
| 139 |
'link' => '#', |
| 140 |
'text' => $button_cancel, |
| 141 |
], |
| 142 |
], |
| 143 |
]; |
| 144 |
|
| 145 |
return $all_notifications; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Send the statistics to the api endpoint. |
| 150 |
*/ |
| 151 |
public function send_log() { |
| 152 |
$environment = array(); |
| 153 |
$theme = wp_get_theme(); |
| 154 |
$environment['theme'] = array(); |
| 155 |
$environment['theme']['name'] = $theme->get( 'Name' ); |
| 156 |
$environment['theme']['author'] = $theme->get( 'Author' ); |
| 157 |
$environment['theme']['parent'] = $theme->parent() !== false ? $theme->parent()->get( 'Name' ) : $theme->get( 'Name' ); |
| 158 |
$environment['plugins'] = get_option( 'active_plugins' ); |
| 159 |
global $wp_version; |
| 160 |
wp_remote_post( |
| 161 |
self::TRACKING_ENDPOINT, |
| 162 |
array( |
| 163 |
'method' => 'POST', |
| 164 |
'timeout' => 3, |
| 165 |
'redirection' => 5, |
| 166 |
'body' => array( |
| 167 |
'site' => get_site_url(), |
| 168 |
'slug' => $this->product->get_slug(), |
| 169 |
'version' => $this->product->get_version(), |
| 170 |
'wp_version' => $wp_version, |
| 171 |
'locale' => get_locale(), |
| 172 |
'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), |
| 173 |
'environment' => $environment, |
| 174 |
'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), |
| 175 |
), |
| 176 |
) |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
|