<?php

/**
 * Register the Tax Tips & News RSS feed
 *
 * This function registers the Tax
 * Tips & News RSS feed.
 *
 * @function tax_tips_rss_feed()
 *
 * @uses add_feed(), tax_tips_rss()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.0.0 
 *
 */
function tax_tips_rss_feed() {
	
	add_feed( 'rss-feed', 'tax_tips_rss' );
}
add_action( 'init', 'tax_tips_rss_feed' );



/**
 * Return the Tax Tips & News RSS feed
 *
 * This function retrieves the Tax Tips
 * & Newss RSS feed from the template in
 * the WordPress Theme.
 *
 * @function tax_tips_rss()
 *
 * @uses get_template_part()
 *
 * @param N/A
 *
 * @return (string) the RSS feed template
 *
 * @since 1.0.0 
 *
 */
function tax_tips_rss() {
	
	get_template_part( 'templates/rss', 'tax-tips' );
}



/**
 * Rewrite rule for Tax Tips & News
 *
 * This function adds the rewrite rule for
 * the Tax Tips & News page. This function
 * is too specific, relying on the directory
 * structure 'news/tax-news'. If the customer
 * changes the title/slug of news or tax-news,
 * the rewrite rule will break the tax
 * news page.
 *
 * @function add_rewrite_rules_tax_news()
 *
 * @uses add_rewrite_rule(), add_rewrite_tag()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.0.0 
 *
 */
function add_rewrite_rules_tax_news() {
	
    add_rewrite_rule(
        '^news/tax-news/article/([0-9]*)$',
        'index.php?page_id=58&tax-news-id=$matches[1]',
        'top'
    );
	add_rewrite_tag( '%tax-news-id%', '([^&]+)' );
}
add_action( 'init', 'add_rewrite_rules_tax_news' );



/**
 * Rewrite YOAST Plugin breadcrumbs for Tax Tips & News
 *
 * This function rewrites the YOAST Plugin
 * breadcrumbs the Tax Tips & News page.
 * This function is too specific, relying
 * on the directory structure 'news/tax-news'.
 * If the customer changes the title/slug
 * of news or tax-news, the breadcrumbs
 * will break. This function also uses a
 * hacky way of retrieving the YOAST Plugin
 * breadcrumb separator.
 *
 * @function tax_news_breadcrumbs()
 *
 * @uses get_query_var(), get_permalink(), get_the_title(), get_tax_news_title()
 *
 * @see get_separator()
 *
 * @param (string) $links a list of links for the breadcrumbs
 *
 * @return (string) the updated list of links for the breadcrumbs
 *
 * @since 1.0.0 
 *
 */
function tax_news_breadcrumbs( $links ) {
	
	global $yoast_sep;
	
	if ( get_query_var( 'tax-news-id' ) ) {
		
		if ( strpos( $links, 'breadcrumb_last' ) !== false ) {

			$links = '<a href="' . get_permalink() . '" rel="v:url" property="v:title">' . get_the_title() . '</a> ' . $yoast_sep;
			$links .= ' / <a href="'.site_url().'/news/tax-news/archived-tax-tips-and-news/">Archived Tax Tips and News</a> / <strong class="breadcrumb_last">' . get_tax_news_title( get_query_var( 'tax-news-id' ) ) . '</strong>';
		}
	}
	return $links;
}
add_filter( 'wpseo_breadcrumb_single_link', 'tax_news_breadcrumbs' );



/**
 * Rewrite YOAST Plugin breadcrumbs for Tax Tips & News
 *
 * This function retrieves the seperator
 * set for the YOAST Plugin breadcrumbs,
 * and sets it as a global variable to be
 * used in tax_news_breadcrumbs().
 *
 * @function get_separator()
 *
 * @see tax_news_breadcrumbs()
 *
 * @param (string) $sep the seperator set via the YOAST Plugin
 *
 * @return (string) the seperator set via the YOAST Plugin
 *
 * @since 1.0.0 
 *
 */
