namespace WPForms\Integrations\LiteConnect;
use WPForms\Integrations\IntegrationInterface;
abstract class LiteConnect implements IntegrationInterface {
* The slug that will be used to save the option of Lite Connect.
const SETTINGS_SLUG = 'lite-connect-enabled';
* The $_GET argument to trigger the auth key endpoint.
const AUTH_KEY_ARG = 'wpforms-liteconnect-auth-key';
* Indicate if current integration is allowed to load.
public function allow_load() {
return self::is_allowed();
* Whether Lite Connect is allowed.
public static function is_allowed() {
// Disable Lite Connect integration for local hosts.
$allowed = ! self::is_local_not_debug() && self::is_production();
// phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName
* Determine whether Lite Connect integration is allowed to load.
* @param bool $is_allowed Is LiteConnect allowed? Value by default: true.
return (bool) apply_filters( 'wpforms_integrations_lite_connect_is_allowed', $allowed );
// phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName
* Whether Lite Connect is enabled.
public static function is_enabled() {
// phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName
* Determine whether LiteConnect is enabled on the WPForms > Settings admin page.
* @param bool $is_enabled Is LiteConnect enabled on WPForms > Settings page?
return (bool) apply_filters( 'wpforms_integrations_lite_connect_is_enabled', wpforms_setting( self::SETTINGS_SLUG ) );
// phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName
* Whether Lite Connect is running locally and not in the debug mode.
private static function is_local_not_debug() {
return ! defined( 'WPFORMS_DEBUG_LITE_CONNECT' ) && self::is_localhost();
* Whether Lite Connect is running locally.
private static function is_localhost() {
if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
$host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) );
foreach ( $local_tlds as $tld ) {
if ( preg_match( '/' . $tld . '$/', $host ) ) {
// Return false if IP and TLD are not local.
* Whether Lite Connect is running on production website.
private static function is_production() {
return wp_get_environment_type() === 'production';
* Provide responses to endpoint requests.
private function endpoints() {
// We check nonce in the endpoint_key().
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_GET[ self::AUTH_KEY_ARG ] ) ) {
* Process endpoint for callback on generate_site_key().
private function endpoint_key() {
$json = file_get_contents( 'php://input' );
$response = json_decode( $json, true );
$this->endpoint_die( 'Lite Connect: No response' );
if ( isset( $response['error'] ) ) {
'Lite Connect: unable to add the site to system',
if ( ! isset( $response['key'], $response['id'], $response['nonce'] ) ) {
'Lite Connect: unknown communication error',
if ( ! wp_verify_nonce( $response['nonce'], API::KEY_NONCE_ACTION ) ) {
'Lite Connect: nonce verification failed',
unset( $response['nonce'] );
$settings = get_option( Integration::get_option_name(), [] );
$settings['site'] = $response;
update_option( API::GENERATE_KEY_ATTEMPT_COUNTER_OPTION, 0 );
update_option( Integration::get_option_name(), $settings );
* Finish the endpoint execution with wp_die().
* @param string $title Log message title.
* @param array $response Response.
* @noinspection ForgottenDebugOutputInspection
private function endpoint_die( $title = '', $response = [] ) { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks
$this->log( $title, $response );
// We call wp_die too early, before the query is run.
// So, we should remove some filters to avoid having PHP notices in error log.
remove_filter( 'wp_robots', 'wp_robots_noindex_embeds' );
remove_filter( 'wp_robots', 'wp_robots_noindex_search' );
esc_html__( 'This is the Lite Connect endpoint page.', 'wpforms-lite' ),
* @param string $title Log message title.
* @param array $response Response.
private function log( $title = '', $response = [] ) {
'domain' => isset( $response['domain'] ) ? $response['domain'] : '',
'admin_email' => Integration::get_enabled_email(),
[ 'type' => [ 'error' ] ]