first commit

This commit is contained in:
Roman Pyrih
2023-07-24 08:30:51 +02:00
commit c2e100a763
7128 changed files with 1622619 additions and 0 deletions

View File

@@ -0,0 +1,148 @@
<?php
/**
* Created by PhpStorm.
* User: alex
* Date: 7/18/18
* Time: 10:48 AM
*/
class Brizy_Admin_Fonts_Api extends Brizy_Admin_AbstractApi {
const nonce = 'brizy-api';
const AJAX_CREATE_FONT_ACTION = '-create-font';
const AJAX_DELETE_FONT_ACTION = '-delete-font';
const AJAX_GET_FONTS_ACTION = '-get-fonts';
/**
* @var Brizy_Admin_Fonts_Manager
*/
private $fontManager;
/**
* @return Brizy_Admin_Fonts_Api
*/
public static function _init() {
static $instance;
if ( ! $instance ) {
$instance = new self();
}
return $instance;
}
/**
* Brizy_Admin_Fonts_Api constructor.
*/
public function __construct() {
$this->fontManager = new Brizy_Admin_Fonts_Manager();
parent::__construct();
}
/**
* @return null
*/
protected function getRequestNonce() {
return $this->param( 'hash' );
}
protected function initializeApiActions() {
$pref = 'wp_ajax_' . Brizy_Editor::prefix();
add_action( $pref . self::AJAX_CREATE_FONT_ACTION, array( $this, 'actionCreateFont' ) );
add_action( $pref . self::AJAX_DELETE_FONT_ACTION, array( $this, 'actionDeleteFont' ) );
add_action( $pref . self::AJAX_GET_FONTS_ACTION, array( $this, 'actionGetFonts' ) );
}
public function actionGetFonts() {
$this->verifyNonce( self::nonce );
$manager = new Brizy_Admin_Fonts_Manager();
$this->success( $manager->getAllFonts() );
}
public function actionCreateFont() {
try {
$this->verifyNonce( self::nonce );
if ( ! ( $fontUidId = $this->param( 'id' ) ) ) {
$this->error( 400, 'Invalid font uid' );
}
if ( ! ( $family = $this->param( 'family' ) ) ) {
$this->error( 400, 'Invalid font family' );
}
if ( ! ( $fontType = $this->param( 'type' ) ) ) {
$fontType = 'uploaded';
}
if ( ! isset( $_FILES['fonts'] ) ) {
$this->error( 400, 'Invalid font files' );
}
$existingFont = $this->fontManager->getFontByFamily( $fontUidId, $family, $fontType );
if ( $existingFont ) {
$this->error( 400, 'This font family already exists.' );
}
try {
$files = array();
// create font attachments
foreach ( $_FILES['fonts']['name'] as $weight => $attachments ) {
foreach ( $attachments as $type => $file ) {
$files[ $weight ][ $type ] = array(
'name' => $_FILES['fonts']['name'][ $weight ][ $type ],
'type' => $_FILES['fonts']['type'][ $weight ][ $type ],
'tmp_name' => $_FILES['fonts']['tmp_name'][ $weight ][ $type ],
'error' => $_FILES['fonts']['error'][ $weight ][ $type ],
'size' => $_FILES['fonts']['size'][ $weight ][ $type ]
);
}
}
$fontPostId = $this->fontManager->createFont( $fontUidId, $family, $files, $fontType );
} catch ( Exception $e ) {
Brizy_Logger::instance()->debug( 'Create font ERROR', [ $e ] );
$this->error( 400, $e->getMessage() );
}
$fontUidId = get_post_meta( $fontPostId, 'brizy_post_uid', true );
$font = $this->fontManager->getFont( $fontUidId );
$this->success( $font );
} catch ( Exception $exception ) {
Brizy_Logger::instance()->critical( $exception->getMessage(), [ $exception ] );
$this->error( 400, $exception->getMessage() );
}
}
public function actionDeleteFont() {
$this->verifyNonce( self::nonce );
if ( ! ( $fontId = $this->param( 'id' ) ) ) {
$this->error( 400, 'Invalid font id' );
}
$manager = new Brizy_Admin_Fonts_Manager();
try {
$manager->deleteFont( $fontId );
} catch ( Exception $exception ) {
Brizy_Logger::instance()->critical( $exception->getMessage(), [ $exception ] );
$this->error( $exception->getCode(), $exception->getMessage() );
}
$this->success( [] );
}
}

