| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Utils; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Block Utils Class |
| 9 |
*/ |
| 10 |
class BlockUtils { |
| 11 |
|
| 12 |
/** |
| 13 |
* Add google font to the list of fonts to be loaded. |
| 14 |
* |
| 15 |
* @param string $font_name font name. |
| 16 |
* @param array $weights font weights. |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public static function add_google_font( $font_name, $weights ) { |
| 20 |
add_filter( |
| 21 |
'optn_google_fonts', |
| 22 |
function ( $font_data ) use ( $font_name, $weights ) { |
| 23 |
|
| 24 |
$sanitized_font_name = str_replace( ' ', '+', sanitize_text_field( $font_name ) ); |
| 25 |
|
| 26 |
$sanitized_weights = array(); |
| 27 |
|
| 28 |
$weights = array_unique( $weights ); |
| 29 |
|
| 30 |
foreach ( $weights as $weigth ) { |
| 31 |
$weigth = intval( $weigth ); |
| 32 |
if ( $weigth >= 100 && $weigth <= 900 ) { |
| 33 |
$sanitized_weights[] = $weigth; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if ( isset( $font_data[ $sanitized_font_name ] ) && is_array( $font_data[ $sanitized_font_name ] ) ) { |
| 38 |
$sanitized_weights = array_unique( array_merge( $font_data[ $sanitized_font_name ], $sanitized_weights ) ); |
| 39 |
} |
| 40 |
|
| 41 |
$font_data[ $sanitized_font_name ] = $sanitized_weights; |
| 42 |
|
| 43 |
return $font_data; |
| 44 |
} |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Icon HTML |
| 50 |
* |
| 51 |
* @param string $icon_name icon name. |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
public static function get_icon_html( $icon_name ) { |
| 55 |
$html = ''; |
| 56 |
|
| 57 |
if ( empty( $icon_name ) ) { |
| 58 |
return $html; |
| 59 |
} |
| 60 |
|
| 61 |
$path = OPTN_DIR . '/assets/images/icon-library/' . $icon_name . '.svg'; |
| 62 |
|
| 63 |
if ( file_exists( $path ) ) { |
| 64 |
$html = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 65 |
} |
| 66 |
|
| 67 |
return $html; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get link opening tag html |
| 72 |
* |
| 73 |
* @param object $link link attribute. |
| 74 |
* @param string $attr extra attributes. |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public static function get_link_opening_tag_html( $link, $attr ) { |
| 78 |
$html = '<a href="' . esc_url( $link->url ) . '" target="' . esc_attr( $link->target ) . '" ' . $attr . ' '; |
| 79 |
|
| 80 |
if ( ! $link->doFollow ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 81 |
$html .= 'rel="nofollow" '; |
| 82 |
} elseif ( $link->sponsored ) { |
| 83 |
$html .= 'rel="sponsored" '; |
| 84 |
} |
| 85 |
|
| 86 |
if ( $link->download ) { |
| 87 |
$html .= 'download '; |
| 88 |
} |
| 89 |
|
| 90 |
$html .= ' >'; |
| 91 |
|
| 92 |
return $html; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Class for hiding elements responsively |
| 97 |
* |
| 98 |
* @param object $attr attributes. |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
public static function get_resp_hide_classes( &$attr ) { |
| 102 |
$cls = array(); |
| 103 |
|
| 104 |
if ( isset( $attr->hideDesktop ) && $attr->hideDesktop ) { // phpcs:ignore |
| 105 |
$cls[] = 'optn-hide-desktop'; |
| 106 |
} |
| 107 |
|
| 108 |
if ( isset( $attr->hideTab ) && $attr->hideTab ) { // phpcs:ignore |
| 109 |
$cls[] = 'optn-hide-tablet'; |
| 110 |
} |
| 111 |
|
| 112 |
if ( isset( $attr->hideMobile ) && $attr->hideMobile ) { // phpcs:ignore |
| 113 |
$cls[] = 'optn-hide-mobile'; |
| 114 |
} |
| 115 |
|
| 116 |
return count( $cls ) < 1 ? '' : ' ' . implode( ' ', $cls ) . ' '; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Watermark HTML |
| 121 |
* |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
public static function get_watermark_html() { |
| 125 |
$html = ''; |
| 126 |
|
| 127 |
$path = OPTN_DIR . '/assets/images/watermark.svg'; |
| 128 |
|
| 129 |
if ( file_exists( $path ) ) { |
| 130 |
$html .= '<a href="https://www.wowoptin.com" target="_blank" style="text-decoration:none;position:absolute;bottom:0px;right:0px;display:flex;align-items:center;justify-content:center;">'; |
| 131 |
$html .= file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 132 |
$html .= '</a>'; |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
return $html; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Undocumented function |
| 141 |
* |
| 142 |
* @param array $attr_array attributes array. |
| 143 |
* @return string |
| 144 |
*/ |
| 145 |
public static function build_html_attrs( $attr_array ) { |
| 146 |
$res = ''; |
| 147 |
foreach ( $attr_array as $key => $value ) { |
| 148 |
if ( is_array( $value ) ) { |
| 149 |
$attrs_str = implode( ' ', $value ); |
| 150 |
$res .= $key . '="' . $attrs_str . '" '; |
| 151 |
} else { |
| 152 |
$res .= $key . '="' . $value . '" '; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return $res; |
| 157 |
} |
| 158 |
} |
| 159 |
|