<?php

class OptimizePress_Assets_Core {

	var $included_assets = array();
	var $assets = array();
	var $asset_keys = array();
	var $parse_list = array();
	var $lang;

	function __construct(){
		global $wpdb;
		if(op_get_option('installed') == 'Y'){
			require_once OP_ASSETS.'default.php';
			$assets = $wpdb->get_col( "SELECT name FROM `{$wpdb->prefix}optimizepress_assets`" );
			if($assets){
				foreach($assets as $asset){
					$this->load_addon($asset);
				}
			}
			add_action('wp_ajax_'.OP_SN.'-assets-folder-list', array($this, 'folder_list'));
			/*
			 * Email marketing services integration hooks
			 */
			add_action('wp_ajax_'.OP_SN.'-email-provider-list', array($this, 'providerList'));
			add_action('wp_ajax_'.OP_SN.'-email-provider-details', array($this, 'providerDetails'));
			add_action('wp_ajax_'.OP_SN.'-email-provider-items', array($this, 'providerItems'));
			add_action('wp_ajax_'.OP_SN.'-email-provider-item-fields', array($this, 'providerItemFields'));

			/**
			 * Live search hooks
			 */
			add_action('wp_ajax_'.OP_SN.'-live-search', array($this, 'liveSearch'));
			add_action('wp_ajax_nopriv_'.OP_SN.'-live-search', array($this, 'liveSearch'));

			add_filter('the_content',array($this,'fixptag'));

			/*
			 * Content template
			 */
			add_action('wp_ajax_' . OP_SN . '-content-layout-delete', array($this, 'deleteContentLayout'));
		}
	}

	function deleteContentLayout()
	{
		check_ajax_referer('op_content_layout_delete', 'nonce');

		$le = new OptimizePress_LiveEditor();
		if ($le->delete_content_layout(op_post('layout')) == 1) {
			wp_send_json_success();
		} else {
			wp_send_json_error();
		}
	}

	/**
	 * Returns list of enabled email marketing service providers
	 * @author Luka Peharda <luka.peharda@gmail.com>
	 * @return void
	 */
	function providerList()
	{
		require_once(OP_LIB . 'sections/dashboard/email_marketing_services.php');
		require_once(OP_MOD . 'email/ProviderFactory.php');

		$enabledProviders = array();

		$services = new OptimizePress_Sections_Email_Marketing_Services();
		$providers = $services->sections();

		if (is_array($providers) && count($providers) > 0) {

			foreach ($providers as $type => $service) {

				/*
				 * Removing GoToWebinar from provider list
				 */
				if ('gotowebinar' === $type) {
					continue;
				}

				$provider = OptimizePress_Modules_Email_ProviderFactory::getFactory($type, true);
				if (null !== $provider && true === $provider->isEnabled()) {
					$enabledProviders[$type] = $service['title'];
				}
			}
		}

		$arg = func_get_arg(0);
		if (empty($arg)) {
			echo json_encode(array('providers' => $enabledProviders));

			/*
			 * Every WP ajax function needs to be exited
			 */
			exit();
		}
		return $enabledProviders;
	}

	/**
	 * Outputs (JSON) email marketing service provider data (lists/forms)
	 * @author Luka Peharda <luka.peharda@gmail.com>
	 * @param string $provider
	 * @param bool $return
	 * @param bool $special
	 * @return void
	 */
	function providerItems($type = null, $return = null, $special = null)
	{
		require_once(OP_MOD . 'email/ProviderFactory.php');

		/*
		 * If $type is not null that means that we are not calling this method via AJAX
		 */
		if (empty($type)) {
			$type = op_post('provider');
		}

		/*
		 * If $type is not null that means that we are not calling this method via AJAX
		 */
		if (empty($special)) {
			$special = (bool) op_post('special');
		}

		$provider = OptimizePress_Modules_Email_ProviderFactory::getFactory($type, true);

		if (null === $provider) {
			exit();
		}

		try {
			if ($type === 'infusionsoft' && $special === true) {
				$data = $provider->getFollowUpSequences();
			} else {
				$data = $provider->getItems();
			}
		} catch (Exception $e) {
			error_log('There seems to be an error with the integration data for "' . $type . '". Check EMS integration data for this provider.');
			$data['lists'] = array('error' => array('name' => __('There seems to be an error with the integration data.', OP_SN)));
		}

		if (null === $return) {
			echo json_encode($data);

			/*
			 * Every WP ajax function needs to be exited
			 */
			exit();
		}

		return $data;
	}

