| 1 |
<?php |
| 2 |
namespace HelloPlus\Includes; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* class Utils |
| 10 |
**/ |
| 11 |
class Utils { |
| 12 |
|
| 13 |
public static function elementor(): ?\Elementor\Plugin { |
| 14 |
return class_exists( '\Elementor\Plugin' ) ? \Elementor\Plugin::instance() : null; |
| 15 |
} |
| 16 |
|
| 17 |
public static function has_pro(): bool { |
| 18 |
return defined( 'ELEMENTOR_PRO_VERSION' ); |
| 19 |
} |
| 20 |
|
| 21 |
public static function are_we_on_elementor_domains(): bool { |
| 22 |
$current_domain = filter_input( INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_URL ); |
| 23 |
$allowed_domains = [ |
| 24 |
'elementor.com', |
| 25 |
'elementor.red', |
| 26 |
]; |
| 27 |
|
| 28 |
foreach ( $allowed_domains as $domain ) { |
| 29 |
if ( str_ends_with( $current_domain, $domain ) ) { |
| 30 |
return true; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
public static function has_hello_biz(): bool { |
| 38 |
if ( defined( 'WP_TESTS_DOMAIN' ) ) { |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
if ( self::are_we_on_elementor_domains() ) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
return defined( 'EHP_THEME_SLUG' ); |
| 47 |
} |
| 48 |
|
| 49 |
public static function has_hello_elementor_theme(): bool { |
| 50 |
return defined( 'EHP_THEME_SLUG' ) && ( 'hello-elementor' === EHP_THEME_SLUG ); |
| 51 |
} |
| 52 |
|
| 53 |
public static function is_elementor_active(): bool { |
| 54 |
static $elementor_active = null; |
| 55 |
if ( is_null( $elementor_active ) ) { |
| 56 |
$elementor_active = defined( 'ELEMENTOR_VERSION' ); |
| 57 |
} |
| 58 |
|
| 59 |
return $elementor_active; |
| 60 |
} |
| 61 |
|
| 62 |
public static function is_elementor_installed(): bool { |
| 63 |
static $elementor_installed = null; |
| 64 |
if ( is_null( $elementor_installed ) ) { |
| 65 |
$elementor_installed = file_exists( WP_PLUGIN_DIR . '/elementor/elementor.php' ); |
| 66 |
} |
| 67 |
|
| 68 |
return $elementor_installed; |
| 69 |
} |
| 70 |
|
| 71 |
public static function get_current_post_id(): int { |
| 72 |
if ( self::elementor() && isset( self::elementor()->documents ) && self::elementor()->documents->get_current() ) { |
| 73 |
return self::elementor()->documents->get_current()->get_main_id(); |
| 74 |
} |
| 75 |
|
| 76 |
return get_the_ID(); |
| 77 |
} |
| 78 |
|
| 79 |
public static function get_update_elementor_message(): string { |
| 80 |
return sprintf( |
| 81 |
/* translators: %s: Elementor version number. */ |
| 82 |
__( 'Elementor plugin version needs to be at least %s for Hello Plus to Work. Please update.', 'hello-plus' ), |
| 83 |
HELLOPLUS_MIN_ELEMENTOR_VERSION, |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
public static function get_client_ip(): string { |
| 88 |
$server_ip_keys = [ |
| 89 |
'HTTP_CLIENT_IP', |
| 90 |
'HTTP_X_FORWARDED_FOR', |
| 91 |
'HTTP_X_FORWARDED', |
| 92 |
'HTTP_X_CLUSTER_CLIENT_IP', |
| 93 |
'HTTP_FORWARDED_FOR', |
| 94 |
'HTTP_FORWARDED', |
| 95 |
'REMOTE_ADDR', |
| 96 |
]; |
| 97 |
|
| 98 |
foreach ( $server_ip_keys as $key ) { |
| 99 |
$value = filter_input( INPUT_SERVER, $key, FILTER_VALIDATE_IP ); |
| 100 |
|
| 101 |
if ( $value ) { |
| 102 |
return $value; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return '127.0.0.1'; |
| 107 |
} |
| 108 |
|
| 109 |
public static function ends_with( $full_string, $end_string ): bool { |
| 110 |
$len = strlen( $end_string ); |
| 111 |
if ( 0 === $len ) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
return ( substr( $full_string, -$len ) === $end_string ); |
| 116 |
} |
| 117 |
|
| 118 |
public static function get_theme_slug(): string { |
| 119 |
if ( defined( 'EHP_THEME_SLUG' ) ) { |
| 120 |
return EHP_THEME_SLUG; |
| 121 |
} |
| 122 |
|
| 123 |
return 'hello-plus'; |
| 124 |
} |
| 125 |
|
| 126 |
public static function get_theme_admin_home(): string { |
| 127 |
if ( defined( 'EHP_THEME_SLUG' ) ) { |
| 128 |
return add_query_arg( [ 'page' => EHP_THEME_SLUG ], self_admin_url( 'edit.php' ) ); |
| 129 |
} |
| 130 |
|
| 131 |
return self_admin_url(); |
| 132 |
} |
| 133 |
|
| 134 |
public static function is_preview_for_document( $post_id ): bool { |
| 135 |
$preview_id = filter_input( INPUT_GET, 'preview_id', FILTER_VALIDATE_INT ); |
| 136 |
$preview = filter_input( INPUT_GET, 'preview', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 137 |
|
| 138 |
return 'true' === $preview && (int) $post_id === (int) $preview_id; |
| 139 |
} |
| 140 |
|
| 141 |
public static function is_installed_elementor_version_supported(): bool { |
| 142 |
$plugin_file = WP_PLUGIN_DIR . '/elementor/elementor.php'; |
| 143 |
|
| 144 |
if ( ! file_exists( $plugin_file ) ) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 149 |
|
| 150 |
$plugin_data = get_plugin_data( $plugin_file ); |
| 151 |
$plugin_version = $plugin_data['Version']; |
| 152 |
|
| 153 |
return self::is_elementor_version_supported( $plugin_version ); |
| 154 |
} |
| 155 |
|
| 156 |
public static function is_active_elementor_version_supported(): bool { |
| 157 |
return self::is_elementor_version_supported( ELEMENTOR_VERSION ); |
| 158 |
} |
| 159 |
|
| 160 |
public static function is_elementor_version_supported( string $version ): bool { |
| 161 |
return version_compare( $version, HELLOPLUS_MIN_ELEMENTOR_VERSION, 'ge' ); |
| 162 |
} |
| 163 |
|
| 164 |
public static function plugin_title(): string { |
| 165 |
return __( 'Hello+', 'hello-plus' ); |
| 166 |
} |
| 167 |
|
| 168 |
public static function are_submissions_enabled(): bool { |
| 169 |
return static::has_pro() && |
| 170 |
class_exists( '\ElementorPro\Modules\Forms\Submissions\Actions\Save_To_Database' ) && |
| 171 |
class_exists( '\ElementorPro\License\API' ) && |
| 172 |
class_exists( '\ElementorPro\Modules\Forms\Submissions\Component' ) && |
| 173 |
self::is_licence_has_feature_compatible( |
| 174 |
\ElementorPro\Modules\Forms\Submissions\Component::NAME |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
private static function is_licence_has_feature_compatible( $feature ) { |
| 179 |
$method = new \ReflectionMethod( |
| 180 |
\ElementorPro\License\API::class, 'is_licence_has_feature' |
| 181 |
); |
| 182 |
$num_params = $method->getNumberOfParameters(); |
| 183 |
|
| 184 |
if ( $num_params > 1 ) { |
| 185 |
return \ElementorPro\License\API::is_licence_has_feature( |
| 186 |
$feature, |
| 187 |
\ElementorPro\License\API::BC_VALIDATION_CALLBACK |
| 188 |
); |
| 189 |
} else { |
| 190 |
return \ElementorPro\License\API::is_licence_has_feature( |
| 191 |
$feature |
| 192 |
); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
public static function get_widgets_depends(): array { |
| 197 |
return [ 'helloplus-button', 'helloplus-image', 'helloplus-shapes', 'helloplus-column-structure' ]; |
| 198 |
} |
| 199 |
|
| 200 |
public static function maybe_has_pro_location_docs( string $location = '' ): array { |
| 201 |
if ( ! self::has_pro() ) { |
| 202 |
return []; |
| 203 |
} |
| 204 |
$theme_builder_module = \ElementorPro\Modules\ThemeBuilder\Module::instance(); |
| 205 |
$conditions_manager = $theme_builder_module->get_conditions_manager(); |
| 206 |
|
| 207 |
return $conditions_manager->get_documents_for_location( $location ); |
| 208 |
} |
| 209 |
|
| 210 |
public static function get_pro_part( string $part = '' ) { |
| 211 |
if ( ! self::has_pro() ) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
$pro_part = self::maybe_has_pro_location_docs( $part ); |
| 216 |
$first_pro_part_id = array_key_first( $pro_part ); |
| 217 |
return ! empty( $first_pro_part_id ) ? $first_pro_part_id : false; |
| 218 |
} |
| 219 |
|
| 220 |
public static function is_test_environment(): bool { |
| 221 |
|
| 222 |
if ( defined( 'WP_TESTS_DOMAIN' ) ) { |
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
if ( getenv( 'TEST_PARALLEL_INDEX' ) !== false ) { |
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
$wp_env = getenv( 'WP_ENV' ); |
| 231 |
if ( $wp_env && in_array( strtolower( $wp_env ), [ 'test', 'testing', 'playwright' ], true ) ) { |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
if ( defined( 'WP_TESTS_CONFIG_FILE_PATH' ) || defined( 'WP_PHPUNIT__TESTS_CONFIG' ) ) { |
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
if ( |
| 240 |
defined( 'WP_DEBUG' ) && |
| 241 |
defined( 'WP_DEBUG_LOG' ) && |
| 242 |
filter_input( INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_URL ) === 'localhost:8888' |
| 243 |
) { |
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
return false; |
| 248 |
} |
| 249 |
} |
| 250 |
|