View difference between Paste ID: f51d2db54 and
SHOW: | | - or go back to the newest paste.
1-
1+
<?php
2
$content_width = '691';
3
4
add_filter( 'the_content', 'make_clickable' );
5
6
add_action( 'wp_ajax_prologue_ajax_tag_search', 'prologue_ajax_tag_search' ); //Tag suggestion
7
add_action( 'wp_ajax_prologue_load_post', 'prologue_load_post' ); // Load posts for inline editing
8
add_action( 'wp_ajax_prologue_load_comment', 'prologue_load_comment' ); // Load comments for inline editing
9
add_action( 'wp_ajax_prologue_inline_save', 'prologue_inline_save' ); // Save post  after inline editing
10
add_action( 'wp_ajax_prologue_inline_comment_save', 'prologue_inline_comment_save' ); // Save comment   after inline editing
11
add_action( 'wp_ajax_prologue_latest_posts', 'prologue_latest_posts' ); // Load new posts 
12
add_action( 'wp_ajax_prologue_latest_comments', 'prologue_latest_comments' ); //check for new comments and loads comments into widget
13
add_action( 'wp_ajax_prologue_new_post', 'prologue_new_post' ); //Ajax posting
14
add_action( 'wp_ajax_prologue_new_comment', 'prologue_new_comment' ); //Ajax Commenting
15
add_action( 'wp_head', 'prologue_widget_recent_comments_avatar_style'); //Load styles for recent comments avatar widget
16
17
if( !class_exists('Services_JSON') ) include (TEMPLATEPATH . '/inc/JSON.php');
18
19
if (!is_admin()) add_action( 'wp_print_scripts', 'prologue_javascript' );
20
21
function prologue_init() {
22
    load_theme_textdomain( 'p2', get_template_directory() . '/languages' );
23
}
24
add_action( 'init', 'prologue_init' );
25
26
function prologue_javascript() {
27
    $prologue_tagsuggest = true;
28
    $prologue_inlineedit = true;
29
    
30
    wp_enqueue_script( 'jquery-color' );
31
	wp_enqueue_script( 'comment-reply' );
32
33
	if ( is_front_page() && $prologue_tagsuggest && is_user_logged_in() )
34
		wp_enqueue_script( 'suggest' );
35
		
36
	wp_enqueue_script( 'p2js', get_bloginfo('template_directory' ).'/inc/p2.js', array( 'jquery' ), '200903301' );
37
	wp_localize_script( 'p2js', 'p2txt', array(
38
	    'tagit' => __('Tag it', 'p2'),
39
	    'goto_homepage' => __('Go to homepage', 'p2'),
40
	    // the number is calculated in the javascript in a complex way, so we can't use ngettext
41
	    'n_new_updates' => __('%d new update(s)', 'p2'),
42
	    'n_new_comments' => __('%d new comment(s)', 'p2'),
43
	    'jump_to_top' => __('Jump to top', 'p2'),
44
	    'not_posted_error' => __('An error has occured, your post was not posted', 'p2'),
45
	    'update_posted' => __('You update has been posted', 'p2'),
46
	    'loading' => __('Loading...', 'p2'),
47
	    'cancel' => __('Cancel', 'p2'),
48
	    'save' => __('Save', 'p2'),
49
	    'hide_threads' => __('Hide threads', 'p2'),
50
	    'show_threads' => __('Show threads', 'p2'),
51
		'unsaved_changes' => __('Your comments or posts will be lost if you continue.', 'p2'),
52
	));
53
	
54
	if ( ( $prologue_inlineedit ) && is_user_logged_in() )
55
		wp_enqueue_script( 'jeditable', get_bloginfo('template_directory').'/inc/jquery.jeditable.js', array( 'jquery' )  );
56
57
	wp_enqueue_script( 'scrollit', get_bloginfo('template_directory').'/inc/jquery.scrollTo-min.js', array( 'jquery' )  );
58
}
59
60
function prologue_pageoptions_init() {
61
	global $page_options;
62
	get_currentuserinfo();
63
	$page_options['nonce']= wp_create_nonce( 'ajaxnonce' );
64
	$page_options['prologue_updates'] = 1;
65
	$page_options['prologue_comments_updates'] = 1;
66
	$page_options['prologue_tagsuggest'] = 1;
67
	$page_options['prologue_inlineedit'] = 1;
68
	$page_options['prologue_comments_inlineedit'] = 1;
69
	$page_options['is_single'] = (int)is_single();
70
	$page_options['is_page'] = (int)is_page();
71
	$page_options['is_front_page'] = (int)is_front_page();
72
	$page_options['is_first_front_page'] = (int)(is_front_page() && !is_paged() );
73
	$page_options['is_user_logged_in'] = (int)is_user_logged_in();
74
}
75
add_action('wp_head', 'prologue_pageoptions_init');
76
77
function prologue_pageoptions_js() {
78
	global $page_options;
79
?><script type='text/javascript'>
80
// <![CDATA[
81
//Prologue Configuration
82
var ajaxUrl = "<?php echo js_escape( get_bloginfo( 'wpurl' ) . '/wp-admin/admin-ajax.php' ); ?>";
83
var updateRate = "30000";
84
var nonce = "<?php echo js_escape( $page_options['nonce'] ); ?>";
85
var templateDir  = "<?php js_escape( bloginfo('template_directory') ); ?>";
86
var isFirstFrontPage = <?php echo $page_options['is_first_front_page'] ?>;
87
var isFrontPage = <?php echo $page_options['is_front_page'] ?>;
88
var isSingle = <?php echo $page_options['is_single'] ?>;
89
var isPage = <?php echo $page_options['is_page'] ?>;
90
var isUserLoggedIn = <?php echo $page_options['is_user_logged_in'] ?>;
91
var prologueTagsuggest = <?php echo $page_options['prologue_tagsuggest'] ?>;
92
var prologuePostsUpdates = <?php echo $page_options['prologue_updates'] ?>;
93
var prologueCommentsUpdates = <?php echo $page_options['prologue_comments_updates']; ?>;
94
var getPostsUpdate = 0;
95
var getCommentsUpdate = 0;
96
var inlineEditPosts =  <?php echo $page_options['prologue_inlineedit'] ?>;
97
var inlineEditComments =  <?php echo $page_options['prologue_comments_inlineedit'] ?>;
98
var wpUrl = "<?php echo js_escape( get_bloginfo( 'wpurl' ) ); ?>";
99
var rssUrl = "<?php js_escape( get_bloginfo( 'rss_url' ) ); ?>";
100
var pageLoadTime = "<?php echo gmdate( 'Y-m-d H:i:s' ); ?>";
101
var latestPermalink = "<?php echo js_escape( latest_post_permalink() ); ?>";
102
var original_title = document.title;
103
var commentsOnPost = new Array;
104
var postsOnPage = new Array;
105
var postsOnPageQS = '';
106
var currPost = -1;
107
var currComment = -1;
108
var commentLoop = false;
109
var lcwidget = false;
110
var hidecomments = false;
111
var commentsLists = '';
112
var newUnseenUpdates = 0;
113
 // ]]>
114
</script> <?php
115
}
116
add_action('wp_head', 'prologue_pageoptions_js');
117
118
119
add_action('admin_menu', 'prologue_plugin_menu');
120
121
function prologue_plugin_menu() {
122
  add_theme_page('Prologue Options', 'Prologue Options', 8, __FILE__, 'prologue_options_page');
123
}
124
125
126
function prologue_options_page() {
127
128
    // variables for the field and option names 
129
    $opt_name = 'prologue_show_titles';
130
    $hidden_field_name = 'prologue_submit_hidden';
131
    $data_field_name = 'prologue_titles';
132
133
    // Read in existing option value from database
134
    $opt_val = get_option( $opt_name );
135
136
    // See if the user has posted us some information
137
    // If they did, this hidden field will be set to 'Y'
138
    if( $_POST[ $hidden_field_name ] == 'Y' ) {
139
        // Read their posted value
140
		$opt_val = 0;
141
		if($_POST[ $data_field_name ] == 1) {
142
			$opt_val = 1;
143
		}
144
145
        // Save the posted value in the database
146
        update_option( $opt_name, $opt_val );
147
148
        // Put an options updated message on the screen
149
?>
150
<div class="updated"><p><strong><?php _e('Options saved.', 'p2' ); ?></strong></p></div>
151
<?php
152
153
    }
154
155
    // Now display the options editing screen
156
157
    echo '<div class="wrap">';
158
159
    // header
160
161
    echo "<h2>" . __( 'Prologue Options', 'p2' ) . "</h2>";
162
163
    // options form
164
    
165
    ?>
166
167
<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
168
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y" />
169
170
<p>
171
	<input id="prologue-display-titles" type="checkbox" name="<?php echo $data_field_name; ?>" <?php if($opt_val == 1) echo 'checked="checked"'; ?> value="1" />
172
	<label for="prologue-display-titles"><?php _e("Display titles", 'p2' ); ?></label> 
173
</p>
174
175
<p class="submit">
176
<input type="submit" name="Submit" value="<?php _e('Update Options', 'p2' ) ?>" />
177
</p>
178
179
</form>
180
</div>
181
182
<?php
183
 
184
}
185
186
187
function prologue_recent_projects_widget( $args ) {
188
	extract( $args );
189
	$options = get_option( 'prologue_recent_projects' );
190
191
	$title = empty( $options['title'] ) ? __( 'Recent Tags' , 'p2') : $options['title'];
192
	$num_to_show = empty( $options['num_to_show'] ) ? 35 : $options['num_to_show'];
193
194
	$num_to_show = (int) $num_to_show;
195
196
	$before = $before_widget;
197
	$before .= $before_title . wp_specialchars( $title ) . $after_title;
198
199
	$after = $after_widget;
200
201
	echo prologue_recent_projects( $num_to_show, $before, $after );
202
}
203
204
function prologue_recent_projects( $num_to_show = 35, $before = '', $after = '' ) {
205
	$cache = wp_cache_get( 'prologue_theme_tag_list', '' );
206
	if( !empty( $cache[$num_to_show] ) ) {
207
		$recent_tags = $cache[$num_to_show];
208
	} else {
209
		$all_tags = (array) get_tags( array( 'get' => 'all' ) );
210
211
		$recent_tags = array();
212
		
213
		foreach( $all_tags as $tag ) {
214
			if( $tag->count < 1 )
215
				continue;
216
217
			$tag_posts = get_objects_in_term( $tag->term_id, 'post_tag' );
218
			$recent_post_id = max( $tag_posts );
219
			$recent_tags[$tag->term_id] = $recent_post_id;
220
		}
221
222
		arsort( $recent_tags );
223
224
		$num_tags = count( $recent_tags );
225
		if( $num_tags > $num_to_show ) {
226
			$reduce_by = (int) $num_tags - $num_to_show;
227
228
			for( $i = 0; $i < $reduce_by; $i++ ) {
229
				array_pop( $recent_tags );
230
			}
231
		}
232
233
		wp_cache_set( 'prologue_theme_tag_list', array( $num_to_show => $recent_tags ) );
234
	}
235
236
	echo $before;
237
	echo "<ul>\n";
238
239
	foreach( $recent_tags as $term_id => $post_id ) {
240
		$tag = get_term( $term_id, 'post_tag' );
241
		$tag_link = get_tag_link( $tag->term_id );
242
?>
243
244
<li>
245
<a class="rss" href="<?php echo get_tag_feed_link( $tag->term_id ); ?>">RSS</a>&nbsp;<a href="<?php echo $tag_link; ?>"><?php echo wp_specialchars( $tag->name ); ?></a>&nbsp;(&nbsp;<?php echo number_format_i18n( $tag->count ); ?>&nbsp;)
246
</li>
247
248
<?php
249
    } // foreach $recent_tags
250
?>
251
252
	</ul>
253
254
<p><a class="allrss" href="<?php bloginfo( 'rss2_url' ); ?>"><?php _e('All Updates RSS', 'p2'); ?></a></p>
255
256
<?php
257
	echo $after;
258
}
259
260
function prologue_flush_tag_cache() {
261
	wp_cache_delete( 'prologue_theme_tag_list' );
262
}
263
add_action( 'save_post', 'prologue_flush_tag_cache' );
264
265
function prologue_recent_projects_control() {
266
	$options = $newoptions = get_option( 'prologue_recent_projects' );
267
268
	if( $_POST['prologue_submit'] ) {
269
		$newoptions['title'] = strip_tags( stripslashes( $_POST['prologue_title'] ) );
270
		$newoptions['num_to_show'] = (int) strip_tags( stripslashes( $_POST['prologue_num_to_show'] ) );
271
	}
272
273
	if( $options != $newoptions ) {
274
		$options = $newoptions;
275
		update_option( 'prologue_recent_projects', $options );
276
	}
277
278
	$title = $options['title'];
279
	$num_to_show = (int) $options['num_to_show'];
280
?>
281
282
<input type="hidden" name="prologue_submit" id="prologue_submit" value="1" />
283
284
<p><label for="prologue_title"><?php _e('Title:', 'p2') ?> 
285
<input type="text" class="widefat" id="prologue_title" name="prologue_title" value="<?php echo attribute_escape($title); ?>" />
286
</label></p>
287
288
<p><label for="prologue_num_to_show"><?php _e('Num of tags to show:', 'p2') ?> 
289
<input type="text" class="widefat" id="prologue_num_to_show" name="prologue_num_to_show" value="<?php echo $num_to_show ?>" />
290
</label></p>
291
292
<?php
293
}
294
wp_register_sidebar_widget( 'prologue_recent_projects_widget', __( 'Recent Tags' , 'p2'), 'prologue_recent_projects_widget' );
295
wp_register_widget_control( 'prologue_recent_projects_widget', __( 'Recent Tags' , 'p2'), 'prologue_recent_projects_control' );
296
297
if( function_exists('register_sidebar') )
298
	register_sidebar();
