PluginProbe
Simple Chat Button / 1.5.0
Simple Chat Button v1.5.0
trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3
simple-chat-button / simple-chat-button.php

simple-chat-button.php in Simple Chat Button 1.5.0, at simple-chat-button.php

190 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: Simple Chat Button
4 * Description: Adds a beautiful WhatsApp Sticky Button on the WordPress frontend.
5 * Author: Rasoul Mousavian
6 * Author URI: https://seramo.ir
7 * Version: 1.5.0
8 * License: GPLv2
9 * Text Domain: scb
10 * Domain Path: /languages/
11 */
12
13 // Exit if accessed directly
14 if (!defined('ABSPATH')) {
15 exit;
16 }
17
18 // Define constants
19 define('SCB_VERSION', '1.5.0');
20 define('SCB_NAME', plugin_basename(__FILE__));
21 define('SCB_DIR', plugin_dir_path(__FILE__));
22 define('SCB_URI', plugin_dir_url(__FILE__));
23 define('SCB_INC', trailingslashit(SCB_DIR . 'includes'));
24
25 // Check main class exists
26 if (!class_exists('SCB_Main')) {
27
28 // Include whatsapp chat button frontend
29 include SCB_INC . 'frontend.php';
30
31 // Define main class
32 class SCB_Main {
33
34 public function __construct() {
35 // Add settings page
36 add_action('admin_menu', array($this, 'scb_settings_page'));
37
38 // Register settings
39 add_action('admin_init', array($this, 'scb_register_settings'));
40
41 // Add custom meta box
42 add_action('add_meta_boxes', array($this,'scb_add_custom_meta_box'));
43
44 // Add save custom meta box data
45 add_action('save_post', array($this, 'scb_save_post_data'));
46
47 // Add settings link
48 add_filter('plugin_action_links_' . SCB_NAME, array($this, 'scb_add_settings_link'));
49
50 // Loads translated strings
51 load_plugin_textdomain('scb', false, dirname(SCB_NAME) . '/languages');
52 }
53
54 // Add setting page to options submenu
55 function scb_settings_page() {
56 add_submenu_page(
57 'options-general.php',
58 esc_html__('Simple Chat Button', 'scb'),
59 esc_html__('Simple Chat Button', 'scb'),
60 'manage_options',
61 'simple-chat-button',
62 array($this, 'scb_settings_page_callback')
63 );
64 }
65
66 // Register settings
67 function scb_register_settings() {
68 $scb_settings_args = array (
69 'sanitize_callback' => 'sanitize_text_field'
70 );
71 $scb_whatsapp_options = array(
72 'scb_whatsapp_number',
73 'scb_whatsapp_chat_text'
74 );
75 $scb_button_options = array(
76 'scb_button_status',
77 'scb_button_text',
78 'scb_button_target',
79 'scb_button_position',
80 'scb_button_z_index',
81 'scb_desktop_link_type',
82 'scb_desktop_bottom_margin',
83 'scb_tablet_bottom_margin',
84 'scb_mobile_bottom_margin'
85 );
86 foreach ($scb_whatsapp_options as $option) {
87 register_setting('scb-whatsapp-settings', $option, $scb_settings_args);
88 }
89 foreach ($scb_button_options as $option) {
90 register_setting('scb-button-settings', $option, $scb_settings_args);
91 }
92 // Initialize options
93 add_option('scb_whatsapp_chat_text', esc_html__('Hello', 'scb'));
94 add_option('scb_button_status', '1');
95 add_option('scb_button_text', esc_html__('Need Help?', 'scb'));
96 add_option('scb_button_target', '_blank');
97 add_option('scb_button_position', 'right');
98 add_option('scb_desktop_link_type', 'web');
99 add_option('scb_desktop_bottom_margin', '20');
100 add_option('scb_tablet_bottom_margin', '20');
101 add_option('scb_mobile_bottom_margin', '20');
102 }
103
104 // Settings page callback
105 function scb_settings_page_callback() {
106 // Check access
107 if (!current_user_can("manage_options") && !is_admin()) {
108 return;
109 }
110 // Include settings page
111 include SCB_INC . 'settings-page.php';
112 }
113
114 // Add custom meta box
115 function scb_add_custom_meta_box() {
116 $screens = array(
117 'post',
118 'page',
119 );
120 foreach ($screens as $screen) {
121 add_meta_box(
122 'scb_custom_meta_box',
123 esc_html__('Simple Chat Button Settings', 'scb'),
124 array($this, 'scb_custom_meta_box_callback'),
125 $screen,
126 'normal',
127 'default',
128 );
129 }
130 }
131
132 // Custom meta box html
133 function scb_custom_meta_box_callback($post) {
134 // Check access
135 if (!current_user_can("manage_options") && !is_admin()) {
136 return;
137 }
138 // Include meta box html
139 include SCB_INC . 'meta-box.php';
140 }
141
142 // Save custom meta box data
143 function scb_save_post_data($post_id){
144 // Check plugin nonce is set
145 if (!isset($_POST['scb_settings_meta_box_nonce'])) {
146 return $post_id;
147 }
148
149 // Verify that the nonce is valid
150 $nonce = sanitize_text_field($_POST['scb_settings_meta_box_nonce']);
151 if (!wp_verify_nonce($nonce, 'scb_settings_meta_box')) {
152 return $post_id;
153 }
154
155 // Check auto save form
156 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
157 return $post_id;
158 }
159
160 // Check the user permissions
161 if ('page' == $_POST['post_type']) {
162 if (!current_user_can('edit_page', $post_id)) {
163 return $post_id;
164 }
165 } else {
166 if (!current_user_can('edit_post', $post_id)) {
167 return $post_id;
168 }
169 }
170
171 // Sanitize the user input and save post meta
172 $button_hide_status = sanitize_text_field($_POST['scb_button_hide_status']);
173 if (!empty($button_hide_status)) {
174 update_post_meta($post_id, '_scb_button_hide_status', $button_hide_status);
175 } else {
176 delete_post_meta($post_id, '_scb_button_hide_status');
177 }
178 }
179
180 // Add settings link
181 function scb_add_settings_link($links) {
182 $links[] = sprintf('<a href="%1$s">%2$s</a>', admin_url('options-general.php?page=simple-chat-button'), esc_html__('Settings', 'scb'));
183 $links[] = sprintf('<a href="https://seramo.ir">%1$s</a>', esc_html__('Website', 'scb'));
184 return $links;
185 }
186
187 }
188
189 new SCB_Main();
190 }