Skip to content

Commit

Permalink
Refactor admin load assets
Browse files Browse the repository at this point in the history
  • Loading branch information
puntope committed Apr 3, 2023
1 parent ad25ad9 commit f3e783b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
7 changes: 6 additions & 1 deletion includes/class-wc-google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
exit; // Exit if accessed directly
}

use WC_Google_Analytics_Integration as Plugin;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;

/**
Expand Down Expand Up @@ -377,7 +378,11 @@ public function load_admin_assets() {
return;
}

wp_enqueue_script( 'wc-google-analytics-admin-enhanced-settings', plugins_url( '/assets/js/build/admin-ga-settings.js', dirname( __FILE__ ) ), array(), WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, true );
wp_enqueue_script('wc-google-analytics-admin-enhanced-settings',
Plugin::get_instance()->get_js_asset_url( 'admin-ga-settings.js' ),
Plugin::get_instance()->get_js_asset_dependencies( 'admin-ga-settings' ),
Plugin::get_instance()->get_js_asset_version( 'admin-ga-settings' ),
true );
}

/**
Expand Down
17 changes: 14 additions & 3 deletions includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
exit;
}

use WC_Google_Analytics_Integration as Plugin;

/**
* WC_Google_Gtag_JS class
*
Expand Down Expand Up @@ -53,16 +55,25 @@ public function setup_frontend_scripts() {
// Filter variation data to include formatted strings required for add_to_cart event
add_filter( 'woocommerce_available_variation', array( $this, 'variant_data' ), 10, 3 );
// Add default inline product data for add to cart tracking
wp_enqueue_script( $this->script_handle );
wp_enqueue_script( $this->script_handle . '-ga-integration' );
}
}

/**
* Register front end JavaScript
*/
public function register_scripts() {
wp_register_script( $this->script_handle . '-variable-product-tracking', plugins_url( 'assets/js/build/ga-integration.js', dirname( __FILE__ ) ), array( 'jquery' ), WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, true );
wp_enqueue_script( $this->script_handle . '-tracking', plugins_url( 'assets/js/build/actions.js', dirname( __FILE__ ) ), array(), WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, true );
wp_register_script(
$this->script_handle . '-ga-integration',
Plugin::get_instance()->get_js_asset_url( 'ga-integration.js' ),
Plugin::get_instance()->get_js_asset_dependencies( 'ga-integration' ),
Plugin::get_instance()->get_js_asset_version( 'ga-integration' ),
true );
wp_enqueue_script( $this->script_handle . '-actions',
Plugin::get_instance()->get_js_asset_url( 'actions.js' ),
Plugin::get_instance()->get_js_asset_dependencies( 'actions' ),
Plugin::get_instance()->get_js_asset_version( 'actions' ),
true );
}

/**
Expand Down
78 changes: 78 additions & 0 deletions woocommerce-google-analytics-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,84 @@ public function maybe_show_ga_pro_notices() {
WC_Admin_Notices::add_custom_notice( 'woocommerce_google_analytics_pro_notice', $notice_html );
update_option( 'woocommerce_google_analytics_pro_notice_shown', true );
}

/**
* Get the path to something in the plugin dir.
*
* @param string $end End of the path.
* @return string
*/
public function path( $end = '' ) {
return untrailingslashit( dirname( __FILE__ ) ) . $end;
}

/**
* Get the URL to something in the plugin dir.
*
* @param string $end End of the URL.
*
* @return string
*/
public function url( $end = '' ) {
return untrailingslashit( plugin_dir_url( plugin_basename( __FILE__ ) ) ) . $end;
}

/**
* Get the URL to something in the plugin admin assets dir.
*
* @param string $end End of the URL.
*
* @return string
*/
public function get_js_asset_url( $end = '' ) {
return $this->url( '/assets/js/build/' . $end );
}

/**
* Get the path to something in the plugin admin dir.
*
* @param string $end End of the path.
* @return string
*/
public function get_js_asset_path($end = '' ) {
return $this->path( '/assets/js/build/' . $end );
}

/**
* Gets the asset.php generated file for an asset name.
*
* @param string $asset_name The name of the asset to get the file from.
* @return array The asset file. Or an empty array if the file doesn't exist.
*/
public function get_js_asset_file( $asset_name ) {
try {
return require $this->get_js_asset_path( $asset_name . '.asset.php' );
} catch ( Exception $e ) {
return [];
}
}

/**
* Gets the dependencies for an assets based on its asset.php generated file.
*
* @param string $asset_name The name of the asset to get the dependencies from.
* @return array The dependencies array. Empty array if no dependencies are found.
*/
public function get_js_asset_dependencies( $asset_name ) {
$script_assets = $this->get_js_asset_file( $asset_name );
return $script_assets[ 'dependencies' ] ?? [];
}

/**
* Gets the version for an assets based on its asset.php generated file.
*
* @param string $asset_name The name of the asset to get the version from.
* @return string|false The version. False in case no version is found.
*/
public function get_js_asset_version( $asset_name ) {
$script_assets = $this->get_js_asset_file( $asset_name );
return $script_assets[ 'version' ] ?? false;
}
}

add_action( 'plugins_loaded', array( 'WC_Google_Analytics_Integration', 'get_instance' ), 0 );
Expand Down

0 comments on commit f3e783b

Please sign in to comment.