299
300
function prologue_get_avatar( $user_id, $email, $size ) {
301
	if ( $user_id )
302
		return get_avatar( $user_id, $size );
303
	else
304
		return get_avatar( $email, $size );
305
}
306
307
function prologue_comment( $comment, $args, $depth ) {
308
	$GLOBALS['comment'] = $comment;
309
?>
310
<li <?php comment_class(); ?> id="comment-<?php comment_ID( ); ?>">
311
	<?php echo prologue_get_avatar( $comment->user_id, $comment->comment_author_email, 32 ); ?>
312
	<h4>
313
		<?php comment_author_link( ); ?>
314
		<span class="meta"><?php comment_time(); ?> <?php _e('on', 'p2'); ?> <?php comment_date(); ?> <span class="actions"><a href="#comment-<?php comment_ID( ); ?>"><?php _e('Permalink', 'p2'); ?></a><?php echo comment_reply_link(array('depth' => $depth, 'max_depth' => $args['max_depth'], 'before' => ' | ')) ?><?php edit_comment_link( __( 'Edit' , 'p2'), ' | ',''); ?></span><br /></span>
315
	</h4>
316
	<div class="commentcontent<?php if (current_user_can('edit_post', $comment->comment_post_ID)) echo(' comment-edit') ?>"  id="commentcontent-<?php comment_ID( ); ?>">
317
			<?php comment_text( ); ?>
318
	<?php if ( $comment->comment_approved == '0' ) : ?>
319
	<p><em><?php _e('Your comment is awaiting moderation.', 'p2') ?></em></p>
320
	<?php endif; ?>
321
	</div>
322
<?php	
323
}
324
325
function whatchat_up_to() {
326
	return (false === get_option('prologue_greeting')) ? __('Whatcha up to?') : get_option('prologue_greeting');
327
}
328
329
function prologue_the_title($before = '<h2>', $after = '</h2>') {
330
	
331
	global $post, $looping;
332
	$t = $post->post_title;
333
	
334
	if($looping == 0)
335
		return $t;
336
	$pos = 0;
337
	
338
	if(get_option('prologue_show_titles') != 1) 
339
		return false;
340
		
341
	$post->post_content = trim($post->post_content);
342
	$post->post_title = trim($post->post_title);
343
	$post->post_title = preg_replace('/\.\.\.$/','',$post->post_title);
344
	$post->post_title = str_replace("\n", ' ', $post->post_title );
345
	$post->post_title = str_replace('  ',' ', $post->post_title);
346
	$post->post_content = str_replace("\n", ' ', strip_tags($post->post_content) );
347
	$post->post_content = str_replace('  ',' ', $post->post_content);
348
	$post->post_content = trim($post->post_content);
349
	$post->post_title = trim($post->post_title);
350
	
351
	if( is_int( strpos($post->post_title, 'http') ) )  {
352
		$split = str_split( $post->post_content, strpos($post->post_content, 'http'));
353
		$post->post_content = $split[0];
354
		$split2 = str_split( $post->post_title, strpos($post->post_title, 'http'));
355
		$post->post_title = $split2[0];
356
	}
357
	$pos = @strpos( $post->post_content, $post->post_title );
358
	
359
	// these are for debugging
360
	/*
361
	error_log('content '.$post->post_content);
362
	error_log('title '.$post->post_title);
363
	error_log($pos);*/
364
365
	if( (false === $pos or $pos > 0) && $post->post_title != '') 
366
		echo (is_single()) ? $before.$t.$after : $before.'<a href="'.get_permalink( $post->id ).'">'.$t.'</a>'.$after;
367
}
368
369
function prologue_loop() {
370
	global $looping;
371
	$looping = ($looping === 1 ) ? 0 : 1;
372
}
373
add_action('loop_start', 'prologue_loop');
374
add_action('loop_end', 'prologue_loop');
375
376
function prologue_comment_widget_html( $comment, $size, $tdclass, $echocomment = true ) {
377
	if ( !$size = (int) $size )
378
		$size = 32;
379
380
	if ( $comment->comment_author == '' )
381
		$comment->comment_author = __('Anonymous', 'p2');
382
	$author = $comment->comment_author;
383
	$excerpt = wp_html_excerpt( $author, 20 );
384
	if ( $author != $excerpt )
385
		$author = $excerpt.'&hellip;';
386
387
	$avatar = get_avatar( $comment, $size );
388
389
	if ( $comment_author_url = $comment->comment_author_url ? clean_url( $comment->comment_author_url ) : '' ) {
390
		$avatar = "<a href='$comment_author_url' rel='nofollow'>$avatar</a>";
391
		// entitities in comment author are kept escaped in the db and tags are not allowed, so
392
		// no need of HTML escaping here
393
		$author = "<a href='$comment_author_url' rel='nofollow'>$author</a>";
394
	}
395
396
	$thiscomment  = '<tr><td title="' . attribute_escape( $comment->comment_author ) . '" class="recentcommentsavatar' . $tdclass . '" style="height:' . $size . 'px; width:' . $size . 'px">' . $avatar . '</td>';
397
	$thiscomment .= '<td class="recentcommentstext' . $tdclass . '">';
398
399
	$post_title = wp_specialchars( strip_tags( get_the_title( $comment->comment_post_ID ) ) );
400
	$excerpt = wp_html_excerpt( $post_title, 30 );
401
	if ( $post_title != $excerpt )
402
		$post_title = $excerpt.'&hellip;';
403
404
	$comment_content = strip_tags( $comment->comment_content );
405
	$excerpt = wp_html_excerpt( $comment_content, 50 );
406
	if ( $comment_content != $excerpt )
407
		$comment_content = $excerpt.'&hellip;';
408
409
	$comment_url = get_comment_link( $comment );
410
411
	// Only use the URL's #fragment if the comment is visible on the pgae.
412
	// Works by detecting if the comment's post is visible on the page... may break if P2 decides to do clever stuff with comments when paginated
413
	if ( @constant( 'DOING_AJAX' ) && isset( $_GET['vp'] ) && is_array( $_GET['vp'] ) && in_array( $comment->comment_post_ID, $_GET['vp'] ) ) {
414
		$comment_url = "#comment-{$comment->comment_ID}";
415
	} else {
416
		static $posts_on_page = false;
417
		if ( false === $posts_on_page ) {
418
			global $wp_query;
419
420
			$posts_on_page = array();
421
			foreach ( array_keys( $wp_query->posts ) as $k )
422
				$posts_on_page[$wp_query->posts[$k]->ID] = true;
423
		}
424
425
		if ( isset( $posts_on_page[$comment->comment_post_ID] ) )
426
			$comment_url = "#comment-{$comment->comment_ID}";
427
	}
428
429
	$thiscomment .= sprintf( __( "%s on <a href='%s' class='tooltip' title='%s'>%s</a>" , 'p2') . '</td></tr>', $author, $comment_url, attribute_escape($comment_content), $post_title );
430
	
431
	if ($echocomment)
432
		echo $thiscomment;
433
	else
434
		return $thiscomment;
435
}
436
437
function prologue_comment_frontpage( $comment, $args, $echocomment = true ) {
438
	$GLOBALS['comment'] = $comment;
439
	
440
	$depth = prologue_get_comment_depth( get_comment_ID() );
441
	$comment_text =  apply_filters( 'comment_text', $comment->comment_content );
442
	$comment_class = comment_class( $class = '', $comment_id = null, $post_id = null, $echo = false );
443
	$comment_time = get_comment_time();
444
	$comment_date = get_comment_date();
445
	$id = get_comment_ID();
446
	$avatar = prologue_get_avatar( $comment->user_id, $comment->comment_author_email, 32 );
447
	$author_link = get_comment_author_link();
448
	$reply_link = prologue_get_comment_reply_link(
449
        array('depth' => $depth, 'max_depth' => $args['max_depth'], 'before' => ' | ', 'reply_text' => __('Reply', 'p2') ),
450
        $comment->comment_ID, $comment->comment_post_ID );
451
	$can_edit = current_user_can( 'edit_post', $comment->comment_post_ID );
452
	$edit_comment_url = get_edit_comment_link( $comment->comment_ID );
453
	$edit_link = $can_edit? " | <a class='comment-edit-link' href='$edit_comment_url' title='".attribute_escape(__('Edit comment', 'p2'))."'>".__('Edit', 'p2')."</a>" : '';
454
	$content_class = $can_edit? 'commentcontent comment-edit' : 'commentcontent';
455
	$awaiting_message = $comment->comment_approved == '0'? '<p><em>'.__('Your comment is awaiting moderation.', 'p2').'</em></p>' : '';
456
	$permalink = clean_url( get_comment_link() );
457
	$permalink_text = __('Permalink', 'p2');
458
	$date_time = sprintf( __('%s <em>on</em> %s', 'p2'),  get_comment_time(), get_comment_date() );
459
	$html = <<<HTML
460
<li $comment_class id="comment-$id">
461
    $avatar
462
    <h4>
463
        $author_link
464
        <span class="meta">
465
            $date_time
466
            <span class="actions"><a href="$permalink">$permalink_text</a> $reply_link $edit_link</span><br />
467
        </span>
468
    </h4>
469
    <div class="$content_class" id="commentcontent-$id">
470
        $comment_text
471
    </div>
472
HTML;
473
	if(get_comment_type() != 'comment')
474
		return false;
475
		
476
	if ($echocomment)
477
		echo $html;
478
	else
479
		return $html;
480
}
481
482
function tags_with_count( $format = 'list', $before = '', $sep = '', $after = '' ) {
483
	global $post;
484
	$posttags = get_the_tags($post->ID, 'post_tag');
485
	
486
	if ( !$posttags )
487
		return;
488
	
489
	foreach ( $posttags as $tag ) {
490
		if ( $tag->count > 1 && !is_tag($tag->slug) ) {
491
			$tag_link = '<a href="' . get_term_link($tag, 'post_tag') . '" rel="tag">' . $tag->name . ' (' . number_format_i18n( $tag->count ) . ')</a>';
492
		} else {
493
			$tag_link = $tag->name;
494
		}
495
		
496
		if ( $format == 'list' )
497
			$tag_link = '<li>' . $tag_link . '</li>';
498
		
499
		$tag_links[] = $tag_link;
500
	}
501
	
502
	echo $before . join( $sep, $tag_links ) . $after;
503
}
504
505
function latest_post_permalink() {
506
	global $wpdb;
507
	$sql = "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1";
508
	$last_post_id = $wpdb->get_var($sql);
509
	$permalink = get_permalink($last_post_id);
510
	return $permalink;
511
}
512
513
function prologue_ajax_tag_search() {
514
	global $wpdb;
515
	$s = $_GET['q'];
516
	if ( false !== strpos( $s, ',' ) ) {
517
		$s = explode( ',', $s );
518
		$s = $s[count( $s ) - 1];
519
	}
520
	$s = trim( $s );
521
	if ( strlen( $s ) < 2 )
522
		die; // require 2 chars for matching
523
	
524
	$results = $wpdb->get_col( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = 'post_tag' AND t.name LIKE ('%". like_escape( $wpdb->escape( $s ) ) . "%')" );
525
	echo join( $results, "\n" );
526
	exit;
527
}
528
529
function prologue_title_from_content( $content ) {
530
	
531
    static $strlen =  null;
532
    if ( !$strlen ) {
533
        $strlen = function_exists('mb_strlen')? 'mb_strlen' : 'strlen';
534
    }
535
    $max_len = 40;
536
    $title = $strlen( $content ) > $max_len? wp_html_excerpt( $content, $max_len ) . '...' : $content;
537
    $title = trim( strip_tags( $title ) );
538
    $title = str_replace("\n", " ", $title);
539
540
	//Try to detect image or video only posts, and set post title accordingly
541
	if ( !$title ) {
542
		if ( preg_match("/<object|<embed/", $content ) )
543
			$title = __('Video Post', 'p2');
544
		elseif ( preg_match( "/<img/", $content ) )
545
			$title = __('Image Post', 'p2');
546
		else
547
			$title = __('No Title', 'p2');
548
	}
549
    return $title;
550
}
551
if ( is_admin() && ( false === get_option('prologue_show_titles') ) ) add_option('prologue_show_titles', 1);
552
553
function prologue_inline_save() {
554
	check_ajax_referer( 'ajaxnonce', '_inline_edit' );
555
	if ( !is_user_logged_in() ) {
556
		die('<p>'.__('Error: not logged in.', 'p2').'</p>');
557
	}
558
559
	$post_id = $_POST['post_ID'];
560
	$post_id = substr( $post_id, strpos( $post_id, '-' ) + 1 );
561
	
562
	if ( !current_user_can( 'edit_post', $post_id )) {
563
		die('<p>'.__('Error: not allowed to edit post.', 'p2').'</p>');
564
	}
565
	
566
	$user_id = $current_user->ID;
567
	$post_content	= $_POST['content'];
568
	
569
	// preserve custom "big" titles
570
571
	$thepost = get_post($post_id);
572
	
573
	$clean_title = str_replace('&hellip;', '', $thepost->post_title);
574
575
	if( strpos($thepost->post_content, $clean_title ) !== 0 ) {
576
		$post_title = $thepost->post_title;
577
	} else {
578
		$post_title = prologue_title_from_content( $post_content );
579
	}
580
581
	$post = wp_update_post( array(
582
		'post_title'	=> $post_title,
583
		'post_content'	=> $post_content,
584
		'post_modified'	=> current_time('mysql'),
585
		'post_modified_gmt'	=> current_time('mysql', 1),
586
		'ID' => $post_id
587
	));
588
	
589
	$thepost = get_post( $post );
590
591
	echo apply_filters( 'the_content', $thepost->post_content );
592
	exit;
593
}
594
595
function prologue_inline_comment_save() {
596
	check_ajax_referer( 'ajaxnonce', '_inline_edit' );
597
	if ( !is_user_logged_in() ) {
598
		die('<p>'.__('Error: not logged in.', 'p2').'</p>');
599
	}
600
601
	$comment_id	= $_POST['comment_ID'];
602
	$comment_id = substr( $comment_id, strpos( $comment_id, '-' ) + 1);
603
	$this_comment = get_comment( $comment_id );
604
	
605
	if ( !current_user_can( 'edit_post', $this_comment->comment_post_ID ) ) {
606
		die('<p>'.__('Error: not allowed to edit this comment.', 'p2').'</p>');
607
	}
608
	
609
	$user_id = $current_user->ID;
610
	$comment_content = $_POST['comment_content'];
611
	
612
	$comment = wp_update_comment( array(
613
		'comment_content'	=> $comment_content,
614
		'comment_ID' => $comment_id
615
	));
616
	
617
	$thecomment = get_comment( $comment_id );
618
	echo apply_filters( 'comment_text', $thecomment->comment_content );
619
	exit;
620
}
621
622
function prologue_load_post() {
623
	check_ajax_referer( 'ajaxnonce', '_inline_edit' );
624
	if ( !is_user_logged_in() ) {
625
		die('<p>'.__('Error: not logged in.', 'p2').'</p>');
626
	}
627
	$post_id = $_GET['post_ID'];
628
	$post_id = substr( $post_id, strpos( $post_id, '-' ) + 1 );
629
	if ( !current_user_can( 'edit_post', $post_id ) ) {
630
		die('<p>'.__('Error: not allowed to edit post.', 'p2').'</p>');
631
	}
632
	$this_post = get_post( $post_id );
633
	echo $this_post->post_content ;
634
	exit;
635
}
636
637
function prologue_load_comment() {
638
	check_ajax_referer( 'ajaxnonce', '_inline_edit' );
639
	if ( !is_user_logged_in() ) {
640
		die('<p>'.__('Error: not logged in.', 'p2').'</p>');
641
	}
642
	$comment_id = attribute_escape($_GET['comment_ID']);
643
	$comment_id = substr( $comment_id, strpos( $comment_id, '-' ) + 1);
644
	$this_comment = get_comment($comment_id);
645
	$comment_content = $this_comment->comment_content;
646
	echo $comment_content;
647
	exit;
648
}
649
650
function prologue_new_post() {
651
	if( 'POST' != $_SERVER['REQUEST_METHOD'] || empty( $_POST['action'] ) || $_POST['action'] != 'prologue_new_post' ) {
652
	    die();
653
	}
654
	if ( !is_user_logged_in() ) {
655
		die('<p>'.__('Error: not logged in.', 'p2').'</p>');
656
	}
657
	if( !current_user_can( 'publish_posts' ) ) {
658
		die('<p>'.__('Error: not allowed to post.', 'p2').'</p>');
659
	}
660
	check_ajax_referer( 'ajaxnonce', '_ajax_post' );
661
	$user_id		= $current_user->user_id;
662
	$post_content	= $_POST['posttext'];
663
	$tags			= trim( $_POST['tags'] );
664
	if ( $tags == __('Tag it', 'p2') || $tags == 'Tag it' ) $tags = '';
665
	
666
    $post_title = prologue_title_from_content( $post_content );
667
668
	$post_id = wp_insert_post( array(
669
		'post_author'	=> $user_id,
670
		'post_title'	=> $post_title,
671
		'post_content'	=> $post_content,
672
		'tags_input'	=> $tags,
673
		'post_status'	=> 'publish'
674
	) );
675
	echo $post_id? $post_id : '0';
676
	exit;
677
}
678
679
680
function prologue_new_post_noajax() {
681
	if( 'POST' != $_SERVER['REQUEST_METHOD'] || empty( $_POST['action'] ) || $_POST['action'] != 'post' ) {
682
	    return;
683
	}
684
	if ( ! is_user_logged_in() )
685
		auth_redirect();
686
	if( !current_user_can( 'publish_posts' ) ) {
687
		wp_redirect( get_bloginfo( 'url' ) . '/' );
688
		exit;
689
	}
690
691
	check_admin_referer( 'new-post' );
692
693
	$user_id		= $current_user->user_id;
694
	$post_content	= $_POST['posttext'];
695
	$tags			= $_POST['tags'];
696
697
	$title = prologue_title_from_content( $post_content );
698
699
	$post_id = wp_insert_post( array(
700
		'post_author'	=> $user_id,
701
		'post_title'	=> $post_title,
702
		'post_content'	=> $post_content,
703
		'tags_input'	=> $tags,
704
		'post_status'	=> 'publish'
705
	) );
706
707
	wp_redirect( get_bloginfo( 'url' ) . '/' );
708
	exit;
709
}
710
711
712
function prologue_new_comment() {
713
	if( 'POST' != $_SERVER['REQUEST_METHOD'] || empty( $_POST['action'] ) || $_POST['action'] != 'prologue_new_comment' ) {
714
	    die();
715
	}
716
	
717
	check_ajax_referer( 'ajaxnonce', '_ajax_post' );
718
	
719
	$comment_content = isset( $_POST['comment'] )? trim( $_POST['comment'] ) : null;
720
	$comment_post_ID = isset( $_POST['comment_post_ID'] )? trim( $_POST['comment_post_ID'] ) : null;
721
	$user = wp_get_current_user();
722
	if ( $user->ID ) {
723
		if ( empty( $user->display_name ) )
724
			$user->display_name = $user->user_login;
725
		$comment_author       = $user->display_name;
726
		$comment_author_email = $user->user_email;
727
		$comment_author_url   = $user->user_url;
728
		$comment_author_url   = $user->user_url;
729
		$user_ID 			  = $user->ID;
730
		if ( current_user_can( 'unfiltered_html' ) ) {
731
		    $unfiltered_html = isset($_POST['_wp_unfiltered_html_comment'])? $_POST['_wp_unfiltered_html_comment'] : '';
732
			if ( wp_create_nonce( 'unfiltered-html-comment_' . $comment_post_ID) != $unfiltered_html ) {
733
				kses_remove_filters(); // start with a clean slate
734
				kses_init_filters(); // set up the filters
735
			}
736
		}
737
	} else {
738
		if ( get_option('comment_registration') ) {
739
		    die('<p>'.__('Error: you must be logged in to post a comment.', 'p2').'</p>');
740
		}
741
	}
742
743
	$comment_type = '';
744
745
	if ( get_option( 'require_name_email' ) && !$user->ID ) {
746
		if ( strlen($comment_author_email) < 6 || '' == $comment_author ) {
747
			die('<p>'.__('Error: please fill the required fields (name, email).', 'p2').'</p>');
748
		} elseif ( !is_email( $comment_author_email ) ) {
749
		    die('<p>'.__('Error: please enter a valid email address.', 'p2').'</p>');
750
		}
751
	}
752
753
	if ( '' == $comment_content ) {
754
	    die('<p>'.__('Error: Please type a comment.', 'p2').'</p>');
755
	}
756
	
757
	$comment_parent = isset( $_POST['comment_parent'] ) ? absint( $_POST['comment_parent'] ) : 0;
758
759
	$commentdata = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID' );
760
761
	$comment_id = wp_new_comment( $commentdata );
762
	$comment = get_comment( $comment_id );
763
	if ( !$user->ID ) {
764
		setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
765
		setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
766
		setcookie('comment_author_url_' . COOKIEHASH, clean_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
767
	}
768
	if ($comment)
769
		echo $comment_id;
770
	else 
771
		echo __("Error: Unknown error occured. Comment not posted.", 'p2');
772
    exit;
773
}
774
775
function prologue_latest_comments() {
776
	global $wpdb, $comments, $comment, $max_depth, $depth, $user_login, $user_ID, $user_identity;
777
	
778
	$number = 10; //max amount of comments to load
779
	$load_time = $_GET['load_time'];
780
	$lc_widget = $_GET['lcwidget'];
781
	$visible_posts =  (array)$_GET['vp'];
782
	
783
	if ( get_option('thread_comments') )
784
		$max_depth = get_option('thread_comments_depth');
785
	else
786
		$max_depth = -1;
787
788
	//Widget info
789
	if ( !isset($options) )
790
		$options = get_option('widget_recent_comments');
791
	$size = $options[ 'avatar_size' ] ? $options[ 'avatar_size' ] : 32;
792
	
793
	//get new comments
794
	if ($user_ID) {
795
		$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ))  AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT $number", $user_ID, $load_time));
796
	} else if ( empty($comment_author) ) {
797
		$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT $number", $load_time));
798
	} else {
799
		$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE (comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT $number", $comment_author, $comment_author_email, $load_time));
800
	}
801
	$number_of_new_comments = count($comments);
802
803
    $prepare_comments = array();
804
	if ($number_of_new_comments > 0) {
805
		foreach ($comments as $comment) {
806
807
			// Setup comment html if post is visible
808
			$comment_html = '';
809
			if ( in_array( $comment->comment_post_ID, $visible_posts ) ) {
810
				$comment_html = prologue_comment_frontpage($comment, array('max_depth' => $max_depth, 'before' => ' | '), $depth, false);
811
			}
812
813
			// Setup widget html if widget is visible
814
			$comment_widget_html = '';
815
			if ( $lc_widget ) {
816
				$comment_widget_html = prologue_comment_widget_html( $comment, $size, 'top', false );
817
			}
818
			
819
			$prepare_comments[] = array( "id" => $comment->comment_ID, "postID" => $comment->comment_post_ID, "commentParent" =>  $comment->comment_parent,
820
				"html" => $comment_html, "widgetHtml" => $comment_widget_html );
821
		}
822
		
823
		$json_data = array("numberofnewcomments" => $number_of_new_comments, "comments" => $prepare_comments, "lastcommenttime" => gmdate( 'Y-m-d H:i:s' ));
824
		echo json_encode( $json_data );
825
	} else { // No new comments
826
        echo('0');
827
	}
828
	exit;
829
}
830
831
function prologue_latest_posts() {
832
	$load_time = $_GET['load_time'];
833
	$frontpage = $_GET['frontpage'];
834
	$num_posts = 10; //max amount of posts to load
835
	$number_of_new_posts = 0;
836
	$prologue_query = new WP_Query('showposts=' . $num_posts . '&post_status=publish');
837
	ob_start();
838
	while ($prologue_query->have_posts()) : $prologue_query->the_post();
839
	    $current_user_id = get_the_author_ID( );
840
		if ( get_gmt_from_date( get_the_time( 'Y-m-d H:i:s' ) ) <=  $load_time ) continue;
841
		$number_of_new_posts++;
842
		if ( $frontpage ) {
843
?>
844
<li id="prologue-<?php the_ID(); ?>" class="newupdates user_id_<?php the_author_ID( ); ?>">
845
    <?php echo prologue_get_avatar( $current_user_id, get_the_author_email( ), 48 ); ?>
846
    <h4>
847
		<?php the_author_posts_link( ); ?>
848
		<span class="meta">
849
		    <?php printf( __('%s <em>on</em> %s', 'p2'),  get_the_time(), get_the_time( get_option('date_format') ) ); ?> |
850
			<?php comments_popup_link( '0', '1', '%' ); ?>
851
			
852
			<span class="actions">
853
				<a href="<?php the_permalink( ); ?>" class="thepermalink"><?php _e('Permalink', 'p2'); ?></a>
854
			<?php if (function_exists('post_reply_link'))
855
				echo post_reply_link(array('before' => ' | ', 'reply_text' => __('Reply', 'p2'), 'add_below' => 'prologue'), get_the_id()); ?>
856
			<?php if (current_user_can('edit_post', get_the_id())) { ?>
857
			|  <a href="<?php echo (get_edit_post_link( get_the_id() ))?>" class="post-edit-link" rel="<?php the_ID(); ?>"><?php _e('Edit', 'p2'); ?></a>
858
			<?php } ?>
859
			</span>
860
			<br />
861
			<?php tags_with_count( '', __( 'Tags:' , 'p2').' ', ', ', ' ' ); ?>
862
		</span>
863
	</h4>
864
	<div class="postcontent<?php if (current_user_can( 'edit_post', get_the_id() )) {?> editarea<?php } ?>" id="content-<?php the_ID(); ?>"><?php the_content( __( '(More ...)' , 'p2') ); ?></div> <!-- // postcontent -->
865
	<div class="bottom_of_entry">&nbsp;</div>
866
</li>
867
<?php
868
}
869
    endwhile;
870
    $posts_html = ob_get_contents();
871
    ob_end_clean();
872
    if ( $number_of_new_posts == 0 ) {
873
    	echo '0';
874
    } else {
875
    	$json_data = array("numberofnewposts" =>$number_of_new_posts, "html" => $posts_html, "lastposttime" => gmdate( 'Y-m-d H:i:s' ));
876
    	echo json_encode( $json_data );
877
    }
878
    exit;
879
}
880
881
/* Recent comments with avatars */
882
function prologue_widget_recent_comments_avatar( $args ) {
883
	global $wpdb, $comments, $comment;
884
	extract($args, EXTR_SKIP);
885
	if ( !isset($options) )
886
		$options = get_option('widget_recent_comments');
887
	$title = empty($options['title']) ? __('Recent Comments', 'p2') : $options['title'];
888
	if ( !$number = (int) $options['number'] )
889
		$number = 5;
890
	else if ( $number < 1 )
891
		$number = 1;
892
	else if ( $number > 15 )
893
		$number = 15;
894
895
	if ( !$comments = wp_cache_get( 'recent_avatar_comments', 'widget' ) ) {
896
		$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
897
		wp_cache_add( 'recent_avatar_comments', $comments, 'widget' );
898
	}
899
	$size = $options[ 'avatar_size' ] ? $options[ 'avatar_size' ] : 24;
900
	?>
901
	<?php echo $before_widget; ?>
902
		<?php echo $before_title . wp_specialchars( $title ) . $after_title; ?>
903
		<table class='recentcommentsavatar' cellspacing='0' cellpadding='0' border='0' id="recentcommentstable"><?php
904
		$tdclass = 'top';
905
		if ( $comments ) : foreach ($comments as $comment) :
906
			prologue_comment_widget_html($comment, $size, $tdclass, true);
907
		endforeach; endif;?></table>
908
	<?php echo $after_widget; ?>
909
<?php
910
}
911
912
if(!function_exists('wp_delete_recent_comments_avatar_cache')) {
913
	function wp_delete_recent_comments_avatar_cache() {
914
		wp_cache_delete( 'recent_avatar_comments', 'widget' );
915
	}
916
	add_action( 'comment_post', 'wp_delete_recent_comments_avatar_cache' );
917
	add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_avatar_cache' );
918
}
919
920
function prologue_widget_recent_comments_avatar_control() {
921
	$options = $newoptions = get_option('widget_recent_comments');
922
	if ( $_POST["recent-comments-submit"] ) {
923
		$newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
924
		$newoptions['number'] = (int) $_POST["recent-comments-number"];
925
		$newoptions['avatar_size'] = (int) $_POST["recent-comments-avatar-size"];
926
		$newoptions['avatar_bg'] = preg_replace('/[^a-z0-9#]/', '', $_POST["recent-comments-avatar-bg"] );
927
		$newoptions['text_bg'] = preg_replace('/[^a-z0-9#]/i', '', $_POST["recent-comments-text-bg"] );
928
	}
929
	if ( print_r( $options, 1 ) != print_r( $newoptions, 1 ) ) {
930
		$options = $newoptions;
931
		update_option('widget_recent_comments', $options);
932
		wp_delete_recent_comments_cache(); // If user selects "No Avatars", the core recent comments widget is used, so we need to clear that cache too.
933
		wp_delete_recent_comments_avatar_cache();
934
	}
935
	$title = $options['title'];
936
	$avatar_bg = $options[ 'avatar_bg' ];
937
	$text_bg   = $options[ 'text_bg' ];
938
	$avatar_size = $options[ 'avatar_size' ] == '' ? '48' : (int) $options[ 'avatar_size' ];
939
	if ( !$number = (int) $options['number'] )
940
		$number = 5;
941
	else if ( $number < 1 )
942
		$number = 1;
943
	else if ( $number > 15 )
944
		$number = 15;
945
?>
946
			<p><label for="recent-comments-title"><?php _e('Title:', 'p2'); ?> <input id="recent-comments-title" name="recent-comments-title" type="text" class="widefat" value="<?php echo attribute_escape($title); ?>" /></label></p>
947
			<p><label for="recent-comments-number"><?php _e('Number of comments to show:', 'p2'); ?> <input style="width: 25px; text-align: center;" id="recent-comments-number" name="recent-comments-number" type="text" value="<?php echo $number; ?>" /></label> <small><?php _e('(at most 15)', 'p2'); ?></small></p>
948
			<p><label for="recent-comments-avatar-size"><?php _e('Avatar Size (px):', 'p2'); ?> <select name='recent-comments-avatar-size'>
949
			<option value='1'<?php echo 1 == $avatar_size ? ' selected' : ''; ?>><?php _e( 'No Avatars' , 'p2'); ?></option>
950
			<option value='16'<?php echo 16 == $avatar_size ? ' selected' : ''; ?>>16x16</option>
951
			<option svalue='32'<?php echo 32 == $avatar_size ? ' selected' : ''; ?>>32x32</option>
952
			<option value='48'<?php echo 48 == $avatar_size ? ' selected' : ''; ?>>48x48</option>
953
			<option value='96'<?php echo 96 == $avatar_size ? ' selected' : ''; ?>>96x96</option>
954
			<option value='128'<?php echo 128 == $avatar_size ? ' selected' : ''; ?>>128x128</option>
955
			</select></label></p>
956
			<p><label for="recent-comments-avatar-bg"><?php _e('Avatar background color:', 'p2'); ?> <input style="width: 50px;" id="recent-comments-avatar-bg" name="recent-comments-avatar-bg" type="text" value="<?php echo $avatar_bg; ?>" /></label></p>
957
			<p><label for="recent-comments-text-bg"><?php _e('Text background color:', 'p2'); ?> <input style="width: 50px;" id="recent-comments-text-bg" name="recent-comments-text-bg" type="text" value="<?php echo attribute_escape($text_bg); ?>" /></label></p>
958
959
			<input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
960
<?php
961
}
962
963
function prologue_widget_recent_comments_avatar_style() {
964
	$options = get_option('widget_recent_comments');
965
	$avatar_bg = $options[ 'avatar_bg' ] == '' ? '' : 'background: ' . $options[ 'avatar_bg' ] . ';';
966
	$text_bg = $options[ 'text_bg' ] == '' ? '' : 'background: ' . $options[ 'text_bg' ] . ';';
967
	$style = "
968
<style type='text/css'>
969
table.recentcommentsavatar img.avatar { border: 0px; margin:0; }
970
table.recentcommentsavatar a {border: 0px !important; background-color: transparent !important}
971
td.recentcommentsavatartop {padding:0px 0px 1px 0px;
972
							margin:   0px;
973
							{$avatar_bg} }
974
td.recentcommentsavatarend {padding:0px 0px 1px 0px;
975
							margin:0px;
976
							{$avatar_bg} }
977
td.recentcommentstexttop {
978
							{$text_bg} border: none !important; padding:0px 0px 0px 10px;}
979
td.recentcommentstextend {
980
							{$text_bg} border: none !important; padding:0px 0px 2px 10px;}
981
</style>";
982
	echo $style;
983
}
984
985
function prologue_widget_recent_comments_avatar_register() {
986
	$options = get_option('widget_recent_comments');
987
	if( isset( $options[ 'avatar_size' ] ) && $options[ 'avatar_size' ] == 1 && is_admin() == false )
988
		return;
989
	$class = array('classname' => 'widget_recent_comments');
990
	wp_register_sidebar_widget('recent-comments', __('Recent Comments', 'p2'), 'prologue_widget_recent_comments_avatar', $class);
991
	wp_register_widget_control('recent-comments', __('Recent Comments', 'p2'), 'prologue_widget_recent_comments_avatar_control' );
992
993
	if ( is_active_widget('prologue_widget_recent_comments_avatar') )
994
		add_action('wp_head', 'prologue_widget_recent_comments_avatar_style');
995
}
996
add_action('init', 'prologue_widget_recent_comments_avatar_register', 10);
997
998
//Search related Functions
999
1000
function search_comments_distinct( $distinct ) {
1001
	global $wp_query;
1002
	if (!empty($wp_query->query_vars['s'])) {
1003
		return 'DISTINCT';
1004
	}
1005
}
1006
add_filter('posts_distinct', 'search_comments_distinct');
1007
1008
function search_comments_where( $where ) {
1009
	global $wp_query, $wpdb;
1010
	if (!empty($wp_query->query_vars['s'])) {
1011
			$or = " OR ( comment_post_ID = ".$wpdb->posts . ".ID  AND comment_approved =  '1' AND comment_content LIKE '%" . like_escape( $wpdb->escape($wp_query->query_vars['s'] ) ) . "%') ";
1012
  			$where = preg_replace( "/\bor\b/i", $or." OR", $where, 1 );
1013
	}
1014
	return $where;
1015
}
1016
add_filter('posts_where', 'search_comments_where');
1017
1018
function search_comments_join( $join ) {
1019
	global $wp_query, $wpdb, $request;
1020
	if (!empty($wp_query->query_vars['s'])) {
1021
		$join .= " LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID  AND comment_approved =  '1')";
1022
	}
1023
	return $join;
1024
}
1025
add_filter('posts_join', 'search_comments_join');
1026
1027
function get_search_query_terms() {
1028
	$search = get_query_var('s');
1029
	$search_terms = get_query_var('search_terms');
1030
	if ( !empty($search_terms) ) {
1031
		return $search_terms;
1032
	} else if ( !empty($search) ) {
1033
		return array($search);
1034
	}
1035
	return array();
1036
}
1037
1038
function hilite( $text ) {
1039
	$query_terms = array_filter( array_map('trim', get_search_query_terms() ) );
1040
	foreach ( $query_terms as $term ) {
1041
	    $term = preg_quote( $term, '/' );
1042
		if ( !preg_match( '/<.+>/', $text ) ) {
1043
			$text = preg_replace( '/(\b'.$term.'\b)/i','<span class="hilite">$1</span>', $text );
1044
		} else {
1045
			$text = preg_replace( '/(?<=>)([^<]+)?(\b'.$term.'\b)/i','$1<span class="hilite">$2</span>', $text );
1046
		}
1047
	}
1048
	return $text;
1049
}
1050
1051
function hilite_tags( $tags ) {
1052
	$query_terms = array_filter( array_map('trim', get_search_query_terms() ) );
1053
	// tags are kept escaped in the db
1054
	$query_terms = array_map( 'wp_specialchars', $query_terms );
1055
	foreach( array_filter((array)$tags) as $tag ) {
1056
	    if ( in_array( trim($tag->name), $query_terms ) ) {
1057
	        $tag->name ="<span class='hilite'>". $tag->name . "</span>";
1058
	    }
1059
	}
1060
	return $tags;
1061
}
1062
1063
// Highlight text and comments:
1064
add_filter('the_content', 'hilite');
1065
add_filter('get_the_tags', 'hilite_tags');
1066
add_filter('the_excerpt', 'hilite');
1067
add_filter('comment_text', 'hilite');
1068
1069
function iphone_css() {
1070
if ( strstr( $_SERVER['HTTP_USER_AGENT'], 'iPhone' ) or isset($_GET['iphone']) && $_GET['iphone'] ) { ?>
1071
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/> 
1072
<style type="text/css">
1073
#header_img img, #sidebar, #postbox .avatar {
1074
	display: none;
1075
}
1076
#header {
1077
	margin: 0;
1078
	padding: 0;
1079
}
1080
#header .sleeve {
1081
	padding: 0;
1082
	margin: 0;
1083
	width: 100%;
1084
}
1085
#header h1, #header small {
1086
	display: block;
