/
whatsapp-sharebutton.php
195 lines (172 loc) · 4.01 KB
/
whatsapp-sharebutton.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* WhatsApp Share Button for Developers
*
* Adds a configurable share button for WhatsApp via shortcode.
*
* @link https://github.com/K3CK/wp-whatsapp-sharebutton
* @since 1.0.0
* @package Whatsapp_Sharebutton
*
* @wordpress-plugin
* Plugin Name: WhatsApp Share Button
* Plugin URI: https://github.com/K3CK/wp-whatsapp-sharebutton
* Description: Adds a simple WhatsApp share button via shortcode.
* Version: 1.0.3
* Author: Martin Keck
* Author URI: https://www.martinkeck.com
* License: GPLv3
* Text Domain: whatsapp-sharebutton
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* WP-Sweep version
*
* @since 1.0.0
*/
define( 'WHATSAPP_SHAREBUTTON_VERSION', '1.0.3' );
/**
* WP-Sweep class
*
* @since 1.0.0
*/
class Whatsapp_Sharebutton {
/**
* Static instance
*
* @since 1.0.0
*
* @access private
* @var $instance
*/
private static $instance;
/**
* Constructor method
*
* @since 1.0.0
*
* @access public
*/
public function __construct() {
// Add Plugin Hooks
add_action( 'plugins_loaded', array( $this, 'add_hooks' ) );
// Add Shortcodes
add_action( 'plugins_loaded', array( $this, 'add_shortcodes') );
// Load Translation
load_plugin_textdomain( 'whatsapp-sharebutton' );
// Plugin Activation/Deactivation
register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'plugin_deactivation' ) );
}
/**
* Initializes the plugin object and returns its instance
*
* @since 1.0.0
*
* @access public
* @return object The plugin object instance
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Adds all the plugin hooks
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function add_hooks() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
}
/**
* Adds all the plugin shortcodes
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function add_shortcodes() {
add_shortcode( 'whatsapp-sharebutton', array( $this, 'whatsapp_sharebutton' ) );
}
/**
* Add share button
*
* @since 1.0.0
*
* @access public
* @return string
*/
public function whatsapp_sharebutton( $atts ) {
$atts = shortcode_atts(
array(
'button_label' => '',
'target_url' => get_permalink(),
'message_text' => get_the_title(),
'message_link_separator' => ': ',
'wrapper_class' => 'whatsapp_sharebutton_wrap',
'button_class' => 'whatsapp_sharebutton',
),
$atts
);
$button = '<div class="' . $atts['wrapper_class'] . '">';
$button .= '<a href="whatsapp://send?text='.$atts['message_text'] . $atts['message_link_separator'] . $atts['target_url'].'" class="' . $atts['button_class'] . '" title="' . $atts['button_label'] . '">' . $atts['button_label'] . '</a>';
$button .= '</div>';
return $button;
}
/**
* Enqueue CSS files
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function enqueue_styles( ) {
wp_enqueue_style( 'whatsapp_sharebutton_style', plugin_dir_url( __FILE__ ) . 'styles.css', array(), WHATSAPP_SHAREBUTTON_VERSION, 'all' );
}
/**
* Admin menu
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function admin_menu() {
// add_management_page( __( 'Sweep', 'wp-sweep' ), __( 'Sweep', 'wp-sweep' ), 'manage_options', 'wp-sweep/admin.php' );
}
/**
* What to do when the plugin is being deactivated
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function plugin_activation() {
}
/**
* What to do when the plugin is being activated
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function plugin_deactivation() {
}
}
/**
* Init Whatsapp_Sharebutton
*/
Whatsapp_Sharebutton::get_instance();