Skip to content

Commit

Permalink
Merge pull request #32 from newfold-labs/solutions
Browse files Browse the repository at this point in the history
Solutions
  • Loading branch information
arunshenoy99 authored Oct 23, 2024
2 parents ac8d961 + e92e6bb commit b5ef14e
Show file tree
Hide file tree
Showing 33 changed files with 1,026 additions and 232 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ on:
push:
paths:
- '**.php'
- '!build/**/*.php'
pull_request:
types: [opened, edited, reopened, ready_for_review]
paths:
- '**.php'
- '!build/**/*.php'
workflow_dispatch:

concurrency:
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20
1 change: 0 additions & 1 deletion build/1.2.0-beta/dataAttrListener.asset.php

This file was deleted.

1 change: 0 additions & 1 deletion build/1.2.0-beta/dataAttrListener.js

This file was deleted.

1 change: 1 addition & 0 deletions build/1.2.0/dataAttrListener.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('wp-dom-ready'), 'version' => 'b208b9a3938b304abbde');
1 change: 1 addition & 0 deletions build/1.2.0/dataAttrListener.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build/1.2.0/installer.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '0374e667fa1f111980fb');
1 change: 1 addition & 0 deletions build/1.2.0/installer.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions build/1.2.0/installer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
},
"require": {
"newfold-labs/wp-module-pls": "^0.1.0"
"newfold-labs/wp-module-pls": "^0.2.0"
},
"require-dev": {
"wp-cli/wp-cli": "^2.11",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion includes/Data/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Constants {
*/
public function __construct( $container ) {
if ( ! defined( 'NFD_INSTALLER_VERSION' ) ) {
define( 'NFD_INSTALLER_VERSION', '1.2.0-beta' );
define( 'NFD_INSTALLER_VERSION', '1.2.0-beta.7' );
}
if ( ! defined( 'NFD_INSTALLER_BUILD_DIR' ) && defined( 'NFD_INSTALLER_VERSION' ) ) {
define( 'NFD_INSTALLER_BUILD_DIR', dirname( __DIR__, 2 ) . '/build/' . NFD_INSTALLER_VERSION );
Expand Down
1 change: 1 addition & 0 deletions includes/Data/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ final class Plugins {
protected static $domains = array(
'downloads.wordpress.org' => true,
'nonapproveddomain.com' => null,
'hiive.cloud' => true,
);
/**
* Holds the possible status codes for a plugin.
Expand Down
17 changes: 11 additions & 6 deletions includes/RestApi/PluginsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ public function get_install_plugin_args() {
'type' => 'boolean',
'default' => false,
),
'provider' => array(
'type' => 'string',
'default' => '',
),
);
}

/**
* Get args for the uninstall route.
*
* @return array
*/
/**
* Get args for the uninstall route.
*
* @return array
*/
public function get_uninstall_plugin_args() {
return array(
'plugin' => array(
Expand Down Expand Up @@ -183,10 +187,11 @@ public function install( \WP_REST_Request $request ) {
$queue = $request->get_param( 'queue' );
$priority = $request->get_param( 'priority' );
$premium = $request->get_param( 'premium' );
$provider = $request->get_param( 'provider' );

// Checks if the plugin is premium and uses the corresponding function for it.
if ( true === $premium ) {
return PluginInstaller::install_premium_plugin( $plugin, $activate );
return PluginInstaller::install_premium_plugin( $plugin, $provider, $activate );
}

// Checks if a plugin with the given slug and activation criteria already exists.
Expand Down
69 changes: 56 additions & 13 deletions includes/Services/PluginInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,42 +156,85 @@ public static function install_from_wordpress( $plugin, $activate ) {
* Provisions a license and installs or activates a premium plugin.
*
* @param string $plugin The slug of the premium plugin.
* @param string $provider The provider name for the premium plugin.
* @param boolean $activate Whether to activate the plugin after installation.
*
* @return \WP_Error|\WP_REST_Response
*/
public static function install_premium_plugin( $plugin, $activate ) {
$status_codes = Plugins::get_status_codes();
public static function install_premium_plugin( $plugin, $provider, $activate ) {
// Ensure plugin and provider are not empty
if ( empty( $plugin ) || empty( $provider ) ) {
return new \WP_Error(
'nfd_installer_error',
__( 'Plugin slug and provider name cannot be empty.', 'wp-module-installer' )
);
}

$pls_utility = new PLSUtility();

// Provision a license for the premium plugin, this returns basename and download URL
$license_response = $pls_utility->provision_license( $plugin, $provider );
if ( is_wp_error( $license_response ) ) {
return $license_response;
}

$premium_status = self::get_plugin_status( $plugin );
// Get the plugin basename from the license response
$plugin_basename = ! empty( $license_response['basename'] ) ? $license_response['basename'] : false;

// Check if the plugin is already installed
if ( $plugin_basename && self::is_plugin_installed( $plugin_basename ) ) {
// Check if the plugin is active
if ( is_plugin_active( $plugin_basename ) ) {
// If plugin is already installed and active, return success
return new \WP_REST_Response(
array(
'message' => __( 'Plugin is already installed and active: ', 'wp-module-installer' ) . $plugin,
),
200
);
}

// Check if the premium plugin is already installed or active
if ( $status_codes['active'] === $premium_status || $status_codes['installed'] === $premium_status ) {
// Activate the plugin if it's installed but not active
$activate_plugin_response = activate_plugin( $plugin_basename );
if ( is_wp_error( $activate_plugin_response ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Failed to activate the plugin: ', 'wp-module-installer' ) . $plugin );
}

// Activate the license
$activation_response = $pls_utility->activate_license( $plugin );
if ( is_wp_error( $activation_response ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Failed to activate the license for the premium plugin: ', 'wp-module-installer' ) . $plugin );
}

// Return success response
return new \WP_REST_Response(
array(
'message' => __( 'Premium plugin already installed or active: ', 'wp-module-installer' ) . $plugin,
'message' => __( 'Successfully provisioned and installed: ', 'wp-module-installer' ) . $plugin,
),
200
);
}

// Provision a license for the premium plugin
$license_response = PLSUtility::provision_license( $plugin );
if ( is_wp_error( $license_response ) ) {
return $license_response;
}

// Check if the download URL is present in the license response
if ( empty( $license_response['downloadUrl'] ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Download URL is missing for premium plugin: ', 'wp-module-installer' ) . $plugin );
}

// Attempt to install and/or activate the premium plugin using the provided download URL
// Plugin is not installed, proceed with installation
$install_status = self::install_from_zip( $license_response['downloadUrl'], $activate );
if ( is_wp_error( $install_status ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Failed to install or activate the premium plugin: ', 'wp-module-installer' ) . $plugin );
}

// If activation is requested, activate the license
if ( $activate ) {
$activation_response = $pls_utility->activate_license( $plugin );
if ( is_wp_error( $activation_response ) ) {
return new \WP_Error( 'nfd_installer_error', __( 'Failed to activate the license for the premium plugin: ', 'wp-module-installer' ) . $plugin );
}
}

// Return success response
return new \WP_REST_Response(
array(
'message' => __( 'Successfully provisioned and installed: ', 'wp-module-installer' ) . $plugin,
Expand Down
57 changes: 22 additions & 35 deletions includes/Services/PluginUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class PluginUpgrader {
public static function upgrade_extended_yith_plugins() {
// Define the list of extended YITH plugins to upgrade.
$yith_plugins_to_upgrade = array(
'yith-woocommerce-ajax-search',
'nfd_slug_yith_woocommerce_ajax_product_filter',
'nfd_slug_yith_woocommerce_wishlist',
'nfd_slug_yith_woocommerce_booking',
'nfd_slug_yith_woocommerce_gift_cards',
'nfd_slug_yith_woocommerce_customize_myaccount_page',
'yith-woocommerce-ajax-search',
);

// Array to store the status of each plugin's upgrade process
Expand All @@ -47,14 +47,13 @@ public static function upgrade_extended_yith_plugins() {
*/
public static function upgrade_extended_yith_plugin( $extended_slug ) {
// Define the list of extended YITH plugins and their corresponding premium versions.
// TODO: Replace the dummy entitlement slug 'nfd_slug_yith_paypal_payments_for_woocommerce' with actual entitlement slugs.
$yith_plugins_to_upgrade = array(
'yith-woocommerce-ajax-search' => 'nfd_slug_yith_paypal_payments_for_woocommerce',
'nfd_slug_yith_woocommerce_ajax_product_filter' => 'yith-woocommerce-ajax-product-filter-premium',
'nfd_slug_yith_woocommerce_wishlist' => 'yith-woocommerce-wishlist-premium',
'nfd_slug_yith_woocommerce_booking' => 'yith-woocommerce-booking-premium',
'nfd_slug_yith_woocommerce_gift_cards' => 'nfd_slug_yith_paypal_payments_for_woocommerce',
'nfd_slug_yith_woocommerce_ajax_product_filter' => 'yith-woocommerce-ajax-navigation',
'nfd_slug_yith_woocommerce_wishlist' => 'yith-woocommerce-wishlist',
'nfd_slug_yith_woocommerce_booking' => 'yith-woocommerce-booking',
'nfd_slug_yith_woocommerce_gift_cards' => 'yith-woocommerce-gift-cards',
'nfd_slug_yith_woocommerce_customize_myaccount_page' => 'yith-woocommerce-customize-myaccount-page',
'yith-woocommerce-ajax-search' => 'yith-woocommerce-ajax-search',
);

// Initialize status array for the plugin upgrade process
Expand Down Expand Up @@ -89,43 +88,31 @@ public static function upgrade_extended_yith_plugin( $extended_slug ) {
return $upgrade_status;
}

// Get the status of the premium version of the plugin
$premium_status = PluginInstaller::get_plugin_status( $premium_slug );

// Skip if the premium plugin is already active or installed
if ( $status_codes['active'] === $premium_status || $status_codes['installed'] === $premium_status ) {
$upgrade_status['message'] = __( 'Premium plugin already installed or active: ', 'wp-module-installer' ) . $premium_slug;
return $upgrade_status;
}

// Provision a license for the premium version of the plugin
$license_response = PLSUtility::provision_license( $premium_slug );
if ( is_wp_error( $license_response ) ) {
$upgrade_status['message'] = __( 'Failed to provision license for: ', 'wp-module-installer' ) . $premium_slug;
return $upgrade_status;
}

// Check if the download URL is present in the license response
if ( empty( $license_response['downloadUrl'] ) ) {
$upgrade_status['message'] = __( 'Download URL is missing for premium plugin: ', 'wp-module-installer' ) . $premium_slug;
return $upgrade_status;
}

// Check if the premium plugin should be activated after installation
$should_activate = ( $status_codes['active'] === $extended_status );

// Deactivate the extended version of the plugin if the premium plugin needs to be activated
if ( $should_activate ) {
PluginInstaller::deactivate( $extended_slug );
$deactivation_response = PluginInstaller::deactivate( $extended_slug );
if ( is_wp_error( $deactivation_response ) ) {
$upgrade_status['message'] = __( 'Failed to deactivate the extended plugin: ', 'wp-module-installer' ) . $extended_slug;
return $upgrade_status;
}
}

// Attempt to install the premium plugin using the provided download URL, and activate it if needed
$premium_install_status = PluginInstaller::install_from_zip( $license_response['downloadUrl'], $should_activate );
if ( is_wp_error( $premium_install_status ) ) {
$upgrade_status['message'] = __( 'Failed to install the premium plugin: ', 'wp-module-installer' ) . $premium_slug;
// Use the install_premium_plugin function to install and activate the premium plugin
$install_status = PluginInstaller::install_premium_plugin( $premium_slug, 'yith', $should_activate );
if ( is_wp_error( $install_status ) ) {
$upgrade_status['message'] = $install_status->get_error_message();

// Reactivate the extended plugin if premium installation failed and it was deactivated
if ( $should_activate ) {
PluginInstaller::activate( $extended_slug );
$reactivation_response = PluginInstaller::activate( $extended_slug );
if ( is_wp_error( $reactivation_response ) ) {
$upgrade_status['message'] .= __( ' Also Failed to reactivate the extended plugin: ', 'wp-module-installer' ) . $extended_slug;
}
}

return $upgrade_status;
}

Expand Down
51 changes: 0 additions & 51 deletions includes/WPAdmin/Listeners/DataAttrListener.php

This file was deleted.

Loading

0 comments on commit b5ef14e

Please sign in to comment.