Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

23851 adobe fonts #1936

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions admin/abstract-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ protected function verifyNonce( $action ) {
$this->error( 400, "Invalid editor version. Please refresh the page and try again" );
}

$this->checkNonce( $action );
}

protected function checkNonce( $action ) {
if ( ! wp_verify_nonce( $this->getRequestNonce(), $action ) ) {
Brizy_Logger::instance()->error( 'Invalid request nonce', $_REQUEST );
$this->error( 400, "Bad request" );
Expand Down
3 changes: 3 additions & 0 deletions editor/accounts/abstract-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ abstract class Brizy_Editor_Accounts_AbstractAccount extends Brizy_Admin_Seriali
const INTEGRATIONS_GROUP = 'form-integration';
const RECAPTCHA_GROUP = 'recaptcha';
const SOCIAL_GROUP = 'social';
const ADOBE_GROUP = 'adobe';


use Brizy_Editor_Forms_DynamicPropsAware;
Expand Down Expand Up @@ -140,6 +141,8 @@ static public function createFromSerializedData( $data ) {
return new Brizy_Editor_Accounts_SocialAccount( $data );
case self::RECAPTCHA_GROUP:
return new Brizy_Editor_Accounts_RecaptchaAccount( $data );
case self::ADOBE_GROUP:
return new Brizy_Editor_Accounts_AdobeAccount( $data );
}

throw new Exception( 'Invalid account group.' );
Expand Down
56 changes: 56 additions & 0 deletions editor/accounts/adobe-account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Created by PhpStorm.
* User: alex
* Date: 11/26/18
* Time: 5:00 PM
*/

class Brizy_Editor_Accounts_AdobeAccount extends Brizy_Editor_Accounts_Account {

/**
* @return mixed
*/
public function getGroup() {
return Brizy_Editor_Accounts_AbstractAccount::ADOBE_GROUP;
}

/**
* @param $data
*
* @return Brizy_Editor_Accounts_AbstractAccount
* @throws Exception
*/
static public function createFromSerializedData( $data ) {

$data['group'] = Brizy_Editor_Accounts_AbstractAccount::ADOBE_GROUP;

return Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( $data );
}

/**
* @param $json_obj
*
* @return Brizy_Editor_Accounts_AbstractAccount
* @throws Exception
*/
public static function createFromJson( $json_obj ) {

if ( ! isset( $json_obj ) ) {
throw new Exception( 'Bad Request', 400 );
}

if ( is_object( $json_obj ) ) {

$json_obj->group = Brizy_Editor_Accounts_AbstractAccount::ADOBE_GROUP;

return Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( get_object_vars( $json_obj ) );
}

throw new Exception( 'Invalid json provided.' );
}

public function getKey() {
return ! empty( $this->data['key'] ) ? $this->data['key'] : null;
}
}
85 changes: 56 additions & 29 deletions editor/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Brizy_Editor_API extends Brizy_Admin_AbstractApi {
const AJAX_GET_TERMS = '_get_terms';
const AJAX_GET_TERMS_BY = '_get_terms_by';
const AJAX_GET_POST_TAXONOMIES = '_get_post_taxonomies';
const AJAX_GET_ADOBE_FONTS = '_get_adobe_fonts';

const AJAX_GET_DYNAMIC_CONTENT = '_get_dynamic_content';

Expand Down Expand Up @@ -69,35 +70,36 @@ protected function initializeApiActions() {
return;
}

$p = 'wp_ajax_' . Brizy_Editor::prefix();
add_action( $p . self::AJAX_REMOVE_LOCK, array( $this, 'removeProjectLock' ) );
add_action( $p . self::AJAX_HEARTBEAT, array( $this, 'heartbeat' ) );
add_action( $p . self::AJAX_TAKE_OVER, array( $this, 'takeOver' ) );
add_action( $p . self::AJAX_GET, array( $this, 'get_item' ) );
add_action( $p . self::AJAX_GET_POST_INFO, array( $this, 'get_post_info' ) );
add_action( $p . self::AJAX_UPDATE, array( $this, 'update_item' ) );
add_action( $p . self::AJAX_GET_PROJECT, array( $this, 'get_project' ) );
add_action( $p . self::AJAX_SET_PROJECT, array( $this, 'set_project' ) );
add_action( $p . self::AJAX_LOCK_PROJECT, array( $this, 'lock_project' ) );
add_action( $p . self::AJAX_SIDEBARS, array( $this, 'get_sidebars' ) );
add_action( $p . self::AJAX_SHORTCODE_CONTENT, array( $this, 'shortcode_content' ) );
add_action( $p . self::AJAX_PLACEHOLDER_CONTENT, array( $this, 'placeholder_content' ) );
add_action( $p . self::AJAX_PLACEHOLDERS_CONTENT, array( $this, 'placeholders_content' ) );
add_action( $p . self::AJAX_GET_POST_OBJECTS, array( $this, 'get_post_objects' ) );
add_action( $p . self::AJAX_SEARCH_POST, array( $this, 'search_post' ) );
add_action( $p . self::AJAX_GET_MENU_LIST, array( $this, 'get_menu_list' ) );
add_action( $p . self::AJAX_GET_TERMS, array( $this, 'get_terms' ) );
add_action( $p . self::AJAX_GET_USERS, array( $this, 'get_users' ) );
add_action( $p . self::AJAX_GET_TERMS_BY, array( $this, 'get_terms_by' ) );
add_action( $p . self::AJAX_MEDIA_METAKEY, array( $this, 'get_media_key' ) );
add_action( $p . self::AJAX_CREATE_ATTACHMENT_UID, array( $this, 'get_attachment_key' ) );
add_action( $p . self::AJAX_SET_FEATURED_IMAGE, array( $this, 'set_featured_image' ) );
add_action( $p . self::AJAX_SET_IMAGE_FOCAL_PT, array( $this, 'set_featured_image_focal_point' ) );
add_action( $p . self::AJAX_TIMESTAMP, array( $this, 'timestamp' ) );
add_action( $p . self::AJAX_SET_TEMPLATE_TYPE, array( $this, 'setTemplateType' ) );
add_action( $p . self::AJAX_GET_POST_TAXONOMIES, array( $this, 'addPostTaxonomies' ) );
add_action( $p . self::AJAX_GET_DYNAMIC_CONTENT, array( $this, 'addDynamicContent' ) );
add_action( $p . 'nopriv_' . Brizy_Editor::prefix( self::AJAX_TIMESTAMP ), array( $this, 'timestamp' ) );
$p = 'wp_ajax_' . Brizy_Editor::prefix();
add_action($p . self::AJAX_REMOVE_LOCK, array($this, 'removeProjectLock'));
add_action($p . self::AJAX_HEARTBEAT, array($this, 'heartbeat'));
add_action($p . self::AJAX_TAKE_OVER, array($this, 'takeOver'));
add_action($p . self::AJAX_GET, array($this, 'get_item'));
add_action($p . self::AJAX_GET_POST_INFO, array($this, 'get_post_info'));
add_action($p . self::AJAX_UPDATE, array($this, 'update_item'));
add_action($p . self::AJAX_GET_PROJECT, array($this, 'get_project'));
add_action($p . self::AJAX_SET_PROJECT, array($this, 'set_project'));
add_action($p . self::AJAX_LOCK_PROJECT, array($this, 'lock_project'));
add_action($p . self::AJAX_SIDEBARS, array($this, 'get_sidebars'));
add_action($p . self::AJAX_SHORTCODE_CONTENT, array($this, 'shortcode_content'));
add_action($p . self::AJAX_PLACEHOLDER_CONTENT, array($this, 'placeholder_content'));
add_action($p . self::AJAX_PLACEHOLDERS_CONTENT, array($this, 'placeholders_content'));
add_action($p . self::AJAX_GET_POST_OBJECTS, array($this, 'get_post_objects'));
add_action($p . self::AJAX_SEARCH_POST, array($this, 'search_post'));
add_action($p . self::AJAX_GET_MENU_LIST, array($this, 'get_menu_list'));
add_action($p . self::AJAX_GET_TERMS, array($this, 'get_terms'));
add_action($p . self::AJAX_GET_USERS, array($this, 'get_users'));
add_action($p . self::AJAX_GET_TERMS_BY, array($this, 'get_terms_by'));
add_action($p . self::AJAX_MEDIA_METAKEY, array($this, 'get_media_key'));
add_action($p . self::AJAX_CREATE_ATTACHMENT_UID, array($this, 'get_attachment_key'));
add_action($p . self::AJAX_SET_FEATURED_IMAGE, array($this, 'set_featured_image'));
add_action($p . self::AJAX_SET_IMAGE_FOCAL_PT, array($this, 'set_featured_image_focal_point'));
add_action($p . self::AJAX_TIMESTAMP, array($this, 'timestamp'));
add_action($p . self::AJAX_SET_TEMPLATE_TYPE, array($this, 'setTemplateType'));
add_action($p . self::AJAX_GET_POST_TAXONOMIES, array($this, 'addPostTaxonomies'));
add_action($p . self::AJAX_GET_DYNAMIC_CONTENT, array($this, 'addDynamicContent'));
add_action($p . self::AJAX_GET_ADOBE_FONTS, array($this, 'getAdobeFonts'));
add_action($p . 'nopriv_' . Brizy_Editor::prefix(self::AJAX_TIMESTAMP), array($this, 'timestamp'));

}

Expand Down Expand Up @@ -1104,4 +1106,29 @@ private function createMediaKey( $postId, $attachmentId ) {

return $uid;
}

public function getAdobeFonts() {
$this->checkNonce( self::nonce );

$manager = new Brizy_Editor_Accounts_ServiceAccountManager( Brizy_Editor_Project::get() );
$accounts = $manager->getAccountsByGroup( Brizy_Editor_Accounts_AbstractAccount::ADOBE_GROUP );

if ( ! isset( $accounts[0] ) ) {
$this->error( 400, 'No adobe account found.' );
}

$adobeKey = $accounts[0]->getKey();

if ( ! $adobeKey ) {
$this->error( 400, 'No adobe key found.' );
}

$response = wp_remote_get( "https://typekit.com/api/v1/json/kits/$adobeKey/published", [ 'timeout' => 20 ] );

if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
$this->error( 400, 'An error occurred creating the request to adobe.' );
}

$this->success( json_decode( wp_remote_retrieve_body( $response ), true ) );
}
}
1 change: 1 addition & 0 deletions editor/editor/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,7 @@ public function getApiActions($config = [], $context = null)
'symbolDelete' => $pref.Brizy_Admin_Symbols_Api::DELETE_ACTION,
'symbolList' => $pref.Brizy_Admin_Symbols_Api::LIST_ACTION,
'getDynamicContentPlaceholders' => $pref.Brizy_Editor_API::AJAX_GET_DYNAMIC_CONTENT,
'adobeFontsUrl' => $pref . Brizy_Editor_API::AJAX_GET_ADOBE_FONTS,
);