1087
	width: 100%;
1088
}
1089
#header h1 {
1090
	padding-left: 16px;
1091
	margin-bottom: 2px;
1092
}
1093
1094
#main h2 .controls {
1095
	display: none;
1096
}
1097
.actions {
1098
	clear: both;
1099
	display: block;
1100
	position: static !important;
1101
	text-align: right;
1102
	height: 1.1em;
1103
	top: 0px !important;
1104
	margin-top: 6px;
1105
	margin-bottom: -1.8em;
1106
}
1107
.actions a { font-size: 1.2em; padding: 6px; }
1108
.meta {
1109
	margin-top: 6px;
1110
	line-height: .8em;
1111
	width: 100%;
1112
}
1113
div.postcontent, div.commentcontent {
1114
	margin-left: 30px;
1115
}
1116
#main h4 {
1117
	line-height: 1.1em;
1118
	clear: both;
1119
	margin-bottom: .5em;
1120
}
1121
.avatar {
1122
position: relative;
1123
left: 0px;
1124
top: 5px;
1125
margin-bottom: -20px;
1126
}
1127
#main #respond.replying, #main .commentlist li #respond.replying  {
1128
position: absolute;
1129
width: 100%;
1130
height: 100%;	
1131
	margin-left: 0 !important;
1132
	z-index: 1000;
