tools.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * Author: ExactMetrics team
  4. * Author URI: https://exactmetrics.com
  5. * Copyright 2018 ExactMetrics team
  6. * License: GPLv2 or later
  7. * License URI: http://www.gnu.org/licenses/gpl-2.0.html
  8. */
  9. // Exit if accessed directly
  10. if ( ! defined( 'ABSPATH' ) )
  11. exit();
  12. if ( ! class_exists( 'GADWP_Tools' ) ) {
  13. class GADWP_Tools {
  14. public static function get_countrycodes() {
  15. include 'iso3166.php';
  16. return $country_codes;
  17. }
  18. public static function guess_default_domain( $profiles ) {
  19. $domain = get_option( 'siteurl' );
  20. $domain = str_ireplace( array( 'http://', 'https://' ), '', $domain );
  21. if ( ! empty( $profiles ) ) {
  22. foreach ( $profiles as $items ) {
  23. if ( strpos( $items[3], $domain ) ) {
  24. return $items[1];
  25. }
  26. }
  27. return $profiles[0][1];
  28. } else {
  29. return '';
  30. }
  31. }
  32. public static function get_selected_profile( $profiles, $profile ) {
  33. if ( ! empty( $profiles ) ) {
  34. foreach ( $profiles as $item ) {
  35. if ( $item[1] == $profile ) {
  36. return $item;
  37. }
  38. }
  39. }
  40. }
  41. public static function get_root_domain() {
  42. $url = site_url();
  43. $root = explode( '/', $url );
  44. preg_match( '/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', str_ireplace( 'www', '', isset( $root[2] ) ? $root[2] : $url ), $root );
  45. if ( isset( $root['domain'] ) ) {
  46. return $root['domain'];
  47. } else {
  48. return '';
  49. }
  50. }
  51. public static function strip_protocol( $domain ) {
  52. return str_replace( array( "https://", "http://", " " ), "", $domain );
  53. }
  54. public static function colourVariator( $colour, $per ) {
  55. $colour = substr( $colour, 1 );
  56. $rgb = '';
  57. $per = $per / 100 * 255;
  58. if ( $per < 0 ) {
  59. // Darker
  60. $per = abs( $per );
  61. for ( $x = 0; $x < 3; $x++ ) {
  62. $c = hexdec( substr( $colour, ( 2 * $x ), 2 ) ) - $per;
  63. $c = ( $c < 0 ) ? 0 : dechex( $c );
  64. $rgb .= ( strlen( $c ) < 2 ) ? '0' . $c : $c;
  65. }
  66. } else {
  67. // Lighter
  68. for ( $x = 0; $x < 3; $x++ ) {
  69. $c = hexdec( substr( $colour, ( 2 * $x ), 2 ) ) + $per;
  70. $c = ( $c > 255 ) ? 'ff' : dechex( $c );
  71. $rgb .= ( strlen( $c ) < 2 ) ? '0' . $c : $c;
  72. }
  73. }
  74. return '#' . $rgb;
  75. }
  76. public static function variations( $base ) {
  77. $variations[] = $base;
  78. $variations[] = self::colourVariator( $base, - 10 );
  79. $variations[] = self::colourVariator( $base, + 10 );
  80. $variations[] = self::colourVariator( $base, + 20 );
  81. $variations[] = self::colourVariator( $base, - 20 );
  82. $variations[] = self::colourVariator( $base, + 30 );
  83. $variations[] = self::colourVariator( $base, - 30 );
  84. return $variations;
  85. }
  86. public static function check_roles( $access_level, $tracking = false ) {
  87. if ( is_user_logged_in() && isset( $access_level ) ) {
  88. $current_user = wp_get_current_user();
  89. $roles = (array) $current_user->roles;
  90. if ( ( current_user_can( 'manage_options' ) ) && ! $tracking ) {
  91. return true;
  92. }
  93. if ( count( array_intersect( $roles, $access_level ) ) > 0 ) {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. }
  100. public static function unset_cookie( $name ) {
  101. $name = 'gadwp_wg_' . $name;
  102. setcookie( $name, '', time() - 3600, '/' );
  103. $name = 'gadwp_ir_' . $name;
  104. setcookie( $name, '', time() - 3600, '/' );
  105. }
  106. public static function set_cache( $name, $value, $expiration = 0 ) {
  107. $option = array( 'value' => $value, 'expires' => time() + (int) $expiration );
  108. update_option( 'gadwp_cache_' . $name, $option, 'no' );
  109. }
  110. public static function delete_cache( $name ) {
  111. delete_option( 'gadwp_cache_' . $name );
  112. }
  113. public static function get_cache( $name ) {
  114. $option = get_option( 'gadwp_cache_' . $name );
  115. if ( false === $option || ! isset( $option['value'] ) || ! isset( $option['expires'] ) ) {
  116. return false;
  117. }
  118. if ( $option['expires'] < time() ) {
  119. delete_option( 'gadwp_cache_' . $name );
  120. return false;
  121. } else {
  122. return $option['value'];
  123. }
  124. }
  125. public static function clear_cache() {
  126. global $wpdb;
  127. $sqlquery = $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'gadwp_cache_qr%%'" );
  128. }
  129. public static function get_sites( $args ) { // Use wp_get_sites() if WP version is lower than 4.6.0
  130. global $wp_version;
  131. if ( version_compare( $wp_version, '4.6.0', '<' ) ) {
  132. return wp_get_sites( $args );
  133. } else {
  134. foreach ( get_sites( $args ) as $blog ) {
  135. $blogs[] = (array) $blog; // Convert WP_Site object to array
  136. }
  137. return $blogs;
  138. }
  139. }
  140. /**
  141. * Loads a view file
  142. *
  143. * $data parameter will be available in the template file as $data['value']
  144. *
  145. * @param string $template - Template file to load
  146. * @param array $data - data to pass along to the template
  147. * @return boolean - If template file was found
  148. **/
  149. public static function load_view( $path, $data = array() ) {
  150. if ( file_exists( GADWP_DIR . $path ) ) {
  151. require_once ( GADWP_DIR . $path );
  152. return true;
  153. }
  154. return false;
  155. }
  156. public static function doing_it_wrong( $function, $message, $version ) {
  157. if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
  158. if ( is_null( $version ) ) {
  159. $version = '';
  160. } else {
  161. /* translators: %s: version number */
  162. $version = sprintf( __( 'This message was added in version %s.', 'google-analytics-dashboard-for-wp' ), $version );
  163. }
  164. /* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: Version information message */
  165. trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', 'google-analytics-dashboard-for-wp' ), $function, $message, $version ) );
  166. }
  167. }
  168. public static function get_dom_from_content( $content ) {
  169. $libxml_previous_state = libxml_use_internal_errors( true );
  170. if ( class_exists( 'DOMDocument' ) ) {
  171. $dom = new DOMDocument();
  172. $result = $dom->loadHTML( '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' . $content . '</body></html>' );
  173. libxml_clear_errors();
  174. libxml_use_internal_errors( $libxml_previous_state );
  175. if ( ! $result ) {
  176. return false;
  177. }
  178. return $dom;
  179. } else {
  180. self::set_error( __( 'DOM is disabled or libxml PHP extension is missing. Contact your hosting provider. Automatic tracking of events for AMP pages is not possible.', 'google-analytics-dashboard-for-wp' ), 24 * 60 * 60 );
  181. return false;
  182. }
  183. }
  184. public static function get_content_from_dom( $dom ) {
  185. $out = '';
  186. $body = $dom->getElementsByTagName( 'body' )->item( 0 );
  187. foreach ( $body->childNodes as $node ) {
  188. $out .= $dom->saveXML( $node );
  189. }
  190. return $out;
  191. }
  192. public static function array_keys_rename( $options, $keys ) {
  193. foreach ( $keys as $key => $newkey ) {
  194. if ( isset( $options[$key] ) ) {
  195. $options[$newkey] = $options[$key];
  196. unset( $options[$key] );
  197. }
  198. }
  199. return $options;
  200. }
  201. public static function set_error( $e, $timeout ) {
  202. if ( is_object( $e ) ) {
  203. self::set_cache( 'last_error', date( 'Y-m-d H:i:s' ) . ': ' . esc_html( $e ), $timeout );
  204. if ( method_exists( $e, 'getCode' ) && method_exists( $e, 'getErrors' ) ) {
  205. self::set_cache( 'gapi_errors', array( $e->getCode(), (array) $e->getErrors() ), $timeout );
  206. }
  207. } else {
  208. self::set_cache( 'last_error', date( 'Y-m-d H:i:s' ) . ': ' . esc_html( $e ), $timeout );
  209. }
  210. // Count Errors until midnight
  211. $midnight = strtotime( "tomorrow 00:00:00" ); // UTC midnight
  212. $midnight = $midnight + 8 * 3600; // UTC 8 AM
  213. $tomidnight = $midnight - time();
  214. $errors_count = self::get_cache( 'errors_count' );
  215. $errors_count = (int) $errors_count + 1;
  216. self::set_cache( 'errors_count', $errors_count, $tomidnight );
  217. }
  218. public static function anonymize_options( $options ) {
  219. global $wp_version;
  220. $options['wp_version'] = $wp_version;
  221. $options['gadwp_version'] = GADWP_CURRENT_VERSION;
  222. if ( $options['token'] ) {
  223. $options['token'] = 'HIDDEN';
  224. }
  225. if ( $options['client_secret'] ) {
  226. $options['client_secret'] = 'HIDDEN';
  227. }
  228. return $options;
  229. }
  230. public static function system_info() {
  231. $info = '';
  232. // Server Software
  233. $server_soft = "-";
  234. if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
  235. $server_soft = $_SERVER['SERVER_SOFTWARE'];
  236. }
  237. $info .= 'Server Info: ' . $server_soft . "\n";
  238. // PHP version
  239. if ( defined( 'PHP_VERSION' ) ) {
  240. $info .= 'PHP Version: ' . PHP_VERSION . "\n";
  241. } else if ( defined( 'HHVM_VERSION' ) ) {
  242. $info .= 'HHVM Version: ' . HHVM_VERSION . "\n";
  243. } else {
  244. $info .= 'Other Version: ' . '-' . "\n";
  245. }
  246. /*
  247. * PHP extensions
  248. * if ( is_callable( 'get_loaded_extensions' ) ) {
  249. * $info .= 'Loaded Extensions: ' . implode(', ', get_loaded_extensions()) . "\n";
  250. * } else {
  251. * $info .= 'Loaded Extensions: ' . '-' . "\n";
  252. * }
  253. */
  254. // cURL Info
  255. if ( function_exists( 'curl_version' ) && function_exists( 'curl_exec' ) ) {
  256. $curl_version = curl_version();
  257. if ( ! empty( $curl_version ) ) {
  258. $curl_ver = $curl_version['version'] . " " . $curl_version['ssl_version'];
  259. } else {
  260. $curl_ver = '-';
  261. }
  262. } else {
  263. $curl_ver = '-';
  264. }
  265. $info .= 'cURL Info: ' . $curl_ver . "\n";
  266. // Gzip
  267. if ( is_callable( 'gzopen' ) ) {
  268. $gzip = true;
  269. } else {
  270. $gzip = false;
  271. }
  272. $gzip_status = ( $gzip ) ? 'Yes' : 'No';
  273. $info .= 'Gzip: ' . $gzip_status . "\n";
  274. return $info;
  275. }
  276. }
  277. }