Skip to content

Commit

Permalink
new: added adobe account group and create adobe account class
Browse files Browse the repository at this point in the history
feat(adobe): add

feat(font): add font awesome call

test

feat: make request tio adobe

fix: adobe url

ref: try headers

check the Host

bagrinsergiu/blox-editor#24306 - Proxy for adobe fonts

added adobe fonts

feat(add) api call

feat(ref) after review

feat(ref) after review

feat(ref) after review

feat(refactor) after review

fix(import) after rebase

fix(remove) brizy.php from PR

fix(remove) comment
  • Loading branch information
alecszaharia authored and CristianSpatari committed May 7, 2024
1 parent 695ac3c commit 1dc67d7
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 39 deletions.
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 ) );
}
}
61 changes: 61 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,67 @@ export const updateBlockScreenshot = async ({

//#endregion

//#region Screenshots
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
});

return request(url, {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8"
},
body: JSON.stringify(body)
}).then((res) => res);
};

//#endregion

//#region Dynamic Content

export const getPlaceholders = (extraData: {
Expand Down
36 changes: 26 additions & 10 deletions 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 @@ -144,17 +146,23 @@ const apiReader = parseStrict<PLUGIN_ENV["api"], API>({
Obj.readKey("imagePatterns"),
Obj.read,
parseStrict<PLUGIN_ENV, ImagePatterns>({
full: pipe(
mPipe(Obj.readKey("full"), Str.read),
throwOnNullish("Invalid API: ImagePatterns full pattern")
full: optional(
pipe(
mPipe(Obj.readKey("full"), Str.read),
throwOnNullish("Invalid API: ImagePatterns full pattern")
)
),
original: pipe(
mPipe(Obj.readKey("original"), Str.read),
throwOnNullish("Invalid API: ImagePatterns original pattern")
original: optional(
pipe(
mPipe(Obj.readKey("original"), Str.read),
throwOnNullish("Invalid API: ImagePatterns original pattern")
)
),
split: pipe(
mPipe(Obj.readKey("split"), Str.read),
throwOnNullish("Invalid API: ImagePatterns split pattern")
split: optional(
pipe(
mPipe(Obj.readKey("split"), Str.read),
throwOnNullish("Invalid API: ImagePatterns split pattern")
)
)
})
),
Expand Down Expand Up @@ -247,6 +255,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

0 comments on commit 1dc67d7

Please sign in to comment.