1133
	left: 0px !important;
1134
}
1135
.single #main .postcontent {
1136
	clear: both;
1137
	margin: 0;
1138
	padding: 0;
1139
}
1140
li #respond textarea {
1141
	width: 80%;
1142
	height: 80%;
1143
}
1144
#main h4 {
1145
	position: relative;
1146
	margin-left: 30px;
1147
}
1148
h1 a {
1149
	display: block;
1150
	width: 295px;
1151
	font-family: Helvetica;
1152
}
1153
#footer {
1154
	width: 100%;
1155
	font-size: 8px;
1156
	min-width: 0;
1157
}
1158
#main ul.commentlist, #main ul.commentlist ul {
1159
	margin-left: 20px !important;
1160
}
1161
1162
#wrapper {
1163
width: 100%;
1164
	
1165
	min-width: 0;
1166
	margin: 0;
1167
	padding: 0;
1168
	overflow: visible;
1169
	position: static;
1170
}
1171
.avatar {
1172
	width: 20px;
1173
	height: 20px;
1174
}
1175
.sleeve_main {
1176
width: 100%;
1177
	margin: 0;
1178
}
1179
#header {
1180
	padding: 0;
1181
	margin: 0;
1182
	width: 100%;
1183
}
1184
1185
#main {
1186
	margin: 0 10px;
