Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 6
132
0.00% covered (danger)
0.00%
0 / 1
 register_hooks
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 connected
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 module_enabled
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 module_disabled
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 publicize
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 detect_plugin_activation
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace NewfoldLabs\WP\Module\Data\Listeners;
4
5use function NewfoldLabs\WP\ModuleLoader\container;
6
7/**
8 * Monitors Jetpack events
9 */
10class Jetpack extends Listener {
11
12    /**
13     * Brand code constants
14     *
15     * @var brand_code
16     */
17    private $brand_code = array(
18        'bluehost'        => '86241',
19        'hostgator'       => '57686',
20        'web'             => '86239',
21        'crazy-domains'   => '57687',
22        'hostgator-india' => '57686',
23        'bluehost-india'  => '86241',
24        'hostgator-latam' => '57686',
25        'default'         => '86240',
26    );
27
28    /**
29     * Register the hooks for the listener
30     *
31     * @return void
32     */
33    public function register_hooks() {
34        // Connected
35        add_action( 'jetpack_site_registered', array( $this, 'connected' ), 10, 3 );
36
37        // Module enabled/disabled
38        add_action( 'jetpack_pre_activate_module', array( $this, 'module_enabled' ) );
39        add_action( 'jetpack_pre_deactivate_module', array( $this, 'module_disabled' ) );
40        add_action( 'activated_plugin', array( $this, 'detect_plugin_activation' ), 10, 1 );
41        // Publicize
42        add_action( 'publicize_save_meta', array( $this, 'publicize' ), 10, 4 );
43    }
44
45    /**
46     * Jetpack connected
47     *
48     * @param integer        $id Jetpack Site ID
49     * @param string         $secret Jetpack blog token
50     * @param integer|boolan $is_public Whether the site is public
51     * @return void
52     */
53    public function connected( $id, $secret, $is_public ) {
54        $this->push(
55            'jetpack_connected',
56            array(
57                'id'     => $id,
58                'public' => $is_public,
59            )
60        );
61    }
62
63    /**
64     * Jetpack module enabled
65     *
66     * @param string $module Name of the module
67     * @return void
68     */
69    public function module_enabled( $module ) {
70        $this->push(
71            'jetpack_module_enabled',
72            array(
73                'label_key' => 'module',
74                'module'    => $module,
75            )
76        );
77    }
78
79    /**
80     * Jetpack module disabled
81     *
82     * @param string $module Name of the module
83     * @return void
84     */
85    public function module_disabled( $module ) {
86        $this->push(
87            'jetpack_module_disabled',
88            array(
89                'label_key' => 'module',
90                'module'    => $module,
91            )
92        );
93    }
94
95    /**
96     * Post publicized
97     *
98     * @param bool    $submit_post Whether to submit the post
99     * @param integer $post_id ID of the post being publicized
100     * @param string  $service_name Service name
101     * @param array   $connection Array of connection details
102     * @return void
103     */
104    public function publicize( $submit_post, $post_id, $service_name, $connection ) {
105        // Bail if it's not being publicized
106        if ( ! $submit_post ) {
107            return;
108        }
109        $this->push(
110            'jetpack_publicized',
111            array(
112                'label_key' => 'service',
113                'service'   => $service_name,
114            )
115        );
116    }
117
118    /**
119     * Post publicized
120     *
121     * @param bool $plugin Plugin information
122     * @return void
123     */
124    public function detect_plugin_activation( $plugin ) {
125        $container = container();
126        if ( 'jetpack/jetpack.php' === $plugin ) {
127            $brand = $container->plugin()->brand;
128            if ( empty( $brand ) || ! array_key_exists( $brand, $this->brand_code ) ) {
129                    $brand = 'default';
130            }
131            $jetpack_affiliate_code = get_option( 'jetpack_affiliate_code' );
132            ! $jetpack_affiliate_code &&
133                                            update_option( 'jetpack_affiliate_code', $this->brand_code[ $brand ] );
134        }
135    }
136}