Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
12 / 15
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Plugin
80.00% covered (warning)
80.00%
12 / 15
60.00% covered (warning)
60.00%
3 / 5
9.65
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
 get_slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 remove
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 is_only
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
5.58
1<?php
2/**
3 * Plugin connection management class.
4 *
5 * @package automattic/jetpack-connection
6 */
7
8namespace Automattic\Jetpack\Connection;
9
10/**
11 * Plugin connection management class.
12 * The class represents a single plugin that uses Jetpack connection.
13 * Its functionality has been pretty simplistic so far: add to the storage (`Plugin_Storage`), remove it from there,
14 * and determine whether it's the last active connection. As the component grows, there'll be more functionality added.
15 */
16class Plugin {
17
18    /**
19     * List of the keys allowed as arguments
20     *
21     * @var array
22     */
23    private $arguments_whitelist = array(
24        'url_info',
25    );
26
27    /**
28     * Plugin slug.
29     *
30     * @var string
31     */
32    private $slug;
33
34    /**
35     * Users Connection Admin instance.
36     *
37     * @var Users_Connection_Admin
38     */
39    private $users_connection_admin;
40
41    /**
42     * Initialize the plugin manager.
43     *
44     * @param string $slug Plugin slug.
45     */
46    public function __construct( $slug ) {
47        $this->slug = $slug;
48
49        // Initialize Users_Connection_Admin
50        $this->users_connection_admin = new Users_Connection_Admin();
51    }
52
53    /**
54     * Get the plugin slug.
55     *
56     * @return string
57     */
58    public function get_slug() {
59        return $this->slug;
60    }
61
62    /**
63     * Add the plugin connection info into Jetpack.
64     *
65     * @param string $name Plugin name, required.
66     * @param array  $args Plugin arguments, optional.
67     *
68     * @return $this
69     * @see $this->arguments_whitelist
70     */
71    public function add( $name, array $args = array() ) {
72        $args = compact( 'name' ) + array_intersect_key( $args, array_flip( $this->arguments_whitelist ) );
73
74        Plugin_Storage::upsert( $this->slug, $args );
75
76        return $this;
77    }
78
79    /**
80     * Remove the plugin connection info from Jetpack.
81     *
82     * @return $this
83     */
84    public function remove() {
85        Plugin_Storage::delete( $this->slug );
86
87        return $this;
88    }
89
90    /**
91     * Determine if this plugin connection is the only one active at the moment, if any.
92     *
93     * @return bool
94     */
95    public function is_only() {
96        $plugins = Plugin_Storage::get_all();
97
98        if ( is_wp_error( $plugins ) ) {
99            if ( 'too_early' === $plugins->get_error_code() ) {
100                _doing_it_wrong( __METHOD__, esc_html( $plugins->get_error_code() . ': ' . $plugins->get_error_message() ), '6.16.1' );
101            } else {
102                wp_trigger_error( __METHOD__, $plugins->get_error_code() . ': ' . $plugins->get_error_message() );
103            }
104            return false;
105        }
106
107        return ! $plugins || ( array_key_exists( $this->slug, $plugins ) && 1 === count( $plugins ) );
108    }
109}