get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) !== $table ) { return false; } } return true; } public static function is_active() { return self::option_bool( 'roleWcAdcellTrackingActive', true ); } public static function option_bool( $name, $default = false ) { $value = get_option( $name, null ); if ( null === $value || false === $value ) { return $default; } return ( 'true' === $value || '1' === $value || 1 === $value || true === $value ); } private static $footer = array(); public static function footer_script( $src ) { $attributes = ( 'off' === RoleWcAdcellBidStore::consent_mode() ) ? array( 'src' => $src, 'async' => true ) : array( 'type' => 'text/plain', 'data-adcell-src' => $src ); self::footer_html( wp_get_script_tag( $attributes ) ); } public static function footer_html( $html ) { self::$footer[ $html ] = $html; } public static function print_footer() { foreach ( self::$footer as $html ) { echo $html; // phpcs:ignore WordPress.Security.EscapeOutput } self::$footer = array(); } public static function log( $message, array $context = array() ) { if ( ! self::option_bool( 'roleWcAdcellDebug', false ) ) { return; } $line = '[ADCELL] ' . $message; if ( $context ) { $line .= ' ' . wp_json_encode( $context ); } if ( function_exists( 'wc_get_logger' ) ) { wc_get_logger()->info( $line, array( 'source' => 'adcell-tracking-und-remarketing' ) ); return; } // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( $line ); } public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $upgrade = ( '' === (string) get_option( 'roleWcAdcellDbVersion', '' ) ) && ( '' !== (string) get_option( 'roleWcAdcellProgramId', '' ) ); if ( $upgrade && false === get_option( 'roleWcAdcellRetargeting', false ) ) { update_option( 'roleWcAdcellRetargeting', '' ); } $charset = $wpdb->get_charset_collate(); $bids = RoleWcAdcellBidStore::table(); $conv = RoleWcAdcellS2S::table(); dbDelta( "CREATE TABLE {$bids} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, vid varchar(64) NOT NULL DEFAULT '', session_key varchar(64) NOT NULL DEFAULT '', user_id bigint(20) unsigned NOT NULL DEFAULT 0, bid varchar(255) NOT NULL DEFAULT '', ck varchar(512) NOT NULL DEFAULT '', vc varchar(512) NOT NULL DEFAULT '', referer varchar(512) NOT NULL DEFAULT '', created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), UNIQUE KEY vid (vid), KEY session_key (session_key), KEY user_id (user_id), KEY updated_at (updated_at) ) {$charset};" ); dbDelta( "CREATE TABLE {$conv} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, order_id bigint(20) unsigned NOT NULL, event_id varchar(64) NOT NULL DEFAULT '', referenz varchar(191) NOT NULL DEFAULT '', betrag varchar(32) NOT NULL DEFAULT '0.00', product_ids text NULL, quantities text NULL, product_count int(11) NOT NULL DEFAULT 0, bid varchar(255) NOT NULL DEFAULT '', ck varchar(512) NOT NULL DEFAULT '', vck varchar(512) NOT NULL DEFAULT '', referer varchar(512) NOT NULL DEFAULT '', vouchers text NULL, status varchar(16) NOT NULL DEFAULT 'pending', attempts smallint(6) NOT NULL DEFAULT 0, http_code smallint(6) NOT NULL DEFAULT 0, last_error varchar(500) NOT NULL DEFAULT '', next_try datetime NOT NULL DEFAULT '0000-00-00 00:00:00', created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', sent_at datetime NULL, PRIMARY KEY (id), UNIQUE KEY order_event (order_id, event_id), KEY status (status, next_try), KEY prune (status, created_at) ) {$charset};" ); if ( ! wp_next_scheduled( RoleWcAdcellS2S::CRON_HOOK ) ) { wp_schedule_event( time() + 300, 'roleWcAdcellFiveMinutes', RoleWcAdcellS2S::CRON_HOOK ); } if ( ! wp_next_scheduled( self::PRUNE_HOOK ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::PRUNE_HOOK ); } if ( self::tables_exist() ) { update_option( 'roleWcAdcellDbVersion', ROLE_WC_ADCELL_DB_VERSION ); } else { self::log( 'Tabellen konnten nicht angelegt werden', array( 'fehler' => $wpdb->last_error ) ); } } public static function uninstall_cron() { foreach ( array( RoleWcAdcellS2S::CRON_HOOK, self::PRUNE_HOOK ) as $hook ) { $timestamp = wp_next_scheduled( $hook ); if ( $timestamp ) { wp_unschedule_event( $timestamp, $hook ); } } } public static function prune() { self::prune_table( RoleWcAdcellS2S::table(), "status IN ('sent','skipped','held') AND created_at < %s", gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ) ); self::prune_table( RoleWcAdcellBidStore::table(), 'updated_at < %s', gmdate( 'Y-m-d H:i:s', time() - 100 * DAY_IN_SECONDS ) ); } private static function prune_table( $table, $condition, $cutoff ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM `{$table}` WHERE {$condition} LIMIT %d", $cutoff, self::PRUNE_LIMIT ) ); foreach ( $ids as $id ) { $wpdb->delete( $table, array( 'id' => (int) $id ), array( '%d' ) ); } } } require_once plugin_dir_path( __FILE__ ) . 'includes/class-adcell-bid-store.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/class-adcell-order-amounts.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/class-adcell-s2s.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/class-adcell-admin.php'; require_once plugin_dir_path( __FILE__ ) . 'roleWcAdcellTrackingRenderTracking.php'; require_once plugin_dir_path( __FILE__ ) . 'roleWcAdcellTrackingRenderRetargeting.php'; register_activation_hook( __FILE__, array( 'RoleWcAdcell', 'install' ) ); register_deactivation_hook( __FILE__, array( 'RoleWcAdcell', 'uninstall_cron' ) ); RoleWcAdcellBidStore::init(); RoleWcAdcellS2S::init(); add_action( 'plugins_loaded', function () { if ( get_option( 'roleWcAdcellDbVersion' ) === ROLE_WC_ADCELL_DB_VERSION ) { return; } if ( get_transient( 'roleWcAdcellInstallTry' ) ) { return; } set_transient( 'roleWcAdcellInstallTry', 1, HOUR_IN_SECONDS ); RoleWcAdcell::install(); } ); add_action( RoleWcAdcell::PRUNE_HOOK, array( 'RoleWcAdcell', 'prune' ) ); add_action( 'wp_footer', array( 'RoleWcAdcell', 'print_footer' ), 99 ); add_action( 'init', function () { load_plugin_textdomain( 'adcell-tracking-und-remarketing', false, dirname( plugin_basename( ROLE_WC_ADCELL_FILE ) ) . '/languages' ); } ); RoleWcAdcellAdmin::init(); add_action( 'before_woocommerce_init', function () { if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true ); } } );