|null */ private $adapters = null; /** * The instance. * * @return Waref_Forms_Plugin */ public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Hooks. */ private function __construct() { add_filter( 'cron_schedules', array( 'Waref_Forms_Queue', 'cron_schedules' ) ); // phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval -- five minutes. add_action( 'plugins_loaded', array( $this, 'maybe_upgrade' ), 5 ); add_action( 'plugins_loaded', array( $this, 'register_adapters' ), 20 ); add_action( 'init', array( 'Waref_Forms_Embed', 'init' ) ); add_action( 'init', array( $this, 'ensure_cron' ) ); add_action( Waref_Forms_Queue::CRON_HOOK, array( 'Waref_Forms_Queue', 'process' ) ); add_action( Waref_Forms_Connect::HEARTBEAT_HOOK, array( 'Waref_Forms_Connect', 'cron_heartbeat' ) ); Waref_Forms_Frontend::init(); if ( is_admin() ) { Waref_Forms_Settings::init(); } } /** * The adapters (filterable). * * @return array */ public function adapters() { if ( null === $this->adapters ) { $list = array( new Waref_Forms_Adapter_CF7(), new Waref_Forms_Adapter_Elementor(), new Waref_Forms_Adapter_WPForms(), new Waref_Forms_Adapter_Ninja(), new Waref_Forms_Adapter_Gravity(), new Waref_Forms_Adapter_Fluent(), new Waref_Forms_Adapter_Formidable(), new Waref_Forms_Adapter_Forminator(), new Waref_Forms_Adapter_Woo_Customers(), new Waref_Forms_Adapter_Woo_Orders(), new Waref_Forms_Adapter_WP_Comments(), new Waref_Forms_Adapter_WP_Register(), ); /** * Filters the adapters (each extends Waref_Forms_Adapter). * * @param Waref_Forms_Adapter[] $list Adapters. */ $list = apply_filters( 'waref_forms_adapters', $list ); $this->adapters = array(); foreach ( (array) $list as $adapter ) { if ( $adapter instanceof Waref_Forms_Adapter ) { $this->adapters[ $adapter->id() ] = $adapter; } } } return $this->adapters; } /** * One adapter. * * @param string $id Id. * @return Waref_Forms_Adapter|null */ public function adapter( $id ) { $adapters = $this->adapters(); return isset( $adapters[ $id ] ) ? $adapters[ $id ] : null; } /** * Adds the hooks of every enabled adapter (only while connected). */ public function register_adapters() { if ( ! Waref_Forms_Options::is_connected() ) { return; } foreach ( $this->adapters() as $adapter ) { try { if ( $adapter->is_enabled() ) { $adapter->register(); } } catch ( \Throwable $e ) { Waref_Forms_Payload::debug( $e ); } } } /** * Source plugin keys of the active and enabled adapters (for the heartbeat). * * @return string[] */ public function active_integrations() { $keys = array(); foreach ( $this->adapters() as $adapter ) { if ( $adapter->is_enabled() ) { $keys[ $adapter->plugin() ] = true; } } return array_keys( $keys ); } /** * Schedules the cron events when they are missing. */ public function ensure_cron() { if ( ! wp_next_scheduled( Waref_Forms_Queue::CRON_HOOK ) ) { wp_schedule_event( time() + MINUTE_IN_SECONDS, Waref_Forms_Queue::SCHEDULE, Waref_Forms_Queue::CRON_HOOK ); } if ( ! wp_next_scheduled( Waref_Forms_Connect::HEARTBEAT_HOOK ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'twicedaily', Waref_Forms_Connect::HEARTBEAT_HOOK ); } } /** * Creates the queue table on a new version and moves 1.x data. */ public function maybe_upgrade() { if ( Waref_Forms_Queue::DB_VERSION !== get_option( Waref_Forms_Options::DB_VERSION ) ) { Waref_Forms_Queue::install(); } $installed = get_option( Waref_Forms_Options::VERSION ); if ( WAREF_FORMS_VERSION === $installed ) { return; } Waref_Forms_Options::ensure_options(); if ( is_string( $installed ) && '' !== $installed && version_compare( $installed, '2.0.0', '<' ) ) { // 1.x kept { api_url, site_key }; the site key is the connection's public key. $old = get_option( Waref_Forms_Options::CONNECTION, array() ); if ( is_array( $old ) && ! empty( $old['site_key'] ) && empty( $old['public_key'] ) ) { update_option( Waref_Forms_Options::CONNECTION, array( 'api_url' => isset( $old['api_url'] ) ? (string) $old['api_url'] : '', 'public_key' => (string) $old['site_key'], 'status' => 'active', ), true ); } foreach ( Waref_Forms_Options::LEGACY as $option ) { delete_option( $option ); } wp_unschedule_hook( 'waref_forms_retry' ); } update_option( Waref_Forms_Options::VERSION, WAREF_FORMS_VERSION, true ); } /** * Activation: options, table, cron. */ public static function activate() { Waref_Forms_Options::ensure_options(); Waref_Forms_Queue::install(); self::instance()->maybe_upgrade(); self::instance()->ensure_cron(); } /** * Deactivation: stops the cron events (data is kept until uninstall). */ public static function deactivate() { wp_unschedule_hook( Waref_Forms_Queue::CRON_HOOK ); wp_unschedule_hook( Waref_Forms_Connect::HEARTBEAT_HOOK ); wp_unschedule_hook( 'waref_forms_retry' ); } }