| 1 |
<?php |
| 2 |
|
| 3 |
namespace OPTN\Includes\Utils; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Durbin_Client |
| 9 |
*/ |
| 10 |
class DurbinClient { |
| 11 |
|
| 12 |
// Do not change these constants. |
| 13 |
const DEACTIVATE_ACTION = 'deactive'; |
| 14 |
const ACTIVATE_ACTION = 'active'; |
| 15 |
const WIZARD_ACTION = 'wizard'; |
| 16 |
const URL = 'https://inside.wpxpo.com/wp-json/durbin/v1/analytics'; |
| 17 |
// const URL = 'http://data.local/wp-json/durbin/v1/analytics'; |
| 18 |
|
| 19 |
const PLUGIN_SLUG = 'optin'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Send data to Durbin |
| 23 |
* |
| 24 |
* @param DurbinClient::DEACTIVATE_ACTION|DurbinClient::ACTIVATE_ACTION|DurbinClient::WIZARD_ACTION $action_type action type. |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public static function send( $action_type ) { |
| 28 |
if ( ! current_user_can( OPTN_MIN_CAPABILITY ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! in_array( $action_type, array( self::DEACTIVATE_ACTION, self::ACTIVATE_ACTION, self::WIZARD_ACTION ), true ) ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
$data = self::get_common_data(); |
| 37 |
|
| 38 |
$data['action_type'] = $action_type; |
| 39 |
|
| 40 |
if ( self::DEACTIVATE_ACTION === $action_type ) { |
| 41 |
|
| 42 |
$id = isset( $_POST['cause_id'] ) ? sanitize_key( wp_unslash( $_POST['cause_id'] ) ) : null; // phpcs:ignore |
| 43 |
|
| 44 |
if ( ! empty( $id ) ) { |
| 45 |
$data['feedback'] = array( |
| 46 |
'id' => $id, |
| 47 |
'details' => isset( $_POST['cause_details'] ) ? sanitize_text_field( wp_unslash( $_POST['cause_details'] ) ) : null, // phpcs:ignore |
| 48 |
); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
wp_remote_post( |
| 53 |
self::URL, |
| 54 |
array( |
| 55 |
'timeout' => 30, |
| 56 |
'redirection' => 5, |
| 57 |
'headers' => array( |
| 58 |
'User-Agent' => 'wpxpo/' . md5( esc_url( home_url() ) ) . ';', |
| 59 |
'Accept' => 'application/json', |
| 60 |
), |
| 61 |
'blocking' => false, |
| 62 |
'httpversion' => '1.0', |
| 63 |
'body' => $data, |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get All the Installed Plugin Data |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
private static function get_installed_plugins() { |
| 74 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 75 |
include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 76 |
} |
| 77 |
|
| 78 |
$active = array(); |
| 79 |
$inactive = array(); |
| 80 |
$all_plugins = get_plugins(); |
| 81 |
$active_plugins = get_option( 'active_plugins', array() ); |
| 82 |
if ( is_multisite() ) { |
| 83 |
$active_plugins = array_merge( $active_plugins, array_keys( get_site_option( 'active_sitewide_plugins', array() ) ) ); |
| 84 |
} |
| 85 |
|
| 86 |
foreach ( $all_plugins as $key => $plugin ) { |
| 87 |
$slug = dirname( $key ); |
| 88 |
if ( empty( $slug ) || self::PLUGIN_SLUG === $slug ) { |
| 89 |
continue; |
| 90 |
} |
| 91 |
|
| 92 |
$arr = array( |
| 93 |
'title' => $plugin['Name'] ?? null, |
| 94 |
'slug' => $slug, |
| 95 |
); |
| 96 |
|
| 97 |
if ( in_array( $key, $active_plugins, true ) ) { |
| 98 |
$active[] = $arr; |
| 99 |
} else { |
| 100 |
$inactive[] = $arr; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return array( |
| 105 |
'active' => $active, |
| 106 |
'inactive' => $inactive, |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get Country Code |
| 112 |
* |
| 113 |
* @return string|null |
| 114 |
*/ |
| 115 |
private static function get_country_code() { |
| 116 |
|
| 117 |
$cached = get_transient( 'durbin_country_code' ); |
| 118 |
|
| 119 |
if ( false !== $cached ) { |
| 120 |
return $cached; |
| 121 |
} |
| 122 |
|
| 123 |
$res = wp_remote_get( |
| 124 |
'https://ipinfo.io/json', |
| 125 |
array( 'timeout' => 30 ) |
| 126 |
); |
| 127 |
|
| 128 |
if ( is_wp_error( $res ) || 200 !== wp_remote_retrieve_response_code( $res ) ) { |
| 129 |
return null; |
| 130 |
} |
| 131 |
|
| 132 |
$body = wp_remote_retrieve_body( $res ); |
| 133 |
$data = json_decode( $body, true ); |
| 134 |
|
| 135 |
$country = isset( $data['country'] ) ? $data['country'] : null; |
| 136 |
|
| 137 |
if ( ! empty( $country ) ) { |
| 138 |
set_transient( 'durbin_country_code', $country, 180 * DAY_IN_SECONDS ); |
| 139 |
} |
| 140 |
|
| 141 |
return $country; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get common data |
| 146 |
* |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
private static function get_common_data() { |
| 150 |
$user = wp_get_current_user(); |
| 151 |
$plugins_data = self::get_installed_plugins(); |
| 152 |
$user_name = $user->user_firstname ? $user->user_firstname . ( $user->user_lastname ? ' ' . $user->user_lastname : '' ) : $user->display_name; |
| 153 |
|
| 154 |
$data = array( |
| 155 |
'email' => $user->user_email, |
| 156 |
'name' => $user_name, |
| 157 |
'site_url' => esc_url( home_url() ), |
| 158 |
'plugin' => self::PLUGIN_SLUG, |
| 159 |
'theme' => get_stylesheet(), |
| 160 |
'plugins' => $plugins_data, |
| 161 |
'country_code' => self::get_country_code(), |
| 162 |
); |
| 163 |
|
| 164 |
return $data; |
| 165 |
} |
| 166 |
} |
| 167 |
|