-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-reading-list-functions.php
293 lines (269 loc) · 9.3 KB
/
wp-reading-list-functions.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
<?php
/*FILE: wp-reading-list-functions.php
*DESCRIPTION: Core plugin functions
*/
defined( 'ABSPATH' ) OR exit;
global $version;
$version = "4.0.1";
/*Load the custom taxonomies for 'works' and 'authors' */
if (!current_theme_supports('post-thumbnails')) {
add_theme_support('post-thumbnails');
}
require 'wprl-core/wp-reading-list-taxonomies.php';
add_action('init', 'wprl_custom_tax', 0);
/*Set wprl default option settings */
function wprl_default_options() {
global $version;
$options = array(
'title' => 'Notes',
'multiple_title' => 'Reading List',
'cover_image' => plugins_url('wp-reading-list/wprl-theme/default.png'),
'order' => 'date',
'direction' => 'DESC',
'grid_width' => '3',
'grid_rows' => '4',
'works_in_feed' => false,
'show_post_date' => false,
'delete' => false,
'show_page_nums' => false,
'show_author' => true,
'post_author' => false,
'show_url' => true,
'show_single_work' => true,
'list_image' => true,
'show_author_link' => true,
'version' => $version,
'show_list_excerpt' => true,
'show_work_type' => true,
'show_type_link' => true,
'override_theme_taxonomies' => true,
);
return $options;
}
/*Initialize wprl options */
global $wprl_options;
$wprl_options = get_option('wprl_plugin_options');
if (!$wprl_options) {
$wprl_options = wprl_default_options();
}
elseif ($wprl_options['version'] != '2.2') {
$wprl_options_default = wprl_default_options();
$wprl_options = array_merge($wprl_options_default,$wprl_options);
}
update_option('wprl_plugin_options', $wprl_options);
/*Check to see if user is in admin/backend */
if (is_admin()){
require 'wprl-admin/wp-reading-list-admin.php';
}
/* queue different files depending on the layout chosen and the location */
function wprl_layout_query($query){
$wprl_options = get_option('wprl_plugin_options');
if (!is_admin() && $query->is_main_query() && $wprl_options['works_in_feed'] && !is_post_type_archive('works')) {
$query->set('post_type', array('post', 'works'));
}
else if (is_post_type_archive('works') && !is_admin() && $query->is_main_query()) {
$query->set('post_type', 'works');
if ($wprl_options['order'] == 'rand') {
$query->set('orderby', 'rand');
}
else {
$orderby = $wprl_options['order'];
if ($orderby == 'author') {
$orderby .= ' title';
}
$query->set('orderby', $orderby);
$query->set('order', $wprl_options['direction']);
}
$numposts = $wprl_options['grid_width'] * $wprl_options['grid_rows'];
$query->set('posts_per_page', $numposts);
return $query;
}
}
add_action('pre_get_posts', 'wprl_layout_query');
/*Enqueue plugin stylesheet and allow for child theme to override */
if (!function_exists('wprl_styles')) {
function wprl_styles() {
global $version;
if (!is_admin()) {
wp_enqueue_style( 'reading-list-default-style', plugins_url('/wprl-theme/default.css?'.$version, __FILE__));
}
}
}
add_action('wp_enqueue_scripts', 'wprl_styles' );
/*Remove taxonomy function */
function remove_taxonomy($taxonomy) {
global $wp_taxonomies;
$terms = get_terms($taxonomy);
foreach ($terms as $term) {
wp_delete_term($term->term_id, $taxonomy );
}
unset($wp_taxonomies[$taxonomy]);
}
/*Delete works and authors function, called after 'save settings' or when deleting the plugin */
function delete_works(){
global $wpdb;
$query = "
DELETE FROM wp_posts
WHERE post_type = 'works'
";
$wpdb->query($query);
remove_taxonomy('work-author');
$GLOBALS['wp_rewrite']->flush_rules();
}
/* Force users to include a title on reading list items */
function force_post_title() {
if (is_admin() && 'works' == get_post_type()) {
echo ("<script type='text/javascript'>
jQuery('#publish').click(function(){
var testervar = jQuery('[id^=\"titlediv\"]')
.find('#title');
if (testervar.val().length < 1) {
setTimeout(\"jQuery('.spinner').css('visibility', 'hidden');\", 100);
alert('Please enter a work title.');
setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
return false;
}
});
</script>\n");
}
}
/*get featured image for works listing in admin columns */
function wprl_get_cover_image($post_ID) {
$post_thumbnail_id = get_post_thumbnail_id($post_ID);
if ($post_thumbnail_id) {
$post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'featured_preview');
return $post_thumbnail_img[0];
}
}
/*add new column to admin listing for works */
function wprl_columns_head($defaults) {
$defaults['author'] = 'Post Author';
$defaults['featured_image'] = 'Featured Image';
return $defaults;
}
/*show the cover image for the admin works listing */
function wprl_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$cover_image= wprl_get_cover_image($post_ID);
if ($cover_image) {
echo ('<img src="'.$cover_image.'"/>');
}
}
}
add_filter('manage_posts_columns', 'wprl_columns_head');
add_action('manage_posts_custom_column', 'wprl_columns_content', 10, 2);
/* add an "author" column to the "all works" list */
function post_author_column( $columns ) {
$columns['author'] = 'author';
return $columns;
}
add_filter('manage_edit-works_sortable_columns', 'post_author_column');
/*Template fallback*/
function wp_reading_list_redirect() {
global $wp_query;
$wprl_options = get_option('wprl_plugin_options');
if (isset($wp_query->query_vars["post_type"]) && $wprl_options['override_theme_taxonomies']) {
if($wp_query->query_vars["post_type"] == 'works') {
if (is_singular('works')) {
$return_template = dirname( __FILE__ ) . '/wprl-theme/single-works.php';
wprl_theme_redirect($return_template);
}
else {
$return_template = dirname( __FILE__ ) . '/wprl-theme/archive-works.php';
wprl_theme_redirect($return_template);
}
}
elseif (isset($wp_query->query_vars["taxonomy"]) && $wp_query->query_vars["taxonomy"] == 'work-author') {
$return_template = dirname( __FILE__ ) . '/wprl-theme/taxonomy-work-author.php';
wprl_theme_redirect($return_template);
}
elseif (isset($wp_query->query_vars["taxonomy"]) && $wp_query->query_vars["taxonomy"] == 'work-type') {
$return_template = dirname( __FILE__ ) . '/wprl-theme/taxonomy-work-type.php';
wprl_theme_redirect($return_template);
}
}
}
function wprl_theme_redirect($url) {
global $post, $wp_query;
if (have_posts()) {
include($url);
die();
} else {
$wp_query->is_404 = true;
}
}
add_action("template_redirect", 'wp_reading_list_redirect');
function wprl_custom_excerpt($limit) {
$excerpt = explode(' ', get_the_content(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
function register_wprl_localization() {
// Localization
load_plugin_textdomain( 'wp_reading_list', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action('init', 'register_wprl_localization');
function wprl_rewrite_flush(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_filter('widget_text', 'do_shortcode');
function wprl_get_works($numPosts){
$wprl_options = get_option('wprl_plugin_options');
$orderby = $wprl_options['order'];
if ($orderby == 'author') {
$orderby .= ' title';
}
$args = array(
'post_type' => 'works',
'order' => $wprl_options['direction'],
'orderby' => $orderby,
'posts_per_page' => $numPosts,
'post_status' => "publish"
);
return new WP_Query($args);
}
function wprl_layout_shortcode($atts) {
$args = shortcode_atts(
array(
'layout' => 'plain',
'number' => '5',
),
$atts);
if (isset($args["layout"]) && isset($args["number"])){
$query = wprl_get_works($args["number"]);
if ($query->have_posts()){
if ($args["layout"] === "plain"){
$output = "<ul>";
while ($query->have_posts() ) {
$query->the_post();
$output .= "<li><a href='".get_permalink()."'>".get_the_title()."</a></li>";
}
return $output."</ul>";
}
}
}
}
add_shortcode('wprl', 'wprl_layout_shortcode');
/**
* End of File
*/