-
Notifications
You must be signed in to change notification settings - Fork 0
/
scholarpress-researcher.php
executable file
·197 lines (164 loc) · 6.62 KB
/
scholarpress-researcher.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
<?php
/*
Plugin Name: ScholarPress Researcher
Plugin URI: http://scholarpress.net/researcher/
Description: Integrate your Zotero account to create a research blog with WordPress
Version: 1.1
Author: Jeremy Boggs, Sean Takats
Author URI: http://scholarpress.net/
*/
/*
Copyright (C) 2009-2012, Jeremy Boggs and Sean Takats. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
class ScholarPress_Researcher {
/**
* ScholarPress Researcher constructor
*
* @since 1.0
* @uses add_action()
* @uses register_activation_hook()
* @uses register_deactivation_hook()
*/
function scholarpress_researcher() {
add_action( 'init', array ( $this, 'init' ) );
add_action( 'admin_init', array ( $this, 'admin_init' ) );
add_action( 'plugins_loaded', array( $this, 'loaded' ) );
// When Researcher is loaded, get includes.
add_action( 'scholarpress_researcher_loaded', array( $this, 'includes' ) );
// When Researcher is initialized, add localization files.
add_action( 'scholarpress_researcher_init', array( $this, 'textdomain' ) );
// Add shortcode
add_shortcode('zotero', array($this,'shortcode'));
}
/**
* Adds a plugin initialization action.
*/
function init() {
do_action( 'scholarpress_researcher_init' );
}
/**
* Adds a plugin admin initialization action.
*/
function admin_init() {
do_action( 'scholarpress_researcher_admin_init');
}
/**
* Adds a plugin loaded action.
*/
function loaded() {
do_action( 'scholarpress_researcher_loaded' );
}
/**
* Includes other necessary plugin files.
*/
function includes() {
require(dirname( __FILE__ ).'/libZotero/build/libZoteroSingle.php');
}
/**
* Handles localization files. Added on scholarpress_researcher_init.
* Plugin core localization files are in the 'languages' directory. Users
* can also add custom localization files in
* 'wp-content/scholarpress-courseware-files/languages' if desired.
*
* @uses load_textdomain()
* @uses get_locale()
*/
function textdomain() {
$locale = get_locale();
$mofile_custom = WP_CONTENT_DIR . "/scholarpress-researcher-files/languages/spresearcher-$locale.mo";
$mofile_packaged = WP_PLUGIN_DIR . "/scholarpress-researcher/languages/spresearcher-$locale.mo";
if ( file_exists( $mofile_custom ) ) {
load_textdomain( 'spresearcher', $mofile_custom );
return;
} else if ( file_exists( $mofile_packaged ) ) {
load_textdomain( 'spresearcher', $mofile_packaged );
return;
}
}
function shortcode($attributes) {
// extract the shortcode attributes
extract(shortcode_atts(array(
'library_type' => false,
'library_id' => false,
'library_slug' => "",
'api_key' => false,
'item_key' => false,
'collection_key' => false,
'content' => 'bib,coins',
'style' => false,
'order' => 'creator',
'sort' => 'asc',
'limit' => "100", //restrict the total items we'll fetch to 100 unless overridden
'format' => false,
'tag_name' => false,
'cache_time' => "3600" // cache defaults to one hour, uses APC
), $attributes));
$params = array();
if ($collection_key)
$params['collectionKey'] = $collection_key;
if ($content)
$params['content'] = $content;
if ($style)
$params['style'] = $style;
if ($order)
$params['order'] = $order;
if ($sort)
$params['sort'] = $sort;
if ($limit)
$totalItemLimit = $limit;
if ($format)
$params['format'] = $format;
if ($item_key)
$params['itemKey'] = $item_key;
$base_url = "http://www.zotero.org";
$library = new Zotero_Library($library_type, $library_id, $library_slug, $api_key, $base_url, $cache_time);
// code to step through multiple requests for bibliographies longer than 100 items
//start at the beginning of our list by setting an offset of 0
$offset = 0;
//limit to 100 items per http request
//this is the maximum number of items the API will return in a single request
$perRequestLimit = 100;
//keep count of the items we've gotten
$fetchedItemsCount = 0;
//keep track of whether there are more items to fetch
$moreItems = true;
//where we'll keep the list of items we retrieve
$items = array();
//while there are more items and we haven't gotten to our limit yet
while(($fetchedItemsCount < $totalItemLimit) && $moreItems){
//fetching items starting at $offset with $perRequestLimit items per request
$fetchedItems = $library->fetchItemsTop(array_merge($params, array('limit'=>$perRequestLimit, 'start'=>$offset)));
//put the items from this last request into our array of items
$items = array_merge($items, $fetchedItems);
//update the count of items we've got and offset by that amount
$fetchedItemsCount += count($fetchedItems);
$offset = $fetchedItemsCount;
//Zotero_Library keeps track of the last feed it got so we can check if there is a 'next' link
//indicating more results to be fetched
if(!isset($library->getLastFeed()->links['next'])){
$moreItems = false;
}
}
return $this->display_zotero_items($items);
}
function display_zotero_items($items) {
$html = '';
foreach($items as $item) {
$html .= Zotero_Lib_Utils::wrapLinks($item->bibContent);
$html .= htmlspecialchars_decode($item->subContents['coins']);
}
return wpautop($html);
}
}
$scholarpressResearcher = new ScholarPress_Researcher();