Below are the two things I'm trying to solve, logically. This is just a proof of concept; and I'm not implementing this on real data nor via full plugin at this time. Just looking at logic and syntax solutions.
Below are the (2) requirements.
1.) Given an API route for retrieving a specific record, do the following:
- Get the id param
- Return a 404 if the record isn't found
- Return a response with the record if it is found
Route format:
/publishers/(?P<id>\d+)
/publishers/397
2.) Add a WordPress hook to determine the value of the visible property on some item. Then use that hook elsewhere to change the value to false;
Below is what I have so far. Which appears correct; but just would like to see if there is a more optimal way, and/or this is still best approach. Just some feedback on my code solution; via the above two points.
My Code solutions:
<?php
function get_publisher( \WP_REST_Request $request ) {
$request = json_decode( $request );
$publisher_id = $request[ 'ID' ];
if ( empty( $publisher ) ) {
$response = [
'status' => 404,
'data' => [
'errors' => [
'general' => 'Sorry, no record was found.'
],
],
];
return json_encode( $response );
}
$response = [
'status' => 200,
'data' => [
'publisher' => $publisher,
]
];
return json_encode( $response );
}
function modify_item( $item ) {
$item->visible = apply_filters( 'item_visibility', true );
return $item;
}
function hide_item() {
return false;
}
add_filter( 'item_visibility', 'hide_item' );