Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Async_Options
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 _print_options_script_tag
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 add_to_plugin_page
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setup
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Automattic\Jetpack\Packages\Async_Option;
4
5class Async_Options {
6    /**
7     * @var Registry
8     */
9    protected $registry;
10
11    /**
12     * @var string Script Handle name to pass the variables to.
13     */
14    protected $script_handle;
15
16    public function __construct( $script_handle, Registry $registry ) {
17        $this->script_handle = $script_handle;
18        $this->registry      = $registry;
19    }
20
21    /**
22     * Don't call this method directly.
23     * It's only public so that it can be called as a hook
24     *
25     * @return void
26     */
27    public function _print_options_script_tag() { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
28        $data = array(
29            'rest_api' => array(
30                'value' => rest_url( $this->registry->get_namespace_http() ),
31                'nonce' => wp_create_nonce( 'wp_rest' ),
32            ),
33        );
34        foreach ( $this->registry->all() as $option ) {
35            $data[ $option->key() ] = array(
36                'value' => $option->get(),
37                'nonce' => $this->registry->get_endpoint( $option->key() )->create_nonce(),
38            );
39        }
40
41        wp_localize_script( $this->script_handle, $this->registry->get_namespace(), $data );
42    }
43
44    /**
45     * Tell WordPress to print script tags in the specified plugin page
46     *
47     * @param string $plugin_page The slug name of the plugin page.
48     * @param string $parent_page The slug name for the parent menu (or the file name of a standard
49     *                            WordPress admin page).
50     *
51     * @return void
52     */
53    public function add_to_plugin_page( $plugin_page, $parent_page ) {
54        $plugin_page_hook = get_plugin_page_hook( $plugin_page, $parent_page );
55        add_action( $plugin_page_hook, array( $this, '_print_options_script_tag' ) );
56    }
57
58    public static function setup( $registry_name, $script_handle, $plugin_page = null, $parent_page = 'admin' ) {
59
60        $registry = Registry::get_instance( $registry_name );
61
62        /**
63         * The plugin page slug can be anything, but makes setup easier to read by making assumptions.
64         * This assumes that the plugin page string is going to match the registry namespace,
65         * formatted as a http parameter. (kebab case)
66         *
67         * Example:
68         * Registry with namespace:  `jetpack_boost` should be
69         * automatically attached to `admin.php?page=jetpack-boost`
70         */
71        if ( $plugin_page === null ) {
72            $plugin_page = $registry->get_namespace_http();
73        }
74
75        $instance = new self( $script_handle, $registry );
76        $instance->add_to_plugin_page( $plugin_page, $parent_page );
77    }
78}