[ Index ]

MailPress 7.1

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


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