PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.8.0
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.8.0
1.8.0 1.7.2 1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / src / Integrations / Reach / ReachFormIntegration.php
hostinger-reach / src / Integrations / Reach Last commit date
ReachFormBuilder.php 1 week ago ReachFormIntegration.php 1 week ago
ReachFormIntegration.php
137 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 protected ReachFormBuilder $form_builder;
24
25 public function __construct( FormRepository $form_repository, ContactListRepository $contact_list_repository, Functions $functions, ReachFormBuilder $form_builder ) {
26 parent::__construct( $form_repository );
27 $this->contact_list_repository = $contact_list_repository;
28 $this->functions = $functions;
29 $this->form_builder = $form_builder;
30 }
31
32 public function init(): void {
33 parent::init();
34 $this->init_default_forms();
35 $this->form_builder->init();
36 }
37
38 public function active_integration_hooks(): void {
39 add_action( 'transition_post_status', array( $this, 'handle_transition_post_status' ), 10, 3 );
40 add_action( 'hostinger_reach_contact_submitted', array( $this, 'handle_submission' ) );
41 }
42
43 public function init_default_forms(): void {
44 $form_ids = apply_filters( 'hostinger_reach_default_forms', array() );
45
46 foreach ( $form_ids as $form_id ) {
47 if ( ! $this->form_repository->exists( $form_id ) ) {
48 $this->form_repository->insert(
49 array(
50 'form_id' => $form_id,
51 )
52 );
53 }
54 }
55 }
56
57 public function handle_transition_post_status( string $new_status, string $old_status, WP_Post $post ): void {
58 if ( $new_status === 'publish' ) {
59 $this->set_forms( $post );
60 $this->maybe_unset_forms( $post );
61 } elseif ( $old_status === 'publish' ) {
62 $this->unset_all_forms( $post );
63 }
64 }
65
66 public function handle_submission( array $data ): void {
67 $this->form_repository->submit( $data );
68 }
69
70 public function get_plugin_data(): PluginData {
71 return PluginData::from_array(
72 array(
73 'id' => self::INTEGRATION_NAME,
74 'title' => __( 'Hostinger Reach', 'hostinger-reach' ),
75 'admin_url' => 'admin.php?page=hostinger-reach',
76 'add_form_url' => 'post-new.php?post_type=page&hostinger_reach_add_block=1',
77 'edit_url' => 'post.php?post={post_id}&action=edit#block-{form_id}',
78 'url' => 'https://wordpress.org/plugins/hostinger-reach',
79 'is_view_form_hidden' => false,
80 'can_toggle_forms' => false,
81 'is_active' => true,
82 )
83 );
84 }
85
86 public static function get_name(): string {
87 return self::INTEGRATION_NAME;
88 }
89
90 public function get_form_ids( WP_Post $post ): array {
91 $blocks = $this->functions->get_reach_subscription_blocks( $post->ID );
92 $current_form_ids = array();
93
94 foreach ( $blocks as $block ) {
95 if ( empty( $block['formId'] ) ) {
96 continue;
97 }
98 $current_form_ids[] = $block['formId'];
99 }
100
101 return $current_form_ids;
102 }
103
104 private function set_contact_list( string $name ): array {
105 if ( ! $this->contact_list_repository->exists( $name ) ) {
106 $this->contact_list_repository->insert( array( 'name' => $name ) );
107 }
108
109 return $this->contact_list_repository->get( $name );
110 }
111
112 private function set_forms( WP_Post $post ): void {
113 $blocks = $this->functions->get_reach_subscription_blocks( $post->ID );
114 if ( ! empty( $blocks ) ) {
115 update_option( Functions::HOSTINGER_REACH_HAS_FORMS, true );
116 }
117
118 foreach ( $blocks as $block ) {
119 if ( empty( $block['formId'] ) ) {
120 continue;
121 }
122 $contact_list = $this->set_contact_list( $block['contactList'] ?? HOSTINGER_REACH_DEFAULT_CONTACT_LIST );
123 $form = array(
124 'form_id' => $block['formId'],
125 'contact_list_id' => $contact_list['id'],
126 'type' => self::INTEGRATION_NAME,
127 );
128
129 if ( $this->form_repository->exists( $block['formId'] ) ) {
130 $this->form_repository->update( $form );
131 } else {
132 $this->form_repository->insert( array_merge( $form, array( 'post_id' => $post->ID ) ) );
133 }
134 }
135 }
136 }
137