ajax.php
3 weeks ago
install-notice.php
3 weeks ago
install-tracker.php
3 weeks ago
onboard-status.php
3 weeks ago
plugin-data-sender.php
1 year ago
plugin-installer.php
3 weeks ago
plugin-skin.php
1 year ago
plugin-status.php
4 years ago
utils.php
1 year ago
install-tracker.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ElementsKit_Lite\Libs\Framework\Classes; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | /** |
| 8 | * Tracks plugins installed during a WPMet onboarding flow. |
| 9 | * |
| 10 | * @since 3.9.5 |
| 11 | */ |
| 12 | class Install_Tracker { |
| 13 | |
| 14 | /** |
| 15 | * Option name used to store the shared WPMet onboarding registry. |
| 16 | * |
| 17 | * @since 3.9.5 |
| 18 | * @var string |
| 19 | */ |
| 20 | const OPTION_KEY = 'wpmet_onboarded_plugins'; |
| 21 | |
| 22 | /** |
| 23 | * Onboarding status options used by known Wpmet plugins. |
| 24 | * |
| 25 | * @since 3.9.5 |
| 26 | * @var array<string, array{option: string, value: mixed, crm_slug?: string}> |
| 27 | */ |
| 28 | const ONBOARD_STATUS_MAP = array( |
| 29 | 'elementskit-lite/elementskit-lite.php' => array( 'option' => 'elements_kit_onboard_status', 'value' => 'onboarded', 'crm_slug' => 'elementskit-lite' ), |
| 30 | 'emailkit/EmailKit.php' => array( 'option' => 'emailkit_onboard_status', 'value' => 'onboarded', 'crm_slug' => 'emailkit' ), |
| 31 | 'shopengine/shopengine.php' => array( 'option' => 'shopengine_onboard_status', 'value' => 'onboarded', 'crm_slug' => 'shopengine' ), |
| 32 | 'metform/metform.php' => array( 'option' => 'met_form_onboard_status', 'value' => 'onboarded', 'crm_slug' => 'metform' ), |
| 33 | 'gutenkit-blocks-addon/gutenkit-blocks-addon.php' => array( 'option' => 'gutenkit_onboard_status', 'value' => 'onboarded', 'crm_slug' => 'gutenkit-blocks-addon' ), |
| 34 | 'getgenie/getgenie.php' => array( 'option' => 'getgenie_onboard_status', 'value' => 'onboarded', 'crm_slug' => 'getgenie' ), |
| 35 | 'popup-builder-block/popup-builder-block.php' => array( 'option' => 'popupkit_onboard_status', 'value' => 'onboarded', 'crm_slug' => 'popupkit' ), |
| 36 | ); |
| 37 | |
| 38 | /** |
| 39 | * Return the filterable plugin onboarding map. |
| 40 | * |
| 41 | * @since 3.9.5 |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | public static function get_onboard_status_map(): array { |
| 46 | return (array) apply_filters( 'wpmet_onboard_status_map', self::ONBOARD_STATUS_MAP ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Record an auto-installed plugin and complete its onboarding. |
| 51 | * |
| 52 | * @since 3.9.5 |
| 53 | * |
| 54 | * @param string $plugin_file Plugin basename. |
| 55 | * @param string $installed_by Plugin that initiated the installation. |
| 56 | * @return void |
| 57 | */ |
| 58 | public static function mark( string $plugin_file, string $installed_by = 'elementskit-lite' ): void { |
| 59 | $registry = self::get_registry(); |
| 60 | |
| 61 | if ( ! isset( $registry[ $plugin_file ] ) ) { |
| 62 | $registry[ $plugin_file ] = array( |
| 63 | 'installed_by' => $installed_by, |
| 64 | 'installed_at' => time(), |
| 65 | ); |
| 66 | update_option( self::OPTION_KEY, $registry, false ); |
| 67 | } |
| 68 | |
| 69 | $map = self::get_onboard_status_map(); |
| 70 | |
| 71 | if ( isset( $map[ $plugin_file ] ) && ! get_option( $map[ $plugin_file ]['option'] ) ) { |
| 72 | update_option( $map[ $plugin_file ]['option'], $map[ $plugin_file ]['value'], false ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Check whether a plugin was installed by a Wpmet onboarding flow. |
| 78 | * |
| 79 | * @since 3.9.5 |
| 80 | * |
| 81 | * @param string $plugin_file Plugin basename. |
| 82 | * @return bool |
| 83 | */ |
| 84 | public static function was_auto_installed( string $plugin_file ): bool { |
| 85 | return isset( self::get_registry()[ $plugin_file ] ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the auto-install registry. |
| 90 | * |
| 91 | * @since 3.9.5 |
| 92 | * |
| 93 | * @return array Shared onboarding registry. |
| 94 | */ |
| 95 | public static function get_registry(): array { |
| 96 | return (array) get_option( self::OPTION_KEY, array() ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Store the collected onboarding email in the shared registry. |
| 101 | * |
| 102 | * @since 3.9.5 |
| 103 | * |
| 104 | * @param string $email Email address collected during onboarding. |
| 105 | * @return void |
| 106 | */ |
| 107 | public static function store_collected_email( string $email ): void { |
| 108 | if ( ! is_email( $email ) ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | $registry = self::get_registry(); |
| 113 | $registry['_shared']['email'] = sanitize_email( $email ); |
| 114 | $registry['_shared']['email_collected'] = true; |
| 115 | $registry['_shared']['updated_at'] = time(); |
| 116 | self::save_registry( $registry ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the email address stored in the shared onboarding registry. |
| 121 | * |
| 122 | * @since 3.9.5 |
| 123 | * |
| 124 | * @return string Sanitized email address, or an empty string when unavailable. |
| 125 | */ |
| 126 | public static function get_collected_email(): string { |
| 127 | $registry = self::get_registry(); |
| 128 | $email = isset( $registry['_shared']['email'] ) ? $registry['_shared']['email'] : ''; |
| 129 | |
| 130 | return is_email( $email ) ? sanitize_email( $email ) : ''; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Determine whether an email address has been collected. |
| 135 | * |
| 136 | * @since 3.9.5 |
| 137 | * |
| 138 | * @return bool True when an email address has been collected. |
| 139 | */ |
| 140 | public static function has_collected_email(): bool { |
| 141 | $registry = self::get_registry(); |
| 142 | |
| 143 | return ! empty( $registry['_shared']['email_collected'] ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Determine whether the install notice was dismissed for a plugin. |
| 148 | * |
| 149 | * @since 3.9.5 |
| 150 | * |
| 151 | * @param string $plugin_file Plugin basename. |
| 152 | * @return bool True when the notice was dismissed. |
| 153 | */ |
| 154 | public static function is_notice_dismissed( string $plugin_file ): bool { |
| 155 | $registry = self::get_registry(); |
| 156 | |
| 157 | return ! empty( $registry['_shared']['dismissed'][ $plugin_file ] ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Mark the install notice as dismissed for a plugin. |
| 162 | * |
| 163 | * @since 3.9.5 |
| 164 | * |
| 165 | * @param string $plugin_file Plugin basename. |
| 166 | * @return void |
| 167 | */ |
| 168 | public static function dismiss_notice( string $plugin_file ): void { |
| 169 | $registry = self::get_registry(); |
| 170 | $registry['_shared']['dismissed'][ $plugin_file ] = true; |
| 171 | $registry['_shared']['notice_resolved_at'][ $plugin_file ] = time(); |
| 172 | $registry['_shared']['updated_at'] = time(); |
| 173 | self::save_registry( $registry ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Save the shared onboarding registry. |
| 178 | * |
| 179 | * @since 3.9.5 |
| 180 | * |
| 181 | * @param array $registry Shared onboarding registry. |
| 182 | * @return void |
| 183 | */ |
| 184 | private static function save_registry( array $registry ): void { |
| 185 | update_option( self::OPTION_KEY, $registry, false ); |
| 186 | } |
| 187 | } |
| 188 |