Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MdAsifHossainNadim committed Apr 5, 2024
2 parents 4079b9b + dcb24aa commit c307a7c
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 225 deletions.
2 changes: 1 addition & 1 deletion assets/build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '019b62242c90b1fbaead');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '7c658741aacc01511b26');
6 changes: 4 additions & 2 deletions assets/build/index.js

Large diffs are not rendered by default.

42 changes: 41 additions & 1 deletion includes/API/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function register_routes() {
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'update_helpfullness' ],
'permission_callback' => '__return_true',
'permission_callback' => [ $this, 'helpful_update_permissions_check', ],
'args' => [
'type' => [
'required' => true,
Expand Down Expand Up @@ -709,6 +709,46 @@ public function create_item_permissions_check( $request ) {
return true;
}

/**
* Check helpful item permission.
*
* @since 2.1.5
*
* @param WP_REST_Request $request full data about the request
*
* @return WP_Error|bool true on success
*/
public function helpful_update_permissions_check( $request ) {
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', __( 'You are not currently logged in.', 'wedocs' ) );
}

$doc_id = absint( $request->get_param( 'id' ) );
$doc_status = get_post_status( $doc_id );
$doc_statuses = array( 'publish', 'private' );

// Check doc status is valid.
if ( ! in_array( $doc_status, $doc_statuses ) ) {
return new WP_Error( 'rest_doc_invalid_status', __( 'Doc status not valid for update helpful data', 'wedocs' ) );
}

$user_id = get_current_user_id();
$updated_doc_ids = get_user_meta( $user_id, 'wedocs_response', true );
$previous = ! empty( $updated_doc_ids ) ? explode( ',', $updated_doc_ids ) : [];

// Check doc status is valid.
if ( in_array( $doc_id, $previous ) ) {
return new WP_Error( 'rest_doc_invalid_helpful_update', __( 'Sorry, we have already recorded your feedback!', 'wedocs' ) );
}

array_push( $previous, $doc_id );
$responsed_doc_ids = implode( ',', $previous );

update_user_meta( $user_id, 'wedocs_response', $responsed_doc_ids );

return true;
}

/**
* Get the post, if the ID is valid.
*
Expand Down
Loading

0 comments on commit c307a7c

Please sign in to comment.