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
6 changes: 4 additions & 2 deletions 3rd-party/domain-mapping.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

/**
* Class Jetpack_3rd_Party_Domain_Mapping
*
Expand Down Expand Up @@ -39,7 +41,7 @@ private function __construct() {
* to try and hook a domain mapping plugin to the Jetpack sync filters for the home_url and site_url callables.
*/
function attempt_to_hook_domain_mapping_plugins() {
if ( ! Jetpack_Constants::is_defined( 'SUNRISE' ) ) {
if ( ! Constants_Manager::is_defined( 'SUNRISE' ) ) {
return;
}

Expand All @@ -58,7 +60,7 @@ function attempt_to_hook_domain_mapping_plugins() {
* @return bool
*/
function hook_wordpress_mu_domain_mapping() {
if ( ! Jetpack_Constants::is_defined( 'SUNRISE_LOADED' ) || ! $this->function_exists( 'domain_mapping_siteurl' ) ) {
if ( ! Constants_Manager::is_defined( 'SUNRISE_LOADED' ) || ! $this->function_exists( 'domain_mapping_siteurl' ) ) {
return false;
}

Expand Down
6 changes: 4 additions & 2 deletions _inc/lib/core-api/wpcom-endpoints/subscribers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

/**
* Subscribers: Get subscriber count
*
Expand Down Expand Up @@ -41,7 +43,7 @@ public function readable_permission_check() {
*/
public function get_subscriber_count( $request ) {
// Get the most up to date subscriber count when request is not a test
if ( ! Jetpack_Constants::is_defined( 'TESTING_IN_JETPACK' ) ) {
if ( ! Constants_Manager::is_defined( 'TESTING_IN_JETPACK' ) ) {
delete_transient( 'wpcom_subscribers_total' );
}

Expand All @@ -56,7 +58,7 @@ public function get_subscriber_count( $request ) {

if (
Jetpack::is_module_active( 'subscriptions' ) ||
( Jetpack_Constants::is_defined( 'TESTING_IN_JETPACK' ) && Jetpack_Constants::get_constant( 'TESTING_IN_JETPACK' ) )
( Constants_Manager::is_defined( 'TESTING_IN_JETPACK' ) && Constants_Manager::get_constant( 'TESTING_IN_JETPACK' ) )
) {
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Subscribers' );
}
6 changes: 4 additions & 2 deletions _inc/lib/debugger/class-jetpack-debug-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package jetpack
*/

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

/**
* Class Jetpack_Debug_Data
*
Expand Down Expand Up @@ -323,8 +325,8 @@ public static function debug_data() {
$idc_urls = array(
'home' => Jetpack_Sync_Functions::home_url(),
'siteurl' => Jetpack_Sync_Functions::site_url(),
'WP_HOME' => Jetpack_Constants::is_defined( 'WP_HOME' ) ? Jetpack_Constants::get_constant( 'WP_HOME' ) : '',
'WP_SITEURL' => Jetpack_Constants::is_defined( 'WP_SITEURL' ) ? Jetpack_Constants::get_constant( 'WP_SITEURL' ) : '',
'WP_HOME' => Constants_Manager::is_defined( 'WP_HOME' ) ? Constants_Manager::get_constant( 'WP_HOME' ) : '',
'WP_SITEURL' => Constants_Manager::is_defined( 'WP_SITEURL' ) ? Constants_Manager::get_constant( 'WP_SITEURL' ) : '',
);

$debug_info['idc_urls'] = array(
Expand Down
6 changes: 4 additions & 2 deletions class.jetpack-data.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

class Jetpack_Data {
/*
* Used internally when we want to look for the Normal Blog Token
Expand Down Expand Up @@ -82,8 +84,8 @@ public static function get_access_token( $user_id = false, $token_key = false )
$possible_normal_tokens[] = $stored_blog_token;
}

$defined_tokens = Jetpack_Constants::is_defined( 'JETPACK_BLOG_TOKEN' )
? explode( ',', Jetpack_Constants::get_constant( 'JETPACK_BLOG_TOKEN' ) )
$defined_tokens = Constants_Manager::is_defined( 'JETPACK_BLOG_TOKEN' )
? explode( ',', Constants_Manager::get_constant( 'JETPACK_BLOG_TOKEN' ) )
: array();

foreach ( $defined_tokens as $defined_token ) {
Expand Down
8 changes: 5 additions & 3 deletions class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @package Jetpack
*/

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

/**
* Wrapper function to safely register a gutenberg block type
*
Expand Down Expand Up @@ -220,7 +222,7 @@ public static function init() {
* @param boolean
*/
if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) {
Jetpack_Constants::set_constant( 'JETPACK_BETA_BLOCKS', true );
Constants_Manager::set_constant( 'JETPACK_BETA_BLOCKS', true );
}

/**
Expand Down Expand Up @@ -318,7 +320,7 @@ public static function get_jetpack_gutenberg_extensions_whitelist() {

$preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array();

if ( Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
if ( Constants_Manager::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
$beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array();
return array_unique( array_merge( $preset_extensions, $beta_extensions ) );
}
Expand Down Expand Up @@ -548,7 +550,7 @@ public static function enqueue_block_editor_assets() {
}

$rtl = is_rtl() ? '.rtl' : '';
$beta = Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : '';
$beta = Constants_Manager::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : '';
$blocks_dir = self::get_blocks_directory();

$editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE );
Expand Down
3 changes: 2 additions & 1 deletion class.jetpack-options.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;
use \Automattic\Jetpack\Options\Manager as Options_Manager;

if ( class_exists( '\Automattic\Jetpack\Options\Manager' ) ) :
Expand Down Expand Up @@ -480,7 +481,7 @@ static function get_raw_option( $name, $default = null ) {
*/
static function bypass_raw_option( $name ) {

if ( Jetpack_Constants::get_constant( 'JETPACK_DISABLE_RAW_OPTIONS' ) ) {
if ( Constants_Manager::get_constant( 'JETPACK_DISABLE_RAW_OPTIONS' ) ) {
return true;
}
/**
Expand Down
12 changes: 7 additions & 5 deletions class.jetpack.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

/*
Options:
jetpack_options (array)
Expand Down Expand Up @@ -1700,7 +1702,7 @@ public static function is_development_version() {
*/
return (bool) apply_filters(
'jetpack_development_version',
! preg_match( '/^\d+(\.\d+)+$/', Jetpack_Constants::get_constant( 'JETPACK__VERSION' ) )
! preg_match( '/^\d+(\.\d+)+$/', Constants_Manager::get_constant( 'JETPACK__VERSION' ) )
);
}

Expand Down Expand Up @@ -6163,10 +6165,10 @@ public static function get_sync_error_idc_option( $response = array() ) {
* @return bool
*/
public static function sync_idc_optin() {
if ( Jetpack_Constants::is_defined( 'JETPACK_SYNC_IDC_OPTIN' ) ) {
$default = Jetpack_Constants::get_constant( 'JETPACK_SYNC_IDC_OPTIN' );
if ( Constants_Manager::is_defined( 'JETPACK_SYNC_IDC_OPTIN' ) ) {
$default = Constants_Manager::get_constant( 'JETPACK_SYNC_IDC_OPTIN' );
} else {
$default = ! Jetpack_Constants::is_defined( 'SUNRISE' ) && ! is_multisite();
$default = ! Constants_Manager::is_defined( 'SUNRISE' ) && ! is_multisite();
}

/**
Expand Down Expand Up @@ -6959,7 +6961,7 @@ public static function is_function_in_backtrace( $names ) {
* @return string The URL to the file
*/
public static function get_file_url_for_environment( $min_path, $non_min_path ) {
$path = ( Jetpack_Constants::is_defined( 'SCRIPT_DEBUG' ) && Jetpack_Constants::get_constant( 'SCRIPT_DEBUG' ) )
$path = ( Constants_Manager::is_defined( 'SCRIPT_DEBUG' ) && Constants_Manager::get_constant( 'SCRIPT_DEBUG' ) )
? $non_min_path
: $min_path;

Expand Down
8 changes: 4 additions & 4 deletions composer.lock

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

/**
* Base class for working with plugins.
*/
Expand Down Expand Up @@ -205,12 +207,12 @@ protected function get_file_mod_capabilities() {
$reasons_can_not_modify_files['has_no_file_system_write_access'] = __( 'The file permissions on this host prevent editing files.', 'jetpack' );
}

$disallow_file_mods = Jetpack_Constants::get_constant('DISALLOW_FILE_MODS' );
$disallow_file_mods = Constants_Manager::get_constant('DISALLOW_FILE_MODS' );
if ( $disallow_file_mods ) {
$reasons_can_not_modify_files['disallow_file_mods'] = __( 'File modifications are explicitly disabled by a site administrator.', 'jetpack' );
}

$automatic_updater_disabled = Jetpack_Constants::get_constant( 'AUTOMATIC_UPDATER_DISABLED' );
$automatic_updater_disabled = Constants_Manager::get_constant( 'AUTOMATIC_UPDATER_DISABLED' );
if ( $automatic_updater_disabled ) {
$reasons_can_not_autoupdate['automatic_updater_disabled'] = __( 'Any autoupdates are explicitly disabled by a site administrator.', 'jetpack' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

new Jetpack_JSON_API_Plugins_Modify_Endpoint(
array(
'description' => 'Activate/Deactivate a Plugin on your Jetpack Site, or set automatic updates',
Expand Down Expand Up @@ -291,7 +294,7 @@ protected function deactivate() {
protected function update() {
$query_args = $this->query_args();
if ( isset( $query_args['autoupdate'] ) && $query_args['autoupdate'] ) {
Jetpack_Constants::set_constant( 'JETPACK_PLUGIN_AUTOUPDATE', true );
Constants_Manager::set_constant( 'JETPACK_PLUGIN_AUTOUPDATE', true );
}
wp_clean_plugins_cache();
ob_start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

class Jetpack_JSON_API_User_Create_Endpoint extends Jetpack_JSON_API_Endpoint {

protected $needed_capabilities = 'create_users';
Expand Down Expand Up @@ -34,7 +36,7 @@ function create_or_get_user() {

$query_args = $this->query_args();
if ( isset( $query_args['invite_accepted'] ) && $query_args['invite_accepted'] ) {
Jetpack_Constants::set_constant( 'JETPACK_INVITE_ACCEPTED', true );
Constants_Manager::set_constant( 'JETPACK_INVITE_ACCEPTED', true );
}

if ( ! $user ) {
Expand Down
4 changes: 3 additions & 1 deletion modules/protect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Additional Search Queries: security, jetpack protect, secure, protection, botnet, brute force, protect, login, bot, password, passwords, strong passwords, strong password, wp-login.php, protect admin
*/

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

include_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php';

class Jetpack_Protect_Module {
Expand Down Expand Up @@ -438,7 +440,7 @@ function check_login_ability( $preauth = false ) {
/**
* JETPACK_ALWAYS_PROTECT_LOGIN will always disable the login page, and use a page provided by Jetpack.
*/
if ( Jetpack_Constants::is_true( 'JETPACK_ALWAYS_PROTECT_LOGIN' ) ) {
if ( Constants_Manager::is_true( 'JETPACK_ALWAYS_PROTECT_LOGIN' ) ) {
$this->kill_login();
}

Expand Down
6 changes: 4 additions & 2 deletions modules/search/class.jetpack-search-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @since 5.8.0
*/

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

/**
* Various helper functions for reuse throughout the Jetpack Search code.
*
Expand Down Expand Up @@ -659,8 +661,8 @@ public static function are_filters_by_widget_disabled() {
*/
public static function site_has_vip_index() {
$has_vip_index = (
Jetpack_Constants::is_defined( 'JETPACK_SEARCH_VIP_INDEX' ) &&
Jetpack_Constants::get_constant( 'JETPACK_SEARCH_VIP_INDEX' )
Constants_Manager::is_defined( 'JETPACK_SEARCH_VIP_INDEX' ) &&
Constants_Manager::get_constant( 'JETPACK_SEARCH_VIP_INDEX' )
);

/**
Expand Down
4 changes: 3 additions & 1 deletion modules/shortcodes/crowdsignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @package Jetpack
*/

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

// Keep compatibility with the PollDaddy plugin.
if (
! class_exists( 'CrowdsignalShortcode' )
Expand Down Expand Up @@ -213,7 +215,7 @@ public function crowdsignal_shortcode( $atts ) {
);

$inline = ! in_the_loop()
&& ! Jetpack_Constants::is_defined( 'TESTING_IN_JETPACK' );
&& ! Constants_Manager::is_defined( 'TESTING_IN_JETPACK' );

$no_script = false;
$infinite_scroll = false;
Expand Down
4 changes: 3 additions & 1 deletion modules/theme-tools/featured-content.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Automattic\Jetpack\Constants\Manager as Constants_Manager;

if ( ! class_exists( 'Featured_Content' ) && isset( $GLOBALS['pagenow'] ) && 'plugins.php' !== $GLOBALS['pagenow'] ) {

/**
Expand Down Expand Up @@ -714,7 +716,7 @@ function wpcom_rest_request_before_callbacks( $request ) {
return $request;
}

if ( Jetpack_Constants::is_true( 'IS_WPCOM' ) && Jetpack_Constants::is_true( 'REST_API_REQUEST' ) ) {
if ( Constants_Manager::is_true( 'IS_WPCOM' ) && Constants_Manager::is_true( 'REST_API_REQUEST' ) ) {
add_filter( 'rest_request_before_callbacks', 'wpcom_rest_request_before_callbacks');
}

Expand Down
56 changes: 56 additions & 0 deletions packages/constants/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Jetpack Constants

A simple constant manager for Jetpack.

Testing constants is hard. Once you define a constant in PHP, it's defined. Constants Manager is an abstraction layer so that unit tests can set constants for tests.

### Usage

Retrieve the value of a constant `CONSTANT_NAME` (returns `null` if it's not defined):

```php
use Automattic\Jetpack\Constants\Manager as Constants_Manager;

$constant_value = Constants_Manager::get_constant( 'CONSTANT_NAME' );
```

Set the value of a constant `CONSTANT_NAME` to a particular value:

```php
use Automattic\Jetpack\Constants\Manager as Constants_Manager;

$value = 'some value';
Constants_Manager::set_constant( 'CONSTANT_NAME', $value );
```

Check whether a constant `CONSTANT_NAME` is defined:

```php
use Automattic\Jetpack\Constants\Manager as Constants_Manager;

$defined = Constants_Manager::is_defined( 'CONSTANT_NAME' );
```

Check whether a constant `CONSTANT_NAME` is truthy:

```php
use Automattic\Jetpack\Constants\Manager as Constants_Manager;

$is_truthy = Constants_Manager::is_true( 'CONSTANT_NAME' );
```

Delete the `CONSTANT_NAME` constant:

```php
use Automattic\Jetpack\Constants\Manager as Constants_Manager;

Constants_Manager::clear_single_constant( 'CONSTANT_NAME' );
```

Delete all known constants:

```php
use Automattic\Jetpack\Constants\Manager as Constants_Manager;

Constants_Manager::clear_constants();
```
Loading