class-groups.php
3 weeks ago
class-licenses.php
3 weeks ago
class-onboarding.php
1 year ago
class-page-quick-edit.php
3 weeks ago
class-placements.php
3 weeks ago
class-utilities.php
1 month ago
class-licenses.php
246 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Licenses Rest route and endpoints. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Rest; |
| 11 | |
| 12 | use AdvancedAds\Admin\Plugin_Auto_Update; |
| 13 | use AdvancedAds\Constants; |
| 14 | use AdvancedAds\Framework\Interfaces\Routes_Interface; |
| 15 | use AdvancedAds\License\License; |
| 16 | use AdvancedAds\License\License_Site_Activation; |
| 17 | use AdvancedAds\License\License_Utils; |
| 18 | use AdvancedAds\Utilities\Conditional; |
| 19 | use WP_Error; |
| 20 | use WP_REST_Request; |
| 21 | use WP_REST_Server; |
| 22 | |
| 23 | defined( 'ABSPATH' ) || exit; |
| 24 | |
| 25 | /** |
| 26 | * Rest Licenses. |
| 27 | */ |
| 28 | class Licenses implements Routes_Interface { |
| 29 | /** |
| 30 | * Registers routes with WordPress. |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | public function register_routes(): void { |
| 35 | register_rest_route( |
| 36 | Constants::REST_BASE, |
| 37 | '/licenses', |
| 38 | [ |
| 39 | [ |
| 40 | 'methods' => WP_REST_Server::READABLE, |
| 41 | 'callback' => [ $this, 'get_licenses' ], |
| 42 | 'permission_callback' => [ $this, 'can_manage' ], |
| 43 | ], |
| 44 | [ |
| 45 | 'methods' => WP_REST_Server::CREATABLE, |
| 46 | 'callback' => [ $this, 'set_licenses' ], |
| 47 | 'permission_callback' => [ $this, 'can_manage' ], |
| 48 | 'args' => [ |
| 49 | 'licenses' => [ |
| 50 | 'required' => true, |
| 51 | 'type' => 'array', |
| 52 | ], |
| 53 | 'activate' => [ |
| 54 | 'required' => false, |
| 55 | 'type' => 'boolean', |
| 56 | 'default' => false, |
| 57 | ], |
| 58 | 'activatingLicenseKey' => [ |
| 59 | 'required' => false, |
| 60 | 'type' => 'string', |
| 61 | 'default' => '', |
| 62 | ], |
| 63 | 'activatingAddonId' => [ |
| 64 | 'required' => false, |
| 65 | 'type' => 'string', |
| 66 | 'default' => '', |
| 67 | ], |
| 68 | 'installOnly' => [ |
| 69 | 'required' => false, |
| 70 | 'type' => 'boolean', |
| 71 | 'default' => false, |
| 72 | ], |
| 73 | 'deactivatingAddonId' => [ |
| 74 | 'required' => false, |
| 75 | 'type' => 'string', |
| 76 | 'default' => '', |
| 77 | ], |
| 78 | 'deactivatingLicenseKey' => [ |
| 79 | 'required' => false, |
| 80 | 'type' => 'string', |
| 81 | 'default' => '', |
| 82 | ], |
| 83 | ], |
| 84 | ], |
| 85 | ] |
| 86 | ); |
| 87 | |
| 88 | register_rest_route( |
| 89 | Constants::REST_BASE, |
| 90 | '/plugin-autoupdate', |
| 91 | [ |
| 92 | 'methods' => WP_REST_Server::CREATABLE, |
| 93 | 'callback' => [ $this, 'set_plugin_autoupdate' ], |
| 94 | 'permission_callback' => [ $this, 'can_manage' ], |
| 95 | 'args' => [ |
| 96 | 'addonId' => [ |
| 97 | 'required' => false, |
| 98 | 'type' => 'string', |
| 99 | 'default' => Plugin_Auto_Update::MAIN_ADDON_KEY, |
| 100 | ], |
| 101 | 'state' => [ |
| 102 | 'required' => true, |
| 103 | 'type' => 'string', |
| 104 | 'enum' => [ Plugin_Auto_Update::STATE_ON, Plugin_Auto_Update::STATE_OFF ], |
| 105 | ], |
| 106 | ], |
| 107 | ] |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Permission callback. |
| 113 | * |
| 114 | * @return bool |
| 115 | */ |
| 116 | public function can_manage(): bool { |
| 117 | return Conditional::user_can( 'advanced_ads_manage_options' ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get persisted licenses. |
| 122 | * |
| 123 | * @return array{licenses: array<int, array<string, mixed>>, appliedAddonKeyMap: array<string, string>, currentActiveLicenses: string, autoUpdateStates: array<string, string>, addonInstallStates: array<string, array{installed: bool, active: bool}>, lastSyncAt: mixed, expiryNoticeFlags: mixed} |
| 124 | */ |
| 125 | public function get_licenses(): array { |
| 126 | License::maybe_complete_legacy_license_migration(); |
| 127 | |
| 128 | $rich = License::get_licenses(); |
| 129 | $hostname = License::get_site_hostname(); |
| 130 | $before = wp_json_encode( $rich ); |
| 131 | |
| 132 | // Heal Sites count when advanced-ads-licenses marks a key active but sitesActivated is empty. |
| 133 | if ( '' !== $hostname ) { |
| 134 | foreach ( License_Site_Activation::get_active_license_keys() as $license_key ) { |
| 135 | $row = License_Utils::get_rich_license_row_by_key( $rich, $license_key ); |
| 136 | if ( ! is_array( $row ) ) { |
| 137 | continue; |
| 138 | } |
| 139 | if ( License::is_site_activated_on_license( $row, $hostname ) ) { |
| 140 | continue; |
| 141 | } |
| 142 | $rich = License::ensure_local_site_slot_on_license( $rich, $license_key ); |
| 143 | } |
| 144 | |
| 145 | if ( wp_json_encode( $rich ) !== $before ) { |
| 146 | update_option( License::OPTION_RICH, $rich, false ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Passive reconcile on read: mirror addon status only; never reshuffle activations. |
| 151 | $rich = License::reconcile_persisted_licenses( $rich, false, false ); |
| 152 | $rich = License::finalize_license_sync( $rich ); |
| 153 | |
| 154 | return $this->licenses_api_response( $rich ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Persist licenses. |
| 159 | * |
| 160 | * @param WP_REST_Request $request the request object. |
| 161 | * |
| 162 | * @return array{licenses: array<int, array<string, mixed>>, appliedAddonKeyMap: array<string, string>, currentActiveLicenses: string, autoUpdateStates: array<string, string>, addonInstallStates: array<string, array{installed: bool, active: bool}>, lastSyncAt: mixed, expiryNoticeFlags: mixed}|WP_Error |
| 163 | */ |
| 164 | public function set_licenses( WP_REST_Request $request ) { |
| 165 | $licenses = $request->get_param( 'licenses' ); |
| 166 | $licenses = is_array( $licenses ) ? $licenses : []; |
| 167 | $licenses = wp_unslash( $licenses ); |
| 168 | |
| 169 | $activate = (bool) $request->get_param( 'activate' ); |
| 170 | $activating_license_key = (string) $request->get_param( 'activatingLicenseKey' ); |
| 171 | $activating_license_key = sanitize_text_field( $activating_license_key ); |
| 172 | $activating_addon_id = (string) $request->get_param( 'activatingAddonId' ); |
| 173 | $activating_addon_id = sanitize_key( $activating_addon_id ); |
| 174 | $install_only = (bool) $request->get_param( 'installOnly' ); |
| 175 | $deactivating_addon_id = (string) $request->get_param( 'deactivatingAddonId' ); |
| 176 | $deactivating_addon_id = sanitize_key( $deactivating_addon_id ); |
| 177 | $deactivating_license_key = (string) $request->get_param( 'deactivatingLicenseKey' ); |
| 178 | $deactivating_license_key = sanitize_text_field( $deactivating_license_key ); |
| 179 | |
| 180 | $rich = License::save_licenses( |
| 181 | $licenses, |
| 182 | [ |
| 183 | 'activate_new' => $activate, |
| 184 | 'activating_license_key' => $activating_license_key, |
| 185 | 'activating_addon_id' => $activating_addon_id, |
| 186 | 'install_only' => $install_only, |
| 187 | 'deactivating_addon_id' => $deactivating_addon_id, |
| 188 | 'deactivating_license_key' => $deactivating_license_key, |
| 189 | ] |
| 190 | ); |
| 191 | |
| 192 | if ( is_wp_error( $rich ) ) { |
| 193 | return $rich; |
| 194 | } |
| 195 | |
| 196 | return $this->licenses_api_response( $rich ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Toggle per-plugin auto-update (advanced-ads-{addon}-autoupdate). |
| 201 | * |
| 202 | * @param WP_REST_Request $request Request. |
| 203 | * @return array{addonId: string, state: string, autoUpdateStates: array<string, string>}|WP_Error |
| 204 | */ |
| 205 | public function set_plugin_autoupdate( WP_REST_Request $request ) { |
| 206 | $addon_id = (string) $request->get_param( 'addonId' ); |
| 207 | $state = (string) $request->get_param( 'state' ); |
| 208 | |
| 209 | $normalized = Plugin_Auto_Update::MAIN_ADDON_KEY === $addon_id || '' === $addon_id |
| 210 | ? null |
| 211 | : sanitize_key( $addon_id ); |
| 212 | |
| 213 | $result = Plugin_Auto_Update::set_state( $normalized, $state ); |
| 214 | |
| 215 | if ( is_wp_error( $result ) ) { |
| 216 | return $result; |
| 217 | } |
| 218 | |
| 219 | $response_key = null === $normalized ? Plugin_Auto_Update::MAIN_ADDON_KEY : $normalized; |
| 220 | |
| 221 | return [ |
| 222 | 'addonId' => $response_key, |
| 223 | 'state' => Plugin_Auto_Update::get_state( $normalized ), |
| 224 | 'autoUpdateStates' => Plugin_Auto_Update::get_all_states(), |
| 225 | ]; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * REST payload: rich rows plus derived addon key map / site activation state. |
| 230 | * |
| 231 | * @param array<int, array<string, mixed>> $rich Rich license list. |
| 232 | * @return array{licenses: array<int, array<string, mixed>>, appliedAddonKeyMap: array<string, string>} |
| 233 | */ |
| 234 | private function licenses_api_response( array $rich ): array { |
| 235 | return [ |
| 236 | 'licenses' => $rich, |
| 237 | 'appliedAddonKeyMap' => License::get_addon_key_map(), |
| 238 | 'currentActiveLicenses' => implode( ',', License_Site_Activation::get_active_license_keys() ), |
| 239 | 'autoUpdateStates' => Plugin_Auto_Update::get_all_states(), |
| 240 | 'addonInstallStates' => License::get_addon_install_states(), |
| 241 | 'lastSyncAt' => License_Utils::get_last_sync(), |
| 242 | 'expiryNoticeFlags' => License_Utils::get_expiry_notice_flags(), |
| 243 | ]; |
| 244 | } |
| 245 | } |
| 246 |