<?php
/*
Simple:Press Forum
Blog Linking - general support routines
$LastChangedDate: 2009-10-17 04:59:43 +0100 (Sat, 17 Oct 2009) $
$Rev: 2832 $
*/

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
	die('Access Denied');
}

# ------------------------------------------------------------------
# sf_blog_links_control()
#
# General postmeta handling for blog linking
#	$action		save, update, read or delete
#	$postid		WP Post id of the link
#	$forumid	ID of target forum
#	$topicid	ID of target topic
#	$syncedit	Optional - sync edit flag
# ------------------------------------------------------------------
function sf_blog_links_control($action, $postid, $forumid=0, $topicid=0, $syncedit=0)
{
	global $wpdb;

	# seems to sometimes get triggered by other plugins although it suggests a core WP bug
	if(!isset($postid)) return;

	if($action == 'save')
	{
		# check if there already...
		$result = $wpdb->get_results("SELECT * FROM ".SFLINKS." WHERE post_id=".$postid);
		if($result)
		{
			$action = 'update';
		} else {
			$sql="INSERT INTO ".SFLINKS." (post_id, forum_id, topic_id, syncedit) VALUES (".$postid.", ".$forumid.", ".$topicid.", ".$syncedit.");";
			$wpdb->query($sql);

//			# sync blog and forum tags
//			sf_sync_blog_tags($postid, $forumid, $topicid);

			return;
		}
	}

	if($action == 'update')
	{
		$sql="UPDATE ".SFLINKS." SET forum_id=".$forumid.", topic_id=".$topicid.", syncedit=".$syncedit." WHERE post_id=".$postid;
		$wpdb->query($sql);

		return;
	}
	if($action == 'read')
	{
		$sql = "SELECT * FROM ".SFLINKS." WHERE post_id=".$postid;
		return($wpdb->get_row($sql));
	}
	if($action == 'delete')
	{
		$sql = "DELETE FROM ".SFLINKS." WHERE post_id=".$postid;
		return($wpdb->get_row($sql));
	}
}

# ------------------------------------------------------------------
# sf_make_excerpt()
#
# Creates an excerpt of x number of words from post content
#	$content	The text of the post
#	$words		Word count required (defaults to 50)
# ------------------------------------------------------------------
function sf_make_excerpt($content, $words)
{
	if((empty($words)) || ($words == 0)) $words = 50;

	if($content != '')
	{
		$length = $words;
		$content = str_replace(']]>', ']]&gt;', $content);

		if($length > count(preg_split('/[\s]+/', strip_tags($content), -1)))
		{
			return $content;
		}

		$text_bits = preg_split('/([\s]+)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
		$in_tag = false;
		$n_words = 0;
		$content = '';
		foreach($text_bits as $chunk)
		{
			if(0 < preg_match('/<[^>]*$/s', $chunk))
			{
				$in_tag = true;
			} elseif(0 < preg_match('/>[^<]*$/s', $chunk)) {
				$in_tag = false;
			}

			if(!$in_tag && '' != trim($chunk) && substr($chunk, -1, 1) != '>')
			{
				$n_words++;
			}
			$content .= $chunk;

			if($n_words >= $length && !$in_tag)
			{
				break;
			}
		}
		$content = $content . '&#8230;';
		$content = force_balance_tags($content);
	}
	return $content;
}

# ------------------------------------------------------------------
# sf_sync_blog_tags()
#
# Creates topic tags based on blog post tags if used
#	$postid		WP Post id of the link
#	$data		forum/topic postmeta data
# ------------------------------------------------------------------
function sf_sync_blog_tags($postid, $forumid, $topicid)
{
	# get tags for wp blog post
	$terms = apply_filters( 'get_the_tags', wp_get_object_terms($postid, 'post_tag') );

	# get the forum id and topic id ($data => forumid@topicid)
	$forum = sf_get_forum_record($forumid);

	# only do tags if the forum is setup for tags and the blog post has tags
	if ($forum->use_tags && $terms)
	{
		$tags = array();
		foreach ($terms as $term)
		{
			$tags[] = $term->name;
		}
		# need tags in a list
		$tags = implode(",", $tags);

		# now save the topic tags but in case its an update use change routine
		sf_change_topic_tags($topicid, $tags);
	}
}

# ------------------------------------------------------------------
# sf_transform_bloglink_label()
#
# Formats the label to display on blog posts
#	$postid		WP Post id of the link
#	$links		forum/topic postmeta data
#	$show_img	Support template tag no image option
# ------------------------------------------------------------------
function sf_transform_bloglink_label($postid, $links, $show_img=true)
{
	if(!defined('SFRESOURCES')) {
		sf_setup_global_constants();
	}

	$out = '';

	$sfpostlinking = array();
	$sfpostlinking = sf_get_option('sfpostlinking');
	$text = sf_filter_text_display($sfpostlinking['sflinkblogtext']);
	$icon = '<img src="'.SFRESOURCES.'bloglink.png" alt="" />';
	$postcount = sf_get_posts_count_in_linked_topic($links->topic_id);
	$href = '<a href="'.sf_build_url(sf_get_forum_slug($links->forum_id), sf_get_topic_slug($links->topic_id), 1, 0).'">';

	if(!$postcount)
	{
		# break the link
		sf_blog_links_control('delete', $postid);
		return $content;
	}

    if ($show_img)
	{
		if(strpos($text, '%ICON%') !== false)
		{
			$text = str_replace('%ICON%', $icon, $text);
		}
	} else {
		$text = str_replace('%ICON%', '', $text);
	}

	if(strpos($text, '%FORUMNAME%') !== false)
	{
		$text = str_replace('%FORUMNAME%', sf_get_forum_name_from_id($links->forum_id), $text);
	}

	if(strpos($text, '%TOPICNAME%') !== false)
	{
		$text = str_replace('%TOPICNAME%', sf_get_topic_name_from_id($links->topic_id), $text);
	}

	if(strpos($text, '%POSTCOUNT%') !== false)
	{
		$text = str_replace('%POSTCOUNT%', $postcount, $text);
	}

	if(strpos($text, '%LINKSTART%') !== false)
	{
		$text = str_replace('%LINKSTART%', $href, $text);
	} else {
		$text = $href.$text;
	}

	if(strpos($text, '%LINKEND%') !== false)
	{
		$text = str_replace('%LINKEND%', '</a>', $text);
	} else {
		$text = $text.'</a>';
	}

	$out = '<span class="sfforumlink">'.$text.'</span>';

	return $out;
}
?>