Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
26.09% covered (danger)
26.09%
6 / 23
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Async_Options
26.09% covered (danger)
26.09%
6 / 23
50.00% covered (danger)
50.00%
2 / 4
26.79
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 _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
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 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
56        // On admin-ajax.php and other requests where the admin menu is not built, get_plugin_page_hook() returns null.
57        if ( ! $plugin_page_hook ) {
58            return;
59        }
60
61        add_action( $plugin_page_hook, array( $this, '_print_options_script_tag' ) );
62    }
63
64    public static function setup( $registry_name, $script_handle, $plugin_page = null, $parent_page = 'admin' ) {
65
66        $registry = Registry::get_instance( $registry_name );
67
68        /**
69         * The plugin page slug can be anything, but makes setup easier to read by making assumptions.
70         * This assumes that the plugin page string is going to match the registry namespace,
71         * formatted as a http parameter. (kebab case)
72         *
73         * Example:
74         * Registry with namespace:  `jetpack_boost` should be
75         * automatically attached to `admin.php?page=jetpack-boost`
76         */
77        if ( $plugin_page === null ) {
78            $plugin_page = $registry->get_namespace_http();
79        }
80
81        $instance = new self( $script_handle, $registry );
82        $instance->add_to_plugin_page( $plugin_page, $parent_page );
83    }
84}