Plugin Directory

Changeset 3244826


Ignore:
Timestamp:
02/22/2025 09:44:10 AM (10 months ago)
Author:
totalpressorg
Message:

2.1.0

Location:
cloud-customer-area
Files:
19 added
4 edited

Legend:

Unmodified
Added
Removed
  • cloud-customer-area/trunk/cloud-customer-area.php

    r3076262 r3244826  
    1111Text Domain: cloud-customer-area
    1212Domain Path: /languages/
    13 Version: 2.0.8
     13Version: 2.1.0
    1414*/
    1515
  • cloud-customer-area/trunk/inc/class-main.php

    r3076262 r3244826  
    33namespace CloudCustomerArea\Inc;
    44
    5 if (!defined('ABSPATH')) {
    6     die('Invalid request.');
     5if ( ! defined( 'ABSPATH' ) ) {
     6    die( 'Invalid request.' );
    77}
    88
    9 class Main
    10 {
    11     /**
    12      * @var array
    13      */
    14     public $default_settings = [
    15         'general' => [
    16             'customer_roles' => [],
    17             'customer_dir_name' => 'user_login',
    18             'customer_can_upload' => 0,
    19             'file_types' => [],
    20             'file_size_limit' => '',
    21         ],
    22         'customize' => [
    23             'label_name' => 'Name',
    24             'label_date' => 'Date',
    25             'label_type' => 'Type',
    26             'label_file_size' => 'Size',
    27             'label_download' => 'Download',
    28             'label_select' => 'Choose file to upload',
    29             'label_upload' => 'Upload file',
    30             'label_supported' => '<strong>Supported files:</strong> %s',
    31             'label_size' => '<strong>File size limit:</strong> %s',
    32             'label_err_supported' => 'An error has occurred, file type not supported.',
    33             'label_err_size' => 'An error has occurred, file size exceeds the limit.',
    34             'label_err_generic' => 'An error has occurred, please try again or contact us.',
    35             'label_uploaded' => 'The file was successfully uploaded.',
    36             'label_nofiles' => 'No files available.',
    37             'label_guest' => 'Before accessing this content, <a href="%s">please log in</a>.',
    38             'label_logout' => 'Don\'t forget to <a href="%s">log out</a>.',
    39             'table_bg' => '#fff',
    40             'table_color' => '#555',
    41             'table_border_color' => '#ccc',
    42             'table_thead_bg' => '#eee',
    43             'table_thead_color' => '#000',
    44         ],
    45         'oauth' => [
    46             'id_client' => '',
    47             'client_secret' => '',
    48             'access_token' => ['access_token' => ''],
    49         ],
    50     ];
    51 
    52     /**
    53      * @var null|\CloudCustomerArea\Inc\GoogleDrive
    54      */
    55     private $google_api = null;
    56 
    57     public function __construct()
    58     {
    59         add_action('init', function () {
    60             $client_id = $this->get_settings('id_client', 'oauth');
    61             $client_secret = $this->get_settings('client_secret', 'oauth');
    62             $redirect_url = $this->get_settings('redirect_url', 'oauth');
    63             if ($client_id && $client_secret && $redirect_url) {
    64                 $this->google_api = new \CloudCustomerArea\Inc\GoogleDrive();
    65             }
    66         });
    67     }
    68 
    69     /**
    70      * @param $setting
    71      * @param $type
    72      * @return false|mixed
    73      */
    74     public function get_settings($setting = '', $type = '')
    75     {
    76         if ($setting == 'redirect_url') {
    77             return admin_url('admin.php?page=' . CCA_STRING) . '&action=oauth';
    78         }
    79 
    80         $default_settings = !empty($this->default_settings[$type]) ? $this->default_settings[$type] : false;
    81         $settings = get_option(CCA_SLUG . '_' . $type, $default_settings);
    82 
    83         return !empty($settings[$setting]) ? $settings[$setting] : $default_settings[$setting];
    84     }
    85 
    86     /**
    87      * @param $setting
    88      * @param $type
    89      * @param $value
    90      * @return bool
    91      */
    92     public function update_setting($setting = '', $type = '', $value = '')
    93     {
    94         if (empty($this->default_settings[$type])) {
    95             return false;
    96         }
    97         $settings = get_option(CCA_SLUG . '_' . $type, $this->default_settings[$type]);
    98         $settings[$setting] = $value;
    99         return update_option(CCA_SLUG . '_' . $type, $settings);
    100     }
    101 
    102     /**
    103      * @return bool
    104      */
    105     private function is_pro_version_active()
    106     {
    107         $return = false;
    108         $pro_version = in_array('cloud-customer-area-pro/cloud-customer-area-pro.php', apply_filters('active_plugins', get_option('active_plugins')));
    109         if ($pro_version) {
    110             $return = true;
    111         }
    112         return $return;
    113     }
    114 
    115     /**
    116      * @param $log
    117      * @return void
    118      */
    119     public function write_log($log)
    120     {
    121         if (true === WP_DEBUG && !empty($log)) {
    122             $log_message = "LOG - CLOUD CUSTOMER AREA\n\n" . (is_array($log) || is_object($log) ? print_r($log, true) : $log);
    123             error_log($log_message . "\n\n");
    124         }
    125     }
    126 
    127     /**
    128      * @return bool
    129      */
    130     public function current_user_can()
    131     {
    132         $return = false;
    133         $customer_roles = ['administrator', 'customer_area_user'];
    134         if (is_user_logged_in() && !empty(array_intersect(wp_get_current_user()->roles, $customer_roles))) {
    135             $return = true;
    136         }
    137         return apply_filters('cca_current_user_can', $return);
    138     }
    139 
    140     /**
    141      * @return array
    142      */
    143     public function get_roles()
    144     {
    145         global $wp_roles;
    146         $roles = [];
    147         $all_roles = $wp_roles->roles;
    148         $editable_roles = apply_filters('editable_roles', $all_roles);
    149         foreach ($editable_roles as $key => $role) {
    150             if (!in_array($key, ['administrator', 'customer_area_user'])) {
    151                 $roles[] = [
    152                     'role' => $key,
    153                     'label' => $role['name'],
    154                 ];
    155             }
    156         }
    157         return $roles;
    158     }
    159 
    160     /**
    161      * @return void
    162      */
    163     public function init_plugin()
    164     {
    165         // Plugin
    166         add_action('after_setup_theme', function () {
    167             if (current_user_can('customer_area_user') && !is_admin()) {
    168                 show_admin_bar(false);
    169             }
    170         });
    171         add_filter('plugin_action_links', function ($links, $file) {
    172             if ($file == 'cloud-customer-area/cloud-customer-area.php') {
    173                 $links[] = sprintf('<a href="%s"> %s </a>', menu_page_url(CCA_STRING, false), __('Settings', 'cloud-customer-area'));
    174                 if (!$this->is_pro_version_active()) {
    175                     $links[] = sprintf('<a href="%s" style="font-weight: bold;"> %s </a>', 'https://www.andreadegiovine.it/risorse/plugin/cloud-customer-area?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=cloud_customer_area', __('Get PRO', 'cloud-customer-area'));
    176                 }
    177             }
    178             return $links;
    179         }, 10, 2);
    180         add_action('init', function () {
    181             load_plugin_textdomain(CCA_STRING, false, CCA_PATH . 'languages');
    182         });
    183         add_action('init', function () {
    184             $all_actived_plugins = apply_filters('active_plugins', get_option('active_plugins'));
    185             if (in_array('cloud-customer-area-pro/cloud-customer-area-pro.php', $all_actived_plugins) &&
    186                 version_compare(CCA_PRO_VER, CCA_PRO_MIN_VER, '<')) {
    187                 require_once(ABSPATH . 'wp-admin/includes/plugin.php');
    188                 deactivate_plugins('cloud-customer-area-pro/cloud-customer-area-pro.php');
    189                 wp_die(sprintf(__('"Cloud Customer Area" requires the minimum version %s of "Cloud Customer Area PRO".', 'cloud-customer-area'), CCA_PRO_MIN_VER));
    190             }
    191         });
    192 
    193         // Settings page
    194         add_action('admin_menu', function () {
    195             add_action('admin_init', function () {
    196                 register_setting(CCA_STRING . '-general', CCA_STRING . '-pro_license_key');
    197                 register_setting(CCA_STRING . '-general', CCA_STRING . '-pro_last_license_check');
    198                 register_setting(CCA_STRING . '-general', CCA_SLUG . '_general');
    199                 register_setting(CCA_STRING . '-customize', CCA_SLUG . '_customize');
    200                 register_setting(CCA_STRING . '-oauth', CCA_SLUG . '_oauth');
    201             });
    202             add_menu_page(__('Cloud Customer Area settings', 'cloud-customer-area'), __('Customer Area', 'cloud-customer-area'), 'administrator', CCA_STRING, function () {
    203                 require_once(CCA_PATH . 'part/settings_page.php');
    204             }, 'dashicons-cloud');
    205         });
    206 
    207         // Admin assets
    208         add_action('admin_enqueue_scripts', function () {
    209             wp_register_style('admin-' . CCA_STRING, CCA_URL . 'assets/css/backend.css', false, '1.0.0');
    210             wp_enqueue_style('admin-' . CCA_STRING);
    211         });
    212 
    213         // Frontend assets
    214         add_action('wp_head', function () {
    215             $table_bg = $this->get_settings('table_bg', 'customize');
    216             $table_border_color = $this->get_settings('table_border_color', 'customize');
    217             $table_color = $this->get_settings('table_color', 'customize');
    218             $table_thead_bg = $this->get_settings('table_thead_bg', 'customize');
    219             $table_thead_color = $this->get_settings('table_thead_color', 'customize');
    220             ?>
    221             <style>
    222                 :root {
    223                     --cca-table-bg: <?php echo $table_bg; ?>;
    224                     --cca-table-border-color: <?php echo $table_border_color; ?>;
    225                     --cca-table-text-color: <?php echo $table_color; ?>;
    226                     --cca-table-head-bg: <?php echo $table_thead_bg; ?>;
    227                     --cca-table-head-text-color: <?php echo $table_thead_color; ?>;
    228                     --cca-table-loading-img: url(<?php echo CCA_URL; ?>assets/loading.svg);
    229                     --cca-loading-width: 0%;
    230                 }
    231             </style>
    232             <?php
    233         });
    234         add_action('wp_enqueue_scripts', function () {
    235             wp_register_style('frontend-' . CCA_STRING, CCA_URL . 'assets/css/frontend.css', false, '1.0.0');
    236             wp_enqueue_style('frontend-' . CCA_STRING);
    237 
    238             wp_enqueue_script('frontend-' . CCA_STRING, CCA_URL . 'assets/js/frontend.js', ['jquery'], false, true);
    239             wp_localize_script(
    240                 'frontend-' . CCA_STRING,
    241                 'frontend_' . CCA_SLUG,
    242                 [
    243                     'ajaxurl' => admin_url('admin-ajax.php'),
    244                     'token' => wp_create_nonce(CCA_SLUG . '_token'),
    245                     'download_label' => $this->get_settings('label_download', 'customize'),
    246                     'unload_label' => $this->get_settings('label_download', 'customize'),
    247                     'upload_max_chunk_size' => apply_filters('cca_upload_max_chunk_size', 3) * 1024 * 1024
    248                 ]
    249             );
    250         });
    251 
    252         // Shortcode
    253         add_shortcode('cloud-customer-area', function () {
    254             $output = '';
    255             if ($this->current_user_can()) {
    256                 $output = apply_filters('cca_form_upload', $output);
    257                 $output .= '<table class="' . CCA_STRING . '-table">';
    258                 $output .= '<thead><tr><th>' . $this->get_settings('label_name', 'customize') . '</th><th>' . $this->get_settings('label_date', 'customize') . '</th><th>' . $this->get_settings('label_type', 'customize') . '</th><th>' . $this->get_settings('label_file_size', 'customize') . '</th><th>' . $this->get_settings('label_download', 'customize') . '</th></tr></thead><tbody>';
    259                 $output .= '<tr class="' . CCA_STRING . '-table-loading"><td colspan="5"></td></tr>';
    260                 $output .= '</tbody></table>';
    261                 if (is_user_logged_in()) {
    262                     $output .= '<div class="' . CCA_STRING . '-table-logout">' . sprintf($this->get_settings('label_logout', 'customize'), wp_logout_url($_SERVER['REQUEST_URI'])) . '</div>';
    263                 }
    264             } else {
    265                 $output .= '<div class="' . CCA_STRING . '-guest">' . sprintf($this->get_settings('label_guest', 'customize'), wp_login_url($_SERVER['REQUEST_URI'])) . '</div>';
    266             }
    267             return '<div class="' . CCA_STRING . '-container">' . $output . '</div>';
    268         });
    269 
    270         try {
    271             // Google Drive init + connect action
    272             add_action('init', function () {
    273                 if (is_admin() && $this->google_api && !empty($_GET['page']) && $_GET['page'] == CCA_STRING && !empty($_GET['action']) && $_GET['action'] == 'oauth' && !empty($_GET['code'])) {
    274                     $token = $this->google_api->getToken($_GET['code']);
    275                     if ($token && !empty(json_decode($token)->access_token)) {
    276                         $this->google_api->updateToken(json_decode($token));
    277                     }
    278                     wp_redirect($this->get_settings('redirect_url', 'oauth'));
    279                     exit;
    280                 }
    281             });
    282 
    283             // Ajax actions
    284             add_action('wp_ajax_cca_get_files', [$this, 'ajax_action_get_files']);
    285             add_action('wp_ajax_nopriv_cca_get_files', [$this, 'ajax_action_get_files']);
    286             add_action('wp_ajax_cca_get_file_info', [$this, 'ajax_action_get_file_info']);
    287             add_action('wp_ajax_nopriv_cca_get_file_info', [$this, 'ajax_action_get_file_info']);
    288             add_action('wp_ajax_cca_download_file', [$this, 'ajax_action_download_file']);
    289             add_action('wp_ajax_nopriv_cca_download_file', [$this, 'ajax_action_download_file']);
    290         } catch (\Exception $e) {
    291             wp_die('', '', ['response' => 500]);
    292         }
    293 
    294         // Utilities
    295         $this->applyUpdates();
    296     }
    297 
    298     /**
    299      * @return void
    300      */
    301     public function ajax_action_get_files()
    302     {
    303         $nonce = !empty($_REQUEST['token']) && wp_verify_nonce($_REQUEST['token'], CCA_SLUG . '_token') ? true : false;
    304         $customer_roles = $this->get_settings('customer_roles', 'general');
    305         if ($this->current_user_can() && (!in_array('guest', $customer_roles) ? $nonce : true)) {
    306             $user_folder = $this->get_user_folder();
    307             $user_files = false;
    308             if (!empty($user_folder)) {
    309                 $user_files_request = $this->google_api->listFiles("'" . $user_folder . "' in parents and trashed=false and mimeType!='application/vnd.google-apps.folder'");
    310                 $user_files = $user_files_request && json_decode($user_files_request) ? json_decode($user_files_request) : false;
    311             }
    312             if (!$user_files) {
    313                 $return_output[] = [
    314                     'name' => $this->get_settings('label_err_generic', 'customize'),
    315                     'date' => ' - ',
    316                     'icon' => ' - ',
    317                     'id' => null,
    318                     'size' => false
    319                 ];
    320             } else {
    321                 foreach ($user_files->files as $file) {
    322                     $date = explode('T', $file->modifiedTime)[0];
    323                     $date = date(get_option('date_format'), strtotime($date));
    324                     $return_output[] = [
    325                         'name' => $file->name,
    326                         'date' => $date,
    327                         'icon' => !empty($file->iconLink) ? $file->iconLink : '',
    328                         'id' => $file->id,
    329                         'size' => !empty($file->size) && strpos($file->mimeType, 'application/vnd.google-apps') === false ? number_format(($file->size / (1024 * 1024)), 2, ',', '') . 'MB' : false
    330                     ];
    331                 }
    332                 if (empty($return_output)) {
    333                     $return_output[] = [
    334                         'name' => $this->get_settings('label_nofiles', 'customize'),
    335                         'date' => ' - ',
    336                         'icon' => ' - ',
    337                         'id' => null,
    338                         'size' => false
    339                     ];
    340                 }
    341             }
    342             do_action('cca_files_list_event', $return_output, wp_get_current_user()->ID);
    343             echo json_encode($return_output);
    344         }
    345         exit;
    346     }
    347 
    348     /**
    349      * @return void
    350      */
    351     public function ajax_action_get_file_info()
    352     {
    353         $nonce = !empty($_REQUEST['token']) && wp_verify_nonce($_REQUEST['token'], CCA_SLUG . '_token') ? true : false;
    354         $fileId = !empty($_REQUEST['file']) && is_string($_REQUEST['file']) ? $_REQUEST['file'] : false;
    355         $customer_roles = $this->get_settings('customer_roles', 'general');
    356         if ($this->current_user_can() && (!in_array('guest', $customer_roles) ? $nonce : true) && $fileId) {
    357 
    358             $fileInfo = $this->google_api->getFileInfo($fileId);
    359 
    360             if (!$fileInfo || empty(json_decode($fileInfo)->name)) {
    361                 wp_send_json_error($this->get_settings('label_err_generic', 'customize'));
    362             }
    363 
    364             $fileInfo = json_decode($fileInfo);
    365             $fileName = $fileInfo->name;
    366             $fileMime = $fileInfo->mimeType;
    367             $fileParts = 1;
    368             $fileMethod = 'download';
    369 
    370             if (strpos($fileInfo->mimeType, 'application/vnd.google-apps') !== false) {
    371                 // Is Google App file
    372                 $fileType = str_replace('application/vnd.google-apps.', '', $fileInfo->mimeType);
    373                 $fileExportTypeMap = [
    374                     'audio' => [
    375                         'mime' => 'audio/mpeg',
    376                         'ext' => '.mp3'
    377                     ],
    378                     'document' => [
    379                         'mime' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    380                         'ext' => '.docx'
    381                     ],
    382                     'drawing' => [
    383                         'mime' => 'application/pdf',
    384                         'ext' => '.pdf'
    385                     ],
    386                     'file' => [
    387                         'mime' => 'application/octet-stream',
    388                         'ext' => ''
    389                     ],
    390                     'presentation' => [
    391                         'mime' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    392                         'ext' => '.pptx'
    393                     ],
    394                     'spreadsheet' => [
    395                         'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    396                         'ext' => '.xlsx'
    397                     ]
    398                 ];
    399                 if (empty($fileExportTypeMap[$fileType])) {
    400                     wp_send_json_error(__('File mime not supported.', 'cloud-customer-area'));
    401                 }
    402                 $fileName = $fileInfo->name . $fileExportTypeMap[$fileType]['ext'];
    403                 $fileMime = $fileExportTypeMap[$fileType]['mime'];
    404                 $fileMethod = 'export';
    405             } else {
    406                 $chunkSizeBytes = apply_filters('cca_download_max_chunk_size', 3) * 1024 * 1024;
    407                 $fileParts = ceil($fileInfo->size / $chunkSizeBytes);
    408             }
    409             $fileName = apply_filters('cca_download_file_name', $fileName);
    410             do_action('cca_download_event', $fileName, wp_get_current_user()->ID);
    411             $return = [
    412                 'name' => $fileName,
    413                 'mime' => $fileMime,
    414                 'parts' => $fileParts,
    415                 'method' => $fileMethod
    416             ];
    417             echo json_encode($return);
    418         }
    419         exit;
    420     }
    421 
    422     /**
    423      * @return void
    424      */
    425     public function ajax_action_download_file()
    426     {
    427         $nonce = !empty($_REQUEST['token']) && wp_verify_nonce($_REQUEST['token'], CCA_SLUG . '_token') ? true : false;
    428         $fileId = !empty($_REQUEST['file']) && is_string($_REQUEST['file']) ? $_REQUEST['file'] : false;
    429         $method = !empty($_REQUEST['method']) && is_string($_REQUEST['method']) ? $_REQUEST['method'] : false;
    430         $mime = !empty($_REQUEST['mime']) && is_string($_REQUEST['mime']) ? $_REQUEST['mime'] : false;
    431         $part = !empty($_REQUEST['part']) && is_string($_REQUEST['part']) ? $_REQUEST['part'] : false;
    432         $customer_roles = $this->get_settings('customer_roles', 'general');
    433         if ($this->current_user_can() && (!in_array('guest', $customer_roles) ? $nonce : true) && $fileId && $method && $mime && $part) {
    434             $content = $this->download_file($fileId, $method, $mime, $part);
    435             header('content-type: "text/plain; charset=us-ascii"');
    436             echo $content;
    437         }
    438         exit;
    439     }
    440 
    441     /**
    442      * @param $fileId
    443      * @param $method
    444      * @param $mime
    445      * @param $part
    446      * @return false|string
    447      */
    448     private function download_file($fileId, $method, $mime, $part)
    449     {
    450         if ($method == 'export') {
    451             $response = $this->google_api->exportFile($fileId, $mime);
    452         } else {
    453             $response = $this->google_api->downloadFile($fileId, $part);
    454         }
    455         if (strlen($response) < 1) {
    456             return $this->download_file($fileId, $method, $mime, $part);
    457         }
    458         return $response;
    459     }
    460 
    461     /**
    462      * @return mixed
    463      */
    464     public function get_user_folder()
    465     {
    466         $current_user = wp_get_current_user();
    467         $user_id = $current_user->ID;
    468 
    469         if ($user_id !== 0) {
    470             $folder_id = get_user_meta($user_id, CCA_SLUG . '_folder', true);
    471         } else {
    472             $folder_id = get_option(CCA_SLUG . '_guest_folder', '');
    473         }
    474         if (!empty($folder_id)) {
    475             $folder_info_request = $this->google_api->getFileInfo($folder_id);
    476             if (404 == $folder_info_request) {
    477                 if ($user_id !== 0) {
    478                     update_user_meta($user_id, CCA_SLUG . '_folder', '');
    479                 } else {
    480                     update_option(CCA_SLUG . '_guest_folder', '');
    481                 }
    482                 return $this->get_user_folder();
    483             }
    484             if (!$folder_info_request) {
    485                 return $this->get_user_folder();
    486             }
    487             if (!empty(json_decode($folder_info_request)->name)) {
    488                 return $folder_id;
    489             }
    490         }
    491 
    492         if ($user_id !== 0) {
    493             $new_folder_use = $this->get_settings('customer_dir_name', 'general');
    494             if (!in_array($new_folder_use, ['user_login', 'display_name', 'user_email'])) {
    495                 $new_folder_use = 'user_login';
    496             }
    497             $new_folder_name = $current_user->$new_folder_use;
    498         } else {
    499             $new_folder_name = 'guest-users';
    500         }
    501 
    502         $new_folder_name = apply_filters('cca_new_folder_name', $new_folder_name, $user_id);
    503         do_action('cca_folder_creation_event', $new_folder_name, $user_id);
    504 
    505         $create_folder_request = $this->google_api->createFolder($new_folder_name);
    506 
    507         $created_folder_id = $create_folder_request && !empty(json_decode($create_folder_request)->id) ? json_decode($create_folder_request)->id : false;
    508 
    509         if ($created_folder_id) {
    510             if ($user_id !== 0) {
    511                 update_user_meta($current_user->ID, CCA_SLUG . '_folder', $created_folder_id);
    512             } else {
    513                 update_option(CCA_SLUG . '_guest_folder', $created_folder_id);
    514             }
    515         } else {
    516             return $this->get_user_folder();
    517         }
    518 
    519         return $created_folder_id;
    520     }
    521 
    522     private function applyUpdates()
    523     {
    524         $installedVersion = get_option('cloud_customer_area_version', null);
    525         $currentVersion = CCA_VER;
    526 
    527         if (version_compare($installedVersion, $currentVersion, '=')) {
    528             return;
    529         }
    530 
    531         if (version_compare($installedVersion, $currentVersion, '<')) {
    532             // Apply updates
    533         }
    534 
    535         update_option('cloud_customer_area_version', $currentVersion);
    536         update_option('cloud_customer_area_installation_time', time());
    537 
    538         if(!empty($installedVersion)){
    539             $request_url = add_query_arg(
    540                 ['id' => 470, 'action' => 'updated', 'domain' => md5(get_home_url()), 'v' => $currentVersion],
    541                 'https://totalpress.org/wp-json/totalpress/v1/plugin-growth'
    542             );
    543             wp_remote_get($request_url);
    544         }
    545     }
     9class Main {
     10    /**
     11     * @var array
     12     */
     13    public $default_settings = array(
     14        'general'   => array(
     15            'customer_roles'      => array(),
     16            'customer_dir_name'   => 'user_login',
     17            'customer_can_upload' => 0,
     18            'file_types'          => array(),
     19            'file_size_limit'     => '',
     20            'subfolder_files'     => 0
     21        ),
     22        'customize' => array(
     23            'label_name'          => 'Name',
     24            'label_date'          => 'Date',
     25            'label_type'          => 'Type',
     26            'label_file_size'     => 'Size',
     27            'label_download'      => 'Download',
     28            'label_select'        => 'Choose file to upload',
     29            'label_upload'        => 'Upload file',
     30            'label_supported'     => '<strong>Supported files:</strong> %s',
     31            'label_size'          => '<strong>File size limit:</strong> %s',
     32            'label_err_supported' => 'An error has occurred, file type not supported.',
     33            'label_err_size'      => 'An error has occurred, file size exceeds the limit.',
     34            'label_err_generic'   => 'An error has occurred, please try again or contact us.',
     35            'label_uploaded'      => 'The file was successfully uploaded.',
     36            'label_nofiles'       => 'No files available.',
     37            'label_guest'         => 'Before accessing this content, <a href="%s">please log in</a>.',
     38            'label_logout'        => 'Don\'t forget to <a href="%s">log out</a>.',
     39            'table_bg'            => '#fff',
     40            'table_color'         => '#555',
     41            'table_border_color'  => '#ccc',
     42            'table_thead_bg'      => '#eee',
     43            'table_thead_color'   => '#000',
     44        ),
     45        'oauth'     => array(
     46            'id_client'     => '',
     47            'client_secret' => '',
     48            'access_token'  => array( 'access_token' => '' ),
     49        ),
     50    );
     51
     52    /**
     53     * @var null|\CloudCustomerArea\Inc\GoogleDrive
     54     */
     55    private $google_api = null;
     56
     57    public function __construct() {
     58        add_action(
     59            'init',
     60            function () {
     61                $client_id     = $this->get_settings( 'id_client', 'oauth' );
     62                $client_secret = $this->get_settings( 'client_secret', 'oauth' );
     63                $redirect_url  = $this->get_settings( 'redirect_url', 'oauth' );
     64                if ( $client_id && $client_secret && $redirect_url ) {
     65                    $this->google_api = new \CloudCustomerArea\Inc\GoogleDrive();
     66                }
     67            }
     68        );
     69    }
     70
     71    /**
     72     * @param $setting
     73     * @param $type
     74     * @return false|mixed
     75     */
     76    public function get_settings( $setting = '', $type = '' ) {
     77        if ( 'redirect_url' === $setting ) {
     78            return admin_url( 'admin.php?page=' . CCA_STRING ) . '&action=oauth';
     79        }
     80
     81        $default_settings = ! empty( $this->default_settings[ $type ] ) ? $this->default_settings[ $type ] : false;
     82        $settings         = get_option( CCA_SLUG . '_' . $type, $default_settings );
     83
     84        return ! empty( $settings[ $setting ] ) ? $settings[ $setting ] : $default_settings[ $setting ];
     85    }
     86
     87    /**
     88     * @param $setting
     89     * @param $type
     90     * @param $value
     91     * @return bool
     92     */
     93    public function update_setting( $setting = '', $type = '', $value = '' ) {
     94        if ( empty( $this->default_settings[ $type ] ) ) {
     95            return false;
     96        }
     97        $settings             = get_option( CCA_SLUG . '_' . $type, $this->default_settings[ $type ] );
     98        $settings[ $setting ] = $value;
     99        return update_option( CCA_SLUG . '_' . $type, $settings );
     100    }
     101
     102    /**
     103     * @return bool
     104     */
     105    private function is_pro_version_active() {
     106        $return      = false;
     107        $pro_version = in_array( 'cloud-customer-area-pro/cloud-customer-area-pro.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true );
     108        if ( $pro_version ) {
     109            $return = true;
     110        }
     111        return $return;
     112    }
     113
     114    /**
     115     * @param $log
     116     * @return void
     117     */
     118    public function write_log( $log ) {
     119        if ( true === WP_DEBUG && ! empty( $log ) ) {
     120            $log_message = "LOG - CLOUD CUSTOMER AREA\n\n" . ( is_array( $log ) || is_object( $log ) ? print_r( $log, true ) : $log );
     121            error_log( $log_message . "\n\n" );
     122        }
     123    }
     124
     125    /**
     126     * @return bool
     127     */
     128    public function current_user_can() {
     129        $return         = false;
     130        $customer_roles = array( 'administrator', 'customer_area_user' );
     131        if ( is_user_logged_in() && ! empty( array_intersect( wp_get_current_user()->roles, $customer_roles ) ) ) {
     132            $return = true;
     133        }
     134        return apply_filters( 'cca_current_user_can', $return );
     135    }
     136
     137    /**
     138     * @return array
     139     */
     140    public function get_roles() {
     141        global $wp_roles;
     142        $roles          = array();
     143        $all_roles      = $wp_roles->roles;
     144        $editable_roles = apply_filters( 'editable_roles', $all_roles );
     145        foreach ( $editable_roles as $key => $role ) {
     146            if ( ! in_array( $key, array( 'administrator', 'customer_area_user' ), true ) ) {
     147                $roles[] = array(
     148                    'role'  => $key,
     149                    'label' => $role['name'],
     150                );
     151            }
     152        }
     153        return $roles;
     154    }
     155
     156    /**
     157     * @return void
     158     */
     159    public function init_plugin() {
     160        // Plugin
     161        add_action(
     162            'after_setup_theme',
     163            function () {
     164                if ( current_user_can( 'customer_area_user' ) && ! is_admin() ) {
     165                    show_admin_bar( false );
     166                }
     167            }
     168        );
     169        add_filter(
     170            'plugin_action_links',
     171            function ( $links, $file ) {
     172                if ( 'cloud-customer-area/cloud-customer-area.php' === $file ) {
     173                    $links[] = sprintf( '<a href="%s"> %s </a>', menu_page_url( CCA_STRING, false ), __( 'Settings', 'cloud-customer-area' ) );
     174                    if ( ! $this->is_pro_version_active() ) {
     175                        $links[] = sprintf( '<a href="%s" style="font-weight: bold;"> %s </a>', 'https://www.andreadegiovine.it/risorse/plugin/cloud-customer-area?utm_source=tools_plugin_page&utm_medium=plugin_page&utm_campaign=cloud_customer_area', __( 'Get PRO', 'cloud-customer-area' ) );
     176                    }
     177                }
     178                return $links;
     179            },
     180            10,
     181            2
     182        );
     183        add_action(
     184            'init',
     185            function () {
     186                load_plugin_textdomain( CCA_STRING, false, CCA_PATH . 'languages' );
     187            }
     188        );
     189        add_action(
     190            'init',
     191            function () {
     192                $all_actived_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
     193                if ( in_array( 'cloud-customer-area-pro/cloud-customer-area-pro.php', $all_actived_plugins, true ) &&
     194                version_compare( CCA_PRO_VER, CCA_PRO_MIN_VER, '<' ) ) {
     195                    require_once ABSPATH . 'wp-admin/includes/plugin.php';
     196                    deactivate_plugins( 'cloud-customer-area-pro/cloud-customer-area-pro.php' );
     197                    wp_die( sprintf( __( '"Cloud Customer Area" requires the minimum version %s of "Cloud Customer Area PRO".', 'cloud-customer-area' ), CCA_PRO_MIN_VER ) );
     198                }
     199            }
     200        );
     201
     202        // Settings page
     203        add_action(
     204            'admin_menu',
     205            function () {
     206                add_action(
     207                    'admin_init',
     208                    function () {
     209                        register_setting( CCA_STRING . '-general', CCA_STRING . '-pro_license_key' );
     210                        register_setting( CCA_STRING . '-general', CCA_STRING . '-pro_last_license_check' );
     211                        register_setting( CCA_STRING . '-general', CCA_SLUG . '_general' );
     212                        register_setting( CCA_STRING . '-customize', CCA_SLUG . '_customize' );
     213                        register_setting( CCA_STRING . '-oauth', CCA_SLUG . '_oauth' );
     214                    }
     215                );
     216                add_menu_page(
     217                    __( 'Cloud Customer Area settings', 'cloud-customer-area' ),
     218                    __( 'Customer Area', 'cloud-customer-area' ),
     219                    'administrator',
     220                    CCA_STRING,
     221                    function () {
     222                        require_once CCA_PATH . 'part/settings_page.php';
     223                    },
     224                    'dashicons-cloud'
     225                );
     226            }
     227        );
     228        add_action( 'show_user_profile', array( $this, 'edit_user_screen' ) );
     229        add_action( 'edit_user_profile', array( $this, 'edit_user_screen' ) );
     230        add_action( 'init', array( $this, 'edit_user_action' ) );
     231
     232        // Admin assets
     233        add_action(
     234            'admin_enqueue_scripts',
     235            function () {
     236                wp_register_style( 'admin-' . CCA_STRING, CCA_URL . 'assets/css/backend.css', false, '1.0.0' );
     237                wp_enqueue_style( 'admin-' . CCA_STRING );
     238            }
     239        );
     240
     241        // Frontend assets
     242        add_action(
     243            'wp_head',
     244            function () {
     245                $table_bg           = $this->get_settings( 'table_bg', 'customize' );
     246                $table_border_color = $this->get_settings( 'table_border_color', 'customize' );
     247                $table_color        = $this->get_settings( 'table_color', 'customize' );
     248                $table_thead_bg     = $this->get_settings( 'table_thead_bg', 'customize' );
     249                $table_thead_color  = $this->get_settings( 'table_thead_color', 'customize' );
     250                ?>
     251            <style>
     252                :root {
     253                    --cca-table-bg: <?php echo $table_bg; ?>;
     254                    --cca-table-border-color: <?php echo $table_border_color; ?>;
     255                    --cca-table-text-color: <?php echo $table_color; ?>;
     256                    --cca-table-head-bg: <?php echo $table_thead_bg; ?>;
     257                    --cca-table-head-text-color: <?php echo $table_thead_color; ?>;
     258                    --cca-table-loading-img: url(<?php echo CCA_URL; ?>assets/loading.svg);
     259                    --cca-loading-width: 0%;
     260                }
     261            </style>
     262                <?php
     263            }
     264        );
     265        add_action(
     266            'wp_enqueue_scripts',
     267            function () {
     268                wp_register_style( 'frontend-' . CCA_STRING, CCA_URL . 'assets/css/frontend.css', false, '1.0.0' );
     269                wp_enqueue_style( 'frontend-' . CCA_STRING );
     270
     271                wp_enqueue_script( 'frontend-' . CCA_STRING, CCA_URL . 'assets/js/frontend.js', array( 'jquery' ), false, true );
     272                wp_localize_script(
     273                    'frontend-' . CCA_STRING,
     274                    'frontend_' . CCA_SLUG,
     275                    array(
     276                        'ajaxurl'               => admin_url( 'admin-ajax.php' ),
     277                        'token'                 => wp_create_nonce( CCA_SLUG . '_token' ),
     278                        'download_label'        => $this->get_settings( 'label_download', 'customize' ),
     279                        'unload_label'          => $this->get_settings( 'label_download', 'customize' ),
     280                        'upload_max_chunk_size' => apply_filters( 'cca_upload_max_chunk_size', 3 ) * 1024 * 1024,
     281                    )
     282                );
     283            }
     284        );
     285
     286        // Shortcode
     287        add_shortcode(
     288            'cloud-customer-area',
     289            function () {
     290                $output = '';
     291                if ( $this->current_user_can() ) {
     292                    $output  = apply_filters( 'cca_form_upload', $output );
     293                    $output .= '<table class="' . CCA_STRING . '-table">';
     294                    $output .= '<thead><tr><th>' . $this->get_settings( 'label_name', 'customize' ) . '</th><th>' . $this->get_settings( 'label_date', 'customize' ) . '</th><th>' . $this->get_settings( 'label_type', 'customize' ) . '</th><th>' . $this->get_settings( 'label_file_size', 'customize' ) . '</th><th>' . $this->get_settings( 'label_download', 'customize' ) . '</th></tr></thead><tbody>';
     295                    $output .= '<tr class="' . CCA_STRING . '-table-loading"><td colspan="5"></td></tr>';
     296                    $output .= '</tbody></table>';
     297                    if ( is_user_logged_in() ) {
     298                        $output .= '<div class="' . CCA_STRING . '-table-logout">' . sprintf( $this->get_settings( 'label_logout', 'customize' ), wp_logout_url( $_SERVER['REQUEST_URI'] ) ) . '</div>';
     299                    }
     300                } else {
     301                    $output .= '<div class="' . CCA_STRING . '-guest">' . sprintf( $this->get_settings( 'label_guest', 'customize' ), wp_login_url( $_SERVER['REQUEST_URI'] ) ) . '</div>';
     302                }
     303                return '<div class="' . CCA_STRING . '-container">' . $output . '</div>';
     304            }
     305        );
     306
     307        try {
     308            // Google Drive init + connect action
     309            add_action(
     310                'init',
     311                function () {
     312                    if ( is_admin() && $this->google_api && ! empty( $_GET['page'] ) && CCA_STRING === $_GET['page'] && ! empty( $_GET['action'] ) && 'oauth' === $_GET['action'] && ! empty( $_GET['code'] ) ) {
     313                        $token = $this->google_api->getToken( $_GET['code'] );
     314                        if ( $token && ! empty( json_decode( $token )->access_token ) ) {
     315                            $this->google_api->updateToken( json_decode( $token ) );
     316                        }
     317                        wp_safe_redirect( $this->get_settings( 'redirect_url', 'oauth' ) );
     318                        exit;
     319                    }
     320                }
     321            );
     322
     323            // Ajax actions
     324            add_action( 'wp_ajax_cca_get_files', array( $this, 'ajax_action_get_files' ) );
     325            add_action( 'wp_ajax_nopriv_cca_get_files', array( $this, 'ajax_action_get_files' ) );
     326            add_action( 'wp_ajax_cca_get_file_info', array( $this, 'ajax_action_get_file_info' ) );
     327            add_action( 'wp_ajax_nopriv_cca_get_file_info', array( $this, 'ajax_action_get_file_info' ) );
     328            add_action( 'wp_ajax_cca_download_file', array( $this, 'ajax_action_download_file' ) );
     329            add_action( 'wp_ajax_nopriv_cca_download_file', array( $this, 'ajax_action_download_file' ) );
     330        } catch ( \Exception $e ) {
     331            wp_die( '', '', array( 'response' => 500 ) );
     332        }
     333
     334        // Utilities
     335        $this->apply_updates();
     336    }
     337
     338    /**
     339     * @param $user
     340     *
     341     * @return void
     342     */
     343    public function edit_user_screen( $user ) {
     344        $user_id   = $user->ID;
     345        $folder_id = get_user_meta( $user_id, CCA_SLUG . '_folder', true );
     346        ?>
     347        <h2><?php esc_html_e( 'Cloud Customer Area settings', 'cloud-customer-area' ); ?></h2>
     348
     349        <table class="form-table" role="presentation">
     350        <tr id="password" class="user-pass1-wrap">
     351            <th><?php esc_html_e( 'User folder', 'cloud-customer-area' ); ?></th>
     352            <td>
     353                <?php
     354                if ( $folder_id ) {
     355                    printf( '<a class="button" href="https://drive.google.com/drive/folders/%s" target="_blank">%s</a>', $folder_id, __( 'Open on Google Drive', 'cloud-customer-area' ) );
     356                    printf(
     357                        ' <a class="button button-primary" href="%s">%s</a>',
     358                        add_query_arg(
     359                            array(
     360                                'user_id' => $user_id,
     361                                'action'  => 'detach',
     362                                'token'   => wp_create_nonce( CCA_SLUG . '_action' ),
     363                            )
     364                        ),
     365                        __( 'Detach', 'cloud-customer-area' )
     366                    );
     367                } else {
     368                    printf(
     369                        ' <a class="button button-primary" href="%s">%s</a>',
     370                        add_query_arg(
     371                            array(
     372                                'user_id' => $user_id,
     373                                'action'  => 'create',
     374                                'token'   => wp_create_nonce( CCA_SLUG . '_action' ),
     375                            )
     376                        ),
     377                        __( 'Create on Google Drive', 'cloud-customer-area' )
     378                    );
     379                }
     380                ?>
     381            </td>
     382        </tr>
     383        </table>
     384        <?php
     385    }
     386
     387    /**
     388     * @return void
     389     */
     390    public function edit_user_action() {
     391        $nonce   = ! empty( $_GET['token'] ) && wp_verify_nonce( $_GET['token'], CCA_SLUG . '_action' ) ? true : false;
     392        $action  = ! empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'detach', 'create' ), true ) ? $_GET['action'] : false;
     393        $user_id = ! empty( $_GET['user_id'] ) && get_user( $_GET['user_id'] ) ? $_GET['user_id'] : false;
     394        if ( $nonce && $action && $user_id ) {
     395            $redirect = remove_query_arg( array( 'action', 'token' ) );
     396            switch ( $action ) {
     397                case 'detach':
     398                    update_user_meta( $user_id, CCA_SLUG . '_folder', '' );
     399                    break;
     400                case 'create':
     401                    $user      = get_user( $user_id );
     402                    $folder_id = $this->get_user_folder( $user );
     403                    break;
     404            }
     405            wp_safe_redirect( $redirect );
     406        }
     407    }
     408
     409    /**
     410     * @param $folder_id
     411     * @param $parent_name
     412     *
     413     * @return array
     414     */
     415    private function get_folder_files( $folder_id, $parent_name = null ) {
     416        $result        = array();
     417        $files_request = $this->google_api->listFiles( "'" . $folder_id . "' in parents and trashed=false" );
     418        $files         = $files_request && json_decode( $files_request ) ? json_decode( $files_request ) : false;
     419        if ( $files ) {
     420            foreach ( $files->files as $file ) {
     421                if ( ! empty( $parent_name ) ) {
     422                    $file->name = $parent_name . ' > ' . $file->name;
     423                }
     424                if ( 'application/vnd.google-apps.folder' !== $file->mimeType ) {
     425                    $result[] = $file;
     426                } elseif ( $this->is_pro_version_active() && "1" === $this->get_settings( 'subfolder_files', 'general' ) ) {
     427                        $sub_folder_files = $this->get_folder_files( $file->id, $file->name );
     428                        $result           = array_merge( $result, $sub_folder_files );
     429                }
     430            }
     431        }
     432        return $result;
     433    }
     434
     435    /**
     436     * @return void
     437     */
     438    public function ajax_action_get_files() {
     439        $nonce          = ! empty( $_REQUEST['token'] ) && wp_verify_nonce( $_REQUEST['token'], CCA_SLUG . '_token' ) ? true : false;
     440        $customer_roles = $this->get_settings( 'customer_roles', 'general' );
     441        if ( $this->current_user_can() && ( ! in_array( 'guest', $customer_roles, true ) ? $nonce : true ) ) {
     442            $user_folder = $this->get_user_folder();
     443            $user_files  = false;
     444            if ( ! empty( $user_folder ) ) {
     445                $user_files = $this->get_folder_files( $user_folder );
     446            }
     447            if ( empty( $user_files ) ) {
     448                $return_output[] = array(
     449                    'name' => $this->get_settings( 'label_err_generic', 'customize' ),
     450                    'date' => ' - ',
     451                    'icon' => ' - ',
     452                    'id'   => null,
     453                    'size' => false,
     454                );
     455            } else {
     456                foreach ( $user_files as $file ) {
     457                    $date            = explode( 'T', $file->modifiedTime )[0];
     458                    $date            = date( get_option( 'date_format' ), strtotime( $date ) );
     459                    $return_output[] = array(
     460                        'name' => $file->name,
     461                        'date' => $date,
     462                        'icon' => ! empty( $file->iconLink ) ? $file->iconLink : '',
     463                        'id'   => $file->id,
     464                        'size' => ! empty( $file->size ) && strpos( $file->mimeType, 'application/vnd.google-apps' ) === false ? number_format( ( $file->size / ( 1024 * 1024 ) ), 2, ',', '' ) . 'MB' : false,
     465                    );
     466                }
     467                if ( empty( $return_output ) ) {
     468                    $return_output[] = array(
     469                        'name' => $this->get_settings( 'label_nofiles', 'customize' ),
     470                        'date' => ' - ',
     471                        'icon' => ' - ',
     472                        'id'   => null,
     473                        'size' => false,
     474                    );
     475                }
     476            }
     477            do_action( 'cca_files_list_event', $return_output, wp_get_current_user()->ID );
     478            echo wp_json_encode( $return_output );
     479        }
     480        exit;
     481    }
     482
     483    /**
     484     * @return void
     485     */
     486    public function ajax_action_get_file_info() {
     487        $nonce          = ! empty( $_REQUEST['token'] ) && wp_verify_nonce( $_REQUEST['token'], CCA_SLUG . '_token' ) ? true : false;
     488        $file_id        = ! empty( $_REQUEST['file'] ) && is_string( $_REQUEST['file'] ) ? $_REQUEST['file'] : false;
     489        $customer_roles = $this->get_settings( 'customer_roles', 'general' );
     490        if ( $this->current_user_can() && ( ! in_array( 'guest', $customer_roles, true ) ? $nonce : true ) && $file_id ) {
     491
     492            $file_info = $this->google_api->getFileInfo( $file_id );
     493
     494            if ( ! $file_info || empty( json_decode( $file_info )->name ) ) {
     495                wp_send_json_error( $this->get_settings( 'label_err_generic', 'customize' ) );
     496            }
     497
     498            $file_info   = json_decode( $file_info );
     499            $file_name   = $file_info->name;
     500            $file_mime   = $file_info->mimeType;
     501            $file_parts  = 1;
     502            $file_method = 'download';
     503
     504            if ( strpos( $file_info->mimeType, 'application/vnd.google-apps' ) !== false ) {
     505                // Is Google App file
     506                $file_type            = str_replace( 'application/vnd.google-apps.', '', $file_info->mimeType );
     507                $file_export_type_map = array(
     508                    'audio'        => array(
     509                        'mime' => 'audio/mpeg',
     510                        'ext'  => '.mp3',
     511                    ),
     512                    'document'     => array(
     513                        'mime' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
     514                        'ext'  => '.docx',
     515                    ),
     516                    'drawing'      => array(
     517                        'mime' => 'application/pdf',
     518                        'ext'  => '.pdf',
     519                    ),
     520                    'file'         => array(
     521                        'mime' => 'application/octet-stream',
     522                        'ext'  => '',
     523                    ),
     524                    'presentation' => array(
     525                        'mime' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
     526                        'ext'  => '.pptx',
     527                    ),
     528                    'spreadsheet'  => array(
     529                        'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
     530                        'ext'  => '.xlsx',
     531                    ),
     532                );
     533                if ( empty( $file_export_type_map[ $file_type ] ) ) {
     534                    wp_send_json_error( __( 'File mime not supported.', 'cloud-customer-area' ) );
     535                }
     536                $file_name   = $file_info->name . $file_export_type_map[ $file_type ]['ext'];
     537                $file_mime   = $file_export_type_map[ $file_type ]['mime'];
     538                $file_method = 'export';
     539            } else {
     540                $chunk_size_bytes = apply_filters( 'cca_download_max_chunk_size', 3 ) * 1024 * 1024;
     541                $file_parts       = ceil( $file_info->size / $chunk_size_bytes );
     542            }
     543            $file_name = apply_filters( 'cca_download_file_name', $file_name );
     544            do_action( 'cca_download_event', $file_name, wp_get_current_user()->ID );
     545            $return = array(
     546                'name'   => $file_name,
     547                'mime'   => $file_mime,
     548                'parts'  => $file_parts,
     549                'method' => $file_method,
     550            );
     551            echo wp_json_encode( $return );
     552        }
     553        exit;
     554    }
     555
     556    /**
     557     * @return void
     558     */
     559    public function ajax_action_download_file() {
     560        $nonce          = ! empty( $_REQUEST['token'] ) && wp_verify_nonce( $_REQUEST['token'], CCA_SLUG . '_token' ) ? true : false;
     561        $file_id        = ! empty( $_REQUEST['file'] ) && is_string( $_REQUEST['file'] ) ? $_REQUEST['file'] : false;
     562        $method         = ! empty( $_REQUEST['method'] ) && is_string( $_REQUEST['method'] ) ? $_REQUEST['method'] : false;
     563        $mime           = ! empty( $_REQUEST['mime'] ) && is_string( $_REQUEST['mime'] ) ? $_REQUEST['mime'] : false;
     564        $part           = ! empty( $_REQUEST['part'] ) && is_string( $_REQUEST['part'] ) ? $_REQUEST['part'] : false;
     565        $customer_roles = $this->get_settings( 'customer_roles', 'general' );
     566        if ( $this->current_user_can() && ( ! in_array( 'guest', $customer_roles, true ) ? $nonce : true ) && $file_id && $method && $mime && $part ) {
     567            $content = $this->download_file( $file_id, $method, $mime, $part );
     568            header( 'content-type: "text/plain; charset=us-ascii"' );
     569            echo $content;
     570        }
     571        exit;
     572    }
     573
     574    /**
     575     * @param $file_id
     576     * @param $method
     577     * @param $mime
     578     * @param $part
     579     *
     580     * @return false|string
     581     */
     582    private function download_file( $file_id, $method, $mime, $part ) {
     583        if ( 'export' === $method ) {
     584            $response = $this->google_api->exportFile( $file_id, $mime );
     585        } else {
     586            $response = $this->google_api->downloadFile( $file_id, $part );
     587        }
     588        if ( strlen( $response ) < 1 ) {
     589            return $this->download_file( $file_id, $method, $mime, $part );
     590        }
     591        return $response;
     592    }
     593
     594    /**
     595     * @param $current_user
     596     *
     597     * @return mixed
     598     */
     599    public function get_user_folder( $current_user = null ) {
     600        $current_user = $current_user ?? wp_get_current_user();
     601        $user_id      = $current_user->ID;
     602
     603        if ( 0 !== $user_id ) {
     604            $folder_id = get_user_meta( $user_id, CCA_SLUG . '_folder', true );
     605        } else {
     606            $folder_id = get_option( CCA_SLUG . '_guest_folder', '' );
     607        }
     608        if ( ! empty( $folder_id ) ) {
     609            $folder_info_request = $this->google_api->getFileInfo( $folder_id );
     610            if ( 404 === $folder_info_request ) {
     611                if ( 0 !== $user_id ) {
     612                    update_user_meta( $user_id, CCA_SLUG . '_folder', '' );
     613                } else {
     614                    update_option( CCA_SLUG . '_guest_folder', '' );
     615                }
     616                return $this->get_user_folder();
     617            }
     618            if ( ! $folder_info_request ) {
     619                return $this->get_user_folder();
     620            }
     621            if ( ! empty( json_decode( $folder_info_request )->name ) ) {
     622                return $folder_id;
     623            }
     624        }
     625
     626        if ( 0 !== $user_id ) {
     627            $new_folder_use = $this->get_settings( 'customer_dir_name', 'general' );
     628            if ( ! in_array( $new_folder_use, array( 'user_login', 'display_name', 'user_email' ), true ) ) {
     629                $new_folder_use = 'user_login';
     630            }
     631            $new_folder_name = $current_user->$new_folder_use;
     632        } else {
     633            $new_folder_name = 'guest-users';
     634        }
     635
     636        $new_folder_name = apply_filters( 'cca_new_folder_name', $new_folder_name, $user_id );
     637        do_action( 'cca_folder_creation_event', $new_folder_name, $user_id );
     638
     639        $create_folder_request = $this->google_api->createFolder( $new_folder_name );
     640
     641        $created_folder_id = $create_folder_request && ! empty( json_decode( $create_folder_request )->id ) ? json_decode( $create_folder_request )->id : false;
     642
     643        if ( $created_folder_id ) {
     644            if ( 0 !== $user_id ) {
     645                update_user_meta( $current_user->ID, CCA_SLUG . '_folder', $created_folder_id );
     646            } else {
     647                update_option( CCA_SLUG . '_guest_folder', $created_folder_id );
     648            }
     649        } else {
     650            return $this->get_user_folder();
     651        }
     652
     653        return $created_folder_id;
     654    }
     655
     656    /**
     657     * @return void
     658     */
     659    private function apply_updates() {
     660        $installed_version = get_option( 'cloud_customer_area_version', null );
     661        $current_version   = CCA_VER;
     662
     663        if ( $installed_version && version_compare( $installed_version, $current_version, '=' ) ) {
     664            return;
     665        }
     666
     667        //        if (version_compare($installed_version, $current_version, '<')) {
     668        //            // Apply updates
     669        //        }
     670
     671        update_option( 'cloud_customer_area_version', $current_version );
     672
     673        if ( ! empty( $installed_version ) ) {
     674            $request_url = add_query_arg(
     675                array(
     676                    'id'     => 470,
     677                    'action' => 'updated',
     678                    'domain' => md5( get_home_url() ),
     679                    'v'      => $current_version,
     680                ),
     681                'https://totalpress.org/wp-json/totalpress/v1/plugin-growth'
     682            );
     683            wp_remote_get( $request_url );
     684            update_option( 'cloud_customer_area_updated_time', time() );
     685        } else {
     686            update_option( 'cloud_customer_area_installation_time', time() );
     687        }
     688    }
    546689}
  • cloud-customer-area/trunk/part/settings_page_main.php

    r2796544 r3244826  
    8989            </td>
    9090        </tr>
     91        <tr valign="top">
     92            <th scope="row"><span class="cca-pro-required"><?php _e('Show subfolder files', 'cloud-customer-area'); ?></span></th>
     93            <td><label class="switch">
     94                    <input type="checkbox" disabled />
     95                    <span class="slider"></span>
     96                </label></td>
     97        </tr>
    9198    </table>
    9299
  • cloud-customer-area/trunk/readme.txt

    r3197601 r3244826  
    44Donate link: https://totalpress.org/donate?utm_source=wordpress_org&utm_medium=plugin_page&utm_campaign=cloud_customer_area
    55Requires at least: 4.0
    6 Tested up to: 6.7
    7 Stable tag: 2.0.8
     6Tested up to: 6.4
     7Stable tag: 2.1.0
    88Requires PHP: 5.6
    99License: GPLv2 or later
     
    101101== Changelog ==
    102102
     103= 2.1.0- 2025-02-22 =
     104* FEAT: add user edit screen actions
     105* FEAT-PRO: show subfolders files on shortcode table
     106
    103107= 2.0.8 - 2024-04-24 =
    104108* FIX: recreate Drive folder on 404 api error
Note: See TracChangeset for help on using the changeset viewer.