public function attachAndGetFormMeta(&$rg_form)
 {
     static $table = 'rg_form_meta';
     $form_id = (int) $rg_form['id'];
     $result = $this->_db->_query('SELECT * FROM ' . $this->_db->getTable($table) . ' WHERE form_id = ' . $form_id);
     if ($result) {
         if (isset($result[1])) {
             throw new Exception('Expected 1 rg_form_meta record per rg_form::id, not ' . count($result));
         }
         foreach ($result as &$item) {
             foreach ($item as $k => $v) {
                 if ($v && isset($v[0]) && isset($v[1])) {
                     $loopResult = $v;
                     if ($v[0] === '{' || $v[0] === '[') {
                         // If object or array in JSON
                         $loopResult = json_decode($v, true);
                     } else {
                         if ($v[0] === 'a' && $v[1] === ':') {
                             // Gravity Forms seems to always serialize an array of data, so 'a:' is
                             // always the first two characters.
                             $loopResult = @unserialize($v);
                             if ($loopResult === FALSE) {
                                 throw new Exception('Failed to unserialize in ' . __CLASS__ . '::' . __FUNCTION__);
                             }
                         }
                     }
                     $item[$k] = $loopResult;
                 }
             }
             unset($item);
         }
         $result = reset($result);
         $rg_form[self::HINT]['meta'] = $result;
     }
     return $result;
 }
 /**
  * Write an ElementContent block with $Content.
  *
  * @return boolean
  */
 public function setElementalContent(DataObjectInterface $record, $post_content)
 {
     if (!isset($this->_classes_using_wordpress_extension['ElementContent'])) {
         throw new WordpressImportException('Must put "WordpressImportDataExtension" on BaseElement.');
     }
     if (!isset($this->_classes_using_elemental[$record->class])) {
         if ($record instanceof SiteTree) {
             throw new WordpressImportException('Must put "WordpressImportDataExtension" on SiteTree.');
         } else {
             throw new WordpressImportException('Must put "WordpressImportDataExtension" on ' . $record->class . '.');
         }
     }
     $post_content = trim($this->_db->process('post_content', $post_content));
     $elementBlocks = $record->ElementArea()->Elements();
     if (!$record->exists()) {
         $subRecord = ElementContent::create();
         $subRecord->HTML = $post_content;
         $subRecord->WordpressData = $record->WordpressData;
         $elementBlocks->add($subRecord);
         // note(Jake): when page $record is written, the rest will write into it. 3.2+ at least.
     } else {
         $subRecord = $elementBlocks->filter(array('WordpressID' => $record->WordpressID, 'ClassName' => 'ElementContent'))->first();
         $isNew = false;
         if (!$subRecord) {
             $subRecord = ElementContent::create();
             $isNew = true;
         }
         if ($subRecord->HTML !== $post_content) {
             // Avoid getChangedFields triggering below by checking equality first.
             $subRecord->HTML = $post_content;
         }
         $subRecord->WordpressData = $record->WordpressData;
         $changedFields = $subRecord->getChangedFields(true, DataObject::CHANGE_VALUE);
         if ($changedFields) {
             try {
                 $isPublished = $subRecord->isPublished();
                 $this->writeAndPublishRecord($subRecord);
                 return true;
             } catch (Exception $e) {
                 $this->log($subRecord, 'error', $e, 1);
                 throw $e;
             }
         }
         if ($isNew) {
             $elementBlocks->add($subRecord);
         }
     }
 }
    public function run($request)
    {
        if (!Director::is_cli() && !isset($_GET['run'])) {
            DB::alteration_message('Must add ?run=1', 'error');
            return false;
        }
        if (!Director::is_cli()) {
            // - Add UTF-8 so characters will render as they should when debugging (so you can visualize inproperly formatted characters easier)
            // - Add base_tag so that printing blocks of HTML works properly with relative links (helps with visualizing errors)
            ?>
			<head>
				<?php 
            echo SSViewer::get_base_tag('');
            ?>
				<meta charset="UTF-8">
			</head>
<?php 
        }
        increase_time_limit_to(300);
        WordpressDatabase::$default_config = $this->config()->default_db;
        $this->db = $this->wordpressImportService->getDatabase();
        // Login as default admin so 'canPublish()' definitely returns true in 'SiteTree::doPublish()'
        if (!Member::currentUser()) {
            $defaultAdmin = Member::default_admin();
            if ($defaultAdmin && $defaultAdmin->exists()) {
                Session::set('loggedInAs', $defaultAdmin->ID);
            }
        }
        // Unsure if the importing functionality can ever hit this, but just incase.
        if (Versioned::current_stage() !== 'Stage') {
            throw new Exception('Versioned::current_stage() must be "Stage".');
        }
        $this->runCustom($request);
    }