Skip to content

Instantly share code, notes, and snippets.

@jim912
Created July 10, 2014 13:14
Show Gist options
  • Save jim912/0dbb976daada7b4c9cf7 to your computer and use it in GitHub Desktop.
Save jim912/0dbb976daada7b4c9cf7 to your computer and use it in GitHub Desktop.
StaticPressでモバイル用に書き出すサンプルプラグイン(WP SiteManager向け) sp ディレクトリに書き出される。
<?php
/*
Plugin Name: StaticPress for Mobile
Plugin URI:
Description:
Author: Hitoshi Omagari
Version: 1.0
Author URI:
*/
class StaticPress_for_Mobile {
var $remote_get_option;
var $enable_theme_dirs;
public function __construct() {
// モバイル用テーマの取得(WP SiteManager用)
add_action( 'init' , array( $this, 'init' ) );
// モバイル用テーマのファイルを書き出すように
add_action( 'StaticPress::update_url' , array( $this, 'enable_mobile_themes' ) );
// モバイル用のチェックボックスを出力
add_action( 'StaticPress::static_press_page_button_before', array( $this, 'add_mobile_check' ) );
// ajaxにチェックボックスの状態が渡るようにする
add_action( 'StaticPress::static_press_init_data' , array( $this, 'add_mobile_data' ) );
add_action( 'StaticPress::static_press_fetch_data' , array( $this, 'add_mobile_data' ) );
// モバイル用書き出しの場合にフィルターフックをかける
add_action( 'StaticPress::pre_ajax_init' , array( $this, 'add_mobile_filter' ) );
add_action( 'StaticPress::pre_ajax_fetch' , array( $this, 'add_mobile_filter' ) );
// google sitemap xmlも自動化
add_filter( 'StaticPress::get_urls' , array( $this, 'add_sitemap_url' ) );
add_filter( 'StaticPress::static_files_filter' , array( $this, 'allow_xsl_file' ) );
// いらないやつら
add_filter( 'StaticPress::author_url' , '__return_empty_array' );
add_filter( 'StaticPress::terms_url' , '__return_empty_array' );
}
public function init() {
global $WP_SiteManager;
$regexes = get_option( 'sitemanager_device_rules', array() );
$this->enable_theme_dirs = array();
foreach ( $regexes as $group => $arr ) {
if ( isset( $arr['theme'] ) && $arr['theme'] ) {
$theme_dir = '/' . trailingslashit( str_replace( trailingslashit( ABSPATH ), '', WP_CONTENT_DIR . '/themes/' . $arr['theme'] ) );
$this->enable_theme_dirs[] = $theme_dir;
}
}
}
public function mobile_remote_get_option( $remote_get_option ) {
$remote_get_option['user-agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53';
return $remote_get_option;
}
public function mobile_file_dest( $file_dest, $url, $static_dir, $static_url ) {
$file_dest = str_replace( $static_dir, $static_dir . 'sp/', $file_dest );
return $file_dest;
}
public function enable_mobile_themes( $url ) {
global $wpdb;
if ( $url['type'] != 'static_file' ) {
return $url;
}
$table = $wpdb->prefix . 'urls';
$is_mobile_theme_file = false;
foreach ( $this->enable_theme_dirs as $dir ) {
if ( strpos( $url['url'], $dir ) === 0 ) {
$is_mobile_theme_file = true;
}
}
$dest = rtrim( static_press_admin::static_dir(), '/' ) . $url['url'];
$source = rtrim( ABSPATH, '/' ) . $url['url'];
if ( $is_mobile_theme_file && $url['enable'] === 0 && ( ! file_exists( $dest ) || filemtime( $dest ) < filemtime( $source ) ) ) {
$wpdb->update( $table, array( 'enable' => 1 ), array( 'url' => $url['url'] ), array( '%d' ), array( '%s' ) );
}
}
public function add_mobile_check() {
?>
<label for="mobile-rebuild">
<input type="checkbox" id="mobile-rebuild">
モバイル用再構築
</label>
<?php
}
public function add_mobile_data( $data ) {
$data .= ", mobile: $('#mobile-rebuild').prop('checked')";
return $data;
}
public function add_mobile_filter() {
if ( isset( $_POST['mobile'] ) && $_POST['mobile'] === 'true' ) {
add_filter( 'StaticPress::remote_get_option' , array( $this, 'mobile_remote_get_option' ) );
add_filter( 'StaticPress::file_dest' , array( $this, 'mobile_file_dest' ), 10, 4 );
}
}
public function add_sitemap_url( $urls ) {
$sitemap_content = wp_remote_get( home_url( '/sitemap.xml' ) );
if ( ! $sitemap_content || $sitemap_content['response']['code'] != 200 ) {
return $urls;
}
preg_match_all( '#<loc>(.*)</loc>#', $sitemap_content['body'], $matches );
preg_match_all( '#<lastmod>(.*)</lastmod>#', $sitemap_content['body'], $time_matches );
$last_modified = max( $time_matches[1] );
$urls[] = array(
'type' => 'other_page',
'url' => '/sitemap.xml',
'last_modified' => date( 'Y-m-d H:i:s', strtotime( $last_modified ) ),
);
if ( isset( $matches[1] ) && $matches[1] ) {
foreach ( $matches[1] as $key => $url ) {
$urls[] = array(
'type' => 'other_page',
'url' => str_replace( home_url(), '', $url ),
'last_modified' => date( 'Y-m-d H:i:s', strtotime( $time_matches[1][$key] ) ),
);
}
}
return $urls;
}
public function allow_xsl_file( $ext ) {
$ext[] = 'xsl';
return $ext;
}
} // class end.
new StaticPress_for_Mobile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment