[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/class/ -> MP_mail_.class.php (source)

   1  <?php
   2  abstract class MP_mail_ extends MP_db_connect_
   3  {
   4      const default_theme = 'twentyten';
   5      public static $_classes = array();
   6      public static $_classes_1st = true;
   7  
   8      // feeding mail contents
   9  
  10  	function the_title($before = '', $after = '', $echo = true) 
  11      {
  12          $title = $this->get_the_title();
  13          if ( strlen($title) == 0 ) return;
  14          $title = $before . $title . $after;
  15          if ( $echo ) echo   $title;
  16          else         return $title;
  17      }
  18  
  19  	function get_the_title()
  20      {
  21          if (isset($this->args->newsletter) && $this->args->newsletter)
  22              return ($this->build->plaintext) ? $this->html_entity_decode(get_the_title()) : get_the_title();
  23  
  24          return isset($this->row->subject) ? $this->row->subject : '';
  25      }
  26  
  27  	function the_content($more_link_text = null, $stripteaser = 0)
  28      {
  29          $content = $this->get_the_content($more_link_text, $stripteaser);
  30          echo ($this->build->plaintext) ? $content : ( ($this->build->filter) ? apply_filters($this->build->filter, $content) : $content );
  31      }
  32  
  33  	function get_the_content($more_link_text = null, $stripteaser = 0)
  34      {
  35          $content = '';
  36  
  37          if (isset($this->args->newsletter) && $this->args->newsletter)
  38          {
  39              if (null !== $more_link_text) 
  40              {
  41                  global $more; 
  42                  $_more = $more; 
  43                  $more = false;
  44              }
  45              $content = get_the_content($more_link_text, $stripteaser);
  46              if (null !== $more_link_text)
  47              {
  48                  $more = $_more;
  49                  $content = preg_replace('/class=[\'"]more-link[\'"]/i', 'class="more-link" ' . $this->classes('more-link', false), $content);
  50              }
  51              return ($this->build->plaintext) ? $this->html2txt(apply_filters('the_content', $content)) : $content;
  52          }
  53  
  54          if (isset($this->args->content) && !empty($this->args->content))
  55              $content = ($this->build->plaintext) ? $this->html2txt(apply_filters('the_content', $this->args->content)) : $this->args->content;
  56  
  57          if ($this->build->plaintext && isset($this->args->plaintext) && !empty($this->args->plaintext))
  58              $content = $this->args->plaintext;
  59  
  60          if (!$this->build->plaintext && isset($this->args->html)     && !empty($this->args->html))
  61              $content = $this->args->html;
  62  
  63          return $content; 
  64      }
  65  
  66  	function the_image($args = array())
  67      {
  68          if ($this->build->plaintext) return;
  69          echo $this->get_the_image($args);
  70      }
  71  
  72  	function get_the_image($args = array())
  73      {
  74          if ($this->build->plaintext) return '';
  75  
  76          $defaults = array (    'pick'    => 1, // 0 is random, 1 is first ... x if not found search for x-1
  77                          'attrs'     => array(),
  78  
  79                          'unit'    => 'px',
  80                          'max_width'    => 100,
  81                          'force_max_width'    => 1,
  82                          'max_height'=> 100,
  83                          'force_max_height'=> 1,
  84                          'priority'    => 1, // 0 width unchanged, 1 height unchanged
  85                      );
  86          $r = wp_parse_args( $args, $defaults );
  87          extract( $r );
  88          if (!is_array($attrs)) $attrs = array();
  89  
  90      // analyse post content
  91          ob_start();
  92              the_content();
  93              $html = ob_get_contents();
  94          ob_end_clean();
  95  
  96          $output = preg_match_all('/<img[^>]*>/Ui', $html, $imgtags, PREG_SET_ORDER); // all img tag
  97          foreach ($imgtags as $imgtag)
  98          {
  99              $output = preg_match_all('/src=[\'"]([^\'"]+)[\'"]/Ui', $imgtag[0], $src, PREG_SET_ORDER); // for src attribute
 100              $matches[] = array(0 => $imgtag[0], 1 => $src[0][1]);
 101          }
 102          if (empty($matches)) return '<!-- MailPress_mail_api ** no img detected ** -->';
 103  
 104      // pick image
 105          if ($pick)     do { $pick--; if (isset($matches[$pick])) $img = $matches[$pick]; } while ( !isset($img) );
 106          else        $img = $matches[array_rand($matches)];
 107  
 108      // compute width & height
 109          $hw = @getimagesize($img[1]);
 110          if (!$hw) return '<!-- MailPress_mail_api ** getimagesize failed ** -->';
 111  
 112          $width = ($max_width  && (force_max_width  || $max_width  < $hw[0])) ? $max_width  : $hw[0];
 113          $height= ($max_height && (force_max_height || $max_height < $hw[1])) ? $max_height : $hw[1];
 114  
 115          if ( ($hw[0]/$hw[1]) != ($width/$height) )
 116          {
 117              if ($priority)    $width  = round ( $height * $hw [0] / $hw [1] );
 118              else            $height = round ( $width  * $hw [1] / $hw [0] );
 119          }
 120  
 121      // formatting args style
 122          $style = array();
 123          if (isset($attrs['style']))
 124          {
 125              $style = (is_array($attrs['style'])) ? $attrs['style'] : $this->retrieve_styles($attrs['style']);
 126              unset($attrs['style']);
 127          }
 128  
 129      // merging existing and args attributes
 130          $attrs = array_merge($this->retrieve_attributes($img[0]), $attrs);
 131          $attrs['style'] = (isset($attrs['style'])) ? array_merge($this->retrieve_styles($attrs['style']), $style) : $style;
 132  
 133      // width & height
 134          unset($attrs['width'], $attrs['height']);
 135          $attrs['style']['width']  = $width  . $unit;
 136          $attrs['style']['height'] = $height . $unit;
 137  
 138      // convert $attrs['style'] from array to string
 139          $wstyle = '';
 140          $quote = '"';
 141          foreach ($attrs['style'] as $k => $v)
 142          {
 143              if (false !== strpos($v, '"')) $quote = "'";
 144              if ($v != '') $wstyle .= "$k:$v;";
 145          }
 146          $attrs['style'] = $wstyle;
 147  
 148      // build new img tag
 149          $wimg = '';
 150          foreach ($attrs as $k => $v) $wimg .= ('style' == $k) ? " $k=$quote$v$quote" : " $k=\"$v\"";
 151  
 152          return "<!-- MailPress_mail_api start -->\n<img$wimg />\n<!-- MailPress_mail_api end -->";
 153      }
 154  
 155  	function retrieve_attributes($img)
 156      {
 157          if (empty($img)) return array();
 158  
 159          $w = str_ireplace('<img ', '', $img);
 160          $w = str_ireplace('/>', '', $w);
 161          $w = trim($w);
 162          do {$w = str_ireplace('  ', ' ', $w, $count);} while ($count);
 163          do {$w = str_ireplace(' =', '=', $w, $count);} while ($count);
 164          do {$w = str_ireplace('= ', '=', $w, $count);} while ($count);
 165  
 166          if ('' == $w) return array();
 167  
 168          do
 169          {
 170              $att         = strpos($w, '=');
 171              $key        = substr($w, 0, $att);
 172              $quote     = substr($w, $att+1, 1);
 173              if ("'" != $quote) if ('"' != $quote) $quote=false;
 174              $start     = ($quote) ? 1 : 0;
 175              $end         = ($quote) ? strpos($w, $quote, $att+1+$start) : strpos($w, ' ') ;
 176              $val         = substr($w, $att+1+$start, $end-($att+1+$start));
 177  
 178              $x[trim($key)]=trim($val);
 179  
 180              $w = trim(substr($w, $end+1));
 181          } while ('' != $w);
 182  
 183          return $x;
 184      }
 185  
 186  	function retrieve_styles($style)
 187      {
 188          if (empty($style)) return array();
 189  
 190          $w = explode(';', $style);
 191          foreach ($w as $v)
 192          {
 193              if ($v)
 194              {
 195                  $zs = explode(':', $v);
 196                  $x[trim($zs[0])] = trim($zs[1]);
 197              }
 198          }
 199  
 200          return $x;
 201      }
 202  
 203      // styling mails utilities
 204  
 205  	function classes($classes, $echo = true, $attr = false)
 206      {
 207          if (self::$_classes_1st)
 208          {
 209              self::$_classes_1st = false;
 210  
 211              $files = $this->get_template_paths( 'style.php', true ) ;
 212  
 213              foreach ($files as $file)
 214              {
 215                  if (!is_file($file)) continue;
 216                  require_once($file);
 217                  if (isset($_classes) && !empty($_classes)) self::$_classes[] = $_classes;
 218                  unset($_classes);
 219              }
 220          }
 221          if (empty(self::$_classes)) return '';
 222  
 223          $count = 1;
 224          while ($count) $classes = str_replace('  ', ' ', $classes, $count);
 225          $a_classes = explode(' ', trim($classes));
 226  
 227          $style = '';
 228  
 229          foreach($a_classes as $class) foreach(self::$_classes as $_classes) if (isset($_classes[$class])) $style .= $this->clean_style($_classes[$class]);
 230  
 231          if ('' != $style) 
 232              if ($echo)         echo   "style=\"" . $style . "\""; 
 233              elseif (!$attr)    return "style=\"" . $style . "\"";
 234              else            return $style;
 235      }
 236  
 237  	function clean_style($style)
 238      {
 239          $style = trim($style);
 240          $style = str_replace("\t",'',$style);
 241          $style = str_replace("\n",'',$style);
 242          $style = str_replace("\r",'',$style);
 243          if (strlen($style)) if ($style[strlen($style) -1] != ';') $style .=';';
 244          return $style;
 245      }
 246  
 247      // convert html to txt
 248  
 249  	function html_entity_decode($html)
 250      {
 251          if (!preg_match('/&[^&;]+;/i', $html)) return $html;
 252          $h = new MP_Html2txt( 'entities' );
 253          return $h->get_text( $html, 0 );
 254      }
 255  
 256  	function html2txt($html)
 257      {
 258          if (empty($html)) return $html;
 259          $h = new MP_Html2txt();
 260          return $h->get_text( $html, 0 );
 261      }
 262  
 263      // special mail attributes (subject)
 264  
 265  	function the_subject($default = '', $echo = true)
 266      {
 267          $subject = $this->get_the_subject($default);
 268          if ( strlen($subject) == 0 ) return;
 269          if ( $echo ) echo $subject;
 270          else         return $subject;
 271      }
 272  
 273  	function get_the_subject($default)
 274      {
 275          if (isset($this->row->subject)) return $this->row->subject;
 276          return $default;
 277      }
 278  
 279      // special files (header, footer, stylesheet, sidebars)
 280  
 281  	function get_header() 
 282      {
 283          $this->get_template_part('header');
 284      }
 285  
 286  	function get_footer() 
 287      {
 288          $this->get_template_part('footer');
 289      }
 290  
 291  	function get_sidebar( $name = null )
 292      {
 293          $this->get_template_part('sidebar', $name);
 294      }
 295  
 296  	function get_stylesheet() 
 297      {
 298          foreach ($this->get_template_paths('style.css', true) as $file)
 299          {
 300              if ( !is_file( $file ) ) continue;
 301              echo "<style type='text/css' media='all'>\n";
 302              include( $file );
 303              echo "</style>\n";
 304          }
 305      }
 306  
 307      // file loaders
 308  
 309  	function get_template_paths( $file, $desc = false ) 
 310      {
 311          $x = array($this->build->stylesheetpath . $file, $this->build->templatepath . $file);
 312          if ($desc) krsort($x);
 313          if (isset($this->build->plaintextpath)) $x[] = $this->build->plaintextpath . $file;
 314          return array_unique($x);
 315      }
 316  
 317  	function get_template_part( $slug, $name = null ) 
 318      {
 319          $template = false;
 320          if ( isset($name) ) $template = "{$slug}-{$name}.php";
 321          else $template = "{$slug}.php";
 322          return $this->locate_template($template, true, false);
 323      }
 324  
 325  	function locate_template($template_name = 'default.php', $load = false, $require_once = true ) 
 326      {
 327          $located = '';
 328  
 329          if ( !$template_name ) continue;
 330  
 331          foreach ($this->get_template_paths($template_name) as $file)
 332          {
 333              if ( is_file($file) )
 334              {
 335                  $located = $file;
 336                  break;
 337              }
 338          }
 339  
 340          if ('' == $located)
 341          {
 342              if (isset($this->trace))
 343              {
 344                  $this->trace->log(sprintf('MAILPRESS [NOTICE] - Missing template : >> %1$s << Folder : %2$s ', $template_name, $this->build->stylesheetpath), $this->trace->levels[512]);
 345                  if ($this->build->stylesheetpath != $this->build->templatepath)
 346                      $this->trace->log(sprintf('MAILPRESS [NOTICE] - Missing template : >> %1$s << Folder : %2$s ', $template_name, $this->build->templatepath), $this->trace->levels[512]);
 347                  if (isset($this->build->plaintextpath) && ($this->build->stylesheetpath != $this->build->plaintextpath) && ($this->build->plaintextpath != $this->build->templatepath))
 348                      $this->trace->log(sprintf('MAILPRESS [NOTICE] - Missing template : >> %1$s << Folder : %2$s ', $template_name, $this->build->plaintextpath), $this->trace->levels[512]);
 349              }
 350          }
 351          elseif ( $load )
 352              $this->load_template( $located, $require_once );
 353  
 354          return $located;
 355      }
 356  
 357  	function load_template( $_template_file, $require_once = false ) 
 358      {
 359          global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
 360  
 361          if ( is_array( $wp_query->query_vars ) ) extract( $wp_query->query_vars, EXTR_SKIP );
 362  
 363          if ( $require_once )     require_once( $_template_file );
 364          else                 require( $_template_file );
 365      }
 366  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1