Completed
Push — feature/assets-cdn ( 4f639f...7aef3f )
by George
07:52
created

Jetpack_Settings_Page::page_render()   F

Complexity

Conditions 14
Paths 257

Size

Total Lines 148

Duplication

Lines 7
Ratio 4.73 %

Importance

Changes 0
Metric Value
cc 14
nc 257
nop 0
dl 7
loc 148
rs 3.7766
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
include_once( 'class.jetpack-admin-page.php' );
3
include_once( JETPACK__PLUGIN_DIR . 'class.jetpack-modules-list-table.php' );
4
5
// Builds the settings page and its menu
6
class Jetpack_Settings_Page extends Jetpack_Admin_Page {
7
8
	// Show the settings page only when Jetpack is connected or in dev mode
9
	protected $dont_show_if_not_active = true;
10
11
	function add_page_actions( $hook ) {}
12
13
	// Adds the Settings sub menu
14
	function get_page_hook() {
15
		return add_submenu_page( null, __( 'Jetpack Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'jetpack_manage_modules', 'jetpack_modules', array( $this, 'render' ) );
16
	}
17
18
	// Renders the module list table where you can use bulk action or row
19
	// actions to activate/deactivate and configure modules
20
	function page_render() {
21
		$list_table = new Jetpack_Modules_List_Table;
22
23
		$static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' );
24
25
		// If static.html isn't there, there's nothing else we can do.
26 View Code Duplication
		if ( false === $static_html ) {
27
			echo '<p>';
28
			esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' );
29
			echo '<code>yarn distclean && yarn build</code>';
30
			echo '</p>';
31
			return;
32
		}
33
34
		// We have static.html so let's continue trying to fetch the others
35
		$noscript_notice = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static-noscript-notice.html' );
36
		$version_notice = $rest_api_notice = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static-version-notice.html' );
37
		$ie_notice = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static-ie-notice.html' );
38
39
		$noscript_notice = str_replace(
40
			'#HEADER_TEXT#',
41
			esc_html__( 'You have JavaScript disabled', 'jetpack' ),
42
			$noscript_notice
43
		);
44
		$noscript_notice = str_replace(
45
			'#TEXT#',
46
			esc_html__( "Turn on JavaScript to unlock Jetpack's full potential!", 'jetpack' ),
47
			$noscript_notice
48
		);
49
50
		$version_notice = str_replace(
51
			'#HEADER_TEXT#',
52
			esc_html__( 'You are using an outdated version of WordPress', 'jetpack' ),
53
			$version_notice
54
		);
55
		$version_notice = str_replace(
56
			'#TEXT#',
57
			esc_html__( "Update WordPress to unlock Jetpack's full potential!", 'jetpack' ),
58
			$version_notice
59
		);
60
61
		$rest_api_notice = str_replace(
62
			'#HEADER_TEXT#',
63
			esc_html( __( 'WordPress REST API is disabled', 'jetpack' ) ),
64
			$rest_api_notice
65
		);
66
		$rest_api_notice = str_replace(
67
			'#TEXT#',
68
			esc_html( __( "Enable WordPress REST API to unlock Jetpack's full potential!", 'jetpack' ) ),
69
			$rest_api_notice
70
		);
71
72
		$ie_notice = str_replace(
73
			'#HEADER_TEXT#',
74
			esc_html__( 'You are using an unsupported browser version.', 'jetpack' ),
75
			$ie_notice
76
		);
77
		$ie_notice = str_replace(
78
			'#TEXT#',
79
			esc_html__( "Update your browser to unlock Jetpack's full potential!", 'jetpack' ),
80
			$ie_notice
81
		);
82
83
		ob_start();
84
85
		$this->admin_page_top();
86
87
		if ( $this->is_wp_version_too_old() ) {
88
			echo $version_notice;
89
		}
90
		if ( ! $this->is_rest_api_enabled() ) {
91
			echo $rest_api_notice;
92
		}
93
		echo $noscript_notice;
94
		echo $ie_notice;
95
		?>
96
97
		<div class="page-content configure">
98
			<div class="frame top hide-if-no-js">
99
				<div class="wrap">
100
					<div class="manage-left">
101
						<table class="table table-bordered fixed-top">
102
							<thead>
103
								<tr>
104
									<th class="check-column"><input type="checkbox" class="checkall"></th>
105
									<th colspan="2">
106
										<?php $list_table->unprotected_display_tablenav( 'top' ); ?>
107
										<span class="filter-search">
108
											<button type="button" class="button">Filter</button>
109
										</span>
110
									</th>
111
								</tr>
112
							</thead>
113
						</table>
114
					</div>
115
				</div><!-- /.wrap -->
116
			</div><!-- /.frame -->
117
			<div class="frame bottom">
118
				<div class="wrap">
119
					<div class="manage-right" style="display: none;">
120
						<div class="bumper">
121
							<form class="navbar-form" role="search">
122
								<input type="hidden" name="page" value="jetpack_modules" />
123
								<?php $list_table->search_box( __( 'Search', 'jetpack' ), 'srch-term' ); ?>
124
								<p><?php esc_html_e( 'View:', 'jetpack' ); ?></p>
125
								<div class="button-group filter-active">
126
									<button type="button" class="button <?php if ( empty( $_GET['activated'] ) ) echo 'active'; ?>"><?php esc_html_e( 'All', 'jetpack' ); ?></button>
127
									<button type="button" class="button <?php if ( ! empty( $_GET['activated'] ) && 'true' == $_GET['activated'] ) echo 'active'; ?>" data-filter-by="activated" data-filter-value="true"><?php esc_html_e( 'Active', 'jetpack' ); ?></button>
128
									<button type="button" class="button <?php if ( ! empty( $_GET['activated'] ) && 'false' == $_GET['activated'] ) echo 'active'; ?>" data-filter-by="activated" data-filter-value="false"><?php esc_html_e( 'Inactive', 'jetpack' ); ?></button>
129
								</div>
130
								<p><?php esc_html_e( 'Sort by:', 'jetpack' ); ?></p>
131
								<div class="button-group sort">
132
									<button type="button" class="button <?php if ( empty( $_GET['sort_by'] ) ) echo 'active'; ?>" data-sort-by="name"><?php esc_html_e( 'Alphabetical', 'jetpack' ); ?></button>
133
									<button type="button" class="button <?php if ( ! empty( $_GET['sort_by'] ) && 'introduced' == $_GET['sort_by'] ) echo 'active'; ?>" data-sort-by="introduced" data-sort-order="reverse"><?php esc_html_e( 'Newest', 'jetpack' ); ?></button>
134
									<button type="button" class="button <?php if ( ! empty( $_GET['sort_by'] ) && 'sort' == $_GET['sort_by'] ) echo 'active'; ?>" data-sort-by="sort"><?php esc_html_e( 'Popular', 'jetpack' ); ?></button>
135
								</div>
136
								<p><?php esc_html_e( 'Show:', 'jetpack' ); ?></p>
137
								<?php $list_table->views(); ?>
138
							</form>
139
						</div>
140
					</div>
141
					<div class="manage-left" style="width: 100%;">
142
						<form class="jetpack-modules-list-table-form" onsubmit="return false;">
143
						<table class="<?php echo implode( ' ', $list_table->get_table_classes() ); ?>">
144
							<tbody id="the-list">
145
								<?php $list_table->display_rows_or_placeholder(); ?>
146
							</tbody>
147
						</table>
148
						</form>
149
					</div>
150
				</div><!-- /.wrap -->
151
			</div><!-- /.frame -->
152
		</div><!-- /.content -->
153
		<?php
154
155
		$this->admin_page_bottom();
156
157
		$page_content = ob_get_contents();
158
		ob_end_clean();
159
160
		echo str_replace(
161
			'<div class="jp-loading-placeholder"><span class="dashicons dashicons-wordpress-alt"></span></div>',
162
			$page_content,
163
			$static_html
164
		);
165
166
		JetpackTracking::record_user_event( 'wpa_page_view', array( 'path' => 'old_settings' ) );
167
	}
168
169
	/**
170
	 * Load styles for static page.
171
	 *
172
	 * @since 4.3.0
173
	 */
174
	function additional_styles() {
175
		$rtl = is_rtl() ? '.rtl' : '';
176
		wp_enqueue_style( 'dops-css', plugins_url( "_inc/build/admin.dops-style$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
177
		wp_enqueue_style( 'components-css', plugins_url( "_inc/build/style.min$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
178
	}
179
180
	// Javascript logic specific to the list table
181
	function page_admin_scripts() {
182
		wp_enqueue_script(
183
			'jetpack-admin-js',
184
			Jetpack::get_file_url_for_environment( '_inc/build/jetpack-admin.min.js', '_inc/jetpack-admin.js' ),
185
			array( 'jquery' ),
186
			JETPACK__VERSION
187
		);
188
	}
189
}
190