-
Notifications
You must be signed in to change notification settings - Fork 0
/
navis-related-content.php
348 lines (301 loc) · 11.9 KB
/
navis-related-content.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
<?php
/***
* Plugin Name: Navis Related Content
* Description: Package related stories into a pleasant chunk of content
* Version: 0.1
* Author: Chris Amico
* License: GPLv2
***/
/*
Copyright 2011 National Public Radio, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Navis_Related_Content {
function __construct() {
// add ajax hook to get posts
// add admin js
// add tinymce plugin
// save metadata to custom post type
add_action('init', array(&$this, 'create_post_type'));
add_action('init', array(&$this, 'register_tinymce_filters'));
// ajax actions
add_action('wp_ajax_related_content_form',
array(&$this, 'ajax_render_form'));
add_action('wp_ajax_get_create_related_module',
array(&$this, 'ajax_get_create_module'));
add_action('wp_ajax_fetch_related_content',
array(&$this, 'ajax_fetch'));
add_action('wp_ajax_save_related_content',
array(&$this, 'ajax_save'));
add_action(
'admin_print_styles-post.php',
array( &$this, 'add_admin_stylesheet' )
);
add_action(
'admin_print_styles-post-new.php',
array( &$this, 'add_admin_stylesheet' )
);
add_action( 'admin_print_scripts-post.php',
array( &$this, 'register_admin_scripts' )
);
add_action( 'admin_print_scripts-post-new.php',
array( &$this, 'register_admin_scripts' )
);
add_action('wp_print_styles', array(&$this, 'add_stylesheet'));
add_shortcode('related_content', array(&$this, 'shortcode'));
}
function create_post_type() {
register_post_type('related_content_module', array(
'public' => false,
'supports' => array('title')
));
}
function register_tinymce_filters() {
add_filter('mce_external_plugins',
array(&$this, 'add_tinymce_plugin')
);
add_filter('mce_buttons',
array(&$this, 'register_button')
);
}
function add_tinymce_plugin($plugin_array) {
$plugin_array['related_content'] = plugins_url(
'js/tinymce/related-content-tinymce.js', __FILE__);
return $plugin_array;
}
function register_button($buttons) {
array_push($buttons, '|', "related_content");
return $buttons;
}
function query( $args = array() ) {
// borrowed rather shamelessly from wordpress itself
// wp-admin/includes/internal-linking.php
$pts = get_post_types( array( 'public' => true ), 'objects' );
$pt_names = array_keys( $pts );
$pt_names = apply_filters('navis_related_content_post_types', $pt_names);
$query = array(
'post_type' => $pt_names,
'suppress_filters' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'post_date',
'posts_per_page' => 50,
);
$query = wp_parse_args($args, $query);
$args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
if ( isset( $args['s'] ) )
$query['s'] = $args['s'];
$query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
// Do main query.
$get_posts = new WP_Query;
$posts = $get_posts->query( $query );
// Check if any posts were found.
if ( ! $get_posts->post_count )
return false;
// Build results.
$results = array();
foreach ( $posts as $post ) {
if ( 'post' == $post->post_type )
$info = mysql2date( __( 'Y/m/d' ), $post->post_date );
else
$info = $pts[ $post->post_type ]->labels->singular_name;
$data = array(
'id' => $post->ID,
'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
'permalink' => get_permalink( $post->ID ),
'date' => mysql2date( __( 'Y/m/d' ), $post->post_date ),
'type' => $post->post_type
);
if (has_post_thumbnail($post->ID)) {
$thumbnail = wp_get_attachment_image_src(
get_post_thumbnail_id($post->ID), '60x60');
if (is_array($thumbnail)) {
$data['thumbnail'] = $thumbnail[0];
}
}
$results[] = $data;
}
return $results;
}
function ajax_render_form() {
?>
<form id="navis-related-content-form" tabindex="-1">
<?php wp_nonce_field( 'navis-related-content-form', '_navis_related_content_nonce', true ); ?>
<div id="related-link-selector">
<div id="related-link-options">
<p class="howto"><?php _e( 'Enter the destination URL' ); ?></p>
<div>
<label><span><?php _e( 'URL' ); ?></span><input id="url-field" type="text" tabindex="10" name="href" /></label>
</div>
<div>
<label><span><?php _e( 'Title' ); ?></span><input id="link-title-field" type="text" tabindex="20" name="linktitle" /></label>
</div>
<div>
<input type="button" value="Add" class="button add"/>
</div>
</div>
<p class="howto">Link to recent content</p>
<div id="related-search-panel">
<div class="link-search-wrapper">
<label>
<span><?php _e( 'Search' ); ?></span>
<input type="text" id="related-search-field" class="link-search-field" tabindex="60" autocomplete="off" />
</label>
</div>
<div id="tabs">
<ul>
<li><a href="#related-search-results">All Content</a></li>
<li><a href="#related-search-topic-results">Topics</a></li>
</ul>
<div id="related-search-results" class="query-results">
<div id="post-search" class="river-waiting">
<img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
</div>
</div>
<div id="related-search-topic-results" class="query-results">
<div id="post-search" class="river-waiting">
<img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
</div>
</div>
</div>
</div>
</div>
<div id="chosen">
<div id="links-wrap">
<h4>Links</h4>
<p class="howto">Click links to remove. Drag to reorder.</p>
<div id="chosen-links"></div>
</div>
<div id="topics-wrap">
<h4>Topics</h4>
<p class="howto">Click links to remove. Drag to reorder.</p>
<div id="chosen-topics"></div>
</div>
</div>
</form>
<?php
die();
}
function ajax_fetch() {
$posts = $this->query($_POST);
header( "Content-Type: application/json" );
echo json_encode($posts);
die();
}
function ajax_save() {
if ($_POST['post_parent']) {
$post_id = $_POST['post_parent'];
update_post_meta($post_id, 'related_links', $_POST['links']);
update_post_meta($post_id, 'related_topics', $_POST['topics']);
//error_log(implode(', ', $_POST['links']));
//error_log($_POST['topics']);
} else {
error_log("No post_parent");
}
die();
}
function ajax_get_create_module() {
if ($_POST['post_parent']) {
// for now, we need to know what post this is attached to
$post_id = $_POST['post_parent'];
$links = get_post_meta($post_id, 'related_links', true);
$topics = get_post_meta($post_id, 'related_topics', true);
$data = array(
'links' => $links ? $links : array(),
'topics' => $topics ? $topics : array()
);
header( "Content-Type: application/json" );
echo json_encode($data);
}
die();
}
function shortcode($atts, $content = null) {
global $post;
$links = get_post_meta($post->ID, 'related_links', true);
$topics = (array)get_post_meta($post->ID, 'related_topics', true);
extract(shortcode_atts(array(
'align' => 'right'
), $atts));
$classnames = array(
'left' => 'alignleft',
'right' => 'alignright'
);
if (array_key_exists($align, $classnames)) {
$classname = $classnames[$align];
} else {
$classname = "alignright";
}
$html = "<div class=\"related-content $classname\">";
$html .= '<h4 class="related-header">Related</h4>';
// links
if ($links):
$html .= '<div class="links">';
$html .= '<h5>Posts</h5>';
$html .= '<ul>';
foreach((array)$links as $link) {
$html .= '<li class="link">';
$html .= "<a href=\"{$link['permalink']}\">{$link['title']}</a>";
$html .= '</li>';
}
$html .= '</ul></div>';
endif;
// topics
if ($topics):
$html .= '<div class="topics">';
$html .= '<h5>Topics</h5>';
foreach((array)$topics as $topic) {
$html .= '<p class="topic">';
if ($topic['thumbnail']) {
$html .= "<img class=\"alignleft\" src=\"{$topic['thumbnail']}\" height=\"60\" width=\"60\" />";
}
$html .= "<a href=\"{$topic['permalink']}\">{$topic['title']}</a>";
$html .= "</p>";
}
$html .= '</div>'; // close .topics
endif;
$html .= '</div>'; // and we're done
return $html;
}
function add_stylesheet() {
$css = plugins_url( 'css/related-content-frontend.css', __FILE__ );
wp_enqueue_style('navis-related-content-frontend', $css, array(), '0.1');
}
function add_admin_stylesheet() {
$css = plugins_url( 'css/related-content.css', __FILE__ );
wp_enqueue_style('wp-jquery-ui-dialog');
wp_enqueue_style(
'navis-related-content', $css, array(), '0.1'
);
}
function register_admin_scripts() {
$jslibs = array(
'underscore' => plugins_url('js/underscore.js', __FILE__),
'backbone' => plugins_url('js/backbone.js', __FILE__),
'related-content' => plugins_url('js/related-content.js', __FILE__),
);
/*** FOR DEBUGGING: Uncomment to use packaged underscore, backbone
wp_deregister_script('underscore');
wp_deregister_script('backbone');
wp_enqueue_script( 'underscore', $jslibs['underscore']);
wp_enqueue_script( 'backbone', $jslibs['backbone'],
array('underscore', 'jquery'));
***/
wp_enqueue_script('jquery-ui-tabs');
wp_enqueue_script( 'related-content', $jslibs['related-content'],
array('jquery', 'underscore', 'backbone'),
"0.3");
}
}
$navis_related_content = new Navis_Related_Content;
?>