From ba1d23517976a2209a1858dfde94ff5c17b3650c Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Mon, 16 Feb 2026 22:36:00 +0600 Subject: [PATCH] feat(integration): add Simple Basic Contact Form integration - Add MC4WP_Simple_Basic_Contact_Form_Integration class - Hook into scf_filter_contact_form to inject checkbox - Hook into scf_send_email to process subscription - Register class in autoload classmap - Register integration in bootstrap Fixes #753 --- autoload.php | 1 + integrations/bootstrap.php | 1 + .../class-simple-basic-contact-form.php | 117 ++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 integrations/simple-basic-contact-form/class-simple-basic-contact-form.php diff --git a/autoload.php b/autoload.php index e2751c05..08c2527f 100755 --- a/autoload.php +++ b/autoload.php @@ -68,6 +68,7 @@ 'MC4WP_Queue' => '/includes/class-queue.php', 'MC4WP_Queue_Job' => '/includes/class-queue-job.php', 'MC4WP_Registration_Form_Integration' => '/integrations/wp-registration-form/class-registration-form.php', + 'MC4WP_Simple_Basic_Contact_Form_Integration' => '/integrations/simple-basic-contact-form/class-simple-basic-contact-form.php', 'MC4WP_Tools' => '/includes/class-tools.php', 'MC4WP_Upgrade_Routines' => '/includes/admin/class-upgrade-routines.php', 'MC4WP_User_Integration' => '/includes/integrations/class-user-integration.php', diff --git a/integrations/bootstrap.php b/integrations/bootstrap.php index 1aa05670..800b9a7c 100755 --- a/integrations/bootstrap.php +++ b/integrations/bootstrap.php @@ -43,6 +43,7 @@ function mc4wp_admin_after_integration_settings(MC4WP_Integration $integration, mc4wp_register_integration('memberpress', 'MC4WP_MemberPress_Integration'); mc4wp_register_integration('affiliatewp', 'MC4WP_AffiliateWP_Integration'); mc4wp_register_integration('give', 'MC4WP_Give_Integration'); +mc4wp_register_integration('simple-basic-contact-form', 'MC4WP_Simple_Basic_Contact_Form_Integration'); mc4wp_register_integration('custom', 'MC4WP_Custom_Integration', true); mc4wp_register_integration('woocommerce', 'MC4WP_WooCommerce_Integration'); diff --git a/integrations/simple-basic-contact-form/class-simple-basic-contact-form.php b/integrations/simple-basic-contact-form/class-simple-basic-contact-form.php new file mode 100644 index 00000000..ba4d664e --- /dev/null +++ b/integrations/simple-basic-contact-form/class-simple-basic-contact-form.php @@ -0,0 +1,117 @@ + tag so it appears + * inside the form, just before the submit area. + * + * @param string $form_html The full contact form HTML. + * @return string Modified form HTML with the checkbox injected. + */ + public function add_checkbox($form_html) + { + // do not add a checkbox when integration is implicit + if ($this->options['implicit']) { + return $form_html; + } + + $checkbox_html = $this->get_checkbox_html(); + + // insert the checkbox just before the closing tag + $form_html = str_ireplace('', $checkbox_html . '', $form_html); + + return $form_html; + } + + /** + * Process the form submission and subscribe the user if the checkbox was checked. + * + * Fires on the `scf_send_email` action after the contact form email is sent. + * + * @param string $recipient The email recipient. + * @param string $topic The email subject. + * @param string $message The email message body. + * @param string $headers The email headers. + * @param string $email The sender's email address (form submitter). + * + * @return bool + */ + public function process($recipient, $topic, $message, $headers, $email) + { + // was sign-up checkbox checked? + if (! $this->triggered()) { + return false; + } + + $parser = new MC4WP_Field_Guesser($this->get_data()); + $data = $parser->combine([ 'guessed', 'namespaced' ]); + + // use the email from the action parameter if not found via field guesser + if (empty($data['EMAIL']) && ! empty($email)) { + $data['EMAIL'] = $email; + } + + // do nothing if no email was found + if (empty($data['EMAIL'])) { + $this->get_log()->warning(sprintf('%s > Unable to find EMAIL field.', $this->name)); + return false; + } + + return $this->subscribe($data); + } + + /** + * Are the required dependencies for this integration installed? + * + * @return bool + */ + public function is_installed() + { + return function_exists('simple_contact_form'); + } + + /** + * Returns the UI elements to show on the settings page. + * + * Removes 'implicit' since this integration uses an explicit checkbox approach. + * + * @since 4.9.0 + * @return array + */ + public function get_ui_elements() + { + return array_diff(parent::get_ui_elements(), [ 'implicit' ]); + } +}