update($wpdb->usermeta, array('meta_key' => '_atcwc_' . $key), array('meta_key' => '_atc_' . $key)); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key } $hpos = $wpdb->prefix . 'wc_orders_meta'; $has_hpos = $hpos === $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($hpos))); foreach ($order_keys as $key) { $wpdb->update($wpdb->postmeta, array('meta_key' => '_atcwc_' . $key), array('meta_key' => '_atc_' . $key)); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key if ($has_hpos) { $wpdb->update($hpos, array('meta_key' => '_atcwc_' . $key), array('meta_key' => '_atc_' . $key)); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key } } $old_table = $wpdb->prefix . 'atc_click_queue'; $new_table = $wpdb->prefix . 'atcwc_click_queue'; $has_old = $old_table === $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($old_table))); $has_new = $new_table === $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($new_table))); if ($has_old && !$has_new) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- tabella del plugin rinominata col nuovo prefisso. $wpdb->query($wpdb->prepare('RENAME TABLE %i TO %i', $old_table, $new_table)); } // phpcs:enable WordPress.DB.DirectDatabaseQuery update_option('atcwc_legacy_migrated', 1, false); } add_action('init', 'atcwc_migrate_legacy_names', 5); add_action('init', function () { // L'evento veniva pianificato SOLO all'attivazione: se spariva (cron // ripulito da un plugin di manutenzione, migrazione del sito, ripristino // di un backup) non tornava più e la manutenzione si fermava in silenzio. if (!wp_next_scheduled('atcwc_cron')) { wp_schedule_event(time() + 300, 'hourly', 'atcwc_cron'); } }); function atcwc_api() { return new Atcwc_Api( get_option('atcwc_api_url', 'https://affiliatetracker.it'), get_option('atcwc_api_key', '') ); } /** * Coda dei click che non sono riusciti a partire. * * Serve una tabella e non un'opzione: sotto traffico ogni click fallito * riscriverebbe la stessa riga di wp_options, con le corse fra richieste * concorrenti che ne seguono. Gemella di ps_atc_click_queue del connettore * PrestaShop (v0.19.0): i due connettori devono comportarsi allo stesso modo, * altrimenti un negozio che cambia piattaforma cambia anche i suoi numeri. * * Versione 2 (0.19.0): colonna `user_agent`, che la piattaforma usa per * separare i click dei bot. NULL = click accodato prima della 0.19.0, che si * ritrasmette senza il campo (vuoto vorrebbe dire bot). */ define('ATCWC_DB_VERSION', 2); function atcwc_click_queue_table() { global $wpdb; return $wpdb->prefix . 'atcwc_click_queue'; } function atcwc_install_db() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = atcwc_click_queue_table(); $collate = $wpdb->get_charset_collate(); // Formattazione dettata da dbDelta: due spazi dopo PRIMARY KEY, tipi in // minuscolo, una definizione per riga. Non e' stile, e' il suo parser. dbDelta( "CREATE TABLE $table ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, event_id char(32) NOT NULL, referral_code varchar(128) NOT NULL, landing_url varchar(512) NOT NULL DEFAULT '', campaign varchar(60) DEFAULT NULL, user_agent varchar(500) DEFAULT NULL, occurred_at datetime NOT NULL, attempts smallint(5) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY event_id (event_id), KEY occurred_at (occurred_at) ) $collate;" ); update_option('atcwc_db_version', ATCWC_DB_VERSION); } // Anche fuori dall'attivazione: aggiornare un plugin non sempre la rilancia // (copia dei file a mano, deploy da git, ripristino di un backup), e senza la // tabella la coda fallirebbe in silenzio proprio nel momento in cui serve. add_action('init', function () { if ((int) get_option('atcwc_db_version') < ATCWC_DB_VERSION) { atcwc_install_db(); } }); register_activation_hook(__FILE__, function () { atcwc_migrate_legacy_names(); add_option('atcwc_api_url', 'https://affiliatetracker.it'); add_option('atcwc_api_key', ''); add_option('atcwc_param', 'referral'); atcwc_install_db(); // Endpoint My Account: serve il flush delle rewrite rules. Atcwc_Account::add_endpoint(); flush_rewrite_rules(); if (!wp_next_scheduled('atcwc_cron')) { wp_schedule_event(time() + 300, 'hourly', 'atcwc_cron'); } }); register_deactivation_hook(__FILE__, function () { wp_clear_scheduled_hook('atcwc_cron'); flush_rewrite_rules(); }); Atcwc_Orders::init(); Atcwc_Account::init(); if (is_admin()) { Atcwc_Admin::init(); }