View File

@@ -0,0 +1,6 @@
<?php
class Brizy_Admin_Fonts_Exception_DuplicateFont extends Exception
{
}

View File

@@ -0,0 +1,165 @@
<?php
class Brizy_Admin_Fonts_Handler extends Brizy_Public_AbstractProxy {
const ENDPOINT = '-font';
/**
* @return array
*/
protected function get_endpoint_keys() {
return array( Brizy_Editor::prefix( self::ENDPOINT ) );
}
/**
* @return mixed|void
* @throws Twig_Error_Loader
* @throws Twig_Error_Runtime
* @throws Twig_Error_Syntax
*/
public function process_query() {
global $wp_query;
$vars = $wp_query->query_vars;
// Check if user is not querying API
$ENDPOINT = Brizy_Editor::prefix( self::ENDPOINT );
if ( ! isset( $vars[ $ENDPOINT ] ) || ! is_string( $vars[ $ENDPOINT ] ) ) {
return;
}
session_write_close();
$fontQueries = $this->explodeFont( $vars[ $ENDPOINT ] );
if ( count( $fontQueries ) == 0 ) {
return;
}
$contexts = array();
foreach ( $fontQueries as $fontUid => $weights ) {
$contexts[ $fontUid ] = array();
$fontPost = $this->getFont( $fontUid );
if ( ! $fontPost ) {
continue;
}
foreach ( $weights as $weight ) {
$contexts[ $fontUid ][ $weight ] = $this->getFontWeightFileUrls( $fontPost->ID, $weight );
}
}
header( 'Content-Type: text/css' );
$twigEngine = Brizy_TwigEngine::instance( path_join( BRIZY_PLUGIN_PATH, "admin/fonts/views" ) );
$twigEngine->getEnvironment()
->addFilter( new Twig_SimpleFilter( 'fontStyle', function ( $weight ) {
$weight = preg_replace( "/\d+/", "", $weight );
if ( trim( $weight ) == "" ) {
return 'normal';
}
return $weight;
} ) );
$twigEngine->getEnvironment()
->addFilter( new Twig_SimpleFilter( 'fontType', function ( $type ) {
if ( $type == 'ttf' ) {
return 'truetype';
} else {
return $type;
}
} ) );
$twigEngine->getEnvironment()->addFilter( new Twig_SimpleFilter( 'fontWeight', function ( $weight ) {
return trim( preg_replace( "/[^\d]+/", "", $weight ) );
} ) );
echo $twigEngine->render( 'fonts.css.twig', array(
'fonts' => $contexts
) );
exit;
}
/**
* @param $request
*
* @return array
*/
private function explodeFont( $request ) {
$fonts = explode( "|", $request );
$fontsParsed = array();
foreach ( $fonts as $fontRequest ) {
$font = explode( ':', $fontRequest );
$fontsParsed[ $font[0] ] = explode( ',', $font['1'] );
}
return $fontsParsed;
}
/**
* @param $uid
*
* @return mixed|null
*/
private function getFont( $uid ) {
$fonts = get_posts( [
'post_type' => Brizy_Admin_Fonts_Main::CP_FONT,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'brizy_post_uid',
'value' => $uid
)
),
] );
if ( is_array( $fonts ) && isset( $fonts[0] ) ) {
return $fonts[0];
}
return null;
}
/**
* @param $fontId
* @param $weight
*
* @return array|bool
*/
function getFontWeightFileUrls( $fontId, $weight ) {
$args = array(
'meta_query' => array(
array(
'key' => 'brizy-font-weight',
'value' => $weight
)
),
'post_type' => 'attachment',
'post_parent' => $fontId
);
$posts = get_posts( $args );
if ( ! $posts || is_wp_error( $posts ) ) {
return false;
}
$result = array();
foreach ( $posts as $post ) {
$type = get_post_meta( $post->ID, 'brizy-font-file-type', true );
$result[ $type ] = wp_get_attachment_url( $post->ID );
}
return $result;
}
}

