[ Index ]

MailPress 544

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


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