| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class ThemeSwitcher extends Plugin |
|---|
| 13 | { |
|---|
| 14 | |
|---|
| 15 | private $shown= false; |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | function info() |
|---|
| 23 | { |
|---|
| 24 | return array ( |
|---|
| 25 | 'name' => 'ThemeSwitcher', |
|---|
| 26 | 'url' => 'http://habariproject.org/', |
|---|
| 27 | 'author' => 'Habari Community', |
|---|
| 28 | 'authorurl' => 'http://habariproject.org/', |
|---|
| 29 | 'version' => '1.1', |
|---|
| 30 | 'description' => 'Allows visitors to change the theme of the site.', |
|---|
| 31 | 'license' => 'Apache License 2.0', |
|---|
| 32 | 'copyright' => '2008' |
|---|
| 33 | ); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | function action_init() |
|---|
| 37 | { |
|---|
| 38 | if ( !empty($_GET['theme_dir'] ) || !empty( $_POST['theme_dir'] ) ) { |
|---|
| 39 | $new_theme_dir= empty( $_GET['theme_dir'] ) ? $_POST['theme_dir'] : $_GET['theme_dir']; |
|---|
| 40 | $all_themes= Themes::get_all(); |
|---|
| 41 | if ( array_key_exists( $new_theme_dir, $all_themes ) ) { |
|---|
| 42 | if ( !isset($_COOKIE['theme_dir'] ) || ( isset($_COOKIE['theme_dir'] ) && ( $_COOKIE['theme_dir'] != $new_theme_dir ) ) ) { |
|---|
| 43 | $_COOKIE['theme_dir'] = $new_theme_dir; |
|---|
| 44 | setcookie( 'theme_dir', $new_theme_dir ); |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | $this->add_template( 'switcher', dirname( __FILE__ ) . '/themeswitcher.php' ); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | public function action_plugin_activation( $file ) |
|---|
| 58 | { |
|---|
| 59 | if(Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__)) { |
|---|
| 60 | if ( Options::get( 'themeswitcher__show' ) == null ) { |
|---|
| 61 | Options::set( 'themeswitcher__show', 1 ); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | function theme_switcher( $theme ) { |
|---|
| 71 | if (!$this->shown) { |
|---|
| 72 | $this->shown= true; |
|---|
| 73 | return $theme->fetch('switcher'); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | function theme_footer( $theme ) { |
|---|
| 82 | if (!$this->shown && Options::get( 'themeswitcher__show')) { |
|---|
| 83 | $this->shown= true; |
|---|
| 84 | return $theme->fetch('switcher'); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | function filter_option_get_value($value, $name) |
|---|
| 89 | { |
|---|
| 90 | if (($name == 'theme_dir') && isset($_COOKIE['theme_dir'])) { |
|---|
| 91 | return $_COOKIE['theme_dir']; |
|---|
| 92 | } |
|---|
| 93 | else { |
|---|
| 94 | return $value; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | public function filter_plugin_config( $actions, $plugin_id ) { |
|---|
| 107 | if ( $plugin_id == $this->plugin_id ) { |
|---|
| 108 | $actions[] = 'Configure'; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | return $actions; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | public function action_plugin_ui( $plugin_id, $action ) { |
|---|
| 122 | if ( $plugin_id == $this->plugin_id ) { |
|---|
| 123 | switch ( $action ) { |
|---|
| 124 | case 'Configure': |
|---|
| 125 | $themes = array_keys( Themes::get_all_data() ); |
|---|
| 126 | $themes = array_combine( $themes, $themes ); |
|---|
| 127 | $ui = new FormUI( 'themeswitcher' ); |
|---|
| 128 | $ts_s = $ui->append( 'select', 'selected_themes', 'themeswitcher__selected_themes', 'Select themes to offer:' ); |
|---|
| 129 | $ts_s->multiple= true; |
|---|
| 130 | $ts_s->options =$themes; |
|---|
| 131 | $ts_y = $ui->append( 'select', 'show', 'themeswitcher__show', |
|---|
| 132 | _t('If not showing with $theme->switcher() always show in footer: ') ); |
|---|
| 133 | $ts_y->options = array( '0' => _t('No'), '1' => _t('Yes') ); |
|---|
| 134 | $ui->append( 'submit', 'save', 'Save' ); |
|---|
| 135 | $ui->out(); |
|---|
| 136 | break; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | public function save_options( $ui ) { |
|---|
| 147 | return true; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | ?> |
|---|