| 1 |
<?php |
| 2 |
|
| 3 |
namespace ReviewX\Rest\Controllers; |
| 4 |
|
| 5 |
\defined("ABSPATH") || exit; |
| 6 |
use Exception; |
| 7 |
use ReviewX\Services\CategorySyncService; |
| 8 |
use ReviewX\Services\DataSyncService; |
| 9 |
use ReviewX\Services\OrderItemSyncService; |
| 10 |
use ReviewX\Services\ProductSyncService; |
| 11 |
use ReviewX\Services\ReviewSyncService; |
| 12 |
use ReviewX\Services\UserSyncService; |
| 13 |
use ReviewX\Utilities\Helper; |
| 14 |
use ReviewX\WPDrill\Contracts\InvokableContract; |
| 15 |
use ReviewX\Services\CacheServices; |
| 16 |
class LogController implements InvokableContract |
| 17 |
{ |
| 18 |
protected CacheServices $cacheServices; |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$this->cacheServices = new CacheServices(); |
| 22 |
} |
| 23 |
/** |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function __invoke() |
| 27 |
{ |
| 28 |
} |
| 29 |
public function rvxRecentLog($request) |
| 30 |
{ |
| 31 |
$data = $request->get_params(); |
| 32 |
$this->directoryCreate($data); |
| 33 |
$this->logDownload($data); |
| 34 |
$this->deleteLog($data); |
| 35 |
} |
| 36 |
public function directoryCreate($data) |
| 37 |
{ |
| 38 |
if ($data['action'] === 'dir') { |
| 39 |
global $wp_filesystem; |
| 40 |
if (empty($wp_filesystem)) { |
| 41 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 42 |
\WP_Filesystem(); |
| 43 |
} |
| 44 |
$log_folder = REVIEWX_DIR_PATH . 'log/'; |
| 45 |
if (!$wp_filesystem->exists($log_folder)) { |
| 46 |
if (!$wp_filesystem->mkdir($log_folder, 0755)) { |
| 47 |
throw new \RuntimeException(\sprintf('Directory "%s" was not created', \esc_html($log_folder))); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
public function logDownload($data) |
| 53 |
{ |
| 54 |
if ($data['action'] === 'log') { |
| 55 |
global $wp_filesystem; |
| 56 |
if (empty($wp_filesystem)) { |
| 57 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 58 |
\WP_Filesystem(); |
| 59 |
} |
| 60 |
$logPath = REVIEWX_DIR_PATH . 'log/'; |
| 61 |
$files = \glob($logPath . '*'); |
| 62 |
// glob is generally acceptable if WP_Filesystem doesn't provide a direct equivalent that is as easy to use here, but we'll stick to contents if possible. |
| 63 |
if (empty($files)) { |
| 64 |
\esc_html_e('No log files found', 'reviewx'); |
| 65 |
return; |
| 66 |
} |
| 67 |
$recentFile = null; |
| 68 |
foreach ($files as $file) { |
| 69 |
if (\is_null($recentFile) || $wp_filesystem->mtime($file) > $wp_filesystem->mtime($recentFile)) { |
| 70 |
$recentFile = $file; |
| 71 |
} |
| 72 |
} |
| 73 |
if (!$wp_filesystem->exists($recentFile)) { |
| 74 |
\esc_html_e('File not found', 'reviewx'); |
| 75 |
return; |
| 76 |
} |
| 77 |
\header('Content-Type: text/plain'); |
| 78 |
\header('Content-Disposition: attachment; filename="' . \basename($recentFile) . '"'); |
| 79 |
\header('Content-Length: ' . $wp_filesystem->size($recentFile)); |
| 80 |
\ob_clean(); |
| 81 |
\flush(); |
| 82 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 83 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 84 |
echo $wp_filesystem->get_contents($recentFile); |
| 85 |
exit; |
| 86 |
} |
| 87 |
} |
| 88 |
public function deleteLog($data) |
| 89 |
{ |
| 90 |
if ($data['action'] === 'remove') { |
| 91 |
global $wp_filesystem; |
| 92 |
if (empty($wp_filesystem)) { |
| 93 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 94 |
\WP_Filesystem(); |
| 95 |
} |
| 96 |
$log_folder = REVIEWX_DIR_PATH . 'log/'; |
| 97 |
if (!$wp_filesystem->is_dir($log_folder)) { |
| 98 |
\esc_html_e('Log folder does not exist', 'reviewx'); |
| 99 |
return; |
| 100 |
} |
| 101 |
$files = $wp_filesystem->dirlist($log_folder); |
| 102 |
if ($files) { |
| 103 |
foreach ($files as $file) { |
| 104 |
$wp_filesystem->delete($log_folder . $file['name']); |
| 105 |
} |
| 106 |
} |
| 107 |
if ($wp_filesystem->rmdir($log_folder)) { |
| 108 |
\esc_html_e('Log folder and files deleted successfully', 'reviewx'); |
| 109 |
} else { |
| 110 |
\esc_html_e('Failed to delete the log folder', 'reviewx'); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
public function appendJsonSync($request) |
| 115 |
{ |
| 116 |
$action = $request->get_param('action'); |
| 117 |
$from = $request->get_param('from'); |
| 118 |
if ($action === 'create_jsonl') { |
| 119 |
$this->createJsonl(); |
| 120 |
} |
| 121 |
if ($action === 'download') { |
| 122 |
$this->downloadJsonl(); |
| 123 |
} |
| 124 |
if ($action === 'manual_sync') { |
| 125 |
$dataResponse = (new DataSyncService())->dataSync($from); |
| 126 |
$this->cacheServices->removeCache(); |
| 127 |
if (!$dataResponse) { |
| 128 |
return Helper::rvxApi(['error' => "Data Sync Failed"])->fails('Data Sync Failed', $dataResponse->getStatusCode()); |
| 129 |
} |
| 130 |
} |
| 131 |
return Helper::rvxApi()->success('Data Synced Successfully'); |
| 132 |
} |
| 133 |
public function createJsonl() |
| 134 |
{ |
| 135 |
try { |
| 136 |
global $wp_filesystem; |
| 137 |
if (empty($wp_filesystem)) { |
| 138 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 139 |
\WP_Filesystem(); |
| 140 |
} |
| 141 |
$file_buffer = ""; |
| 142 |
$syncedCaterories = new CategorySyncService(); |
| 143 |
$syncedCaterories->syncCategory($file_buffer); |
| 144 |
(new UserSyncService())->syncUser($file_buffer); |
| 145 |
$processProduct = new ProductSyncService(); |
| 146 |
$processProduct->processProductForSync($file_buffer, 'product'); |
| 147 |
(new ReviewSyncService())->processReviewForSync($file_buffer, 'product'); |
| 148 |
if (\class_exists('WooCommerce')) { |
| 149 |
$order = new OrderItemSyncService(); |
| 150 |
$order->syncOrder($file_buffer); |
| 151 |
$order->syncOrderItem($file_buffer); |
| 152 |
} |
| 153 |
$file_path = REVIEWX_DIR_PATH . 'sync.jsonl'; |
| 154 |
$wp_filesystem->put_contents($file_path, $file_buffer, \FS_CHMOD_FILE); |
| 155 |
\esc_html_e('jsonl create done', 'reviewx'); |
| 156 |
} catch (Exception $e) { |
| 157 |
return \esc_html($e->getMessage()); |
| 158 |
} |
| 159 |
} |
| 160 |
public function downloadJsonl() |
| 161 |
{ |
| 162 |
$syncJsonlDownload = REVIEWX_DIR_PATH . 'sync.jsonl'; |
| 163 |
global $wp_filesystem; |
| 164 |
if (empty($wp_filesystem)) { |
| 165 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 166 |
\WP_Filesystem(); |
| 167 |
} |
| 168 |
if ($wp_filesystem->exists($syncJsonlDownload)) { |
| 169 |
\header('Content-Type: text/plain'); |
| 170 |
\header('Content-Disposition: attachment; filename="' . \basename($syncJsonlDownload) . '"'); |
| 171 |
\header('Content-Length: ' . $wp_filesystem->size($syncJsonlDownload)); |
| 172 |
\ob_clean(); |
| 173 |
\flush(); |
| 174 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 175 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 176 |
echo $wp_filesystem->get_contents($syncJsonlDownload); |
| 177 |
$wp_filesystem->delete($syncJsonlDownload); |
| 178 |
exit; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|