entities = $args;
// Run migration from old "analytics" option names to new "usage" names.
$this->maybe_migrate_options();
define( 'BSF_ANALYTICS_VERSION', $analytics_version );
define( 'BSF_ANALYTICS_URI', $this->get_analytics_url( $analytics_path ) );
add_action( 'admin_init', array( $this, 'handle_optin_optout' ) );
add_action( 'admin_init', array( $this, 'option_notice' ) );
add_action( 'init', array( $this, 'maybe_track_analytics' ), 99 );
$this->set_actions();
add_action( 'admin_init', array( $this, 'register_usage_tracking_setting' ) );
$this->includes();
$this->load_deactivation_survey_actions();
}
/**
* Function to load the deactivation survey form actions.
*
* @since 1.1.6
* @return void
*/
public function load_deactivation_survey_actions() {
// If not in a admin area then return it.
if ( ! is_admin() ) {
return;
}
add_filter( 'uds_survey_vars', array( $this, 'add_slugs_to_uds_vars' ) );
add_action( 'admin_footer', array( $this, 'load_deactivation_survey_form' ) );
}
/**
* Setup actions for admin notice style and analytics cron event.
*
* @since 1.0.4
*/
public function set_actions() {
foreach ( $this->entities as $key => $data ) {
add_action( 'astra_notice_before_markup_' . $key . '-optin-notice', array( $this, 'enqueue_assets' ) );
add_action( 'update_option_' . $key . '_usage_optin', array( $this, 'update_analytics_option_callback' ), 10, 3 );
add_action( 'add_option_' . $key . '_usage_optin', array( $this, 'add_analytics_option_callback' ), 10, 2 );
}
}
/**
* BSF Analytics URL
*
* @param string $analytics_path directory path to analytics library.
* @return String URL of bsf-analytics directory.
* @since 1.0.0
*/
public function get_analytics_url( $analytics_path ) {
$content_dir_path = wp_normalize_path( WP_CONTENT_DIR );
$analytics_path = wp_normalize_path( $analytics_path );
return str_replace( $content_dir_path, content_url(), $analytics_path );
}
/**
* Enqueue Scripts.
*
* @since 1.0.0
* @return void
*/
public function enqueue_assets() {
/**
* Load unminified if SCRIPT_DEBUG is true.
*
* Directory and Extensions.
*/
$dir_name = ( SCRIPT_DEBUG ) ? 'unminified' : 'minified';
$file_rtl = ( is_rtl() ) ? '-rtl' : '';
$css_ext = ( SCRIPT_DEBUG ) ? '.css' : '.min.css';
$css_uri = BSF_ANALYTICS_URI . '/assets/css/' . $dir_name . '/style' . $file_rtl . $css_ext;
wp_enqueue_style( 'bsf-analytics-admin-style', $css_uri, false, BSF_ANALYTICS_VERSION, 'all' );
}
/**
* Send analytics API call.
*
* @since 1.0.0
*/
public function send() {
$api_url = BSF_Analytics_Helper::get_api_url();
wp_remote_post(
$api_url . 'api/analytics/',
array(
'body' => BSF_Analytics_Stats::instance()->get_stats(),
'timeout' => 5,
'blocking' => false,
)
);
}
/**
* Check if usage tracking is enabled.
*
* @return bool
* @since 1.0.0
*/
public function is_tracking_enabled() {
foreach ( $this->entities as $key => $data ) {
$is_enabled = get_site_option( $key . '_usage_optin', false ) === 'yes' ? true : false;
$is_enabled = $this->is_white_label_enabled( $key ) ? false : $is_enabled;
if ( apply_filters( $key . '_tracking_enabled', $is_enabled ) ) {
return true;
}
}
return false;
}
/**
* Check if WHITE label is enabled for BSF products.
*
* @param string $source source of analytics.
* @return bool
* @since 1.0.0
*/
public function is_white_label_enabled( $source ) {
$options = apply_filters( $source . '_white_label_options', array() );
$is_enabled = false;
if ( is_array( $options ) ) {
foreach ( $options as $option ) {
if ( true === $option ) {
$is_enabled = true;
break;
}
}
}
return $is_enabled;
}
/**
* Display admin notice for usage tracking.
*
* @since 1.0.0
*/
public function option_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if( $this->is_tracking_enabled() ) {
return; // Don't need to display notice if any of our plugin already have the permission.
}
// If the user has opted out of tracking, don't show the notice till 7 days.
$last_displayed_time = get_site_option( 'bsf_usage_last_displayed_time', false );
if ( $last_displayed_time && $last_displayed_time > time() - ( 7 * DAY_IN_SECONDS ) ) {
return; // Don't display the notice if it was displayed recently.
}
foreach ( $this->entities as $key => $data ) {
$time_to_display = isset( $data['time_to_display'] ) ? $data['time_to_display'] : '+24 hours';
$usage_doc_link = isset( $data['usage_doc_link'] ) ? $data['usage_doc_link'] : $this->usage_doc_link;
// Don't display the notice if tracking is disabled or White Label is enabled for any of our plugins.
if ( false !== get_site_option( $key . '_usage_optin', false ) || $this->is_white_label_enabled( $key ) ) {
continue;
}
// Show tracker consent notice after 24 hours from installed time.
if ( strtotime( $time_to_display, $this->get_analytics_install_time( $key ) ) > time() ) {
continue;
}
/* translators: %s product name */
$notice_string = sprintf(
__(
'Help us improve %1$s and our other products!
With your permission, we\'d like to collect non-sensitive information from your website — like your PHP version and which features you use — so we can fix bugs faster, make smarter decisions, and build features that actually matter to you. No personal info. Ever.', 'astra'
),
'' . esc_html( $data['product_name'] ) . ''
);
if ( is_multisite() ) {
$notice_string .= __( 'This will be applicable for all sites from the network.', 'astra' );
}
$language_dir = is_rtl() ? 'rtl' : 'ltr';
Astra_Notices::add_notice(
array(
'id' => $key . '-optin-notice',
'type' => '',
'message' => sprintf(
'