Advertisement
BakerMan

Flip currency symbol / value in cost field

Oct 30th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. /**
  2.  * Handle reversing the position of the currency symbol such that it displays
  3.  * on the right, rather than the left, and optionally adds some space between
  4.  * them.
  5.  */
  6. class Tribe_Reverse_Currency_Position
  7. {
  8.     public static $space = '';
  9.  
  10.  
  11.     public static function take_effect($space = '') {
  12.         self::$space = $space;
  13.         add_filter('tribe_get_cost', array(__CLASS__, 'filter_cost'), 10, 3);
  14.     }
  15.  
  16.     public static function filter_cost($cost, $post_id, $has_symbol) {
  17.         if (!$has_symbol) return $cost;
  18.         $symbol = self::post_currency_symbol($post_id);
  19.  
  20.         if (0 === strpos($cost, $symbol))
  21.             return substr($cost, strlen($symbol)) . self::$space . $symbol;
  22.         else return $cost;
  23.     }
  24.  
  25.     protected static function post_currency_symbol($post_id) {
  26.         if ( ! ($symbol = tribe_get_event_meta($post_id, '_EventCurrencySymbol', true)) )
  27.             return tribe_get_option( 'defaultCurrencySymbol', '$' );
  28.         else return $symbol;
  29.     }
  30. }
  31.  
  32.  
  33. /**
  34.  * Without any parameters it simply swaps the currency symbol so that it displays on
  35.  * the right of the value. Optionally a string can be passed to separate the value
  36.  * and the symbol with some space. Examples:
  37.  *
  38.  *  Tribe_Reverse_Currency_Position::take_effect(' ');
  39.  *  Tribe_Reverse_Currency_Position::take_effect(' ');
  40.  */
  41. Tribe_Reverse_Currency_Position::take_effect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement