forked from thingsym/multi-device-switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpc-switcher-widget.php
105 lines (81 loc) · 3.03 KB
/
pc-switcher-widget.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
<?php
/*
Widget Name: PC Switcher Widget (Sparkart Fork)
Plugin URI: https://github.com/SparkartGroupInc/multi-device-switcher
Description: PC Switcher Widget add-on for the Multi Device Switcher. Use this widget to add the PC Switcher to a widget.
Version: 1.3.0
Author: Sparkart
Author URI: http://www.sparkart.com/
License: GPL2
*/
/*
Copyright 2013 thingsym (http://www.thingslabo.com/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
/**
* PC Switcher Widget
*
* @since 1.2
*/
if ( class_exists('Multi_Device_Switcher') )
add_action('widgets_init', 'PC_Switcher_load_widgets');
function PC_Switcher_load_widgets() {
register_widget('PC_Switcher');
}
class PC_Switcher extends WP_Widget {
function __construct() {
load_plugin_textdomain('multi-device-switcher', false, 'multi-device-switcher/languages');
$widget_ops = array('classname' => 'widget_pc_switcher', 'description' => __( "Add the PC Switcher to a widget.", 'multi-device-switcher') );
parent::__construct('pc-switcher', __('PC Switcher', 'multi-device-switcher'), $widget_ops);
$this->alt_option_name = 'widget_pc_switcher';
add_action( 'save_post', array(&$this, 'flush_widget_cache') );
add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
}
function widget($args, $instance) {
if ( !function_exists( 'multi_device_switcher_add_pc_switcher' ) )
return;
global $multi_device_switcher;
$name = $multi_device_switcher->get_device_theme();
if ( $name && $name != 'None' ) {
$cache = wp_cache_get('widget_pc_switcher', 'widget');
if ( !is_array($cache) )
$cache = array();
if ( isset($cache[ $args['widget_id'] ]) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
extract($args);
echo $before_widget;
multi_device_switcher_add_pc_switcher();
echo $after_widget;
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set('widget_pc_switcher', $cache, 'widget');
}
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_pc_switcher']) )
delete_option('widget_pc_switcher');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_pc_switcher', 'widget');
}
function form( $instance ) {
}
}
?>