'."\n";
$html_code_end = "\n".''."\n";
$html_code = $html_code_start.$html_code.$html_code_end;
return $html_code;
}
/**
* Outputs the code as html element (so not as iframe)
*
* @param $attributes
* @param $file_path_config
* @param $product_url
* @param $rotatetool_js_url
* @return string
*/
function yofla_360_get_embed_code($attributes,$file_path_config,$product_url,$rotatetool_js_url){
global $yofla_360_settings;
$html = '';
//get unique productid
$product_id = yofla_360_get_product_id($file_path_config);
// set unique div id
$div_id = "productRotation_".$product_id;
// get width, height,...
$width = $attributes['width'];
$height = $attributes['height'];
$styles = $attributes['styles'];
//analytics
$ga_data = array(
'ga_label' => $attributes['ga_label'],
'ga_category' => $attributes['ga_category'],
'ga_enabled' => ($attributes['ga_enabled'])?"true":"false",
'ga_tracking_id' => $attributes['ga_tracking_id'],
);
$ga_data_json = json_encode($ga_data);
$html .= "
";
//cloud based player url
$yofla_360_settings = ($yofla_360_settings)?$yofla_360_settings:get_option( 'yofla_360_options' );
if ( isset($yofla_360_settings['license_id']) ){
if(strlen($yofla_360_settings['license_id']) > 0){
$rotatetool_js_url = YOFLA_PLAYER_URL.'?id='.$yofla_360_settings['license_id'];
}
}
wp_enqueue_script( 'yofla_360_player', $rotatetool_js_url);
return $html;
}
/**
* Outputs html code (thumb or link) that launches lightbox when clicked on
*
* @param $attributes
* @param $file_path_config
* @param $product_url
* @param $product_path
* @return string
*/
function yofla_360_get_popup_code($attributes,$file_path_config,$product_url,$product_path){
wp_enqueue_style( 'nivo-lightbox-css', YOFLA_360_URL.'vendor/nivo-lightbox/nivo-lightbox.css' );
wp_enqueue_style( 'nivo-lightbox-theme-css', YOFLA_360_URL.'vendor/nivo-lightbox/themes/default/default.css' );
wp_enqueue_script( 'nivo-lightbox', YOFLA_360_URL.'vendor/nivo-lightbox/nivo-lightbox.js' );
wp_enqueue_script( 'nivo-lightbox-innit', YOFLA_360_URL.'js/init-nivo-lightbox.js' );
switch($attributes['using']){
case 'thumb':
$link_body = yofla_360_get_thumb_image($attributes,$file_path_config,$product_url,$product_path);
break;
case 'link':
$link_body = $attributes['name'];
default:
}
$html = '';
return $html;
}
/**
* Returns the html code for displaying a thumb image of the 360 rotation
*
* @param $attributes
* @param $file_path_config
* @param $product_url
* @param $product_path
* @return string
*/
function yofla_360_get_thumb_image($attributes,$file_path_config,$product_url,$product_path){
$images_dir_path = $product_path.'images/';
//get list of images
$images_in_folder = @yofla_360_get_list_of_images($images_dir_path);
if(sizeof($images_in_folder) >= 1){
$img_path = $images_in_folder[0];
$image_name = basename($img_path);
$image_url = $product_url.'images/'.$image_name;
$out = '
';
}
else{
$out = $attributes['name'];
}
return $out;
}
/**
* Utility function, returns an array with images in given folder
*
* @param $path
* @return array
*/
function yofla_360_get_list_of_images($path){
$folder = opendir($path);
$pic_types = array("jpg", "jpeg", "gif", "png");
$index = array();
while ($file = readdir ($folder)) {
if(in_array(substr(strtolower($file), strrpos($file,".") + 1),$pic_types))
{
array_push($index,$file);
}
}
closedir($folder);
sort($index);
return $index;
}
/**
* Outputs the Product Rotation code as iframe
*
* @param $attributes
* @param $product_url
* @param $product_path
* @return string
*/
function yofla_360_output_legacy($attributes,$product_url,$product_path){
//
$html = '';
// get width, height
$width = $attributes['width_original'];
$height = $attributes['height_original'];
$iframe_name = 'iframe'.$width.'x'.$height.'.html';
$iframe_path = $product_path.$iframe_name;
$iframe_url = $product_url.$iframe_name;
if(file_exists($iframe_path)){
//no action, iframe already generated
}
else{
//load template
$template_file_path = YOFLA_360_PATH.'includes/template-iframe-legacy.tpl';
$template_string = file_get_contents($template_file_path);
//modify template
$search = array('{width}','{height}');
$replace = array($width,$height);
$new_html = str_replace($search,$replace,$template_string);
//save file
file_put_contents($iframe_path,$new_html);
}
//output iframe
$html .= '';
return $html;
}
/**
* Returns iframe embed code
*
* @param $attributes
* @param $product_url
* @param $product_path
* @return string
*/
function yofla_360_get_iframe_code($attributes,$product_url,$product_path){
//globals
global $yofla_360_embed_map;
global $yofla_360_settings;
//get database settings
$yofla_360_settings = ($yofla_360_settings)?$yofla_360_settings:get_option( 'yofla_360_options' );
$html = '';
// get width, height
$width = $attributes['width_original'];
$height = $attributes['height_original'];
$iframe_url = $product_url.'/iframe.html';
//construct iframe html page if license id is set
if($yofla_360_settings && isset($yofla_360_settings['license_id'])){
//pass data for iframe html creation
$data = array(
'license_id' => $yofla_360_settings['license_id'],
'product_url' => $product_url,
'product_name' => $attributes['name'],
'ga_label' => $attributes['ga_label'],
'ga_category' => $attributes['ga_category'],
'ga_tracking_id' => ($attributes['ga_enabled'] && $attributes['ga_tracking_id'])?$attributes['ga_tracking_id']:false
);
$iframe_url = YOFLA_360_URL.'iframe.php?'.http_build_query($data);
}
//output iframe
$html .= '';
return $html;
}
/**
* Ads default values to attributes
*
* @param $attributes
* @return mixed
*/
function yofla_360_process_attributes($attributes) {
//globals
global $yofla_360_settings;
//get database settings
$yofla_360_settings = ($yofla_360_settings)?$yofla_360_settings:get_option( 'yofla_360_options' );
//db defaults
//set iframe styles based on user settings
if(!$attributes['iframe_styles'])
if($yofla_360_settings['iframe_styles']) $attributes['iframe_styles'] = $yofla_360_settings['iframe_styles'];
//set ga_enabled based on user settings
if(!$attributes['ga_enabled'])
if($yofla_360_settings['ga_enabled']) $attributes['ga_enabled'] = $yofla_360_settings['ga_enabled'];
//set ga_tracking_id based on user settings
if(!$attributes['ga_tracking_id'])
if($yofla_360_settings['ga_tracking_id']) $attributes['ga_tracking_id'] = $yofla_360_settings['ga_tracking_id'];
//local defaults
$defaults = array(
'width' => '500',
'height' => '375',
'show' => 'embed',
'name' => '360° product rotation',
'iframe' => 'true',
'iframe_styles' => 'max-width: 100%; border: 1px solid silver;',
'styles' => 'border: 1px solid silver;',
'ga_enabled' => false,
'ga_tracking_id' => null,
'ga_category' => 'YOFLA_360',
'using' => false
);
//enhance provided attributes with defaults
foreach ($defaults as $default => $value) {
if (!array_key_exists($default, $attributes)) {
$attributes[$default] = $value;
}
}
//set default ga_label, if not set
if(!$attributes['ga_label'])
$attributes['ga_label'] = ($attributes['name'])?$attributes['name']:'yofla_360_undefined_product';
//fix false
if($attributes['ga_enabled'] === "false") $attributes['ga_enabled'] = false;
//fix iframe switch
$attributes['iframe'] = strtolower($attributes['iframe']);
//check src parameter
if (!isset($attributes['src'])){
$html = yofla_360_format_error('[src] parameter is not set!');
$attributes['error'] = $html;
}
else{
$attributes['src'] = yofla_360_format_provided_src_attribute($attributes['src']);
}
//store original width,height values
$attributes['width_original'] = yofla_360_remove_px($attributes['width']);
$attributes['height_original'] = yofla_360_remove_px($attributes['height']);
//enhance width/height with px or %
if(substr($attributes['width'],-1) != 'x' && substr($attributes['width'],-1) != '%'){
$attributes['width'] = $attributes['width']."px";
}
if(substr($attributes['height'],-1) != 'x' && substr($attributes['height'],-1) != '%'){
$attributes['height'] = $attributes['height']."px";
}
//sanitize
$attributes['ga_label'] = addslashes($attributes['ga_label']);
return $attributes;
}
/**
* Adds trailing and leading slash to string
*
* @param $src
* @return string
*/
function yofla_360_format_provided_src_attribute($src) {
if (substr($src,-1) != '/') $src .= '/';
if (substr($src,0,1) != '/') $src = '/'.$src;
return $src;
}
/**
* Adds html code to error message that is returned to page
*
* @param $msg
* @return string
*/
function yofla_360_format_error($msg){
$str = '360 Plugin Error: '.$msg.'
';
return $str;
}
/**
* If string is ending with px, remove px and return just number
*
* @param $string
* @return mixed
*/
function yofla_360_remove_px($string){
//no change if ending with %
if(substr($string, -1) == '%') return $string;
//remove all non number chars
$string = preg_replace('#[^0-9]#','',strip_tags($string));
return $string;
}
/**
* Extract unique product_id from config.js file.
*
* @param $file_path_config
* @return mixed
*/
function yofla_360_get_product_id($file_path_config){
$config_file = file_get_contents($file_path_config);
$first_line = strtok($config_file,"\n");
preg_match('/_(\d*)/',$first_line,$matches);
$product_id = $matches[1];
return $product_id;
}