custom_font_post_type_callback(); add_action( 'add_meta_boxes', array( $this, 'init_metabox_callback' ) ); add_action( 'save_post', array( $this, 'metabox_save_data' ) ); add_filter( 'manage_ultp_custom_font_posts_columns', array( $this, 'templates_table_head' ) ); add_action( 'manage_ultp_custom_font_posts_custom_column', array( $this, 'templates_table_content' ), 10, 2 ); add_filter( 'upload_mimes', array( $this, 'add_file_types_to_uploads' ) ); add_filter( 'wp_check_filetype_and_ext', array( $this, 'font_correct_filetypes' ), 10, 5 ); add_filter( 'wp_handle_upload_prefilter', array( $this, 'sanitize_svg_upload' ) ); add_filter( 'wp_handle_sideload_prefilter', array( $this, 'sanitize_svg_upload' ) ); add_filter( 'enter_title_here', array( $this, 'update_custom_font_title' ), 10, 2 ); } public function update_custom_font_title( $title, $post ) { if ( isset( $post->post_type ) && 'ultp_custom_font' === $post->post_type ) { return __( 'Add Font Family Name', 'ultimate-post' ); } return $title; } public function font_correct_filetypes( $data, $file, $filename, $mimes, $real_mime ) { if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) { return $data; } $wp_file_type = wp_check_filetype( $filename, $mimes ); if ( 'ttf' === $wp_file_type['ext'] ) { $data['ext'] = 'ttf'; $data['type'] = 'font/ttf'; } return $data; } public function add_file_types_to_uploads( $file_types ) { if ( 'ultp_custom_font' == get_post_type() || current_user_can( 'manage_options' ) ) { $new_filetypes = array(); $new_filetypes['woff'] = 'font/woff'; $new_filetypes['woff2'] = 'font/woff2'; $new_filetypes['ttf'] = 'font/ttf'; $new_filetypes['eot'] = 'font/ttf'; // SVG only for a custom font, where every upload is sanitized by sanitize_svg_upload(). if ( $this->is_custom_font_upload() ) { $new_filetypes['svg'] = 'image/svg+xml'; } $file_types = array_merge( $file_types, $new_filetypes ); return $file_types; } else { return $file_types; } } /** * Whether the current request uploads to, or edits, a custom font the user may edit. * * Uploads from the font screen's media modal send the font as `post_id`; async-upload.php * checks its own nonce and refuses a `post_id` the user cannot edit. The screen itself is * recognised by the global post. * * @return bool */ private function is_custom_font_upload() { if ( ! current_user_can( 'manage_options' ) ) { return false; } $post_id = isset( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read only to choose the context; async-upload.php verifies the media-form nonce. if ( ! $post_id ) { $post_id = (int) get_the_ID(); } return $post_id > 0 && 'ultp_custom_font' === get_post_type( $post_id ) && current_user_can( 'edit_post', $post_id ); } /** * Clean an SVG uploaded to a custom font. * * Only runs for custom font uploads, the one place this add-on allows SVG. The file keeps * only its font markup; an SVG that is not a font is refused. SVG uploads anywhere else * are not touched here: WordPress refuses them unless another plugin allows SVG. * * @param array $file Upload data from `$_FILES`. * @return array */ public function sanitize_svg_upload( $file ) { if ( empty( $file['name'] ) || empty( $file['tmp_name'] ) || ! empty( $file['error'] ) ) { return $file; } if ( 'svg' !== strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) ) ) { return $file; } if ( ! $this->is_custom_font_upload() ) { return $file; } require_once __DIR__ . '/Svg_Font_Sanitizer.php'; $sanitized = Svg_Font_Sanitizer::sanitize_file( $file['tmp_name'] ); if ( is_wp_error( $sanitized ) ) { $file['error'] = $sanitized->get_error_message(); } return $file; } /** * Font weight as stored and printed: 100-900 in steps of 100, normal or bold. * * @param string $weight Raw weight. * @return string */ public static function sanitize_font_weight( $weight ) { $weight = strtolower( trim( (string) $weight ) ); if ( in_array( $weight, array( 'normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900' ), true ) ) { return $weight; } return 'normal'; } /** * Font family name safe to print inside a CSS string or a style attribute. * * @param string $font_name Post title of the font. * @return string */ public static function sanitize_font_family( $font_name ) { return str_replace( array( '"', '\\', ';', '{', '}', '<', '>' ), '', (string) $font_name ); } // Template Heading Add public function templates_table_head( $defaults ) { $type_array = array( 'preview' => '' . __( 'Preview', 'ultimate-post' ) . '', 'woff' => __( 'WOFF', 'ultimate-post' ), 'woff2' => __( 'WOFF2', 'ultimate-post' ), 'ttf' => __( 'TTF', 'ultimate-post' ), 'svg' => __( 'SVG', 'ultimate-post' ), 'eot' => __( 'EOT', 'ultimate-post' ), ); $defaults['title'] = __( 'Font Family', 'ultimate-post' ); array_splice( $defaults, 2, 0, $type_array ); return $defaults; } // Get Font Face public function get_font_face( $settings, $font_name ) { $font_src = array(); if ( $settings['woff'] ) { array_push( $font_src, 'url(' . esc_url( $settings['woff'] ) . ') format("woff")' ); } if ( $settings['woff2'] ) { array_push( $font_src, 'url(' . esc_url( $settings['woff2'] ) . ') format("woff2")' ); } if ( $settings['ttf'] ) { array_push( $font_src, 'url(' . esc_url( $settings['ttf'] ) . ') format("TrueType")' ); } if ( $settings['svg'] ) { array_push( $font_src, 'url(' . esc_url( $settings['svg'] ) . ') format("svg")' ); } if ( $settings['eot'] ) { array_push( $font_src, 'url(' . esc_url( $settings['eot'] ) . ') format("eot")' ); } $font_face = '@font-face { font-family: "' . self::sanitize_font_family( $font_name ) . '"; font-weight: ' . self::sanitize_font_weight( isset( $settings['weight'] ) ? $settings['weight'] : '' ) . '; src: ' . implode( ', ', $font_src ) . '; }'; return $font_face; } // Column Content public function templates_table_content( $column_id, $post_id ) { $woff = $woff2 = $ttf = $svg = $eot = false; $settings = get_post_meta( $post_id, '__font_settings', true ); if ( $settings ) { foreach ( $settings as $key => $value ) { if ( $value['woff'] ) { $woff = true; } if ( $value['woff2'] ) { $woff2 = true; } if ( $value['ttf'] ) { $ttf = true; } if ( $value['svg'] ) { $svg = true; } if ( $value['eot'] ) { $eot = true; } } $font_face = $this->get_font_face( $settings[0], get_the_title( $post_id ) ); echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS from get_font_face(): URLs pass esc_url(), family and weight pass sanitize_font_family()/sanitize_font_weight(). switch ( $column_id ) { case 0: echo '' . esc_html__( 'The quick brown fox jumps over the lazy dog.', 'ultimate-post' ) . ''; break; case 1: echo ''; break; case 2: echo ''; break; case 3: echo ''; break; case 4: echo ''; break; case 5: echo ''; break; default: break; } } } function init_metabox_callback() { add_meta_box( 'ultp-custom-font-id', __( 'Font Vaiations', 'ultimate-post' ), array( $this, 'custom_font_callback' ), 'ultp_custom_font', 'advanced' ); } function set_data( $arr = array(), $font_name = '' ) { ?>