| 1 |
<?php |
| 2 |
/** |
| 3 |
* Widget Name: PC Switcher Widget |
| 4 |
* Plugin URI: https://github.com/thingsym/multi-device-switcher |
| 5 |
* Description: PC Switcher Widget add-on for the Multi Device Switcher. Use this widget to add the PC Switcher to a widget. |
| 6 |
* Version: 1.5.2 |
| 7 |
* Author: thingsym |
| 8 |
* Author URI: http://www.thingslabo.com/ |
| 9 |
* License: GPL2 |
| 10 |
* Text Domain: multi-device-switcher |
| 11 |
* Domain Path: /languages/ |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
* Copyright 2013 thingsym (http://www.thingslabo.com/) |
| 16 |
* |
| 17 |
* This program is free software; you can redistribute it and/or modify |
| 18 |
* it under the terms of the GNU General Public License as published by |
| 19 |
* the Free Software Foundation; either version 2 of the License, or |
| 20 |
* (at your option) any later version. |
| 21 |
* |
| 22 |
* This program is distributed in the hope that it will be useful, |
| 23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 |
* GNU General Public License for more details. |
| 26 |
* |
| 27 |
* You should have received a copy of the GNU General Public License |
| 28 |
* along with this program; if not, write to the Free Software |
| 29 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA |
| 30 |
*/ |
| 31 |
|
| 32 |
/** |
| 33 |
* PC Switcher Widget |
| 34 |
* |
| 35 |
* @since 1.2 |
| 36 |
*/ |
| 37 |
if ( class_exists( 'Multi_Device_Switcher' ) ) { |
| 38 |
add_action( 'widgets_init', 'pc_switcher_load_widgets' ); |
| 39 |
} |
| 40 |
|
| 41 |
function pc_switcher_load_widgets() { |
| 42 |
register_widget( 'PC_Switcher' ); |
| 43 |
} |
| 44 |
|
| 45 |
class PC_Switcher extends WP_Widget { |
| 46 |
|
| 47 |
function __construct() { |
| 48 |
load_plugin_textdomain( 'multi-device-switcher', false, 'multi-device-switcher/languages' ); |
| 49 |
$widget_ops = array( 'classname' => 'widget_pc_switcher', 'description' => __( 'Add the PC Switcher to a widget.', 'multi-device-switcher' ) ); |
| 50 |
parent::__construct( 'pc-switcher', __( 'PC Switcher', 'multi-device-switcher' ), $widget_ops ); |
| 51 |
$this->alt_option_name = 'widget_pc_switcher'; |
| 52 |
|
| 53 |
add_action( 'save_post', array( &$this, 'flush_widget_cache' ) ); |
| 54 |
add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) ); |
| 55 |
add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) ); |
| 56 |
} |
| 57 |
|
| 58 |
function widget( $args, $instance ) { |
| 59 |
if ( ! function_exists( 'multi_device_switcher_add_pc_switcher' ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
global $multi_device_switcher; |
| 64 |
$name = $multi_device_switcher->get_device_theme(); |
| 65 |
|
| 66 |
if ( $name && 'None' !== $name ) { |
| 67 |
|
| 68 |
$cache = wp_cache_get( 'widget_pc_switcher', 'widget' ); |
| 69 |
|
| 70 |
if ( ! is_array( $cache ) ) { |
| 71 |
$cache = array(); |
| 72 |
} |
| 73 |
|
| 74 |
if ( isset( $cache[ $args['widget_id'] ] ) ) { |
| 75 |
echo $cache[ $args['widget_id'] ]; |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
ob_start(); |
| 80 |
|
| 81 |
echo $args['before_widget']; |
| 82 |
multi_device_switcher_add_pc_switcher(); |
| 83 |
echo $args['after_widget']; |
| 84 |
|
| 85 |
$cache[ $args['widget_id'] ] = ob_get_flush(); |
| 86 |
wp_cache_set( 'widget_pc_switcher', $cache, 'widget' ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
function update( $new_instance, $old_instance ) { |
| 91 |
$instance = $old_instance; |
| 92 |
|
| 93 |
$this->flush_widget_cache(); |
| 94 |
|
| 95 |
$alloptions = wp_cache_get( 'alloptions', 'options' ); |
| 96 |
if ( isset( $alloptions['widget_pc_switcher'] ) ) { |
| 97 |
delete_option( 'widget_pc_switcher' ); |
| 98 |
} |
| 99 |
|
| 100 |
return $instance; |
| 101 |
} |
| 102 |
|
| 103 |
function flush_widget_cache() { |
| 104 |
wp_cache_delete( 'widget_pc_switcher', 'widget' ); |
| 105 |
} |
| 106 |
|
| 107 |
function form( $instance ) { |
| 108 |
} |
| 109 |
} |
| 110 |
?> |
| 111 |
|