| 1 |
<?php |
| 2 |
/* |
| 3 |
Name: 移动主题 |
| 4 |
URI: https://mp.weixin.qq.com/s/DAqil-PxyL8rxzWBiwlA3A |
| 5 |
Description: 给当前站点设置移动设备设置上使用单独的主题。 |
| 6 |
Version: 2.0 |
| 7 |
*/ |
| 8 |
class WPJAM_Mobile_Stylesheetl{ |
| 9 |
public static function __callStatic($method, $args){ |
| 10 |
return wpjam_call_option(static::class, $method, ...$args); |
| 11 |
} |
| 12 |
|
| 13 |
public static function get_fields(){ |
| 14 |
$themes = array_map(fn($v)=> $v->get('Name'), wp_get_themes(['allowed'=>true])); |
| 15 |
$themes = wpjam_pick($themes, [get_stylesheet()])+$themes; |
| 16 |
|
| 17 |
return ['mobile_stylesheet'=>['title'=>'移动主题', 'options'=>$themes]]; |
| 18 |
} |
| 19 |
|
| 20 |
public static function load(){ |
| 21 |
$mobile = self::get_setting('mobile_stylesheet'); |
| 22 |
$button = wpjam_register_page_action('set_mobile_stylesheet', [ |
| 23 |
'button_text' => '移动主题', |
| 24 |
'class' => 'mobile-theme button', |
| 25 |
'direct' => true, |
| 26 |
'confirm' => true, |
| 27 |
'response' => 'redirect', |
| 28 |
'callback' => fn()=> self::update_setting('mobile_stylesheet', wpjam_param('stylesheet', 'data')) |
| 29 |
])->get_button(['data'=>['stylesheet'=>'slug']]); |
| 30 |
|
| 31 |
wpjam_script(sprintf(<<<'JS' |
| 32 |
if(wp && wp.Backbone && wp.themes && wp.themes.view.Theme){ |
| 33 |
let render = wp.themes.view.Theme.prototype.render; |
| 34 |
|
| 35 |
wp.themes.view.Theme.prototype.render = function(){ |
| 36 |
render.apply(this, arguments); |
| 37 |
|
| 38 |
this.$el.find('.theme-actions').append(this.$el.data('slug') == %s ? '<span class="mobile-theme button button-primary">移动主题</span>' : (%s).replace('slug', this.$el.data('slug'))); |
| 39 |
}; |
| 40 |
} |
| 41 |
JS, wpjam_json_encode($mobile), wpjam_json_encode($button))); |
| 42 |
|
| 43 |
// wpjam_style('.mobile-theme{position: absolute; top: 45px; right: 18px;}'); |
| 44 |
} |
| 45 |
|
| 46 |
public static function add_hooks(){ |
| 47 |
$name = wp_is_mobile() ? self::get_setting('mobile_stylesheet') : null; |
| 48 |
$name = $name ?: ($_GET['wpjam_theme'] ?? null); |
| 49 |
$theme = $name ? wp_get_theme($name) : null; |
| 50 |
|
| 51 |
$theme && wpjam_map(['stylesheet', 'template'], fn($k)=> add_filter($k, fn()=> $theme->{'get_'.$k}())); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
wpjam_add_option_section('wpjam-basic', 'enhance', ['model'=>'WPJAM_Mobile_Stylesheet', 'admin_load'=>['base'=>'themes']]); |