	/**
	 * Outputs (JSON) email marketing service provider data (lists/forms)
	 * @author Luka Peharda <luka.peharda@gmail.com>
	 * @param string $type
	 * @param string $list
	 * @param bool $return
	 * @return void
	 */
	function providerItemFields($type = null, $list = null, $return = null)
	{
		require_once(OP_MOD . 'email/ProviderFactory.php');

		/*
		 * If $type is not null that means that we are not calling this method via AJAX
		 */
		if (empty($type)) {
			$type = op_post('provider');
		}

		/*
		 * If $type is not null that means that we are not calling this method via AJAX
		 */
		if (empty($list)) {
			$list = op_post('list');
		}

		/*
		 * If $type is not null that means that we are not calling this method via AJAX
		 */
		if (empty($special)) {
			$special = (bool) op_post('special');
		}

		$provider = OptimizePress_Modules_Email_ProviderFactory::getFactory($type, true);

		if (null === $provider) {
			exit();
		}

		if (empty($list)) {
			exit();
		}

		$data = $provider->getListFields($list);

		if (null === $return) {
			echo json_encode($data);

			/*
			 * Every WP ajax function needs to be exited
			 */
			exit();
		}

		return $data;
	}

	/**
	 * Outputs (JSON) email marketing service provider data (just list names and IDs)
	 * @author Luka Peharda <luka.peharda@gmail.com>
	 * @param string $provider
	 * @param bool $return
	 * @param bool $special
	 * @return void
	 */
	function providerDetails($type = null, $return = null, $special = null)
	{
		require_once(OP_MOD . 'email/ProviderFactory.php');

		/*
		 * If $type is not null that means that we are not calling this method via AJAX
		 */
		if (empty($type)) {
			$type = op_post('provider');
		}

		/*
		 * If $type is not null that means that we are not calling this method via AJAX
		 */
		if (empty($special)) {
			$special = (bool) op_post('special');
		}

		$provider = OptimizePress_Modules_Email_ProviderFactory::getFactory($type, true);

		if (null === $provider) {
			exit();
		}

		if ($type === 'infusionsoft' && $special === true) {
			$data = $provider->getFollowUpSequences();
		} else {
			$data = $provider->getData();
		}

		if (null === $return) {
			echo json_encode($data);

			/*
			 * Every WP ajax function needs to be exited
			 */
			exit();
		}

		return $data;
	}

	/**
	 * Returns provider enabled status
	 * @author Luka Peharda <luka.peharda@gmail.com>
	 * @param  string $type
	 * @return bool
	 */
	function providerEnabled($type)
	{
		require_once(OP_MOD . 'email/ProviderFactory.php');
		$provider = OptimizePress_Modules_Email_ProviderFactory::getFactory($type, true);
		return $provider->isEnabled();
	}

	/**
	 * Registers user on provider
	 * @author Luka Peharda <luka.peharda@gmail.com>
	 * @param  string $type
	 * @param  string $list
	 * @param  string $email
	 * @param  string $fname
	 * @param  string $lname
	 * @return bool
	 */
	function providerRegister($type, $list, $email, $fname, $lname)
	{
		require_once(OP_MOD . 'email/ProviderFactory.php');
		$provider = OptimizePress_Modules_Email_ProviderFactory::getFactory($type, true);
		return $provider->register($list, $email, $fname, $lname);
	}

