[ Index ]

MailPress 7.1

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

title

Body

[close]

/mp-includes/class/options/import/importers/ -> subscribe2.php (source)

   1  <?php
   2  class MP_import_subscribe2 extends MP_import_importer_
   3  {
   4      var $id = 'subscribe2';
   5  
   6  	function dispatch() 
   7      {
   8          global $wpdb;
   9          $this->maintable    = $wpdb->prefix . 'subscribe2';
  10  
  11          $step = $this->get_step();
  12  
  13          $this->header();
  14          switch ( $step ) 
  15          {
  16              case 0 :
  17                  $this->greet();
  18              break;
  19              case 1 :
  20                  $this->start_trace( $step );
  21                  $validate_data = $this->validate_data();
  22                  if ( $validate_data )
  23                  {
  24                      $sniff = $this->sniff();
  25                      $this->end_trace( $sniff );
  26                      if ( $sniff )
  27                          echo $this->step1;
  28                      else
  29                          $this->error( $this->step1 );
  30                  }
  31                  else
  32                  {
  33                      $this->end_trace( $validate_data );
  34                      $this->error( $this->step1 );
  35                  }
  36                  
  37              break;
  38              case 2:
  39                  $this->start_trace( $step );
  40                  $import = $this->import();
  41                  $this->end_trace( $import );
  42                  if ( $import )
  43                      $this->success( '<p>' . __( "<b>Data imported</b>", 'MailPress' ) . '</p>' );
  44                  else 
  45                      $this->error();
  46              break;
  47          }
  48          $this->footer();
  49      }
  50  
  51  // step 0
  52  
  53  	function greet() 
  54      {
  55          $args = array( 'mp_import' => $this->id, 'step' => 1 );
  56  
  57          $x = '<div style="text-align:center;">' . "\n";
  58          $x .= "<br />\n";
  59          $x .= __( 'First Things First', 'MailPress' );
  60          $x .= "<br />\n";        $x .= "<br />\n";
  61          $x .= __( 'Before importing your Subscribe2 datas : ', 'MailPress' );
  62          $x .= "<br />\n";        $x .= "<br />\n";
  63          $x .= '<span style="color:red;font-weight:bold;">';
  64          $x .= __( 'SAVE YOUR DATABASE', 'MailPress' );
  65          $x .= "</span>\n";
  66          $x .= "<br />\n";        $x .= "<br />\n";
  67          $x .= __( 'and make sure you can restore it !', 'MailPress' );
  68          $x .= "<br />\n";        $x .= "<br />\n";
  69          $x .= "</div>\n";
  70  ?>
  71  <?php MP_AdminPage::message( $x, false ); ?>
  72  <br />
  73  <div>
  74      <?php _e( 'Howdy! Ready to import your <b>Subscribe2</b> data into <b>MailPress</b> ...', 'MailPress' ); ?>
  75      <?php $nl = ( class_exists( 'MailPress_newsletter' ) )  ? __( "newsletters", 'MailPress' ) : '' ; ?>
  76      <?php $ml = ( class_exists( 'MailPress_mailinglist' ) ) ? __( "mailing lists", 'MailPress' ) : '' ; ?>
  77      <p><?php _e( '<b>Subscribe2</b> data is stored into the following tables :', 'MailPress' ); ?></p>
  78      <ol>
  79          <li>usermeta
  80              <p>
  81                  <?php _e( "In this table, 'WP users' subscribers can subscribe to categories.", 'MailPress' ); ?>
  82                  <br />
  83                  <?php printf( __( 'You will be able to convert this %1$s to subscriptions in %2$s.', 'MailPress' ), __( "'categories subscription'", 'MailPress' ), ( empty( $nl ) || empty( $ml ) ) ? "$nl$ml" : "$nl, $ml" ); ?>
  84              </p>
  85          </li>
  86          <li>subscribe2
  87              <p>
  88                  <?php _e( 'In this table, subscribers have default subscription set by admin.', 'MailPress' ); ?>
  89                  <br />
  90                  <?php printf( __( 'You will be able to convert this %1$s to subscriptions in %2$s.', 'MailPress' ), __( "'default subscription'", 'MailPress' ), ( empty( $nl ) || empty( $ml ) ) ? "$nl$ml" : "$nl, $ml" ); ?>
  91              </p>
  92          </li>
  93      </ol>
  94      <br />
  95      <p><?php _e( 'Note 1 : The tables are processed in the above order.', 'MailPress' ); ?></p>
  96      <p><?php _e( 'Note 2 : If a subscriber already exists in MailPress, the settings for this email remains <b>unchanged</b>.', 'MailPress' ); ?></p>
  97  
  98      <form id="export" method="post" action="<?php echo esc_url( add_query_arg( $args, MailPress_import ) ); ?>">
  99          <p class="submit">
 100              <input type="submit" class="button-primary" name="Submit" value="<?php _e( 'Continue', 'MailPress' ); ?>" />
 101          </p>
 102      </form>
 103  </div>
 104  <?php
 105      }
 106  
 107  // step 1
 108  
 109  	function validate_data()
 110      {
 111          $this->message_report( " ANALYSIS   !" );
 112          if ( $this->tableExists( $this->maintable ) )
 113          {
 114              $this->message_report( '            ! MySQL Table detected  >>> ' . $this->maintable );
 115              return true;
 116          }
 117          $this->message_report( '** ERROR ** ! MySQL Table not detected  >>> ' . $this->maintable );
 118  
 119           $this->step1  = '<style type="text/css"> .general th {font-weight:bold;width:auto;} .general td, .general th {border:solid 1px #555;margin:0;padding:5px;vertical-align:top;} </style>';
 120           $this->step1 .= "<div>\n";
 121           $this->step1 .= "<h3>" . __( 'Data Analysis', 'MailPress' ) . "</h3>\n";
 122           $this->step1 .= '<table class="form-table">' . "\n";
 123           $this->step1 .= "<tr>\n";
 124           $this->step1 .= "<th>" . $this->maintable . "</th>\n";
 125           $this->step1 .= "<td>\n";
 126           $this->step1 .= "<p>" . __( '*** ERROR *** Table not detected', 'MailPress' ) . "</p>\n";
 127           $this->step1 .= "</td>\n";
 128           $this->step1 .= "</tr>\n";
 129           $this->step1 .= "</table>\n";
 130           $this->step1 .= '</div>';
 131  
 132          return false;
 133      }
 134  
 135  	function sniff() 
 136      {
 137          global $wpdb;
 138          $import = false;
 139          $args = array( 'mp_import' => $this->id, 'step' => 2 );
 140  
 141          $this->step1 = '';
 142  
 143          $countcat    = $wpdb->get_var( "SELECT count( * ) FROM $wpdb->usermeta WHERE meta_key = 's2_subscribed' " );
 144          $subs        = $wpdb->get_var( "SELECT count( * ) FROM $this->maintable " );
 145  
 146  // $countcat 
 147          if ( $countcat || $subs )
 148          {
 149               $head1  = '<style type="text/css"> .general th {font-weight:bold;width:auto;} .general td, .general th {border:solid 1px #555;margin:0;padding:5px;vertical-align:top;} </style>';
 150               $head1 .= "<h3>" . __( 'Data Analysis', 'MailPress' ) . "</h3>\n";
 151               $head1 .= '<form action="' . esc_url( add_query_arg( $args, MailPress_import ) ) . '" method="post"><table class="form-table">' . "\n";
 152  
 153               $foot1 = "</table>\n";
 154              $foot1 .= '<p class="submit">' ; "\n";
 155              $foot1 .= '<input type="submit" class="button-primary" value="' . esc_attr( __( 'Submit' ) ) . '" />' . "\n";
 156              $foot1 .= "</p>\n";
 157              $foot1 .= "</form>\n";
 158  
 159              $import = true; 
 160          }
 161          else
 162          {     
 163               $head1  = '<style type="text/css"> .general th {font-weight:bold;width:auto;} .general td, .general th {border:solid 1px #555;margin:0;padding:5px;vertical-align:top;} </style>';
 164               $head1 .= "<h3>" . __( 'Data Analysis', 'MailPress' ) . "</h3>\n";
 165              $head1 = '<table class="form-table">' . "\n";
 166  
 167               $foot1 = "</table>\n";
 168          }
 169  
 170          if ( $countcat )
 171          {
 172              $this->message_report( "            ! " . sprintf( '%1$s WP user(s) subscriber(s) found', $countcat ) );
 173              $this->sniff_usermeta( $countcat );
 174          }
 175          else
 176          {
 177              $this->message_report( "** ERROR ** ! " . sprintf( 'Usermeta table: %1$s, no data', $wpdb->usermeta ) );
 178  
 179              $this->step1 .= "<tr>\n";
 180              $this->step1 .= "<th>" . $wpdb->usermeta . "</th>\n";
 181              $this->step1 .= "<td>\n";
 182              $this->step1 .= "<p>" . __( 'no data', 'MailPress' ) . "</p>\n";
 183              $this->step1 .= "</td>\n";
 184              $this->step1 .= "</tr>\n";
 185          }
 186  
 187  // $subs
 188          $subs = $wpdb->get_results( "SELECT active, count( * ) as count FROM $this->maintable GROUP BY active ORDER BY active;" );
 189  
 190          if ( $subs ) 
 191          {
 192              $this->message_report( "            !" . sprintf( '%1$s Subscribe2 subscriber(s) found', count( $subs ) ) );
 193              $this->sniff_subscribe2( $subs );
 194          }
 195          elseif( $wpdb->last_error )
 196          {
 197              $this->message_report( "** ERROR ** ! " . sprintf( 'Database error : %1$s', $wpdb->last_error ) );
 198  
 199              $this->step1 .= "<tr>\n";
 200              $this->step1 .= "<th>" . $this->maintable . "</th>\n";
 201              $this->step1 .= "<td>\n";
 202              $this->step1 .= $this->error( '<p>' . sprintf( __( "*** ERROR *** Database error : %1$s", 'MailPress' ), $wpdb->last_error ) . "</p>\n", false );
 203              $this->step1 .= "</td>\n";
 204              $this->step1 .= "</tr>\n";
 205          }
 206          else
 207          {
 208              $this->message_report( "** ERROR ** ! " . sprintf( 'Main table : %1$s empty', $this->maintable ) );
 209  
 210              $this->step1 .= "<tr>\n";
 211              $this->step1 .= "<th>" . $this->maintable . "</th>\n";
 212              $this->step1 .= "<td>\n";
 213              $this->step1 .= "<p>" . __( 'no data', 'MailPress' ) . "</p>\n";
 214              $this->step1 .= "</td>\n";
 215              $this->step1 .= "</tr>\n";
 216          }
 217  
 218          $this->step1 = $head1 . $this->step1 . $foot1;
 219          return $import;
 220      }
 221  
 222  	function sniff_usermeta( $countcat )
 223      {
 224          global $wpdb;
 225  
 226          $recap = array();
 227          $metas = $wpdb->get_results( "SELECT DISTINCT meta_value FROM $wpdb->usermeta WHERE meta_key = 's2_subscribed'" );
 228  
 229          if ( $metas )
 230          {
 231              foreach ( $metas as $meta )
 232              {
 233                  if ( '-1' == $meta->meta_value ) continue;
 234                  $x = explode( ', ', $meta->meta_value );
 235                  if ( empty( $x ) ) continue;
 236                  $x = array_diff( $x, $recap );
 237                  foreach ( $x as $y ) array_push( $recap, $y );
 238              }
 239          }
 240          sort( $recap );
 241  
 242          $this->step1 .= "<tr>\n";
 243          $this->step1 .= "<th>" . $wpdb->usermeta . "</th>\n";
 244          $this->step1 .= "<td>\n";
 245  
 246          if ( class_exists( 'MailPress_sync_wordpress_user' ) ) 
 247          {
 248              $this->step1 .= "<p>" . sprintf( __( 'You are using %1$s, usermeta will not be processed', 'MailPress' ), "'MailPress_sync_wordpress_user'" ) . "</p>\n";
 249          }
 250          else
 251          {
 252              $this->step1 .= "<ul>\n";
 253              $this->step1 .= "<li>\n";
 254              $this->step1 .= sprintf( __( '%1$s WP user(s) subscriber(s) found', 'MailPress' ), $countcat ) . "\n";
 255              $this->step1 .= "</li>\n";
 256              $this->step1 .= "</ul>\n";
 257  
 258              if ( !empty( $recap ) )
 259              {
 260                  $this->message_report( "            ! " . '>>>> ' . count( $recap ) . ' Categories found' );
 261  
 262                  $this->step1 .= '<table class="general">' . "\n";
 263                  $this->step1 .= "\n";
 264                  $this->step1 .= "<thead>\n";    
 265                  $this->step1 .= "<tr>\n";    
 266                  $this->step1 .= "<th>\n";
 267                  $this->step1 .= __( 'Categories used in Subscribe2', 'MailPress' ) . "\n";
 268                  $this->step1 .= "</th>\n";
 269                  if ( class_exists( 'MailPress_newsletter' ) ) 
 270                  {
 271                      $this->step1 .= "<th>\n";
 272                      $this->step1 .= __( "MailPress Newsletters", 'MailPress' ) . "\n";
 273                      $this->step1 .= "</th>\n";
 274                  }
 275                  if ( class_exists( 'MailPress_mailinglist' ) ) 
 276                  {
 277                      $this->step1 .= "<th>\n";
 278                      $this->step1 .= __( 'MailPress Mailing lists', 'MailPress' ) . "\n";
 279                      $this->step1 .= "</th>\n";
 280                  }
 281                  $this->step1 .= "</tr>\n";
 282                  $this->step1 .= "</thead>\n";
 283                  $this->step1 .= "<tbody>\n";
 284  
 285                  foreach ( $recap as $cat_id )
 286                  {
 287                      $this->step1 .= "<tr>\n";    
 288                      $this->step1 .= "<td>\n";
 289                      $this->step1 .= get_cat_name( $cat_id ) . "\n";
 290                      $this->step1 .= "</td>\n";
 291                      if ( class_exists( 'MailPress_newsletter' ) ) 
 292                      {
 293                          $this->step1 .= '<td style="text-align:center;">' . "\n";
 294                          $dropdown_options = array( 'show_option_all' => __( 'None', 'MailPress' ), 'echo' => 0, 'type' => 'select', 'htmlname' => 'usermeta_nl_' . $cat_id, 'admin' => true, 'selected' => $this->get_newsletter_id( $cat_id ) );
 295                          $this->step1 .= MailPress_newsletter::get_checklist( false, $dropdown_options ) . "\n";
 296                          $this->step1 .= "</td>\n";
 297                      }
 298                      if ( class_exists( 'MailPress_mailinglist' ) ) 
 299                      {
 300                          $this->step1 .= "<td>\n";
 301                          $dropdown_options = array( 'show_option_all' => __( 'None', 'MailPress' ), 'echo' => 0, 'type' => 'select', 'htmlname' => 'usermeta_ml_' . $cat_id );
 302                          $this->step1 .= MailPress_mailinglist::get_checklist( false, $dropdown_options ) . "\n";
 303                          $this->step1 .= "</td>\n";
 304                      }
 305  
 306                      $this->step1 .= "</tr>\n";    
 307                  }
 308                  $this->step1 .= "</tbody>\n";
 309                  $this->step1 .= "</table>\n";
 310              }
 311          }
 312          $this->step1 .= "</td>\n";
 313          $this->step1 .= "</tr>\n";
 314      }
 315  
 316  	function get_newsletter_id( $cat_id, $admin = true )
 317      {
 318          do { $x = get_category( $cat_id ); if ( !$x->category_parent ) break; $cat_id = $x->category_parent; } while( $x->category_parent );
 319  
 320          global $mp_registered_newsletters;
 321          $subscriptions = get_option( MailPress::option_name_subscriptions );
 322  
 323          $lib_nl = ( $admin ) ? 'desc' : 'display';
 324  
 325          foreach ( $mp_registered_newsletters as $newsletter )
 326          {
 327              if ( !isset( $subscriptions['newsletters'][$newsletter['id']] ) ) continue;
 328              if ( $newsletter[$lib_nl] ) 
 329                  if ( isset( $newsletter['params']['cat_id'] ) ) 
 330                      if ( $cat_id == $newsletter['params']['cat_id'] ) return $newsletter['id'];
 331   
 332          }
 333  
 334          return false;
 335      }
 336  
 337  	function sniff_subscribe2( $subs )
 338      {
 339          global $wpdb;
 340  
 341          $checklist_newsletters = $checklist_mailinglists = false;
 342  
 343          if ( class_exists( 'MailPress_newsletter' ) )  $checklist_newsletters  = MailPress_newsletter::get_checklist( false, array( 'admin' => true ) );
 344          if ( class_exists( 'MailPress_mailinglist' ) ) $checklist_mailinglists = MailPress_mailinglist::get_checklist();
 345  
 346          $this->step1 .= "<tr>\n";
 347          $this->step1 .= "<th>" . $this->maintable . "</th>\n";
 348          $this->step1 .= "<td>\n";
 349          $this->step1 .= "<ul>\n";
 350  
 351          foreach ( $subs as $sub )
 352          {
 353              $status = __( 'waiting', 'MailPress' );
 354              if ( 1 == $sub->active ) $status = __( 'active', 'MailPress' );
 355  
 356              $this->step1 .= "<li>\n";
 357              $this->step1 .= sprintf( __( ' %1$s subscriber(s) will be imported with status : "%2$s" .', 'MailPress' ), $sub->count, $status ) . "\n";
 358              $this->step1 .= "</li>\n";
 359          }
 360  
 361          $this->step1 .= "</ul>\n";
 362  
 363          if ( $checklist_newsletters || $checklist_mailinglists ) 
 364          {
 365              $this->step1 .= __( "Select subscriptions for ACTIVE subscribers :", 'MailPress' ) . "\n"; 
 366  
 367              $this->step1 .= '<table class="general">' . "\n";
 368              $this->step1 .= "\n";
 369              $this->step1 .= "<thead>\n";
 370              $this->step1 .= "<tr>\n";    
 371      
 372               if ( $checklist_newsletters ) 
 373              {
 374                  $this->step1 .= "<th>\n";    
 375                   $this->step1 .= __( "MailPress Newsletters", 'MailPress' ) . "\n";    
 376                  $this->step1 .= "</th>\n";    
 377              }
 378              if ( $checklist_mailinglists )
 379              {
 380                  $this->step1 .= "<th>\n";    
 381                   $this->step1 .= __( "MailPress Mailing lists", 'MailPress' ) . "\n";    
 382                  $this->step1 .= "</th>\n";
 383              }
 384              $this->step1 .= "</tr>\n";
 385              $this->step1 .= "</thead>\n";
 386              $this->step1 .= "<tbody>\n";
 387              $this->step1 .= "<tr>\n";
 388              $this->step1 .= "<td>\n";
 389              $this->step1 .= $checklist_newsletters . "\n";
 390              $this->step1 .= "</td>\n";
 391              if ( $checklist_mailinglists )
 392              {
 393                  $this->step1 .= "<td>\n";
 394                  $this->step1 .= $checklist_mailinglists . "\n";
 395                  $this->step1 .= "</td>\n";
 396              }
 397              $this->step1 .= "</tr>\n";
 398              $this->step1 .= "</tbody>\n";
 399              $this->step1 .= "</table>\n";
 400          }
 401          $this->step1 .= "</td>\n";
 402          $this->step1 .= "</tr>\n";
 403          return true;
 404      }
 405  
 406  // step 2
 407  
 408  	function import() 
 409      {
 410          global $wpdb;
 411          if ( !class_exists( 'MailPress_sync_wordpress_user' ) ) $this->import_usermeta();
 412          $this->import_subscribe2();
 413          return true;
 414      }
 415  
 416  	function import_usermeta() 
 417      {
 418          global $wpdb;
 419          $metas = $wpdb->get_results( "SELECT * FROM $wpdb->usermeta WHERE meta_key = 's2_subscribed'" );
 420  
 421          if ( $metas ) 
 422          {
 423              $this->message_report( " IMPORTING  ! " . $wpdb->usermeta );
 424  
 425              foreach ( $metas as $meta )
 426              {
 427                  $user     = get_userdata( $meta->user_id );
 428                  $email     = $user->user_email;
 429                  $mp_user_id    = MP_User::get_id_by_email( $email );
 430  
 431                  if ( 'deleted' != MP_User::get_status_by_email( $email ) )
 432                  {
 433                      $xl = strlen( $email );
 434                      $xl = ( ( 25 - $xl ) < 0 ) ? 0 : 25 - $xl;
 435                      $x = $email . str_repeat( ' ', $xl );
 436                      $this->message_report( " **WARNING* ! $x already exists." );
 437                      continue;
 438                  }
 439  
 440                  $mp_user_id = $this->sync_mp_user( $email );
 441  
 442                  if ( '-1' == $meta->meta_value ) continue;
 443                  $recap = explode( ', ', $meta->meta_value );
 444                  if ( empty( $recap ) ) continue;
 445                  
 446                  unset( MP_AdminPage::$pst_['keep_newsletters'] );
 447                  unset( MP_AdminPage::$pst_['keep_mailinglists'] );
 448  
 449                  foreach ( $recap as $cat_id )
 450                  {
 451                      if ( isset( MP_AdminPage::$pst_['usermeta_nl_' . $cat_id] ) ) if ( '' != MP_AdminPage::$pst_['usermeta_nl_' . $cat_id] ) MP_AdminPage::$pst_['keep_newsletters'] [MP_AdminPage::$pst_['usermeta_nl_' . $cat_id]] = 'on';
 452                      if ( isset( MP_AdminPage::$pst_['usermeta_ml_' . $cat_id] ) ) if ( '' != MP_AdminPage::$pst_['usermeta_ml_' . $cat_id] ) MP_AdminPage::$pst_['keep_mailinglists'][MP_AdminPage::$pst_['usermeta_ml_' . $cat_id]] = 'on';
 453                  }
 454                  if ( isset( MP_AdminPage::$pst_['keep_newsletters'] ) )     MailPress_newsletter::update_checklist( $mp_user_id );
 455                  if ( isset( MP_AdminPage::$pst_['keep_mailinglists'] ) )     MailPress_mailinglist::update_checklist( $mp_user_id );
 456              }
 457          }
 458          return true;
 459      }
 460  
 461  	function import_subscribe2() 
 462      {
 463          global $wpdb;
 464          $subs = $wpdb->get_results( "SELECT email, active FROM $this->maintable " );
 465  
 466          if ( $subs ) 
 467          {
 468              $this->message_report( " IMPORTING  ! " .$this->maintable );
 469              foreach ( $subs as $sub )
 470              {
 471                  switch ( $sub->active )
 472                  {
 473                      case 1 :
 474                          if ( 'deleted' != MP_User::get_status_by_email( $sub->email ) )
 475                          {
 476                              $xl = strlen( $sub->email );
 477                              $xl = ( ( 25 - $xl ) < 0 ) ? 0 : 25 - $xl;
 478                              $x = $sub->email . str_repeat( ' ', $xl );
 479                              $this->message_report( " **WARNING* ! $x already exists." );
 480                              break;
 481                          }
 482  
 483                          $mp_user_id = $this->sync_mp_user( $sub->email );
 484  
 485                          if ( $mp_user_id )
 486                          {
 487                               if ( isset( MP_AdminPage::$pst_['keep_newsletters'] ) )     MailPress_newsletter::update_checklist( $mp_user_id );
 488                               if ( isset( MP_AdminPage::$pst_['keep_mailinglists'] ) )     MailPress_mailinglist::update_checklist( $mp_user_id );
 489                          }
 490                      break;
 491                      default :
 492                          if ( 'deleted' != MP_User::get_status_by_email( $sub->email ) ) break;
 493  
 494                          $mp_user_id = $this->sync_mp_user( $sub->email, 'waiting' );
 495                      break;
 496                  }
 497              }
 498  
 499          }
 500          elseif( $wpdb->last_error )
 501              $this->message_report( "** ERROR ** ! " . sprintf( 'Database error : %1$s', $wpdb->last_error ) );
 502          else
 503              $this->message_report( "** ERROR ** ! " . sprintf( 'Main table : %1$s empty', $this->maintable ) );
 504          return true;
 505      }
 506  }
 507  new MP_import_subscribe2( __( 'Import data from <strong>subscribe2</strong> plugin.', 'MailPress' ), __( 'Import from Subscribe2', 'MailPress' ) );


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