<?php

/**
 * This filter disabled XML-RPC for security purposes.
 *
 * @since 1.3.7
 */
add_filter( 'xmlrpc_enabled', '__return_false' );

/**
 * Prevent search engines from indexing the site unless it is live.
 *
 * @since 1.2.1
 */
function ow_robots_txt( $output ) {
	if ( ! ow_global_var( 'in_production' ) ) {	
		$output = "User-agent: *\r\nDisallow: /";
	}
	return $output;
}
add_filter( 'robots_txt', 'ow_robots_txt' );



// START SESSION
function start_session() {

    if ( ! session_id() ) {
        session_start();
    }
}
add_action( 'init', 'start_session', 1 );


// LIMIT POST REVISIONS TO KEEP DATABASE SMALLER
const WP_POST_REVISIONS = 3;

// TURNS OFF PHP FILE EDITOR
const DISALLOW_FILE_EDIT = true;

/**
 * Gets the customer information from the database.
 *
 * This function will get the customer
 * details (global configuration values),
 * returning them as a global array.
 *
 * @function query_db()
 *
 * @uses $wpdb::get_results()
 *
 * @return (array) the global variables in a global array
 * 
 * @since 1.0.0 
 *
 */
function query_db() {
	
	global $wpdb;
	global $ow_global_variables;
	
	$global_option = $wpdb->get_results( 'SELECT gc_option, gc_value FROM ow_global_configuration' );
	$array = [];
	
	foreach ( $global_option as $row ) {
		
		$array[$row->gc_option] = $row->gc_value;
	}
	$ow_global_variables = $array;

	return false;
}
add_action( 'plugins_loaded', 'query_db', 1 );



/**
 * Gets the customer information from the global array.
 *
 * This function will get the customer
 * details (global configuration values).
 * The function query_db() returns the
 * values as a global array so whenever
 * we run this function, it isn't
 * querying the database each time.
 *
 * @function ow_global_var()
 *
 * @uses developer_console_message()
 *
 * @param (string) $var the global variable name
 *
 * @return (string) the global variable value
 * 
 * @since 1.0.0 
 *
 */
function ow_global_var( $var ) {
	
	global $ow_global_variables;
	
	if ( is_array($ow_global_variables) && ! array_key_exists( $var, $ow_global_variables ) ) {
		
		return false;
	} else {
		
		$result = $ow_global_variables[$var];

		if ( $result != NULL ) {
			
			return $result;
		} else {
			
			developer_console_message( $var . ' is empty', 'warning' );
			return false;
		}
	}
}



/**
 * Register a new global variable.
 *
 * This function will register a key and value
 * pair to be used in the ow_global_var() function.
 *
 * @function ow_new_global_var()
 *
 * @uses ow_global_var(), developer_console_message()
 *
 * @param (string) $title the global variable key
 * @param (string) $var the global variable value
 *
 * @return N/A registers the new variable.
 * 
 * @since 1.1.0 
 *
 */
function ow_new_global_var( $title, $var ) {
	
	global $wpdb;
	
	$wpdb->query( $wpdb->prepare( 'REPLACE INTO ow_global_configuration ( gc_option, gc_value ) VALUES ( %s, %s )', $title, $var ) );
	
	$backtrace = debug_backtrace(1);
	
	$debug_file = basename( $backtrace[0]['file'] );
	$debug_line = $backtrace[0]['line'];
	
	developer_console_message( "$title: $var - This variable has been registered, you can now remove the function 'ow_new_global_var()' from $debug_file on line $debug_line.", 'alert' );
};



/**
 * Adds merge tags
 *
 * This function will swap out merge
 * tags in the main WordPress content
 *
 * @function content_merge_tags()
 *
 * @uses ow_global_var(), securemail()
 *
 * @param (string) $input the WordPress content
 *
 * @return (string) the Wordpress content after merge tags have been added.
 * 
 * @since 1.0.14 
 *
 */
function content_merge_tags( $input ) {

	$merge_tags_list = array(
		'/\%\%company\%\%/' => ow_global_var( 'accountant_name' ),
		'/\%\%tel\%\%/' 	=> ow_global_var( 'accountant_phone' ),
		'/\%\%email\%\%/'   => securemail( ow_global_var( 'accountant_email' ) ),
		'/\%\%website\%\%/' => '<a href="' . site_url() . '">' . site_url() . '</a>',
		'/\%\%area\%\%/' 	=> ow_global_var( 'accountant_main_area' ),
	);

	foreach ( $merge_tags_list as $tag => $value ) {
		
		$input = preg_replace( $tag, $value, $input );
	}
	return $input;
}
add_filter( 'the_content', 'content_merge_tags' );



