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

add caching and request header #1760

Closed
wants to merge 8 commits into from
Closed
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
104 changes: 52 additions & 52 deletions includes/classes/class-extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,37 +210,38 @@ public function has_match_in_active_plugins( $plugin_name = '' ) {
// get_the_products_list
public function setup_products_list() {

$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',
);
$url = 'https://app.directorist.com/wp-json/directorist/v1/get-remote-products?';

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

$response_body = array();
$cached_response = get_transient( 'directorist_get_promo_products_v' . ATBDP_VERSION );

try {
$response = wp_remote_get( $url, $config );
if( $cached_response ) {
$response_body = $cached_response;
} else {

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'];
try {
$response = wp_remote_get( $url, $headers );

if ( ! is_wp_error( $response ) ) {
$response_body = is_string( $response['body'] ) ? json_decode( $response['body'], true ) : $response['body'];
set_transient( 'directorist_get_promo_products_v' . ATBDP_VERSION, $response_body, MONTH_IN_SECONDS );
}
} catch ( Exception $e ) {

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

if( empty( $response_body ) ) {
return;
}

$extensions = $response_body['extensions'];
$themes = $response_body['themes'];

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

// exclude_purchased_extensions
Expand Down Expand Up @@ -2063,15 +2064,10 @@ public static function remote_activate_license( $license_item = array() ) {
'license' => $license,
);

$headers = self::request_header( 'GET',$query_args );

try {
$response = wp_remote_get(
$activation_url,
array(
'timeout' => 15,
'sslverify' => false,
'body' => $query_args,
)
);
$response = wp_remote_get( $activation_url, $headers );

$response_status = json_decode( $response['body'], true );
} catch ( Exception $e ) {
Expand All @@ -2096,30 +2092,34 @@ public static function remote_activate_license( $license_item = array() ) {
return $status;
}

// remote_authenticate_user
public static function remote_authenticate_user( $user_credentials = array() ) {
$status = array( 'success' => true );
public static function request_header( $type = 'GET', $body = [] ) {

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

$config = array(
'method' => 'GET',
return array(
'method' => $type,
'timeout' => 30,
'redirection' => 5,
'httpversion' => '1.0',
'headers' => $headers,
'headers' => [
'user-agent' => 'Directorist/' . ATBDP_VERSION . ';',
'Accept' => 'application/json',
],
'cookies' => array(),
'body' => $user_credentials, // [ 'user' => '', 'password' => '']
'body' => $body,
);
}

// remote_authenticate_user
public static function remote_authenticate_user( $user_credentials = array() ) {
$status = array( 'success' => true );

$url = 'https://directorist.com/wp-json/directorist/v1/licencing';

$headers = self::request_header( 'GET',$user_credentials );

$response_body = array();

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

if ( is_wp_error( $response ) ) {
$status['success'] = false;
Expand Down Expand Up @@ -2147,6 +2147,11 @@ public static function remote_authenticate_user( $user_credentials = array() ) {

// get_file_download_link
public static function get_file_download_link( $file_item = array(), $product_type = 'plugin' ) {

if ( isset( $file_item['download_link'] ) ) {
return $file_item['download_link'];
}

if ( ! is_array( $file_item ) ) {
return '';
}
Expand All @@ -2171,15 +2176,10 @@ public static function get_file_download_link( $file_item = array(), $product_ty
'get_info' => 'download_link',
);

$headers = self::request_header( 'GET',$query_args );

try {
$response = wp_remote_get(
$activation_url,
array(
'timeout' => 15,
'sslverify' => false,
'body' => $query_args,
)
);
$response = wp_remote_get( $activation_url, $headers );

$response = json_decode( $response['body'], true );
} catch ( Exception $e ) {
Expand Down
37 changes: 22 additions & 15 deletions includes/classes/class-upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,46 @@ public function bfcm_notice() {
}

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

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

$response_body = [];


// Get the cached response
$cached_response = get_transient( 'directorist_get_promo_banner' );

if( $cached_response ) {
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;

// Determine the promo end date and calculate the remaining duration
$promo_end_date = ! empty( $response_body->promo_end_date ) ? $response_body->promo_end_date : time() + DAY_IN_SECONDS;
$remaining_duration = $promo_end_date - time();

// Cache the response only for the remaining duration
set_transient( 'directorist_get_promo_banner', $response_body, $remaining_duration );
}

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


return $response_body;
}

Expand Down