PayloadBuilder.php
6 days ago
RegisterSite.php
6 days ago
Scheduler.php
6 days ago
Sender.php
6 days ago
PayloadBuilder.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Builds the full usage report payload (schema_version, site_token, snapshot, dynamic_metrics). |
| 5 | * |
| 6 | * @package SmashBalloon\Reviews\Common\UsageTracking\Core |
| 7 | * @since 1.0 |
| 8 | */ |
| 9 | |
| 10 | namespace SmashBalloon\Reviews\Common\UsageTracking\Core; |
| 11 | |
| 12 | use SmashBalloon\Reviews\Common\UsageTracking\Config; |
| 13 | use SmashBalloon\Reviews\Common\UsageTracking\ReporterInterface; |
| 14 | |
| 15 | if (! defined('ABSPATH')) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | class PayloadBuilder { |
| 20 | /** |
| 21 | * @var ReporterInterface |
| 22 | */ |
| 23 | private $reporter; |
| 24 | |
| 25 | /** |
| 26 | * @param ReporterInterface $reporter Plugin-specific reporter. |
| 27 | */ |
| 28 | public function __construct(ReporterInterface $reporter) |
| 29 | { |
| 30 | $this->reporter = $reporter; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Build the full payload for the usage-report endpoint. |
| 35 | * |
| 36 | * @param string $site_token Site token from RegisterSite. |
| 37 | * @param string $period_start Period start (e.g. Y-m-d). |
| 38 | * @param string $period_end Period end (e.g. Y-m-d). |
| 39 | * @return array |
| 40 | */ |
| 41 | public function build($site_token, $period_start, $period_end) |
| 42 | { |
| 43 | $payload_id = $this->generate_payload_id(); |
| 44 | $slug = $this->reporter->get_plugin_slug(); |
| 45 | $snapshot = $this->reporter->get_configuration_snapshot(); |
| 46 | |
| 47 | $license_tier = isset($snapshot['license_tier']) ? $snapshot['license_tier'] : 'free'; |
| 48 | $license_status = isset($snapshot['license_status']) ? $snapshot['license_status'] : null; |
| 49 | $license_expires = isset($snapshot['license_expires']) ? $snapshot['license_expires'] : null; |
| 50 | $license_item_id = isset($snapshot['license_item_id']) ? $snapshot['license_item_id'] : null; |
| 51 | $version = isset($snapshot['version']) ? $snapshot['version'] : (defined('SBRVER') ? SBRVER : ''); |
| 52 | unset($snapshot['license_tier'], $snapshot['license_status'], $snapshot['license_expires'], $snapshot['license_item_id'], $snapshot['version']); |
| 53 | |
| 54 | $type = \SmashBalloon\Reviews\Common\Util::sbr_is_pro() ? 'pro' : 'free'; |
| 55 | |
| 56 | $dynamic_metrics = $this->reporter->get_dynamic_metrics($period_start, $period_end); |
| 57 | $errors = isset($dynamic_metrics['errors']) ? $dynamic_metrics['errors'] : array(); |
| 58 | unset($dynamic_metrics['errors']); |
| 59 | |
| 60 | return array( |
| 61 | 'schema_version' => $this->reporter->get_schema_version(), |
| 62 | 'plugin' => $slug, |
| 63 | 'type' => $type, |
| 64 | 'site_token' => $site_token, |
| 65 | 'payload_id' => $payload_id, |
| 66 | 'period_start' => $period_start, |
| 67 | 'period_end' => $period_end, |
| 68 | 'plugin_info' => array( |
| 69 | 'slug' => $slug, |
| 70 | 'version' => $version, |
| 71 | 'license_tier' => $license_tier, |
| 72 | 'license_status' => $license_status, |
| 73 | 'license_expires' => $license_expires, |
| 74 | 'license_item_id' => $license_item_id, |
| 75 | ), |
| 76 | 'configuration_snapshot' => $snapshot, |
| 77 | 'errors' => $errors, |
| 78 | 'dynamic_metrics' => $dynamic_metrics, |
| 79 | 'active_plugins' => $this->get_active_plugins(), |
| 80 | 'active_theme' => $this->get_active_theme(), |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Generate a unique payload ID (UUID v4 style). |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | private function generate_payload_id() |
| 90 | { |
| 91 | if (function_exists('wp_generate_uuid4')) { |
| 92 | return wp_generate_uuid4(); |
| 93 | } |
| 94 | return sprintf( |
| 95 | '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
| 96 | wp_rand(0, 0xffff), |
| 97 | wp_rand(0, 0xffff), |
| 98 | wp_rand(0, 0xffff), |
| 99 | wp_rand(0, 0x0fff) | 0x4000, |
| 100 | wp_rand(0, 0x3fff) | 0x8000, |
| 101 | wp_rand(0, 0xffff), |
| 102 | wp_rand(0, 0xffff), |
| 103 | wp_rand(0, 0xffff) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | private function get_active_plugins(): array |
| 108 | { |
| 109 | if (! function_exists('get_plugins')) { |
| 110 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 111 | } |
| 112 | |
| 113 | $all_plugins = get_plugins(); |
| 114 | $active_basenames = array_merge( |
| 115 | (array) get_option('active_plugins', array()), |
| 116 | array_keys((array) get_site_option('active_sitewide_plugins', array())) |
| 117 | ); |
| 118 | |
| 119 | $result = array(); |
| 120 | foreach (array_unique($active_basenames) as $basename) { |
| 121 | if (! isset($all_plugins[ $basename ])) { |
| 122 | continue; |
| 123 | } |
| 124 | $data = $all_plugins[ $basename ]; |
| 125 | $result[] = array( |
| 126 | 'slug' => $basename, |
| 127 | 'name' => isset($data['Name']) ? sanitize_text_field((string) $data['Name']) : null, |
| 128 | 'version' => isset($data['Version']) ? sanitize_text_field((string) $data['Version']) : null, |
| 129 | 'author' => isset($data['Author']) ? wp_strip_all_tags((string) $data['Author']) : null, |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | return $result; |
| 134 | } |
| 135 | |
| 136 | private function get_active_theme(): ?array |
| 137 | { |
| 138 | $theme = wp_get_theme(); |
| 139 | if (! $theme->exists()) { |
| 140 | return null; |
| 141 | } |
| 142 | return array( |
| 143 | 'name' => sanitize_text_field((string) $theme->get('Name')), |
| 144 | 'version' => sanitize_text_field((string) $theme->get('Version')), |
| 145 | ); |
| 146 | } |
| 147 | } |
| 148 |