Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
64.71% covered (warning)
64.71%
11 / 17
33.33% covered (danger)
33.33%
1 / 3
CRAP
n/a
0 / 0
wpcomsh_init_connection_handlers
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
wpcomsh_init_external_storage
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
4.02
wpcomsh_init_protected_owner_handler
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Jetpack Connection handlers for wpcomsh.
4 *
5 * Initialization for:
6 * - External Storage (Atomic Persistent Data)
7 * - Protected Owner Error Handler
8 *
9 * @package wpcomsh
10 */
11
12// Load required classes. wpcomsh_init_external_storage() loads the storage provider.
13require_once __DIR__ . '/class-protected-owner-error-handler.php';
14
15/**
16 * Initialize Jetpack Connection handlers.
17 */
18function wpcomsh_init_connection_handlers() {
19    /*
20     * Register the provider now, because connection versions before 8.3.0 never fire
21     * `jetpack_external_storage_init`. When the classes cannot load yet, register on that action,
22     * which fires on the first read. Load this file before code that reads the connection options.
23     */
24    if ( ! wpcomsh_init_external_storage() ) {
25        add_action( 'jetpack_external_storage_init', 'wpcomsh_init_external_storage' );
26    }
27
28    wpcomsh_init_protected_owner_handler();
29}
30
31/**
32 * Initialize external storage provider for Jetpack connection data.
33 *
34 * @return bool Whether the provider was registered.
35 */
36function wpcomsh_init_external_storage() {
37    /**
38     * Filters whether to register the Atomic external storage provider.
39     *
40     * The default skips it during PHPUnit tests to avoid logging. Tests of the registration turn it on.
41     *
42     * @param bool $register Whether to register the provider.
43     */
44    if ( ! apply_filters( 'wpcomsh_register_external_storage_provider', ! defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) ) {
45        return false;
46    }
47
48    if ( ! class_exists( 'Automattic\Jetpack\Connection\External_Storage' )
49        || ! interface_exists( 'Automattic\Jetpack\Connection\Storage_Provider_Interface' ) ) {
50        return false;
51    }
52
53    // Load the provider only now: it declares its class only when the interface exists.
54    require_once __DIR__ . '/class-atomic-storage-provider.php';
55
56    return \Automattic\Jetpack\Connection\External_Storage::register_provider(
57        new Atomic_Storage_Provider()
58    );
59}
60
61/**
62 * Initialize Protected Owner Error Handler.
63 */
64function wpcomsh_init_protected_owner_handler() {
65    // Only initialize if both the handler class and Jetpack Error_Handler are available
66    if ( class_exists( 'Automattic\WPComSH\Connection\Protected_Owner_Error_Handler' ) &&
67        class_exists( 'Automattic\Jetpack\Connection\Error_Handler' ) ) {
68        // Initialize the Protected Owner Error Handler singleton
69        \Automattic\WPComSH\Connection\Protected_Owner_Error_Handler::get_instance();
70    }
71}
72
73wpcomsh_init_connection_handlers();