-
Notifications
You must be signed in to change notification settings - Fork 534
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
How to save annotations with store plugin? #711
Comments
Hi I'm successfully using this with WordPress. I will share some of my code here. wp_enqueue_script(
'annotator',
THEME_REL . '/assets/annotator.min.js',
'jquery',
filemtime( THEME_ABS . '/assets/annotator.min.js' ),
true
);
wp_enqueue_script(
'e-edition-script',
THEME_REL . '/assets/e-edition.js',
jquery',
filemtime( THEME_ABS . '/assets/e-edition.js' ),
true
); The jQuery(function($) {
let app = new annotator.App();
app.include(annotator.ui.main);
app.include(annotator.storage.http, {
prefix: theme_l10n.siteURL +'/wp-json/annotation-notes/v1'
});
app.start().then(function () {
app.annotations.load();
})
}); Note: theme_l10n.siteURL is passed through The last thing you will need is to build your API like so: add_action( 'rest_api_init', 'annotation_note_api');
function annotation_note_api() {
register_rest_route( 'annotation-notes/v1', '/annotations/([\w-]+)', array(
// DELETE
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => 'delete_annotation_note'
),
// PUT
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => 'edit_annotation_note'
)
));
register_rest_route( 'annotation-notes/v1', '/annotations/', array(
// POST
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'add_annotation_note'
)
));
register_rest_route( 'annotation-notes/v1', '/search/', array(
// GET
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'get_all_notes'
)
));
} Note: All the |
Thank you, I try it but now I have this errors:
Maybe I can't configure my api correctly, I don't know. Annotator loads correctly, but can't save anything. |
Здрасти, предпочитам на английски да го караме, че може да е от полза на някой друг с подобен пробле. |
I tried it on a blank test site, you can see it here: |
Thanks for the link. I've checked the site and found that even https://tezi.tk/wp-json returns 403 which means that error would be valid for any API call. I've checked the WP REST API documentation and found that the most possible problem for this is the permalink. Most probably they are in the standard (default) ugly format. Try to change them to |
Can you check now? I get 404 could not connect to the annotation store, but I think https://tezi.tk/wp-json and annotation-notes are working now. |
The callbacks are easy to create. Check the code related to the Also where have you paste the API code (the PHP one). If you are adding thin into a theme it should be in the |
Ok, thank you! I previously put API code (without callback part, because I don't have functions right now) in ScriptsnStyles plugin, now I move it to functions.php, but it's the same result. I enabled debug and error log, but can't find any errors related to annotator. I will try to create callbacks but can't understand are they needed for annotation store to function properly? Is this 404 error related to this, or there is something else? |
I think there is another problem here (for the 404) I can see in https://tezi.tk/wp-json the new api for the annotations. But their namespace is
When an annotation is made it need to be stored some where, so next time the page is loaded it will check for the saved annotations and will fetch them if there is a match. Without the callback you can't save or search for the annotations. Basically your callback are going to do just that: save annotation, get annotation, delete annotation. And you will need to provide that functionality in the callbacks. |
|
On each place where you have The way you have registered the endpoints will point to |
Ah, and you are missing the callback parameter. This most probably will end in Fatal error (500 or 503). You need to add callback to make the annotation functional. |
Ok, fix this but as you said now I have 500 error, even with callback parameter, maybe because I don't create the corresponding functions. Can you help me with some examples of how to make functions for read, add, edit and save annotations, I'm not really sure how my functions to looks like because I never used API calls before. |
What is your use case of the annotation tool? I can past my callback here but may be you need something else. |
Sorry for the late response! For one of my sites I want to use annotator as comment tool, I want unregistered users to make annotations, and all annotations to be visible for all visitors, but only I as admin to have privileges to edit or delete them. I'm not sure how easy will be to manage all published annotations and if there is any way to prevent spam. |
Ok, so my case is quite simpler. I needed to store annotations per user. And each annotation is visible only to the use that have created it. Any way, You might find my code useful and may be modify it to fit your needs. Here is the code: dd_action( 'rest_api_init', 'aa_note_api' );
function aa_note_api() {
register_rest_route( 'annotation-notes/v1', '/annotations/([\w-]+)', array(
// DELETE
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => 'annotations_delete_annotation_note'
),
// PUT
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => 'annotations_edit_annotation_note'
)
) );
register_rest_route( 'annotation-notes/v1', '/annotations/', array(
// POST
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'annotations_add_annotation_note'
)
) );
register_rest_route( 'annotation-notes/v1', '/search/', array(
// GET
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'annotations_get_all_notes'
)
) );
}
function annotations_get_all_notes( WP_REST_Request $request ) {
$user_id = ! empty( $_COOKIE['uid'] ) ? $_COOKIE['uid'] : '';
$page_id = ! empty( $_COOKIE['pid'] ) ? $_COOKIE['pid'] : '';
if ( $user_id == '' || $page_id == '' ) {
return new WP_REST_Response( [], 200 );
}
global $wpdb;
$sql = "SELECT uid, ranges, note, quote FROM {$wpdb->prefix}annotations_notes WHERE user_id = %d AND page_id = %d";
$results = $wpdb->get_results( $wpdb->prepare( $sql, $user_id, $page_id ) );
if ( ! empty( $results ) ) {
$data = [ 'total' => count( $results ), 'rows' => [] ];
foreach ( $results as $note ) {
$data['rows'][] = [ 'id' => $note->uid, 'ranges' => unserialize( $note->ranges ), 'text' => $note->note, 'quote' => $note->quote ];
}
return new WP_REST_Response( $data, 200 );
}
return new WP_REST_Response( [], 200 );
}
function annotations_add_annotation_note( WP_REST_Request $request ) {
$json = $request->get_json_params();
$user_id = ! empty( $_COOKIE['uid'] ) ? $_COOKIE['uid'] : '';
$page_id = ! empty( $_COOKIE['pid'] ) ? $_COOKIE['pid'] : '';
if ( $user_id == '' || $page_id == '' ) {
return new WP_REST_Response( [], 200 );
}
$note_id = $user_id . '-' . uniqid() . '-' . $page_id;
$ranges = serialize( $json['ranges'] );
global $wpdb;
$sql = "INSERT INTO {$wpdb->prefix}annotations_notes (uid, user_id, page_id, ranges, note, quote) VALUES (%s, %d, %d, %s, %s, %s)";
$wpdb->query( $wpdb->prepare( $sql, $note_id, $user_id, $page_id, $ranges, $json['text'], $json['quote'] ) );
if ( $wpdb->insert_id ) {
$data = [ 'id' => $note_id, 'ranges' => $json['ranges'], 'text' => $json['text'], 'quote' => $json['quote'] ];
return new WP_REST_Response( $data, 200 );
}
return new WP_Error( 'cant-delete', __( 'Can\'t create!', 'cpotheme' ), array( 'status' => 404 ) );
}
function annotations_delete_annotation_note( WP_REST_Request $request ) {
$json = $request->get_json_params();
global $wpdb;
$sql = "DELETE FROM {$wpdb->prefix}annotations_notes WHERE uid LIKE '%s'";
$delete = $wpdb->query( $wpdb->prepare( $sql, $json['id'] ) );
if ( $delete !== false && $delete !== 0 ) {
$data = [];
return new WP_REST_Response( $data, 204 );
}
return new WP_Error( 'cant-edit', __( 'Note can\'t be deleted', 'cpotheme' ), array( 'status' => 404 ) );
}
function annotations_edit_annotation_note( WP_REST_Request $request ) {
$json = $request->get_json_params();
global $wpdb;
$sql = "UPDATE {$wpdb->prefix}annotations_notes SET note = '%s' WHERE uid LIKE '%s'";
$update = $wpdb->query( $wpdb->prepare( $sql, $json['text'], $json['id'] ) );
if ( $update !== false && $update !== 0 ) {
$data = [
'id' => $json['id'],
'ranges' => $json['ranges'],
'text' => $json['text'],
'quote' => $json['quote']
];
return new WP_REST_Response( $data, 200 );
}
return new WP_Error( 'cant-edit', __( 'Note can\'t be edited', 'cpotheme' ), array( 'status' => 400 ) );
} Note that there is custom database where I store the annotations. It is called {$wpdb->prefix}annotations_notes and has the following columns: id, uid, user_id, page_id, ranges, note, quote. Hope this helps. |
Thank you! I use your code and create table annotations_notes with same prefix as my other tables in the database and add id, uid, user_id, page_id, ranges, note, quote columns to the table. Now I can't see any errors, but when I try to add annotation noting happened. The text is not highlighted and nothing is stored in database table. |
Check the function |
I try to set cookies in single post template file but still not working. |
This might be a mistake from the markdown but you don't set php code in if ( $user_id == '' || $page_id == '' ) {
return new WP_REST_Response( [], 200 );
} |
I test annotator on wordpress site, stable version works great, but can't understand how to use store plugin to save annotations. I use this code:
`jQuery(function ($) {
$('.main-container').annotator()
.annotator('addPlugin', 'Store', {
prefix: 'http://mysite.com/api' ,
annotationData: {
'uri': '/annotations'
},
});
});
`
the plugin gets error messeges and in the console i see this error:
I'm not sure how to make it work.
The text was updated successfully, but these errors were encountered: