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

Refactored directorist service api with caching and unification #1766

Merged
merged 3 commits into from
Jun 27, 2024
Merged
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
100 changes: 100 additions & 0 deletions includes/classes/class-directorist-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Directorist API class.
*/
namespace Directorist\Core;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

class API {

const URL = 'https://app.directorist.com/wp-json/directorist/';

/**
* @return object
*/
public static function get_promotion() {
$promotion = get_transient( 'directorist_promotion' );

if ( ! empty( $promotion ) ) {
return $promotion;
}

$promotion = static::get( 'v1/get-promo' );
$promotion = json_decode( $promotion );
$end_time = static::get_promotion_end_time( $promotion );

set_transient( 'directorist_promotion', $promotion, $end_time );

return $promotion;
}

protected static function get_promotion_end_time( $promotion ) {
if ( empty( $promotion ) ||
( is_object( $promotion ) && empty( $promotion->promo_end_date ) ) ||
( is_array( $promotion ) && empty( $promotion['promo_end_date'] ) ) ) {
return ( 3 * DAY_IN_SECONDS );
}

$promotion = (object) $promotion;
$end_time = is_numeric( $promotion->promo_end_date ) ? (int) $promotion->promo_end_date : strtotime( $promotion->promo_end_date );
$end_time = $end_time - time();

return $end_time;
}

/**
* @return object
*/
public static function get_products() {
$products = get_transient( 'directorist_products' );

if ( ! empty( $products ) ) {
return $products;
}

$products = static::get( 'v1/get-remote-products' );

if ( empty( $products ) ) {
return array(
'themes' => array(),
'extensions' => array(),
);
}

$products = json_decode( $products, true );

set_transient( 'directorist_products', $products, 30 * DAY_IN_SECONDS );

return $products;
}

/**
* @return array
*/
protected static function get_request_args() {
return array(
'method' => 'GET',
'timeout' => 30,
'redirection' => 5,
'headers' => array(
'user-agent' => 'Directorist/' . ATBDP_VERSION,
'Accept' => 'application/json',
),
'cookies' => array(),
);
}

/**
*
* @return string
*/
public static function get( $endpoint = '' ) {
$url = static::URL . $endpoint;
$response = wp_remote_get( $url, static::get_request_args() );

return wp_remote_retrieve_body( $response );
}
}
50 changes: 11 additions & 39 deletions includes/classes/class-extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
return;
}

use Directorist\Core\API;

