GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
Helpers/Core.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @license GPL-2.0-or-later
4  *
5  * Modified by gravityview on 13-January-2023 using Strauss.
6  * @see https://github.com/BrianHenryIE/strauss
7  */
8 
10 
11 use Closure;
12 use Exception;
13 
14 class Core {
15  /**
16  * Processes return object based on the request type (e.g., AJAX) and status.
17  *
18  * @since 1.0.0
19  *
20  * @param mixed|Exception $return_object Return object (default: 'true').
21  *
22  * @throws Exception
23  *
24  * @return void|mixed Send JSON response if an AJAX request or return the response as is.
25  */
26  public static function process_return( $return_object = true ) {
27  $is_error = $return_object instanceof Exception;
28 
29  if ( wp_doing_ajax() ) {
30  $buffer = ob_get_clean();
31 
32  if ( $buffer ) {
33  error_log( "[GravityKit] Buffer output before returning AJAX response: {$buffer}" );
34 
35  header( 'GravityKit: ' . json_encode( $buffer ) );
36  }
37 
38  if ( $is_error ) {
39  wp_send_json_error( $return_object->getMessage() );
40  } else {
41  wp_send_json_success( $return_object );
42  }
43  }
44 
45  if ( $is_error ) {
46  throw new Exception( $return_object->getMessage() );
47  }
48 
49  return $return_object;
50  }
51 
52  /**
53  * Returns path to UI assets.
54  *
55  * @since 1.0.0
56  *
57  * @param string $file (optional) File name to append to path.
58  *
59  * @return string
60  */
61  public static function get_assets_path( $file = '' ) {
62  $path = realpath( __DIR__ . '/../../assets' );
63 
64  return $file ? trailingslashit( $path ) . $file : $path;
65  }
66 
67  /**
68  * Returns URL to UI assets.
69  *
70  * @since 1.0.0
71  *
72  * @param string $file (optional) File name to append to URL.
73  *
74  * @return string
75  */
76  public static function get_assets_url( $file = '' ) {
77  $url = plugin_dir_url( self::get_assets_path() ) . 'assets';
78 
79  return $file ? trailingslashit( $url ) . $file : $url;
80  }
81 
82  /**
83  * Checks if the current page is a network admin area.
84  * The AJAX check is not to be fully relied upon as the referer can be changed.
85  *
86  * @since 1.0.0
87  *
88  * @return bool
89  */
90  public static function is_network_admin() {
91  return ! wp_doing_ajax()
93  : is_multisite() && strpos( wp_get_referer(), network_admin_url() ) !== false;
94  }
95 
96  /**
97  * Checks if the current page is a main network site, but not the network admin area.
98  *
99  * @since 1.0.4
100  *
101  * @return bool
102  */
103  public static function is_main_network_site() {
104  return is_multisite() && is_main_site() && ! self::is_network_admin();
105  }
106 
107  /**
108  * Checks if the current page is not a main network site.
109  *
110  * @since 1.0.4
111  *
112  * @return bool
113  */
114  public static function is_not_main_network_site() {
115  return is_multisite() && ! is_main_site();
116  }
117 
118  /**
119  * Wrapper for WP's get_plugins() function.
120  *
121  * @see https://github.com/WordPress/wordpress-develop/blob/2bb5679d666474d024352fa53f07344affef7e69/src/wp-admin/includes/plugin.php#L274-L411
122  *
123  * @since 1.0.0
124  *
125  * @return array[]
126  */
127  public static function get_plugins() {
128  if ( ! function_exists( 'get_plugins' ) ) {
129  require_once ABSPATH . 'wp-admin/includes/plugin.php';
130  }
131 
132  return get_plugins();
133  }
134 
135  /**
136  * Returns a list of installed products keyed by text domain.
137  *
138  * @since 1.0.0
139  * @since 1.0.4 Moved from GravityKit\Foundation\Licenses\ProductManager to GravityKit\Foundation\Helpers\Core.
140  *
141  * @return array{name:string, path: string, plugin_file:string, version: string, text_domain: string, active: bool, network_active?: bool}
142  */
143  public static function get_installed_plugins() {
144  if ( ! function_exists( 'is_plugin_active' ) ) {
145  require_once ABSPATH . 'wp-admin/includes/plugin.php';
146  }
147 
148  $plugins = [];
149 
150  foreach ( self::get_plugins() as $path => $plugin ) {
151  if ( empty( $plugin['TextDomain'] ) ) {
152  continue;
153  }
154 
155  $plugins[ $plugin['TextDomain'] ] = array(
156  'name' => $plugin['Name'],
157  'path' => $path,
158  'plugin_file' => file_exists( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $path ) ? WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $path : null,
159  'version' => $plugin['Version'],
160  'text_domain' => $plugin['TextDomain'],
161  'active' => is_plugin_active( $path ),
162  'network_activated' => is_plugin_active_for_network( $path ),
163  );
164  }
165 
166  return $plugins;
167  }
168 
169  /**
170  * Searches installed plugin by text domain(s) and returns its data.
171  *
172  * @since 1.0.0
173  * @since 1.0.4 Moved from GravityKit\Foundation\Licenses\ProductManager to GravityKit\Foundation\Helpers\Core.
174  *
175  * @param string $text_domains_str Text domain(s). Optionally pipe-separated (e.g. 'gravityview|gk-gravtiyview').
176  *
177  * @return array|null An array with plugin data or null if not installed.
178  */
179  public static function get_installed_plugin_by_text_domain( $text_domains_str ) {
180  $installed_plugins = self::get_installed_plugins();
181 
182  $text_domains_arr = explode( '|', $text_domains_str );
183 
184  foreach ( $text_domains_arr as $text_domain ) {
185  if ( isset( $installed_plugins[ $text_domain ] ) ) {
186  return $installed_plugins[ $text_domain ];
187  }
188  }
189 
190  return null;
191  }
192 
193  /**
194  * Wrapper for WP's get_plugin_data() function.
195  *
196  * @see https://github.com/WordPress/wordpress-develop/blob/2bb5679d666474d024352fa53f07344affef7e69/src/wp-admin/includes/plugin.php#L72-L118
197  *
198  * @since 1.0.0
199  *
200  * @param string $plugin_file Absolute path to the main plugin file.
201  * @param bool $markup (optional) If the returned data should have HTML markup applied. Default is true.
202  * @param bool $translate (optional) If the returned data should be translated. Default is true.
203  *
204  * @return array[]
205  */
206  public static function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
207  if ( ! function_exists( 'get_plugin_data' ) ) {
208  require_once ABSPATH . 'wp-admin/includes/plugin.php';
209  }
210 
211  return get_plugin_data( $plugin_file, $markup, $translate );
212  }
213 
214  /**
215  * Checks if value is a callable function.
216  *
217  * @since 1.0.0
218  *
219  * @param string|mixed $value Value to check.
220  *
221  * @return bool
222  */
223  public static function is_callable_function( $value ) {
224  return ( is_string( $value ) && function_exists( $value ) ) ||
225  ( is_object( $value ) && ( $value instanceof Closure ) );
226  }
227 
228  /**
229  * Checks if value is a callable class method.
230  *
231  * @since 1.0.0
232  *
233  * @param array|mixed $value Value to check.
234  *
235  * @return bool
236  */
237  public static function is_callable_class_method( $value ) {
238  if ( ! is_array( $value ) || count( $value ) !== 2 ) {
239  return false;
240  }
241 
242  $value = array_values( $value );
243 
244  return ( is_object( $value[0] ) || is_string( $value[0] ) ) &&
245  method_exists( $value[0], $value[1] );
246  }
247 }
$url
Definition: post_image.php:25
static get_assets_url( $file='')
Returns URL to UI assets.
static get_plugin_data( $plugin_file, $markup=true, $translate=true)
Wrapper for WP&#39;s get_plugin_data() function.
static is_main_network_site()
Checks if the current page is a main network site, but not the network admin area.
static get_installed_plugin_by_text_domain( $text_domains_str)
Searches installed plugin by text domain(s) and returns its data.
static get_installed_plugins()
Returns a list of installed products keyed by text domain.
static is_callable_function( $value)
Checks if value is a callable function.
static is_not_main_network_site()
Checks if the current page is not a main network site.
static is_network_admin()
Checks if the current page is a network admin area.
static get_assets_path( $file='')
Returns path to UI assets.
static process_return( $return_object=true)
Processes return object based on the request type (e.g., AJAX) and status.
static is_callable_class_method( $value)
Checks if value is a callable class method.
static get_plugins()
Wrapper for WP&#39;s get_plugins() function.