• Resolved numero5

    (@numero5)


    Hello,

    I am writing to report a potential bug I found and fixed in the validateLicense method within your plugin’s includes/Api/V2/Licenses.php file. 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 the public function validateLicense(WP_REST_Request $request) method located in includes/Api/V2/Licenses.php, the $activation variable (which receives results from ActivationsResourceRepository::instance()->findBy() or findAllBy()) was not consistently ensured to be an array (Iterable). This could lead to a PHP error in the subsequent foreach( $activation as $act) loop.

    Specifically, if $activationToken was present and ActivationsResourceRepository::instance()->findBy() returned a single ActivationResourceModel object (or null), $activation would not be an array, causing the foreach loop to fail.

    [Proposed Fix]
    I have modified the validateLicense method to ensure that the $activation variable is initialized and consistently treated as an array. This ensures that the foreach loop 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]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support muddasirhayat

    (@muddasirhayat)

    Hi @numero5.

    Thank you for your detailed report regarding the potential issue in the validateLicense() method of the plugin, particularly concerning the handling of the $activation variable.

    Your suggestion to ensure the variable is consistently treated as an array is greatly appreciated. This approach enhances the method’s reliability and avoids potential errors in scenarios where a single object or null is returned.

    We have reviewed your input and have escalated this to our development team for further evaluation and implementation in a future update.

    We are grateful for your contribution and continued efforts in helping us improve the plugin.

    Thanks & Regards,
    WP Experts Support Team

    Thread Starter numero5

    (@numero5)

    Hi WP Experts Support Team,

    Thank you very much for your prompt and positive response regarding the $activation variable handling issue in the validateLicense() method.

    I’m truly glad to hear that my suggestion was appreciated and that this matter has been escalated to your development team for a future update. It’s rewarding to know my findings can contribute to improving the plugin’s reliability.

    I look forward to seeing the fix in an upcoming release.

    Thank you again for your excellent support.

    Best regards,
    numero5

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘PHP error in Licenses.php::validateLicense – $activation variable’ is closed to new replies.