/**
  *@ singleton method used to instantiate class object
  *@ access public
  *@ return EE_Registry instance
  */
 public static function instance()
 {
     // check if class object is instantiated
     if (!self::$_instance instanceof EE_CPT_Strategy) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 /**
  * Runs the data migration scripts (well, each request to this method calls one of the
  * data migration scripts' migration_step() functions).
  *
  * @param int $step_size
  * @throws EE_Error
  * @return array {
  * 		// where the first item is one EE_Data_Migration_Script_Base's stati,
  * 		//and the second item is a string describing what was done
  *    	@type int $records_to_migrate from the current migration script
  *    	@type int $records_migrated
  * 		@type string $status one of EE_Data_Migration_Manager::status_*
  * 		@type string $script verbose name of the current DMS
  * 		@type string $message string describing what was done during this step
  * }
  */
 public function migration_step($step_size = 0)
 {
     //bandaid fix for issue https://events.codebasehq.com/projects/event-espresso/tickets/7535
     if (class_exists('EE_CPT_Strategy')) {
         remove_action('pre_get_posts', array(EE_CPT_Strategy::instance(), 'pre_get_posts'), 5);
     }
     try {
         $currently_executing_script = $this->get_last_ran_script();
         if (!$currently_executing_script) {
             //Find the next script that needs to execute
             $scripts = $this->check_for_applicable_data_migration_scripts();
             if (!$scripts) {
                 //huh, no more scripts to run... apparently we're done!
                 //but dont forget to make sure initial data is there
                 //we should be good to allow them to exit maintenance mode now
                 EE_Maintenance_Mode::instance()->set_maintenance_level(intval(EE_Maintenance_Mode::level_0_not_in_maintenance));
                 //saving migrations ran should actually be unnecessary, but leaving in place just in case
                 //remember this migration was finished (even if we timeout initing db for core and plugins)
                 $this->_save_migrations_ran();
                 //make sure DB was updated AFTER we've recorded the migration was done
                 $this->initialize_db_for_enqueued_ee_plugins();
                 return array('records_to_migrate' => 1, 'records_migrated' => 1, 'status' => self::status_no_more_migration_scripts, 'script' => __("Data Migration Completed Successfully", "event_espresso"), 'message' => __("All done!", "event_espresso"));
             }
             $currently_executing_script = array_shift($scripts);
             //and add to the array/wp option showing the scripts ran
             //				$this->_data_migrations_ran[$this->script_migrates_to_version(get_class($currently_executing_script))] = $currently_executing_script;
             $migrates_to = $this->script_migrates_to_version(get_class($currently_executing_script));
             $plugin_slug = $migrates_to['slug'];
             $version = $migrates_to['version'];
             $this->_data_migrations_ran[$plugin_slug][$version] = $currently_executing_script;
         }
         $current_script_name = get_class($currently_executing_script);
     } catch (Exception $e) {
         //an exception occurred while trying to get migration scripts
         $message = sprintf(__("Error Message: %sStack Trace:%s", "event_espresso"), $e->getMessage() . '<br>', $e->getTraceAsString());
         //record it on the array of data migration scripts ran. This will be overwritten next time we try and try to run data migrations
         //but that's ok-- it's just an FYI to support that we couldn't even run any data migrations
         $this->add_error_to_migrations_ran(sprintf(__("Could not run data migrations because: %s", "event_espresso"), $message));
         return array('records_to_migrate' => 1, 'records_migrated' => 0, 'status' => self::status_fatal_error, 'script' => __("Error loading data migration scripts", "event_espresso"), 'message' => $message);
     }
     //ok so we definitely have a data migration script
     try {
         //how big of a bite do we want to take? Allow users to easily override via their wp-config
         if (!absint($step_size) > 0) {
             $step_size = defined('EE_MIGRATION_STEP_SIZE') && absint(EE_MIGRATION_STEP_SIZE) ? EE_MIGRATION_STEP_SIZE : EE_Data_Migration_Manager::step_size;
         }
         //do what we came to do!
         $currently_executing_script->migration_step($step_size);
         //can we wrap it up and verify default data?
         $init_dbs = false;
         switch ($currently_executing_script->get_status()) {
             case EE_Data_Migration_Manager::status_continue:
                 $response_array = array('records_to_migrate' => $currently_executing_script->count_records_to_migrate(), 'records_migrated' => $currently_executing_script->count_records_migrated(), 'status' => EE_Data_Migration_Manager::status_continue, 'message' => $currently_executing_script->get_feedback_message(), 'script' => $currently_executing_script->pretty_name());
                 break;
             case EE_Data_Migration_Manager::status_completed:
                 //ok so THAT script has completed
                 $this->update_current_database_state_to($this->script_migrates_to_version($current_script_name));
                 $response_array = array('records_to_migrate' => $currently_executing_script->count_records_to_migrate(), 'records_migrated' => $currently_executing_script->count_records_migrated(), 'status' => EE_Data_Migration_Manager::status_completed, 'message' => $currently_executing_script->get_feedback_message(), 'script' => sprintf(__("%s Completed", 'event_espresso'), $currently_executing_script->pretty_name()));
                 //check if there are any more after this one.
                 $scripts_remaining = $this->check_for_applicable_data_migration_scripts();
                 if (!$scripts_remaining) {
                     //we should be good to allow them to exit maintenance mode now
                     EE_Maintenance_Mode::instance()->set_maintenance_level(intval(EE_Maintenance_Mode::level_0_not_in_maintenance));
                     ////huh, no more scripts to run... apparently we're done!
                     //but dont forget to make sure initial data is there
                     $init_dbs = true;
                     $response_array['status'] = self::status_no_more_migration_scripts;
                 }
                 break;
             default:
                 $response_array = array('records_to_migrate' => $currently_executing_script->count_records_to_migrate(), 'records_migrated' => $currently_executing_script->count_records_migrated(), 'status' => $currently_executing_script->get_status(), 'message' => sprintf(__("Minor errors occurred during %s: %s", "event_espresso"), $currently_executing_script->pretty_name(), implode(", ", $currently_executing_script->get_errors())), 'script' => $currently_executing_script->pretty_name());
                 break;
         }
     } catch (Exception $e) {
         //ok so some exception was thrown which killed the data migration script
         //double-check we have a real script
         if ($currently_executing_script instanceof EE_Data_Migration_Script_Base) {
             $script_name = $currently_executing_script->pretty_name();
             $currently_executing_script->set_broken();
             $currently_executing_script->add_error($e->getMessage());
         } else {
             $script_name = __("Error getting Migration Script", "event_espresso");
         }
         $response_array = array('records_to_migrate' => 1, 'records_migrated' => 0, 'status' => self::status_fatal_error, 'message' => sprintf(__("A fatal error occurred during the migration: %s", "event_espresso"), $e->getMessage()), 'script' => $script_name);
     }
     $successful_save = $this->_save_migrations_ran();
     if ($successful_save !== TRUE) {
         //ok so the current wp option didn't save. that's tricky, because we'd like to update it
         //and mark it as having a fatal error, but remember- WE CAN'T SAVE THIS WP OPTION!
         //however, if we throw an exception, and return that, then the next request
         //won't have as much info in it, and it may be able to save
         throw new EE_Error(sprintf(__("The error '%s' occurred updating the status of the migration. This is a FATAL ERROR, but the error is preventing the system from remembering that. Please contact event espresso support.", "event_espresso"), $successful_save));
     }
     //if we're all done, initialize EE plugins' default data etc.
     if ($init_dbs) {
         $this->initialize_db_for_enqueued_ee_plugins();
     }
     return $response_array;
 }