/**
 * Returns the accountant address
 *
 * This function returns the accountants address
 * separated with a <br /> tag as default. The
 * address can be filtered to add the accountant
 * name at the beginning if needed.
 *
 * @function get_address()
 *
 * @uses ow_global_var(), apply_filters()
 *
 * @param (string) $sep The separator of each line in the address
 * @param (string) $alt An alternative address.
 *
 * @return (string) the accountant address enclodes in <address> tags
 *
 * @since 1.0.14 
 *
 */
function get_address( $sep = '<br />', $alt = '' ) {
	
	$address = ( $alt == '' ) ? ow_global_var( 'accountant_address' ) : $alt;
 
	$address = preg_replace( '/\s{2,}/', $sep, $address );
	
	$address = apply_filters( 'address', $address );
 
	echo '<address>' . $address . '</address>';
}



/**
 * Returns an attributes string for use in a html tag.	
 *
 * This function takes an array and returns a string of attributes.
 *
 * @function ow_array_attributes()
 *
 * @uses esc_attr()
 *
 * @param (array) $array  The attributes in an array.
 *
 * @return (string)		  The string of attributes.
 *
 * @since 1.1.6 
 *
 */
function ow_array_attributes( $array ) {
    $attributes = [];
	foreach ( $array as $key => $value ) {
		if ( is_bool( $value ) ) {
			$attributes[] = $key;
		}
		else if ( $value ) {
			$attributes[] = $key . '="' . esc_attr( $value ) . '"';
		}
	}
	return implode( ' ', $attributes );
}



/**
 * Echoes the telephone number in a link.
 *
 * This function echoes the telephone number given as a tel link.
 *
 * @function ow_the_telephone()
 *
 * @uses ow_array_attributes()
 *
 * @param (string) $number  The phone global variable.
 * @param (array)  $attr 	The attributes for the link.
 *
 * @return (string) 		The phone number in a tel link.
 *
 * @since 1.1.6 
 *
 */
function ow_the_telephone( $number, $attr = array() ) {
	$attributes = ow_array_attributes( array_merge( array(
		'href' => 'tel:' . str_replace( ' ', '', $number ),
	), $attr ) );
	return "<a {$attributes}>{$number}</a>";
}



/**
 * Returns The trading name sentence
 *
 * If the practice mname is different from
 * the full practice name, this function
 * will output a sentence e.g. XXX is a
 * trading name of YYY.
 *
 * @function company_trading_name()
 *
 * @uses ow_global_var()
 *
 * @return (string) the sentence explaining the trading name.
 *
 * @since 1.0.17 
 *
 */
function company_trading_name() {
	
	$name = ow_global_var( 'accountant_name' );
	$full_name = ow_global_var( 'accountant_full_name' );
	
	if ( $name != $full_name ) {
		
		echo $name . ' is a trading name of ' . $full_name ;
	} else {
		
		return false;
	}
}



// ADD CALL TO ACTION
function add_call_to_action( $link = '', $content = '' ) {
	
	$link = shortcode_atts( array( 'href' => '' ), $link, 'call_to_action' );
	return '<a href="' . $link['href'] . '" class="call-to-action">' . $content . '</a>';
}
add_shortcode( 'call_to_action', 'add_call_to_action' );



// REDIRECT AFTER LOGIN TO IRIS PLUGIN DASHBOARD
function ow_login_redirect( $redirect_to, $request, $user ) {
	
    return admin_url( 'admin.php?page=iris-dashboard' );
}
add_filter( 'login_redirect', 'ow_login_redirect', 10, 3 ); 



// ADD IRIS DEFAULT AVATAR
function ow_iris_avatar( $avatar ) {
	
	if ( current_user_can('administrator') ) {
		
    	$avatar = plugins_url( 'img/iris-logo-avatar.png', __FILE__ );
	}
	return $avatar;
}
add_filter( 'pre_option_avatar_default', 'ow_iris_avatar' );



function mtd_enabled() {
	
	if( ow_global_var( 'iris_mtd' ) === '1' || ! is_package_1() ) {
		
		return true;
	}
	return false;
}

function opt_newsletter() {
	
	if( ow_global_var( 'opt_newsletter' ) === '0') {
		
		return true;
	}
	return false;
}

function opt_card() {
	
	if( ow_global_var( 'opt_card' ) === '0') {
		
		return true;
	}
	return false;
}

