Completed
Push — add/changelog-71 ( b70f6c...ac535c )
by Jeremy
10:29
created

minileven.php ➔ jetpack_mobile_request_handler()   C

Complexity

Conditions 10
Paths 33

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 33
nop 0
dl 0
loc 67
rs 6.8533
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
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="'. home_url( '?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 if (!empty($_SERVER['HTTP_REFERER'])) {
180
				$go = $_SERVER['HTTP_REFERER'];
181
			}
182
			else {
183
				$go = remove_query_arg( array( 'ak_action' ) );
184
			}
185
			wp_safe_redirect( $go );
186
			exit;
187
		}
188
	}
189
}
190
add_action('init', 'jetpack_mobile_request_handler');
191
192
function jetpack_mobile_theme_setup() {
193
	if ( jetpack_check_mobile() ) {
194
		// Redirect to download page if user clicked mobile app promo link in mobile footer
195
		if ( isset( $_GET['app-download'] ) ) {
196
			/**
197
			 * Fires before you're redirected to download page if you clicked the mobile app promo link in mobile footer
198
			 *
199
			 * @module minileven
200
			 *
201
			 * @since 1.8.0
202
			 *
203
			 * @param string $_GET['app-download'] app-download URL parameter.
204
			 */
205
			do_action( 'mobile_app_promo_download', $_GET['app-download'] );
206
207
			switch ( $_GET['app-download'] ) {
208
				case 'android':
209
					header( 'Location: market://search?q=pname:org.wordpress.android' );
210
					exit;
211
				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...
212
				case 'ios':
213
					header( 'Location: http://itunes.apple.com/us/app/wordpress/id335703880?mt=8' );
214
					exit;
215
				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...
216
				case 'blackberry':
217
					header( 'Location: http://blackberry.wordpress.org/download/' );
218
					exit;
219
				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...
220
			}
221
		}
222
223
		add_action('stylesheet', 'jetpack_mobile_stylesheet');
224
		add_action('template', 'jetpack_mobile_template');
225
		add_action('option_template', 'jetpack_mobile_template');
226
		add_action('option_stylesheet', 'jetpack_mobile_stylesheet');
227
228
		if ( class_exists( 'Jetpack_Custom_CSS' ) && method_exists( 'Jetpack_Custom_CSS', 'disable' ) && ! get_option( 'wp_mobile_custom_css' ) )
229
			add_action( 'init', array( 'Jetpack_Custom_CSS', 'disable' ), 11 );
230
231
		/**
232
		 * Fires after Jetpack's mobile theme has been setup.
233
		 *
234
		 * @module minileven
235
		 *
236
		 * @since 1.8.0
237
		 */
238
		do_action( 'mobile_setup' );
239
	}
240
}
241
242
// Need a hook after plugins_loaded (since this code won't be loaded in Jetpack
243
// until then) but after init (because it has its own init hooks to add).
244
add_action( 'setup_theme', 'jetpack_mobile_theme_setup' );
245
246
if (isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'false') {
247
	add_action('wp_footer', 'jetpack_mobile_available');
248
}
249
250
function jetpack_mobile_app_promo()  {
251
	?>
252
	<script type="text/javascript">
253
		if ( ! navigator.userAgent.match( /wp-(iphone|android|blackberry|nokia|windowsphone)/i ) ) {
254
			if ( ( navigator.userAgent.match( /iphone/i ) ) || ( navigator.userAgent.match( /ipod/i ) ) )
255
			   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 />' );
256
			else if ( ( navigator.userAgent.match( /android/i ) ) && ( null == navigator.userAgent.match( /playbook/i ) && null == navigator.userAgent.match( /bb10/i ) ) )
257
			   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 />' );
258
			else if ( ( navigator.userAgent.match( /blackberry/i ) ) || ( navigator.userAgent.match( /playbook/i ) ) || ( navigator.userAgent.match( /bb10/i ) ) )
259
			   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 />' );
260
		}
261
	</script>
262
	<?php
263
}
264
265
add_action( 'wp_mobile_theme_footer', 'jetpack_mobile_app_promo' );
266
267
/**
268
 * Adds an option to allow your Custom CSS to also be applied to the Mobile Theme.
269
 * It's disabled by default, but this should allow people who know what they're
270
 * doing to customize the mobile theme.
271
 */
