| 1 |
<?php |
| 2 |
namespace ABlocks; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\FileUpload; |
| 9 |
use ABlocks\Classes\BlockGlobal; |
| 10 |
use ABlocks\traits\Importer; |
| 11 |
|
| 12 |
class Helper { |
| 13 |
|
| 14 |
use Importer; |
| 15 |
|
| 16 |
public static function get_time() { |
| 17 |
return time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
| 18 |
} |
| 19 |
|
| 20 |
public static function get_settings( $key, $default = null ) { |
| 21 |
global $ablocks_settings; |
| 22 |
|
| 23 |
if ( isset( $ablocks_settings->{$key} ) ) { |
| 24 |
return $ablocks_settings->{$key}; |
| 25 |
} |
| 26 |
|
| 27 |
return $default; |
| 28 |
} |
| 29 |
|
| 30 |
public static function get_page_permalink( $page, $fallback = null ) { |
| 31 |
$page_id = self::get_settings( $page ); |
| 32 |
$permalink = 0 < $page_id ? get_permalink( $page_id ) : ''; |
| 33 |
if ( ! $permalink ) { |
| 34 |
$permalink = is_null( $fallback ) ? get_home_url() : $fallback; |
| 35 |
} |
| 36 |
|
| 37 |
return apply_filters( 'ablocks/get_' . $page . '_permalink', $permalink ); |
| 38 |
} |
| 39 |
|
| 40 |
public static function get_logout_url( $redirect = '' ) { |
| 41 |
$redirect = $redirect ? $redirect : apply_filters( 'ablocks/logout_default_redirect_url', self::get_page_permalink( 'dashboard_page' ) ); |
| 42 |
|
| 43 |
return wp_logout_url( $redirect ); |
| 44 |
} |
| 45 |
public static function is_enabled_block( $block_name, $parent_block_name = '' ) { |
| 46 |
global $ablocks_blocks; |
| 47 |
$block_name = ! empty( $parent_block_name ) ? $parent_block_name : $block_name; |
| 48 |
if ( isset( $ablocks_blocks->{$block_name} ) ) { |
| 49 |
return (bool) $ablocks_blocks->{$block_name}; |
| 50 |
} |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public static function is_plugin_installed( $path ) { |
| 56 |
$installed_plugins = get_plugins(); |
| 57 |
return isset( $installed_plugins[ $path ] ); |
| 58 |
} |
| 59 |
|
| 60 |
public static function is_active_academy() { |
| 61 |
$academy = 'academy/academy.php'; |
| 62 |
return self::is_plugin_active( $academy ); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public static function is_active_storeengine() { |
| 67 |
$storeengine = 'storeengine/storeengine.php'; |
| 68 |
return self::is_plugin_active( $storeengine ); |
| 69 |
} |
| 70 |
|
| 71 |
public static function is_active_ablocks_pro() { |
| 72 |
$ablocks = 'ablocks-pro/ablocks-pro.php'; |
| 73 |
return self::is_plugin_active( $ablocks ); |
| 74 |
} |
| 75 |
public static function is_active_wp_map_block() { |
| 76 |
$wp_map_block = 'wp-map-block/wp-map-block.php'; |
| 77 |
return self::is_plugin_active( $wp_map_block ); |
| 78 |
} |
| 79 |
public static function is_active_quizpress() { |
| 80 |
return class_exists( 'QuizPress' ); |
| 81 |
} |
| 82 |
public static function is_active_easy_content_manager() { |
| 83 |
$easy_content_manager = 'easy-content-manager/easy-content-manager.php'; |
| 84 |
return self::is_plugin_active( $easy_content_manager ); |
| 85 |
} |
| 86 |
|
| 87 |
public static function is_enabled_assets_generation() { |
| 88 |
$flag = (bool) self::get_settings( 'enabled_assets_file_generation' ); |
| 89 |
return apply_filters( 'ablocks/is_enabled_assets_generation', $flag ); |
| 90 |
} |
| 91 |
|
| 92 |
public static function is_plugin_active( $basename ) { |
| 93 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 94 |
include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 95 |
} |
| 96 |
return is_plugin_active( $basename ); |
| 97 |
} |
| 98 |
|
| 99 |
public static function is_dev_mode_enable() { |
| 100 |
$environment = wp_get_environment_type(); |
| 101 |
if ( 'local' === $environment || 'development' === $environment ) { |
| 102 |
return true; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public static function get_admin_menu_list() { |
| 107 |
$menu = []; |
| 108 |
$menu[ ABLOCKS_PLUGIN_SLUG ] = [ |
| 109 |
'parent_slug' => ABLOCKS_PLUGIN_SLUG, |
| 110 |
'title' => __( 'Dashboard', 'ablocks' ), |
| 111 |
'capability' => 'manage_options', |
| 112 |
]; |
| 113 |
if ( self::is_enabled_block( 'form-builder' ) ) { |
| 114 |
$menu[ ABLOCKS_PLUGIN_SLUG . '-submissions' ] = [ |
| 115 |
'parent_slug' => ABLOCKS_PLUGIN_SLUG, |
| 116 |
'title' => __( 'Submissions', 'ablocks' ), |
| 117 |
'capability' => 'manage_options', |
| 118 |
]; |
| 119 |
} |
| 120 |
if ( self::get_addon_active_status( 'theme-builder' ) ) { |
| 121 |
$menu[ ABLOCKS_PLUGIN_SLUG . '-theme-builder' ] = [ |
| 122 |
'parent_slug' => ABLOCKS_PLUGIN_SLUG, |
| 123 |
'title' => __( 'Theme Builder', 'ablocks' ), |
| 124 |
'capability' => 'manage_options', |
| 125 |
]; |
| 126 |
} |
| 127 |
$menu[ ABLOCKS_PLUGIN_SLUG . '-addons' ] = [ |
| 128 |
'parent_slug' => ABLOCKS_PLUGIN_SLUG, |
| 129 |
'title' => __( 'Add-ons', 'ablocks' ), |
| 130 |
'capability' => 'manage_options', |
| 131 |
]; |
| 132 |
$menu[ ABLOCKS_PLUGIN_SLUG . '-settings' ] = [ |
| 133 |
'parent_slug' => ABLOCKS_PLUGIN_SLUG, |
| 134 |
'title' => __( 'Settings', 'ablocks' ), |
| 135 |
'capability' => 'manage_options', |
| 136 |
]; |
| 137 |
if ( ! defined( 'ABLOCKS_PRO_VERSION' ) ) { |
| 138 |
$menu[ ABLOCKS_PLUGIN_SLUG . '-get-pro' ] = [ |
| 139 |
'parent_slug' => ABLOCKS_PLUGIN_SLUG, |
| 140 |
'title' => '<span class="dashicons dashicons-awards academy-blue-color"></span> ' . __( 'Get Pro', 'ablocks' ), |
| 141 |
'capability' => 'manage_options', |
| 142 |
]; |
| 143 |
} |
| 144 |
return apply_filters( 'ablocks/admin_menu_list', $menu ); |
| 145 |
} |
| 146 |
|
| 147 |
public static function get_preloader_html() { |
| 148 |
ob_start(); |
| 149 |
?> |
| 150 |
<div class="ablocks-initial-preloader"><?php esc_html_e( 'Loading...', 'ablocks' ); ?></div> |
| 151 |
<?php |
| 152 |
return ob_get_clean(); |
| 153 |
} |
| 154 |
public static function has_value( $value ) { |
| 155 |
return isset( $value ) && ! empty( $value ); |
| 156 |
} |
| 157 |
|
| 158 |
public static function get_array_value( $array, $key, $default ) { |
| 159 |
return ( isset( $array[ $key ] ) && ! empty( $array[ $key ] ) ) ? $array[ $key ] : $default; |
| 160 |
} |
| 161 |
|
| 162 |
public static function get_responsive_value( $attribute, $attribute_object_key, $device, $attribute_default_value = [] ) { |
| 163 |
// Closure to "clean" a value (similar to your JS clean() helper) |
| 164 |
$clean = function( $value ) { |
| 165 |
return self::has_value( $value ) ? $value : null; |
| 166 |
}; |
| 167 |
|
| 168 |
// Desktop value (fallback to default, or false if nothing found) |
| 169 |
$desktop_value = |
| 170 |
$clean( $attribute[ $attribute_object_key ] ?? null ) ?? |
| 171 |
$clean( $attribute_default_value[ $attribute_object_key ] ?? null ) ?? |
| 172 |
false; |
| 173 |
|
| 174 |
// Tablet value (fallback to desktop if nothing found) |
| 175 |
$tablet_key = $attribute_object_key . 'Tablet'; |
| 176 |
$tablet_value = |
| 177 |
$clean( $attribute[ $tablet_key ] ?? null ) ?? |
| 178 |
$clean( $attribute_default_value[ $tablet_key ] ?? null ) ?? |
| 179 |
$desktop_value; |
| 180 |
|
| 181 |
// Mobile value (fallback to tablet if nothing found) |
| 182 |
$mobile_key = $attribute_object_key . 'Mobile'; |
| 183 |
$mobile_value = |
| 184 |
$clean( $attribute[ $mobile_key ] ?? null ) ?? |
| 185 |
$clean( $attribute_default_value[ $mobile_key ] ?? null ) ?? |
| 186 |
$tablet_value; |
| 187 |
|
| 188 |
// Return based on device |
| 189 |
switch ( $device ) { |
| 190 |
case 'Mobile': |
| 191 |
return $mobile_value; |
| 192 |
case 'Tablet': |
| 193 |
return $tablet_value; |
| 194 |
default: |
| 195 |
return $desktop_value; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
public static function is_gutenberg_editor() { |
| 200 |
global $pagenow; |
| 201 |
if ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) { |
| 202 |
return true; |
| 203 |
} |
| 204 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 205 |
return ( isset( $_GET['context'] ) && 'edit' === $_GET['context'] ) || ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] ); |
| 206 |
} |
| 207 |
public static function attr_shortcode( $attr_array ) { |
| 208 |
$html_attr = ''; |
| 209 |
foreach ( $attr_array as $attr_name => $attr_val ) { |
| 210 |
if ( empty( $attr_val ) ) { |
| 211 |
continue; |
| 212 |
} |
| 213 |
if ( is_array( $attr_val ) ) { |
| 214 |
$html_attr .= $attr_name . '="' . implode( ',', $attr_val ) . '" '; |
| 215 |
} else { |
| 216 |
$html_attr .= $attr_name . '="' . $attr_val . '" '; |
| 217 |
} |
| 218 |
} |
| 219 |
return $html_attr; |
| 220 |
} |
| 221 |
|
| 222 |
public static function get_attribute_value( $attributes, $attribute_name ) { |
| 223 |
return isset( $attributes[ $attribute_name ] ) ? $attributes[ $attribute_name ] : ''; |
| 224 |
} |
| 225 |
|
| 226 |
public static function get_terms_list( $taxonomy = 'category' ) { |
| 227 |
$options = []; |
| 228 |
$terms = get_terms( [ |
| 229 |
'taxonomy' => $taxonomy, |
| 230 |
'hide_empty' => true, |
| 231 |
] ); |
| 232 |
|
| 233 |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 234 |
foreach ( $terms as $term ) { |
| 235 |
$options[] = [ |
| 236 |
'label' => $term->name, |
| 237 |
'value' => $term->term_id, |
| 238 |
]; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return $options; |
| 243 |
} |
| 244 |
|
| 245 |
public static function get_author_data( $post_id, $author_id = false ) { |
| 246 |
$author_id = $author_id ?: get_post_field( 'post_author', $post_id ); |
| 247 |
$user = get_userdata( $author_id ); |
| 248 |
|
| 249 |
if ( ! $user ) { |
| 250 |
wp_send_json_error( 'Author not found.', 404 ); |
| 251 |
} |
| 252 |
|
| 253 |
$data = [ |
| 254 |
'author_id' => $author_id, |
| 255 |
'author_name' => sanitize_text_field( $user->display_name ), |
| 256 |
'author_posts_count' => count_user_posts( $author_id ), |
| 257 |
'author_posts_url' => esc_url( get_author_posts_url( $author_id ) ), |
| 258 |
'author_profile_picture_url' => esc_url( get_avatar_url( $author_id ) ), |
| 259 |
'author_bio' => sanitize_text_field( get_user_meta( $author_id, 'description', true ) ), |
| 260 |
'author_email' => sanitize_email( $user->user_email ), |
| 261 |
'author_website' => esc_url( $user->user_url ), |
| 262 |
'author_first_name' => sanitize_text_field( get_user_meta( $author_id, 'first_name', true ) ), |
| 263 |
'author_last_name' => sanitize_text_field( get_user_meta( $author_id, 'last_name', true ) ) |
| 264 |
]; |
| 265 |
|
| 266 |
return $data; |
| 267 |
} |
| 268 |
|
| 269 |
public static function get_icon_picker_attribute( $attributePrefix = 'icon', $defaultValue = [] ) { |
| 270 |
$svgPathKey = $attributePrefix . 'SvgPath'; |
| 271 |
$svgViewBoxKey = $attributePrefix . 'SvgViewBox'; |
| 272 |
$svgClassKey = $attributePrefix . 'Class'; |
| 273 |
|
| 274 |
$attribute = [ |
| 275 |
$svgPathKey => [ |
| 276 |
'type' => 'string', |
| 277 |
'source' => 'attribute', |
| 278 |
'selector' => 'svg.ablocks-svg-icon path', |
| 279 |
'attribute' => 'd', |
| 280 |
], |
| 281 |
$svgViewBoxKey => [ |
| 282 |
'type' => 'string', |
| 283 |
'source' => 'attribute', |
| 284 |
'selector' => 'svg.ablocks-svg-icon', |
| 285 |
'attribute' => 'viewBox', |
| 286 |
], |
| 287 |
$svgClassKey => [ |
| 288 |
'type' => 'string', |
| 289 |
], |
| 290 |
]; |
| 291 |
|
| 292 |
if ( isset( $defaultValue['path'] ) && isset( $defaultValue['viewBox'] ) ) { |
| 293 |
$attribute[ $svgPathKey ]['default'] = $defaultValue['path']; |
| 294 |
$attribute[ $svgViewBoxKey ]['default'] = $defaultValue['viewBox']; |
| 295 |
} |
| 296 |
if ( isset( $defaultValue['className'] ) ) { |
| 297 |
$attribute[ $svgClassKey ]['default'] = $defaultValue['className']; |
| 298 |
} |
| 299 |
return $attribute; |
| 300 |
} |
| 301 |
|
| 302 |
public static function get_terms_for_post( $taxonomy, $post_id ) { |
| 303 |
$terms = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'all' ] ); |
| 304 |
|
| 305 |
if ( is_wp_error( $terms ) ) { |
| 306 |
return []; |
| 307 |
} |
| 308 |
|
| 309 |
return array_map( function( $term ) { |
| 310 |
return [ |
| 311 |
'id' => $term->term_id, |
| 312 |
'name' => $term->name, |
| 313 |
'slug' => $term->slug, |
| 314 |
]; |
| 315 |
}, $terms ); |
| 316 |
} |
| 317 |
|
| 318 |
public static function get_taxonomies_data_for_post_type( $post_type ) { |
| 319 |
$all_taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 320 |
return array_values(array_map( function( $taxonomy ) { |
| 321 |
return [ |
| 322 |
'value' => $taxonomy->name, |
| 323 |
'label' => $taxonomy->label, |
| 324 |
]; |
| 325 |
}, $all_taxonomies )); |
| 326 |
} |
| 327 |
|
| 328 |
public static function get_post_excerpt( $post_id, $length = false ) { |
| 329 |
$excerpt = get_the_excerpt( $post_id ); |
| 330 |
if ( $length ) { |
| 331 |
return wp_trim_words( $excerpt, $length, '...' ); |
| 332 |
} |
| 333 |
return $excerpt; |
| 334 |
} |
| 335 |
|
| 336 |
public static function get_post_terms_as_string( $post_id, $taxonomy, $separator = ', ' ) { |
| 337 |
$terms = wp_get_post_terms( $post_id, $taxonomy ); |
| 338 |
|
| 339 |
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { |
| 340 |
$term_names = wp_list_pluck( $terms, 'name' ); |
| 341 |
return implode( $separator, $term_names ); |
| 342 |
} |
| 343 |
|
| 344 |
return ''; |
| 345 |
} |
| 346 |
|
| 347 |
public static function get_post_time_date( $post_id, $dynamicContentAttribute, $is_time = false ) { |
| 348 |
$dateTimeType = $dynamicContentAttribute['dateTimeType']; |
| 349 |
$dateTimeFormat = $dynamicContentAttribute['dateTimeFormat']; |
| 350 |
$customDateTimeFormat = $dynamicContentAttribute['customDateTimeFormat']; |
| 351 |
|
| 352 |
$format = $dateTimeFormat; |
| 353 |
$defaultFormat = $is_time ? 'g:i a' : 'j M, Y'; |
| 354 |
if ( ! $dateTimeFormat ) { |
| 355 |
$format = $defaultFormat; |
| 356 |
} elseif ( 'custom' === $dateTimeFormat ) { |
| 357 |
$format = $customDateTimeFormat || $defaultFormat; |
| 358 |
} |
| 359 |
|
| 360 |
$date_time = ''; |
| 361 |
if ( ! $dateTimeType || 'post_published' === $dateTimeType ) { |
| 362 |
$date_time = get_the_date( $format, $post_id ); |
| 363 |
} elseif ( 'post_modified' === $dateTimeType ) { |
| 364 |
$date_time = get_the_modified_date( $format, $post_id ); |
| 365 |
} |
| 366 |
|
| 367 |
return $date_time; |
| 368 |
} |
| 369 |
|
| 370 |
public static function is_fse_theme() { |
| 371 |
return function_exists( 'wp_is_block_theme' ) && wp_is_block_theme(); |
| 372 |
} |
| 373 |
|
| 374 |
public static function check_post_type_from_admin( $post_type ) { |
| 375 |
global $post; |
| 376 |
if ( is_admin() ) { |
| 377 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 378 |
if ( $post && get_post_type( $post ) === $post_type ) { |
| 379 |
return true; |
| 380 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 381 |
} elseif ( isset( $_GET['post_type'] ) && $_GET['post_type'] === $post_type ) { |
| 382 |
return true; |
| 383 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 384 |
} elseif ( isset( $_GET['post'] ) ) { |
| 385 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 386 |
$queried_post_type = get_post_type( sanitize_text_field( wp_unslash( $_GET['post'] ) ) ); |
| 387 |
if ( $queried_post_type === $post_type ) { |
| 388 |
return true; |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
public static function get_content_by_object_id( string $id_or_fse_slug ) : ?string { |
| 396 |
if ( is_numeric( $id_or_fse_slug ) ) { |
| 397 |
return get_post_field( 'post_content', intval( $id_or_fse_slug ) ); |
| 398 |
} elseif ( |
| 399 |
! empty( $template = get_block_template( $id_or_fse_slug, 'wp_template_part' ) ) || |
| 400 |
! empty( $template = get_block_template( $id_or_fse_slug ) ) |
| 401 |
) { |
| 402 |
return $template->content; |
| 403 |
} |
| 404 |
return null; |
| 405 |
} |
| 406 |
|
| 407 |
public static function get_block_attributes( string $post_id, string $block_id, string $block_name ) : array { |
| 408 |
if ( ! is_null( $post_content = self::get_content_by_object_id( $post_id ) ) ) { |
| 409 |
if ( is_array( $blocks = parse_blocks( $post_content ) ) ) { |
| 410 |
return self::get_block_attributes_recursive( $block_id, $block_name, $blocks ); |
| 411 |
} |
| 412 |
} |
| 413 |
return []; |
| 414 |
} |
| 415 |
|
| 416 |
public static function get_block_attributes_recursive( string $block_id, string $block_name, array $blocks ) : array { |
| 417 |
foreach ( $blocks as $block ) { |
| 418 |
|
| 419 |
if ( |
| 420 |
( $block['attrs']['block_id'] ?? '' ) === $block_id && |
| 421 |
( $block['blockName'] ?? '' ) === $block_name |
| 422 |
) { |
| 423 |
return [ |
| 424 |
'parentAttributes' => $block['attrs'] ?? [], |
| 425 |
'innerBlocks' => self::extract_inner_blocks( $block['innerBlocks'] ?? [] ), |
| 426 |
]; |
| 427 |
} |
| 428 |
|
| 429 |
if ( |
| 430 |
array_key_exists( 'innerBlocks', $block ) && |
| 431 |
is_array( $block['innerBlocks'] ) && |
| 432 |
count( $block['innerBlocks'] ) > 0 |
| 433 |
) { |
| 434 |
$data = self::get_block_attributes_recursive( |
| 435 |
$block_id, |
| 436 |
$block_name, |
| 437 |
$block['innerBlocks'] |
| 438 |
); |
| 439 |
if ( ! empty( $data ) ) { |
| 440 |
return $data; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
if ( |
| 445 |
isset( $block['blockName'] ) && |
| 446 |
$block['blockName'] === 'core/template-part' && |
| 447 |
! empty( $block['attrs']['slug'] ) |
| 448 |
) { |
| 449 |
$part_slug = $block['attrs']['theme'] . '//' . $block['attrs']['slug']; |
| 450 |
$data = self::get_block_attributes( $part_slug, $block_id, $block_name ); |
| 451 |
if ( ! empty( $data ) ) { |
| 452 |
return $data; |
| 453 |
} |
| 454 |
} |
| 455 |
}//end foreach |
| 456 |
return []; |
| 457 |
} |
| 458 |
|
| 459 |
public static function extract_inner_blocks( $innerBlocks ) { |
| 460 |
return array_map(function ( $inner_block ) { |
| 461 |
$block_data = [ |
| 462 |
'blockName' => $inner_block['blockName'], |
| 463 |
'attributes' => $inner_block['attrs'], |
| 464 |
]; |
| 465 |
if ( ! empty( $inner_block['innerBlocks'] ) ) { |
| 466 |
$block_data['innerBlocks'] = self::extract_inner_blocks( $inner_block['innerBlocks'] ); |
| 467 |
} |
| 468 |
return $block_data; |
| 469 |
}, $innerBlocks); |
| 470 |
} |
| 471 |
|
| 472 |
public static function generate_schema_using_form_data( $customFields ) { |
| 473 |
$schema = []; |
| 474 |
|
| 475 |
foreach ( $customFields as $inputField ) { |
| 476 |
// Check if 'name' exists in the input field |
| 477 |
if ( isset( $inputField['name'] ) ) { |
| 478 |
$field_name = $inputField['name']; |
| 479 |
$input_type = $inputField['inputType'] ?? 'text'; // Default to 'text' if not specified |
| 480 |
|
| 481 |
// Map the input types to schema types |
| 482 |
switch ( $input_type ) { |
| 483 |
case 'Text': |
| 484 |
$schema[ $field_name ] = 'string'; |
| 485 |
break; |
| 486 |
case 'Email': |
| 487 |
$schema[ $field_name ] = 'email'; |
| 488 |
break; |
| 489 |
case 'Password': |
| 490 |
$schema[ $field_name ] = 'string'; |
| 491 |
break; |
| 492 |
case 'Number': |
| 493 |
$schema[ $field_name ] = 'number'; |
| 494 |
break; |
| 495 |
case 'Url': |
| 496 |
$schema[ $field_name ] = 'url'; |
| 497 |
break; |
| 498 |
case 'Boolean': |
| 499 |
$schema[ $field_name ] = 'boolean'; |
| 500 |
break; |
| 501 |
case 'Textarea': |
| 502 |
$schema[ $field_name ] = 'textarea'; |
| 503 |
break; |
| 504 |
default: |
| 505 |
$schema[ $field_name ] = 'string'; // Default to string for unknown types |
| 506 |
break; |
| 507 |
}//end switch |
| 508 |
}//end if |
| 509 |
}//end foreach |
| 510 |
|
| 511 |
return $schema; |
| 512 |
} |
| 513 |
|
| 514 |
public static function sorted_input_fields_by_input_type( $blockData ) { |
| 515 |
$sorted_custom_fields = []; |
| 516 |
foreach ( $blockData as $block ) { |
| 517 |
$name = $block['attributes']['name'] ?? null; |
| 518 |
$inputType = $block['attributes']['inputType'] ?? null; |
| 519 |
|
| 520 |
if ( $name && isset( $custom_fields[ $name ] ) ) { |
| 521 |
$sorted_custom_fields[] = [ |
| 522 |
'name' => $name, |
| 523 |
'inputType' => $inputType, |
| 524 |
'value' => $custom_fields[ $name ] |
| 525 |
]; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
return $sorted_custom_fields; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
public static function render_svg_icon_using_attr( $attributes = array() ) { |
| 534 |
$default_attributes = array( |
| 535 |
'path' => '', |
| 536 |
'viewBox' => '0 0 24 24', |
| 537 |
'className' => 'icon-class', |
| 538 |
'width' => '24', |
| 539 |
'height' => '24', |
| 540 |
); |
| 541 |
|
| 542 |
// Merge passed attributes with default values |
| 543 |
$attributes = array_merge( $default_attributes, $attributes ); |
| 544 |
|
| 545 |
// Sanitize attributes for safety |
| 546 |
$path = esc_attr( $attributes['path'] ); |
| 547 |
$viewBox = esc_attr( $attributes['viewBox'] ); |
| 548 |
$className = esc_attr( $attributes['className'] ); |
| 549 |
$width = esc_attr( $attributes['width'] ); |
| 550 |
$height = esc_attr( $attributes['height'] ); |
| 551 |
|
| 552 |
// Output the SVG |
| 553 |
return ' |
| 554 |
<svg |
| 555 |
xmlns="http://www.w3.org/2000/svg" |
| 556 |
viewBox="' . $viewBox . '" |
| 557 |
class="' . $className . '" |
| 558 |
width="' . $width . '" |
| 559 |
height="' . $height . '"> |
| 560 |
<path d="' . $path . '"></path> |
| 561 |
</svg>'; |
| 562 |
} |
| 563 |
|
| 564 |
public static function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) { |
| 565 |
$template = false; |
| 566 |
|
| 567 |
if ( ! $template ) { |
| 568 |
$template = self::locate_template( $template_name, $template_path, $default_path ); |
| 569 |
} |
| 570 |
|
| 571 |
// Allow 3rd party plugin filter template file from their plugin. |
| 572 |
$filter_template = apply_filters( 'ablocks/get_template', $template, $template_name, $args, $template_path, $default_path ); |
| 573 |
|
| 574 |
if ( $filter_template !== $template ) { |
| 575 |
if ( ! file_exists( $filter_template ) ) { |
| 576 |
/* translators: %s template */ |
| 577 |
wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'ablocks' ), '<code>' . $filter_template . '</code>' ), '1.0.0' ); |
| 578 |
|
| 579 |
return; |
| 580 |
} |
| 581 |
$template = $filter_template; |
| 582 |
} |
| 583 |
|
| 584 |
$action_args = array( |
| 585 |
'template_name' => $template_name, |
| 586 |
'template_path' => $template_path, |
| 587 |
'located' => $template, |
| 588 |
'args' => $args, |
| 589 |
); |
| 590 |
|
| 591 |
if ( ! empty( $args ) && is_array( $args ) ) { |
| 592 |
if ( isset( $args['action_args'] ) ) { |
| 593 |
wc_doing_it_wrong( |
| 594 |
__FUNCTION__, |
| 595 |
__( 'action_args should not be overwritten when calling ablocks/get_template.', 'ablocks' ), |
| 596 |
'1.0.0' |
| 597 |
); |
| 598 |
unset( $args['action_args'] ); |
| 599 |
} |
| 600 |
extract( $args ); // @codingStandardsIgnoreLine |
| 601 |
} |
| 602 |
|
| 603 |
do_action( 'ablocks/before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] ); |
| 604 |
include $action_args['located']; |
| 605 |
|
| 606 |
do_action( 'ablocks/after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] ); |
| 607 |
} |
| 608 |
|
| 609 |
public static function locate_template( $template_name, $template_path = '', $default_path = '' ) { |
| 610 |
if ( ! $template_path ) { |
| 611 |
$template_path = self::template_path(); |
| 612 |
} |
| 613 |
|
| 614 |
if ( ! $default_path ) { |
| 615 |
$default_path = self::plugin_path() . 'templates/'; |
| 616 |
} |
| 617 |
|
| 618 |
if ( empty( $template ) ) { |
| 619 |
$template = locate_template( |
| 620 |
array( |
| 621 |
trailingslashit( $template_path ) . $template_name, |
| 622 |
$template_name, |
| 623 |
) |
| 624 |
); |
| 625 |
} |
| 626 |
if ( ! $template ) { |
| 627 |
$template = $default_path . $template_name; |
| 628 |
} |
| 629 |
|
| 630 |
// Return what we found. |
| 631 |
return apply_filters( 'ablocks/locate_template', $template, $template_name, $template_path ); |
| 632 |
} |
| 633 |
|
| 634 |
public static function template_path() { |
| 635 |
return apply_filters( 'ablocks/template_path', 'ablocks/' ); |
| 636 |
} |
| 637 |
public static function plugin_path() { |
| 638 |
return apply_filters( 'ablocks/plugin_path', ABLOCKS_ROOT_DIR_PATH ); |
| 639 |
} |
| 640 |
|
| 641 |
public static function get_addon_active_status( $addon_name, $is_pro = false ) { |
| 642 |
global $ablocks_addons; |
| 643 |
if ( $is_pro && ! self::is_active_ablocks_pro() ) { |
| 644 |
return false; |
| 645 |
} |
| 646 |
if ( isset( $ablocks_addons->{$addon_name} ) ) { |
| 647 |
return (bool) $ablocks_addons->{$addon_name}; |
| 648 |
} |
| 649 |
|
| 650 |
return false; |
| 651 |
} |
| 652 |
|
| 653 |
public static function sanitize_checkbox_field( $boolean ) { |
| 654 |
return filter_var( sanitize_text_field( $boolean ), FILTER_VALIDATE_BOOLEAN ); |
| 655 |
} |
| 656 |
|
| 657 |
public static function is_valid_site_url( string $url ): bool { |
| 658 |
return str_starts_with( $url, get_option( 'siteurl' ) ); |
| 659 |
} |
| 660 |
|
| 661 |
public static function get_script_loading_strategy() { |
| 662 |
return 'defer'; |
| 663 |
} |
| 664 |
|
| 665 |
public static function is_static_front_page( $post_id ) { |
| 666 |
$show_on_front = get_option( 'show_on_front' ); |
| 667 |
$page_on_front = (int) get_option( 'page_on_front' ); |
| 668 |
return ( $show_on_front === 'page' && $page_on_front === (int) $post_id ); |
| 669 |
} |
| 670 |
|
| 671 |
public static function get_public_post_type_options() { |
| 672 |
$args = [ |
| 673 |
'public' => true, |
| 674 |
'_builtin' => true, |
| 675 |
]; |
| 676 |
$post_types = get_post_types( $args, 'objects' ); |
| 677 |
unset( $post_types['attachment'] ); // Remove 'attachment' if present |
| 678 |
|
| 679 |
// Get custom post types |
| 680 |
$args['_builtin'] = false; |
| 681 |
$custom_post_types = get_post_types( $args, 'objects' ); |
| 682 |
|
| 683 |
// Allow filters to modify the combined post types |
| 684 |
$all_post_types = apply_filters( |
| 685 |
'ablocks_theme_builder/location_rule_post_types', |
| 686 |
array_merge( $post_types, $custom_post_types ) |
| 687 |
); |
| 688 |
|
| 689 |
// Format result |
| 690 |
$result = []; |
| 691 |
foreach ( $all_post_types as $post_type => $post_type_obj ) { |
| 692 |
$result[] = [ |
| 693 |
'label' => $post_type_obj->label, |
| 694 |
'value' => $post_type, |
| 695 |
]; |
| 696 |
} |
| 697 |
|
| 698 |
return $result; |
| 699 |
} |
| 700 |
|
| 701 |
public static function clear_third_party_plugin_cache() { |
| 702 |
|
| 703 |
// Breeze |
| 704 |
try { |
| 705 |
if ( class_exists( 'Breeze_Purge' ) ) { |
| 706 |
\Breeze_Purge::breeze_cache_flush(); |
| 707 |
} |
| 708 |
} catch ( \Throwable $e ) { |
| 709 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 710 |
error_log( 'Breeze Cache Clear Failed: ' . $e->getMessage() ); |
| 711 |
} |
| 712 |
|
| 713 |
// W3 Total Cache |
| 714 |
try { |
| 715 |
if ( function_exists( 'w3tc_flush_all' ) ) { |
| 716 |
\w3tc_flush_all(); |
| 717 |
} |
| 718 |
} catch ( \Throwable $e ) { |
| 719 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 720 |
error_log( 'W3 Total Cache Clear Failed: ' . $e->getMessage() ); |
| 721 |
} |
| 722 |
|
| 723 |
// WP Super Cache |
| 724 |
try { |
| 725 |
if ( function_exists( 'wp_cache_clear_cache' ) ) { |
| 726 |
\wp_cache_clear_cache(); |
| 727 |
} |
| 728 |
} catch ( \Throwable $e ) { |
| 729 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 730 |
error_log( 'WP Super Cache Clear Failed: ' . $e->getMessage() ); |
| 731 |
} |
| 732 |
|
| 733 |
// LiteSpeed Cache |
| 734 |
try { |
| 735 |
if ( class_exists( 'LiteSpeed_Cache_API' ) ) { |
| 736 |
\LiteSpeed_Cache_API::purge_all(); |
| 737 |
} |
| 738 |
} catch ( \Throwable $e ) { |
| 739 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 740 |
error_log( 'LiteSpeed Cache Clear Failed: ' . $e->getMessage() ); |
| 741 |
} |
| 742 |
|
| 743 |
// WP Fastest Cache |
| 744 |
try { |
| 745 |
if ( class_exists( 'WpFastestCache' ) ) { |
| 746 |
$wpfc = new \WpFastestCache(); |
| 747 |
$wpfc->deleteCache(); |
| 748 |
} |
| 749 |
} catch ( \Throwable $e ) { |
| 750 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 751 |
error_log( 'WP Fastest Cache Clear Failed: ' . $e->getMessage() ); |
| 752 |
} |
| 753 |
|
| 754 |
// Autoptimize |
| 755 |
try { |
| 756 |
if ( function_exists( 'autoptimize_clearall' ) ) { |
| 757 |
\autoptimize_clearall(); |
| 758 |
} |
| 759 |
} catch ( \Throwable $e ) { |
| 760 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 761 |
error_log( 'Autoptimize Clear Failed: ' . $e->getMessage() ); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|