[ Index ]

MailPress 7.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-content/add-ons/ -> MailPress_filter_img.php (source)

   1  <?php
   2  if ( class_exists( 'MailPress' ) && !class_exists( 'MailPress_filter_img' ) )
   3  {
   4  /*
   5  Plugin Name: MailPress_filter_img
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/filter_img/
   7  Description: Mails : filter ALL html img tags before mailing them
   8  Version: 7.1
   9  */
  10  
  11  class MailPress_filter_img
  12  {
  13      const option_name = 'MailPress_filter_img';
  14  
  15  	function __construct()
  16      {
  17  // prepare mail
  18          add_filter( 'MailPress_img_mail_keepurl',         array( __CLASS__, 'img_mail_keepurl' ), 8, 1 );
  19          add_filter( 'MailPress_img_mail',             array( __CLASS__, 'img_mail' ), 8, 1 );
  20  
  21          if ( is_admin() )
  22          {
  23          // for link on plugin page
  24              add_filter( 'plugin_action_links',         array( __CLASS__, 'plugin_action_links' ), 10, 2 );
  25          // for settings
  26              add_filter( 'MailPress_styles',         array( __CLASS__, 'styles' ), 8, 2 );
  27              add_filter( 'MailPress_settings_tab',     array( __CLASS__, 'settings_tab' ), 50, 1 );
  28          }
  29      }
  30  
  31  // prepare mail
  32  	public static function img_mail_keepurl( $bool )
  33      {
  34          $option = get_option( MailPress_filter_img::option_name );
  35  
  36          if ( isset( $option['keepurl'] ) ) return true;
  37          return $bool;
  38      }
  39  
  40  	public static function img_mail( $img )
  41      {
  42          $wstyle     = $inline_style = $default_style = array();
  43          $wattr     = $inline_attr  = $default_attr  = array();
  44  
  45          $option    = get_option( MailPress_filter_img::option_name );
  46  
  47          if ( isset( $option['align'] ) )
  48          {
  49              if ( 'center' == $option['align'] )     $default_style['text-align'] = 'center';
  50              elseif ( 'none' != $option['align'] ) $default_style['float']      = $option['align'];
  51          }
  52  
  53          if ( !empty( $option['extra_style'] ) )
  54          {
  55               $x = self::retrieve_styles( stripslashes( $option['extra_style'] ) );
  56              foreach( $x as $k => $v ) $default_style[$k] = $v;
  57          }
  58  
  59          $x = self::retrieve_attributes( $img );
  60          foreach( $x as $k => $v )
  61          {
  62              switch ( $k )
  63              {
  64                  case 'style' :
  65                      $inline_style = self::retrieve_styles( $v );
  66                  break;
  67                  case 'class' :
  68                      $inline_attr[$k] = $v;
  69                      if ( false !== stripos( $v, 'left' ) )  $wstyle['float'] = 'left';
  70                      if ( false !== stripos( $v, 'right' ) ) $wstyle['float'] = 'right';
  71                  break;
  72                  default :
  73                      $inline_attr[$k] = $v;
  74                  break;                
  75              }
  76          }
  77  
  78          $inline_attr  = array_merge( $wattr , $default_attr , $inline_attr );
  79          $inline_style = array_merge( $wstyle, $default_style, $inline_style );
  80  
  81  // format style
  82          $wstyle = '';
  83          $quote = '"';
  84          foreach ( $inline_style as $k => $v )
  85          {
  86              if ( false !== strpos( $v, '"' ) ) $quote = "'";
  87              if ( $v != '' ) $wstyle .= $k . ':' . $v . ';';
  88          }
  89  
  90          $wimg = '<img';
  91  // format attributes
  92          foreach ( $inline_attr as $k => $v ) if ( $v != '' ) $wimg .= ' ' . $k . '="' . $v . '"';
  93          if ( $wstyle != '' ) $wimg .= ' style=' . $quote . $wstyle . $quote;
  94          $wimg .= ' />';
  95          $wimg = "<!-- MailPress_filter_img start -->\n" . $wimg .  "\n<!-- MailPress_filter_img end -->" ;
  96  
  97          return $wimg;
  98      }
  99  
 100  	public static function retrieve_attributes( $img )
 101      {
 102          if ( empty( $img ) ) return array();
 103  
 104          $w = str_ireplace( '<img ', '', $img );
 105          $w = str_ireplace( '/>', '', $w );
 106          $w = trim( $w );
 107          do {$w = str_ireplace( '  ', ' ', $w, $count );} while ( $count>0 );
 108          do {$w = str_ireplace( ' =', '=', $w, $count );} while ( $count>0 );
 109          do {$w = str_ireplace( '= ', '=', $w, $count );} while ( $count>0 );
 110  
 111          if ( '' == $w ) return array();
 112  
 113          do
 114          {
 115              $att         = strpos( $w, '=' );
 116              $key       = substr( $w, 0, $att );
 117              $quote     = substr( $w, $att+1, 1 );
 118              if ( "'" != $quote ) if ( '"' != $quote ) $quote=false;
 119              $start     = ( $quote ) ? 1 : 0;
 120              $end         = ( $quote ) ? strpos( $w, $quote, $att+1+$start ) : strpos( $w, ' ' ) ;
 121              $val         = substr( $w, $att+1+$start, $end-( $att+1+$start ) );
 122  
 123              $x[trim( $key )]=trim( $val );
 124  
 125              $w = trim( substr( $w, $end+1 ) );
 126          } while ( '' != $w );
 127  
 128          return $x;
 129      }
 130  
 131  	public static function retrieve_styles( $style )
 132      {
 133          if ( empty( $style ) ) return array();
 134  
 135          $w = explode( ';', $style );
 136          foreach ( $w as $v )
 137          {
 138              if ( $v )
 139              {
 140                  $zs = explode( ':', $v );
 141                  $x[trim( $zs[0] )] = trim( $zs[1] );
 142              }
 143          }
 144  
 145          return $x;
 146      }
 147  
 148  ////  ADMIN  ////
 149  ////  ADMIN  ////
 150  ////  ADMIN  ////
 151  ////  ADMIN  ////
 152  
 153  // for link on plugin page
 154  	public static function plugin_action_links( $links, $file )
 155      {
 156          return MailPress::plugin_links( $links, $file, plugin_basename( __FILE__ ), 'filter_img' );
 157      }
 158  
 159  // for settings
 160  	public static function styles( $styles, $screen ) 
 161      {
 162          if ( $screen != MailPress_page_settings ) return $styles;
 163  
 164          wp_register_style( 'mp-filter-img', '/' . MP_PATH . 'mp-admin/css/settings_filter_img.css' );
 165  
 166          $styles[] = 'mp-filter-img';
 167  
 168          return $styles;
 169      }
 170  
 171  	public static function settings_tab( $tabs )
 172      {
 173          $tabs['filter_img'] = __( 'Image filter', 'MailPress' );
 174          return $tabs;
 175      }
 176  }
 177  new MailPress_filter_img();
 178  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1