| 1 |
<?php |
| 2 |
/** |
| 3 |
* Custom Fonts |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Modules |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Init module |
| 11 |
*/ |
| 12 |
class Powerkit_Custom_Fonts extends Powerkit_Module { |
| 13 |
|
| 14 |
/** |
| 15 |
* Webfonts method |
| 16 |
* |
| 17 |
* @var string $load_method Webfonts method. |
| 18 |
*/ |
| 19 |
public $load_method = 'async'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Register module |
| 23 |
*/ |
| 24 |
public function register() { |
| 25 |
$this->name = esc_html__( 'Custom Fonts', 'powerkit' ); |
| 26 |
$this->desc = esc_html__( 'Adds the ability to download custom fonts.', 'powerkit' ); |
| 27 |
$this->slug = 'custom_fonts'; |
| 28 |
$this->type = 'default'; |
| 29 |
$this->category = 'basic'; |
| 30 |
$this->priority = 1040; |
| 31 |
$this->public = true; |
| 32 |
$this->enabled = false; |
| 33 |
$this->badge = esc_html__( 'Advanced', 'powerkit' ); |
| 34 |
$this->load_extensions = array( |
| 35 |
'fonts', |
| 36 |
); |
| 37 |
$this->links = array( |
| 38 |
array( |
| 39 |
'name' => esc_html__( 'Go to settings', 'powerkit' ), |
| 40 |
'url' => powerkit_get_page_url( 'fonts&tab=' . $this->slug, 'themes' ), |
| 41 |
), |
| 42 |
array( |
| 43 |
'name' => esc_html__( 'View documentation', 'powerkit' ), |
| 44 |
'url' => powerkit_get_setting( 'documentation' ) . '/content-presentation/custom-fonts/', |
| 45 |
'target' => '_blank', |
| 46 |
), |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Initialize module |
| 52 |
*/ |
| 53 |
public function initialize() { |
| 54 |
add_filter( 'powerkit_fonts_list', array( $this, 'custom_fonts' ), 10 ); |
| 55 |
add_filter( 'upload_mimes', array( $this, 'allow_mimes' ) ); |
| 56 |
add_filter( 'powerkit_fonts_register_settings', array( $this, 'register_settings' ), 10 ); |
| 57 |
add_action( 'customize_controls_print_styles', array( $this, 'frontend_enqueue' ), 100 ); |
| 58 |
add_action( 'wp_head', array( $this, 'frontend_enqueue' ), 100 ); |
| 59 |
add_action( 'admin_head', array( $this, 'editor_enqueue' ), 100 ); |
| 60 |
add_filter( 'init', array( $this, 'set_load_method' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Set Webfonts method |
| 65 |
*/ |
| 66 |
public function set_load_method() { |
| 67 |
$this->load_method = apply_filters( 'powerkit_webfonts_load_method', 'async' ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Add custom fonts |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* @param array $fonts List fonts. |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function custom_fonts( $fonts ) { |
| 78 |
|
| 79 |
if ( is_customize_preview() ) { |
| 80 |
|
| 81 |
$custom_fonts = get_option( 'powerkit_custom_fonts_list' ); |
| 82 |
|
| 83 |
if ( is_array( $custom_fonts ) && $custom_fonts ) { |
| 84 |
|
| 85 |
$exclude = array(); |
| 86 |
|
| 87 |
$fonts['families']['custom_fonts'] = array( |
| 88 |
'text' => esc_html__( 'Custom Fonts', 'powerkit' ), |
| 89 |
'children' => array(), |
| 90 |
); |
| 91 |
|
| 92 |
foreach ( $custom_fonts as $key => $item ) { |
| 93 |
if ( 'clone' === $key ) { |
| 94 |
continue; |
| 95 |
} |
| 96 |
$id = sanitize_title( $item['name'] ); |
| 97 |
|
| 98 |
if ( $id ) { |
| 99 |
if ( ! in_array( $id, $exclude, true ) ) { |
| 100 |
$fonts['families']['custom_fonts']['children'][] = array( |
| 101 |
'id' => $id, |
| 102 |
'text' => $item['name'], |
| 103 |
); |
| 104 |
|
| 105 |
$exclude[] = $id; |
| 106 |
} |
| 107 |
|
| 108 |
$variant = str_replace( '400', 'regular', $item['weight'] ); |
| 109 |
|
| 110 |
if ( 'italic' === $item['style'] ) { |
| 111 |
$variant .= 'italic'; |
| 112 |
} |
| 113 |
|
| 114 |
if ( isset( $fonts['variants'][ $id ] ) && $fonts['variants'][ $id ] ) { |
| 115 |
if ( ! in_array( $variant, $fonts['variants'][ $id ], true ) ) { |
| 116 |
$fonts['variants'][ $id ][] = $variant; |
| 117 |
} |
| 118 |
} else { |
| 119 |
$fonts['variants'][ $id ][] = $variant; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $fonts; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Register new mime types and file extensions. |
| 131 |
* |
| 132 |
* @since 1.0.0 |
| 133 |
* @param array $mimes Current array of mime types.. |
| 134 |
*/ |
| 135 |
public function allow_mimes( $mimes ) { |
| 136 |
$mimes['woff'] = 'application/x-font-woff'; |
| 137 |
$mimes['woff2'] = 'application/x-font-woff2'; |
| 138 |
|
| 139 |
return $mimes; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Register settings |
| 145 |
* |
| 146 |
* @since 1.0.0 |
| 147 |
* @param array $settings List settings. |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
public function register_settings( $settings ) { |
| 151 |
|
| 152 |
$settings[] = array( |
| 153 |
'id' => $this->slug, |
| 154 |
'name' => esc_html__( 'Custom Fonts', 'powerkit' ), |
| 155 |
'function' => array( $this, 'build_setting_page' ), |
| 156 |
); |
| 157 |
|
| 158 |
return $settings; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Build admin page |
| 163 |
* |
| 164 |
* @since 1.0.0 |
| 165 |
*/ |
| 166 |
public function build_setting_page() { |
| 167 |
|
| 168 |
$this->save_options_page(); |
| 169 |
|
| 170 |
$powerkit_custom_fonts_id = get_option( 'powerkit_custom_fonts_counter', 1 ); |
| 171 |
|
| 172 |
$params = array( |
| 173 |
'name' => '', |
| 174 |
'weight' => '400', |
| 175 |
'style' => 'normal', |
| 176 |
'file_woff' => '', |
| 177 |
'file_woff2' => '', |
| 178 |
); |
| 179 |
|
| 180 |
$action = 'new'; |
| 181 |
|
| 182 |
// Check custom action. |
| 183 |
if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['action'] ) && wp_verify_nonce( $_GET['_wpnonce'] ) ) { // Input var ok; sanitization ok. |
| 184 |
$action = sanitize_title( $_GET['action'] ); // Input var ok; sanitization ok. |
| 185 |
|
| 186 |
if ( isset( $_GET['powerkit_custom_fonts_id'] ) && 'edit' === $action ) { // Input var ok. |
| 187 |
$powerkit_custom_fonts_id = sanitize_key( $_GET['powerkit_custom_fonts_id'] ); // Input var ok. |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// Edit font. |
| 192 |
if ( 'edit' === $action ) { |
| 193 |
$font_list = get_option( 'powerkit_custom_fonts_list' ); |
| 194 |
|
| 195 |
if ( isset( $font_list[ $powerkit_custom_fonts_id ] ) ) { |
| 196 |
$params = array_merge( (array) $params, (array) $font_list[ $powerkit_custom_fonts_id ] ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// Delete font. |
| 201 |
if ( 'delete' === $action ) { |
| 202 |
$this->delete_font(); |
| 203 |
} |
| 204 |
|
| 205 |
// Settings page link. |
| 206 |
$powerkit_custom_fonts_link = powerkit_get_page_url( 'fonts&tab=' . $this->slug, 'themes' ); |
| 207 |
?> |
| 208 |
<form method="post" action="<?php echo esc_url( $powerkit_custom_fonts_link ); ?>"> |
| 209 |
<div class="col-container"> |
| 210 |
<div id="col-left"> |
| 211 |
<div class="col-wrap"> |
| 212 |
<div class="form-wrap"> |
| 213 |
<div class="form-field"> |
| 214 |
<h2><?php esc_html_e( 'Instructions', 'powerkit' ); ?></h2> |
| 215 |
<ol> |
| 216 |
<li> |
| 217 |
<?php |
| 218 |
$link_webfont = sprintf( '<a href="%1$s" target="_blank">%2$s</a>', esc_url( 'https://www.fontsquirrel.com/tools/webfont-generator' ), esc_html__( 'Font Squirrel', 'powerkit' ) ); |
| 219 |
|
| 220 |
// translators: args - format woff, format woff, format woff2, link webfont-generator. |
| 221 |
echo sprintf( esc_html__( 'Prepare your webfont files in %1$s and %2$s formats. You may convert your %3$s fonts at %4$s.', 'powerkit' ), '<code>woff</code>', '<code>woff2</code>', '<code>ttf</code>', wp_kses( $link_webfont, 'post' ) ); |
| 222 |
?> |
| 223 |
</li> |
| 224 |
<li><?php esc_html_e( 'Upload your font files.', 'powerkit' ); ?></li> |
| 225 |
<li><?php esc_html_e( 'Navigate to Appearance → Customise → Typography and select your font in typography controls under Custom Fonts.', 'powerkit' ); ?></li> |
| 226 |
</ol> |
| 227 |
</div> |
| 228 |
|
| 229 |
<?php if ( 'edit' === $action ) : ?> |
| 230 |
<h3> |
| 231 |
<?php esc_html_e( 'Edit Font', 'powerkit' ); ?> |
| 232 |
|
| 233 |
<small><a href="<?php echo esc_url( $powerkit_custom_fonts_link ); ?>"><?php esc_html_e( ' cancel ', 'powerkit' ); ?></a></small> |
| 234 |
</h3> |
| 235 |
<?php else : ?> |
| 236 |
<h3><?php esc_html_e( 'Add New Font', 'powerkit' ); ?></h3> |
| 237 |
<?php endif; ?> |
| 238 |
|
| 239 |
<!-- Name --> |
| 240 |
<div class="form-field form-required"> |
| 241 |
<label for="powerkit_custom_fonts_name"><?php esc_html_e( 'Name', 'powerkit' ); ?></label> |
| 242 |
|
| 243 |
<input id="powerkit_custom_fonts_name" name="powerkit_custom_fonts_name" type="text" aria-required="true" value="<?php echo esc_attr( stripslashes( $params['name'] ) ); ?>" /> |
| 244 |
</div> |
| 245 |
|
| 246 |
<!-- Font File [WOFF] --> |
| 247 |
<div class="form-field"> |
| 248 |
<label><?php esc_html_e( 'Font .woff', 'powerkit' ); ?></label> |
| 249 |
|
| 250 |
<div class="upload-font-container" data-type="application/x-font-woff"> |
| 251 |
|
| 252 |
<?php $file_woff = $params['file_woff'] ? basename( get_attached_file( $params['file_woff'] ) ) : null; ?> |
| 253 |
|
| 254 |
<p><input class="filename" type="text" placeholder="<?php esc_html_e( 'Choose file..', 'powerkit' ); ?>" readonly value="<?php echo esc_html( $file_woff ); ?>"></p> |
| 255 |
|
| 256 |
<!-- Add & remove file links --> |
| 257 |
<div class="submitbox"> |
| 258 |
<a class="upload-font-link button <?php echo esc_attr( $params['file_woff'] ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Add File', 'powerkit' ); ?></a> |
| 259 |
<a class="delete-font-link submitdelete <?php echo esc_attr( ! $params['file_woff'] ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Remove', 'powerkit' ); ?></a> |
| 260 |
</div> |
| 261 |
|
| 262 |
<input class="uploaded-font-id" id="powerkit_custom_fonts_file_woff" name="powerkit_custom_fonts_file_woff" type="hidden" value="<?php echo esc_attr( stripslashes( $params['file_woff'] ) ); ?>" /> |
| 263 |
</div> |
| 264 |
</div> |
| 265 |
|
| 266 |
<!-- Font File [WOFF2] --> |
| 267 |
<div class="form-field"> |
| 268 |
<label><?php esc_html_e( 'Font .woff2', 'powerkit' ); ?></label> |
| 269 |
|
| 270 |
<div class="upload-font-container" data-type="application/x-font-woff2"> |
| 271 |
|
| 272 |
<?php $file_woff2 = $params['file_woff2'] ? basename( get_attached_file( $params['file_woff2'] ) ) : null; ?> |
| 273 |
|
| 274 |
<p><input class="filename" type="text" placeholder="<?php esc_html_e( 'Choose file..', 'powerkit' ); ?>" readonly value="<?php echo esc_html( $file_woff2 ); ?>"></p> |
| 275 |
|
| 276 |
<!-- Add & remove file links --> |
| 277 |
<div class="submitbox"> |
| 278 |
<a class="upload-font-link button <?php echo esc_attr( $params['file_woff2'] ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Add File', 'powerkit' ); ?></a> |
| 279 |
<a class="delete-font-link submitdelete <?php echo esc_attr( ! $params['file_woff2'] ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Remove', 'powerkit' ); ?></a> |
| 280 |
</div> |
| 281 |
|
| 282 |
<input class="uploaded-font-id" id="powerkit_custom_fonts_file_woff2" name="powerkit_custom_fonts_file_woff2" type="hidden" value="<?php echo esc_attr( stripslashes( $params['file_woff2'] ) ); ?>" /> |
| 283 |
</div> |
| 284 |
</div> |
| 285 |
|
| 286 |
<!-- Font Weight --> |
| 287 |
<div class="form-field"> |
| 288 |
<label for="powerkit_custom_fonts_weight"><?php esc_html_e( 'Weight', 'powerkit' ); ?></label> |
| 289 |
|
| 290 |
<select class="regular-text" id="powerkit_custom_fonts_weight" name="powerkit_custom_fonts_weight"> |
| 291 |
<option value="100" <?php selected( '100', $params['weight'] ); ?>><?php esc_html_e( '100', 'powerkit' ); ?></option> |
| 292 |
<option value="200" <?php selected( '200', $params['weight'] ); ?>><?php esc_html_e( '200', 'powerkit' ); ?></option> |
| 293 |
<option value="300" <?php selected( '300', $params['weight'] ); ?>><?php esc_html_e( '300', 'powerkit' ); ?></option> |
| 294 |
<option value="400" <?php selected( '400', $params['weight'] ); ?>><?php esc_html_e( '400 (regular)', 'powerkit' ); ?></option> |
| 295 |
<option value="500" <?php selected( '500', $params['weight'] ); ?>><?php esc_html_e( '500', 'powerkit' ); ?></option> |
| 296 |
<option value="600" <?php selected( '600', $params['weight'] ); ?>><?php esc_html_e( '600', 'powerkit' ); ?></option> |
| 297 |
<option value="700" <?php selected( '700', $params['weight'] ); ?>><?php esc_html_e( '700', 'powerkit' ); ?></option> |
| 298 |
<option value="800" <?php selected( '800', $params['weight'] ); ?>><?php esc_html_e( '800', 'powerkit' ); ?></option> |
| 299 |
<option value="900" <?php selected( '900', $params['weight'] ); ?>><?php esc_html_e( '900', 'powerkit' ); ?></option> |
| 300 |
</select> |
| 301 |
</div> |
| 302 |
|
| 303 |
<!-- Font Style --> |
| 304 |
<div class="form-field"> |
| 305 |
<label for="powerkit_custom_fonts_style"><?php esc_html_e( 'Style', 'powerkit' ); ?></label> |
| 306 |
|
| 307 |
<select class="regular-text" id="powerkit_custom_fonts_style" name="powerkit_custom_fonts_style"> |
| 308 |
<option value="normal" <?php selected( 'normal', $params['style'] ); ?>><?php esc_html_e( 'normal', 'powerkit' ); ?></option> |
| 309 |
<option value="italic" <?php selected( 'italic', $params['style'] ); ?>><?php esc_html_e( 'italic', 'powerkit' ); ?></option> |
| 310 |
</select> |
| 311 |
</div> |
| 312 |
|
| 313 |
<input name="powerkit_custom_fonts_id" type="hidden" value="<?php echo esc_html( $powerkit_custom_fonts_id ); ?>" /> |
| 314 |
|
| 315 |
<?php wp_nonce_field(); ?> |
| 316 |
|
| 317 |
<?php if ( 'edit' === $action ) : ?> |
| 318 |
<p class="submit"><input class="button button-primary" name="edit_settings" type="submit" value="<?php esc_html_e( 'Save Change', 'powerkit' ); ?>" /></p> |
| 319 |
<?php else : ?> |
| 320 |
<p class="submit"><input class="button button-primary" name="save_settings" type="submit" value="<?php esc_html_e( 'Add Font', 'powerkit' ); ?>" /></p> |
| 321 |
<?php endif; ?> |
| 322 |
</div> |
| 323 |
</div> |
| 324 |
</div> |
| 325 |
<div id="col-right"> |
| 326 |
<div class="col-wrap wrap"> |
| 327 |
<table class="wrap wp-list-table widefat fixed striped"> |
| 328 |
<thead> |
| 329 |
<tr> |
| 330 |
<td scope="col" class="manage-column"><?php esc_html_e( 'Name', 'powerkit' ); ?></td> |
| 331 |
<td scope="col" class="manage-column"><?php esc_html_e( 'Font Family', 'powerkit' ); ?></td> |
| 332 |
<td scope="col" class="manage-column"><?php esc_html_e( 'Font Weight', 'powerkit' ); ?></td> |
| 333 |
<td scope="col" class="manage-column"><?php esc_html_e( 'Font Style', 'powerkit' ); ?></td> |
| 334 |
</tr> |
| 335 |
</thead> |
| 336 |
<tbody id="the-list"> |
| 337 |
<?php |
| 338 |
$families = get_option( 'powerkit_custom_fonts_list', array() ); |
| 339 |
|
| 340 |
// Sort families. |
| 341 |
if ( is_array( $families ) && $families ) { |
| 342 |
$families_keys = array_keys( $families ); |
| 343 |
|
| 344 |
foreach ( $families as $key => $row ) { |
| 345 |
$families_value[ $key ] = $row['name']; |
| 346 |
$families_name[ $key ] = $row['weight']; |
| 347 |
$families_order[ $key ] = $row['style']; |
| 348 |
} |
| 349 |
array_multisort( $families_value, SORT_DESC, $families_order, SORT_DESC, $families_name, SORT_ASC, $families, $families_keys ); |
| 350 |
|
| 351 |
$families = array_combine( $families_keys, $families ); |
| 352 |
} |
| 353 |
|
| 354 |
// Loop families. |
| 355 |
if ( $families ) { |
| 356 |
foreach ( $families as $key => $family ) { |
| 357 |
|
| 358 |
$edit_link = add_query_arg( |
| 359 |
array( |
| 360 |
'_wpnonce' => wp_create_nonce(), |
| 361 |
'powerkit_custom_fonts_id' => $key, |
| 362 |
'action' => 'edit', |
| 363 |
), $powerkit_custom_fonts_link |
| 364 |
); |
| 365 |
|
| 366 |
$delete_link = add_query_arg( |
| 367 |
array( |
| 368 |
'_wpnonce' => wp_create_nonce(), |
| 369 |
'powerkit_custom_fonts_id' => $key, |
| 370 |
'action' => 'delete', |
| 371 |
), $powerkit_custom_fonts_link |
| 372 |
); |
| 373 |
?> |
| 374 |
<tr> |
| 375 |
<td scope="col" class="manage-column"> |
| 376 |
<a class="row-title" href="<?php echo esc_url( $edit_link ); ?>"><?php echo esc_html( $family['name'] ); ?></a> |
| 377 |
|
| 378 |
<div class="row-actions"> |
| 379 |
<span class="edit"> |
| 380 |
<a href="<?php echo esc_url( $edit_link ); ?>" role="button"><?php esc_html_e( 'Edit', 'powerkit' ); ?></a> | |
| 381 |
</span> |
| 382 |
<span class="delete"> |
| 383 |
<a href="<?php echo esc_url( $delete_link ); ?>" class="powerkit-fonts-delete" role="button"><?php esc_html_e( 'Delete', 'powerkit' ); ?></a> | |
| 384 |
</span> |
| 385 |
<span class="view"> |
| 386 |
<a href="#" class="powerkit-fonts-view-code" role="button"><?php esc_html_e( 'View Code', 'powerkit' ); ?></a> |
| 387 |
</span> |
| 388 |
</div> |
| 389 |
</td> |
| 390 |
<td scope="col" class="manage-column"><?php echo esc_html( $family['slug'] ); ?></td> |
| 391 |
<td scope="col" class="manage-column"><?php echo esc_html( $family['weight'] ); ?></td> |
| 392 |
<td scope="col" class="manage-column"><?php echo esc_html( $family['style'] ); ?></td> |
| 393 |
</tr> |
| 394 |
<tr class="template-code hidden"> |
| 395 |
<td scope="col" class="manage-column" colspan="4"> |
| 396 |
<?php |
| 397 |
// Example code. |
| 398 |
$code = sprintf( |
| 399 |
'h1 { |
| 400 |
font-family: "%s"; |
| 401 |
font-weight: %s; |
| 402 |
font-style: %s; |
| 403 |
}', |
| 404 |
$family['slug'], |
| 405 |
$family['weight'], |
| 406 |
$family['style'] |
| 407 |
); |
| 408 |
|
| 409 |
$code = str_replace( "\t\t", '', $code ); |
| 410 |
?> |
| 411 |
<div id="template" class="template-box hidden"> |
| 412 |
<textarea readonly="readonly"><?php echo wp_kses( $code, 'post' ); ?></textarea> |
| 413 |
</div> |
| 414 |
</td> |
| 415 |
</tr> |
| 416 |
<tr class="hidden"></tr> |
| 417 |
<?php |
| 418 |
} |
| 419 |
} else { |
| 420 |
?> |
| 421 |
<tr> |
| 422 |
<td scope="col" colspan="4"><?php esc_html_e( 'No fonts found. Add new fonts with the form to the left.', 'powerkit' ); ?></td> |
| 423 |
</tr> |
| 424 |
<?php |
| 425 |
} |
| 426 |
?> |
| 427 |
</tbody> |
| 428 |
</table> |
| 429 |
</div> |
| 430 |
</div> |
| 431 |
</div> |
| 432 |
</form> |
| 433 |
<?php |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Delete font |
| 438 |
* |
| 439 |
* @since 1.0.0 |
| 440 |
*/ |
| 441 |
protected function delete_font() { |
| 442 |
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'] ) ) { // Input var ok; sanitization ok. |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
if ( ! isset( $_GET['powerkit_custom_fonts_id'] ) ) { // Input var ok. |
| 447 |
return; |
| 448 |
} |
| 449 |
|
| 450 |
// ID Font. |
| 451 |
$id = sanitize_key( $_GET['powerkit_custom_fonts_id'] ); // Input var ok. |
| 452 |
|
| 453 |
// Custom List. |
| 454 |
$font_list = (array) get_option( 'powerkit_custom_fonts_list', array() ); |
| 455 |
|
| 456 |
// If exist font. |
| 457 |
if ( isset( $font_list[ $id ] ) ) { |
| 458 |
|
| 459 |
// Attachments delete. |
| 460 |
if ( isset( $font_list[ $id ]['file_woff'] ) ) { |
| 461 |
wp_delete_attachment( $font_list[ $id ]['file_woff'], true ); |
| 462 |
} |
| 463 |
if ( isset( $font_list[ $id ]['file_woff2'] ) ) { |
| 464 |
wp_delete_attachment( $font_list[ $id ]['file_woff2'], true ); |
| 465 |
} |
| 466 |
|
| 467 |
// Delete font. |
| 468 |
unset( $font_list[ $id ] ); |
| 469 |
|
| 470 |
// Save list. |
| 471 |
update_option( 'powerkit_custom_fonts_list', $font_list ); |
| 472 |
|
| 473 |
// Output message. |
| 474 |
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Font successfully deleted.', 'powerkit' ) ); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Settings save |
| 480 |
* |
| 481 |
* @since 1.0.0 |
| 482 |
*/ |
| 483 |
protected function save_options_page() { |
| 484 |
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok. |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
if ( ! isset( $_POST['powerkit_custom_fonts_id'] ) ) { // Input var ok. |
| 489 |
return; |
| 490 |
} |
| 491 |
|
| 492 |
// Update id counter. |
| 493 |
if ( isset( $_POST['save_settings'] ) ) { // Input var ok. |
| 494 |
update_option( 'powerkit_custom_fonts_counter', intval( get_option( 'powerkit_custom_fonts_counter', 1 ) ) + 1 ); |
| 495 |
} |
| 496 |
|
| 497 |
// ID Font. |
| 498 |
$id = sanitize_key( $_POST['powerkit_custom_fonts_id'] ); // Input var ok. |
| 499 |
|
| 500 |
// Custom List. |
| 501 |
$font_list = (array) get_option( 'powerkit_custom_fonts_list', array() ); |
| 502 |
|
| 503 |
// Reset current settings. |
| 504 |
$font_list[ $id ] = array(); |
| 505 |
|
| 506 |
// Set new settings. |
| 507 |
if ( isset( $_POST['powerkit_custom_fonts_name'] ) ) { // Input var ok. |
| 508 |
$font_list[ $id ]['slug'] = sanitize_title( $_POST['powerkit_custom_fonts_name'] ); // Input var ok; sanitization ok. |
| 509 |
} |
| 510 |
if ( isset( $_POST['powerkit_custom_fonts_name'] ) ) { // Input var ok. |
| 511 |
$font_list[ $id ]['name'] = sanitize_text_field( $_POST['powerkit_custom_fonts_name'] ); // Input var ok; sanitization ok. |
| 512 |
} |
| 513 |
if ( isset( $_POST['powerkit_custom_fonts_file_woff'] ) ) { // Input var ok. |
| 514 |
$font_list[ $id ]['file_woff'] = sanitize_text_field( $_POST['powerkit_custom_fonts_file_woff'] ); // Input var ok; sanitization ok. |
| 515 |
} |
| 516 |
if ( isset( $_POST['powerkit_custom_fonts_file_woff2'] ) ) { // Input var ok. |
| 517 |
$font_list[ $id ]['file_woff2'] = sanitize_text_field( $_POST['powerkit_custom_fonts_file_woff2'] ); // Input var ok; sanitization ok. |
| 518 |
} |
| 519 |
if ( isset( $_POST['powerkit_custom_fonts_weight'] ) ) { // Input var ok. |
| 520 |
$font_list[ $id ]['weight'] = sanitize_text_field( $_POST['powerkit_custom_fonts_weight'] ); // Input var ok; sanitization ok. |
| 521 |
} |
| 522 |
if ( isset( $_POST['powerkit_custom_fonts_style'] ) ) { // Input var ok. |
| 523 |
$font_list[ $id ]['style'] = sanitize_text_field( $_POST['powerkit_custom_fonts_style'] ); // Input var ok; sanitization ok. |
| 524 |
} |
| 525 |
|
| 526 |
// Save list. |
| 527 |
update_option( 'powerkit_custom_fonts_list', $font_list ); |
| 528 |
|
| 529 |
// Output message. |
| 530 |
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Settings saved.', 'powerkit' ) ); |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* Register fonts in the editor. |
| 535 |
*/ |
| 536 |
public function editor_enqueue() { |
| 537 |
global $pagenow; |
| 538 |
|
| 539 |
if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) { |
| 540 |
$this->frontend_enqueue(); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* Frontend enqueue |
| 546 |
* |
| 547 |
* @since 1.0.0 |
| 548 |
*/ |
| 549 |
public function frontend_enqueue() { |
| 550 |
|
| 551 |
$custom_fonts = get_option( 'powerkit_custom_fonts_list' ); |
| 552 |
|
| 553 |
if ( is_array( $custom_fonts ) && $custom_fonts ) { |
| 554 |
|
| 555 |
$exclude = array(); |
| 556 |
|
| 557 |
$font_face = null; |
| 558 |
|
| 559 |
foreach ( $custom_fonts as $key => $item ) { |
| 560 |
|
| 561 |
$id = sanitize_title( $item['name'] ); |
| 562 |
|
| 563 |
if ( $id ) { |
| 564 |
$src = array(); |
| 565 |
$files = array(); |
| 566 |
|
| 567 |
$files['woff'] = wp_get_attachment_url( $item['file_woff'] ); |
| 568 |
$files['woff2'] = wp_get_attachment_url( $item['file_woff2'] ); |
| 569 |
|
| 570 |
foreach ( $files as $key_file => $file_url ) { |
| 571 |
if ( $file_url ) { |
| 572 |
$src[] = sprintf( 'url("%1$s") format("%2$s")', $file_url, $key_file ); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
if ( $src ) { |
| 577 |
$src_line = implode( ',', $src ); |
| 578 |
|
| 579 |
$display = 'async' === $this->load_method ? 'swap' : 'auto'; |
| 580 |
|
| 581 |
$font_face .= sprintf( '@font-face { font-family: "%1$s"; src: %2$s; font-display: %3$s; font-weight: %4$s; font-style: %5$s;}', $id, $src_line, $display, $item['weight'], $item['style'] ); |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
if ( $font_face ) { |
| 587 |
?> |
| 588 |
<style><?php print( $font_face ); // XSS. ?></style> |
| 589 |
<?php |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
new Powerkit_Custom_Fonts(); |
| 596 |
|