attributes-trait.php
5 years ago
condition-helper.php
5 years ago
curl-helper.php
5 years ago
factory.php
5 years ago
gallery.php
5 years ago
get-icon-trait.php
5 years ago
get-template-trait.php
5 years ago
instance-trait.php
5 years ago
listing-filter-manager.php
5 years ago
listing-filter.php
5 years ago
messages-helper-trait.php
5 years ago
tools.php
5 years ago
tools.php
381 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Classes; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | use Jet_Form_Builder\Plugin; |
| 7 | |
| 8 | if ( ! defined( 'WPINC' ) ) { |
| 9 | die; |
| 10 | } |
| 11 | |
| 12 | |
| 13 | class Tools { |
| 14 | |
| 15 | const EMPTY_DEEP_VALUE = self::class; |
| 16 | |
| 17 | public static function is_editor() { |
| 18 | return self::is_block_editor() || self::is_elementor_editor(); |
| 19 | } |
| 20 | |
| 21 | public static function is_block_editor() { |
| 22 | $action = ! empty( $_GET['context'] ) ? $_GET['context'] : ''; |
| 23 | |
| 24 | if ( isset( $_GET['action'] ) ) { |
| 25 | $action = $action ? $action : $_GET['action']; |
| 26 | } |
| 27 | |
| 28 | return in_array( $action, array( 'add', 'edit' ) ); |
| 29 | } |
| 30 | |
| 31 | public static function is_elementor_editor() { |
| 32 | if ( ! defined( 'ELEMENTOR_VERSION' ) ) { |
| 33 | return false; |
| 34 | } |
| 35 | return ( \Elementor\Plugin::instance()->editor->is_edit_mode() ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns all post types list to use in JS components |
| 40 | * |
| 41 | * @param bool $placeholder |
| 42 | * |
| 43 | * @param array $args |
| 44 | * @param string $operator |
| 45 | * |
| 46 | * @return array [type] [description] |
| 47 | */ |
| 48 | public static function get_post_types_for_js( $placeholder = false, $args = array(), $operator = 'and' ) { |
| 49 | |
| 50 | $post_types = get_post_types( $args, 'objects', $operator ); |
| 51 | |
| 52 | $post_types_list = array(); |
| 53 | |
| 54 | if ( $placeholder && is_array( $placeholder ) ) { |
| 55 | $placeholder['value'] = isset( $placeholder['value'] ) ? $placeholder['value'] : ''; |
| 56 | $post_types_list[] = $placeholder; |
| 57 | } |
| 58 | |
| 59 | foreach ( $post_types as $post_type ) { |
| 60 | if ( $post_type->name !== Plugin::instance()->post_type->slug() ) { |
| 61 | $post_types_list[] = array( |
| 62 | 'value' => $post_type->name, |
| 63 | 'label' => $post_type->label, |
| 64 | ); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return self::with_placeholder( $post_types_list ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get post types list for options. |
| 73 | * |
| 74 | * @return array |
| 75 | */ |
| 76 | public static function get_post_types_for_options() { |
| 77 | return self::get_post_types_for_js( false, array( 'public' => true ) ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sanitize WYSIWYG field |
| 82 | * |
| 83 | * @param $input |
| 84 | * |
| 85 | * @return string |
| 86 | */ |
| 87 | public static function sanitize_wysiwyg( $input ) { |
| 88 | $input = wpautop( $input ); |
| 89 | |
| 90 | return wp_kses_post( $input ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return all taxonomies list to use in JS components |
| 95 | * |
| 96 | * @return [type] [description] |
| 97 | */ |
| 98 | public static function get_taxonomies_for_js() { |
| 99 | $taxonomies = get_taxonomies( array(), 'objects' ); |
| 100 | |
| 101 | return self::with_placeholder( self::prepare_list_for_js( $taxonomies, 'name', 'label' ) ); |
| 102 | } |
| 103 | |
| 104 | public static function get_generators_list_for_js() { |
| 105 | $generators = Plugin::instance()->form->get_generators_list(); |
| 106 | |
| 107 | return self::prepare_list_for_js( $generators ); |
| 108 | } |
| 109 | |
| 110 | public static function get_allowed_mimes_list_for_js() { |
| 111 | $mimes = get_allowed_mime_types(); |
| 112 | |
| 113 | $mimes_list = array(); |
| 114 | foreach ( $mimes as $mime ) { |
| 115 | $mimes_list[] = array( |
| 116 | 'label' => $mime, |
| 117 | 'value' => $mime |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | return $mimes_list; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns all registeredroles for JS |
| 126 | */ |
| 127 | public static function get_user_roles_for_js() { |
| 128 | |
| 129 | $roles = self::get_user_roles(); |
| 130 | $result = array(); |
| 131 | |
| 132 | foreach ( $roles as $role => $label ) { |
| 133 | $result[] = array( |
| 134 | 'value' => $role, |
| 135 | 'label' => $label, |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | return self::with_placeholder( $result ); |
| 140 | } |
| 141 | |
| 142 | public static function get_options_pages_for_js() { |
| 143 | $pages = array(); |
| 144 | |
| 145 | if ( function_exists( 'jet_engine' ) ) { |
| 146 | $pages = jet_engine()->options_pages->get_options_pages_for_select(); |
| 147 | } |
| 148 | |
| 149 | return self::prepare_list_for_js( $pages ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns pages list |
| 154 | * @return [type] [description] |
| 155 | */ |
| 156 | public static function get_pages_list_for_js() { |
| 157 | $pages = get_pages(); |
| 158 | |
| 159 | return self::prepare_list_for_js( $pages, 'ID', 'post_title' ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns pages list |
| 164 | * |
| 165 | * @param bool $for_elementor |
| 166 | * |
| 167 | * @return array [description] |
| 168 | */ |
| 169 | public static function get_forms_list_for_js( $for_elementor = false ) { |
| 170 | $posts = get_posts( array( |
| 171 | 'post_status' => 'publish', |
| 172 | 'posts_per_page' => - 1, |
| 173 | 'post_type' => jet_form_builder()->post_type->slug(), |
| 174 | ) ); |
| 175 | |
| 176 | return self::prepare_list_for_js( $posts, 'ID', 'post_title', $for_elementor ); |
| 177 | } |
| 178 | |
| 179 | public static function get_form_settings_options( $for_elementor = false ) { |
| 180 | $submit_type = array( |
| 181 | 'reload' => 'Page Reload', |
| 182 | 'ajax' => 'AJAX', |
| 183 | ); |
| 184 | $fields_layout = array( |
| 185 | 'column' => 'Column', |
| 186 | 'row' => 'Row' |
| 187 | ); |
| 188 | |
| 189 | if ( ! $for_elementor ) { |
| 190 | $submit_type = self::prepare_list_for_js( $submit_type ); |
| 191 | $fields_layout = self::prepare_list_for_js( $fields_layout ); |
| 192 | } |
| 193 | |
| 194 | return array( |
| 195 | 'submit_type' => $submit_type, |
| 196 | 'fields_layout' => $fields_layout |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns all registered user roles |
| 202 | * |
| 203 | * @param string[] $exclude |
| 204 | * |
| 205 | * @return array [type] [description] |
| 206 | */ |
| 207 | public static function get_user_roles( $exclude = array( 'administrator' ) ) { |
| 208 | |
| 209 | if ( ! function_exists( 'get_editable_roles' ) ) { |
| 210 | return array(); |
| 211 | } else { |
| 212 | $roles = get_editable_roles(); |
| 213 | $result = array(); |
| 214 | |
| 215 | foreach ( $roles as $role => $data ) { |
| 216 | if ( ! in_array( $role, $exclude ) ) { |
| 217 | $result[ $role ] = $data['name']; |
| 218 | } |
| 219 | } |
| 220 | return $result; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Prepare passed array for using in JS options |
| 226 | * |
| 227 | * @return [type] [description] |
| 228 | */ |
| 229 | public static function prepare_list_for_js( $array = array(), $value_key = null, $label_key = null, $for_elementor = false ) { |
| 230 | |
| 231 | $result = array(); |
| 232 | |
| 233 | if ( ! is_array( $array ) || empty( $array ) ) { |
| 234 | return $result; |
| 235 | } |
| 236 | |
| 237 | foreach ( $array as $key => $item ) { |
| 238 | |
| 239 | $value = null; |
| 240 | $label = null; |
| 241 | |
| 242 | if ( is_object( $item ) ) { |
| 243 | $value = $item->$value_key; |
| 244 | $label = $item->$label_key; |
| 245 | } elseif ( is_array( $item ) ) { |
| 246 | $value = $item[ $value_key ]; |
| 247 | $label = $item[ $label_key ]; |
| 248 | } else { |
| 249 | $value = $key; |
| 250 | $label = $item; |
| 251 | } |
| 252 | |
| 253 | if ( $for_elementor ) { |
| 254 | $result[ $value ] = $label; |
| 255 | } else { |
| 256 | $result[] = array( |
| 257 | 'value' => $value, |
| 258 | 'label' => $label, |
| 259 | ); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return $result; |
| 264 | |
| 265 | } |
| 266 | |
| 267 | public static function with_placeholder( $array, $label = '--' ) { |
| 268 | return array_merge( |
| 269 | array( |
| 270 | array( 'label' => $label, 'value' => '' ), |
| 271 | ), |
| 272 | $array |
| 273 | ); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Check if is valid timestamp |
| 278 | * |
| 279 | * @param mixed $timestamp |
| 280 | * |
| 281 | * @return boolean |
| 282 | */ |
| 283 | public static function is_valid_timestamp( $timestamp ) { |
| 284 | return ( ( string ) ( int ) $timestamp === $timestamp || ( int ) $timestamp === $timestamp ) |
| 285 | && ( $timestamp <= PHP_INT_MAX ) |
| 286 | && ( $timestamp >= ~PHP_INT_MAX ); |
| 287 | } |
| 288 | |
| 289 | public static function array_merge_intersect_key( $source, $arrays ) { |
| 290 | foreach ( $source as $index => $path ) { |
| 291 | $name = isset( $path['path'] ) ? $path['path'] : $index; |
| 292 | |
| 293 | $deep_value = self::getDeepValue( $name, $arrays ); |
| 294 | |
| 295 | if ( self::EMPTY_DEEP_VALUE === $deep_value ) { |
| 296 | unset( $source[ $index ] ); |
| 297 | } else { |
| 298 | $source[ $index ] = $deep_value; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return $source; |
| 303 | } |
| 304 | |
| 305 | public static function getDeepValue( $key, $source ) { |
| 306 | $keys = explode( '/', $key ); |
| 307 | $last = end( $keys ); |
| 308 | reset( $keys ); |
| 309 | |
| 310 | return self::deep( $keys, current( $keys ), $last, $source ); |
| 311 | } |
| 312 | |
| 313 | private static function deep( $array, $key, $last, $source ) { |
| 314 | |
| 315 | if ( isset( $source[ $key ] ) ) { |
| 316 | if ( $last !== $key ) { |
| 317 | return self::deep( $array, next( $array ), $last, $source[ $key ] ); |
| 318 | } |
| 319 | |
| 320 | return $source[ $key ]; |
| 321 | } |
| 322 | |
| 323 | return self::EMPTY_DEEP_VALUE; |
| 324 | } |
| 325 | |
| 326 | public static function run_callbacks( $callbacks = array(), ...$params ) { |
| 327 | if ( ! $callbacks ) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | foreach ( $callbacks as $callback ) { |
| 332 | if ( ! is_callable( $callback ) ) { |
| 333 | continue; |
| 334 | } |
| 335 | call_user_func( $callback, ...$params ); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | public static function decode_unserializable( $value ) { |
| 340 | $data = json_decode( $value, true ); |
| 341 | |
| 342 | return $data ? $data : maybe_unserialize( $value ); |
| 343 | } |
| 344 | |
| 345 | public static function maybe_recursive_sanitize( $source = null ) { |
| 346 | if ( ! is_array( $source ) ) { |
| 347 | return esc_attr( $source ); |
| 348 | } |
| 349 | |
| 350 | $result = array(); |
| 351 | foreach ( $source as $key => $value ) { |
| 352 | $result[ $key ] = self::maybe_recursive_sanitize( $value ); |
| 353 | } |
| 354 | |
| 355 | return $result; |
| 356 | } |
| 357 | |
| 358 | public static function sanitize_files( $source ) { |
| 359 | if ( ! is_array( $source ) ) { |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | $response = array(); |
| 364 | |
| 365 | foreach ( $source as $index => $item ) { |
| 366 | foreach ( $item as $key => $value ) { |
| 367 | switch ( $key ) { |
| 368 | case 'error': |
| 369 | case 'size': |
| 370 | $response[ $index ][ $key ] = absint( $value ); |
| 371 | break; |
| 372 | default: |
| 373 | $response[ $index ][ $key ] = esc_attr( $value ); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return $response; |
| 379 | } |
| 380 | |
| 381 | } |