Completed
Push — branch-4.1 ( 3fb9ba...cc3fd4 )
by Jeremy
272:42 queued 263:17
created

Abstract_Jetpack_Site::get_jetpack_modules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 9.4285
1
<?php
2
3
4
require_once dirname( __FILE__ ) . '/class.json-api-site-base.php';
5
6
abstract class Abstract_Jetpack_Site extends SAL_Site {
7
	abstract protected function get_constant( $name );
8
9
	abstract protected function current_theme_supports( $feature_name );
10
11
	abstract protected function get_theme_support( $feature_name );
12
13
	abstract protected function get_mock_option( $name );
14
15
	abstract protected function get_jetpack_version();
16
17
	abstract protected function get_updates();
18
19
	function before_render() {
20
	}
21
22
	function after_render( &$response ) {
23
		// Add the updates only make them visible if the user has manage options permission and the site is the main site of the network
24
		if ( current_user_can( 'manage_options' ) && $this->is_main_site( $response ) ) {
25
			$jetpack_update = $this->get_updates();
26
			if ( ! empty( $jetpack_update ) ) {
27
				// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates
28
				unset( $jetpack_update['wp_version'] );
29
				// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates
30
				unset( $jetpack_update['site_is_version_controlled'] );
31
32
				$response['updates'] = $jetpack_update;
33
			}
34
		}
35
	}
36
37
	function after_render_options( &$options ) {
38
		$options['jetpack_version'] = $this->get_jetpack_version();
39
40
		if ( $main_network_site = $this->get_mock_option( 'main_network_site' ) ) {
41
			$options['main_network_site'] = (string) rtrim( $main_network_site, '/' );
42
		}
43
44
		if ( is_array( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) ) {
45
			$options['active_modules'] = (array) array_values( $active_modules );
46
		}
47
48
		$options['software_version'] = (string) $this->get_mock_option( 'wp_version' );
49
		$options['max_upload_size']  = $this->get_mock_option( 'max_upload_size', false );
0 ignored issues
show
Unused Code introduced by
The call to Abstract_Jetpack_Site::get_mock_option() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
50
51
		// Sites have to prove that they are not main_network site.
52
		// If the sync happends right then we should be able to see that we are not dealing with a network site
53
		$options['is_multi_network'] = (bool) $this->get_mock_option( 'is_main_network', true );
0 ignored issues
show
Unused Code introduced by
The call to Abstract_Jetpack_Site::get_mock_option() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
		$options['is_multi_site']    = (bool) $this->get_mock_option( 'is_multi_site', true );
0 ignored issues
show
Unused Code introduced by
The call to Abstract_Jetpack_Site::get_mock_option() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
56
		$file_mod_disabled_reasons = array_keys( array_filter( array(
57
			'automatic_updater_disabled'      => (bool) $this->get_constant( 'AUTOMATIC_UPDATER_DISABLED' ),
58
			// WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false.
59
			'wp_auto_update_core_disabled'    => ! ( (bool) $this->get_constant( 'WP_AUTO_UPDATE_CORE' ) ),
60
			'is_version_controlled'           => (bool) $this->get_mock_option( 'is_version_controlled' ),
61
			// By default we assume that site does have system write access if the value is not set yet.
62
			'has_no_file_system_write_access' => ! (bool) ( $this->get_mock_option( 'has_file_system_write_access' ) ),
63
			'disallow_file_mods'              => (bool) $this->get_constant( 'DISALLOW_FILE_MODS' ),
64
		) ) );
65
66
		$options['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons;
67
	}
68
69
	function get_jetpack_modules() {
70
		if ( is_user_member_of_blog() ) {
71
			return array_values( Jetpack_Options::get_option( 'active_modules', array() ) );
72
		}
73
74
		return null;
75
	}
76
77
	function is_vip() {
78
		return false; // this may change for VIP Go sites, which sync using Jetpack
79
	}
80
81
	function is_multisite() {
82
		return (bool) $this->get_mock_option( 'is_multi_site' );
83
	}
84
85
	function is_single_user_site() {
86
		return (bool) $this->get_mock_option( 'single_user_site' );
87
	}
88
89
	function featured_images_enabled() {
90
		return $this->current_theme_supports( 'post-thumbnails' );
91
	}
92
93
	function get_post_formats() {
94
		// deprecated - see separate endpoint. get a list of supported post formats
95
		$all_formats = get_post_format_strings();
96
		$supported   = $this->get_theme_support( 'post-formats' );
97
98
		$supported_formats = array();
99
100 View Code Duplication
		if ( isset( $supported[0] ) ) {
101
			foreach ( $supported[0] as $format ) {
102
				$supported_formats[ $format ] = $all_formats[ $format ];
103
			}
104
		}
105
106
		return $supported_formats;
107
	}
108
109
	/**
110
	 * Private methods
111
	 **/
112
113
	private function is_main_site( $response ) {
114
		if ( isset( $response['options']->main_network_site, $response['options']->unmapped_url ) ) {
115
			$main_network_site_url = set_url_scheme( $response['options']->main_network_site, 'http' );
116
			$unmapped_url          = set_url_scheme( $response['options']->unmapped_url, 'http' );
117
			if ( $unmapped_url === $main_network_site_url ) {
118
				return true;
119
			}
120
		}
121
122
		return false;
123
	}
124
}
125