Skip to content

Commit

Permalink
CloudFlare Analytics: move feature to mu-wpcom (#37061)
Browse files Browse the repository at this point in the history
* CloudFlare Analytics: move feature to mu-wpcom

The feature is only used on WordPress.com sites, it is not needed in the Jetpack plugin.

Since the feature was loaded on all connected Jetpack sites, it also had the inconvenient of making an option call on every frontend call.

* Update Phan config

* Stop loading the Jetpack module

* Update projects/packages/jetpack-mu-wpcom/src/features/cloudflare-analytics/cloudflare-analytics.php

Co-authored-by: Foteini Giannaropoulou <[email protected]>

* Fix PHPCS error

---------

Co-authored-by: Foteini Giannaropoulou <[email protected]>
  • Loading branch information
jeherve and fgiannar authored Apr 25, 2024
1 parent 4411109 commit a747fd1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 23 deletions.
1 change: 1 addition & 0 deletions projects/packages/jetpack-mu-wpcom/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'src/features/admin-color-schemes/admin-color-schemes.php' => ['PhanUndeclaredConstant'],
'src/features/block-patterns/block-patterns.php' => ['PhanTypeMismatchArgumentProbablyReal', 'PhanUndeclaredTypeParameter'],
'src/features/block-patterns/class-wpcom-block-patterns-utils.php' => ['PhanTypeMismatchReturnNullable'],
'src/features/cloudflare-analytics/cloudflare-analytics.php' => ['PhanUndeclaredClassMethod'],
'src/features/coming-soon/coming-soon.php' => ['PhanTypeArraySuspicious', 'PhanTypeMismatchArgumentInternal', 'PhanUndeclaredFunction', 'PhanUndeclaredFunctionInCallable'],
'src/features/coming-soon/fallback-coming-soon-page.php' => ['PhanTypeMismatchArgument', 'PhanTypeVoidArgument'],
'src/features/error-reporting/error-reporting.php' => ['PhanPluginDuplicateConditionalNullCoalescing'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

CloudFlare Analytics: add tracking code management (originally in the Jetpack plugin).
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static function load_features() {
require_once __DIR__ . '/features/admin-color-schemes/admin-color-schemes.php';
require_once __DIR__ . '/features/block-patterns/block-patterns.php';
require_once __DIR__ . '/features/blog-privacy/blog-privacy.php';
require_once __DIR__ . '/features/cloudflare-analytics/cloudflare-analytics.php';
require_once __DIR__ . '/features/error-reporting/error-reporting.php';
require_once __DIR__ . '/features/first-posts-stream/first-posts-stream-helpers.php';
require_once __DIR__ . '/features/import-customizations/import-customizations.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Cloudflare Analytics
* Let WPCOM users automatically insert a Cloudflare analytics JS snippet into their site footer.
*
* @since $$next-version$$ -- Ported from Jetpack.
*
* @package automattic/jetpack-mu-wpcom
*/

namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Cloudflare_Analytics;

/**
* Add Cloudflare Analytics tracking code to the head.
*
* @since $$next-version$$ -- Ported from Jetpack.
*/
function insert_tracking_id() {
$option = get_option( 'jetpack_cloudflare_analytics' );

if (
! empty( $option['code'] )
&& ! is_admin()
&& (
! class_exists( 'Jetpack_AMP_Support' )
|| ( class_exists( 'Jetpack_AMP_Support' ) && ! \Jetpack_AMP_Support::is_amp_request() )
)
) {
// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
printf(
"<!-- Jetpack Cloudflare Web Analytics -->
<script defer
src='https://static.cloudflareinsights.com/beacon.min.js'
data-cf-beacon='{\"token\": \"%s\"}'>
</script>
<!-- End Jetpack Cloudflare Web Analytics -->\r\n",
esc_html( $option['code'] )
);
// phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedScript
}
}
add_action( 'wp_footer', __NAMESPACE__ . '\insert_tracking_id', 999 );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

CloudFlare Analytics: deprecate feature and move to mu-wpcom.
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,24 @@
* Cloudflare Analytics
* Let WPCOM users automatically insert a Cloudflare analytics JS snippet into their site header.
*
* @deprecated $$next-version$$ Moved to mu-wpcom.
*
* @package automattic/jetpack
*/

namespace Automattic\Jetpack\Cloudflare_Analytics;

use Automattic\Jetpack\Status\Host;
_deprecated_file( __FILE__, 'jetpack-$$next-version$$' );

/**
* Add Cloudflare Analytics tracking code to the head.
* This is currently only available to Atomic and WordPress.com Simple sites.
*
* @deprecated $$next-version$$ Moved to mu-wpcom.
* @since 9.5.0
*
* @return void
*/
function insert_tracking_id() {
$option = get_option( 'jetpack_cloudflare_analytics' );

if (
! empty( $option['code'] )
&& ! is_admin()
&& ( class_exists( 'Jetpack_AMP_Support' ) && ! \Jetpack_AMP_Support::is_amp_request() )
&& ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || ( new Host() )->is_woa_site() )
) {
// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
printf(
"<!-- Jetpack Cloudflare Web Analytics -->
<script defer
src='https://static.cloudflareinsights.com/beacon.min.js'
data-cf-beacon='{\"token\": \"%s\"}'>
</script>
<!-- End Jetpack Cloudflare Web Analytics -->\r\n",
esc_html( $option['code'] )
);
// phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedScript
}
_deprecated_function( __FUNCTION__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Jetpack_Mu_Wpcom\\Cloudflare_Analytics\\insert_tracking_id' );
}
add_action( 'wp_footer', __NAMESPACE__ . '\insert_tracking_id', 999 );
1 change: 0 additions & 1 deletion projects/plugins/jetpack/modules/module-extras.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
// Some features are only available when connected to WordPress.com.
$connected_tools = array(
'calypsoify/class-jetpack-calypsoify.php',
'cloudflare-analytics/cloudflare-analytics.php',
'plugin-search.php',
'scan/scan.php', // Shows Jetpack Scan alerts in the admin bar if threats found.
'simple-payments/simple-payments.php',
Expand Down

0 comments on commit a747fd1

Please sign in to comment.