| 1 |
<?php |
| 2 |
namespace ULTP; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Custom_Font { |
| 7 |
public function __construct() { |
| 8 |
$this->custom_font_post_type_callback(); |
| 9 |
add_action( 'add_meta_boxes', array( $this, 'init_metabox_callback' ) ); |
| 10 |
add_action( 'save_post', array( $this, 'metabox_save_data' ) ); |
| 11 |
add_filter( 'manage_ultp_custom_font_posts_columns', array( $this, 'templates_table_head' ) ); |
| 12 |
add_action( 'manage_ultp_custom_font_posts_custom_column', array( $this, 'templates_table_content' ), 10, 2 ); |
| 13 |
add_filter( 'upload_mimes', array( $this, 'add_file_types_to_uploads' ) ); |
| 14 |
add_filter( 'wp_check_filetype_and_ext', array( $this, 'font_correct_filetypes' ), 10, 5 ); |
| 15 |
add_filter( 'enter_title_here', array( $this, 'update_custom_font_title' ), 10, 2 ); |
| 16 |
} |
| 17 |
|
| 18 |
public function update_custom_font_title( $title, $post ) { |
| 19 |
if ( isset( $post->post_type ) && 'ultp_custom_font' === $post->post_type ) { |
| 20 |
return __( 'Add Font Family Name', 'ultimate-post' ); |
| 21 |
} |
| 22 |
return $title; |
| 23 |
} |
| 24 |
|
| 25 |
public function font_correct_filetypes( $data, $file, $filename, $mimes, $real_mime ) { |
| 26 |
if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) { |
| 27 |
return $data; |
| 28 |
} |
| 29 |
|
| 30 |
$wp_file_type = wp_check_filetype( $filename, $mimes ); |
| 31 |
|
| 32 |
if ( 'ttf' === $wp_file_type['ext'] ) { |
| 33 |
$data['ext'] = 'ttf'; |
| 34 |
$data['type'] = 'font/ttf'; |
| 35 |
} |
| 36 |
return $data; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
public function add_file_types_to_uploads( $file_types ) { |
| 41 |
if ( 'ultp_custom_font' == get_post_type() || current_user_can( 'manage_options' ) ) { |
| 42 |
$new_filetypes = array(); |
| 43 |
$new_filetypes['woff'] = 'font/woff'; |
| 44 |
$new_filetypes['woff2'] = 'font/woff2'; |
| 45 |
$new_filetypes['ttf'] = 'font/ttf'; |
| 46 |
$new_filetypes['svg'] = 'image/svg+xml'; |
| 47 |
$new_filetypes['eot'] = 'font/ttf'; |
| 48 |
$file_types = array_merge( $file_types, $new_filetypes ); |
| 49 |
return $file_types; |
| 50 |
} else { |
| 51 |
return $file_types; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
// Template Heading Add |
| 57 |
public function templates_table_head( $defaults ) { |
| 58 |
$type_array = array( |
| 59 |
'preview' => '<span class="ultp-custom-font-preview-th">' . __( 'Preview', 'ultimate-post' ) . '</span>', |
| 60 |
'woff' => __( 'WOFF', 'ultimate-post' ), |
| 61 |
'woff2' => __( 'WOFF2', 'ultimate-post' ), |
| 62 |
'ttf' => __( 'TTF', 'ultimate-post' ), |
| 63 |
'svg' => __( 'SVG', 'ultimate-post' ), |
| 64 |
'eot' => __( 'EOT', 'ultimate-post' ), |
| 65 |
); |
| 66 |
$defaults['title'] = __( 'Font Family', 'ultimate-post' ); |
| 67 |
array_splice( $defaults, 2, 0, $type_array ); |
| 68 |
|
| 69 |
return $defaults; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
// Get Font Face |
| 74 |
public function get_font_face( $settings, $font_name ) { |
| 75 |
$font_src = array(); |
| 76 |
if ( $settings['woff'] ) { |
| 77 |
array_push( $font_src, 'url(' . esc_url( $settings['woff'] ) . ') format("woff")' ); |
| 78 |
} |
| 79 |
if ( $settings['woff2'] ) { |
| 80 |
array_push( $font_src, 'url(' . esc_url( $settings['woff2'] ) . ') format("woff2")' ); |
| 81 |
} |
| 82 |
if ( $settings['ttf'] ) { |
| 83 |
array_push( $font_src, 'url(' . esc_url( $settings['ttf'] ) . ') format("TrueType")' ); |
| 84 |
} |
| 85 |
if ( $settings['svg'] ) { |
| 86 |
array_push( $font_src, 'url(' . esc_url( $settings['svg'] ) . ') format("svg")' ); |
| 87 |
} |
| 88 |
if ( $settings['eot'] ) { |
| 89 |
array_push( $font_src, 'url(' . esc_url( $settings['eot'] ) . ') format("eot")' ); |
| 90 |
} |
| 91 |
$font_face = '@font-face { |
| 92 |
font-family: "' . $font_name . '"; |
| 93 |
font-weight: ' . $settings['weight'] . '; |
| 94 |
src: ' . implode( ', ', $font_src ) . '; |
| 95 |
}'; |
| 96 |
|
| 97 |
return $font_face; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
// Column Content |
| 102 |
public function templates_table_content( $column_id, $post_id ) { |
| 103 |
$woff = $woff2 = $ttf = $svg = $eot = false; |
| 104 |
$settings = get_post_meta( $post_id, '__font_settings', true ); |
| 105 |
|
| 106 |
if ( $settings ) { |
| 107 |
foreach ( $settings as $key => $value ) { |
| 108 |
if ( $value['woff'] ) { |
| 109 |
$woff = true; } |
| 110 |
if ( $value['woff2'] ) { |
| 111 |
$woff2 = true; } |
| 112 |
if ( $value['ttf'] ) { |
| 113 |
$ttf = true; } |
| 114 |
if ( $value['svg'] ) { |
| 115 |
$svg = true; } |
| 116 |
if ( $value['eot'] ) { |
| 117 |
$eot = true; } |
| 118 |
} |
| 119 |
$font_face = $this->get_font_face( $settings[0], get_the_title( $post_id ) ); |
| 120 |
echo '<style type="text/css">' . $font_face . '</style>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 121 |
|
| 122 |
switch ( $column_id ) { |
| 123 |
case 0: |
| 124 |
echo '<span class="ultp-custom-font-preview" style="font-family: ' . esc_attr( get_the_title( $post_id ) ) . '">' . esc_html__( 'The quick brown fox jumps over the lazy dog.', 'ultimate-post' ) . '</span>'; |
| 125 |
break; |
| 126 |
case 1: |
| 127 |
echo '<span class="dashicons ' . ( $woff ? 'dashicons-yes' : 'dashicons-no-alt' ) . '"></span>'; |
| 128 |
break; |
| 129 |
case 2: |
| 130 |
echo '<span class="dashicons ' . ( $woff2 ? 'dashicons-yes' : 'dashicons-no-alt' ) . '"></span>'; |
| 131 |
break; |
| 132 |
case 3: |
| 133 |
echo '<span class="dashicons ' . ( $ttf ? 'dashicons-yes' : 'dashicons-no-alt' ) . '"></span>'; |
| 134 |
break; |
| 135 |
case 4: |
| 136 |
echo '<span class="dashicons ' . ( $svg ? 'dashicons-yes' : 'dashicons-no-alt' ) . '"></span>'; |
| 137 |
break; |
| 138 |
case 5: |
| 139 |
echo '<span class="dashicons ' . ( $eot ? 'dashicons-yes' : 'dashicons-no-alt' ) . '"></span>'; |
| 140 |
break; |
| 141 |
default: |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
function init_metabox_callback() { |
| 149 |
add_meta_box( |
| 150 |
'ultp-custom-font-id', |
| 151 |
__( 'Font Vaiations', 'ultimate-post' ), |
| 152 |
array( $this, 'custom_font_callback' ), |
| 153 |
'ultp_custom_font', |
| 154 |
'advanced' |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
function set_data( $arr = array(), $font_name = '' ) { |
| 160 |
?> |
| 161 |
<div class="ultp-custom-font-container ultp-custom-font<?php echo empty( $arr ) ? '-copy' : ''; ?>"> |
| 162 |
<div class="ultp-custom-font-heading"> |
| 163 |
<div> |
| 164 |
<label class="font-label"><?php echo esc_html__( 'Weight: ', 'ultimate-post' ); ?> <span class="ultp-custom-font-weight"> <?php echo esc_html__( isset( $arr['weight'] ) ? $arr['weight'] : '', 'ultimate-post' ); ?> </span></label> |
| 165 |
<select name="weight[]"> |
| 166 |
<?php $weight = isset( $arr['weight'] ) ? $arr['weight'] : ''; ?> |
| 167 |
<option <?php selected( $weight, 'normal' ); ?> value="normal"><?php echo esc_html__( 'Normal', 'ultimate-post' ); ?></option> |
| 168 |
<option <?php selected( $weight, '100' ); ?> value="100"><?php echo esc_html__( '100', 'ultimate-post' ); ?></option> |
| 169 |
<option <?php selected( $weight, '200' ); ?> value="200"><?php echo esc_html__( '200', 'ultimate-post' ); ?></option> |
| 170 |
<option <?php selected( $weight, '300' ); ?> value="300"><?php echo esc_html__( '300', 'ultimate-post' ); ?></option> |
| 171 |
<option <?php selected( $weight, '400' ); ?> value="400"><?php echo esc_html__( '400', 'ultimate-post' ); ?></option> |
| 172 |
<option <?php selected( $weight, '500' ); ?> value="500"><?php echo esc_html__( '500', 'ultimate-post' ); ?></option> |
| 173 |
<option <?php selected( $weight, '600' ); ?> value="600"><?php echo esc_html__( '600', 'ultimate-post' ); ?></option> |
| 174 |
<option <?php selected( $weight, '700' ); ?> value="700"><?php echo esc_html__( '700', 'ultimate-post' ); ?></option> |
| 175 |
<option <?php selected( $weight, '800' ); ?> value="800"><?php echo esc_html__( '800', 'ultimate-post' ); ?></option> |
| 176 |
<option <?php selected( $weight, '900' ); ?> value="900"><?php echo esc_html__( '900', 'ultimate-post' ); ?></option> |
| 177 |
</select> |
| 178 |
</div> |
| 179 |
<?php |
| 180 |
$styles = ''; |
| 181 |
if ( ! empty( $arr ) ) { |
| 182 |
$font_face = $this->get_font_face( $arr, $font_name ); |
| 183 |
echo '<style type="text/css">' . $font_face . '</style>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 184 |
$styles = 'style="font-family: ' . $font_name . '; font-weight: ' . $arr['weight'] . ' "'; |
| 185 |
} |
| 186 |
?> |
| 187 |
<span <?php echo $styles; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> class="ultp-custom-font-preview"><?php echo esc_html__( 'The quick brown fox jumps over the lazy dog', 'ultimate-post' ); ?></span> |
| 188 |
<div class="ultp-custom-font-actions"> |
| 189 |
<span class="ultp-custom-font-edit"><span class="dashicons dashicons-edit"></span><?php echo esc_html__( 'Edit', 'ultimate-post' ); ?></span> |
| 190 |
<span class="ultp-custom-font-close"><span class="dashicons dashicons-no-alt"></span><?php echo esc_html__( 'Close', 'ultimate-post' ); ?></span> |
| 191 |
<span class="ultp-custom-font-delete"><span class="dashicons dashicons-trash"></span><?php echo esc_html__( 'Delete', 'ultimate-post' ); ?></span> |
| 192 |
</div> |
| 193 |
</div> |
| 194 |
<div class="ultp-custom-font-content"> |
| 195 |
<div class="ultp-font-file-list"> |
| 196 |
<label><?php echo esc_html__( 'WOFF File', 'ultimate-post' ); ?></label> |
| 197 |
<input type="text" name="woff[]" value="<?php echo esc_attr( isset( $arr['woff'] ) ? $arr['woff'] : '' ); ?>" placeholder="<?php echo esc_html__( 'The web open Font Format, Used by Modern Browsers', 'ultimate-post' ); ?>"/> |
| 198 |
<span class="button ultp-font-upload"><span class="dashicons dashicons-upload"></span><?php echo esc_html__( 'Upload', 'ultimate-post' ); ?></span> |
| 199 |
</div> |
| 200 |
<div class="ultp-font-file-list"> |
| 201 |
<label><?php echo esc_html__( 'WOFF2 File', 'ultimate-post' ); ?></label> |
| 202 |
<input type="text" name="woff2[]" value="<?php echo esc_attr( isset( $arr['woff2'] ) ? $arr['woff2'] : '' ); ?>" placeholder="<?php echo esc_html__( 'The web open Font Format 2, Used by Super Modern Browsers', 'ultimate-post' ); ?>"/> |
| 203 |
<span class="button ultp-font-upload"><span class="dashicons dashicons-upload"></span><?php echo esc_html__( 'Upload', 'ultimate-post' ); ?></span> |
| 204 |
</div> |
| 205 |
<div class="ultp-font-file-list"> |
| 206 |
<label><?php echo esc_html__( 'TTF File', 'ultimate-post' ); ?></label> |
| 207 |
<input type="text" name="ttf[]" value="<?php echo esc_attr( isset( $arr['ttf'] ) ? $arr['ttf'] : '' ); ?>" placeholder="<?php echo esc_html__( 'TrueType Fonts, Used for better supporting Safari, Android, iOS', 'ultimate-post' ); ?>"/> |
| 208 |
<span class="button ultp-font-upload"><span class="dashicons dashicons-upload"></span><?php echo esc_html__( 'Upload', 'ultimate-post' ); ?></span> |
| 209 |
</div> |
| 210 |
<div class="ultp-font-file-list"> |
| 211 |
<label><?php echo esc_html__( 'SVG File', 'ultimate-post' ); ?></label> |
| 212 |
<input type="text" name="svg[]" value="<?php echo esc_attr( isset( $arr['svg'] ) ? $arr['svg'] : '' ); ?>" placeholder="<?php echo esc_html__( 'SVG font allow SVG to be used as glyphs when displaying text, Used by Legacy iOS', 'ultimate-post' ); ?>"/> |
| 213 |
<span class="button ultp-font-upload"><span class="dashicons dashicons-upload"></span><?php echo esc_html__( 'Upload', 'ultimate-post' ); ?></span> |
| 214 |
</div> |
| 215 |
<div class="ultp-font-file-list"> |
| 216 |
<label><?php echo esc_html__( 'EOT File', 'ultimate-post' ); ?></label> |
| 217 |
<input type="text" name="eot[]" value="<?php echo esc_attr( isset( $arr['eot'] ) ? $arr['eot'] : '' ); ?>" placeholder="<?php echo esc_html__( 'Embedded OpenType, Used by IE6-IE9 Browsers', 'ultimate-post' ); ?>"/> |
| 218 |
<span class="button ultp-font-upload"><span class="dashicons dashicons-upload"></span><?php echo esc_html__( 'Upload', 'ultimate-post' ); ?></span> |
| 219 |
</div> |
| 220 |
</div> |
| 221 |
</div> |
| 222 |
<?php |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
function custom_font_callback( $post ) { |
| 227 |
wp_nonce_field( 'font_meta_box', 'custom_font_nonce' ); |
| 228 |
$settings = get_post_meta( $post->ID, '__font_settings', true ); |
| 229 |
|
| 230 |
$this->set_data(); // Set Empty Data |
| 231 |
|
| 232 |
if ( is_array( $settings ) && ! empty( $settings ) ) { |
| 233 |
foreach ( $settings as $key => $val ) { |
| 234 |
$this->set_data( $val, $post->post_title ); |
| 235 |
} |
| 236 |
} |
| 237 |
echo '<span class="button ultp-font-variation-action">' . esc_html__( 'Add Variation', 'ultimate-post' ) . '</span>'; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
function metabox_save_data( $post_id ) { |
| 242 |
if ( ! isset( $_POST['custom_font_nonce'] ) ) { |
| 243 |
return; } |
| 244 |
if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['custom_font_nonce'] ) ), 'font_meta_box' ) ) { |
| 245 |
return; } |
| 246 |
|
| 247 |
$arr = array(); |
| 248 |
if ( isset( $_POST['weight'] ) && is_array( $_POST['weight'] ) ) { |
| 249 |
foreach ($_POST['weight'] as $i => $value) { //phpcs:ignore |
| 250 |
if ( isset( $_POST['weight'][ $i ] ) && |
| 251 |
( ! empty( $_POST['woff'][ $i ] ) || |
| 252 |
! empty( $_POST['woff2'][ $i ] ) || |
| 253 |
! empty( $_POST['ttf'][ $i ] ) || |
| 254 |
! empty( $_POST['svg'][ $i ] ) || |
| 255 |
! empty( $_POST['eot'][ $i ] ) ) ) { |
| 256 |
$temp = array(); |
| 257 |
$temp['weight'] = isset( $_POST['weight'][ $i ] ) ? sanitize_text_field( $_POST['weight'][ $i ] ) : ''; |
| 258 |
$temp['woff'] = isset( $_POST['woff'][ $i ] ) ? sanitize_text_field( $_POST['woff'][ $i ] ) : ''; |
| 259 |
$temp['woff2'] = isset( $_POST['woff2'][ $i ] ) ? sanitize_text_field( $_POST['woff2'][ $i ] ) : ''; |
| 260 |
$temp['ttf'] = isset( $_POST['ttf'][ $i ] ) ? sanitize_text_field( $_POST['ttf'][ $i ] ) : ''; |
| 261 |
$temp['svg'] = isset( $_POST['svg'][ $i ] ) ? sanitize_text_field( $_POST['svg'][ $i ] ) : ''; |
| 262 |
$temp['eot'] = isset( $_POST['eot'][ $i ] ) ? sanitize_text_field( $_POST['eot'][ $i ] ) : ''; |
| 263 |
$arr[] = $temp; |
| 264 |
} |
| 265 |
} |
| 266 |
update_post_meta( $post_id, '__font_settings', $arr ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// Templates Post Type Register |
| 271 |
public function custom_font_post_type_callback() { |
| 272 |
$labels = array( |
| 273 |
'name' => _x( 'Custom Fonts', 'Custom Font', 'ultimate-post' ), |
| 274 |
'singular_name' => _x( 'Saved Custom Font', 'Custom Font', 'ultimate-post' ), |
| 275 |
'menu_name' => __( 'Saved Custom Font', 'ultimate-post' ), |
| 276 |
'parent_item_colon' => __( 'Parent Custom Font', 'ultimate-post' ), |
| 277 |
'all_items' => __( 'Saved Custom Font', 'ultimate-post' ), |
| 278 |
'view_item' => __( 'View Custom Font', 'ultimate-post' ), |
| 279 |
'add_new_item' => __( 'Add New', 'ultimate-post' ), |
| 280 |
'add_new' => __( 'Add New', 'ultimate-post' ), |
| 281 |
'edit_item' => __( 'Edit Custom Font', 'ultimate-post' ), |
| 282 |
'update_item' => __( 'Update Custom Font', 'ultimate-post' ), |
| 283 |
'search_items' => __( 'Search Custom Font', 'ultimate-post' ), |
| 284 |
'not_found' => __( 'No Custom Font Found', 'ultimate-post' ), |
| 285 |
'not_found_in_trash' => __( 'Not Custom Font found in Trash', 'ultimate-post' ), |
| 286 |
); |
| 287 |
$args = array( |
| 288 |
'labels' => $labels, |
| 289 |
'show_in_rest' => true, |
| 290 |
'supports' => array( 'title' ), |
| 291 |
'hierarchical' => false, |
| 292 |
'public' => false, |
| 293 |
'rewrite' => false, |
| 294 |
'show_ui' => true, |
| 295 |
'show_in_menu' => false, |
| 296 |
'show_in_nav_menus' => false, |
| 297 |
'exclude_from_search' => true, |
| 298 |
'capability_type' => 'page', |
| 299 |
); |
| 300 |
register_post_type( 'ultp_custom_font', $args ); |
| 301 |
} |
| 302 |
} |
| 303 |
|