-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocial-media.php
131 lines (110 loc) · 4.57 KB
/
social-media.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
Plugin Name: Social Media Menu
Description: Used to add a social media menu to your pages.
Version: 1.0.0
Author: ghm
Text Domain: socialmedia
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('SocialMedia_Widget')) {
class SocialMedia_Widget extends WP_Widget
{
const ICONS_CSS_CLASSES = [
"facebook" => "fab fa-facebook-f",
"instagram" => "fab fa-instagram",
"twitter" => "fab fa-twitter",
"snapchat" => "fab fa-snapchat",
"youtube" => "fab fa-youtube",
"pinterest" => "fab fa-pinterest",
"linkedin" => "fab fa-linkedin"
];
public function __construct()
{
load_plugin_textdomain('socialmedia');
parent::__construct(
'social-media',
__('Social Media', 'socialmedia'),
);
add_action('widgets_init', [$this, 'init']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']);
add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
}
public function init()
{
register_widget(self::class);
}
public function enqueue_admin_scripts($hook)
{
if ($hook == 'widgets.php') {
wp_register_script('font-awesome-icons', 'https://use.fontawesome.com/releases/v5.13.0/js/all.js', [], false, true);
wp_enqueue_script('font-awesome-icons');
wp_register_script('socialmedia-admin-script', plugins_url('js/social-media-admin.js', __FILE__), ['jquery'], false, true);
wp_enqueue_script('socialmedia-admin-script');
wp_register_style('socialmedia-admin-style', plugins_url('css/social-media-admin.css', __FILE__), [], false, 'all');
wp_enqueue_style('socialmedia-admin-style');
}
}
public function enqueue_scripts()
{
if (is_active_widget(false, false, $this->id_base)) {
wp_register_script('font-awesome-icons', 'https://use.fontawesome.com/releases/v5.13.0/js/all.js', [], false, true);
wp_enqueue_script('font-awesome-icons');
wp_register_style('socialmedia-style', plugins_url('css/social-media.css', __FILE__), [], false, 'all');
wp_enqueue_style('socialmedia-style');
}
}
public $args = array(
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
'before_widget' => '<div class="widget-wrap social-icons">',
'after_widget' => '</div></div>'
);
public function widget($args, $instance)
{
echo $args['before_widget'];
foreach ($instance['social_media'] as $value) {
$href = !empty($value['href']) ? esc_url($value['href']) : '#';
$class = !empty($value['class']) ? esc_attr($value['class']) : '';
echo '<a class="social-icon" href="' . $href . '" target="_blank"><i class="' . $class . '"></i></a>';
}
echo $args['after_widget'];
}
public function form($instance)
{
?>
<div id="widget-sm-<?= $this->number ?>" data-widget-number="<?= $this->number ?>">
<div style="margin:10px 0px 5px 0px;"><small>* Click to add a social media</small></div>
<div class="widget-social-icons" id="widget-social-icons" style="margin-bottom:20px;">
<?php foreach (self::ICONS_CSS_CLASSES as $class) : ?>
<div class="social-icon" data-class="<?= $class ?>" onclick="widget.addSocialMedia(this)"><i class="<?= $class ?>"></i></div>
<?php endforeach ?>
</div>
<?php foreach (($instance['social_media'] ?: []) as $k => $v) : ?>
<div style="display:flex; padding:3px 0px;" class="sm-input-wrapper">
<div class="widget-social-icons">
<div class="social-icon"><i class="<?= esc_attr($v['class']) ?>"></i></div>
</div>
<input class="widefat" name="<?= esc_attr($this->get_field_name('social_media[' . $k . '][class]')); ?>" type="hidden" value="<?= esc_attr($v['class']) ?: ''; ?>">
<input class="widefat" name="<?= esc_attr($this->get_field_name('social_media[' . $k . '][href]')); ?>" type="text" value="<?= esc_attr($v['href']) ?: ''; ?>" placeholder="<?= __("Enter link url", "socialmedia") ?>">
<div class="widget-social-icons">
<div class="delete" onclick="widget.deleteSocialMedia(this)"><i class="fas fa-times"></i></div>
</div>
</div>
<?php endforeach ?>
</div>
<?php
}
public function update($new_instance, $old_instance)
{
$instance = array();
$instance['social_media'] = (!empty($new_instance['social_media'])) ? $new_instance['social_media'] : '';
return $instance;
}
}
}
new SocialMedia_Widget();
?>