Skip to content

Commit

Permalink
refactor remote
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoladj77 committed Jan 17, 2025
1 parent 8860186 commit 871d172
Showing 1 changed file with 1 addition and 173 deletions.
174 changes: 1 addition & 173 deletions modules/template-parts/classes/sources/source-remote-ehp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
exit; // Exit if accessed directly
}

class Source_Remote_Ehp extends \Elementor\TemplateLibrary\Source_Base {
class Source_Remote_Ehp extends \Elementor\TemplateLibrary\Source_Remote {

const API_TEMPLATES_URL = 'https://ba-templates.stg.elementor.red/v1/templates/';

Expand All @@ -27,152 +27,6 @@ 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
*/
Expand All @@ -184,7 +38,6 @@ protected function get_templates_remotely( string $editor_layout_type ) {
'headers' => apply_filters( 'stg-cf-headers', [] ),
] );

error_log( print_r( $response, true ) );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
return false;
}
Expand All @@ -198,35 +51,10 @@ protected function get_templates_remotely( string $editor_layout_type ) {
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 {
$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;
}
}

0 comments on commit 871d172

Please sign in to comment.