<?php
require( WPCTP_CUSTOMIZER_DIR . '/args.php' );
require( WPCTP_CUSTOMIZER_DIR . '/googlefont.php' );
require( WPCTP_CUSTOMIZER_DIR . '/image-choices.php' );
require( WPCTP_CUSTOMIZER_DIR . '/customizer.php' );
require( WPCTP_RP_DIR . '/class-tgm-plugin-activation.php' );
require( WPCTP_RP_DIR . '/plugin-list.php' );
require( WPCTP_PT_DIR . '/class-slider-post-type.php' );
require( WPCTP_PT_DIR . '/class-testimonials-post-type.php' );
require( WPCTP_WIDGETS_DIR . '/class-news-widget.php' );
require( WPCTP_WIDGETS_DIR . '/class-services-widget.php' );
require( WPCTP_WIDGETS_DIR . '/class-slider-search-widget.php' );
require( WPCTP_WIDGETS_DIR . '/class-sliders-widget.php' );
require( WPCTP_WIDGETS_DIR . '/class-testimonials-slider-widget.php' );
require( WPCTP_ADMIN_DIR . '/updates.php' );
function wpctp_get_contents($url){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
function wpctp_load_footer_widgets() {
	if( get_option( 'stylesheet' ) == 'wpcargo-pro' ){
		$nav_menu = get_option( 'widget_nav_menu' );
		$texts = get_option( 'widget_text' );
		$wysijas = get_option( 'widget_wysija' );
		$widgets  = get_option( 'sidebars_widgets' );
		$menu_index = 0;
		$has_menu_index = 0;
		$text_index = 0;
		$text_index_1 = 0;
		$text_index_2 = 0;
		$wysija_index = 0;
		$has_wysija_index = 0;
		if( isset( $nav_menu[2] ) ){
			foreach( $nav_menu as $key => $menu ){
				if( ( $key > $menu_index ) && is_numeric( $key ) ){
					$menu_index = $key;
				}
				if( $menu['title'] === "QUICK LINKS" ){
					$has_menu_index = $key;
					break;
				}
			}
			$nav_menu[$menu_index] = array(
				'title' => __( 'QUICK LINKS', 'wpctp' ),
			);
			if( ! $has_menu_index ){
				update_option( 'widget_nav_menu', $nav_menu );
			}
		}else if ( ! isset( $nav_menu[2] ) ) {
			$menu_index = 2;
			$nav_menu[2] = array(
				'title' => __( 'QUICK LINKS', 'wpctp' ),
			);
			update_option( 'widget_nav_menu', $nav_menu );
		}
		if( isset( $texts[2] ) && isset( $texts[3] ) ){
			foreach( $texts as $key => $text ){
				if( ( $key > $text_index ) && is_numeric( $key ) ){
					$text_index = $key;
				}
				if( ! $text_index_1 && $text['title'] === "ABOUT OUR COMPANY" ){
					$text_index_1 = $key;
				}
				if( ! $text_index_2 && $text['title'] === "CONTACT INFO" ){
					$text_index_2 = $key;
				}
			}
			if( ! $text_index_1 ){
				$texts[$text_index] = array(
					'title' => __( 'ABOUT OUR COMPANY', 'wpctp' ),
					'text' => sprintf(
						'<p><img src="%s/wpctp-white.png" alt="WPCargo Pro"></p><p>%s</p>',
						WPCTP_IMAGES_URL,
						__( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eu justo dapibus, auctor risus id, aliquam arcu. Lorem ipsum dolor sit amet, aliquam arcu. Lorem ipsum dolor sit consectetur.', 'wpctp' )
					)
				);
			}
			if( ! $text_index_2 ){
				$texts[$text_index + 1] = array(
					'title' => __( 'CONTACT INFO', 'wpctp' ),
					'text' => sprintf(
						'<p><strong>%s</strong> %s</p><p><strong>%s</strong> %s</p>',
						__( 'Address:', 'wpctp' ),
						__( 'A Great Company Name 123 Main St, Warwick, RI 02889', 'wpctp' ),
						__( 'Phone Number:', 'wpctp' ),
						__( '718.555.0062', 'wpctp' )
					)
				);
			}
			update_option( 'widget_text', $texts );
		}else if ( ! isset( $texts[2] ) && ! isset( $texts[3] ) ) {
			$text_index = 2;
			$texts[2] = array(
				'title' => __( 'ABOUT OUR COMPANY', 'wpctp' ),
				'text' => sprintf(
					'<p><img src="%s/wpctp-white.png" alt="WPCargo Pro"></p><p>%s</p>',
					WPCTP_IMAGES_URL,
					__( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eu justo dapibus, auctor risus id, aliquam arcu. Lorem ipsum dolor sit amet, aliquam arcu. Lorem ipsum dolor sit consectetur.', 'wpctp' )
				)
			);
			$texts[3] = array(
				'title' => __( 'CONTACT INFO', 'wpctp' ),
				'text' => sprintf(
					'<p><strong>%s</strong> %s</p><p><strong>%s</strong> %s</p>',
					__( 'Address:', 'wpctp' ),
					__( 'A Great Company Name 123 Main St, Warwick, RI 02889', 'wpctp' ),
					__( 'Phone Number:', 'wpctp' ),
					__( '718.555.0062', 'wpctp' )
				)
			);
			update_option( 'widget_text', $texts );
		}
		if( isset( $wysijas[2] ) ){
			foreach( $wysijas as $key => $wysija ){
				if( ( $key > $wysija_index ) && is_numeric( $key ) ){
					$wysija_index = $key;
				}
				if( $wysija['title'] === "NEWSLETTER" ){
					$has_wysija_index = $key;
					break;
				}
			}
			$wysija_menu[$wysija_index] = array(
				'title' => __( 'NEWSLETTER', 'wpctp' ),
				'form' => 1
			);
			if( ! $has_wysija_index ){
				update_option( 'widget_wysija', $wysijas );
			}
		}else if ( ! isset( $wysijas[2] ) ) {
			$wysija_index = 2;
			$wysijas[2] = array(
				'title' => __( 'NEWSLETTER', 'wpctp' ),
				'form' => 1
			);
			update_option( 'widget_wysija', $wysijas );
		}
		if ( ! isset( $widgets['primary-sidebar'] ) ) {
			$widgets['primary-sidebar'] = array( 'search-2', 'recent-posts-2', 'categories-2' );
		}
		$textindex1 = $text_index_1 ? $text_index_1 : $text_index;
		if ( $textindex1 ) {
			$widgets['footer-widgets-1'] = array( 'text-' . $textindex1 );
		}
		$menuindex = $has_menu_index ? $has_menu_index : $menu_index;
		if ( $menuindex ) {
			$widgets['footer-widgets-2'] = array( 'nav_menu-' . $menuindex );
		}
	
		$textindex2 = $text_index_2 ? $text_index_2 : ( $text_index + 1 );
		if ( $textindex2 ) {
			$widgets['footer-widgets-3'] = array( 'text-' . $textindex2 );
		}
		$wysijaindex = $has_wysija_index ? $has_wysija_index : $wysija_index;
		if ( $wysijaindex ) {
			$widgets['footer-widgets-4'] = array( 'wysija-' . $wysijaindex );
		}
		update_option( 'sidebars_widgets', $widgets );	
	}
}
