Completed
Push — branch-4.5 ( 315d88...943fb3 )
by
unknown
15:37 queued 07:20
created

Abstract_Jetpack_Site::get_icon()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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