Skip to content

Commit

Permalink
refactor: Recommended plugins installation based on user input
Browse files Browse the repository at this point in the history
  • Loading branch information
devAsadNur committed Jan 17, 2025
1 parent 3ae552a commit 8605cc3
Showing 1 changed file with 38 additions and 85 deletions.
123 changes: 38 additions & 85 deletions includes/Admin/SetupWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,53 +733,25 @@ public function dokan_setup_recommended() {
* @return void
*/
public function dokan_setup_recommended_save() {
check_admin_referer( 'dokan-setup' );
foreach ( $this->recommended_plugins->get() as $plugin ) {
if ( ! $this->should_install_plugin( $plugin ) ) {
continue;
}

$setup_store_growth = isset( $_POST['setup_store_growth'] ) && 'yes' === sanitize_text_field( wp_unslash( $_POST['setup_store_growth'] ) );
$setup_wc_conversion_tracking = isset( $_POST['setup_wc_conversion_tracking'] ) && 'yes' === sanitize_text_field( wp_unslash( $_POST['setup_wc_conversion_tracking'] ) );
$setup_wemail = isset( $_POST['setup_wemail'] ) && 'yes' === sanitize_text_field( wp_unslash( $_POST['setup_wemail'] ) );
$setup_texty = isset( $_POST['setup_texty'] ) && 'yes' === sanitize_text_field( wp_unslash( $_POST['setup_texty'] ) );
$plugin_details = $plugin['plugins'][0] ?? null;

if ( $setup_store_growth && ! $this->is_store_growth_active() ) {
$this->install_plugin(
'storegrowth-sales-booster',
[
'name' => __( 'StoreGrowth', 'dokan-lite' ),
'repo-slug' => 'storegrowth-sales-booster',
'file' => 'storegrowth-sales-booster.php',
]
);
}

if ( $setup_wc_conversion_tracking && ! $this->is_wc_conversion_tracking_active() ) {
$this->install_plugin(
'woocommerce-conversion-tracking',
[
'name' => __( 'WooCommerce Conversion Tracking', 'dokan-lite' ),
'repo-slug' => 'woocommerce-conversion-tracking',
'file' => 'conversion-tracking.php',
]
);
}
if ( ! $plugin_details ) {
continue;
}

if ( $setup_wemail && ! $this->is_wemail_active() ) {
$this->install_plugin(
'wemail',
[
'name' => __( 'weMail', 'dokan-lite' ),
'repo-slug' => 'wemail',
'file' => 'wemail.php',
]
);
}
$plugin_details_arr = explode( '/', $plugin_details['basename'] ?? '' );

if ( $setup_texty && ! $this->is_texty_active() ) {
$this->install_plugin(
'texty',
$plugin_details['slug'],
[
'name' => __( 'Texty', 'dokan-lite' ),
'repo-slug' => 'texty',
'file' => 'texty.php',
'name' => $plugin_details['name'] ?? '',
'repo-slug' => $plugin_details_arr[0] ?? '',
'file' => $plugin_details_arr[1] ?? '',
]
);
}
Expand All @@ -800,6 +772,27 @@ public function dokan_setup_recommended_save() {
exit;
}

/**
* Determines if a plugin should be installed based on POST data.
*
* @since DOKAN_SINCE
*
* @param array $plugin Plugin configuration array
*
* @return bool
*/
private function should_install_plugin( array $plugin ): bool {
check_admin_referer( 'dokan-setup' );

$setup_key = 'setup_' . $plugin['type'];

if ( ! isset( $_POST[ $setup_key ] ) ) {
return false;
}

return 'yes' === sanitize_text_field( wp_unslash( $_POST[ $setup_key ] ) );
}

/**
* Save withdraw options.
*/
Expand Down Expand Up @@ -871,50 +864,6 @@ protected function should_show_recommended_step() {
return true;
}

/**
* Check if StoreGrowth is active or not.
*
* @since DOKAN_SINCE
*
* @return bool
*/
protected function is_store_growth_active() {
return is_plugin_active( 'storegrowth-sales-booster/storegrowth-sales-booster.php' );
}

/**
* Check if WC Conversion Tracking is active or not
*
* @since 2.8.7
*
* @return bool
*/
protected function is_wc_conversion_tracking_active() {
return is_plugin_active( 'woocommerce-conversion-tracking/conversion-tracking.php' );
}

/**
* Check if weMail is active or not
*
* @since 3.0.0
*
* @return bool
*/
protected function is_wemail_active() {
return is_plugin_active( 'wemail/wemail.php' );
}

/**
* Check if texty is active or not
*
* @since 3.2.11
*
* @return bool
*/
protected function is_texty_active() {
return is_plugin_active( 'texty/texty.php' );
}

/**
* Should we show the WooCommerce Conversion Tracking install option?
*
Expand Down Expand Up @@ -988,6 +937,10 @@ public function plugin_install_info() {
* @param array $plugin_info Plugin info array containing name and repo-slug, and optionally file if different from [repo-slug].php.
*/
protected function install_plugin( $plugin_id, $plugin_info ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}

// Make sure we don't trigger multiple simultaneous installs.
if ( get_option( 'woocommerce_setup_background_installing_' . $plugin_id ) ) {
return;
Expand Down

0 comments on commit 8605cc3

Please sign in to comment.