2020-04-07 13:03:04 +00:00
< ? php
/**
* The logger model class for ThemeIsle SDK
*
* @ package ThemeIsleSDK
* @ subpackage Modules
* @ copyright Copyright ( c ) 2017 , Marius Cristea
* @ license http :// opensource . org / licenses / gpl - 3.0 . php GNU Public License
* @ since 1.0 . 0
*/
namespace ThemeisleSDK\Modules ;
use ThemeisleSDK\Common\Abstract_Module ;
use ThemeisleSDK\Loader ;
use ThemeisleSDK\Product ;
// Exit if accessed directly.
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* Logger module for ThemeIsle SDK .
*/
class Logger extends Abstract_Module {
/**
* Endpoint where to collect logs .
*/
2020-07-24 14:08:54 +00:00
const TRACKING_ENDPOINT = 'https://api.themeisle.com/tracking/log' ;
2020-04-07 13:03:04 +00:00
2024-01-03 21:51:15 +00:00
/**
* Endpoint where to collect telemetry .
*/
const TELEMETRY_ENDPOINT = 'https://api.themeisle.com/tracking/events' ;
2020-04-07 13:03:04 +00:00
/**
* Check if we should load the module for this product .
*
* @ param Product $product Product to load the module for .
*
* @ return bool Should we load ?
*/
public function can_load ( $product ) {
return apply_filters ( $product -> get_slug () . '_sdk_enable_logger' , true );
}
/**
* Load module logic .
*
* @ param Product $product Product to load .
*
* @ return Logger Module object .
*/
public function load ( $product ) {
$this -> product = $product ;
$this -> setup_notification ();
$this -> setup_actions ();
return $this ;
}
/**
* Setup notification on admin .
*/
public function setup_notification () {
if ( ! $this -> product -> is_wordpress_available () ) {
return ;
}
add_filter ( 'themeisle_sdk_registered_notifications' , [ $this , 'add_notification' ] );
}
/**
* Setup tracking actions .
*/
public function setup_actions () {
if ( ! $this -> is_logger_active () ) {
return ;
}
2024-01-03 21:51:15 +00:00
$can_load_telemetry = apply_filters ( 'themeisle_sdk_enable_telemetry' , false );
if ( $can_load_telemetry ) {
add_action ( 'admin_enqueue_scripts' , array ( $this , 'load_telemetry' ) );
}
2020-04-07 13:03:04 +00:00
$action_key = $this -> product -> get_key () . '_log_activity' ;
if ( ! wp_next_scheduled ( $action_key ) ) {
2021-07-25 23:25:06 +00:00
wp_schedule_single_event ( time () + ( wp_rand ( 1 , 24 ) * 3600 ), $action_key );
2020-04-07 13:03:04 +00:00
}
add_action ( $action_key , array ( $this , 'send_log' ) );
}
/**
* Check if the logger is active .
*
* @ return bool Is logger active ?
*/
private function is_logger_active () {
2020-07-24 14:08:54 +00:00
$default = 'no' ;
2020-04-07 13:03:04 +00:00
2020-07-24 14:08:54 +00:00
if ( ! $this -> product -> is_wordpress_available () ) {
$default = 'yes' ;
} else {
$pro_slug = $this -> product -> get_pro_slug ();
if ( ! empty ( $pro_slug ) ) {
$all_products = Loader :: get_products ();
if ( isset ( $all_products [ $pro_slug ] ) ) {
$default = 'yes' ;
}
2020-04-07 13:03:04 +00:00
}
}
2020-07-24 14:08:54 +00:00
return ( get_option ( $this -> product -> get_key () . '_logger_flag' , $default ) === 'yes' );
2020-04-07 13:03:04 +00:00
}
/**
* Add notification to queue .
*
* @ param array $all_notifications Previous notification .
*
* @ return array All notifications .
*/
public function add_notification ( $all_notifications ) {
$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.' );
$message = str_replace (
array ( '{product}' ),
$this -> product -> get_friendly_name (),
$message
);
$button_submit = apply_filters ( $this -> product -> get_key () . '_logger_button_submit' , 'Sure, I would love to help.' );
$button_cancel = apply_filters ( $this -> product -> get_key () . '_logger_button_cancel' , 'No, thanks.' );
$all_notifications [] = [
'id' => $this -> product -> get_key () . '_logger_flag' ,
'message' => $message ,
'ctas' => [
'confirm' => [
'link' => '#' ,
'text' => $button_submit ,
],
'cancel' => [
'link' => '#' ,
'text' => $button_cancel ,
],
],
];
return $all_notifications ;
}
/**
* Send the statistics to the api endpoint .
*/
public function send_log () {
$environment = array ();
$theme = wp_get_theme ();
$environment [ 'theme' ] = array ();
$environment [ 'theme' ][ 'name' ] = $theme -> get ( 'Name' );
$environment [ 'theme' ][ 'author' ] = $theme -> get ( 'Author' );
2020-07-24 14:08:54 +00:00
$environment [ 'theme' ][ 'parent' ] = $theme -> parent () !== false ? $theme -> parent () -> get ( 'Name' ) : $theme -> get ( 'Name' );
2020-04-07 13:03:04 +00:00
$environment [ 'plugins' ] = get_option ( 'active_plugins' );
global $wp_version ;
wp_remote_post (
self :: TRACKING_ENDPOINT ,
array (
'method' => 'POST' ,
'timeout' => 3 ,
'redirection' => 5 ,
'body' => array (
'site' => get_site_url (),
'slug' => $this -> product -> get_slug (),
'version' => $this -> product -> get_version (),
'wp_version' => $wp_version ,
2020-07-24 14:08:54 +00:00
'locale' => get_locale (),
2020-04-07 13:03:04 +00:00
'data' => apply_filters ( $this -> product -> get_key () . '_logger_data' , array () ),
'environment' => $environment ,
'license' => apply_filters ( $this -> product -> get_key () . '_license_status' , '' ),
),
)
);
}
2024-01-03 21:51:15 +00:00
/**
* Load telemetry .
*
* @ return void
*/
public function load_telemetry () {
// See which products have telemetry enabled.
try {
$products_with_telemetry = array ();
$all_products = Loader :: get_products ();
$all_products [ $this -> product -> get_slug () ] = $this -> product ; // Add current product to the list of products to check for telemetry.
foreach ( $all_products as $product_slug => $product ) {
// Ignore pro products.
if ( false !== strstr ( $product_slug , 'pro' ) ) {
continue ;
}
$default = 'no' ;
if ( ! $product -> is_wordpress_available () ) {
$default = 'yes' ;
} else {
$pro_slug = $product -> get_pro_slug ();
if ( ! empty ( $pro_slug ) && isset ( $all_products [ $pro_slug ] ) ) {
$default = 'yes' ;
}
}
if ( 'yes' === get_option ( $product -> get_key () . '_logger_flag' , $default ) ) {
$main_slug = explode ( '-' , $product_slug );
$main_slug = $main_slug [ 0 ];
$pro_slug = $product -> get_pro_slug ();
$track_hash = Licenser :: create_license_hash ( str_replace ( '-' , '_' , ! empty ( $pro_slug ) ? $pro_slug : $product_slug ) );
// Check if product was already tracked.
$active_telemetry = false ;
foreach ( $products_with_telemetry as & $product_with_telemetry ) {
if ( $product_with_telemetry [ 'slug' ] === $main_slug ) {
$active_telemetry = true ;
break ;
}
}
if ( $active_telemetry ) {
continue ;
}
$products_with_telemetry [] = array (
'slug' => $main_slug ,
'trackHash' => $track_hash ? $track_hash : 'free' ,
'consent' => true ,
);
}
}
$products_with_telemetry = apply_filters ( 'themeisle_sdk_telemetry_products' , $products_with_telemetry );
if ( 0 === count ( $products_with_telemetry ) ) {
return ;
}
2024-03-28 09:39:59 +00:00
$tracking_handler = apply_filters ( 'themeisle_sdk_dependency_script_handler' , 'tracking' );
if ( ! empty ( $tracking_handler ) ) {
do_action ( 'themeisle_sdk_dependency_enqueue_script' , 'tracking' );
wp_localize_script (
$tracking_handler ,
'tiTelemetry' ,
array (
'products' => $products_with_telemetry ,
'endpoint' => self :: TELEMETRY_ENDPOINT ,
)
);
}
2024-01-03 21:51:15 +00:00
} catch ( \Exception $e ) {
if ( defined ( 'WP_DEBUG' ) && WP_DEBUG && defined ( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
error_log ( $e -> getMessage () ); // phpcs:ignore
}
} catch ( \Error $e ) {
if ( defined ( 'WP_DEBUG' ) && WP_DEBUG && defined ( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
error_log ( $e -> getMessage () ); // phpcs:ignore
}
} finally {
return ;
}
}
2020-04-07 13:03:04 +00:00
}