<?php
function wpfresh5_default_robots_txt() {
	$public = get_option('blog_public');

	$output = "User-agent: *\n";
	if ('0' == $public) {
	    $output .= "Disallow: /\n";
	} else {
	    $path = parse_url(site_url(), PHP_URL_PATH);
	    $output .= "Disallow: $path/wp-admin/\n";
	    $output .= "Allow: $path/wp-admin/admin-ajax.php\n";
	}

	return $output;
}


add_filter('robots_txt', 'wpfresh5_filter_robots', 10, 2 );

function wpfresh5_filter_robots( $rv, $public ) {
	$robotstxt = wpfresh5_get_settings( array( 'seo', 'general', 'robotstxt' ) );
	if( !$robotstxt )
		return $rv;

	$content = wpfresh5_get_settings( array( 'seo', 'general', 'robotstxt_txt' ) );
	if ( $content ) {
	    $rv = esc_attr( strip_tags( $content ) );
	}

	return $rv;
}


/* function to create sitemap.xml file in root directory of site  */
add_action( "save_post", "wpfresh5_create_sitemap" );
function wpfresh5_create_sitemap() {
	$sitemap_xml = wpfresh5_get_settings( array( 'seo', 'general', 'sitemap_xml' ) );
	if( !$sitemap_xml )
		return;
    if ( str_replace( '-', '', get_option( 'gmt_offset' ) ) < 10 ) { 
        $tempo = '-0' . str_replace( '-', '', get_option( 'gmt_offset' ) ); 
    } else { 
        $tempo = get_option( 'gmt_offset' ); 
    }
    if( strlen( $tempo ) == 3 ) { $tempo = $tempo . ':00'; }
    $postsForSitemap = get_posts( array(
        'numberposts' => -1,
        'orderby'     => 'modified',
        'post_type'   => array( 'post', 'page' ),
        'order'       => 'DESC'
    ) );
    $sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . '<?xml-stylesheet type="text/xsl" href="' . 
        esc_url( WPFRESH5_URL.'/lib/seo/' ) . 'xml-sitemap.xsl"?>';
    $sitemap .= "\n" . '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
    $sitemap .= "\t" . '<url>' . "\n" .
        "\t\t" . '<loc>' . esc_url( home_url( '/' ) ) . '</loc>' .
        "\n\t\t" . '<lastmod>' . date( "Y-m-d\TH:i:s", current_time( 'timestamp', 0 ) ) . $tempo . '</lastmod>' .
        "\n\t\t" . '<changefreq>daily</changefreq>' .
        "\n\t\t" . '<priority>1.0</priority>' .
        "\n\t" . '</url>' . "\n";
    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post);
        $postdate = explode( " ", $post->post_modified );
        $sitemap .= "\t" . '<url>' . "\n" .
            "\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' .
            "\n\t\t" . '<lastmod>' . $postdate[0] . 'T' . $postdate[1] . $tempo . '</lastmod>' .
            "\n\t\t" . '<changefreq>weekly</changefreq>' .
            "\n\t\t" . '<priority>0.5</priority>' .
            "\n\t" . '</url>' . "\n";
    }
    $sitemap .= '</urlset>';
    $fp = fopen( ABSPATH . "sitemaps.xml", 'w' );
    fwrite( $fp, $sitemap );
    fclose( $fp );
}

add_action( 'init', 'wpfresh5_check_seo' );

function wpfresh5_check_seo() {
    if( wpfresh5_get_settings( array( 'seo', 'general', 'f0ftohome' ) ) )
        add_action('template_redirect', 'wpfresh5_tohome' );
}

function wpfresh5_tohome(){
    if(is_404()){
        wp_redirect( home_url(), 301 );
    }
}

