| 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.7.0 |
| 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_VERSION', '1.7.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 . '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')); |
| 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', 'web'); |
| 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){ |
| 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 |
if ('page' == $_POST['post_type']) { |
| 163 |
if (!current_user_can('edit_page', $post_id)) { |
| 164 |
return $post_id; |
| 165 |
} |
| 166 |
} else { |
| 167 |
if (!current_user_can('edit_post', $post_id)) { |
| 168 |
return $post_id; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// Sanitize the user input and save post meta |
| 173 |
$button_hide_status = sanitize_text_field($_POST['scb_button_hide_status']); |
| 174 |
if (!empty($button_hide_status)) { |
| 175 |
update_post_meta($post_id, '_scb_button_hide_status', $button_hide_status); |
| 176 |
} else { |
| 177 |
delete_post_meta($post_id, '_scb_button_hide_status'); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// Add settings link |
| 182 |
function scb_add_settings_link($links) { |
| 183 |
$links[] = sprintf('<a href="%1$s">%2$s</a>', admin_url('options-general.php?page=simple-chat-button'), esc_html__('Settings', 'simple-chat-button')); |
| 184 |
$links[] = sprintf('<a href="https://seramo.ir">%1$s</a>', esc_html__('Website', 'simple-chat-button')); |
| 185 |
return $links; |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
new SCB_Main(); |
| 191 |
} |