| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger\Reach; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
die; |
| 7 |
} |
| 8 |
|
| 9 |
class Functions { |
| 10 |
public const ASSET_PAGES = array( |
| 11 |
'admin.php?page=hostinger-reach', |
| 12 |
'post-new.php', |
| 13 |
'post.php', |
| 14 |
'site-editor.php', |
| 15 |
); |
| 16 |
|
| 17 |
public function get_frontend_dir(): string { |
| 18 |
return HOSTINGER_REACH_PLUGIN_DIR . 'frontend/dist/'; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_frontend_url(): string { |
| 22 |
return HOSTINGER_REACH_PLUGIN_URL . 'frontend/dist/'; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_blocks_dir(): string { |
| 26 |
return $this->get_frontend_dir() . 'blocks/'; |
| 27 |
} |
| 28 |
|
| 29 |
public function block_file_exists( string $file_name ): bool { |
| 30 |
$file = $this->get_block_file_name( $file_name ); |
| 31 |
|
| 32 |
return file_exists( $file ); |
| 33 |
} |
| 34 |
|
| 35 |
public function get_block_file_name( string $file_name ): string { |
| 36 |
return $this->get_blocks_dir() . $file_name; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_blocks_url(): string { |
| 40 |
return $this->get_frontend_url() . 'blocks/'; |
| 41 |
} |
| 42 |
|
| 43 |
public function need_to_load_assets(): bool { |
| 44 |
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
$current_uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 49 |
|
| 50 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
foreach ( self::ASSET_PAGES as $page ) { |
| 59 |
if ( $this->is_current_uri_in_page( $current_uri, $page ) ) { |
| 60 |
return true; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $this->is_single_hostinger_plugin_page( $current_uri ); |
| 65 |
} |
| 66 |
|
| 67 |
public function is_current_uri_in_page( string $current_uri, string $page ): bool { |
| 68 |
$admin_path = wp_parse_url( admin_url(), PHP_URL_PATH ); |
| 69 |
return stripos( $current_uri, $admin_path . $page ) !== false; |
| 70 |
} |
| 71 |
|
| 72 |
public function is_single_hostinger_plugin_page( string $current_uri ): bool { |
| 73 |
global $submenu; |
| 74 |
|
| 75 |
if ( is_null( $submenu ) || ! array_key_exists( 'hostinger', $submenu ) ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
$hostinger_menu = $submenu['hostinger']; |
| 80 |
|
| 81 |
foreach ( $hostinger_menu as $submenu_item_index => $submenu_item ) { |
| 82 |
if ( ! in_array( 'hostinger-reach', $submenu_item, true ) ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
// If Hostinger reach submenu is in position 1. Load the assets as well in Hostinger parent page. |
| 87 |
if ( $submenu_item_index === 1 ) { |
| 88 |
return $this->is_current_uri_in_page( $current_uri, 'admin.php?page=hostinger' ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
public function get_host_info(): string { |
| 96 |
$host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; |
| 97 |
$site_url = get_site_url(); |
| 98 |
$site_url = preg_replace( '#^https?://#', '', $site_url ); |
| 99 |
$site_url = preg_replace( '/^www\./', '', $site_url ); |
| 100 |
|
| 101 |
if ( ! empty( $site_url ) && ! empty( $host ) && strpos( $site_url, $host ) === 0 ) { |
| 102 |
if ( $site_url === $host ) { |
| 103 |
return $host; |
| 104 |
} else { |
| 105 |
return substr( $site_url, strlen( $host ) + 1 ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return $host; |
| 110 |
} |
| 111 |
|
| 112 |
public function is_hostinger_user(): bool { |
| 113 |
return ! empty( $_SERVER['H_PLATFORM'] ); |
| 114 |
} |
| 115 |
|
| 116 |
public function is_staging(): bool { |
| 117 |
return isset( $_SERVER['H_STAGING'] ) && filter_var( wp_unslash( $_SERVER['H_STAGING'] ), FILTER_VALIDATE_BOOLEAN ) === true; |
| 118 |
} |
| 119 |
|
| 120 |
public function has_reach_subscription_block( int $id ): bool { |
| 121 |
$content = get_post_field( 'post_content', $id ); |
| 122 |
|
| 123 |
return str_contains( $content, '<!-- wp:hostinger-reach/subscription' ); |
| 124 |
} |
| 125 |
|
| 126 |
public function get_reach_subscription_blocks( int $id ): array { |
| 127 |
$content = get_post_field( 'post_content', $id ); |
| 128 |
$blocks = parse_blocks( $content ); |
| 129 |
|
| 130 |
return $this->get_parsed_blocks( $blocks ); |
| 131 |
} |
| 132 |
|
| 133 |
public function get_parsed_blocks( array $blocks ): array { |
| 134 |
$parsed_blocks = array(); |
| 135 |
foreach ( $blocks as $block ) { |
| 136 |
if ( $block['blockName'] === 'hostinger-reach/subscription' ) { |
| 137 |
$parsed_blocks[] = $block['attrs']; |
| 138 |
} |
| 139 |
|
| 140 |
if ( ! empty( $block['innerBlocks'] ) ) { |
| 141 |
$parsed_blocks = array_merge( $parsed_blocks, $this->get_parsed_blocks( $block['innerBlocks'] ) ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return $parsed_blocks; |
| 146 |
} |
| 147 |
} |
| 148 |
|