BraveIntegration.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations\Brave; |
| 4 | |
| 5 | use Hostinger\Reach\Dto\PluginData; |
| 6 | use Hostinger\Reach\Integrations\Integration; |
| 7 | use Hostinger\Reach\Integrations\IntegrationInterface; |
| 8 | |
| 9 | if ( ! DEFINED( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | class BraveIntegration extends Integration implements IntegrationInterface { |
| 14 | |
| 15 | public const INTEGRATION_NAME = 'brave-popup-builder'; |
| 16 | |
| 17 | public function get_post_type(): array { |
| 18 | return array( 'popup' ); |
| 19 | } |
| 20 | |
| 21 | public static function get_name(): string { |
| 22 | return self::INTEGRATION_NAME; |
| 23 | } |
| 24 | |
| 25 | public function get_plugin_data(): PluginData { |
| 26 | return PluginData::from_array( |
| 27 | array( |
| 28 | 'id' => self::INTEGRATION_NAME, |
| 29 | 'title' => __( 'Brave Popup Builder', 'hostinger-reach' ), |
| 30 | 'folder' => 'brave-popup-builder', |
| 31 | 'file' => 'index.php', |
| 32 | 'admin_url' => 'admin.php?page=bravepop-submissions', |
| 33 | 'add_form_url' => 'admin.php?page=bravepop', |
| 34 | 'edit_url' => 'admin.php?page=bravepop&id={post_id}', |
| 35 | 'url' => 'https://wordpress.org/plugins/brave-popup-builder/', |
| 36 | 'download_url' => 'https://downloads.wordpress.org/plugin/brave-popup-builder.zip', |
| 37 | 'icon' => 'https://ps.w.org/brave-popup-builder/assets/icon-256x256.gif', |
| 38 | 'is_view_form_hidden' => true, |
| 39 | 'can_toggle_forms' => true, |
| 40 | ) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function active_integration_hooks(): void { |
| 45 | add_action( 'bravepop_user_submitted_form', array( $this, 'handle_form_submission' ), 10, 2 ); |
| 46 | } |
| 47 | |
| 48 | public function handle_form_submission( string $popup_id, array $form_settings ): void { |
| 49 | if ( ! $this->is_form_enabled( $popup_id ) ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $post = get_post( $popup_id ); |
| 54 | $email = $this->find_email( $form_settings ); |
| 55 | if ( $email ) { |
| 56 | do_action( |
| 57 | 'hostinger_reach_submit', |
| 58 | array( |
| 59 | 'group' => $post ? $post->post_title : self::INTEGRATION_NAME, |
| 60 | 'email' => $email, |
| 61 | 'metadata' => array( |
| 62 | 'plugin' => self::INTEGRATION_NAME, |
| 63 | 'form_id' => $popup_id, |
| 64 | ), |
| 65 | ) |
| 66 | ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private function find_email( array $form_settings ): string { |
| 71 | if ( ! isset( $form_settings['fields'] ) ) { |
| 72 | return ''; |
| 73 | } |
| 74 | |
| 75 | foreach ( $form_settings['fields'] as $field ) { |
| 76 | $value = $field?->value ?? ''; |
| 77 | $sanitized_value = sanitize_email( $value ); |
| 78 | if ( filter_var( $sanitized_value, FILTER_VALIDATE_EMAIL ) ) { |
| 79 | return $value; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return ''; |
| 84 | } |
| 85 | } |
| 86 |