Completed
Push — upgrade/eslint-5 ( 071910...9f2520 )
by
unknown
07:52
created

minileven.php ➔ jetpack_mobile_request_handler()   B

Complexity

Conditions 9
Paths 25

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 25
nop 0
dl 0
loc 64
rs 7.2298
c 0
b 0
f 0

How to fix   Long Method   

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
3
// ********** modify blog option 'wp_mobile_template' manually to specify a theme (ex. 'vip/cnnmobile')
4
5
// WordPress Mobile Edition
6
//
7
// Copyright (c) 2002-2008 Alex King
8
// http://alexking.org/projects/wordpress
9
//
10
// Released under the GPL license
11
// http://www.opensource.org/licenses/gpl-license.php
12
//
13
// **********************************************************************
14
// This program is distributed in the hope that it will be useful, but
15
// WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
// *****************************************************************
18
19
/*
20
Plugin Name: WordPress Mobile Edition
21
Plugin URI: http://alexking.org/projects/wordpress
22
Description: Show a mobile view of the post/page if the visitor is on a known mobile device. Questions on configuration, etc.? Make sure to read the README.
23
Author: Alex King
24
Author URI: http://alexking.org
25
Version: 2.1a-WPCOM
26
*/
27
28
$_SERVER['REQUEST_URI'] = ( isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME'] . (( isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '')));
29
30
function jetpack_check_mobile() {
31
	if ( ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) || ( defined('APP_REQUEST') && APP_REQUEST ) )
32
		return false;
33
	if ( !isset($_SERVER["HTTP_USER_AGENT"]) || (isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'false') )
34
		return false;
35
	if ( jetpack_mobile_exclude() )
36
		return false;
37
	if ( 1 == Jetpack_Options::get_option_and_ensure_autoload( 'wp_mobile_disable', '0' ) )
38
		return false;
39
	if ( isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'true' )
40
		return true;
41
42
	$is_mobile = jetpack_is_mobile();
43
44
	/**
45
	 * Filter the Mobile check results.
46
	 *
47
	 * @module minileven
48
	 *
49
	 * @since 1.8.0
50
	 *
51
	 * @param bool $is_mobile Is the reader on a mobile device.
52
	 */
53
	return apply_filters( 'jetpack_check_mobile', $is_mobile );
54
}
55
56
function jetpack_mobile_exclude() {
57
	$exclude = false;
58
	$pages_to_exclude = array(
59
		'wp-admin',
60
		'wp-comments-post.php',
61
		'wp-mail.php',
62
		'wp-login.php',
63
		'wp-activate.php',
64
	);
65
	foreach ( $pages_to_exclude as $exclude_page ) {
66
		if ( strstr( strtolower( $_SERVER['REQUEST_URI'] ), $exclude_page ) )
67
			$exclude = true;
68
	}
69
70
	if ( defined( 'DOING_AJAX' ) && true === DOING_AJAX )
71
		$exclude = false;
72
73
	if ( isset( $GLOBALS['wp_customize'] ) )
74
		return true;
75
76
	return $exclude;
77
}
78
79
function wp_mobile_get_main_template() {
80
	remove_action( 'option_template', 'jetpack_mobile_template' );
81
	$template = get_option( 'template' );
82
	add_action( 'option_template', 'jetpack_mobile_template' );
83
	return $template;
84
}
85
86 View Code Duplication
function wp_mobile_get_main_stylesheet() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
	remove_action( 'option_stylesheet', 'jetpack_mobile_stylesheet' );
88
	$stylesheet = get_option( 'stylesheet' );
89
	add_action( 'option_stylesheet', 'jetpack_mobile_stylesheet' );
90
	return $stylesheet;
