plugin-data-sender.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Core\Onboard; |
| 4 | |
| 5 | use ShopEngine\Core\Onboard\Onboard; |
| 6 | use ShopEngine\Traits\Singleton; |
| 7 | |
| 8 | defined('ABSPATH') || exit; |
| 9 | |
| 10 | class Plugin_Data_Sender |
| 11 | { |
| 12 | use Singleton; |
| 13 | |
| 14 | /** |
| 15 | * @var array |
| 16 | */ |
| 17 | private $installedPlugins = []; |
| 18 | /** |
| 19 | * @var array |
| 20 | */ |
| 21 | private $themes = []; |
| 22 | /** |
| 23 | * @var array |
| 24 | */ |
| 25 | private $activatedPlugins = []; |
| 26 | |
| 27 | public function __construct() |
| 28 | { |
| 29 | $this->set_activated_plugins(); |
| 30 | $this->set_installed_plugins(); |
| 31 | $this->setThemes(); |
| 32 | } |
| 33 | |
| 34 | private function set_activated_plugins() |
| 35 | { |
| 36 | foreach (apply_filters('active_plugins', get_option('active_plugins')) as $plugin) { |
| 37 | array_push($this->activatedPlugins, $plugin); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | private function set_installed_plugins() |
| 42 | { |
| 43 | foreach (get_plugins() as $key => $plugin) { |
| 44 | $status = false; |
| 45 | if (in_array($key, $this->activatedPlugins)) { |
| 46 | $status = true; |
| 47 | } |
| 48 | array_push($this->installedPlugins, [ |
| 49 | 'name' => $plugin['Name'], |
| 50 | 'version' => $plugin['Version'], |
| 51 | 'is_active' => $status |
| 52 | ]); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | private function setThemes() |
| 57 | { |
| 58 | $activeTheme = wp_get_theme()->get('Name'); |
| 59 | foreach (wp_get_themes() as $key => $theme) { |
| 60 | array_push($this->themes, [ |
| 61 | "name" => $theme->Name, |
| 62 | "version" => $theme->Version, |
| 63 | 'is_active' => $activeTheme == $theme->Name |
| 64 | ]); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param $route |
| 70 | */ |
| 71 | private function getUrl($route) |
| 72 | { |
| 73 | return Onboard::ACCOUNT_URL . '/sync/api/' . $route; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param $route |
| 78 | */ |
| 79 | public function send($route) |
| 80 | { |
| 81 | return wp_remote_post( |
| 82 | $this->getUrl($route), |
| 83 | [ |
| 84 | 'method' => 'POST', |
| 85 | 'data_format' => 'body', |
| 86 | 'headers' => [ |
| 87 | 'Content-Type' => 'application/json' |
| 88 | ], |
| 89 | 'body' => json_encode($this->get_data()) |
| 90 | ] |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param $route |
| 96 | * @param $data |
| 97 | */ |
| 98 | public function sendAutomizyData($route, $data) |
| 99 | { |
| 100 | return wp_remote_post( |
| 101 | $this->getUrl($route), |
| 102 | [ |
| 103 | 'method' => 'POST', |
| 104 | 'data_format' => 'body', |
| 105 | 'headers' => [ |
| 106 | 'Content-Type' => 'application/json' |
| 107 | ], |
| 108 | 'body' => json_encode($data) |
| 109 | ] |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | public function get_data() |
| 114 | { |
| 115 | return [ |
| 116 | 'environment_id' => Onboard::ENVIRONMENT_ID, |
| 117 | "domain" => get_site_url(), |
| 118 | "total_user" => count_users()['total_users'], |
| 119 | "themes" => $this->themes, |
| 120 | "plugins" => $this->installedPlugins, |
| 121 | "php_version" => phpversion(), |
| 122 | "db_version" => mysqli_get_client_version(), //phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysqli_get_client_version |
| 123 | "server_name" => !empty($_SERVER['SERVER_SOFTWARE']) ? explode(' ', sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE'])))[0] : '', |
| 124 | "max_execution_time" => ini_get('max_execution_time'), |
| 125 | "php_memory_size" => ini_get('memory_limit'), |
| 126 | "language" => get_locale() |
| 127 | ]; |
| 128 | } |
| 129 | } |
| 130 |