Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: integrations not loading properly #1486

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions includes/Integrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace WeDevs\Wpuf;

/**
* The installer class
*
* @since 2.6.0
*/
class Integrations {
/**
* Holds various class instances
*
* @since 4.0.9
*
* @var array
*/
public $container = [];

public function __construct() {
if ( class_exists( 'WeDevs_Dokan' ) ) {
$this->container['dokan'] = new Integrations\WPUF_Dokan_Integration();
}

if ( class_exists( 'WC_Vendors' ) ) {
$this->container['wc_vendors'] = new Integrations\WPUF_WC_Vendors_Integration();
}

if ( class_exists( 'WCMp' ) ) {
$this->container['wcmp'] = new Integrations\WPUF_WCMp_Integration();
}

if ( class_exists( 'ACF' ) ) {
$this->container['acf'] = new Integrations\WPUF_ACF_Compatibility();
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider refactoring for improved maintainability and flexibility.

While the constructor logic is functional, there are opportunities for improvement:

  1. Consider using an array or configuration file to define the mappings between external classes and their corresponding integration classes. This would make it easier to add or modify integrations in the future.

  2. The integration class names are currently hardcoded. Consider using a naming convention or configuration that allows for more dynamic loading of integration classes.

  3. You might want to add error handling or logging for cases where an integration class fails to initialize.

Here's a potential refactor:

private $integrations = [
    'WeDevs_Dokan' => 'WPUF_Dokan_Integration',
    'WC_Vendors' => 'WPUF_WC_Vendors_Integration',
    'WCMp' => 'WPUF_WCMp_Integration',
    'ACF' => 'WPUF_ACF_Compatibility',
];

public function __construct() {
    foreach ($this->integrations as $externalClass => $integrationClass) {
        if (class_exists($externalClass)) {
            $fullClassName = __NAMESPACE__ . '\\Integrations\\' . $integrationClass;
            try {
                $this->container[strtolower($externalClass)] = new $fullClassName();
            } catch (\Exception $e) {
                // Log the error or handle it appropriately
            }
        }
    }
}

This approach would make it easier to add new integrations in the future and provides better error handling.

}
1 change: 1 addition & 0 deletions wpuf.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
$this->container['bank'] = new WeDevs\Wpuf\Lib\Gateway\Bank();
$this->container['paypal'] = new WeDevs\Wpuf\Lib\Gateway\Paypal();
$this->container['api'] = new WeDevs\Wpuf\API();
$this->container['integrations'] = new WeDevs\Wpuf\Integrations();
sapayth marked this conversation as resolved.
Show resolved Hide resolved

if ( is_admin() ) {
$this->container['admin'] = new WeDevs\Wpuf\Admin();
Expand Down Expand Up @@ -223,7 +224,7 @@
public function process_wpuf_pro_version() {
// check whether the version of wpuf pro is prior to the code restructure
if ( defined( 'WPUF_PRO_VERSION' ) && version_compare( WPUF_PRO_VERSION, '4', '<' ) ) {
// deactivate_plugins( WPUF_PRO_FILE );

Check warning on line 227 in wpuf.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

This comment is 43% valid code; is this commented out code?

add_action( 'admin_notices', [ $this, 'wpuf_upgrade_notice' ] );
}
Expand All @@ -241,7 +242,7 @@
<p>
<?php
/* translators: 1: opening anchor tag, 2: closing anchor tag. */
echo sprintf( __( 'We\'ve pushed a major update on both <b>WP User Frontend Free</b> and <b>WP User Frontend Pro</b> that requires you to use latest version of both. Please update the WPUF pro to the latest version. <br><strong>Please make sure to take a complete backup of your site before updating.</strong>', 'wp-user-frontend' ), '<a target="_blank" href="https://wordpress.org/plugins/wp-user-frontend/">', '</a>' );

Check failure on line 245 in wpuf.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Unnecessary "echo sprintf(...)" found. Use "printf(...)" instead.
?>
</p>
</div>
Expand Down Expand Up @@ -296,7 +297,7 @@
* @param string $msg
*/
public static function log( $type = '', $msg = '' ) {
$msg = sprintf( "[%s][%s] %s\n", date( 'd.m.Y h:i:s' ), $type, $msg );

Check failure on line 300 in wpuf.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

date() is affected by runtime timezone changes which can cause date/time to be incorrectly displayed. Use gmdate() instead.
error_log( $msg, 3, __DIR__ . '/log.txt' );
}

Expand Down Expand Up @@ -390,7 +391,7 @@
*
* @return WP_User_Frontend
*/
function wpuf() {

Check failure on line 394 in wpuf.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

A file should either contain function declarations or OO structure declarations, but not both. Found 1 function declaration(s) and 1 OO structure declaration(s). The first function declaration was found on line 394; the first OO declaration was found on line 38
return WP_User_Frontend::instance();
}

Expand Down
Loading