File: /home/zeusxp5/zenoxpressjobs.com/wp-content/themes/zxldfw-tcompanyllcMain/widgets/webcom-cta-01.php
<?php
/**
* Plugin Name: Custom CTA Widget
* Plugin URI: http://www.web.com
* Description: Custom CTA Text Area Widget
* Author: Web.com
* Author URI: http://www.web.com
* Version: 2.0
*
* To duplicate this widget code, save this file as a new file name (eg: webcom-widget-02.php).
* Change any occurrence of WebcomWidget01 inside this file to WebcomWidget02.
* Inside the themes functions.php file, add the line of include_once 'widgets/webcom-cta-02.php';
*
* @package WebcomWidget01
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'No direct access allowed' );
}
add_action( 'widgets_init', function() {
return register_widget( 'WebcomWidget01' );
});
/**
* WebcomWidget01 is a class to create a custom WordPress widget.
*
* @package WebcomWidget01
* @author Web.com
* @version 2.0
* @access public
*/
class WebcomWidget01 extends WP_Widget {
/**
* Settings array
*
* @var array $setings Array of widget settings.
* @access private
*/
private $settings = array(
'widget_name' => 'A Custom Web.com Widget',
'widget_desc' => '',
'show_image' => true,
'show_fa' => false,
'show_title' => true,
'show_textarea' => true,
'show_learn_more' => true,
'image_as_bg' => false,
'image_fa_clickable' => true,
'title_clickable' => true,
'wrap_anchor' => false,
'wrap_content' => false,
'wrap_content_class' => '',
);
/**
* WebcomWidget01 widget constructor
*
* @access public
*/
public function __construct() {
parent::__construct( false, $this->settings['widget_name'], array( 'description' => $this->settings['widget_desc'] ) );
$this->settings['cta_id'] = sanitize_title( $this->settings['widget_name'] );
if ( is_admin() ) {
add_action( 'admin_enqueue_scripts', 'wp_enqueue_media' );
add_action( 'admin_print_scripts', array( $this, 'upload_scripts' ) );
add_action( 'admin_print_styles', array( $this, 'upload_styles' ) );
}
}
/**
* Output custom CSS to enhance the layout of widget items
*
* @access public
*/
public function upload_styles() {
echo '<style>
.webcom_widget_upload_button:before, .webcom_widget_upload_button:after { content: \'\'; display: table; line-height: 0; }
.webcom_widget_upload_button:after { clear: both; }
.widget.open .webcom_widget_upload_button button { margin-top: 4px; }
.widget.open .webcom_widget_upload_button button { margin-top: 4px; }
.widget.open .webcom_widget_upload_button label.has_image { display: none; }
img.cta-img-preview {max-width: 100%; height: auto; display: block; margin: 0 auto;}
</style>';
}
/**
* Output custom Javascript for media uploads.
*
* @access public
*/
public function upload_scripts() {
echo '<script>
window.addEventListener("load", function(){
jQuery(document).ready(function ($) {
$(document).on("click", ".' . $this->settings['cta_id'] . '_webcom_cta_upload_image_button", function(e){
e.preventDefault();
var $button = $(this);
// Create the media frame.
var file_frame = wp.media.frames.file_frame = wp.media({
title: \'Select or upload image\',
library: { // remove these to show all
type: \'image\' // specific mime
},
button: {
text: \'Select\'
},
multiple: false // Set to true to allow multiple files to be selected
});
// When an image is selected, run a callback.
file_frame.on(\'select\', function () {
// We set multiple to false so only get one image from the uploader
var attachment = file_frame.state().get(\'selection\' ).first().toJSON();
$button.siblings(\'label\').addClass("has_image");
$button.siblings(\'input.imgurl\').val(attachment.url);
$button.siblings(\'input.imgid\').val(attachment.id);
$button.siblings(\'img.cta-img-preview\').css(\'display\', \'block\').attr("src",attachment.url).attr("srcset","").attr("width",attachment.width).attr("height",attachment.height);
$button.siblings(\'.' . $this->settings['cta_id'] . '_webcom_cta_remove_image_button\').css(\'display\', \'inline-block\');
$button.html(\'Change Image\');
});
// Finally, open the modal
file_frame.open();
});
$(document).on("click", ".' . $this->settings['cta_id'] . '_webcom_cta_remove_image_button", function(e){
e.preventDefault();
var $button = $(this);
$button.siblings(\'input.imgid\').val(0);
$button.siblings(\'img.cta-img-preview\').css(\'display\', \'none\');
$button.css(\'display\', \'none\');
$button.siblings(\'.' . $this->settings['cta_id'] . '_webcom_cta_upload_image_button\').html(\'Select Image\');
$button.siblings(\'label\').removeClass("has_image");
});
});
});
</script>';
}
/**
* Calculate new dimensions for scaling the image.
*
* @param array $size width height of exiting image.
* @access public
*/
public function scale_image( $size = array() ) {
// Target dimensions.
$max_width = ($size['width'] * 2);
// If the target dimensions do not exceed 780, then force it to be 780.
if ( $max_width < 780 ) {
$max_width = 780;
}
// If the target dimensions exceeds 2000, then force it to be 2000.
if ( $max_width > 2000 ) {
$max_width = 2000;
}
// Get current dimensions.
$old_width = $size['width'];
$old_height = $size['height'];
// Calculate the scaling we need to do to fit the image inside our frame.
$scale = $max_width / $old_width;
// Get the new dimensions.
$new_width = ceil( $scale * $old_width );
$new_height = ceil( $scale * $old_height );
return array( $new_width, $new_height );
}
/**
* Output custom CSS to enhance the layout of widget items
*
* @param array $instance Current widget settings.
* @access public
*/
public function form( $instance ) {
// Check values.
if ( $instance ) {
$title = $instance['title'];
$cta_img_id = $instance['cta_img_id'];
$cta_fa = $instance['cta_fa'];
$textarea = $instance['textarea'];
$learn_more_title = $instance['learn_more_title'];
$learn_more_url = $instance['learn_more_url'];
$learn_more_target = $instance['learn_more_target'];
} else {
$title = '';
$cta_img_id = 0;
$cta_fa = '';
$textarea = '';
$learn_more_title = '';
$learn_more_url = '';
$learn_more_target = '';
}
$number = $this->number;
$has_image = ( 0 !== intval( $cta_img_id ) ) ? true : false;
?>
<?php if ( $this->settings['show_image'] ) : ?>
<p class="webcom_widget_upload_button">
<label for="<?php echo esc_attr( $this->get_field_id( 'cta_img' ) ); ?>" class="<?php echo ( $has_image ) ? 'has_image' : ''; ?>">Image</label>
<?php
if ( $has_image ) {
echo wp_get_attachment_image( $cta_img_id, $this->settings['cta_id'].'-image', false, array( 'class' => 'cta-img-preview' ) );
} else {
echo '<img src="" style="display: none;" class="cta-img-preview">';
}
?>
<input class="imgid" id="<?php echo esc_attr( $this->get_field_id( 'cta_img_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'cta_img_id' ) ); ?>" type="hidden" value="<?php echo intval( $cta_img_id ); ?>" />
<button id="<?php echo $this->settings['cta_id']; ?>_webcom_widget_image_button_id_<?php echo intval( $number ); ?>" class="<?php echo $this->settings['cta_id']; ?>_webcom_cta_upload_image_button <?php echo $this->settings['cta_id']; ?>_webcom_widget_image_button_id_<?php echo intval( $number ); ?> button"><?php echo ( $has_image ) ? 'Change Image' : 'Select Image'; ?></button>
<button id="<?php echo $this->settings['cta_id']; ?>_webcom_widget_remove_image_button_id_<?php echo intval( $number ); ?>" class="<?php echo $this->settings['cta_id']; ?>_webcom_cta_remove_image_button <?php echo $this->settings['cta_id']; ?>_webcom_widget_remove_image_button_id_<?php echo intval( $number ); ?> button" style="<?php echo ($has_image) ? 'a' : 'display:none'; ?>">Remove Image</button>
</p>
<?php endif; ?>
<?php if ( $this->settings['show_fa'] ) : ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'cta_fa' ) ); ?>">Font Awesome Icon - <small><em>Use the <i> tag from <a href="http://fortawesome.github.io/Font-Awesome/icons/" target="_blank">Font Awesome Icons</a></em></small></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'cta_fa' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'cta_fa' ) ); ?>" type="text" value="<?php echo esc_html( $cta_fa ); ?>" />
</p>
<?php endif; ?>
<?php if ( $this->settings['show_title'] ) : ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">Title</label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>1" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>1" type="text" value="<?php echo esc_html( $title ); ?>" />
</p>
<?php endif; ?>
<?php if ( $this->settings['show_textarea'] ) : ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'textarea' ) ); ?>">Description</label>
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'textarea' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'textarea' ) ); ?>" rows="7" cols="20" ><?php echo esc_html( $textarea ); ?></textarea>
</p>
<?php endif; ?>
<?php if ( $this->settings['show_learn_more'] ) : ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'learn_more_title' ) ); ?>">Learn More Link Text</label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'learn_more_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'learn_more_title' ) ); ?>" type="text" value="<?php echo esc_html( $learn_more_title ); ?>" />
</p>
<?php endif; ?>
<?php if ( $this->settings['show_learn_more'] or $this->settings['image_fa_clickable'] or $this->settings['title_clickable'] or $this->settings['wrap_anchor'] ) : ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'learn_more_url' ) ); ?>"><?php echo ($this->settings['wrap_anchor']) ? 'Widget Link URL' : 'Learn More Link URL' ; ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'learn_more_url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'learn_more_url' ) ); ?>" type="text" value="<?php echo esc_attr( $learn_more_url ); ?>" placeholder="http://" />
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'learn_more_target' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'learn_more_target' ) ); ?>" type="checkbox" value="_blank" <?php echo ( '_blank' === $learn_more_target ) ? ' checked' : ''; ?>> <label for="<?php echo esc_attr( $this->get_field_id( 'learn_more_target' ) ); ?>" style="display:inline-block; vertical-align: sub;">Open in new window</label>
</p>
<?php endif; ?>
<?php
}
/**
* Output custom CSS to enhance the layout of widget items
*
* @param array $new_instance Current widget settings.
* @param array $old_instance Previous widget settings.
* @access public
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// Fields.
$instance['cta_img_id'] = $new_instance['cta_img_id'];
$instance['cta_fa'] = $new_instance['cta_fa'];
$instance['title'] = $new_instance['title'];
$instance['textarea'] = $new_instance['textarea'];
$instance['learn_more_title'] = $new_instance['learn_more_title'];
$instance['learn_more_url'] = $new_instance['learn_more_url'];
$instance['learn_more_target'] = $new_instance['learn_more_target'];
return $instance;
}
/**
* Output custom CSS to enhance the layout of widget items
*
* @param array $args WordPress widget options.
* @param array $instance Current widget settings.
* @access public
*/
public function widget( $args, $instance ) {
// These are the widget options.
$title = $instance['title'];
$textarea = $instance['textarea'];
$cta_img_id = intval( $instance['cta_img_id'] );
$cta_fa = $instance['cta_fa'];
$learn_more_title = $instance['learn_more_title'];
$learn_more_url = $instance['learn_more_url'];
$learn_more_target = $instance['learn_more_target'];
// Default widget variables.
$bg_image = '';
$srcset = '';
$src = '';
$width = 0;
$height = 0;
if ( $this->settings['image_as_bg'] ) {
$src_value = wp_get_attachment_image_src( $cta_img_id, $this->settings['cta_id'].'-image' );
if ( $src_value ) {
$src = esc_attr( $src_value[0] );
}
$bg_image = 'style="background-image: url(\'' . esc_attr( $src ) . '\')" ';
}
$target = ($learn_more_target && $learn_more_url) ? 'target="_blank"' : '';
// If there is an ID set then count number of widgets in the current sidebar for use in ssgrid.
if ( isset( $args['id'] ) or $bg_image ) {
if ( isset( $args['id'] ) ) {
$sidebar_id = $args['id'];
$total_widgets = wp_get_sidebars_widgets();
$sidebar_widgets_count = count( $total_widgets[ $sidebar_id ] );
} else {
$sidebar_widgets_count = 0;
}
// Replace the $before_widget class, with a grid class.
echo str_replace( 'class="', $bg_image . 'class="col-1-' . $sidebar_widgets_count . ' ', $args['before_widget'] ); // WPCS: XSS OK.
} else {
echo $args['before_widget']; // WPCS: XSS OK.
}
echo ( $this->settings['wrap_content'] ) ? '<div class="' . esc_attr( $this->settings['wrap_content_class'] ) . '">' : '';
echo ( $this->settings['wrap_anchor'] ) ? '<a href="' . esc_attr( $learn_more_url ) . '" target="' . $target . '">' : '';
// Output the title.
if ( '' !== $title and $this->settings['show_title'] ) {
echo $args['before_title']; // WPCS: XSS OK.
echo ( $this->settings['title_clickable'] and $learn_more_url and $this->settings['wrap_anchor'] === false ) ? '<a href="' . esc_attr( $learn_more_url ) . '" ' . $target . '>' : '';
echo $title; // WPCS: XSS OK.
echo ( $this->settings['title_clickable'] and $learn_more_url and $this->settings['wrap_anchor'] === false ) ? '</a>' : '';
echo $args['after_title']; // WPCS: XSS OK.
}
// Output the Textarea, if it's set.
if ( '' !== $textarea and $this->settings['show_textarea'] ) {
echo '<div class="widget-textarea">' ;
echo '<div class="widget-text">' . nl2br( $textarea ) . '</div>'; // WPCS: XSS OK.
echo '</div>';
}
// Output the CTA image, if it's set.
if ( 0 !== $cta_img_id and $this->settings['show_image'] and ! $this->settings['image_as_bg'] ) {
$alt = get_post_meta( $cta_img_id, '_wp_attachment_image_alt', true );
if ( '' === $title and '' === $alt ) {
$alt = get_bloginfo( 'name' );
}
if ( '' !== $title and '' === $alt ) {
$alt = $title;
}
if ( '' === $title and '' !== $alt ) {
$alt = $alt;
}
$srcset_value = wp_get_attachment_image_srcset( $cta_img_id, $this->settings['cta_id'].'-image' );
if ( $srcset_value ) {
$srcset = 'srcset="' . esc_attr( $srcset_value ) . '"';
}
$src_value = wp_get_attachment_image_src( $cta_img_id, $this->settings['cta_id'].'-image' );
if ( $src_value ) {
$src = 'src="' . esc_attr( $src_value[0] ) . '"';
$width = 'width="' . esc_attr( $src_value[1] ) . '"';
$height = 'height="' . esc_attr( $src_value[2] ) . '"';
}
echo '<div class="widget-img-wrap">';
echo ( $this->settings['image_fa_clickable'] and $learn_more_url and $this->settings['wrap_anchor'] === false ) ? '<a href="' . esc_attr( $learn_more_url ) . '" ' . $target . ' class="widget-img-link">' : '';
echo '<img ' . $src . ' ' . $width . ' ' . $height . ' alt="' . esc_attr( strip_tags( htmlspecialchars_decode( $alt ) ) ) . '" ' . $srcset . ' class="widget-img">';
echo ( $this->settings['image_fa_clickable'] and $learn_more_url and $this->settings['wrap_anchor'] === false ) ? '</a>' : '';
echo '</div>';
}
// Output the Font Awesome icon, if it's set.
if ( '' !== $cta_fa and $this->settings['show_fa'] ) {
if ( '<' !== $cta_fa[0] ) {
if ( 'fa-' !== substr( $cta_fa, 0, 3 ) ) {
$cta_fa = 'fa-' . $cta_fa;
}
$cta_fa = '<i class="fa ' . $cta_fa . '" aria-hidden="true"></i>';
}
echo ( $this->settings['image_fa_clickable'] and $learn_more_url and $this->settings['wrap_anchor'] === false ) ? '<a href="' . esc_attr( $learn_more_url ) . '" ' . $target . ' class="widget-fa-link">' : '';
echo $cta_fa; // WPCS: XSS OK.
echo ( $this->settings['image_fa_clickable'] and $learn_more_url and $this->settings['wrap_anchor'] === false ) ? '</a>' : '';
}
// Output the Learn More link, if it's set.
if ( '' !== $learn_more_title and $this->settings['show_learn_more'] ) {
echo '<div class="widget-learn-more">';
echo ( $this->settings['wrap_anchor'] === false ) ? '<a href="' . esc_attr( $learn_more_url ) . '" ' . $target . '>' : ''; // WPCS: XSS OK.
echo $learn_more_title; // WPCS: XSS OK.
echo ( $this->settings['wrap_anchor'] === false ) ? '</a>' : '';
echo '</div>';
}
echo ( $this->settings['wrap_anchor'] ) ? '</a>' : '';
echo ( $this->settings['wrap_content'] ) ? '</div>' : '';
echo $args['after_widget']; // WPCS: XSS OK.
}
}