Admin
3 weeks ago
Amplitude
4 weeks ago
Api
3 weeks ago
Blocks
1 month ago
Dto
5 months ago
Integrations
2 weeks ago
Jobs
3 months ago
Models
6 months ago
Providers
4 weeks ago
Repositories
6 months ago
Setup
1 week ago
Tracking
6 months ago
Boot.php
4 weeks ago
Container.php
9 months ago
Functions.php
3 months ago
Functions.php
207 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach; |
| 4 | |
| 5 | use Hostinger\Reach\Integrations\Elementor\SubscriptionFormElementorWidget; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | class Functions { |
| 12 | |
| 13 | public const HOSTINGER_REACH_HAS_USER_ACTION = 'hostinger_reach_has_user_action'; |
| 14 | public const HOSTINGER_REACH_HAS_FORMS = 'hostinger_reach_has_forms'; |
| 15 | |
| 16 | public const ASSET_PAGES = array( |
| 17 | 'admin.php?page=hostinger-reach', |
| 18 | 'post-new.php', |
| 19 | 'post.php', |
| 20 | 'site-editor.php', |
| 21 | ); |
| 22 | |
| 23 | public function get_frontend_dir(): string { |
| 24 | return HOSTINGER_REACH_PLUGIN_DIR . 'frontend/dist/'; |
| 25 | } |
| 26 | |
| 27 | public static function get_frontend_url(): string { |
| 28 | return HOSTINGER_REACH_PLUGIN_URL . 'frontend/dist/'; |
| 29 | } |
| 30 | |
| 31 | public function get_blocks_dir(): string { |
| 32 | return $this->get_frontend_dir() . 'blocks/'; |
| 33 | } |
| 34 | |
| 35 | public function block_file_exists( string $file_name ): bool { |
| 36 | $file = $this->get_block_file_name( $file_name ); |
| 37 | |
| 38 | return file_exists( $file ); |
| 39 | } |
| 40 | |
| 41 | public function get_block_file_name( string $file_name ): string { |
| 42 | return $this->get_blocks_dir() . $file_name; |
| 43 | } |
| 44 | |
| 45 | public function get_blocks_url(): string { |
| 46 | return Functions::get_frontend_url() . 'blocks/'; |
| 47 | } |
| 48 | |
| 49 | public function need_to_load_assets(): bool { |
| 50 | if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | $current_uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 55 | |
| 56 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | foreach ( self::ASSET_PAGES as $page ) { |
| 65 | if ( $this->is_current_uri_in_page( $current_uri, $page ) ) { |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return $this->is_single_hostinger_plugin_page( $current_uri ); |
| 71 | } |
| 72 | |
| 73 | public function is_current_uri_in_page( string $current_uri, string $page ): bool { |
| 74 | $admin_path = wp_parse_url( admin_url(), PHP_URL_PATH ); |
| 75 | return stripos( $current_uri, $admin_path . $page ) !== false; |
| 76 | } |
| 77 | |
| 78 | public function is_single_hostinger_plugin_page( string $current_uri ): bool { |
| 79 | global $submenu; |
| 80 | |
| 81 | if ( is_null( $submenu ) || ! array_key_exists( 'hostinger', $submenu ) ) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | $hostinger_menu = $submenu['hostinger']; |
| 86 | |
| 87 | foreach ( $hostinger_menu as $submenu_item_index => $submenu_item ) { |
| 88 | if ( ! in_array( 'hostinger-reach', $submenu_item, true ) ) { |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | // If Hostinger reach submenu is in position 1. Load the assets as well in Hostinger parent page. |
| 93 | if ( $submenu_item_index === 1 ) { |
| 94 | return $this->is_current_uri_in_page( $current_uri, 'admin.php?page=hostinger' ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | public function get_host_info(): string { |
| 102 | $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; |
| 103 | $site_url = get_site_url(); |
| 104 | $site_url = preg_replace( '#^https?://#', '', $site_url ); |
| 105 | $site_url = preg_replace( '/^www\./', '', $site_url ); |
| 106 | |
| 107 | if ( ! empty( $site_url ) && ! empty( $host ) && strpos( $site_url, $host ) === 0 ) { |
| 108 | if ( $site_url === $host ) { |
| 109 | return $host; |
| 110 | } else { |
| 111 | return substr( $site_url, strlen( $host ) + 1 ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $host; |
| 116 | } |
| 117 | |
| 118 | public function is_hostinger_user(): bool { |
| 119 | return ! empty( $_SERVER['H_PLATFORM'] ); |
| 120 | } |
| 121 | |
| 122 | public function is_staging(): bool { |
| 123 | return isset( $_SERVER['H_STAGING'] ) && filter_var( wp_unslash( $_SERVER['H_STAGING'] ), FILTER_VALIDATE_BOOLEAN ) === true; |
| 124 | } |
| 125 | |
| 126 | public function has_reach_subscription_block( int $post_id ): bool { |
| 127 | $content = get_post_field( 'post_content', $post_id ); |
| 128 | |
| 129 | return str_contains( $content, '<!-- wp:hostinger-reach/subscription' ); |
| 130 | } |
| 131 | |
| 132 | public function is_elementor( int $post_id ): bool { |
| 133 | return get_post_meta( $post_id, '_elementor_data', true ); |
| 134 | } |
| 135 | |
| 136 | public function has_reach_subscription_elementor_widget( int $post_id ): bool { |
| 137 | |
| 138 | if ( ! class_exists( 'Elementor\Widget_Base' ) ) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | $meta = get_post_meta( $post_id, '_elementor_data', true ); |
| 143 | if ( ! empty( $meta ) ) { |
| 144 | $meta = json_decode( $meta, true ) ?? array(); |
| 145 | $elements = $meta[0]['elements'] ?? array(); |
| 146 | } |
| 147 | |
| 148 | if ( empty( $elements ) ) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | return $this->elementor_contains_reach_widget( $elements ); |
| 153 | } |
| 154 | |
| 155 | private function elementor_contains_reach_widget( array $elements ): bool { |
| 156 | foreach ( $elements as $element ) { |
| 157 | if ( ! is_array( $element ) ) { |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | $is_widget = isset( $element['elType'] ) && $element['elType'] === 'widget'; |
| 162 | $widget_type = $element['widgetType'] ?? ''; |
| 163 | |
| 164 | if ( $is_widget && $widget_type === SubscriptionFormElementorWidget::WIDGET_NAME ) { |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | $children = isset( $element['elements'] ) && is_array( $element['elements'] ) ? $element['elements'] : array(); |
| 169 | if ( ! empty( $children ) && $this->elementor_contains_reach_widget( $children ) ) { |
| 170 | return true; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | public function has_reach_form( int $post_id ): bool { |
| 178 | return $this->has_reach_subscription_elementor_widget( $post_id ) || $this->has_reach_subscription_block( $post_id ); |
| 179 | } |
| 180 | |
| 181 | public function get_reach_subscription_blocks( int $id ): array { |
| 182 | $content = get_post_field( 'post_content', $id ); |
| 183 | $blocks = parse_blocks( $content ); |
| 184 | |
| 185 | return $this->get_parsed_blocks( $blocks ); |
| 186 | } |
| 187 | |
| 188 | public function get_parsed_blocks( array $blocks ): array { |
| 189 | $parsed_blocks = array(); |
| 190 | foreach ( $blocks as $block ) { |
| 191 | if ( $block['blockName'] === 'hostinger-reach/subscription' ) { |
| 192 | $parsed_blocks[] = $block['attrs']; |
| 193 | } |
| 194 | |
| 195 | if ( ! empty( $block['innerBlocks'] ) ) { |
| 196 | $parsed_blocks = array_merge( $parsed_blocks, $this->get_parsed_blocks( $block['innerBlocks'] ) ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return $parsed_blocks; |
| 201 | } |
| 202 | |
| 203 | public function get_asset_url( string $asset ): string { |
| 204 | return $this->get_frontend_url() . $asset; |
| 205 | } |
| 206 | } |
| 207 |