Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Registry
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 10
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_instance
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 sanitize_option_name
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 sanitize_http_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 all
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_endpoint
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_option
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_namespace
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_namespace_http
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Automattic\Jetpack\Packages\Async_Option;
4
5class Registry {
6
7    /**
8     * @var Registry[]
9     */
10    private static $instance = array();
11
12    /**
13     * @var Async_Option[]
14     */
15    private $options = array();
16
17    /**
18     * @var Endpoint[]
19     */
20    private $endpoints = array();
21
22    /**
23     * @var string
24     */
25    private $namespace;
26
27    private function __construct( $namespace ) {
28        $this->namespace = $namespace;
29    }
30
31    public static function get_instance( $namespace ) {
32        if ( ! isset( static::$instance[ $namespace ] ) ) {
33            static::$instance[ $namespace ] = new static( $namespace );
34        }
35
36        return static::$instance[ $namespace ];
37    }
38
39    public function sanitize_option_name( $key ) {
40        $sanitized_key = sanitize_key( $key );
41        $sanitized_key = str_replace( '-', '_', $sanitized_key );
42        if ( defined( 'WP_DEBUG' ) && WP_DEBUG && $sanitized_key !== $key ) {
43            throw new \Exception( "Invalid key '$key'. Keys should only include alphanumeric characters and underscores." );
44        }
45        return $sanitized_key;
46    }
47
48    public function sanitize_http_name( $key ) {
49        return str_replace( '_', '-', sanitize_key( $key ) );
50    }
51
52    /**
53     * Register an option using an Async Option Template.
54     *
55     * @param $key       string
56     * @param $template  Async_Option_Template
57     *
58     * @return Async_Option
59     * @throws \Exception
60     */
61    public function register( $key, $template ) {
62
63        $key = $this->sanitize_option_name( $key );
64
65        $option                = new Async_Option( $this->namespace, $key, $template );
66        $this->options[ $key ] = $option;
67
68        $endpoint                = new Endpoint( $this->get_namespace_http(), $this->sanitize_http_name( $option->key() ), $option );
69        $this->endpoints[ $key ] = $endpoint;
70
71        add_action( 'rest_api_init', array( $endpoint, 'register_rest_route' ) );
72
73        return $option;
74    }
75
76    public function all() {
77        return $this->options;
78    }
79
80    public function get_endpoint( $key ) {
81        if ( ! isset( $this->endpoints[ $key ] ) ) {
82            return false;
83        }
84        return $this->endpoints[ $key ];
85    }
86
87    public function get_option( $key ) {
88        if ( ! isset( $this->options[ $key ] ) ) {
89            return false;
90        }
91        return $this->options[ $key ];
92    }
93
94    public function get_namespace() {
95        return $this->namespace;
96    }
97
98    public function get_namespace_http() {
99        return $this->sanitize_http_name( $this->namespace );
100    }
101}