brasofilo

Stack Overflow 18968615

Sep 23rd, 2013
615
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * RTL-Tester plugin.
  4.  *
  5.  * Adds a button to the admin bar that allow super admins to switch the text direction of the site.
  6.  *
  7.  * @package RTL_Tester
  8.  * @author Automattic
  9.  * @author Yoav Farhi
  10.  * @version 1.0.3
  11.  *
  12.  * @wordpress
  13.  * Plugin Name: RTL Tester
  14.  * Plugin URI: http://wordpress.org/extend/plugins/rtl-tester/
  15.  * Description: This plugin adds a button to the admin bar that allow super admins to switch the text direction of the site. It can be used to test WordPress themes and plugins with Right To Left (RTL) text direction.
  16.  * Author: <a href="http://blog.yoavfarhi.com">Yoav Farhi</a>, <a href="http://automattic.com">Automattic</a>
  17.  * Version: 1.0.3
  18.  */
  19.  
  20. /**
  21.  * Plugin class for RTL Tester plugin
  22.  *
  23.  * @package RTL_Tester
  24.  */
  25. class RTLTester {
  26.  
  27.     /**
  28.      * Loads plugin textdomain, and hooks in further actions.
  29.      */
  30.     function __construct() {
  31.  
  32.         load_plugin_textdomain( 'rtl-tester', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
  33.  
  34.         add_action( 'init', array( $this, 'set_direction' ) );
  35.         add_action( 'admin_bar_menu', array( $this, 'admin_bar_rtl_switcher' ), 999 );
  36.     }
  37.  
  38.     /**
  39.      * Adds button to admin bar.
  40.      *
  41.      * @global object $wp_admin_bar Most likely instance of WP_Admin_Bar but this is filterable.
  42.      *
  43.      * @return null Retuns early if not site admin, or admin bar should not be showing.
  44.      */
  45.     function admin_bar_rtl_switcher() {
  46.         global $wp_admin_bar;
  47.  
  48.         if ( ! is_super_admin() || ! is_admin_bar_showing() || is_admin() )
  49.           return;
  50.  
  51.         // Get opposite direction for button text
  52.         $direction = is_rtl() ? 'ltr' : 'rtl';
  53.  
  54.         $wp_admin_bar->add_menu(
  55.             array(
  56.                 'id'    => 'RTL',
  57.                 'title' => sprintf( __( 'Switch to %s', 'rtl-tester' ), strtoupper( $direction ) ),
  58.                 'href'  => add_query_arg( array( 'd' => $direction ) )
  59.             )
  60.         );
  61.     }
  62.  
  63.     /**
  64.      * Save the currently chosen direction on a per-user basis.
  65.      *
  66.      * @global WP_Locale $wp_locale Locale object.
  67.      * @global WP_Styles $wp_styles Styles object.
  68.      */
  69.     function set_direction() {
  70.         global $wp_locale, $wp_styles;
  71.  
  72.         $_user_id = get_current_user_id();
  73.         if( is_admin() )
  74.             $direction = 'ltr';
  75.         else
  76.         {
  77.             if ( isset( $_GET['d'] ) ) {
  78.                 $direction = $_GET['d'] == 'rtl' ? 'rtl' : 'ltr';
  79.                 update_user_meta( $_user_id, 'rtladminbar', $direction );
  80.             } else {
  81.                 $direction = get_user_meta( $_user_id, 'rtladminbar', true );
  82.                 if ( false === $direction )
  83.                     $direction = isset( $wp_locale->text_direction ) ? $wp_locale->text_direction : 'ltr' ;
  84.             }
  85.         }
  86.         $wp_locale->text_direction = $direction;
  87.         if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
  88.             $wp_styles = new WP_Styles();
  89.             $wp_styles->text_direction = $direction;
  90.         }
  91.     }
  92.  
  93. }
  94.  
  95. new RTLTester;
RAW Paste Data