/* Local SEO */
function wpfresh5_lseo_btypes() {
    return array(
    array( 'value' => 'AccountingService', 'label' => 'Accounting Service' ),
    array( 'value' => 'AdultEntertainment', 'label' => 'Adult Entertainment' ),
    array( 'value' => 'AmusementPark', 'label' => 'Amusement Park' ),
    array( 'value' => 'AnimalShelter', 'label' => 'Animal Shelter' ),
    array( 'value' => 'ArtGallery', 'label' => 'Art Gallery' ),
    array( 'value' => 'Attorney', 'label' => 'Attorney' ),
    array( 'value' => 'AutoBodyShop', 'label' => 'Auto Body Shop' ),
    array( 'value' => 'AutoDealer', 'label' => 'Auto Dealer' ),
    array( 'value' => 'AutomatedTeller', 'label' => 'Automated Teller' ),
    array( 'value' => 'AutomotiveBusiness', 'label' => 'Automotive Business' ),
    array( 'value' => 'AutoPartsStore', 'label' => 'Auto Parts Store' ),
    array( 'value' => 'AutoRental', 'label' => 'Auto Rental' ),
    array( 'value' => 'AutoRepair', 'label' => 'Auto Repair' ),
    array( 'value' => 'AutoWash', 'label' => 'Auto Wash' ),
    array( 'value' => 'Bakery', 'label' => 'Bakery' ),
    array( 'value' => 'BankOrCreditUnion', 'label' => 'Bank Or Credit Union' ),
    array( 'value' => 'BarOrPub', 'label' => 'Bar Or Pub' ),
    array( 'value' => 'BeautySalon', 'label' => 'Beauty Salon' ),
    array( 'value' => 'BedAndBreakfast', 'label' => 'Bed And Breakfast' ),
    array( 'value' => 'BikeStore', 'label' => 'Bike Store' ),
    array( 'value' => 'BookStore', 'label' => 'Book Store' ),
    array( 'value' => 'BowlingAlley', 'label' => 'Bowling Alley' ),
    array( 'value' => 'Brewery', 'label' => 'Brewery' ),
    array( 'value' => 'CafeOrCoffee Shop', 'label' => 'Cafe Or CoffeeShop' ),
    array( 'value' => 'Casino', 'label' => 'Casino' ),
    array( 'value' => 'ChildCare', 'label' => 'Child Care' ),
    array( 'value' => 'ClothingStore', 'label' => 'Clothing Store' ),
    array( 'value' => 'ComedyClub', 'label' => 'Comedy Club' ),
    array( 'value' => 'ComputerStore', 'label' => 'Computer Store' ),
    array( 'value' => 'ConvenienceStore', 'label' => 'Convenience Store' ),
    array( 'value' => 'DaySpa', 'label' => 'Day Spa' ),
    array( 'value' => 'Dentist', 'label' => 'Dentist' ),
    array( 'value' => 'DepartmentStore', 'label' => 'Department Store' ),
    array( 'value' => 'DiagnosticLab', 'label' => 'Diagnostic Lab' ),
    array( 'value' => 'DryCleaningOrLaundry', 'label' => 'Dry Cleaning Or Laundry' ),
    array( 'value' => 'Electrician', 'label' => 'Electrician' ),
    array( 'value' => 'ElectronicsStore', 'label' => 'Electronics Store' ),
    array( 'value' => 'EmergencyService', 'label' => 'EmergencyService' ),
    array( 'value' => 'EmploymentAgency', 'label' => 'Employment Agency' ),
    array( 'value' => 'EntertainmentBusiness', 'label' => 'Entertainment Business' ),
    array( 'value' => 'ExerciseGym', 'label' => 'Exercise Gym' ),
    array( 'value' => 'FastFoodRestaurant', 'label' => 'Fast Food Restaurant' ),
    array( 'value' => 'FireStation', 'label' => 'Fire Station' ),
    array( 'value' => 'FinancialService', 'label' => 'Financial Service' ),
    array( 'value' => 'Florist', 'label' => 'Florist' ),
    array( 'value' => 'FoodEstablishment', 'label' => 'Food Establishment' ),
    array( 'value' => 'FurnitureStore', 'label' => 'Furniture Store' ),
    array( 'value' => 'GardenStore', 'label' => 'Garden Store' ),
    array( 'value' => 'GasStation', 'label' => 'Gas Station' ),
    array( 'value' => 'GeneralContractor', 'label' => 'General Contractor' ),
    array( 'value' => 'GolfCourse', 'label' => 'Golf Course' ),
    array( 'value' => 'GroceryStore', 'label' => 'Grocery Store' ),
    array( 'value' => 'GovernmentOffice', 'label' => 'Government Office' ),
    array( 'value' => 'HairSalon', 'label' => 'Hair Salon' ),
    array( 'value' => 'HardwareStore', 'label' => 'Hardware Store' ),
    array( 'value' => 'HealthAndBeautyBusiness', 'label' => 'Health And Beauty Business' ),
    array( 'value' => 'HealthClub', 'label' => 'Health Club' ),
    array( 'value' => 'HobbyShop', 'label' => 'HobbyShop' ),
    array( 'value' => 'HomeAndConstructionBusiness', 'label' => 'Home and Construction Business' ),
    array( 'value' => 'HomeGoodsStore', 'label' => 'Home Goods Store' ),
    array( 'value' => 'Hospital', 'label' => 'Hospital' ),
    array( 'value' => 'Hostel', 'label' => 'Hostel' ),
    array( 'value' => 'Hotel', 'label' => 'Hotel' ),
    array( 'value' => 'HousePainter', 'label' => 'House Painter' ),
    array( 'value' => 'HVACBusiness', 'label' => 'HVAC Business' ),
    array( 'value' => 'IceCreamShop', 'label' => 'Ice Cream Shop' ),
    array( 'value' => 'InsuranceAgency', 'label' => 'Insurance Agency' ),
    array( 'value' => 'InternetCafe', 'label' => 'Internet Cafe' ),
    array( 'value' => 'JewelryStore', 'label' => 'JewelryStore' ),
    array( 'value' => 'Library', 'label' => 'Library' ),
    array( 'value' => 'LiquorStore', 'label' => 'Liquor Store' ),
    array( 'value' => 'Locksmith', 'label' => 'Locksmith' ),
    array( 'value' => 'LodgingBusiness', 'label' => 'Lodging Business' ),
    array( 'value' => 'MedicalClinic', 'label' => 'Medical Clinic' ),
    array( 'value' => 'MedicalOrganization', 'label' => 'Medical Organization' ),
    array( 'value' => 'MensClothingStore', 'label' => 'Mens Clothing Store' ),
    array( 'value' => 'MobilePhoneStore', 'label' => 'Mobile Phone Store' ),
    array( 'value' => 'Motel', 'label' => 'Motel' ),
    array( 'value' => 'MotorcycleDealer', 'label' => 'Motorcycle Dealer' ),
    array( 'value' => 'MotorcycleRepair', 'label' => 'Motorcycle Repair' ),
    array( 'value' => 'MovieRentalStore', 'label' => 'Movie Rental Store' ),
    array( 'value' => 'MovieTheater', 'label' => 'Movie Theater' ),
    array( 'value' => 'MovingCompany', 'label' => 'Moving Company' ),
    array( 'value' => 'MusicStore', 'label' => 'Music Store' ),
    array( 'value' => 'NailSalon', 'label' => 'Nail Salon' ),
    array( 'value' => 'NightClub', 'label' => 'Night Club' ),
    array( 'value' => 'Notary', 'label' => 'Notary' ),
    array( 'value' => 'OfficeEquipmentStore', 'label' => 'Office Equipment Store' ),
    array( 'value' => 'Optician', 'label' => 'Optician' ),
    array( 'value' => 'OutletStore', 'label' => 'Outlet Store' ),
    array( 'value' => 'PawnShop', 'label' => 'Pawn Shop' ),
    array( 'value' => 'PetStore', 'label' => 'Pet Store' ),
    array( 'value' => 'Pharmacy', 'label' => 'Pharmacy' ),
    array( 'value' => 'Physician', 'label' => 'Physician' ),
    array( 'value' => 'Plumber', 'label' => 'Plumber' ),
    array( 'value' => 'PoliceStation', 'label' => 'Police Station' ),
    array( 'value' => 'PostOffice', 'label' => 'Post Office' ),
    array( 'value' => 'ProfessionalService', 'label' => 'Professional Service' ),
    array( 'value' => 'PublicSwimmingPool', 'label' => 'Public Swimming Pool' ),
    array( 'value' => 'RadioStation', 'label' => 'Radio Station' ),
    array( 'value' => 'RealEstateAgent', 'label' => 'Real Estate Agent' ),
    array( 'value' => 'RecyclingCenter', 'label' => 'Recycling Center' ),
    array( 'value' => 'Restaurant', 'label' => 'Restaurant' ),
    array( 'value' => 'RoofingContractor', 'label' => 'Roofing Contractor' ),
    array( 'value' => 'SelfStorage', 'label' => 'Self Storage' ),
    array( 'value' => 'ShoeStore', 'label' => 'Shoe Store' ),
    array( 'value' => 'ShoppingCenter', 'label' => 'Shopping Center' ),
    array( 'value' => 'SkiResort', 'label' => 'Ski Resort' ),
    array( 'value' => 'SportsActivityLocation', 'label' => 'Sports Activity Location' ),
    array( 'value' => 'SportingGoodsStore', 'label' => 'Sporting Goods Store' ),
    array( 'value' => 'SportsClub', 'label' => 'Sports Club' ),
    array( 'value' => 'Store', 'label' => 'Store' ),
    array( 'value' => 'StadiumOrArena', 'label' => 'Stadium Or Arena' ),
    array( 'value' => 'TattooParlor', 'label' => 'Tattoo Parlor' ),
    array( 'value' => 'TelevisionStation', 'label' => 'Television Station' ),
    array( 'value' => 'TennisComplex', 'label' => 'Tennis Complex' ),
    array( 'value' => 'TireShop', 'label' => 'Tire Shop' ),
    array( 'value' => 'TouristInformationCenter', 'label' => 'Tourist Information Center' ),
    array( 'value' => 'ToyStore', 'label' => 'Toy Store' ),
    array( 'value' => 'TravelAgency', 'label' => 'Travel Agency' ),
    array( 'value' => 'VeterinaryCare', 'label' => 'Veterinary Care' ),
    array( 'value' => 'WholesaleStore', 'label' => 'Wholesale Store' ),
    array( 'value' => 'Winery', 'label' => 'Winery' ),
    );
}

