prefix . 'hookbird_endpoints'; } /** * Deliveries table name. * * @return string */ public static function deliveries_table() { global $wpdb; return $wpdb->prefix . 'hookbird_deliveries'; } /** * Activation callback. */ public static function activate() { self::create_tables(); add_option( Settings::OPTION, Settings::defaults(), '', false ); add_option( 'hookbird_show_welcome', 1, '', false ); self::schedule(); } /** * Deactivation callback. */ public static function deactivate() { wp_clear_scheduled_hook( self::CRON_PROCESS ); wp_clear_scheduled_hook( self::CRON_SWEEP ); wp_clear_scheduled_hook( self::CRON_CLEANUP ); delete_option( Dispatcher::LOCK_OPTION ); } /** * Create tables for new sites (for example after network activation) and run schema upgrades. */ public static function maybe_upgrade() { if ( get_option( 'hookbird_db_version' ) !== HOOKBIRD_DB_VERSION ) { self::create_tables(); add_option( Settings::OPTION, Settings::defaults(), '', false ); } self::schedule(); } /** * Ensure recurring cron events exist. */ public static function schedule() { if ( wp_next_scheduled( self::CRON_SWEEP ) && self::SWEEP_SCHEDULE !== wp_get_schedule( self::CRON_SWEEP ) ) { wp_clear_scheduled_hook( self::CRON_SWEEP ); } if ( ! wp_next_scheduled( self::CRON_SWEEP ) ) { wp_schedule_event( time() + MINUTE_IN_SECONDS, self::SWEEP_SCHEDULE, self::CRON_SWEEP ); } if ( ! wp_next_scheduled( self::CRON_CLEANUP ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_CLEANUP ); } } /** * Create or update tables with dbDelta. */ public static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $endpoints = self::endpoints_table(); $deliveries = self::deliveries_table(); dbDelta( "CREATE TABLE {$endpoints} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, name varchar(191) NOT NULL DEFAULT '', url text NOT NULL, secret varchar(191) NOT NULL DEFAULT '', events longtext NOT NULL, format varchar(20) NOT NULL DEFAULT 'json', headers longtext NULL, conditions longtext NULL, options longtext NULL, active tinyint(1) NOT NULL DEFAULT 1, paused_at datetime NULL, consecutive_failures int(10) unsigned NOT NULL DEFAULT 0, last_success_at datetime NULL, last_failure_at datetime NULL, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY active (active) ) {$charset};" ); dbDelta( "CREATE TABLE {$deliveries} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, uuid varchar(64) NOT NULL DEFAULT '', event_id varchar(64) NOT NULL DEFAULT '', endpoint_id bigint(20) unsigned NOT NULL DEFAULT 0, event varchar(100) NOT NULL DEFAULT '', payload longtext NOT NULL, status varchar(20) NOT NULL DEFAULT 'pending', attempts smallint(5) unsigned NOT NULL DEFAULT 0, next_attempt_at datetime NULL, response_code smallint(5) unsigned NULL, response_body text NULL, error text NULL, duration_ms int(10) unsigned NULL, is_test tinyint(1) NOT NULL DEFAULT 0, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY status_next (status,next_attempt_at), KEY endpoint_id (endpoint_id), KEY event (event), KEY created_at (created_at) ) {$charset};" ); update_option( 'hookbird_db_version', HOOKBIRD_DB_VERSION, false ); } }