Completed
Push — mobile/hide-edit-post-link ( 3ef095 )
by
unknown
89:19 queued 80:30
created

minileven.php ➔ jetpack_mobile_app_edit_post_link()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
	/*
121
	 * Create HTML markup with a link to "View Mobile Site".
122
	 * The link adds "ak_action=accept_mobile" to the current URL.
123
	 */
124
	global $wp;
125
	$url_params = array(
126
		'ak_action' => 'accept_mobile',
127
	);
128
	if ( is_array( $_GET ) && ! empty( $_GET ) ) {
129
		$url_params[] = $_GET;
130
	}
131
	$target_url = home_url( add_query_arg( $url_params, $wp->request ) );
132
	$anchor = '<a href="' . esc_url( $target_url ) . '">' . __( 'View Mobile Site', 'jetpack' ) . '</a>';
133
	echo '<div class="jetpack-mobile-link" style="text-align:center;margin:10px 0;">' . $anchor . '</div>';
134
}
135
136
function jetpack_mobile_request_handler() {
137
	global $wpdb;
138
	if (isset($_GET['ak_action'])) {
139
		$url = parse_url( get_bloginfo( 'url' ) );
140
		$domain = $url['host'];
141
		if (!empty($url['path'])) {
142
			$path = $url['path'];
143
		}
144
		else {
145
			$path = '/';
146
		}
147
		$redirect = false;
148
		switch ($_GET['ak_action']) {
149
			case 'reject_mobile':
150
				setcookie(
151
					'akm_mobile'
152
					, 'false'
153
					, time() + 300000
154
					, $path
155
					, $domain
156
				);
157
				$redirect = true;
158
159
				/**
160
				 * In Jetpack's Mobile theme, fires after the user taps on the link to display a full version of the site.
161
				 *
162
				 * @module minileven
163
				 *
164
				 * @since 1.8.0
165
				 */
166
				do_action( 'mobile_reject_mobile' );
167
				break;
168
			case 'force_mobile':
169
			case 'accept_mobile':
170
				setcookie(
171
					'akm_mobile'
172
					, 'true'
173
					, time() + 300000
174
					, $path
175
					, $domain
176
				);
177
				$redirect = true;
178
179
				/**
180
				 * In Jetpack's Mobile theme, fires after the user taps on the link to go back from full site to mobile site.
181
				 *
182
				 * @module minileven
183
				 *
184
				 * @since 1.8.0
185
				 */
186
				do_action( 'mobile_force_mobile' );
187
				break;
188
		}
189
		if ($redirect) {
190
			if ( isset( $_GET['redirect_to'] ) && $_GET['redirect_to'] ) {
191
				$go = urldecode( $_GET['redirect_to'] );
192
			} else {
193
				$go = remove_query_arg( array( 'ak_action' ) );
194
			}
195
			wp_safe_redirect( $go );
196
			exit;
197
		}
198
	}
199
}
200
add_action('init', 'jetpack_mobile_request_handler');
201
202
function jetpack_mobile_theme_setup() {
203
	if ( jetpack_check_mobile() ) {
204
		// Redirect to download page if user clicked mobile app promo link in mobile footer
205
		if ( isset( $_GET['app-download'] ) ) {
206
			/**
207
			 * Fires before you're redirected to download page if you clicked the mobile app promo link in mobile footer
208
			 *
209
			 * @module minileven
210
			 *
211
			 * @since 1.8.0
212
			 *
213
			 * @param string $_GET['app-download'] app-download URL parameter.
214
			 */
215
			do_action( 'mobile_app_promo_download', $_GET['app-download'] );
216
217
			switch ( $_GET['app-download'] ) {
218
				case 'android':
219
					header( 'Location: market://search?q=pname:org.wordpress.android' );
220
					exit;
221
				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...
222
				case 'ios':
223
					header( 'Location: http://itunes.apple.com/us/app/wordpress/id335703880?mt=8' );
224
					exit;
225
				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...
226
				case 'blackberry':
227
					header( 'Location: http://blackberry.wordpress.org/download/' );
228
					exit;
229
				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...
230
			}
231
		}
232
233
		add_action('stylesheet', 'jetpack_mobile_stylesheet');
234
		add_action('template', 'jetpack_mobile_template');
235
		add_action('option_template', 'jetpack_mobile_template');
236
		add_action('option_stylesheet', 'jetpack_mobile_stylesheet');
237
238
		if ( class_exists( 'Jetpack_Custom_CSS' ) && method_exists( 'Jetpack_Custom_CSS', 'disable' ) && ! get_option( 'wp_mobile_custom_css' ) )
239
			add_action( 'init', array( 'Jetpack_Custom_CSS', 'disable' ), 11 );
240
241
		/**
242
		 * Fires after Jetpack's mobile theme has been setup.
243
		 *
244
		 * @module minileven
245
		 *
246
		 * @since 1.8.0
247
		 */
248
		do_action( 'mobile_setup' );
249
	}
250
}
251
252
// Need a hook after plugins_loaded (since this code won't be loaded in Jetpack
253
// until then) but after init (because it has its own init hooks to add).
254
add_action( 'setup_theme', 'jetpack_mobile_theme_setup' );
255
256
if (isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'false') {
257
	add_action('wp_footer', 'jetpack_mobile_available');
258
}
259
260
function jetpack_mobile_app_promo()  {
261
	?>
262
	<script type="text/javascript">
263
		if ( ! navigator.userAgent.match( /wp-(iphone|android|blackberry|nokia|windowsphone)/i ) ) {
264
			if ( ( navigator.userAgent.match( /iphone/i ) ) || ( navigator.userAgent.match( /ipod/i ) ) )
265
			   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 />' );
266
			else if ( ( navigator.userAgent.match( /android/i ) ) && ( null == navigator.userAgent.match( /playbook/i ) && null == navigator.userAgent.match( /bb10/i ) ) )
267
			   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 />' );
268
			else if ( ( navigator.userAgent.match( /blackberry/i ) ) || ( navigator.userAgent.match( /playbook/i ) ) || ( navigator.userAgent.match( /bb10/i ) ) )
269
			   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 />' );
270
		}
271
	</script>
272
	<?php
273
}
274
275
add_action( 'wp_mobile_theme_footer', 'jetpack_mobile_app_promo' );
276
277
/**
278
 * Adds an option to allow your Custom CSS to also be applied to the Mobile Theme.
279
 * It's disabled by default, but this should allow people who know what they're
280
 * doing to customize the mobile theme.
281
 */