View File

@@ -0,0 +1,118 @@
<?php
/**
* Created by PhpStorm.
* User: alex
* Date: 1/11/19
* Time: 10:59 AM
*/
class Brizy_Admin_Fonts_Main {
const CP_FONT = 'brizy-font';
const SVG_MIME = 'image/svg+xml';
/**
* @return Brizy_Admin_Fonts_Main
*/
public static function _init() {
static $instance;
if ( ! $instance ) {
$instance = new self();
}
return $instance;
}
/**
* BrizyPro_Admin_Popups constructor.
*/
public function __construct() {
$urlBuilder = new Brizy_Editor_UrlBuilder();
$handler = new Brizy_Admin_Fonts_Handler( $urlBuilder, null );
if ( Brizy_Editor_User::is_user_allowed() ) {
add_action( 'wp_loaded', array( $this, 'initializeActions' ) );
add_filter( 'upload_mimes', array( $this, 'addFontTypes' ) );
add_filter( 'wp_check_filetype_and_ext', array( $this, 'wp_check_filetype_and_ext' ), 10, 4 );
}
}
public function initializeActions() {
Brizy_Admin_Fonts_Api::_init();
}
public function addFontTypes( $mime_types ) {
$mime_types['ttf'] = 'application/x-font-ttf';
$mime_types['eot'] = 'application/vnd.ms-fontobject';
$mime_types['woff'] = 'application/x-font-woff';
$mime_types['woff2'] = 'application/x-font-woff2';
return $mime_types;
}
/**
* @param $data
* @param $file
* @param $filename
* @param $mimes
* @param $real_mime
*
* @return array
*/
public function wp_check_filetype_and_ext( $data, $file, $filename, $mimes ) {
if ( ! $data['ext'] ) {
// Do basic extension validation and MIME mapping
$wp_filetype = wp_check_filetype( $filename, $mimes );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
if ( $ext === 'ttf' ) {
return array( 'ext' => $ext, 'type' => 'application/x-font-ttf', 'proper_filename' => false );
}
if ( $ext === 'eot' ) {
return array( 'ext' => $ext, 'type' => 'application/vnd.ms-fontobject', 'proper_filename' => false );
}
if ( $ext === 'woff' ) {
return array( 'ext' => $ext, 'type' => 'application/x-font-woff', 'proper_filename' => false );
}
if ( $ext === 'woff2' ) {
return array( 'ext' => $ext, 'type' => 'application/x-font-woff2', 'proper_filename' => false );
}
}
return $data;
}
public static function registerCustomPosts() {
$labels = array(
'name' => _x( 'Fonts', 'post type general name' ),
);
register_post_type( self::CP_FONT,
array(
'labels' => $labels,
'public' => false,
'has_archive' => false,
'description' => __bt( 'brizy', 'Brizy' ) . ' ' . __( 'font', 'brizy' ) . '.',
'publicly_queryable' => false,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => false,
'capability_type' => 'page',
'hierarchical' => false,
'show_in_rest' => false,
'exclude_from_search' => true,
'supports' => array( 'title', 'page-attributes' )
)
);
}
}

View File