	/**
	 * Live Search function called from the live search element on key up or pressed enter
	 * @author Zvonko Biškup <zbiskup@gmail.com>
	 * @return string
	 */
	function liveSearch()
	{
		global $wpdb;
		if (!empty($_POST['all_pages'])) { // search through all pages
			$sql = "
				SELECT ID, post_title FROM $wpdb->posts
				WHERE post_status = 'publish'
				AND post_title LIKE '%".esc_sql($_POST['searchTerm'])."%'
				ORDER BY post_title
				LIMIT 10
			";
			$results = $wpdb->get_results($sql);
		} else {
			if (!empty($_POST['product']) && empty($_POST['category']) && empty($_POST['subcategory'])) { //within product subpages
				$post_parent = esc_sql($_POST['product']);
			} else if (!empty($_POST['product']) && !empty($_POST['category']) && empty($_POST['subcategory'])) {
				$post_parent = esc_sql($_POST['category']);
			} else if (!empty($_POST['product']) && !empty($_POST['category']) && !empty($_POST['subcategory'])) {
				$post_parent = esc_sql($_POST['subcategory']);
			}
			$temp = get_pages('child_of='.$post_parent);
			$results = array();
			if (!empty($temp)) {
				foreach($temp as $result) {
					if (false !== stripos($result->post_title, esc_sql($_POST['searchTerm']))) {
						$obj = new stdClass();
						$obj->ID = $result->ID;
						$obj->post_title = $result->post_title;
						array_push($results, $obj);
					}
				}
			}
		}
		if (!empty($results)) {
			foreach ($results as $result) {
				echo '<li class="op-live-search-results-item"><a href="'.get_permalink($result->ID).'">'.$result->post_title.'</a></li>';
			}
		} else {
			echo '<li class="op-live-search-results-item op-live-search-results-item--empty">' . __('No results', OP_SN) . '</li>';
		}

		exit();
	}

	function fixptag($pee){
		$pee = preg_replace('!\]<br \/>\s*<h([1-6]*)!i',']<h$1',$pee);
		$pee = preg_replace('!<p>\s*\[\/!', "[/", $pee);
		return $pee;
	}

	function assets($group='',$asset='',$clear=false){
		global $wpdb;
		if(count($this->assets) == 0 || $clear){
			$assets = array('core'=>array(),'addon'=>array(),'theme'=>array());
			$assets = apply_filters('op_assets_before_addons',$assets);
			$assets_results = $wpdb->get_results( "SELECT name,title,settings FROM `{$wpdb->prefix}optimizepress_assets` ORDER BY title ASC" );
			if($assets_results){
				foreach($assets_results as $result){
					$assets['addon'][$result->name] = array(
						'title' => $result->title,
						'settings' => $result->settings,
					);
				}
			}
			$assets = apply_filters('op_assets_after_addons',$assets);
			$this->assets = array_filter($assets);
		}
		if(count($this->assets) > 0 && !empty($group)){
			return isset($this->assets[$group]) && isset($this->assets[$group][$asset]) ? $this->assets[$group][$asset] : false;
		}
		return $this->assets;
	}

	function parse_assets(){
		if(count($this->parse_list) == 0){
			$assets = array();
			$assets = apply_filters('op_assets_parse_list',$assets);
			$this->parse_list = array_filter($assets);
		}
		return $this->parse_list;
	}

	function lang(){
		if(!isset($this->lang)){
			$arr = array();
			if(file_exists(OP_LIB.'js/assets/lang.php')){
				include OP_LIB.'js/assets/lang.php';
			}
			$this->lang = apply_filters(OP_SN.'-asset-lang',$arr);
		}
		return $this->lang;
	}

	function lang_key($key){
		$lang = $this->lang();
		if(isset($lang[$key])){
			return $lang[$key];
		}
		return $key;
	}

	function asset_keys(){
		if(count($this->assets) == 0){
			$this->assets();
		}
		if(count($this->asset_keys) == 0){
			$keys = array();
			foreach($this->assets as $assets){
				$keys = array_merge($keys,$assets);
			}
			$this->asset_keys = array_keys($keys);
		}
		return $this->asset_keys;
	}

