first commit

This commit is contained in:
User A0264400
2026-04-01 23:20:16 +03:00
commit a766acdc90
23071 changed files with 4933189 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
<?php
/**
* Plugin Name: BSF UTM Analytics
* Description: It is a started and simple which helps you to speedup the process.
* Author: brainstormforce
* Version: 0.0.3
* License: GPL v2
* Text Domain: bsf-utm-analytics
*
* @package bsf-utm-analytics
*/
/**
* Set constants
*/
define( 'BSF_UTM_ANALYTICS_FILE', __FILE__ );
define( 'BSF_UTM_ANALYTICS_BASE', plugin_basename( BSF_UTM_ANALYTICS_FILE ) );
define( 'BSF_UTM_ANALYTICS_DIR', plugin_dir_path( BSF_UTM_ANALYTICS_FILE ) );
define( 'BSF_UTM_ANALYTICS_URL', plugins_url( '/', BSF_UTM_ANALYTICS_FILE ) );
define( 'BSF_UTM_ANALYTICS_VER', '0.0.3' );
define( 'BSF_UTM_ANALYTICS_REFERER_OPTION', 'bsf_product_referers' );
require_once 'plugin-loader.php';

View File

@@ -0,0 +1,144 @@
<?php
/**
* Utils class
*
* @package bsf-utm-analytics
*/
namespace BSF_UTM_Analytics\Inc;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Utils class
*
* @since 0.0.1
*/
class Utils {
/**
* List of slugs of all the bsf products that will be referer, referring another product.
*
* @var array<string>
* @since 0.0.1
*/
private static $bsf_product_slugs = [
'astra',
'astra-sites',
'cartflows',
'checkout-paypal-woo',
'checkout-plugins-stripe-woo',
'presto-player',
'surecart',
'sureforms',
'suretriggers',
'ultimate-addons-for-gutenberg',
'woo-cart-abandonment-recovery',
'variation-swatches-woo',
'zipwp',
];
/**
* This function will help to determine if provided slug is a valid bsf product or not,
* This way we will maintain consistency through out all our products.
*
* @param string $slug unique slug of the product which can be used for referer, product.
* @since 0.0.1
* @return boolean
*/
public static function is_valid_bsf_product_slug( $slug ) {
if ( empty( $slug ) || ! is_string( $slug ) ) {
return false;
}
return in_array( $slug, self::$bsf_product_slugs, true );
}
/**
* This function updates value of referer and product in option
* bsf_product_referer in form of key value pair as 'product' => 'referer'
*
* @param string $referer slug of the product which is refering another product.
* @param string $product slug of the product which is refered.
* @since 0.0.1
* @return void
*/
public static function update_referer( $referer, $product ) {
$slugs = [
'referer' => $referer,
'product' => $product,
];
$error_count = 0;
foreach ( $slugs as $type => $slug ) {
if ( ! self::is_valid_bsf_product_slug( $slug ) ) {
error_log( sprintf( 'Invalid %1$s slug provided "%2$s", does not match bsf_product_slugs', $type, $slug ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- adding logs in case of failure will help in debugging.
$error_count++;
}
}
if ( $error_count > 0 ) {
return;
}
$slugs = array_map( 'sanitize_text_field', $slugs );
$bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER_OPTION, [] );
if ( ! is_array( $bsf_product_referers ) ) {
$bsf_product_referers = [];
}
$bsf_product_referers[ $slugs['product'] ] = $slugs['referer'];
update_option( BSF_UTM_ANALYTICS_REFERER_OPTION, $bsf_product_referers );
}
/**
* This function will add utm_args to pro link or purchase link
* added utm_source by default additional utm_args such as utm_medium etc can be provided to generate location specific links
*
* @param string $link Ideally this should be product site link where utm_params can be tracked.
* @param string $product Product slug whose utm_link need to be created.
* @param mixed $utm_args additional args to be passed ex: [ 'utm_medium' => 'dashboard'].
* @since 0.0.1
* @return string
*/
public static function get_utm_ready_link( $link, $product, $utm_args = [] ) {
if ( false === wp_http_validate_url( $link ) ) {
error_log( 'Invalid url passed to get_utm_ready_link function' ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- adding logs in case of failure will help in debugging.
return $link;
}
if ( empty( $product ) || ! is_string( $product ) || ! self::is_valid_bsf_product_slug( $product ) ) {
error_log( sprintf( 'Invalid product slug provided "%1$s", does not match bsf_product_slugs', $product ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- adding logs in case of failure will help in debugging.
return $link;
}
$bsf_product_referers = get_option( BSF_UTM_ANALYTICS_REFERER_OPTION, [] );
if ( ! is_array( $bsf_product_referers ) || empty( $bsf_product_referers[ $product ] ) ) {
return $link;
}
if ( ! self::is_valid_bsf_product_slug( $bsf_product_referers[ $product ] ) ) {
return $link;
}
if ( ! is_array( $utm_args ) ) {
$utm_args = [];
}
$utm_args['utm_source'] = $bsf_product_referers[ $product ];
$link = add_query_arg(
$utm_args,
$link
);
return $link;
}
}

View File

@@ -0,0 +1,139 @@
<?php
/**
* Plugin Loader.
*
* @package bsf-utm-analytics
* @since 0.0.1
*/
namespace BSF_UTM_Analytics;
/**
* Plugin_Loader
*
* @since 0.0.1
*/
class Plugin_Loader {
/**
* Instance
*
* @access private
* @var object Class Instance.
* @since 0.0.1
*/
private static $instance;
/**
* Initiator
*
* @since 0.0.1
* @return object initialized object of class.
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Autoload classes.
*
* @param string $class class name.
*/
public function autoload( $class ) {
if ( 0 !== strpos( $class, __NAMESPACE__ ) ) {
return;
}
$class_to_load = $class;
$filename = strtolower(
preg_replace(
[ '/^' . __NAMESPACE__ . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ],
[ '', '$1-$2', '-', DIRECTORY_SEPARATOR ],
$class_to_load
)
);
$file = BSF_UTM_ANALYTICS_DIR . $filename . '.php';
// if the file readable, include it.
if ( is_readable( $file ) ) {
require_once $file;
}
}
/**
* Constructor
*
* @since 0.0.1
*/
public function __construct() {
spl_autoload_register( [ $this, 'autoload' ] );
add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );
}
/**
* Load Plugin Text Domain.
* This will load the translation textdomain depending on the file priorities.
* 1. Global Languages /wp-content/languages/bsf-utm-analytics/ folder
* 2. Local directory /wp-content/plugins/bsf-utm-analytics/languages/ folder
*
* @since 0.0.1
* @return void
*/
public function load_textdomain() {
// Default languages directory.
$lang_dir = BSF_UTM_ANALYTICS_DIR . 'languages/';
/**
* Filters the languages directory path to use for plugin.
*
* @param string $lang_dir The languages directory path.
*/
$lang_dir = apply_filters( 'bsf_utm_analytics_languages_directory', $lang_dir );
// Traditional WordPress plugin locale filter.
global $wp_version;
$get_locale = get_locale();
if ( $wp_version >= 4.7 ) {
$get_locale = get_user_locale();
}
/**
* Language Locale for plugin
*
* @var $get_locale The locale to use.
* Uses get_user_locale()` in WordPress 4.7 or greater,
* otherwise uses `get_locale()`.
*/
$locale = apply_filters( 'plugin_locale', $get_locale, 'bsf-utm-analytics' );
$mofile = sprintf( '%1$s-%2$s.mo', 'bsf-utm-analytics', $locale );
// Setup paths to current locale file.
$mofile_global = WP_LANG_DIR . '/plugins/' . $mofile;
$mofile_local = $lang_dir . $mofile;
if ( file_exists( $mofile_global ) ) {
// Look in global /wp-content/languages/bsf-utm-analytics/ folder.
load_textdomain( 'bsf-utm-analytics', $mofile_global );
} elseif ( file_exists( $mofile_local ) ) {
// Look in local /wp-content/plugins/bsf-utm-analytics/languages/ folder.
load_textdomain( 'bsf-utm-analytics', $mofile_local );
} else {
// Load the default language files.
load_plugin_textdomain( 'bsf-utm-analytics', false, $lang_dir );
}
}
}
/**
* Kicking this off by calling 'get_instance()' method
*/
Plugin_Loader::get_instance();

View File

@@ -0,0 +1,3 @@
{
"bsf-utm-analytics": "0.0.3"
}