Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
5 / 10
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tokens_Locks
50.00% covered (danger)
50.00%
5 / 10
25.00% covered (danger)
25.00%
1 / 4
8.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 aiowpm_set_lock
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 aiowpm_remove_lock
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 init_aiowpm
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * The Jetpack Connection Tokens Locks class file.
4 *
5 * @package automattic/jetpack-connection
6 */
7
8namespace Automattic\Jetpack\Connection;
9
10/**
11 *
12 * Jetpack Connection tokens cleanup during migration.
13 * This class encapsulates plugin or tool specific code that activates token lock upon migration.
14 *
15 * The connection tokens are locked to the current domain.
16 * If the database is imported on another site (domain name doesn't match), the tokens get removed.
17 *
18 * @see https://github.com/Automattic/jetpack/pull/23597
19 * @see \Automattic\Jetpack\Connection\Tokens::is_locked()
20 *
21 * @phan-constructor-used-for-side-effects
22 */
23class Tokens_Locks {
24
25    /**
26     * Whether the class has been initialized.
27     *
28     * @var bool
29     */
30    private static $is_initialized = false;
31
32    /**
33     * Run the initializers if they haven't been run already.
34     */
35    public function __construct() {
36        if ( static::$is_initialized ) {
37            return;
38        }
39
40        $this->init_aiowpm();
41
42        static::$is_initialized = true;
43    }
44
45    /**
46     * Set the token lock for AIOWPM plugin export.
47     *
48     * @param array $params The filter parameters.
49     *
50     * @return array
51     */
52    public function aiowpm_set_lock( $params ) {
53        ( new Tokens() )->set_lock();
54        return $params;
55    }
56
57    /**
58     * Remove the token lock for AIOWPM plugin export.
59     *
60     * @param array $params The filter parameters.
61     *
62     * @return array
63     */
64    public function aiowpm_remove_lock( $params ) {
65        ( new Tokens() )->remove_lock();
66        return $params;
67    }
68
69    /**
70     * Initialize the All-in-One-WP-Migration plugin hooks.
71     *
72     * @return void
73     */
74    private function init_aiowpm() {
75        add_filter( 'ai1wm_export', array( $this, 'aiowpm_set_lock' ), 180 );
76        add_filter( 'ai1wm_export', array( $this, 'aiowpm_remove_lock' ), 250 );
77    }
78}