1187
	padding: 0;
1188
	float: none;
1189
}
1190
#main ul#postlist ul li {
1191
	margin-left: 0;
1192
}
1193
h1 {
1194
	font-size: 2em;
1195
	font-family: Georgia, "Times New Roman", serif;
1196
	margin-left: 0;
1197
	margin-top: 5px;
1198
	margin-bottom: 10px;
1199
	padding: 0;
1200
}
1201
1202
h2 {
1203
	font-size: 1.2em;
1204
	font-weight: bold;
1205
	color: #555;
1206
}
1207
1208
#postbox form {
1209
	padding: 5px;
1210
}
1211
1212
#postbox textarea#posttext {
1213
	height: 50px;
1214
	border: 1px solid #c6d9e9;
1215
	margin-bottom: 10px;
1216
	padding: 2px;
1217
	font: 1.4em/1.2em "Lucida Grande",Verdana,"Bitstream Vera Sans",Arial,sans-serif;
1218
}
1219
#postbox input#tags,  #commentform #comment {
1220
	font-size: 1.2em;
1221
	padding: 2px;
1222
	border: 1px solid #c6d9e9;
1223
	width: 300px;
1224
	margin-left: 0;
1225
}
1226
#postbox {
1227
	margin: 0;
1228
	padding: 0;
