pronamic / wp-pay-core

Core components for the WordPress payment processing library. This library is used in the WordPress plugin Pronamic Pay: https://www.pronamicpay.com/, but also allows other plugin developers to set up a payment plugin.
https://www.wp-pay.org/
GNU General Public License v3.0
27 stars 3 forks source link

Action Scheduler requirement #154

Closed remcotolsma closed 10 months ago

remcotolsma commented 10 months ago

The "Action Scheduler" library is required for correct operation of our payment library:

https://github.com/pronamic/wp-pay-core/blob/529e6f2568a3e2fd380b23a08e32cf4b22af42a0/composer.json#L61

https://github.com/pronamic/wp-pay-core/blob/529e6f2568a3e2fd380b23a08e32cf4b22af42a0/src/Plugin.php#L299-L308

In WordPress plugins such as "Pronamic Pay" or "Pronamic Pay with Mollie for Gravity Forms" it is not immediately known in which folder the "Action Scheduler" library is located. However, we need to know this location to load the "Action Scheduler" library. To do this, we pass the "Action Scheduler" location when creating a plugin instance.

\Pronamic\WordPress\Pay\Plugin::instance(
    [
        'file'                 => __FILE__,
        'options'              => [
            'about_page_file' => __DIR__ . '/admin/page-about.php',
        ],
        'action_scheduler'     => __DIR__ . '/packages/woocommerce/action-scheduler/action-scheduler.php',
        'pronamic_service_url' => 'https://api.wp-pay.org/wp-json/pronamic-pay/v1/payments',
    ]
);

https://github.com/pronamic/wp-pronamic-pay/blob/e17985afe9c984ce9748ac956b98f4a12b884797/pronamic-ideal.php#L48C1-L57C3

\Pronamic\WordPress\Pay\Plugin::instance(
    [
        'file'             => __FILE__,
        'action_scheduler' => __DIR__ . '/packages/woocommerce/action-scheduler/action-scheduler.php',
    ]
);

https://github.com/pronamic/wp-pronamic-pay-with-mollie-for-gravity-forms/blob/0b20c960ef8174c12da414b4f1b1aa8b0f4046b8/pronamic-pay-with-mollie-for-gravity-forms.php#L44-L49

This works fine, but we have to define the location of the "Action Scheduler" library in more and more places. For example, while writing PHPUnit tests I also encountered the following error:

require_once(/wp-content/plugins/action-scheduler/action-scheduler.php): Failed to open stream: No such file or directory

I can create the plugin instance in the PHPUnit bootstrap routine and pass the path of the "Action Scheduler", but I also recently came across the following:

\Composer\InstalledVersions::getInstallPath('vendor/package');

https://getcomposer.org/doc/07-runtime.md#knowing-the-path-in-which-a-package-is-installed

Shall we change the following code?

https://github.com/pronamic/wp-pay-core/blob/529e6f2568a3e2fd380b23a08e32cf4b22af42a0/src/Plugin.php#L299-L308

To:

/**
 * Action scheduler.
 *
 * @link https://actionscheduler.org/
 */
$action_scheduler_path = \Composer\InstalledVersions::getInstallPath( 'woocommerce/action-scheduler' );

if ( null === $action_scheduler_path ) {
    throw new \Exception( 'The required Action Scheduler library could not be found.' );
}

require_once $action_scheduler_path . '/action-scheduler.php';

@rvdsteege What do you think? Can \Composer\InstalledVersions::getInstallPath('vendor/package'); be used safely?

remcotolsma commented 10 months ago

Just tested this, but it's not working with automattic/jetpack-autoloader ☹️, reported in:

For now we can't use \Composer\InstalledVersions::getInstallPath( 'vendor/package' ); in our plugins.

If Automattic implements a solution we can review this, for now I will close this issue.

https://github.com/pronamic/wp-pay-core/commit/4afa13bdca7805179c488c849a223d3ff6bab89f