BackupExcluder.php
2 weeks ago
CacheIntegrator.php
2 weeks ago
CloudflareConfigurator.php
2 weeks ago
DeactivationModalLoader.php
2 weeks ago
EnvDetector.php
2 weeks ago
FileLoader.php
2 weeks ago
MediaStatusViewer.php
2 weeks ago
OptionsAccessManager.php
2 weeks ago
PathsGenerator.php
2 weeks ago
RestApiUnlocker.php
2 weeks ago
ServerConfigurator.php
2 weeks ago
SiteHealthDetector.php
2 weeks ago
StatsManager.php
2 weeks ago
TokenValidator.php
2 weeks ago
ViewLoader.php
2 weeks ago
WpCliManager.php
2 weeks ago
DeactivationModalLoader.php
226 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WebpConverter\Service; |
| 4 | |
| 5 | use WebpConverter\Error\Notice\LibsNotInstalledNotice; |
| 6 | use WebpConverter\Error\Notice\LibsWithoutWebpSupportNotice; |
| 7 | use WebpConverter\HookableInterface; |
| 8 | use WebpConverter\PluginData; |
| 9 | use WebpConverter\PluginInfo; |
| 10 | use WebpConverter\Settings\Page\AdvancedSettingsPage; |
| 11 | use WebpConverter\Settings\Page\PageIntegrator; |
| 12 | use WebpConverterVendor\MattPlugins\DeactivationModal; |
| 13 | |
| 14 | /** |
| 15 | * Initiates the popup displayed when the plugin is deactivated. |
| 16 | */ |
| 17 | class DeactivationModalLoader implements HookableInterface { |
| 18 | |
| 19 | const API_URL = 'https://data.mattplugins.com/deactivations/%s'; |
| 20 | |
| 21 | private PluginInfo $plugin_info; |
| 22 | |
| 23 | private PluginData $plugin_data; |
| 24 | |
| 25 | private StatsManager $stats_manager; |
| 26 | |
| 27 | public function __construct( |
| 28 | PluginInfo $plugin_info, |
| 29 | PluginData $plugin_data, |
| 30 | ?StatsManager $stats_manager = null |
| 31 | ) { |
| 32 | $this->plugin_info = $plugin_info; |
| 33 | $this->plugin_data = $plugin_data; |
| 34 | $this->stats_manager = $stats_manager ?: new StatsManager(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * {@inheritdoc} |
| 39 | */ |
| 40 | public function init_hooks(): void { |
| 41 | add_action( 'load-plugins.php', [ $this, 'load_modal' ] ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @throws DeactivationModal\Exception\DuplicatedFormOptionKeyException |
| 46 | * @throws DeactivationModal\Exception\DuplicatedFormValueKeyException |
| 47 | * @internal |
| 48 | */ |
| 49 | public function load_modal(): void { |
| 50 | new DeactivationModal\Modal( |
| 51 | $this->plugin_info->get_plugin_slug(), |
| 52 | new DeactivationModal\Model\FormTemplate( |
| 53 | sprintf( self::API_URL, $this->plugin_info->get_plugin_slug() ), |
| 54 | sprintf( |
| 55 | /* translators: %s: plugin name */ |
| 56 | __( 'We are sorry that you are leaving our %s plugin', 'webp-converter-for-media' ), |
| 57 | 'Converter for Media' |
| 58 | ), |
| 59 | __( 'Can you, please, take a moment to tell us why you are deactivating this plugin (anonymous answer)?', 'webp-converter-for-media' ), |
| 60 | __( 'Submit and Deactivate', 'webp-converter-for-media' ), |
| 61 | __( 'Skip and Deactivate', 'webp-converter-for-media' ), |
| 62 | $this->load_notice_message() |
| 63 | ), |
| 64 | ( new DeactivationModal\Model\FormOptions() ) |
| 65 | ->set_option( |
| 66 | new DeactivationModal\Model\FormOption( |
| 67 | 'server_config', |
| 68 | 10, |
| 69 | sprintf( |
| 70 | /* translators: %s: notice title */ |
| 71 | __( 'I have the %s notice in the plugin settings', 'webp-converter-for-media' ), |
| 72 | __( 'Server configuration error', 'webp-converter-for-media' ) |
| 73 | ), |
| 74 | function () { |
| 75 | $errors = apply_filters( 'webpc_server_errors', [] ); |
| 76 | if ( ! in_array( |
| 77 | $errors, |
| 78 | [ [ LibsWithoutWebpSupportNotice::ERROR_KEY ], [ LibsNotInstalledNotice::ERROR_KEY ] ] |
| 79 | ) ) { |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | return sprintf( |
| 84 | /* translators: %1$s: open anchor tag, %2$s: close anchor tag */ |
| 85 | __( 'If your server does not meet the technical requirements, you can use "Remote server" as "Conversion method", in %1$sthe plugin settings%2$s.', 'webp-converter-for-media' ), |
| 86 | '<a href="' . esc_url( PageIntegrator::get_settings_page_url() ) . '">', |
| 87 | '</a>' |
| 88 | ); |
| 89 | }, |
| 90 | __( 'What is your error? Have you been looking for a solution to this issue?', 'webp-converter-for-media' ) |
| 91 | ) |
| 92 | ) |
| 93 | ->set_option( |
| 94 | new DeactivationModal\Model\FormOption( |
| 95 | 'misunderstanding', |
| 96 | 20, |
| 97 | __( 'Images are not displayed in the WebP format', 'webp-converter-for-media' ), |
| 98 | function () { |
| 99 | return sprintf( |
| 100 | /* translators: %1$s: open anchor tag, %2$s: close anchor tag */ |
| 101 | __( 'Check out %1$sour instructions%2$s and see how to check if the plugin is working properly.', 'webp-converter-for-media' ), |
| 102 | '<a href="https://url.mattplugins.com/converter-deactivation-misunderstanding-instruction" target="_blank">', |
| 103 | '</a>' |
| 104 | ); |
| 105 | }, |
| 106 | __( 'Did you check the operation of the plugin in accordance with the instructions?', 'webp-converter-for-media' ) |
| 107 | ) |
| 108 | ) |
| 109 | ->set_option( |
| 110 | new DeactivationModal\Model\FormOption( |
| 111 | 'website_broken', |
| 112 | 30, |
| 113 | __( 'This plugin broke my website', 'webp-converter-for-media' ), |
| 114 | function () { |
| 115 | return sprintf( |
| 116 | /* translators: %1$s: option label, %2$s: open anchor tag, %3$s: close anchor tag */ |
| 117 | __( 'Check the %1$s option in %2$sthe plugin settings%3$s - this should solve the problem.', 'webp-converter-for-media' ), |
| 118 | __( 'Disable rewrite inheritance in .htaccess files', 'webp-converter-for-media' ), |
| 119 | '<a href="' . esc_url( PageIntegrator::get_settings_page_url( AdvancedSettingsPage::PAGE_SLUG ) ) . '">', |
| 120 | '</a>' |
| 121 | ); |
| 122 | }, |
| 123 | __( 'What exactly happened?', 'webp-converter-for-media' ) |
| 124 | ) |
| 125 | ) |
| 126 | ->set_option( |
| 127 | new DeactivationModal\Model\FormOption( |
| 128 | 'better_plugin', |
| 129 | 40, |
| 130 | __( 'I found a better plugin', 'webp-converter-for-media' ), |
| 131 | null, |
| 132 | __( 'What is the name of this plugin? Why is it better?', 'webp-converter-for-media' ) |
| 133 | ) |
| 134 | ) |
| 135 | ->set_option( |
| 136 | new DeactivationModal\Model\FormOption( |
| 137 | 'temporary_deactivation', |
| 138 | 50, |
| 139 | __( 'This is a temporary deactivation', 'webp-converter-for-media' ), |
| 140 | null, |
| 141 | null |
| 142 | ) |
| 143 | ) |
| 144 | ->set_option( |
| 145 | new DeactivationModal\Model\FormOption( |
| 146 | 'other', |
| 147 | 60, |
| 148 | __( 'Other reason', 'webp-converter-for-media' ), |
| 149 | null, |
| 150 | __( 'What is the reason? What can we improve for you?', 'webp-converter-for-media' ) |
| 151 | ) |
| 152 | ), |
| 153 | ( new DeactivationModal\Model\FormValues() ) |
| 154 | ->set_value( |
| 155 | new DeactivationModal\Model\FormValue( |
| 156 | 'request_error_codes', |
| 157 | function () { |
| 158 | return implode( ',', apply_filters( 'webpc_server_errors', [] ) ); |
| 159 | } |
| 160 | ) |
| 161 | ) |
| 162 | ->set_value( |
| 163 | new DeactivationModal\Model\FormValue( |
| 164 | 'request_plugin_settings', |
| 165 | function () { |
| 166 | $settings_json = json_encode( $this->plugin_data->get_plugin_settings_public() ); |
| 167 | return base64_encode( $settings_json ?: '' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions |
| 168 | } |
| 169 | ) |
| 170 | ) |
| 171 | ->set_value( |
| 172 | new DeactivationModal\Model\FormValue( |
| 173 | 'request_plugin_stats', |
| 174 | function () { |
| 175 | $stats_data = [ |
| 176 | 'usage_time' => $this->stats_manager->get_plugin_usage_time(), |
| 177 | 'first_version' => $this->stats_manager->get_plugin_first_version(), |
| 178 | 'regeneration_images' => $this->stats_manager->get_regeneration_images(), |
| 179 | 'webp_all' => $this->stats_manager->get_images_webp_all(), |
| 180 | 'webp_unconverted' => $this->stats_manager->get_images_webp_unconverted(), |
| 181 | 'avif_all' => $this->stats_manager->get_images_avif_all(), |
| 182 | 'avif_unconverted' => $this->stats_manager->get_images_avif_unconverted(), |
| 183 | 'rewrite_root' => PathsGenerator::get_rewrite_root(), |
| 184 | 'rewrite_path' => PathsGenerator::get_rewrite_path(), |
| 185 | ]; |
| 186 | |
| 187 | $stats_json = json_encode( $stats_data ); |
| 188 | return base64_encode( $stats_json ?: '' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions |
| 189 | } |
| 190 | ) |
| 191 | ) |
| 192 | ->set_value( |
| 193 | new DeactivationModal\Model\FormValue( |
| 194 | 'request_plugin_version', |
| 195 | function () { |
| 196 | return $this->plugin_info->get_plugin_version(); |
| 197 | } |
| 198 | ) |
| 199 | ) |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @return string|null |
| 205 | */ |
| 206 | private function load_notice_message(): ?string { |
| 207 | if ( ( apply_filters( 'webpc_server_errors', [] ) !== [] ) || is_multisite() ) { |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | $images_all = $this->stats_manager->get_images_webp_all() ?: 0; |
| 212 | $images_left = $this->stats_manager->get_images_webp_unconverted() ?: 0; |
| 213 | if ( ( $images_all === 0 ) || ( $images_left === 0 ) ) { |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | return sprintf( |
| 218 | /* translators: %1$s: button label, %2$s: open anchor tag, %3$s: close anchor tag */ |
| 219 | __( 'You have unconverted images on your website - click the %1$s button in %2$sthe plugin settings%3$s. This is all you need to do after installing the plugin.', 'webp-converter-for-media' ), |
| 220 | '"' . __( 'Start Bulk Optimization', 'webp-converter-for-media' ) . '"', |
| 221 | '<a href="' . esc_url( admin_url( 'upload.php?page=' . PageIntegrator::UPLOAD_MENU_PAGE ) ) . '">', |
| 222 | '</a>' |
| 223 | ); |
| 224 | } |
| 225 | } |
| 226 |