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

Internal: Header/Footer templates-api [TMZ-276] #151

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { __ } from '@wordpress/i18n';

export class EhpAddLibraryTab extends $e.modules.hookUI.Before {
getCommand() {
return 'library/open';
}

getId() {
return 'ehp-add-library-tab';
}

getConditions() {
return [ 'ehp-header', 'ehp-footer' ].includes( elementor?.config?.document?.type );
}

getTitle() {
switch ( elementor?.config?.document?.type ) {
case 'ehp-header':
return __( 'Hello+ Header', 'hello-plus' );
case 'ehp-footer':
return __( 'Hello+ Footer', 'hello-plus' );
default:
return __( 'Hello Plus', 'elementor' );
}
}

apply() {
$e.components.get( 'library' ).addTab( 'templates/ehp-elements', {
title: this.getTitle(),
filter: {
source: 'remote-ehp',
type: 'block',
},
}, 2 );

$e.components.get( 'library' ).removeTab( 'templates/blocks' );
$e.components.get( 'library' ).removeTab( 'templates/pages' );
}
}

export default EhpAddLibraryTab;
3 changes: 3 additions & 0 deletions modules/template-parts/assets/js/editor/hooks/ui/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { SelectEhpElementOnOpen } from './attach-preview';
export { SelectAfterContainer } from './select';
export { EhpAddLibraryTab } from './add-library-tab';
export { EhpRemoveLibraryTab } from './remove-library-tab';
export { EhpOpenLibraryAfterDelete } from './open-library-after-delete';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export class EhpOpenLibraryAfterDelete extends $e.modules.hookUI.After {
getCommand() {
return 'document/elements/delete';
}

getId() {
return 'ehp-open-library-after-delete';
}

getConditions( args ) {
let type = args?.container?.document?.config?.type;
if ( ! type ) {
type = args?.containers[ 0 ]?.document?.config?.type;
}

return [ 'ehp-header', 'ehp-footer' ].includes( type );
}

apply() {
$e.run( 'library/open' );
}
}

export default EhpOpenLibraryAfterDelete;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export class EhpRemoveLibraryTab extends $e.modules.hookUI.After {
getCommand() {
return 'editor/documents/unload';
}

getId() {
return 'ehp-remove-library-tab';
}

getConditions( args ) {
const document = elementor.documents.get( args.id );
return [ 'ehp-header', 'ehp-footer' ].includes( document.config.type );
}

apply() {
$e.components.get( 'library' ).removeTab( 'templates/ehp-elements' );

$e.components.get( 'library' ).addTab( 'templates/pages' );
$e.components.get( 'library' ).addTab( 'templates/blocks' );
}
}

export default EhpRemoveLibraryTab;
17 changes: 17 additions & 0 deletions modules/template-parts/assets/js/editor/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,22 @@ export default class TemplatesModule extends elementorModules.editor.utils.Modul
elementor.hooks.addFilter( 'elements/widget/controls/common/default', this.resetCommonControls.bind( this ) );
elementor.hooks.addFilter( 'elements/widget/controls/common-optimized/default', this.resetCommonControls.bind( this ) );

const types = [
'core/modal/close/ehp-footer',
'core/modal/close/ehp-header',
];

types.forEach( ( type ) => {
window.addEventListener( type, this.redirectToHelloPlus );
} );

window.templatesModule = this;
}

redirectToHelloPlus() {
window.location.href = elementor.config.close_modal_redirect_hello_plus;
}

async openSiteIdentity() {
await $e.run( 'panel/global/open' );
$e.route( 'panel/global/settings-site-identity' );
Expand All @@ -22,4 +35,8 @@ export default class TemplatesModule extends elementorModules.editor.utils.Modul

return commonControls;
}

isEhpDocument() {
return [ 'ehp-footer', 'ehp-header' ].includes( elementor.config.document.type );
}
}
230 changes: 230 additions & 0 deletions modules/template-parts/classes/sources/source-remote-ehp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

namespace HelloPlus\Modules\TemplateParts\Classes\Sources;

nuritsha marked this conversation as resolved.
Show resolved Hide resolved
use Elementor\Api as Elementor_Api;
use Elementor\Core\Common\Modules\Connect\Module as Elementor_Connect_Module;
use HelloPlus\Includes\Utils;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

