PluginProbe
Simple Chat Button / trunk
Simple Chat Button vtrunk
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 trunk, at simple-chat-button.php

186 lines 6.5 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.9.3
8 * License: GPLv2
9 * Text Domain: simple-chat-button
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_VER', '1.9.3');
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 . 'custom_functions.php';
30 include SCB_INC . 'frontend.php';
31
32 // Define main class
33 class SCB_Main {
34
35 public function __construct() {
36 // Add settings page
37 add_action('admin_menu', array($this, 'scb_settings_page'));
38
39 // Register settings
40 add_action('admin_init', array($this, 'scb_register_settings'));
41
42 // Add custom meta box
43 add_action('add_meta_boxes', array($this,'scb_add_custom_meta_box'));
44
45 // Add save custom meta box data
46 add_action('save_post', array($this, 'scb_save_post_data'), 10, 2);
47
48 // Add settings link
49 add_filter('plugin_action_links_' . SCB_NAME, array($this, 'scb_add_settings_link'));
50
51 // Loads translated strings
52 load_plugin_textdomain('simple-chat-button', false, dirname(SCB_NAME) . '/languages');
53 }
54
55 // Add setting page to options submenu
56 function scb_settings_page() {
57 add_submenu_page(
58 'options-general.php',
59 esc_html__('Simple Chat Button', 'simple-chat-button'),
60 esc_html__('Simple Chat Button', 'simple-chat-button'),
61 'manage_options',
62 'simple-chat-button',
63 array($this, 'scb_settings_page_callback')
64 );
65 }
66
67 // Register settings
68 function scb_register_settings() {
69 $scb_settings_args = array (
70 'sanitize_callback' => 'sanitize_text_field'
71 );
72 $scb_whatsapp_options = array(
73 'scb_whatsapp_number',
74 'scb_whatsapp_chat_text'
75 );
76 $scb_button_options = array(
77 'scb_button_status',
78 'scb_button_text',
79 'scb_button_target',
80 'scb_button_position',
81 'scb_button_z_index',
82 'scb_desktop_link_type',
83 'scb_desktop_bottom_margin',
84 'scb_tablet_bottom_margin',
85 'scb_mobile_bottom_margin'
86 );
87 foreach ($scb_whatsapp_options as $option) {
88 register_setting('scb-whatsapp-settings', $option, $scb_settings_args);
89 }
90 foreach ($scb_button_options as $option) {
91 register_setting('scb-button-settings', $option, $scb_settings_args);
92 }
93 // Initialize options
94 add_option('scb_whatsapp_chat_text', esc_html__('Hello', 'simple-chat-button'));
95 add_option('scb_button_status', '1');
96 add_option('scb_button_text', esc_html__('Need Help?', 'simple-chat-button'));
97 add_option('scb_button_target', '_blank');
98 add_option('scb_button_position', 'right');
99 add_option('scb_desktop_link_type', 'api');
100 add_option('scb_desktop_bottom_margin', '20');
101 add_option('scb_tablet_bottom_margin', '20');
102 add_option('scb_mobile_bottom_margin', '20');
103 }
104
105 // Settings page callback
106 function scb_settings_page_callback() {
107 // Check access
108 if (!current_user_can("manage_options") && !is_admin()) {
109 return;
110 }
111 // Include settings page
112 include SCB_INC . 'settings-page.php';
113 }
114
115 // Add custom meta box
116 function scb_add_custom_meta_box() {
117 $screens = array(
118 'post',
119 'page',
120 );
121 foreach ($screens as $screen) {
122 add_meta_box(
123 'scb_custom_meta_box',
124 esc_html__('Simple Chat Button Settings', 'simple-chat-button'),
125 array($this, 'scb_custom_meta_box_callback'),
126 $screen,
127 'normal',
128 'default'
129 );
130 }
131 }
132
133 // Custom meta box html
134 function scb_custom_meta_box_callback($post) {
135 // Check access
136 if (!current_user_can("manage_options") && !is_admin()) {
137 return;
138 }
139 // Include meta box html
140 include SCB_INC . 'meta-box.php';
141 }
142
143 // Save custom meta box data
144 function scb_save_post_data($post_id, $post){
145 // Check plugin nonce is set
146 if (!isset($_POST['scb_settings_meta_box_nonce'])) {
147 return $post_id;
148 }
149
150 // Verify that the nonce is valid
151 $nonce = sanitize_text_field($_POST['scb_settings_meta_box_nonce']);
152 if (!wp_verify_nonce($nonce, 'scb_settings_meta_box')) {
153 return $post_id;
154 }
155
156 // Check auto save form
157 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
158 return $post_id;
159 }
160
161 // Check the user permissions
162 $permission = ($post->post_type == 'page') ? 'edit_page' : 'edit_post';
163 if (!current_user_can($permission, $post_id)) {
164 return $post_id;
165 }
166
167 // Sanitize the user input and save post meta
168 $button_hide_status = isset($_POST['scb_button_hide_status']) ? sanitize_text_field($_POST['scb_button_hide_status']) : '';
169 if (!empty($button_hide_status)) {
170 update_post_meta($post_id, '_scb_button_hide_status', $button_hide_status);
171 } else {
172 delete_post_meta($post_id, '_scb_button_hide_status');
173 }
174 }
175
176 // Add settings link
177 function scb_add_settings_link($links) {
178 $links[] = sprintf('<a href="%1$s">%2$s</a>', admin_url('options-general.php?page=simple-chat-button'), esc_html__('Settings', 'simple-chat-button'));
179 $links[] = sprintf('<a href="https://seramo.ir">%1$s</a>', esc_html__('Website', 'simple-chat-button'));
180 return $links;
181 }
182
183 }
184
185 new SCB_Main();
186 }