'."\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 without 3DRT setup utility
if(yofla_360_is_just_images_directory($attributes)){
$iframe_url = yofla_360_get_iframe_url_for_just_images_directory($attributes,$yofla_360_settings);
}
//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) {
$src = trim($src);
//trailing
if (substr($src,-1) != '/') $src .= '/';
//leading
if (substr($src,0,1) != '/' && yofla_360_starts_with_http($src)=== false) $src = '/'.$src;
return $src;
}
/**
* Checks if string starts with http
*
* @param $str
* @return bool
*/
function yofla_360_starts_with_http($str){
if(strpos($str,'http') === 0)
return true;
return false;
}
/**
* 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;
}
/**
* Checks if images directory is created by 3DRT Setup Utility or contains just images
*
* @param $attributes Array
* @return bool
*/
function yofla_360_is_just_images_directory($attributes) {
//if src is a absolute url path
if(yofla_360_starts_with_http($attributes['src'])) return false;
$wp_uploads = wp_upload_dir();
$product_path_full = $wp_uploads['basedir'].$attributes['src'];
$rotatetool_js_file_full_path = $product_path_full.'rotatetool.js';
$config_file_full_path = $product_path_full.'config.js';
if(file_exists($rotatetool_js_file_full_path) && file_exists($config_file_full_path) ){
return false;
}
else{
return true;
}
}
/**
* Automatically generate config.js & iframe.html based on images in products folder.
* Based on: http://www.yofla.com/3d-rotate/support/plugins/php-lib-for-360-product-view/
*
* @param $attributes
* @param $yofla_360_settings
* @return bool|string
*/
function yofla_360_get_iframe_url_for_just_images_directory($attributes,$yofla_360_settings) {
//include library
include_once(YOFLA_360_PATH.'includes/yofla_3drt/lib/yofla/Rotate_Tool.php');
$wp_uploads = wp_upload_dir();
$uploads_url = $wp_uploads["baseurl"];
$products_url = $uploads_url.'/'.YOFLA_360_PRODUCTS_DIRECTORY_NAME.'/';
$products_path = $wp_uploads['basedir'].'/'.YOFLA_360_PRODUCTS_DIRECTORY_NAME.'/';
$product_path_full = $wp_uploads['basedir'].$attributes['src'];
$product_path_relative = substr($attributes['src'],strpos($attributes['src'],YOFLA_360_PRODUCTS_DIRECTORY_NAME) + strlen(YOFLA_360_PRODUCTS_DIRECTORY_NAME) + 1); //get relative path by removing the yofla360 products directory name
//check paths
if(!file_exists($product_path_full)) return FALSE;
//plugin stored settings, set "cloud" rotatetool.js, if set
$rotatetool_js_src = YOFLA_PLAYER_URL;
if($yofla_360_settings && isset($yofla_360_settings['license_id']) && strlen($yofla_360_settings['license_id']) > 5){
$rotatetool_js_src = YOFLA_PLAYER_URL.'?id='.$yofla_360_settings['license_id'];;
Rotate_Tool::$rotatetool_js_src = $rotatetool_js_src;
}
//set vars
$system_url = plugins_url()."/360-product-rotation/includes/yofla_3drt/";
Rotate_Tool::set_system_url($system_url);
Rotate_Tool::$products_path = $products_path;
Rotate_Tool::$products_url = $products_url;
Rotate_Tool::$is_cache_disabled = true;
//if cache attribute is sett to true, use the cache
if(isset($attributes['cache']) && strtolower($attributes['cache'])=="true"){
Rotate_Tool::$is_cache_disabled = false;
}
//generate config.js, always, let wordpress cache plugin do the "hard job"
$settings = Rotate_Tool::get_cascading_settings_for_directory($product_path_full);
$config_content = Rotate_Tool::get_config_file_content($product_path_full,$settings);
if($config_content === FALSE) return FALSE;
//generate iframe.html, always, let wordpress cache plugin do the "hard job"
Rotate_Tool::get_page_for_iframe($product_path_relative,$settings);
//iframe page url
$iframe_url = Rotate_Tool::get_cached_iframe_page_url($product_path_relative);
return $iframe_url;
}
function yofla_360_activation_hook() {
yofla_360_check_products_folder_initialized();
}
/**
* Checks if settings ini in uplaods/yofla360 folder exists, creates if not
*/
function yofla_360_check_products_folder_initialized()
{
$wp_uploads = wp_upload_dir();
$products_path = $wp_uploads['basedir'].'/'.YOFLA_360_PRODUCTS_DIRECTORY_NAME.'/';
$settings_path = $products_path.'settings.ini';
$settings_source = YOFLA_360_PATH.'/includes/yofla_3drt/settings.ini';
if(!file_exists($settings_path)){
//create directory
wp_mkdir_p($products_path);
//copy settings file
copy($settings_source,$settings_path);
}
}
//HOOKS
//define shortcode and callback
add_shortcode('360', 'yofla_360_embed_shortcode');
//activate hook
register_activation_hook( __FILE__, 'yofla_360_activation_hook' );