Skip to content

Commit

Permalink
Kits API | Fetch kits by attributes [TMZ-141]
Browse files Browse the repository at this point in the history
  • Loading branch information
nuritsha committed Nov 28, 2024
1 parent cc7a12a commit 964e210
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions modules/admin/classes/rest/onboarding-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use WP_REST_Server;

class Onboarding_Settings {
protected array $kits_ids = [];

const EHP_KITS_TRANSIENT = 'e_hello_plus_kits';

public function rest_api_init() {
Expand All @@ -30,39 +28,42 @@ public function rest_api_init() {
);
}

// TODO: implement using new kits api.
public function get_kits() {
$kits = get_transient( self::EHP_KITS_TRANSIENT );
if ( ! empty( $kits ) ) {
return $kits;
}

if ( ! class_exists( 'Elementor\App\Modules\KitLibrary\Connect\Kit_Library' ) ) {
return [];
}

if ( ! $kits ) {
$kits = ['banana'];
if ( class_exists( 'Elementor\App\Modules\KitLibrary\Connect\Kit_Library' ) ) {
$args = [
'products' => 'ehp',
];

/**
* Filter the arguments used to fetch the Hello+ kits.
*
* @param array $args default arguments.
*/
$args = apply_filters( 'hello-plus/onboarding/kits-args', $args );

$endpoint_url = add_query_arg( $args, Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits' );
try {
$kits = $this->call_and_check( $endpoint_url );

foreach ( $kits as $index => $kit ) {
$kits[ $index ]['manifest'] = $this->call_and_check(
Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits/' . $kit['_id'] . '/manifest'
);
}

set_transient( self::EHP_KITS_TRANSIENT, $kits, 24 * HOUR_IN_SECONDS );
} catch ( \Exception $e ) {
$kits = [];
}
$args = [
'products' => 'ehp',
'visibility' => 'restricted',
'editor_layout_type' => 'container_flexbox'

Check failure on line 44 in modules/admin/classes/rest/onboarding-settings.php

View workflow job for this annotation

GitHub Actions / Lint PHP files

There should be a comma after the last array item in a multi-line array.
];

/**
* Filter the arguments used to fetch the Hello+ kits.
*
* @param array $args default arguments.
*/
$args = apply_filters( 'hello-plus/onboarding/kits-args', $args );

$endpoint_url = add_query_arg( $args, Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits' );
try {
$kits = $this->call_and_check( $endpoint_url );

foreach ( $kits as $index => $kit ) {
$kits[ $index ]['manifest'] = $this->call_and_check(
Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits/' . $kit['_id'] . '/manifest'
);
}

set_transient( self::EHP_KITS_TRANSIENT, $kits, 24 * HOUR_IN_SECONDS );
} catch ( \Exception $e ) {
return [];
}

return $kits;
Expand All @@ -77,8 +78,6 @@ public function get_kits() {
public function call_and_check( string $url ) {
$response = wp_remote_get( $url );

error_log( var_export( $response, true ) );

if ( is_wp_error( $response ) ) {
if ( 0 === strpos( $url, Kit_Library::DEFAULT_BASE_ENDPOINT ) ) {
return $this->call_and_check(
Expand Down

0 comments on commit 964e210

Please sign in to comment.