-
+
- setting_field( $type, $variable, $field_data ) ?>
-
+ setting_field( $type, $variable, $field_data ); ?>
+
- [ 'href' => [] ], 'br' => [] ] ); ?>
+ [ 'href' => [] ],
+ 'br' => [],
+ ]
+ );
+ ?>
@@ -119,9 +135,9 @@ public function setting_row( string $type, string $variable, string $label, arra
/**
* Add setting row.
*
- * @param string $type
- * @param string $variable
- * @param array $field_data
+ * @param string $type The setting type.
+ * @param string $variable The variable.
+ * @param array $field_data Optional field data.
*/
public function setting_field( string $type, string $variable, array $field_data = [] ): void {
@@ -139,7 +155,6 @@ public function setting_field( string $type, string $variable, array $field_data
$this->select( $variable, $field_data );
break;
}
-
}
/**
@@ -182,9 +197,7 @@ public function admin_sidebar() {
* Output a label element.
*
* @param string $text Label text string.
- * @param array $attribute HTML attributes set.
- *
- * @return string The HTML label
+ * @param array $attribute HTML attributes set.
*/
public function label( string $text, array $attribute ): void {
$defaults = [
@@ -204,14 +217,13 @@ public function label( string $text, array $attribute ): void {
if ( $attribute['close'] ) {
echo '';
}
-
}
/**
* Output a legend element.
*
* @param string $text Legend text string.
- * @param array $attribute HTML attributes set.
+ * @param array $attribute HTML attributes set.
*/
public function legend( string $text, array $attribute ): void {
$defaults = [
@@ -229,7 +241,7 @@ public function legend( string $text, array $attribute ): void {
* Create a Checkbox input toggle.
*
* @param string $variable The variable within the option to create the checkbox for.
- * @param array $field_data
+ * @param array $field_data Optional field data.
*/
public function checkbox_toggle_field( string $variable, array $field_data = [] ): void {
@@ -268,7 +280,7 @@ public function checkbox_toggle_field( string $variable, array $field_data = []
* Create a Text input field.
*
* @param string $variable The variable within the option to create the text input field for.
- * @param array $field_data
+ * @param array $field_data Optional field data.
*/
public function text_input_field( string $variable, array $field_data = [] ): void {
@@ -303,15 +315,13 @@ class="textinput "
/>
get_disabled_attribute( $variable, $attribute );
- echo '
';
+ echo '
';
}
/**
@@ -348,7 +358,7 @@ public function textarea( string $variable, string $label, $attribute = [] ): vo
*
* @param string $variable The variable within the option to create the hidden input for.
* @param string $id The ID of the element.
- * @param mixed $val Optional. The value to set in the input field. Otherwise, the value from the options will be used.
+ * @param mixed $val Optional. The value to set in the input field. Otherwise, the value from the options will be used.
*/
public function hidden( string $variable, string $id = '', $val = null ): void {
if ( is_null( $val ) ) {
@@ -370,7 +380,7 @@ public function hidden( string $variable, string $id = '', $val = null ): void {
* Create a Select Box.
*
* @param string $variable The variable within the option to create the select for.
- * @param array $fieldset_data
+ * @param array $fieldset_data Options fieldset data.
*/
public function select( string $variable, array $fieldset_data = [] ): void {
@@ -380,12 +390,12 @@ public function select( string $variable, array $fieldset_data = [] ): void {
$defaults = [
'disabled' => false,
- 'attributes' => []
+ 'attributes' => [],
];
$fieldset_data = wp_parse_args( $fieldset_data, $defaults );
if ( $this->is_control_disabled( $variable )
- || ( isset( $fieldset_data['disabled'] ) && $fieldset_data['disabled'] ) ) {
+ || ( isset( $fieldset_data['disabled'] ) && $fieldset_data['disabled'] ) ) {
$disabled = true;
} else {
$disabled = false;
@@ -410,15 +420,14 @@ public function select( string $variable, array $fieldset_data = [] ): void {
);
}
- echo "";
-
+ echo '';
}
/**
* Create a Radio input field.
*
* @param string $variable The variable within the option to create the radio button for.
- * @param array $fieldset_data
+ * @param array $fieldset_data Options fieldset data.
*/
public function radio_fieldset( string $variable, array $fieldset_data = [] ): void {
@@ -433,7 +442,7 @@ public function radio_fieldset( string $variable, array $fieldset_data = [] ): v
'legend' => '',
'legend_attr' => [],
'line_break' => true,
- 'attributes' => []
+ 'attributes' => [],
];
$fieldset_data = wp_parse_args( $fieldset_data, $defaults );
@@ -485,7 +494,6 @@ class="radio"
}
}
echo '';
-
}
/**
@@ -519,7 +527,7 @@ protected function is_control_disabled( string $variable ): bool {
* Returns the disabled attribute HTML.
*
* @param string $variable The variable within the option of the related form element.
- * @param array $attribute Extra attributes added to the form element.
+ * @param array $attribute Extra attributes added to the form element.
*
* @return string The disabled attribute HTML.
*/
diff --git a/src/Admin/SetupWizard.php b/src/Admin/SetupWizard.php
index 624d69f..75330c4 100644
--- a/src/Admin/SetupWizard.php
+++ b/src/Admin/SetupWizard.php
@@ -1,4 +1,9 @@
options = $options;
- $this->util = $util;
+ /**
+ * Constructor
+ *
+ * @param Options $options An instance of Options.
+ * @param Util $util An instance of Util.
+ */
+ public function __construct( Options $options, Util $util ) {
+ $this->options = $options;
+ $this->util = $util;
}
/**
@@ -45,6 +60,11 @@ public function hooks() {
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
}
+ /**
+ * Initialize REST
+ *
+ * @return void
+ */
public function rest_init() {
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
}
@@ -80,7 +100,7 @@ public function maybe_redirect_after_activation() {
}
// Only do this for single site installs.
- if ( isset( $_GET['activate-multi'] ) || is_network_admin() ) {
+ if ( isset( $_GET['activate-multi'] ) || is_network_admin() ) { // phpcs:ignore
return;
}
@@ -101,6 +121,8 @@ public function add_dashboard_page(): void {
/**
* Load the assets needed for the Setup Wizard.
+ *
+ * @param string $hook The asset hook.
*/
public function enqueue_assets( string $hook ) {
@@ -108,10 +130,10 @@ public function enqueue_assets( string $hook ) {
return;
}
- $deps_file = GTMKIT_PATH . 'build/admin/wizard.asset.php';
+ $deps_file = GTMKIT_PATH . 'build/admin/wizard.asset.php';
$dependency = [];
if ( file_exists( $deps_file ) ) {
- $deps_file = require( $deps_file );
+ $deps_file = require $deps_file;
$dependency = $deps_file['dependencies'];
$version = $deps_file['version'];
}
@@ -177,8 +199,8 @@ private function settings_error_page( string $id = 'gtmkit-react-site-settings',
@@ -194,7 +216,7 @@ class="gtmkit-py-12 gtmkit-px-20 gtmkit-bg-white gtmkit-border-1 gtmkit-border-c
@@ -214,7 +236,7 @@ class="gtmkit-bg-color-primary gtmkit-text-white gtmkit-rounded-md gtmkit-py-4 g
* @return void
*/
public function register_rest_routes(): void {
- $this->util->rest_API_server->register_rest_route(
+ $this->util->rest_api_server->register_rest_route(
'/get-install-data',
[
'methods' => 'POST',
@@ -222,7 +244,7 @@ public function register_rest_routes(): void {
]
);
- $this->util->rest_API_server->register_rest_route(
+ $this->util->rest_api_server->register_rest_route(
'/get-options',
[
'methods' => 'POST',
@@ -230,7 +252,7 @@ public function register_rest_routes(): void {
]
);
- $this->util->rest_API_server->register_rest_route(
+ $this->util->rest_api_server->register_rest_route(
'/set-options',
[
'methods' => 'POST',
@@ -238,14 +260,13 @@ public function register_rest_routes(): void {
]
);
- $this->util->rest_API_server->register_rest_route(
+ $this->util->rest_api_server->register_rest_route(
'/get-site-data',
[
'methods' => 'POST',
'callback' => [ $this, 'get_site_data' ],
]
);
-
}
/**
@@ -298,8 +319,8 @@ public function get_options(): void {
* @return void
*/
public function set_options(): void {
- $newOptions = json_decode( file_get_contents( 'php://input' ), true );
- $this->options->set( $newOptions );
+ $new_options = json_decode( file_get_contents( 'php://input' ), true );
+ $this->options->set( $new_options );
wp_send_json_success( $this->options->get_all_raw() );
}
@@ -313,5 +334,4 @@ public function get_site_data(): void {
$site_data = $this->util->get_site_data( $this->options->get_all_raw() );
wp_send_json_success( $site_data );
}
-
}
diff --git a/src/Common/RestAPIServer.php b/src/Common/RestAPIServer.php
index cfdde78..a8a51fc 100644
--- a/src/Common/RestAPIServer.php
+++ b/src/Common/RestAPIServer.php
@@ -1,4 +1,9 @@
rest_API_server = $rest_API_server;
+ public function __construct( RestAPIServer $rest_api_server ) {
+ $this->rest_api_server = $rest_api_server;
}
+ /**
+ * Get the site data
+ *
+ * @param array $options The options.
+ *
+ * @return array
+ */
public function get_site_data( array $options ): array {
global $wp_version;
@@ -42,7 +56,6 @@ public function get_site_data( array $options ): array {
$data['multisite'] = is_multisite();
return $data;
-
}
/**
@@ -69,9 +82,9 @@ function get_active_plugins(): array {
/**
* Add plugin to array if active.
*
- * @param string $plugin
- * @param string $key
- * @param array $data
+ * @param string $plugin The plugin slug.
+ * @param string $key The key.
+ * @param array $data The data.
*
* @return array An array of active plugins names.
*/
@@ -88,7 +101,7 @@ function add_active_plugin_and_version( string $plugin, string $key, array $data
/**
* Anonymize options
*
- * @param array $options
+ * @param array $options The options.
*
* @return array
*/
@@ -110,7 +123,7 @@ function anonymize_options( array $options ): array {
/**
* Shorten version number
*
- * @param string $version
+ * @param string $version The version number.
*
* @return string
*/
@@ -148,31 +161,30 @@ function get_web_server(): string {
* @return string
*/
function get_plugin_version(): string {
- return ( wp_get_environment_type() == 'local' ) ? time() : GTMKIT_VERSION;
+ return ( wp_get_environment_type() === 'local' ) ? time() : GTMKIT_VERSION;
}
/**
* Enqueue script in build
*
- * @param string $handle
- * @param string $script
+ * @param string $handle The script hande.
+ * @param string $script The script name.
*
* @return void
*/
- function enqueue_script( string $handle, string $script): void {
+ function enqueue_script( string $handle, string $script ): void {
$deps_file = GTMKIT_PATH . 'build/' . $script . '.asset.php';
$dependency = [];
- $version = false;
+ $version = false;
if ( file_exists( $deps_file ) ) {
- $deps_file = require( $deps_file );
+ $deps_file = require $deps_file;
$dependency = $deps_file['dependencies'];
$version = $deps_file['version'];
}
wp_enqueue_script( $handle, GTMKIT_URL . 'build/' . $script . '.js', $dependency, $version, true );
}
-
}
diff --git a/src/Frontend/BasicDatalayerData.php b/src/Frontend/BasicDatalayerData.php
index 8cbc381..346819f 100644
--- a/src/Frontend/BasicDatalayerData.php
+++ b/src/Frontend/BasicDatalayerData.php
@@ -1,9 +1,17 @@
options = $options;
}
/**
* Register frontend
*
- * @param Options $options
+ * @param Options $options An instance of Options.
*/
public static function register( Options $options ): void {
$page = new BasicDatalayerData( $options );
@@ -36,6 +44,10 @@ public static function register( Options $options ): void {
/**
* Get the basic dataLayer data
+ *
+ * @param array $datalayer The datalayer.
+ *
+ * @return array
*/
public function get_datalayer_content( array $datalayer ): array {
@@ -92,7 +104,6 @@ public function get_datalayer_content( array $datalayer ): array {
if ( $this->options->get( 'general', 'datalayer_post_author_name' ) ) {
$datalayer['authorId'] = (int) $post->post_author;
}
-
}
if ( is_archive() || is_post_type_archive() ) {
@@ -151,18 +162,18 @@ public function get_datalayer_content( array $datalayer ): array {
/**
* Get priority dataLayer data
*
- * @param array $datalayer
+ * @param array $datalayer The datalayer.
*
* @return array
*/
public function get_priority_datalayer_content( array $datalayer ): array {
if ( $this->options->get( 'general', 'datalayer_page_type' ) ) {
- $page_type = get_post_meta( get_the_ID(), 'gtmkit_page_type', true);
- if ( $page_type ) $datalayer['pageType'] = $page_type;
-
+ $page_type = get_post_meta( get_the_ID(), 'gtmkit_page_type', true );
+ if ( $page_type ) {
+ $datalayer['pageType'] = $page_type;
+ }
}
return $datalayer;
}
-
}
diff --git a/src/Frontend/Frontend.php b/src/Frontend/Frontend.php
index cb73c73..e1b3c36 100644
--- a/src/Frontend/Frontend.php
+++ b/src/Frontend/Frontend.php
@@ -1,9 +1,17 @@
options = $options;
$this->datalayer_name = ( $this->options->get( 'general', 'datalayer_name' ) ) ?: 'dataLayer';
}
@@ -33,7 +41,7 @@ final public function __construct( Options $options ) {
/**
* Register frontend
*
- * @param Options $options
+ * @param Options $options An instance of Options.
*/
public static function register( Options $options ): void {
$page = new Frontend( $options );
@@ -56,9 +64,9 @@ public static function register( Options $options ): void {
$noscript_implementation = Options::init()->get( 'general', 'noscript_implementation' );
- if ( $noscript_implementation == '0' && $container_active ) {
+ if ( $noscript_implementation === '0' && $container_active ) {
add_action( 'wp_body_open', [ $page, 'get_body_script' ] );
- } elseif ( $noscript_implementation == '1' && $container_active ) {
+ } elseif ( $noscript_implementation === '1' && $container_active ) {
add_action( 'body_footer', [ $page, 'get_body_script' ] );
}
}
@@ -69,7 +77,7 @@ public static function register( Options $options ): void {
public function get_header_datalayer(): void {
$settings = [
'datalayer_name' => $this->datalayer_name,
- 'console_log' => Options::init()->get( 'general', 'console_log' )
+ 'console_log' => Options::init()->get( 'general', 'console_log' ),
];
?>
@@ -85,9 +93,13 @@ public function get_header_datalayer(): void {
* Get attributes
*/
public function get_attributes(): void {
- $attributes = apply_filters( 'gtmkit_header_script_attributes', [ 'data-cfasync' => 'false',
- 'data-nowprocket' => ''
- ] );
+ $attributes = apply_filters(
+ 'gtmkit_header_script_attributes',
+ [
+ 'data-cfasync' => 'false',
+ 'data-nowprocket' => '',
+ ]
+ );
foreach ( $attributes as $attribute => $value ) {
echo ' ' . esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
@@ -97,8 +109,8 @@ public function get_attributes(): void {
/**
* The dataLayer content included before the GTM container script
*/
-public function get_datalayer_content(): void {
- ?>
+ public function get_datalayer_content(): void {
+ ?>
\n";
-
- }
+ }
/**
* The Google Tag Manager container script
*/
- public function get_header_script(): void {
+ public function get_header_script(): void {
$gtm_id = Options::init()->get( 'general', 'gtm_id' );
@@ -130,10 +141,10 @@ public function get_header_script(): void {
?>
\n\n";
-}
+ echo "\n\n";
+ }
/**
* Get GTM script
+ *
+ * @param string $gtm_id The GTM container ID.
*/
public function get_gtm_script( string $gtm_id ): void {
$domain = ( Options::init()->get( 'general', 'sgtm_domain' ) ) ?: 'www.googletagmanager.com';
@@ -168,8 +181,8 @@ public function get_gtm_script( string $gtm_id ): void {
echo "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
- 'https://" . esc_attr( $domain ) . "/" . esc_attr( $loader ) . ".js?id='+i+dl;f.parentNode.insertBefore(j,f);
- })(window,document,'script','" . esc_js( $this->datalayer_name ) ."','" . esc_attr( $gtm_id ) . "');";
+ 'https://" . esc_attr( $domain ) . '/' . esc_attr( $loader ) . ".js?id='+i+dl;f.parentNode.insertBefore(j,f);
+ })(window,document,'script','" . esc_js( $this->datalayer_name ) . "','" . esc_attr( $gtm_id ) . "');";
}
/**
@@ -189,7 +202,14 @@ public static function container_disabled(): void {
echo '';
}
- public function wp_rocket_exclude_javascript( $pattern ) {
+ /**
+ * Exclude GTM Kit in WP Rocket
+ *
+ * @param array $pattern The exclude list.
+ *
+ * @return array
+ */
+ public function wp_rocket_exclude_javascript( array $pattern ): array {
$pattern[] = 'dataLayer';
$pattern[] = 'gtmkit';
@@ -199,7 +219,7 @@ public function wp_rocket_exclude_javascript( $pattern ) {
/**
* Adds Google Tag Manager domain DNS Prefetch printed by wp_resource_hints
*
- * @param array $hints URLs to print for resource hints.
+ * @param array $hints URLs to print for resource hints.
* @param string $relation_type The relation type the URL are printed for.
*
* @return array URL to print
diff --git a/src/Frontend/UserData.php b/src/Frontend/UserData.php
index 2623f94..495eee2 100644
--- a/src/Frontend/UserData.php
+++ b/src/Frontend/UserData.php
@@ -1,9 +1,17 @@
options = $options;
}
/**
* Register frontend
*
- * @param Options $options
+ * @param Options $options An instance of Options.
*/
public static function register( Options $options ): void {
$page = new static( $options );
@@ -35,6 +43,10 @@ public static function register( Options $options ): void {
/**
* Get the dataLayer content
+ *
+ * @param array $datalayer The datalayer.
+ *
+ * @return array
*/
public function get_datalayer_content( array $datalayer ): array {
@@ -68,5 +80,4 @@ public function get_datalayer_content( array $datalayer ): array {
return $datalayer;
}
-
}
diff --git a/src/Installation/Installation.php b/src/Installation/Installation.php
index 323be23..b320772 100644
--- a/src/Installation/Installation.php
+++ b/src/Installation/Installation.php
@@ -1,9 +1,17 @@
$firstInstall,
+ $plugin_data = [
+ 'first_install' => $first_install,
'import_available' => false,
'woocommerce_integration' => is_plugin_active( 'woocommerce/woocommerce.php' ),
'cf7_integration' => is_plugin_active( 'contact-form-7/wp-contact-form-7.php' ),
@@ -36,12 +41,12 @@ public function get_all(): array {
foreach ( $plugins as $plugin ) {
$settings = $this->get( $plugin );
if ( ! empty( $settings ) ) {
- $pluginData['import_data'][ $plugin ] = $settings;
- $pluginData['import_available'] = true;
+ $plugin_data['import_data'][ $plugin ] = $settings;
+ $plugin_data['import_available'] = true;
}
}
- return $pluginData;
+ return $plugin_data;
}
/**
@@ -65,7 +70,7 @@ public function get( string $slug ): array {
/**
* Extract the container ID from the container script
*
- * @param string $container_script
+ * @param string $container_script The GTM container script.
*
* @return string
*/
@@ -172,7 +177,7 @@ private function get_metronet_tag_manager(): array {
'name' => 'Metronet Tag Manager',
'general' => [
'gtm_id' => $gtm_id,
- ]
+ ],
];
}
@@ -194,7 +199,7 @@ private function get_google_analytics_and_google_tag_manager(): array {
'name' => 'Google Analytics and Google Tag Manager',
'general' => [
'gtm_id' => $gtm_id,
- ]
+ ],
];
}
@@ -215,8 +220,7 @@ private function get_google_tag_manager(): array {
'name' => 'Google Tag Manager',
'general' => [
'gtm_id' => $gtm_id,
- ]
+ ],
];
}
-
}
diff --git a/src/Installation/Upgrade.php b/src/Installation/Upgrade.php
index 73aaf28..db0e1b1 100644
--- a/src/Installation/Upgrade.php
+++ b/src/Installation/Upgrade.php
@@ -1,11 +1,22 @@
get_upgrades();
@@ -50,15 +61,14 @@ protected function v111_upgrade(): void {
$script_implementation = Options::init()->get( 'general', 'script_implementation' );
- if ( $script_implementation == 2 ) {
+ if ( $script_implementation === 2 ) {
$values = [
'general' => [
- 'script_implementation' => '1'
+ 'script_implementation' => '1',
],
];
Options::init()->set( $values, false, false );
}
}
-
}
diff --git a/src/Integration/AbstractEcommerce.php b/src/Integration/AbstractEcommerce.php
index ee684ac..3e25d8b 100644
--- a/src/Integration/AbstractEcommerce.php
+++ b/src/Integration/AbstractEcommerce.php
@@ -1,8 +1,9 @@
grouped_product_position = 1;
@@ -54,15 +58,16 @@ abstract static function instance();
/**
* Register frontend
*
- * @param Options $options
- * @param Util $util
+ * @param Options $options The Options instance.
+ * @param Util $util The Util instance.
*/
- abstract public static function register( Options $options, Util $util): void;
+ abstract public static function register( Options $options, Util $util ): void;
/**
* Get the primary product category of a product. If the primary category is not available the first assigned category will be returned.
*
- * @param int $product_id The product ID
+ * @param int $product_id The product ID.
+ * @param string $taxonomy The taxonomy slug.
*
* @return array The category breadcrumb for the given product ID.
*/
@@ -109,7 +114,8 @@ function get_primary_product_category( int $product_id, string $taxonomy ): arra
/**
* Get the product category breadcrumb elements as an array.
*
- * @param int $category_id The ID of the product category.
+ * @param int $category_id The ID of the product category.
+ * @param string $taxonomy The taxonomy slug.
*
* @return array The category path elements as an array.
*/
@@ -138,8 +144,8 @@ function get_category_breadcrumb( int $category_id, string $taxonomy ): array {
/**
* Get product term value.
*
- * @param int $product_id A product ID
- * @param string $taxonomy The taxonomy slug
+ * @param int $product_id A product ID.
+ * @param string $taxonomy The taxonomy slug.
*
* @return string Returns the first assigned taxonomy value.
*/
@@ -160,10 +166,9 @@ function get_product_term( int $product_id, string $taxonomy ): string {
/**
* Prefix an item ID
*
- * @param string $item_id
+ * @param string $item_id Tje item ID.
*
* @return string
*/
abstract public function prefix_item_id( string $item_id ): string;
-
}
diff --git a/src/Integration/AbstractIntegration.php b/src/Integration/AbstractIntegration.php
index bd61f92..9ee80a6 100644
--- a/src/Integration/AbstractIntegration.php
+++ b/src/Integration/AbstractIntegration.php
@@ -1,6 +1,8 @@
options = $options;
- $this->util = $util;
+ $this->util = $util;
}
/**
@@ -41,9 +48,8 @@ abstract static function instance();
/**
* Register frontend
*
- * @param Options $options
- * @param Util $util
+ * @param Options $options An instance of Options.
+ * @param Util $util An instance of Util.
*/
- abstract public static function register( Options $options, Util $util): void;
-
+ abstract public static function register( Options $options, Util $util ): void;
}
diff --git a/src/Integration/ContactForm7.php b/src/Integration/ContactForm7.php
index cdd6004..a6dd808 100644
--- a/src/Integration/ContactForm7.php
+++ b/src/Integration/ContactForm7.php
@@ -1,6 +1,8 @@
options->get( 'integrations', 'cf7_load_js' ) == 1 && ! wp_script_is( 'contact-form-7' ) ) {
+ if ( $this->options->get( 'integrations', 'cf7_load_js' ) === 1 && ! wp_script_is( 'contact-form-7' ) ) {
return;
}
@@ -73,6 +77,5 @@ public function enqueue_scripts(): void {
$this->util->get_plugin_version(),
true
);
-
}
}
diff --git a/src/Integration/EasyDigitalDownloads.php b/src/Integration/EasyDigitalDownloads.php
index 64d1202..f2d8b73 100644
--- a/src/Integration/EasyDigitalDownloads.php
+++ b/src/Integration/EasyDigitalDownloads.php
@@ -3,6 +3,7 @@
* Easy Digital Downloads.
*
* @see https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?hl=en&client_type=gtm
+ * @package GTM Kit
*/
namespace TLA_Media\GTM_Kit\Integration;
@@ -21,16 +22,18 @@ final class EasyDigitalDownloads extends AbstractEcommerce {
/**
* Instance.
+ *
+ * @var EasyDigitalDownloads
*/
protected static $instance = null;
/**
* Constructor.
*
- * @param Options $options
- * @param Util $util
+ * @param Options $options The Options instance.
+ * @param Util $util The Util Instance.
*/
- final public function __construct( Options $options, Util $util) {
+ public function __construct( Options $options, Util $util ) {
$this->store_currency = edd_get_currency();
// Call parent constructor.
@@ -42,10 +45,10 @@ final public function __construct( Options $options, Util $util) {
*/
public static function instance(): ?EasyDigitalDownloads {
if ( is_null( self::$instance ) ) {
- $options = new Options();
- $rest_API_server = new RestAPIServer();
- $util = new Util( $rest_API_server );
- self::$instance = new self( $options, $util );
+ $options = new Options();
+ $rest_api_server = new RestAPIServer();
+ $util = new Util( $rest_api_server );
+ self::$instance = new self( $options, $util );
}
return self::$instance;
@@ -54,10 +57,10 @@ public static function instance(): ?EasyDigitalDownloads {
/**
* Register frontend
*
- * @param Options $options
- * @param Util $util
+ * @param Options $options The Options instance.
+ * @param Util $util The Util Instance.
*/
- public static function register( Options $options, Util $util): void {
+ public static function register( Options $options, Util $util ): void {
self::$instance = new self( $options, $util );
@@ -101,7 +104,7 @@ public function enqueue_scripts(): void {
/**
* Get the global script settings
*
- * @param array $global_settings Script settings
+ * @param array $global_settings Script settings.
*
* @return array
*/
@@ -119,15 +122,15 @@ public function get_global_settings( array $global_settings ): array {
/**
* Get the global script data
*
- * @param array $global_data Script data
+ * @param array $global_data Script data.
*
* @return array
*/
public function get_global_data( array $global_data ): array {
- $global_data['edd']['currency'] = $this->store_currency;
- $global_data['edd']['is_checkout'] = ( is_page( edd_get_option( 'purchase_page' ) ) );
- $global_data['edd']['add_payment_info']['fired'] = false;
+ $global_data['edd']['currency'] = $this->store_currency;
+ $global_data['edd']['is_checkout'] = ( is_page( edd_get_option( 'purchase_page' ) ) );
+ $global_data['edd']['add_payment_info']['fired'] = false;
if ( is_page( edd_get_option( 'purchase_page' ) ) ) {
$global_data['edd']['cart_items'] = $this->get_cart_items( 'begin_checkout' );
@@ -142,7 +145,7 @@ public function get_global_data( array $global_data ): array {
/**
* Get the dataLayer content
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -166,7 +169,7 @@ public function get_datalayer_content( array $data_layer ): array {
/**
* Get the dataLayer data for product pages
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -187,7 +190,7 @@ public function get_datalayer_content_product_page( array $data_layer ): array {
$data_layer['event'] = 'view_item';
$data_layer['ecommerce'] = [
'items' => [ $item ],
- 'value' => $item['price']
+ 'value' => $item['price'],
];
return $data_layer;
@@ -196,7 +199,7 @@ public function get_datalayer_content_product_page( array $data_layer ): array {
/**
* Get the dataLayer data for category pages
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -212,7 +215,7 @@ public function get_datalayer_content_product_category( array $data_layer ): arr
/**
* Get the dataLayer data for product tag pages
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -228,7 +231,7 @@ public function get_datalayer_content_product_tag( array $data_layer ): array {
/**
* Get the dataLayer data for checkout page
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -239,7 +242,7 @@ public function get_datalayer_content_checkout( array $data_layer ): array {
$data_layer['event'] = 'begin_checkout';
$data_layer['ecommerce'] = [
- 'items' => $this->global_data['data']['edd']['cart_items']
+ 'items' => $this->global_data['data']['edd']['cart_items'],
];
return $data_layer;
@@ -248,7 +251,7 @@ public function get_datalayer_content_checkout( array $data_layer ): array {
/**
* Get the dataLayer data for order_received page
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -287,7 +290,9 @@ public function get_datalayer_content_order_received( array $data_layer ): array
$order_items = [];
- if ( $items = $order->get_items() ) {
+ $items = $order->get_items();
+
+ if ( $items ) {
foreach ( $items as $item ) {
$product = edd_get_download( $item->product_id );
@@ -303,7 +308,7 @@ public function get_datalayer_content_order_received( array $data_layer ): array
[],
[
'quantity' => $item->quantity,
- 'price' => $price
+ 'price' => $price,
],
'purchase'
);
@@ -316,7 +321,7 @@ public function get_datalayer_content_order_received( array $data_layer ): array
'value' => (float) $order_value,
'tax' => (float) $order->tax,
'currency' => $order->currency,
- 'items' => $order_items
+ 'items' => $order_items,
];
edd_add_order_meta( $order_id, 'gtmkit_order_tracked', 1 );
@@ -338,18 +343,6 @@ function get_cart_items( string $event_context ): array {
$product = edd_get_download( $cart_item['id'] );
$options = $cart_item['options'];
- $prices = edd_get_variable_prices( $cart_item['id'] );
-
- if ( isset( $options['price_id'] ) ) {
-
- }
- if ( $prices ) {
- foreach ( $prices as $price_id => $price ) {
- // $price['name'] is the name of the price
- // $price['amount'] is the amount of the price
- }
- }
-
$cart_items[] = $this->get_item_data( $product, $options, [ 'quantity' => $cart_item['quantity'] ], $event_context );
}
@@ -359,10 +352,10 @@ function get_cart_items( string $event_context ): array {
/**
* Get item data.
*
- * @param EDD_Download $download
- * @param array $options
- * @param array $additional_item_attributes Any key-value pair that needs to be added to the item data.
- * @param string $event_context The event context of the item data.
+ * @param EDD_Download $download An instance of EDD_Download.
+ * @param array $options Optional options.
+ * @param array $additional_item_attributes Any key-value pair that needs to be added to the item data.
+ * @param string $event_context The event context of the item data.
*
* @return array The item data.
*/
@@ -406,11 +399,10 @@ function get_item_data( EDD_Download $download, array $options = [], array $addi
if ( $number_of_elements ) {
- for ( $element = 0; $element < $number_of_elements; $element ++ ) {
- $designator = ( $element == 0 ) ? '' : $element + 1;
+ for ( $element = 0; $element < $number_of_elements; $element++ ) {
+ $designator = ( $element === 0 ) ? '' : $element + 1;
$item_data[ 'item_category' . $designator ] = $item_category_elements[ $element ];
}
-
}
$item_data = array_merge( $item_data, $additional_item_attributes );
@@ -424,7 +416,7 @@ function get_item_data( EDD_Download $download, array $options = [], array $addi
*
* @hook woocommerce_after_add_to_cart_button
*
- * @param int $download_id
+ * @param int $download_id The download ID.
*
* @return void
*/
@@ -434,15 +426,14 @@ function add_to_cart_tracking( int $download_id ): void {
if ( ( $product instanceof EDD_Download ) ) {
$item_data = $this->get_item_data( $product );
- echo '
' . "\n";
+ echo '
' . "\n"; // phpcs:ignore
}
-
}
/**
* Prefix an item ID
*
- * @param string $item_id
+ * @param string $item_id The item ID.
*
* @return string
*/
@@ -455,12 +446,12 @@ function prefix_item_id( string $item_id ): string {
/**
* Get price to display
*
- * @param int $download_id
- * @param $price_index
+ * @param int $download_id The download ID.
+ * @param string|null $price_index The price index.
*
* @return float
*/
- function get_price_to_display( int $download_id, $price_index = null ): float {
+ function get_price_to_display( int $download_id, string $price_index = null ): float {
if ( edd_has_variable_prices( $download_id ) ) {
@@ -476,7 +467,6 @@ function get_price_to_display( int $download_id, $price_index = null ): float {
$price = $prices[ $default_option ]['amount'];
}
-
} else {
$price = edd_get_download_price( $download_id );
@@ -484,7 +474,6 @@ function get_price_to_display( int $download_id, $price_index = null ): float {
}
return (float) $price;
-
}
/**
@@ -497,15 +486,14 @@ function get_payment_key() {
$session = edd_get_purchase_session();
- if ( isset( $_GET['payment_key'] ) ) {
- return urldecode( $_GET['payment_key'] );
- } else if ( $session && isset( $session['purchase_key'] ) ) {
+ if ( isset( $_GET['payment_key'] ) ) { // phpcs:ignore
+ return urldecode( $_GET['payment_key'] ); // phpcs:ignore
+ } elseif ( $session && isset( $session['purchase_key'] ) ) {
return $session['purchase_key'];
} elseif ( $edd_receipt_args && isset( $edd_receipt_args['payment_key'] ) && $edd_receipt_args['payment_key'] ) {
return $edd_receipt_args['payment_key'];
} else {
return false;
}
-
}
}
diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php
index 0ca7680..4e025eb 100644
--- a/src/Integration/WooCommerce.php
+++ b/src/Integration/WooCommerce.php
@@ -3,6 +3,7 @@
* WooCommerce.
*
* @see https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?hl=en&client_type=gtm
+ * @package GTM Kit
*/
namespace TLA_Media\GTM_Kit\Integration;
@@ -23,10 +24,12 @@
/**
* WooCommerce integration
*/
-final class WooCommerce extends AbstractEcommerce {
+final class WooCommerce extends AbstractEcommerce {
/**
* Instance.
+ *
+ * @var WooCommerce
*/
protected static $instance = null;
@@ -40,10 +43,10 @@ final class WooCommerce extends AbstractEcommerce {
/**
* Constructor.
*
- * @param Options $options
- * @param Util $util
+ * @param Options $options An instance of Options.
+ * @param Util $util An instance of Util.
*/
- public function __construct( Options $options, Util $util) {
+ public function __construct( Options $options, Util $util ) {
$this->store_currency = get_woocommerce_currency();
$this->extend = StoreApi::container()->get( ExtendSchema::class );
@@ -57,10 +60,10 @@ public function __construct( Options $options, Util $util) {
*/
public static function instance(): ?WooCommerce {
if ( is_null( self::$instance ) ) {
- $options = new Options();
- $rest_API_server = new RestAPIServer();
- $util = new Util( $rest_API_server );
- self::$instance = new self( $options, $util );
+ $options = new Options();
+ $rest_api_server = new RestAPIServer();
+ $util = new Util( $rest_api_server );
+ self::$instance = new self( $options, $util );
}
return self::$instance;
@@ -69,8 +72,8 @@ public static function instance(): ?WooCommerce {
/**
* Register frontend
*
- * @param Options $options
- * @param Util $util
+ * @param Options $options An instance of Options.
+ * @param Util $util An instance of Util.
*/
public static function register( Options $options, Util $util ): void {
@@ -81,63 +84,100 @@ public static function register( Options $options, Util $util ): void {
add_filter( 'gtmkit_datalayer_content', [ self::$instance, 'get_datalayer_content' ] );
add_action( 'wp_enqueue_scripts', [ self::$instance, 'enqueue_scripts' ] );
- // Add-to-cart tracking
- add_action( 'woocommerce_after_add_to_cart_button', [
- self::$instance,
- 'single_product_add_to_cart_tracking'
- ] );
- add_filter( 'woocommerce_grouped_product_list_column_label', [
- self::$instance,
- 'grouped_product_add_to_cart_tracking'
- ], 10, 2 );
- add_filter( 'woocommerce_blocks_product_grid_item_html', [
- self::$instance,
- 'product_block_add_to_cart_tracking'
- ], 20, 3 );
+ // Add-to-cart tracking.
+ add_action(
+ 'woocommerce_after_add_to_cart_button',
+ [
+ self::$instance,
+ 'single_product_add_to_cart_tracking',
+ ]
+ );
+ add_filter(
+ 'woocommerce_grouped_product_list_column_label',
+ [
+ self::$instance,
+ 'grouped_product_add_to_cart_tracking',
+ ],
+ 10,
+ 2
+ );
+ add_filter(
+ 'woocommerce_blocks_product_grid_item_html',
+ [
+ self::$instance,
+ 'product_block_add_to_cart_tracking',
+ ],
+ 20,
+ 3
+ );
add_filter( 'tinvwl_wishlist_item_meta_post', [ self::$instance, 'Compatibility_With_TI_Wishlist' ] );
add_action( 'woocommerce_after_shop_loop_item', [ self::$instance, 'product_list_loop_add_to_cart_tracking' ] );
add_filter( 'woocommerce_cart_item_remove_link', [ self::$instance, 'cart_item_remove_link' ], 10, 2 );
- // Set list name in WooCommerce loop
+ // Set list name in WooCommerce loop.
add_filter( 'woocommerce_product_loop_start', [ self::$instance, 'set_list_name_on_category_and_tag' ] );
add_filter( 'woocommerce_related_products_columns', [ self::$instance, 'set_list_name_in_woocommerce_loop_filter' ] );
add_filter( 'woocommerce_cross_sells_columns', [ self::$instance, 'set_list_name_in_woocommerce_loop_filter' ] );
add_filter( 'woocommerce_upsells_columns', [ self::$instance, 'set_list_name_in_woocommerce_loop_filter' ] );
- add_action( 'woocommerce_shortcode_before_best_selling_products_loop', [
- self::$instance,
- 'set_list_name_in_woocommerce_loop'
- ] );
- add_filter( 'safe_style_css', function( $styles ) {
- $styles[] = 'display';
- $styles[] = 'visibility';
- return $styles;
- } );
-
- add_action( 'woocommerce_shortcode_before_featured_products_loop', [
- self::$instance,
- 'set_list_name_in_woocommerce_loop'
- ] );
- add_action( 'woocommerce_shortcode_before_recent_products_loop', [
- self::$instance,
- 'set_list_name_in_woocommerce_loop'
- ] );
- add_action( 'woocommerce_shortcode_before_related_products_loop', [
- self::$instance,
- 'set_list_name_in_woocommerce_loop'
- ] );
- add_action( 'woocommerce_shortcode_before_sale_products_loop', [
- self::$instance,
- 'set_list_name_in_woocommerce_loop'
- ] );
- add_action( 'woocommerce_shortcode_before_top_rated_products_loop', [
- self::$instance,
- 'set_list_name_in_woocommerce_loop'
- ] );
- add_action( 'woocommerce_shortcode_before_product_category_loop', [
- self::$instance,
- 'set_list_name_in_woocommerce_loop'
- ] );
+ add_action(
+ 'woocommerce_shortcode_before_best_selling_products_loop',
+ [
+ self::$instance,
+ 'set_list_name_in_woocommerce_loop',
+ ]
+ );
+ add_filter(
+ 'safe_style_css',
+ function ( $styles ) {
+ $styles[] = 'display';
+ $styles[] = 'visibility';
+ return $styles;
+ }
+ );
+
+ add_action(
+ 'woocommerce_shortcode_before_featured_products_loop',
+ [
+ self::$instance,
+ 'set_list_name_in_woocommerce_loop',
+ ]
+ );
+ add_action(
+ 'woocommerce_shortcode_before_recent_products_loop',
+ [
+ self::$instance,
+ 'set_list_name_in_woocommerce_loop',
+ ]
+ );
+ add_action(
+ 'woocommerce_shortcode_before_related_products_loop',
+ [
+ self::$instance,
+ 'set_list_name_in_woocommerce_loop',
+ ]
+ );
+ add_action(
+ 'woocommerce_shortcode_before_sale_products_loop',
+ [
+ self::$instance,
+ 'set_list_name_in_woocommerce_loop',
+ ]
+ );
+ add_action(
+ 'woocommerce_shortcode_before_top_rated_products_loop',
+ [
+ self::$instance,
+ 'set_list_name_in_woocommerce_loop',
+ ]
+ );
+ add_action(
+ 'woocommerce_shortcode_before_product_category_loop',
+ [
+ self::$instance,
+ 'set_list_name_in_woocommerce_loop',
+ ]
+ );
add_action( 'woocommerce_blocks_loaded', [ self::$instance, 'extend_store' ] );
}
@@ -188,9 +228,9 @@ public function enqueue_scripts(): void {
}
/**
- * get the global script settings
+ * Get the global script settings
*
- * @param array $global_settings Script settings
+ * @param array $global_settings Script settings.
*
* @return array
*/
@@ -219,7 +259,7 @@ public function get_global_settings( array $global_settings ): array {
/**
* Get the global script data
*
- * @param array $global_data Script data
+ * @param array $global_data Script data.
*
* @return array
*/
@@ -228,19 +268,19 @@ public function get_global_data( array $global_data ): array {
$global_data['wc']['currency'] = $this->store_currency;
$global_data['wc']['is_cart'] = is_cart();
$global_data['wc']['is_checkout'] = ( is_checkout() && ! is_order_received_page() );
- $global_data['wc']['blocks'] = $this->get_woocommerce_blocks();
+ $global_data['wc']['blocks'] = $this->get_woocommerce_blocks();
if ( is_cart() ) {
$global_data['wc']['cart_items'] = $this->get_cart_items( 'view_cart' );
}
if ( is_checkout() && ! is_order_received_page() ) {
- $global_data['wc']['cart_items'] = $this->get_cart_items( 'begin_checkout' );
- $global_data['wc']['cart_value'] = (float) WC()->cart->cart_contents_total;
- $global_data['wc']['chosen_shipping_method'] = WC()->session->get('chosen_shipping_methods')[0] ?? '';
- $global_data['wc']['chosen_payment_method'] = $this->get_payment_method();
- $global_data['wc']['add_payment_info']['fired'] = false;
- $global_data['wc']['add_shipping_info']['fired'] = false;
+ $global_data['wc']['cart_items'] = $this->get_cart_items( 'begin_checkout' );
+ $global_data['wc']['cart_value'] = (float) WC()->cart->cart_contents_total;
+ $global_data['wc']['chosen_shipping_method'] = WC()->session->get( 'chosen_shipping_methods' )[0] ?? '';
+ $global_data['wc']['chosen_payment_method'] = $this->get_payment_method();
+ $global_data['wc']['add_payment_info']['fired'] = false;
+ $global_data['wc']['add_shipping_info']['fired'] = false;
}
$this->global_data = $global_data;
@@ -248,11 +288,16 @@ public function get_global_data( array $global_data ): array {
return $global_data;
}
+ /**
+ * Get the payment method
+ *
+ * @return string|null
+ */
private function get_payment_method(): ?string {
$payment_method = WC()->session->get( 'chosen_payment_method' );
- if (! $payment_method ) {
+ if ( ! $payment_method ) {
$payment_method = array_key_first( WC()->payment_gateways()->get_available_payment_gateways() );
}
@@ -262,7 +307,7 @@ private function get_payment_method(): ?string {
/**
* Get the WooCommerce dataLayer content
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -307,7 +352,7 @@ public function get_datalayer_content( array $data_layer ): array {
/**
* Get the dataLayer data for product pages
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -323,7 +368,7 @@ public function get_datalayer_content_product_page( array $data_layer ): array {
$data_layer['pageType'] = 'product-page';
}
- if ( $product->get_type() == 'variable' && Options::init()->get( 'integrations', 'woocommerce_variable_product_tracking' ) == 2 ) {
+ if ( $product->get_type() === 'variable' && Options::init()->get( 'integrations', 'woocommerce_variable_product_tracking' ) === 2 ) {
return $data_layer;
}
@@ -333,7 +378,7 @@ public function get_datalayer_content_product_page( array $data_layer ): array {
$data_layer['event'] = 'view_item';
$data_layer['ecommerce'] = [
'items' => [ $item ],
- 'value' => (float) $item['price']
+ 'value' => (float) $item['price'],
];
return $data_layer;
@@ -342,7 +387,7 @@ public function get_datalayer_content_product_page( array $data_layer ): array {
/**
* Get the dataLayer data for category pages
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -358,7 +403,7 @@ public function get_datalayer_content_product_category( array $data_layer ): arr
/**
* Get the dataLayer data for product tag pages
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -374,7 +419,7 @@ public function get_datalayer_content_product_tag( array $data_layer ): array {
/**
* Get the dataLayer data for cart page
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -394,7 +439,7 @@ public function get_datalayer_content_cart( array $data_layer ): array {
$data_layer['ecommerce'] = [
'currency' => $this->store_currency,
'value' => (float) $cart_value,
- 'items' => $this->get_cart_items( 'view_cart' )
+ 'items' => $this->get_cart_items( 'view_cart' ),
];
return $data_layer;
@@ -403,7 +448,7 @@ public function get_datalayer_content_cart( array $data_layer ): array {
/**
* Get the dataLayer data for checkout page
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -436,7 +481,7 @@ public function get_datalayer_content_checkout( array $data_layer ): array {
/**
* Get the dataLayer data for order_received page
*
- * @param array $data_layer The datalayer content
+ * @param array $data_layer The datalayer content.
*
* @return array The datalayer content
*/
@@ -454,7 +499,7 @@ public function get_datalayer_content_order_received( array $data_layer ): array
if ( $order instanceof WC_Order ) {
- $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( wp_unslash( $_GET['key'] ) ) );
+ $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( wp_unslash( $_GET['key'] ) ) ); // phpcs:ignore
if ( $order->get_order_key() !== $order_key ) {
return $data_layer;
@@ -488,10 +533,12 @@ public function get_datalayer_content_order_received( array $data_layer ): array
$order_value -= $shipping_total;
}
- $coupons = $order->get_coupon_codes();
+ $coupons = $order->get_coupon_codes();
$order_items = [];
- if ( $items = $order->get_items() ) {
+ $items = $order->get_items();
+
+ if ( $items ) {
foreach ( $items as $item ) {
$product = $item->get_product();
@@ -500,7 +547,7 @@ public function get_datalayer_content_order_received( array $data_layer ): array
$additional_item_attributes = [
'quantity' => $item->get_quantity(),
- 'price' => $product_price
+ 'price' => $product_price,
];
$coupon_discount = $this->get_coupon_discount( $coupons, $item->get_data() );
@@ -537,7 +584,7 @@ public function get_datalayer_content_order_received( array $data_layer ): array
if ( $this->options->get( 'integrations', 'woocommerce_include_customer_data' ) ) {
- if (is_user_logged_in()) {
+ if ( is_user_logged_in() ) {
try {
$wc_customer = new WC_Customer( WC()->customer->get_id() );
$order_count = $wc_customer->get_order_count();
@@ -570,7 +617,7 @@ public function get_datalayer_content_order_received( array $data_layer ): array
$data_layer['ecommerce']['customer']['billing_postcode'] = $wc_customer->get_billing_postcode();
$data_layer['ecommerce']['customer']['billing_country'] = $wc_customer->get_billing_country();
$data_layer['ecommerce']['customer']['billing_email'] = $wc_customer->get_billing_email();
- $data_layer['ecommerce']['customer']['billing_email_hash'] = ($wc_customer->get_billing_email() ) ? hash( 'sha256', $wc_customer->get_billing_email() ) : '' ;
+ $data_layer['ecommerce']['customer']['billing_email_hash'] = ( $wc_customer->get_billing_email() ) ? hash( 'sha256', $wc_customer->get_billing_email() ) : '';
$data_layer['ecommerce']['customer']['billing_phone'] = $wc_customer->get_billing_phone();
$data_layer['ecommerce']['customer']['shipping_firstName'] = $wc_customer->get_shipping_first_name();
@@ -598,16 +645,16 @@ public function get_datalayer_content_order_received( array $data_layer ): array
*/
function get_cart_items( string $event_context ): array {
$cart_items = [];
- $coupons = WC()->cart->get_applied_coupons();
+ $coupons = WC()->cart->get_applied_coupons();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
- $item_data = [
- 'product_id' => $cart_item['product_id'],
- 'quantity' => $cart_item['quantity'],
- 'total' => $cart_item['line_total'],
- 'total_tax' => $cart_item['line_tax'],
- 'subtotal' => $cart_item['line_subtotal'],
+ $item_data = [
+ 'product_id' => $cart_item['product_id'],
+ 'quantity' => $cart_item['quantity'],
+ 'total' => $cart_item['line_total'],
+ 'total_tax' => $cart_item['line_tax'],
+ 'subtotal' => $cart_item['line_subtotal'],
'subtotal_tax' => $cart_item['line_subtotal_tax'],
];
$coupon_discount = $this->get_coupon_discount( $coupons, $item_data );
@@ -633,9 +680,9 @@ function get_cart_items( string $event_context ): array {
/**
* Get item data.
*
- * @param WC_Product $product An instance of WP_Product
- * @param array $additional_item_attributes Any key-value pair that needs to be added to the item data.
- * @param string $event_context The event context of the item data.
+ * @param WC_Product $product An instance of WP_Product.
+ * @param array $additional_item_attributes Any key-value pair that needs to be added to the item data.
+ * @param string $event_context The event context of the item data.
*
* @return array The item data.
*/
@@ -650,7 +697,7 @@ function get_item_data( WC_Product $product, array $additional_item_attributes =
}
$item_data = [
- 'id' => $this->prefix_item_id( $item_id ),
+ 'id' => $this->prefix_item_id( $item_id ),
'item_id' => $this->prefix_item_id( $item_id ),
'item_name' => $product->get_title(),
'currency' => $this->store_currency,
@@ -674,14 +721,13 @@ function get_item_data( WC_Product $product, array $additional_item_attributes =
if ( $number_of_elements ) {
- for ( $element = 0; $element < $number_of_elements; $element ++ ) {
- $designator = ( $element == 0 ) ? '' : $element + 1;
+ for ( $element = 0; $element < $number_of_elements; $element++ ) {
+ $designator = ( $element === 0 ) ? '' : $element + 1;
$item_data[ 'item_category' . $designator ] = $item_category_elements[ $element ];
}
-
}
- if ( $product->get_type() == 'variation' ) {
+ if ( $product->get_type() === 'variation' ) {
$item_data['item_variant'] = implode( ',', array_filter( $product->get_attributes() ) );
}
@@ -693,8 +739,8 @@ function get_item_data( WC_Product $product, array $additional_item_attributes =
/**
* Get the coupons and discount for an item
*
- * @param array $coupons
- * @param array $item
+ * @param array $coupons The coupons.
+ * @param array $item The item.
*
* @return array
*/
@@ -712,22 +758,29 @@ function get_coupon_discount( array $coupons, array $item ): array {
$included_products = true;
$included_cats = true;
- if ( count( $product_ids = $coupon->get_product_ids() ) > 0 ) {
+ $product_ids = $coupon->get_product_ids();
+ if ( count( $product_ids ) > 0 ) {
if ( ! in_array( $item['product_id'], $product_ids ) ) {
$included_products = false;
}
}
- if ( count( $excluded_product_ids = $coupon->get_excluded_product_ids() ) > 0 ) {
+
+ $excluded_product_ids = $coupon->get_excluded_product_ids();
+ if ( count( $excluded_product_ids ) > 0 ) {
if ( in_array( $item['product_id'], $excluded_product_ids ) ) {
$included_products = false;
}
}
- if ( count( $product_cats = $coupon->get_product_categories() ) > 0 ) {
+
+ $product_cats = $coupon->get_product_categories();
+ if ( count( $product_cats ) > 0 ) {
if ( ! has_term( $product_cats, 'product_cat', $item['product_id'] ) ) {
$included_cats = false;
}
}
- if ( count( $excluded_product_cats = $coupon->get_excluded_product_categories() ) > 0 ) {
+
+ $excluded_product_cats = $coupon->get_excluded_product_categories();
+ if ( count( $excluded_product_cats ) > 0 ) {
if ( has_term( $excluded_product_cats, 'product_cat', $item['product_id'] ) ) {
$included_cats = false;
}
@@ -746,7 +799,10 @@ function get_coupon_discount( array $coupons, array $item ): array {
}
}
- return [ 'coupon_codes' => $coupon_codes, 'discount' => $discount ];
+ return [
+ 'coupon_codes' => $coupon_codes,
+ 'discount' => $discount,
+ ];
}
/**
@@ -761,7 +817,7 @@ function single_product_add_to_cart_tracking(): void {
$item_data = $this->get_item_data( $product );
- echo '
' . "\n";
+ echo '
' . "\n"; // phpcs:ignore
}
/**
@@ -769,7 +825,7 @@ function single_product_add_to_cart_tracking(): void {
*
* @hook woocommerce_grouped_product_list_column_label
*
- * @param string $label_value Product label.
+ * @param string $label_value Product label.
* @param WC_Product $product The product.
*
* @return string The product label string.
@@ -786,8 +842,8 @@ function grouped_product_add_to_cart_tracking( string $label_value, WC_Product $
*
* @hook woocommerce_blocks_product_grid_item_html.
*
- * @param string $html Product grid item HTML.
- * @param object $data Product data passed to the template.
+ * @param string $html Product grid item HTML.
+ * @param object $data Product data passed to the template.
* @param WC_Product $product Product object.
*
* @return string Updated product grid item HTML.
@@ -802,8 +858,8 @@ function product_block_add_to_cart_tracking( string $html, object $data, WC_Prod
* Generates a hidden
element that contains the item data.
*
* @param WC_Product $product Product object.
- * @param string $item_list_name Name of the list associated with the event.
- * @param int $index The index of the product in the product list. The first product should have the index no. 1.
+ * @param string $item_list_name Name of the list associated with the event.
+ * @param int $index The index of the product in the product list. The first product should have the index no. 1.
*
* @return string A hidden element that contains the item data.
*/
@@ -893,7 +949,7 @@ public function set_list_name_in_woocommerce_loop(): void {
*
* @hook woocommerce_after_shop_loop_item.
*
- * @param mixed $columns
+ * @param mixed $columns The columns.
*
* @return mixed
*/
@@ -905,6 +961,13 @@ public function set_list_name_in_woocommerce_loop_filter( $columns ) {
return $columns;
}
+ /**
+ * Set the list name on categories and tags
+ *
+ * @param mixed $value The product loop start.
+ *
+ * @return mixed
+ */
public function set_list_name_on_category_and_tag( $value ) {
global $woocommerce_loop;
@@ -925,7 +988,7 @@ public function set_list_name_on_category_and_tag( $value ) {
* @hook woocommerce_cart_item_remove_link.
*
* @param string $woocommerce_cart_item_remove_link The cart item remove link.
- * @param string $cart_item_key The cart item key
+ * @param string $cart_item_key The cart item key.
*
* @return string The updated cart item remove link containing product data.
*/
@@ -940,22 +1003,22 @@ function cart_item_remove_link( string $woocommerce_cart_item_remove_link, strin
$item_data = $this->get_item_data(
$cart_item['data'],
[
- 'quantity' => $cart_item['quantity']
+ 'quantity' => $cart_item['quantity'],
],
'remove_from_cart'
);
- $findHref = ' href="';
+ $find_href = ' href="';
$replace_width_product_data = sprintf( ' data-gtmkit_product_data="%s" href="', esc_attr( json_encode( $item_data ) ) );
- $substring_pos = strpos( $woocommerce_cart_item_remove_link, $findHref );
+ $substring_pos = strpos( $woocommerce_cart_item_remove_link, $find_href );
if ( $substring_pos !== false ) {
$woocommerce_cart_item_remove_link = substr_replace(
$woocommerce_cart_item_remove_link,
$replace_width_product_data,
$substring_pos,
- strlen( $findHref )
+ strlen( $find_href )
);
}
@@ -965,19 +1028,19 @@ function cart_item_remove_link( string $woocommerce_cart_item_remove_link, strin
/**
* Prefix an item ID
*
- * @param string $item_id
+ * @param string $item_id The item ID.
*
* @return string
*/
function prefix_item_id( string $item_id ): string {
$prefix = ( Options::init()->get( 'integrations', 'woocommerce_product_id_prefix' ) ) ?: '';
- return $prefix.$item_id;
+ return $prefix . $item_id;
}
/**
* Compatibility with TI WooCommerce Wishlist
*
- * @param array $item_data
+ * @param array $item_data Item data.
*
* @return array
*/
@@ -997,24 +1060,24 @@ function Compatibility_With_TI_Wishlist( array $item_data ): array {
*/
public function extend_store() {
- // Register into `cart/items`
+ // Register into `cart/items`.
$this->extend->register_endpoint_data(
array(
'endpoint' => ProductSchema::IDENTIFIER,
'namespace' => 'gtmkit',
'data_callback' => array( self::$instance, 'extend_product_data' ),
'schema_callback' => array( self::$instance, 'extend_product_schema' ),
- 'schema_type' => ARRAY_A,
+ 'schema_type' => ARRAY_A,
)
);
$this->extend->register_endpoint_data(
array(
- 'endpoint' => CartItemSchema::IDENTIFIER,
+ 'endpoint' => CartItemSchema::IDENTIFIER,
'namespace' => 'gtmkit',
'data_callback' => array( self::$instance, 'extend_cart_data' ),
'schema_callback' => array( self::$instance, 'extend_product_schema' ),
- 'schema_type' => ARRAY_A,
+ 'schema_type' => ARRAY_A,
)
);
}
@@ -1028,7 +1091,7 @@ public function extend_store() {
*/
public function extend_product_data( $product ): array {
return array(
- 'item' => json_encode( $this->get_item_data( $product ) ),
+ 'item' => json_encode( $this->get_item_data( $product ) ),
);
}
@@ -1041,7 +1104,7 @@ public function extend_product_data( $product ): array {
*/
public function extend_cart_data( array $cart_item ): array {
return array(
- 'item' => json_encode( $this->get_item_data( $cart_item['data'] ) ),
+ 'item' => json_encode( $this->get_item_data( $cart_item['data'] ) ),
);
}
@@ -1053,35 +1116,34 @@ public function extend_cart_data( array $cart_item ): array {
*/
public function extend_product_schema(): array {
- return array(
- 'gtmkit_data' => array(
+ return array(
+ 'gtmkit_data' => array(
'description' => __( 'GTM Kit data.', 'gtm-kit' ),
'type' => array( 'string', 'null' ),
'readonly' => true,
),
);
-
}
/**
* Has WooCommerce blocks
*
- * @param int $post_id
+ * @param int $post_id Tne post ID.
*
* @return array
*/
function has_woocommerce_blocks( int $post_id ): array {
- $post_content = get_the_content(null, false, $post_id);
+ $post_content = get_the_content( null, false, $post_id );
$woocommerce_blocks = array();
- // This will return an array of blocks
- $blocks = parse_blocks($post_content);
+ // This will return an array of blocks.
+ $blocks = parse_blocks( $post_content );
- // Then you can loop over the array and check if any of the blocks are WooCommerce blocks
- foreach($blocks as $block) {
- if(!empty($block['blockName']) && strpos($block['blockName'], 'woocommerce/') !== false) {
- $woocommerce_blocks[] = str_replace('woocommerce/', '', $block['blockName']);
+ // Then you can loop over the array and check if any of the blocks are WooCommerce blocks.
+ foreach ( $blocks as $block ) {
+ if ( ! empty( $block['blockName'] ) && strpos( $block['blockName'], 'woocommerce/' ) !== false ) {
+ $woocommerce_blocks[] = str_replace( 'woocommerce/', '', $block['blockName'] );
}
}
@@ -1096,5 +1158,4 @@ function has_woocommerce_blocks( int $post_id ): array {
function get_woocommerce_blocks(): array {
return $this->has_woocommerce_blocks( get_the_ID() );
}
-
}
diff --git a/src/Options.php b/src/Options.php
index e10a78b..166b158 100644
--- a/src/Options.php
+++ b/src/Options.php
@@ -1,8 +1,15 @@
get('general', 'gtm_id');
- *
*/
public static function init(): Options {
@@ -88,13 +100,13 @@ public static function init(): Options {
public static function get_defaults(): array {
return [
- 'general' => [
- 'gtm_id' => '',
+ 'general' => [
+ 'gtm_id' => '',
'script_implementation' => '0',
'noscript_implementation' => '0',
'container_active' => 'on',
],
- 'integrations' => []
+ 'integrations' => [],
];
}
@@ -103,11 +115,10 @@ public static function get_defaults(): array {
*
* @param string $group The option group.
* @param string $key The option key.
- * @param bool $strip_slashes If the slashes should be stripped from string values.
+ * @param bool $strip_slashes If the slashes should be stripped from string values.
*
* @return mixed|null Null if value doesn't exist anywhere: in constants, in DB, in a map. So it's completely custom or a typo.
* @example Options::init()->get( 'general', 'gtm_id' ).
- *
*/
public function get( string $group, string $key, bool $strip_slashes = true ) {
@@ -119,13 +130,12 @@ public function get( string $group, string $key, bool $strip_slashes = true ) {
// Ordinary database or default values.
if ( isset( $this->options[ $group ] ) ) {
$value = $this->options[ $group ][ $key ] ?? $this->postprocess_key_defaults( $key );
- } else {
- if (
+ } elseif (
isset( self::$map[ $group ] ) &&
in_array( $key, self::$map[ $group ], true )
) {
+
$value = $this->postprocess_key_defaults( $key );
- }
}
}
@@ -140,7 +150,7 @@ public function get( string $group, string $key, bool $strip_slashes = true ) {
/**
* Postprocess options.
*
- * @param string $key
+ * @param string $key The key.
*
* @return int|string
*/
@@ -166,9 +176,9 @@ protected function postprocess_key_defaults( string $key ) {
/**
* Get constant value if defined.
*
- * @param string $group
- * @param string $key
- * @param mixed $value
+ * @param string $group The option group.
+ * @param string $key The option key.
+ * @param mixed $value The option value.
*
* @return mixed
*/
@@ -184,15 +194,15 @@ protected function get_const_value( string $group, string $key, $value ) {
case 'general':
switch ( $key ) {
case 'gtm_id':
- /** @noinspection PhpUndefinedConstantInspection */
+ /** @noinspection PhpUndefinedConstantInspection */ // phpcs:ignore
$return = $this->is_const_defined( $group, $key ) ? GTMKIT_CONTAINER_ID : $value;
break;
case 'container_active':
- /** @noinspection PhpUndefinedConstantInspection */
+ /** @noinspection PhpUndefinedConstantInspection */ // phpcs:ignore
$return = $this->is_const_defined( $group, $key ) ? GTMKIT_CONTAINER_ACTIVE : $value;
break;
case 'console_log':
- /** @noinspection PhpUndefinedConstantInspection */
+ /** @noinspection PhpUndefinedConstantInspection */ // phpcs:ignore
$return = $this->is_const_defined( $group, $key ) ? GTMKIT_CONSOLE_LOG : $value;
break;
}
@@ -212,14 +222,13 @@ protected function get_const_value( string $group, string $key, $value ) {
public function is_const_enabled(): bool {
return defined( 'GTMKIT_ON' ) && GTMKIT_ON === true;
-
}
/**
* Is constant defined.
*
- * @param string $group
- * @param string $key
+ * @param string $group The option group.
+ * @param string $key The option key.
*
* @return bool
*/
@@ -235,22 +244,22 @@ public function is_const_defined( string $group, string $key ): bool {
case 'general':
switch ( $key ) {
case 'gtm_id':
- /** @noinspection PhpUndefinedConstantInspection */
+ /** @noinspection PhpUndefinedConstantInspection */ // phpcs:ignore
$return = defined( 'GTMKIT_CONTAINER_ID' ) && GTMKIT_CONTAINER_ID;
break;
case 'container_active':
- /** @noinspection PhpUndefinedConstantInspection */
+ /** @noinspection PhpUndefinedConstantInspection */ // phpcs:ignore
$return = defined( 'GTMKIT_CONTAINER_ACTIVE' ) && ( GTMKIT_CONTAINER_ACTIVE === false || GTMKIT_CONTAINER_ACTIVE === true );
break;
case 'console_log':
- /** @noinspection PhpUndefinedConstantInspection */
+ /** @noinspection PhpUndefinedConstantInspection */ // phpcs:ignore
$return = defined( 'GTMKIT_CONSOLE_LOG' ) && ( GTMKIT_CONTAINER_ACTIVE === false || GTMKIT_CONSOLE_LOG === true );
break;
}
break;
case 'integration':
- if ( $key == 'woocommerce_debug_track_purchase' ) {
+ if ( $key === 'woocommerce_debug_track_purchase' ) {
$return = defined( 'GTMKIT_WC_DEBUG_TRACK_PURCHASE' ) && GTMKIT_WC_DEBUG_TRACK_PURCHASE === true;
}
@@ -264,8 +273,8 @@ public function is_const_defined( string $group, string $key ): bool {
* Set plugin options.
*
* @param array $options Plugin options.
- * @param bool $once Update existing options or only add once.
- * @param bool $overwrite_existing Overwrite existing settings or merge.
+ * @param bool $once Update existing options or only add once.
+ * @param bool $overwrite_existing Overwrite existing settings or merge.
*/
public function set( array $options, bool $once = false, bool $overwrite_existing = true ): void {
@@ -278,17 +287,14 @@ public function set( array $options, bool $once = false, bool $overwrite_existin
// Whether to update existing options or to add these options only once if they don't exist yet.
if ( $once ) {
add_option( self::OPTION_NAME, $options, '', 'no' ); // Do not autoload these options.
- } else {
- if ( is_multisite() ) {
+ } elseif ( is_multisite() ) {
update_blog_option( get_main_site_id(), self::OPTION_NAME, $options );
- } else {
- update_option( self::OPTION_NAME, $options, 'no' );
- }
+ } else {
+ update_option( self::OPTION_NAME, $options, 'no' );
}
// Now we need to re-cache values.
$this->options = get_option( self::OPTION_NAME, [] );
-
}
/**
@@ -304,20 +310,22 @@ private function process_genericoptions( array $options ): array {
foreach ( $keys as $option_name => $option_value ) {
switch ( $group ) {
case 'general':
- if ( $option_name == 'gtm_id' ) {
+ if ( $option_name === 'gtm_id' ) {
$options[ $group ][ $option_name ] = sanitize_text_field( $option_value );
}
break;
case 'debug_events':
- if ( $option_name == 'email_debug' ) {
+ if ( $option_name === 'email_debug' ) {
$options[ $group ][ $option_name ] = (bool) $option_value;
}
}
}
}
- if (!isset($options['integrations'])) $options['integrations'] = [];
+ if ( ! isset( $options['integrations'] ) ) {
+ $options['integrations'] = [];
+ }
return $options;
}
@@ -385,5 +393,4 @@ public function get_all_raw(): array {
return $options;
}
-
}
diff --git a/src/js/frontend/woocommerce-blocks.js b/src/js/frontend/woocommerce-blocks.js
index ba0fb03..476f2d2 100644
--- a/src/js/frontend/woocommerce-blocks.js
+++ b/src/js/frontend/woocommerce-blocks.js
@@ -1 +1,7 @@
+/**
+ * GTM Kit plugin file.
+ *
+ * @package GTM Kit
+ */
+
import './woocommerce-blocks/index';
diff --git a/src/js/frontend/woocommerce-blocks/constants.js b/src/js/frontend/woocommerce-blocks/constants.js
index 237f540..a617dc3 100644
--- a/src/js/frontend/woocommerce-blocks/constants.js
+++ b/src/js/frontend/woocommerce-blocks/constants.js
@@ -1,3 +1,9 @@
+/**
+ * GTM Kit plugin file.
+ *
+ * @package GTM Kit
+ */
+
export const namespace = 'gtmkit-woocommerce-google-analytics';
export const actionPrefix = 'experimental__woocommerce_blocks';
diff --git a/src/views/admin-sidebar.php b/src/views/admin-sidebar.php
index 17b8b18..b2f15cb 100644
--- a/src/views/admin-sidebar.php
+++ b/src/views/admin-sidebar.php
@@ -1,6 +1,8 @@
'WooCommerce',
- 'description' => __( 'The #1 open source eCommerce platform built for WordPress', 'gtm-kit' ),
- 'plugin_active' => is_plugin_active( 'woocommerce/woocommerce.php' ),
+ 'name' => 'WooCommerce',
+ 'description' => __( 'The #1 open source eCommerce platform built for WordPress', 'gtm-kit' ),
+ 'plugin_active' => is_plugin_active( 'woocommerce/woocommerce.php' ),
'integration_active' => Options::init()->get( 'integrations', 'woocommerce_integration' ),
- 'tab_id' => 'woocommerce',
- 'plugin_search' => 'woocommerce',
+ 'tab_id' => 'woocommerce',
+ 'plugin_search' => 'woocommerce',
],
[
- 'name' => 'Contact Form 7',
- 'description' => __( 'Just another contact form plugin for WordPress. Simple but flexible', 'gtm-kit' ),
- 'plugin_active' => is_plugin_active( 'contact-form-7/wp-contact-form-7.php' ),
+ 'name' => 'Contact Form 7',
+ 'description' => __( 'Just another contact form plugin for WordPress. Simple but flexible', 'gtm-kit' ),
+ 'plugin_active' => is_plugin_active( 'contact-form-7/wp-contact-form-7.php' ),
'integration_active' => Options::init()->get( 'integrations', 'cf7_integration' ),
- 'tab_id' => 'cf7',
- 'plugin_search' => 'Contact Form 7',
+ 'tab_id' => 'cf7',
+ 'plugin_search' => 'Contact Form 7',
],
[
- 'name' => 'Easy Digital Downloads',
- 'description' => __( 'Easy way to sell Digital Products With WordPress', 'gtm-kit' ),
- 'plugin_active' => ( is_plugin_active( 'easy-digital-downloads/easy-digital-downloads.php' ) || is_plugin_active( 'easy-digital-downloads-pro/easy-digital-downloads.php' )),
+ 'name' => 'Easy Digital Downloads',
+ 'description' => __( 'Easy way to sell Digital Products With WordPress', 'gtm-kit' ),
+ 'plugin_active' => ( is_plugin_active( 'easy-digital-downloads/easy-digital-downloads.php' ) || is_plugin_active( 'easy-digital-downloads-pro/easy-digital-downloads.php' ) ),
'integration_active' => Options::init()->get( 'integrations', 'edd_integration' ),
- 'tab_id' => 'edd',
- 'plugin_search' => 'Easy Digital Downloads',
+ 'tab_id' => 'edd',
+ 'plugin_search' => 'Easy Digital Downloads',
],
];
?>