PluginProbe
Raychat / trunk
Raychat vtrunk
trunk 2.2.0 2.2.1
raychat / raychat.php

raychat.php in Raychat trunk, at raychat.php

264 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Raychat
4 * Plugin URI: https://www.raychat.io
5 * Description: افزونه وردپرس گفتگوی آنلاین رایچت | با �
6 شتریانتون صحبت کنید ، پشتیبانی کنید و از فروش خود لذت ببرید :)
7 * Version: 2.2.1
8 * Author: Raychat
9 * Author URI: https://www.raychat.io
10 * Text Domain: raychat
11 * Domain Path: /languages/
12 * License: GPLv2 or later
13 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
14 */
15
16 if (!defined('ABSPATH')) {
17 exit;
18 }
19
20 // Load translation files
21 load_plugin_textdomain('raychat', false, dirname(plugin_basename(__FILE__)) . '/languages');
22
23 $lang = get_bloginfo('language');
24 $raychat_addr = 'https://www.raychat.io';
25
26 define('RAYCHAT_LANG', substr($lang, 0, 2));
27 define('RAYCHAT_WIDGET_URL', 'raychat.io');
28 define('RAYCHAT_URL', $raychat_addr);
29 define('RAYCHAT_INTEGRATION_URL', RAYCHAT_URL . '/integration');
30 define('RAYCHAT_PLUGIN_URL', plugin_dir_url(__FILE__));
31 define('RAYCHAT_IMG_URL', plugin_dir_url(__FILE__) . 'img/');
32
33 register_activation_hook(__FILE__, 'raychatInstall');
34 register_deactivation_hook(__FILE__, 'raychatDelete');
35
36 function catalog_admin_menu_raychat()
37 {
38 // Reload translations in admin
39 load_plugin_textdomain('raychat', false, dirname(plugin_basename(__FILE__)) . '/languages');
40
41 add_menu_page(
42 esc_html__('رایچت', 'raychat'),
43 esc_html__('رایچت', 'raychat'),
44 'edit_pages',
45 'raychat',
46 'raychatPreferences',
47 esc_url(RAYCHAT_IMG_URL . 'raychat.svg')
48 );
49 }
50 add_action('admin_menu', 'catalog_admin_menu_raychat');
51
52 function raychat_options_validate($args)
53 {
54 return $args;
55 }
56
57 add_action('admin_init', 'raychat_register_settings');
58 function raychat_register_settings()
59 {
60 register_setting('raychat_token', 'raychat_token', 'raychat_options_validate');
61 register_setting('raychat_widget_id', 'raychat_widget_id', 'raychat_options_validate');
62 }
63
64 add_action('admin_post_wp_save_token', 'wp_save_token_function_raychat');
65 add_action('admin_post_nopriv_wp_save_token', 'wp_save_token_function_raychat');
66
67 add_action('wp_footer', 'raychatAppend', 100000);
68
69 function raychatInstall()
70 {
71 return raychat::getInstance()->install();
72 }
73
74 function raychatDelete()
75 {
76 return raychat::getInstance()->delete();
77 }
78
79 function raychatAppend()
80 {
81 raychat::getInstance()->append(raychat::getInstance()->getId());
82 }
83
84 function raychatPreferences()
85 {
86 // Verify capability
87 if (!current_user_can('edit_pages')) {
88 wp_die(esc_html__('Unauthorized', 'raychat'));
89 }
90
91 if (isset($_POST['raychat_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['raychat_nonce'])), 'save_raychat_token')) {
92 raychat::getInstance()->save();
93 }
94
95 // Reload translations
96 load_plugin_textdomain('raychat', false, dirname(plugin_basename(__FILE__)) . '/languages');
97
98 // Enqueue style
99 $style_path = plugin_dir_path(__FILE__) . 'raychat.css';
100 wp_register_style(
101 'raychat_style',
102 plugins_url('raychat.css', __FILE__),
103 array(),
104 file_exists($style_path) ? filemtime($style_path) : false
105 );
106 wp_enqueue_style('raychat_style');
107
108 raychat::getInstance()->render();
109 }
110
111 function wp_save_token_function_raychat()
112 {
113 // Verify nonce and referer
114 if (empty($_POST['raychat_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['raychat_nonce'])), 'save_raychat_token')) {
115 wp_die(esc_html__('Security check failed', 'raychat'));
116 }
117
118 $tokenError = null;
119 $token = isset($_POST['token-id']) ? sanitize_text_field(wp_unslash($_POST['token-id'])) : '';
120 $version_id = 'version_2';
121
122 if ($token) {
123 if (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $token)) {
124 update_option('raychat_widget_id', $token);
125 update_option('version_id', $version_id);
126 raychat::getInstance()->install();
127 } else {
128 $tokenError = esc_html__('توکن نا�
129 عتبر است.', 'raychat');
130 }
131 } else {
132 $tokenError = esc_html__('توکن ن�
133 ی‌تواند خالی باشد.', 'raychat');
134 }
135
136 set_transient('error_token_uuid', $tokenError);
137
138 $redirect = wp_get_referer() ? wp_get_referer() : admin_url();
139 wp_safe_redirect(esc_url_raw($redirect));
140 exit();
141 }
142
143 class raychat
144 {
145 protected static $instance, $lang;
146 private $widget_id = '', $token = '', $version = '';
147
148 private function __construct()
149 {
150 $this->token = get_option('raychat_token');
151 $this->widget_id = get_option('raychat_widget_id');
152 $this->version = get_option('version_id');
153 }
154
155 private function __clone()
156 {
157 }
158 public function __wakeup()
159 {
160 }
161
162 public static function getInstance()
163 {
164 if (is_null(self::$instance)) {
165 self::$instance = new self();
166 }
167 self::$lang = (isset($_GET['lang']) && 'ru' === $_GET['lang']) ? 'ru' : 'en';
168 return self::$instance;
169 }
170
171 public function save()
172 {
173 if (isset($_POST['token-id'])) {
174 $this->widget_id = sanitize_text_field(wp_unslash($_POST['token-id']));
175 update_option('raychat_widget_id', $this->widget_id);
176 }
177 }
178
179 public function install()
180 {
181 $file = plugin_dir_path(__FILE__) . 'id';
182 if (file_exists($file)) {
183 $uuid = file_get_contents($file);
184 update_option('raychat_widget_id', $uuid);
185 wp_delete_file($file);
186 $this->widget_id = $uuid;
187 }
188 }
189
190 public function delete()
191 {
192 delete_option('raychat_widget_id');
193 }
194
195 public function getId()
196 {
197 return $this->widget_id;
198 }
199
200 public function render()
201 {
202 $result = $this->catchPost();
203 $error = is_array($result) && isset($result['error']) ? $result['error'] : '';
204 $widget_id = $this->widget_id;
205 $requirementsOk = ini_get('allow_url_fopen') || extension_loaded('curl');
206
207 if ($requirementsOk) {
208 include plugin_dir_path(__FILE__) . 'templates/page.php';
209 } else {
210 include plugin_dir_path(__FILE__) . 'templates/error.php';
211 }
212 }
213
214 public function append($widget_id = false)
215 {
216 if ($widget_id) {
217 include plugin_dir_path(__FILE__) . 'templates/script.php';
218 }
219 }
220
221 public function catchPost()
222 {
223 if (isset($_POST['raychat_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['raychat_nonce'])), 'save_raychat_token')) {
224 $this->save();
225 return true;
226 }
227
228 if (isset($_POST['email'], $_POST['userPassword'])) {
229 $query = array_map('sanitize_text_field', wp_unslash($_POST));
230 $query['siteUrl'] = esc_url_raw(get_site_url());
231 $query['partnerId'] = 'wordpress';
232 $query['authToken'] = md5(time() . get_site_url());
233 $query['agent_id'] = isset($query['agent_id']) ? absint($query['agent_id']) : 0;
234 $query['lang'] = RAYCHAT_LANG;
235
236 $path = RAYCHAT_INTEGRATION_URL . '/install';
237 if (!extension_loaded('openssl')) {
238 $path = str_replace('https:', 'http:', $path);
239 }
240
241 try {
242 $response = wp_remote_post(esc_url_raw($path), [
243 'body' => $query,
244 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
245 ]);
246
247 if (is_wp_error($response)) {
248 return ['error' => $response->get_error_message()];
249 }
250
251 $body = wp_remote_retrieve_body($response);
252 if (false !== strpos($body, 'Error')) {
253 return ['error' => sanitize_text_field($body)];
254 }
255
256 $this->widget_id = sanitize_text_field($body);
257 update_option('raychat_widget_id', $this->widget_id);
258 return true;
259 } catch (Exception $e) {
260 return ['error' => esc_html__('Connection error', 'raychat')];
261 }
262 }
263 }
264 }