Completed
Push — update/try-react-0.15 ( ce045e...4c8711 )
by
unknown
26:02 queued 15:13
created

Jetpack_Admin_Page   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 108
Duplicated Lines 20.37 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 22
loc 108
rs 10
wmc 21
lcom 2
cbo 1

16 Methods

Rating   Name   Duplication   Size   Complexity  
add_page_actions() 0 1 ?
get_page_hook() 0 1 ?
page_admin_scripts() 0 1 ?
page_render() 0 1 ?
A additional_styles() 0 1 1
A __construct() 0 3 1
B add_actions() 0 23 4
A admin_head() 13 15 4
A render() 0 3 1
A admin_help() 0 3 1
A admin_page_load() 0 4 1
A admin_page_top() 0 3 1
A admin_page_bottom() 0 3 1
A admin_scripts() 0 4 1
A admin_styles() 9 9 3
A is_wp_version_too_old() 0 4 2

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
// 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
	 * Function called after admin_styles to load any additional needed styles.
19
	 *
20
	 * @since 4.3.0
21
	 */
22
	function additional_styles() {}
23
24
	function __construct() {
25
		$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...
26
	}
27
28
	function add_actions() {
29
		/**
30
		 * Don't add in the modules page unless modules are available!
31
		 */
32
		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...
33
			return;
34
		}
35
36
		// Initialize menu item for the page in the admin
37
		$hook = $this->get_page_hook();
38
39
		// Attach hooks common to all Jetpack admin pages based on the created
40
		// hook
41
		add_action( "load-$hook",                array( $this, 'admin_help'      ) );
42
		add_action( "load-$hook",                array( $this, 'admin_page_load' ) );
43
		add_action( "admin_head-$hook",          array( $this, 'admin_head'      ) );
44
45
		add_action( "admin_print_styles-$hook",  array( $this, 'admin_styles'    ) );
46
		add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts'   ) );
47
48
		// Attach page specific actions in addition to the above
49
		$this->add_page_actions( $hook );
50
	}
51
52
	function admin_head() {
53 View Code Duplication
		if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) {
54
			/**
55
			 * Fires in the <head> of a particular Jetpack configuation page.
56
			 *
57
			 * The dynamic portion of the hook name, `$_GET['configure']`,
58
			 * refers to the slug of module, such as 'stats', 'sso', etc.
59
			 * A complete hook for the latter would be
60
			 * 'jetpack_module_configuation_head_sso'.
61
			 *
62
			 * @since 3.0.0
63
			 */
64
			do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] );
65
		}
66
	}
67
68
	// Render the page with a common top and bottom part, and page specific content
69
	function render() {
70
		$this->page_render();
71
	}
72
73
	function admin_help() {
74
		$this->jetpack->admin_help();
75
	}
76
77
	function admin_page_load() {
78
		// This is big.  For the moment, just call the existing one.
79
		$this->jetpack->admin_page_load();
80
	}
81
82
	function admin_page_top() {
83
		include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
84
	}
85
86
	function admin_page_bottom() {
87
		include_once( JETPACK__PLUGIN_DIR . '_inc/footer.php' );
88
	}
89
90
	// Add page specific scripts and jetpack stats for all menu pages
91
	function admin_scripts() {
92
		$this->page_admin_scripts(); // Delegate to inheriting class
93
		add_action( 'admin_footer', array( $this->jetpack, 'do_stats' ) );
94
	}
95
96
	// Enqueue the Jetpack admin stylesheet
97 View Code Duplication
	function admin_styles() {
98
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
99
100
		wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' );
101
		wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' );
102
		wp_style_add_data( 'jetpack-admin', 'suffix', $min );
103
104
		$this->additional_styles();
105
	}
106
107
	function is_wp_version_too_old() {
108
		global $wp_version;
109
		return ( ! function_exists( 'rest_api_init' ) || version_compare( $wp_version, '4.4-z', '<=' ) );
110
	}
111
}
112