<?php

function wpfresh5_escape_input_txt( $text ) {
	return htmlentities( $text, ENT_QUOTES, 'UTF-8' );
}

function wpfresh5_strip_magic_slashes( $str )
{
	if( is_array( $str ) ) {
		return wpfresh5_array_map_recursive( 'wpfresh5_stripslashes', $str);
	}
	else
		return get_magic_quotes_gpc() ? wpfresh5_stripslashes( $str ) : $str;
}

function wpfresh5_stripslashes( $str ) {
	return stripslashes( $str );
}

function wpfresh5_strip_magic_slashes_all( $str )
{
	return str_replace( array("\'", '\"'), array("'", '"'), $str );
    return get_magic_quotes_gpc() ? stripslashes($str) : $str;
}

function wpfresh5_array_map_recursive(/*callable*/ $func, array $array) {
    return filter_var($array, \FILTER_CALLBACK, array( 'options' => $func ) );
}

function wpfresh5_raw_json_encode($input) {
	/*if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
	    return preg_replace_callback(
	        '/\\\\u([0-9a-zA-Z]{4})/',
	        function ($matches) {
	            return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
	        },
	        'wpfresh5_anon_encode_sup',
	        json_encode($input, JSON_HEX_APOS)
	    );
	}
	else {*/
		return preg_replace_callback(
		    '/\\\\u([0-9a-zA-Z]{4})/',
		    'wpfresh5_anon_encode',
		    json_encode($input, JSON_HEX_APOS)
		);
	// }
}

function wpfresh5_anon_encode_sup( $matches ) {
    return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
}
function wpfresh5_anon_encode( $matches ) {
    return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
}

function wpfs5_sanitize_textarea_field( $val ) {
	return $val;
}
function wpfresh5_json_decode_nice($json, $assoc = false){
    $json = str_replace("\n","\\n",$json);
    $json = str_replace("\r","",$json);
    $json = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/','$1"$3":',$json);
    $json = preg_replace('/(,)\s*}$/','}',$json);
    return json_decode($json,$assoc);
}

function wpfresh5_escape_text( $text, $do_utf = false ) {
	global $wpdb;
	return esc_sql( $text );
}

function wpfresh5_encode_quotes( $settings ) {
	$array_ret = array();
	foreach( $settings as $key => $setting ) {
		$array_ret[$key] = htmlspecialchars( $setting );
	}
	return $array_ret;
}

function wpfresh5_separate_capital ( $str )
{

	preg_match_all ( '/[A-Z][^A-Z]*/' , $str , $results );
	$result  = "";
	$results = $results[ 0 ];
	foreach ( $results as $val ) {
		$result .= $val . ' ';
	}

	return rtrim ( $result , ' ' );
}

function wpfresh5_get_array_keys_checked( $arr ) {
    if( empty( $arr ) )
        return array();
    $arr_ret = array();
    foreach( $arr as $key => $value ) {
        $arr_ret[] = $key;
    }
    return $arr_ret;
}

if ( !function_exists( 'sanitize_textarea_field' ) ) {
	function sanitize_textarea_field( $str ) {
		$filtered = wpfresh5_sanitize_text_fields( $str, true );

		/**
		 * Filters a sanitized textarea field string.
		 *
		 * @since 4.7.0
		 *
		 * @param string $filtered The sanitized string.
		 * @param string $str      The string prior to being sanitized.
		 */
		return apply_filters( 'sanitize_textarea_field', $filtered, $str );
	}

	function wpfresh5_sanitize_text_fields( $str, $keep_newlines = false ) {
		$filtered = wp_check_invalid_utf8( $str );

		if ( strpos($filtered, '<') !== false ) {
			$filtered = wp_pre_kses_less_than( $filtered );
			// This will strip extra whitespace for us.
			$filtered = wp_strip_all_tags( $filtered, false );

			// Use html entities in a special case to make sure no later
			// newline stripping stage could lead to a functional tag
			$filtered = str_replace("<\n", "&lt;\n", $filtered);
		}

		if ( ! $keep_newlines ) {
			$filtered = preg_replace( '/[\r\n\t ]+/', ' ', $filtered );
		}
		$filtered = trim( $filtered );

		$found = false;
		while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) {
			$filtered = str_replace($match[0], '', $filtered);
			$found = true;
		}

		if ( $found ) {
			// Strip out the whitespace that may now exist after removing the octets.
			$filtered = trim( preg_replace('/ +/', ' ', $filtered) );
		}

		return $filtered;
	}

}

function wpfresh5_remove_site_path( $path ) {
	$site = site_url();
	return str_replace( $site, '', $path );
}

add_filter( 'plugin_action_links_' . WPFRESH5_PLUGIN_NAME.'/'.WPFRESH5_PLUGIN_NAME.'.php', 'wpfresh5_add_plugin_action_links' );

function wpfresh5_add_plugin_action_links( $links ) {
	$autocontent_options = array(
		'Support' => '<a target="_blank" href="http://wpfreshstart5.com/support">Support</a>'
	);

	if( !wpfresh5_is_pro() ) {
		$autocontent_options = array_merge( $autocontent_options,
			array(
				'Upgrade to Pro' => '<a target="_blank" href="http://wpfreshstart5.com/v5/pro/" style="font-weight:bold;color:red">Upgrade to Pro</a>'
			)
		);
	}

	return array_merge(
		$links,
		$autocontent_options
	);
}

function wpfresh5_is_pro() {
	return true;
	return false;
}

?>