| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
class WpSaioInit { |
| 4 |
|
| 5 |
private static $_instance = null; |
| 6 |
private $main_menu_slug = 'wp-support-all-in-one.php'; |
| 7 |
private $admin_page_hookfix = ''; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
add_filter( 'plugin_action_links_' . WP_SAIO_BASE_NAME, array( $this, 'settings_link' ) ); |
| 11 |
/* |
| 12 |
* Load Text Domain |
| 13 |
*/ |
| 14 |
add_action( 'init', array( $this, 'loadTextDomain' ) ); |
| 15 |
|
| 16 |
/* |
| 17 |
* Register Enqueue |
| 18 |
*/ |
| 19 |
add_action( 'admin_enqueue_scripts', array( $this, 'registerAdminEnqueue' ) ); |
| 20 |
add_action( 'wp_enqueue_scripts', array( $this, 'registerEnqueue' ) ); |
| 21 |
|
| 22 |
/* |
| 23 |
* Register Menu |
| 24 |
*/ |
| 25 |
add_action( 'admin_menu', array( $this, 'registerAdminMenu' ) ); |
| 26 |
|
| 27 |
/* |
| 28 |
* Admin head |
| 29 |
*/ |
| 30 |
add_action( 'admin_head', array( $this, 'removeAdminNotices' ), 999 ); |
| 31 |
|
| 32 |
/* |
| 33 |
* WP Footer |
| 34 |
*/ |
| 35 |
add_action( 'wp_footer', array( $this, 'wpFooter' ) ); |
| 36 |
|
| 37 |
/* |
| 38 |
* Register Shortcode |
| 39 |
*/ |
| 40 |
WpSaioShortcodes::instance(); |
| 41 |
|
| 42 |
/* |
| 43 |
* Register Ajax |
| 44 |
*/ |
| 45 |
WpSaioAjax::instance(); |
| 46 |
|
| 47 |
/* |
| 48 |
* Register Helper |
| 49 |
*/ |
| 50 |
WpSaioHelper::instance(); |
| 51 |
|
| 52 |
add_action( 'admin_init', array( $this, 'registerSettings' ) ); |
| 53 |
} |
| 54 |
public static function instance() { |
| 55 |
if ( is_null( self::$_instance ) ) { |
| 56 |
self::$_instance = new self(); |
| 57 |
} |
| 58 |
return self::$_instance; |
| 59 |
} |
| 60 |
public function registerAdminEnqueue( $hook_suffix ) { |
| 61 |
if ( $hook_suffix !== $this->admin_page_hookfix ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
wp_register_style( 'wp-saio', WP_SAIO_URL . '/assets/admin/css/wp-saio.css', array(), WP_SAIO_VERSION ); |
| 65 |
wp_register_style( 'wp-saio-preview', WP_SAIO_URL . '/assets/home/css/wp-saio.css', array(), WP_SAIO_VERSION ); |
| 66 |
wp_register_style( 'ui-range', WP_SAIO_URL . '/assets/admin/css/ui-range.css', array(), WP_SAIO_VERSION ); |
| 67 |
wp_enqueue_style( 'wp-saio' ); |
| 68 |
wp_enqueue_style( 'wp-saio-preview' ); |
| 69 |
wp_style_add_data( 'wp-saio', 'rtl', 'replace' ); |
| 70 |
wp_style_add_data( 'wp-saio-preview', 'rtl', 'replace' ); |
| 71 |
wp_enqueue_style( 'ui-range' ); |
| 72 |
|
| 73 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 74 |
wp_enqueue_style( 'wp-color-picker' ); |
| 75 |
wp_enqueue_script( 'wp-color-picker'); |
| 76 |
wp_enqueue_script( 'sortable', WP_SAIO_URL . '/assets/admin/js/Sortable.min.js', array(), '1.13.0', false ); |
| 77 |
|
| 78 |
// Load our React app |
| 79 |
$asset_file = WP_SAIO_DIR . '/app/build/index.asset.php'; |
| 80 |
|
| 81 |
if ( ! file_exists( $asset_file ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$asset = include $asset_file; |
| 86 |
wp_register_script( 'wp-saio', WP_SAIO_URL . '/app/build/index.js', $asset['dependencies'], $asset['version'], true ); |
| 87 |
wp_enqueue_script( 'wp-saio' ); |
| 88 |
|
| 89 |
// wp_register_script( 'wp-saio-preview', WP_SAIO_URL . '/assets/home/js/wp-saio.min.js', array(), WP_SAIO_VERSION, false ); |
| 90 |
// wp_enqueue_script( 'wp-saio-preview' ); |
| 91 |
wp_enqueue_media(); |
| 92 |
|
| 93 |
wp_localize_script( |
| 94 |
'wp-saio', |
| 95 |
'wp_saio_object', |
| 96 |
array( |
| 97 |
'are_you_sure' => esc_html__( 'Are you sure you want to remove this app. All data will be erase?', 'support-chat' ), |
| 98 |
'wp_saio_html_inputs' => json_encode( WpSaio::renderForm() ), |
| 99 |
'add_media_text_title' => esc_html__( 'Choose Image', 'support-chat' ), |
| 100 |
'add_media_text_button' => esc_html__( 'Choose Image', 'support-chat' ), |
| 101 |
'translate' => array( |
| 102 |
// Footer texts |
| 103 |
'footerText' => esc_html__( 'We need your support to keep updating and improving the plugin. Please,', 'support-chat' ), |
| 104 |
'reviewLink' => esc_html__( 'help us by leaving a good review', 'support-chat' ), |
| 105 |
'thanks' => esc_html__( 'Thanks!', 'support-chat' ), |
| 106 |
'thankYouText' => esc_html__( 'Thank you for using Support Chat from NinjaTeam', 'support-chat' ), |
| 107 |
|
| 108 |
// Choose Apps Tab |
| 109 |
'selectAppIcons' => esc_html__( 'Select app icons to add them to your list.', 'support-chat' ), |
| 110 |
'addNewApp' => esc_html__( 'Add New App', 'support-chat' ), |
| 111 |
'enterAppTitle' => esc_html__( 'Enter your app title', 'support-chat' ), |
| 112 |
'uploadIcon' => esc_html__( 'Upload Icon', 'support-chat' ), |
| 113 |
'yourAppIcon' => esc_html__( 'Your app icon/image', 'support-chat' ), |
| 114 |
'settingsSavedSuccess' => esc_html__( 'Settings saved successfully!', 'support-chat' ), |
| 115 |
'errorSavingSettings' => esc_html__( 'Error saving settings. Please try again.', 'support-chat' ), |
| 116 |
'saving' => esc_html__( 'Saving...', 'support-chat' ), |
| 117 |
'saveChanges' => esc_html__( 'Save Changes', 'support-chat' ), |
| 118 |
|
| 119 |
// Design Tab |
| 120 |
'settingStyleWidget' => esc_html__( 'Setting style for the floating widget.', 'support-chat' ), |
| 121 |
'enablePlugin' => esc_html__( 'Enable plugin', 'support-chat' ), |
| 122 |
'widgetPosition' => esc_html__( 'Widget position', 'support-chat' ), |
| 123 |
'left' => esc_html__( 'Left', 'support-chat' ), |
| 124 |
'right' => esc_html__( 'Right', 'support-chat' ), |
| 125 |
'style' => esc_html__( 'Style', 'support-chat' ), |
| 126 |
'redirect' => esc_html__( 'Redirect', 'support-chat' ), |
| 127 |
'popup' => esc_html__( 'Popup', 'support-chat' ), |
| 128 |
'tooltip' => esc_html__( 'Tooltip', 'support-chat' ), |
| 129 |
'appName' => esc_html__( 'App Name', 'support-chat' ), |
| 130 |
'appContent' => esc_html__( 'App Content', 'support-chat' ), |
| 131 |
'paddingFromBottom' => esc_html__( 'Padding from bottom', 'support-chat' ), |
| 132 |
'customIconAvatar' => esc_html__( 'Custom icon/avatar', 'support-chat' ), |
| 133 |
'chooseImage' => esc_html__( 'Choose Image', 'support-chat' ), |
| 134 |
'buttonStyle' => esc_html__( 'Button style', 'support-chat' ), |
| 135 |
'contain' => esc_html__( 'Contain', 'support-chat' ), |
| 136 |
'cover' => esc_html__( 'Cover', 'support-chat' ), |
| 137 |
'buttonColor' => esc_html__( 'Button color', 'support-chat' ), |
| 138 |
'designSettingsSavedSuccess' => esc_html__( 'Design settings saved successfully!', 'support-chat' ), |
| 139 |
'errorSavingDesignSettings' => esc_html__( 'Error saving settings. Please try again.', 'support-chat' ), |
| 140 |
|
| 141 |
// Display Tab |
| 142 |
'settingTextStyleWidget' => esc_html__( 'Setting text and style for the floating widget.', 'support-chat' ), |
| 143 |
'showOnDesktop' => esc_html__( 'Show on desktop', 'support-chat' ), |
| 144 |
'showOnMobile' => esc_html__( 'Show on mobile', 'support-chat' ), |
| 145 |
'display' => esc_html__( 'Display', 'support-chat' ), |
| 146 |
'showOnAllPages' => esc_html__( 'Show on all pages', 'support-chat' ), |
| 147 |
'showOnThesePages' => esc_html__( 'Show on these pages...', 'support-chat' ), |
| 148 |
'hideOnThesePages' => esc_html__( 'Hide on these pages...', 'support-chat' ), |
| 149 |
'all' => esc_html__( 'All', 'support-chat' ), |
| 150 |
'displaySettingsSavedSuccess' => esc_html__( 'Display settings saved successfully!', 'support-chat' ), |
| 151 |
'errorSavingDisplaySettings' => esc_html__( 'Error saving settings. Please try again.', 'support-chat' ), |
| 152 |
|
| 153 |
// Tabs |
| 154 |
'chooseApps' => esc_html__( 'Choose Apps', 'support-chat' ), |
| 155 |
'design' => esc_html__( 'Design', 'support-chat' ), |
| 156 |
'display' => esc_html__( 'Display', 'support-chat' ), |
| 157 |
|
| 158 |
// App.jsx |
| 159 |
'doYouNeedHelp' => esc_html__( 'Do you need help?', 'support-chat' ), |
| 160 |
'thanksUsingNinjaTeam' => __( 'Thanks for using NinjaTeam\'s Products!', 'support-chat' ), |
| 161 |
'contactSupport' => esc_html__( 'contact support', 'support-chat' ), |
| 162 |
'rateUs' => esc_html__( 'rate us', 'support-chat' ), |
| 163 |
'bestWishes' => esc_html__( 'Best wishes,', 'support-chat' ), |
| 164 |
'kellyFromNinjaTeam' => esc_html__( 'Kelly from NinjaTeam', 'support-chat' ), |
| 165 |
|
| 166 |
// Header |
| 167 |
'clickToChat' => esc_html__( 'Click to Chat', 'support-chat' ), |
| 168 |
'byNinjaTeam' => esc_html__( 'by NinjaTeam', 'support-chat' ), |
| 169 |
|
| 170 |
// Toast messages |
| 171 |
'close' => esc_html__( 'Close', 'support-chat' ), |
| 172 |
), |
| 173 |
'pages' => get_pages(), |
| 174 |
'style' => get_option( 'wpsaio_style' ), |
| 175 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 176 |
'nonce' => wp_create_nonce( 'wpsaio_nonce' ), |
| 177 |
'add_icon_text_title' => esc_html__( 'Choose Icon', 'support-chat' ), |
| 178 |
'add_icon_text_button' => esc_html__( 'Choose Icon', 'support-chat' ), |
| 179 |
'is_reviewed' => get_option( 'wpsaio_review_tracked', '0' ), |
| 180 |
'plugin_url' => WP_SAIO_URL, |
| 181 |
'enablePlugin' => get_option( 'wpsaio_enable_plugin', 1 ), |
| 182 |
'widgetPosition' => get_option( 'wpsaio_widget_position', 'right' ), |
| 183 |
'tooltip' => get_option( 'wpsaio_tooltip', 'appname' ), |
| 184 |
'bottomDistance' => get_option( 'wpsaio_bottom_distance', 30 ), |
| 185 |
'buttonIcon' => get_option( 'wpsaio_button_icon', '' ), |
| 186 |
'buttonImage' => get_option( 'wpsaio_button_image', 'contain' ), |
| 187 |
'buttonColor' => get_option( 'wpsaio_button_color', '' ), |
| 188 |
'showOnDesktop' => get_option( 'wpsaio_show_on_desktop', 1 ), |
| 189 |
'showOnMobile' => get_option( 'wpsaio_show_on_mobile', 1 ), |
| 190 |
'displayCondition' => get_option( 'wpsaio_display_condition', 'allPages' ), |
| 191 |
'includePages' => get_option( 'wpsaio_includes_pages', array() ), |
| 192 |
'excludePages' => get_option( 'wpsaio_excludes_pages', array() ), |
| 193 |
//choose apps page |
| 194 |
'page_choose_apps' => [ |
| 195 |
'apps' => WpSaio::defaultAppsWithCustomApps(), |
| 196 |
'app_order' => WpSaio::addedAppsOrder(), |
| 197 |
], |
| 198 |
'plugin_url' => WP_SAIO_URL, |
| 199 |
) |
| 200 |
); |
| 201 |
} |
| 202 |
public function registerEnqueue() { |
| 203 |
if ( ! $this->isActivePlugin() ) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
wp_register_style( 'wp-saio', WP_SAIO_URL . '/assets/home/css/wp-saio.css', [], WP_SAIO_VERSION ); |
| 207 |
wp_enqueue_style( 'wp-saio' ); |
| 208 |
wp_style_add_data( 'wp-saio', 'rtl', 'replace' ); |
| 209 |
|
| 210 |
wp_register_script( 'wp-saio', WP_SAIO_URL . '/assets/home/js/wp-saio.min.js', array( 'jquery' ), WP_SAIO_VERSION, false ); |
| 211 |
wp_enqueue_script( 'wp-saio' ); |
| 212 |
|
| 213 |
wp_localize_script( |
| 214 |
'wp-saio', |
| 215 |
'wp_saio_object', |
| 216 |
array( |
| 217 |
'style' => get_option( 'wpsaio_style' ), |
| 218 |
) |
| 219 |
); |
| 220 |
} |
| 221 |
public function loadTextDomain() { |
| 222 |
if ( function_exists( 'determine_locale' ) ) { |
| 223 |
$locale = determine_locale(); |
| 224 |
} else { |
| 225 |
$locale = is_admin() ? get_user_locale() : get_locale(); |
| 226 |
} |
| 227 |
unload_textdomain( 'support-chat' ); |
| 228 |
load_textdomain( 'support-chat', WP_SAIO_DIR . '/languages/' . $locale . '.mo' ); |
| 229 |
// load_plugin_textdomain( 'support-chat', false, WP_SAIO_DIR . '/languages' ); |
| 230 |
} |
| 231 |
public function registerAdminMenu() { |
| 232 |
$page_title = esc_html__( 'Support Chat All In One', 'support-chat' ); |
| 233 |
$menu_title = esc_html__( 'Click to Chat', 'support-chat' ); |
| 234 |
|
| 235 |
$this->admin_page_hookfix = add_menu_page( $page_title, $menu_title, 'manage_options', $this->main_menu_slug, array( $this, 'wpSaioMenuCallBack' ), WP_SAIO_URL . '/assets/admin/img/support-icon.svg' ); |
| 236 |
} |
| 237 |
// public function wpsaioLoadMainMenu() { |
| 238 |
// global $plugin_page; |
| 239 |
// $data = array(); |
| 240 |
// if ( isset( $_POST['save-wp-saio'] ) && isset( $_POST['data'] ) ) { |
| 241 |
// $_data = WpSaioHelper::sanitize_array( $_POST['data'] ); |
| 242 |
// foreach ( $_data as $k => $v ) { |
| 243 |
// $data[ $k ]['params'] = array(); |
| 244 |
// foreach ( $v as $k2 => $v2 ) { |
| 245 |
// $data[ $k ]['params'][ $k2 ] = wp_unslash( trim( $v2 ) ); |
| 246 |
// } |
| 247 |
// } |
| 248 |
// update_option( 'njt_wp_saio', $data ); |
| 249 |
|
| 250 |
// wp_safe_redirect( |
| 251 |
// esc_url( |
| 252 |
// add_query_arg( array( 'page' => $this->main_menu_slug ), admin_url( 'admin.php' ) ) |
| 253 |
// ) |
| 254 |
// ); |
| 255 |
// } |
| 256 |
// } |
| 257 |
public function removeAdminNotices() { |
| 258 |
$current_screen = get_current_screen(); |
| 259 |
if( $current_screen->id !== 'toplevel_page_wp-support-all-in-one' ) { |
| 260 |
return; |
| 261 |
} |
| 262 |
remove_all_actions( 'admin_notices' ); |
| 263 |
remove_all_actions( 'all_admin_notices' ); |
| 264 |
remove_all_actions( 'user_admin_notices' ); |
| 265 |
remove_all_actions( 'network_admin_notices' ); |
| 266 |
} |
| 267 |
public function wpFooter() { |
| 268 |
if ( ! $this->isActivePlugin() ) { |
| 269 |
return; |
| 270 |
} |
| 271 |
$icon_bg_color = get_option( 'wpsaio_button_color', '' ); |
| 272 |
$btn_icon = get_option( 'wpsaio_button_icon', '' ); |
| 273 |
$btn_image = get_option( 'wpsaio_button_image', 'contain' ); |
| 274 |
$data = array( |
| 275 |
'buttons' => WpSaio::generateFrontendButtons(), |
| 276 |
'contents' => do_shortcode( implode( '', WpSaio::renderShortcodes() ) ), |
| 277 |
'icon_bg_color' => $icon_bg_color, |
| 278 |
'btn_icon' => $btn_icon, |
| 279 |
'btn_image' => $btn_image, |
| 280 |
); |
| 281 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 282 |
echo WpSaioView::load( 'home.main', $data ); |
| 283 |
} |
| 284 |
private function isActivePlugin() { |
| 285 |
return ( get_option( 'wpsaio_enable_plugin' ) == 1 ); |
| 286 |
} |
| 287 |
public function wpSaioMenuCallBack() { |
| 288 |
?> |
| 289 |
<div id="wpsaio-react-root"></div> |
| 290 |
<?php |
| 291 |
} |
| 292 |
|
| 293 |
public function wpSaioMenuSettingsCallBack() { |
| 294 |
wp_enqueue_media(); |
| 295 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 296 |
echo WpSaioView::load( 'admin.settings' ); |
| 297 |
} |
| 298 |
public function registerSettings() { |
| 299 |
register_setting( 'wpsaio', 'wpsaio_enable_plugin', array( 'type' => 'boolean', 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 300 |
register_setting( 'wpsaio', 'wpsaio_style', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 301 |
register_setting( 'wpsaio', 'wpsaio_tooltip', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 302 |
register_setting( 'wpsaio', 'wpsaio_widget_position', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 303 |
register_setting( 'wpsaio', 'wpsaio_bottom_distance', array( 'type' => 'integer', 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 304 |
register_setting( 'wpsaio', 'wpsaio_button_icon', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 305 |
register_setting( 'wpsaio', 'wpsaio_button_color', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 306 |
} |
| 307 |
public static function activate() { |
| 308 |
$installed = get_option( 'wpsaio_enable_plugin' ); |
| 309 |
|
| 310 |
if ( ! $installed ) { |
| 311 |
update_option( 'wpsaio_enable_plugin', 1 ); |
| 312 |
update_option( 'wpsaio_style', 'redirect' ); |
| 313 |
update_option( 'wpsaio_tooltip', 'appname' ); |
| 314 |
update_option( 'wpsaio_widget_position', 'right' ); |
| 315 |
update_option( 'wpsaio_button_image', 'contain' ); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
public static function deactivate() { |
| 320 |
} |
| 321 |
|
| 322 |
public function settings_link( $link ) { |
| 323 |
// add custom link |
| 324 |
$setting_link = '<a href="admin.php?page=wp-support-all-in-one.php">Settings</a>'; |
| 325 |
array_unshift( $link, $setting_link ); |
| 326 |
return $link; |
| 327 |
} |
| 328 |
} |
| 329 |
|