prepare_template( $data );
}
return $templates;
}
public function get_tags() {
$library_data = self::get_library_data();
return ( ! empty( $library_data['tags'] ) ? $library_data['tags'] : [] );
}
private function get_placeholder( $alt = '' ) {
return sprintf(
'
',
esc_url( ELEMENTOR_ASSETS_URL . 'images/placeholder.png' ),
esc_attr( $alt )
);
}
/**
* Prepare template items to match model
*
* @param array $data
*
* @return array
*/
private function prepare_template( $data ) {
$data['favorite'] = false;
if ( ! $data['thumbnail'] ) {
$data['thumbnail'] = '
';
}
return $data;
}
/**
* Get library data from remote source and cache
*
* @param boolean $force_update
*
* @return array
*/
private static function request_library_data( $force_update = false ) {
/**
* Do not use WP transient API, set cache ttl separately.
* We should keep the data until new data received, transient will remove old data upon expire.
*/
$data = get_option( self::LIBRARY_CACHE_KEY, false );
if ( $force_update || false === $data ) {
$response = self::request( self::get_endpoint( 'info' ) );
if ( ! is_wp_error( $response ) && 200 === (int) wp_remote_retrieve_response_code( $response ) ) {
$data = json_decode( wp_remote_retrieve_body( $response ), true );
}
if ( ! is_array( $data ) ) {
$data = [];
}
if ( ! empty( $data ) ) {
update_option( self::LIBRARY_CACHE_KEY, $data, 'no' );
update_option( self::LIBRARY_CACHE_KEY . '_updated_at', self::LIBRARY_CACHE_TTL, 'no' );
}
}
return $data;
}
public static function request_template_data( $template_id ) {
if ( empty( $template_id ) ) {
return new WP_Error( 'template-id-missing', __( 'Template id is missing', 'absolute-addons' ) );
}
$response = self::request( self::get_endpoint( 'download', $template_id ) );
if ( is_wp_error( $response ) ) {
return $response;
}
$data = wp_remote_retrieve_body( $response );
$data = $data ? json_decode( $data, true ) : false;
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
if ( $data && isset( $data['code'], $data['message'] ) ) {
return new WP_Error(
$data['code'],
$data['message'],
( isset( $data['data'] ) ? $data['data'] : '' )
);
}
}
return $data;
}
private static function request( $url, $payload = [], $args = [] ) {
$payload = wp_parse_args( $payload, self::prepare_common_payload() );
$args = wp_parse_args(
$args,
[
'body' => $payload,
'timeout' => 25, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'headers' => [
'Accept' => 'application/json',
],
]
);
return absp_remote_get( $url, $args );
}
private static function prepare_common_payload() {
$payload = [
'home_url' => trailingslashit( home_url() ),
'version' => ABSOLUTE_ADDONS_VERSION,
];
if ( absp_has_pro() ) {
$payload['has_pro'] = 1;
$payload['pro_version'] = ABSOLUTE_ADDONS_PRO_VERSION;
$payload['license'] = absolute_addons_pro_license_data();
}
return $payload;
}
/**
* Get library data
*
* @param boolean $force_update
*
* @return array
*/
public static function get_library_data( $force_update = false ) {
self::request_library_data( $force_update );
$data = get_option( self::LIBRARY_CACHE_KEY, [] );
$updated_at = get_option( self::LIBRARY_CACHE_KEY . '_updated_at' );
// Check if cache expired.
if ( ! $updated_at || $updated_at + self::LIBRARY_CACHE_TTL >= time() ) {
$_data = self::request_library_data( true );
if ( ! empty( $_data ) ) {
return $_data;
}
}
// Return cached data
return $data;
}
/**
* Get remote template.
*
* Retrieve a single remote template from Elementor.com servers.
*
* @param int $template_id The template ID.
*
* @return array Remote template.
*/
public function get_item( $template_id ) {
$templates = $this->get_items();
return $templates[ $template_id ];
}
/**
* Get remote template data.
*
* Retrieve the data of a single remote template from Elementor.com servers.
*
* @return array|WP_Error Remote Template data.
* @throws Exception
*/
public function get_data( array $args, $context = 'display' ) {
// Download Template.
$data = self::request_template_data( $args['template_id'] );
if ( is_wp_error( $data ) ) {
throw new Exception( $data->get_error_message() );
}
if ( ! is_array( $data ) || empty( $data ) || empty( $data['content'] ) ) {
throw new Exception( __( 'Template does not have any content', 'absolute-addons' ) );
}
$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 = ElementorPlug::instance()->documents->get( $post_id );
if ( $document ) {
$data['content'] = $document->get_elements_raw_data( $data['content'], true );
}
return $data;
}
}
// End of file class-base-widget.php.