| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: raychat |
| 4 |
* Author: raychat |
| 5 |
* Author URI: https://www.raychat.io |
| 6 |
* Plugin URI: https://www.raychat.io |
| 7 |
* Description: افزونه وردپرس گفتگوی آنلاین رایچت | با � |
| 8 |
شتریانتون صحبت کنید ، پشتیبانی کنید و از فروش خود لذت ببرید :) |
| 9 |
* Version: 2.2.0 |
| 10 |
* Text Domain: raychat |
| 11 |
* Domain Path: /languages |
| 12 |
*/ |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; // Exit if accessed directly |
| 16 |
} |
| 17 |
|
| 18 |
// � |
| 19 |
CHANGED: Fixed path for language loading |
| 20 |
load_plugin_textdomain('raychat', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 21 |
|
| 22 |
$lang = get_bloginfo("language"); |
| 23 |
$raychat_addr = 'https://www.raychat.io'; |
| 24 |
|
| 25 |
define("RAYCHAT_LANG", substr($lang, 0, 2)); |
| 26 |
define("RAYCHAT_WIDGET_URL", "raychat.io"); |
| 27 |
define("RAYCHAT_URL", $raychat_addr); |
| 28 |
define("RAYCHAT_INTEGRATION_URL", RAYCHAT_URL . "/integration"); |
| 29 |
define("RAYCHAT_PLUGIN_URL", plugin_dir_url(__FILE__)); |
| 30 |
define("RAYCHAT_IMG_URL", plugin_dir_url(__FILE__) . "img/"); |
| 31 |
|
| 32 |
register_activation_hook(__FILE__, 'raychatInstall'); |
| 33 |
register_deactivation_hook(__FILE__, 'raychatDelete'); |
| 34 |
|
| 35 |
function catalog_admin_menu_raychat() { |
| 36 |
// � |
| 37 |
CHANGED: Ensure i18n loads in menu callback |
| 38 |
load_plugin_textdomain('raychat', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 39 |
|
| 40 |
// � |
| 41 |
CHANGED: Use capability instead of deprecated role |
| 42 |
add_menu_page(__('رایچت', 'raychat'), __('رایچت', 'raychat'), 'edit_pages', basename(__FILE__), 'raychatPreferences', RAYCHAT_IMG_URL . "raychat.svg"); |
| 43 |
} |
| 44 |
add_action('admin_menu', 'catalog_admin_menu_raychat'); |
| 45 |
|
| 46 |
function raychat_options_validate($args) { |
| 47 |
return $args; |
| 48 |
} |
| 49 |
|
| 50 |
add_action('admin_init', 'raychat_register_settings'); |
| 51 |
function raychat_register_settings() { |
| 52 |
register_setting('raychat_token', 'raychat_token', 'raychat_options_validate'); |
| 53 |
register_setting('raychat_widget_id', 'raychat_widget_id', 'raychat_options_validate'); |
| 54 |
} |
| 55 |
|
| 56 |
add_action('admin_post_wp_save_token', 'wp_save_token_function_raychat'); |
| 57 |
add_action('admin_post_nopriv_wp_save_token', 'wp_save_token_function_raychat'); |
| 58 |
|
| 59 |
add_action('wp_footer', 'raychatAppend', 100000); |
| 60 |
|
| 61 |
function raychatInstall() { |
| 62 |
return raychat::getInstance()->install(); |
| 63 |
} |
| 64 |
|
| 65 |
function raychatDelete() { |
| 66 |
return raychat::getInstance()->delete(); |
| 67 |
} |
| 68 |
|
| 69 |
function raychatAppend() { |
| 70 |
echo raychat::getInstance()->append(raychat::getInstance()->getId()); |
| 71 |
} |
| 72 |
|
| 73 |
function raychatPreferences() { |
| 74 |
if (isset($_POST["widget_id"])) { |
| 75 |
raychat::getInstance()->save(); |
| 76 |
} |
| 77 |
|
| 78 |
// � |
| 79 |
CHANGED: Added i18n reload here as well |
| 80 |
load_plugin_textdomain('raychat', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 81 |
|
| 82 |
wp_register_style('raychat_style', plugins_url('raychat.css', __FILE__)); |
| 83 |
wp_enqueue_style('raychat_style'); |
| 84 |
echo raychat::getInstance()->render(); |
| 85 |
} |
| 86 |
|
| 87 |
function wp_save_token_function_raychat() { |
| 88 |
$tokenError = null; |
| 89 |
if (!empty($_POST['submit'])) { |
| 90 |
$token = esc_html($_POST['token-id']); |
| 91 |
$version_id = 'version_2'; |
| 92 |
|
| 93 |
// � |
| 94 |
CHANGED: Added more validation error checks and i18n-ready errors |
| 95 |
if (!empty($token) && !empty($version_id)) { |
| 96 |
if (preg_match("/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/", $token)) { |
| 97 |
update_option('raychat_widget_id', $token); |
| 98 |
update_option("version_id", $version_id); |
| 99 |
raychat::getInstance()->install(); |
| 100 |
} else { |
| 101 |
$tokenError = "توکن نا� |
| 102 |
عتبر است."; |
| 103 |
} |
| 104 |
} else { |
| 105 |
$tokenError = "توکن یا ورژن ن� |
| 106 |
ی توانند خالی باشند."; |
| 107 |
} |
| 108 |
set_transient('error_token_uuid', $tokenError); |
| 109 |
} |
| 110 |
wp_redirect($_SERVER['HTTP_REFERER']); |
| 111 |
exit(); |
| 112 |
} |
| 113 |
|
| 114 |
class raychat { |
| 115 |
protected static $instance, $lang; |
| 116 |
private $widget_id = '', $token = '', $version = ''; |
| 117 |
|
| 118 |
private function __construct() { |
| 119 |
$this->token = get_option('raychat_token'); |
| 120 |
$this->widget_id = get_option('raychat_widget_id'); |
| 121 |
$this->version = get_option("version_id"); |
| 122 |
} |
| 123 |
|
| 124 |
private function __clone() {} |
| 125 |
public function __wakeup() {} |
| 126 |
|
| 127 |
public static function getInstance() { |
| 128 |
if (is_null(self::$instance)) { |
| 129 |
self::$instance = new raychat(); |
| 130 |
} |
| 131 |
self::$lang = isset($_GET["lang"]) && $_GET["lang"] === 'ru' ? "ru" : "en"; |
| 132 |
return self::$instance; |
| 133 |
} |
| 134 |
|
| 135 |
public function setID($id) { $this->widget_id = $id; } |
| 136 |
public function setToken($token) { $this->token = $token; } |
| 137 |
public function setVersion($version) { $this->version = $version; } |
| 138 |
|
| 139 |
public function install() { |
| 140 |
$file = dirname(__FILE__) . '/id'; |
| 141 |
if (file_exists($file)) { |
| 142 |
$uuid = file_get_contents($file); |
| 143 |
update_option('raychat_widget_id', $uuid); |
| 144 |
unlink($file); |
| 145 |
$this->widget_id = $uuid; |
| 146 |
$this->save(); |
| 147 |
} else { |
| 148 |
if (!$this->widget_id && ($out = get_option('raychat_widget_id'))) { |
| 149 |
$this->widget_id = $out; |
| 150 |
} |
| 151 |
$this->save(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public function delete() { |
| 156 |
delete_option('raychat_widget_id'); |
| 157 |
} |
| 158 |
|
| 159 |
public function getId() { |
| 160 |
return $this->widget_id; |
| 161 |
} |
| 162 |
|
| 163 |
public function render() { |
| 164 |
$result = $this->catchPost(); |
| 165 |
$error = is_array( $result ) && isset( $result['error'] ) ? $result['error'] : ''; |
| 166 |
$widget_id = $this->widget_id; // � |
| 167 |
CHANGED: define it for the template |
| 168 |
$requirementsOk = ini_get('allow_url_fopen') || extension_loaded('curl'); |
| 169 |
|
| 170 |
if ( $requirementsOk ) { |
| 171 |
require_once "templates/page.php"; |
| 172 |
} else { |
| 173 |
require_once "templates/error.php"; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
public function append($widget_id = false) { |
| 179 |
if ($widget_id) { |
| 180 |
require_once "templates/script.php"; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
public function save() { |
| 185 |
update_option('raychat_widget_id', $this->widget_id); |
| 186 |
update_option('raychat_token', $this->token); |
| 187 |
} |
| 188 |
|
| 189 |
public function catchPost() { |
| 190 |
if (isset($_GET['mode']) && $_GET['mode'] == 'reset') { |
| 191 |
$this->widget_id = ''; |
| 192 |
$this->token = ''; |
| 193 |
$this->save(); |
| 194 |
} |
| 195 |
|
| 196 |
if (isset($_POST['widget_id'])) { |
| 197 |
$this->widget_id = sanitize_text_field($_POST['widget_id']); |
| 198 |
$this->save(); |
| 199 |
} elseif (isset($_POST['email'], $_POST['userPassword'])) { |
| 200 |
$query = $_POST; |
| 201 |
$query['siteUrl'] = get_site_url(); |
| 202 |
$query['partnerId'] = "wordpress"; |
| 203 |
$query['authToken'] = md5(time() . get_site_url()); |
| 204 |
$query['agent_id'] = $query['agent_id'] ?? 0; |
| 205 |
$query['lang'] = RAYCHAT_LANG; |
| 206 |
|
| 207 |
$path = RAYCHAT_INTEGRATION_URL . "/install"; |
| 208 |
// � |
| 209 |
CHANGED: Ensure HTTP fallback without `openssl` |
| 210 |
if (!extension_loaded('openssl')) { |
| 211 |
$path = str_replace('https:', 'http:', $path); |
| 212 |
} |
| 213 |
|
| 214 |
try { |
| 215 |
$response = wp_remote_post($path, [ |
| 216 |
'body' => $query, |
| 217 |
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'] |
| 218 |
]); |
| 219 |
|
| 220 |
$body = wp_remote_retrieve_body($response); |
| 221 |
if (strstr($body, 'Error')) { |
| 222 |
return ['error' => $body]; |
| 223 |
} else { |
| 224 |
$this->widget_id = $body; |
| 225 |
$this->token = $query['authToken']; |
| 226 |
$this->save(); |
| 227 |
return true; |
| 228 |
} |
| 229 |
} catch (Exception $e) { |
| 230 |
_e("Connection error", 'raychat'); // � |
| 231 |
CHANGED: Wrap with translation |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|