Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add new method to request eligibility to Blaze from WordPress.com.
3 changes: 1 addition & 2 deletions projects/packages/blaze/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"yoast/phpunit-polyfills": "1.0.4",
"automattic/jetpack-assets": "@dev",
"automattic/jetpack-changelogger": "@dev",
"automattic/jetpack-status": "@dev",
"automattic/wordbless": "@dev"
},
"autoload": {
Expand Down Expand Up @@ -60,7 +59,7 @@
"link-template": "https://github.com/automattic/jetpack-blaze/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "0.3.x-dev"
"dev-trunk": "0.4.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.3.5-alpha",
"version": "0.4.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
113 changes: 68 additions & 45 deletions projects/packages/blaze/src/class-blaze.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

namespace Automattic\Jetpack;

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection;
use Automattic\Jetpack\Status\Host;
use Automattic\Jetpack\Sync\Settings as Sync_Settings;

/**
* Class for promoting posts.
*/
class Blaze {

const PACKAGE_VERSION = '0.3.5-alpha';
const PACKAGE_VERSION = '0.4.0-alpha';

/**
* The configuration method that is called from the jetpack-config package.
Expand Down Expand Up @@ -57,64 +57,87 @@ public function register() {
}

/**
* Determines if criteria is met to enable Blaze features.
* Check the WordPress.com REST API
* to ensure that the site supports the Blaze feature.
*
* @todo - Get response from API if requirements are met on the wpcom-side.
* @param int $blog_id The blog ID to check.
*
* @return bool
*/
public static function should_initialize() {
$status = ( new Status() );
$should_initialize = true;
$is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
$user_data = $is_wpcom
? array( 'user_locale' => get_user_locale() )
: ( new Jetpack_Connection() )->get_connected_user_data();

public static function site_supports_blaze( $blog_id ) {
/*
* These features currently only work on WordPress.com,
* so the user should either be connected to WordPress.com for things to work,
* or be on a WordPress.com site where we have direct access to user data such as user locale.
* On WordPress.com, we don't need to make an API request,
* we can query directly.
*/
if ( ! $user_data ) {
$should_initialize = false;
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'blaze_is_site_eligible' ) ) {
return blaze_is_site_eligible( $blog_id );
}

// We currently do not show the UI for non-English WordPress.com users.
if (
! empty( $user_data['user_locale'] )
&& ! in_array( $user_data['user_locale'], array( 'en', 'en-gb' ), true )
) {
$should_initialize = false;
}
// Make the API request.
$url = sprintf( '/sites/%d/blaze/status', $blog_id );
$response = Client::wpcom_json_api_request_as_blog(
$url,
'2',
array( 'method' => 'GET' ),
null,
'wpcom'
);

// The whole thing is also powered by Sync!
if ( ! Sync_Settings::is_sync_enabled() ) {
$should_initialize = false;
// Bail if there was an error or malformed response.
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}

// Only show the UI on WordPress.com Simple and WoA sites for now.
if (
! $is_wpcom
&& ! ( new Host() )->is_woa_site()
) {
$should_initialize = false;
// Decode the results.
$result = json_decode( wp_remote_retrieve_body( $response ), true );

// Bail if there were no results returned.
if ( ! is_array( $result ) || empty( $result['approved'] ) ) {
return false;
}

/*
* Do not show the UI on private sites
* nor on sites that have not been launched yet.
*/
if (
$status->is_private_site()
|| $status->is_coming_soon()
) {
$should_initialize = false;
return (bool) $result['approved'];
}

/**
* Determines if criteria is met to enable Blaze features.
*
* @return bool
*/
public static function should_initialize() {
$should_initialize = true;
$is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
$connection = new Jetpack_Connection();
$site_id = Jetpack_Connection::get_site_id();

// On self-hosted sites, we must do some additional checks.
if ( ! $is_wpcom ) {
/*
* These features currently only work on WordPress.com,
* so the site must be connected to WordPress.com, and the user as well for things to work.
*/
if (
is_wp_error( $site_id )
|| ! $connection->is_connected()
|| ! $connection->is_user_connected()
) {
$should_initialize = false;
}

// The whole thing is powered by Sync!
if ( ! Sync_Settings::is_sync_enabled() ) {
$should_initialize = false;
}

// The feature relies on this module for now.
// See 1386-gh-dotcom-forge
if ( ! ( new Modules() )->is_active( 'json-api' ) ) {
$should_initialize = false;
}
}

// The feature relies on this module for now.
// See 1386-gh-dotcom-forge
if ( ! $is_wpcom && ! ( new Modules() )->is_active( 'json-api' ) ) {
// Check if the site supports Blaze.
if ( is_numeric( $site_id ) && ! self::site_supports_blaze( $site_id ) ) {
$should_initialize = false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


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

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