Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
XMLRPC_Provider
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 xmlrpc_methods
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 get_blog
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 filter_esc_html_check_if_string
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The Stats XMLRPC Provider class
4 *
5 * @package automattic/jetpack-stats
6 */
7
8namespace Automattic\Jetpack\Stats;
9
10use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11use Automattic\Jetpack\Modules;
12
13/**
14 * Stats XMLRPC Provider.
15 *
16 * Adds additional methods to the WordPress XML-RPC API for handling Stats specific features.
17 *
18 * @since 0.1.0
19 */
20class XMLRPC_Provider {
21
22    /**
23     * Singleton XMLRPC_Provider instance.
24     *
25     * @var XMLRPC_Provider
26     **/
27    private static $instance = null;
28
29    /**
30     * Private XMLRPC_Provider constructor.
31     *
32     * Use the XMLRPC_Provider::init() method to get an instance.
33     */
34    private function __construct() {
35        add_filter( 'jetpack_xmlrpc_unauthenticated_methods', array( $this, 'xmlrpc_methods' ), 10, 3 );
36    }
37
38    /**
39     * Initialize class and get back a singleton instance.
40     *
41     * @param bool $new_instance Force create new instance.
42     *
43     * @return XMLRPC_Provider
44     */
45    public static function init( $new_instance = false ) {
46        if ( null === self::$instance || $new_instance ) {
47            self::$instance = new XMLRPC_Provider();
48        }
49
50        return self::$instance;
51    }
52
53    /**
54     * Adds additional methods to the WordPress xmlrpc API for handling Stats specific features.
55     *
56     * @param array $methods The Jetpack API methods.
57     *
58     * @return array
59     */
60    public function xmlrpc_methods( $methods ) {
61        if ( ! ( new Connection_Manager() )->is_connected() ) {
62            return $methods;
63        }
64
65        if ( ! ( new Modules() )->is_active( 'stats' ) ) {
66            return $methods;
67        }
68
69        $methods['jetpack.getBlog'] = array( $this, 'get_blog' );
70
71        return $methods;
72    }
73
74    /**
75     * Stats Get Blog.
76     *
77     * @return array
78     */
79    public function get_blog() {
80        $home = wp_parse_url( trailingslashit( get_option( 'home' ) ) );
81        $blog = array(
82            'host'                => $home['host'],
83            'path'                => $home['path'],
84            'blogname'            => get_option( 'blogname' ),
85            'blogdescription'     => get_option( 'blogdescription' ),
86            'siteurl'             => get_option( 'siteurl' ),
87            'gmt_offset'          => get_option( 'gmt_offset' ),
88            'timezone_string'     => get_option( 'timezone_string' ),
89            'stats_version'       => Main::STATS_VERSION,
90            'stats_api'           => 'jetpack',
91            'page_on_front'       => get_option( 'page_on_front' ),
92            'permalink_structure' => get_option( 'permalink_structure' ),
93            'category_base'       => get_option( 'category_base' ),
94            'tag_base'            => get_option( 'tag_base' ),
95        );
96        $blog = array_merge( Options::get_options(), $blog );
97        unset( $blog['roles'], $blog['blog_id'] );
98
99        add_filter( 'esc_html', array( $this, 'filter_esc_html_check_if_string' ), 10, 2 );
100        $blog = map_deep( $blog, 'esc_html' );
101        remove_filter( 'esc_html', array( $this, 'filter_esc_html_check_if_string' ) );
102
103        return $blog;
104    }
105
106    /**
107     * Make sure we are only escaping html if the input is a string.
108     * Used for `esc_html` filter-hook.
109     *
110     * @param  string $safe_text The output after esc_html has been applied.
111     * @param  mixed  $text      The initial input.
112     * @return mixed
113     */
114    public function filter_esc_html_check_if_string( $safe_text, $text ) {
115        if ( is_string( $text ) ) {
116            return $safe_text;
117        }
118
119        return $text;
120    }
121}