class-epsilon-plugin-request-sab.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. class Epsilon_Plugin_Request_SAB {
  3. /**
  4. * Url for the request
  5. *
  6. * @var string
  7. */
  8. private $url = 'https://tamewp.com/';
  9. /**
  10. * Api endpoint
  11. *
  12. * @var string
  13. */
  14. private $endpoint = 'wp-json/epsilon/v1/add-tracking-data';
  15. /**
  16. * Private data
  17. *
  18. * @var array
  19. */
  20. private $data = array(
  21. 'server' => array(),
  22. 'user' => array(),
  23. 'wordpress' => array(
  24. 'deactivated_plugin' => array(),
  25. ),
  26. );
  27. /**
  28. * Plugin file
  29. *
  30. * @var string
  31. */
  32. private $plugin_file = '';
  33. private $allow_tracking = 0;
  34. public $request_successful = false;
  35. function __construct( $_plugin_file, $args ) {
  36. // Set variables
  37. $this->allow_tracking = $args['tracking'];
  38. $this->plugin_file = $_plugin_file;
  39. $this->data['unique'] = md5( home_url() . get_bloginfo( 'admin_email' ) );
  40. $this->data['wordpress']['deactivated_plugin']['uninstall_reason'] = $args['reason'];
  41. $this->data['wordpress']['deactivated_plugin']['uninstall_details'] = $args['details'];
  42. // Start collecting data
  43. $this->_collect_data();
  44. $this->_generate_url();
  45. $this->request_successful = $this->_send_request();
  46. }
  47. /**
  48. * Collect all data for the request.
  49. *
  50. */
  51. private function _collect_data() {
  52. $current_plugin = get_plugin_data( $this->plugin_file );
  53. // Plugin data
  54. $this->data['wordpress']['deactivated_plugin']['slug'] = $current_plugin['TextDomain'];
  55. $this->data['wordpress']['deactivated_plugin']['name'] = $current_plugin['Name'];
  56. $this->data['wordpress']['deactivated_plugin']['version'] = $current_plugin['Version'];
  57. $this->data['wordpress']['deactivated_plugin']['author'] = $current_plugin['AuthorName'];
  58. if ( $this->allow_tracking ) {
  59. $this->_collect_wordpress_data();
  60. $this->_collect_server_data();
  61. $this->_collect_user_data();
  62. }
  63. }
  64. /**
  65. * Collect WordPress data.
  66. *
  67. */
  68. private function _collect_wordpress_data() {
  69. $this->data['wordpress']['locale'] = ( get_bloginfo( 'version' ) >= 4.7 ) ? get_user_locale() : get_locale();
  70. $this->data['wordpress']['wp_version'] = get_bloginfo( 'version' );
  71. $this->data['wordpress']['multisite'] = is_multisite();
  72. $this->data['wordpress']['themes'] = $this->get_themes();
  73. $this->data['wordpress']['plugins'] = $this->get_plugins();
  74. }
  75. /**
  76. * Collect server data.
  77. *
  78. */
  79. private function _collect_server_data() {
  80. $this->data['server']['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '';
  81. $this->data['server']['php_version'] = phpversion();
  82. $this->data['server']['url'] = home_url();
  83. }
  84. /**
  85. * Collect user data.
  86. *
  87. */
  88. private function _collect_user_data() {
  89. $admin = get_user_by( 'email', get_bloginfo( 'admin_email' ) );
  90. if ( ! $admin ) {
  91. $this->data['user']['email'] = '';
  92. $this->data['user']['first_name'] = '';
  93. $this->data['user']['last_name'] = '';
  94. } else {
  95. $this->data['user']['email'] = get_bloginfo( 'admin_email' );
  96. $this->data['user']['first_name'] = $admin->first_name;
  97. $this->data['user']['last_name'] = $admin->last_name;
  98. }
  99. }
  100. /**
  101. * Get current themes
  102. *
  103. * @return array
  104. */
  105. private function get_themes() {
  106. $theme = wp_get_theme();
  107. return array(
  108. 'installed' => $this->_get_installed_themes(),
  109. 'active' => array(
  110. 'slug' => get_stylesheet(),
  111. 'name' => $theme->get( 'Name' ),
  112. 'version' => $theme->get( 'Version' ),
  113. 'author' => $theme->get( 'Author' ),
  114. ),
  115. );
  116. }
  117. /**
  118. * Get an array of installed themes
  119. */
  120. private function _get_installed_themes() {
  121. $installed = wp_get_themes();
  122. $theme = get_stylesheet();
  123. $arr = array();
  124. foreach ( $installed as $slug => $info ) {
  125. if ( $slug === $theme ) {
  126. continue;
  127. }
  128. $arr[ $slug ] = array(
  129. 'slug' => $slug,
  130. 'name' => $info->get( 'Name' ),
  131. 'version' => $info->get( 'Version' ),
  132. 'author' => $info->get( 'Author' ),
  133. );
  134. };
  135. return $arr;
  136. }
  137. /**
  138. * Get a list of installed plugins
  139. */
  140. private function get_plugins() {
  141. if ( ! function_exists( 'get_plugins' ) ) {
  142. include ABSPATH . '/wp-admin/includes/plugin.php';
  143. }
  144. $plugins = get_plugins();
  145. $option = get_option( 'active_plugins', array() );
  146. $active = array();
  147. $installed = array();
  148. foreach ( $plugins as $id => $info ) {
  149. if ( in_array( $id, $active ) ) {
  150. continue;
  151. }
  152. $id = explode( '/', $id );
  153. $id = ucwords( str_replace( '-', ' ', $id[0] ) );
  154. $installed[] = $id;
  155. }
  156. foreach ( $option as $id ) {
  157. $id = explode( '/', $id );
  158. $id = ucwords( str_replace( '-', ' ', $id[0] ) );
  159. $active[] = $id;
  160. }
  161. return array(
  162. 'installed' => $installed,
  163. 'active' => $active,
  164. );
  165. }
  166. /**
  167. * Generate the url
  168. */
  169. protected function _generate_url() {
  170. $this->url = $this->url . $this->endpoint;
  171. }
  172. /**
  173. * Send dat to server.
  174. *
  175. */
  176. private function _send_request() {
  177. $request = wp_remote_post(
  178. $this->url, array(
  179. 'method' => 'POST',
  180. 'timeout' => 20,
  181. 'redirection' => 5,
  182. 'httpversion' => '1.1',
  183. 'blocking' => true,
  184. 'body' => $this->data,
  185. 'user-agent' => 'MT/EPSILON-CUSTOMER-TRACKING/' . esc_url( home_url() ),
  186. )
  187. );
  188. if ( is_wp_error( $request ) ) {
  189. return false;
  190. }
  191. return true;
  192. }
  193. }