-
Notifications
You must be signed in to change notification settings - Fork 0
/
themeswitcher.plugin.php
132 lines (115 loc) · 3.62 KB
/
themeswitcher.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* ThemeSwitcher - Allows visitors to change the theme of the site.
*
* Usage: http://domain.com/?theme_dir=themedir or
* add <?php $theme->switcher(); ?> somewere.
*
*
*/
class ThemeSwitcher extends Plugin
{
// True if template was shown, false otherwise
private $shown= false;
function action_init()
{
if ( !empty($_GET['theme_dir'] ) || !empty( $_POST['theme_dir'] ) ) {
$new_theme_dir= empty( $_GET['theme_dir'] ) ? $_POST['theme_dir'] : $_GET['theme_dir'];
$all_themes= Themes::get_all();
if ( array_key_exists( $new_theme_dir, $all_themes ) ) {
if ( !isset($_COOKIE['theme_dir'] ) || ( isset($_COOKIE['theme_dir'] ) && ( $_COOKIE['theme_dir'] != $new_theme_dir ) ) ) {
$_COOKIE['theme_dir'] = $new_theme_dir; // Without this, the cookie isn't get in time to change the theme NOW.
setcookie( 'theme_dir', $new_theme_dir );
}
}
}
$this->add_template( 'switcher', dirname( __FILE__ ) . '/themeswitcher.php' );
}
/**
* Sets default to always show themeswitcher in footer if not using $theme->swithcer();
**/
public function action_plugin_activation( $file )
{
if(Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__)) {
if ( Options::get( 'themeswitcher__show' ) == null ) {
Options::set( 'themeswitcher__show', 1 );
}
}
}
/**
* Call $theme->switcher() in your theme to display the template where you want.
*/
function theme_switcher( $theme ) {
if (!$this->shown) {
$this->shown= true;
return $theme->fetch('switcher');
}
}
/**
* Failsafe, if $theme->switcher() was not called, display the template in the footer.
* If you enabled it.
*/
function theme_footer( $theme ) {
if (!$this->shown && Options::get( 'themeswitcher__show')) {
$this->shown= true;
return $theme->fetch('switcher');
}
}
function filter_option_get_value($value, $name)
{
if (($name == 'theme_dir') && isset($_COOKIE['theme_dir'])) {
return $_COOKIE['theme_dir'];
}
else {
return $value;
}
}
/**
* Add our menu to the FormUI for plugins.
*
* @param array $actions Array of menu items for this plugin.
* @param string $plugin_id A unique plugin ID, it needs to match ours.
* @return array Original array with our added menu.
*/
public function filter_plugin_config( $actions, $plugin_id ) {
if ( $plugin_id == $this->plugin_id ) {
$actions[] = 'Configure';
}
return $actions;
}
/**
* Handle calls from FormUI actions.
* Show the form to manage the plugin's options.
*
* @param string $plugin_id A unique plugin ID, it needs to match ours.
* @param string $action The menu item the user clicked.
*/
public function action_plugin_ui( $plugin_id, $action ) {
if ( $plugin_id == $this->plugin_id ) {
switch ( $action ) {
case 'Configure':
$themes = array_keys( Themes::get_all_data() );
$themes = array_combine( $themes, $themes );
$ui = new FormUI( 'themeswitcher' );
$ts_s = $ui->append( 'select', 'selected_themes', 'themeswitcher__selected_themes', 'Select themes to offer:' );
$ts_s->multiple= true;
$ts_s->options =$themes;
$ts_y = $ui->append( 'select', 'show', 'themeswitcher__show',
_t('If not showing with $theme->switcher() always show in footer: ') );
$ts_y->options = array( '0' => _t('No'), '1' => _t('Yes') );
$ui->append( 'submit', 'save', 'Save' );
$ui->out();
break;
}
}
}
/**
* Fail-safe method to force options to be saved in Habari's options table.
*
* @return bool Return true to force options to be saved in Habari's options table.
*/
public function save_options( $ui ) {
return true;
}
}
?>