Completed
Push — feature/sync-json-endpoints ( bf1919...f7fa9b )
by
unknown
33:49 queued 21:50
created

Abstract_Jetpack_Site::is_single_user_site()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.json-api-site-base.php';
4
5
abstract class Abstract_Jetpack_Site extends SAL_Site {
6
	abstract protected function get_constant( $name );
7
8
	abstract protected function current_theme_supports( $feature_name );
9
10
	abstract protected function get_theme_support( $feature_name );
11
12
	abstract protected function get_mock_option( $name );
13
14
	abstract public function get_jetpack_version();
15
16
	abstract public function get_updates();
17
18
	abstract protected function main_network_site();
19
20
	abstract protected function wp_version();
21
22
	abstract protected function max_upload_size();
23
24
	abstract protected function is_main_network();
25
26
	abstract protected function is_version_controlled();
27
28
	abstract protected function file_system_write_access();
29
30
	function before_render() {
31
	}
32
33
	function after_render( &$response ) {
34
		// Add the updates only make them visible if the user has manage options permission and the site is the main site of the network
35
		if ( current_user_can( 'manage_options' ) && $this->is_main_site( $response ) ) {
36
			$jetpack_update = $this->get_updates();
37
			if ( ! empty( $jetpack_update ) ) {
38
				// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates
39
				unset( $jetpack_update['wp_version'] );
40
				// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates
41
				unset( $jetpack_update['site_is_version_controlled'] );
42
43
				$response['updates'] = $jetpack_update;
44
			}
45
		}
46
	}
47
48
	function after_render_options( &$options ) {
49
50
		$options['jetpack_version'] = $this->get_jetpack_version();
51
52
		if ( $main_network_site = $this->main_network_site() ) {
53
			$options['main_network_site'] = (string) rtrim( $main_network_site, '/' );
54
		}
55
56
		if ( is_array( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) ) {
57
			$options['active_modules'] = (array) array_values( $active_modules );
58
		}
59
60
		$options['software_version'] = (string) $this->wp_version();
61
		$options['max_upload_size']  = $this->max_upload_size();
62
63
		// Sites have to prove that they are not main_network site.
64
		// If the sync happends right then we should be able to see that we are not dealing with a network site
65
		$options['is_multi_network'] = (bool) $this->is_main_network();
66
		$options['is_multi_site']    = (bool) $this->is_multisite();
67
68
		$file_mod_disabled_reasons = array_keys( array_filter( array(
69
			'automatic_updater_disabled'      => (bool) $this->get_constant( 'AUTOMATIC_UPDATER_DISABLED' ),
70
			// WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false.
71
			'wp_auto_update_core_disabled'    => ! ( (bool)  $this->get_constant( 'WP_AUTO_UPDATE_CORE' ) ),
72
			'is_version_controlled'           => (bool) $this->is_version_controlled(),
73
			// By default we assume that site does have system write access if the value is not set yet.
74
			'has_no_file_system_write_access' => ! (bool) $this->file_system_write_access(),
75
			'disallow_file_mods'              => (bool)  $this->get_constant( 'DISALLOW_FILE_MODS' ),
76
		) ) );
77
78
		$options['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons;
79
	}
80
81
	function get_jetpack_modules() {
82
		if ( is_user_member_of_blog() ) {
83
			return array_values( Jetpack_Options::get_option( 'active_modules', array() ) );
84
		}
85
86
		return null;
87
	}
88
89
	function is_vip() {
90
		return false; // this may change for VIP Go sites, which sync using Jetpack
91
	}
92
93
	function featured_images_enabled() {
94
		return $this->current_theme_supports( 'post-thumbnails' );
95
	}
96
97
	function get_post_formats() {
98
		// deprecated - see separate endpoint. get a list of supported post formats
99
		$all_formats = get_post_format_strings();
100
		$supported   = $this->get_theme_support( 'post-formats' );
101
102
		$supported_formats = array();
103
104 View Code Duplication
		if ( isset( $supported[0] ) ) {
105
			foreach ( $supported[0] as $format ) {
106
				$supported_formats[ $format ] = $all_formats[ $format ];
107
			}
108
		}
109
110
		return $supported_formats;
111
	}
112
113
	function get_icon() {
114
		$icon_id = get_option( 'site_icon' );
115
		if ( empty( $icon_id ) ) {
116
			$icon_id = Jetpack_Options::get_option( 'site_icon_id' );
117
		}
118
119
		if ( empty( $icon_id ) ) {
120
			return null;
121
		}
122
123
		$icon = array_filter( array(
124
			'img' => wp_get_attachment_image_url( $icon_id, 'full' ),
125
			'ico' => wp_get_attachment_image_url( $icon_id, array( 16, 16 ) )
126
		) );
127
128
		if ( empty( $icon ) ) {
129
			return null;
130
		}
131
132
		if ( current_user_can( 'edit_posts', $icon_id ) ) {
133
			$icon['media_id'] = (int) $icon_id;
134
		}
135
136
		return $icon;
137
	}
138
139
	/**
140
	 * Private methods
141
	 **/
142
143
	private function is_main_site( $response ) {
144
		if ( isset( $response['options']->main_network_site, $response['options']->unmapped_url ) ) {
145
			$main_network_site_url = set_url_scheme( $response['options']->main_network_site, 'http' );
146
			$unmapped_url          = set_url_scheme( $response['options']->unmapped_url, 'http' );
147
			if ( $unmapped_url === $main_network_site_url ) {
148
				return true;
149
			}
150
		}
151
152
		return false;
153
	}
154
155
	// For Jetpack sites this will always return false
156
	protected function is_a8c_publication( $post_id ) {
157
		return false;
158
	}
159
}
160