Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete meta functionality #419

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions includes/classes/API/SubscriptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,21 @@ public function receive_item_permissions_check( $request ) {
* @return WP_REST_Response|\WP_Error Response object on success, or \WP_Error object on failure.
*/
public function receive_item( $request ) {
$post = get_post( (int) $request['post_id'] );
$post_id = (int) $request['post_id'];
$post = get_post( $post_id );
if ( empty( $post ) ) {
return new \WP_REST_Response( null, 404, [ 'X-Distributor-Post-Deleted' => 'yes' ] );
}

$original_post_id = get_post_meta( $request['post_id'], 'dt_original_post_id', true );
$original_post_id = get_post_meta( $post_id, 'dt_original_post_id', true );

if ( empty( $original_post_id ) ) {
return new \WP_Error( 'rest_post_not_distributed', esc_html__( 'Post not distributed.', 'distributor' ), array( 'status' => 400 ) );
}

// This endpoint updates post data and unlinks posts
if ( isset( $request['original_deleted'] ) ) {
update_post_meta( $request['post_id'], 'dt_original_post_deleted', true );
update_post_meta( $post_id, 'dt_original_post_deleted', true );

$response = new \WP_REST_Response();
$response->set_data( array( 'updated' => true ) );
Expand Down Expand Up @@ -246,9 +247,24 @@ public function receive_item( $request ) {
'media' => ( isset( $request['post_data']['distributor_media'] ) ) ? $request['post_data']['distributor_media'] : [],
];

update_post_meta( (int) $request['post_id'], 'dt_subscription_update', $update );
// Check for deleted metas
$previous_update = get_post_meta( $post_id, 'dt_subscription_update', true );
if ( ! empty( $previous_update['meta'] ) && ! empty( $update['meta'] ) ) {
$new_meta_keys = array_keys( $update['meta'] );
$updated_meta_keys = array_keys( $previous_update['meta'] );
$diff = array_diff( $updated_meta_keys, $new_meta_keys );

if ( ! empty( $diff ) ) {
foreach ( $diff as $meta_key ) {
// Delete post metas which were present in previous request but do not exist in the current
delete_post_meta( $post_id, $meta_key );
}
}
}

update_post_meta( $post_id, 'dt_subscription_update', $update );

$unlinked = (bool) get_post_meta( $request['post_id'], 'dt_unlinked', true );
$unlinked = (bool) get_post_meta( $post_id, 'dt_unlinked', true );

if ( ! empty( $unlinked ) ) {
$response = new \WP_REST_Response();
Expand All @@ -259,7 +275,7 @@ public function receive_item( $request ) {

wp_update_post(
[
'ID' => $request['post_id'],
'ID' => $post_id,
'post_title' => $request['post_data']['title'],
'post_content' => $content,
'post_excerpt' => $request['post_data']['excerpt'],
Expand All @@ -271,15 +287,15 @@ public function receive_item( $request ) {
* We check if each of these exist since the API removes empty arrays from requests
*/
if ( ! empty( $request['post_data']['distributor_meta'] ) ) {
\Distributor\Utils\set_meta( $request['post_id'], $request['post_data']['distributor_meta'] );
\Distributor\Utils\set_meta( $post_id, $request['post_data']['distributor_meta'] );
}

if ( ! empty( $request['post_data']['distributor_terms'] ) ) {
\Distributor\Utils\set_taxonomy_terms( $request['post_id'], $request['post_data']['distributor_terms'] );
\Distributor\Utils\set_taxonomy_terms( $post_id, $request['post_data']['distributor_terms'] );
}

if ( ! empty( $request['post_data']['distributor_media'] ) ) {
\Distributor\Utils\set_media( $request['post_id'], $request['post_data']['distributor_media'] );
\Distributor\Utils\set_media( $post_id, $request['post_data']['distributor_media'] );
}

/**
Expand Down