This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
forked from digibib/wp-review-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-review-publish.php
381 lines (341 loc) · 14.3 KB
/
wp-review-publish.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/*
Plugin Name: WP-review-publish
Plugin URI: https://github.com/digibib/wp-review-publish
Description: Publisér bokanbefalinger til Decihmans RDF-base direkte fra Wordpress
Version: 0.2.1
Author: Petter Goksøyr Åsen
Author URI: https://github.com/boutros
License: GPLv3
*/
add_action( 'init', 'create_bookreview_type' );
add_action( 'admin_init', 'book_reviews_admin_init' );
add_action( 'admin_menu', 'book_reviews_settings_menu' );
add_action( 'save_post', 'process_book_review_fields', 10, 2 );
add_filter( 'pre_get_posts', 'show_book_reviews_as_posts' );
add_action( 'wp_trash_post', 'remove_rdf' );
add_action( 'untrash_post', 'remove_uri' );
add_action( 'admin_notices', 'my_admin_notices' );
add_action( 'save_post', 'remove_rdf_if_draft' );
add_filter( 'manage_edit-book_reviews_columns', 'my_columns' );
add_action('admin_head-post.php','validate_metadata');
add_action('admin_head-post-new.php','validate_metadata');
if (!session_id())
session_start();
// Display custom messages to inform user if successfull push to
// deichman's rdfstore or not
function my_admin_notices(){
if(!empty($_SESSION['my_admin_notices'])) {
print $_SESSION['my_admin_notices'];
unset ($_SESSION['my_admin_notices']);
}
}
function create_bookreview_type() {
register_post_type( 'book_reviews',
array(
'labels' => array(
'name' => __('Bokanbefalinger', 'wp-review-publish'),
'singular_name' => __('Bokanbefaling', 'wp-review-publish'),
'add_new' => __('Skriv ny', 'wp-review-publish'),
'add_new_item' => __('Skriv bokanbefaling', 'wp-review-publish'),
'edit' => __('Rediger', 'wp-review-publish'),
'edit_item' => __('Rediger bokanbefaling', 'wp-review-publish'),
'new_item' => __('Ny bokanbefaling', 'wp-review-publish'),
'view' => __('Vis', 'wp-review-publish'),
'view_item' => __('Vis bokanbefaling', 'wp-review-publish'),
'search_items' => __('Søk i bokanbefalinger', 'wp-review-publish'),
'not_found' => __('Fant ingen bokanbefalinger', 'wp-review-publish'),
'not_found_in_trash' => __('Ingen bokanbefalinger i søppelkurven', 'wp-review-publish'),
'parent' => __('Overordnet bokanbefaling', 'wp-review-publish')
),
'public' => true,
'menu_position' => 20,
'supports' =>
array( 'author', 'title', 'editor', 'comments','thumbnail'),
'taxonomies' => array( '' ),
'has_archive' => true,
'rewrite' => array( 'slug' => 'bokomtaler')
)
);
add_theme_support( 'post-thumbnails', array( 'post', 'page', 'book_reviews' ) );
}
function book_reviews_admin_init() {
if ( get_option( 'deichman_api_key') == false) {
add_option( 'deichman_api_key');
}
// allow tagging and categories for book reviews
register_taxonomy_for_object_type('category', 'book_reviews');
register_taxonomy_for_object_type('post_tag', 'book_reviews');
add_action( 'admin_post_save_book_reviews_options', 'save_book_reviews_options');
add_meta_box ( 'book_reviews_metadata',
'Metadata',
'display_book_review_metadata_box',
'book_reviews', 'normal', 'high');
}
function display_book_review_metadata_box ( $book_review ) {
$book_isbn = esc_html( get_post_meta( $book_review->ID, 'book_isbn', true ) );
$review_teaser = esc_html( get_post_meta( $book_review->ID, 'review_teaser', true ) );
$review_audience = esc_html( get_post_meta( $book_review->ID, 'review_audience', true ) );
$review_reviewer = esc_html( get_post_meta( $book_review->ID, 'review_reviewer', true ) );
$review_reviewer_email = esc_html( get_post_meta( $book_review->ID, 'review_reviewer_email', true ) );
$review_uri = esc_html( get_post_meta( $book_review->ID, 'review_uri', true ) );
?>
<p style="color:red"><strong><?php _e('Alle felt er obligatoriske', 'wp-review-publish'); ?></strong></p>
<table>
<tr>
<td style="width: 100%"><?php _e('ISBN', 'wp-review-publish'); ?><span style="color:red">*</span></td>
<td><input id="metadata-isbn" type="text" size="80" name="book_review_book_isbn"
value="<?php echo $book_isbn; ?>" /></td>
</tr>
<tr>
<td style="width: 100%"><?php _e('Teaser', 'wp-review-publish'); ?><span style="color:red">*</span></td>
<td>
<textarea id="metadata-teaser" name="review_teaser" rows="5" cols="79"><?php echo $review_teaser; ?></textarea>
</td>
</tr>
<tr>
<td style="width: 100%"><?php _e('Målgruppe(r)<span style="color:red">*</span> for <em>anbefalingen</em>, ikke for boka:', 'wp-review-publish'); ?></td>
<td><div id="metadata-audience" style="border: 1px solid #dedede; padding: 4px">
<fieldset>
<input id="a1" <?php if ( preg_match("/barn/", $review_audience)) echo 'checked="checked"'; ?> class="audiences" name="audience[]" type="checkbox" value="barn">
<label for="a1" class="checklabel"><?php _e('barn', 'wp-review-publish'); ?></label>
<input id="a2" <?php if ( preg_match("/ungdom/", $review_audience)) echo 'checked="checked"'; ?>class="audiences" name="audience[]" type="checkbox" value="ungdom">
<label for="a2" class="checklabel"><?php _e('ungdom', 'wp-review-publish'); ?></label>
<input id="a3" <?php if ( preg_match("/voksen/", $review_audience)) echo 'checked="checked"'; ?>class="audiences" name="audience[]" type="checkbox" value="voksen">
<label for="a3" class="checklabel"><?php _e('voksen', 'wp-review-publish'); ?></label>
</fieldset>
</div>
</td>
</tr>
<tr>
<td style="width: 100%"><?php _e('Anmelders epost<span style="color:red">*</span>', 'wp-review-publish'); ?></td>
<td><input id="metadata-email" type="text" size="80" name="review_reviewer_email"
value="<?php echo $review_reviewer_email; ?>" /></td>
</tr>
<tr>
<td style="width: 100%"><?php _e('Anmelders navn<span style="color:red">*</span>', 'wp-review-publish'); ?></td>
<td><input id="metadata-name" type="text" size="80" name="review_reviewer"
value="<?php echo $review_reviewer; ?>" /></td>
</tr>
<tr>
<td style="width: 100%"><?php _e('URI (fylles ut automatisk etter publisering)', 'wp-review-publish'); ?></td>
<td><input type="text" disabled="true" size="80" name="review_uri"
value="<?php echo $review_uri; ?>" /></td>
</tr>
</table>
<?php
}
function validate_metadata() {
global $post;
if ( is_admin() && $post->post_type == 'book_reviews' ){
?>
<script language="javascript" type="text/javascript">
jQuery(document).ready(function() {
jQuery('#publish').on('click', function(e) {
var inputs = ["title", "metadata-isbn", "metadata-name", "metadata-teaser", "metadata-email"];
var valid = true;
inputs.forEach(function( i ) {
var el = document.getElementById(i);
if ( !el.value ) {
valid = false;
el.style.borderColor = "red";
} else {
el.style.borderColor = "#dedede";
}
});
var audienceMissing = true;
jQuery("#"+"metadata-audience").find("input").each(function ( i ) {
if ( this.checked == true ) {
audienceMissing = false;
}
});
if ( audienceMissing ) {
document.getElementById("metadata-audience").style.borderColor = "red";
valid = false;
}
if ( !valid ) {
jQuery('.temp-error').remove();
jQuery('#publish').parent().parent().prepend("<div class='error temp-error'><?php _e('Fyll ut alle obligatoriske felt!', 'wp-review-publish'); ?></div>");
e.preventDefault();
}
});
});
</script>
<?php
}
}
function process_book_review_fields( $book_review_id, $book_review ) {
// Check post type for book reviews
if ( $book_review->post_type != 'book_reviews' )
return;
// Store data in meta table if present in post data
if ( isset( $_POST['book_review_book_isbn'] ) ) {
update_post_meta( $book_review_id, 'book_isbn', $_POST['book_review_book_isbn'] );
}
if ( isset( $_POST['review_teaser'] ) ) {
update_post_meta( $book_review_id, 'review_teaser', $_POST['review_teaser'] );
}
if ( isset( $_POST['audience'] ) ) {
$s= join("|", $_POST['audience']);
update_post_meta( $book_review_id, 'review_audience', $s );
} else {
update_post_meta( $book_review_id, 'review_audience', "" );
}
if ( isset( $_POST['review_reviewer'] ) ) {
update_post_meta( $book_review_id, 'review_reviewer', $_POST['review_reviewer'] );
}
if ( isset( $_POST['review_reviewer_email'] ) ) {
update_post_meta( $book_review_id, 'review_reviewer_email', $_POST['review_reviewer_email'] );
}
if ( isset( $_POST['review_audience'] ) && $_POST['review_audience'] != '0') {
update_post_meta( $book_review_id, 'review_audience', $_POST['review_audience'] );
}
// Don't do continue if post is draft
if ( $book_review->post_status != "publish" )
return;
// set up HTTP request for push data.deichman.no
if( !class_exists( 'WP_Http' ) ) {
include_once( ABSPATH . WPINC. '/class-http.php' );
}
$request = new WP_Http;
$url = 'http://marc2rdf.deichman.no/api/reviews';
// Check if all parameters are present:
// required: text, teaser, author, (review)title, audience
// optional: reviewer
$cleaned_text = preg_replace('~^\s*(.*?)\s*$~sm', '<p>$1</p>', $book_review->post_content);
$body = array (
"published" => true,
"title" => $book_review->post_title,
"text" => $cleaned_text,
"teaser" => get_post_meta( $book_review_id, 'review_teaser', true ),
"api_key" => get_option( 'deichman_api_key' ),
"reviewer" => get_post_meta( $book_review_id, 'review_reviewer_email', true )
);
$audience = get_post_meta( $book_review_id, 'review_audience', true );
if ( $audience != "0" && !empty($audience) ) {
$body["audience"] = $audience;
}
$reviewer = get_post_meta( $book_review_id, 'review_reviewer', true );
if ( $reviewer != "0" && !empty($reviewer) ) {
$body["reviewer_name"] = $reviewer;
}
$uri = get_post_meta( $book_review_id, 'review_uri', true );
// If review is published not before
if ( empty($uri) ) {
// perform POST
$body["isbn"] = get_post_meta( $book_review_id, 'book_isbn', true );
$body = json_encode( $body );
$result = $request->request( $url,
array( 'method' => 'POST', 'body' => $body ) );
if ( $result["response"]["code"] != 201 ) {
$_SESSION['my_admin_notices'] .= '<div class="error"><p>' . __('Bokanbefaling push feilet fordi:', 'wp-review-publish') . '</p><p>'. $result["body"] .'</p></div>';
return false;
} else {
$_SESSION['my_admin_notices'] .= '<div class="updated"><p>' . __('Bokanbefaling pushet til anbefalinger.deichman.no', 'wp-review-publish') . '</p></div>';
}
$json = json_decode( $result["body"], true );
// If success, save uri to review metadata
if ( $json["works"][0]["reviews"][0]["uri"] != "" )
update_post_meta( $book_review_id, 'review_uri', $json["works"][0]["reviews"][0]["uri"] );
// else if review is updating an allready published review
} else {
$body["uri"] = $uri;
// perform PUT
$body = json_encode( $body );
$result = $request->request( $url,
array( 'method' => 'PUT', 'body' => $body ) );
if ( $result["response"]["code"] != 200 ) {
$_SESSION['my_admin_notices'] .= '<div class="error"><p>' . __('Bokanbefaling opdatering feilet fordi:', 'wp-review-publish') . '</p><p>'. $result["body"] .'</p></div>';
} else {
$_SESSION['my_admin_notices'] .= '<div class="updated"><p>' . __('Bokanbefaling oppdatert på anbefalinger.deichman.no', 'wp-review-publish') . '</p></div>';
}
}
}
function remove_rdf ( $id ) {
// Return if uri not present
$uri = get_post_meta( $id, 'review_uri', true );
if ( empty( $uri ) )
return;
$url = 'http://marc2rdf.deichman.no/api/reviews';
$post_data = array (
"uri" => $uri,
"api_key" => get_option( 'deichman_api_key' )
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
// adding the post variables to the request
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);
curl_close($ch);
if ( $result == FALSE ) {
$_SESSION['my_admin_notices'] .= '<div class="error"><p>' . __('Bokanbefaling sletting feilet fordi:', 'wp-review-publish') . '</p><p>'. curl_error($ch) .'</p></div>';
} else {
$_SESSION['my_admin_notices'] .= '<div class="updated"><p>' . __('Bokanbefaling fjernet fra anbefalinger.deichman.no', 'wp-review-publish') . '</p></div>';
}
}
function remove_rdf_if_draft ( $id ) {
$book_review = get_post( $id );
if ( $book_review->post_status == 'draft' || $book_review->post_status == 'pending' ) {
remove_rdf( $id );
remove_uri( $id );
}
}
function remove_uri ( $id) {
// delete uri, in case post is restored from trash
delete_post_meta( $id, 'review_uri');
}
function save_book_reviews_options() {
if ( !current_user_can( 'manage_options') )
wp_die( 'Mangler rettigheter' );
check_admin_referer( 'deichman' );
if ( isset( $_POST['deichman_api_key'] ) ) {
$key = sanitize_text_field( $_POST['deichman_api_key'] );
update_option ( 'deichman_api_key', $key );
}
wp_redirect ( add_query_arg ( array( 'page' => 'wp-review-publish', 'message' => '1' ),
admin_url( 'options-general.php' ) ) );
exit;
}
function book_reviews_settings_menu() {
add_options_page( 'Bokanbefalinger konfigurasjon',
'Bokanbefalinger', 'manage_options',
'wp-review-publish', 'book_reviews_config_page');
}
function book_reviews_config_page() {
$key = get_option( 'deichman_api_key' );
?>
<div id="deichman-general" class="wrap">
<h2><?php _e('Bokanbefalinger konfigurasjon', 'wp-review-publish'); ?></h2>
<?php if ( isset( $_GET['message'] )
&& $_GET['message'] == '1' ) { ?>
<div id='message' class='updated fade'><p><strong><?php _e('Endring lagret.', 'wp-review-publish'); ?></strong></p></div>
<?php } ?>
<form method="post" action="admin-post.php">
<input type="hidden" name="action" value="save_book_reviews_options" />
<?php wp_nonce_field( 'deichman' ); ?>
API-nøkkel: <input type="text" name="deichman_api_key" value="<?php echo $key; ?>"/>
<input type="submit" value="Lagre" class="button-primary"/>
</form>
<p><?php _e('Har du ikke API-nøkkel, får du denne ved å kontakte Deichmanske bibliotek', 'wp-review-publish'); ?></p>
</div>
<?php
}
function show_book_reviews_as_posts ( $query ) {
if( ( is_home() && $query->is_main_query() ) || is_feed() || is_category() || is_tag() ){
if ( is_category() || is_tag() ) {
$post_types = array('nav_menu_item', 'post', 'book_reviews');
} else {
$post_types = array('post', 'book_reviews');
}
$query->set ( 'post_type', $post_types);
}
return $query;
}
function my_columns( $columns ) {
$columns['author'] = __('Forfatter', 'wp-review-publish');
unset( $columns['comments'] );
return $columns;
}
?>