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