1229
}
1230
#postbox label {
1231
	color: #333;
1232
	display: block;
1233
	font-size: 1.2em;
1234
	margin-bottom: 4px;
1235
	margin-left: 0;
1236
	font-weight: bold;
1237
}
1238
#postbox .inputarea {
1239
	padding-left: 0;
1240
}
1241
1242
#notify {
1243
	width: 70%;
1244
left: 15%;
1245
top: 30%;
1246
}
1247
#postbox input#submit {
1248
	font-size: 1.2em;
1249
1250
	margin-top: 5px;
1251
}
1252
1253
#main ul {
1254
	list-style: none;
1255
	margin-top: 16px;
1256
	margin-left: 0;
1257
}
1258
1259
#wpcombar {
1260
	display: none;
1261
}
1262
body {
1263
	padding-top: 0 !important;
1264
}
1265
</style>
1266
<?php } }
1267
add_action('wp_head', 'iphone_css');
1268
1269
/*
1270
	Modified to replace query string with blog url in output string
1271
*/
1272
function prologue_get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
1273
	global $user_ID;
1274
1275
	$defaults = array('add_below' => 'comment', 'respond_id' => 'respond', 'reply_text' => __('Reply', 'p2'),
1276
		'login_text' => __('Log in to Reply', 'p2'), 'depth' => 0, 'before' => '', 'after' => '');