@@ -0,0 +1,349 @@
<?php
class Brizy_Admin_Fonts_Manager {
/**
* Get all fonts as arrays
*
* @return array
*/
public function getAllFonts() {
global $wpdb;
$fonts = get_posts( array(
'post_type' => Brizy_Admin_Fonts_Main::CP_FONT,
'post_status' => 'publish',
'numberposts' => - 1,
'orderby' => 'ID',
'order' => 'ASC',
) );
$result = array();
foreach ( $fonts as $font ) {
$weights = $wpdb->get_results( $wpdb->prepare(
"SELECT m.meta_value FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} m ON m.post_id=p.ID && m.meta_key='brizy-font-weight'
WHERE p.post_parent=%d
",
array( $font->ID ) ), ARRAY_A
);
$result[] = array(
'id' => get_post_meta( $font->ID, 'brizy_post_uid', true ),
'family' => $font->post_title,
'type' => get_post_meta( $font->ID, 'brizy-font-type', true ),
'weights' => array_values( array_unique( array_map( function ( $v ) {
return $v['meta_value'];
}, $weights ) ) )
);
}
return $result;
}
/**
* @param $uid
*
* @return array|null
*/
public function getFont( $uid ) {
global $wpdb;
$fonts = get_posts( array(
'post_type' => Brizy_Admin_Fonts_Main::CP_FONT,
'post_status' => 'publish',
'meta_key' => 'brizy_post_uid',
'meta_value' => $uid,
'numberposts' => - 1,
'orderby' => 'ID',
'order' => 'DESC',
) );
if ( isset( $fonts[0] ) ) {
$font = $fonts[0];
} else {
return null;
}
return $this->getFontReturnData( $font );
}
/**
* @param $uid
*
* @return array|null
*/
public function getFontForExport( $uid ) {
global $wpdb;
$fonts = get_posts( array(
'post_type' => Brizy_Admin_Fonts_Main::CP_FONT,
'post_status' => 'publish',
'meta_key' => 'brizy_post_uid',
'meta_value' => $uid,
'numberposts' => - 1,
'orderby' => 'ID',
'order' => 'DESC',
) );
if ( isset( $fonts[0] ) ) {
$font = $fonts[0];
} else {
return null;
}
$fontData = $this->getFontReturnData( $font );
$files = $wpdb->get_results(
$wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} p WHERE p.post_parent=%d", array( $font->ID ) ),
ARRAY_A );
$weightFiles = array();
foreach ( $files as $fileId ) {
$weight = get_post_meta( $fileId['ID'], 'brizy-font-weight', true );
$type = get_post_meta( $fileId['ID'], 'brizy-font-file-type', true );
$weightFiles[ $weight ][ $type ] = get_attached_file( $fileId['ID'] );
}
$fontData['weights'] = $weightFiles;
return $fontData;
}
public function getFontByFamily( $uid, $family, $type ) {
$fonts = get_posts( array(
'title' => $family,
'post_type' => Brizy_Admin_Fonts_Main::CP_FONT,
'post_status' => 'publish',
'meta_key' => 'brizy-font-type',
'meta_value' => $type,
'numberposts' => - 1,
'orderby' => 'ID',
'order' => 'DESC',
) );
if ( isset( $fonts[0] ) ) {
$font = $fonts[0];
if ( $uid == get_post_meta( $font->ID, 'brizy_post_uid', true ) ) {
return $this->getFontReturnData( $font );
}
}
return null;
}
/**
* @param $uid
* @param $family
* @param $fontWeights
* @param $fontType
*
* @return int
* @throws Exception
*/
public function createFont( $uid, $family, $fontWeights, $fontType ) {
global $wpdb;
if ( $uid == '' ) {
throw new Exception( 'Invalid font uid' );
}
if ( $family == '' ) {
throw new Exception( 'Invalid font family' );
}
if ( $fontType == '' ) {
throw new Exception( 'Invalid font type' );
}
if ( ! is_array( $fontWeights ) || empty( $fontWeights ) ) {
throw new Exception( 'Invalid font weights' );
}
$font = $this->getFont( $uid );
if ( $font ) {
throw new Brizy_Admin_Fonts_Exception_DuplicateFont( 'This font already exists' );
}
// Need to require these files
if ( ! function_exists( 'media_handle_upload' ) ) {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
require_once( ABSPATH . "wp-admin" . '/includes/file.php' );
require_once( ABSPATH . "wp-admin" . '/includes/media.php' );
}
try {
$wpdb->query( 'START TRANSACTION' );
// create font post
$fontId = wp_insert_post( [
'post_title' => $family,
'post_name' => $family,
'post_type' => Brizy_Admin_Fonts_Main::CP_FONT,
'post_status' => 'publish',
] );
if ( ! $fontId || is_wp_error( $fontId ) ) {
throw new Exception( 'Unable to create font' );
}
update_post_meta( $fontId, 'brizy_post_uid', $uid );
update_post_meta( $fontId, 'brizy-font-type', $fontType );
// create font attachments
foreach ( $fontWeights as $weight => $weightType ) {
if ( count( $weightType ) == 0 ) {
throw new Exception( 'No font files provided' );
}
foreach ( $weightType as $type => $file ) {
$id = media_handle_sideload( $file, $fontId, "Attached font file" );
if ( ! $id || is_wp_error( $id ) ) {
Brizy_Logger::instance()->critical( 'Unable to handle font sideload', [ $id ] );
throw new Exception( 'Unable to handle font side load' );
}
update_post_meta( $id, 'brizy-font-weight', $weight );
update_post_meta( $id, 'brizy-font-file-type', $type );
}
}
$wpdb->query( 'COMMIT' );
return (int) $fontId;
} catch ( Exception $e ) {
$wpdb->query( 'ROLLBACK' );
Brizy_Logger::instance()->critical( 'Create font ERROR', [ $e->getMessage() ] );
throw new Exception( 'Unable to create font' );
}
}
/**
* @param $fontUid
*
* @return bool
* @throws Exception
*/
public function deleteFont( $fontUid ) {
global $wpdb;
if ( ! $fontUid ) {
throw new Exception( 'Invalid font uid' );
}
$font = get_posts( [
'post_type' => Brizy_Admin_Fonts_Main::CP_FONT,
'meta_query' => array(
array(
'key' => 'brizy_post_uid',
'value' => $fontUid
)
),
'posts_per_page' => - 1,
'orderby' => 'ID',
'order' => 'DESC',
] );
if ( count( $font ) > 0 ) {
$font = $font[0];
} else {
$font = null;
}
if ( ! $font ) {
throw new Exception( 'Font not found', 404 );
}
try {
$wpdb->query( 'START TRANSACTION ' );
// delete all attachments first
$attachments = get_attached_media( '', $font->ID );
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, 'true' );
}
// delete font
wp_delete_post( $font->ID );
$wpdb->query( 'COMMIT' );
} catch ( Exception $e ) {
$wpdb->query( 'ROLLBACK' );
Brizy_Logger::instance()->debug( 'Delete font ERROR', [ $e ] );
throw new Exception( 'Failed to delete font', 400 );
}
return true;
}
/**
* @param wpdb $wpdb
* @param $font
*
* @return array
*/
private function getFontReturnData( $font ) {
global $wpdb;
$weights = $wpdb->get_results( $wpdb->prepare(
"SELECT DISTINCT m.meta_value FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} m ON m.post_id=p.ID && p.post_parent=%d && m.meta_key='brizy-font-weight'
",
array( $font->ID ) ), ARRAY_A
);
return array(
'id' => get_post_meta( $font->ID, 'brizy_post_uid', true ),
'family' => $font->post_title,
'type' => get_post_meta( $font->ID, 'brizy-font-type', true ),
'weights' => array_map( function ( $v ) {
return $v['meta_value'];
}, $weights )
);
}
/**
* @param wpdb $wpdb
* @param $font
*
* @return array
*/
private function getFontReturnDataForExport( $font ) {
global $wpdb;
$weights = $wpdb->get_results( $wpdb->prepare(
"SELECT DISTINCT m.meta_value FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} m ON m.post_id=p.ID && p.post_parent=%d && m.meta_key='brizy-font-weight'
",
array( $font->ID ) ), ARRAY_A
);
return array(
'id' => get_post_meta( $font->ID, 'brizy_post_uid', true ),
'family' => $font->post_title,
'type' => get_post_meta( $font->ID, 'brizy-font-type', true ),
'weights' => array_map( function ( $v ) {
return $v['meta_value'];
}, $weights )
);
}
}

View File

@@ -0,0 +1,13 @@
{% for family,font in fonts %}
/* {{ family }} */
{% for weight,fontData in font %}
{% for type,font_url in fontData %}
@font-face {
font-family: '{{ family }}';
font-style: {{ weight|fontStyle }};
font-weight: {{ weight|fontWeight }};
src: local('{{ family }}'), url({{ font_url }}) format('{{ type | fontType }}');
}
{% endfor %}
{% endfor %}
{% endfor %}