forked from EEXCESS/wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheexcess-functionality.php
More file actions
278 lines (250 loc) · 11.6 KB
/
eexcess-functionality.php
File metadata and controls
278 lines (250 loc) · 11.6 KB
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
<?php
/**
* Plugin Name: EEXCESS
* Plugin URI: https://github.com/EEXCESS/eexcess
* Description: Gives you the ability to enrich your blog with well-selected and high quality content
* Version: 0.3
* Author: Andreas Eisenkolb and Nils Witt
* Author URI: https://github.com/AEisenkolb
* Author URI: https://github.com/n-witt
* License: Apache 2.0
*/
/* Copyright 2014 University of Passau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
?>
<?php
add_action( 'admin_init', 'init_eexcess_plugin' );
// Prepare stuff
function init_eexcess_plugin() {
global $pagenow;
// Register the script first.
wp_register_script( 'some_handle', plugins_url( '/js/eexcess-jquery-eventhandlers.js', __FILE__ ) );
// Now we can localize the script with our data.
$translation_array = array( 'pluginsPath' => plugin_dir_url( __FILE__ ) );
wp_localize_script( 'some_handle', 'pluginURL', $translation_array );
// The script can be enqueued now or later.
wp_enqueue_script( 'some_handle' );
// Load the scripts for the post creation / editing page
if($pagenow == 'post-new.php' || $pagenow == 'post.php') {
// init jQuery
wp_enqueue_script('jquery');
// init JavaScript
wp_enqueue_script( 'eexcess-settings', plugins_url( '/js/eexcess-settings.js', __FILE__ ), array('jquery') );
wp_enqueue_script( 'eexcess-pagination-script', plugins_url( '/js/lib/jquery.paginate.js', __FILE__), array('jquery') );
wp_enqueue_script( 'eexcess-templating-script', plugins_url( '/js/lib/handlebars-v1.3.0.js', __FILE__), array('jquery') );
wp_enqueue_script( 'eexcess-script', plugins_url( '/js/eexcess-script.js', __FILE__ ), array('jquery') );
wp_enqueue_script( 'eexcess-jquery-plugins', plugins_url( '/js/eexcess-jquery-plugins.js', __FILE__ ), array('jquery') );
//wp_enqueue_script( 'eexcess-jquery-eventhandlers', plugins_url( '/js/eexcess-jquery-eventhandlers.js', __FILE__ ), array('jquery') );
//for citeproc
wp_enqueue_script( 'CLSWrapper', plugins_url( '/js/CLSWrapper.js', __FILE__ ), array('jquery', 'eexcess-xmldom', 'eexcess-citeproc') );
wp_enqueue_script( 'eexcess-citeproc', plugins_url( '/js/lib/citeproc.js', __FILE__ ));
wp_enqueue_script( 'eexcess-xmldom', plugins_url( '/js/lib/xmldom.js', __FILE__ ));
// init styles
wp_enqueue_style( 'eexcess-styles', plugins_url( '/styles/eexcess-styles.css', __FILE__ ) );
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
/**
* Adds a box to the main column on the Post edit screen.
*/
function myplugin_add_meta_box() {
// @see http://codex.wordpress.org/Function_Reference/add_meta_box
add_meta_box(
'eexcess_container', // id
'EEXCESS', // title
'eexcess_meta_box_callback', // callback
'post' // post_type
);
}
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function eexcess_meta_box_callback( $post ) { ?>
<?php // List template ?>
<script id="list-template" type="text/x-handlebars-template">
<div id="recommendationList">
<ul id="eexcess-recommendationList">
{{#each result}}
<li data-id="{{id}}">
<div>
{{#if previewImage}}
<div class="eexcess-previewPlaceholder">
<a href="{{previewImage}}" target="_blank">
<img src="{{previewImage}}" alt="thumbnail"></img>
</a>
</div>
{{else}}
<div class="eexcess-previewPlaceholder"></div>
{{/if}}
<div class="recommendationTextArea">
<a target="_blank" href="{{uri}}">{{title}}</a>
</br>
{{#if creator}}
Creator: {{creator}}
{{else}}
Provider: {{facets.provider}}
{{/if}}
<br/>
{{#if facets.year}}
Published: {{facets.year}}
{{else}}
Language: {{facets.language}}
{{/if}}
<br/>
{{#if collectionName}}
<input type="hidden" name="collectionName" value="{{collectionName}}">
{{/if}}
{{#if creator}}
<input type="hidden" name="creator" value="{{creator}}">
{{/if}}
{{#if description}}
<input type="hidden" name="description" value="{{description}}">
{{/if}}
{{#if eexcessURI}}
<input type="hidden" name="eexcessURI" value="{{eexcessURI}}">
{{/if}}
{{#if facets.year}}
<input type="hidden" name="facets.year" value="{{facets.year}}">
{{/if}}
{{#if facets.language}}
<input type="hidden" name="facets.language" value="{{facets.language}}">
{{/if}}
{{#if id}}
<input type="hidden" name="id" value="{{id}}">
{{/if}}
<input name="addMatch" class="button button-small" id="addMatch" value="add" style="width: 40px" onfocus="this.blur();">
</div>
</div>
</li>
{{/each}}
</ul>
<div class='pagination-container'>
<div id='recommandationList-pagination'></div>
</div>
</div>
</script>
<input name="getRecommendations" class="button button-primary" id="getRecommendations" value="Get Recommendations">
<input name="abortRequest" class="button button-primary" id="abortRequest" value="Abort Request">
<select name="citationStyleDropDown" id="citationStyleDropDown" style="float: right">
<option value="default" selected="selected">Citation Style (default Hyperlink)</option>
<?php
// corresponds to EEXCESS.citeproc.stylesDir from eexcess-settings.js.
// unfortunatley there is no way to share that variable. At least AFAIK.
$citeprocStylesPath = plugin_dir_path(__FILE__) . 'js/lib/citeproc-js/citionStyles';
if ($handle = opendir($citeprocStylesPath)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$entry = str_replace(".csl", "", $entry);
echo '<option value="' . $entry . '">' . $entry . '</option>';
}
}
closedir($handle);
}
?>
</select>
<div id="searchQueryReflection" class="searchQueryReflection">
<span id="numResults"></span> Results on:
<span id="searchQuery" style="color: #000000"></span>
</div>
<div id="content">
<p>
Get recommendations for keywords by using "#eexcess:Keyword#" inside the textarea.
Furthermore, you can select parts of the text and then either click the "Get Recommendations"
button or you can use the keyboard shortcut ctrl + e.
</p>
<div class='eexcess-spinner'></div>
<div id='list'></div>
</div>
<?php }
// Ajax action handler
add_action( 'wp_ajax_get_recommendations', 'get_recommendations' );
// Callback function for the Ajax call
function get_recommendations() {
// Read the term form the POST variable
$items = $_POST['terms'];
$context = $_POST['trigger'];
/**
* URL: http://eexcess-dev.joanneum.at/eexcess-privacy-proxy/api/v1/recommend
* Alternative URL: http://132.231.111.197:8080/eexcess-privacy-proxy/api/v1/recommend
* METHOD: POST
* DEV: http://eexcess-dev.joanneum.at/eexcess-federated-recommender-web-service-1.0-SNAPSHOT/recommender/recommend
* Privacy Proxy
*/
//dev
//$proxyURL = "http://eexcess-dev.joanneum.at/eexcess-privacy-proxy/api/v1/recommend";
//stable
$proxyURL = "http://eexcess.joanneum.at/eexcess-privacy-proxy/api/v1/recommend";
// Data for the api call
$postData = array(
"numResults" => 100,
"contextKeywords" => array(),
"origin" => "WP",
"context" => array("reason" => $context, "value" => "")
);
// Creating the context list for the api call
foreach($items as $term) {
array_push($postData["contextKeywords"], array( "weight" => strval (1.0 / sizeof($items)), "text" => $term ));
}
//array_push($postData["context"], );
// Create context for the API call
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n",
'content' => json_encode($postData)
)
));
// Send the request and return the result
echo @file_get_contents($proxyURL, FALSE, $context);
die(); // this is required to return a proper result
}
// Hook for the WYSIWYG editor
add_filter( 'tiny_mce_before_init', 'tiny_mce_before_init' );
// Setting up the onKeyUp event for the WYSIWYG editor
function tiny_mce_before_init( $initArray ) {
$initArray['setup'] = "function(ed) {
ed.onKeyUp.add(function(ed, e) {
eexcessMethods.extractTerm(ed);
});
ed.onKeyDown.add(function(ed, e) {
eexcessMethods.assessKeystroke(e);
});
}";
return $initArray;
}
//adding a button to tinyMCE
add_action( 'admin_head', 'EEXCESS_add_tinymce' );
function EEXCESS_add_tinymce() {
global $typenow;
// only on Post Type: post and page
if( ! in_array( $typenow, array( 'post', 'page' ) ) )
return ;
// registers the method that registers our javascript file that implements our button
add_filter( 'mce_external_plugins', 'EEXCESS_add_tinymce_plugin' );
// registers the method that registers our button
add_filter( 'mce_buttons', 'EEXCESS_add_tinymce_button' );
}
// inlcude the js for tinymce
function EEXCESS_add_tinymce_plugin( $plugin_array ) {
$plugin_array['EEXCESS_get_recommendations'] = plugins_url( 'js/tinyMCE_Get_Recommendations_Button.js', __FILE__ );
$plugin_array['EEXCESS_alter_citations'] = plugins_url( 'js/tinyMCE_Alter_Citations_Button.js', __FILE__ );
return $plugin_array;
}
// Add the button key for address via JS
function EEXCESS_add_tinymce_button( $buttons ) {
array_push( $buttons, 'Get_Recommendations_Button');
array_push( $buttons, 'Alter_Citations_Button');
return $buttons;
}
?>