Last active
September 5, 2019 14:48
-
-
Save wpmudev-sls/359083c3dcfddda84c2493b2568051f5 to your computer and use it in GitHub Desktop.
[WPMUDEV General] - Custom OT Shortcode to support CDN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [WPMUDEV General] - Custom OT Shortcode to support CDN | |
* Description: [WPMUDEV General] - Custom OT Shortcode to support CDN - 1137420430818785 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_general_smush_custom_ot_shortcode_func', 100 ); | |
function wpmudev_general_smush_custom_ot_shortcode_func() { | |
if( defined('WP_SMUSH_VERSION') && class_exists( 'WP_Smush' ) ){ | |
class WPMUDEV_Smush_Custom_Shortcode_OTComposer{ | |
private $cdn_core; | |
private $enabled_cdn; | |
public function __construct(){ | |
$this->custom_ot_shortcode(); | |
} | |
public function custom_ot_shortcode(){ | |
remove_shortcode( 'banner_slider' ); | |
add_shortcode('banner_slider', array( $this, 'banner_slider') ); | |
remove_shortcode( 'logos' ); | |
add_shortcode('logos', array( $this, 'logos_func' ) ); | |
remove_shortcode('banner_blog', 'banner_blog_func'); | |
add_shortcode('banner_blog', array( $this, 'banner_blog_func') ); | |
remove_shortcode('team', 'team_func'); | |
add_shortcode('team', array( $this, 'team_func') ); | |
} | |
public function team_func($atts, $content = null){ | |
extract(shortcode_atts(array( | |
'photo' => '', | |
'name' => '', | |
'job' => '', | |
'icon1' => '', | |
'icon2' => '', | |
'icon3' => '', | |
'icon4' => '', | |
'url1' => '', | |
'url2' => '', | |
'url3' => '', | |
'url4' => '', | |
), $atts)); | |
$img = wp_get_attachment_image_src($photo,'full'); | |
// $img = $img[0]; | |
$icon11 = (!empty($icon1) ? '<li class="icon-soc"><a href="'.esc_url($url1).'" target="_blank"><i class="fa fa-'.esc_attr($icon1).'"></i></a></li>' : ''); | |
$icon22 = (!empty($icon2) ? '<li class="icon-soc"><a href="'.esc_url($url2).'" target="_blank"><i class="fa fa-'.esc_attr($icon2).'"></i></a></li>' : ''); | |
$icon33 = (!empty($icon3) ? '<li class="icon-soc"><a href="'.esc_url($url3).'" target="_blank"><i class="fa fa-'.esc_attr($icon3).'"></i></a></li>' : ''); | |
$icon44 = (!empty($icon4) ? '<li class="icon-soc"><a href="'.esc_url($url4).'" target="_blank"><i class="fa fa-'.esc_attr($icon4).'"></i></a></li>' : ''); | |
ob_start(); ?> | |
<!-- WPMUDEV Custom --> | |
<div class="team-wrap"> | |
<?php if( $img ): ?> | |
<img src="<?php echo esc_url( $this->generate_cdn_url( $img[0] ) ); ?>" alt=""> | |
<?php endif; ?> | |
<div class="mask-team"> | |
<?php if($name) { ?><h6><?php echo htmlspecialchars_decode($name); ?></h6><?php } ?> | |
<?php if($job) { ?><p><span><?php echo htmlspecialchars_decode($job); ?></span></p><?php } ?> | |
<?php if($content) { echo htmlspecialchars_decode($content); } ?> | |
<div class="social-team"> | |
<ul class="list-social"> | |
<?php echo htmlspecialchars_decode($icon11); ?> | |
<?php echo htmlspecialchars_decode($icon22); ?> | |
<?php echo htmlspecialchars_decode($icon33); ?> | |
<?php echo htmlspecialchars_decode($icon44); ?> | |
</ul> | |
</div> | |
</div> | |
</div> | |
<?php | |
return ob_get_clean(); | |
} | |
public function banner_blog_func($atts, $content = null){ | |
extract(shortcode_atts(array( | |
'head1' => '', | |
'head2' => '', | |
'btntext' => '', | |
'btnlink' => '', | |
'image' => '', | |
'opacity' => '', | |
'overlay_color' => '', | |
'extraclass' => '', | |
), $atts)); | |
$img = wp_get_attachment_image_src($image,'full'); | |
$opacity1 = (!empty($opacity) ? 'opacity:'.esc_attr( $opacity ).';' : ' '); | |
$overlay_color1 = (!empty($overlay_color) ? 'background:'.esc_attr( $overlay_color ).';' : ' '); | |
ob_start(); ?> | |
<!-- WPMUDEV Custom --> | |
<div class="section home boxed-width <?php echo esc_attr($extraclass); ?>"> | |
<?php if( $img ): ?> | |
<div class="parallax-home" <?php if($image !=''){ ?> style="background-image: url(<?php echo esc_url( $this->generate_cdn_url( $img[0] ) ); ?>);" <?php } ?> ></div> | |
<?php endif; ?> | |
<div class="home-background-mask" <?php if($opacity or $overlay_color){ ?> style="<?php echo htmlspecialchars_decode($overlay_color1).htmlspecialchars_decode($opacity1); ?>" <?php } ?> ></div> | |
<div class="cd-intro"> | |
<h1 class="cd-headline loading-bar"> | |
<?php if($head1) { ?><span><?php echo htmlspecialchars_decode($head1); ?></span><?php } ?> | |
<?php if($content) { ?> | |
<span class="cd-words-wrapper"><?php echo htmlspecialchars_decode($content); ?></span> | |
<?php } ?> | |
</h1> | |
</div> | |
<?php if($head2 != '') { ?><div class="small-intro"><?php echo htmlspecialchars_decode($head2); ?></div><?php } ?> | |
<?php if($btnlink != '') { ?><a href="<?php echo esc_attr($btnlink); ?>" data-gal="m_PageScroll2id" data-ps2id-offset="67"><div class="scroll-down"><span></span> <?php echo htmlspecialchars_decode($btntext); ?></div></a><?php } ?> | |
</div> | |
<?php | |
return ob_get_clean(); | |
} | |
public function banner_slider($atts, $content = null){ | |
extract(shortcode_atts(array( | |
'head1' => '', | |
'head2' => '', | |
'btntext' => '', | |
'btnlink' => '', | |
'gallery' => '', | |
'opacity' => '', | |
'overlay_color' => '', | |
'extraclass' => '', | |
'timer' => '', | |
'effTimer' => '', | |
'effect' => 'zoom', | |
), $atts)); | |
$main_id = uniqid( 'homeslider-' ); | |
$control_id = uniqid( 'controls-' ); | |
$opacity1 = (!empty($opacity) ? 'opacity:'.esc_attr( $opacity ).';' : ' '); | |
$overlay_color1 = (!empty($overlay_color) ? 'background:'.esc_attr( $overlay_color ).';' : ' '); | |
$timer1 = (!empty($timer) ? $timer : 3000); | |
$effTimer1 = (!empty($effTimer) ? $effTimer : 4000); | |
ob_start(); ?> | |
<!-- WPMUDEV Custom --> | |
<div id="<?php echo esc_attr($main_id); ?>" class="section home boxed-width <?php echo esc_attr($extraclass); ?>"> | |
<div id="wrapper-slider"> | |
<div id="<?php echo esc_attr($control_id); ?>"> | |
<div class="pause"></div> | |
<div class="play"></div> | |
<div class="prev"></div> | |
<div class="next"></div> | |
</div> | |
</div> | |
<div class="home-background-mask" <?php if($opacity or $overlay_color){ ?> style="<?php echo htmlspecialchars_decode($overlay_color1).htmlspecialchars_decode($opacity1); ?>" <?php } ?> ></div> | |
<div class="cd-intro"> | |
<h1 class="cd-headline loading-bar"> | |
<?php if($head1) { ?><span><?php echo htmlspecialchars_decode($head1); ?></span><?php } ?> | |
<?php if($content) { ?> | |
<span class="cd-words-wrapper"><?php echo htmlspecialchars_decode($content); ?></span> | |
<?php } ?> | |
</h1> | |
</div> | |
<?php if($head2 != '') { ?><div class="small-intro"><?php echo htmlspecialchars_decode($head2); ?></div><?php } ?> | |
<?php if($btnlink != '') { ?><a href="<?php echo esc_attr($btnlink); ?>" data-gal="m_PageScroll2id" data-ps2id-offset="30"><div class="scroll-down"><span></span> <?php echo htmlspecialchars_decode($btntext); ?></div></a><?php } ?> | |
</div> | |
<script type="text/javascript"> | |
(function($) { "use strict"; | |
//Home Background Slider | |
$.mbBgndGallery.buildGallery({ | |
containment:"#<?php echo esc_attr($main_id); ?>", | |
autoStart:true, | |
timer:<?php echo esc_js($timer1); ?>, | |
effTimer:<?php echo esc_js($effTimer1); ?>, | |
controls:"#<?php echo esc_attr($control_id); ?>", | |
grayScale:false, | |
shuffle:true, | |
preserveWidth:false, | |
effect:"<?php echo esc_js($effect); ?>", /* fade, slideUp, slideDown, slideRight, slideLeft, zoom */ | |
images:[ | |
<?php | |
$img_ids = explode(",",$gallery); | |
$image_size = ''; | |
$is_mobile = false; | |
if( wp_is_mobile() ){ | |
if( false !== strpos( $_SERVER['HTTP_USER_AGENT'], 'iPad' ) ){ | |
$image_size = array(1024, 9999); | |
}else{ | |
$is_mobile = true; | |
$image_size = array(768, 9999); | |
} | |
} | |
// disable on mobile because already use parllax home | |
if( $is_mobile ){ | |
$img_ids = array(); | |
} | |
foreach( $img_ids as $idex => $img_id ){ | |
$meta = wp_prepare_attachment_for_js($img_id); | |
$caption = $meta['caption']; | |
$title = $meta['title']; | |
$description = $meta['description']; | |
$image_src = wp_get_attachment_image_src($img_id, $image_size); | |
if( $image_src ){ | |
$src = $this->generate_cdn_url( $image_src[0] ); | |
}else{ | |
continue; | |
} | |
?> | |
"<?php echo esc_url( $src ); ?>", | |
<?php } ?> | |
], | |
onStart:function(){}, | |
onPause:function(){}, | |
onPlay:function(opt){}, | |
onChange:function(opt,idx){}, | |
onNext:function(opt){}, | |
onPrev:function(opt){} | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
return ob_get_clean(); | |
} | |
public function logos_func($atts, $content = null){ | |
extract(shortcode_atts(array( | |
'gallery' => '', | |
'bgimage' => '', | |
'opacity' => '', | |
'overlay_color' => '', | |
), $atts)); | |
if( empty( $bgimage ) ) return; | |
$img = wp_get_attachment_image_src($bgimage,'full'); | |
$opacity1 = (!empty($opacity) ? 'opacity:'.esc_attr( $opacity ).';' : ' '); | |
$overlay_color1 = (!empty($overlay_color) ? 'background:'.esc_attr( $overlay_color ).';' : ' '); | |
ob_start(); ?> | |
<!-- WPMUDEV Custom --> | |
<div class="section boxed-width padding-top-bottom-parallax"> | |
<?php if( $img ): ?> | |
<div class="parallax-sep-2" <?php if($bgimage !=''){ ?> style="background-image: url(<?php echo esc_url($this->generate_cdn_url( $img[0] ) ); ?>);" <?php } ?> ></div> | |
<?php endif; ?> | |
<div class="sep-background-mask" <?php if($opacity or $overlay_color){ ?> style="<?php echo htmlspecialchars_decode($overlay_color1).htmlspecialchars_decode($opacity1); ?>" <?php } ?> ></div> | |
<div class="container"> | |
<div class="twelve columns"> | |
<div id="owl-logos" class="owl-carousel owl-theme"> | |
<?php | |
$img_ids = explode(",",$gallery); | |
foreach( $img_ids AS $img_id ){ | |
$meta = wp_prepare_attachment_for_js($img_id); | |
$caption = $meta['caption']; | |
$title = $meta['title']; | |
$alt = $meta['alt']; | |
$description = $meta['description']; | |
$image_src = wp_get_attachment_image_src($img_id,''); | |
if( ! $image_src ) continue; | |
$image_src = $this->generate_cdn_url( $image_src[0] ); | |
?> | |
<div class="item"><a href="<?php echo esc_url($caption); ?>" target="_blank" ><img src="<?php echo esc_url( $image_src ); ?>" alt=""></a></div> | |
<?php } ?> | |
</div> | |
</div> | |
</div> | |
</div> | |
<?php | |
return ob_get_clean(); | |
} | |
public function is_enabled_cdn(){ | |
if( is_null( $this->enabled_cdn ) ){ | |
$this->enabled_cdn = WP_Smush_Settings::get_instance()->get('cdn'); | |
} | |
return $this->enabled_cdn; | |
} | |
public function cdn_core(){ | |
if( is_null( $this->cdn_core ) ){ | |
$this->cdn_core = WP_Smush::get_instance()->core()->mod->cdn; | |
} | |
return $this->cdn_core; | |
} | |
public function generate_cdn_url($image_url){ | |
if( $this->is_enabled_cdn() && $this->is_supported_path( $image_url ) ){ | |
$image_url = $this->cdn_core()->generate_cdn_url( $image_url ); | |
} | |
return $image_url; | |
} | |
/** | |
* Check if the image path is supported by the CDN. | |
* | |
* @since 3.0 | |
* | |
* @param string $src Image path. | |
* | |
* @return bool|string | |
*/ | |
private function is_supported_path( $src ) { | |
$url_parts = wp_parse_url( $src ); | |
// Unsupported scheme. | |
if ( isset( $url_parts['scheme'] ) && 'http' !== $url_parts['scheme'] && 'https' !== $url_parts['scheme'] ) { | |
return false; | |
} | |
if ( ! isset( $url_parts['scheme'] ) && 0 === strpos( $src, '//' ) ) { | |
$src = is_ssl() ? 'https:' : 'http:' . $src; | |
} | |
// This is a relative path, try to get the URL. | |
if ( ! isset( $url_parts['host'] ) && ! isset( $url_parts['scheme'] ) ) { | |
$src = site_url( $src ); | |
} | |
$mapped_domain = $this->check_mapped_domain(); | |
if ( false === strpos( $src, content_url() ) || ( is_multisite() && $mapped_domain && false === strpos( $src, $mapped_domain ) ) ) { | |
return false; | |
} | |
// Allow only these extensions in CDN. | |
$ext = strtolower( pathinfo( $src, PATHINFO_EXTENSION ) ); | |
if ( ! in_array( $ext, array( 'gif', 'jpg', 'jpeg', 'png' ), true ) ) { | |
return false; | |
} | |
return $src; | |
} | |
/** | |
* Support for domain mapping plugin. | |
* | |
* @since 3.1.1 | |
*/ | |
private function check_mapped_domain() { | |
if ( ! is_multisite() ) { | |
return false; | |
} | |
if ( ! defined( 'DOMAINMAP_BASEFILE' ) ) { | |
return false; | |
} | |
$domain = wp_cache_get( 'smush_mapped_site_domain', 'smush' ); | |
if ( ! $domain ) { | |
global $wpdb; | |
$domain = $wpdb->get_var( | |
$wpdb->prepare( | |
"SELECT domain FROM {$wpdb->base_prefix}domain_mapping WHERE blog_id = %d ORDER BY id ASC LIMIT 1", | |
get_current_blog_id() | |
) | |
); // Db call ok. | |
if ( null !== $domain ) { | |
wp_cache_add( 'smush_mapped_site_domain', $domain, 'smush' ); | |
} | |
} | |
return $domain; | |
} | |
} | |
$run = new WPMUDEV_Smush_Custom_Shortcode_OTComposer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment