PluginProbe
Hello Plus / 1.7.4
Hello Plus v1.7.4
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / includes / utils.php

utils.php in Hello Plus 1.7.4, at includes/utils.php

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