DeactivationTracker.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FCF\Free\Tracker; |
| 4 | |
| 5 | use FcfVendor\WPDesk\DeactivationModal; |
| 6 | use FcfVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 7 | use FcfVendor\WPDesk_Plugin_Info; |
| 8 | use WPDesk\FCF\Free\Settings\Form\EditFieldsForm; |
| 9 | use WPDesk\FCF\Free\Settings\Option\CustomFieldOption; |
| 10 | |
| 11 | /** |
| 12 | * Handles the modal displayed when the plugin is deactivated. |
| 13 | */ |
| 14 | class DeactivationTracker implements Hookable { |
| 15 | |
| 16 | const PLUGIN_SLUG = 'flexible-checkout-fields'; |
| 17 | const ACTIVATION_OPTION_NAME = 'plugin_activation_%s'; |
| 18 | |
| 19 | /** |
| 20 | * @var WPDesk_Plugin_Info |
| 21 | */ |
| 22 | private $plugin_info; |
| 23 | |
| 24 | /** |
| 25 | * @param WPDesk_Plugin_Info $plugin_info . |
| 26 | */ |
| 27 | public function __construct( WPDesk_Plugin_Info $plugin_info ) { |
| 28 | $this->plugin_info = $plugin_info; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * {@inheritdoc} |
| 33 | */ |
| 34 | public function hooks() { |
| 35 | add_action( 'admin_init', [ $this, 'load_deactivation_modal' ] ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return void |
| 40 | * @internal |
| 41 | */ |
| 42 | public function load_deactivation_modal() { |
| 43 | new DeactivationModal\Modal( |
| 44 | self::PLUGIN_SLUG, |
| 45 | ( new DeactivationModal\Model\FormTemplate( $this->plugin_info->get_plugin_name() ) ), |
| 46 | ( new DeactivationModal\Model\FormOptions() ) |
| 47 | ->set_option( |
| 48 | new DeactivationModal\Model\FormOption( |
| 49 | 'no_checkout_support', |
| 50 | 10, |
| 51 | __( 'The plugin does not work with the default WooCommerce checkout.', 'flexible-checkout-fields' ), |
| 52 | sprintf( |
| 53 | /* translators: %1$s: Open link tag, %2$s: Close link tag */ |
| 54 | esc_html__( |
| 55 | 'Pssst… Did you know that we support block checkout via our Checkout Fields For Blocks plugin? It allows you to add, edit and personalize fields in the WooCommerce block checkout - quickly, conveniently and without coding. It is free - find it %1$shere%2$s', |
| 56 | 'flexible-checkout-fields' |
| 57 | ), |
| 58 | '<a href="' . esc_url( 'https://wordpress.org/plugins/checkout-fields-for-blocks/' ) . '" target="_blank">', |
| 59 | '</a>' |
| 60 | ) |
| 61 | ) |
| 62 | ) |
| 63 | ->set_option( |
| 64 | new DeactivationModal\Model\FormOption( |
| 65 | 'plugin_not_working', |
| 66 | 10, |
| 67 | __( 'The plugin does not work properly', 'flexible-checkout-fields' ), |
| 68 | sprintf( |
| 69 | /* translators: %1$s: anchor opening tag, %2$s: anchor closing tag, %3$s: anchor opening tag, %4$s: anchor closing tag */ |
| 70 | __( 'Contact us on %1$sthe support forum%2$s or read %3$sthe plugin FAQ%4$s for help.', 'flexible-checkout-fields' ), |
| 71 | '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-doesnt-work-properly-support-forum' ) ) . '" target="_blank">', |
| 72 | '</a>', |
| 73 | '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-doesnt-work-properly-faq' ) ) . '" target="_blank">', |
| 74 | '</a>' |
| 75 | ), |
| 76 | __( 'Please tell us what was the problem.', 'flexible-checkout-fields' ) |
| 77 | ) |
| 78 | ) |
| 79 | ->set_option( |
| 80 | new DeactivationModal\Model\FormOption( |
| 81 | 'difficult_to_use', |
| 82 | 20, |
| 83 | __( 'The plugin is difficult to use', 'flexible-checkout-fields' ), |
| 84 | sprintf( |
| 85 | /* translators: %1$s: anchor opening tag, %2$s: anchor closing tag, %3$s: anchor opening tag, %4$s: anchor closing tag */ |
| 86 | __( 'Check %1$sthe documentation%2$s or contact us on %3$sthe support forum%4$s for help.', 'flexible-checkout-fields' ), |
| 87 | '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-plugin-is-difficult-docs' ) ) . '" target="_blank">', |
| 88 | '</a>', |
| 89 | '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-plugin-is-difficult-support-forum' ) ) . '" target="_blank">', |
| 90 | '</a>' |
| 91 | ), |
| 92 | __( 'How can we do it better? Please write what was problematic.', 'flexible-checkout-fields' ) |
| 93 | ) |
| 94 | ) |
| 95 | ->set_option( |
| 96 | new DeactivationModal\Model\FormOption( |
| 97 | 'not_meet_requirements', |
| 98 | 30, |
| 99 | __( 'The plugin does not meet all the requirements', 'flexible-checkout-fields' ), |
| 100 | null, |
| 101 | __( 'Please write what function is missing.', 'flexible-checkout-fields' ) |
| 102 | ) |
| 103 | ) |
| 104 | ->set_option( |
| 105 | new DeactivationModal\Model\FormOption( |
| 106 | 'found_better_plugin', |
| 107 | 40, |
| 108 | __( 'I found a better plugin', 'flexible-checkout-fields' ), |
| 109 | null, |
| 110 | __( 'Please write what plugin is it and what was the reason for choosing it.', 'flexible-checkout-fields' ) |
| 111 | ) |
| 112 | ) |
| 113 | ->set_option( |
| 114 | new DeactivationModal\Model\FormOption( |
| 115 | 'no_longer_need', |
| 116 | 50, |
| 117 | __( 'The plugin is no longer needed', 'flexible-checkout-fields' ), |
| 118 | null, |
| 119 | __( 'What is the reason for that?', 'flexible-checkout-fields' ) |
| 120 | ) |
| 121 | ) |
| 122 | ->set_option( |
| 123 | new DeactivationModal\Model\FormOption( |
| 124 | 'temporary_deactivation', |
| 125 | 60, |
| 126 | __( 'It\'s a temporary deactivation (I\'m just debugging an issue)', 'flexible-checkout-fields' ) |
| 127 | ) |
| 128 | ) |
| 129 | ->set_option( |
| 130 | new DeactivationModal\Model\FormOption( |
| 131 | 'other', |
| 132 | 70, |
| 133 | __( 'Other', 'flexible-checkout-fields' ), |
| 134 | null, |
| 135 | __( 'Please tell us what made you click this option.', 'flexible-checkout-fields' ) |
| 136 | ) |
| 137 | ), |
| 138 | ( new DeactivationModal\Model\FormValues() ) |
| 139 | ->set_value( |
| 140 | new DeactivationModal\Model\FormValue( |
| 141 | 'is_localhost', |
| 142 | [ $this, 'is_localhost' ] |
| 143 | ) |
| 144 | ) |
| 145 | ->set_value( |
| 146 | new DeactivationModal\Model\FormValue( |
| 147 | 'plugin_using_time', |
| 148 | [ $this, 'get_time_of_plugin_using' ] |
| 149 | ) |
| 150 | ) |
| 151 | ->set_value( |
| 152 | new DeactivationModal\Model\FormValue( |
| 153 | 'settings_saved', |
| 154 | [ $this, 'check_if_plugin_settings_saved' ] |
| 155 | ) |
| 156 | ) |
| 157 | ->set_value( |
| 158 | new DeactivationModal\Model\FormValue( |
| 159 | 'custom_fields_added', |
| 160 | [ $this, 'check_if_custom_field_created' ] |
| 161 | ) |
| 162 | ), |
| 163 | new DeactivationModal\Sender\DataWpdeskSender( |
| 164 | $this->plugin_info->get_plugin_file_name(), |
| 165 | $this->plugin_info->get_plugin_name() |
| 166 | ) |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @internal |
| 172 | */ |
| 173 | public function is_localhost(): bool { |
| 174 | return ( in_array( $_SERVER['REMOTE_ADDR'] ?? '', [ '127.0.0.1', '::1' ], true ) ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @return string|null |
| 179 | * @internal |
| 180 | */ |
| 181 | public function get_time_of_plugin_using() { |
| 182 | $option_activation = sprintf( self::ACTIVATION_OPTION_NAME, $this->plugin_info->get_plugin_file_name() ); |
| 183 | $activation_date = get_option( $option_activation, null ); |
| 184 | if ( $activation_date === null ) { |
| 185 | return null; |
| 186 | } |
| 187 | |
| 188 | $current_date = current_time( 'mysql' ); |
| 189 | return ( strtotime( $current_date ) - strtotime( $activation_date ) ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @internal |
| 194 | */ |
| 195 | public function check_if_plugin_settings_saved(): bool { |
| 196 | $settings = get_option( EditFieldsForm::SETTINGS_OPTION_NAME, null ); |
| 197 | return ( $settings !== null ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @internal |
| 202 | */ |
| 203 | public function check_if_custom_field_created(): bool { |
| 204 | $settings = get_option( EditFieldsForm::SETTINGS_OPTION_NAME, [] ); |
| 205 | foreach ( $settings as $section_fields ) { |
| 206 | foreach ( $section_fields as $section_field ) { |
| 207 | if ( $section_field[ CustomFieldOption::FIELD_NAME ] ?? false ) { |
| 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | } |
| 215 |