auto-install-notice.php
2 weeks ago
auto-install-tracker.php
2 weeks ago
plugin-installer.php
2 weeks ago
plugin-skin.php
1 year ago
auto-install-tracker.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Utils\Onboard; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | /** |
| 8 | * Tracks which plugins were silently installed during a wpmet onboarding flow. |
| 9 | * |
| 10 | * When ShopEngine installs another plugin during onboarding, mark() does two things: |
| 11 | * 1. Writes the slug to the shared `wpmet_onboarded_plugins` option (registry). |
| 12 | * 2. Directly sets the installed plugin's own onboard status option so its wizard |
| 13 | * never appears — even if that plugin hasn't been updated to check the registry. |
| 14 | */ |
| 15 | class Auto_Install_Tracker { |
| 16 | |
| 17 | const OPTION_KEY = 'wpmet_onboarded_plugins'; |
| 18 | |
| 19 | /** |
| 20 | * Core wpmet plugins that ShopEngine knows about by default. |
| 21 | * Each plugin should also register itself via the 'wpmet_onboard_status_map' filter |
| 22 | * so no single plugin needs to maintain the full list. |
| 23 | * |
| 24 | * 'plugin-file' => [ 'option' => 'wp_option_name', 'value' => value_when_done ] |
| 25 | */ |
| 26 | const ONBOARD_STATUS_MAP = [ |
| 27 | 'metform/metform.php' => [ 'option' => 'met_form_onboard_status', 'value' => 'onboarded' ], |
| 28 | 'elementskit-lite/elementskit-lite.php' => [ 'option' => 'elements_kit_onboard_status', 'value' => 'onboarded' ], |
| 29 | 'emailkit/EmailKit.php' => [ 'option' => 'emailkit_onboard_status', 'value' => 'onboarded' ], |
| 30 | 'shopengine/shopengine.php' => [ 'option' => 'shopengine_onboard_status', 'value' => true ], |
| 31 | 'gutenkit-blocks-addon/gutenkit-blocks-addon.php' => [ 'option' => 'gutenkit_onboard_status', 'value' => 'onboarded' ], |
| 32 | 'getgenie/getgenie.php' => [ 'option' => 'getgenie_onboard_status', 'value' => 'onboarded' ], |
| 33 | 'popup-builder-block/popup-builder-block.php' => [ 'option' => 'popupkit_onboard_status', 'value' => 'onboarded' ], |
| 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * Record a plugin as auto-installed and directly mark its onboarding as complete. |
| 38 | * |
| 39 | * @param string $plugin_file e.g. 'metform/metform.php' |
| 40 | * @param string $installed_by Slug of the plugin that triggered the install. |
| 41 | */ |
| 42 | public static function mark( string $plugin_file, string $installed_by = 'shopengine' ): void { |
| 43 | // 1. Write to shared registry. |
| 44 | $registry = self::get_registry(); |
| 45 | if ( ! isset( $registry[ $plugin_file ] ) ) { |
| 46 | $registry[ $plugin_file ] = [ |
| 47 | 'installed_by' => $installed_by, |
| 48 | 'installed_at' => time(), |
| 49 | ]; |
| 50 | update_option( self::OPTION_KEY, $registry, false ); |
| 51 | } |
| 52 | |
| 53 | // 2. Directly set the installed plugin's own onboard status. |
| 54 | // Each plugin registers itself via 'wpmet_onboard_status_map' filter; |
| 55 | // ONBOARD_STATUS_MAP is just the fallback for plugins that haven't done so yet. |
| 56 | $map = apply_filters( 'wpmet_onboard_status_map', self::ONBOARD_STATUS_MAP ); |
| 57 | if ( isset( $map[ $plugin_file ] ) && ! get_option( $map[ $plugin_file ]['option'] ) ) { |
| 58 | update_option( $map[ $plugin_file ]['option'], $map[ $plugin_file ]['value'], false ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Check whether a plugin was installed during any wpmet onboarding flow. |
| 64 | * |
| 65 | * @param string $plugin_file e.g. 'metform/metform.php' |
| 66 | * @return bool |
| 67 | */ |
| 68 | public static function was_auto_installed( string $plugin_file ): bool { |
| 69 | return isset( self::get_registry()[ $plugin_file ] ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Return the full registry array. |
| 74 | * |
| 75 | * @return array<string, array{installed_by: string, installed_at: int}> |
| 76 | */ |
| 77 | public static function get_registry(): array { |
| 78 | return (array) get_option( self::OPTION_KEY, [] ); |
| 79 | } |
| 80 | } |
| 81 |