function get_separator( $sep ) {
	
	global $yoast_sep;
	
	$yoast_sep = $sep;
	return $sep;
}
add_filter( 'wpseo_breadcrumb_separator', 'get_separator' );



/**
 * Replace the WordPress content title
 *
 * This function replaces the WordPress
 * content title with the current Tax
 * Tips & News title.
 *
 * @function tax_news_article_title()
 *
 * @uses in_the_loop(), get_query_var(), get_tax_news_title()
 *
 * @param (string) $the_title the title of WordPress post
 *
 * @return (string) the title of the Tax Tips & News article
 *
 * @since 1.0.0 
 *
 */
function tax_news_article_title( $the_title ) {
	
    if ( in_the_loop() && get_query_var( 'tax-news-id' ) ) {
		
		$the_title = get_tax_news_title( get_query_var( 'tax-news-id' ) );
	}
    return $the_title;  
}
add_filter( 'the_title', 'tax_news_article_title' );



/**
 * Return the current Tax Tips & News title
 *
 * This function retrieves the Tax Tips
 * & News article title from the database.
 *
 * @function get_tax_news_title()
 *
 * @uses $iris_db_old::get_row()
 *
 * @param (string) $id the id of the current Tax Tips & News article
 *
 * @return (string) the title of the current Tax Tips & News article
 *
 * @since 1.0.0 
 *
 */
function get_tax_news_title( $id ) {
	
	global $iris_db_old;
	
	$tax_news = $iris_db_old->get_row( "SELECT title FROM t_news WHERE id = '$id'" );
	return $tax_news->title;
}



/**
 * Return the latest Tax Tips & News content or list
 *
 * This function retrieves the Tax Tips &
 * News list or article content from the
 * database depending on the URL parameter
 * 'tax-news-id'. If the URL has the parameter
 * 'tax-news-id', the function will query
 * the database for the article ID. The
 * function then converts any [[ or ]]
 * characters into " quotes. This is because
 * version 3 <A> links were escaped with
 * square brackets instead of quotes.
 *
 * @function get_tax_tips_and_news()
 *
 * @uses get_query_var(), $iris_db_old::get_row(), ow_tax_news_list()
 *
 * @param N/A
 *
 * @return (string) the title of the current Tax Tips & News article
 *
 * @since 1.3.1 
 *
 */
function get_tax_tips_and_news() {
	
	// This is a WP Super Cache constant to prevent the newsletter page being cached.
    define( 'DONOTCACHEPAGE', true );
	// This is a Litespeed Cache constant to prevent the newsletter page being cached.
	define( 'LSCACHE_NO_CACHE', true );
	
	global $iris_db_old;
	
	$tax_news_slug = str_replace( '-', ' ', get_query_var( 'tax-news-id' ) );
	
	if ( $tax_news_slug ) {

		$tax_news_item = $iris_db_old->get_row( "SELECT * FROM t_news WHERE id = '$tax_news_slug'" );
		$tax_news_content = str_replace( ']]', '"', str_replace( '[[', '"', $tax_news_item->details ) );
		return $tax_news_content;
	}
	else {
		
		return ow_tax_news_list( array( 'id' => 'tax-tips-and-news', 'class' => '', 'summary' => true, 'readmore' => 'Read More', 'sep' => '' ) ) .'<blockquote><a href="'.site_url().'/news/tax-news/archived-tax-tips-and-news/" style="text-decoration:none;">Archived Tax Tips and News</a></blockquote>'. ow_rss_icon() .'<br>';
	}
}
add_shortcode( 'tax_tips_and_news', 'get_tax_tips_and_news' );



/**
 * Return the latest Tax Tips & News date
 *
 * This function retrieves the Tax Tips &
 * News datestamp from the database and
 * converts it into the specified date format.
 *
 * @function ow_tax_news_date()
 *
 * @uses $iris_db_old::get_row(), ow_tax_news_list()
 *
 * @param (string) $format the format of the date() conversion
 *
 * @return (string) the date of the latest Tax Tips & News
 *
 * @since 1.0.0 
 *
 */
