From 43e87b2415f0f2613d93328656979cca71d3b92e Mon Sep 17 00:00:00 2001 From: Aziz <104441723+AzGasim@users.noreply.github.com> Date: Fri, 13 Sep 2024 15:31:10 +0200 Subject: [PATCH] Add UpdateWordPressPlugin command --- config/press.php | 8 +++- packages/press/config/press.php | 48 +++++++++++-------- .../src/Commands/UpdateWordPressPlugin.php | 32 +++++++++++++ .../plugins/moox-press/moox-press.php | 31 ++++++++---- packages/press/wordpress/wp-config.php | 3 ++ 5 files changed, 93 insertions(+), 29 deletions(-) diff --git a/config/press.php b/config/press.php index a43227eb9..9ed939502 100644 --- a/config/press.php +++ b/config/press.php @@ -1088,10 +1088,16 @@ */ 'redirect_index' => env('REDIRECT_INDEX', false), - 'redirect_to_wp' => env('REDIRECT_TO_WP', false), 'redirect_login' => env('REDIRECT_LOGIN', false), 'redirect_logout' => env('REDIRECT_LOGOUT', false), 'redirect_editor' => env('REDIRECT_EDITOR', false), + // Deprecated + 'redirect_to_wp' => env('REDIRECT_TO_WP', false), + // New + 'redirect_after_login' => env('REDIRECT_AFTER_LOGIN', ''), + // Default: '' means: go to Moox Admin + // Frontend: 'frontend' means: go to frontend (currently WordPress) + // Admin: 'wpadmin' means: go to wp-admin /* |-------------------------------------------------------------------------- diff --git a/packages/press/config/press.php b/packages/press/config/press.php index de21a0a8e..9ed939502 100644 --- a/packages/press/config/press.php +++ b/packages/press/config/press.php @@ -46,7 +46,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -93,7 +93,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -140,7 +140,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -187,7 +187,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -234,7 +234,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -281,7 +281,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -344,7 +344,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -391,7 +391,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -478,7 +478,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -525,7 +525,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -572,7 +572,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -619,7 +619,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -666,7 +666,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -713,7 +713,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -760,7 +760,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -807,7 +807,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -854,7 +854,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -901,7 +901,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -948,7 +948,7 @@ | Tabs |-------------------------------------------------------------------------- | - | Define the tabs for the Resource table. They are optional, but + | Define the tabs for the Expiry table. They are optional, but | pretty awesome to filter the table by certain values. | You may simply do a 'tabs' => [], to disable them. | @@ -1088,10 +1088,16 @@ */ 'redirect_index' => env('REDIRECT_INDEX', false), - 'redirect_to_wp' => env('REDIRECT_TO_WP', false), 'redirect_login' => env('REDIRECT_LOGIN', false), 'redirect_logout' => env('REDIRECT_LOGOUT', false), 'redirect_editor' => env('REDIRECT_EDITOR', false), + // Deprecated + 'redirect_to_wp' => env('REDIRECT_TO_WP', false), + // New + 'redirect_after_login' => env('REDIRECT_AFTER_LOGIN', ''), + // Default: '' means: go to Moox Admin + // Frontend: 'frontend' means: go to frontend (currently WordPress) + // Admin: 'wpadmin' means: go to wp-admin /* |-------------------------------------------------------------------------- @@ -1127,6 +1133,7 @@ 'Author' => serialize(['author' => true]), 'Contributor' => serialize(['contributor' => true]), 'Subscriber' => serialize(['subscriber' => true]), + 'No role for this site' => serialize(['' => true]), ], 'wp_user_levels' => [ @@ -1297,4 +1304,5 @@ ], ], ], + 'ip_whitelist' => config('user-session.whitelisted_ips'), ]; diff --git a/packages/press/src/Commands/UpdateWordPressPlugin.php b/packages/press/src/Commands/UpdateWordPressPlugin.php index f49cd8ee9..60203ca6d 100644 --- a/packages/press/src/Commands/UpdateWordPressPlugin.php +++ b/packages/press/src/Commands/UpdateWordPressPlugin.php @@ -35,6 +35,8 @@ public function handle() $this->welcome(); $this->checkDotenv(); $env = $this->getDotenv(); + $this->pressPluginInstall(); + $this->updateWpConfig(); $this->sayGoodbye(); } @@ -140,6 +142,36 @@ public function pressPluginInstall(): void File::copyDirectory($pluginSource, $pluginDestination); } + public function updateWpConfig(): void + { + info('Updating the wp-config.php file...'); + + $wpPath = env('WP_PATH', '/public/wp'); + $fullWpPath = base_path(trim($wpPath, '/')); + + $wpconfigPath = $fullWpPath.'/wp-config.php'; + + $configSource = __DIR__.'/../../wordpress/wp-config.php'; + $configDestination = $wpconfigPath; + + if (! File::exists($configSource)) { + alert('The source wp-config.php file does not exist.'); + exit(1); + } + + if (File::exists($configDestination)) { + info('Deleting the existing wp-config.php file...'); + File::delete($configDestination); + } + + if (File::copy($configSource, $configDestination)) { + info('wp-config.php file updated successfully.'); + } else { + alert('Error occurred while copying the wp-config.php file.'); + exit(1); + } + } + public function sayGoodbye(): void { note('Moox Press WordPress Plugin updated successfully. Enjoy!'); diff --git a/packages/press/wordpress/plugins/moox-press/moox-press.php b/packages/press/wordpress/plugins/moox-press/moox-press.php index 9581dee6d..c104344a3 100644 --- a/packages/press/wordpress/plugins/moox-press/moox-press.php +++ b/packages/press/wordpress/plugins/moox-press/moox-press.php @@ -34,6 +34,10 @@ $redirectLogout = REDIRECT_LOGOUT; } +if (defined('REDIRECT_AFTER_LOGIN')) { + $redirectAfterLogin = REDIRECT_AFTER_LOGIN; +} + function moox_lock_wp_frontend() { global $lockWp; @@ -48,14 +52,17 @@ function moox_lock_wp_frontend() function moox_auth_token() { + global $authWp; + global $adminSlug; + global $redirectAfterLogin; if ($authWp === 'true') { + if (isset($_GET['auth_token'])) { $token = $_GET['auth_token']; - $rememberMe = isset($_GET['remember_me']) && $_GET['remember_me'] === 'true'; // Check for remember me parameter - $parts = explode('.', $token); + if (count($parts) === 2) { $payload = $parts[0]; $signature = $parts[1]; @@ -65,20 +72,28 @@ function moox_auth_token() $user_id = base64_decode($payload); wp_clear_auth_cookie(); + wp_set_auth_cookie($user_id); - if ($rememberMe) { - wp_set_auth_cookie($user_id, true); - } else { - wp_set_auth_cookie($user_id, false); - } + if ($redirectAfterLogin === '') { + wp_redirect('https://'.$_SERVER['SERVER_NAME'].$adminSlug); - wp_redirect(admin_url()); + } elseif ($redirectAfterLogin === 'frontend') { + wp_redirect(home_url()); + + } elseif ($redirectAfterLogin === 'wpadmin') { + wp_redirect(admin_url()); + } exit; + } } + } + } + } + add_action('init', 'moox_auth_token'); function moox_redirect_logout() diff --git a/packages/press/wordpress/wp-config.php b/packages/press/wordpress/wp-config.php index 63f359cca..00f7561a5 100644 --- a/packages/press/wordpress/wp-config.php +++ b/packages/press/wordpress/wp-config.php @@ -29,6 +29,7 @@ 'ADMIN_SLUG' => getenv('ADMIN_SLUG'), 'LOCK_WP' => getenv('LOCK_WP'), 'AUTH_WP' => getenv('AUTH_WP'), + 'REDIRECT_AFTER_LOGIN' => getenv('REDIRECT_AFTER_LOGIN'), 'REDIRECT_INDEX' => getenv('REDIRECT_INDEX'), 'REDIRECT_TO_WP' => getenv('REDIRECT_TO_WP'), 'REDIRECT_LOGIN' => getenv('REDIRECT_LOGIN'), @@ -63,6 +64,7 @@ 'ADMIN_SLUG' => $_ENV['ADMIN_SLUG'], 'LOCK_WP' => $_ENV['LOCK_WP'], 'AUTH_WP' => $_ENV['AUTH_WP'], + 'REDIRECT_AFTER_LOGIN' => $_ENV['REDIRECT_AFTER_LOGIN'], 'REDIRECT_INDEX' => $_ENV['REDIRECT_INDEX'], 'REDIRECT_TO_WP' => $_ENV['REDIRECT_TO_WP'], 'REDIRECT_LOGIN' => $_ENV['REDIRECT_LOGIN'], @@ -104,6 +106,7 @@ define('LOCK_WP', isset($env['LOCK_WP']) ? $env['LOCK_WP'] : false); define('AUTH_WP', isset($env['AUTH_WP']) ? $env['AUTH_WP'] : false); +define('REDIRECT_AFTER_LOGIN', isset($env['REDIRECT_AFTER_LOGIN']) ? $env['REDIRECT_AFTER_LOGIN'] : ''); define('REDIRECT_INDEX', isset($env['REDIRECT_INDEX']) ? $env['REDIRECT_INDEX'] : false); define('REDIRECT_TO_WP', isset($env['REDIRECT_TO_WP']) ? $env['REDIRECT_TO_WP'] : false); define('REDIRECT_LOGIN', isset($env['REDIRECT_LOGIN']) ? $env['REDIRECT_LOGIN'] : false);