class Source_Remote_Ehp extends \Elementor\TemplateLibrary\Source_Base {

const API_TEMPLATES_URL = 'https://ba-templates-api.platform-prod.elementor.red/v1/templates/';

const TEMPLATES_DATA_TRANSIENT_KEY_PREFIX = 'elementor_remote_templates_ehp_data_';

public function get_id(): string {
return 'remote-ehp';
}

/**
* @inheritDoc
*/
public function get_title() {
return esc_html__( 'Remote-Ehp', 'hello-plus' );
}

/**
* @inheritDoc
*/
public function register_data() {}

/**
* @inheritDoc
*/
public function get_items( $args = [] ) {
$force_update = ! empty( $args['force_update'] ) && is_bool( $args['force_update'] );

$templates_data = $this->get_templates_data( $force_update );

$templates = [];

foreach ( $templates_data as $template_data ) {
$templates[] = $this->prepare_template( $template_data );
}

return $templates;
}


protected function prepare_template( array $template_data ) {
$favorite_templates = $this->get_user_meta( 'favorites' );

// BC: Support legacy APIs that don't have access tiers.
if ( isset( $template_data['access_tier'] ) ) {
$access_tier = $template_data['access_tier'];
} else {
$access_tier = 0 === $template_data['access_level']
? Elementor_Connect_Module::ACCESS_TIER_FREE
: Elementor_Connect_Module::ACCESS_TIER_ESSENTIAL;
}

return [
'template_id' => $template_data['id'],
'source' => $this->get_id(),
'type' => $template_data['type'],
'subtype' => $template_data['subtype'],
'title' => $template_data['title'],
'thumbnail' => $template_data['thumbnail'],
'date' => $template_data['tmpl_created'],
'author' => $template_data['author'],
'tags' => json_decode( $template_data['tags'] ),
'isPro' => ( '1' === $template_data['is_pro'] ),
'accessLevel' => $template_data['access_level'],
'accessTier' => $access_tier,
'popularityIndex' => (int) $template_data['popularity_index'],
'trendIndex' => (int) $template_data['trend_index'],
'hasPageSettings' => ( '1' === $template_data['has_page_settings'] ),
'url' => $template_data['url'],
'favorite' => ! empty( $favorite_templates[ $template_data['id'] ] ),
];
}


/**
* @inheritDoc
*/
public function get_item( $template_id ) {
$templates = $this->get_items();

return $templates[ $template_id ];
}

/**
* @inheritDoc
*/
public function save_item( $template_data ) {
return new \WP_Error( 'invalid_request', 'Cannot save template to a remote source' );
}

/**
* @inheritDoc
*/
public function update_item( $new_data ) {
return new \WP_Error( 'invalid_request', 'Cannot update template to a remote source' );
}

/**
* @inheritDoc
*/
public function delete_template( $template_id ) {
return new \WP_Error( 'invalid_request', 'Cannot delete template from a remote source' );
}

/**
* @inheritDoc
*/
public function get_data( array $args, $context = 'display' ) {
$data = Elementor_Api::get_template_content( $args['template_id'] );

if ( is_wp_error( $data ) ) {
return $data;
}

// Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
Utils::elementor()->uploads_manager->set_elementor_upload_state( true );

// BC.
$data = (array) $data;

$data['content'] = $this->replace_elements_ids( $data['content'] );
$data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );

$post_id = $args['editor_post_id'];
$document = Utils::elementor()->documents->get( $post_id );
if ( $document ) {
$data['content'] = $document->get_elements_raw_data( $data['content'], true );
}

// After the upload complete, set the elementor upload state back to false
Utils::elementor()->uploads_manager->set_elementor_upload_state( false );

return $data;
}

protected function get_template_data_transient_key(): string {
return static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . HELLO_PLUS_VERSION;
}

/**
* @inheritDoc
*/
public function export_template( $template_id ) {
return new \WP_Error( 'invalid_request', 'Cannot export template from a remote source' );
}

/**
* @inheritDoc
*/
protected function get_templates( string $editor_layout_type ): array {
$templates_data_cache_key = $this->get_template_data_transient_key();

$templates_data = $this->get_templates_remotely( $editor_layout_type );

if ( empty( $templates_data ) ) {
return [];
}

set_transient( $templates_data_cache_key, $templates_data, 12 * HOUR_IN_SECONDS );

return $templates_data;
}

/**
* @inheritDoc
*/
protected function get_templates_remotely( string $editor_layout_type ) {
$query_args = $this->get_url_params( $editor_layout_type );
$url = add_query_arg( $query_args, static::API_TEMPLATES_URL );

$response = wp_remote_get( $url, [
'headers' => apply_filters( 'stg-cf-headers', [] ),
] );

if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
return false;
}

$templates_data = json_decode( wp_remote_retrieve_body( $response ), true );

if ( empty( $templates_data ) || ! is_array( $templates_data ) ) {
return [];
}

return $templates_data;
}

protected function get_templates_body_args( string $editor_layout_type ): array {
return [];
}
protected function get_url_params( string $editor_layout_type ): array {
return [
'products' => 'ehp',
'editor_layout_type' => $editor_layout_type,
];
}

/**
* @inheritDoc
*/
protected function get_templates_data( bool $force_update ) : array {

Check failure on line 213 in modules/template-parts/classes/sources/source-remote-ehp.php

View workflow job for this annotation

GitHub Actions / Lint PHP files

There must not be a space before the colon in a return type declaration
$templates_data_cache_key = $this->get_template_data_transient_key();

$editor_layout_type = 'container_flexbox';

if ( $force_update ) {
return $this->get_templates( $editor_layout_type );
}

$templates_data = get_transient( $templates_data_cache_key );

if ( empty( $templates_data ) ) {
return $this->get_templates( $editor_layout_type );
}

return $templates_data;
}
}
9 changes: 9 additions & 0 deletions modules/template-parts/components/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
}

use Elementor\Core\Documents_Manager;
use HelloPlus\Includes\Utils;

/**
* class Document
Expand Down Expand Up @@ -46,7 +47,15 @@ public function register( Documents_Manager $documents_manager ) {
}
}


public function register_remote_source() {
Utils::elementor()->templates_manager->register_source(
'HelloPlus\Modules\TemplateParts\Classes\Sources\Source_Remote_Ehp'
);
}

public function __construct() {
add_action( 'elementor/documents/register', [ $this, 'register' ] );
add_action( 'elementor/init', [ $this, 'register_remote_source' ] );
}
}
Loading
Loading