attributes-trait.php
5 years ago
gallery.php
5 years ago
get-icon-trait.php
5 years ago
get-template-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
283 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 | public static function is_editor() { |
| 16 | $action = ! empty( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; |
| 17 | |
| 18 | return in_array( $action, array( 'add', 'edit' ) ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Returns all post types list to use in JS components |
| 23 | * |
| 24 | * @param bool $placeholder |
| 25 | * |
| 26 | * @param array $args |
| 27 | * @param string $operator |
| 28 | * |
| 29 | * @return array [type] [description] |
| 30 | */ |
| 31 | public static function get_post_types_for_js( $placeholder = false, $args = array(), $operator = 'and' ) { |
| 32 | |
| 33 | $post_types = get_post_types( $args, 'objects', $operator ); |
| 34 | |
| 35 | $post_types_list = array(); |
| 36 | |
| 37 | if ( $placeholder && is_array( $placeholder ) ) { |
| 38 | $placeholder['value'] = isset( $placeholder['value'] ) ? $placeholder['value'] : ''; |
| 39 | $post_types_list[] = $placeholder; |
| 40 | } |
| 41 | |
| 42 | foreach ( $post_types as $post_type ) { |
| 43 | if ( $post_type->name !== Plugin::instance()->post_type->slug() ) { |
| 44 | $post_types_list[] = array( |
| 45 | 'value' => $post_type->name, |
| 46 | 'label' => $post_type->label, |
| 47 | ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
| 52 | return $post_types_list; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get post types list for options. |
| 57 | * |
| 58 | * @return array |
| 59 | */ |
| 60 | public static function get_post_types_for_options() { |
| 61 | |
| 62 | return self::get_post_types_for_js( |
| 63 | array( 'label' => '--' ), |
| 64 | array( 'public' => true ) |
| 65 | ); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Sanitize WYSIWYG field |
| 71 | * |
| 72 | * @param $input |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public static function sanitize_wysiwyg( $input ) { |
| 77 | $input = wpautop( $input ); |
| 78 | |
| 79 | return wp_kses_post( $input ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Return all taxonomies list to use in JS components |
| 84 | * |
| 85 | * @return [type] [description] |
| 86 | */ |
| 87 | public static function get_taxonomies_for_js() { |
| 88 | $taxonomies = get_taxonomies( array(), 'objects' ); |
| 89 | |
| 90 | return self::with_placeholder( self::prepare_list_for_js( $taxonomies, 'name', 'label' ) ); |
| 91 | } |
| 92 | |
| 93 | public static function get_generators_list_for_js() { |
| 94 | $generators = Plugin::instance()->form->get_generators_list(); |
| 95 | |
| 96 | return self::prepare_list_for_js( $generators ); |
| 97 | } |
| 98 | |
| 99 | public static function get_allowed_mimes_list_for_js() { |
| 100 | $mimes = get_allowed_mime_types(); |
| 101 | |
| 102 | $mimes_list = array(); |
| 103 | foreach ( $mimes as $mime ) { |
| 104 | $mimes_list[] = array( |
| 105 | 'label' => $mime, |
| 106 | 'value' => $mime |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return $mimes_list; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns all registeredroles for JS |
| 115 | */ |
| 116 | public static function get_user_roles_for_js() { |
| 117 | |
| 118 | $roles = self::get_user_roles(); |
| 119 | $result = array(); |
| 120 | |
| 121 | foreach ( $roles as $role => $label ) { |
| 122 | $result[] = array( |
| 123 | 'value' => $role, |
| 124 | 'label' => $label, |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | return self::with_placeholder( $result ); |
| 129 | } |
| 130 | |
| 131 | public static function get_options_pages_for_js() { |
| 132 | $pages = array(); |
| 133 | |
| 134 | if ( function_exists( 'jet_engine' ) ) { |
| 135 | $pages = jet_engine()->options_pages->get_options_pages_for_select(); |
| 136 | } |
| 137 | |
| 138 | return self::prepare_list_for_js( $pages ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns pages list |
| 143 | * @return [type] [description] |
| 144 | */ |
| 145 | public static function get_pages_list_for_js() { |
| 146 | $pages = get_pages(); |
| 147 | |
| 148 | return self::prepare_list_for_js( $pages, 'ID', 'post_title' ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns pages list |
| 153 | * @return [type] [description] |
| 154 | */ |
| 155 | public static function get_forms_list_for_js() { |
| 156 | $posts = get_posts( array( |
| 157 | 'post_status' => 'publish', |
| 158 | 'posts_per_page' => - 1, |
| 159 | 'post_type' => jet_form_builder()->post_type->slug(), |
| 160 | ) ); |
| 161 | |
| 162 | return self::prepare_list_for_js( $posts, 'ID', 'post_title' ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns all registered user roles |
| 167 | * |
| 168 | * @return [type] [description] |
| 169 | */ |
| 170 | public static function get_user_roles() { |
| 171 | |
| 172 | if ( ! function_exists( 'get_editable_roles' ) ) { |
| 173 | return array(); |
| 174 | } else { |
| 175 | $roles = get_editable_roles(); |
| 176 | $result = array(); |
| 177 | |
| 178 | foreach ( $roles as $role => $data ) { |
| 179 | $result[ $role ] = $data['name']; |
| 180 | } |
| 181 | |
| 182 | return $result; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Prepare passed array for using in JS options |
| 188 | * |
| 189 | * @return [type] [description] |
| 190 | */ |
| 191 | public static function prepare_list_for_js( $array = array(), $value_key = null, $label_key = null ) { |
| 192 | |
| 193 | $result = array(); |
| 194 | |
| 195 | if ( ! is_array( $array ) || empty( $array ) ) { |
| 196 | return $result; |
| 197 | } |
| 198 | |
| 199 | foreach ( $array as $key => $item ) { |
| 200 | |
| 201 | $value = null; |
| 202 | $label = null; |
| 203 | |
| 204 | if ( is_object( $item ) ) { |
| 205 | $value = $item->$value_key; |
| 206 | $label = $item->$label_key; |
| 207 | } elseif ( is_array( $item ) ) { |
| 208 | $value = $item[ $value_key ]; |
| 209 | $label = $item[ $label_key ]; |
| 210 | } else { |
| 211 | $value = $key; |
| 212 | $label = $item; |
| 213 | } |
| 214 | |
| 215 | $result[] = array( |
| 216 | 'value' => $value, |
| 217 | 'label' => $label, |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | return $result; |
| 222 | |
| 223 | } |
| 224 | |
| 225 | public static function with_placeholder( $array, $label = '--' ) { |
| 226 | return array_merge( |
| 227 | array( |
| 228 | array( 'label' => $label, 'value' => '' ), |
| 229 | ), |
| 230 | $array |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Check if is valid timestamp |
| 236 | * |
| 237 | * @param mixed $timestamp |
| 238 | * |
| 239 | * @return boolean |
| 240 | */ |
| 241 | public static function is_valid_timestamp( $timestamp ) { |
| 242 | return ( ( string ) ( int ) $timestamp === $timestamp || ( int ) $timestamp === $timestamp ) |
| 243 | && ( $timestamp <= PHP_INT_MAX ) |
| 244 | && ( $timestamp >= ~PHP_INT_MAX ); |
| 245 | } |
| 246 | |
| 247 | public static function maybe_recursive_sanitize( $source = null ) { |
| 248 | if ( ! is_array( $source ) ) { |
| 249 | return esc_attr( $source ); |
| 250 | } |
| 251 | |
| 252 | $result = array(); |
| 253 | foreach ( $source as $key => $value ) { |
| 254 | $result[ $key ] = self::maybe_recursive_sanitize( $value ); |
| 255 | } |
| 256 | |
| 257 | return $result; |
| 258 | } |
| 259 | |
| 260 | public static function sanitize_files( $source ) { |
| 261 | if ( ! is_array( $source ) ) { |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | $response = array(); |
| 266 | |
| 267 | foreach ( $source as $index => $item ) { |
| 268 | foreach ( $item as $key => $value ) { |
| 269 | switch ( $key ) { |
| 270 | case 'error': |
| 271 | case 'size': |
| 272 | $response[ $index ][ $key ] = absint( $value ); |
| 273 | break; |
| 274 | default: |
| 275 | $response[ $index ][ $key ] = esc_attr( $value ); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return $response; |
| 281 | } |
| 282 | |
| 283 | } |