activation-tracker.php
1 week ago
display-options.php
1 week ago
field-type-settings.php
1 week ago
field.php
1 week ago
filed-validation.php
1 week ago
myaccount-edit-address.php
1 week ago
myaccount-field-processor.php
1 week ago
plugin.php
1 week ago
settings.php
1 week ago
tracker.php
1 week ago
user-meta-checkout.php
1 week ago
user-meta.php
1 week ago
user-profile.php
1 week ago
tracker.php
370 lines
| 1 | <?php |
| 2 | |
| 3 | use WPDesk\FCF\Free\Settings\Form\EditFieldsForm; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; // Exit if accessed directly |
| 7 | } |
| 8 | |
| 9 | if ( ! class_exists( 'WPDesk_Flexible_Checkout_Fields_Tracker' ) ) { |
| 10 | class WPDesk_Flexible_Checkout_Fields_Tracker { |
| 11 | |
| 12 | public static $script_version = '11'; |
| 13 | |
| 14 | public function __construct() { |
| 15 | $this->hooks(); |
| 16 | } |
| 17 | |
| 18 | public function hooks() { |
| 19 | add_filter( 'wpdesk_tracker_data', [ $this, 'wpdesk_tracker_data' ], 11 ); |
| 20 | add_filter( 'wpdesk_tracker_notice_screens', [ $this, 'wpdesk_tracker_notice_screens' ] ); |
| 21 | |
| 22 | add_filter( 'plugin_action_links_flexible-checkout-fields/flexible-checkout-fields.php', [ $this, 'plugin_action_links' ] ); |
| 23 | add_action( 'activated_plugin', [ $this, 'activated_plugin' ], 10, 2 ); |
| 24 | } |
| 25 | |
| 26 | public function wpdesk_tracker_data( $data ) { |
| 27 | $sections = [ |
| 28 | 'billing', |
| 29 | 'shipping', |
| 30 | 'order', |
| 31 | 'before_customer_details', |
| 32 | 'after_customer_details', |
| 33 | 'before_checkout_billing_form', |
| 34 | 'after_checkout_billing_form', |
| 35 | 'before_checkout_shipping_form', |
| 36 | 'after_checkout_shipping_form', |
| 37 | 'before_checkout_registration_form', |
| 38 | 'after_checkout_registration_form', |
| 39 | 'before_order_notes', |
| 40 | 'after_order_notes', |
| 41 | 'review_order_before_submit', |
| 42 | 'review_order_after_submit', |
| 43 | ]; |
| 44 | $settings_fields = get_option( EditFieldsForm::SETTINGS_OPTION_NAME, [] ); |
| 45 | if ( ! is_array( $settings_fields ) ) { |
| 46 | $settings_fields = []; |
| 47 | } |
| 48 | |
| 49 | $plugin_data = [ |
| 50 | 'sections' => $this->get_sections_data( $sections, $settings_fields ), |
| 51 | 'fields_names' => $this->get_fields_names( $settings_fields ), |
| 52 | 'options' => [ |
| 53 | 'css_disable' => get_option( 'inspire_checkout_fields_css_disable', '0' ), |
| 54 | ], |
| 55 | 'pro_version' => [ |
| 56 | 'is_active' => is_flexible_checkout_fields_pro_active() ? '1' : '0', |
| 57 | 'is_activated' => ( get_option( 'api_flexible-checkout-fields-pro_activated', '' ) === 'Activated' ) ? '1' : '0', |
| 58 | ], |
| 59 | ]; |
| 60 | |
| 61 | $data['flexible_checkout_fields'] = $plugin_data; |
| 62 | |
| 63 | return $data; |
| 64 | } |
| 65 | |
| 66 | private function get_fields_names( $settings_fields ) { |
| 67 | $items = []; |
| 68 | foreach ( $settings_fields as $section_key => $fields ) { |
| 69 | foreach ( $fields as $field_name => $field ) { |
| 70 | $name = str_replace( $section_key . '_', '', $field_name ); |
| 71 | if ( ! isset( $items[ $name ] ) ) { |
| 72 | $items[ $name ] = 0; |
| 73 | } |
| 74 | ++$items[ $name ]; |
| 75 | } |
| 76 | } |
| 77 | return $items; |
| 78 | } |
| 79 | |
| 80 | private function get_sections_data( $sections, $settings_fields ) { |
| 81 | $settings_sections = get_option( 'inspire_checkout_fields_section_settings', [] ); |
| 82 | if ( ! is_array( $settings_sections ) ) { |
| 83 | $settings_sections = []; |
| 84 | } |
| 85 | $default_data = [ |
| 86 | 'enabled' => 0, |
| 87 | 'has_title' => 0, |
| 88 | 'has_css' => 0, |
| 89 | 'fields' => [], |
| 90 | 'fields_count' => 0, |
| 91 | ]; |
| 92 | |
| 93 | $data = []; |
| 94 | foreach ( $sections as $section ) { |
| 95 | $data[ $section ] = $default_data; |
| 96 | if ( in_array( $section, [ 'billing', 'shipping', 'order' ] ) |
| 97 | || get_option( 'inspire_checkout_fields_' . $section, '0' ) ) { |
| 98 | $data[ $section ]['enabled'] = '1'; |
| 99 | } |
| 100 | if ( isset( $settings_sections[ $section ] ) && ! empty( $settings_sections[ $section ]['section_title'] ) ) { |
| 101 | $data[ $section ]['has_title'] = '1'; |
| 102 | } |
| 103 | if ( isset( $settings_sections[ $section ] ) && ! empty( $settings_sections[ $section ]['section_css'] ) ) { |
| 104 | $data[ $section ]['has_css'] = '1'; |
| 105 | } |
| 106 | $data[ $section ]['fields'] = $this->get_fields_data( $section, $settings_fields ); |
| 107 | if ( isset( $settings_fields[ $section ] ) ) { |
| 108 | $data[ $section ]['fields_count'] = count( $settings_fields[ $section ] ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $data; |
| 113 | } |
| 114 | |
| 115 | private function get_fields_data( $section, $settings ) { |
| 116 | if ( ! isset( $settings[ $section ] ) ) { |
| 117 | return []; |
| 118 | } |
| 119 | $default_data = [ |
| 120 | 'count' => 0, |
| 121 | 'enabled' => 0, |
| 122 | 'required' => 0, |
| 123 | 'validation' => [], |
| 124 | 'default_value' => 0, |
| 125 | 'placeholder' => 0, |
| 126 | 'display_on' => [ |
| 127 | 'thank_you' => 0, |
| 128 | 'on_address' => 0, |
| 129 | 'on_order' => 0, |
| 130 | 'on_emails' => 0, |
| 131 | 'option_new_line_before' => 0, |
| 132 | 'option_show_label' => 0, |
| 133 | ], |
| 134 | 'conditional_logic' => [], |
| 135 | 'pricing' => [ |
| 136 | 'enabled' => 0, |
| 137 | 'types' => [], |
| 138 | 'values' => [], |
| 139 | 'tax_classes' => [], |
| 140 | ], |
| 141 | ]; |
| 142 | |
| 143 | $data = []; |
| 144 | foreach ( $settings[ $section ] as $field ) { |
| 145 | $field_type = ( isset( $field['type'] ) ) ? $field['type'] : '_null_'; |
| 146 | |
| 147 | if ( ! isset( $data[ $field_type ] ) ) { |
| 148 | $data[ $field_type ] = $default_data; |
| 149 | } |
| 150 | ++$data[ $field_type ]['count']; |
| 151 | if ( isset( $field['visible'] ) && ! $field['visible'] ) { |
| 152 | ++$data[ $field_type ]['enabled']; |
| 153 | } |
| 154 | if ( isset( $field['required'] ) && $field['required'] ) { |
| 155 | ++$data[ $field_type ]['required']; |
| 156 | } |
| 157 | if ( isset( $field['validation'] ) && $field['validation'] ) { |
| 158 | if ( ! isset( $data[ $field_type ]['validation'][ $field['validation'] ] ) ) { |
| 159 | $data[ $field_type ]['validation'][ $field['validation'] ] = 0; |
| 160 | } |
| 161 | ++$data[ $field_type ]['validation'][ $field['validation'] ]; |
| 162 | } |
| 163 | if ( isset( $field['default'] ) && $field['default'] ) { |
| 164 | ++$data[ $field_type ]['default_value']; |
| 165 | } |
| 166 | if ( isset( $field['placeholder'] ) && $field['placeholder'] ) { |
| 167 | ++$data[ $field_type ]['placeholder']; |
| 168 | } |
| 169 | if ( isset( $field['display_on_thank_you'] ) && $field['display_on_thank_you'] ) { |
| 170 | ++$data[ $field_type ]['display_on']['thank_you']; |
| 171 | } |
| 172 | if ( isset( $field['display_on_address'] ) && $field['display_on_address'] ) { |
| 173 | ++$data[ $field_type ]['display_on']['on_address']; |
| 174 | } |
| 175 | if ( isset( $field['display_on_order'] ) && $field['display_on_order'] ) { |
| 176 | ++$data[ $field_type ]['display_on']['on_order']; |
| 177 | } |
| 178 | if ( isset( $field['display_on_emails'] ) && $field['display_on_emails'] ) { |
| 179 | ++$data[ $field_type ]['display_on']['on_emails']; |
| 180 | } |
| 181 | if ( isset( $field['display_on_option_new_line_before'] ) && $field['display_on_option_new_line_before'] ) { |
| 182 | ++$data[ $field_type ]['display_on']['option_new_line_before']; |
| 183 | } |
| 184 | if ( isset( $field['display_on_option_show_label'] ) && $field['display_on_option_show_label'] ) { |
| 185 | ++$data[ $field_type ]['display_on']['option_show_label']; |
| 186 | } |
| 187 | if ( isset( $field['pricing_enabled'] ) && $field['pricing_enabled'] ) { |
| 188 | ++$data[ $field_type ]['pricing']['enabled']; |
| 189 | } |
| 190 | if ( isset( $field['pricing_values'] ) && $field['pricing_values'] ) { |
| 191 | foreach ( $field['pricing_values'] as $pricing_value ) { |
| 192 | if ( ! isset( $data[ $field_type ]['pricing']['types'][ $pricing_value['type'] ] ) ) { |
| 193 | $data[ $field_type ]['pricing']['types'][ $pricing_value['type'] ] = 0; |
| 194 | } |
| 195 | ++$data[ $field_type ]['pricing']['types'][ $pricing_value['type'] ]; |
| 196 | } |
| 197 | foreach ( $field['pricing_values'] as $pricing_value ) { |
| 198 | if ( ! isset( $data[ $field_type ]['pricing']['values'][ $pricing_value['value'] ] ) ) { |
| 199 | $data[ $field_type ]['pricing']['values'][ $pricing_value['value'] ] = 0; |
| 200 | } |
| 201 | ++$data[ $field_type ]['pricing']['values'][ $pricing_value['value'] ]; |
| 202 | } |
| 203 | foreach ( $field['pricing_values'] as $pricing_value ) { |
| 204 | if ( ! isset( $data[ $field_type ]['pricing']['tax_classes'][ $pricing_value['tax_class'] ] ) ) { |
| 205 | $data[ $field_type ]['pricing']['tax_classes'][ $pricing_value['tax_class'] ] = 0; |
| 206 | } |
| 207 | ++$data[ $field_type ]['pricing']['tax_classes'][ $pricing_value['tax_class'] ]; |
| 208 | } |
| 209 | } |
| 210 | $data[ $field_type ]['conditional_logic'] = $this->get_conditional_logic_data( |
| 211 | $field, |
| 212 | $data[ $field_type ]['conditional_logic'] |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | return $data; |
| 217 | } |
| 218 | |
| 219 | private function get_conditional_logic_data( $field, $current_data ) { |
| 220 | $default_data = [ |
| 221 | 'enabled' => 0, |
| 222 | 'action' => [], |
| 223 | 'operator' => [], |
| 224 | 'rule_options' => [], |
| 225 | 'rule_operators' => [], |
| 226 | ]; |
| 227 | |
| 228 | $data = ( $current_data ) |
| 229 | ? $current_data |
| 230 | : [ |
| 231 | 'products' => $default_data, |
| 232 | 'fields' => $default_data, |
| 233 | 'shipping' => $default_data, |
| 234 | ]; |
| 235 | |
| 236 | if ( isset( $field['conditional_logic'] ) && $field['conditional_logic'] ) { |
| 237 | ++$data['products']['enabled']; |
| 238 | } |
| 239 | if ( isset( $field['conditional_logic_action'] ) && $field['conditional_logic_action'] ) { |
| 240 | if ( ! isset( $data['products']['action'][ $field['conditional_logic_action'] ] ) ) { |
| 241 | $data['products']['action'][ $field['conditional_logic_action'] ] = 0; |
| 242 | } |
| 243 | ++$data['products']['action'][ $field['conditional_logic_action'] ]; |
| 244 | } |
| 245 | if ( isset( $field['conditional_logic_operator'] ) && $field['conditional_logic_operator'] ) { |
| 246 | if ( ! isset( $data['products']['operator'][ $field['conditional_logic_operator'] ] ) ) { |
| 247 | $data['products']['operator'][ $field['conditional_logic_operator'] ] = 0; |
| 248 | } |
| 249 | ++$data['products']['operator'][ $field['conditional_logic_operator'] ]; |
| 250 | } |
| 251 | if ( isset( $field['conditional_logic_rules'] ) && $field['conditional_logic_rules'] ) { |
| 252 | foreach ( $field['conditional_logic_rules'] as $rule ) { |
| 253 | if ( ! isset( $data['products']['rule_options'][ $rule['condition'] ] ) ) { |
| 254 | $data['products']['rule_options'][ $rule['condition'] ] = 0; |
| 255 | } |
| 256 | ++$data['products']['rule_options'][ $rule['condition'] ]; |
| 257 | if ( ! isset( $data['products']['rule_operators'][ $rule['what'] ] ) ) { |
| 258 | $data['products']['rule_operators'][ $rule['what'] ] = 0; |
| 259 | } |
| 260 | ++$data['products']['rule_operators'][ $rule['what'] ]; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if ( isset( $field['conditional_logic_fields'] ) && $field['conditional_logic_fields'] ) { |
| 265 | ++$data['fields']['enabled']; |
| 266 | } |
| 267 | if ( isset( $field['conditional_logic_fields_action'] ) && $field['conditional_logic_fields_action'] ) { |
| 268 | if ( ! isset( $data['fields']['action'][ $field['conditional_logic_fields_action'] ] ) ) { |
| 269 | $data['fields']['action'][ $field['conditional_logic_fields_action'] ] = 0; |
| 270 | } |
| 271 | ++$data['fields']['action'][ $field['conditional_logic_fields_action'] ]; |
| 272 | } |
| 273 | if ( isset( $field['conditional_logic_fields_operator'] ) && $field['conditional_logic_fields_operator'] ) { |
| 274 | if ( ! isset( $data['fields']['operator'][ $field['conditional_logic_fields_operator'] ] ) ) { |
| 275 | $data['fields']['operator'][ $field['conditional_logic_fields_operator'] ] = 0; |
| 276 | } |
| 277 | ++$data['fields']['operator'][ $field['conditional_logic_fields_operator'] ]; |
| 278 | } |
| 279 | if ( isset( $field['conditional_logic_fields_rules'] ) && $field['conditional_logic_fields_rules'] ) { |
| 280 | foreach ( $field['conditional_logic_fields_rules'] as $rule ) { |
| 281 | if ( ! isset( $data['fields']['rule_operators'][ $rule['condition'] ] ) ) { |
| 282 | $data['fields']['rule_operators'][ $rule['condition'] ] = 0; |
| 283 | } |
| 284 | ++$data['fields']['rule_operators'][ $rule['condition'] ]; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if ( isset( $field['conditional_logic_shipping_fields'] ) && $field['conditional_logic_shipping_fields'] ) { |
| 289 | ++$data['shipping']['enabled']; |
| 290 | } |
| 291 | if ( isset( $field['conditional_logic_shipping_fields_action'] ) && $field['conditional_logic_shipping_fields_action'] ) { |
| 292 | if ( ! isset( $data['shipping']['action'][ $field['conditional_logic_shipping_fields_action'] ] ) ) { |
| 293 | $data['shipping']['action'][ $field['conditional_logic_shipping_fields_action'] ] = 0; |
| 294 | } |
| 295 | ++$data['shipping']['action'][ $field['conditional_logic_shipping_fields_action'] ]; |
| 296 | } |
| 297 | if ( isset( $field['conditional_logic_shipping_fields_operator'] ) && $field['conditional_logic_shipping_fields_operator'] ) { |
| 298 | if ( ! isset( $data['shipping']['operator'][ $field['conditional_logic_shipping_fields_operator'] ] ) ) { |
| 299 | $data['shipping']['operator'][ $field['conditional_logic_shipping_fields_operator'] ] = 0; |
| 300 | } |
| 301 | ++$data['shipping']['operator'][ $field['conditional_logic_shipping_fields_operator'] ]; |
| 302 | } |
| 303 | |
| 304 | return $data; |
| 305 | } |
| 306 | |
| 307 | public function wpdesk_tracker_notice_screens( $screens ) { |
| 308 | $current_screen = get_current_screen(); |
| 309 | if ( $current_screen->id == 'woocommerce_page_wpdesk_checkout_fields_settings' ) { |
| 310 | $screens[] = $current_screen->id; |
| 311 | } |
| 312 | return $screens; |
| 313 | } |
| 314 | |
| 315 | public function plugin_action_links( $links ) { |
| 316 | if ( ! wpdesk_tracker_enabled() || apply_filters( 'wpdesk_tracker_do_not_ask', false ) ) { |
| 317 | return $links; |
| 318 | } |
| 319 | $options = get_option( 'wpdesk_helper_options', [] ); |
| 320 | if ( ! is_array( $options ) ) { |
| 321 | $options = []; |
| 322 | } |
| 323 | if ( empty( $options['wpdesk_tracker_agree'] ) ) { |
| 324 | $options['wpdesk_tracker_agree'] = '0'; |
| 325 | } |
| 326 | $plugin_links = []; |
| 327 | if ( $options['wpdesk_tracker_agree'] == '0' ) { |
| 328 | $opt_in_link = admin_url( 'admin.php?page=wpdesk_tracker&plugin=flexible-checkout-fields/flexible-checkout-fields.php' ); |
| 329 | $plugin_links[] = '<a href="' . $opt_in_link . '">' . __( 'Opt-in', 'flexible-checkout-fields' ) . '</a>'; |
| 330 | } else { |
| 331 | $opt_in_link = admin_url( 'plugins.php?wpdesk_tracker_opt_out=1&plugin=flexible-checkout-fields/flexible-checkout-fields.php' ); |
| 332 | $plugin_links[] = '<a href="' . $opt_in_link . '">' . __( 'Opt-out', 'flexible-checkout-fields' ) . '</a>'; |
| 333 | } |
| 334 | return array_merge( $plugin_links, $links ); |
| 335 | } |
| 336 | |
| 337 | public function activated_plugin( $plugin, $network_wide ) { |
| 338 | if ( $network_wide ) { |
| 339 | return; |
| 340 | } |
| 341 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 342 | return; |
| 343 | } |
| 344 | if ( ! wpdesk_tracker_enabled() ) { |
| 345 | return; |
| 346 | } |
| 347 | if ( $plugin == 'flexible-checkout-fields/flexible-checkout-fields.php' ) { |
| 348 | $options = get_option( 'wpdesk_helper_options', [] ); |
| 349 | |
| 350 | if ( empty( $options ) ) { |
| 351 | $options = []; |
| 352 | } |
| 353 | if ( empty( $options['wpdesk_tracker_agree'] ) ) { |
| 354 | $options['wpdesk_tracker_agree'] = '0'; |
| 355 | } |
| 356 | $wpdesk_tracker_skip_plugin = get_option( 'wpdesk_tracker_skip_flexible_checkout_fields', '0' ); |
| 357 | if ( $options['wpdesk_tracker_agree'] == '0' && $wpdesk_tracker_skip_plugin == '0' ) { |
| 358 | update_option( 'wpdesk_tracker_notice', '1' ); |
| 359 | update_option( 'wpdesk_tracker_skip_flexible_checkout_fields', '1' ); |
| 360 | if ( ! apply_filters( 'wpdesk_tracker_do_not_ask', false ) ) { |
| 361 | wp_redirect( admin_url( 'admin.php?page=wpdesk_tracker&plugin=flexible-checkout-fields/flexible-checkout-fields.php' ) ); |
| 362 | exit; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | } |
| 370 |