PHP error in Licenses.php::validateLicense – $activation variable
-
Hello,
I am writing to report a potential bug I found and fixed in the
validateLicensemethod within your plugin’sincludes/Api/V2/Licenses.phpfile. This issue, observed in my local development environment using Local by Flywheel, could lead to PHP errors under certain conditions. The problem is specifically located in the file accessible at:https://plugins.trac.ww.wp.xz.cn/browser/license-manager-for-woocommerce/trunk/includes/Api/V2/Licenses.php[Environment Information]
- License Manager for WooCommerce Version: 3.0.12
- WordPress Version: 6.8.2
- PHP Version: 8.4.4
[Problem Description]
In thepublic function validateLicense(WP_REST_Request $request)method located inincludes/Api/V2/Licenses.php, the$activationvariable (which receives results fromActivationsResourceRepository::instance()->findBy()orfindAllBy()) was not consistently ensured to be an array (Iterable). This could lead to a PHP error in the subsequentforeach( $activation as $act)loop.Specifically, if
$activationTokenwas present andActivationsResourceRepository::instance()->findBy()returned a singleActivationResourceModelobject (ornull),$activationwould not be an array, causing theforeachloop to fail.[Proposed Fix]
I have modified thevalidateLicensemethod to ensure that the$activationvariable is initialized and consistently treated as an array. This ensures that theforeachloop executes safely, leading to a stable license validation response.Relevant Code Before and After Fix:
// Relevant code BEFORE the fix // ... } else { $activation = ActivationsResourceRepository::instance()->findAllBy( array( 'license_id' => $license->getId() ) ); } } catch (Exception $e) { // ... } // ... foreach( $activation as $act){ // Potential error here if $activation is not an array $activation_data[] = $act->toArray(); } // ...`php // Relevant code AFTER the fix // ... } else { $activation = ActivationsResourceRepository::instance()->findAllBy( array( 'license_id' => $license->getId() ) ); } // ★Added: Ensure $activation is an array if $activation_single was found if ($activation_single) { $activation[] = $activation_single; } } catch (Exception $e) { // ... } // ... $activation_data = []; // ★Added: Initialize $activation_data to ensure it's an array foreach( $activation as $act){ // Loop executes safely $activation_data[] = $act->toArray(); } // ...</code></pre><p>(Note: The code above is a simplified representation to highlight the issue and fix. Please refer to the actual file for full context.)</p>
<p>This fix has resolved the issue in my environment, and the license validation process now operates stably.<br>I hope this report is helpful for your consideration.</p>
<p>Thank you.</p>
The page I need help with: [log in to see the link]
The topic ‘PHP error in Licenses.php::validateLicense – $activation variable’ is closed to new replies.