/* 
    'localseo'
        'business'
        'contact'
        'rating'

*/

        /*$settings = wpfresh5_get_plugin_settings();
        $settings = isset( $settings['seo']['local'] ) ? $settings['seo']['local'] : false;
        $enable_business = isset( $settings['enable_business'] ) ? intval( $settings['enable_business'] ) : 0;
        $enable_rating = isset( $settings['enable_rating'] ) ? intval( $settings['enable_rating'] ) : 0;
        $info = isset( $settings['info'] ) ? $settings['info'] : 0;
        $info_business = !empty( $info ) && isset( $info['business'] ) && !empty( $info['business' ] ) ? $info['business'] : array();
        $info_contact = !empty( $info ) && isset( $info['contact'] ) && !empty( $info['contact' ] ) ? $info['contact'] : array();
        $info_rating = !empty( $info ) && isset( $info['rating'] ) && !empty( $info['rating' ] ) ? $info['rating'] : array();
        $btype = isset( $info['business']['type'] ) ? $info['business']['type'] : 0;

        $business_name = wpfresh5_array_option( $info_business, 'name' );*/
        /*
         * Class for displaying the local business information
         */

        if (!class_exists('WPFS4LocalBusiness')) {

            class WPFS4LocalBusiness
            {

                public $name;
                public $settings;
                public function __construct()
                {

                    $this->name = 'WPFS4LocalBusiness';
                    $settings = wpfresh5_get_plugin_settings();
                    $this->settings = isset( $settings['seo']['local'] ) ? $settings['seo']['local'] : false;
                    $this->settings_info = isset( $this->settings['info'] ) ? $this->settings['info'] : 0;
                    $this->settings_contact = isset( $this->settings['contact'] ) ? $this->settings['contact'] : 0;
                    $this->enable_business = isset( $this->settings['enable_business'] ) ? intval( $this->settings['enable_business'] ) : 0;
                    $this->enable_rating = isset( $this->settings['enable_rating'] ) ? intval( $this->settings['enable_rating'] ) : 0;
                    $this->settings_rating = isset(  $this->settings_info['rating'] ) ?  $this->settings_info['rating'] : 0;
                }

                /** function/getParent
                 * Usage: create tables if not exist and activates the plugin
                 * Arg(0): null
                 * Return: if found return parent type of business
                 */
                public function getParent()
                {
        // $settings = isset( $settings['seo']['local'] ) ? $settings['seo']['local'] : false;

                    $parent = wpfresh5_array_option( $this->settings_info, 'type' ) /*get_option('mdp_type')*/;
                    $array = array(
                        'AutoBodyShop:AutomotiveBusiness',
                        'AutoDealer:AutomotiveBusiness',
                        'AutoPartsStore:AutomotiveBusiness',
                        'AutoRental:AutomotiveBusiness',
                        'AutoRepair:AutomotiveBusiness',
                        'AutoWash:AutomotiveBusiness',
                        'GasStation:AutomotiveBusiness',
                        'MotorcycleDealer:AutomotiveBusiness',
                        'MotorcycleRepair:AutomotiveBusiness',
                        'FireStation:EmergencyService',
                        'Hospital:EmergencyService',
                        'PoliceStation:EmergencyService',
                        'AdultEntertainment:EntertainmentBusiness',
                        'AmusementPark:EntertainmentBusiness',
                        'ArtGallery:EntertainmentBusiness',
                        'Casino:EntertainmentBusiness',
                        'ComedyClub:EntertainmentBusiness',
                        'MovieTheater:EntertainmentBusiness',
                        'NightClub:EntertainmentBusiness',
                        'FinancialService:FinancialService',
                        'AccountingService:FinancialService',
                        'AutomatedTeller:FinancialService',
                        'BankOrCreditUnion:FinancialService',
                        'InsuranceAgency:FinancialService',
                        'FoodEstablishment:FoodEstablishment',
                        'Bakery:FoodEstablishment',
                        'BarOrPub:FoodEstablishment',
                        'Brewery:FoodEstablishment',
                        'CafeOrCoffeeShop:FoodEstablishment',
                        'FastFoodRestaurant:FoodEstablishment',
                        'IceCreamShop:FoodEstablishment',
                        'Restaurant:FoodEstablishment',
                        'Winery:FoodEstablishment',
                        'PostOffice:GovernmentOffice',
                        'BeautySalon:HealthAndBeautyBusiness',
                        'DaySpa:HealthAndBeautyBusiness',
                        'HairSalon:HealthAndBeautyBusiness',
                        'HealthClub:HealthAndBeautyBusiness',
                        'NailSalon:HealthAndBeautyBusiness',
                        'TattooParlor:HealthAndBeautyBusiness',
                        'HomeAndConstructionBusiness:HomeAndConstructionBusiness',
                        'Electrician:HomeAndConstructionBusiness',
                        'GeneralContractor:HomeAndConstructionBusiness',
                        'HVACBusiness:HomeAndConstructionBusiness',
                        'HousePainter:HomeAndConstructionBusiness',
                        'Locksmith:HomeAndConstructionBusiness',
                        'MovingCompany:HomeAndConstructionBusiness',
                        'Plumber:HomeAndConstructionBusiness',
                        'RoofingContractor:HomeAndConstructionBusiness',
                        'BedAndBreakfast:LodgingBusiness',
                        'Hostel:LodgingBusiness',
                        'Hotel:LodgingBusiness',
                        'Motel:LodgingBusiness',
                        'Dentist:MedicalOrganization',
                        'DiagnosticLab:MedicalOrganization',
                        'Hospital:MedicalOrganization',
                        'MedicalClinic:MedicalOrganization',
                        'Optician:MedicalOrganization',
                        'Pharmacy:MedicalOrganization',
                        'Physician:MedicalOrganization',
                        'VeterinaryCare:MedicalOrganization',
                        'AccountingService:ProfessionalService',
                        'Attorney:ProfessionalService',
                        'Dentist:ProfessionalService',
                        'Electrician:ProfessionalService',
                        'GeneralContractor:ProfessionalService',
                        'HousePainter:ProfessionalService',
                        'Locksmith:ProfessionalService',
                        'Notary:ProfessionalService',
                        'Plumber:ProfessionalService',
                        'RoofingContractor:ProfessionalService',
                        'BowlingAlley:SportsActivityLocation',
                        'ExerciseGym:SportsActivityLocation',
                        'GolfCourse:SportsActivityLocation',
                        'HealthClub:SportsActivityLocation',
                        'PublicSwimmingPool:SportsActivityLocation',
                        'SkiResort:SportsActivityLocation',
                        'SportsClub:SportsActivityLocation',
                        'StadiumOrArena:SportsActivityLocation',
                        'TennisComplex:SportsActivityLocation',
                        'AutoPartsStore:Store',
                        'BikeStore:Store',
                        'BookStore:Store',
                        'ClothingStore:Store',
                        'ComputerStore:Store',
                        'ConvenienceStore:Store',
                        'DepartmentStore:Store',
                        'ElectronicsStore:Store',
                        'Florist:Store',
                        'FurnitureStore:Store',
                        'GardenStore:Store',
                        'GroceryStore:Store',
                        'HardwareStore:Store',
                        'HobbyShop:Store',
                        'HomeGoodsStore:Store',
                        'JewelryStore:Store',
                        'LiquorStore:Store',
                        'MensClothingStore:Store',
                        'MobilePhoneStore:Store',
                        'MovieRentalStore:Store',
                        'MusicStore:Store',
                        'OfficeEquipmentStore:Store',
                        'OutletStore:Store',
                        'PawnShop:Store',
                        'PetStore:Store',
                        'ShoeStore:Store',
                        'SportingGoodsStore:Store',
                        'TireShop:Store',
                        'ToyStore:Store',
                        'WholesaleStore:Store'
                    );
                    $set = false;
                    foreach ($array as $var) {

                        $val = explode(':' , $var);

                        switch ($val[0]) {

                            case $parent:
                                $set = $val[1];
                                break;
                        }

                    }

                    if ($set) {
                        return $set;
                    } else {
                        return false;
                    }

                }

                /** function/localBusiness
                 * Usage: creates localBusiness schema and outputs information
                 * Arg(0): null
                 * Return: void
                 */
                public function showLocalBusiness()
                {

                    $mdp_info = '<!--begin WP Freshstart 4 Local SEO '.WPFRESH5_VERSION.' -->';

                    if( $this->enable_business ) {
                        $mdp_info .= '<span itemscope itemtype="http://schema.org/LocalBusiness">';

                        if (self::getParent())
                        {
                            $mdp_info .= '<span itemscope itemtype="http://schema.org/' . self::getParent() . '">';
                        }

                        // $btype = isset( $this->settings_info['business']['type'] ) ? $info['business']['type'] : 0;

                        if ( $business_type = wpfresh5_array_option( $this->settings_info, 'type' ) /*get_option('mdp_type')*/)
                        {
                            $mdp_info .= '<span itemscope itemtype="http://schema.org/' . $business_type . '">';
                        }

                        /* name of business and url */
                        if ( $business_name = wpfresh5_array_option( $this->settings_info, 'name' ) )
                        {

                            /*if (get_option('mdp_url'))
                            {
                                $mdp_info .= '<link itemprop="url" href="' . get_option('mdp_url') . '"><meta itemprop="name" content="' . get_option('mdp_name') . '"/>';
                            }
                            else
                            {*/
                                $mdp_info .= '<link itemprop="url" href="' . get_bloginfo('url') . '"><meta itemprop="name" content="' . $business_name . '"/>';
                            //}

                        } else {
                                $mdp_info .= '<link itemprop="url" href="' . get_bloginfo('url') . '" ><meta itemprop="name" content="' . get_bloginfo('name') . '" />';
                        }

                        /* description of business */
                        if ( $business_description = wpfresh5_array_option( $this->settings_info, 'description' ) )
                        {
                            $mdp_info .= '<meta itemprop="description" content="' . $business_description . '" />';
                        } else {
                            $mdp_info .= '<meta itemprop="description" content="' . get_bloginfo('description') . '" />';
                        }

                        /* logo of business */
                        if ( $business_logo = wpfresh5_array_option( $this->settings_info, 'logo' ) )
                        {
                            $mdp_info .= '<meta itemprop="image" content="' . $business_logo . '" />';
                        }

                        $mdp_info .= '<span itemscope itemtype="http://schema.org/Place">';
                        /* physical address */
                        $mdp_info .= self::PostalAddress();

                        /* geo location */
                        //if (get_option('mdp_geo_location')) {

                        $mdp_info .= self::geoLocation();

                    }
                    else {
                        $mdp_info .= '<span itemscope itemtype="http://schema.org/Place">';
                    }

                    if( $this->enable_rating )
                        $mdp_info .= self::aggregateRating();

                    $mdp_info .= '</span>';
                    if( $this->enable_business ) {
                        /* type of payments visa, master card, check, cash */
                        if ( $business_payments = wpfresh5_array_option( $this->settings_info, 'payments' ) )
                        {
                            $mdp_info .= '<meta itemprop="paymentAccepted" content="' . $business_payments . '" />';
                        }

                        /* price range of items*/
                        if ( $business_pricerange = wpfresh5_array_option( $this->settings_info, 'pricerange' ) )
                        {
                            $mdp_info .= '<meta itemprop="priceRange" content="' . $business_pricerange . '" />';
                        }


                        /* opening hours of business */
                        if (/*(get_option('mdp_dow')) && (get_option('mdp_open')) && (get_option('mdp_close'))*/
                            $business_days = wpfresh5_array_option( $this->settings_contact, 'days' ) &&
                            $business_open = wpfresh5_array_option( $this->settings_contact, 'open' ) &&
                            $business_close = wpfresh5_array_option( $this->settings_contact, 'close' )
                        )
                        {
                            $mdp_info .= '<meta itemprop="openingHours" content="' . $business_days . ' ' . $business_open . '-' . $business_close . '" />';
                        }
                    }
                    /*if (get_option('mdp_employee_role'))
                    {
                        $mdp_info .= self::getEmployee();
                    }

                    if (get_option('mdp_founder_role'))
                    {
                        $mdp_info .= self::getFounder();
                    }*/

                    /* reviews */
                    if ( 0 && $this->enable_rating )
                    {
                        // $mdp_info .= self::getReviews();
                    }

                    /* member of Microdata Project */
                    /*if ((get_option('mdp_member')) || ((get_option('mdp_member_url')) && (get_option('mdp_member_name'))))
                    {
                        $mdp_info .= self::member();
                    }*/

                    /* seeks SEO */
                    /*if ((get_option('mdp_seeks')) || ((get_option('mdp_seeks_url')) && (get_option('mdp_seeks_name'))))
                    {
                        $mdp_info .= self::seeks();
                    }*/

                    if (self::getParent())
                    {
                        $mdp_info .= '</span>';
                    }

                    if (get_option('mdp_type'))
                    {
                        $mdp_info .= '</span>';
                    }

                    $mdp_info .= '</span>';

                    $mdp_info .= '<!--end WP Freshstart 4 Local SEO '.WPFRESH5_VERSION.' -->';


                    echo str_replace("\t" , " " , str_replace(array("\r" , "\n") , "" , str_replace(" " , " " , $mdp_info)));

                }

                /** function/postalAddress
                 * Usage: create postal address schema
                 * Arg(0): null
                 * Return: postal address schema
                 */
                private function PostalAddress()
                {

                    $error = 0;
                    $mdp_info = "";

                    $mdp_info .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';

                    /* street address */
                    if ( $business_street = wpfresh5_array_option( $this->settings_info, 'street' ) )
                    {
                        $mdp_info .= '<meta itemprop="streetAddress" content="' . $business_street . '" />';
                    }

                    /* city */
                    if ( $business_city = wpfresh5_array_option( $this->settings_info, 'city' ) )
                    {
                        $mdp_info .= '<meta itemprop="addressLocality" content="' . $business_city . '" />';
                    }
                    else
                    {
                        $error = 1;
                    }

                    /* state */
                    if ( $business_state = wpfresh5_array_option( $this->settings_info, 'state' ) )
                    {
                        $mdp_info .= '<meta itemprop="addressRegion" content="' . $business_state . '" />';
                    }
                    else
                    {
                        $error = 1;
                    }

                    /* zip code */
                    if ( $business_zip = wpfresh5_array_option( $this->settings_info, 'zip' ) )
                    {
                        $mdp_info .= '<meta itemprop="postalCode" content="' . $business_zip . '" />';
                    }

                    /* county is USA */
                    if ( $business_country = wpfresh5_array_option( $this->settings_info, 'country' ) )
                    {
                        $mdp_info .= '<meta itemprop="addressCountry" content="' . $business_country . '" />';
                    }

                    /* email */
                    if ( $contact_email = wpfresh5_array_option( $this->settings_contact, 'email' ) )
                    {
                        $mdp_info .= '<meta itemprop="email" content="' . $contact_email . '" />';
                    }

                    /* telephone */
                    if ( $contact_phone = wpfresh5_array_option( $this->settings_contact, 'phone' ) )
                    {
                        $mdp_info .= '<meta itemprop="telephone" content="' . $contact_phone . '" />';
                    }

                    /* fax */
                    if ( $contact_fax = wpfresh5_array_option( $this->settings_contact, 'fax' ) )
                    {
                        $mdp_info .= '<meta itemprop="faxNumber" content="' . $contact_fax . '" />';
                    }


                    $mdp_info .= '</span>';

                    if ($error == 0)
                    {
                        return $mdp_info;
                    }

                }


                /** function/aggregateRating
                 * Usage: creates aggregate rating for schema
                 * Arg(0): null
                 * Return: aggregate rating schema
                 */
                private function aggregateRating()
                {
// return 'rmi353;';
                    $error = 0;
                    $mdp_info = "";

// var_dump($this->settings_rating);
                    $mdp_info .= '<span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
                    /* total value i.e 1-5 should be 5 */
                    if ( $rating_value = wpfresh5_array_option( $this->settings_rating, 'rvalue' ) )
                    {
                        $mdp_info .= '<meta itemprop="ratingValue" content="' . $rating_value . '" />';
                    }
                    else
                    {
                        $error = 1;
                    }

                    /* best rating */
                    if ( $rating_best = wpfresh5_array_option( $this->settings_rating, 'best' ) )
                    {
                        $mdp_info .= '<meta itemprop="bestRating" content="' . $rating_best . '" />';
                    }
                    else
                    {
                        $error = 1;
                    }

                    /* lowest rating */
                    if ( $rating_worst = wpfresh5_array_option( $this->settings_rating, 'worst' ) )
                    {
                        $mdp_info .= '<meta itemprop="worstRating" content="' . $rating_worst . '" />';
                    }
                    else
                    {
                        $error = 1;
                    }

                    /* how many votes, count */
                    if ( $rating_count = wpfresh5_array_option( $this->settings_rating, 'rcount' ) )
                    {
                        $mdp_info .= '<meta itemprop="ratingCount" content="' . $rating_count . '" />';
                    }
                    else
                    {
                        $error = 1;
                    }

                    $mdp_info .= '</span>';
                    if ($error == 0)
                    {
                        return $mdp_info;
                    }
                    return '';
                }

                /** function/seeks
                 * Usage: creates seeks for schema
                 * Arg(0): null
                 * Return: seeks schema
                 */
                private function seeks()
                {
                    return '';
                    $mdp_info = "";

                    $mdp_info .= '<span itemprop="seeks" itemscope itemtype="http://schema.org/Demand" >';

                    $mdp_info .= '<link itemprop="url" href="' . get_option('mdp_seeks_url') . '" ><meta itemprop="name" content="' . get_option('mdp_seeks_name') . '" />';

                    $mdp_info .= '<meta itemprop="description" content="' . get_option('mdp_seeks_description') . '" />';

                    $mdp_info .= '</span>';


                    return $mdp_info;

                }



                /** function/member
                 * Usage: creates member for schema
                 * Arg(0): null
                 * Return: member schema
                 */
                private function member()
                {
                    return '';
                    $mdp_info = "";

                    $mdp_info .= '<span itemprop="member" itemscope itemtype="http://schema.org/Organization">';

                    $mdp_info .= '<link itemprop="url" href="' . get_option('mdp_member_url') . '"><meta itemprop="name" content="' . get_option('mdp_member_name') . '">';

                    $mdp_info .= '<meta itemprop="description" content="' . get_option('mdp_member_description') . '" />';

                    $mdp_info .= '</span>';


                    return $mdp_info;
                }

                /** function/geoLocation
                 * Usage: creates geo location for schema
                 * Arg(0): null
                 * Return: geo location schema
                 */
                private function geoLocation()
                {

                    if ((get_option('wpf5_lseo_latitude')) == "" || (get_option('wpf5_lseo_longitude')) == ""
                        || (preg_match
                        (
                            '#[0-9]{2}\.[0-9]{2}#' , get_option
                                                   (
                                                       'wpf5_lseo_longitude'
                                                   )
                        ))
                    ) {
                        self::getGeoLocation();
                    }

                    $mdp_info = "";

                    $mdp_info .= '<span itemprop="geo" itemscope itemtype="http://schema.org/GeoCoordinates">';

                    $mdp_info .= '<meta itemprop="latitude" content="' . get_option('wpf5_lseo_latitude') . '" />';

                    $mdp_info .= '<meta itemprop="longitude" content="' . get_option('wpf5_lseo_longitude') . '" />';

                    $mdp_info .= '</span>';

                    return $mdp_info;

                }

                /** function/getEmployee
                 * Usage: creates employees for schema
                 * Arg(0): null
                 * Return: employees schema
                 */
                private function getEmployee()
                {

                    $mdp_info = "";

                    $employees = get_users('role=' . get_option('mdp_employee_role') . '&all_user_meta');

                    $mdp_info .= '<span itemprop="employees" itemscope itemtype="http://schema.org/employees">';

                    foreach ($employees as $employee)
                    {

                        if ($employee->display_name)
                        {

                            $employee_name = $employee->display_name;

                        }
                        else
                        {

                            $employee_name = $employee->user_nicename;
                        }

                        $mdp_info .= '<span itemprop="employee" itemscope itemtype="http://schema.org/Person">';


                        $mdp_info .= '<link itemprop="url" href="http://microdataproject.org/author/' . $employee->user_login . '/"><meta itemprop="name" content="' . $employee_name . '"/>';

                        /* email */
                        if ($employee->user_email)
                        {
                            $mdp_info .= '<meta itemprop="email" content="' . $employee->user_email . '" />';
                        }

                        if ($employee->description)
                        {

                            $mdp_info .= '<meta itemprop="description" content="' . strip_tags($employee->description). '" />';
                        }

                        $mdp_info .= '</span>';
                    }

                    $mdp_info .= '</span>';

                    return $mdp_info;

                }

                /** function/getFounder
                 * Usage: creates founders for schema
                 * Arg(0): null
                 * Return: founders schema
                 */
                private function getFounder()
                {

                    $mdp_info = "";

                    $founders = get_users('role=' . get_option('mdp_founder_role') . '&all_user_meta');

                    $mdp_info .= '<span itemprop="founders" itemscope itemtype="http://schema.org/founders">';

                    foreach ($founders as $founder)
                    {

                        if ($founder->display_name)
                        {

                            $founder_name = $founder->display_name;

                        }
                        else
                        {

                            $founder_name = $founder->user_nicename;
                        }

                        $mdp_info .= '<span itemprop="founder"  itemscope itemtype="http://schema.org/Person">';


                        $mdp_info .= '<link itemprop="url" href="http://microdataproject.org/author/' . $founder->user_login . '/" title="' . $founder_name . '"><meta itemprop="name" content="' . $founder_name . '"/>';

                        /* email */
                        if ($founder->user_email)
                        {
                            $mdp_info .= '<meta itemprop="email" content="' . $founder->user_email . '" />';
                        }

                        if ($founder->description)
                        {
                            $mdp_info .= '<meta itemprop="description" content="' . strip_tags($founder->description). '" />';
                        }

                        $mdp_info .= '</span>';
                    }

                    $mdp_info .= '</span>';

                    return $mdp_info;

                }

                public function getReviews()
                {

                    global $wpdb;

                    $pid = get_the_ID();
                    $post = get_post($pid);
                    $table_name = $wpdb->prefix . "mdp_reviews";

                    $sql = "SELECT * FROM $table_name WHERE pid = '$post->ID'";
                    $row = $wpdb->get_results($sql);

                    if (!$row)
                    {

                        $mdpInfo = new mdpInfo();
                        $mdpInfo->genReveiw($pid);

                    }

                    if (get_option('mdp_keyword'))
                    {
                        $mdp_keyword = get_option('mdp_keyword');
                    }
                    else
                    {
                        $mdp_keyword = get_bloginfo('name');
                    }
                    $review_body = str_replace('[keyword]' , $mdp_keyword , $row[0]->review_body);

                    if (get_option('mdp_type'))
                    {
                        $type = get_option('mdp_type');
                    }
                    else
                    {
                        $type = 'LocalBusiness';
                    }

                    $mdp_info = "";

                    $mdp_info .= '<span itemprop="reviews" itemscope itemtype="http://schema.org/reviews">';

                    $mdp_info .= '<span itemscope itemtype="http://schema.org/WebPage">';

                    $mdp_info .= '<link itemprop="url" href="' . get_bloginfo('url') . $_SERVER["REQUEST_URI"] . '"><meta itemprop="name" content="' . $post->post_title . '" />';

                    $mdp_info .= '<span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';

                    $mdp_info .= '<meta itemprop="ratingValue" content="5" />';

                    $mdp_info .= '<meta itemprop="reviewCount" content="' . $row[0]->id . '" />';

                    $mdp_info .= '</span>';

                    $mdp_info .= '<span itemscope itemtype="http://schema.org/Review">';

                    $mdp_info .= '<span itemprop="itemReviewed" itemscope itemtype="http://schema.org/' . $type . '">';

                    $mdp_info .= '<link itemprop="url" href="' . get_bloginfo('url') . $_SERVER["REQUEST_URI"] . '"><meta itemprop="name" content="' . $post->post_title . '"/>';

                    $mdp_info .= '</span>';

                    $mdp_info .= '<meta itemprop="author" content="' . $row[0]->author . '" />';

                    $mdp_info .= '<meta itemprop="datePublished" content="' . $row[0]->date_created . '" />';

                    $mdp_info .= '<meta itemprop="description" content="' . $review_body . '" />';

                    $mdp_info .= '<span itemprop="provider" itemscope itemtype="http://schema.org/Organization">';

                    $mdp_info .= '<link itemprop="url" href="' . $row[0]->url . '" ><meta itemprop="name" content="' . $row[0]->provider . '" />';

                    $mdp_info .= '<meta itemprop="description" content="' . $row[0]->description . '" />';

                    $mdp_info .= '</span>';

                    $mdp_info .= '</span>';

                    $mdp_info .= '</span>';

                    $mdp_info .= '</span>';

                    return $mdp_info;

                }


                public function visuallyHiddenMdp()
                {
// die();
                    echo '<style type="text/css"> .visually-hidden-mdp { border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; }</style>';

                }

                public function getGeoLocation()
                {
                    $geolocation = wpfresh5_array_option( $this->settings_info, 'geolocation' );
                    if( empty( $geolocation ) )
                        return;

                    $address = wpfresh5_array_option( $this->settings_info, 'street' ) . ', ' . wpfresh5_array_option( $this->settings_info, 'city' ) . ', ' . wpfresh5_array_option( $this->settings_info, 'state' ) . ' ' . wpfresh5_array_option( $this->settings_info, 'zip' );

                    // fetching lat&amp;lng from Google Maps
                    $request_uri = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address). '&sensor=true';
                    $google_xml = simplexml_load_file($request_uri);
                    if( $google_xml && isset( $google_xml->result->geometry ) ) {
                        $lat = (string) $google_xml->result->geometry->location->lat;
                        // fetching time from earth tools
                        $lng = (string) $google_xml->result->geometry->location->lng;

                        update_option('wpf5_lseo_latitude' , $lat);
                        update_option('wpf5_lseo_longitude' , $lng);
                    }

                }


            }
        }






add_action( 'init', 'wpfresh5_start_local_seo' );

function wpfresh5_start_local_seo() {
    $settings = wpfresh5_get_plugin_settings();
    $settings = isset( $settings['seo']['local'] ) ? $settings['seo']['local'] : false;
    $enable_business = isset( $settings['enable_business'] ) ? intval( $settings['enable_business'] ) : 0;
    $enable_rating = isset( $settings['enable_rating'] ) ? intval( $settings['enable_rating'] ) : 0;
    if( !$enable_business && !$enable_rating )
        return;
    $WPFS4LocalBusiness = new WPFS4LocalBusiness();
    add_action( 'wp_head', array ( /*'WPFS4LocalBusiness'*/$WPFS4LocalBusiness , 'visuallyHiddenMdp' ) );
    if ( is_singular() /*get_the_content()*/)
    {
        add_filter('the_content' , array($WPFS4LocalBusiness , 'showLocalBusiness') , 20);
    }
    else
    {
        add_filter('wp_footer' , array($WPFS4LocalBusiness , 'showLocalBusiness'));
    }

}
?>