| Current Path : /home/ereika83/public_html/wp-content/plugins/genesis-club-pro/classes/ |
| Current File : /home/ereika83/public_html/wp-content/plugins/genesis-club-pro/classes/class-background.php |
<?php
class Genesis_Club_Background extends Genesis_Club_Module {
const BACKSTRETCH_METAKEY = '_genesis_club_backstretch';
const BIGVIDEO_METAKEY = '_genesis_club_show_bigvideo';
private $defaults = array(
'backstretch' => array('location' => '', 'src' => '',
'centeredX' => false, 'centeredY' => false, 'fade' => 0.75, 'duration' => 5, 'once' => false,
'bgcolor' => false, 'cbgcolor' => false, 'cbgelement' => false, 'opacity' => ''),
'bigvideo' => array('setting' => 'demand', 'location' => 'body', 'url' => '',
'sound' => false, 'controls' => false, 'loop' => false, 'start' => 0, 'stop' => 0, 'veil' => 0,
'youtube' => false, 'ratio' => '16/9', 'quality' => 'default', 'pause' => true, 'logo' => false,
'splash' => '', 'splash_end' => '', 'fallback' => '', 'fallback_duration' => 5,
'bgcolor' => false, 'cbgcolor' => false, 'opacity' => 0.9),
);
private $backstretches = array();
private $bigvideos = array();
private $is_html5 = false;
private $is_videojs = false;
private $is_youtube = false;
private $scripts = '';
function get_options_name() {
return false; //no global settings
}
function get_defaults($option_name ='') {
if (! empty($option_name) && array_key_exists($option_name,$this->defaults))
return $this->defaults[$option_name];
else
return $this->defaults;
}
function init() {
$this->is_html5 = $this->utils->is_html5();
add_action('widgets_init',array( $this,'register_widgets'));
if ( ! is_admin()) add_action('wp',array($this,'prepare'),20);
}
function register_widgets() {
register_widget( 'Genesis_Club_Backstretch_Widget' );
register_widget( 'Genesis_Club_BigVideo_Widget' );
register_widget( 'Genesis_Club_SectionVideo_Widget' );
}
function prepare() {
$this->check_page_bigvideo();
$this->check_backstretch();
}
function check_page_bigvideo() {
if (is_singular()
&& ($meta = @unserialize(get_post_meta(get_queried_object_id(), self::BIGVIDEO_METAKEY,true))))
$this->add_bigvideo($meta);
}
function validate_bigvideo($instance) {
if (is_array($instance) && isset($instance['url']) && $instance['url']) {
$bigvideo = shortcode_atts($this->defaults['bigvideo'], $instance);
if ($bigvideo['youtube'] = $this->is_youtube($bigvideo['url'])) {
$this->is_youtube = true;
} else {
$this->is_videojs = true;
}
return $bigvideo;
} else {
return false;
}
}
function is_youtube($url) {
$fname = substr(strrchr($url,'/'),1);
if (strrchr($fname,'.'))
$youtube = false;
else {
if ($watch = strpos($fname,'v='))
$youtube = substr($fname,$watch+2);
else
$youtube = $fname;
}
return $youtube;
}
function add_bigvideo($instance) {
if ($bigvideo = $this->validate_bigvideo($instance)) {
if ($this->utils->is_mobile_device()) {
$this->fallback_backstretch($bigvideo);
} else {
$this->bigvideos[$bigvideo['location']] = $bigvideo; //only allow one big video at each location
add_action('wp_enqueue_scripts', array($this, 'enqueue_bigvideo'),10);
add_action('wp_print_footer_scripts', array($this, 'do_bigvideos'),10);
add_action('wp_print_footer_scripts', array($this, 'output_scripts'),20);
}
}
}
function maybe_enqueue_bigvideo($instance) {
if ($this->validate_bigvideo($instance)
&& ! $this->utils->is_mobile_device()) {
$this->enqueue_bigvideo();
}
}
function enqueue_bigvideo() {
$this->enqueue_youtube();
$this->enqueue_videojs();
add_action('wp_footer', array($this,'dequeue_redundant_scripts'),1);
}
function enqueue_youtube() {
wp_enqueue_style('mbYTPlayer', plugins_url('styles/mb.YTPlayer.css', dirname(__FILE__)), false, '2.9.4', 'screen');
wp_enqueue_script('mbYTPlayer', plugins_url('scripts/jquery.mb.YTPlayer.js', dirname(__FILE__)), array('jquery'), '2.9.4', true);
}
function dequeue_youtube() {
wp_dequeue_script('mbYTPlayer');
}
function enqueue_videojs() {
wp_register_script('videojs', plugins_url( 'video-js/video.js' , dirname(__FILE__)) );
wp_enqueue_script('videojs');
wp_register_style('videojs', plugins_url( 'video-js/video-js.css' ,dirname(__FILE__)) );
wp_enqueue_style('videojs');
wp_enqueue_script('jquery-imagesloaded', plugins_url('scripts/jquery.imagesloaded.min.js',dirname(__FILE__)), array(), '1.0', true);
wp_enqueue_style('jquery-bigvideo', plugins_url('styles/bigvideo.css',dirname(__FILE__)), array(), '1.1');
wp_enqueue_script('jquery-bigvideo', plugins_url('scripts/jquery.bigvideo.js',dirname(__FILE__)), array('jquery','jquery-ui-slider'), '1.1', true);
}
function dequeue_videojs() {
wp_dequeue_script('jquery-bigvideo');
wp_dequeue_script('jquery-imagesloaded');
wp_dequeue_script('videojs');
}
function get_setting($property, $settings) {
return array_key_exists($property,$settings) ? trim($settings[$property]) : $this->defaults['bigvideo'][$property];
}
function set_opacity_for_video($container, $opacity, $cbgcolor) {
$s = '';
if ($opacity)
$s .= sprintf('%1$s.css({"opacity": %2$s });', $container, $opacity);
if ($cbgcolor) {
$content = $this->is_html5 ? 'article,aside' :'#inner';
$s .= sprintf('%1$s.find("%2$s").css({ "background-color": "%3$s" });',$container, $content, $cbgcolor);
}
return $s;
}
function set_opacity_for_image($backstretch, $isbody = false, $n='') {
$overflow = $isbody ? '' : '"overflow":"hidden"';
$bgcolor = empty($backstretch['bgcolor']) ? '' : sprintf('"background-color":"%1$s"', $backstretch['bgcolor']);
$css = sprintf('%1$s%2$s%3$s', $overflow, $overflow ? ',' : '', $bgcolor);
$s = empty($css) ? '' : sprintf('_container%1$s.css({%2$s});', $n, $css);
if ($backstretch['cbgelement'] && $backstretch['opacity']) {
$s .= sprintf('var _content%3$s = _container%3$s.find("%1$s"); var _opacity%3$s = %2$s; if (_content%3$s) {', $backstretch['cbgelement'], $backstretch['opacity'], $n);
if (!empty($backstretch['cbgcolor'])) $s .= sprintf('_content%2$s.css("background-color","%1$s");', $backstretch['cbgcolor'], $n);
$s .= sprintf('var bgcolor%1$s = _content%1$s.css("background-color"); if ((bgcolor%1$s !== undefined) && (bgcolor%1$s != "rgba(0, 0, 0, 0)") && (bgcolor%1$s != "transparent") ) { var newColor%1$s = "rgba"+ bgcolor%1$s.slice(bgcolor%1$s.indexOf("("), bgcolor%1$s.lastIndexOf(bgcolor%1$s.substr(0,4)=="rgba" ? ",":")"))+ ", " + _opacity%1$s + ")"; _content%1$s.css("background-color", newColor%1$s); } } ', $n);
}
return $s;
}
function do_bigvideos() {
$n=0;
foreach ($this->bigvideos as $location => $bigvideo) {
if ($bigvideo['youtube'])
$this->do_youtube($bigvideo, $n);
else
$this->do_videojs($bigvideo, $n);
$n++;
}
}
function do_youtube($instance, $n=0) {
$params = shortcode_atts($this->defaults['bigvideo'], $instance);
$url = $this->get_setting('url', $params);
if (!empty($url)) {
$settings = array();
$settings['videoURL'] = $url;
$start = $this->get_setting('start', $params) ;
if (!empty($start)) $settings['startAt'] = $start;
$stop = $this->get_setting('stop', $params) ;
if (!empty($stop)) $settings['stopAt'] = $stop;
$splash = $this->get_setting('splash',$params) ;
if (!empty($splash)) $settings['splashURL'] = $splash;
$splash_end = $this->get_setting('splash_end',$params) ;
if (!empty($splash_end)) $settings['endSplashURL'] = $splash_end;
$settings['quality'] = $this->get_setting('quality', $params);
$settings['veil'] = $this->get_setting('veil', $params);
$settings['loop'] = $this->get_setting('loop', $params);
$settings['mute'] = $this->get_setting('sound', $params) ? 'false' : 'true';
$settings['showControls'] = $this->get_setting('controls', $params) ? 'true' : 'false';
$settings['stopMovieOnBlur'] = $this->get_setting('pause', $params) ? 'true' : 'false';
$settings['showYTLogo'] = $this->get_setting('logo', $params) ? 'true' : 'false';
$settings['bgcolor'] = $this->get_setting('bgcolor', $params);
$container = $this->get_setting('location', $params);
$settings['containment'] = $container=='body' ? 'body' : 'self';
$jsonparams = $this->utils->json_encode($settings);
if ($container=='body') {
$container = $this->is_html5 ? '.site-container' :'#wrap';
$element = '_container'.$n;
$opacity = $this->get_setting('opacity', $params);
$cbgcolor = $this->get_setting('cbgcolor', $params);
$this->scripts .= sprintf('var params = jQuery.parseJSON(\'%1$s\'); jQuery(\'<div/>\').prependTo(\'body\').mb_YTPlayer(params); var %2$s = jQuery(\'%3$s\'); %4$s',
$jsonparams, $element, $container, $this->set_opacity_for_video($element, $opacity, $cbgcolor )) . "\n";
} else {
$this->scripts .= sprintf('var params = jQuery.parseJSON(\'%1$s\'); jQuery(\'%2$s\').mb_YTPlayer(params);',
$jsonparams, $container) . "\n";
}
}
}
function do_videojs($instance, $n) {
$params = shortcode_atts($this->defaults['bigvideo'], $instance);
$url = $this->get_setting('url', $params);
if (!empty($url)) {
$container = $this->get_setting('location', $params);
if (empty($container) || ($container=='body')) $container = $this->is_html5 ? '.site-container' :'#wrap';
$ambient = $this->get_setting('sound', $params) ? 'false' : 'true';
$controls = $this->get_setting('controls', $params) ? 'true' : 'false';
$loop = $this->get_setting('loop', $params) ? 'true' : 'false';
$start = $this->get_setting('start', $params) ;
$startAt = empty($start) ? '' :sprintf(',startAt:%1$s',$start);
$stop = $this->get_setting('stop', $params) ;
$stopAt = empty($stop) ? '' :sprintf(',stopAt:%1$s',$stop);
$splash = $this->get_setting('splash',$params);
$splashURL = empty($splash) ? '' :sprintf(',splashURL:"%1$s"',$splash);
$splash_end = $this->get_setting('splash_end', $params);
$endSplashURL = empty($splash_end) ? '' :sprintf(',endSplashURL:"%1$s"',$splash_end);
$element = '_container'.$n;
$opacity = $this->get_setting('opacity', $params);
$cbgcolor = $this->get_setting('cbgcolor', $params);
$set_opacity = $this->set_opacity_for_video($element, $opacity, $cbgcolor );
$this->scripts .= sprintf('var %1$s = jQuery(\'%2$s\'); var BV = new $.BigVideo({container:%1$s, ambient:%3$s,doLoop:%4$s,controls:%5$s%6$s%7$s%8$s%9$s}); BV.init(); BV.show("%10$s");%11$s',
$element, $container, $ambient, $loop, $controls, $startAt, $stopAt, $splashURL, $endSplashURL, $url, $set_opacity) . "\n";
}
}
function fallback_backstretch($instance) {
$url = $this->get_setting('fallback', $instance);
if (!empty($url)) {
$backstretch = $this->get_defaults('backstretch');
$backstretch['src'] = $url;
$backstretch['location'] = $this->get_setting('location', $instance);
$backstretch['duration'] = $this->get_setting('fallback_duration', $instance);
$this->add_backstretch($backstretch);
}
}
function check_backstretch() {
if (is_singular()
&& ($backstretch = $this->get_backstretch(get_queried_object_id()))
&& array_key_exists('src', $backstretch)
&& !empty($backstretch['src']) ) {
$this->add_backstretch($backstretch);
}
}
function get_backstretch($post_id) {
if ( ($backstretch = $this->utils->get_meta($post_id, self::BACKSTRETCH_METAKEY))
&& ! empty($backstretch['src']))
return $backstretch;
else
return false;
}
function add_backstretch($backstretch) {
if (empty($backstretch['src']) || $this->backstretch_exists($backstretch['location']))
return false;
else {
$this->backstretches[] = $backstretch;
if (count($this->backstretches) == 1) {
wp_enqueue_script('backstretcher', plugins_url('scripts/jquery.backstretch.min.js', dirname(__FILE__)),array('jquery'), '1.0', true);
add_action ('wp_print_footer_scripts', array($this,'do_backstretches'));
add_action('wp_print_footer_scripts', array($this, 'output_scripts'),20);
}
}
}
function backstretch_exists($location) {
foreach ($this->backstretches as $backstretch)
if ($location == $backstretch['location']) return true;
return false;
}
function do_backstretches(){
$n = 0;
foreach ($this->backstretches as $backstretch) {
$this->do_backstretch($backstretch, $n);
$n++;
}
}
function do_backstretch($backstretch, $n=0){
$container = empty($backstretch['location']) ? 'body' : $backstretch['location'];
$srclist = '';
$srcarray = explode(',',trim($backstretch['src']));
foreach ($srcarray as $src) $srclist .= sprintf('"%1$s",',trim($src));
$jsonsrc = sprintf('[%1$s]',substr($srclist,0,-1));
if (strpos($jsonsrc, ',') !== FALSE) {
$params['duration'] = $backstretch['duration'] * 1000;
$params['once'] = $backstretch['once'] ? 'true' : 'false';;
}
$params['centeredX'] = $backstretch['centeredX'] ? 'true' : 'false';
$params['centeredY'] = $backstretch['centeredY'] ? 'true' : 'false';
$params['fade'] = $backstretch['fade'] * 1000;
$jsonparams = $this->utils->json_encode($params);
if (empty($n)) $n= '';
$set_opacity = $this->set_opacity_for_image($backstretch, $container=='body', $n);
$element = $container=='body' ? '$' : ('_container'.$n);
$this->scripts .= sprintf('var _container%6$s = jQuery(\'%1$s\'); %2$s.backstretch(%3$s,%4$s); %5$s',
$container, $element, $jsonsrc, $jsonparams, $set_opacity, $n) . "\n";
}
function dequeue_redundant_scripts() {
if (! $this->is_videojs) $this->dequeue_videojs();
if (! $this->is_youtube) $this->dequeue_youtube();
}
function output_scripts() {
if (!empty($this->scripts))
print <<< SCRIPTS
<script type="text/javascript">
//<![CDATA[
jQuery.migrateMute = true;
jQuery(document).ready(function($){
{$this->scripts}
});
//]]>
</script>
SCRIPTS;
}
}