91
}
92
93
function jetpack_mobile_stylesheet( $theme ) {
94
	/**
95
	 * Filter Jetpack's Mobile stylesheet.
96
	 *
97
	 * @module minileven
98
	 *
99
	 * @since 1.8.0
100
	 *
101
	 * @param string $theme Theme.
102
	 */
103
	return apply_filters( 'jetpack_mobile_stylesheet', 'pub/minileven', $theme );
104
}
105
106
function jetpack_mobile_template( $theme ) {
107
	/**
108
	 * Filter Jetpack's Mobile template.
109
	 *
110
	 * @module minileven
111
	 *
112
	 * @since 1.8.0
113
	 *
114
	 * @param string $theme Theme.
115
	 */
116
	return apply_filters( 'jetpack_mobile_template', 'pub/minileven', $theme );
117
}
118
119
function jetpack_mobile_available() {
120
	echo '<div class="jetpack-mobile-link" style="text-align:center;margin:10px 0;"><a href="'. esc_url( home_url( add_query_arg('ak_action', 'accept_mobile') ) ) . '">' . __( 'View Mobile Site', 'jetpack' ) . '</a></div>';
121
}
122
123
function jetpack_mobile_request_handler() {
124
	global $wpdb;
125
	if (isset($_GET['ak_action'])) {
126
		$url = parse_url( get_bloginfo( 'url' ) );
127
		$domain = $url['host'];
128
		if (!empty($url['path'])) {
129
			$path = $url['path'];
130
		}
131
		else {
132
			$path = '/';
133
		}
134
		$redirect = false;
135
		switch ($_GET['ak_action']) {
136
			case 'reject_mobile':
137
				setcookie(
138
					'akm_mobile'
139
					, 'false'
140
					, time() + 300000
141
					, $path
142
					, $domain
143
				);
144
				$redirect = true;
145
146
				/**
147
				 * In Jetpack's Mobile theme, fires after the user taps on the link to display a full version of the site.
148
				 *
149
				 * @module minileven
150
				 *
151
				 * @since 1.8.0
152
				 */
153
				do_action( 'mobile_reject_mobile' );
154
				break;
155
			case 'force_mobile':
156
			case 'accept_mobile':
157
				setcookie(
158
					'akm_mobile'
159
					, 'true'
160
					, time() + 300000
161
					, $path
162
					, $domain
163
				);
164
				$redirect = true;
165
166
				/**
167
				 * In Jetpack's Mobile theme, fires after the user taps on the link to go back from full site to mobile site.
168
				 *
169
				 * @module minileven
170
				 *
171
				 * @since 1.8.0
172
				 */
173
				do_action( 'mobile_force_mobile' );
174
				break;
175
		}
176
		if ($redirect) {
177
			if ( isset( $_GET['redirect_to'] ) && $_GET['redirect_to'] ) {
178
				$go = urldecode( $_GET['redirect_to'] );
179
			} else {
180
				$go = remove_query_arg( array( 'ak_action' ) );
181
			}
182
			wp_safe_redirect( $go );
183
			exit;
184
		}
185
	}
186
}
187
add_action('init', 'jetpack_mobile_request_handler');
188
189
function jetpack_mobile_theme_setup() {
190
	if ( jetpack_check_mobile() ) {
191
		// Redirect to download page if user clicked mobile app promo link in mobile footer
192
		if ( isset( $_GET['app-download'] ) ) {
193
			/**
194
			 * Fires before you're redirected to download page if you clicked the mobile app promo link in mobile footer
195
			 *
196
			 * @module minileven
197
			 *
198
			 * @since 1.8.0
199
			 *
200
			 * @param string $_GET['app-download'] app-download URL parameter.
201
			 */
202
			do_action( 'mobile_app_promo_download', $_GET['app-download'] );
203
204
			switch ( $_GET['app-download'] ) {
205
				case 'android':
206
					header( 'Location: market://search?q=pname:org.wordpress.android' );
207
					exit;
208
				break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
209
				case 'ios':
210
					header( 'Location: http://itunes.apple.com/us/app/wordpress/id335703880?mt=8' );
211
					exit;
212
				break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
213
				case 'blackberry':
214
					header( 'Location: http://blackberry.wordpress.org/download/' );
215
					exit;
216
				break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
217
			}
218
		}
219
220
		add_action('stylesheet', 'jetpack_mobile_stylesheet');
221
		add_action('template', 'jetpack_mobile_template');
222
		add_action('option_template', 'jetpack_mobile_template');
223
		add_action('option_stylesheet', 'jetpack_mobile_stylesheet');
224
225
		if ( class_exists( 'Jetpack_Custom_CSS' ) && method_exists( 'Jetpack_Custom_CSS', 'disable' ) && ! get_option( 'wp_mobile_custom_css' ) )
226
			add_action( 'init', array( 'Jetpack_Custom_CSS', 'disable' ), 11 );
227
228
		/**
229
		 * Fires after Jetpack's mobile theme has been setup.
230
		 *
231
		 * @module minileven
232
		 *
233
		 * @since 1.8.0
234
		 */
235
		do_action( 'mobile_setup' );
236
	}
237
}
238
239
// Need a hook after plugins_loaded (since this code won't be loaded in Jetpack
240
// until then) but after init (because it has its own init hooks to add).
241
add_action( 'setup_theme', 'jetpack_mobile_theme_setup' );
242
243
if (isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'false') {
244
	add_action('wp_footer', 'jetpack_mobile_available');
245
}
246
247
function jetpack_mobile_app_promo()  {
248
	?>
249
	<script type="text/javascript">
250
		if ( ! navigator.userAgent.match( /wp-(iphone|android|blackberry|nokia|windowsphone)/i ) ) {
251
			if ( ( navigator.userAgent.match( /iphone/i ) ) || ( navigator.userAgent.match( /ipod/i ) ) )
252
			   document.write( '<span id="wpcom-mobile-app-promo" style="margin-top: 10px; font-size: 13px;"><strong>Now Available!</strong> <a href="/index.php?app-download=ios">Download WordPress for iOS</a></span><br /><br />' );
253
			else if ( ( navigator.userAgent.match( /android/i ) ) && ( null == navigator.userAgent.match( /playbook/i ) && null == navigator.userAgent.match( /bb10/i ) ) )
254
			   document.write( '<span id="wpcom-mobile-app-promo" style="margin-top: 10px; font-size: 13px;"><strong>Now Available!</strong> <a href="/index.php?app-download=android">Download WordPress for Android</a></span><br /><br />' );
255
			else if ( ( navigator.userAgent.match( /blackberry/i ) ) || ( navigator.userAgent.match( /playbook/i ) ) || ( navigator.userAgent.match( /bb10/i ) ) )
256
			   document.write( '<span id="wpcom-mobile-app-promo" style="margin-top: 10px; font-size: 13px;"><strong>Now Available!</strong> <a href="/index.php?app-download=blackberry">Download WordPress for BlackBerry</a></span><br /><br />' );
257
		}
258
	</script>
259
	<?php
260
}
261
262
add_action( 'wp_mobile_theme_footer', 'jetpack_mobile_app_promo' );
263
264
/**
265
 * Adds an option to allow your Custom CSS to also be applied to the Mobile Theme.
266
 * It's disabled by default, but this should allow people who know what they're
267
 * doing to customize the mobile theme.
268
 */
