[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  abstract class MP_import_importer_
   3  {
   4      const bt             = 133;
   5      var     $trace_header     = false;
   6  
   7  	function __construct($desc, $header, $callback = false) 
   8      {
   9          $this->desc     = $desc;
  10          $this->header     = $header;
  11          if (!$callback) $this->callback = array($this, 'dispatch');
  12  
  13          add_filter('MailPress_import_importers_register', array($this, 'register'), 8, 1);
  14      }
  15  
  16  	function register($importers) 
  17      {
  18          if ( is_wp_error( $this->callback ) ) return $importers;
  19          $importers[$this->id] = array ( $this->id, $this->desc, $this->callback);
  20          return $importers;
  21      }
  22  
  23  	function header() 
  24      {
  25          echo "<div class='wrap'><div id='icon-mailpress-tools' class='icon32'><br /></div><h2>" . $this->header . '</h2>';
  26      }
  27  
  28  	function footer() 
  29      {
  30          echo '</div>';
  31      }
  32  
  33  // step 0
  34  
  35  	function greet() 
  36      {
  37  ?>
  38  <div>
  39      <p>
  40  <?php        _e('Howdy! Upload your file and we&#8217;ll import the emails and much more ... into this blog.', MP_TXTDOM); ?>
  41          <br />
  42  <?php        _e('Choose a file to upload, then click Upload file and import.', MP_TXTDOM); ?>
  43      </p>
  44  <?php wp_import_upload_form( MailPress_import . '&amp;mp_import=' . $this->id . '&amp;step=1'); ?>
  45  </div>
  46  <?php
  47      }
  48  
  49  // step 1
  50  
  51  	function handle_upload() 
  52      {
  53          $file = wp_import_handle_upload();
  54          if ( isset($file['error']) )
  55          {
  56              $this->trace->end(true);
  57              $this->error('<p><strong>' . $file['error'] . '</strong></p>');
  58              return false;
  59          }
  60          
  61          $this->file    = $file['file'];
  62          $this->file_id = (int) $file['id'];
  63          return true;
  64      }
  65  
  66  // for files
  67  
  68  	function fopen($filename, $mode='r') 
  69      {
  70          if ( $this->has_gzip() ) return gzopen($filename, $mode);
  71          return fopen($filename, $mode);
  72      }
  73  
  74  	function feof($fp) 
  75      {
  76          if ( $this->has_gzip() ) return gzeof($fp);
  77          return feof($fp);
  78      }
  79  
  80  	function fgets($fp, $len=8192) 
  81      {
  82          if ( $this->has_gzip() ) return gzgets($fp, $len);
  83          return fgets($fp, $len);
  84      }
  85  
  86  	function fclose($fp) 
  87      {
  88          if ( $this->has_gzip() ) return gzclose($fp);
  89          return fclose($fp);
  90      }
  91  
  92  	function has_gzip() 
  93      {
  94          return is_callable('gzopen');
  95      }
  96  
  97  // for db tables
  98  
  99  	public static function tableExists($table) 
 100      {
 101          global $wpdb;
 102          return strcasecmp($wpdb->get_var("show tables like '$table'"), $table) == 0;
 103      }
 104  
 105  // for logs
 106  
 107  	function start_trace($step)
 108      {
 109          $this->trace = new MP_Log('mp_import_' . $this->id, array('option_name' => 'import'));
 110          $this->header_report($step);
 111      }
 112  
 113  	function end_trace($rc)
 114      {
 115             $this->footer_report();
 116          $this->trace->end($rc);
 117      }
 118  
 119  	function header_report($step)
 120      {
 121          if ((isset($this->trace_header)) && $this->trace_header) return;
 122          $this->trace_header = true;
 123          $this->message_report(str_repeat( '-', self::bt));
 124          $this->message_report(str_repeat( ' ', 5) . "Batch Report   importer : " . $this->id . "     step : $step" );
 125          $this->message_report(str_repeat( '-', self::bt));
 126      }
 127  
 128  	function message_report($bm)
 129      {
 130          if (!$this->trace_header) $this->header_report('?');
 131  
 132          $bl = strlen($bm);
 133          $bl = self::bt - $bl;
 134          $bl = ($bl < 0) ? 0 : $bl;
 135          $bm = '!' . $bm . str_repeat( ' ', $bl ) . '!';
 136          if ($this->trace) 
 137              $this->trace->log($bm);
 138          else
 139              echo '<pre>' . $bm . "</pre>\n";
 140      }
 141  
 142  	function footer_report()
 143      {
 144          if ((isset($this->trace_footer)) && $this->trace_footer) return;
 145          $this->trace_footer = true;
 146          $this->message_report(str_repeat( '-', self::bt));
 147      }
 148  
 149  	function link_trace()
 150      {
 151          if ( isset($this->trace->file) && is_file($this->trace->file) )
 152          { 
 153              $file = $this->trace->file;
 154              $y = str_replace('\\', '/', substr( $this->trace->file, strpos( $this->trace->file, str_replace( ABSPATH, '', WP_CONTENT_DIR ) ) ) );
 155              return "<p><a href='../$y' target='_blank'>" . __('See the log', MP_TXTDOM) . '</a></p>';
 156          }
 157          return '';
 158      }
 159  
 160  // for success & errors
 161  
 162  	function success($text = '', $echo = true)
 163      {
 164          $x  = '<div><h3>' . __('Process successful', MP_TXTDOM) . '</h3>';
 165          $x .= $text;
 166          $x .= $this->link_trace();
 167          $x .= '</div>';
 168  
 169          if ($echo) echo $x;
 170          return $x;
 171      }
 172  
 173  	function error($text = '', $echo = true)
 174      {
 175          $x  = '<div><h3>'.__('Sorry, there has been an error.', MP_TXTDOM).'</h3>';
 176          $x .= $text;
 177          $x .= $this->link_trace();
 178          $x .= '</div>';
 179  
 180          if ($echo) echo $x;
 181          return $x;
 182      }
 183  
 184  ////  IMPORT API  ////
 185  
 186  	function sync_mp_user($email, $name, $status = 'active')
 187      {
 188          $xl = strlen($email);
 189          $xl = ((25 - $xl) < 0) ? 0 : 25 - $xl;
 190          $x = $email . str_repeat( ' ', $xl);
 191  
 192          if ( !is_email($email))
 193          {
 194              $this->message_report("** ERROR ** ! $x not an email ($name)");
 195               return false;
 196          }
 197          
 198          if ( 'deleted' != MP_User::get_status_by_email($email) )
 199          {
 200              $this->message_report(" **WARNING* ! $x already exists ($name) (processed if extra work to do)");
 201          }
 202          else
 203          {
 204              MP_User::insert($email, $name, array('status' => $status, 'stopPropagation' => true));
 205  
 206              $this->message_report(" insert     ! $x inserted ($name)");
 207          }
 208          return MP_User::get_id_by_email($email);
 209      }
 210  
 211  	function sync_mp_usermeta($mp_user_id, $meta_key, $meta_value)
 212      {
 213          if (!MP_User_meta::add(    $mp_user_id, $meta_key, $meta_value, true))
 214              MP_User_meta::update($mp_user_id, $meta_key, $meta_value);
 215  
 216          $this->message_report(" meta       ! user [$mp_user_id]=> update of meta data key=>\"$meta_key\" data=>\"$meta_value\"");
 217      }
 218  
 219  	function sync_mailinglist($mailinglist) 
 220      {
 221          if (!class_exists('MailPress_mailinglist')) return false;
 222  
 223          if ($id = MP_Mailinglist::get_id('MailPress_import_' . $mailinglist))
 224          {
 225              $this->message_report(" mailinglist! mailing list found : [$id] => $mailinglist");
 226              return $id;
 227          }
 228  
 229          if ($id = MP_Mailinglist::insert(array('name'=>'MailPress_import_' . $mailinglist)))
 230          {
 231              $this->message_report(" mailinglist! mailing list inserted : [$id] => $mailinglist");
 232              return $id;
 233          }
 234  
 235          $this->message_report("** ERROR ** ! Unable to read or create a mailing list : $mailinglist");
 236          return false;
 237      }
 238  
 239  	function sync_mp_user_mailinglist($mp_user_id, $mailinglist_ID, $email='', $mailinglist='', $trace=false) 
 240      {
 241          if (!class_exists('MailPress_mailinglist')) return false;
 242  
 243          $user_mailinglists = MP_Mailinglist::get_object_terms($mp_user_id);
 244          if (in_array($mailinglist_ID, $user_mailinglists))
 245          {
 246              $xl = strlen($email);
 247              $xl = ((25 - $xl) < 0) ? 0 : 25 - $xl;
 248              $x = $email . str_repeat( ' ', $xl);
 249              $this->message_report(" mailinglist! $x [$mp_user_id] already in mailing list [$mailinglist_ID] => $mailinglist");
 250          }
 251          else
 252          {
 253              array_push($user_mailinglists, $mailinglist_ID);
 254              MP_Mailinglist::set_object_terms( $mp_user_id, $user_mailinglists );
 255  
 256              $xl = strlen($email);
 257              $xl = ((25 - $xl) < 0) ? 0 : 25 - $xl;
 258              $x = $email . str_repeat( ' ', $xl);
 259              $this->message_report(" mailinglist! $x [$mp_user_id] inserted in mailing list [$mailinglist_ID] => $mailinglist");
 260          }
 261      }
 262  
 263  	function sync_mp_user_newsletter($mp_user_id) 
 264      {
 265          if (!class_exists('MailPress_newsletter')) return false;
 266  
 267          MP_Newsletter::set_object_terms( $mp_user_id, array_merge(MP_Newsletter::get_object_terms($mp_user_id), MP_Newsletter::get_defaults()) );        
 268      }
 269  
 270  	function sync_mp_user_no_newsletter($mp_user_id) 
 271      {
 272          if (!class_exists('MailPress_newsletter')) return false;
 273          MP_Newsletter::set_object_terms( $mp_user_id );        
 274      }
 275  
 276  ////  ATTACHMENTS ////
 277  
 278  	function insert_attachment($file)
 279      {
 280          $uploads = wp_upload_dir();
 281  
 282          if ( $uploads['error'] )
 283          {
 284              $this->message_report(' **WARNING* ! ' . $uploads['error']);
 285              return false;
 286          }
 287  
 288          $filename = wp_unique_filename( $uploads['path'], $file['name'] );
 289  
 290          $new_file = $uploads['path'] . "/$filename";
 291  
 292          if ( copy($file['tmp_name'], $new_file) ) 
 293          {
 294              @unlink($file['tmp_name']);
 295              
 296          // Set correct file permissions
 297              $stat = stat( dirname( $new_file ));
 298              $perms = $stat['mode'] & 0000666;
 299              @ chmod( $new_file, $perms );
 300  
 301          // Compute the URL
 302              $url = $uploads['url'] . "/$filename";
 303  
 304              $object = array(     'post_title' => $filename,
 305                          'post_content' => $url,
 306                          'post_mime_type' => $file['type'],
 307                          'guid' => $url,
 308                          'context' => 'export',
 309                          'post_status' => 'private'
 310              );
 311              wp_insert_attachment($object);
 312              return $url;
 313          }
 314          return false;
 315      }
 316  }


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