282
function jetpack_mobile_css_settings() {
283
	$mobile_css = get_option( 'wp_mobile_custom_css' );
284
285
	?>
286
	<div class="misc-pub-section">
287
		<label><?php esc_html_e( 'Mobile-compatible:', 'jetpack' ); ?></label>
288
		<span id="mobile-css-display"><?php echo $mobile_css ? __( 'Yes', 'jetpack' ) : __( 'No', 'jetpack' ); ?></span>
289
		<a class="edit-mobile-css hide-if-no-js" href="#mobile-css"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a>
290
		<div id="mobile-css-select" class="hide-if-js">
291
			<input type="hidden" name="mobile_css" id="mobile-css" value="<?php echo intval( $mobile_css ); ?>" />
292
			<label>
293
				<input type="checkbox" id="mobile-css-visible" <?php checked( get_option( 'wp_mobile_custom_css' ) ); ?> />
294
				<?php esc_html_e( 'Include this CSS in the Mobile Theme', 'jetpack' ); ?>
295
			</label>
296
			<p>
297
				<a class="save-mobile-css hide-if-no-js button" href="#mobile-css"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
298
				<a class="cancel-mobile-css hide-if-no-js" href="#mobile-css"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a>
299
			</p>
300
		</div>
301
	</div>
302
	<script type="text/javascript">
303
		jQuery( function ( $ ) {
304
			$( '.edit-mobile-css' ).bind( 'click', function ( e ) {
305
				e.preventDefault();
306
307
				$( '#mobile-css-select' ).slideDown();
308
				$( this ).hide();
309
			} );
310
311
			$( '.cancel-mobile-css' ).bind( 'click', function ( e ) {
312
				e.preventDefault();
313
314
				$( '#mobile-css-select' ).slideUp( function () {
315
					$( '.edit-mobile-css' ).show();
316
317
					$( '#mobile-css-visible' ).prop( 'checked', $( '#mobile-css' ).val() == '1' );
318
				} );
319
			} );
320
321
			$( '.save-mobile-css' ).bind( 'click', function ( e ) {
322
				e.preventDefault();
323
324
				$( '#mobile-css-select' ).slideUp();
325
				$( '#mobile-css-display' ).text( $( '#mobile-css-visible' ).prop( 'checked' ) ? 'Yes' : 'No' );
326
				$( '#mobile-css' ).val( $( '#mobile-css-visible' ).prop( 'checked' ) ? '1' : '0' );
327
				$( '.edit-mobile-css' ).show();
328
			} );
329
		} );
330
	</script>
331
	<?php
332
}
333
334
add_action( 'custom_css_submitbox_misc_actions', 'jetpack_mobile_css_settings' );
335
336
function jetpack_mobile_customizer_controls( $wp_customize ) {
337
	$wp_customize->add_setting( 'wp_mobile_custom_css' , array(
338
		'default' => true,
339
		'transport' => 'postMessage',
340
		'type' => 'option'
341
	) );
342
343
	$wp_customize->add_control( 'jetpack_mobile_css_control', array(
344
		'type' => 'checkbox',
345
		'label' => __( 'Include this CSS in the Mobile Theme', 'jetpack' ),
346
		'section' => 'jetpack_custom_css',
347
		'settings' => 'wp_mobile_custom_css',
348
	) );
349
}
350
351
add_action( 'jetpack_custom_css_customizer_controls', 'jetpack_mobile_customizer_controls' );
352
353
/**
354
 * Process edit post link tags for mobile apps.
355
 *
356
 * @param $link
357
 */
358
function jetpack_mobile_app_edit_post_link( $link ) {
359
	// If WordPress mobile apps, then hide edit links to avoid confusion/conflict.
360
	if ( Jetpack_User_Agent_Info::is_mobile_app() ) {
361
		return '';
362
	}
363
	return $link;
364
}
365
add_filter( 'edit_post_link', 'jetpack_mobile_app_edit_post_link', 20 );
366
367
function jetpack_mobile_save_css_settings() {
368
	update_option( 'wp_mobile_custom_css', isset( $_POST['mobile_css'] ) && ! empty( $_POST['mobile_css'] ) );
369
}
370
371
add_action( 'safecss_save_pre', 'jetpack_mobile_save_css_settings' );
372