Brave
5 months ago
ContactForm7
4 months ago
Elementor
2 months ago
Forminator
2 months ago
NinjaForms
5 months ago
OptInMonster
5 months ago
Reach
2 months ago
SureForms
5 months ago
ThriveLeads
5 months ago
WPFormsLite
5 months ago
WSForms
8 months ago
WooCommerce
4 months ago
ContactForm7Integration.php
10 months ago
ImportManager.php
5 months ago
Integration.php
5 months ago
IntegrationInterface.php
9 months ago
IntegrationWithForms.php
9 months ago
PluginManager.php
9 months ago
ReachFormIntegration.php
9 months ago
WpFormsLiteIntegration.php
10 months ago
ContactForm7Integration.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations; |
| 4 | |
| 5 | use Hostinger\Reach\Api\Handlers\IntegrationsApiHandler; |
| 6 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 7 | use WPCF7_ContactForm; |
| 8 | use WPCF7_Submission; |
| 9 | use WP_Post; |
| 10 | use WPCF7_FormTag; |
| 11 | |
| 12 | if ( ! DEFINED( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | class ContactForm7Integration extends Integration implements IntegrationInterface { |
| 17 | |
| 18 | public const INTEGRATION_NAME = 'contact-form-7'; |
| 19 | protected ReachApiHandler $reach_api_handler; |
| 20 | protected IntegrationsApiHandler $integrations_api_handler; |
| 21 | |
| 22 | public function __construct( ReachApiHandler $reach_api_handler, IntegrationsApiHandler $integrations_api_handler ) { |
| 23 | parent::__construct(); |
| 24 | $this->integrations_api_handler = $integrations_api_handler; |
| 25 | $this->reach_api_handler = $reach_api_handler; |
| 26 | } |
| 27 | |
| 28 | public function init(): void { |
| 29 | if ( $this->integrations_api_handler->is_active( self::INTEGRATION_NAME ) ) { |
| 30 | add_action( 'wpcf7_mail_sent', array( $this, 'handle_submission' ), 10, 1 ); |
| 31 | add_filter( 'hostinger_reach_forms', array( $this, 'load_forms' ), 10, 2 ); |
| 32 | add_filter( 'hostinger_reach_after_form_state_is_set', array( $this, 'on_form_activation_change' ), 10, 3 ); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public function handle_submission( WPCF7_ContactForm $contact_form ): void { |
| 37 | if ( ! $this->is_form_enabled( $contact_form->id() ) ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $contact_list = $contact_form->title(); |
| 42 | $email = $this->get_field_data( $contact_form, array( 'basetype' => 'email' ) ); |
| 43 | if ( $email ) { |
| 44 | $response = $this->reach_api_handler->post_contact( |
| 45 | array( |
| 46 | 'group' => $contact_list, |
| 47 | 'email' => $email, |
| 48 | 'metadata' => array( |
| 49 | 'plugin' => self::INTEGRATION_NAME, |
| 50 | ), |
| 51 | ) |
| 52 | ); |
| 53 | |
| 54 | if ( $response->get_status() < 300 ) { |
| 55 | $this->update_form_submissions( $contact_form->id() ); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public function get_field_data( WPCF7_ContactForm $contact_form, array $condition ): string { |
| 61 | $tag = $this->find_field( $contact_form, $condition ); |
| 62 | |
| 63 | if ( ! is_null( $tag ) ) { |
| 64 | $submission = WPCF7_Submission::get_instance(); |
| 65 | return $submission->get_posted_data( $tag->name ); |
| 66 | } |
| 67 | |
| 68 | return ''; |
| 69 | } |
| 70 | |
| 71 | public function find_field( WPCF7_ContactForm $contact_form, array $condition ): ?WPCF7_FormTag { |
| 72 | $tags = $contact_form->scan_form_tags( $condition ); |
| 73 | |
| 74 | if ( ! empty( $tags ) ) { |
| 75 | return $tags[0]; |
| 76 | } |
| 77 | |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | public function get_post_type(): string { |
| 82 | return 'wpcf7_contact_form'; |
| 83 | } |
| 84 | |
| 85 | public static function get_name(): string { |
| 86 | return self::INTEGRATION_NAME; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | public function is_form_valid( WP_Post $post ): bool { |
| 91 | $contact_form = WPCF7_ContactForm::get_instance( $post->ID ); |
| 92 | if ( is_null( $contact_form ) ) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | return ! is_null( $this->find_field( $contact_form, array( 'basetype' => 'email' ) ) ); |
| 97 | } |
| 98 | |
| 99 | public function get_plugin_data( array $plugin_data ): array { |
| 100 | $plugin_data[ self::INTEGRATION_NAME ] = array( |
| 101 | 'folder' => 'contact-form-7', |
| 102 | 'file' => 'wp-contact-form-7.php', |
| 103 | 'admin_url' => 'admin.php?page=wpcf7', |
| 104 | 'add_form_url' => 'admin.php?page=wpcf7-new', |
| 105 | 'edit_url' => 'admin.php?page=wpcf7&post={form_id}&action=edit', |
| 106 | 'url' => 'https://wordpress.org/plugins/contact-form-7/', |
| 107 | 'download_url' => 'https://downloads.wordpress.org/plugin/contact-form-7.zip', |
| 108 | 'title' => __( 'Contact Form 7', 'hostinger-reach' ), |
| 109 | ); |
| 110 | |
| 111 | return $plugin_data; |
| 112 | } |
| 113 | } |
| 114 |