ReachFormIntegration.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations\Reach; |
| 4 | |
| 5 | use Hostinger\Reach\Functions; |
| 6 | use Hostinger\Reach\Integrations\IntegrationInterface; |
| 7 | use Hostinger\Reach\Integrations\IntegrationWithForms; |
| 8 | use Hostinger\Reach\Dto\PluginData; |
| 9 | use Hostinger\Reach\Repositories\ContactListRepository; |
| 10 | use Hostinger\Reach\Repositories\FormRepository; |
| 11 | use WP_Post; |
| 12 | |
| 13 | if ( ! DEFINED( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | class ReachFormIntegration extends IntegrationWithForms implements IntegrationInterface { |
| 18 | |
| 19 | public const INTEGRATION_NAME = 'hostinger-reach'; |
| 20 | |
| 21 | protected ContactListRepository $contact_list_repository; |
| 22 | protected Functions $functions; |
| 23 | |
| 24 | public function __construct( FormRepository $form_repository, ContactListRepository $contact_list_repository, Functions $functions ) { |
| 25 | parent::__construct( $form_repository ); |
| 26 | $this->contact_list_repository = $contact_list_repository; |
| 27 | $this->functions = $functions; |
| 28 | } |
| 29 | |
| 30 | public function init(): void { |
| 31 | parent::init(); |
| 32 | $this->init_default_forms(); |
| 33 | } |
| 34 | |
| 35 | public function active_integration_hooks(): void { |
| 36 | add_action( 'transition_post_status', array( $this, 'handle_transition_post_status' ), 10, 3 ); |
| 37 | add_action( 'hostinger_reach_contact_submitted', array( $this, 'handle_submission' ) ); |
| 38 | } |
| 39 | |
| 40 | public function init_default_forms(): void { |
| 41 | $form_ids = apply_filters( 'hostinger_reach_default_forms', array() ); |
| 42 | |
| 43 | foreach ( $form_ids as $form_id ) { |
| 44 | if ( ! $this->form_repository->exists( $form_id ) ) { |
| 45 | $this->form_repository->insert( |
| 46 | array( |
| 47 | 'form_id' => $form_id, |
| 48 | ) |
| 49 | ); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function handle_transition_post_status( string $new_status, string $old_status, WP_Post $post ): void { |
| 55 | if ( $new_status === 'publish' ) { |
| 56 | $this->set_forms( $post ); |
| 57 | $this->maybe_unset_forms( $post ); |
| 58 | } elseif ( $old_status === 'publish' ) { |
| 59 | $this->unset_all_forms( $post ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public function handle_submission( array $data ): void { |
| 64 | $this->form_repository->submit( $data ); |
| 65 | } |
| 66 | |
| 67 | public function get_plugin_data(): PluginData { |
| 68 | return PluginData::from_array( |
| 69 | array( |
| 70 | 'id' => self::INTEGRATION_NAME, |
| 71 | 'title' => __( 'Hostinger Reach', 'hostinger-reach' ), |
| 72 | 'admin_url' => 'admin.php?page=hostinger-reach', |
| 73 | 'add_form_url' => 'post-new.php?post_type=page&hostinger_reach_add_block=1', |
| 74 | 'edit_url' => 'post.php?post={post_id}&action=edit#block-{form_id}', |
| 75 | 'url' => 'https://wordpress.org/plugins/hostinger-reach', |
| 76 | 'is_view_form_hidden' => false, |
| 77 | 'can_toggle_forms' => false, |
| 78 | 'is_active' => true, |
| 79 | ) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | public static function get_name(): string { |
| 84 | return self::INTEGRATION_NAME; |
| 85 | } |
| 86 | |
| 87 | public function get_form_ids( WP_Post $post ): array { |
| 88 | $blocks = $this->functions->get_reach_subscription_blocks( $post->ID ); |
| 89 | $current_form_ids = array(); |
| 90 | |
| 91 | foreach ( $blocks as $block ) { |
| 92 | if ( empty( $block['formId'] ) ) { |
| 93 | continue; |
| 94 | } |
| 95 | $current_form_ids[] = $block['formId']; |
| 96 | } |
| 97 | |
| 98 | return $current_form_ids; |
| 99 | } |
| 100 | |
| 101 | private function set_contact_list( string $name ): array { |
| 102 | if ( ! $this->contact_list_repository->exists( $name ) ) { |
| 103 | $this->contact_list_repository->insert( array( 'name' => $name ) ); |
| 104 | } |
| 105 | |
| 106 | return $this->contact_list_repository->get( $name ); |
| 107 | } |
| 108 | |
| 109 | private function set_forms( WP_Post $post ): void { |
| 110 | $blocks = $this->functions->get_reach_subscription_blocks( $post->ID ); |
| 111 | if ( ! empty( $blocks ) ) { |
| 112 | update_option( Functions::HOSTINGER_REACH_HAS_FORMS, true ); |
| 113 | } |
| 114 | |
| 115 | foreach ( $blocks as $block ) { |
| 116 | if ( empty( $block['formId'] ) ) { |
| 117 | continue; |
| 118 | } |
| 119 | $contact_list = $this->set_contact_list( $block['contactList'] ?? HOSTINGER_REACH_DEFAULT_CONTACT_LIST ); |
| 120 | $form = array( |
| 121 | 'form_id' => $block['formId'], |
| 122 | 'contact_list_id' => $contact_list['id'], |
| 123 | 'type' => self::INTEGRATION_NAME, |
| 124 | ); |
| 125 | |
| 126 | if ( $this->form_repository->exists( $block['formId'] ) ) { |
| 127 | $this->form_repository->update( $form ); |
| 128 | } else { |
| 129 | $this->form_repository->insert( array_merge( $form, array( 'post_id' => $post->ID ) ) ); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 |