1277
1278
	$args = wp_parse_args($args, $defaults);
1279
	if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )
1280
		return;
1281
1282
	extract($args, EXTR_SKIP);
1283
1284
	$comment = get_comment($comment);
1285
	$post = get_post($post);
1286
1287
	if ( 'open' != $post->comment_status )
1288
		return false;
1289
1290
	$link = '';
1291
1292
	$reply_text = wp_specialchars( $reply_text );
1293
1294
	if ( get_option('comment_registration') && !$user_ID )
1295
		$link = '<a rel="nofollow" href="' . site_url('wp-login.php?redirect_to=' . urlencode( get_permalink() ) ) . '">' . wp_specialchars( $login_text ) . '</a>';
1296
	else
1297
		$link = "<a rel='nofollow' class='comment-reply-link' href='". get_permalink($post). "#" . urlencode( $respond_id ) . "' onclick='return addComment.moveForm(\"" . js_escape( "$add_below-$comment->comment_ID" ) . "\", \"$comment->comment_ID\", \"" . js_escape( $respond_id ) . "\", \"$post->ID\")'>$reply_text</a>";
1298
	return apply_filters('comment_reply_link', $before . $link . $after, $args, $comment, $post);
1299
}
1300
1301
function prologue_comment_depth_loop( $comment_id, $depth )  {
1302
	$comment = get_comment( $comment_id );
1303
	if ( $comment->comment_parent != 0 ) {
1304
		return prologue_comment_depth_loop( $comment->comment_parent, $depth + 1 );
1305
	}
1306
	return $depth;
1307
}
1308
1309
function prologue_get_comment_depth( $comment_id ) {
1310
	return prologue_comment_depth_loop( $comment_id, 1 );
1311
}
1312
1313
function prologue_comment_depth( $comment_id ) {
1314
	echo prologue_get_comment_depth( $comment_id );
1315
}
1316
1317
function prologue_navigation() { ?>
1318
	<div class="navigation"><p>
1319
	<?php if(!is_single()) { ?>
1320
   		<?php posts_nav_link(' | ', __('&larr;&nbsp;Newer&nbsp;Posts', 'p2'), __('Older&nbsp;Posts&nbsp;&rarr;', 'p2')); ?>
1321
	<?php } else { ?>
1322
		<?php previous_post_link('%link', __('&larr;&nbsp;Older&nbsp;Posts', 'p2') ) ?> | <?php next_post_link('%link', __('Newer&nbsp;Posts&nbsp;&rarr;', 'p2')) ?>
1323
	<?php } ?>
1324
</p></div>
1325
<?php }
1326
1327
function prologue_poweredby_link() {
1328
    return apply_filters( 'prologue_poweredby_link',
1329
        sprintf( __('<strong>%1$s</strong> is proudly powered by %2$s.', 'p2'),
1330
        get_bloginfo('name'), '<a href="http://wordpress.org/" rel="generator">WordPress</a>' ) );
1331
}
1332
1333
if ( defined('IS_WPCOM') && IS_WPCOM ) {
1334
    add_filter( 'prologue_poweredby_link', returner('<a href="http://wordpress.com/">'.__('Blog at WordPress.com', 'p2').'.</a>') );
1335
}