ContactForm7Integration.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations\ContactForm7; |
| 4 | |
| 5 | use Hostinger\Reach\Dto\PluginData; |
| 6 | use Hostinger\Reach\Integrations\Integration; |
| 7 | use Hostinger\Reach\Integrations\IntegrationInterface; |
| 8 | use WP_Post; |
| 9 | use WPCF7_ContactForm; |
| 10 | use WPCF7_FormTag; |
| 11 | use WPCF7_Submission; |
| 12 | |
| 13 | if ( ! DEFINED( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | class ContactForm7Integration extends Integration implements IntegrationInterface { |
| 18 | |
| 19 | public const INTEGRATION_NAME = 'contact-form-7'; |
| 20 | |
| 21 | public function active_integration_hooks(): void { |
| 22 | add_action( 'wpcf7_mail_sent', array( $this, 'handle_submission' ), 10, 1 ); |
| 23 | } |
| 24 | |
| 25 | public function handle_submission( WPCF7_ContactForm $contact_form ): void { |
| 26 | if ( ! $this->is_form_enabled( $contact_form->id() ) ) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | $contact_list = $contact_form->title(); |
| 31 | $email = $this->get_field_data( $contact_form, array( 'basetype' => 'email' ) ); |
| 32 | $name = $this->get_name_field( $contact_form ); |
| 33 | if ( $email ) { |
| 34 | do_action( |
| 35 | 'hostinger_reach_submit', |
| 36 | array( |
| 37 | 'group' => $contact_list, |
| 38 | 'email' => $email, |
| 39 | 'name' => $name, |
| 40 | 'metadata' => array( |
| 41 | 'plugin' => self::INTEGRATION_NAME, |
| 42 | 'form_id' => $contact_form->id(), |
| 43 | ), |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public function get_field_data( WPCF7_ContactForm $contact_form, array $condition ): string { |
| 50 | $tag = $this->find_field( $contact_form, $condition ); |
| 51 | |
| 52 | if ( ! is_null( $tag ) ) { |
| 53 | $submission = WPCF7_Submission::get_instance(); |
| 54 | |
| 55 | return $submission->get_posted_data( $tag->name ); |
| 56 | } |
| 57 | |
| 58 | return ''; |
| 59 | } |
| 60 | |
| 61 | public function find_field( WPCF7_ContactForm $contact_form, array $condition ): ?WPCF7_FormTag { |
| 62 | $tags = $contact_form->scan_form_tags( $condition ); |
| 63 | if ( ! empty( $tags ) ) { |
| 64 | return $tags[0]; |
| 65 | } |
| 66 | |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | public function get_name_field( WPCF7_ContactForm $contact_form ): string { |
| 71 | $tags = $contact_form->scan_form_tags( array( 'basetype' => 'text' ) ); |
| 72 | $name_tag = null; |
| 73 | foreach ( $tags as $tag ) { |
| 74 | if ( str_contains( $tag->name, 'name' ) ) { |
| 75 | $name_tag = $tag; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if ( ! is_null( $name_tag ) ) { |
| 81 | $submission = WPCF7_Submission::get_instance(); |
| 82 | |
| 83 | return $submission->get_posted_data( $name_tag->name ); |
| 84 | } |
| 85 | |
| 86 | return ''; |
| 87 | } |
| 88 | |
| 89 | public function get_post_type(): array { |
| 90 | return array( 'wpcf7_contact_form' ); |
| 91 | } |
| 92 | |
| 93 | public static function get_name(): string { |
| 94 | return self::INTEGRATION_NAME; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public function is_form_valid( WP_Post $post ): bool { |
| 99 | $contact_form = WPCF7_ContactForm::get_instance( $post->ID ); |
| 100 | if ( is_null( $contact_form ) ) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | return ! is_null( $this->find_field( $contact_form, array( 'basetype' => 'email' ) ) ); |
| 105 | } |
| 106 | |
| 107 | public function get_plugin_data(): PluginData { |
| 108 | return PluginData::from_array( |
| 109 | array( |
| 110 | 'id' => self::INTEGRATION_NAME, |
| 111 | 'title' => __( 'Contact Form 7', 'hostinger-reach' ), |
| 112 | 'folder' => 'contact-form-7', |
| 113 | 'file' => 'wp-contact-form-7.php', |
| 114 | 'admin_url' => 'admin.php?page=wpcf7', |
| 115 | 'add_form_url' => 'admin.php?page=wpcf7-new', |
| 116 | 'edit_url' => 'admin.php?page=wpcf7&post={form_id}&action=edit', |
| 117 | 'url' => 'https://wordpress.org/plugins/contact-form-7/', |
| 118 | 'download_url' => 'https://downloads.wordpress.org/plugin/contact-form-7.zip', |
| 119 | ) |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 |