272
function jetpack_mobile_css_settings() {
273
	$mobile_css = get_option( 'wp_mobile_custom_css' );
274
275
	?>
276
	<div class="misc-pub-section">
277
		<label><?php esc_html_e( 'Mobile-compatible:', 'jetpack' ); ?></label>
278
		<span id="mobile-css-display"><?php echo $mobile_css ? __( 'Yes', 'jetpack' ) : __( 'No', 'jetpack' ); ?></span>
279
		<a class="edit-mobile-css hide-if-no-js" href="#mobile-css"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
280
		<div id="mobile-css-select" class="hide-if-js">
281
			<input type="hidden" name="mobile_css" id="mobile-css" value="<?php echo intval( $mobile_css ); ?>" />
282
			<label>
283
				<input type="checkbox" id="mobile-css-visible" <?php checked( get_option( 'wp_mobile_custom_css' ) ); ?> />
284
				<?php esc_html_e( 'Include this CSS in the Mobile Theme', 'jetpack' ); ?>
285
			</label>
286
			<p>
287
				<a class="save-mobile-css hide-if-no-js button" href="#mobile-css"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
288
				<a class="cancel-mobile-css hide-if-no-js" href="#mobile-css"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
289
			</p>
290
		</div>
291
	</div>
292
	<script type="text/javascript">
293
		jQuery( function ( $ ) {
294
			$( '.edit-mobile-css' ).bind( 'click', function ( e ) {
295
				e.preventDefault();
296
297
				$( '#mobile-css-select' ).slideDown();
298
				$( this ).hide();
299
			} );
300
301
			$( '.cancel-mobile-css' ).bind( 'click', function ( e ) {
302
				e.preventDefault();
303
304
				$( '#mobile-css-select' ).slideUp( function () {
305
					$( '.edit-mobile-css' ).show();
306
307
					$( '#mobile-css-visible' ).prop( 'checked', $( '#mobile-css' ).val() == '1' );
308
				} );
309
			} );
310
311
			$( '.save-mobile-css' ).bind( 'click', function ( e ) {
312
				e.preventDefault();
313
314
				$( '#mobile-css-select' ).slideUp();
315
				$( '#mobile-css-display' ).text( $( '#mobile-css-visible' ).prop( 'checked' ) ? 'Yes' : 'No' );
316
				$( '#mobile-css' ).val( $( '#mobile-css-visible' ).prop( 'checked' ) ? '1' : '0' );
317
				$( '.edit-mobile-css' ).show();
318
			} );
319
		} );
320
	</script>
321
	<?php
322
}
323
324
add_action( 'custom_css_submitbox_misc_actions', 'jetpack_mobile_css_settings' );
325
326
function jetpack_mobile_customizer_controls( $wp_customize ) {
327
	$wp_customize->add_setting( 'wp_mobile_custom_css' , array(
328
		'default' => true,
329
		'transport' => 'postMessage',
330
		'type' => 'option'
331
	) );
332
333
	$wp_customize->add_control( 'jetpack_mobile_css_control', array(
334
		'type' => 'checkbox',
335
		'label' => __( 'Include this CSS in the Mobile Theme', 'jetpack' ),
336
		'section' => 'jetpack_custom_css',
337
		'settings' => 'wp_mobile_custom_css',
338
	) );
339
}
340
341
add_action( 'jetpack_custom_css_customizer_controls', 'jetpack_mobile_customizer_controls' );
342
343
function jetpack_mobile_save_css_settings() {
344
	update_option( 'wp_mobile_custom_css', isset( $_POST['mobile_css'] ) && ! empty( $_POST['mobile_css'] ) );
345
}
346
347
add_action( 'safecss_save_pre', 'jetpack_mobile_save_css_settings' );
348