/modules/minileven/minileven.php

  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 == get_option('wp_mobile_disable') ) 
  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. function jetpack_mobile_exclude() { 
  56. $exclude = false; 
  57. $pages_to_exclude = array( 
  58. 'wp-admin',  
  59. 'wp-comments-post.php',  
  60. 'wp-mail.php',  
  61. 'wp-login.php',  
  62. 'wp-activate.php',  
  63. ); 
  64. foreach ( $pages_to_exclude as $exclude_page ) { 
  65. if ( strstr( strtolower( $_SERVER['REQUEST_URI'] ), $exclude_page ) ) 
  66. $exclude = true; 
  67.  
  68. if ( defined( 'DOING_AJAX' ) && true === DOING_AJAX ) 
  69. $exclude = false; 
  70.  
  71. if ( isset( $GLOBALS['wp_customize'] ) ) 
  72. return true; 
  73.  
  74. return $exclude; 
  75.  
  76. function wp_mobile_get_main_template() { 
  77. remove_action( 'option_template', 'jetpack_mobile_template' ); 
  78. $template = get_option( 'template' ); 
  79. add_action( 'option_template', 'jetpack_mobile_template' ); 
  80. return $template; 
  81.  
  82. function wp_mobile_get_main_stylesheet() { 
  83. remove_action( 'option_stylesheet', 'jetpack_mobile_stylesheet' ); 
  84. $stylesheet = get_option( 'stylesheet' ); 
  85. add_action( 'option_stylesheet', 'jetpack_mobile_stylesheet' ); 
  86. return $stylesheet; 
  87.  
  88. function jetpack_mobile_stylesheet( $theme ) { 
  89. /** 
  90. * Filter Jetpack's Mobile stylesheet. 
  91. * 
  92. * @module minileven 
  93. * 
  94. * @since 1.8.0 
  95. * 
  96. * @param string $theme Theme. 
  97. */ 
  98. return apply_filters( 'jetpack_mobile_stylesheet', 'pub/minileven', $theme ); 
  99.  
  100. function jetpack_mobile_template( $theme ) { 
  101. /** 
  102. * Filter Jetpack's Mobile template. 
  103. * 
  104. * @module minileven 
  105. * 
  106. * @since 1.8.0 
  107. * 
  108. * @param string $theme Theme. 
  109. */ 
  110. return apply_filters( 'jetpack_mobile_template', 'pub/minileven', $theme ); 
  111.  
  112. function jetpack_mobile_available() { 
  113. 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>'; 
  114.  
  115. function jetpack_mobile_request_handler() { 
  116. global $wpdb; 
  117. if (isset($_GET['ak_action'])) { 
  118. $url = parse_url( get_bloginfo( 'url' ) ); 
  119. $domain = $url['host']; 
  120. if (!empty($url['path'])) { 
  121. $path = $url['path']; 
  122. else { 
  123. $path = '/'; 
  124. $redirect = false; 
  125. switch ($_GET['ak_action']) { 
  126. case 'reject_mobile': 
  127. setcookie( 
  128. 'akm_mobile' 
  129. , 'false' 
  130. , time() + 300000 
  131. , $path 
  132. , $domain 
  133. ); 
  134. $redirect = true; 
  135.  
  136. /** 
  137. * In Jetpack's Mobile theme, fires after the user taps on the link to display a full version of the site. 
  138. * 
  139. * @module minileven 
  140. * 
  141. * @since 1.8.0 
  142. */ 
  143. do_action( 'mobile_reject_mobile' ); 
  144. break; 
  145. case 'force_mobile': 
  146. case 'accept_mobile': 
  147. setcookie( 
  148. 'akm_mobile' 
  149. , 'true' 
  150. , time() + 300000 
  151. , $path 
  152. , $domain 
  153. ); 
  154. $redirect = true; 
  155.  
  156. /** 
  157. * In Jetpack's Mobile theme, fires after the user taps on the link to go back from full site to mobile site. 
  158. * 
  159. * @module minileven 
  160. * 
  161. * @since 1.8.0 
  162. */ 
  163. do_action( 'mobile_force_mobile' ); 
  164. break; 
  165. if ($redirect) { 
  166. if ( isset( $_GET['redirect_to'] ) && $_GET['redirect_to'] ) { 
  167. $go = urldecode( $_GET['redirect_to'] ); 
  168. } else if (!empty($_SERVER['HTTP_REFERER'])) { 
  169. $go = $_SERVER['HTTP_REFERER']; 
  170. else { 
  171. $go = remove_query_arg( array( 'ak_action' ) ); 
  172. wp_safe_redirect( $go ); 
  173. exit; 
  174. add_action('init', 'jetpack_mobile_request_handler'); 
  175.  
  176. function jetpack_mobile_theme_setup() { 
  177. if ( jetpack_check_mobile() ) { 
  178. // Redirect to download page if user clicked mobile app promo link in mobile footer 
  179. if ( isset( $_GET['app-download'] ) ) { 
  180. /** 
  181. * Fires before you're redirected to download page if you clicked the mobile app promo link in mobile footer 
  182. * 
  183. * @module minileven 
  184. * 
  185. * @since 1.8.0 
  186. * 
  187. * @param string $_GET['app-download'] app-download URL parameter. 
  188. */ 
  189. do_action( 'mobile_app_promo_download', $_GET['app-download'] ); 
  190.  
  191. switch ( $_GET['app-download'] ) { 
  192. case 'android': 
  193. header( 'Location: market://search?q=pname:org.wordpress.android' ); 
  194. exit; 
  195. break; 
  196. case 'ios': 
  197. header( 'Location: http://itunes.apple.com/us/app/wordpress/id335703880?mt=8' ); 
  198. exit; 
  199. break; 
  200. case 'blackberry': 
  201. header( 'Location: http://blackberry.wordpress.org/download/' ); 
  202. exit; 
  203. break; 
  204.  
  205. add_action('stylesheet', 'jetpack_mobile_stylesheet'); 
  206. add_action('template', 'jetpack_mobile_template'); 
  207. add_action('option_template', 'jetpack_mobile_template'); 
  208. add_action('option_stylesheet', 'jetpack_mobile_stylesheet'); 
  209.  
  210. if ( class_exists( 'Jetpack_Custom_CSS' ) && method_exists( 'Jetpack_Custom_CSS', 'disable' ) && ! get_option( 'wp_mobile_custom_css' ) ) 
  211. add_action( 'init', array( 'Jetpack_Custom_CSS', 'disable' ), 11 ); 
  212.  
  213. /** 
  214. * Fires after Jetpack's mobile theme has been setup. 
  215. * 
  216. * @module minileven 
  217. * 
  218. * @since 1.8.0 
  219. */ 
  220. do_action( 'mobile_setup' ); 
  221.  
  222. // Need a hook after plugins_loaded (since this code won't be loaded in Jetpack 
  223. // until then) but after init (because it has its own init hooks to add). 
  224. add_action( 'setup_theme', 'jetpack_mobile_theme_setup' ); 
  225.  
  226. if (isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'false') { 
  227. add_action('wp_footer', 'jetpack_mobile_available'); 
  228.  
  229. function jetpack_mobile_app_promo() { 
  230. ?> 
  231. <script type="text/javascript"> 
  232. if ( ! navigator.userAgent.match( /wp-(iphone|android|blackberry|nokia|windowsphone)/i ) ) { 
  233. if ( ( navigator.userAgent.match( /iphone/i ) ) || ( navigator.userAgent.match( /ipod/i ) ) ) 
  234. 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 />' ); 
  235. else if ( ( navigator.userAgent.match( /android/i ) ) && ( null == navigator.userAgent.match( /playbook/i ) && null == navigator.userAgent.match( /bb10/i ) ) ) 
  236. 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 />' ); 
  237. else if ( ( navigator.userAgent.match( /blackberry/i ) ) || ( navigator.userAgent.match( /playbook/i ) ) || ( navigator.userAgent.match( /bb10/i ) ) ) 
  238. 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 />' ); 
  239. </script> 
  240. <?php 
  241.  
  242. add_action( 'wp_mobile_theme_footer', 'jetpack_mobile_app_promo' ); 
  243.  
  244. /** 
  245. * Adds an option to allow your Custom CSS to also be applied to the Mobile Theme. 
  246. * It's disabled by default, but this should allow people who know what they're 
  247. * doing to customize the mobile theme. 
  248. */ 
  249. function jetpack_mobile_css_settings() { 
  250. $mobile_css = get_option( 'wp_mobile_custom_css' ); 
  251.  
  252. ?> 
  253. <div class="misc-pub-section"> 
  254. <label><?php esc_html_e( 'Mobile-compatible:', 'jetpack' ); ?></label> 
  255. <span id="mobile-css-display"><?php echo $mobile_css ? __( 'Yes', 'jetpack' ) : __( 'No', 'jetpack' ); ?></span> 
  256. <a class="edit-mobile-css hide-if-no-js" href="#mobile-css"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a> 
  257. <div id="mobile-css-select" class="hide-if-js"> 
  258. <input type="hidden" name="mobile_css" id="mobile-css" value="<?php echo intval( $mobile_css ); ?>" /> 
  259. <label> 
  260. <input type="checkbox" id="mobile-css-visible" <?php checked( get_option( 'wp_mobile_custom_css' ) ); ?> /> 
  261. <?php esc_html_e( 'Include this CSS in the Mobile Theme', 'jetpack' ); ?> 
  262. </label> 
  263. <p> 
  264. <a class="save-mobile-css hide-if-no-js button" href="#mobile-css"><?php esc_html_e( 'OK', 'jetpack' ); ?></a> 
  265. <a class="cancel-mobile-css hide-if-no-js" href="#mobile-css"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a> 
  266. </p> 
  267. </div> 
  268. </div> 
  269. <script type="text/javascript"> 
  270. jQuery( function ( $ ) { 
  271. $( '.edit-mobile-css' ).bind( 'click', function ( e ) { 
  272. e.preventDefault(); 
  273.  
  274. $( '#mobile-css-select' ).slideDown(); 
  275. $( this ).hide(); 
  276. } ); 
  277.  
  278. $( '.cancel-mobile-css' ).bind( 'click', function ( e ) { 
  279. e.preventDefault(); 
  280.  
  281. $( '#mobile-css-select' ).slideUp( function () { 
  282. $( '.edit-mobile-css' ).show(); 
  283.  
  284. $( '#mobile-css-visible' ).prop( 'checked', $( '#mobile-css' ).val() == '1' ); 
  285. } ); 
  286. } ); 
  287.  
  288. $( '.save-mobile-css' ).bind( 'click', function ( e ) { 
  289. e.preventDefault(); 
  290.  
  291. $( '#mobile-css-select' ).slideUp(); 
  292. $( '#mobile-css-display' ).text( $( '#mobile-css-visible' ).prop( 'checked' ) ? 'Yes' : 'No' ); 
  293. $( '#mobile-css' ).val( $( '#mobile-css-visible' ).prop( 'checked' ) ? '1' : '0' ); 
  294. $( '.edit-mobile-css' ).show(); 
  295. } ); 
  296. } ); 
  297. </script> 
  298. <?php 
  299.  
  300. add_action( 'custom_css_submitbox_misc_actions', 'jetpack_mobile_css_settings' ); 
  301.  
  302. function jetpack_mobile_customizer_controls( $wp_customize ) { 
  303. $wp_customize->add_setting( 'wp_mobile_custom_css' , array( 
  304. 'default' => true,  
  305. 'transport' => 'postMessage',  
  306. 'type' => 'option' 
  307. ) ); 
  308.  
  309. $wp_customize->add_control( 'jetpack_mobile_css_control', array( 
  310. 'type' => 'checkbox',  
  311. 'label' => __( 'Include this CSS in the Mobile Theme', 'jetpack' ),  
  312. 'section' => 'jetpack_custom_css',  
  313. 'settings' => 'wp_mobile_custom_css',  
  314. ) ); 
  315.  
  316. add_action( 'jetpack_custom_css_customizer_controls', 'jetpack_mobile_customizer_controls' ); 
  317.  
  318. function jetpack_mobile_save_css_settings() { 
  319. update_option( 'wp_mobile_custom_css', isset( $_POST['mobile_css'] ) && ! empty( $_POST['mobile_css'] ) ); 
  320.  
  321. add_action( 'safecss_save_pre', 'jetpack_mobile_save_css_settings' ); 
.