269
function jetpack_mobile_css_settings() {
270
	$mobile_css = get_option( 'wp_mobile_custom_css' );
271
272
	?>
273
	<div class="misc-pub-section">
274
		<label><?php esc_html_e( 'Mobile-compatible:', 'jetpack' ); ?></label>
275
		<span id="mobile-css-display"><?php echo $mobile_css ? __( 'Yes', 'jetpack' ) : __( 'No', 'jetpack' ); ?></span>
276
		<a class="edit-mobile-css hide-if-no-js" href="#mobile-css"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
277
		<div id="mobile-css-select" class="hide-if-js">
278
			<input type="hidden" name="mobile_css" id="mobile-css" value="<?php echo intval( $mobile_css ); ?>" />
279
			<label>
280
				<input type="checkbox" id="mobile-css-visible" <?php checked( get_option( 'wp_mobile_custom_css' ) ); ?> />
281
				<?php esc_html_e( 'Include this CSS in the Mobile Theme', 'jetpack' ); ?>
282
			</label>
283
			<p>
284
				<a class="save-mobile-css hide-if-no-js button" href="#mobile-css"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
285
				<a class="cancel-mobile-css hide-if-no-js" href="#mobile-css"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
286
			</p>
287
		</div>
288
	</div>
289
	<script type="text/javascript">
290
		jQuery( function ( $ ) {
291
			$( '.edit-mobile-css' ).bind( 'click', function ( e ) {
292
				e.preventDefault();
293
294
				$( '#mobile-css-select' ).slideDown();
295
				$( this ).hide();
296
			} );
297
298
			$( '.cancel-mobile-css' ).bind( 'click', function ( e ) {
299
				e.preventDefault();
300
301
				$( '#mobile-css-select' ).slideUp( function () {
302
					$( '.edit-mobile-css' ).show();
303
304
					$( '#mobile-css-visible' ).prop( 'checked', $( '#mobile-css' ).val() == '1' );
305
				} );
306
			} );
307
308
			$( '.save-mobile-css' ).bind( 'click', function ( e ) {
309
				e.preventDefault();
310
311
				$( '#mobile-css-select' ).slideUp();
312
				$( '#mobile-css-display' ).text( $( '#mobile-css-visible' ).prop( 'checked' ) ? 'Yes' : 'No' );
313
				$( '#mobile-css' ).val( $( '#mobile-css-visible' ).prop( 'checked' ) ? '1' : '0' );
314
				$( '.edit-mobile-css' ).show();
315
			} );
316
		} );
317
	</script>
318
	<?php
319
}
320
321
add_action( 'custom_css_submitbox_misc_actions', 'jetpack_mobile_css_settings' );
322
323
function jetpack_mobile_customizer_controls( $wp_customize ) {
324
	$wp_customize->add_setting( 'wp_mobile_custom_css' , array(
325
		'default' => true,
326
		'transport' => 'postMessage',
327
		'type' => 'option'
328
	) );
329
330
	$wp_customize->add_control( 'jetpack_mobile_css_control', array(
331
		'type' => 'checkbox',
332
		'label' => __( 'Include this CSS in the Mobile Theme', 'jetpack' ),
333
		'section' => 'jetpack_custom_css',
334
		'settings' => 'wp_mobile_custom_css',
335
	) );
336
}
337
338
add_action( 'jetpack_custom_css_customizer_controls', 'jetpack_mobile_customizer_controls' );
339
340
function jetpack_mobile_save_css_settings() {
341
	update_option( 'wp_mobile_custom_css', isset( $_POST['mobile_css'] ) && ! empty( $_POST['mobile_css'] ) );
342
}
343
344
add_action( 'safecss_save_pre', 'jetpack_mobile_save_css_settings' );
345