	function refresh_assets($asset='',$clear=false){
		static $assets;
		if(!isset($assets) || $clear){
			$assets = apply_filters('op_addon_assets',array());
			$assets = array_filter($assets);
		}
		if(!empty($asset)){
			return isset($assets[$asset]) ? $assets[$asset] : false;
		}
		return $assets;
	}

	function save_assets(){
		global $wpdb;
		$dir = @ dir(OP_ASSETS.'addon');
		if($dir){
			while(($file = $dir->read()) !== false){
				if($file != '.' && $file != '..' && $file != 'index.php' && strpos($file, '.') !== 0){
					$this->load_addon($file);
				}
			}
		}
		$assets = $this->refresh_assets('','',true);
		$keys = array_keys($assets);
		$str = '';
		for($i=0,$il=count($keys);$i<$il;$i++){
			$str .= ($str == '' ? '' :',').'%s';
		}

		/*
		 * On some occasions $str was empty and this is a check for it. I don't know what this table does or if it is used in any way
		 */
		if (empty($str)) {
			$wpdb->query("DELETE FROM `{$wpdb->prefix}optimizepress_assets`");
		} else {
			$wpdb->query($wpdb->prepare("DELETE FROM `{$wpdb->prefix}optimizepress_assets` WHERE name NOT IN({$str})",$keys));
		}
		foreach($assets as $tag => $info){
			$this->check_add($tag,$info['title'],op_get_var($info,'settings','N'));
		}
	}

	function check_add($name,$title,$settings='N'){
		global $wpdb;
		$entry = $wpdb->get_col( $wpdb->prepare(
			"SELECT id FROM `{$wpdb->prefix}optimizepress_assets` WHERE `name` = %s",
			$name
		));
		if(!$entry){
			$wpdb->insert($wpdb->prefix.'optimizepress_assets',array('name'=>$name,'title'=>$title,'settings'=>$settings));
		}
	}

	function load_addon($asset){
		if(!isset($this->included_assets[$asset]) && is_dir(OP_ASSETS.'addon/'.$asset) && file_exists(OP_ASSETS.'addon/'.$asset.'/'.$asset.'.php')){
			$included_assets[$asset] = true;
			require_once OP_ASSETS.'addon/'.$asset.'/'.$asset.'.php';
		}
	}

	function folder_list(){
		$files = array();
		$checks = array('image','style','checkbox');
		foreach($checks as $chk){
			if(($folders = op_post('folders',$chk)) && is_array($folders)){
				$files[$chk] = $this->_process_files($folders,$chk);
			}
		}
		echo json_encode($files);
		exit;
	}

	function _process_files($folders,$type='style',$returnfiles=false){
		$files = array();
		static $checked_paths = array();
		foreach($folders as $f){
			if(isset($f['group']) && isset($f['tag']) && isset($f['folder'])){
				switch($f['group']){
					case 'addon':
						$path = OP_ASSETS.'addon/';
						$url = OP_ASSETS_URL.'addon/';
						break;
					case 'theme':
						if(defined('OP_THEME_PATH')){
							$path = OP_THEME_PATH.'assets/';
							$url = OP_THEME_URL.'assets/';
						} elseif(defined('OP_PAGE_URL')){
							$path = OP_PAGE_PATH.'assets/';
							$url = OP_PAGE_URL.'assets/';
						}
						break;
					default:
						$path = OP_ASSETS.'images/';
						$url = OP_ASSETS_URL.'images/';
						break;
				}
				$folder = ($f['folder'] == '' ? '':'/'.$f['folder']);
				$tmppath = $path.$f['tag'].$folder;
				$url .= $f['tag'].$folder.'/';
				if(isset($checked_paths[$tmppath])){
					$files[$f['fieldid']] = $checked_paths[$tmppath];
				} else {
					$newfiles = array();
					if(is_dir($tmppath)){
						$dir = @ dir($tmppath);
						if($dir){
							while(($file = $dir->read()) !== false){
								if($file != '.' && $file != '..' && $file != 'index.php' && strpos($file, '.') !== 0){
									$newfiles[$file] = $url.$file;
								}
							}
						}
					}
					natsort($newfiles);
					$checked_paths[$tmppath] = $files[$f['fieldid']] = $newfiles;
				}
				if(!$returnfiles){
					$func = '_generate_'.$type.'_selector';
					$tmp = $this->$func($tmppath,$files[$f['fieldid']],$f['fieldid'],$f);
					$files[$f['fieldid']] = $tmp[0];
				}
			}
		}
		return $files;
	}

