initial
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Telemetry;
|
||||
|
||||
use Gravity_Forms\Gravity_Tools\Telemetry\Telemetry_Processor;
|
||||
|
||||
class Telemetry_Background_Processor extends Telemetry_Processor {
|
||||
|
||||
public function send_data( $entries ) {
|
||||
// allow overriding the endpoint to use the local or staging environment for testing purposes.
|
||||
$endpoint = defined( 'GF_TELEMETRY_ENDPOINT' ) ? GF_TELEMETRY_ENDPOINT : self::TELEMETRY_ENDPOINT;
|
||||
$site_url = get_site_url();
|
||||
|
||||
if ( array_key_exists( 'of', $entries ) ) {
|
||||
$entries = array( $entries );
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'license_key_md5' => md5( get_option( 'rg_gforms_key', '' ) ),
|
||||
'site_url' => $site_url,
|
||||
'product' => 'gravitysmtp',
|
||||
'tag' => 'system_report',
|
||||
'data' => $entries,
|
||||
);
|
||||
|
||||
return wp_remote_post(
|
||||
$endpoint . 'api/telemetry_data_bulk',
|
||||
array(
|
||||
'headers' => array(
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => sha1( $site_url ),
|
||||
),
|
||||
'method' => 'POST',
|
||||
'data_format' => 'body',
|
||||
'body' => json_encode( $data ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Telemetry;
|
||||
|
||||
use Gravity_Forms\Gravity_Tools\Logging\Logger;
|
||||
use Gravity_Forms\Gravity_Tools\Utils\Utils_Service_Provider;
|
||||
|
||||
class Telemetry_Handler {
|
||||
|
||||
/**
|
||||
* @var Logger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var Telemetry_Snapshot_Data
|
||||
*/
|
||||
protected $snapshot_data;
|
||||
|
||||
/**
|
||||
* @var Telemetry_Background_Processor
|
||||
*/
|
||||
protected $processor;
|
||||
|
||||
public function __construct( $logger, $snapshot_data, $processor ) {
|
||||
$this->logger = $logger;
|
||||
$this->snapshot_data = $snapshot_data;
|
||||
$this->processor = $processor;
|
||||
}
|
||||
|
||||
public function handle() {
|
||||
// Only run once a week.
|
||||
$last_run = get_option( 'gravitysmtp_last_telemetry_run', 0 );
|
||||
$current_time = time();
|
||||
|
||||
if ( $current_time - $last_run < WEEK_IN_SECONDS ) {
|
||||
return;
|
||||
}
|
||||
|
||||
update_option( 'gravitysmtp_last_telemetry_run', $current_time );
|
||||
|
||||
$this->logger->log_debug( __METHOD__ . sprintf( '(): Enqueuing telemetry batches' ) );
|
||||
|
||||
$this->snapshot_data->record_data();
|
||||
|
||||
$all_data = $this->snapshot_data->get_existing_data();
|
||||
$snapshot = $all_data['snapshot'];
|
||||
|
||||
// Enqueue the snapshot first, alone, to be sent to its own endpoint.
|
||||
$this->processor->push_to_queue( $snapshot );
|
||||
$this->processor->save()->dispatch();
|
||||
|
||||
// Clear saved telemetry data except the snapshot.
|
||||
update_option(
|
||||
$this->snapshot_data->data_setting_name,
|
||||
array(
|
||||
'snapshot' => $snapshot,
|
||||
'events' => array(),
|
||||
),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Telemetry;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Service_Provider;
|
||||
use Gravity_Forms\Gravity_SMTP\Logging\Logging_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Tools\Service_Container;
|
||||
use Gravity_Forms\Gravity_Tools\Service_Provider;
|
||||
use Gravity_Forms\Gravity_Tools\Utils\Utils_Service_Provider;
|
||||
|
||||
class Telemetry_Service_Provider extends Service_Provider {
|
||||
|
||||
const TELEMETRY_BACKGROUND_PROCESSOR = 'telemetry_background_processor';
|
||||
const TELEMETRY_SNAPSHOT_DATA = 'telemetry_snapshot_data';
|
||||
const TELEMETRY_HANDLER = 'telemetry_handler';
|
||||
|
||||
const TELEMETRY_SCHEDULED_TASK = 'gravitysmtp_telemetry_dispatcher';
|
||||
const CRON_HOOK = 'gravitysmtp_cron';
|
||||
const BATCH_SIZE = 10;
|
||||
|
||||
public function register( Service_Container $container ) {
|
||||
$container->add( self::TELEMETRY_BACKGROUND_PROCESSOR, function () use ( $container ) {
|
||||
return new Telemetry_Background_Processor( $container->get( Utils_Service_Provider::COMMON ), $container->get( Logging_Service_Provider::DEBUG_LOGGER ), $container->get( Utils_Service_Provider::CACHE ) );
|
||||
} );
|
||||
|
||||
$container->add( self::TELEMETRY_SNAPSHOT_DATA, function () use ( $container ) {
|
||||
return new Telemetry_Snapshot_Data( $container->get( Utils_Service_Provider::COMMON ), $container->get( Logging_Service_Provider::DEBUG_LOGGER ), $container->get( Connector_Service_Provider::EVENT_MODEL ), $container->get( Connector_Service_Provider::DATA_STORE_ROUTER ) );
|
||||
} );
|
||||
|
||||
$container->add( self::TELEMETRY_HANDLER, function () use ( $container ) {
|
||||
return new Telemetry_Handler( $container->get( Logging_Service_Provider::DEBUG_LOGGER ), $container->get( self::TELEMETRY_SNAPSHOT_DATA ), $container->get( self::TELEMETRY_BACKGROUND_PROCESSOR ) );
|
||||
} );
|
||||
}
|
||||
|
||||
public function init( Service_Container $container ) {
|
||||
add_action( self::TELEMETRY_SCHEDULED_TASK, function () use ( $container ) {
|
||||
$container->get( self::TELEMETRY_HANDLER )->handle();
|
||||
} );
|
||||
|
||||
add_filter( 'gravity_api_remote_post_params', function ( $post ) use ( $container ) {
|
||||
/**
|
||||
* @var Telemetry_Snapshot_Data $snapshot
|
||||
*/
|
||||
$snapshot = $container->get( self::TELEMETRY_SNAPSHOT_DATA );
|
||||
$snapshot->record_data();
|
||||
$telemetry_data = $snapshot->get_existing_data();
|
||||
|
||||
// safety check in case data doesn't exist.
|
||||
if ( empty( $telemetry_data ) ) {
|
||||
return $post;
|
||||
}
|
||||
|
||||
unset( $telemetry_data['snapshot']['gravitysmtp_config'] );
|
||||
|
||||
return $telemetry_data['snapshot'];
|
||||
} );
|
||||
|
||||
if ( ! wp_next_scheduled( self::CRON_HOOK ) ) {
|
||||
wp_schedule_event( time(), 'daily', self::CRON_HOOK );
|
||||
}
|
||||
|
||||
add_action( self::CRON_HOOK, function() {
|
||||
do_action( self::TELEMETRY_SCHEDULED_TASK );
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Telemetry;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Endpoints\Save_Connector_Settings_Endpoint;
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Endpoints\Save_Plugin_Settings_Endpoint;
|
||||
use Gravity_Forms\Gravity_SMTP\Data_Store\Data_Store_Router;
|
||||
use Gravity_Forms\Gravity_SMTP\Models\Event_Model;
|
||||
use Gravity_Forms\Gravity_Tools\Logging\Logger;
|
||||
use Gravity_Forms\Gravity_Tools\Telemetry\Telemetry_Data;
|
||||
use Gravity_Forms\Gravity_Tools\Utils\Common;
|
||||
|
||||
class Telemetry_Snapshot_Data extends Telemetry_Data {
|
||||
|
||||
const METRIC_MIGRATIONS_RUN = 'migrations_used';
|
||||
|
||||
/**
|
||||
* @var string $key Identifier for this data object.
|
||||
*/
|
||||
public $key = 'snapshot';
|
||||
|
||||
public $enabled_setting_name = Save_Plugin_Settings_Endpoint::PARAM_USAGE_ANALYTICS;
|
||||
|
||||
public $data_setting_name = 'gravitysmtp_usage_analytics';
|
||||
|
||||
/**
|
||||
* @var Common
|
||||
*/
|
||||
protected $common;
|
||||
|
||||
/**
|
||||
* @var Logger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var Event_Model
|
||||
*/
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @var Data_Store_Router
|
||||
*/
|
||||
protected $plugin_settings;
|
||||
|
||||
public function __construct( Common $common, Logger $logger, Event_Model $events, Data_Store_Router $plugin_settings ) {
|
||||
$this->common = $common;
|
||||
$this->logger = $logger;
|
||||
$this->events = $events;
|
||||
$this->plugin_settings = $plugin_settings;
|
||||
}
|
||||
|
||||
public function is_data_collection_allowed() {
|
||||
static $is_allowed;
|
||||
|
||||
if ( ! is_null( $is_allowed ) ) {
|
||||
return $is_allowed;
|
||||
}
|
||||
|
||||
$is_allowed = $this->plugin_settings->get_plugin_setting( $this->enabled_setting_name, true );
|
||||
|
||||
return $is_allowed;
|
||||
}
|
||||
|
||||
public function record_data() {
|
||||
/**
|
||||
* Array of callback functions returning an array of data to be included in the telemetry snapshot.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
$callbacks = array(
|
||||
array( $this, 'get_site_basic_info' ),
|
||||
array( $this, 'get_event_counts' ),
|
||||
array( $this, 'get_gsmtp_config' ),
|
||||
);
|
||||
|
||||
// Merges the default callbacks with any additional callbacks added via the gform_telemetry_snapshot_data filter. Default callbacks are added last so they can't be overridden.
|
||||
$callbacks = array_merge( $this->get_callbacks(), $callbacks );
|
||||
|
||||
$data = array();
|
||||
|
||||
foreach ( $callbacks as $callback ) {
|
||||
if ( is_callable( $callback ) ) {
|
||||
$data = array_merge( $data, call_user_func( $callback ) );
|
||||
}
|
||||
}
|
||||
|
||||
$this->save_data( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get additional callbacks that return data to be included in the telemetry snapshot.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_callbacks() {
|
||||
/**
|
||||
* Filters the non-default data to be included in the telemetry snapshot.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array $new_callbacks An array of callbacks returning an array of data to be included in the telemetry snapshot. Default empty array.
|
||||
*/
|
||||
return apply_filters( 'gravitysmtp_telemetry_snapshot_data', array() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get basic site info for telemetry.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_site_basic_info() {
|
||||
|
||||
if ( ! function_exists( 'get_plugins' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
|
||||
$plugin_list = get_plugins();
|
||||
$plugins = array();
|
||||
|
||||
$active_plugins = get_option( 'active_plugins' );
|
||||
|
||||
foreach ( $plugin_list as $key => $plugin ) {
|
||||
$is_active = in_array( $key, $active_plugins );
|
||||
|
||||
$slug = substr( $key, 0, strpos( $key, '/' ) );
|
||||
if ( empty( $slug ) ) {
|
||||
$slug = str_replace( '.php', '', $key );
|
||||
}
|
||||
|
||||
$plugins[] = array(
|
||||
'name' => str_replace( 'phpinfo()', 'PHP Info', $plugin['Name'] ),
|
||||
'slug' => $slug,
|
||||
'version' => $plugin['Version'],
|
||||
'is_active' => $is_active,
|
||||
);
|
||||
}
|
||||
|
||||
$theme = wp_get_theme();
|
||||
$theme_name = $theme->get( 'Name' );
|
||||
$theme_uri = $theme->get( 'ThemeURI' );
|
||||
$theme_version = $theme->get( 'Version' );
|
||||
$theme_author = $theme->get( 'Author' );
|
||||
$theme_author_uri = $theme->get( 'AuthorURI' );
|
||||
|
||||
$im = is_multisite();
|
||||
$lang = get_locale();
|
||||
$db = Common::get_dbms_type();
|
||||
|
||||
$post = array(
|
||||
'of' => 'gravitysmtp',
|
||||
'key' => $this->common->get_key(),
|
||||
'wp' => get_bloginfo( 'version' ),
|
||||
'php' => phpversion(),
|
||||
'mysql' => Common::get_db_version(),
|
||||
'plugins' => $plugins,
|
||||
'tn' => $theme_name,
|
||||
'tu' => $theme_uri,
|
||||
'tv' => $theme_version,
|
||||
'ta' => $theme_author,
|
||||
'tau' => $theme_author_uri,
|
||||
'im' => $im,
|
||||
'lang' => $lang,
|
||||
'db' => $db,
|
||||
);
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect data regarding event counts.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_event_counts() {
|
||||
if ( ! $this->is_data_collection_allowed() ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return array( 'gravitysmtp_event_counts' => $this->events->get_counts() );
|
||||
}
|
||||
|
||||
public function get_gsmtp_config() {
|
||||
if ( ! $this->is_data_collection_allowed() ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return array(
|
||||
'gravitysmtp_config' => array(
|
||||
Save_Plugin_Settings_Endpoint::PARAM_TEST_MODE => $this->plugin_settings->get_plugin_setting( Save_Plugin_Settings_Endpoint::PARAM_TEST_MODE, false ),
|
||||
Save_Plugin_Settings_Endpoint::PARAM_EVENT_LOG_ENABLED => $this->plugin_settings->get_plugin_setting( Save_Plugin_Settings_Endpoint::PARAM_EVENT_LOG_ENABLED, true ),
|
||||
Save_Plugin_Settings_Endpoint::PARAM_EVENT_LOG_RETENTION => $this->plugin_settings->get_plugin_setting( Save_Plugin_Settings_Endpoint::PARAM_EVENT_LOG_RETENTION, 180 ),
|
||||
Save_Plugin_Settings_Endpoint::PARAM_USAGE_ANALYTICS => $this->plugin_settings->get_plugin_setting( Save_Plugin_Settings_Endpoint::PARAM_USAGE_ANALYTICS, true ),
|
||||
Save_Connector_Settings_Endpoint::SETTING_ENABLED_CONNECTOR => $this->plugin_settings->get_plugin_setting( Save_Connector_Settings_Endpoint::SETTING_ENABLED_CONNECTOR ),
|
||||
self::METRIC_MIGRATIONS_RUN => $this->plugin_settings->get_plugin_setting( self::METRIC_MIGRATIONS_RUN, array() ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the response from the version.php endpoint, to be used by the license service.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array $response Raw response from the API endpoint.
|
||||
*/
|
||||
public function after_send( $response ) {
|
||||
$version_info = array(
|
||||
'is_valid_key' => '1',
|
||||
'version' => '',
|
||||
'url' => '',
|
||||
'is_error' => '1',
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) || $this->common->rgars( $response, 'response/code' ) != 200 ) {
|
||||
$version_info['timestamp'] = time();
|
||||
|
||||
return $version_info;
|
||||
}
|
||||
|
||||
$decoded = json_decode( $response['body'], true );
|
||||
|
||||
if ( empty( $decoded ) ) {
|
||||
$version_info['timestamp'] = time();
|
||||
|
||||
return $version_info;
|
||||
}
|
||||
|
||||
$decoded['timestamp'] = time();
|
||||
|
||||
update_option( 'gsmtp_version_info', $decoded, false );
|
||||
|
||||
$this->logger->log_debug( __METHOD__ . sprintf( '(): Version info cached.' ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user