getDiff() public static method

Or the $newEntityData might be another full entity, for example in its new state. It doesn't really matter, the algo is always the same: it scans for new keys in $newEntityData and changed values on existing keys and adds those two things to the result. 'vp_id' key is ignored by default. Note: keys may never be "removed" (or marked as removed) in the diff because that will just not ever happen - the SQL UPDATE command is only capable of sending a change, clearing a key value at most but never "removing" it entirely.
public static getDiff ( array $oldEntityData, array $newEntityData ) : array
$oldEntityData array Usually a full entity data in its original state
$newEntityData array Full or partial data of the new entity
return array Key=>value pairs of things that are new or changed in $newEntity (they can never be "removed", that will never happen when capturing SQL UPDATE actions)
Ejemplo n.º 1
0
 public function save($data)
 {
     $vpid = $data[$this->entityInfo->vpidColumnName];
     if (!$vpid) {
         return null;
     }
     if ($this->entityInfo->usesGeneratedVpids) {
         // to avoid merge conflicts
         unset($data[$this->entityInfo->idColumnName]);
     }
     $data = $this->removeUnwantedColumns($data);
     if (!$this->shouldBeSaved($data)) {
         return null;
     }
     $filename = $this->getEntityFilename($vpid);
     $oldSerializedEntity = "";
     $isExistingEntity = $this->exists($vpid);
     if ($isExistingEntity) {
         $oldSerializedEntity = file_get_contents($filename);
     }
     $oldEntity = $this->deserializeEntity($oldSerializedEntity);
     $diff = EntityUtils::getDiff($oldEntity, $data);
     if (count($diff) > 0) {
         $newEntity = array_merge($oldEntity, $diff);
         $newEntity = array_filter($newEntity, function ($value) {
             return $value !== false;
         });
         FileSystem::mkdir(dirname($this->getEntityFilename($vpid)));
         file_put_contents($filename, $this->serializeEntity($vpid, $newEntity));
         return $this->createChangeInfo($oldEntity, $newEntity, !$isExistingEntity ? 'create' : 'edit');
     } else {
         return null;
     }
 }
Ejemplo n.º 2
0
 protected function createChangeInfo($oldEntity, $newEntity, $action = null)
 {
     if ($action === 'edit') {
         $diff = EntityUtils::getDiff($oldEntity, $newEntity);
     }
     if (isset($diff['comment_approved'])) {
         // determine more specific edit action
         if ($oldEntity['comment_approved'] === 'trash' && $newEntity['comment_approved'] === 'post-trashed' || $oldEntity['comment_approved'] === 'post-trashed' && $newEntity['comment_approved'] === 'trash') {
             $action = 'edit';
             // trash -> post-trashed and post-trashed -> trash are not interesting action for us
         } elseif ($diff['comment_approved'] === 'trash') {
             $action = 'trash';
         } elseif ($oldEntity['comment_approved'] === 'trash') {
             $action = 'untrash';
         } elseif ($diff['comment_approved'] === 'spam') {
             $action = 'spam';
         } elseif ($oldEntity['comment_approved'] === 'spam') {
             $action = 'unspam';
         } elseif ($oldEntity['comment_approved'] == 0 && $newEntity['comment_approved'] == 1) {
             $action = 'approve';
         } elseif ($oldEntity['comment_approved'] == 1 && $newEntity['comment_approved'] == 0) {
             $action = 'unapprove';
         }
     }
     if ($action === 'create' && $newEntity['comment_approved'] == 0) {
         $action = 'create-pending';
     }
     $author = $newEntity["comment_author"];
     $postTable = $this->database->prefix . 'posts';
     $vpIdTable = $this->database->prefix . 'vp_id';
     $result = $this->database->get_row("SELECT post_title FROM {$postTable} JOIN {$vpIdTable} ON {$postTable}.ID = {$vpIdTable}.id WHERE vp_id = UNHEX('{$newEntity['vp_comment_post_ID']}')");
     $postTitle = $result->post_title;
     return new CommentChangeInfo($action, $newEntity["vp_id"], $author, $postTitle);
 }
Ejemplo n.º 3
0
 protected function createChangeInfo($oldEntity, $newEntity, $action = null)
 {
     $diff = EntityUtils::getDiff($oldEntity, $newEntity);
     if ($oldEntity && isset($diff['name'])) {
         return new TermChangeInfo('rename', $newEntity['vp_id'], $newEntity['name'], 'term', $oldEntity['name']);
     }
     return new TermChangeInfo($action, $newEntity['vp_id'], $newEntity['name'], 'term');
 }
Ejemplo n.º 4
0
 protected function createChangeInfo($oldEntity, $newEntity, $action)
 {
     $diff = array();
     if ($action === 'edit') {
         // determine more specific edit action
         $diff = EntityUtils::getDiff($oldEntity, $newEntity);
         if (isset($diff['post_status']) && $diff['post_status'] === 'trash') {
             $action = 'trash';
         } elseif (isset($diff['post_status']) && $oldEntity['post_status'] === 'trash') {
             $action = 'untrash';
         } elseif (isset($diff['post_status']) && $oldEntity['post_status'] === 'draft' && $newEntity['post_status'] === 'publish') {
             $action = 'publish';
         }
     }
     if ($action == 'create' && $newEntity['post_status'] === 'draft') {
         $action = 'draft';
     }
     $title = $newEntity['post_title'];
     $type = $newEntity['post_type'];
     return new PostChangeInfo($action, $newEntity['vp_id'], $type, $title, array_keys($diff));
 }
Ejemplo n.º 5
0
}, 10, 4);
add_filter('vp_entity_files_option', function ($files, $oldEntity, $newEntity) {
    $optionName = isset($newEntity['option_name']) ? $newEntity['option_name'] : $oldEntity['option_name'];
    if ($optionName === 'rewrite_rules') {
        $files[] = ["type" => "path", "path" => ABSPATH . '/.htaccess'];
    }
    return $files;
}, 10, 3);
add_filter('vp_entity_action_comment', function ($action, $oldEntity, $newEntity) {
    if ($action === 'create' && $newEntity['comment_approved'] == 0) {
        return 'create-pending';
    }
    if ($action !== 'edit') {
        return $action;
    }
    $diff = EntityUtils::getDiff($oldEntity, $newEntity);
    if (!isset($diff['comment_approved'])) {
        return $action;
    }
    if ($oldEntity['comment_approved'] === 'trash' && $newEntity['comment_approved'] === 'post-trashed' || $oldEntity['comment_approved'] === 'post-trashed' && $newEntity['comment_approved'] === 'trash') {
        return 'edit';
        // trash -> post-trashed and post-trashed -> trash are not interesting action for us
    }
    if ($diff['comment_approved'] === 'trash') {
        return 'trash';
    }
    if ($oldEntity['comment_approved'] === 'trash') {
        return 'untrash';
    }
    if ($diff['comment_approved'] === 'spam') {
        return 'spam';