Changeset 3102025
- Timestamp:
- 06/12/2024 09:22:20 PM (22 months ago)
- Location:
- udimi-optin
- Files:
-
- 2 edited
-
tags/2.2/class.php (modified) (2 diffs)
-
trunk/class.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
udimi-optin/tags/2.2/class.php
r3101946 r3102025 3 3 class UdimiOptin 4 4 { 5 const IS_LOCAL = false;6 const API_HOST_PROD = 'https://api.udimi.com';7 const API_HOST_LOCAL = 'https://api.udimi.me';8 const HOST_PROD = 'https://udimi.com';9 const HOST_LOCAL = 'https://udimi.me';10 const PLUGIN_NAME = 'udimioptin';11 const PLUGIN_FOLDER = 'udimi-optin';12 const OPTIN_CODE_TTL_HOURS = 24;5 const IS_LOCAL = false; 6 const API_HOST_PROD = 'https://api.udimi.com'; 7 const API_HOST_LOCAL = 'https://api.udimi.me'; 8 const HOST_PROD = 'https://udimi.com'; 9 const HOST_LOCAL = 'https://udimi.me'; 10 const PLUGIN_NAME = 'udimioptin'; 11 const PLUGIN_FOLDER = 'udimi-optin'; 12 const OPTIN_CODE_TTL_HOURS = 24; 13 13 const UA = 'UdimiOptinWordpress-12062024'; 14 14 15 private $apiError = ''; 16 17 /** 18 * Вызывается при каждом F5. Обновить сохраненный в плагине трекинг код с удалённого API 19 * @return void 20 */ 21 public function updateScript() 22 { 23 if ($key = get_option('udimi_optin_key')) { 24 $date = get_option('udimi_optin_date'); 25 if (!$date || !strtotime($date) || time() - strtotime($date) > self::OPTIN_CODE_TTL_HOURS * 3600) { 26 if ($trackingCode = $this->getTrackingCode($key)) { 27 update_option('udimi_optin_script', $trackingCode); 28 update_option('udimi_optin_date', date('Y-m-d H:i:s')); 29 } 30 } 31 } 32 } 33 34 public function ajax_connect() 35 { 36 $key = isset($_POST['key']) && !is_array($_POST['key']) ? trim($_POST['key']) : ''; 37 38 $error = ''; 39 if (empty($key)) { 40 $error = '"Client Key" is empty'; 41 } else { 42 $trackingCode = $this->getTrackingCode($key); 43 if (!$trackingCode) { 44 $error = $this->getApiError(); 45 } 46 } 47 48 if ($error) { 49 wp_send_json_error([ 50 'error' => $error, 51 ]); 52 } else { 53 update_option('udimi_optin_key', $key); 54 update_option('udimi_optin_script', stripslashes($this->getTrackingCode($key))); 55 update_option('udimi_optin_date', date('Y-m-d H:i:s')); 56 wp_send_json_success(); 57 } 58 } 59 60 public function ajax_disconnect() 61 { 62 delete_option('udimi_optin_key'); 63 delete_option('udimi_optin_script'); 64 delete_option('udimi_optin_date'); 65 wp_send_json_success(); 66 } 67 68 /** 69 * @return string 70 */ 71 public function getApiError() 72 { 73 return $this->apiError; 74 } 75 76 public function addAdminScript() 77 { 78 wp_enqueue_script('jquery'); 79 wp_register_script('udimi_optin_admin_js', plugins_url(self::PLUGIN_FOLDER . '/js/adminpanel.js'), ['jquery']); 80 wp_localize_script( 81 'udimi_optin_admin_js', 82 'config', 83 [ 84 'saveUrl' => admin_url('admin-ajax.php'), 85 ] 86 ); 87 wp_enqueue_script('udimi_optin_admin_js', false, ['jquery']); 88 } 89 90 /** 91 * Получание трекинг кода с удаленного апи 92 * @param $key 93 * 94 * @return false|mixed 95 */ 96 private function getTrackingCode($key) 97 { 98 if (self::IS_LOCAL) { 99 add_action('http_api_curl', function($handle) { 100 //Don't verify SSL certs 101 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); 102 curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); 103 }, 10); 104 } 15 private $apiError = ''; 16 17 /** 18 * Вызывается при каждом F5. Обновить сохраненный в плагине трекинг код с удалённого API 19 * @return void 20 */ 21 public function updateScript() 22 { 23 if ($key = get_option('udimi_optin_key')) { 24 $date = get_option('udimi_optin_date'); 25 if (!$date || !strtotime($date) || time() - strtotime($date) > self::OPTIN_CODE_TTL_HOURS * 3600) { 26 if ($trackingCode = $this->getTrackingCode($key)) { 27 update_option('udimi_optin_script', $trackingCode); 28 update_option('udimi_optin_date', date('Y-m-d H:i:s')); 29 } 30 } 31 } 32 } 33 34 public function ajax_connect() 35 { 36 $key = isset($_POST['key']) && !is_array($_POST['key']) ? trim($_POST['key']) : ''; 37 38 $error = ''; 39 if (empty($key)) { 40 $error = '"Client Key" is empty'; 41 } else { 42 43 $trackingCode = $this->getTrackingCode($key); 44 if (!$trackingCode) { 45 $error = $this->getApiError(); 46 } 47 } 48 49 if ($error) { 50 wp_send_json_error([ 51 'error' => $error, 52 ]); 53 } else { 54 55 update_option('udimi_optin_key', $key); 56 update_option('udimi_optin_script', stripslashes($trackingCode)); 57 update_option('udimi_optin_date', date('Y-m-d H:i:s')); 58 wp_send_json_success(); 59 } 60 } 61 62 public function ajax_disconnect() 63 { 64 delete_option('udimi_optin_key'); 65 delete_option('udimi_optin_script'); 66 delete_option('udimi_optin_date'); 67 wp_send_json_success(); 68 } 69 70 /** 71 * @return string 72 */ 73 public function getApiError() 74 { 75 return $this->apiError; 76 } 77 78 public function addAdminScript() 79 { 80 wp_enqueue_script('jquery'); 81 wp_register_script('udimi_optin_admin_js', plugins_url(self::PLUGIN_FOLDER . '/js/adminpanel.js'), ['jquery']); 82 wp_localize_script( 83 'udimi_optin_admin_js', 84 'config', 85 [ 86 'saveUrl' => admin_url('admin-ajax.php'), 87 ] 88 ); 89 wp_enqueue_script('udimi_optin_admin_js', false, ['jquery']); 90 } 91 92 /** 93 * Получание трекинг кода с удаленного апи 94 * @param $key 95 * 96 * @return false|mixed 97 */ 98 private function getTrackingCode($key) 99 { 100 if (self::IS_LOCAL) { 101 add_action('http_api_curl', function($handle) { 102 //Don't verify SSL certs 103 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); 104 curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); 105 }, 10); 106 } 105 107 106 108 $res = wp_remote_get($this->getApiUrl($key), [ … … 110 112 ]); 111 113 112 if (!is_array($res)) {113 $this->apiError = 'WP_error';114 115 return false;116 } else {117 if ($res['response']['code'] != 200) {118 if (!$errorResponse = json_decode($res['body'])) {119 $errorMessage = 'Unknown error';120 } else {121 $errorMessage = $errorResponse->message;122 }123 124 if (self::IS_LOCAL) {125 $this->apiError = '['.$res['response']['code'].'] ' . $res['response']['message'] . '. ' . $errorMessage;126 } else {127 $this->apiError = $res['response']['code'] === 404 ?128 'Client not found. Make sure the "Client Key" is correct.' :129 $errorMessage;130 }131 132 return false;133 } else {134 if (!$successResponse = json_decode($res['body'])) {135 $tracking_code = '';136 } else {137 $tracking_code = !empty($successResponse->tracking_code) ? $successResponse->tracking_code : '';138 }139 140 if (empty($tracking_code)) {141 $this->apiError = 'Invalid Udimi API service response format';142 return false;143 }144 145 return $tracking_code;146 }147 }148 }149 150 private function getApiUrl($key)151 {114 if (!is_array($res)) { 115 $this->apiError = 'WP_error'; 116 117 return false; 118 } else { 119 if ($res['response']['code'] != 200) { 120 if (!$errorResponse = json_decode($res['body'])) { 121 $errorMessage = 'Unknown error'; 122 } else { 123 $errorMessage = $errorResponse->message; 124 } 125 126 if (self::IS_LOCAL) { 127 $this->apiError = '['.$res['response']['code'].'] ' . $res['response']['message'] . '. ' . $errorMessage; 128 } else { 129 $this->apiError = $res['response']['code'] === 404 ? 130 'Client not found. Make sure the "Client Key" is correct.' : 131 $errorMessage; 132 } 133 134 return false; 135 } else { 136 if (!$successResponse = json_decode($res['body'])) { 137 $tracking_code = ''; 138 } else { 139 $tracking_code = !empty($successResponse->tracking_code) ? $successResponse->tracking_code : ''; 140 } 141 142 if (empty($tracking_code)) { 143 $this->apiError = 'Invalid Udimi API service response format'; 144 return false; 145 } 146 147 return $tracking_code; 148 } 149 } 150 } 151 152 private function getApiUrl($key) 153 { 152 154 return (self::IS_LOCAL ? self::API_HOST_LOCAL : self::API_HOST_PROD) . "/v1/guests/wordpress-plugin-ot/get-code/$key?v=" . self::UA; 153 155 } 154 156 155 //////////// 156 157 public function registerPlugin($plugin_array) 158 { 159 $plugin_array[self::PLUGIN_NAME] = get_site_url() . '/index.php?scplugin'; 160 161 return $plugin_array; 162 } 163 164 /** 165 * Метод WP. Вставить трекинг код в исходный код блога 166 * @return void 167 */ 168 public function addOptinScript() 169 { 170 if ($trackingCode = get_option('udimi_optin_script', '')) { 171 echo $trackingCode; 172 } 173 } 174 175 /** 176 * Метод WP. Вставить пункт в админскую панель 177 * @return void 178 */ 179 public function addAdminMenu() 180 { 181 add_menu_page('Udimi tracker', 'Udimi tracker', 'manage_options', 'udimi-optin-settings', [ 182 $this, 183 'renderSettings' 184 ], plugins_url(self::PLUGIN_FOLDER . '/images/logo16.png')); 185 186 add_submenu_page('udimi-optin-settings', 'Settings', 'Settings', 'manage_options', 'udimi-optin-settings', [ 187 $this, 188 'renderSettings' 189 ]); 190 } 191 192 public function renderSettings() 193 { 194 include(dirname(__FILE__) . '/admin/settings.php'); 195 } 157 //////////// 158 159 public function registerPlugin($plugin_array) 160 { 161 $plugin_array[self::PLUGIN_NAME] = get_site_url() . '/index.php?scplugin'; 162 163 return $plugin_array; 164 } 165 166 /** 167 * Метод WP. Вставить трекинг код в исходный код блога 168 * @return void 169 */ 170 public function addOptinScript() 171 { 172 if ($trackingCode = get_option('udimi_optin_script', '')) { 173 echo $trackingCode; 174 } 175 } 176 177 /** 178 * Метод WP. Вставить пункт в админскую панель 179 * @return void 180 */ 181 public function addAdminMenu() 182 { 183 add_menu_page('Udimi tracker', 'Udimi tracker', 'manage_options', 'udimi-optin-settings', [ 184 $this, 185 'renderSettings' 186 ], plugins_url(self::PLUGIN_FOLDER . '/images/logo16.png')); 187 188 add_submenu_page('udimi-optin-settings', 'Settings', 'Settings', 'manage_options', 'udimi-optin-settings', [ 189 $this, 190 'renderSettings' 191 ]); 192 } 193 194 public function renderSettings() 195 { 196 include(dirname(__FILE__) . '/admin/settings.php'); 197 } 198 199 private function logToFile($message) { 200 $upload_dir = wp_upload_dir(); 201 $log_file = $upload_dir['basedir'] . '/api_log.txt'; 202 203 // Откроем файл для записи, если файла нет - он будет создан 204 $file = fopen($log_file, 'a'); 205 if ($file) { 206 fwrite($file, $message . "\n"); 207 fclose($file); 208 } 209 } 196 210 } -
udimi-optin/trunk/class.php
r3101942 r3102025 3 3 class UdimiOptin 4 4 { 5 const IS_LOCAL = false;6 const API_HOST_PROD = 'https://api.udimi.com';7 const API_HOST_LOCAL = 'https://api.udimi.me';8 const HOST_PROD = 'https://udimi.com';9 const HOST_LOCAL = 'https://udimi.me';10 const PLUGIN_NAME = 'udimioptin';11 const PLUGIN_FOLDER = 'udimi-optin';12 const OPTIN_CODE_TTL_HOURS = 24;5 const IS_LOCAL = false; 6 const API_HOST_PROD = 'https://api.udimi.com'; 7 const API_HOST_LOCAL = 'https://api.udimi.me'; 8 const HOST_PROD = 'https://udimi.com'; 9 const HOST_LOCAL = 'https://udimi.me'; 10 const PLUGIN_NAME = 'udimioptin'; 11 const PLUGIN_FOLDER = 'udimi-optin'; 12 const OPTIN_CODE_TTL_HOURS = 24; 13 13 const UA = 'UdimiOptinWordpress-12062024'; 14 14 15 private $apiError = ''; 16 17 /** 18 * Вызывается при каждом F5. Обновить сохраненный в плагине трекинг код с удалённого API 19 * @return void 20 */ 21 public function updateScript() 22 { 23 if ($key = get_option('udimi_optin_key')) { 24 $date = get_option('udimi_optin_date'); 25 if (!$date || !strtotime($date) || time() - strtotime($date) > self::OPTIN_CODE_TTL_HOURS * 3600) { 26 if ($trackingCode = $this->getTrackingCode($key)) { 27 update_option('udimi_optin_script', $trackingCode); 28 update_option('udimi_optin_date', date('Y-m-d H:i:s')); 29 } 30 } 31 } 32 } 33 34 public function ajax_connect() 35 { 36 $key = isset($_POST['key']) && !is_array($_POST['key']) ? trim($_POST['key']) : ''; 37 38 $error = ''; 39 if (empty($key)) { 40 $error = '"Client Key" is empty'; 41 } else { 42 $trackingCode = $this->getTrackingCode($key); 43 if (!$trackingCode) { 44 $error = $this->getApiError(); 45 } 46 } 47 48 if ($error) { 49 wp_send_json_error([ 50 'error' => $error, 51 ]); 52 } else { 53 update_option('udimi_optin_key', $key); 54 update_option('udimi_optin_script', stripslashes($this->getTrackingCode($key))); 55 update_option('udimi_optin_date', date('Y-m-d H:i:s')); 56 wp_send_json_success(); 57 } 58 } 59 60 public function ajax_disconnect() 61 { 62 delete_option('udimi_optin_key'); 63 delete_option('udimi_optin_script'); 64 delete_option('udimi_optin_date'); 65 wp_send_json_success(); 66 } 67 68 /** 69 * @return string 70 */ 71 public function getApiError() 72 { 73 return $this->apiError; 74 } 75 76 public function addAdminScript() 77 { 78 wp_enqueue_script('jquery'); 79 wp_register_script('udimi_optin_admin_js', plugins_url(self::PLUGIN_FOLDER . '/js/adminpanel.js'), ['jquery']); 80 wp_localize_script( 81 'udimi_optin_admin_js', 82 'config', 83 [ 84 'saveUrl' => admin_url('admin-ajax.php'), 85 ] 86 ); 87 wp_enqueue_script('udimi_optin_admin_js', false, ['jquery']); 88 } 89 90 /** 91 * Получание трекинг кода с удаленного апи 92 * @param $key 93 * 94 * @return false|mixed 95 */ 96 private function getTrackingCode($key) 97 { 98 if (self::IS_LOCAL) { 99 add_action('http_api_curl', function($handle) { 100 //Don't verify SSL certs 101 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); 102 curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); 103 }, 10); 104 } 15 private $apiError = ''; 16 17 /** 18 * Вызывается при каждом F5. Обновить сохраненный в плагине трекинг код с удалённого API 19 * @return void 20 */ 21 public function updateScript() 22 { 23 if ($key = get_option('udimi_optin_key')) { 24 $date = get_option('udimi_optin_date'); 25 if (!$date || !strtotime($date) || time() - strtotime($date) > self::OPTIN_CODE_TTL_HOURS * 3600) { 26 if ($trackingCode = $this->getTrackingCode($key)) { 27 update_option('udimi_optin_script', $trackingCode); 28 update_option('udimi_optin_date', date('Y-m-d H:i:s')); 29 } 30 } 31 } 32 } 33 34 public function ajax_connect() 35 { 36 $key = isset($_POST['key']) && !is_array($_POST['key']) ? trim($_POST['key']) : ''; 37 38 $error = ''; 39 if (empty($key)) { 40 $error = '"Client Key" is empty'; 41 } else { 42 43 $trackingCode = $this->getTrackingCode($key); 44 if (!$trackingCode) { 45 $error = $this->getApiError(); 46 } 47 } 48 49 if ($error) { 50 wp_send_json_error([ 51 'error' => $error, 52 ]); 53 } else { 54 55 update_option('udimi_optin_key', $key); 56 update_option('udimi_optin_script', stripslashes($trackingCode)); 57 update_option('udimi_optin_date', date('Y-m-d H:i:s')); 58 wp_send_json_success(); 59 } 60 } 61 62 public function ajax_disconnect() 63 { 64 delete_option('udimi_optin_key'); 65 delete_option('udimi_optin_script'); 66 delete_option('udimi_optin_date'); 67 wp_send_json_success(); 68 } 69 70 /** 71 * @return string 72 */ 73 public function getApiError() 74 { 75 return $this->apiError; 76 } 77 78 public function addAdminScript() 79 { 80 wp_enqueue_script('jquery'); 81 wp_register_script('udimi_optin_admin_js', plugins_url(self::PLUGIN_FOLDER . '/js/adminpanel.js'), ['jquery']); 82 wp_localize_script( 83 'udimi_optin_admin_js', 84 'config', 85 [ 86 'saveUrl' => admin_url('admin-ajax.php'), 87 ] 88 ); 89 wp_enqueue_script('udimi_optin_admin_js', false, ['jquery']); 90 } 91 92 /** 93 * Получание трекинг кода с удаленного апи 94 * @param $key 95 * 96 * @return false|mixed 97 */ 98 private function getTrackingCode($key) 99 { 100 if (self::IS_LOCAL) { 101 add_action('http_api_curl', function($handle) { 102 //Don't verify SSL certs 103 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); 104 curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); 105 }, 10); 106 } 105 107 106 108 $res = wp_remote_get($this->getApiUrl($key), [ … … 110 112 ]); 111 113 112 if (!is_array($res)) {113 $this->apiError = 'WP_error';114 115 return false;116 } else {117 if ($res['response']['code'] != 200) {118 if (!$errorResponse = json_decode($res['body'])) {119 $errorMessage = 'Unknown error';120 } else {121 $errorMessage = $errorResponse->message;122 }123 124 if (self::IS_LOCAL) {125 $this->apiError = '['.$res['response']['code'].'] ' . $res['response']['message'] . '. ' . $errorMessage;126 } else {127 $this->apiError = $res['response']['code'] === 404 ?128 'Client not found. Make sure the "Client Key" is correct.' :129 $errorMessage;130 }131 132 return false;133 } else {134 if (!$successResponse = json_decode($res['body'])) {135 $tracking_code = '';136 } else {137 $tracking_code = !empty($successResponse->tracking_code) ? $successResponse->tracking_code : '';138 }139 140 if (empty($tracking_code)) {141 $this->apiError = 'Invalid Udimi API service response format';142 return false;143 }144 145 return $tracking_code;146 }147 }148 }149 150 private function getApiUrl($key)151 {114 if (!is_array($res)) { 115 $this->apiError = 'WP_error'; 116 117 return false; 118 } else { 119 if ($res['response']['code'] != 200) { 120 if (!$errorResponse = json_decode($res['body'])) { 121 $errorMessage = 'Unknown error'; 122 } else { 123 $errorMessage = $errorResponse->message; 124 } 125 126 if (self::IS_LOCAL) { 127 $this->apiError = '['.$res['response']['code'].'] ' . $res['response']['message'] . '. ' . $errorMessage; 128 } else { 129 $this->apiError = $res['response']['code'] === 404 ? 130 'Client not found. Make sure the "Client Key" is correct.' : 131 $errorMessage; 132 } 133 134 return false; 135 } else { 136 if (!$successResponse = json_decode($res['body'])) { 137 $tracking_code = ''; 138 } else { 139 $tracking_code = !empty($successResponse->tracking_code) ? $successResponse->tracking_code : ''; 140 } 141 142 if (empty($tracking_code)) { 143 $this->apiError = 'Invalid Udimi API service response format'; 144 return false; 145 } 146 147 return $tracking_code; 148 } 149 } 150 } 151 152 private function getApiUrl($key) 153 { 152 154 return (self::IS_LOCAL ? self::API_HOST_LOCAL : self::API_HOST_PROD) . "/v1/guests/wordpress-plugin-ot/get-code/$key?v=" . self::UA; 153 155 } 154 156 155 //////////// 156 157 public function registerPlugin($plugin_array) 158 { 159 $plugin_array[self::PLUGIN_NAME] = get_site_url() . '/index.php?scplugin'; 160 161 return $plugin_array; 162 } 163 164 /** 165 * Метод WP. Вставить трекинг код в исходный код блога 166 * @return void 167 */ 168 public function addOptinScript() 169 { 170 if ($trackingCode = get_option('udimi_optin_script', '')) { 171 echo $trackingCode; 172 } 173 } 174 175 /** 176 * Метод WP. Вставить пункт в админскую панель 177 * @return void 178 */ 179 public function addAdminMenu() 180 { 181 add_menu_page('Udimi tracker', 'Udimi tracker', 'manage_options', 'udimi-optin-settings', [ 182 $this, 183 'renderSettings' 184 ], plugins_url(self::PLUGIN_FOLDER . '/images/logo16.png')); 185 186 add_submenu_page('udimi-optin-settings', 'Settings', 'Settings', 'manage_options', 'udimi-optin-settings', [ 187 $this, 188 'renderSettings' 189 ]); 190 } 191 192 public function renderSettings() 193 { 194 include(dirname(__FILE__) . '/admin/settings.php'); 195 } 157 //////////// 158 159 public function registerPlugin($plugin_array) 160 { 161 $plugin_array[self::PLUGIN_NAME] = get_site_url() . '/index.php?scplugin'; 162 163 return $plugin_array; 164 } 165 166 /** 167 * Метод WP. Вставить трекинг код в исходный код блога 168 * @return void 169 */ 170 public function addOptinScript() 171 { 172 if ($trackingCode = get_option('udimi_optin_script', '')) { 173 echo $trackingCode; 174 } 175 } 176 177 /** 178 * Метод WP. Вставить пункт в админскую панель 179 * @return void 180 */ 181 public function addAdminMenu() 182 { 183 add_menu_page('Udimi tracker', 'Udimi tracker', 'manage_options', 'udimi-optin-settings', [ 184 $this, 185 'renderSettings' 186 ], plugins_url(self::PLUGIN_FOLDER . '/images/logo16.png')); 187 188 add_submenu_page('udimi-optin-settings', 'Settings', 'Settings', 'manage_options', 'udimi-optin-settings', [ 189 $this, 190 'renderSettings' 191 ]); 192 } 193 194 public function renderSettings() 195 { 196 include(dirname(__FILE__) . '/admin/settings.php'); 197 } 198 199 private function logToFile($message) { 200 $upload_dir = wp_upload_dir(); 201 $log_file = $upload_dir['basedir'] . '/api_log.txt'; 202 203 // Откроем файл для записи, если файла нет - он будет создан 204 $file = fopen($log_file, 'a'); 205 if ($file) { 206 fwrite($file, $message . "\n"); 207 fclose($file); 208 } 209 } 196 210 }
Note: See TracChangeset
for help on using the changeset viewer.