: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* EmbedPress_Plugin_Usage_Tracker
* This class is responsible for data sending to insights.
namespace EmbedPress\Includes\Classes;
* Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
* Main SDK for EmbedPress_Plugin_Usage_Tracker.
if( ! class_exists('EmbedPress_Plugin_Usage_Tracker') ) :
class EmbedPress_Plugin_Usage_Tracker {
const WPINS_VERSION = '3.0.2';
const API_URL = 'https://send.wpinsight.com/process-plugin-data';
private $plugin_file = null;
private $plugin_name = null;
* How often the event should subsequently
public $recurrence = 'daily';
private $event_hook = null;
* Instace of EmbedPress_Plugin_Usage_Tracker
* @var EmbedPress_Plugin_Usage_Tracker
private static $_instance = null;
private $disabled_wp_cron;
private $enable_self_cron;
private $include_goodbye_form;
* Get Instance of EmbedPress_Plugin_Usage_Tracker
* @return EmbedPress_Plugin_Usage_Tracker
public static function get_instance( $plugin_file, $args = [] ){
if( is_null( static::$_instance ) ) {
static::$_instance = new static( $plugin_file, $args );
return static::$_instance;
* Automatically Invoked when initialized.
public function __construct( $plugin_file, $args = [] ){
$this->plugin_file = $plugin_file;
$this->plugin_name = basename( $this->plugin_file, '.php' );
$this->disabled_wp_cron = defined('DISABLE_WP_CRON') && DISABLE_WP_CRON == true;
$this->enable_self_cron = $this->disabled_wp_cron == true ? true : false;
$this->event_hook = 'put_do_weekly_action';
$this->require_optin = isset( $args['opt_in'] ) ? $args['opt_in'] : true;
$this->include_goodbye_form = isset( $args['goodbye_form'] ) ? $args['goodbye_form'] : true;
$this->marketing = isset( $args['email_marketing'] ) ? $args['email_marketing'] : true;
$this->options = isset( $args['options'] ) ? $args['options'] : [];
$this->item_id = isset( $args['item_id'] ) ? $args['item_id'] : false;
register_activation_hook( $this->plugin_file, array( $this, 'activate_this_plugin' ) );
register_deactivation_hook( $this->plugin_file, array( $this, 'deactivate_this_plugin' ) );
* When user agreed to opt-in tracking schedule is enabled.
public function schedule_tracking() {
if( $this->disabled_wp_cron ) {
if ( ! wp_next_scheduled( $this->event_hook ) ) {
wp_schedule_event( time(), $this->recurrence, $this->event_hook );
* Add the schedule event if the plugin is tracked.
public function activate_this_plugin(){
$allow_tracking = $this->is_tracking_allowed();
if( ! $allow_tracking ) {
$this->schedule_tracking();
* Remove the schedule event when plugin is deactivated and send the deactivated reason to inishghts if user submitted.
public function deactivate_this_plugin() {
* Check tracking is allowed or not.
$allow_tracking = $this->is_tracking_allowed();
if( ! $allow_tracking ) {
$body = $this->get_data();
$body['status'] = 'Deactivated';
$body['deactivated_date'] = time();
// Check deactivation reason and add for insights data.
if( false !== get_option( 'wpins_deactivation_reason_' . $this->plugin_name ) ) {
$body['deactivation_reason'] = get_option( 'wpins_deactivation_reason_' . $this->plugin_name );
if( false !== get_option( 'wpins_deactivation_details_' . $this->plugin_name ) ) {
$body['deactivation_details'] = get_option( 'wpins_deactivation_details_' . $this->plugin_name );
$this->send_data( $body );
delete_option( 'wpins_deactivation_reason_' . $this->plugin_name );
delete_option( 'wpins_deactivation_details_' . $this->plugin_name );
* Clear the event schedule.
if( ! $this->disabled_wp_cron ) {
wp_clear_scheduled_hook( $this->event_hook );
* Initial Method to Hook Everything.
add_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name, array( $this, 'clicked' ) );
add_action( $this->event_hook, array( $this, 'do_tracking' ) );
// add_action( 'admin_init', array( $this, 'force_tracking' ) );
add_action( 'wpdeveloper_optin_notice_for_' . $this->plugin_name, array( $this, 'notice' ) );
* Deactivation Reason Form and Submit Data to Insights.
add_filter( 'plugin_action_links_' . plugin_basename( $this->plugin_file ), array( $this, 'deactivate_action_links' ) );
add_action( 'admin_footer-plugins.php', array( $this, 'deactivate_reasons_form' ) );
add_action( 'wp_ajax_deactivation_form_' . esc_attr( $this->plugin_name ), array( $this, 'deactivate_reasons_form_submit' ) );
* For Redirecting Current Page without Arguments!
private function redirect_to(){
$request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$query_string = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
parse_str( $query_string, $current_url );
$unset_array = array( 'dismiss', 'plugin', '_wpnonce', 'later', 'plugin_action', 'marketing_optin' );
foreach( $unset_array as $value ) {
if( isset( $current_url[ $value ] ) ) {
unset( $current_url[ $value ] );
$current_url = http_build_query($current_url);
$redirect_url = $request_uri;
$redirect_url .= '?' . $current_url;
* This method forcing the do_tracking method to execute instant.
public function force_tracking(){
$this->do_tracking( true );
* This method is responsible for all the magic from the front of the plugin.
* @param $force Force tracking if it's not the correct time to track/
public function do_tracking( $force = false ) {
* Check URL is set or not.
if ( empty( self::API_URL ) ) {
* Check is tracking allowed or not.
if( ! $this->is_tracking_allowed() ) {
* Check is this the correct time to track or not.
if( ! $this->is_time_to_track() && ! $force ) {
$body = $this->get_data();
return $this->send_data( $body );
private function is_tracking_allowed() {
// First, check if the user has changed their mind and opted out of tracking
if( $this->has_user_opted_out() ) {
$this->set_is_tracking_allowed( false, $this->plugin_name );
// The wpins_allow_tracking option is an array of plugins that are being tracked
$allow_tracking = get_option( 'wpins_allow_tracking' );
// If this plugin is in the array, then tracking is allowed
if( isset( $allow_tracking[$this->plugin_name] ) ) {
* Set a flag in DB If tracking is allowed.
* @param $is_allowed Boolean true if is allowed.
protected function set_is_tracking_allowed( $is_allowed, $plugin = null ) {
$plugin = $this->plugin_name;
* Get All Tracked Plugin List using this Tracker.
$allow_tracking = get_option( 'wpins_allow_tracking' );
* Check user is opted out for tracking or not.
if( $this->has_user_opted_out() ) {
if( isset( $allow_tracking[$plugin] ) ) {
unset( $allow_tracking[$plugin] );
} else if( $is_allowed || ! $this->require_optin ) {
* If user has agreed to allow tracking
if( empty( $allow_tracking ) || ! is_array( $allow_tracking ) ) {
$allow_tracking = array( $plugin => $plugin );
$allow_tracking[$plugin] = $plugin;
if( isset( $allow_tracking[$plugin] ) ) {
unset( $allow_tracking[$plugin] );
update_option( 'wpins_allow_tracking', $allow_tracking );
* Check the user has opted out or not.
protected function has_user_opted_out() {
if( ! empty( $this->options ) ) {
foreach( $this->options as $option_name ) {
$options = get_option( $option_name );
if( ! empty( $options['wpins_opt_out'] ) ) {
* Check if it's time to track
public function is_time_to_track() {
$track_times = get_option( 'wpins_last_track_time', array() );
return ! isset( $track_times[$this->plugin_name] ) ? true :
( ( isset( $track_times[$this->plugin_name] ) && $track_times[$this->plugin_name] ) < strtotime( '-1 day' ) ? true : false );
public function set_track_time() {
$track_times = get_option( 'wpins_last_track_time', array() );
$track_times[ $this->plugin_name ] = time();
update_option( 'wpins_last_track_time', $track_times );
* This method is responsible for collecting all data.
public function get_data() {
'plugin_slug' => sanitize_text_field( $this->plugin_name ),
'url' => get_bloginfo( 'url' ),
'site_name' => get_bloginfo( 'name' ),
'site_version' => get_bloginfo( 'version' ),
'site_language' => get_bloginfo( 'language' ),
'charset' => get_bloginfo( 'charset' ),
'wpins_version' => self::WPINS_VERSION,
'php_version' => phpversion(),
'multisite' => is_multisite(),
'file_location' => __FILE__
// Collect the email if the correct option has been set
if( ! function_exists( 'wp_get_current_user' ) ) {
include ABSPATH . 'wp-includes/pluggable.php';
$current_user = wp_get_current_user();
$email = $current_user->user_email;
if( is_email( $email ) ) {
$body['marketing_method'] = $this->marketing;
$body['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '';
* Collect all active and inactive plugins
if( ! function_exists( 'get_plugins' ) ) {
include ABSPATH . '/wp-admin/includes/plugin.php';
$plugins = array_keys( get_plugins() );
$active_plugins = is_network_admin() ? array_keys( get_site_option( 'active_sitewide_plugins', array() ) ) : get_option( 'active_plugins', array() );
foreach ( $plugins as $key => $plugin ) {
if ( in_array( $plugin, $active_plugins ) ) {
$body['active_plugins'] = $active_plugins;
$body['inactive_plugins'] = $plugins;
$body['text_direction'] = ( function_exists( 'is_rtl' ) ? ( is_rtl() ? 'RTL' : 'LTR' ) : 'NOT SET' );
$plugin = $this->plugin_data();
$body['message'] .= __( 'We can\'t detect any plugin information. This is most probably because you have not included the code in the plugin main file.', 'embedpress' );
$body['status'] = 'NOT FOUND';
if( isset( $plugin['Name'] ) ) {
$body['plugin'] = sanitize_text_field( $plugin['Name'] );
if( isset( $plugin['Version'] ) ) {
$body['version'] = sanitize_text_field( $plugin['Version'] );
$body['status'] = 'Active';
// $options = $this->options;
// $plugin_options = array();
// if( ! empty( $options ) && is_array( $options ) ) {
// foreach( $options as $option ) {
// $fields = get_option( $option );
// // Check for permission to send this option
// if( isset( $fields['wpins_registered_setting'] ) ) {
// foreach( $fields as $key=>$value ) {
// $plugin_options[$key] = $value;
// $body['plugin_options'] = $this->options; // Returns array
// $body['plugin_options_fields'] = $plugin_options; // Returns object
$body['optional_data'] = $this->get_count_source_data('elementor_source_data', 'gutenberg_source_data');
* Get active theme name and version
$body['theme'] = sanitize_text_field( $theme->Name );
$body['theme_version'] = sanitize_text_field( $theme->Version );
* This method is responsible for couting source data.
public function get_count_source_data($elementor_source_option_name, $gutenbert_source_option_name) {
$elementor_sources = json_decode(get_option($elementor_source_option_name), true);
$gutenberg_sources = json_decode(get_option($gutenbert_source_option_name), true);
$elementor_option_name = str_replace("_source_data", "", $elementor_source_option_name);
$gutenberg_option_name = str_replace("_source_data", "", $gutenbert_source_option_name);
foreach ($elementor_sources as $item) {
if (isset($item['source']['name'])) {
$name = strtolower(str_replace(' ', '-', $item['source']['name'])); // normalize source name
$e_counts["$elementor_option_name-$name"] = isset($e_counts["$elementor_option_name-$name"]) ? $e_counts["$elementor_option_name-$name"] + 1 : 1;
foreach ($gutenberg_sources as $item) {
if (isset($item['source']['name'])) {
$name = strtolower(str_replace(' ', '-', $item['source']['name'])); // normalize source name
$g_counts["$gutenberg_option_name-$name"] = isset($g_counts["$gutenberg_option_name-$name"]) ? $g_counts["$gutenberg_option_name-$name"] + 1 : 1;
$counts = array_merge($e_counts, $g_counts);
* Retrieve current plugin information
public function plugin_data() {
if( ! function_exists( 'get_plugin_data' ) ) {
include ABSPATH . '/wp-admin/includes/plugin.php';
$plugin = get_plugin_data( $this->plugin_file );
* Send the data to insights.
public function send_data( $body ) {
$site_id_key = "wpins_{$this->plugin_name}_site_id";
$site_id = get_option( $site_id_key, false );