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; } }
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); }
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'); }
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)); }
}, 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';