Completed
Push — update/api-get-site-endpoint ( 87e1bc )
by
unknown
09:51
created

Abstract_Jetpack_Site   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 99
Duplicated Lines 5.05 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 5
loc 99
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
get_constant() 0 1 ?
current_theme_supports() 0 1 ?
get_theme_support() 0 1 ?
get_mock_option() 0 1 ?
get_jetpack_version() 0 1 ?
A before_render() 0 1 1
A after_render() 0 14 4
B after_render_options() 0 31 4
A get_jetpack_modules() 0 7 2
A featured_images_enabled() 0 3 1
A get_post_formats() 5 15 3
A is_main_site() 0 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
	abstract protected function current_theme_supports( $feature_name );
9
	abstract protected function get_theme_support( $feature_name );
10
	abstract protected function get_mock_option( $name );
11
	abstract protected function get_jetpack_version();
12
13
	function before_render() {}
14
15
	function after_render( &$response ) {
16
		// Add the updates only make them visible if the user has manage options permission and the site is the main site of the network
17
		if ( current_user_can( 'manage_options' ) && $this->is_main_site( $response ) ) {
18
			$jetpack_update = (array) get_option( 'jetpack_updates' );
19
			if ( ! empty( $jetpack_update ) ) {
20
				// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates
21
				unset( $jetpack_update['wp_version'] );
22
				// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates
23
				unset( $jetpack_update['site_is_version_controlled'] );
24
				
25
				$response['updates'] = (array) $jetpack_update;
26
			}
27
		}
28
	}
29
30
	function after_render_options( &$options ) {
31
		$options['jetpack_version'] = $this->get_jetpack_version();
32
33
		if ( $main_network_site = $this->get_mock_option( 'main_network_site' ) ) {
34
			$options['main_network_site'] = (string) rtrim( $main_network_site, '/' );
35
		}
36
37
		if ( is_array( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) ) {
38
			$options['active_modules'] = (array) array_values( $active_modules );
39
		}
40
41
		$options['software_version'] = (string) $this->get_mock_option( 'wp_version' );
42
		$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...
43
44
		// Sites have to prove that they are not main_network site.
45
		// If the sync happends right then we should be able to see that we are not dealing with a network site
46
		$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...
47
		$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...
48
49
		$file_mod_disabled_reasons = array_keys( array_filter( array(
50
			'automatic_updater_disabled'      => (bool) $this->get_constant( 'AUTOMATIC_UPDATER_DISABLED' ),
51
			// WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false.
52
			'wp_auto_update_core_disabled'    =>  ! ( (bool) $this->get_constant( 'WP_AUTO_UPDATE_CORE', 'minor' ) ),
0 ignored issues
show
Unused Code introduced by
The call to Abstract_Jetpack_Site::get_constant() has too many arguments starting with 'minor'.

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...
53
			'is_version_controlled'           => (bool) $this->get_mock_option( 'is_version_controlled' ),
54
			// By default we assume that site does have system write access if the value is not set yet.
55
			'has_no_file_system_write_access' => ! (bool)( $this->get_mock_option( 'has_file_system_write_access', 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...
56
			'disallow_file_mods'              => (bool) $this->get_constant( 'DISALLOW_FILE_MODS' )
57
		) ) );
58
59
		$options['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons;
60
	}
61
62
	function get_jetpack_modules() {
63
		if ( is_user_member_of_blog() ) {
64
			return array_values( Jetpack_Options::get_option( 'active_modules', array() ) );	
65
		}
66
67
		return null;
68
	}
69
70
	function featured_images_enabled() {
71
		return $this->current_theme_supports( 'post-thumbnails' );
72
	}
73
74
	function get_post_formats() {
75
		// deprecated - see separate endpoint. get a list of supported post formats
76
		$all_formats       = get_post_format_strings();
77
		$supported         = $this->get_theme_support( 'post-formats' );
78
79
		$supported_formats = array();
80
81 View Code Duplication
		if ( isset( $supported[0] ) ) {
82
			foreach ( $supported[0] as $format ) {
83
				$supported_formats[ $format ] = $all_formats[ $format ];
84
			}
85
		}
86
87
		return $supported_formats;
88
	}
89
90
	/**
91
	 * Private methods
92
	 **/
93
94
	private function is_main_site( $response ) {
95
		if ( isset( $response['options']['main_network_site'], $response['options']['unmapped_url'] ) ) {
96
			$main_network_site_url = set_url_scheme( $response['options']['main_network_site'], 'http' );
97
			$unmapped_url          = set_url_scheme( $response['options']['unmapped_url'], 'http' );
98
			if ( $unmapped_url === $main_network_site_url ) {
99
				return true;
100
			}
101
		}
102
		return false;
103
	}
104
}