| 1 |
<?php |
| 2 |
/** |
| 3 |
* Attract high-quality traffic to your site. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-blaze |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack; |
| 9 |
|
| 10 |
use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 11 |
use Automattic\Jetpack\Blaze\Dashboard as Blaze_Dashboard; |
| 12 |
use Automattic\Jetpack\Blaze\Dashboard_REST_Controller as Blaze_Dashboard_REST_Controller; |
| 13 |
use Automattic\Jetpack\Blaze\REST_Controller; |
| 14 |
use Automattic\Jetpack\Connection\Client; |
| 15 |
use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 16 |
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection; |
| 17 |
use Automattic\Jetpack\Status\Host; |
| 18 |
use Automattic\Jetpack\Sync\Settings as Sync_Settings; |
| 19 |
use WP_Post; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class for promoting posts. |
| 23 |
*/ |
| 24 |
class Blaze { |
| 25 |
/** |
| 26 |
* Script handle for the JS file we enqueue in the post editor. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
const SCRIPT_HANDLE = 'jetpack-promote-editor'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Transient prefix for active campaign status checks. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
const ACTIVE_CAMPAIGNS_STATUS_TRANSIENT_PREFIX = 'jetpack_blaze_active_campaigns_status_'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Transient TTL for active campaign status checks that find campaigns. |
| 41 |
* |
| 42 |
* @var int |
| 43 |
*/ |
| 44 |
const ACTIVE_CAMPAIGNS_STATUS_TRANSIENT_TTL = HOUR_IN_SECONDS; |
| 45 |
|
| 46 |
/** |
| 47 |
* Transient TTL for active campaign status checks that cannot determine campaign status. |
| 48 |
* |
| 49 |
* @var int |
| 50 |
*/ |
| 51 |
const UNKNOWN_ACTIVE_CAMPAIGNS_STATUS_TRANSIENT_TTL = 5 * MINUTE_IN_SECONDS; |
| 52 |
|
| 53 |
/** |
| 54 |
* Minimum campaign statuses that should trigger a warning. |
| 55 |
* |
| 56 |
* @var string[] |
| 57 |
*/ |
| 58 |
const ACTIVE_CAMPAIGN_STATUSES = array( 'active' ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Path of the JS file we enqueue in the post editor. |
| 62 |
* |
| 63 |
* @var string |
| 64 |
*/ |
| 65 |
public static $script_path = '../build/editor.js'; |
| 66 |
|
| 67 |
/** |
| 68 |
* Initializer. |
| 69 |
* Used to configure the blaze package, eg when called via the Config package. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public static function init() { |
| 74 |
// On the edit screen, add a row action to promote the post. |
| 75 |
add_action( 'load-edit.php', array( __CLASS__, 'add_post_links_actions' ) ); |
| 76 |
// After the quick-edit screen is processed, ensure the blaze row action is still present |
| 77 |
if ( 'edit.php' === $GLOBALS['pagenow'] || |
| 78 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification is not needed here, we're not saving anything. |
| 79 |
( 'admin-ajax.php' === $GLOBALS['pagenow'] && ! empty( $_POST['post_view'] ) && 'list' === $_POST['post_view'] && ! empty( $_POST['action'] ) && 'inline-save' === $_POST['action'] ) ) { |
| 80 |
self::add_post_links_actions(); |
| 81 |
} |
| 82 |
// In the post editor, add a post-publish panel to allow promoting the post. |
| 83 |
add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_assets' ) ); |
| 84 |
// Add a Blaze Menu. |
| 85 |
add_action( 'admin_menu', array( __CLASS__, 'enable_blaze_menu' ), 999 ); |
| 86 |
// Redirect legacy/duplicate advertising URLs (the old tools.php location, or the |
| 87 |
// admin.php?page=advertising slug when the standalone Blaze Ads plugin owns the |
| 88 |
// menu). Runs late (after WooCommerce and core register their menus, so |
| 89 |
// get_menu_parent() resolves the real parent) but still before WordPress |
| 90 |
// validates the page parameter. |
| 91 |
add_action( 'admin_menu', array( __CLASS__, 'redirect_legacy_advertising_url' ), 999 ); |
| 92 |
// Add Blaze dashboard app REST API endpoints. |
| 93 |
add_action( 'rest_api_init', array( Blaze_Dashboard_REST_Controller::class, 'register' ) ); |
| 94 |
// Add general Blaze REST API endpoints. |
| 95 |
add_action( 'rest_api_init', array( REST_Controller::class, 'register' ) ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Add links under each published post in the wp-admin post list. |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public static function add_post_links_actions() { |
| 104 |
if ( self::should_initialize()['can_init'] ) { |
| 105 |
add_filter( 'post_row_actions', array( __CLASS__, 'jetpack_blaze_row_action' ), 10, 2 ); |
| 106 |
add_filter( 'page_row_actions', array( __CLASS__, 'jetpack_blaze_row_action' ), 10, 2 ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Is the wp-admin Dashboard enabled? |
| 112 |
* That dashboard is not available or necessary on WordPress.com sites when the nav redesign is disabled. |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public static function is_dashboard_enabled() { |
| 117 |
$is_dashboard_enabled = true; |
| 118 |
|
| 119 |
// On WordPress.com sites, the dashboard is not needed if the nav redesign is not enabled. |
| 120 |
if ( get_option( 'wpcom_admin_interface' ) !== 'wp-admin' && ( new Host() )->is_wpcom_platform() ) { |
| 121 |
$is_dashboard_enabled = false; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Enable a wp-admin dashboard for Blaze campaign management. |
| 126 |
* |
| 127 |
* @since 0.7.0 |
| 128 |
* |
| 129 |
* @param bool $should_enable Should the dashboard be enabled? |
| 130 |
*/ |
| 131 |
return apply_filters( 'jetpack_blaze_dashboard_enable', $is_dashboard_enabled ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Enable the Blaze menu. |
| 136 |
* |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public static function enable_blaze_menu() { |
| 140 |
if ( ! self::should_initialize()['can_init'] ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Filter the menu page slug used for the Blaze dashboard. |
| 146 |
* |
| 147 |
* @since 0.28.0 |
| 148 |
* |
| 149 |
* @param string $menu_slug The menu page slug. Default 'advertising'. |
| 150 |
*/ |
| 151 |
$menu_slug = apply_filters( 'jetpack_blaze_menu_slug', 'advertising' ); |
| 152 |
|
| 153 |
// Avoid a duplicate Blaze Ads menu when the standalone Blaze Ads plugin |
| 154 |
// (Automattic/blaze-ads) is also active. Releases <= 0.9.0 register their own |
| 155 |
// menu and leave our slug at the default 'advertising', so we bail to avoid a |
| 156 |
// second entry; the standalone owns the menu under 'wp-blaze' and |
| 157 |
// redirect_legacy_advertising_url() forwards 'advertising' links there. When the |
| 158 |
// standalone instead delegates registration to us by filtering the slug (e.g. to |
| 159 |
// 'wp-blaze'), we are the only registrant and must register normally -- the |
| 160 |
// non-default slug is what tells the two cases apart. |
| 161 |
if ( 'advertising' === $menu_slug && self::is_standalone_blaze_ads_active() ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Filter the menu label for the Blaze dashboard menu item. |
| 167 |
* |
| 168 |
* @since 0.28.0 |
| 169 |
* |
| 170 |
* @param string $menu_label The menu label. Default 'Blaze Ads'. |
| 171 |
*/ |
| 172 |
$menu_label = apply_filters( 'jetpack_blaze_menu_label', 'Blaze Ads' ); // Product name, do not translate. |
| 173 |
|
| 174 |
/** |
| 175 |
* Filter the CSS class prefix for the Blaze dashboard. |
| 176 |
* |
| 177 |
* @since 0.28.0 |
| 178 |
* |
| 179 |
* @param string $css_prefix The CSS class prefix. Default 'jp-blaze'. |
| 180 |
*/ |
| 181 |
$css_prefix = apply_filters( 'jetpack_blaze_dashboard_css_prefix', 'jp-blaze' ); |
| 182 |
|
| 183 |
$parent_slug = self::get_menu_parent(); |
| 184 |
$blaze_dashboard = new Blaze_Dashboard( 'admin.php', $menu_slug, $css_prefix ); |
| 185 |
|
| 186 |
if ( self::is_dashboard_enabled() || ( new Host() )->is_wpcom_platform() ) { |
| 187 |
if ( 'jetpack' === $parent_slug ) { |
| 188 |
// Register through Admin_Menu: on WordPress.com Simple sites the Jetpack |
| 189 |
// parent menu does not exist yet at this priority, and registering the |
| 190 |
// submenu before its parent produces a broken menu URL. |
| 191 |
$page_suffix = Admin_Menu::add_menu( |
| 192 |
esc_attr( $menu_label ), |
| 193 |
$menu_label, |
| 194 |
'manage_options', |
| 195 |
$menu_slug, |
| 196 |
array( $blaze_dashboard, 'render' ) |
| 197 |
); |
| 198 |
} else { |
| 199 |
// Other parents already exist at this priority, so add_submenu_page is safe. |
| 200 |
$page_suffix = add_submenu_page( |
| 201 |
$parent_slug, |
| 202 |
esc_attr( $menu_label ), |
| 203 |
$menu_label, |
| 204 |
'manage_options', |
| 205 |
$menu_slug, |
| 206 |
array( $blaze_dashboard, 'render' ), |
| 207 |
1 |
| 208 |
); |
| 209 |
} |
| 210 |
add_action( 'load-' . $page_suffix, array( $blaze_dashboard, 'admin_init' ) ); |
| 211 |
|
| 212 |
// Temporary entry at the old Tools location; remove ~1 month after the move ships. |
| 213 |
if ( 'tools.php' !== $parent_slug ) { |
| 214 |
self::add_migration_notice_menu(); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Detect whether the standalone Blaze Ads plugin (Automattic/blaze-ads) is active. |
| 221 |
* |
| 222 |
* Both this package and the standalone plugin can register a Blaze Ads menu, which |
| 223 |
* would result in a duplicate entry when both are active on the same site (for |
| 224 |
* example a WooCommerce store running Jetpack alongside the standalone plugin). When |
| 225 |
* the standalone plugin is present it owns the menu, so this package defers to it. |
| 226 |
* |
| 227 |
* Detection relies on a class/constant defined by the standalone plugin rather than |
| 228 |
* on any cooperation from it, so it works with every released version of the plugin, |
| 229 |
* including ones that predate the jetpack_blaze_menu_* filters. |
| 230 |
* |
| 231 |
* @return bool True if the standalone Blaze Ads plugin is active. |
| 232 |
*/ |
| 233 |
public static function is_standalone_blaze_ads_active() { |
| 234 |
$is_active = defined( 'BLAZEADS_PLUGIN_FILE' ) |
| 235 |
|| defined( 'BLAZE_ADS_VERSION_NUMBER' ) |
| 236 |
|| class_exists( 'Blaze_Ads' ); |
| 237 |
|
| 238 |
/** |
| 239 |
* Filter whether the standalone Blaze Ads plugin (Automattic/blaze-ads) is |
| 240 |
* considered active. When active and this package is still using the default |
| 241 |
* menu slug, the package skips registering its own Blaze Ads menu to avoid a |
| 242 |
* duplicate entry. |
| 243 |
* |
| 244 |
* @since 0.28.0 |
| 245 |
* |
| 246 |
* @param bool $is_active Whether the standalone Blaze Ads plugin is active. |
| 247 |
*/ |
| 248 |
return (bool) apply_filters( 'jetpack_blaze_standalone_active', $is_active ); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Determine the appropriate parent menu slug based on the installation context. |
| 253 |
* |
| 254 |
* - WooCommerce active (Marketing menu registered): parent is 'woocommerce-marketing' |
| 255 |
* - WordPress.com platform or Jetpack connected: parent is 'jetpack' |
| 256 |
* - Otherwise: parent is 'tools.php' |
| 257 |
* |
| 258 |
* @return string The parent menu slug. |
| 259 |
*/ |
| 260 |
public static function get_menu_parent() { |
| 261 |
if ( class_exists( 'WooCommerce' ) ) { |
| 262 |
// Only use woocommerce-marketing if the menu is actually registered. |
| 263 |
global $menu; |
| 264 |
foreach ( (array) $menu as $item ) { |
| 265 |
if ( isset( $item[2] ) && 'woocommerce-marketing' === $item[2] ) { |
| 266 |
/** |
| 267 |
* Filter the parent menu slug for the Blaze dashboard submenu item. |
| 268 |
* |
| 269 |
* @since 0.28.0 |
| 270 |
* |
| 271 |
* @param string $parent The parent menu slug. |
| 272 |
*/ |
| 273 |
return apply_filters( 'jetpack_blaze_menu_parent', 'woocommerce-marketing' ); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if ( ( new Host() )->is_wpcom_platform() ) { |
| 279 |
/** This filter is documented above. */ |
| 280 |
return apply_filters( 'jetpack_blaze_menu_parent', 'jetpack' ); |
| 281 |
} |
| 282 |
|
| 283 |
if ( ( new Jetpack_Connection() )->is_connected() ) { |
| 284 |
/** This filter is documented above. */ |
| 285 |
return apply_filters( 'jetpack_blaze_menu_parent', 'jetpack' ); |
| 286 |
} |
| 287 |
|
| 288 |
/** This filter is documented above. */ |
| 289 |
return apply_filters( 'jetpack_blaze_menu_parent', 'tools.php' ); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Register a temporary "moved" notice page at the old Tools menu location. |
| 294 |
* |
| 295 |
* The menu keeps its old "Advertising" label so users who knew it by that |
| 296 |
* name still recognize it; the page itself explains the move to Blaze Ads. |
| 297 |
* |
| 298 |
* @return void |
| 299 |
*/ |
| 300 |
public static function add_migration_notice_menu() { |
| 301 |
/** |
| 302 |
* Filter whether to show the temporary migration notice at the old |
| 303 |
* Tools > Advertising location. |
| 304 |
* |
| 305 |
* @since 0.28.0 |
| 306 |
* |
| 307 |
* @param bool $show_notice Whether to show the migration notice. Default true. |
| 308 |
*/ |
| 309 |
if ( ! apply_filters( 'jetpack_blaze_show_migration_notice', true ) ) { |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
add_submenu_page( |
| 314 |
'tools.php', |
| 315 |
esc_attr__( 'Advertising', 'jetpack-blaze' ), |
| 316 |
__( 'Advertising', 'jetpack-blaze' ), |
| 317 |
'manage_options', |
| 318 |
'advertising-moved', |
| 319 |
array( __CLASS__, 'render_migration_notice' ), |
| 320 |
1 |
| 321 |
); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Render the temporary migration notice shown at the old Tools menu location. |
| 326 |
* |
| 327 |
* @return void |
| 328 |
*/ |
| 329 |
public static function render_migration_notice() { |
| 330 |
/** This filter is documented in Blaze::enable_blaze_menu() */ |
| 331 |
$menu_slug = apply_filters( 'jetpack_blaze_menu_slug', 'advertising' ); |
| 332 |
|
| 333 |
$parent_slug = self::get_menu_parent(); |
| 334 |
$parent_label = 'woocommerce-marketing' === $parent_slug |
| 335 |
? __( 'Marketing', 'jetpack-blaze' ) |
| 336 |
: 'Jetpack'; |
| 337 |
|
| 338 |
$dashboard_url = admin_url( 'admin.php?page=' . $menu_slug ); |
| 339 |
$image_file = 'woocommerce-marketing' === $parent_slug |
| 340 |
? 'blaze-ads-moved-woo.webp' |
| 341 |
: 'blaze-ads-moved.webp'; |
| 342 |
$image_path = dirname( __DIR__ ) . '/assets/images/' . $image_file; |
| 343 |
$image_url = plugins_url( 'assets/images/' . $image_file, __DIR__ ); |
| 344 |
|
| 345 |
?> |
| 346 |
<div class="wrap blaze-ads-migration"> |
| 347 |
<div class="blaze-ads-migration__header"> |
| 348 |
<svg class="blaze-ads-migration__logo" xmlns="http://www.w3.org/2000/svg" height="32" width="32" viewBox="0 0 32 32" aria-hidden="true" focusable="false"> |
| 349 |
<path d="M16,0C7.2,0,0,7.2,0,16s7.2,16,16,16s16-7.2,16-16S24.8,0,16,0z" fill="#069e08" /> |
| 350 |
<polygon points="15,19 7,19 15,3" fill="#fff" /> |
| 351 |
<polygon points="17,29 17,13 25,13" fill="#fff" /> |
| 352 |
</svg> |
| 353 |
<span class="blaze-ads-migration__brand">Blaze Ads</span> |
| 354 |
</div> |
| 355 |
<h1 class="blaze-ads-migration__title"> |
| 356 |
<?php |
| 357 |
printf( |
| 358 |
/* translators: %s is the product name (not translatable). */ |
| 359 |
esc_html__( '%s has moved', 'jetpack-blaze' ), |
| 360 |
'Blaze Ads' |
| 361 |
); |
| 362 |
?> |
| 363 |
</h1> |
| 364 |
<p class="blaze-ads-migration__subtitle"> |
| 365 |
<?php |
| 366 |
printf( |
| 367 |
/* translators: %s is the menu path where the section lives now, e.g. "Jetpack → Blaze Ads" or "Marketing → Blaze Ads". */ |
| 368 |
esc_html__( "Now it's part of %s", 'jetpack-blaze' ), |
| 369 |
esc_html( $parent_label . ' → Blaze Ads' ) |
| 370 |
); |
| 371 |
?> |
| 372 |
</p> |
| 373 |
<p> |
| 374 |
<a class="button button-primary button-hero" href="<?php echo esc_url( $dashboard_url ); ?>"> |
| 375 |
<?php |
| 376 |
printf( |
| 377 |
/* translators: %s is the product name (not translatable). */ |
| 378 |
esc_html__( 'Check new %s', 'jetpack-blaze' ), |
| 379 |
'Blaze Ads' |
| 380 |
); |
| 381 |
?> |
| 382 |
</a> |
| 383 |
</p> |
| 384 |
<?php if ( file_exists( $image_path ) ) : ?> |
| 385 |
<p class="blaze-ads-migration__image"> |
| 386 |
<img |
| 387 |
src="<?php echo esc_url( $image_url ); ?>" |
| 388 |
alt="<?php echo esc_attr( sprintf( /* translators: %s is the product name (not translatable). */ __( '%s has moved', 'jetpack-blaze' ), 'Blaze Ads' ) ); ?>" |
| 389 |
/> |
| 390 |
</p> |
| 391 |
<?php endif; ?> |
| 392 |
</div> |
| 393 |
<style> |
| 394 |
.blaze-ads-migration__header { display: flex; align-items: center; gap: 8px; margin: 16px 0 24px; } |
| 395 |
.blaze-ads-migration__brand { font-size: 1.65em; font-weight: 500; line-height: 1; } |
| 396 |
.blaze-ads-migration__title { font-size: 2.5em; margin-bottom: 0.25em; padding: 0; } |
| 397 |
.blaze-ads-migration__subtitle { font-size: 1.4em; margin-top: 0; color: #50575e; } |
| 398 |
.blaze-ads-migration__image { margin-top: 3em; } |
| 399 |
.blaze-ads-migration__image img { max-width: 100%; height: auto; } |
| 400 |
</style> |
| 401 |
<?php |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Redirect legacy/duplicate Blaze dashboard URLs to wherever the menu actually lives. |
| 406 |
* |
| 407 |
* Runs early on admin_menu, before WordPress validates the ?page= parameter against |
| 408 |
* registered submenus. The target is computed by get_legacy_advertising_redirect_target(); |
| 409 |
* this method only performs the redirect. |
| 410 |
* |
| 411 |
* @return void |
| 412 |
*/ |
| 413 |
public static function redirect_legacy_advertising_url() { |
| 414 |
$target = self::get_legacy_advertising_redirect_target(); |
| 415 |
if ( null !== $target ) { |
| 416 |
wp_safe_redirect( $target, 302 ); |
| 417 |
exit; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Compute where a legacy/duplicate `page=advertising` request should be redirected, or |
| 423 |
* null if it should be left alone. Pure (no side effects) so it is unit-testable. |
| 424 |
* |
| 425 |
* Two cases produce a redirect: |
| 426 |
* |
| 427 |
* - Standard move: `tools.php?page=advertising` -> `admin.php?page=advertising`, once the |
| 428 |
* menu has moved away from Tools. |
| 429 |
* - Standalone Blaze Ads plugin present: this package does not register the 'advertising' |
| 430 |
* page (the standalone owns the menu under 'wp-blaze'), so BOTH the tools.php and the |
| 431 |
* admin.php `page=advertising` entry points are forwarded to the standalone's page. We |
| 432 |
* forward both ourselves rather than relying on the standalone shipping its own |
| 433 |
* redirect, so correctness never depends on a particular standalone release. |
| 434 |
* |
| 435 |
* @return string|null The redirect URL, or null if no redirect should happen. |
| 436 |
*/ |
| 437 |
public static function get_legacy_advertising_redirect_target() { |
| 438 |
global $pagenow; |
| 439 |
|
| 440 |
if ( |
| 441 |
! isset( $_GET['page'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check to redirect, no data is processed. |
| 442 |
|| 'advertising' !== $_GET['page'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check to redirect, no data is processed. |
| 443 |
) { |
| 444 |
return null; |
| 445 |
} |
| 446 |
|
| 447 |
if ( ! self::should_initialize()['can_init'] ) { |
| 448 |
return null; |
| 449 |
} |
| 450 |
|
| 451 |
if ( self::is_standalone_blaze_ads_active() ) { |
| 452 |
// Only the two URLs that can carry the dashboard page parameter. |
| 453 |
if ( 'admin.php' !== $pagenow && 'tools.php' !== $pagenow ) { |
| 454 |
return null; |
| 455 |
} |
| 456 |
/** |
| 457 |
* Filter the standalone Blaze Ads plugin's menu slug used as the redirect |
| 458 |
* target when this package defers to it. |
| 459 |
* |
| 460 |
* @since 0.28.0 |
| 461 |
* |
| 462 |
* @param string $standalone_slug The standalone menu slug. Default 'wp-blaze'. |
| 463 |
*/ |
| 464 |
$standalone_slug = apply_filters( 'jetpack_blaze_standalone_menu_slug', 'wp-blaze' ); |
| 465 |
return admin_url( 'admin.php?page=' . $standalone_slug ); |
| 466 |
} |
| 467 |
|
| 468 |
if ( 'tools.php' !== $pagenow || 'tools.php' === self::get_menu_parent() ) { |
| 469 |
return null; |
| 470 |
} |
| 471 |
|
| 472 |
/** This filter is documented in Blaze::enable_blaze_menu() */ |
| 473 |
$menu_slug = apply_filters( 'jetpack_blaze_menu_slug', 'advertising' ); |
| 474 |
return admin_url( 'admin.php?page=' . $menu_slug ); |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* Check the WordPress.com REST API |
| 479 |
* to ensure that the site supports the Blaze feature. |
| 480 |
* |
| 481 |
* - If the site is on WordPress.com Simple, we do not query the API. |
| 482 |
* - Results are cached for a day after getting response from API. |
| 483 |
* - If the API returns an error, we cache the result for an hour. |
| 484 |
* |
| 485 |
* @param int $blog_id The blog ID to check. |
| 486 |
* |
| 487 |
* @return bool |
| 488 |
*/ |
| 489 |
public static function site_supports_blaze( $blog_id ) { |
| 490 |
$transient_name = 'jetpack_blaze_site_supports_blaze_' . $blog_id; |
| 491 |
|
| 492 |
/* |
| 493 |
* On WordPress.com, we don't need to make an API request, |
| 494 |
* we can query directly. |
| 495 |
*/ |
| 496 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'blaze_is_site_eligible' ) ) { |
| 497 |
return blaze_is_site_eligible( $blog_id ); |
| 498 |
} |
| 499 |
|
| 500 |
$cached_result = get_transient( $transient_name ); |
| 501 |
if ( false !== $cached_result ) { |
| 502 |
if ( is_array( $cached_result ) ) { |
| 503 |
return $cached_result['approved']; |
| 504 |
} |
| 505 |
|
| 506 |
return (bool) $cached_result; |
| 507 |
} |
| 508 |
|
| 509 |
// Make the API request. |
| 510 |
$url = sprintf( '/sites/%d/blaze/status', $blog_id ); |
| 511 |
$response = Client::wpcom_json_api_request_as_blog( |
| 512 |
$url, |
| 513 |
'2', |
| 514 |
array( 'method' => 'GET' ), |
| 515 |
null, |
| 516 |
'wpcom' |
| 517 |
); |
| 518 |
|
| 519 |
// If there was an error or malformed response, bail and save response for an hour. |
| 520 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 521 |
set_transient( $transient_name, array( 'approved' => false ), HOUR_IN_SECONDS ); |
| 522 |
return false; |
| 523 |
} |
| 524 |
|
| 525 |
// Decode the results. |
| 526 |
$result = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 527 |
|
| 528 |
// Bail if there were no results returned. |
| 529 |
if ( ! is_array( $result ) || ! isset( $result['approved'] ) ) { |
| 530 |
return false; |
| 531 |
} |
| 532 |
|
| 533 |
// Cache the result for 24 hours. |
| 534 |
set_transient( $transient_name, array( 'approved' => (bool) $result['approved'] ), DAY_IN_SECONDS ); |
| 535 |
|
| 536 |
return (bool) $result['approved']; |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* Get the active campaign status for a site. |
| 541 |
* |
| 542 |
* @param int $blog_id The blog ID to check. |
| 543 |
* |
| 544 |
* @return array Active campaign status. |
| 545 |
*/ |
| 546 |
public static function get_active_campaigns_status( $blog_id ) { |
| 547 |
$blog_id = absint( $blog_id ); |
| 548 |
|
| 549 |
if ( empty( $blog_id ) ) { |
| 550 |
return self::get_unknown_active_campaigns_status(); |
| 551 |
} |
| 552 |
|
| 553 |
$transient_name = self::ACTIVE_CAMPAIGNS_STATUS_TRANSIENT_PREFIX . $blog_id; |
| 554 |
$cached_result = get_transient( $transient_name ); |
| 555 |
|
| 556 |
if ( false !== $cached_result ) { |
| 557 |
return $cached_result; |
| 558 |
} |
| 559 |
|
| 560 |
/** |
| 561 |
* Filter the campaign statuses that should trigger the active campaign warning. |
| 562 |
* |
| 563 |
* @since 0.27.23 |
| 564 |
* |
| 565 |
* @param string[] $statuses Campaign statuses to check. |
| 566 |
* @param int $blog_id The blog ID being checked. |
| 567 |
*/ |
| 568 |
$statuses = apply_filters( 'jetpack_blaze_active_campaign_statuses', self::ACTIVE_CAMPAIGN_STATUSES, $blog_id ); |
| 569 |
$statuses = array_filter( array_map( 'sanitize_key', (array) $statuses ) ); |
| 570 |
|
| 571 |
if ( empty( $statuses ) ) { |
| 572 |
$statuses = self::ACTIVE_CAMPAIGN_STATUSES; |
| 573 |
} |
| 574 |
|
| 575 |
$url = add_query_arg( |
| 576 |
array( |
| 577 |
'status' => implode( ',', $statuses ), |
| 578 |
'limit' => 1, |
| 579 |
), |
| 580 |
sprintf( '/sites/%d/wordads/dsp/api/v1/search/campaigns/site/%d', $blog_id, $blog_id ) |
| 581 |
); |
| 582 |
|
| 583 |
$response = Client::wpcom_json_api_request_as_user( |
| 584 |
$url, |
| 585 |
'2', |
| 586 |
array( 'method' => 'GET' ), |
| 587 |
null, |
| 588 |
'wpcom' |
| 589 |
); |
| 590 |
|
| 591 |
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 592 |
return self::get_unknown_active_campaigns_status( $transient_name ); |
| 593 |
} |
| 594 |
|
| 595 |
$result = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 596 |
|
| 597 |
if ( ! is_array( $result ) || ! isset( $result['campaigns'] ) || ! is_array( $result['campaigns'] ) ) { |
| 598 |
return self::get_unknown_active_campaigns_status( $transient_name ); |
| 599 |
} |
| 600 |
|
| 601 |
$has_active_campaigns = ! empty( $result['campaigns'] ); |
| 602 |
$status = array( |
| 603 |
'has_active_campaigns' => $has_active_campaigns, |
| 604 |
'status' => $has_active_campaigns ? 'active' : 'none', |
| 605 |
); |
| 606 |
|
| 607 |
if ( $has_active_campaigns ) { |
| 608 |
set_transient( $transient_name, $status, self::ACTIVE_CAMPAIGNS_STATUS_TRANSIENT_TTL ); |
| 609 |
} |
| 610 |
|
| 611 |
return $status; |
| 612 |
} |
| 613 |
|
| 614 |
/** |
| 615 |
* Get the unknown active campaign status response. |
| 616 |
* |
| 617 |
* @param string|null $transient_name Optional transient name used to cache the unknown status. |
| 618 |
* @return array Unknown active campaign status. |
| 619 |
*/ |
| 620 |
private static function get_unknown_active_campaigns_status( $transient_name = null ) { |
| 621 |
$status = array( |
| 622 |
'has_active_campaigns' => false, |
| 623 |
'status' => 'unknown', |
| 624 |
); |
| 625 |
|
| 626 |
if ( $transient_name ) { |
| 627 |
set_transient( $transient_name, $status, self::UNKNOWN_ACTIVE_CAMPAIGNS_STATUS_TRANSIENT_TTL ); |
| 628 |
} |
| 629 |
|
| 630 |
return $status; |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* Determines if criteria is met to enable Blaze features. |
| 635 |
* Keep in mind that this makes remote requests, so we want to avoid calling it when unnecessary, like in the frontend. |
| 636 |
* |
| 637 |
* @return array |
| 638 |
*/ |
| 639 |
public static function should_initialize() { |
| 640 |
$is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; |
| 641 |
$connection = new Jetpack_Connection(); |
| 642 |
$site_id = Jetpack_Connection::get_site_id(); |
| 643 |
|
| 644 |
// Only admins should be able to Blaze posts on a site. |
| 645 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 646 |
return array( |
| 647 |
'can_init' => false, |
| 648 |
'reason' => 'user_not_admin', |
| 649 |
); |
| 650 |
} |
| 651 |
|
| 652 |
// Allow short-circuiting the Blaze initialization via a filter. |
| 653 |
if ( has_filter( 'jetpack_blaze_enabled' ) ) { |
| 654 |
/** |
| 655 |
* Filter to disable all Blaze functionality. |
| 656 |
* |
| 657 |
* @since 0.3.0 |
| 658 |
* |
| 659 |
* @param bool $should_initialize Whether Blaze should be enabled. Default to true. |
| 660 |
*/ |
| 661 |
$should_init = apply_filters( 'jetpack_blaze_enabled', true ); |
| 662 |
|
| 663 |
return array( |
| 664 |
'can_init' => $should_init, |
| 665 |
'reason' => $should_init ? null : 'initialization_disabled', |
| 666 |
); |
| 667 |
} |
| 668 |
|
| 669 |
// On self-hosted sites, we must do some additional checks. |
| 670 |
if ( ! $is_wpcom ) { |
| 671 |
/* |
| 672 |
* These features currently only work on WordPress.com, |
| 673 |
* so the site must be connected to WordPress.com, and the user as well for things to work. |
| 674 |
*/ |
| 675 |
if ( |
| 676 |
is_wp_error( $site_id ) |
| 677 |
) { |
| 678 |
return array( |
| 679 |
'can_init' => false, |
| 680 |
'reason' => 'wp_error', |
| 681 |
); |
| 682 |
} |
| 683 |
|
| 684 |
if ( ! $connection->is_connected() ) { |
| 685 |
return array( |
| 686 |
'can_init' => false, |
| 687 |
'reason' => 'site_not_connected', |
| 688 |
); |
| 689 |
} |
| 690 |
|
| 691 |
if ( ! $connection->is_user_connected() ) { |
| 692 |
return array( |
| 693 |
'can_init' => false, |
| 694 |
'reason' => 'user_not_connected', |
| 695 |
); |
| 696 |
} |
| 697 |
|
| 698 |
// The whole thing is powered by Sync! |
| 699 |
if ( ! Sync_Settings::is_sync_enabled() ) { |
| 700 |
return array( |
| 701 |
'can_init' => false, |
| 702 |
'reason' => 'sync_disabled', |
| 703 |
); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
// Check if the site supports Blaze. |
| 708 |
if ( is_numeric( $site_id ) && ! self::site_supports_blaze( $site_id ) ) { |
| 709 |
return array( |
| 710 |
'can_init' => false, |
| 711 |
'reason' => 'site_not_eligible', |
| 712 |
); |
| 713 |
} |
| 714 |
|
| 715 |
// Final fallback. |
| 716 |
return array( |
| 717 |
'can_init' => true, |
| 718 |
'reason' => null, |
| 719 |
); |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Get URL to create a Blaze campaign for a specific post. |
| 724 |
* |
| 725 |
* This can return 2 different types of URL: |
| 726 |
* - Calypso Links |
| 727 |
* - wp-admin Links if access to the wp-admin Blaze Dashboard is enabled. |
| 728 |
* |
| 729 |
* @param int|string $post_id Post ID. |
| 730 |
* |
| 731 |
* @return array An array with the link, and whether this is a Calypso or a wp-admin link. |
| 732 |
*/ |
| 733 |
public static function get_campaign_management_url( $post_id ) { |
| 734 |
if ( self::is_dashboard_enabled() ) { |
| 735 |
/** This filter is documented in Blaze::enable_blaze_menu() */ |
| 736 |
$menu_slug = apply_filters( 'jetpack_blaze_menu_slug', 'advertising' ); |
| 737 |
|
| 738 |
// When the standalone Blaze Ads plugin owns the menu under its own slug and |
| 739 |
// this package did not register 'advertising', point the link straight at the |
| 740 |
// standalone's page. Relying on the admin.php?page=advertising redirect would |
| 741 |
// drop the #! route fragment and land the user on the dashboard home instead |
| 742 |
// of the specific post's promotion flow. |
| 743 |
if ( 'advertising' === $menu_slug && self::is_standalone_blaze_ads_active() ) { |
| 744 |
/** This filter is documented in Blaze::redirect_legacy_advertising_url() */ |
| 745 |
$menu_slug = apply_filters( 'jetpack_blaze_standalone_menu_slug', 'wp-blaze' ); |
| 746 |
} |
| 747 |
|
| 748 |
$admin_url = admin_url( 'admin.php?page=' . $menu_slug ); |
| 749 |
$hostname = wp_parse_url( get_site_url(), PHP_URL_HOST ); |
| 750 |
// The dashboard SPA routes under its menu slug (see Dashboard), so the #! path |
| 751 |
// prefix must match the slug rather than being hardcoded to 'advertising'. |
| 752 |
$blaze_url = sprintf( |
| 753 |
'%1$s#!/%2$s/posts/promote/post-%3$s/%4$s', |
| 754 |
$admin_url, |
| 755 |
$menu_slug, |
| 756 |
esc_attr( $post_id ), |
| 757 |
$hostname |
| 758 |
); |
| 759 |
|
| 760 |
return array( |
| 761 |
'link' => $blaze_url, |
| 762 |
'external' => false, |
| 763 |
); |
| 764 |
} |
| 765 |
|
| 766 |
// Default Calypso link. |
| 767 |
$blaze_url = Redirect::get_url( |
| 768 |
'jetpack-blaze', |
| 769 |
array( |
| 770 |
'query' => 'blazepress-widget=post-' . esc_attr( $post_id ), |
| 771 |
) |
| 772 |
); |
| 773 |
return array( |
| 774 |
'link' => $blaze_url, |
| 775 |
'external' => true, |
| 776 |
); |
| 777 |
} |
| 778 |
|
| 779 |
/** |
| 780 |
* Adds the Promote link to the posts list row action. |
| 781 |
* |
| 782 |
* @param array $post_actions The current array of post actions. |
| 783 |
* @param WP_Post $post The current post in the post list table. |
| 784 |
* |
| 785 |
* @return array |
| 786 |
*/ |
| 787 |
public static function jetpack_blaze_row_action( $post_actions, $post ) { |
| 788 |
/** |
| 789 |
* Allow third-party plugins to disable Blaze row actions. |
| 790 |
* |
| 791 |
* @since 0.16.0 |
| 792 |
* |
| 793 |
* @param bool $are_quick_links_enabled Should Blaze row actions be enabled. |
| 794 |
* @param WP_Post $post The current post in the post list table. |
| 795 |
*/ |
| 796 |
$are_quick_links_enabled = apply_filters( 'jetpack_blaze_post_row_actions_enable', true, $post ); |
| 797 |
|
| 798 |
// Bail if we are not looking at one of the supported post types (post, page, or product). |
| 799 |
if ( |
| 800 |
! $are_quick_links_enabled |
| 801 |
|| ! in_array( $post->post_type, array( 'post', 'page', 'product' ), true ) |
| 802 |
) { |
| 803 |
return $post_actions; |
| 804 |
} |
| 805 |
|
| 806 |
// Bail if the post is not published. |
| 807 |
if ( $post->post_status !== 'publish' ) { |
| 808 |
return $post_actions; |
| 809 |
} |
| 810 |
|
| 811 |
// Bail if the post has a password. |
| 812 |
if ( '' !== $post->post_password ) { |
| 813 |
return $post_actions; |
| 814 |
} |
| 815 |
|
| 816 |
$blaze_url = self::get_campaign_management_url( $post->ID ); |
| 817 |
$text = __( 'Promote with Blaze', 'jetpack-blaze' ); |
| 818 |
$title = get_the_title( $post ); |
| 819 |
$label = sprintf( |
| 820 |
/* translators: post title */ |
| 821 |
__( 'Blaze “%s” to Tumblr and WordPress.com audiences.', 'jetpack-blaze' ), |
| 822 |
$title |
| 823 |
); |
| 824 |
|
| 825 |
$post_actions['blaze'] = sprintf( |
| 826 |
'<a href="%1$s" title="%2$s" aria-label="%2$s" %4$s>%3$s</a>', |
| 827 |
esc_url( $blaze_url['link'] ), |
| 828 |
esc_attr( $label ), |
| 829 |
esc_html( $text ), |
| 830 |
( true === $blaze_url['external'] ? 'target="_blank" rel="noopener noreferrer"' : '' ) |
| 831 |
); |
| 832 |
|
| 833 |
return $post_actions; |
| 834 |
} |
| 835 |
|
| 836 |
/** |
| 837 |
* Enqueue block editor assets. |
| 838 |
*/ |
| 839 |
public static function enqueue_block_editor_assets() { |
| 840 |
/* |
| 841 |
* We do not want (nor need) Blaze in the site editor, or the widget editor, or the classic editor. |
| 842 |
* We only want it in the post editor. |
| 843 |
* Enqueueing the script in those editors would cause a fatal error. |
| 844 |
* See #20357 for more info. |
| 845 |
*/ |
| 846 |
if ( ! function_exists( 'get_current_screen' ) ) { // When Gutenberg is loaded in the frontend. |
| 847 |
return; |
| 848 |
} |
| 849 |
$current_screen = get_current_screen(); |
| 850 |
if ( |
| 851 |
empty( $current_screen ) |
| 852 |
|| $current_screen->base !== 'post' |
| 853 |
|| ! $current_screen->is_block_editor() |
| 854 |
) { |
| 855 |
return; |
| 856 |
} |
| 857 |
// Bail if criteria is not met to enable Blaze features. |
| 858 |
if ( ! self::should_initialize()['can_init'] ) { |
| 859 |
return; |
| 860 |
} |
| 861 |
|
| 862 |
Assets::register_script( |
| 863 |
self::SCRIPT_HANDLE, |
| 864 |
self::$script_path, |
| 865 |
__FILE__, |
| 866 |
array( |
| 867 |
'enqueue' => true, |
| 868 |
'in_footer' => true, |
| 869 |
'textdomain' => 'jetpack-blaze', |
| 870 |
) |
| 871 |
); |
| 872 |
|
| 873 |
// Adds Connection package initial state. |
| 874 |
Connection_Initial_State::render_script( self::SCRIPT_HANDLE ); |
| 875 |
|
| 876 |
// Pass additional data to our script. |
| 877 |
wp_localize_script( |
| 878 |
self::SCRIPT_HANDLE, |
| 879 |
'blazeInitialState', |
| 880 |
array( |
| 881 |
'blazeUrlTemplate' => self::get_campaign_management_url( '__POST_ID__' ), |
| 882 |
) |
| 883 |
); |
| 884 |
} |
| 885 |
} |
| 886 |
|