	function _remove_ignore_vals($arr,$folder){
		if(isset($folder['ignore_vals'])){
			foreach($folder['ignore_vals'] as $ignore){
				if(isset($arr[$ignore])){
					unset($arr[$ignore]);
				}
			}
		}
		return $arr;
	}

	function _generate_image_selector($path,$files,$fieldid,$folder){
		static $html = array();
		static $array_elements = array();
		if(!isset($html[$path])){
			$arr = array();
			$str = '<ul class="cf">';
			$strarr = array();
			foreach($files as $file => $url){
				$strarr[$file] = '<li class="op-asset-dropdown-list-item"><a href="#"><img alt="'.esc_attr($file).'" src="'.esc_url($url).'" /></a></li>';
				$arr[esc_attr($file)] = $file;
			}
			$strarr = $this->_remove_ignore_vals($strarr,$folder);
			$arr = $this->_remove_ignore_vals($arr,$folder);
			$str .= implode('',$strarr).'</ul>';
			$html[$path] = $str;
			$array_elements[$path] = $arr;
		}
		return array($html[$path],$array_elements[$path]);
	}

	function _generate_style_selector($path,$files,$fieldid,$folder){
		static $html = array();
		static $array_elements = array();
		if(!isset($html[$path])){
			$styles = array();
			$arr = array();
			foreach($files as $file => $url){
				$file = explode('.',$file);
				$file = end(explode('_',$file[0]));
				$styles[$file] = '<li class="op-asset-dropdown-list-item"><a href="#"><img alt="'.esc_attr($file).'" src="'.esc_url($url).'" /></a></li>';
				$arr[esc_attr($file)] = $file;
			}
			$styles = $this->_remove_ignore_vals($styles,$folder);
			$arr = $this->_remove_ignore_vals($arr,$folder);
			$str = '<ul class="cf">'.implode('',$styles).'</ul>';
			$html[$path] = $str;
			$array_elements[$path] = $arr;
		}
		return array($html[$path],$array_elements[$path]);
	}

	function _generate_checkbox_selector($path,$files,$fieldid){
		static $html = array();
		static $array_elements = array();
		if(!isset($html[$path])){
			$arr = array();
			$str = '<ul class="cf">';
			$count = 0;
			foreach($files as $file => $url){
				$str .= '<li><label><input type="checkbox" name="'.$fieldid.'[]" id="'.$fieldid.$count.'" value="'.$file.'" /><img alt="'.esc_attr($file).'" src="'.esc_url($url).'" /></label></li>';
				$count++;
				$arr[esc_attr($file)] = $file;
			}
			$str .= '</ul>';
			$html[$path] = $str;
			$array_elements[$path] = $arr;
		}
		return array($html[$path],$array_elements[$path]);
	}

	function style_selector($folder,$fieldname='',$selected='',$addclass=''){
		$files = $this->_process_files(array($folder),'',true);
		$selecthtml = $html = '';
		//ksort($files[$folder['fieldid']]);
		foreach($files[$folder['fieldid']] as $file => $url){
			$file = explode('.',$file);
			$file = explode('_', $file[0]);
			$file = end($file);
			$html .= '<li class="op-asset-dropdown-list-item"><a href="#"><img alt="'.esc_attr($file).'" src="'.esc_url($url).'" /></a></li>';
			$selecthtml .= '<option value="'.esc_attr($file).'"'.($selected==$file?' selected="selected"':'').'>'.$url.'</option>';
		}
		return '
<select name="'.$fieldname.'" id="'.$folder['fieldid'].'" class="style-selector" style="display:none">'.$selecthtml.'</select>
<div class="op-asset-dropdown '.$addclass.'" id="'.$folder['fieldid'].'_container">
	<a class="selected-item" href="#"></a>
	<div class="op-asset-dropdown-list">'.$html.'</div>
</div>';
	}