function ow_tax_news_date( $format = 'F Y' ) {
	
	global $iris_db_old;
	
	$tax_news_date = $iris_db_old->get_row( "SELECT newsMonthName FROM t_news_months WHERE newsMonthCurrent = 'yes'" );
	$date = date( $format, strtotime( $tax_news_date->newsMonthName ) );
	return $date;
}



/**
 * Return the latest Tax Tips & News list
 *
 * This function retrieves the Tax Tips &
 * News list from the database and outputs
 * it as an HTML list. When the function
 * is called, it is possible to change the
 * format of the list to include/exclude a
 * summary paragraph of each article, and
 * to include/exclude a 'read more' link.
 * The title of the news list links to the
 * Tax Tips & News article.
 *
 * @function ow_tax_news_list()
 *
 * @uses $iris_db_old::get_results()
 *
 * @param (array) $var[ (string) id, (string) class, (boolean) summary, (string) readmore, (string) sep ]
 *
 * @return (string) the list of the latest Tax Tips & News articles
 *
 * @since 1.3.8 
 *
 */
function ow_tax_news_list( $var = array() ) {
	
	global $iris_db_old;
	
	$defaults = array( 'id' => '', 'class' => '', 'summary' => false, 'readmore' => '', 'sep' => '' );
	$args = wp_parse_args( $var, $defaults );
	$tax_news_list = $iris_db_old->get_results( "SELECT * FROM t_news LEFT JOIN t_news_months ON month_id = newsMonthId WHERE newsMonthCurrent = 'yes' ORDER BY inmonth_order" );
	$tax_news_items = array();
	
	foreach ( $tax_news_list as $tax_news_item ) {
		
		$news_link = '/news/tax-news/article/' . $tax_news_item->id . '/';
		$news_item = '<h2><a href="' . $news_link . '">' . $tax_news_item->title . '</a></h2>';
		
		if ( $args['summary'] == true ) {
			
			$news_item .= '<p>' . wp_trim_words( $tax_news_item->details, 55 );
		}
		if ( $args['readmore'] != '' ) {
			
			$news_item .= '<a href="' . $news_link . '" class="read-more">' . $args['readmore'] . '</a>';
		}
		if ( $args['summary'] == true ) {
			
			$news_item .= '</p>';
		}
		$tax_news_items[] = $news_item;
	}
	$id = ( $args['id'] != '' ) ? 'id="' . $args['id'] . '" ' : '';
	$class = ( $args['class'] != '' ) ? 'class="' . $args['class'] . '"' : '';
	return '<ul ' . $id . $class . '><li>' . join( $args['sep'] . '</li><li>', $tax_news_items ) . '</li></ul>';
}



function post_read_more( $more ) {
	
    return '&hellip;<a href="' . get_the_permalink() . '" rel="nofollow" class="read-more">Read More</a>';
}
add_filter( 'excerpt_more', 'post_read_more' );



/**
 * Publish/unpublish the tax tips & news
 *
 * This function will, depending on the
 * website package, publish or unpublish
 * the tax tips. This is to prevent
 * the wrong packages being able to
 * access the tax tips. For whatever
 * reason, likely the order of events
 * on the WordPress query, this function
 * wouldn't work using has_newsletter()
 * function so the website package has
 * to be coded here too.
 *
 * @function update_tax_tips_status()
 *
 * @uses wp_update_post()
 *
 * @param (string) $status the website package title
 *
 * @return N/A
 *
 * @since 1.0.0 
 *
 */
function update_tax_tips_status( $status ) {
	
	if( $status === 'Essentials' || $status === 'Select' ) {	
	
		wp_update_post( array( 'ID' => 58, 'post_status' => 'draft' ) );
	} else {
		
		wp_update_post( array( 'ID' => 58, 'post_status' => 'publish' ) );
	}
}