| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utility functions for Blocks plugin. |
| 4 |
* |
| 5 |
* Pure helpers that cannot be autoloaded (not classes). |
| 6 |
* Registration logic lives in Blocks_Post_Types. |
| 7 |
* Page builder helpers live in Blocks_Page_Builders. |
| 8 |
* |
| 9 |
* @package Blocks |
| 10 |
* @license GPL-2.0-or-later |
| 11 |
*/ |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
// Plugin URL. |
| 16 |
|
| 17 |
function blocks_plugin_url( $path = '' ) { |
| 18 |
$url = plugins_url( $path, BLOCKS_PLUGIN ); |
| 19 |
if ( is_ssl() && 'http:' === substr( $url, 0, 5 ) ) { |
| 20 |
$url = 'https:' . substr( $url, 5 ); |
| 21 |
} |
| 22 |
return $url; |
| 23 |
} |
| 24 |
|
| 25 |
// HTML formatting. |
| 26 |
|
| 27 |
function blocks_format_atts( $atts ) { |
| 28 |
$html = ''; |
| 29 |
$prioritized_atts = array( 'type', 'name', 'value' ); |
| 30 |
|
| 31 |
foreach ( $prioritized_atts as $att ) { |
| 32 |
if ( isset( $atts[ $att ] ) ) { |
| 33 |
$value = trim( $atts[ $att ] ); |
| 34 |
$html .= sprintf( ' %s="%s"', $att, esc_attr( $value ) ); |
| 35 |
unset( $atts[ $att ] ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
foreach ( $atts as $key => $value ) { |
| 40 |
$key = strtolower( trim( $key ) ); |
| 41 |
if ( ! preg_match( '/^[a-z_:][a-z_:.0-9-]*$/', $key ) ) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
$value = trim( $value ); |
| 45 |
if ( '' !== $value ) { |
| 46 |
$html .= sprintf( ' %s="%s"', $key, esc_attr( $value ) ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return trim( $html ); |
| 51 |
} |
| 52 |
|
| 53 |
// String utilities. |
| 54 |
|
| 55 |
function blocks_is_valid_locale( $locale ) { |
| 56 |
return preg_match( '/^[a-z]{2,3}(?:_[A-Z]{2})?$/', $locale ); |
| 57 |
} |
| 58 |
|
| 59 |
function blocks_normalize_newline( $text, $to = "\n" ) { |
| 60 |
if ( ! is_string( $text ) ) { |
| 61 |
return $text; |
| 62 |
} |
| 63 |
return str_replace( array( "\r\n", "\r", "\n" ), $to, $text ); |
| 64 |
} |
| 65 |
|
| 66 |
function blocks_normalize_newline_deep( $arr, $to = "\n" ) { |
| 67 |
if ( is_array( $arr ) ) { |
| 68 |
$result = array(); |
| 69 |
foreach ( $arr as $key => $text ) { |
| 70 |
$result[ $key ] = blocks_normalize_newline_deep( $text, $to ); |
| 71 |
} |
| 72 |
return $result; |
| 73 |
} |
| 74 |
return blocks_normalize_newline( $arr, $to ); |
| 75 |
} |
| 76 |
|
| 77 |
function blocks_array_flatten( $input ) { |
| 78 |
if ( ! is_array( $input ) ) { |
| 79 |
return array( $input ); |
| 80 |
} |
| 81 |
|
| 82 |
$output = array(); |
| 83 |
|
| 84 |
foreach ( $input as $value ) { |
| 85 |
if ( is_array( $value ) ) { |
| 86 |
$output = array_merge( $output, blocks_array_flatten( $value ) ); |
| 87 |
} else { |
| 88 |
$output[] = $value; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $output; |
| 93 |
} |
| 94 |
|
| 95 |
// Widget text filter. |
| 96 |
|
| 97 |
function blocks_widget_text_filter( $content ) { |
| 98 |
if ( ! preg_match( '/\[[\r\n\t ]*block[\r\n\t ].*?\]/', $content ) ) { |
| 99 |
return $content; |
| 100 |
} |
| 101 |
return do_shortcode( $content ); |
| 102 |
} |
| 103 |
|
| 104 |
// Page-builder wrappers. |
| 105 |
|
| 106 |
function blocks_render_for_builder( $block_id, $empty_message = '' ) { |
| 107 |
return Blocks_Page_Builders::render_for_builder( $block_id, $empty_message ); |
| 108 |
} |
| 109 |
|
| 110 |
function blocks_get_all_blocks_for_dropdown() { |
| 111 |
return Blocks_Page_Builders::get_all_blocks_for_dropdown(); |
| 112 |
} |
| 113 |
|
| 114 |
// Block instance helpers. |
| 115 |
|
| 116 |
function blocks( $id ) { |
| 117 |
return Blocks::get_instance( $id ); |
| 118 |
} |
| 119 |
|
| 120 |
function blocks_get_by_title( $title ) { |
| 121 |
$args = array( |
| 122 |
'post_type' => Blocks::POST_TYPE, |
| 123 |
'title' => $title, |
| 124 |
'post_status' => 'any', |
| 125 |
'numberposts' => 1, |
| 126 |
'fields' => 'ids', |
| 127 |
); |
| 128 |
|
| 129 |
$posts = get_posts( $args ); |
| 130 |
|
| 131 |
if ( empty( $posts ) ) { |
| 132 |
return null; |
| 133 |
} |
| 134 |
|
| 135 |
return blocks( $posts[0] ); |
| 136 |
} |
| 137 |
|
| 138 |
function blocks_get_current() { |
| 139 |
$current = Blocks::get_current(); |
| 140 |
if ( $current ) { |
| 141 |
return $current; |
| 142 |
} |
| 143 |
} |
| 144 |
|