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