Completed
Push — branch-4.4 ( b40dc3...78a28d )
by
unknown
15:53 queued 07:15
created

check_plan_deactivate_modules()   C

Complexity

Conditions 13
Paths 15

Size

Total Lines 37
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 30
nc 15
nop 1
dl 0
loc 37
rs 5.1234
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// Shared logic between Jetpack admin pages
4
abstract class Jetpack_Admin_Page {
5
	// Add page specific actions given the page hook
6
	abstract function add_page_actions( $hook );
7
8
	// Create a menu item for the page and returns the hook
9
	abstract function get_page_hook();
10
11
	// Enqueue and localize page specific scripts
12
	abstract function page_admin_scripts();
13
14
	// Render page specific HTML
15
	abstract function page_render();
16
17
	/**
18
	 * Should we block the page rendering because the site is in IDC?
19
	 * @var bool
20
	 */
21
	static $block_page_rendering_for_idc;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $block_page_rendering_for_idc.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
22
23
	/**
24
	 * Function called after admin_styles to load any additional needed styles.
25
	 *
26
	 * @since 4.3.0
27
	 */
28
	function additional_styles() {}
29
30
	function __construct() {
31
		$this->jetpack = Jetpack::init();
0 ignored issues
show
Bug introduced by
The property jetpack does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
		self::$block_page_rendering_for_idc = (
33
			Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' )
34
		);
35
36
		if ( ! self::$block_page_rendering_for_idc ) {
37
			add_action( 'admin_enqueue_scripts', array( $this, 'additional_styles' ) );
38
		}
39
	}
40
41
	function add_actions() {
42
43
		// If user is not an admin and site is in Dev Mode, don't do anything
44
		if ( ! current_user_can( 'manage_options' ) && Jetpack::is_development_mode() ) {
45
			return;
46
		}
47
48
		// Don't add in the modules page unless modules are available!
49
		if ( $this->dont_show_if_not_active && ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
0 ignored issues
show
Bug introduced by
The property dont_show_if_not_active does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
			return;
51
		}
52
53
		// Initialize menu item for the page in the admin
54
		$hook = $this->get_page_hook();
55
56
		// Attach hooks common to all Jetpack admin pages based on the created
57
		// hook
58
		add_action( "load-$hook",                array( $this, 'admin_help'      ) );
59
		add_action( "load-$hook",                array( $this, 'admin_page_load' ) );
60
		add_action( "admin_head-$hook",          array( $this, 'admin_head'      ) );
61
62
		add_action( "admin_print_styles-$hook",  array( $this, 'admin_styles'    ) );
63
		add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts'   ) );
64
65
		// Attach page specific actions in addition to the above
66
		$this->add_page_actions( $hook );
67
	}
68
69
	function admin_head() {
70 View Code Duplication
		if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) {
71
			/**
72
			 * Fires in the <head> of a particular Jetpack configuation page.
73
			 *
74
			 * The dynamic portion of the hook name, `$_GET['configure']`,
75
			 * refers to the slug of module, such as 'stats', 'sso', etc.
76
			 * A complete hook for the latter would be
77
			 * 'jetpack_module_configuation_head_sso'.
78
			 *
79
			 * @since 3.0.0
80
			 */
81
			do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] );
82
		}
83
	}
84
85
	// Render the page with a common top and bottom part, and page specific content
86
	function render() {
87
		// We're in an IDC: we need a decision made before we show the UI again.
88
		if ( self::$block_page_rendering_for_idc ) {
89
			return;
90
		}
91
92
		$this->page_render();
93
	}
94
95
	function admin_help() {
96
		$this->jetpack->admin_help();
97
	}
98
99
	function admin_page_load() {
100
		// This is big.  For the moment, just call the existing one.
101
		$this->jetpack->admin_page_load();
102
	}
103
104
	function admin_page_top() {
105
		include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
106
	}
107
108
	function admin_page_bottom() {
109
		include_once( JETPACK__PLUGIN_DIR . '_inc/footer.php' );
110
	}
111
112
	// Add page specific scripts and jetpack stats for all menu pages
113
	function admin_scripts() {
114
		$this->page_admin_scripts(); // Delegate to inheriting class
115
		add_action( 'admin_footer', array( $this->jetpack, 'do_stats' ) );
116
	}
117
118
	// Enqueue the Jetpack admin stylesheet
119
	function admin_styles() {
120
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
121
122
		wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' );
123
		wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' );
124
		wp_style_add_data( 'jetpack-admin', 'suffix', $min );
125
	}
126
127
	function is_wp_version_too_old() {
128
		global $wp_version;
129
		return ( ! function_exists( 'rest_api_init' ) || version_compare( $wp_version, '4.4-z', '<=' ) );
130
	}
131
132
	/**
133
	 * Checks the site plan and deactivates modules that were active but are no longer included in the plan.
134
	 *
135
	 * @since 4.4.0
136
	 *
137
	 * @param $page
138
	 *
139
	 * @return bool|array
140
	 */
141
	function check_plan_deactivate_modules( $page ) {
142
		if ( 'admin_page_jetpack_modules' !== $page && 'jetpack_page_jetpack_modules' !== $page ) {
143
			return false;
144
		}
145
		$previous = get_option( 'jetpack_active_plan', '' );
146
		$response = rest_do_request( new WP_REST_Request( 'GET', '/jetpack/v4/site' ) );
147
		$current = $response->get_data();
148
		$current = json_decode( $current['data'] );
149
		$to_deactivate = array();
150
		if ( isset( $current->plan->product_slug ) ) {
151
			if ( empty( $previous ) || ! isset( $previous['product_slug'] ) || $previous['product_slug'] !== $current->plan->product_slug ) {
152
				$active = Jetpack::get_active_modules();
153
				switch ( $current->plan->product_slug ) {
154
					case 'jetpack_free':
155
						$to_deactivate = array( 'seo-tools', 'videopress' );
156
						break;
157
					case 'jetpack_personal':
158
					case 'jetpack_personal_monthly':
159
						$to_deactivate = array( 'seo-tools', 'videopress' );
160
						break;
161
					case 'jetpack_premium':
162
					case 'jetpack_premium_monthly':
163
						$to_deactivate = array( 'seo-tools' );
164
						break;
165
				}
166
				$to_deactivate = array_intersect( $active, $to_deactivate );
167
				if ( ! empty( $to_deactivate ) ) {
168
					Jetpack::update_active_modules( array_filter( array_diff( $active, $to_deactivate ) ) );
169
				}
170
			}
171
		}
172
		return array(
173
			'previous'   => $previous,
174
			'current'    => $current,
175
			'deactivate' => $to_deactivate
176
		);
177
	}
178
}
179