if ( ! class_exists( 'ATBDP_Extensions' ) ) {

/**
Expand Down Expand Up @@ -209,38 +211,10 @@ public function has_match_in_active_plugins( $plugin_name = '' ) {

// get_the_products_list
public function setup_products_list() {
$products = API::get_products();

$url = 'https://app.directorist.com/wp-json/directorist/v1/get-remote-products?' . ATBDP_VERSION;
$headers = array(
'user-agent' => 'Directorist/' . md5( esc_url( home_url() ) ) . ';',
'Accept' => 'application/json',
);

$config = array(
'method' => 'GET',
'timeout' => 30,
'redirection' => 5,
'httpversion' => '1.0',
'headers' => $headers,
'cookies' => array(),
);

$response_body = array();

try {
$response = wp_remote_get( $url, $config );

if ( ! is_wp_error( $response ) ) {
$response_body = is_string( $response['body'] ) ? json_decode( $response['body'], true ) : $response['body'];
$extensions = $response_body['extensions'];
$themes = $response_body['themes'];

$this->extensions = apply_filters( 'atbdp_extension_list', $extensions );
$this->themes = apply_filters( 'atbdp_theme_list', $themes );
}
} catch ( Exception $e ) {

}
$this->extensions = apply_filters( 'atbdp_extension_list', $products['extensions'] );
$this->themes = apply_filters( 'atbdp_theme_list', $products['themes'] );
}

// exclude_purchased_extensions
Expand Down Expand Up @@ -319,13 +293,12 @@ public function exclude_purchased_themes( $themes ) {
public function get_active_extensions() {
$active_extensions = array();

foreach ( $this->extensions as $extension_key => $extension_args ) {

if ( empty( $extension_args['active'] ) ) {
foreach ( $this->extensions as $extension_slug => $extension ) {
if ( empty( $extension['active'] ) ) {
continue;
}

$active_extensions[ $extension_key ] = $extension_args;
$active_extensions[ $extension_slug ] = $extension;
}

return $active_extensions;
Expand All @@ -335,13 +308,12 @@ public function get_active_extensions() {
public function get_active_themes() {
$active_themes = array();

foreach ( $this->themes as $theme_key => $theme_args ) {

if ( empty( $theme_args['active'] ) ) {
foreach ( $this->themes as $theme_slug => $theme ) {
if ( empty( $theme['active'] ) ) {
continue;
}

$active_themes[ $theme_key ] = $theme_args;
$active_themes[ $theme_slug ] = $theme;
}

return $active_themes;
Expand Down
39 changes: 4 additions & 35 deletions includes/classes/class-upgrade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Directorist\Core\API;

// it handles directorist upgrade
class ATBDP_Upgrade
{
Expand Down Expand Up @@ -82,40 +84,7 @@ public function bfcm_notice() {
}

public static function promo_remote_get() {
$url = 'https://app.directorist.com/wp-json/directorist/v1/get-promo';
$headers = [
'user-agent' => 'Directorist/' . md5( esc_url( home_url() ) ) . ';',
'Accept' => 'application/json',
];

$config = [
'method' => 'GET',
'timeout' => 30,
'redirection' => 5,
'httpversion' => '1.0',
'headers' => $headers,
'cookies' => [],
];

$response_body = [];

$cached_response = get_transient( 'directorist_get_promo_banner' );

if( $cached_response ) {
$response_body = $cached_response;
} else {
try {
$response = wp_remote_get( $url, $config );
$response_body = ! is_wp_error( $response ) ? wp_remote_retrieve_body( $response ) : [];
set_transient( 'directorist_get_promo_banner', $response_body, 24 * HOUR_IN_SECONDS );
} catch ( Exception $e ) {
return $response_body;
}
}

$response_body = is_string( $response_body ) ? json_decode( $response_body ) : $response_body;

return $response_body;
return API::get_promotion();
}

public function upgrade_notice() {
Expand Down Expand Up @@ -192,4 +161,4 @@ public static function promo_link( $link ) {
return $link;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<br>
<!-- Themes and Extensions -->
<div class="extension-theme-wrapper">
<?php if ( ! empty( $args['extensions_promo_list'] ) ) : ?>
<?php
if ( ! empty( $args['ATBDP_Extensions']->extensions ) ) : ?>
<div class="et-column">
<h3><?php esc_html_e( 'Extensions', 'directorist' ) ?></h3>

<?php foreach( $args['extensions_promo_list'] as $extension_key => $extension ) :
<?php foreach( $args['ATBDP_Extensions']->extensions as $extension_key => $extension ) :
$link = ATBDP_Upgrade::promo_link( $extension['link'] );
?>
<div class="et-card">
Expand All @@ -25,11 +26,11 @@
</div><!-- ends: .et-column -->
<?php endif; ?>

<?php if ( ! empty( $args['themes_promo_list'] ) ) : ?>
<?php if ( ! empty( $args['ATBDP_Extensions']->themes ) ) : ?>
<div class="et-column">
<h3><?php esc_html_e( 'Themes', 'directorist' ) ?></h3>

<?php foreach( $args['themes_promo_list'] as $theme_key => $theme ) :
<?php foreach( $args['ATBDP_Extensions']->themes as $theme_key => $theme ) :
$link = ATBDP_Upgrade::promo_link( $theme['link'] );
?>
<div class="et-card">
Expand Down