| 1 |
<?php |
| 2 |
|
| 3 |
use Automattic\Jetpack\Schema\Schema; |
| 4 |
use Automattic\Jetpack\Schema\Schema_Parser; |
| 5 |
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Action; |
| 6 |
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Entry; |
| 7 |
use Automattic\Jetpack\WP_JS_Data_Sync\Data_Sync; |
| 8 |
use Automattic\Jetpack\WP_JS_Data_Sync\Data_Sync_Readonly; |
| 9 |
use Automattic\Jetpack_Boost\Data_Sync\Getting_Started_Entry; |
| 10 |
use Automattic\Jetpack_Boost\Data_Sync\Mergeable_Array_Entry; |
| 11 |
use Automattic\Jetpack_Boost\Lib\Connection; |
| 12 |
use Automattic\Jetpack_Boost\Lib\My_Jetpack; |
| 13 |
use Automattic\Jetpack_Boost\Lib\Premium_Features; |
| 14 |
use Automattic\Jetpack_Boost\Lib\Premium_Pricing; |
| 15 |
|
| 16 |
if ( ! defined( 'JETPACK_BOOST_DATASYNC_NAMESPACE' ) ) { |
| 17 |
define( 'JETPACK_BOOST_DATASYNC_NAMESPACE', 'jetpack_boost_ds' ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Make it easier to register a Jetpack Boost Data-Sync option. |
| 22 |
* |
| 23 |
* @param string $key - The key for this option. |
| 24 |
* @param \Automattic\Jetpack\Schema\Parser $parser - The schema for this option. |
| 25 |
* @param \Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get|null $entry - The entry handler for this option. |
| 26 |
*/ |
| 27 |
function jetpack_boost_register_option( $key, $parser, $entry = null ) { |
| 28 |
Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE ) |
| 29 |
->register( $key, $parser, $entry ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Register a new Jetpack Boost Data_Sync Action |
| 34 |
* |
| 35 |
* @param string $key |
| 36 |
* @param string $action_name |
| 37 |
* @param Schema_Parser $request_schema |
| 38 |
* @param Data_Sync_Action $instance |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
function jetpack_boost_register_action( $key, $action_name, $request_schema, $instance ) { |
| 43 |
Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE ) |
| 44 |
->register_action( $key, $action_name, $request_schema, $instance ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Make it easier to register a Jetpack Boost Read-only Data-Sync option. |
| 49 |
*/ |
| 50 |
function jetpack_boost_register_readonly_option( $key, $callback ) { |
| 51 |
jetpack_boost_register_option( $key, Schema::as_unsafe_any(), new Data_Sync_Readonly( $callback ) ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param string $key |
| 56 |
* |
| 57 |
* @return Data_Sync_Entry |
| 58 |
*/ |
| 59 |
function jetpack_boost_ds_entry( $key ) { |
| 60 |
return Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE ) |
| 61 |
->get_registry() |
| 62 |
->get_entry( $key ); |
| 63 |
} |
| 64 |
|
| 65 |
function jetpack_boost_ds_get( $key ) { |
| 66 |
$entry = jetpack_boost_ds_entry( $key ); |
| 67 |
if ( ! $entry ) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
return $entry->get(); |
| 71 |
} |
| 72 |
|
| 73 |
function jetpack_boost_ds_set( $key, $value ) { |
| 74 |
$entry = jetpack_boost_ds_entry( $key ); |
| 75 |
if ( ! $entry ) { |
| 76 |
return null; |
| 77 |
} |
| 78 |
return $entry->set( $value ); |
| 79 |
} |
| 80 |
|
| 81 |
function jetpack_boost_ds_delete( $key ) { |
| 82 |
$entry = jetpack_boost_ds_entry( $key ); |
| 83 |
if ( ! $entry ) { |
| 84 |
return null; |
| 85 |
} |
| 86 |
return $entry->delete(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Ensure that Async Options are passed to the relevant scripts. |
| 91 |
*/ |
| 92 |
function jetpack_boost_initialize_datasync() { |
| 93 |
$data_sync = Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE ); |
| 94 |
$data_sync->attach_to_plugin( 'jetpack-boost-admin', 'jetpack_page_jetpack-boost' ); |
| 95 |
} |
| 96 |
|
| 97 |
add_action( 'admin_init', 'jetpack_boost_initialize_datasync' ); |
| 98 |
|
| 99 |
/** |
| 100 |
* Entry to store alerts that shouldn't be shown again. |
| 101 |
*/ |
| 102 |
jetpack_boost_register_option( |
| 103 |
'dismissed_alerts', |
| 104 |
Schema::as_assoc_array( |
| 105 |
array( |
| 106 |
'legacy_minify_notice' => Schema::as_boolean(), |
| 107 |
'performance_history_fresh_start' => Schema::as_boolean(), |
| 108 |
'score_increase' => Schema::as_boolean(), |
| 109 |
'score_decrease' => Schema::as_boolean(), |
| 110 |
) |
| 111 |
)->fallback( |
| 112 |
array( |
| 113 |
'legacy_minify_notice' => false, |
| 114 |
'performance_history_fresh_start' => false, |
| 115 |
'score_increase' => false, |
| 116 |
'score_decrease' => false, |
| 117 |
) |
| 118 |
), |
| 119 |
new Mergeable_Array_Entry( JETPACK_BOOST_DATASYNC_NAMESPACE . '_dismissed_alerts' ) |
| 120 |
); |
| 121 |
|
| 122 |
jetpack_boost_register_readonly_option( 'connection', array( new Connection(), 'get_connection_api_response' ) ); |
| 123 |
jetpack_boost_register_readonly_option( 'pricing', array( Premium_Pricing::class, 'get_yearly_pricing' ) ); |
| 124 |
jetpack_boost_register_readonly_option( 'product', array( My_Jetpack::class, 'get_product' ) ); |
| 125 |
jetpack_boost_register_readonly_option( 'premium_features', array( Premium_Features::class, 'get_features' ) ); |
| 126 |
|
| 127 |
jetpack_boost_register_option( 'getting_started', Schema::as_boolean()->fallback( false ), new Getting_Started_Entry() ); |
| 128 |
|