Skip to content

Commit

Permalink
Add new filters for the Blaze module (#36950)
Browse files Browse the repository at this point in the history
* Adds new filters for the Blaze module

* Fixed hook comment to use next-version variable

* FIx changelog
  • Loading branch information
sbarbosa authored Apr 19, 2024
1 parent 9df9b80 commit 06f4893
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 36 deletions.
4 changes: 4 additions & 0 deletions projects/packages/blaze/changelog/add-blaze-new-filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Adds new filters for the Blaze module
2 changes: 1 addition & 1 deletion projects/packages/blaze/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"link-template": "https://github.com/automattic/jetpack-blaze/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "0.20.x-dev"
"dev-trunk": "0.21.x-dev"
},
"textdomain": "jetpack-blaze",
"version-constants": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/blaze/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-blaze",
"version": "0.20.3",
"version": "0.21.0-alpha",
"description": "Attract high-quality traffic to your site using Blaze. Using this service, you can advertise a post or page on some of the millions of pages across WordPress.com and Tumblr from just $5 per day.",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/blaze/#readme",
"bugs": {
Expand Down
25 changes: 4 additions & 21 deletions projects/packages/blaze/src/class-blaze.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,13 @@ public static function is_dashboard_enabled() {
return apply_filters( 'jetpack_blaze_dashboard_enable', $is_dashboard_enabled );
}

/**
* Is the Woo Blaze plugin active?
* The dashboard provided by that plugin takes precedence over Jetpack Blaze
*
* @return bool
*/
public static function is_woo_blaze_active() {
return is_plugin_active( 'woocommerce/woocommerce.php' ) && is_plugin_active( 'woo-blaze/woo-blaze.php' );
}

/**
* Enable the Blaze menu.
*
* @return void
*/
public static function enable_blaze_menu() {
if ( ! self::should_initialize() || self::is_woo_blaze_active() ) {
if ( ! self::should_initialize() ) {
return;
}

Expand Down Expand Up @@ -256,19 +246,12 @@ public static function should_initialize() {
* @return array An array with the link, and whether this is a Calypso or a wp-admin link.
*/
public static function get_campaign_management_url( $post_id ) {
$is_woo_blaze_active = self::is_woo_blaze_active();

if ( self::is_dashboard_enabled() || $is_woo_blaze_active ) {
// Woo Blaze uses a different admin section and path prefix
$admin_section = $is_woo_blaze_active ? 'admin.php' : 'tools.php';
$blaze_path_prefix = $is_woo_blaze_active ? 'wc-blaze' : 'advertising';

$admin_url = admin_url( sprintf( '%1$s?page=%2$s', $admin_section, $blaze_path_prefix ) );
if ( self::is_dashboard_enabled() ) {
$admin_url = admin_url( 'tools.php?page=advertising' );
$hostname = wp_parse_url( get_site_url(), PHP_URL_HOST );
$blaze_url = sprintf(
'%1$s#!/%2$s/posts/promote/post-%3$s/%4$s',
'%1$s#!/advertising/posts/promote/post-%2$s/%3$s',
$admin_url,
$blaze_path_prefix,
esc_attr( $post_id ),
$hostname
);
Expand Down
41 changes: 39 additions & 2 deletions projects/packages/blaze/src/class-dashboard-config-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@
* Class Dashboard_Config_Data
*/
class Dashboard_Config_Data {

/**
* Blaze dashboard admin page. Default is tools.php.
*
* @var string
*/
private $admin_page;

/**
* Blaze dashboard menu slug. Default is 'advertising'.
*
* @var string
*/
private $menu_slug;

/**
* Dashboard config constructor.
*
* @param string $admin_page Dashboard admin page. Default is tools.php.
* @param string $menu_slug Dashboard menu slug. Default is 'advertising'.
*/
public function __construct( $admin_page = 'tools.php', $menu_slug = 'advertising' ) {
$this->admin_page = $admin_page;
$this->menu_slug = $menu_slug;
}

/**
* Set configData to window.configData.
*
Expand All @@ -38,7 +64,7 @@ public function get_data() {

$user = $this->get_connected_user_identity();

return array(
$data = array(
'admin_page_base' => $this->get_admin_path(),
'api_root' => esc_url_raw( rest_url() ),
'blog_id' => $blog_id,
Expand Down Expand Up @@ -87,6 +113,15 @@ public function get_data() {
),
),
);

/**
* Filter to allow modification of the Blaze dashboard config data.
*
* @param bool $data Blaze dashboard config data.
*
* @since $$next-version$$
*/
return apply_filters( 'jetpack_blaze_dashboard_config_data', $data );
}

/**
Expand Down Expand Up @@ -129,7 +164,9 @@ protected function get_gmt_offset() {
protected function get_admin_path() {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! isset( $_SERVER['PHP_SELF'] ) || ! isset( $_SERVER['QUERY_STRING'] ) ) {
$parsed = wp_parse_url( admin_url( 'tools.php?page=advertising' ) );
$admin_path = $this->admin_page . '?page=' . $this->menu_slug;
$parsed = wp_parse_url( admin_url( $admin_path ) );

return $parsed['path'] . '?' . $parsed['query'];
}
// We do this because page.js requires the exactly page base to be set otherwise it will not work properly.
Expand Down
48 changes: 41 additions & 7 deletions projects/packages/blaze/src/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Dashboard {
*
* @var string
*/
const PACKAGE_VERSION = '0.20.3';
const PACKAGE_VERSION = '0.21.0-alpha';

/**
* List of dependencies needed to render the dashboard in wp-admin.
Expand Down Expand Up @@ -58,18 +58,52 @@ class Dashboard {
*/
const BLAZEDASH_CACHE_BUSTER_CACHE_KEY = 'jetpack_blaze_admin_asset_cache_buster';

/**
* Blaze dashboard admin page. Default is tools.php.
*
* @var string
*/
private $admin_page;

/**
* Blaze dashboard menu slug. Default is 'advertising'.
*
* @var string
*/
private $menu_slug;

/**
* Blaze dashboard css prefix. Default is 'jp-blaze'.
*
* @var string
*/
private $css_prefix;

/**
* Dashboard constructor.
*
* @param string $admin_page Dashboard admin page. Default is tools.php.
* @param string $menu_slug Dashboard menu slug. Default is 'advertising'.
* @param string $css_prefix Dashboard css prefix. Default is 'jp-blaze'.
*/
public function __construct( $admin_page = 'tools.php', $menu_slug = 'advertising', $css_prefix = 'jp-blaze' ) {
$this->admin_page = $admin_page;
$this->menu_slug = $menu_slug;
$this->css_prefix = $css_prefix;
}

/**
* Override render funtion
*
* @return void
*/
public function render() {
?>
<div id="wpcom" class="jp-blaze-dashboard" style="min-height: calc(100vh - 100px);">
<div class="hide-if-js"><?php esc_html_e( 'Your Jetpack Blaze dashboard requires JavaScript to function properly.', 'jetpack-blaze' ); ?></div>
<div id="wpcom" class="<?php echo esc_attr( $this->css_prefix ); ?>-dashboard" style="min-height: calc(100vh - 100px);">
<div class="hide-if-js"><?php esc_html_e( 'Your Jetpack Blaze dashboard requires JavaScript to function properly.', 'jetpack-blaze' ); ?></div>
<div class="hide-if-no-js" style="height: 100%">
<img
class="jp-blaze-dashboard-loading-spinner"
class="<?php echo esc_attr( $this->css_prefix ); ?>-dashboard-loading-spinner"
width="32"
height="32"
style="position: absolute; left: 50%; top: 50%;"
Expand All @@ -90,7 +124,7 @@ class="jp-blaze-dashboard-loading-spinner"
// we intercept on all anchor tags and change it to hashbang style.
$("#wpcom").on('click', 'a', function (e) {
const link = e && e.currentTarget && e.currentTarget.attributes && e.currentTarget.attributes.href && e.currentTarget.attributes.href.value;
if( link && link.startsWith( '/advertising' ) ) {
if (link && link.startsWith( '/<?php echo esc_attr( $this->menu_slug ); ?>' ) ) {
location.hash = `#!${link}`;
return false;
}
Expand All @@ -113,14 +147,14 @@ public function admin_init() {
* @param string $hook The current admin page.
*/
public function load_admin_scripts( $hook ) {
if ( 'tools_page_advertising' !== $hook ) {
if ( ! str_ends_with( $hook, 'page_' . $this->menu_slug ) ) {
return;
}

$asset_handle = self::SCRIPT_HANDLE;
$asset_name = 'build.min';

$dashboard_config = new Dashboard_Config_Data();
$dashboard_config = new Dashboard_Config_Data( $this->admin_page, $this->menu_slug );

$config_data = $dashboard_config->get_data();

Expand Down
14 changes: 12 additions & 2 deletions projects/packages/blaze/tests/php/test-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public function test_render() {
( new Dashboard() )->render();
}

/**
* Test has root dom with the provided overridden classname.
*
* @covers Automattic\Jetpack\Blaze\Dashboard::render
*/
public function test_render_with_overridden_class() {
$this->expectOutputRegex( '/<div id="wpcom" class="custom-class-dashboard".*>/i' );
( new Dashboard( 'tools.php', 'advertising', 'custom-class' ) )->render();
}

/**
* Ensure the script can be enqueued in admin.
*
Expand All @@ -44,12 +54,12 @@ public function test_load_admin_scripts() {
$style_handle = $script_handle . '-style';

// Scripts and style should not be enqueued on the main dashboard.
( new Dashboard() )->load_admin_scripts( 'index.php' );
( new Dashboard( 'tools.php', 'custom-menu' ) )->load_admin_scripts( 'index.php' );
$this->assertFalse( wp_script_is( $script_handle, 'enqueued' ) );
$this->assertFalse( wp_style_is( $style_handle, 'enqueued' ) );

// They should, however, be enqueued on the Advertising page.
( new Dashboard() )->load_admin_scripts( 'tools_page_advertising' );
( new Dashboard( 'tools.php', 'custom-menu' ) )->load_admin_scripts( 'tools_page_custom-menu' );
$this->assertTrue( wp_script_is( $script_handle, 'enqueued' ) );
$this->assertTrue( wp_style_is( $style_handle, 'enqueued' ) );
}
Expand Down
5 changes: 5 additions & 0 deletions projects/plugins/jetpack/changelog/add-blaze-new-filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06f4893

Please sign in to comment.