save() public method

Chooses an appropriate storage and calls its {@see VersionPress\Storages\Storage::save() save()} method.
See also: Storage::save()
public save ( string $entityName, array $data )
$entityName string Entity type determines the storage used
$data array Data passed to the `VersionPress\Storages\Storage::save()` method
示例#1
0
 /**
  * @test
  */
 public function mirrorUsesRightStorage()
 {
     $entityName = 'some-entity';
     $someEntity = ['foo' => 'bar'];
     $fakeStorageFactory = $this->getMockBuilder(StorageFactory::class)->disableOriginalConstructor()->getMock();
     $fakeReplacer = $this->getMockBuilder(AbsoluteUrlReplacer::class)->disableOriginalConstructor()->getMock();
     $fakeStorage = $this->getMockBuilder(Storage::class)->disableOriginalConstructor()->getMockForAbstractClass();
     $fakeReplacer->expects($this->once())->method('replace')->will($this->returnArgument(0));
     $fakeStorageFactory->expects($this->once())->method('getStorage')->with($entityName)->will($this->returnValue($fakeStorage));
     $fakeStorage->expects($this->once())->method('save')->with($someEntity);
     /**
      * @var StorageFactory $fakeStorageFactory
      * @var AbsoluteUrlReplacer $fakeReplacer
      */
     $mirror = new Mirror($fakeStorageFactory, $fakeReplacer);
     $mirror->save($entityName, $someEntity);
 }
示例#2
0
 private function updateEntity($data, $entityName, $id)
 {
     $vpId = $this->vpidRepository->getVpidForEntity($entityName, $id);
     $data['vp_id'] = $vpId;
     if ($this->dbSchemaInfo->isChildEntity($entityName)) {
         $entityInfo = $this->dbSchemaInfo->getEntityInfo($entityName);
         $parentVpReference = "vp_" . $entityInfo->parentReference;
         if (!isset($data[$parentVpReference])) {
             $table = $this->dbSchemaInfo->getPrefixedTableName($entityName);
             $parentTable = $this->dbSchemaInfo->getTableName($entityInfo->references[$entityInfo->parentReference]);
             $vpidTable = $this->dbSchemaInfo->getPrefixedTableName('vp_id');
             $parentVpidSql = "SELECT HEX(vpid.vp_id) FROM {$table} t JOIN {$vpidTable} vpid ON t.{$entityInfo->parentReference} = vpid.id AND `table` = '{$parentTable}' WHERE {$entityInfo->idColumnName} = {$id}";
             $parentVpid = $this->database->get_var($parentVpidSql);
             $data[$parentVpReference] = $parentVpid;
         }
     }
     $shouldBeSaved = $this->mirror->shouldBeSaved($entityName, $data);
     if (!$shouldBeSaved) {
         return;
     }
     $savePostmeta = !$vpId && $entityName === 'post';
     // the post exists in DB for a while but until now it wasn't tracked, so we have to save its postmeta
     if (!$vpId) {
         $data = $this->vpidRepository->identifyEntity($entityName, $data, $id);
     }
     $this->mirror->save($entityName, $data);
     if (!$savePostmeta) {
         return;
     }
     $postmeta = $this->database->get_results("SELECT meta_id, meta_key, meta_value FROM {$this->database->postmeta} WHERE post_id = {$id}", ARRAY_A);
     foreach ($postmeta as $meta) {
         $meta['vp_post_id'] = $data['vp_id'];
         $meta = $this->vpidRepository->replaceForeignKeysWithReferences('postmeta', $meta);
         if (!$this->mirror->shouldBeSaved('postmeta', $meta)) {
             continue;
         }
         $meta = $this->vpidRepository->identifyEntity('postmeta', $meta, $meta['meta_id']);
         $this->mirror->save('postmeta', $meta);
     }
 }
 /**
  * Saves all already existing meta and M:N references for an entity that wasn't tracked yet
  *
  * @param array $data
  * @param string $entityName
  */
 private function storeRelatedEntities($data, $entityName)
 {
     $id = $data[$this->dbSchemaInfo->getEntityInfo($entityName)->idColumnName];
     foreach ($this->dbSchemaInfo->getAllEntityNames() as $referencedEntityName) {
         $entityInfo = $this->dbSchemaInfo->getEntityInfo($referencedEntityName);
         if ($this->dbSchemaInfo->isChildEntity($referencedEntityName) && $entityInfo->references[$entityInfo->parentReference] === $entityName) {
             $childEntities = $this->database->get_results("SELECT * FROM {$this->dbSchemaInfo->getPrefixedTableName($referencedEntityName)} WHERE `{$entityInfo->parentReference}` = '{$id}'", ARRAY_A);
             foreach ($childEntities as $childEntity) {
                 $childEntity = $this->vpidRepository->replaceForeignKeysWithReferences($referencedEntityName, $childEntity);
                 if (!$this->mirror->shouldBeSaved($referencedEntityName, $childEntity)) {
                     continue;
                 }
                 $id = $childEntity[$entityInfo->idColumnName];
                 $vpid = $this->vpidRepository->getVpidForEntity($referencedEntityName, $id);
                 if ($vpid) {
                     $childEntity[$entityInfo->vpidColumnName] = $vpid;
                 } else {
                     $childEntity = $this->vpidRepository->identifyEntity($referencedEntityName, $childEntity, $childEntity[$entityInfo->idColumnName]);
                 }
                 $childEntity = $this->shortcodesReplacer->replaceShortcodesInEntity($referencedEntityName, $childEntity);
                 $this->mirror->save($referencedEntityName, $childEntity);
             }
         }
     }
     foreach ($this->dbSchemaInfo->getAllMnReferences() as $mnReferenceDetails) {
         if ($mnReferenceDetails['source-entity'] === $entityName) {
             $junctionTable = $mnReferenceDetails['junction-table'];
             $prefixedJunctionTable = $this->dbSchemaInfo->getPrefixedTableName($junctionTable);
             $sourceColumn = $mnReferenceDetails['source-column'];
             $references = $this->database->get_results("SELECT * FROM `{$prefixedJunctionTable}` WHERE `{$sourceColumn}` = '{$id}'", ARRAY_A);
             foreach ($references as $reference) {
                 $reference = $this->vpidRepository->replaceForeignKeysWithReferences($junctionTable, $reference);
                 $this->mirror->save($junctionTable, $reference);
             }
         }
     }
 }
示例#4
0
function createUpdatePostTermsHook(Mirror $mirror, VpidRepository $vpidRepository)
{
    return function ($postId) use($mirror, $vpidRepository) {
        /** @var array $post */
        $post = get_post($postId, ARRAY_A);
        if (!$mirror->shouldBeSaved('post', $post)) {
            return;
        }
        $postType = $post['post_type'];
        $taxonomies = get_object_taxonomies($postType);
        $postVpId = $vpidRepository->getVpidForEntity('post', $postId);
        $postUpdateData = array('vp_id' => $postVpId, 'vp_term_taxonomy' => array());
        foreach ($taxonomies as $taxonomy) {
            $terms = get_the_terms($postId, $taxonomy);
            if ($terms) {
                $referencedTaxonomies = array_map(function ($term) use($vpidRepository) {
                    return $vpidRepository->getVpidForEntity('term_taxonomy', $term->term_taxonomy_id);
                }, $terms);
                $postUpdateData['vp_term_taxonomy'] = array_merge($postUpdateData['vp_term_taxonomy'], $referencedTaxonomies);
            }
        }
        if (count($taxonomies) > 0) {
            $mirror->save("post", $postUpdateData);
        }
    };
}