HTforms Builder';
}
/**
* Render page.
*/
public static function page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to access this page.', 'htforms-builder' ) );
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing.
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : 'htform';
$status = get_option( HTForm_API::OPT_LAST, array() );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only notice.
$msg = isset( $_GET['htform_msg'] ) ? sanitize_key( wp_unslash( $_GET['htform_msg'] ) ) : '';
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- brand() constructs fixed markup and escapes its asset URL.
echo '' . self::brand() . '';
self::notice( $msg, $status );
if ( 'htform-connection' === $page || ! HTForm_API::is_configured() ) {
self::connection( $status );
} elseif ( 'htform-tools' === $page ) {
self::tools( $status );
} else {
self::forms();
}
HTForm_Attribution::card();
echo '
';
}
/**
* Notice helper.
*
* @param string $msg Message key.
* @param array $status Last status.
*/
private static function notice( $msg, $status ) {
$map = array(
'saved' => array( 'success', __( 'Connected to HTforms.', 'htforms-builder' ) ),
'failed' => array( 'error', __( 'Saved, but the connection test failed: ', 'htforms-builder' ) . ( isset( $status['error'] ) ? $status['error'] : '' ) ),
'disconnected' => array( 'success', __( 'Disconnected. Embedded forms will stop rendering.', 'htforms-builder' ) ),
'flushed' => array( 'success', __( 'Cache cleared.', 'htforms-builder' ) ),
'emailsaved' => array( 'success', __( 'Email settings saved.', 'htforms-builder' ) ),
'invalid' => array( 'error', __( 'Please enter a valid https URL and API key.', 'htforms-builder' ) ),
);
if ( isset( $map[ $msg ] ) ) {
printf( '', esc_attr( $map[ $msg ][0] ), esc_html( $map[ $msg ][1] ) );
}
}
/**
* Connection screen.
*
* @param array $status Last status.
*/
private static function connection( $status ) {
$connected = HTForm_API::is_configured() && ! empty( $status['ok'] );
$key = HTForm_API::api_key();
?>
' . esc_html__( 'Your forms', 'htforms-builder' ) . '
';
if ( empty( $forms ) ) {
echo '' . esc_html__( 'No forms found. Create and publish one in your HTforms dashboard, then clear the cache.', 'htforms-builder' ) . '
';
} else {
echo '| ' . esc_html__( 'Form', 'htforms-builder' ) . ' | ' . esc_html__( 'Status', 'htforms-builder' ) . ' | ' . esc_html__( 'Shortcode', 'htforms-builder' ) . ' |
';
foreach ( $forms as $f ) {
$id = isset( $f['id'] ) ? (string) $f['id'] : '';
if ( ! HTForm_API::valid_form_id( $id ) ) {
continue;
}
printf(
'| %1$s | %2$s | [htform id="%3$s"] |
',
esc_html( isset( $f['name'] ) ? $f['name'] : '' ),
esc_html( isset( $f['status'] ) ? $f['status'] : '' ),
esc_html( $id )
);
}
echo '
';
}
echo '' . esc_html__( 'Open HTforms dashboard (entries & reports)', 'htforms-builder' ) . '
';
echo '' . esc_html__( 'Elementor: drag a “Shortcode” widget onto your page and paste the shortcode above. Gutenberg: use the HTforms block.', 'htforms-builder' ) . '
';
self::email_card();
}
/**
* Email settings card.
*/
private static function email_card() {
$s = HTForm_Mailer::settings();
?>
in_array( $mode, array( 'wordpress', 'htforms', 'both' ), true ) ? $mode : 'wordpress',
'notify' => ! empty( $_POST['notify'] ),
'to' => isset( $_POST['to'] ) ? implode( ',', array_filter( array_map( 'sanitize_email', explode( ',', wp_unslash( $_POST['to'] ) ) ), 'is_email' ) ) : '', // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
'confirm' => ! empty( $_POST['confirm'] ),
'notify_subject' => isset( $_POST['notify_subject'] ) ? substr( sanitize_text_field( wp_unslash( $_POST['notify_subject'] ) ), 0, 200 ) : '',
'subject' => isset( $_POST['subject'] ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : '',
'message' => isset( $_POST['message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['message'] ) ) : '',
),
false
);
self::back( 'htform', 'emailsaved' );
}
/**
* Troubleshooting screen.
*
* @param array $status Last status.
*/
private static function tools( $status ) {
$r = HTForm_API::status();
$speed = HTForm_Compat::speed_check();
$tools = HTForm_Compat::detect();
?>
$page, 'htform_msg' => $msg ), admin_url( 'admin.php' ) ) );
exit;
}
/**
* Save settings.
*/
public static function save() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Not allowed.', 'htforms-builder' ), 403 );
}
check_admin_referer( 'htform_save' );
$url = isset( $_POST['htform_base_url'] ) ? esc_url_raw( wp_unslash( $_POST['htform_base_url'] ), array( 'https', 'http' ) ) : '';
$key = isset( $_POST['htform_api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['htform_api_key'] ) ) : '';
if ( '' === $key ) {
$key = HTForm_API::api_key();
}
if ( '' === $url || 1 !== preg_match( '/^htf_live_[a-f0-9]{48}$/', $key ) ) {
self::back( 'htform-connection', 'invalid' );
}
// Keep only scheme + host: people often paste a dashboard page or API link, which caused HTTP 404.
$parts = wp_parse_url( $url );
if ( empty( $parts['host'] ) ) {
self::back( 'htform-connection', 'invalid' );
}
$url = ( isset( $parts['scheme'] ) ? $parts['scheme'] : 'https' ) . '://' . $parts['host'] . ( isset( $parts['port'] ) ? ':' . (int) $parts['port'] : '' );
update_option( HTForm_API::OPT_URL, $url, false );
update_option( HTForm_API::OPT_KEY, $key, false );
HTForm_API::flush_cache();
$r = HTForm_API::status();
self::back( 'htform-connection', $r['ok'] ? 'saved' : 'failed' );
}
/**
* Disconnect.
*/
public static function disconnect() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Not allowed.', 'htforms-builder' ), 403 );
}
check_admin_referer( 'htform_disconnect' );
delete_option( HTForm_API::OPT_KEY );
delete_option( HTForm_API::OPT_LAST );
HTForm_API::flush_cache();
self::back( 'htform-connection', 'disconnected' );
}
/**
* Flush cache.
*/
public static function flush() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Not allowed.', 'htforms-builder' ), 403 );
}
check_admin_referer( 'htform_flush' );
HTForm_API::flush_cache();
HTForm_Compat::purge_page_caches();
self::back( 'htform-tools', 'flushed' );
}
}