<?php

namespace MasterAddons\Inc\Classes\Notifications\Base;

// No, Direct access Sir !!!
if (!defined('ABSPATH')) {
	exit;
}

trait User_Data
{

	/**
	 * Get plugins list
	 *
	 * @author Jewel Theme <support@jeweltheme.com>
	 */
	public function get_plugins_list()
	{
		if (!function_exists('get_plugins')) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$plugins             = get_plugins();
		$activated_plugins   = '==== Activated Plugins List ====' . PHP_EOL;
		$deactivated_plugins = PHP_EOL . PHP_EOL . '==== Deactivated Plugins List ====' . PHP_EOL;

		$active_plugins_keys = get_option('active_plugins', array());
		$inactive_counter    = array();
		$active_counter      = array();

		foreach ($plugins as $key => $plugin) {
			$network_plugins = !empty($plugin['Network']) ? $plugin['Network'] : 'n/a';
			$PluginURI       = !empty($plugin['PluginURI']) ? $plugin['PluginURI'] : 'n/a';
			$new_plugin      = $plugin['Name'] . '- v' . $plugin['Version'] . ', URL: ' . $PluginURI . ', Network: ' . $network_plugins;

			if (is_plugin_inactive($key)) {
				$deactivated_plugins .= $new_plugin . PHP_EOL;
			} else {
				$activated_plugins .= $new_plugin . PHP_EOL;
			}

			if (in_array($key, $active_plugins_keys)) {
				// Remove active plugins from list so we can show active and inactive separately .
				unset($plugins[$key]);
				$inactive_counter[$key] = $key;
			} else {
				$active_counter[$key] = $key;
			}
		}

		return array(
			'active_plugins'            => $activated_plugins,
			'active_plugins_count'      => count($active_counter),
			'deactivated_plugins'       => $deactivated_plugins,
			'deactivated_plugins_count' => count($inactive_counter),
		);
	}

	/**
	 * Get Server Info
	 *
	 * @author Jewel Theme <support@jeweltheme.com>
	 */
	public function get_server_info()
	{
		global $wpdb;

		$server_data = array();

		$server_software = (isset($_SERVER['SERVER_SOFTWARE']) && !empty($_SERVER['SERVER_SOFTWARE'])) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE'])) : '';
		if ($server_software) {
			$server_data['software'] = $server_software;
		}

		if (function_exists('phpversion')) {
			$server_data['php_version'] = phpversion();
		}

		$server_data['mysql_version'] = $wpdb->db_version();

		$server_data['php_max_upload_size']  = size_format(wp_max_upload_size());
		$server_data['php_default_timezone'] = date_default_timezone_get();
		$server_data['php_soap']             = class_exists('SoapClient') ? 'Yes' : 'No';
		$server_data['php_fsockopen']        = function_exists('fsockopen') ? 'Yes' : 'No';
		$server_data['php_curl']             = function_exists('curl_init') ? 'Yes' : 'No';

		return $server_data;
	}

	/**
	 * Get WP Info
	 *
	 * @author Jewel Theme <support@jeweltheme.com>
	 */
	public function get_wp_info()
	{
		$wp_data = array();

		$wp_data['memory_limit'] = WP_MEMORY_LIMIT;
		$wp_data['debug_mode']   = (defined('WP_DEBUG') && WP_DEBUG) ? 'Yes' : 'No';
		$wp_data['locale']       = get_locale();
		$wp_data['version']      = get_bloginfo('version');
		$wp_data['multisite']    = is_multisite() ? 'Yes' : 'No';
		$wp_data['theme_slug']   = get_stylesheet();

		$theme = wp_get_theme($wp_data['theme_slug']);

		$wp_data['theme_name']    = $theme->get('Name');
		$wp_data['theme_version'] = $theme->get('Version');
		$wp_data['theme_uri']     = $theme->get('ThemeURI');
		$wp_data['theme_author']  = $theme->get('Author');

		return $wp_data;
	}

	/**
	 * Get User Counts
	 *
	 * @author Jewel Theme <support@jeweltheme.com>
	 */
	public function get_user_counts()
	{
		$user_count          = array();
		$user_count_data     = count_users();
		$user_count['total'] = $user_count_data['total_users'];

		// Get user count based on user role .
		foreach ($user_count_data['avail_roles'] as $role => $count) {
			if (!$count) {
				continue;
			}

			$user_count[$role] = $count;
		}

		return $user_count;
	}

	/**
	 * Get Site Name
	 *
	 * @author Jewel Theme <support@jeweltheme.com>
	 */
	public function get_site_name()
	{
		$site_name = get_bloginfo('name');

		if (empty($site_name)) {
			$site_name = get_bloginfo('description');
			$site_name = wp_trim_words($site_name, 3, '');
		}

		if (empty($site_name)) {
			$site_name = esc_url(home_url());
		}

		return $site_name;
	}

	/**
	 * Check if Local Server
	 *
	 * @return boolean
	 * @author Jewel Theme <support@jeweltheme.com>
	 */
	public function is_local_server()
	{
		$host     = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : 'localhost';
		$ip       = isset($_SERVER['SERVER_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_ADDR'])) : '127.0.0.1';
		$is_local = false;

		if (
			in_array($ip, array('127.0.0.1', '::1'))
			|| !strpos($host, '.')
			|| in_array(strrchr($host, '.'), array('.test', '.testing', '.local', '.localhost', '.localdomain'))
		) {
			$is_local = true;
		}

		return apply_filters('jltma_is_local', $is_local);
	}

	/**
	 * Get Collection Data
	 *
	 * @return void
	 * @author Jewel Theme <support@jeweltheme.com>
	 */
	public function get_collect_data($user_id, $arr = [])
	{
		// Only the data the administrator actively submits through the form,
		// plus the site URL and plugin slug needed to route the request.
		// No site telemetry, plugin list, IP address or customer billing data is sent.
		$data = array(
			'site_url'               => \get_site_url(),
			'installed_product_slug' => JLTMA_SLUG,
		);

		$payload_data =  array_merge($arr, $data);
		$endpoint_url = \MasterAddons\Inc\Classes\Helper::crm_endpoint();
		if ('local' === wp_get_environment_type()) {
			$response     = wp_remote_post(
				$endpoint_url,
				array(
					'body'      => json_encode( $payload_data ),
					'timeout'   => 100,
					'sslverify' => false,
				)
			);
		} else {
			// 'production' === wp_get_environment_type() .
			$response     = wp_safe_remote_post(
				$endpoint_url,
				array(
					'body'      => json_encode( $payload_data ),
					'timeout'   => 100,
				)
			);
		}

		return $response;
	}
}