	/**
	 * Generates markup for preset selector (similair to style selector but with less surrounding markup)
	 * @param  string $folder
	 * @param  string $fieldname
	 * @param  string $selected
	 * @param  string $addClass
	 * @return string
	 */
	function preset_selector($folder, $fieldname = '', $selected = '', $addClass = '')
	{
		$files = $this->_process_files(array($folder), '', true);
		$select = $list = '';

		foreach ($files[$folder['fieldid']] as $file => $url) {
			$file = explode('.', $file);
			$file = explode('_', $file[0]);
			$file = end($file);

			$list .= '<li class="op-asset-dropdown-list-item"><a href="#"><img alt="' . esc_attr($file) . '" src="' . esc_url($url) . '" /></a></li>';
			$select .= '<option value="' . esc_attr($file) . '"' . ($selected == $file ? ' selected="selected"' : '') . '>' . $url . '</option>';
		}

		return '
			<div class="op-asset-dropdown-list"><ul>' . $list . '</ul></div>
			<select name="' . $fieldname . '" id="' . $folder['fieldid'] . '" class="preset-selector" style="display:none">' . $select . '</select>';
	}

	function image_selector($folder,$fieldname='',$selected='',$addclass=''){
		$files = $this->_process_files(array($folder),'',true);
		$selecthtml = $html = '';
		//ksort($files[$folder['fieldid']]);
		foreach($files[$folder['fieldid']] as $file => $url){
			$html .= '<li class="op-asset-dropdown-list-item"><a href="#"><img alt="'.esc_attr($file).'" src="'.esc_url($url).'" /></a></li>';
			$selecthtml .= '<option value="'.esc_attr($file).'"'.($selected==$file?' selected="selected"':'').'>'.$url.'</option>';
		}
		return '
<select name="'.$fieldname.'" id="'.$folder['fieldid'].'" class="style-selector" style="display:none">'.$selecthtml.'</select>
<div class="op-asset-dropdown '.$addclass.'" id="'.$folder['fieldid'].'_container">
	<a class="selected-item" href="#"></a>
	<div class="op-asset-dropdown-list">'.$html.'</div>
</div>';
	}
}
function _op_assets(){
	static $op_assets;
	if(!isset($op_assets)){
		$op_assets = new OptimizePress_Assets_Core;
	}
	$args = func_get_args();
	if(count($args)){
		$func = array_shift($args);
		return call_user_func_array(array($op_assets,$func),$args);
	}
}
function op_assets_provider_list()
{
	return _op_assets('providerList', true);
}
function op_assets_provider_details($provider, $special = false)
{
	return _op_assets('providerDetails', $provider, true, $special);
}
function op_assets_provider_items($provider, $special = false)
{
	return _op_assets('providerItems', $provider, true, $special);
}
function op_assets_provider_item_fields($provider, $list, $special = false)
{
	return _op_assets('providerItemFields', $provider, $list, true, $special);
}
function op_assets_provider_enabled($provider)
{
	return _op_assets('providerEnabled', $provider);
}
function op_assets_provider_register($provider, $list, $email, $fname, $lname)
{
	return _op_assets('providerRegister', $provider, $list, $email, $fname, $lname);
}
function op_assets($group='',$asset=''){
	return _op_assets('assets',$group,$asset);
}
function op_assets_parse_list(){
	return _op_assets('parse_assets');
}
function op_asset_tags(){
	return _op_assets('asset_keys');
}
function op_assets_lang(){
	return _op_assets('lang');
}
function op_assets_lang_key($key=''){
	return _op_assets('lang_key',$key);
}
function op_asset_font_style($atts,$prefix='font_'){
	$vars = shortcode_atts(array(
		$prefix.'size' => '',
		$prefix.'font' => '',
		$prefix.'style' => '',
		$prefix.'color' => '',
		$prefix.'spacing' => '',
		$prefix.'shadow' => '',
	), $atts);
	return op_font_style_str($vars,$prefix);
}