return $actions;
Expand Down
67 changes: 67 additions & 0 deletions public/editor-client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,73 @@ export const updateBlockScreenshot = async ({

//#endregion

//#region AdobeFonts
interface AddAccount {
group: string;
key: string;
}

export const getAdobeFont = async () => {
const config = getConfig();

if (!config) {
throw new Error(t("Invalid __BRZ_PLUGIN_ENV__"));
}

const { editorVersion, url: _url, hash, actions } = config;

const url = makeUrl(_url, {
hash,
action: actions.adobeFontsUrl,
version: editorVersion
});

const r = await request(url, {
method: "GET"
});

if (r.ok) {
const d = await r.json();

if (d) {
return d.data;
}
} else {
throw new Error(t("Failed to get adobe fonts"));
}
};

export const addAdobeAccount = async (body: AddAccount) => {
const config = getConfig();

if (!config) {
throw new Error(t("Invalid __BRZ_PLUGIN_ENV__"));
}

const { url: _url, hash, editorVersion, actions } = config;

const url = makeUrl(_url, {
hash,
action: actions.addAccount,
version: editorVersion
});

try {
const res = await request(url, {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8"
},
body: JSON.stringify(body)
});
return res;
} catch (error) {
throw new Error(`Failed to add Adobe account: ${error.message}`);
}
};

//#endregion

//#region Dynamic Content

export const getPlaceholders = (extraData: {
Expand Down
12 changes: 11 additions & 1 deletion public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readIconUrl } from "@/types/Icon";
import { Arr, Bool, Obj, Str } from "@brizy/readers";
import { match, mPipe, optional, parseStrict } from "fp-utilities";
import { CollectionType } from "./types/Collections";
import { PLUGIN_ENV } from "./types/global";
import { ImagePatterns, PLUGIN_ENV } from "./types/global";
import { pipe } from "./utils/fp/pipe";
import { onNullish } from "./utils/onNullish";
import { throwOnNullish } from "./utils/throwOnNullish";
Expand Down Expand Up @@ -41,6 +41,8 @@ interface Actions {

createBlockScreenshot: string;
updateBlockScreenshot: string;
adobeFontsUrl: string;
addAccount: string;

getDynamicContentPlaceholders: string;

Expand Down Expand Up @@ -248,6 +250,14 @@ const actionsReader = parseStrict<PLUGIN_ENV["actions"], Actions>({
mPipe(Obj.readKey("updateBlockScreenshot"), Str.read),
throwOnNullish("Invalid actions: updateBlockScreenshot")
),
adobeFontsUrl: pipe(
mPipe(Obj.readKey("adobeFontsUrl"), Str.read),
throwOnNullish("Invalid actions: adobeFontsUrl")
),
addAccount: pipe(
mPipe(Obj.readKey("addAccount"), Str.read),
throwOnNullish("Invalid actions: addAccount")
),
getDynamicContentPlaceholders: pipe(
mPipe(Obj.readKey("getDynamicContentPlaceholders"), Str.read),
throwOnNullish("Invalid actions: getDynamicContentPlaceholders")
Expand Down
42 changes: 42 additions & 0 deletions public/editor-client/src/fonts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
import { addAdobeAccount, getAdobeFont } from "../api";
import { AdobeFonts } from "../types/AdobeFonts";
import { t } from "../utils/i18n";
import { Fonts, KitData } from "./types";
import { FontsData, getUploadedFonts } from "../api";
import { Response } from "../types/Response";

const convertDataToLocal = (mockTypeKitData: KitData): Fonts => {
const families = mockTypeKitData.kit.families.map((family) => ({
id: family.id,
family: family.name,
category: family.slug,
kind: "webfonts#webfont",
subsets: [family.css_names[0]],
variants: family.variations
}));

return {
kit: {
id: mockTypeKitData.kit.id,
families
}
};
};

export const adobeFont = (): AdobeFonts => {
return {
async get(res, rej) {
try {
const r = await getAdobeFont();
res(convertDataToLocal(r));
} catch (e) {
rej(t("Failed to get AdobeFont"));
}
},
async add(res, rej, extra) {
try {
const r = await addAdobeAccount(extra);
res(r);
} catch (e) {
rej(t("Failed to add Adobe account"));
}
}
};
};
export const uploadedFonts = {
async get(res: Response<Array<FontsData>>, rej: Response<string>) {
try {
Expand Down
30 changes: 30 additions & 0 deletions public/editor-client/src/fonts/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
interface Family {
id: string;
name: string;
slug: string;
css_names: string[];
variations: string[];
}

export interface KitData {
kit: {
id: string;
families: Family[];
};
}

interface Font {
id: string;
family: string;
category: string;
kind: string;
subsets: string[];
variants: string[];
}

export interface Fonts {
kit: {
id: string;
families: Font[];
};
}
Loading