Plugin Directory

Changeset 2752034


Ignore:
Timestamp:
07/05/2022 10:08:20 AM (3 years ago)
Author:
rafiq91
Message:

released version 7.2.3

Location:
directorist
Files:
907 added
5 edited

Legend:

Unmodified
Added
Removed
  • directorist/trunk/config.php

    r2730565 r2752034  
    11<?php
    22// Plugin version.
    3 if ( ! defined( 'ATBDP_VERSION' ) ) {define( 'ATBDP_VERSION', '7.2.2' );}
     3if ( ! defined( 'ATBDP_VERSION' ) ) {define( 'ATBDP_VERSION', '7.2.3' );}
    44// Plugin Folder Path.
    55if ( ! defined( 'ATBDP_DIR' ) ) { define( 'ATBDP_DIR', plugin_dir_path( __FILE__ ) ); }
  • directorist/trunk/directorist-base.php

    r2730565 r2752034  
    44 * Plugin URI: https://wpwax.com
    55 * Description: A comprehensive solution to create professional looking directory site of any kind. Like Yelp, Foursquare, etc.
    6  * Version: 7.2.2
     6 * Version: 7.2.3
    77 * Author: wpWax
    88 * Author URI: https://wpwax.com
  • directorist/trunk/includes/classes/class-extension.php

    r2720359 r2752034  
    11<?php
    2 
    32/**
    43 * ATBDP Extensions class
     
    1514
    1615if ( ! defined( 'ABSPATH' ) ) {
    17     die( 'Direct access is not allowed.' );
     16    die( 'Direct access is not allowed.' );
    1817}
    1918
     19if ( ! is_admin() ) {
     20    return;
     21}
     22
    2023if ( ! class_exists( 'ATBDP_Extensions' ) ) {
    2124
    22     /**
    23      * Class ATBDP_Extensions
    24      */
    25     class ATBDP_Extensions {
    26         public static $extensions_aliases = [];
    27 
    28         public $extensions          = [];
    29         public $themes              = [];
    30         public $required_extensions = [];
    31 
    32         public function __construct() {
    33             add_action( 'admin_menu', [$this, 'admin_menu'], 100 );
    34 
    35             if( ! empty( $_GET['page'] ) && ( 'atbdp-extension' === $_GET['page'] ) ){
    36                 add_action( 'admin_init', [ $this, 'initial_setup' ] );
    37             }
    38            
    39             add_action( 'admin_init', [ $this, 'setup_ajax_actions' ] );
    40         }
    41 
    42         public function setup_ajax_actions() {
    43             if ( ! current_user_can( 'manage_options' ) ) {
    44                 return;
    45             }
    46 
    47             // Ajax
    48             add_action( 'wp_ajax_atbdp_authenticate_the_customer', [$this, 'authenticate_the_customer'] );
    49             add_action( 'wp_ajax_atbdp_download_file', [$this, 'handle_file_download_request'] );
    50             add_action( 'wp_ajax_atbdp_install_file_from_subscriptions', [$this, 'handle_file_install_request_from_subscriptions'] );
    51             add_action( 'wp_ajax_atbdp_plugins_bulk_action', [$this, 'plugins_bulk_action'] );
    52             add_action( 'wp_ajax_atbdp_activate_theme', [$this, 'activate_theme'] );
    53             add_action( 'wp_ajax_atbdp_activate_plugin', [$this, 'activate_plugin'] );
    54             add_action( 'wp_ajax_atbdp_update_plugins', [$this, 'handle_plugins_update_request'] );
    55             add_action( 'wp_ajax_atbdp_update_theme', [$this, 'handle_theme_update_request'] );
    56             add_action( 'wp_ajax_atbdp_refresh_purchase_status', [$this, 'handle_refresh_purchase_status_request'] );
    57             add_action( 'wp_ajax_atbdp_close_subscriptions_sassion', [$this, 'handle_close_subscriptions_sassion_request'] );
    58 
    59             // add_action( 'wp_ajax_atbdp_download_purchased_items', array($this, 'download_purchased_items') );
    60         }
    61 
    62         // initial_setup
    63         public function initial_setup() {
    64             if ( ! current_user_can( 'manage_options' ) ) {
    65                 return;
    66             }
    67 
    68             $this->setup_extensions_alias();
    69 
    70             wp_update_plugins();
    71            
    72             // Apply hook to required extensions
    73             $this->required_extensions = apply_filters( 'directorist_required_extensions', [] );
    74 
    75             $this->setup_products_list();
    76         }
    77 
    78         // setup_extensions_alias
    79         public function setup_extensions_alias() {
    80 
    81             // Latest Key     => Deprecated key
    82             // Deprecated key => Latest Key
    83             self::$extensions_aliases = apply_filters( 'directorist_extensions_aliases', [
    84                 'directorist-listings-with-map'        => 'directorist-listings-map',
    85                 'directorist-listings-map'             => 'directorist-listings-with-map',
    86 
    87                 'directorist-adverts-manager'          => 'directorist-ads-manager',
    88                 'directorist-ads-manager'              => 'directorist-adverts-manager',
    89 
    90                 'directorist-gallery'                  => 'directorist-image-gallery',
    91                 'directorist-image-gallery'            => 'directorist-gallery',
    92 
    93                 'directorist-slider-carousel'          => 'directorist-listings-slider-carousel',
    94                 'directorist-listings-slider-carousel' => 'directorist-slider-carousel',
    95 
    96                 'directorist-faqs'                     => 'directorist-listing-faqs',
    97                 'directorist-listing-faqs'             => 'directorist-faqs',
    98             ] );
    99         }
    100 
    101         // get_required_extension_list
    102         public function get_required_extension_list() {
    103             $required_extensions = [];
    104 
    105             foreach ( $this->required_extensions as $recommandation ) {
    106 
    107                 if ( ! isset( $recommandation['extensions'] ) ) {
    108                     continue;
    109                 }
    110 
    111                 if ( ! is_array( $recommandation['extensions'] ) ) {
    112                     continue;
    113                 }
    114 
    115                 foreach ( $recommandation['extensions'] as $extension ) {
    116                     $extension_alias = $this->get_extension_alias_key( $extension );
    117 
    118                     if ( ! ( isset( $this->extensions[$extension] ) || isset( $this->extensions[$extension_alias] ) ) ) {
    119                         continue;
    120                     }
    121 
    122                     if ( empty( $required_extensions[$extension] ) ) {
    123                         $required_extensions[$extension] = [];
    124                     }
    125 
    126                     $required_extensions[$extension][] = $recommandation['ref'];
    127                 }
    128 
    129             }
    130 
    131             return $required_extensions;
    132         }
    133 
    134         // prepare_the_final_requred_extension_list
    135         public function prepare_the_final_requred_extension_list( array $args = [] ) {
    136             $recommandation = [];
    137 
    138             $required_extensions_list = $this->get_required_extension_list();
    139             $purchased_extension_list = self::get_purchased_extension_list();
    140             $purchased_extensions     = ( ! empty( $purchased_extension_list ) && is_array( $purchased_extension_list ) ) ? array_keys( $purchased_extension_list ) : [];
    141             $plugin_dir_path          = trailingslashit( dirname( ATBDP_DIR ) );
    142 
    143             foreach ( $required_extensions_list as $extension => $recommanded_by ) {
    144                 $extension_alias = $this->get_extension_alias_key( $extension );
    145 
    146                 if ( $this->has_match_in_active_plugins( [ $extension, $extension_alias ] ) ) {
    147                     continue;
    148                 }
    149 
    150                 $is_purchased       = ( in_array( $extension, $purchased_extensions ) ) ? true : false;
    151                 $is_purchased_alias = ( in_array( $extension_alias, $purchased_extensions ) ) ? true : false;
    152 
    153                 $is_installed = file_exists( $plugin_dir_path . $extension );
    154                 $is_installed_alias = ( ! empty( $extension_alias ) && file_exists( $plugin_dir_path . $extension_alias ) ) ? true : false;
    155 
    156 
    157                 $base = "{$extension}/{$extension}.php";
    158 
    159                 if ( ! empty( $this->extensions[ $extension ] ) && ! empty( $this->extensions[ $extension ]['base'] ) ) {
    160                     $base = $this->extensions[ $extension ]['base'];
    161                 }
    162 
    163                 if ( ! empty( $this->extensions[ $extension_alias ] ) && ! empty( $this->extensions[ $extension_alias ]['base'] ) ) {
    164                     $base = $this->extensions[ $extension_alias ]['base'];
    165                 }
    166 
    167                 $recommandation[$extension]              = [];
    168                 $recommandation[$extension]['ref']       = $recommanded_by;
    169                 $recommandation[$extension]['base']      = $base;
    170                 $recommandation[$extension]['purchased'] = ( $is_purchased || $is_purchased_alias ) ? true : false;
    171                 $recommandation[$extension]['installed'] = ( $is_installed || $is_installed_alias ) ? true : false;
    172             }
    173 
    174             return $recommandation;
    175         }
    176 
    177         public function has_match_in_active_plugins( $plugin_name = '' ) {
    178             $match_found = false;
    179 
    180             $active_plugins = get_option( 'active_plugins', array() );
    181            
    182             if ( empty( $plugin_name ) ) {
    183                 return false;
    184             }
    185 
    186             if ( empty( $active_plugins ) ) {
    187                 return false;
    188             }
    189 
    190             if ( ! is_array( $active_plugins ) ) {
    191                 return false;
    192             }
    193 
    194             foreach ( $active_plugins as $plugin_path ) {
    195                 if ( empty( $plugin_name ) && ( false !== strpos( $plugin_path, $plugin_name ) ) ) {
    196                     return true;
    197                 }
    198 
    199                 if ( is_array( $plugin_name ) ) {
    200                     foreach ( $plugin_name as $plugin_key ) {
    201                         if ( is_string( $plugin_key ) && ! empty( $plugin_key ) && false !== strpos( $plugin_path, $plugin_key ) ) {
    202                             return true;
    203                         }
    204                     }
    205                 }
    206             }
    207 
    208             return $match_found;
    209         }
    210 
    211         // get_the_products_list
    212         public function setup_products_list() {
    213 
    214 
    215             $url     = 'https://app.directorist.com/wp-json/directorist/v1/get-remote-products';
    216             $headers = [
    217                 'user-agent' => 'Directorist/' . md5( esc_url( home_url() ) ) . ';',
    218                 'Accept'     => 'application/json',
    219             ];
    220 
    221             $config = [
    222                 'method'      => 'GET',
    223                 'timeout'     => 30,
    224                 'redirection' => 5,
    225                 'httpversion' => '1.0',
    226                 'headers'     => $headers,
    227                 'cookies'     => [],
    228             ];
    229 
    230             $response_body = [];
    231 
    232             try {
    233                 $response = wp_remote_get( $url, $config );
    234 
    235                 if ( ! is_wp_error( $response ) ) {
    236                     $response_body = ( 'string' === gettype( $response['body'] ) ) ? json_decode( $response['body'], true ) : $response['body'];
    237                     $extensions = $response_body['extensions'];
    238                     $themes = $response_body['themes'];
    239 
    240                     $this->extensions = apply_filters( 'atbdp_extension_list', $extensions );
    241                     $this->themes = apply_filters( 'atbdp_theme_list', $themes );
    242                 }
    243                
    244             } catch ( Exception $e ) {
    245 
    246             }
    247         }
    248 
    249         // exclude_purchased_extensions
    250         public function exclude_purchased_extensions( $extensions ) {
    251             $has_subscriptions_sassion = get_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
    252             $is_logged_in              = ( ! empty( $has_subscriptions_sassion ) ) ? true : false;
    253 
    254             if ( ! $is_logged_in ) {
    255                 return $extensions;
    256             }
    257 
    258             $purchased_products = get_user_meta( get_current_user_id(), '_atbdp_purchased_products', true );
    259 
    260             if ( empty( $purchased_products ) ) {
    261                 return $extensions;
    262             }
    263 
    264             $purchased_extensions = ( ! empty( $purchased_products['plugins'] ) ) ? $purchased_products['plugins'] : '';
    265 
    266             if ( empty( $purchased_extensions ) ) {
    267                 return $extensions;
    268             }
    269 
    270             $purchased_extensions_keys = ( is_array( $purchased_extensions ) ) ? array_keys( $purchased_extensions ) : [];
    271             $excluded_extensions       = $extensions;
    272 
    273             foreach ( $excluded_extensions as $extension_key => $extension ) {
    274 
    275                 if ( ! in_array( $extension_key, $purchased_extensions_keys ) ) {
    276                     continue;
    277                 }
    278 
    279                 $excluded_extensions[$extension_key]['active'] = false;
    280             }
    281 
    282             return $excluded_extensions;
    283         }
    284 
    285         // exclude_purchased_themes
    286         public function exclude_purchased_themes( $themes ) {
    287             $has_subscriptions_sassion = get_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
    288             $is_logged_in              = ( ! empty( $has_subscriptions_sassion ) ) ? true : false;
    289 
    290             if ( ! $is_logged_in ) {
    291                 return $themes;
    292             }
    293 
    294             $purchased_products = get_user_meta( get_current_user_id(), '_atbdp_purchased_products', true );
    295 
    296             if ( empty( $purchased_products ) ) {
    297                 return $themes;
    298             }
    299 
    300             $purchased_themes = ( ! empty( $purchased_products['themes'] ) ) ? $purchased_products['themes'] : '';
    301 
    302             if ( empty( $purchased_themes ) ) {
    303                 return $themes;
    304             }
    305 
    306             $purchased_themes_keys = is_array( $purchased_themes ) ? array_keys( $purchased_themes ) : [];
    307             $excluded_themes       = $themes;
    308 
    309             foreach ( $excluded_themes as $theme_key => $theme ) {
    310 
    311                 if ( ! in_array( $theme_key, $purchased_themes_keys ) ) {
    312                     continue;
    313                 }
    314 
    315                 $excluded_themes[$theme_key]['active'] = false;
    316             }
    317 
    318             return $excluded_themes;
    319         }
    320 
    321         // get_active_extensions
    322         public function get_active_extensions() {
    323             $active_extensions = [];
    324 
    325             foreach ( $this->extensions as $extension_key => $extension_args ) {
    326 
    327                 if ( empty( $extension_args['active'] ) ) {
    328                     continue;
    329                 }
    330 
    331                 $active_extensions[$extension_key] = $extension_args;
    332             }
    333 
    334             return $active_extensions;
    335         }
    336 
    337         // get_active_themes
    338         public function get_active_themes() {
    339             $active_themes = [];
    340 
    341             foreach ( $this->themes as $theme_key => $theme_args ) {
    342 
    343                 if ( empty( $theme_args['active'] ) ) {
    344                     continue;
    345                 }
    346 
    347                 $active_themes[$theme_key] = $theme_args;
    348             }
    349 
    350             return $active_themes;
    351         }
    352 
    353         // handle_plugins_update_request
    354         public function handle_plugins_update_request() {
    355 
    356             if ( ! $this->is_verified_nonce() ) {
    357                 $status            = [];
    358                 $status['success'] = false;
    359                 $status['message'] = 'Invalid request';
    360 
    361                 wp_send_json( ['status' => $status] );
    362             }
    363 
    364             $plugin_key = ( isset( $_POST['plugin_key'] ) ) ? $_POST['plugin_key'] : '';
    365             $status     = $this->update_plugins( ['plugin_key' => $plugin_key] );
    366 
    367             wp_send_json( $status );
    368         }
    369 
    370         // update_plugins
    371         public function update_plugins( array $args = [] ) {
    372             $default = ['plugin_key' => ''];
    373             $args    = array_merge( $default, $args );
    374 
    375             $status     = ['success' => true];
    376             $plugin_key = $args['plugin_key'];
    377 
    378             $plugin_updates       = get_site_transient( 'update_plugins' );
    379             $outdated_plugins     = $plugin_updates->response;
    380             $outdated_plugins_key = ( is_array( $outdated_plugins ) ) ? array_keys( $outdated_plugins ) : [];
    381 
    382             if ( empty( $outdated_plugins_key ) ) {
    383                 $status['message'] = __( 'All plugins are up to date', 'directorist' );
    384 
    385                 return ['status' => $status];
    386             }
    387 
    388             if ( ! empty( $plugin_key ) && ! in_array( $plugin_key, $outdated_plugins_key ) ) {
    389                 $status['message'] = __( 'The plugin is up to date', 'directorist' );
    390 
    391                 return ['status' => $status];
    392             }
    393 
    394             $plugins_available_in_subscriptions = self::get_purchased_extension_list();
    395 
    396             // Update single
    397             if ( ! empty( $plugin_key ) ) {
    398                 $plugin_key  = self::filter_plugin_key_from_base_name( $plugin_key );
    399                 $plugin_item = self::extract_plugin_from_list( $plugin_key, $plugins_available_in_subscriptions );
    400                 $url         = self::get_file_download_link( $plugin_item, 'plugin' );
    401 
    402                 $download_status = $this->download_plugin( ['url' => $url] );
    403 
    404                 if ( ! $download_status['success'] ) {
    405                     $status['success'] = false;
    406                     $status['message'] = __( 'The plugin could not update', 'directorist' );
    407                     $status['log']     = $download_status['message'];
    408                 } else {
    409                     $status['success'] = true;
    410                     $status['message'] = __( 'The plugin has been updated successfully', 'directorist' );
    411                     $status['log']     = $download_status['message'];
    412                 }
    413 
    414                 return ['status' => $status];
    415             }
    416 
    417             // Update all
    418             $updated_plugins       = [];
    419             $update_failed_plugins = [];
    420 
    421             foreach ( $outdated_plugins as $plugin_base => $plugin ) {
    422                 $plugin_key  = self::filter_plugin_key_from_base_name( $plugin_key );
    423                 $plugin_item = self::extract_plugin_from_list( $plugin_key, $plugins_available_in_subscriptions );
    424                 $url         = self::get_file_download_link( $plugin_item, 'plugin' );
    425 
    426                 $download_status = $this->download_plugin( ['url' => $url] );
    427 
    428                 if ( ! $download_status['success'] ) {
    429                     $update_failed_plugins[$plugin_base] = $plugin;
    430                 } else {
    431                     $updated_plugins[$plugin_base] = $plugin;
    432                 }
    433 
    434             }
    435 
    436             $status['updated_plugins']       = $updated_plugins;
    437             $status['update_failed_plugins'] = $update_failed_plugins;
    438 
    439             if ( ! empty( $updated_plugins ) && ! empty( $update_failed_plugins ) ) {
    440                 $status['success'] = false;
    441                 $status['message'] = __( 'Some of the plugin could not update', 'directorist' );
    442             }
    443 
    444             if ( empty( $update_failed_plugins ) ) {
    445                 $status['success'] = true;
    446                 $status['message'] = __( 'All the plugins are updated successfully', 'directorist' );
    447             }
    448 
    449             if ( empty( $updated_plugins ) ) {
    450                 $status['success'] = true;
    451                 $status['message'] = __( 'No plugins could not update', 'directorist' );
    452             }
    453 
    454             return ['status' => $status];
    455         }
    456 
    457         // extract_plugin_from_list
    458         public static function extract_plugin_from_list( $plugin_key = '', $list = [] ) {
    459 
    460             $plugin_item = [];
    461             $plugin_key  = ( is_string( $plugin_key ) ) ? $plugin_key : '';
    462             $list        = ( is_array( $list ) ) ? $list : [];
    463 
    464             $keys_in_list = array_keys( $list );
    465 
    466             if ( in_array( $plugin_key, $keys_in_list ) ) {
    467                 $plugin_item = $list[$plugin_key];
    468             }
    469 
    470             $plugin_alias_key = self::get_extension_alias_key( $plugin_key );
    471 
    472             if ( in_array( $plugin_alias_key, $keys_in_list ) ) {
    473                 $plugin_item = $list[$plugin_alias_key];
    474             }
    475 
    476             return $plugin_item;
    477         }
    478 
    479         // filter_plugin_key_from_base_name
    480         public static function filter_plugin_key_from_base_name( $plugin_key = '' ) {
    481 
    482             if ( ! is_string( $plugin_key ) ) {
    483                 return '';
    484             }
    485 
    486             $plugin_key = preg_replace( '/\/.+/', '', $plugin_key );
    487 
    488             return $plugin_key;
    489         }
    490 
    491         // get_extension_alias_key
    492         public static function get_extension_alias_key( string $plugin_key = '' ) {
    493             $extensions_aliases      = self::$extensions_aliases;
    494             $extensions_aliases_keys = ( is_array( $extensions_aliases ) && ! empty( $extensions_aliases ) ) ? array_keys( $extensions_aliases ) : [];
    495             $plugin_alias_key        = in_array( $plugin_key, $extensions_aliases_keys ) ? $extensions_aliases[$plugin_key] : '';
    496 
    497             return $plugin_alias_key;
    498         }
    499 
    500         // plugins_bulk_action
    501         public function plugins_bulk_action() {
    502             $status = ['success' => true];
    503 
    504             if ( ! $this->is_verified_nonce() ) {
    505                 $status['success'] = false;
    506                 $status['message'] = 'Invalid request';
    507 
    508                 wp_send_json( ['status' => $status] );
    509             }
    510 
    511             $task         = ( isset( $_POST['task'] ) ) ? $_POST['task'] : '';
    512             $plugin_items = ( isset( $_POST['plugin_items'] ) ) ? $_POST['plugin_items'] : '';
    513 
    514             // Validation
    515             if ( empty( $task ) ) {
    516                 $status['success'] = false;
    517                 $status['message'] = 'No task found';
    518                 wp_send_json( ['status' => $status] );
    519             }
    520 
    521             if ( empty( $plugin_items ) ) {
    522                 $status['success'] = false;
    523                 $status['message'] = 'No plugin items found';
    524                 wp_send_json( ['status' => $status] );
    525             }
    526 
    527             // Activate
    528             if ( 'activate' === $task ) {
    529                 foreach ( $plugin_items as $plugin ) {
    530                     activate_plugin( $plugin );
    531                 }
    532             }
    533 
    534             // Deactivate
    535             if ( 'deactivate' === $task ) {
    536                 deactivate_plugins( $plugin_items );
    537             }
    538 
    539             // Uninstall
    540             if ( 'uninstall' === $task ) {
    541                 delete_plugins( $plugin_items );
    542             }
    543 
    544             wp_send_json( ['status' => $status] );
    545         }
    546 
    547         // activate_theme
    548         public function activate_theme() {
    549             $status           = ['success' => true];
    550             $theme_stylesheet = ( isset( $_POST['theme_stylesheet'] ) ) ? $_POST['theme_stylesheet'] : '';
    551 
    552             if ( ! $this->is_verified_nonce() ) {
    553                 $status['success'] = false;
    554                 $status['message'] = 'Invalid request';
    555 
    556                 wp_send_json( ['status' => $status] );
    557             }
    558 
    559             if ( empty( $theme_stylesheet ) ) {
    560                 $status['success'] = false;
    561                 $status['message'] = __( 'Theme\'s stylesheet is missing', 'directorist' );
    562 
    563                 wp_send_json( ['status' => $status] );
    564             }
    565 
    566             switch_theme( $theme_stylesheet );
    567             wp_send_json( ['status' => $status] );
    568         }
    569 
    570         // activate_plugin
    571         public function activate_plugin() {
    572             $status     = ['success' => true];
    573             $plugin_key = ( isset( $_POST['item_key'] ) ) ? $_POST['item_key'] : '';
    574 
    575             if ( ! $this->is_verified_nonce() ) {
    576                 $status['success'] = false;
    577                 $status['message'] = 'Invalid request';
    578 
    579                 wp_send_json( ['status' => $status] );
    580             }
    581 
    582             if ( empty( $plugin_key ) ) {
    583                 $status['success'] = false;
    584                 $status['log']     = ['$plugin_key' => $plugin_key];
    585                 $status['message'] = __( 'Please specefy which plugin to activate', 'directorist' );
    586 
    587                 wp_send_json( ['status' => $status] );
    588             }
    589 
    590             activate_plugin( $plugin_key );
    591             wp_send_json( ['status' => $status] );
    592         }
    593 
    594         // handle_theme_update_request
    595         public function handle_theme_update_request() {
    596 
    597             if ( ! $this->is_verified_nonce() ) {
    598                 $status            = [];
    599                 $status['success'] = false;
    600                 $status['message'] = 'Invalid request';
    601 
    602                 wp_send_json( ['status' => $status] );
    603             }
    604 
    605             $theme_stylesheet = ( isset( $_POST['theme_stylesheet'] ) ) ? $_POST['theme_stylesheet'] : '';
    606 
    607             $update_theme_status = $this->update_the_themes( ['theme_stylesheet' => $theme_stylesheet] );
    608             wp_send_json( $update_theme_status );
    609         }
    610 
    611         // update_the_theme
    612         public function update_the_themes( array $args = [] ) {
    613             $default = ['theme_stylesheet' => ''];
    614             $args    = array_merge( $default, $args );
    615 
    616             $status = ['success' => true];
    617 
    618             $theme_stylesheet    = $args['theme_stylesheet'];
    619             $theme_updates       = get_site_transient( 'update_themes' );
    620             $outdated_themes     = $theme_updates->response;
    621             $outdated_themes_key = ( is_array( $outdated_themes ) ) ? array_keys( $outdated_themes ) : [];
    622 
    623             if ( empty( $outdated_themes_key ) ) {
    624                 $status['message'] = __( 'All themes are up to date', 'directorist' );
    625 
    626                 return ['status' => $status];
    627             }
    628 
    629             if ( ! empty( $theme_stylesheet ) && ! in_array( $theme_stylesheet, $outdated_themes_key ) ) {
    630                 $status['message'] = __( 'The theme is up to date', 'directorist' );
    631 
    632                 return ['status' => $status];
    633             }
    634 
    635             $themes_available_in_subscriptions      = self::get_purchased_theme_list();
    636             $themes_available_in_subscriptions_keys = ( is_array( $themes_available_in_subscriptions ) ) ? array_keys( $themes_available_in_subscriptions ) : [];
    637 
    638             // Check if stylesheet is present
    639             if ( ! empty( $theme_stylesheet ) ) {
    640 
    641                 // Check if the the update is available
    642                 if ( ! in_array( $theme_stylesheet, $outdated_themes_key ) ) {
    643                     $status['success'] = false;
    644                     $status['message'] = __( 'The theme is already upto date', 'directorist' );
    645 
    646                     return ['status' => $status];
    647                 }
    648 
    649                 $theme_item = $themes_available_in_subscriptions[$theme_stylesheet];
    650                 $url        = self::get_file_download_link( $theme_item, 'theme' );
    651                 $url        = ( empty( $url ) && ! empty( $outdated_themes[ $theme_stylesheet ]['package'] ) ) ? $outdated_themes[ $theme_stylesheet ]['package'] : $url;
    652                
    653                 $download_status = $this->download_theme( ['url' => $url] );
    654                
    655                 if ( ! $download_status['success'] ) {
    656                     $status['success'] = false;
    657                     $status['message'] = __( 'The theme could not update', 'directorist' );
    658                     $status['log']     = $download_status['message'];
    659                 } else {
    660                     $status['success'] = true;
    661                     $status['message'] = __( 'The theme has been updated successfully', 'directorist' );
    662                     $status['log']     = $download_status['message'];
    663                     wp_clean_themes_cache();
    664                 };
    665 
    666                 return ['status' => $status];
    667             }
    668 
    669             // Update all
    670             $updated_themes       = [];
    671             $update_failed_themes = [];
    672 
    673             foreach ( $outdated_themes as $theme_key => $theme ) {
    674                 $url = '';
    675 
    676                 if ( ! in_array( $theme_key, $themes_available_in_subscriptions_keys ) ) {
    677                     continue;
    678                 }
    679 
    680                 $theme_item = $themes_available_in_subscriptions[$theme_key];
    681                 $url        = self::get_file_download_link( $theme_item, 'theme' );
    682 
    683                 $download_status = $this->download_theme( ['url' => $url] );
    684 
    685                 if ( ! $download_status['success'] ) {
    686                     $update_failed_themes[$theme_key] = $theme;
    687                 } else {
    688                     $updated_themes[$theme_key] = $theme;
    689                 }
    690 
    691             }
    692 
    693             $status['updated_themes']       = $updated_themes;
    694             $status['update_failed_themes'] = $update_failed_themes;
    695 
    696             if ( ! empty( $updated_themes ) && ! empty( $update_failed_themes ) ) {
    697                 $status['success'] = false;
    698                 $status['message'] = __( 'Some of the theme could not update', 'directorist' );
    699             }
    700 
    701             if ( empty( $update_failed_themes ) ) {
    702                 $status['success'] = true;
    703                 $status['message'] = __( 'All the themes are updated successfully', 'directorist' );
    704             }
    705 
    706             if ( empty( $updated_themes ) ) {
    707                 $status['success'] = true;
    708                 $status['message'] = __( 'No themes could not update', 'directorist' );
    709             }
    710 
    711             return ['status' => $status];
    712         }
    713 
    714         // authenticate_the_customer
    715         public function authenticate_the_customer() {
    716 
    717             $status = ['success' => true, 'log' => []];
    718 
    719             if ( ! $this->is_verified_nonce() ) {
    720                 $status['success']                 = false;
    721                 $status['log']['invalid_request'] = [
    722                     'type'    => 'error',
    723                     'message' => 'Invalid request',
    724                 ];
    725             }
    726 
    727             // Get form data
    728             $username = ( isset( $_POST['username'] ) ) ? $_POST['username'] : '';
    729             $password = ( isset( $_POST['password'] ) ) ? urlencode( $_POST['password'] ) : '';
    730 
    731             // Validate username
    732             if ( empty( $username ) && ! empty( $password ) ) {
    733                 $status['success']                 = false;
    734                 $status['log']['username_missing'] = [
    735                     'type'    => 'error',
    736                     'message' => 'Username is required',
    737                 ];
    738             }
    739 
    740             // Validate password
    741             if ( empty( $password ) && ! empty( $username ) ) {
    742                 $status['success']                 = false;
    743                 $status['log']['password_missing'] = [
    744                     'type'    => 'error',
    745                     'message' => 'Password is required',
    746                 ];
    747             }
    748 
    749             // Validate username && password
    750             if ( empty( $password ) && empty( $username ) ) {
    751                 $status['success']                 = false;
    752                 $status['log']['password_missing'] = [
    753                     'type'    => 'error',
    754                     'message' => 'Username and Password is required',
    755                 ];
    756             }
    757 
    758             if ( ! $status['success'] ) {
    759                 wp_send_json( ['status' => $status] );
    760             }
    761 
    762             // Get licencing data
    763             $response = self::remote_authenticate_user( ['user' => $username, 'password' => $password] );
    764 
    765             // Validate response
    766             if ( ! $response['success'] ) {
    767                 $status['success']      = false;
    768                 $default_status_message = ( isset( $response['message'] ) ) ? $response['message'] : '';
    769 
    770                 if ( isset( $response['log'] ) && isset( $response['log']['errors'] ) && is_array( $response['log']['errors'] ) ) {
    771                     foreach ( $response['log']['errors'] as $error_key => $error_value ) {
    772                         $status['log'][$error_key] = [
    773                             'type'    => 'error',
    774                             'message' => ( is_array( $error_value ) ) ? $error_value[0] : $error_value,
    775                         ];
    776                     }
    777 
    778                 } else {
    779                     $status['log']['unknown_error'] = [
    780                         'type'    => 'error',
    781                         'message' => ( ! empty( $default_status_message ) ) ? $default_status_message : __( 'Something went wrong', 'directorist' ),
    782                     ];
    783                 }
    784 
    785                 wp_send_json( ['status' => $status, 'response_body' => $response] );
    786             }
    787 
    788             $previous_username = get_user_meta( get_current_user_id(), '_atbdp_subscribed_username', true );
    789 
    790             // Enable Sassion
    791             update_user_meta( get_current_user_id(), '_atbdp_subscribed_username', $username );
    792             update_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
    793 
    794             $plugins_available_in_subscriptions = self::get_purchased_extension_list();
    795             $themes_available_in_subscriptions  = self::get_purchased_theme_list();
    796             $has_previous_subscriptions         = ( ! empty( $plugins_available_in_subscriptions ) || ! empty( $themes_available_in_subscriptions ) ) ? true : false;
    797 
    798             if ( $previous_username === $username && $has_previous_subscriptions ) {
    799                 // Enable Sassion
    800                 update_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
    801                 $this->refresh_purchase_status( $args = ['password' => $password] );
    802 
    803                 wp_send_json( ['status' => $status, 'has_previous_subscriptions' => true] );
    804             }
    805 
    806             delete_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions' );
    807             delete_user_meta( get_current_user_id(), '_themes_available_in_subscriptions' );
    808 
    809             $license_data = $response['license_data'];
    810 
    811             // Update user meta
    812             if ( ! empty( $license_data['themes'] ) ) {
    813                 $themes_available_in_subscriptions = $this->prepare_available_in_subscriptions( $license_data['themes'] );
    814                 update_user_meta( get_current_user_id(), '_themes_available_in_subscriptions', $themes_available_in_subscriptions );
    815             }
    816 
    817             if ( ! empty( $license_data['plugins'] ) ) {
    818                 $plugins_available_in_subscriptions = $this->prepare_available_in_subscriptions( $license_data['plugins'] );
    819                 update_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions', $plugins_available_in_subscriptions );
    820             }
    821 
    822             $status['success']                 = true;
    823             $status['log']['login_successful'] = [
    824                 'type'    => 'success',
    825                 'message' => 'Login is successful',
    826             ];
    827 
    828             wp_send_json( ['status' => $status, 'license_data' => $license_data] );
    829         }
    830 
    831         // handle_refresh_purchase_status_request
    832         public function handle_refresh_purchase_status_request() {
    833             $status   = ['success' => true];
    834 
    835             if ( ! $this->is_verified_nonce() ) {
    836                 $status['success'] = false;
    837                 $status['message'] = 'Invalid request';
    838 
    839                 wp_send_json( ['status' => $status] );
    840             }
    841 
    842             $password = ( isset( $_POST['password'] ) ) ? $_POST['password'] : '';
    843 
    844             $status = $this->refresh_purchase_status( ['password' => $password] );
    845 
    846             wp_send_json( $status );
    847         }
    848 
    849         // refresh_purchase_status
    850         public function refresh_purchase_status( array $args = [] ) {
    851             $status  = ['success' => true];
    852             $default = ['password' => ''];
    853             $args    = array_merge( $default, $args );
    854 
    855             if ( empty( $args['password'] ) ) {
    856                 $status['success'] = false;
    857                 $status['message'] = __( 'Password is required', 'directorist' );
    858 
    859                 return ['status' => $status];
    860             }
    861 
    862             $username = get_user_meta( get_current_user_id(), '_atbdp_subscribed_username', true );
    863             $password = $args['password'];
    864 
    865             if ( empty( $username ) ) {
    866                 $status['success'] = false;
    867                 $status['reload']  = true;
    868                 $status['message'] = __( 'Sassion is destroyed, please sign-in again', 'directorist' );
    869 
    870                 delete_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion' );
    871 
    872                 return ['status' => $status];
    873             }
    874 
    875             // Get licencing data
    876             $authentication = self::remote_authenticate_user( ['user' => $username, 'password' => $password] );
    877 
    878             // Validate response
    879             if ( ! $authentication['success'] ) {
    880                 $status['success'] = false;
    881                 $status['message'] = $authentication['message'];
    882 
    883                 return ['status' => $status, 'response_body' => $authentication];
    884             }
    885 
    886             $license_data = $authentication['license_data'];
    887 
    888             // Update user meta
    889             if ( ! empty( $license_data['themes'] ) ) {
    890                 $themes_available_in_subscriptions = $this->prepare_available_in_subscriptions( $license_data['themes'] );
    891                 update_user_meta( get_current_user_id(), '_themes_available_in_subscriptions', $themes_available_in_subscriptions );
    892             }
    893 
    894             if ( ! empty( $license_data['plugins'] ) ) {
    895                 $plugins_available_in_subscriptions = $this->prepare_available_in_subscriptions( $license_data['plugins'] );
    896                 update_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions', $plugins_available_in_subscriptions );
    897             }
    898 
    899             $status['success'] = true;
    900             $status['message'] = __( 'Your purchase has been refreshed successfuly', 'directorist' );
    901 
    902             return ['status' => $status];
    903         }
    904 
    905         // handle_close_subscriptions_sassion_request
    906         public function handle_close_subscriptions_sassion_request() {
    907 
    908             if ( ! $this->is_verified_nonce() ) {
    909                 $status            = [];
    910                 $status['success'] = false;
    911                 $status['message'] = 'Invalid request';
    912 
    913                 wp_send_json( ['status' => $status] );
    914             }
    915 
    916             $hard_logout_state = ( isset( $_POST['hard_logout'] ) ) ? $_POST['hard_logout'] : false;
    917             $status            = $this->close_subscriptions_sassion( ['hard_logout' => $hard_logout_state] );
    918 
    919             wp_send_json( $status );
    920         }
    921 
    922         // close_subscriptions_sassion
    923         public function close_subscriptions_sassion( array $args = [] ) {
    924             $default = ['hard_logout' => false];
    925             $args    = array_merge( $default, $args );
    926 
    927             $status = ['success' => true];
    928             delete_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion' );
    929 
    930             if ( $args['hard_logout'] ) {
    931                 delete_user_meta( get_current_user_id(), '_atbdp_subscribed_username' );
    932                 delete_user_meta( get_current_user_id(), '_themes_available_in_subscriptions' );
    933                 delete_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions' );
    934             }
    935 
    936             return $status;
    937         }
    938 
    939         // prepare_available_in_subscriptions
    940         public function prepare_available_in_subscriptions( array $products = [] ) {
    941             $available_in_subscriptions = [];
    942 
    943             if ( empty( $products ) ) {
    944                 return $available_in_subscriptions;
    945             }
    946 
    947             foreach ( $products as $product ) {
    948                 $product_key                              = $this->get_product_key_from_permalink( $product['permalink'] );
    949                 $available_in_subscriptions[$product_key] = $product;
    950             }
    951 
    952             return $available_in_subscriptions;
    953         }
    954 
    955         // get_product_key_from_permalink
    956         public function get_product_key_from_permalink( string $permalink = '' ) {
    957             $product_key = str_replace( 'http://directorist.com/product/', '', $permalink );
    958             $product_key = str_replace( 'https://directorist.com/product/', '', $product_key );
    959             $product_key = str_replace( '/', '', $product_key );
    960 
    961             return $product_key;
    962         }
    963 
    964         // handle_license_activation_request
    965         public function handle_license_activation_request() {
    966             $status       = ['success' => true];
    967             $license_item = ( isset( $_POST['license_item'] ) ) ? $_POST['license_item'] : '';
    968             $product_type = ( isset( $_POST['product_type'] ) ) ? $_POST['product_type'] : '';
    969 
    970             if ( empty( $license_item ) ) {
    971                 $status['success'] = false;
    972                 $status['message'] = 'License item is missing';
    973 
    974                 wp_send_json( ['status' => $status] );
    975             }
    976 
    977             if ( empty( $product_type ) ) {
    978                 $status['success'] = false;
    979                 $status['message'] = 'Product type is required';
    980 
    981                 wp_send_json( ['status' => $status] );
    982             }
    983 
    984             $activation_status = $this->activate_license( $license_item, $product_type );
    985             $status['success'] = $activation_status['success'];
    986 
    987             wp_send_json( ['status' => $status, 'activation_status' => $activation_status] );
    988         }
    989 
    990         // activate_license
    991         public function activate_license( $license_item, $product_type = '' ) {
    992             $status            = ['success' => true];
    993             $activation_status = self::remote_activate_license( $license_item );
    994 
    995             if ( empty( $activation_status['success'] ) ) {
    996                 $status['success'] = false;
    997             }
    998 
    999             $status['response'] = $activation_status['response'];
    1000             $product_type       = self::filter_product_type( $product_type );
    1001 
    1002             if ( $status['success'] && ( 'plugin' === $product_type || 'theme' === $product_type ) ) {
    1003                 $user_purchased = get_user_meta( get_current_user_id(), '_atbdp_purchased_products', true );
    1004 
    1005                 if ( empty( $user_purchased ) ) {
    1006                     $user_purchased = [];
    1007                 }
    1008 
    1009                 if ( empty( $user_purchased[$product_type] ) ) {
    1010                     $user_purchased[$product_type] = [];
    1011                 }
    1012 
    1013                 $purchased_items = $user_purchased[$product_type];
    1014 
    1015                 // Append new product
    1016                 $product_key                   = $this->get_product_key_from_permalink( $license_item['permalink'] );
    1017                 $purchased_items[$product_key] = $license_item;
    1018 
    1019                 $user_purchased[$product_type] = $purchased_items;
    1020                 update_user_meta( get_current_user_id(), '_atbdp_purchased_products', $user_purchased );
    1021 
    1022                 $status['purchased_products'] = $user_purchased;
    1023             }
    1024 
    1025             return $status;
    1026         }
    1027 
    1028         // handle_file_install_request_from_subscriptions
    1029         public function handle_file_install_request_from_subscriptions() {
    1030             $item_key = ( isset( $_POST['item_key'] ) ) ? $_POST['item_key'] : '';
    1031             $type     = ( isset( $_POST['type'] ) ) ? $_POST['type'] : '';
    1032 
    1033             if ( ! $this->is_verified_nonce() ) {
    1034                 $status            = [];
    1035                 $status['success'] = false;
    1036                 $status['message'] = 'Invalid request';
    1037 
    1038                 wp_send_json( ['status' => $status] );
    1039             }
    1040 
    1041             $installation_status = $this->install_file_from_subscriptions( ['item_key' => $item_key, 'type' => $type] );
    1042             wp_send_json( $installation_status );
    1043         }
    1044 
    1045         // install_file_from_subscriptions
    1046         public function install_file_from_subscriptions( array $args = [] ) {
    1047             $default = ['item_key' => '', 'type' => ''];
    1048             $args    = array_merge( $default, $args );
    1049 
    1050             $item_key = $args['item_key'];
    1051             $type     = $args['type'];
    1052 
    1053             $status = ['success' => true];
    1054 
    1055             if ( empty( $item_key ) ) {
    1056                 $status['success'] = false;
    1057                 $status['message'] = __( 'Item key is missing', 'directorist' );
    1058 
    1059                 return ['status' => $status];
    1060             }
    1061 
    1062             if ( empty( $type ) ) {
    1063                 $status['success'] = false;
    1064                 $status['message'] = __( 'Type not specified', 'directorist' );
    1065 
    1066                 return ['status' => $status];
    1067             }
    1068 
    1069             if ( 'plugin' !== $type && 'theme' !== $type ) {
    1070                 $status['success'] = false;
    1071                 $status['message'] = __( 'Invalid type', 'directorist' );
    1072 
    1073                 return ['status' => $status];
    1074             }
    1075 
    1076             if ( 'theme' === $type ) {
    1077                 $available_in_subscriptions = self::get_purchased_theme_list();
    1078             }
    1079 
    1080             if ( 'plugin' === $type ) {
    1081                 $available_in_subscriptions = self::get_purchased_extension_list();
    1082             }
    1083 
    1084             if ( empty( $available_in_subscriptions ) ) {
    1085                 $status['success'] = false;
    1086                 $status['message'] = __( 'Nothing available in subscriptions', 'directorist' );
    1087 
    1088                 return ['status' => $status];
    1089             }
    1090 
    1091             if ( empty( $available_in_subscriptions[$item_key] ) ) {
    1092                 $status['success'] = false;
    1093                 $status['message'] = __( 'The item is not available in your subscriptions', 'directorist' );
    1094 
    1095                 return ['status' => $status];
    1096             }
    1097 
    1098             $installing_file = $available_in_subscriptions[$item_key];
    1099 
    1100             $activatation_status = $this->activate_license( $installing_file, $type );
    1101             $status['log']       = $activatation_status;
    1102 
    1103             if ( ! $activatation_status['success'] ) {
    1104                 $status['success'] = false;
    1105                 $status['message'] = __( 'The license is not valid, please check you subscription.', 'directorist' );
    1106 
    1107                 return ['status' => $status];
    1108             }
    1109 
    1110             $link          = $installing_file['download_link'];
    1111             $download_args = ['url' => $link];
    1112 
    1113             if ( 'plugin' === $type ) {
    1114                 $download_status = $this->download_plugin( $download_args );
    1115             }
    1116 
    1117             if ( 'theme' === $type ) {
    1118                 $download_status = $this->download_theme( $download_args );
    1119             }
    1120 
    1121             if ( ! $download_status['success'] ) {
    1122                 return $download_status;
    1123             }
    1124 
    1125             $status['success'] = true;
    1126             $status['message'] = __( 'Installed Successfully', 'directorist' );
    1127 
    1128             return ['status' => $status];
    1129         }
    1130 
    1131         // handle_plugin_download_request
    1132         public function handle_file_download_request() {
    1133             $status        = ['success' => true];
    1134 
    1135             if ( ! $this->is_verified_nonce() ) {
    1136                 $status['success'] = false;
    1137                 $status['message'] = 'Invalid request';
    1138 
    1139                 wp_send_json( ['status' => $status] );
    1140             }
    1141 
    1142             $download_item = ( isset( $_POST['download_item'] ) ) ? $_POST['download_item'] : '';
    1143             $type          = ( isset( $_POST['type'] ) ) ? $_POST['type'] : '';
    1144 
    1145             if ( empty( $download_item ) ) {
    1146                 $status['success'] = false;
    1147                 $status['message'] = 'Download item is missing';
    1148 
    1149                 wp_send_json( ['status' => $status] );
    1150             }
    1151 
    1152             if ( empty( $type ) ) {
    1153                 $status['success'] = false;
    1154                 $status['message'] = 'Type not specified';
    1155 
    1156                 wp_send_json( ['status' => $status] );
    1157             }
    1158 
    1159             if ( 'plugin' !== $type && 'theme' !== $type ) {
    1160                 $status['success'] = false;
    1161                 $status['message'] = 'Invalid type';
    1162 
    1163                 wp_send_json( ['status' => $status] );
    1164             }
    1165 
    1166             $activate_license = $this->activate_license( $download_item, $type );
    1167 
    1168             if ( ! $activate_license['success'] ) {
    1169                 $status['success'] = false;
    1170                 $status['message'] = __( 'Activation failed', 'directorist' );
    1171                 $status['ref']     = $activate_license;
    1172 
    1173                 wp_send_json( ['status' => $status] );
    1174             }
    1175 
    1176             if ( empty( $download_item['download_link'] ) ) {
    1177                 $status['success'] = false;
    1178                 $status['message'] = 'Download Link not found';
    1179 
    1180                 wp_send_json( ['status' => $status] );
    1181             }
    1182 
    1183             if ( ! is_string( $download_item['download_link'] ) ) {
    1184                 $status['success'] = false;
    1185                 $status['message'] = 'Download Link not found';
    1186 
    1187                 wp_send_json( ['status' => $status] );
    1188             }
    1189 
    1190             $link          = $download_item['download_link'];
    1191             $download_args = ['url' => $link];
    1192 
    1193             if ( 'plugin' === $type ) {
    1194                 $download_status = $this->download_plugin( $download_args );
    1195             }
    1196 
    1197             if ( 'theme' === $type ) {
    1198                 $download_status = $this->download_theme( $download_args );
    1199             }
    1200 
    1201             if ( ! $download_status['success'] ) {
    1202                 return $download_status;
    1203             }
    1204 
    1205             $status['success'] = true;
    1206             $status['message'] = __( 'Donloaded', 'directorist' );
    1207 
    1208             wp_send_json( ['status' => $status] );
    1209         }
    1210 
    1211         // download_plugin
    1212         public function download_plugin( array $args = [] ) {
    1213             $status = ['success' => false];
    1214 
    1215             $default = ['url' => '', 'init_wp_filesystem' => true];
    1216             $args    = array_merge( $default, $args );
    1217 
    1218             if ( empty( $default ) ) {
    1219                 return $status;
    1220             }
    1221 
    1222             if ( empty( $args['url'] ) ) {
    1223                 $status['success'] = false;
    1224                 $status['message'] = __( 'Download link not found', 'directorist' );
    1225 
    1226                 return $status;
    1227             }
    1228 
    1229             global $wp_filesystem;
    1230 
    1231             if ( $args['init_wp_filesystem'] ) {
    1232 
    1233                 if ( ! function_exists( 'WP_Filesystem' ) ) {
    1234                     include ABSPATH . 'wp-admin/includes/file.php';
    1235                 }
    1236 
    1237                 WP_Filesystem();
    1238             }
    1239 
    1240             $plugin_path = ABSPATH . 'wp-content/plugins';
    1241             $temp_dest   = "{$plugin_path}/atbdp-temp-dir";
    1242             $file_url    = $args['url'];
    1243             $file_name   = basename( $file_url );
    1244             $tmp_file    = download_url( $file_url );
    1245 
    1246             if ( ! is_string( $tmp_file ) ) {
    1247                 $status['success']  = false;
    1248                 $status['tmp_file'] = $tmp_file;
    1249                 $status['file_url'] = $file_url;
    1250                 $status['message']  = 'Could not download the file';
    1251 
    1252                 return $status;
    1253             }
    1254 
    1255             // Make Temp Dir
    1256             if ( $wp_filesystem->exists( $temp_dest ) ) {
    1257                 $wp_filesystem->delete( $temp_dest, true );
    1258             }
    1259 
    1260             $wp_filesystem->mkdir( $temp_dest );
    1261 
    1262             if ( ! file_exists( $temp_dest ) ) {
    1263                 $status['success'] = false;
    1264                 $status['message'] = __( 'Could not create temp directory', 'directorist' );
    1265 
    1266                 return $status;
    1267             }
    1268 
    1269             // Sets file temp destination.
    1270             $file_path = "{$temp_dest}/{$file_name}";
    1271 
    1272             set_error_handler( function ( $errno, $errstr, $errfile, $errline ) {
    1273                 // error was suppressed with the @-operator
    1274                 if ( 0 === error_reporting() ) {
    1275                     return false;
    1276                 }
    1277 
    1278                 throw new ErrorException( $errstr, 0, $errno, $errfile, $errline );
    1279             } );
    1280 
    1281             // Copies the file to the final destination and deletes temporary file.
    1282             try {
    1283                 copy( $tmp_file, $file_path );
    1284             } catch ( Exception $e ) {
    1285                 $status['success'] = false;
    1286                 $status['message'] = $e->getMessage();
    1287 
    1288                 return $status;
    1289             }
    1290 
    1291             @unlink( $tmp_file );
    1292             unzip_file( $file_path, $temp_dest );
    1293 
    1294             if ( "{$plugin_path}/" !== $file_path || $file_path !== $plugin_path ) {
    1295                 @unlink( $file_path );
    1296             }
    1297 
    1298             $extracted_file_dir = glob( "{$temp_dest}/*", GLOB_ONLYDIR );
    1299 
    1300             foreach ( $extracted_file_dir as $dir_path ) {
    1301                 $dir_name  = basename( $dir_path );
    1302                 $dest_path = "{$plugin_path}/{$dir_name}";
    1303 
    1304                 // Delete Previous Files if Exists
    1305                 if ( $wp_filesystem->exists( $dest_path ) ) {
    1306                     $wp_filesystem->delete( $dest_path, true );
    1307                 }
    1308 
    1309             }
    1310 
    1311             copy_dir( $temp_dest, $plugin_path );
    1312             $wp_filesystem->delete( $temp_dest, true );
    1313 
    1314             $status['success'] = true;
    1315             $status['message'] = __( 'The plugin has been downloaded successfully', 'directorist' );
    1316 
    1317             return $status;
    1318         }
    1319 
    1320         // download_theme
    1321         public function download_theme( array $args = [] ) {
    1322             $status = ['success' => false];
    1323 
    1324             $default = ['url' => '', 'init_wp_filesystem' => true];
    1325             $args    = array_merge( $default, $args );
    1326 
    1327             if ( empty( $default ) ) {
    1328                 return $status;
    1329             }
    1330 
    1331             if ( empty( $args['url'] ) ) {
    1332                 $status['success'] = false;
    1333                 $status['message'] = __( 'Download link not found', 'directorist' );
    1334 
    1335                 return $status;
    1336             }
    1337 
    1338             global $wp_filesystem;
    1339 
    1340             if ( $args['init_wp_filesystem'] ) {
    1341 
    1342                 if ( ! function_exists( 'WP_Filesystem' ) ) {
    1343                     include ABSPATH . 'wp-admin/includes/file.php';
    1344                 }
    1345 
    1346                 WP_Filesystem();
    1347             }
    1348 
    1349             $theme_path = ABSPATH . 'wp-content/themes';
    1350             $temp_dest  = "{$theme_path}/atbdp-temp-dir";
    1351             $file_url   = $args['url'];
    1352             $file_name  = basename( $file_url );
    1353             $tmp_file   = download_url( $file_url );
    1354 
    1355             if ( ! is_string( $tmp_file ) ) {
    1356                 $status['success']  = false;
    1357                 $status['tmp_file'] = $tmp_file;
    1358                 $status['file_url'] = $file_url;
    1359                 $status['message']  = 'Could not download the file';
    1360 
    1361                 return $status;
    1362             }
    1363 
    1364             // Make Temp Dir
    1365             if ( $wp_filesystem->exists( $temp_dest ) ) {
    1366                 $wp_filesystem->delete( $temp_dest, true );
    1367             }
    1368 
    1369             $wp_filesystem->mkdir( $temp_dest );
    1370 
    1371             if ( ! file_exists( $temp_dest ) ) {
    1372                 $status['success'] = false;
    1373                 $status['message'] = __( 'Could not create temp directory', 'directorist' );
    1374 
    1375                 return $status;
    1376             }
    1377 
    1378             // Sets file temp destination.
    1379             $file_path = "{$temp_dest}/{$file_name}";
    1380 
    1381             set_error_handler( function ( $errno, $errstr, $errfile, $errline ) {
    1382                 // error was suppressed with the @-operator
    1383                 if ( 0 === error_reporting() ) {
    1384                     return false;
    1385                 }
    1386 
    1387                 throw new ErrorException( $errstr, 0, $errno, $errfile, $errline );
    1388             } );
    1389 
    1390             // Copies the file to the final destination and deletes temporary file.
    1391             try {
    1392                 copy( $tmp_file, $file_path );
    1393             } catch ( Exception $e ) {
    1394                 $status['success'] = false;
    1395                 $status['message'] = $e->getMessage();
    1396 
    1397                 return $status;
    1398             }
    1399 
    1400             @unlink( $tmp_file );
    1401             unzip_file( $file_path, $temp_dest );
    1402 
    1403             if ( "{$theme_path}/" !== $file_path || $file_path !== $theme_path ) {
    1404                 @unlink( $file_path );
    1405             }
    1406 
    1407             $extracted_file_dir = glob( "{$temp_dest}/*", GLOB_ONLYDIR );
    1408             $dir_path           = $extracted_file_dir[0];
    1409 
    1410             $dir_name  = basename( $dir_path );
    1411             $dest_path = "{$theme_path}/{$dir_name}";
    1412             $zip_files = glob( "{$dir_path}/*.zip" );
    1413 
    1414             // If has child theme
    1415             if ( ! empty( $zip_files ) ) {
    1416                 $new_temp_dest = "{$temp_dest}/_temp_dest";
    1417                 $this->install_themes_from_zip_files( $zip_files, $new_temp_dest, $wp_filesystem );
    1418 
    1419                 copy_dir( $new_temp_dest, $theme_path );
    1420                 $wp_filesystem->delete( $temp_dest, true );
    1421 
    1422                 $status['success'] = false;
    1423                 $status['message'] = __( 'The theme has been downloaded successfully', 'directorist' );
    1424             }
    1425 
    1426             // Delete Previous Files If Exists
    1427             if ( $wp_filesystem->exists( $dest_path ) ) {
    1428                 $wp_filesystem->delete( $dest_path, true );
    1429             }
    1430 
    1431             copy_dir( $temp_dest, $theme_path );
    1432             $wp_filesystem->delete( $temp_dest, true );
    1433 
    1434             $status['success'] = true;
    1435             $status['message'] = __( 'The theme has been downloaded successfully', 'directorist' );
    1436 
    1437             return $status;
    1438         }
    1439 
    1440         // install_theme_from_zip
    1441         public function install_themes_from_zip_files( $zip_files, $temp_dest, $wp_filesystem ) {
    1442             $theme_path = ABSPATH . 'wp-content/themes';
    1443 
    1444             foreach ( $zip_files as $zip ) {
    1445                 $file     = basename( $zip );
    1446                 $dir_name = str_replace( '.zip', '', $file );
    1447 
    1448                 if ( preg_match( '/[-]child[.]zip$/', $file ) ) {
    1449                     $temp_dest_path = "{$temp_dest}/{$dir_name}";
    1450                     $main_dest_path = "{$theme_path}/{$dir_name}";
    1451 
    1452                     // Skip if has child
    1453                     if ( $wp_filesystem->exists( $main_dest_path ) ) {
    1454                         continue;
    1455                     }
    1456 
    1457                     $wp_filesystem->mkdir( $temp_dest_path );
    1458                     unzip_file( $zip, $temp_dest_path );
    1459                     // @unlink( $zip );
    1460 
    1461                     continue;
    1462                 }
    1463 
    1464                 $main_dest_path = "{$theme_path}/{$dir_name}";
    1465 
    1466                 if ( $wp_filesystem->exists( $main_dest_path ) ) {
    1467                     $wp_filesystem->delete( $main_dest_path, true );
    1468                 }
    1469 
    1470                 unzip_file( $zip, $temp_dest );
    1471                 // @unlink( $zip );
    1472             }
    1473 
    1474         }
    1475 
    1476         // get_customers_purchased
    1477         public function get_customers_purchased( $license_data ) {
    1478             // Activate the licenses
    1479             $activation_url = 'https://directorist.com';
    1480 
    1481             // Activate the Extensions
    1482             $purchased_extensions_meta    = [];
    1483             $purchased_extensions         = [];
    1484             $invalid_purchased_extensions = [];
    1485 
    1486             if ( ! empty( $license_data['plugins'] ) ) {
    1487 
    1488                 foreach ( $license_data['plugins'] as $extension ) {
    1489                     $license              = ( ! empty( $response_body['all_access'] ) ) ? $response_body['active_licenses'][0] : $extension['license'];
    1490                     $extension['license'] = $license;
    1491 
    1492                     $activation_status = self::remote_activate_license( $extension, 'plugin' );
    1493 
    1494                     if ( empty( $activation_status['success'] ) ) {
    1495                         $invalid_purchased_extensions[] = ['extension' => $extension, 'response' => $activation_status['response']];
    1496                         continue;
    1497                     }
    1498 
    1499                     $purchased_extensions[] = $extension;
    1500 
    1501                     // Store the ref for db
    1502                     $link    = $extension['permalink'];
    1503                     $ext_key = str_replace( 'http://directorist.com/product/', '', $link );
    1504                     $ext_key = str_replace( 'https://directorist.com/product/', '', $ext_key );
    1505                     $ext_key = str_replace( '/', '', $ext_key );
    1506 
    1507                     $purchased_extensions_meta[$ext_key] = [
    1508                         'item_id' => $extension['item_id'],
    1509                         'license' => $extension['license'],
    1510                         'license' => $extension['license'],
    1511                         'file'    => $extension['links'],
    1512                     ];
    1513                 }
    1514 
    1515             }
    1516 
    1517             // Activate the Themes
    1518             $purchased_themes_meta    = [];
    1519             $purchased_themes         = [];
    1520             $invalid_purchased_themes = [];
    1521 
    1522             if ( ! empty( $license_data['themes'] ) ) {
    1523 
    1524                 foreach ( $license_data['themes'] as $theme ) {
    1525                     $license          = ( ! empty( $response_body['all_access'] ) ) ? $response_body['active_licenses'][0] : $theme['license'];
    1526                     $theme['license'] = $license;
    1527 
    1528                     $activation_status = self::remote_activate_license( $theme );
    1529 
    1530                     if ( empty( $activation_status['success'] ) ) {
    1531                         $invalid_purchased_themes[] = $theme;
    1532                         $invalid_purchased_themes[] = ['extension' => $theme, 'response' => $activation_status['response']];
    1533                         continue;
    1534                     }
    1535 
    1536                     $purchased_themes[] = $theme;
    1537 
    1538                     // Store the ref for db
    1539                     $link      = $theme['permalink'];
    1540                     $theme_key = str_replace( 'http://directorist.com/product/', '', $link );
    1541                     $theme_key = str_replace( 'https://directorist.com/product/', '', $theme_key );
    1542                     $theme_key = str_replace( '/', '', $theme_key );
    1543 
    1544                     $purchased_themes_meta[$theme_key] = [
    1545                         'item_id' => $extension['item_id'],
    1546                         'license' => $extension['license'],
    1547                         'file'    => $extension['links'],
    1548                     ];
    1549                 }
    1550 
    1551             }
    1552 
    1553             $customers_purchased = [
    1554                 'extensions' => $purchased_extensions_meta,
    1555                 'themes'     => $purchased_themes_meta,
    1556             ];
    1557 
    1558             update_user_meta( get_current_user_id(), '_atbdp_purchased_products', $customers_purchased );
    1559 
    1560             $status['purchased_extensions']         = $purchased_extensions;
    1561             $status['invalid_purchased_extensions'] = $invalid_purchased_extensions;
    1562 
    1563             $status['purchased_themes']         = $purchased_themes;
    1564             $status['invalid_purchased_themes'] = $invalid_purchased_themes;
    1565 
    1566             $status['customers_purchased'] = $customers_purchased;
    1567 
    1568             return $status;
    1569         }
    1570 
    1571         // download_purchased_items
    1572         public function download_purchased_items() {
    1573             $status = ['success' => true, 'log' => []];
    1574 
    1575             $cart = ( isset( $_POST['customers_purchased'] ) ) ? $_POST['customers_purchased'] : '';
    1576 
    1577             if ( empty( $cart ) ) {
    1578                 $status['success']                        = false;
    1579                 $status['log']['no_purchased_data_found'] = [
    1580                     'type'    => 'error',
    1581                     'message' => 'No purchased data found',
    1582                 ];
    1583                 wp_send_json( ['status' => $status] );
    1584             }
    1585 
    1586             // Download the extensions
    1587             if ( ! function_exists( 'WP_Filesystem' ) ) {
    1588                 include ABSPATH . 'wp-admin/includes/file.php';
    1589             }
    1590 
    1591             WP_Filesystem();
    1592 
    1593             // Download Extenstions
    1594             if ( ! empty( $cart['purchased_extensions'] ) ) {
    1595                 foreach ( $cart['purchased_extensions'] as $extension ) {
    1596                     $download_link = $extension['download_link'];
    1597                     if ( empty( $download_link ) ) {
    1598                         continue;
    1599                     }
    1600 
    1601                     $this->download_plugin( ['url' => $download_link, 'init_wp_filesystem' => false] );
    1602                 }
    1603 
    1604             }
    1605 
    1606             // Download Themes
    1607             if ( ! empty( $cart['purchased_themes'] ) ) {
    1608                 foreach ( $cart['purchased_themes'] as $theme ) {
    1609                     $download_link = $extension['download_link'];
    1610                     if ( empty( $download_link ) ) {
    1611                         continue;
    1612                     }
    1613 
    1614                     $this->download_theme( ['url' => $download_link, 'init_wp_filesystem' => false] );
    1615                 }
    1616 
    1617             }
    1618 
    1619             $status['message'] = 'Download has been completed, redirecting...';
    1620 
    1621             wp_send_json( ['status' => $status] );
    1622         }
    1623 
    1624         /**
    1625          * It Adds menu item
    1626          */
    1627         public function admin_menu() {
    1628             add_submenu_page(
    1629                 'edit.php?post_type=at_biz_dir',
    1630                 __( 'Get Extensions', 'directorist' ),
    1631                 __( '<span>Themes & Extensions</span>', 'directorist' ),
    1632                 'manage_options',
    1633                 'atbdp-extension',
    1634                 [$this, 'show_extension_view']
    1635             );
    1636         }
    1637 
    1638         // get_extensions_overview
    1639         public function get_extensions_overview() {
    1640             // Get Extensions Details
    1641             $plugin_updates       = get_site_transient( 'update_plugins' );
    1642             $outdated_plugins     = $plugin_updates->response;
    1643             $outdated_plugins_key = ( is_array( $outdated_plugins ) ) ? array_keys( $outdated_plugins ) : [];
    1644             $official_extensions  = is_array( $this->extensions ) ? array_keys( $this->extensions ) : [];
    1645 
    1646             $all_installed_plugins_list = get_plugins();
    1647             $installed_extensions       = [];
    1648             $total_active_extensions    = 0;
    1649             $total_outdated_extensions  = 0;
    1650          
    1651             foreach ( $all_installed_plugins_list as $plugin_base => $plugin_data ) {
    1652 
    1653                 $folder_base = strtok( $plugin_base, '/' );
    1654 
    1655                 if ( preg_match( '/^directorist-/', $plugin_base ) && in_array( $folder_base, $official_extensions ) ) {
    1656                     $installed_extensions[$plugin_base] = $plugin_data;
    1657 
    1658                     if ( is_plugin_active( $plugin_base ) ) {
    1659                         $total_active_extensions++;
    1660                     }
    1661 
    1662                     if ( in_array( $plugin_base, $outdated_plugins_key ) ) {
    1663                         $total_outdated_extensions++;
    1664                     }
    1665 
    1666                 }
    1667 
    1668             }
    1669 
    1670             // ---
    1671             $extensions_available_in_subscriptions = $this->get_extensions_available_in_subscriptions( [
    1672                 'installed_extensions' => $installed_extensions,
    1673             ] );
    1674 
    1675             // ---
    1676             $extensions_promo_list = $this->get_extensions_promo_list( [
    1677                 'extensions_available_in_subscriptions' => $extensions_available_in_subscriptions,
    1678                 'installed_extensions'                  => $installed_extensions,
    1679             ] );
    1680 
    1681             $required_extensions_list = $this->prepare_the_final_requred_extension_list( [
    1682                 'installed_extension_list'              => $installed_extensions,
    1683                 'extensions_available_in_subscriptions' => $extensions_available_in_subscriptions,
    1684             ] );
    1685 
    1686             $total_installed_ext_list             = count( $installed_extensions );
    1687             $total_ext_available_in_subscriptions = count( $extensions_available_in_subscriptions );
    1688             $total_available_extensions           = $total_installed_ext_list + $total_ext_available_in_subscriptions;
    1689 
    1690             $overview = [
    1691                 'outdated_plugin_list'                  => $outdated_plugins,
    1692                 'outdated_plugins_key'                  => $outdated_plugins_key,
    1693                 'all_installed_plugins_list'            => $all_installed_plugins_list,
    1694                 'installed_extension_list'              => $installed_extensions,
    1695                 'total_active_extensions'               => $total_active_extensions,
    1696                 'total_outdated_extensions'             => $total_outdated_extensions,
    1697                 'extensions_promo_list'                 => $extensions_promo_list,
    1698                 'extensions_available_in_subscriptions' => $extensions_available_in_subscriptions,
    1699                 'total_available_extensions'            => $total_available_extensions,
    1700                 'required_extensions'                   => $required_extensions_list,
    1701             ];
    1702 
    1703             return $overview;
    1704         }
    1705 
    1706         // get_extensions_available_in_subscriptions
    1707         public function get_extensions_available_in_subscriptions( array $args = [] ) {
    1708             $installed_extensions      = ( ! empty( $args['installed_extensions'] ) ) ? $args['installed_extensions'] : [];
    1709             $installed_extensions_keys = $this->get_sanitized_extensions_keys( $installed_extensions );
    1710 
    1711             $extensions_available_in_subscriptions = self::get_purchased_extension_list();
    1712             $extensions_available_in_subscriptions = ( is_array( $extensions_available_in_subscriptions ) ) ? $extensions_available_in_subscriptions : [];
    1713 
    1714             if ( ! empty( $extensions_available_in_subscriptions ) && is_array( $extensions_available_in_subscriptions ) ) {
    1715 
    1716                 foreach ( $extensions_available_in_subscriptions as $base => $args ) {
    1717                     $base_alias       = $this->get_extension_alias_key( $base );
    1718                     $plugin_key       = preg_replace( '/(directorist-)/', '', $base );
    1719                     $plugin_alias_key = preg_replace( '/(directorist-)/', '', $base_alias );
    1720 
    1721                     $is_in_installed_extensions       = in_array( $plugin_key, $installed_extensions_keys ) ? true : false;
    1722                     $is_in_installed_extensions_alias = in_array( $plugin_alias_key, $installed_extensions_keys ) ? true : false;
    1723 
    1724                     if ( $is_in_installed_extensions || $is_in_installed_extensions_alias ) {
    1725                         unset( $extensions_available_in_subscriptions[$base] );
    1726                     }
    1727 
    1728                 }
    1729 
    1730             }
    1731 
    1732             return $extensions_available_in_subscriptions;
    1733         }
    1734 
    1735         // get_extensions_promo_list
    1736         public function get_extensions_promo_list( array $args = [] ) {
    1737             $installed_extensions      = ( ! empty( $args['installed_extensions'] ) ) ? $args['installed_extensions'] : [];
    1738             $installed_extensions_keys = $this->get_sanitized_extensions_keys( $installed_extensions );
    1739 
    1740             $extensions_available_in_subscriptions      = ( ! empty( $args['extensions_available_in_subscriptions'] ) ) ? $args['extensions_available_in_subscriptions'] : [];
    1741             $extensions_available_in_subscriptions_keys = is_array( $extensions_available_in_subscriptions ) ? array_keys( $extensions_available_in_subscriptions ) : [];
    1742 
    1743             // Filter extensions available in subscriptions
    1744             $promo_extensions = $this->get_active_extensions();
    1745 
    1746             if ( ! empty( $promo_extensions ) && is_array( $installed_extensions_keys ) ) {
    1747 
    1748                 foreach ( $promo_extensions as $_extension_base => $_extension_args ) {
    1749                     $extension_base_alias = $this->get_extension_alias_key( $_extension_base );
    1750                     $ext_key              = preg_replace( '/(directorist-)/', '', $_extension_base );
    1751                     $ext_alias_key        = preg_replace( '/(directorist-)/', '', $extension_base_alias );
    1752 
    1753                     // Exclude Installed Extensions
    1754                     $in_installed_extensions       = in_array( $ext_key, $installed_extensions_keys ) ? true : false;
    1755                     $in_installed_extensions_alias = in_array( $ext_alias_key, $installed_extensions_keys ) ? true : false;
    1756 
    1757                     if ( $in_installed_extensions || $in_installed_extensions_alias ) {
    1758                         unset( $promo_extensions[$_extension_base] );
    1759                     }
    1760 
    1761                     // Exclude Subscripted Extensions
    1762                     $is_available_in_subscriptions       = in_array( $_extension_base, $extensions_available_in_subscriptions_keys ) ? true : false;
    1763                     $is_available_in_subscriptions_alias = in_array( $extension_base_alias, $extensions_available_in_subscriptions_keys ) ? true : false;
    1764 
    1765                     if ( $is_available_in_subscriptions || $is_available_in_subscriptions_alias ) {
    1766                         unset( $promo_extensions[$_extension_base] );
    1767                     }
    1768 
    1769                 }
    1770 
    1771             }
    1772 
    1773             return $promo_extensions;
    1774         }
    1775 
    1776         // get_sanitized_extensions_keys
    1777         public function get_sanitized_extensions_keys( array $extensions_list = [] ) {
    1778             $extensions_keys = ( is_array( $extensions_list ) ) ? array_keys( $extensions_list ) : [];
    1779 
    1780             if ( ! empty( $extensions_keys ) && is_array( $extensions_keys ) ) {
    1781 
    1782                 foreach ( $extensions_keys as $index => $key ) {
    1783                     $new_key = preg_replace( '/\/.+/', '', $key );
    1784                     $new_key = preg_replace( '/(directorist-)/', '', $new_key );
    1785 
    1786                     $extensions_keys[$index] = $new_key;
    1787                 }
    1788 
    1789             }
    1790 
    1791             return $extensions_keys;
    1792         }
    1793 
    1794         // get_themes_overview
    1795         public function get_themes_overview() {
    1796             // Check form theme update
    1797             $current_theme = wp_get_theme();
    1798             get_theme_update_available( $current_theme->stylesheet );
    1799 
    1800             $sovware_themes       = ( is_array( $this->themes ) ) ? array_keys( $this->themes ) : [];
    1801             $theme_updates        = get_site_transient( 'update_themes' );
    1802             $outdated_themes      = $theme_updates->response;
    1803             $outdated_themes_keys = ( is_array( $outdated_themes ) ) ? array_keys( $outdated_themes ) : [];
    1804 
    1805             $all_themes            = wp_get_themes();
    1806             $active_theme_slug     = get_option( 'stylesheet' );
    1807             $installed_theme_list  = [];
    1808             $total_active_themes   = 0;
    1809             $total_outdated_themes = 0;
    1810 
    1811             foreach ( $all_themes as $theme_base => $theme_data ) {
    1812 
    1813                 if ( in_array( $theme_base, $sovware_themes ) ) {
    1814                     $customizer_link = "customize.php?theme={$theme_data->stylesheet}&return=%2Fwp-admin%2Fthemes.php";
    1815                     $customizer_link = admin_url( $customizer_link );
    1816 
    1817                     $installed_theme_list[$theme_base] = [
    1818                         'name'            => $theme_data->name,
    1819                         'version'         => $theme_data->version,
    1820                         'thumbnail'       => $theme_data->get_screenshot(),
    1821                         'customizer_link' => $customizer_link,
    1822                         'has_update'      => ( in_array( $theme_data->stylesheet, $outdated_themes_keys ) ) ? true : false,
    1823                         'stylesheet'      => $theme_data->stylesheet,
    1824                     ];
    1825 
    1826                     if ( $active_theme_slug === $theme_base ) {
    1827                         $total_active_themes++;
    1828                     }
    1829 
    1830                     if ( in_array( $theme_base, $outdated_themes_keys ) ) {
    1831                         $total_outdated_themes++;
    1832                     }
    1833 
    1834                 }
    1835 
    1836             }
    1837 
    1838             $installed_themes_keys = ( is_array( $installed_theme_list ) ) ? array_keys( $installed_theme_list ) : [];
    1839 
    1840             // Themes available in subscriptions
    1841             $themes_available_in_subscriptions = self::get_purchased_theme_list();
    1842             $themes_available_in_subscriptions = ( ! empty( $themes_available_in_subscriptions ) && is_array( $themes_available_in_subscriptions ) ) ? $themes_available_in_subscriptions : [];
    1843 
    1844             if ( ! empty( $themes_available_in_subscriptions ) ) {
    1845 
    1846                 foreach ( $themes_available_in_subscriptions as $base => $args ) {
    1847                     $item = $themes_available_in_subscriptions[$base];
    1848 
    1849                     // Merge Local Theme Info
    1850                     if ( ! empty( $this->themes[$base] ) ) {
    1851                         $item = array_merge( $this->themes[$base], $item );
    1852                     }
    1853 
    1854                     // Merge Local Theme Info
    1855                     if ( in_array( $base, $installed_themes_keys ) ) {
    1856                         $item = array_merge( $installed_theme_list[$base], $item );
    1857                     }
    1858 
    1859                     $is_installed         = ( in_array( $base, $installed_themes_keys ) ) ? true : false;
    1860                     $item['is_installed'] = $is_installed;
    1861 
    1862                     $themes_available_in_subscriptions[$base] = $item;
    1863                 }
    1864 
    1865             }
    1866 
    1867             // total_available_themes
    1868             $total_available_themes = count( $themes_available_in_subscriptions );
    1869 
    1870             // themes_promo_list
    1871             $themes_promo_list = $this->get_themes_promo_list( [
    1872                 'installed_theme_list'              => $installed_theme_list,
    1873                 'themes_available_in_subscriptions' => $themes_available_in_subscriptions,
    1874             ] );
    1875 
    1876             // current_active_theme_info
    1877             $current_active_theme_info = $this->get_current_active_theme_info(
    1878                 [
    1879                     'outdated_themes_keys' => $outdated_themes_keys,
    1880                     'installed_theme_list' => $installed_theme_list,
    1881                 ]
    1882             );           
    1883             $current_active_theme_info['stylesheet'];
    1884 
    1885             $themes_available_in_subscriptions_keys = array_keys( $themes_available_in_subscriptions );
    1886 
    1887             if ( in_array( $current_active_theme_info['stylesheet'], $themes_available_in_subscriptions_keys ) ) {
    1888                 unset( $themes_available_in_subscriptions[$current_active_theme_info['stylesheet']] );
    1889             }
    1890 
    1891             $overview = [
    1892                 'total_active_themes'               => $total_active_themes,
    1893                 'total_outdated_themes'             => $total_outdated_themes,
    1894                 'installed_theme_list'              => $installed_theme_list,
    1895                 'current_active_theme_info'         => $current_active_theme_info,
    1896                 'themes_promo_list'                 => $themes_promo_list,
    1897                 'themes_available_in_subscriptions' => $themes_available_in_subscriptions,
    1898                 'total_available_themes'            => $total_available_themes,
    1899             ];
    1900 
    1901             return $overview;
    1902         }
    1903 
    1904         // get_current_active_theme_info
    1905         public function get_current_active_theme_info( array $args = [] ) {
    1906             // Get Current Active Theme Info
    1907             $current_active_theme = wp_get_theme();
    1908             $customizer_link      = "customize.php?theme={$current_active_theme->stylesheet}&return=%2Fwp-admin%2Fthemes.php";
    1909             $customizer_link      = admin_url( $customizer_link );
    1910 
    1911             // Check form theme update
    1912             $has_update = isset( $args[ 'installed_theme_list' ][ $current_active_theme->stylesheet ] ) ? $args[ 'installed_theme_list' ][ $current_active_theme->stylesheet ][ 'has_update' ] : '';
    1913            
    1914             $active_theme_info = [
    1915                 'name'            => $current_active_theme->name,
    1916                 'version'         => $current_active_theme->version,
    1917                 'thumbnail'       => $current_active_theme->get_screenshot(),
    1918                 'customizer_link' => $customizer_link,
    1919                 'has_update'      => $has_update,
    1920                 'stylesheet'      => $current_active_theme->stylesheet,
    1921             ];
    1922 
    1923             return $active_theme_info;
    1924         }
    1925 
    1926         // get_themes_promo_list
    1927         public function get_themes_promo_list( array $args = [] ) {
    1928             $installed_theme_list  = ( ! empty( $args['installed_theme_list'] ) ) ? $args['installed_theme_list'] : [];
    1929             $installed_themes_keys = $this->get_sanitized_themes_keys( $installed_theme_list );
    1930 
    1931             $themes_available_in_subscriptions      = ( ! empty( $args['themes_available_in_subscriptions'] ) ) ? $args['themes_available_in_subscriptions'] : [];
    1932             $themes_available_in_subscriptions_keys = is_array( $themes_available_in_subscriptions ) ? array_keys( $themes_available_in_subscriptions ) : [];
    1933 
    1934             // Filter all active themes
    1935             $themes_promo_list = $this->get_active_themes();
    1936 
    1937             if ( ! empty( $themes_promo_list ) ) {
    1938 
    1939                 foreach ( $themes_promo_list as $_theme_base => $_extension_args ) {
    1940 
    1941                     // Exclude Installed Themes
    1942                     if ( in_array( $_theme_base, $installed_themes_keys ) ) {
    1943                         unset( $themes_promo_list[$_theme_base] );
    1944                     }
    1945 
    1946                     // Exclude Subscripted Themes
    1947                     if ( in_array( $_theme_base, $themes_available_in_subscriptions_keys ) ) {
    1948                         unset( $themes_promo_list[$_theme_base] );
    1949                     }
    1950 
    1951                 }
    1952 
    1953             }
    1954 
    1955             return $themes_promo_list;
    1956         }
    1957 
    1958         // get_sanitized_themes_keys
    1959         public function get_sanitized_themes_keys( array $theme_list = [] ) {
    1960             $theme_keys = ( is_array( $theme_list ) ) ? array_keys( $theme_list ) : [];
    1961 
    1962             return $theme_keys;
    1963         }
    1964 
    1965         // remote_activate_license
    1966         public static function remote_activate_license( $license_item = [] ) {
    1967             $status = ['success' => false];
    1968 
    1969             if ( ! is_array( $license_item ) ) {
    1970                 $status['message'] = __( 'Nothing to activate', 'directorist' );
    1971 
    1972                 return $status;
    1973             }
    1974 
    1975             if ( isset( $license_item['skip_licencing'] ) && ! empty( $license_item['skip_licencing'] ) ) {
    1976                 $status['success'] = true;
    1977 
    1978                 return $status;
    1979             }
    1980 
    1981             $item_id = ( ! empty( $license_item['item_id'] ) ) ? $license_item['item_id'] : 0;
    1982             $license = ( ! empty( $license_item['license'] ) ) ? $license_item['license'] : '';
    1983 
    1984             $activation_url = 'https://directorist.com';
    1985             $query_args     = [
    1986                 'edd_action' => 'activate_license',
    1987                 'url'        => home_url(),
    1988                 'item_id'    => $item_id,
    1989                 'license'    => $license,
    1990             ];
    1991 
    1992             try {
    1993                 $response = wp_remote_get( $activation_url, [
    1994                     'timeout'   => 15,
    1995                     'sslverify' => false,
    1996                     'body'      => $query_args,
    1997                 ] );
    1998 
    1999                 $response_status = json_decode( $response['body'], true );
    2000             } catch ( Exception $e ) {
    2001                 $status['success']  = false;
    2002                 $status['message']  = $e->getMessage();
    2003                 $status['response'] = null;
    2004 
    2005                 return $status;
    2006             }
    2007 
    2008             $status['response'] = $response_status;
    2009 
    2010             if ( empty( $response_status['success'] ) ) {
    2011                 $status['success'] = false;
    2012                 $status['message'] = __( 'Activation failed', 'directorist' );
    2013 
    2014                 return $status;
    2015             }
    2016 
    2017             $status['success'] = true;
    2018 
    2019             return $status;
    2020         }
    2021 
    2022         // remote_authenticate_user
    2023         public static function remote_authenticate_user( $user_credentials = [] ) {
    2024             $status = ['success' => true];
    2025 
    2026             $url     = 'https://directorist.com/wp-json/directorist/v1/licencing';
    2027             $headers = [
    2028                 'user-agent' => 'Directorist/' . md5( esc_url( home_url() ) ) . ';',
    2029                 'Accept'     => 'application/json',
    2030             ];
    2031 
    2032             $config = [
    2033                 'method'      => 'GET',
    2034                 'timeout'     => 30,
    2035                 'redirection' => 5,
    2036                 'httpversion' => '1.0',
    2037                 'headers'     => $headers,
    2038                 'cookies'     => [],
    2039                 'body'        => $user_credentials, // [ 'user' => '', 'password' => '']
    2040             ];
    2041 
    2042             $response_body = [];
    2043 
    2044             try {
    2045                 $response = wp_remote_get( $url, $config );
    2046 
    2047                 if ( is_wp_error( $response ) ) {
    2048                     $status['success'] = false;
    2049                     $status['message'] = Directorist\Helper::get_first_wp_error_message( $response );
    2050                 } else {
    2051                     $response_body = ( 'string' === gettype( $response['body'] ) ) ? json_decode( $response['body'], true ) : $response['body'];
    2052                 }
    2053                
    2054             } catch ( Exception $e ) {
    2055                 $status['success'] = false;
    2056                 $status['message'] = $e->getMessage();
    2057             }
    2058 
    2059             if ( is_array( $response_body ) ) {
    2060                 $status = array_merge(  $status, $response_body );
    2061             }
    2062            
    2063             if ( empty( $response_body['success'] ) ) {
    2064                 $status['success'] = false;
    2065             }
    2066 
    2067             $status['response'] = $response_body;
    2068 
    2069             return $status;
    2070         }
    2071 
    2072         // get_file_download_link
    2073         public static function get_file_download_link( $file_item = [], $product_type = 'plugin' ) {
    2074             if ( ! is_array( $file_item ) ) {
    2075                 return '';
    2076             }
    2077 
    2078             if ( ! isset( $file_item['item_id'] ) ) {
    2079                 return '';
    2080             }
    2081 
    2082             if ( ! isset( $file_item['license'] ) ) {
    2083                 return '';
    2084             }
    2085 
    2086             if ( empty( $file_item['item_id'] ) || empty( $file_item['license'] ) ) {
    2087                 return '';
    2088             }
    2089 
    2090             $activation_url = 'https://directorist.com/wp-json/directorist/v1/get-product-data/';
    2091             $query_args     = [
    2092                 'product_type' => $product_type,
    2093                 'license'      => $file_item['license'],
    2094                 'item_id'      => $file_item['item_id'],
    2095                 'get_info'     => 'download_link',
    2096             ];
    2097 
    2098             try {
    2099                 $response = wp_remote_get( $activation_url, [
    2100                     'timeout'   => 15,
    2101                     'sslverify' => false,
    2102                     'body'      => $query_args,
    2103                 ] );
    2104 
    2105                 $response = json_decode( $response['body'], true );
    2106             } catch ( Exception $e ) {
    2107                 return '';
    2108             }
    2109 
    2110             $status['response'] = $response;
    2111 
    2112             if ( empty( $response['success'] ) && empty( $response['data'] ) ) {
    2113                 return '';
    2114             }
    2115 
    2116             return $response['data'];
    2117         }
    2118 
    2119         // get_purchased_extension_list
    2120         public static function get_purchased_extension_list() {
    2121             $extensions_available_in_subscriptions = get_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions', true );
    2122             $directorist_purchased_extension_list  = apply_filters( 'directorist_purchased_extension_list', $extensions_available_in_subscriptions );
    2123 
    2124             if ( is_array( $directorist_purchased_extension_list ) ) {
    2125                 return $directorist_purchased_extension_list;
    2126             }
    2127 
    2128             return $extensions_available_in_subscriptions;
    2129         }
    2130 
    2131         // get_purchased_theme_list
    2132         public static function get_purchased_theme_list() {
    2133             $themes_available_in_subscriptions = get_user_meta( get_current_user_id(), '_themes_available_in_subscriptions', true );
    2134             $directorist_purchased_theme_list  = apply_filters( 'directorist_purchased_theme_list', $themes_available_in_subscriptions );
    2135 
    2136             if ( is_array( $directorist_purchased_theme_list ) ) {
    2137                 return $directorist_purchased_theme_list;
    2138             }
    2139 
    2140             return $themes_available_in_subscriptions;
    2141         }
    2142 
    2143         // filter_product_name
    2144         public static function filter_product_type( $product_type = '' ) {
    2145             $product_type = ( 'plugins' === $product_type ) ? 'plugin' : $product_type;
    2146             $product_type = ( 'themes' === $product_type ) ? 'theme' : $product_type;
    2147 
    2148             return $product_type;
    2149         }
    2150 
    2151         /**
    2152          * It Loads Extension view
    2153          */
    2154         public function show_extension_view() {
    2155             // delete_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion' );
    2156             // delete_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion' );
    2157 
    2158             // Check Sassion
    2159             $has_subscriptions_sassion = get_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
    2160             $is_logged_in              = ( ! empty( $has_subscriptions_sassion ) ) ? true : false;
    2161 
    2162             $settings_url = admin_url( 'edit.php?post_type=at_biz_dir&page=atbdp-settings#extension_settings__extensions_general' );
    2163 
    2164             $extensions_overview = $this->get_extensions_overview();
    2165             $themes_overview     = $this->get_themes_overview();
    2166 
    2167             $hard_logout = apply_filters( 'atbdp_subscriptions_hard_logout', false );
    2168             $hard_logout = ( $hard_logout ) ? 1 : 0;
    2169 
    2170             $data = [
    2171                 'ATBDP_Extensions'                      => $this,
    2172                 'is_logged_in'                          => $is_logged_in,
    2173                 'hard_logout'                           => $hard_logout,
    2174 
    2175                 'total_active_extensions'               => $extensions_overview['total_active_extensions'],
    2176                 'total_outdated_extensions'             => $extensions_overview['total_outdated_extensions'],
    2177                 'outdated_plugin_list'                  => $extensions_overview['outdated_plugin_list'],
    2178                 'installed_extension_list'              => $extensions_overview['installed_extension_list'],
    2179                 'extensions_available_in_subscriptions' => $extensions_overview['extensions_available_in_subscriptions'],
    2180                 'total_available_extensions'            => $extensions_overview['total_available_extensions'],
    2181                 'extensions_promo_list'                 => $extensions_overview['extensions_promo_list'],
    2182                 'required_extensions_list'              => $extensions_overview['required_extensions'],
    2183 
    2184                 'total_active_themes'                   => $themes_overview['total_active_themes'],               // $my_active_themes,
    2185                 'total_outdated_themes'                 => $themes_overview['total_outdated_themes'],             // $my_outdated_themes,
    2186                 'installed_theme_list'                  => $themes_overview['installed_theme_list'],              // $installed_theme_list,
    2187                 'current_active_theme_info'             => $themes_overview['current_active_theme_info'],         // $active_theme,
    2188                 'themes_available_in_subscriptions'     => $themes_overview['themes_available_in_subscriptions'], // $themes_available_in_subscriptions,
    2189                 'total_available_themes'                => $themes_overview['total_available_themes'],
    2190                 'themes_promo_list'                     => $themes_overview['themes_promo_list'],
    2191 
    2192                 'extension_list'                        => $this->extensions,
    2193                 'theme_list'                            => $this->themes,
    2194 
    2195                 'settings_url'                          => $settings_url,
    2196             ];
    2197 
    2198             ATBDP()->load_template( 'admin-templates/theme-extensions/theme-extension', $data );
    2199         }
    2200 
    2201 
    2202         private function is_verified_nonce(){
    2203             $nonce = ! empty( $_POST['nonce'] ) ? $_POST['nonce'] : '';
    2204             return wp_verify_nonce( $nonce, 'atbdp_nonce_action_js' );
    2205         }
    2206 
    2207     }
     25    /**
     26     * Class ATBDP_Extensions
     27     */
     28    class ATBDP_Extensions {
     29        public static $extensions_aliases = [];
     30
     31        public $extensions          = [];
     32        public $themes              = [];
     33        public $required_extensions = [];
     34
     35        public function __construct() {
     36            add_action( 'admin_menu', [ $this, 'admin_menu' ], 100 );
     37            add_action( 'admin_init', [ $this, 'setup_ajax_actions' ] );
     38
     39            if ( ! empty( $_GET['page'] ) && ( 'atbdp-extension' === $_GET['page'] ) ) {
     40                add_action( 'admin_init', [ $this, 'initial_setup' ] );
     41            }
     42        }
     43
     44        public function setup_ajax_actions() {
     45            if ( ! current_user_can( 'manage_options' ) ) {
     46                return;
     47            }
     48
     49            // Ajax
     50            add_action( 'wp_ajax_atbdp_authenticate_the_customer', [$this, 'authenticate_the_customer'] );
     51            add_action( 'wp_ajax_atbdp_download_file', [$this, 'handle_file_download_request'] );
     52            add_action( 'wp_ajax_atbdp_install_file_from_subscriptions', [$this, 'handle_file_install_request_from_subscriptions'] );
     53            add_action( 'wp_ajax_atbdp_plugins_bulk_action', [$this, 'plugins_bulk_action'] );
     54            add_action( 'wp_ajax_atbdp_activate_theme', [$this, 'activate_theme'] );
     55            add_action( 'wp_ajax_atbdp_activate_plugin', [$this, 'activate_plugin'] );
     56            add_action( 'wp_ajax_atbdp_update_plugins', [$this, 'handle_plugins_update_request'] );
     57            add_action( 'wp_ajax_atbdp_update_theme', [$this, 'handle_theme_update_request'] );
     58            add_action( 'wp_ajax_atbdp_refresh_purchase_status', [$this, 'handle_refresh_purchase_status_request'] );
     59            add_action( 'wp_ajax_atbdp_close_subscriptions_sassion', [$this, 'handle_close_subscriptions_sassion_request'] );
     60
     61            // add_action( 'wp_ajax_atbdp_download_purchased_items', array($this, 'download_purchased_items') );
     62        }
     63
     64        // initial_setup
     65        public function initial_setup() {
     66            $this->setup_extensions_alias();
     67
     68            wp_update_plugins();
     69
     70            // Apply hook to required extensions
     71            $this->required_extensions = apply_filters( 'directorist_required_extensions', [] );
     72
     73            $this->setup_products_list();
     74        }
     75
     76        // setup_extensions_alias
     77        public function setup_extensions_alias() {
     78
     79            // Latest Key     => Deprecated key
     80            // Deprecated key => Latest Key
     81            self::$extensions_aliases = apply_filters( 'directorist_extensions_aliases', [
     82                'directorist-listings-with-map'        => 'directorist-listings-map',
     83                'directorist-listings-map'             => 'directorist-listings-with-map',
     84
     85                'directorist-adverts-manager'          => 'directorist-ads-manager',
     86                'directorist-ads-manager'              => 'directorist-adverts-manager',
     87
     88                'directorist-gallery'                  => 'directorist-image-gallery',
     89                'directorist-image-gallery'            => 'directorist-gallery',
     90
     91                'directorist-slider-carousel'          => 'directorist-listings-slider-carousel',
     92                'directorist-listings-slider-carousel' => 'directorist-slider-carousel',
     93
     94                'directorist-faqs'                     => 'directorist-listing-faqs',
     95                'directorist-listing-faqs'             => 'directorist-faqs',
     96            ] );
     97        }
     98
     99        // get_required_extension_list
     100        public function get_required_extension_list() {
     101            $required_extensions = [];
     102
     103            foreach ( $this->required_extensions as $recommandation ) {
     104
     105                if ( ! isset( $recommandation['extensions'] ) ) {
     106                    continue;
     107                }
     108
     109                if ( ! is_array( $recommandation['extensions'] ) ) {
     110                    continue;
     111                }
     112
     113                foreach ( $recommandation['extensions'] as $extension ) {
     114                    $extension_alias = $this->get_extension_alias_key( $extension );
     115
     116                    if ( ! ( isset( $this->extensions[$extension] ) || isset( $this->extensions[$extension_alias] ) ) ) {
     117                        continue;
     118                    }
     119
     120                    if ( empty( $required_extensions[$extension] ) ) {
     121                        $required_extensions[$extension] = [];
     122                    }
     123
     124                    $required_extensions[$extension][] = $recommandation['ref'];
     125                }
     126
     127            }
     128
     129            return $required_extensions;
     130        }
     131
     132        // prepare_the_final_requred_extension_list
     133        public function prepare_the_final_requred_extension_list( array $args = [] ) {
     134            $recommandation = [];
     135
     136            $required_extensions_list = $this->get_required_extension_list();
     137            $purchased_extension_list = self::get_purchased_extension_list();
     138            $purchased_extensions     = ( ! empty( $purchased_extension_list ) && is_array( $purchased_extension_list ) ) ? array_keys( $purchased_extension_list ) : [];
     139            $plugin_dir_path          = trailingslashit( dirname( ATBDP_DIR ) );
     140
     141            foreach ( $required_extensions_list as $extension => $recommanded_by ) {
     142                $extension_alias = $this->get_extension_alias_key( $extension );
     143
     144                if ( $this->has_match_in_active_plugins( [ $extension, $extension_alias ] ) ) {
     145                    continue;
     146                }
     147
     148                $is_purchased       = ( in_array( $extension, $purchased_extensions ) ) ? true : false;
     149                $is_purchased_alias = ( in_array( $extension_alias, $purchased_extensions ) ) ? true : false;
     150
     151                $is_installed = file_exists( $plugin_dir_path . $extension );
     152                $is_installed_alias = ( ! empty( $extension_alias ) && file_exists( $plugin_dir_path . $extension_alias ) ) ? true : false;
     153
     154
     155                $base = "{$extension}/{$extension}.php";
     156
     157                if ( ! empty( $this->extensions[ $extension ] ) && ! empty( $this->extensions[ $extension ]['base'] ) ) {
     158                    $base = $this->extensions[ $extension ]['base'];
     159                }
     160
     161                if ( ! empty( $this->extensions[ $extension_alias ] ) && ! empty( $this->extensions[ $extension_alias ]['base'] ) ) {
     162                    $base = $this->extensions[ $extension_alias ]['base'];
     163                }
     164
     165                $recommandation[$extension]              = [];
     166                $recommandation[$extension]['ref']       = $recommanded_by;
     167                $recommandation[$extension]['base']      = $base;
     168                $recommandation[$extension]['purchased'] = ( $is_purchased || $is_purchased_alias ) ? true : false;
     169                $recommandation[$extension]['installed'] = ( $is_installed || $is_installed_alias ) ? true : false;
     170            }
     171
     172            return $recommandation;
     173        }
     174
     175        public function has_match_in_active_plugins( $plugin_name = '' ) {
     176            $match_found = false;
     177
     178            $active_plugins = get_option( 'active_plugins', array() );
     179
     180            if ( empty( $plugin_name ) ) {
     181                return false;
     182            }
     183
     184            if ( empty( $active_plugins ) ) {
     185                return false;
     186            }
     187
     188            if ( ! is_array( $active_plugins ) ) {
     189                return false;
     190            }
     191
     192            foreach ( $active_plugins as $plugin_path ) {
     193                if ( empty( $plugin_name ) && ( false !== strpos( $plugin_path, $plugin_name ) ) ) {
     194                    return true;
     195                }
     196
     197                if ( is_array( $plugin_name ) ) {
     198                    foreach ( $plugin_name as $plugin_key ) {
     199                        if ( is_string( $plugin_key ) && ! empty( $plugin_key ) && false !== strpos( $plugin_path, $plugin_key ) ) {
     200                            return true;
     201                        }
     202                    }
     203                }
     204            }
     205
     206            return $match_found;
     207        }
     208
     209        // get_the_products_list
     210        public function setup_products_list() {
     211
     212
     213            $url     = 'https://app.directorist.com/wp-json/directorist/v1/get-remote-products';
     214            $headers = [
     215                'user-agent' => 'Directorist/' . md5( esc_url( home_url() ) ) . ';',
     216                'Accept'     => 'application/json',
     217            ];
     218
     219            $config = [
     220                'method'      => 'GET',
     221                'timeout'     => 30,
     222                'redirection' => 5,
     223                'httpversion' => '1.0',
     224                'headers'     => $headers,
     225                'cookies'     => [],
     226            ];
     227
     228            $response_body = [];
     229
     230            try {
     231                $response = wp_remote_get( $url, $config );
     232
     233                if ( ! is_wp_error( $response ) ) {
     234                    $response_body = ( 'string' === gettype( $response['body'] ) ) ? json_decode( $response['body'], true ) : $response['body'];
     235                    $extensions = $response_body['extensions'];
     236                    $themes = $response_body['themes'];
     237
     238                    $this->extensions = apply_filters( 'atbdp_extension_list', $extensions );
     239                    $this->themes = apply_filters( 'atbdp_theme_list', $themes );
     240                }
     241
     242            } catch ( Exception $e ) {
     243
     244            }
     245        }
     246
     247        // exclude_purchased_extensions
     248        public function exclude_purchased_extensions( $extensions ) {
     249            $has_subscriptions_sassion = get_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
     250            $is_logged_in              = ( ! empty( $has_subscriptions_sassion ) ) ? true : false;
     251
     252            if ( ! $is_logged_in ) {
     253                return $extensions;
     254            }
     255
     256            $purchased_products = get_user_meta( get_current_user_id(), '_atbdp_purchased_products', true );
     257
     258            if ( empty( $purchased_products ) ) {
     259                return $extensions;
     260            }
     261
     262            $purchased_extensions = ( ! empty( $purchased_products['plugins'] ) ) ? $purchased_products['plugins'] : '';
     263
     264            if ( empty( $purchased_extensions ) ) {
     265                return $extensions;
     266            }
     267
     268            $purchased_extensions_keys = ( is_array( $purchased_extensions ) ) ? array_keys( $purchased_extensions ) : [];
     269            $excluded_extensions       = $extensions;
     270
     271            foreach ( $excluded_extensions as $extension_key => $extension ) {
     272
     273                if ( ! in_array( $extension_key, $purchased_extensions_keys ) ) {
     274                    continue;
     275                }
     276
     277                $excluded_extensions[$extension_key]['active'] = false;
     278            }
     279
     280            return $excluded_extensions;
     281        }
     282
     283        // exclude_purchased_themes
     284        public function exclude_purchased_themes( $themes ) {
     285            $has_subscriptions_sassion = get_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
     286            $is_logged_in              = ( ! empty( $has_subscriptions_sassion ) ) ? true : false;
     287
     288            if ( ! $is_logged_in ) {
     289                return $themes;
     290            }
     291
     292            $purchased_products = get_user_meta( get_current_user_id(), '_atbdp_purchased_products', true );
     293
     294            if ( empty( $purchased_products ) ) {
     295                return $themes;
     296            }
     297
     298            $purchased_themes = ( ! empty( $purchased_products['themes'] ) ) ? $purchased_products['themes'] : '';
     299
     300            if ( empty( $purchased_themes ) ) {
     301                return $themes;
     302            }
     303
     304            $purchased_themes_keys = is_array( $purchased_themes ) ? array_keys( $purchased_themes ) : [];
     305            $excluded_themes       = $themes;
     306
     307            foreach ( $excluded_themes as $theme_key => $theme ) {
     308
     309                if ( ! in_array( $theme_key, $purchased_themes_keys ) ) {
     310                    continue;
     311                }
     312
     313                $excluded_themes[$theme_key]['active'] = false;
     314            }
     315
     316            return $excluded_themes;
     317        }
     318
     319        // get_active_extensions
     320        public function get_active_extensions() {
     321            $active_extensions = [];
     322
     323            foreach ( $this->extensions as $extension_key => $extension_args ) {
     324
     325                if ( empty( $extension_args['active'] ) ) {
     326                    continue;
     327                }
     328
     329                $active_extensions[$extension_key] = $extension_args;
     330            }
     331
     332            return $active_extensions;
     333        }
     334
     335        // get_active_themes
     336        public function get_active_themes() {
     337            $active_themes = [];
     338
     339            foreach ( $this->themes as $theme_key => $theme_args ) {
     340
     341                if ( empty( $theme_args['active'] ) ) {
     342                    continue;
     343                }
     344
     345                $active_themes[$theme_key] = $theme_args;
     346            }
     347
     348            return $active_themes;
     349        }
     350
     351        // handle_plugins_update_request
     352        public function handle_plugins_update_request() {
     353
     354            if ( ! $this->is_verified_nonce() ) {
     355                $status            = [];
     356                $status['success'] = false;
     357                $status['message'] = 'Invalid request';
     358
     359                wp_send_json( ['status' => $status] );
     360            }
     361
     362            $plugin_key = ( isset( $_POST['plugin_key'] ) ) ? wp_unslash( $_POST['plugin_key'] ) : '';
     363            $status     = $this->update_plugins( ['plugin_key' => $plugin_key] );
     364
     365            wp_send_json( $status );
     366        }
     367
     368        // update_plugins
     369        public function update_plugins( array $args = [] ) {
     370            $default = ['plugin_key' => ''];
     371            $args    = array_merge( $default, $args );
     372
     373            $status     = ['success' => true];
     374            $plugin_key = $args['plugin_key'];
     375
     376            $plugin_updates       = get_site_transient( 'update_plugins' );
     377            $outdated_plugins     = $plugin_updates->response;
     378            $outdated_plugins_key = ( is_array( $outdated_plugins ) ) ? array_keys( $outdated_plugins ) : [];
     379
     380            if ( empty( $outdated_plugins_key ) ) {
     381                $status['message'] = __( 'All plugins are up to date', 'directorist' );
     382
     383                return ['status' => $status];
     384            }
     385
     386            if ( ! empty( $plugin_key ) && ! in_array( $plugin_key, $outdated_plugins_key ) ) {
     387                $status['message'] = __( 'The plugin is up to date', 'directorist' );
     388
     389                return ['status' => $status];
     390            }
     391
     392            $plugins_available_in_subscriptions = self::get_purchased_extension_list();
     393
     394            // Update single
     395            if ( ! empty( $plugin_key ) ) {
     396                $plugin_key  = self::filter_plugin_key_from_base_name( $plugin_key );
     397                $plugin_item = self::extract_plugin_from_list( $plugin_key, $plugins_available_in_subscriptions );
     398                $url         = self::get_file_download_link( $plugin_item, 'plugin' );
     399
     400                $download_status = $this->download_plugin( ['url' => $url] );
     401
     402                if ( ! $download_status['success'] ) {
     403                    $status['success'] = false;
     404                    $status['message'] = __( 'The plugin could not update', 'directorist' );
     405                    $status['log']     = $download_status['message'];
     406                } else {
     407                    $status['success'] = true;
     408                    $status['message'] = __( 'The plugin has been updated successfully', 'directorist' );
     409                    $status['log']     = $download_status['message'];
     410                }
     411
     412                return ['status' => $status];
     413            }
     414
     415            // Update all
     416            $updated_plugins       = [];
     417            $update_failed_plugins = [];
     418
     419            foreach ( $outdated_plugins as $plugin_base => $plugin ) {
     420                $plugin_key  = self::filter_plugin_key_from_base_name( $plugin_key );
     421                $plugin_item = self::extract_plugin_from_list( $plugin_key, $plugins_available_in_subscriptions );
     422                $url         = self::get_file_download_link( $plugin_item, 'plugin' );
     423
     424                $download_status = $this->download_plugin( ['url' => $url] );
     425
     426                if ( ! $download_status['success'] ) {
     427                    $update_failed_plugins[$plugin_base] = $plugin;
     428                } else {
     429                    $updated_plugins[$plugin_base] = $plugin;
     430                }
     431
     432            }
     433
     434            $status['updated_plugins']       = $updated_plugins;
     435            $status['update_failed_plugins'] = $update_failed_plugins;
     436
     437            if ( ! empty( $updated_plugins ) && ! empty( $update_failed_plugins ) ) {
     438                $status['success'] = false;
     439                $status['message'] = __( 'Some of the plugin could not update', 'directorist' );
     440            }
     441
     442            if ( empty( $update_failed_plugins ) ) {
     443                $status['success'] = true;
     444                $status['message'] = __( 'All the plugins are updated successfully', 'directorist' );
     445            }
     446
     447            if ( empty( $updated_plugins ) ) {
     448                $status['success'] = true;
     449                $status['message'] = __( 'No plugins could not update', 'directorist' );
     450            }
     451
     452            return ['status' => $status];
     453        }
     454
     455        // extract_plugin_from_list
     456        public static function extract_plugin_from_list( $plugin_key = '', $list = [] ) {
     457
     458            $plugin_item = [];
     459            $plugin_key  = ( is_string( $plugin_key ) ) ? $plugin_key : '';
     460            $list        = ( is_array( $list ) ) ? $list : [];
     461
     462            $keys_in_list = array_keys( $list );
     463
     464            if ( in_array( $plugin_key, $keys_in_list ) ) {
     465                $plugin_item = $list[$plugin_key];
     466            }
     467
     468            $plugin_alias_key = self::get_extension_alias_key( $plugin_key );
     469
     470            if ( in_array( $plugin_alias_key, $keys_in_list ) ) {
     471                $plugin_item = $list[$plugin_alias_key];
     472            }
     473
     474            return $plugin_item;
     475        }
     476
     477        // filter_plugin_key_from_base_name
     478        public static function filter_plugin_key_from_base_name( $plugin_key = '' ) {
     479
     480            if ( ! is_string( $plugin_key ) ) {
     481                return '';
     482            }
     483
     484            $plugin_key = preg_replace( '/\/.+/', '', $plugin_key );
     485
     486            return $plugin_key;
     487        }
     488
     489        // get_extension_alias_key
     490        public static function get_extension_alias_key( string $plugin_key = '' ) {
     491            $extensions_aliases      = self::$extensions_aliases;
     492            $extensions_aliases_keys = ( is_array( $extensions_aliases ) && ! empty( $extensions_aliases ) ) ? array_keys( $extensions_aliases ) : [];
     493            $plugin_alias_key        = in_array( $plugin_key, $extensions_aliases_keys ) ? $extensions_aliases[$plugin_key] : '';
     494
     495            return $plugin_alias_key;
     496        }
     497
     498        // plugins_bulk_action
     499        public function plugins_bulk_action() {
     500            $status = ['success' => true];
     501
     502            if ( ! $this->is_verified_nonce() ) {
     503                $status['success'] = false;
     504                $status['message'] = 'Invalid request';
     505
     506                wp_send_json( ['status' => $status] );
     507            }
     508
     509            $task         = ( isset( $_POST['task'] ) ) ? wp_unslash( $_POST['task'] ) : '';
     510            $plugin_items = ( isset( $_POST['plugin_items'] ) ) ? wp_unslash( $_POST['plugin_items'] ) : '';
     511
     512            // Validation
     513            if ( empty( $task ) ) {
     514                $status['success'] = false;
     515                $status['message'] = 'No task found';
     516                wp_send_json( ['status' => $status] );
     517            }
     518
     519            if ( empty( $plugin_items ) ) {
     520                $status['success'] = false;
     521                $status['message'] = 'No plugin items found';
     522                wp_send_json( ['status' => $status] );
     523            }
     524
     525            // Activate
     526            if ( 'activate' === $task ) {
     527                foreach ( $plugin_items as $plugin ) {
     528                    activate_plugin( $plugin );
     529                }
     530            }
     531
     532            // Deactivate
     533            if ( 'deactivate' === $task ) {
     534                deactivate_plugins( $plugin_items );
     535            }
     536
     537            // Uninstall
     538            if ( 'uninstall' === $task ) {
     539                delete_plugins( $plugin_items );
     540            }
     541
     542            wp_send_json( ['status' => $status] );
     543        }
     544
     545        // activate_theme
     546        public function activate_theme() {
     547            $status           = ['success' => true];
     548            $theme_stylesheet = ( isset( $_POST['theme_stylesheet'] ) ) ? wp_unslash( $_POST['theme_stylesheet'] ) : '';
     549
     550            if ( ! $this->is_verified_nonce() ) {
     551                $status['success'] = false;
     552                $status['message'] = 'Invalid request';
     553
     554                wp_send_json( ['status' => $status] );
     555            }
     556
     557            if ( empty( $theme_stylesheet ) ) {
     558                $status['success'] = false;
     559                $status['message'] = __( 'Theme\'s stylesheet is missing', 'directorist' );
     560
     561                wp_send_json( ['status' => $status] );
     562            }
     563
     564            switch_theme( $theme_stylesheet );
     565            wp_send_json( ['status' => $status] );
     566        }
     567
     568        // activate_plugin
     569        public function activate_plugin() {
     570            $status     = ['success' => true];
     571            $plugin_key = ( isset( $_POST['item_key'] ) ) ? wp_unslash( $_POST['item_key'] ) : '';
     572
     573            if ( ! $this->is_verified_nonce() ) {
     574                $status['success'] = false;
     575                $status['message'] = 'Invalid request';
     576
     577                wp_send_json( ['status' => $status] );
     578            }
     579
     580            if ( empty( $plugin_key ) ) {
     581                $status['success'] = false;
     582                $status['log']     = ['$plugin_key' => $plugin_key];
     583                $status['message'] = __( 'Please specefy which plugin to activate', 'directorist' );
     584
     585                wp_send_json( ['status' => $status] );
     586            }
     587
     588            activate_plugin( $plugin_key );
     589            wp_send_json( ['status' => $status] );
     590        }
     591
     592        // handle_theme_update_request
     593        public function handle_theme_update_request() {
     594
     595            if ( ! $this->is_verified_nonce() ) {
     596                $status            = [];
     597                $status['success'] = false;
     598                $status['message'] = 'Invalid request';
     599
     600                wp_send_json( ['status' => $status] );
     601            }
     602
     603            $theme_stylesheet = ( isset( $_POST['theme_stylesheet'] ) ) ? wp_unslash( $_POST['theme_stylesheet'] ) : '';
     604
     605            $update_theme_status = $this->update_the_themes( ['theme_stylesheet' => $theme_stylesheet] );
     606            wp_send_json( $update_theme_status );
     607        }
     608
     609        // update_the_theme
     610        public function update_the_themes( array $args = [] ) {
     611            $default = ['theme_stylesheet' => ''];
     612            $args    = array_merge( $default, $args );
     613
     614            $status = ['success' => true];
     615
     616            $theme_stylesheet    = $args['theme_stylesheet'];
     617            $theme_updates       = get_site_transient( 'update_themes' );
     618            $outdated_themes     = $theme_updates->response;
     619            $outdated_themes_key = ( is_array( $outdated_themes ) ) ? array_keys( $outdated_themes ) : [];
     620
     621            if ( empty( $outdated_themes_key ) ) {
     622                $status['message'] = __( 'All themes are up to date', 'directorist' );
     623
     624                return ['status' => $status];
     625            }
     626
     627            if ( ! empty( $theme_stylesheet ) && ! in_array( $theme_stylesheet, $outdated_themes_key ) ) {
     628                $status['message'] = __( 'The theme is up to date', 'directorist' );
     629
     630                return ['status' => $status];
     631            }
     632
     633            $themes_available_in_subscriptions      = self::get_purchased_theme_list();
     634            $themes_available_in_subscriptions_keys = ( is_array( $themes_available_in_subscriptions ) ) ? array_keys( $themes_available_in_subscriptions ) : [];
     635
     636            // Check if stylesheet is present
     637            if ( ! empty( $theme_stylesheet ) ) {
     638
     639                // Check if the the update is available
     640                if ( ! in_array( $theme_stylesheet, $outdated_themes_key ) ) {
     641                    $status['success'] = false;
     642                    $status['message'] = __( 'The theme is already upto date', 'directorist' );
     643
     644                    return ['status' => $status];
     645                }
     646
     647                $theme_item = $themes_available_in_subscriptions[$theme_stylesheet];
     648                $url        = self::get_file_download_link( $theme_item, 'theme' );
     649                $url        = ( empty( $url ) && ! empty( $outdated_themes[ $theme_stylesheet ]['package'] ) ) ? $outdated_themes[ $theme_stylesheet ]['package'] : $url;
     650
     651                $download_status = $this->download_theme( ['url' => $url] );
     652
     653                if ( ! $download_status['success'] ) {
     654                    $status['success'] = false;
     655                    $status['message'] = __( 'The theme could not update', 'directorist' );
     656                    $status['log']     = $download_status['message'];
     657                } else {
     658                    $status['success'] = true;
     659                    $status['message'] = __( 'The theme has been updated successfully', 'directorist' );
     660                    $status['log']     = $download_status['message'];
     661                    wp_clean_themes_cache();
     662                };
     663
     664                return ['status' => $status];
     665            }
     666
     667            // Update all
     668            $updated_themes       = [];
     669            $update_failed_themes = [];
     670
     671            foreach ( $outdated_themes as $theme_key => $theme ) {
     672                $url = '';
     673
     674                if ( ! in_array( $theme_key, $themes_available_in_subscriptions_keys ) ) {
     675                    continue;
     676                }
     677
     678                $theme_item = $themes_available_in_subscriptions[$theme_key];
     679                $url        = self::get_file_download_link( $theme_item, 'theme' );
     680
     681                $download_status = $this->download_theme( ['url' => $url] );
     682
     683                if ( ! $download_status['success'] ) {
     684                    $update_failed_themes[$theme_key] = $theme;
     685                } else {
     686                    $updated_themes[$theme_key] = $theme;
     687                }
     688
     689            }
     690
     691            $status['updated_themes']       = $updated_themes;
     692            $status['update_failed_themes'] = $update_failed_themes;
     693
     694            if ( ! empty( $updated_themes ) && ! empty( $update_failed_themes ) ) {
     695                $status['success'] = false;
     696                $status['message'] = __( 'Some of the theme could not update', 'directorist' );
     697            }
     698
     699            if ( empty( $update_failed_themes ) ) {
     700                $status['success'] = true;
     701                $status['message'] = __( 'All the themes are updated successfully', 'directorist' );
     702            }
     703
     704            if ( empty( $updated_themes ) ) {
     705                $status['success'] = true;
     706                $status['message'] = __( 'No themes could not update', 'directorist' );
     707            }
     708
     709            return ['status' => $status];
     710        }
     711
     712        /**
     713         * Authenticate users as directorist customer.
     714         *
     715         * @return void
     716         */
     717        public function authenticate_the_customer() {
     718
     719            $status = ['success' => true, 'log' => []];
     720
     721            if ( ! $this->is_verified_nonce() ) {
     722                $status['success']                 = false;
     723                $status['log']['invalid_request'] = [
     724                    'type'    => 'error',
     725                    'message' => 'Invalid request',
     726                ];
     727            }
     728
     729            // Get form data
     730            $username = ( isset( $_POST['username'] ) ) ? $_POST['username'] : '';
     731            $password = ( isset( $_POST['password'] ) ) ? urlencode( $_POST['password'] ) : '';
     732
     733            // Validate username
     734            if ( empty( $username ) && ! empty( $password ) ) {
     735                $status['success']                 = false;
     736                $status['log']['username_missing'] = [
     737                    'type'    => 'error',
     738                    'message' => 'Username is required',
     739                ];
     740            }
     741
     742            // Validate password
     743            if ( empty( $password ) && ! empty( $username ) ) {
     744                $status['success']                 = false;
     745                $status['log']['password_missing'] = [
     746                    'type'    => 'error',
     747                    'message' => 'Password is required',
     748                ];
     749            }
     750
     751            // Validate username && password
     752            if ( empty( $password ) && empty( $username ) ) {
     753                $status['success']                 = false;
     754                $status['log']['password_missing'] = [
     755                    'type'    => 'error',
     756                    'message' => 'Username and Password is required',
     757                ];
     758            }
     759
     760            if ( ! $status['success'] ) {
     761                wp_send_json( ['status' => $status] );
     762            }
     763
     764            // Get licencing data
     765            $response = self::remote_authenticate_user( ['user' => $username, 'password' => $password] );
     766
     767            // Validate response
     768            if ( ! $response['success'] ) {
     769                $status['success']      = false;
     770                $default_status_message = ( isset( $response['message'] ) ) ? $response['message'] : '';
     771
     772                if ( isset( $response['log'] ) && isset( $response['log']['errors'] ) && is_array( $response['log']['errors'] ) ) {
     773                    foreach ( $response['log']['errors'] as $error_key => $error_value ) {
     774                        $status['log'][$error_key] = [
     775                            'type'    => 'error',
     776                            'message' => ( is_array( $error_value ) ) ? $error_value[0] : $error_value,
     777                        ];
     778                    }
     779
     780                } else {
     781                    $status['log']['unknown_error'] = [
     782                        'type'    => 'error',
     783                        'message' => ( ! empty( $default_status_message ) ) ? $default_status_message : __( 'Something went wrong', 'directorist' ),
     784                    ];
     785                }
     786
     787                wp_send_json( ['status' => $status, 'response_body' => $response] );
     788            }
     789
     790            $previous_username = get_user_meta( get_current_user_id(), '_atbdp_subscribed_username', true );
     791
     792            // Enable Sassion
     793            update_user_meta( get_current_user_id(), '_atbdp_subscribed_username', $username );
     794            update_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
     795
     796            $plugins_available_in_subscriptions = self::get_purchased_extension_list();
     797            $themes_available_in_subscriptions  = self::get_purchased_theme_list();
     798            $has_previous_subscriptions         = ( ! empty( $plugins_available_in_subscriptions ) || ! empty( $themes_available_in_subscriptions ) ) ? true : false;
     799
     800            if ( $previous_username === $username && $has_previous_subscriptions ) {
     801                // Enable Sassion
     802                update_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
     803                $this->refresh_purchase_status( $args = ['password' => $password] );
     804
     805                wp_send_json( ['status' => $status, 'has_previous_subscriptions' => true] );
     806            }
     807
     808            delete_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions' );
     809            delete_user_meta( get_current_user_id(), '_themes_available_in_subscriptions' );
     810
     811            $license_data = $response['license_data'];
     812
     813            // Update user meta
     814            if ( ! empty( $license_data['themes'] ) ) {
     815                $themes_available_in_subscriptions = $this->prepare_available_in_subscriptions( $license_data['themes'] );
     816                update_user_meta( get_current_user_id(), '_themes_available_in_subscriptions', $themes_available_in_subscriptions );
     817            }
     818
     819            if ( ! empty( $license_data['plugins'] ) ) {
     820                $plugins_available_in_subscriptions = $this->prepare_available_in_subscriptions( $license_data['plugins'] );
     821                update_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions', $plugins_available_in_subscriptions );
     822            }
     823
     824            $status['success']                 = true;
     825            $status['log']['login_successful'] = [
     826                'type'    => 'success',
     827                'message' => 'Login is successful',
     828            ];
     829
     830            wp_send_json( ['status' => $status, 'license_data' => $license_data] );
     831        }
     832
     833        // handle_refresh_purchase_status_request
     834        public function handle_refresh_purchase_status_request() {
     835            $status   = ['success' => true];
     836
     837            if ( ! $this->is_verified_nonce() ) {
     838                $status['success'] = false;
     839                $status['message'] = 'Invalid request';
     840
     841                wp_send_json( ['status' => $status] );
     842            }
     843
     844            $password = ( isset( $_POST['password'] ) ) ? $_POST['password'] : '';
     845
     846            $status = $this->refresh_purchase_status( ['password' => $password] );
     847
     848            wp_send_json( $status );
     849        }
     850
     851        // refresh_purchase_status
     852        public function refresh_purchase_status( array $args = [] ) {
     853            $status  = ['success' => true];
     854            $default = ['password' => ''];
     855            $args    = array_merge( $default, $args );
     856
     857            if ( empty( $args['password'] ) ) {
     858                $status['success'] = false;
     859                $status['message'] = __( 'Password is required', 'directorist' );
     860
     861                return ['status' => $status];
     862            }
     863
     864            $username = get_user_meta( get_current_user_id(), '_atbdp_subscribed_username', true );
     865            $password = $args['password'];
     866
     867            if ( empty( $username ) ) {
     868                $status['success'] = false;
     869                $status['reload']  = true;
     870                $status['message'] = __( 'Sassion is destroyed, please sign-in again', 'directorist' );
     871
     872                delete_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion' );
     873
     874                return ['status' => $status];
     875            }
     876
     877            // Get licencing data
     878            $authentication = self::remote_authenticate_user( ['user' => $username, 'password' => $password] );
     879
     880            // Validate response
     881            if ( ! $authentication['success'] ) {
     882                $status['success'] = false;
     883                $status['message'] = $authentication['message'];
     884
     885                return ['status' => $status, 'response_body' => $authentication];
     886            }
     887
     888            $license_data = $authentication['license_data'];
     889
     890            // Update user meta
     891            if ( ! empty( $license_data['themes'] ) ) {
     892                $themes_available_in_subscriptions = $this->prepare_available_in_subscriptions( $license_data['themes'] );
     893                update_user_meta( get_current_user_id(), '_themes_available_in_subscriptions', $themes_available_in_subscriptions );
     894            }
     895
     896            if ( ! empty( $license_data['plugins'] ) ) {
     897                $plugins_available_in_subscriptions = $this->prepare_available_in_subscriptions( $license_data['plugins'] );
     898                update_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions', $plugins_available_in_subscriptions );
     899            }
     900
     901            $status['success'] = true;
     902            $status['message'] = __( 'Your purchase has been refreshed successfuly', 'directorist' );
     903
     904            return ['status' => $status];
     905        }
     906
     907        // handle_close_subscriptions_sassion_request
     908        public function handle_close_subscriptions_sassion_request() {
     909
     910            if ( ! $this->is_verified_nonce() ) {
     911                $status            = [];
     912                $status['success'] = false;
     913                $status['message'] = 'Invalid request';
     914
     915                wp_send_json( ['status' => $status] );
     916            }
     917
     918            $hard_logout_state = ( isset( $_POST['hard_logout'] ) ) ? $_POST['hard_logout'] : false;
     919            $status            = $this->close_subscriptions_sassion( ['hard_logout' => $hard_logout_state] );
     920
     921            wp_send_json( $status );
     922        }
     923
     924        // close_subscriptions_sassion
     925        public function close_subscriptions_sassion( array $args = [] ) {
     926            $default = ['hard_logout' => false];
     927            $args    = array_merge( $default, $args );
     928
     929            $status = ['success' => true];
     930            delete_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion' );
     931
     932            if ( $args['hard_logout'] ) {
     933                delete_user_meta( get_current_user_id(), '_atbdp_subscribed_username' );
     934                delete_user_meta( get_current_user_id(), '_themes_available_in_subscriptions' );
     935                delete_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions' );
     936            }
     937
     938            return $status;
     939        }
     940
     941        // prepare_available_in_subscriptions
     942        public function prepare_available_in_subscriptions( array $products = [] ) {
     943            $available_in_subscriptions = [];
     944
     945            if ( empty( $products ) ) {
     946                return $available_in_subscriptions;
     947            }
     948
     949            foreach ( $products as $product ) {
     950                $product_key                              = $this->get_product_key_from_permalink( $product['permalink'] );
     951                $available_in_subscriptions[$product_key] = $product;
     952            }
     953
     954            return $available_in_subscriptions;
     955        }
     956
     957        // get_product_key_from_permalink
     958        public function get_product_key_from_permalink( string $permalink = '' ) {
     959            $product_key = str_replace( 'http://directorist.com/product/', '', $permalink );
     960            $product_key = str_replace( 'https://directorist.com/product/', '', $product_key );
     961            $product_key = str_replace( '/', '', $product_key );
     962
     963            return $product_key;
     964        }
     965
     966        // handle_license_activation_request
     967        public function handle_license_activation_request() {
     968            $status       = ['success' => true];
     969            $license_item = ( isset( $_POST['license_item'] ) ) ? wp_unslash( $_POST['license_item'] ) : '';
     970            $product_type = ( isset( $_POST['product_type'] ) ) ? wp_unslash( $_POST['product_type'] ) : '';
     971
     972            if ( empty( $license_item ) ) {
     973                $status['success'] = false;
     974                $status['message'] = 'License item is missing';
     975
     976                wp_send_json( ['status' => $status] );
     977            }
     978
     979            if ( empty( $product_type ) ) {
     980                $status['success'] = false;
     981                $status['message'] = 'Product type is required';
     982
     983                wp_send_json( ['status' => $status] );
     984            }
     985
     986            $activation_status = $this->activate_license( $license_item, $product_type );
     987            $status['success'] = $activation_status['success'];
     988
     989            wp_send_json( ['status' => $status, 'activation_status' => $activation_status] );
     990        }
     991
     992        // activate_license
     993        public function activate_license( $license_item, $product_type = '' ) {
     994            $status            = ['success' => true];
     995            $activation_status = self::remote_activate_license( $license_item );
     996
     997            if ( empty( $activation_status['success'] ) ) {
     998                $status['success'] = false;
     999            }
     1000
     1001            $status['response'] = $activation_status['response'];
     1002            $product_type       = self::filter_product_type( $product_type );
     1003
     1004            if ( $status['success'] && ( 'plugin' === $product_type || 'theme' === $product_type ) ) {
     1005                $user_purchased = get_user_meta( get_current_user_id(), '_atbdp_purchased_products', true );
     1006
     1007                if ( empty( $user_purchased ) ) {
     1008                    $user_purchased = [];
     1009                }
     1010
     1011                if ( empty( $user_purchased[$product_type] ) ) {
     1012                    $user_purchased[$product_type] = [];
     1013                }
     1014
     1015                $purchased_items = $user_purchased[$product_type];
     1016
     1017                // Append new product
     1018                $product_key                   = $this->get_product_key_from_permalink( $license_item['permalink'] );
     1019                $purchased_items[$product_key] = $license_item;
     1020
     1021                $user_purchased[$product_type] = $purchased_items;
     1022                update_user_meta( get_current_user_id(), '_atbdp_purchased_products', $user_purchased );
     1023
     1024                $status['purchased_products'] = $user_purchased;
     1025            }
     1026
     1027            return $status;
     1028        }
     1029
     1030        // handle_file_install_request_from_subscriptions
     1031        public function handle_file_install_request_from_subscriptions() {
     1032            $item_key = ( isset( $_POST['item_key'] ) ) ? wp_unslash( $_POST['item_key'] ) : '';
     1033            $type     = ( isset( $_POST['type'] ) ) ? wp_unslash( $_POST['type'] ) : '';
     1034
     1035            if ( ! $this->is_verified_nonce() ) {
     1036                $status            = [];
     1037                $status['success'] = false;
     1038                $status['message'] = 'Invalid request';
     1039
     1040                wp_send_json( ['status' => $status] );
     1041            }
     1042
     1043            $installation_status = $this->install_file_from_subscriptions( ['item_key' => $item_key, 'type' => $type] );
     1044            wp_send_json( $installation_status );
     1045        }
     1046
     1047        // install_file_from_subscriptions
     1048        public function install_file_from_subscriptions( array $args = [] ) {
     1049            $default = ['item_key' => '', 'type' => ''];
     1050            $args    = array_merge( $default, $args );
     1051
     1052            $item_key = $args['item_key'];
     1053            $type     = $args['type'];
     1054
     1055            $status = ['success' => true];
     1056
     1057            if ( empty( $item_key ) ) {
     1058                $status['success'] = false;
     1059                $status['message'] = __( 'Item key is missing', 'directorist' );
     1060
     1061                return ['status' => $status];
     1062            }
     1063
     1064            if ( empty( $type ) ) {
     1065                $status['success'] = false;
     1066                $status['message'] = __( 'Type not specified', 'directorist' );
     1067
     1068                return ['status' => $status];
     1069            }
     1070
     1071            if ( 'plugin' !== $type && 'theme' !== $type ) {
     1072                $status['success'] = false;
     1073                $status['message'] = __( 'Invalid type', 'directorist' );
     1074
     1075                return ['status' => $status];
     1076            }
     1077
     1078            if ( 'theme' === $type ) {
     1079                $available_in_subscriptions = self::get_purchased_theme_list();
     1080            }
     1081
     1082            if ( 'plugin' === $type ) {
     1083                $available_in_subscriptions = self::get_purchased_extension_list();
     1084            }
     1085
     1086            if ( empty( $available_in_subscriptions ) ) {
     1087                $status['success'] = false;
     1088                $status['message'] = __( 'Nothing available in subscriptions', 'directorist' );
     1089
     1090                return ['status' => $status];
     1091            }
     1092
     1093            if ( empty( $available_in_subscriptions[$item_key] ) ) {
     1094                $status['success'] = false;
     1095                $status['message'] = __( 'The item is not available in your subscriptions', 'directorist' );
     1096
     1097                return ['status' => $status];
     1098            }
     1099
     1100            $installing_file = $available_in_subscriptions[$item_key];
     1101
     1102            $activatation_status = $this->activate_license( $installing_file, $type );
     1103            $status['log']       = $activatation_status;
     1104
     1105            if ( ! $activatation_status['success'] ) {
     1106                $status['success'] = false;
     1107                $status['message'] = __( 'The license is not valid, please check you subscription.', 'directorist' );
     1108
     1109                return ['status' => $status];
     1110            }
     1111
     1112            $link          = $installing_file['download_link'];
     1113            $download_args = ['url' => $link];
     1114
     1115            if ( 'plugin' === $type ) {
     1116                $download_status = $this->download_plugin( $download_args );
     1117            }
     1118
     1119            if ( 'theme' === $type ) {
     1120                $download_status = $this->download_theme( $download_args );
     1121            }
     1122
     1123            if ( ! $download_status['success'] ) {
     1124                return $download_status;
     1125            }
     1126
     1127            $status['success'] = true;
     1128            $status['message'] = __( 'Installed Successfully', 'directorist' );
     1129
     1130            return ['status' => $status];
     1131        }
     1132
     1133        // handle_plugin_download_request
     1134        public function handle_file_download_request() {
     1135            $status        = ['success' => true];
     1136
     1137            if ( ! $this->is_verified_nonce() ) {
     1138                $status['success'] = false;
     1139                $status['message'] = 'Invalid request';
     1140
     1141                wp_send_json( ['status' => $status] );
     1142            }
     1143
     1144            $download_item = ( isset( $_POST['download_item'] ) ) ? wp_unslash( $_POST['download_item'] ) : '';
     1145            $type          = ( isset( $_POST['type'] ) ) ? wp_unslash( $_POST['type'] ) : '';
     1146
     1147            if ( empty( $download_item ) ) {
     1148                $status['success'] = false;
     1149                $status['message'] = 'Download item is missing';
     1150
     1151                wp_send_json( ['status' => $status] );
     1152            }
     1153
     1154            if ( empty( $type ) ) {
     1155                $status['success'] = false;
     1156                $status['message'] = 'Type not specified';
     1157
     1158                wp_send_json( ['status' => $status] );
     1159            }
     1160
     1161            if ( 'plugin' !== $type || 'theme' !== $type ) {
     1162                $status['success'] = false;
     1163                $status['message'] = 'Invalid type';
     1164
     1165                wp_send_json( ['status' => $status] );
     1166            }
     1167
     1168            $activate_license = $this->activate_license( $download_item, $type );
     1169
     1170            if ( ! $activate_license['success'] ) {
     1171                $status['success'] = false;
     1172                $status['message'] = __( 'Activation failed', 'directorist' );
     1173                $status['ref']     = $activate_license;
     1174
     1175                wp_send_json( ['status' => $status] );
     1176            }
     1177
     1178            if ( empty( $download_item['download_link'] ) ) {
     1179                $status['success'] = false;
     1180                $status['message'] = 'Download Link not found';
     1181
     1182                wp_send_json( ['status' => $status] );
     1183            }
     1184
     1185            if ( ! is_string( $download_item['download_link'] ) ) {
     1186                $status['success'] = false;
     1187                $status['message'] = 'Download Link not found';
     1188
     1189                wp_send_json( ['status' => $status] );
     1190            }
     1191
     1192            $link          = $download_item['download_link'];
     1193            $download_args = ['url' => $link];
     1194
     1195            if ( 'plugin' === $type ) {
     1196                $download_status = $this->download_plugin( $download_args );
     1197            }
     1198
     1199            if ( 'theme' === $type ) {
     1200                $download_status = $this->download_theme( $download_args );
     1201            }
     1202
     1203            if ( ! $download_status['success'] ) {
     1204                return $download_status;
     1205            }
     1206
     1207            $status['success'] = true;
     1208            $status['message'] = __( 'Donloaded', 'directorist' );
     1209
     1210            wp_send_json( ['status' => $status] );
     1211        }
     1212
     1213        // download_plugin
     1214        public function download_plugin( array $args = [] ) {
     1215            $status = ['success' => false];
     1216
     1217            $default = ['url' => '', 'init_wp_filesystem' => true];
     1218            $args    = array_merge( $default, $args );
     1219
     1220            if ( empty( $args['url'] ) || ! self::is_varified_host( $args['url'] ) ) {
     1221                $status['success'] = false;
     1222                $status['message'] = __( 'Invalid download link', 'directorist' );
     1223
     1224                return $status;
     1225            }
     1226
     1227            global $wp_filesystem;
     1228
     1229            if ( $args['init_wp_filesystem'] ) {
     1230
     1231                if ( ! function_exists( 'WP_Filesystem' ) ) {
     1232                    include ABSPATH . 'wp-admin/includes/file.php';
     1233                }
     1234
     1235                WP_Filesystem();
     1236            }
     1237
     1238            $plugin_path = ABSPATH . 'wp-content/plugins';
     1239            $temp_dest   = "{$plugin_path}/atbdp-temp-dir";
     1240            $file_url    = $args['url'];
     1241            $file_name   = basename( $file_url );
     1242            $tmp_file    = download_url( $file_url );
     1243
     1244            if ( ! is_string( $tmp_file ) ) {
     1245                $status['success']  = false;
     1246                $status['tmp_file'] = $tmp_file;
     1247                $status['file_url'] = $file_url;
     1248                $status['message']  = 'Could not download the file';
     1249
     1250                return $status;
     1251            }
     1252
     1253            // Make Temp Dir
     1254            if ( $wp_filesystem->exists( $temp_dest ) ) {
     1255                $wp_filesystem->delete( $temp_dest, true );
     1256            }
     1257
     1258            $wp_filesystem->mkdir( $temp_dest );
     1259
     1260            if ( ! file_exists( $temp_dest ) ) {
     1261                $status['success'] = false;
     1262                $status['message'] = __( 'Could not create temp directory', 'directorist' );
     1263
     1264                return $status;
     1265            }
     1266
     1267            // Sets file temp destination.
     1268            $file_path = "{$temp_dest}/{$file_name}";
     1269
     1270            set_error_handler( function ( $errno, $errstr, $errfile, $errline ) {
     1271                // error was suppressed with the @-operator
     1272                if ( 0 === error_reporting() ) {
     1273                    return false;
     1274                }
     1275
     1276                throw new ErrorException( $errstr, 0, $errno, $errfile, $errline );
     1277            } );
     1278
     1279            // Copies the file to the final destination and deletes temporary file.
     1280            try {
     1281                copy( $tmp_file, $file_path );
     1282            } catch ( Exception $e ) {
     1283                $status['success'] = false;
     1284                $status['message'] = $e->getMessage();
     1285
     1286                return $status;
     1287            }
     1288
     1289            @unlink( $tmp_file );
     1290            unzip_file( $file_path, $temp_dest );
     1291
     1292            if ( "{$plugin_path}/" !== $file_path || $file_path !== $plugin_path ) {
     1293                @unlink( $file_path );
     1294            }
     1295
     1296            $extracted_file_dir = glob( "{$temp_dest}/*", GLOB_ONLYDIR );
     1297
     1298            foreach ( $extracted_file_dir as $dir_path ) {
     1299                $dir_name  = basename( $dir_path );
     1300                $dest_path = "{$plugin_path}/{$dir_name}";
     1301
     1302                // Delete Previous Files if Exists
     1303                if ( $wp_filesystem->exists( $dest_path ) ) {
     1304                    $wp_filesystem->delete( $dest_path, true );
     1305                }
     1306
     1307            }
     1308
     1309            copy_dir( $temp_dest, $plugin_path );
     1310            $wp_filesystem->delete( $temp_dest, true );
     1311
     1312            $status['success'] = true;
     1313            $status['message'] = __( 'The plugin has been downloaded successfully', 'directorist' );
     1314
     1315            return $status;
     1316        }
     1317
     1318        // download_theme
     1319        public function download_theme( array $args = [] ) {
     1320            $status = ['success' => false];
     1321
     1322            $default = ['url' => '', 'init_wp_filesystem' => true];
     1323            $args    = array_merge( $default, $args );
     1324
     1325            if ( empty( $args['url'] ) || ! self::is_varified_host( $args['url'] ) ) {
     1326                $status['success'] = false;
     1327                $status['message'] = __( 'Invalid download link', 'directorist' );
     1328
     1329                return $status;
     1330            }
     1331
     1332            global $wp_filesystem;
     1333
     1334            if ( $args['init_wp_filesystem'] ) {
     1335
     1336                if ( ! function_exists( 'WP_Filesystem' ) ) {
     1337                    include ABSPATH . 'wp-admin/includes/file.php';
     1338                }
     1339
     1340                WP_Filesystem();
     1341            }
     1342
     1343            $theme_path = ABSPATH . 'wp-content/themes';
     1344            $temp_dest  = "{$theme_path}/atbdp-temp-dir";
     1345            $file_url   = $args['url'];
     1346            $file_name  = basename( $file_url );
     1347            $tmp_file   = download_url( $file_url );
     1348
     1349            if ( ! is_string( $tmp_file ) ) {
     1350                $status['success']  = false;
     1351                $status['tmp_file'] = $tmp_file;
     1352                $status['file_url'] = $file_url;
     1353                $status['message']  = 'Could not download the file';
     1354
     1355                return $status;
     1356            }
     1357
     1358            // Make Temp Dir
     1359            if ( $wp_filesystem->exists( $temp_dest ) ) {
     1360                $wp_filesystem->delete( $temp_dest, true );
     1361            }
     1362
     1363            $wp_filesystem->mkdir( $temp_dest );
     1364
     1365            if ( ! file_exists( $temp_dest ) ) {
     1366                $status['success'] = false;
     1367                $status['message'] = __( 'Could not create temp directory', 'directorist' );
     1368
     1369                return $status;
     1370            }
     1371
     1372            // Sets file temp destination.
     1373            $file_path = "{$temp_dest}/{$file_name}";
     1374
     1375            set_error_handler( function ( $errno, $errstr, $errfile, $errline ) {
     1376                // error was suppressed with the @-operator
     1377                if ( 0 === error_reporting() ) {
     1378                    return false;
     1379                }
     1380
     1381                throw new ErrorException( $errstr, 0, $errno, $errfile, $errline );
     1382            } );
     1383
     1384            // Copies the file to the final destination and deletes temporary file.
     1385            try {
     1386                copy( $tmp_file, $file_path );
     1387            } catch ( Exception $e ) {
     1388                $status['success'] = false;
     1389                $status['message'] = $e->getMessage();
     1390
     1391                return $status;
     1392            }
     1393
     1394            @unlink( $tmp_file );
     1395            unzip_file( $file_path, $temp_dest );
     1396
     1397            if ( "{$theme_path}/" !== $file_path || $file_path !== $theme_path ) {
     1398                @unlink( $file_path );
     1399            }
     1400
     1401            $extracted_file_dir = glob( "{$temp_dest}/*", GLOB_ONLYDIR );
     1402            $dir_path           = $extracted_file_dir[0];
     1403
     1404            $dir_name  = basename( $dir_path );
     1405            $dest_path = "{$theme_path}/{$dir_name}";
     1406            $zip_files = glob( "{$dir_path}/*.zip" );
     1407
     1408            // If has child theme
     1409            if ( ! empty( $zip_files ) ) {
     1410                $new_temp_dest = "{$temp_dest}/_temp_dest";
     1411                $this->install_themes_from_zip_files( $zip_files, $new_temp_dest, $wp_filesystem );
     1412
     1413                copy_dir( $new_temp_dest, $theme_path );
     1414                $wp_filesystem->delete( $temp_dest, true );
     1415
     1416                $status['success'] = false;
     1417                $status['message'] = __( 'The theme has been downloaded successfully', 'directorist' );
     1418            }
     1419
     1420            // Delete Previous Files If Exists
     1421            if ( $wp_filesystem->exists( $dest_path ) ) {
     1422                $wp_filesystem->delete( $dest_path, true );
     1423            }
     1424
     1425            copy_dir( $temp_dest, $theme_path );
     1426            $wp_filesystem->delete( $temp_dest, true );
     1427
     1428            $status['success'] = true;
     1429            $status['message'] = __( 'The theme has been downloaded successfully', 'directorist' );
     1430
     1431            return $status;
     1432        }
     1433
     1434        // install_theme_from_zip
     1435        public function install_themes_from_zip_files( $zip_files, $temp_dest, $wp_filesystem ) {
     1436            $theme_path = ABSPATH . 'wp-content/themes';
     1437
     1438            foreach ( $zip_files as $zip ) {
     1439                $file     = basename( $zip );
     1440                $dir_name = str_replace( '.zip', '', $file );
     1441
     1442                if ( preg_match( '/[-]child[.]zip$/', $file ) ) {
     1443                    $temp_dest_path = "{$temp_dest}/{$dir_name}";
     1444                    $main_dest_path = "{$theme_path}/{$dir_name}";
     1445
     1446                    // Skip if has child
     1447                    if ( $wp_filesystem->exists( $main_dest_path ) ) {
     1448                        continue;
     1449                    }
     1450
     1451                    $wp_filesystem->mkdir( $temp_dest_path );
     1452                    unzip_file( $zip, $temp_dest_path );
     1453                    // @unlink( $zip );
     1454
     1455                    continue;
     1456                }
     1457
     1458                $main_dest_path = "{$theme_path}/{$dir_name}";
     1459
     1460                if ( $wp_filesystem->exists( $main_dest_path ) ) {
     1461                    $wp_filesystem->delete( $main_dest_path, true );
     1462                }
     1463
     1464                unzip_file( $zip, $temp_dest );
     1465                // @unlink( $zip );
     1466            }
     1467
     1468        }
     1469
     1470        // get_customers_purchased
     1471        public function get_customers_purchased( $license_data ) {
     1472            // Activate the licenses
     1473            $activation_url = 'https://directorist.com';
     1474
     1475            // Activate the Extensions
     1476            $purchased_extensions_meta    = [];
     1477            $purchased_extensions         = [];
     1478            $invalid_purchased_extensions = [];
     1479
     1480            if ( ! empty( $license_data['plugins'] ) ) {
     1481
     1482                foreach ( $license_data['plugins'] as $extension ) {
     1483                    $license              = ( ! empty( $response_body['all_access'] ) ) ? $response_body['active_licenses'][0] : $extension['license'];
     1484                    $extension['license'] = $license;
     1485
     1486                    $activation_status = self::remote_activate_license( $extension, 'plugin' );
     1487
     1488                    if ( empty( $activation_status['success'] ) ) {
     1489                        $invalid_purchased_extensions[] = ['extension' => $extension, 'response' => $activation_status['response']];
     1490                        continue;
     1491                    }
     1492
     1493                    $purchased_extensions[] = $extension;
     1494
     1495                    // Store the ref for db
     1496                    $link    = $extension['permalink'];
     1497                    $ext_key = str_replace( 'http://directorist.com/product/', '', $link );
     1498                    $ext_key = str_replace( 'https://directorist.com/product/', '', $ext_key );
     1499                    $ext_key = str_replace( '/', '', $ext_key );
     1500
     1501                    $purchased_extensions_meta[$ext_key] = [
     1502                        'item_id' => $extension['item_id'],
     1503                        'license' => $extension['license'],
     1504                        'license' => $extension['license'],
     1505                        'file'    => $extension['links'],
     1506                    ];
     1507                }
     1508
     1509            }
     1510
     1511            // Activate the Themes
     1512            $purchased_themes_meta    = [];
     1513            $purchased_themes         = [];
     1514            $invalid_purchased_themes = [];
     1515
     1516            if ( ! empty( $license_data['themes'] ) ) {
     1517
     1518                foreach ( $license_data['themes'] as $theme ) {
     1519                    $license          = ( ! empty( $response_body['all_access'] ) ) ? $response_body['active_licenses'][0] : $theme['license'];
     1520                    $theme['license'] = $license;
     1521
     1522                    $activation_status = self::remote_activate_license( $theme );
     1523
     1524                    if ( empty( $activation_status['success'] ) ) {
     1525                        $invalid_purchased_themes[] = $theme;
     1526                        $invalid_purchased_themes[] = ['extension' => $theme, 'response' => $activation_status['response']];
     1527                        continue;
     1528                    }
     1529
     1530                    $purchased_themes[] = $theme;
     1531
     1532                    // Store the ref for db
     1533                    $link      = $theme['permalink'];
     1534                    $theme_key = str_replace( 'http://directorist.com/product/', '', $link );
     1535                    $theme_key = str_replace( 'https://directorist.com/product/', '', $theme_key );
     1536                    $theme_key = str_replace( '/', '', $theme_key );
     1537
     1538                    $purchased_themes_meta[$theme_key] = [
     1539                        'item_id' => $extension['item_id'],
     1540                        'license' => $extension['license'],
     1541                        'file'    => $extension['links'],
     1542                    ];
     1543                }
     1544
     1545            }
     1546
     1547            $customers_purchased = [
     1548                'extensions' => $purchased_extensions_meta,
     1549                'themes'     => $purchased_themes_meta,
     1550            ];
     1551
     1552            update_user_meta( get_current_user_id(), '_atbdp_purchased_products', $customers_purchased );
     1553
     1554            $status['purchased_extensions']         = $purchased_extensions;
     1555            $status['invalid_purchased_extensions'] = $invalid_purchased_extensions;
     1556
     1557            $status['purchased_themes']         = $purchased_themes;
     1558            $status['invalid_purchased_themes'] = $invalid_purchased_themes;
     1559
     1560            $status['customers_purchased'] = $customers_purchased;
     1561
     1562            return $status;
     1563        }
     1564
     1565        // download_purchased_items
     1566        public function download_purchased_items() {
     1567            $status = ['success' => true, 'log' => []];
     1568
     1569            $cart = ( isset( $_POST['customers_purchased'] ) ) ? wp_unslash( $_POST['customers_purchased'] ) : '';
     1570
     1571            if ( empty( $cart ) ) {
     1572                $status['success']                        = false;
     1573                $status['log']['no_purchased_data_found'] = [
     1574                    'type'    => 'error',
     1575                    'message' => 'No purchased data found',
     1576                ];
     1577                wp_send_json( ['status' => $status] );
     1578            }
     1579
     1580            // Download the extensions
     1581            if ( ! function_exists( 'WP_Filesystem' ) ) {
     1582                include ABSPATH . 'wp-admin/includes/file.php';
     1583            }
     1584
     1585            WP_Filesystem();
     1586
     1587            // Download Extenstions
     1588            if ( ! empty( $cart['purchased_extensions'] ) ) {
     1589                foreach ( $cart['purchased_extensions'] as $extension ) {
     1590                    $download_link = $extension['download_link'];
     1591                    if ( empty( $download_link ) ) {
     1592                        continue;
     1593                    }
     1594
     1595                    $this->download_plugin( ['url' => $download_link, 'init_wp_filesystem' => false] );
     1596                }
     1597
     1598            }
     1599
     1600            // Download Themes
     1601            if ( ! empty( $cart['purchased_themes'] ) ) {
     1602                foreach ( $cart['purchased_themes'] as $theme ) {
     1603                    $download_link = $extension['download_link'];
     1604                    if ( empty( $download_link ) ) {
     1605                        continue;
     1606                    }
     1607
     1608                    $this->download_theme( ['url' => $download_link, 'init_wp_filesystem' => false] );
     1609                }
     1610
     1611            }
     1612
     1613            $status['message'] = 'Download has been completed, redirecting...';
     1614
     1615            wp_send_json( ['status' => $status] );
     1616        }
     1617
     1618        /**
     1619         * It Adds menu item
     1620         */
     1621        public function admin_menu() {
     1622            add_submenu_page(
     1623                'edit.php?post_type=at_biz_dir',
     1624                __( 'Get Extensions', 'directorist' ),
     1625                __( 'Themes & Extensions', 'directorist' ),
     1626                'manage_options',
     1627                'atbdp-extension',
     1628                [$this, 'show_extension_view']
     1629            );
     1630        }
     1631
     1632        // get_extensions_overview
     1633        public function get_extensions_overview() {
     1634            // Get Extensions Details
     1635            $plugin_updates       = get_site_transient( 'update_plugins' );
     1636            $outdated_plugins     = $plugin_updates->response;
     1637            $outdated_plugins_key = ( is_array( $outdated_plugins ) ) ? array_keys( $outdated_plugins ) : [];
     1638            $official_extensions  = is_array( $this->extensions ) ? array_keys( $this->extensions ) : [];
     1639
     1640            $all_installed_plugins_list = get_plugins();
     1641            $installed_extensions       = [];
     1642            $total_active_extensions    = 0;
     1643            $total_outdated_extensions  = 0;
     1644
     1645            foreach ( $all_installed_plugins_list as $plugin_base => $plugin_data ) {
     1646
     1647                $folder_base = strtok( $plugin_base, '/' );
     1648
     1649                if ( preg_match( '/^directorist-/', $plugin_base ) && in_array( $folder_base, $official_extensions ) ) {
     1650                    $installed_extensions[$plugin_base] = $plugin_data;
     1651
     1652                    if ( is_plugin_active( $plugin_base ) ) {
     1653                        $total_active_extensions++;
     1654                    }
     1655
     1656                    if ( in_array( $plugin_base, $outdated_plugins_key ) ) {
     1657                        $total_outdated_extensions++;
     1658                    }
     1659
     1660                }
     1661
     1662            }
     1663
     1664            // ---
     1665            $extensions_available_in_subscriptions = $this->get_extensions_available_in_subscriptions( [
     1666                'installed_extensions' => $installed_extensions,
     1667            ] );
     1668
     1669            // ---
     1670            $extensions_promo_list = $this->get_extensions_promo_list( [
     1671                'extensions_available_in_subscriptions' => $extensions_available_in_subscriptions,
     1672                'installed_extensions'                  => $installed_extensions,
     1673            ] );
     1674
     1675            $required_extensions_list = $this->prepare_the_final_requred_extension_list( [
     1676                'installed_extension_list'              => $installed_extensions,
     1677                'extensions_available_in_subscriptions' => $extensions_available_in_subscriptions,
     1678            ] );
     1679
     1680            $total_installed_ext_list             = count( $installed_extensions );
     1681            $total_ext_available_in_subscriptions = count( $extensions_available_in_subscriptions );
     1682            $total_available_extensions           = $total_installed_ext_list + $total_ext_available_in_subscriptions;
     1683
     1684            $overview = [
     1685                'outdated_plugin_list'                  => $outdated_plugins,
     1686                'outdated_plugins_key'                  => $outdated_plugins_key,
     1687                'all_installed_plugins_list'            => $all_installed_plugins_list,
     1688                'installed_extension_list'              => $installed_extensions,
     1689                'total_active_extensions'               => $total_active_extensions,
     1690                'total_outdated_extensions'             => $total_outdated_extensions,
     1691                'extensions_promo_list'                 => $extensions_promo_list,
     1692                'extensions_available_in_subscriptions' => $extensions_available_in_subscriptions,
     1693                'total_available_extensions'            => $total_available_extensions,
     1694                'required_extensions'                   => $required_extensions_list,
     1695            ];
     1696
     1697            return $overview;
     1698        }
     1699
     1700        // get_extensions_available_in_subscriptions
     1701        public function get_extensions_available_in_subscriptions( array $args = [] ) {
     1702            $installed_extensions      = ( ! empty( $args['installed_extensions'] ) ) ? $args['installed_extensions'] : [];
     1703            $installed_extensions_keys = $this->get_sanitized_extensions_keys( $installed_extensions );
     1704
     1705            $extensions_available_in_subscriptions = self::get_purchased_extension_list();
     1706            $extensions_available_in_subscriptions = ( is_array( $extensions_available_in_subscriptions ) ) ? $extensions_available_in_subscriptions : [];
     1707
     1708            if ( ! empty( $extensions_available_in_subscriptions ) && is_array( $extensions_available_in_subscriptions ) ) {
     1709
     1710                foreach ( $extensions_available_in_subscriptions as $base => $args ) {
     1711                    $base_alias       = $this->get_extension_alias_key( $base );
     1712                    $plugin_key       = preg_replace( '/(directorist-)/', '', $base );
     1713                    $plugin_alias_key = preg_replace( '/(directorist-)/', '', $base_alias );
     1714
     1715                    $is_in_installed_extensions       = in_array( $plugin_key, $installed_extensions_keys ) ? true : false;
     1716                    $is_in_installed_extensions_alias = in_array( $plugin_alias_key, $installed_extensions_keys ) ? true : false;
     1717
     1718                    if ( $is_in_installed_extensions || $is_in_installed_extensions_alias ) {
     1719                        unset( $extensions_available_in_subscriptions[$base] );
     1720                    }
     1721
     1722                }
     1723
     1724            }
     1725
     1726            return $extensions_available_in_subscriptions;
     1727        }
     1728
     1729        // get_extensions_promo_list
     1730        public function get_extensions_promo_list( array $args = [] ) {
     1731            $installed_extensions      = ( ! empty( $args['installed_extensions'] ) ) ? $args['installed_extensions'] : [];
     1732            $installed_extensions_keys = $this->get_sanitized_extensions_keys( $installed_extensions );
     1733
     1734            $extensions_available_in_subscriptions      = ( ! empty( $args['extensions_available_in_subscriptions'] ) ) ? $args['extensions_available_in_subscriptions'] : [];
     1735            $extensions_available_in_subscriptions_keys = is_array( $extensions_available_in_subscriptions ) ? array_keys( $extensions_available_in_subscriptions ) : [];
     1736
     1737            // Filter extensions available in subscriptions
     1738            $promo_extensions = $this->get_active_extensions();
     1739
     1740            if ( ! empty( $promo_extensions ) && is_array( $installed_extensions_keys ) ) {
     1741
     1742                foreach ( $promo_extensions as $_extension_base => $_extension_args ) {
     1743                    $extension_base_alias = $this->get_extension_alias_key( $_extension_base );
     1744                    $ext_key              = preg_replace( '/(directorist-)/', '', $_extension_base );
     1745                    $ext_alias_key        = preg_replace( '/(directorist-)/', '', $extension_base_alias );
     1746
     1747                    // Exclude Installed Extensions
     1748                    $in_installed_extensions       = in_array( $ext_key, $installed_extensions_keys ) ? true : false;
     1749                    $in_installed_extensions_alias = in_array( $ext_alias_key, $installed_extensions_keys ) ? true : false;
     1750
     1751                    if ( $in_installed_extensions || $in_installed_extensions_alias ) {
     1752                        unset( $promo_extensions[$_extension_base] );
     1753                    }
     1754
     1755                    // Exclude Subscripted Extensions
     1756                    $is_available_in_subscriptions       = in_array( $_extension_base, $extensions_available_in_subscriptions_keys ) ? true : false;
     1757                    $is_available_in_subscriptions_alias = in_array( $extension_base_alias, $extensions_available_in_subscriptions_keys ) ? true : false;
     1758
     1759                    if ( $is_available_in_subscriptions || $is_available_in_subscriptions_alias ) {
     1760                        unset( $promo_extensions[$_extension_base] );
     1761                    }
     1762
     1763                }
     1764
     1765            }
     1766
     1767            return $promo_extensions;
     1768        }
     1769
     1770        // get_sanitized_extensions_keys
     1771        public function get_sanitized_extensions_keys( array $extensions_list = [] ) {
     1772            $extensions_keys = ( is_array( $extensions_list ) ) ? array_keys( $extensions_list ) : [];
     1773
     1774            if ( ! empty( $extensions_keys ) && is_array( $extensions_keys ) ) {
     1775
     1776                foreach ( $extensions_keys as $index => $key ) {
     1777                    $new_key = preg_replace( '/\/.+/', '', $key );
     1778                    $new_key = preg_replace( '/(directorist-)/', '', $new_key );
     1779
     1780                    $extensions_keys[$index] = $new_key;
     1781                }
     1782
     1783            }
     1784
     1785            return $extensions_keys;
     1786        }
     1787
     1788        // get_themes_overview
     1789        public function get_themes_overview() {
     1790            // Check form theme update
     1791            $current_theme = wp_get_theme();
     1792            get_theme_update_available( $current_theme->stylesheet );
     1793
     1794            $sovware_themes       = ( is_array( $this->themes ) ) ? array_keys( $this->themes ) : [];
     1795            $theme_updates        = get_site_transient( 'update_themes' );
     1796            $outdated_themes      = $theme_updates->response;
     1797            $outdated_themes_keys = ( is_array( $outdated_themes ) ) ? array_keys( $outdated_themes ) : [];
     1798
     1799            $all_themes            = wp_get_themes();
     1800            $active_theme_slug     = get_option( 'stylesheet' );
     1801            $installed_theme_list  = [];
     1802            $total_active_themes   = 0;
     1803            $total_outdated_themes = 0;
     1804
     1805            foreach ( $all_themes as $theme_base => $theme_data ) {
     1806
     1807                if ( in_array( $theme_base, $sovware_themes ) ) {
     1808                    $customizer_link = "customize.php?theme={$theme_data->stylesheet}&return=%2Fwp-admin%2Fthemes.php";
     1809                    $customizer_link = admin_url( $customizer_link );
     1810
     1811                    $installed_theme_list[$theme_base] = [
     1812                        'name'            => $theme_data->name,
     1813                        'version'         => $theme_data->version,
     1814                        'thumbnail'       => $theme_data->get_screenshot(),
     1815                        'customizer_link' => $customizer_link,
     1816                        'has_update'      => ( in_array( $theme_data->stylesheet, $outdated_themes_keys ) ) ? true : false,
     1817                        'stylesheet'      => $theme_data->stylesheet,
     1818                    ];
     1819
     1820                    if ( $active_theme_slug === $theme_base ) {
     1821                        $total_active_themes++;
     1822                    }
     1823
     1824                    if ( in_array( $theme_base, $outdated_themes_keys ) ) {
     1825                        $total_outdated_themes++;
     1826                    }
     1827
     1828                }
     1829
     1830            }
     1831
     1832            $installed_themes_keys = ( is_array( $installed_theme_list ) ) ? array_keys( $installed_theme_list ) : [];
     1833
     1834            // Themes available in subscriptions
     1835            $themes_available_in_subscriptions = self::get_purchased_theme_list();
     1836            $themes_available_in_subscriptions = ( ! empty( $themes_available_in_subscriptions ) && is_array( $themes_available_in_subscriptions ) ) ? $themes_available_in_subscriptions : [];
     1837
     1838            if ( ! empty( $themes_available_in_subscriptions ) ) {
     1839
     1840                foreach ( $themes_available_in_subscriptions as $base => $args ) {
     1841                    $item = $themes_available_in_subscriptions[$base];
     1842
     1843                    // Merge Local Theme Info
     1844                    if ( ! empty( $this->themes[$base] ) ) {
     1845                        $item = array_merge( $this->themes[$base], $item );
     1846                    }
     1847
     1848                    // Merge Local Theme Info
     1849                    if ( in_array( $base, $installed_themes_keys ) ) {
     1850                        $item = array_merge( $installed_theme_list[$base], $item );
     1851                    }
     1852
     1853                    $is_installed         = ( in_array( $base, $installed_themes_keys ) ) ? true : false;
     1854                    $item['is_installed'] = $is_installed;
     1855
     1856                    $themes_available_in_subscriptions[$base] = $item;
     1857                }
     1858
     1859            }
     1860
     1861            // total_available_themes
     1862            $total_available_themes = count( $themes_available_in_subscriptions );
     1863
     1864            // themes_promo_list
     1865            $themes_promo_list = $this->get_themes_promo_list( [
     1866                'installed_theme_list'              => $installed_theme_list,
     1867                'themes_available_in_subscriptions' => $themes_available_in_subscriptions,
     1868            ] );
     1869
     1870            // current_active_theme_info
     1871            $current_active_theme_info = $this->get_current_active_theme_info(
     1872                [
     1873                    'outdated_themes_keys' => $outdated_themes_keys,
     1874                    'installed_theme_list' => $installed_theme_list,
     1875                ]
     1876            );
     1877            $current_active_theme_info['stylesheet'];
     1878
     1879            $themes_available_in_subscriptions_keys = array_keys( $themes_available_in_subscriptions );
     1880
     1881            if ( in_array( $current_active_theme_info['stylesheet'], $themes_available_in_subscriptions_keys ) ) {
     1882                unset( $themes_available_in_subscriptions[$current_active_theme_info['stylesheet']] );
     1883            }
     1884
     1885            $overview = [
     1886                'total_active_themes'               => $total_active_themes,
     1887                'total_outdated_themes'             => $total_outdated_themes,
     1888                'installed_theme_list'              => $installed_theme_list,
     1889                'current_active_theme_info'         => $current_active_theme_info,
     1890                'themes_promo_list'                 => $themes_promo_list,
     1891                'themes_available_in_subscriptions' => $themes_available_in_subscriptions,
     1892                'total_available_themes'            => $total_available_themes,
     1893            ];
     1894
     1895            return $overview;
     1896        }
     1897
     1898        // get_current_active_theme_info
     1899        public function get_current_active_theme_info( array $args = [] ) {
     1900            // Get Current Active Theme Info
     1901            $current_active_theme = wp_get_theme();
     1902            $customizer_link      = "customize.php?theme={$current_active_theme->stylesheet}&return=%2Fwp-admin%2Fthemes.php";
     1903            $customizer_link      = admin_url( $customizer_link );
     1904
     1905            // Check form theme update
     1906            $has_update = isset( $args[ 'installed_theme_list' ][ $current_active_theme->stylesheet ] ) ? $args[ 'installed_theme_list' ][ $current_active_theme->stylesheet ][ 'has_update' ] : '';
     1907
     1908            $active_theme_info = [
     1909                'name'            => $current_active_theme->name,
     1910                'version'         => $current_active_theme->version,
     1911                'thumbnail'       => $current_active_theme->get_screenshot(),
     1912                'customizer_link' => $customizer_link,
     1913                'has_update'      => $has_update,
     1914                'stylesheet'      => $current_active_theme->stylesheet,
     1915            ];
     1916
     1917            return $active_theme_info;
     1918        }
     1919
     1920        // get_themes_promo_list
     1921        public function get_themes_promo_list( array $args = [] ) {
     1922            $installed_theme_list  = ( ! empty( $args['installed_theme_list'] ) ) ? $args['installed_theme_list'] : [];
     1923            $installed_themes_keys = $this->get_sanitized_themes_keys( $installed_theme_list );
     1924
     1925            $themes_available_in_subscriptions      = ( ! empty( $args['themes_available_in_subscriptions'] ) ) ? $args['themes_available_in_subscriptions'] : [];
     1926            $themes_available_in_subscriptions_keys = is_array( $themes_available_in_subscriptions ) ? array_keys( $themes_available_in_subscriptions ) : [];
     1927
     1928            // Filter all active themes
     1929            $themes_promo_list = $this->get_active_themes();
     1930
     1931            if ( ! empty( $themes_promo_list ) ) {
     1932
     1933                foreach ( $themes_promo_list as $_theme_base => $_extension_args ) {
     1934
     1935                    // Exclude Installed Themes
     1936                    if ( in_array( $_theme_base, $installed_themes_keys ) ) {
     1937                        unset( $themes_promo_list[$_theme_base] );
     1938                    }
     1939
     1940                    // Exclude Subscripted Themes
     1941                    if ( in_array( $_theme_base, $themes_available_in_subscriptions_keys ) ) {
     1942                        unset( $themes_promo_list[$_theme_base] );
     1943                    }
     1944
     1945                }
     1946
     1947            }
     1948
     1949            return $themes_promo_list;
     1950        }
     1951
     1952        // get_sanitized_themes_keys
     1953        public function get_sanitized_themes_keys( array $theme_list = [] ) {
     1954            $theme_keys = ( is_array( $theme_list ) ) ? array_keys( $theme_list ) : [];
     1955
     1956            return $theme_keys;
     1957        }
     1958
     1959        // remote_activate_license
     1960        public static function remote_activate_license( $license_item = [] ) {
     1961            $status = ['success' => false];
     1962
     1963            if ( ! is_array( $license_item ) ) {
     1964                $status['message'] = __( 'Nothing to activate', 'directorist' );
     1965
     1966                return $status;
     1967            }
     1968
     1969            if ( isset( $license_item['skip_licencing'] ) && ! empty( $license_item['skip_licencing'] ) ) {
     1970                $status['success'] = true;
     1971
     1972                return $status;
     1973            }
     1974
     1975            $item_id = ( ! empty( $license_item['item_id'] ) ) ? $license_item['item_id'] : 0;
     1976            $license = ( ! empty( $license_item['license'] ) ) ? $license_item['license'] : '';
     1977
     1978            $activation_url = 'https://directorist.com';
     1979            $query_args     = [
     1980                'edd_action' => 'activate_license',
     1981                'url'        => home_url(),
     1982                'item_id'    => $item_id,
     1983                'license'    => $license,
     1984            ];
     1985
     1986            try {
     1987                $response = wp_remote_get( $activation_url, [
     1988                    'timeout'   => 15,
     1989                    'sslverify' => false,
     1990                    'body'      => $query_args,
     1991                ] );
     1992
     1993                $response_status = json_decode( $response['body'], true );
     1994            } catch ( Exception $e ) {
     1995                $status['success']  = false;
     1996                $status['message']  = $e->getMessage();
     1997                $status['response'] = null;
     1998
     1999                return $status;
     2000            }
     2001
     2002            $status['response'] = $response_status;
     2003
     2004            if ( empty( $response_status['success'] ) ) {
     2005                $status['success'] = false;
     2006                $status['message'] = __( 'Activation failed', 'directorist' );
     2007
     2008                return $status;
     2009            }
     2010
     2011            $status['success'] = true;
     2012
     2013            return $status;
     2014        }
     2015
     2016        // remote_authenticate_user
     2017        public static function remote_authenticate_user( $user_credentials = [] ) {
     2018            $status = ['success' => true];
     2019
     2020            $url     = 'https://directorist.com/wp-json/directorist/v1/licencing';
     2021            $headers = [
     2022                'user-agent' => 'Directorist/' . md5( esc_url( home_url() ) ) . ';',
     2023                'Accept'     => 'application/json',
     2024            ];
     2025
     2026            $config = [
     2027                'method'      => 'GET',
     2028                'timeout'     => 30,
     2029                'redirection' => 5,
     2030                'httpversion' => '1.0',
     2031                'headers'     => $headers,
     2032                'cookies'     => [],
     2033                'body'        => $user_credentials, // [ 'user' => '', 'password' => '']
     2034            ];
     2035
     2036            $response_body = [];
     2037
     2038            try {
     2039                $response = wp_remote_get( $url, $config );
     2040
     2041                if ( is_wp_error( $response ) ) {
     2042                    $status['success'] = false;
     2043                    $status['message'] = Directorist\Helper::get_first_wp_error_message( $response );
     2044                } else {
     2045                    $response_body = ( 'string' === gettype( $response['body'] ) ) ? json_decode( $response['body'], true ) : $response['body'];
     2046                }
     2047
     2048            } catch ( Exception $e ) {
     2049                $status['success'] = false;
     2050                $status['message'] = $e->getMessage();
     2051            }
     2052
     2053            if ( is_array( $response_body ) ) {
     2054                $status = array_merge(  $status, $response_body );
     2055            }
     2056
     2057            if ( empty( $response_body['success'] ) ) {
     2058                $status['success'] = false;
     2059            }
     2060
     2061            $status['response'] = $response_body;
     2062
     2063            return $status;
     2064        }
     2065
     2066        // get_file_download_link
     2067        public static function get_file_download_link( $file_item = [], $product_type = 'plugin' ) {
     2068            if ( ! is_array( $file_item ) ) {
     2069                return '';
     2070            }
     2071
     2072            if ( ! isset( $file_item['item_id'] ) ) {
     2073                return '';
     2074            }
     2075
     2076            if ( ! isset( $file_item['license'] ) ) {
     2077                return '';
     2078            }
     2079
     2080            if ( empty( $file_item['item_id'] ) || empty( $file_item['license'] ) ) {
     2081                return '';
     2082            }
     2083
     2084            $activation_url = 'https://directorist.com/wp-json/directorist/v1/get-product-data/';
     2085            $query_args     = [
     2086                'product_type' => $product_type,
     2087                'license'      => $file_item['license'],
     2088                'item_id'      => $file_item['item_id'],
     2089                'get_info'     => 'download_link',
     2090            ];
     2091
     2092            try {
     2093                $response = wp_remote_get( $activation_url, [
     2094                    'timeout'   => 15,
     2095                    'sslverify' => false,
     2096                    'body'      => $query_args,
     2097                ] );
     2098
     2099                $response = json_decode( $response['body'], true );
     2100            } catch ( Exception $e ) {
     2101                return '';
     2102            }
     2103
     2104            $status['response'] = $response;
     2105
     2106            if ( empty( $response['success'] ) && empty( $response['data'] ) ) {
     2107                return '';
     2108            }
     2109
     2110            return $response['data'];
     2111        }
     2112
     2113        // get_purchased_extension_list
     2114        public static function get_purchased_extension_list() {
     2115            $extensions_available_in_subscriptions = get_user_meta( get_current_user_id(), '_plugins_available_in_subscriptions', true );
     2116            $directorist_purchased_extension_list  = apply_filters( 'directorist_purchased_extension_list', $extensions_available_in_subscriptions );
     2117
     2118            if ( is_array( $directorist_purchased_extension_list ) ) {
     2119                return $directorist_purchased_extension_list;
     2120            }
     2121
     2122            return $extensions_available_in_subscriptions;
     2123        }
     2124
     2125        // get_purchased_theme_list
     2126        public static function get_purchased_theme_list() {
     2127            $themes_available_in_subscriptions = get_user_meta( get_current_user_id(), '_themes_available_in_subscriptions', true );
     2128            $directorist_purchased_theme_list  = apply_filters( 'directorist_purchased_theme_list', $themes_available_in_subscriptions );
     2129
     2130            if ( is_array( $directorist_purchased_theme_list ) ) {
     2131                return $directorist_purchased_theme_list;
     2132            }
     2133
     2134            return $themes_available_in_subscriptions;
     2135        }
     2136
     2137        // filter_product_name
     2138        public static function filter_product_type( $product_type = '' ) {
     2139            if ( 'plugins' === $product_type ) {
     2140                $product_type = 'plugin';
     2141            }
     2142
     2143            if ( 'themes' === $product_type ) {
     2144                $product_type = 'theme';
     2145            }
     2146
     2147            return $product_type;
     2148        }
     2149
     2150        /**
     2151         * It Loads Extension view
     2152         */
     2153        public function show_extension_view() {
     2154            // delete_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion' );
     2155            // delete_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion' );
     2156
     2157            // Check Sassion
     2158            $has_subscriptions_sassion = get_user_meta( get_current_user_id(), '_atbdp_has_subscriptions_sassion', true );
     2159            $is_logged_in              = ( ! empty( $has_subscriptions_sassion ) ) ? true : false;
     2160
     2161            $settings_url = admin_url( 'edit.php?post_type=at_biz_dir&page=atbdp-settings#extension_settings__extensions_general' );
     2162
     2163            $extensions_overview = $this->get_extensions_overview();
     2164            $themes_overview     = $this->get_themes_overview();
     2165
     2166            $hard_logout = apply_filters( 'atbdp_subscriptions_hard_logout', false );
     2167            $hard_logout = ( $hard_logout ) ? 1 : 0;
     2168
     2169            $data = [
     2170                'ATBDP_Extensions'                      => $this,
     2171                'is_logged_in'                          => $is_logged_in,
     2172                'hard_logout'                           => $hard_logout,
     2173
     2174                'total_active_extensions'               => $extensions_overview['total_active_extensions'],
     2175                'total_outdated_extensions'             => $extensions_overview['total_outdated_extensions'],
     2176                'outdated_plugin_list'                  => $extensions_overview['outdated_plugin_list'],
     2177                'installed_extension_list'              => $extensions_overview['installed_extension_list'],
     2178                'extensions_available_in_subscriptions' => $extensions_overview['extensions_available_in_subscriptions'],
     2179                'total_available_extensions'            => $extensions_overview['total_available_extensions'],
     2180                'extensions_promo_list'                 => $extensions_overview['extensions_promo_list'],
     2181                'required_extensions_list'              => $extensions_overview['required_extensions'],
     2182
     2183                'total_active_themes'                   => $themes_overview['total_active_themes'],               // $my_active_themes,
     2184                'total_outdated_themes'                 => $themes_overview['total_outdated_themes'],             // $my_outdated_themes,
     2185                'installed_theme_list'                  => $themes_overview['installed_theme_list'],              // $installed_theme_list,
     2186                'current_active_theme_info'             => $themes_overview['current_active_theme_info'],         // $active_theme,
     2187                'themes_available_in_subscriptions'     => $themes_overview['themes_available_in_subscriptions'], // $themes_available_in_subscriptions,
     2188                'total_available_themes'                => $themes_overview['total_available_themes'],
     2189                'themes_promo_list'                     => $themes_overview['themes_promo_list'],
     2190
     2191                'extension_list'                        => $this->extensions,
     2192                'theme_list'                            => $this->themes,
     2193
     2194                'settings_url'                          => $settings_url,
     2195            ];
     2196
     2197            ATBDP()->load_template( 'admin-templates/theme-extensions/theme-extension', $data );
     2198        }
     2199
     2200        private function is_verified_nonce(){
     2201            $nonce = ! empty( $_POST['nonce'] ) ? wp_unslash( $_POST['nonce'] ) : '';
     2202            return wp_verify_nonce( $nonce, 'atbdp_nonce_action_js' );
     2203        }
     2204
     2205        /**
     2206         * Check the extension is being downloaded from varified source.
     2207         *
     2208         * @param  string $extension_url
     2209         *
     2210         * @return bool
     2211         */
     2212        protected function is_varified_host( $extension_url ) {
     2213            $signed_hostnames = array( 'directorist.com' );
     2214
     2215            return in_array( parse_url( $extension_url, PHP_URL_HOST ), $signed_hostnames, true );
     2216        }
     2217
     2218    }
    22082219
    22092220}
  • directorist/trunk/languages/directorist.pot

    r2730565 r2752034  
    10881088msgstr ""
    10891089
    1090 #: ../includes/classes/class-extension.php:383
     1090#: ../includes/classes/class-extension.php:381
    10911091msgid "All plugins are up to date"
    10921092msgstr ""
    10931093
    1094 #: ../includes/classes/class-extension.php:389
     1094#: ../includes/classes/class-extension.php:387
    10951095msgid "The plugin is up to date"
    10961096msgstr ""
    10971097
    1098 #: ../includes/classes/class-extension.php:410
     1098#: ../includes/classes/class-extension.php:408
    10991099msgid "The plugin has been updated successfully"
    11001100msgstr ""
    11011101
    1102 #: ../includes/classes/class-extension.php:406
     1102#: ../includes/classes/class-extension.php:404
    11031103msgid "The plugin could not update"
    11041104msgstr ""
    11051105
    1106 #: ../includes/classes/class-extension.php:441
     1106#: ../includes/classes/class-extension.php:439
    11071107msgid "Some of the plugin could not update"
    11081108msgstr ""
    11091109
    1110 #: ../includes/classes/class-extension.php:446
     1110#: ../includes/classes/class-extension.php:444
    11111111msgid "All the plugins are updated successfully"
    11121112msgstr ""
    11131113
    1114 #: ../includes/classes/class-extension.php:451
     1114#: ../includes/classes/class-extension.php:449
    11151115msgid "No plugins could not update"
    11161116msgstr ""
    11171117
    1118 #: ../includes/classes/class-extension.php:561
     1118#: ../includes/classes/class-extension.php:559
    11191119msgid "Theme's stylesheet is missing"
    11201120msgstr ""
    11211121
    1122 #: ../includes/classes/class-extension.php:585
     1122#: ../includes/classes/class-extension.php:583
    11231123msgid "Please specefy which plugin to activate"
    11241124msgstr ""
    11251125
    1126 #: ../includes/classes/class-extension.php:624
     1126#: ../includes/classes/class-extension.php:622
    11271127msgid "All themes are up to date"
    11281128msgstr ""
    11291129
    1130 #: ../includes/classes/class-extension.php:630
     1130#: ../includes/classes/class-extension.php:628
    11311131msgid "The theme is up to date"
    11321132msgstr ""
    11331133
    1134 #: ../includes/classes/class-extension.php:644
     1134#: ../includes/classes/class-extension.php:642
    11351135msgid "The theme is already upto date"
    11361136msgstr ""
    11371137
    1138 #: ../includes/classes/class-extension.php:661
     1138#: ../includes/classes/class-extension.php:659
    11391139msgid "The theme has been updated successfully"
    11401140msgstr ""
    11411141
    1142 #: ../includes/classes/class-extension.php:657
     1142#: ../includes/classes/class-extension.php:655
    11431143msgid "The theme could not update"
    11441144msgstr ""
    11451145
    1146 #: ../includes/classes/class-extension.php:698
     1146#: ../includes/classes/class-extension.php:696
    11471147msgid "Some of the theme could not update"
    11481148msgstr ""
    11491149
    1150 #: ../includes/classes/class-extension.php:703
     1150#: ../includes/classes/class-extension.php:701
    11511151msgid "All the themes are updated successfully"
    11521152msgstr ""
    11531153
    1154 #: ../includes/classes/class-extension.php:708
     1154#: ../includes/classes/class-extension.php:706
    11551155msgid "No themes could not update"
    11561156msgstr ""
    11571157
    1158 #: ../includes/classes/class-extension.php:781
     1158#: ../includes/classes/class-extension.php:783
    11591159msgid "Something went wrong"
    11601160msgstr ""
    11611161
    1162 #: ../includes/classes/class-extension.php:857
     1162#: ../includes/classes/class-extension.php:859
    11631163msgid "Password is required"
    11641164msgstr ""
    11651165
    1166 #: ../includes/classes/class-extension.php:868
     1166#: ../includes/classes/class-extension.php:870
    11671167msgid "Sassion is destroyed, please sign-in again"
    11681168msgstr ""
    11691169
    1170 #: ../includes/classes/class-extension.php:900
     1170#: ../includes/classes/class-extension.php:902
    11711171msgid "Your purchase has been refreshed successfuly"
    11721172msgstr ""
    11731173
    1174 #: ../includes/classes/class-extension.php:1057
     1174#: ../includes/classes/class-extension.php:1059
    11751175msgid "Item key is missing"
    11761176msgstr ""
    11771177
    1178 #: ../includes/classes/class-extension.php:1064
     1178#: ../includes/classes/class-extension.php:1066
    11791179msgid "Type not specified"
    11801180msgstr ""
    11811181
    1182 #: ../includes/classes/class-extension.php:1071
     1182#: ../includes/classes/class-extension.php:1073
    11831183msgid "Invalid type"
    11841184msgstr ""
    11851185
    1186 #: ../includes/classes/class-extension.php:1086
     1186#: ../includes/classes/class-extension.php:1088
    11871187msgid "Nothing available in subscriptions"
    11881188msgstr ""
    11891189
    1190 #: ../includes/classes/class-extension.php:1093
     1190#: ../includes/classes/class-extension.php:1095
    11911191msgid "The item is not available in your subscriptions"
    11921192msgstr ""
    11931193
    1194 #: ../includes/classes/class-extension.php:1105
     1194#: ../includes/classes/class-extension.php:1107
    11951195msgid "The license is not valid, please check you subscription."
    11961196msgstr ""
    11971197
    1198 #: ../includes/classes/class-extension.php:1126
     1198#: ../includes/classes/class-extension.php:1128
    11991199msgid "Installed Successfully"
    12001200msgstr ""
    12011201
    1202 #: ../includes/classes/class-extension.php:1170, ../includes/classes/class-extension.php:2012
     1202#: ../includes/classes/class-extension.php:1172, ../includes/classes/class-extension.php:2006
    12031203msgid "Activation failed"
    12041204msgstr ""
    12051205
    1206 #: ../includes/classes/class-extension.php:1206
     1206#: ../includes/classes/class-extension.php:1208
    12071207msgid "Donloaded"
    12081208msgstr ""
    12091209
    1210 #: ../includes/classes/class-extension.php:1224, ../includes/classes/class-extension.php:1333
    1211 msgid "Download link not found"
    1212 msgstr ""
    1213 
    1214 #: ../includes/classes/class-extension.php:1264, ../includes/classes/class-extension.php:1373
     1210#: ../includes/classes/class-extension.php:1222, ../includes/classes/class-extension.php:1327
     1211msgid "Invalid download link"
     1212msgstr ""
     1213
     1214#: ../includes/classes/class-extension.php:1262, ../includes/classes/class-extension.php:1367
    12151215msgid "Could not create temp directory"
    12161216msgstr ""
    12171217
    1218 #: ../includes/classes/class-extension.php:1315
     1218#: ../includes/classes/class-extension.php:1313
    12191219msgid "The plugin has been downloaded successfully"
    12201220msgstr ""
    12211221
    1222 #: ../includes/classes/class-extension.php:1423, ../includes/classes/class-extension.php:1435
     1222#: ../includes/classes/class-extension.php:1417, ../includes/classes/class-extension.php:1429
    12231223msgid "The theme has been downloaded successfully"
    12241224msgstr ""
    12251225
    1226 #: ../includes/classes/class-extension.php:1630
     1226#: ../includes/classes/class-extension.php:1624
    12271227msgid "Get Extensions"
    12281228msgstr ""
    12291229
    1230 #: ../includes/classes/class-extension.php:1631
    1231 msgid "<span>Themes & Extensions</span>"
    1232 msgstr ""
    1233 
    1234 #: ../includes/classes/class-extension.php:1970
     1230#: ../includes/classes/class-extension.php:1625
     1231msgid "Themes & Extensions"
     1232msgstr ""
     1233
     1234#: ../includes/classes/class-extension.php:1964
    12351235msgid "Nothing to activate"
    12361236msgstr ""
  • directorist/trunk/readme.txt

    r2731298 r2752034  
    66Tested up to: 6.0
    77Requires PHP: 7.0
    8 Stable tag: 7.2.2
     8Stable tag: 7.2.3
    99License: GPLv3
    1010License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    310310Directorist Business Directory offers free and cost-effective premium [Directorist Themes](https://directorist.com/theme/) to extend the look & functionalities of your directory listing website.
    311311
     312🔥 **[dHotels](https://directorist.com/product/dhotels/)** - dHotels is mapped out with the aim to allow you to create a hotel directory website exactly the same way you have in your imagination.This hotel theme for WordPress is 100 % mobile responsive. Meaning, it fits any screen from mobile phones to desktops.
     313
    312314🔥 **[dClassified](https://directorist.com/product/dclassified/)** - dClassified is a theme that is specially rigged up to create classified listing websites with aplomb. The theme is clean and simple yet one of the most powerful Classified WordPress themes with a string of useful features that you need to build your classified website.
    313315
     
    338340* dEvent (Theme)
    339341* dJobs (Theme)
    340 * dHotels (Theme)
    341342
    342343Apart from purchasing above mentioned individual extensions or themes, you can get all of the **Directorist Business Directory** extensions and themes in membership as a bonus at a discounted price from the [Directorist Membership](https://directorist.com/pricing/) plan.
     
    396397== Changelog ==
    397398
     3997.2.3 – Jul 05, 2022
     400
     401* Improve - Plugin security
     402
    3984037.2.2 – May 25, 2022
    399404
    400 * Add - WordPress 6.0 compatibility
    401405* Add - WPML extension compatibility
    402406* Add - Auto field mapping on CSV importer
Note: See TracChangeset for help on using the changeset viewer.