????
| Current Path : /home2/morganrand/www/wp-content/themes/wanderfuls-2/framework/classes/widgets/ |
| Current File : /home2/morganrand/www/wp-content/themes/wanderfuls-2/framework/classes/widgets/social.php |
<?php
/**
* Image social widget
*
* Learn more: http://codex.wordpress.org/Widgets_API
*
* @package Wanderfuls WordPress Theme
* @subpackage Widgets
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Start class
if ( ! class_exists( 'TB_Social_Widget' ) ) {
class TB_Social_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*
* @since 1.0.0
*/
function __construct() {
parent::__construct(
'tb_social_widget',
TB_THEME_BRANDING . ' - '. __( 'Image Icons Social Widget', 'tb' ),
array(
'description' => __( 'Displays icons with links to your social profiles with drag and drop support.', 'tb' )
)
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
* @since 1.0.0
*
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
function widget( $args, $instance ) {
// Extract args
extract( $args );
// Get social services and return nothing if none are defined
$social_services = isset( $instance['social_services'] ) ? $instance['social_services'] : '';
if ( ! $social_services ) return;
// Define vars
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$style = isset( $instance['style'] ) ? $instance['style'] : '';
$target = isset( $instance['target'] ) ? $instance['target'] : '';
$size = isset( $instance['size'] ) ? $instance['size'] : '';
$img_location = get_template_directory_uri() .'/images/social/';
// Sanitize vars
$target = 'blank' == $target ? ' target="_blank"' : '';
$size = $size ? intval( $size ) : '';
// Apply filters
$title = apply_filters( 'widget_title', $title );
$img_location = apply_filters( 'tb_social_widget_img_dir', $img_location ); ?>
<?php
// Before widget WP hook
echo $before_widget; ?>
<?php
// Display widget title if defined
if ( $title ) {
echo $before_title . $title . $after_title;
} ?>
<ul class="tb-social-widget-output">
<?php foreach( $social_services as $key => $service ) { ?>
<?php $link = ! empty( $service['url'] ) ? esc_url( $service['url'] ) : null; ?>
<?php $name = $service['name']; ?>
<?php if ( $link ) { ?>
<?php echo '<li><a href="'. $link .'" title="'. esc_attr( $name ) .'"'. $target .'><img src="'. $img_location . strtolower ( $name ) .'.png" alt="'. esc_attr( $name ) .'" height="'. $size .'" width="'. $size .'" /></a></li>'; ?>
<?php } ?>
<?php } ?>
</ul><!-- .tb-social-widget-output -->
<?php
// After widget WP hook
echo $after_widget; ?>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
* @since 1.0.0
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
function update( $new, $old ) {
$instance = $old;
$instance['title'] = ! empty( $new['title'] ) ? strip_tags( $new['title'] ) : null;
$instance['style'] = ! empty( $new['style'] ) ? strip_tags( $new['style'] ) : 'color-square';
$instance['target'] = ! empty( $new['target'] ) ? strip_tags( $new['target'] ) : 'blank';
$instance['size'] = ! empty( $new['size'] ) ? strip_tags( $new['size'] ) : '32px';
$instance['social_services'] = $new['social_services'];
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
* @since 1.0.0
*
* @param array $instance Previously saved values from database.
*/
function form( $instance ) {
$defaults = array(
'title' => __('Follow Us','tb'),
'style' => 'color-square',
'target' => 'blank',
'size' => '30px',
'social_services' => array(
'twitter' => array(
'name' => 'Twitter',
'url' => ''
),
'facebook' => array(
'name' => 'Facebook',
'url' => ''
),
'instagram' => array(
'name' => 'Instagram',
'url' => ''
),
'linkedin' => array(
'name' => 'LinkedIn',
'url' => ''
),
'pinterest' => array(
'name' => 'Pinterest',
'url' => ''
),
'googleplus' => array(
'name' => 'GooglePlus',
'url' => ''
),
'rss' => array(
'name' => 'RSS',
'url' => ''
),
'dribbble' => array(
'name' => 'Dribbble',
'url' => ''
),
'flickr' => array(
'name' => 'Flickr',
'url' => ''
),
'forrst' => array(
'name' => 'Forrst',
'url' => ''
),
'github' => array(
'name' => 'GitHub',
'url' => ''
),
'tumblr' => array(
'name' => 'Tumblr',
'url' => ''
),
'vimeo' => array(
'name' => 'Vimeo',
'url' => ''
),
'youtube' => array(
'name' => 'Youtube',
'url' => ''
),
),
);
$instance = wp_parse_args( ( array ) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','tb'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('target'); ?>"><?php _e('Link Target:', 'tb'); ?></label>
<br />
<select class='tb-widget-select' name="<?php echo $this->get_field_name('target'); ?>" id="<?php echo $this->get_field_id('target'); ?>">
<option value="blank" <?php if($instance['target'] == 'blank') { ?>selected="selected"<?php } ?>><?php _e( 'Blank', 'tb' ); ?></option>
<option value="self" <?php if($instance['target'] == 'self') { ?>selected="selected"<?php } ?>><?php _e( 'Self', 'tb' ); ?></option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('size'); ?>"><?php _e('Size:', 'tb'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('size'); ?>" name="<?php echo $this->get_field_name('size'); ?>" type="text" value="<?php echo $instance['size']; ?>" />
<small><?php _e('Size in pixels. Icon images are 36px.', 'tb'); ?></small>
</p>
<?php
$field_id_services = $this->get_field_id( 'social_services' );
$field_name_services = $this->get_field_name( 'social_services' ); ?>
<h3 style="margin-top:20px;margin-bottom:0;"><?php _e( 'Social Links','tb' ); ?></h3>
<small style="display:block;margin-bottom:10px;"><?php _e( 'Enter the full URL to your social profile', 'tb' ); ?></small>
<ul id="<?php echo $field_id_services; ?>" class="tb-services-list">
<input type="hidden" id="<?php echo $field_name_services; ?>" value="<?php echo $field_name_services; ?>">
<input type="hidden" id="<?php echo wp_create_nonce( 'tb_fontawesome_social_widget_nonce' ); ?>">
<?php
$display_services = isset ( $instance['social_services'] ) ? $instance['social_services']: '';
if ( ! empty( $display_services ) ) {
foreach( $display_services as $key => $service ) {
$url = isset( $service['url'] ) ? $service['url'] : 0;
$name = isset( $service['name'] ) ? $service['name'] : ''; ?>
<li id="<?php echo $field_id_services; ?>_0<?php echo $key ?>">
<p>
<label for="<?php echo $field_id_services; ?>-<?php echo $key ?>-name"><?php echo $name; ?>:</label>
<input type="hidden" id="<?php echo $field_id_services; ?>-<?php echo $key ?>-url" name="<?php echo $field_name_services .'['.$key.'][name]'; ?>" value="<?php echo $name; ?>">
<input type="url" class="widefat" id="<?php echo $field_id_services; ?>-<?php echo $key ?>-url" name="<?php echo $field_name_services .'['.$key.'][url]'; ?>" value="<?php echo $url; ?>" />
</p>
</li>
<?php }
} ?>
</ul>
<?php
}
}
}
// Register the TB_Tabs_Widget custom widget
if ( ! function_exists( 'register_tb_social_widget' ) ) {
function register_tb_social_widget() {
register_widget( 'TB_Social_Widget' );
}
}
add_action( 'widgets_init', 'register_tb_social_widget' );
// Widget Styles
if ( ! function_exists( 'tb_social_widget_style' ) ) {
function tb_social_widget_style() { ?>
<style>
.tb-services-list li {
cursor: move;
background: #fafafa;
padding: 10px;
border: 1px solid #e5e5e5;
margin-bottom: 10px;
}
.tb-services-list li p {
margin: 0;
}
.tb-services-list li label {
margin-bottom: 3px;
display: block;
color: #222;
}
.tb-services-list .placeholder {
border: 1px dashed #e3e3e3;
}
</style>
<?php
}
}
// Widget AJAX functions
function load_tb_social_widget_scripts() {
if ( ! is_admin() ) {
return;
}
global $pagenow;
if ( $pagenow == "widgets.php" ) {
add_action( 'admin_head', 'tb_social_widget_style' );
add_action( 'admin_footer', 'add_new_tb_social_ajax_trigger' );
function add_new_tb_social_ajax_trigger() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
jQuery(document).ajaxSuccess(function(e, xhr, settings) { //fires when widget saved
var widget_id_base = 'tb_social_widget';
if(settings.data.search('action=save-widget') != -1 && settings.data.search('id_base=' + widget_id_base) != -1) {
tbSortServices();
}
});
function tbSortServices() {
jQuery('.tb-services-list').each( function() {
var id = jQuery(this).attr('id');
$('#'+ id).sortable({
placeholder: "placeholder",
opacity: 0.6
});
});
}
tbSortServices();
});
</script>
<?php
}
}
}
add_action( 'admin_init', 'load_tb_social_widget_scripts' );