Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Knowledge | |
100.00% |
16 / 16 |
|
100.00% |
4 / 4 |
11 | |
100.00% |
1 / 1 |
| init | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| bootstrap | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
6 | |||
| has_provider | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| register_rest_routes | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Knowledge runtime provider selection. |
| 4 | * |
| 5 | * @package automattic/jetpack-knowledge |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack; |
| 9 | |
| 10 | use Automattic\Jetpack\Knowledge\Knowledge_Post_Type; |
| 11 | use Automattic\Jetpack\Knowledge\Knowledge_Scopes_REST_Controller; |
| 12 | |
| 13 | /** |
| 14 | * Provides Knowledge when Gutenberg's experiment is unavailable. |
| 15 | */ |
| 16 | class Knowledge { |
| 17 | const PACKAGE_VERSION = '0.1.0'; |
| 18 | |
| 19 | /** |
| 20 | * Whether this package has registered its fallback hooks. |
| 21 | * |
| 22 | * @var bool |
| 23 | */ |
| 24 | private static $initialized = false; |
| 25 | |
| 26 | /** |
| 27 | * Schedule provider resolution after all plugin files have loaded. |
| 28 | * |
| 29 | * @since 0.1.0 |
| 30 | * @return void |
| 31 | */ |
| 32 | public static function init() { |
| 33 | add_action( 'plugins_loaded', array( __CLASS__, 'bootstrap' ), 100 ); |
| 34 | add_action( 'init', array( __CLASS__, 'bootstrap' ), 0 ); |
| 35 | add_action( 'rest_api_init', array( __CLASS__, 'bootstrap' ), -100 ); |
| 36 | |
| 37 | if ( did_action( 'plugins_loaded' ) ) { |
| 38 | self::bootstrap(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Register the fallback only after giving Gutenberg the first choice. |
| 44 | * |
| 45 | * @since 0.1.0 |
| 46 | * @return void |
| 47 | */ |
| 48 | public static function bootstrap() { |
| 49 | if ( self::$initialized || ! did_action( 'plugins_loaded' ) || self::has_provider() ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | self::$initialized = true; |
| 54 | require_once __DIR__ . '/knowledge.php'; |
| 55 | |
| 56 | add_action( 'init', array( Knowledge_Post_Type::class, 'register' ) ); |
| 57 | add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ), 0 ); |
| 58 | |
| 59 | if ( did_action( 'init' ) || doing_action( 'rest_api_init' ) ) { |
| 60 | Knowledge_Post_Type::register(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Determine whether Gutenberg or another provider owns Knowledge. |
| 66 | * |
| 67 | * @return bool Whether a native provider is available. |
| 68 | */ |
| 69 | private static function has_provider() { |
| 70 | return post_type_exists( 'wp_knowledge' ) || function_exists( 'wp_knowledge_types' ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Register storage before REST routes, including early REST initialization. |
| 75 | * |
| 76 | * @return void |
| 77 | */ |
| 78 | public static function register_rest_routes() { |
| 79 | Knowledge_Post_Type::register(); |
| 80 | ( new Knowledge_Scopes_REST_Controller() )->register_routes(); |
| 81 | } |
| 82 | } |