| 1 |
<?php |
| 2 |
namespace Add_Chat_App_Button; |
| 3 |
|
| 4 |
use Add_Chat_App_Button\Includes\Scripts_Manager; |
| 5 |
use Add_Chat_App_Button\Includes\Styles_Manager; |
| 6 |
use Add_Chat_App_Button\Admin\Admin_Settings; |
| 7 |
|
| 8 |
// Exit if accessed directly |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class Plugin { |
| 14 |
|
| 15 |
/** |
| 16 |
* Plugin Folder Name. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
const PLUGIN_NAME = 'add-whatsapp-button'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Plugin Options. |
| 25 |
* |
| 26 |
* Holds the scripts manager. |
| 27 |
* |
| 28 |
* @since 2.0.0 |
| 29 |
* @access public |
| 30 |
*/ |
| 31 |
public $plugin_options; |
| 32 |
|
| 33 |
/** |
| 34 |
* Scripts manager. |
| 35 |
* |
| 36 |
* Holds the scripts manager. |
| 37 |
* |
| 38 |
* @since 2.0.0 |
| 39 |
* @access public |
| 40 |
* |
| 41 |
* @var AWB_Scripts_Manager |
| 42 |
*/ |
| 43 |
public $scripts_manager; |
| 44 |
|
| 45 |
/** |
| 46 |
* Instance. |
| 47 |
* |
| 48 |
* Holds the plugin instance. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* @access public |
| 52 |
* @static |
| 53 |
* |
| 54 |
* @var Plugin |
| 55 |
*/ |
| 56 |
public static $instance = null; |
| 57 |
|
| 58 |
public function __construct() { |
| 59 |
// Initialize all of the plugin's functionality. |
| 60 |
add_action( 'init', [ $this, 'init' ], 0 ); |
| 61 |
// Load Plugin Textdomain |
| 62 |
add_action( 'plugins_loaded', [ $this, 'load_awb_textdomain' ] ); |
| 63 |
|
| 64 |
// Load Plugin Settings in the Admin Settings Page |
| 65 |
if ( is_admin() ) { |
| 66 |
$this->admin_actions(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Instance. |
| 72 |
* |
| 73 |
* Implements a Singleton pattern - Ensures only one instance of the plugin class is loaded or can be loaded. |
| 74 |
* |
| 75 |
* @since 2.0.0 |
| 76 |
* @static |
| 77 |
* |
| 78 |
* @return Plugin An instance of the class. |
| 79 |
*/ |
| 80 |
public static function instance() { |
| 81 |
if ( is_null( self::$instance ) ) { |
| 82 |
self::$instance = new self(); |
| 83 |
|
| 84 |
/** |
| 85 |
* Add Chat App loaded. |
| 86 |
* |
| 87 |
* Fires when Add Chat App Button was fully loaded and instantiated. |
| 88 |
* |
| 89 |
* @since 1.0.0 |
| 90 |
*/ |
| 91 |
do_action( 'add_chat_app_button_loaded' ); |
| 92 |
} |
| 93 |
|
| 94 |
return self::$instance; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Admin Actions |
| 99 |
* |
| 100 |
* Run actions for the Admin Dashboard. |
| 101 |
* |
| 102 |
* @since 2.0.0 |
| 103 |
*/ |
| 104 |
private function admin_actions() { |
| 105 |
require_once( __DIR__ . '/admin/settings.php' ); |
| 106 |
|
| 107 |
//Load admin notice dismissal management library |
| 108 |
require_once( plugin_dir_path( __FILE__ ) . '/vendors/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php' ); |
| 109 |
add_action( 'admin_init', array( 'PAnD', 'init' ) ); |
| 110 |
|
| 111 |
// Add a link to the plugin's Settings page from the Plugins Page |
| 112 |
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), [ $this, 'add_plugin_action_link' ], 10, 5 ); |
| 113 |
|
| 114 |
add_filter( 'plugin_action_links_add-whatsapp-button/add-whatsapp-button.php', [ $this, 'add_settings_link_to_plugins_page' ] ); |
| 115 |
|
| 116 |
new Admin_Settings(); |
| 117 |
} |
| 118 |
|
| 119 |
public function add_settings_link_to_plugins_page( $links ) { |
| 120 |
$url = esc_url( add_query_arg( |
| 121 |
'page', |
| 122 |
'awb-options', |
| 123 |
get_admin_url() . '/options-general.php' |
| 124 |
) ); |
| 125 |
|
| 126 |
$settings_link = "<a href='$url'>" . __( 'Settings' ) . '</a>'; |
| 127 |
|
| 128 |
array_push( |
| 129 |
$links, |
| 130 |
$settings_link |
| 131 |
); |
| 132 |
return $links; |
| 133 |
} |
| 134 |
|
| 135 |
// Load Plugin Textdomain |
| 136 |
public function load_awb_textdomain() { |
| 137 |
load_plugin_textdomain( 'add-whatsapp-button', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Add Plugin Action Link |
| 142 |
* |
| 143 |
* Add a link to the plugin settings page to the list of action links displayed for a specific plugin in the Plugins list table. |
| 144 |
* |
| 145 |
* @since 2.0.0 |
| 146 |
*/ |
| 147 |
public function add_plugin_action_link( $links ) { |
| 148 |
$settings = array( |
| 149 |
'settings' => '<a href="options-general.php?page=awb-options">' . esc_html__( 'Settings', 'add-whatsapp-button' ) . '</a>' |
| 150 |
); |
| 151 |
|
| 152 |
return array_merge( $settings, $links ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Init |
| 157 |
* |
| 158 |
* Initializes the plugin's functionality. |
| 159 |
* |
| 160 |
* @since 2.0.0 |
| 161 |
*/ |
| 162 |
public function init() { |
| 163 |
require_once( plugin_dir_path( __FILE__ ) . '/includes/scripts-manager.php' ); |
| 164 |
require_once( plugin_dir_path( __FILE__ ) . '/includes/styles-manager.php' ); |
| 165 |
|
| 166 |
$this->scripts_manager = new Scripts_Manager(); |
| 167 |
$this->styles_manager = new Styles_Manager(); |
| 168 |
|
| 169 |
if ( $this->get_plugin_options( 'enable' ) && ! is_admin() ) { |
| 170 |
// Print the WhatsApp button |
| 171 |
add_action('wp_footer', [ $this, 'print_button' ] ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
public function print_button() { |
| 176 |
$settings = $this->get_plugin_options(); |
| 177 |
|
| 178 |
$allowed_label_html = [ 'strong' => [], 'em' => [], 'br' => [] ]; |
| 179 |
$button_text = isset( $settings['button_text'] ) ? wp_kses( $settings['button_text'], $allowed_label_html ) : __( 'Message Us on WhatsApp', 'add-whatsapp-button' ); |
| 180 |
$displayNoneIfIcon = ( $settings['button_type'] == 'wab-icon-plain' ) ? 'awb-hide' : ''; |
| 181 |
$button_style = !empty( $settings['button_type'] ) ? $settings['button_type'] : 'wab-side-rectangle'; |
| 182 |
$close_button_icon = ''; |
| 183 |
|
| 184 |
if ( 'wab-bottom-rectangle' !== $settings['button_type'] ) { |
| 185 |
$button_location = isset( $settings['button_location'] ) ? 'wab-pull-'.$settings['button_location'] : 'wab-pull-left'; |
| 186 |
} |
| 187 |
|
| 188 |
if ( isset( $settings['hide_button'] ) && 'full' === $settings['hide_button'] ) { |
| 189 |
$close_button_icon = '<span class="wab-x">x</span>'; |
| 190 |
} |
| 191 |
else if ( isset( $settings['hide_button'] ) && 'hide' === $settings['hide_button'] ) { |
| 192 |
if ( 'right' === $settings['button_location'] && 'wab-bottom-rectangle' !== $settings['button_type'] ) { |
| 193 |
$close_button_icon = '<img class="wab-chevron wab-right" src="' . plugins_url( '/img/chevron-right.svg', __FILE__ ) . '" />'; |
| 194 |
} else if ( 'left' === $settings['button_location'] && 'wab-bottom-rectangle' !== $settings['button_type'] ) { |
| 195 |
$close_button_icon = '<img class="wab-chevron wab-left" src="' . plugins_url( '/img/chevron-left.svg', __FILE__ ) . '" />'; |
| 196 |
} else if ( 'wab-bottom-rectangle' === $settings['button_type'] ) { |
| 197 |
$close_button_icon = '<img class="wab-chevron wab-down" src="' . plugins_url( '/img/chevron-down.svg', __FILE__ ) . '" />'; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
ob_start(); |
| 202 |
?> |
| 203 |
|
| 204 |
<div id="wab_cont" class="wab-cont ui-draggable <?php echo $button_style; ?> <?php echo $button_location; ?>"> |
| 205 |
<?php |
| 206 |
$wa_href = 'https://wa.me/' . esc_attr( $settings['phone_number'] ) . ( ( ! empty( $settings['default_message'] ) && $settings['enable_message'] == '1' ) ? '/?text=' . rawurlencode( $settings['default_message'] ) : '' ); |
| 207 |
if ( 'wab-icon-plain' === $button_style && ! empty( $settings['icon_label_enable'] ) ) : |
| 208 |
?> |
| 209 |
<div class="wab-icon-label-wrapper"> |
| 210 |
<a id="whatsAppButton" href="<?php echo $wa_href; ?>" target="_blank"></a> |
| 211 |
<span class="wab-icon-label"><?php echo $button_text; ?></span> |
| 212 |
</div> |
| 213 |
<?php else : ?> |
| 214 |
<a id="whatsAppButton" href="<?php echo $wa_href; ?>" target="_blank"><span class="<?php echo $displayNoneIfIcon; ?>"><?php echo $button_text; ?></span></a> |
| 215 |
<?php endif; ?> |
| 216 |
<?php if ( isset( $settings['enable_dragging'] ) ) : ?> |
| 217 |
<div id="wab_drag"><img src="<?php echo plugins_url( '/img/drag-horizontal.svg', __FILE__ ) ?>"></div> |
| 218 |
<?php endif; ?> |
| 219 |
<?php if ( isset( $settings['enable_hide_button'] ) && isset( $settings['hide_button'] ) ) : ?> |
| 220 |
<div id="wab_close"><?php echo $close_button_icon; ?></div> |
| 221 |
<?php endif; ?> |
| 222 |
</div> |
| 223 |
|
| 224 |
<?php |
| 225 |
echo ob_get_clean(); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get Plugin Options |
| 230 |
* |
| 231 |
* Retrieves either a passed option, if it exists, or the entire options array for the plugin. |
| 232 |
* |
| 233 |
* @since 2.0.0 |
| 234 |
* |
| 235 |
* @param string $option_name |
| 236 |
*/ |
| 237 |
public function get_plugin_options( $option_name = null ) { |
| 238 |
// Cache the options for this page load. |
| 239 |
if ( ! $this->plugin_options ) { |
| 240 |
$this->plugin_options = get_option( 'awb_settings' ); |
| 241 |
} |
| 242 |
|
| 243 |
if ( ! empty( $option_name ) ) { |
| 244 |
// If an option name is passed, check if it exists. If it does, return it. Otherwise, return false. |
| 245 |
if ( isset( $this->plugin_options[ $option_name ] ) ) { |
| 246 |
return $this->plugin_options[ $option_name ]; |
| 247 |
} |
| 248 |
|
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
return $this->plugin_options; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
Plugin::instance(); |