function is_package_1() {
	
	if( ow_global_var( 'website_package' ) === PACKAGE_1 ) {
		
		return true;
	} else {
		
		return false;
	}
}


function is_package_2() {
	
	if( ow_global_var( 'website_package' ) === PACKAGE_2 ) {
		
		return true;
	} else {
		
		return false;
	}
}

function is_package_3() {
	
	if( ow_global_var( 'website_package' ) === PACKAGE_3 ) {
		
		return true;
				
	} else {
		
		return false;
	}
}

function is_package_4() {
	
	if( ow_global_var( 'website_package' ) === PACKAGE_4 ) {
		
		return true;
	} else {
		
		return false;
	}
}

function is_package_5() {
	
	if( ow_global_var( 'website_package' ) === PACKAGE_5 ) {
		
		return true;
	} else {
		
		return false;
	}
}
function is_package_6() {
	
	if( ow_global_var( 'website_package' ) === PACKAGE_6 ) {
		
		return true;
	} else {
		
		return false;
	}
}
function is_package_7() {
	
	if( ow_global_var( 'website_package' ) === PACKAGE_7 ) {
		
		return true;
	} else {
		
		return false;
	}
}
function is_package_8() {
	
	if( ow_global_var( 'website_package' ) === PACKAGE_8 ) {
		
		return true;
	} else {
		
		return false;
	}
}

function add_version() {
	
	echo "<!--\r\nCopyright © " . date( 'Y' ) . " | " . PRODUCT_NAME . " | " . PRODUCT_WEBSITE . "\r\n";
	echo "Version: " . OW_VERSION . "\r\nPackage: " . ow_global_var( 'website_package' ) . "\r\n-->\r\n";
}
add_action( 'copyright', 'add_version', 1 );






// THE MESSAGE SHOWN ON A FAILED LOGIN ATTEMPT
function wrong_login() {
	
  return 'Wrong username or password.';
}
add_filter( 'login_errors', 'wrong_login' );


// RETURNS THE LATEST UPDATE VERSION
function get_update_version() {
	
	$json = file_get_contents( 'https://cdn.iriswebportallive.co.uk/v4/plugins/ow-plugin-meta.json' );
	$obj = json_decode( $json );
	return $obj->version;
}

// COMPARES THE VERSION NUMBER TO THE AVAILABLE UPDATE
function update_available() {
	
	if ( version_compare( OW_VERSION, UPDATE_VERSION ) < 0 ) {
		
		return true;
	} else {
		
		return false;
	}
}
define( 'UPDATE_VERSION', get_update_version() );



function demo_website() {
	
	$host = $_SERVER['SERVER_NAME'];
	$templates = array(
	   'design1.iriswebportallive.co.uk',
	   'design2.iriswebportallive.co.uk',
	   'design3.iriswebportallive.co.uk',
	   'design4.iriswebportallive.co.uk',
	   'design5.iriswebportallive.co.uk',
	   'design6.iriswebportallive.co.uk',
	   'design7.iriswebportallive.co.uk',
	   'design8.iriswebportallive.co.uk',
	);
	
	if ( in_array( $host, $templates ) ) {
		
		wp_register_script( 'demo-bar', 'https://cdn.iriswebportallive.co.uk/v4/demo-site-bar/iris-demo-bar.min.js', array( 'jquery' ), null, true );
		wp_enqueue_script( 'demo-bar' );		
	}
}
add_action('wp_head','demo_website');





function add_demo_async_attribute( $tag, $handle ) {
	
    if ( 'demo-bar' !== $handle ) {
		
        return $tag;
	}
    return str_replace( ' src', ' async src', $tag );
}
add_filter('script_loader_tag', 'add_demo_async_attribute', 10, 2);

// Removes site health check widget
add_action('wp_dashboard_setup', 'remove_site_health_dashboard_widget');
function remove_site_health_dashboard_widget()
{
remove_meta_box('dashboard_site_health', 'dashboard', 'normal');
}
/*REMOVE COMMENTS - added on plugin update 1.5.2 in April 2023*/
add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;
     
    if ($pagenow === 'edit-comments.php') {
        wp_safe_redirect(admin_url());
        exit;
    }
 
    // Remove comments metabox from dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
 
    // Disable support for comments and trackbacks in post types
    foreach (get_post_types() as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
});
 
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
 
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
 
// Remove comments page in menu
add_action('admin_menu', function () {
    remove_menu_page('edit-comments.php');
});
 
// Remove comments links from admin bar
add_action('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});
/*REMOVE COMMENTS END*/