Skip to content

Commit

Permalink
add/billwerk logs page
Browse files Browse the repository at this point in the history
  • Loading branch information
0zd0 committed Jun 13, 2024
1 parent eecdaf8 commit 38dc360
Show file tree
Hide file tree
Showing 39 changed files with 1,384 additions and 5,996 deletions.
49 changes: 49 additions & 0 deletions includes/Admin/LogsPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Add logs page
*
* @package Reepay\Checkout\Admin
*/

namespace Reepay\Checkout\Admin;

/**
* Class
*
* @package Reepay\Checkout\Admin
*/
class LogsPage {
public const SLUG = 'bw-logs';

/**
* Constructor.
*/
public function __construct() {
add_action( 'woocommerce_admin_status_tabs', array( $this, 'add_tab' ) );
add_action( 'woocommerce_admin_status_content_' . self::SLUG, array( $this, 'tab_content' ) );
}

/**
* Add tab.
*
* @param array $tabs slugs and tab names.
* @return array
*/
public function add_tab( array $tabs ): array {
$tabs[ self::SLUG ] = __( 'Billwerk logs', 'reepay-checkout-gateway' );
return $tabs;
}

/**
* Tab content
*
* @return void
*/
public function tab_content() {
$template_args = array();
reepay()->get_template(
'admin/logs-page.php',
$template_args
);
}
}
10 changes: 8 additions & 2 deletions includes/Admin/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Main {
public function __construct() {
new PluginsPage();
new DebugPage();
new LogsPage();
new Ajax();
new MetaBoxes\Order();
new MetaBoxes\User();
Expand All @@ -37,7 +38,8 @@ public function __construct() {
* @param string $hook current page hook.
*/
public function admin_enqueue_scripts( string $hook ) {
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
$vite_dev = defined( 'VITE_DEV' ) && VITE_DEV;
if ( 'post.php' === $hook || rp_hpos_is_order_page() ) {
$suffix = $debug ? '' : '.min';

Expand Down Expand Up @@ -87,8 +89,12 @@ public function admin_enqueue_scripts( string $hook ) {
'nested_path' => '/debug-page/',
'file' => 'src/admin/debug-page/main.tsx',
),
array(
'nested_path' => '/logs-page/',
'file' => 'src/admin/logs-page/main.tsx',
),
);
if ( $debug ) {
if ( $vite_dev ) {
ViteAssetsLoader::dev( $vite_entry_points );
} else {
ViteAssetsLoader::production(
Expand Down
77 changes: 77 additions & 0 deletions includes/Api/Controller/LogsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Controller logs
*
* @package Reepay\Checkout\Api\Controller
*/

namespace Reepay\Checkout\Api\Controller;

use WP_Error;
use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;

/**
* Class controller
*
* @package Reepay\Checkout\Api\Controller
*/
class LogsController extends WP_REST_Controller {
/**
* Constructor
*/
public function __construct() {
$this->namespace = 'billwerk/v1';
$this->rest_base = 'logs';
}

/**
* Register API routes
*
* @return void
*/
public function register_routes() {
register_rest_route(
$this->namespace,
"/$this->rest_base/",
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_logs' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
}

/**
* Retrieves files logs
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_logs( WP_REST_Request $request ) {
$files = reepay()->log()->get_files();

return rest_ensure_response( $files );
}

/**
* Checks if a given request has access to get items.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function get_items_permissions_check( $request ) {
if ( ! current_user_can( 'manage_options' ) ) {
return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the post resource.' ), array( 'status' => rest_authorization_required_code() ) );
}

return true;
}
}
69 changes: 69 additions & 0 deletions includes/Utils/FilesystemUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Class for working with WP_Filesystem
*
* @package Reepay\Checkout\Utils
*/

namespace Reepay\Checkout\Utils;

use Exception;
use WP_Filesystem_Base;

/**
* FilesystemUtil class.
*
* @package Reepay\Checkout\Utils
*/
class FilesystemUtil {
/**
* Wrapper to retrieve the class instance contained in the $wp_filesystem global, after initializing if necessary.
*
* @return WP_Filesystem_Base
* @throws Exception Thrown when the filesystem fails to initialize.
*/
public static function get_wp_filesystem(): WP_Filesystem_Base {
global $wp_filesystem;

if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) {
$initialized = self::initialize_wp_filesystem();

if ( false === $initialized ) {
throw new Exception( 'The WordPress filesystem could not be initialized.' );
}
}

return $wp_filesystem;
}

/**
* Wrapper to initialize the WP filesystem with defined credentials if they are available.
*
* @return bool True if the $wp_filesystem global was successfully initialized.
*/
protected static function initialize_wp_filesystem(): bool {
global $wp_filesystem;

if ( $wp_filesystem instanceof WP_Filesystem_Base ) {
return true;
}

require_once ABSPATH . 'wp-admin/includes/file.php';

$method = get_filesystem_method();
$initialized = false;

if ( 'direct' === $method ) {
$initialized = WP_Filesystem();
} elseif ( false !== $method ) {
// See https://core.trac.wordpress.org/changeset/56341.
ob_start();
$credentials = request_filesystem_credentials( '' );
ob_end_clean();

$initialized = $credentials && WP_Filesystem( $credentials );
}

return is_null( $initialized ) ? false : $initialized;
}
}
73 changes: 66 additions & 7 deletions includes/Utils/Logger/JsonLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
namespace Reepay\Checkout\Utils\Logger;

use DateTime;
use Exception;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Reepay\Checkout\Utils\FilesystemUtil;
use SplFileInfo;
use WP_Filesystem_Base;

/**
Expand All @@ -16,6 +21,7 @@
* @package Reepay\Checkout\Utils\Logger
*/
class JsonLogger {
public const FOLDER = 'billwerk-logs';
/**
* Wp Filesystem
*
Expand All @@ -30,6 +36,13 @@ class JsonLogger {
*/
private string $directory_path;

/**
* Directory url logs
*
* @var string $directory_url
*/
private string $directory_url;

/**
* Source log.
*
Expand All @@ -48,17 +61,16 @@ class JsonLogger {
* Class constructor
*
* @param string $directory_path Directory path logs.
* @param string $directory_url Directory url logs.
* @param string $source Source log.
*
* @throws Exception Filesystem error.
*/
public function __construct( string $directory_path, string $source ) {
global $wp_filesystem;

if ( empty( $wp_filesystem ) ) {
WP_Filesystem();
}
public function __construct( string $directory_path, string $directory_url, string $source ) {
$this->wp_filesystem = FilesystemUtil::get_wp_filesystem();

$this->wp_filesystem = $wp_filesystem;
$this->directory_path = $directory_path;
$this->directory_url = $directory_url;
$this->source = $source;
}

Expand Down Expand Up @@ -182,6 +194,53 @@ private function log( string $level, $message, array $context = array() ) {
$this->wp_filesystem->put_contents( $log_file_path, $log_entry_json, FS_CHMOD_FILE );
}

/**
* Get file information
*
* @param SplFileInfo $file file path.
*
* @return array
*/
public function get_file_details( SplFileInfo $file ): array {
$pathname = $file->getPathname();
$split_path = explode( self::FOLDER, $pathname );
$nested_path = null;
if ( $split_path ) {
$nested_path = $this->directory_url . $split_path[1];
}
return array(
'name' => $file->getBasename( '.json' ),
'url' => $nested_path ?? $pathname,
'size' => filesize( $pathname ),
'created' => gmdate( DATE_ATOM, filectime( $pathname ) ),
'modified' => gmdate( DATE_ATOM, filemtime( $pathname ) ),
);
}

/**
* Get files logs
*
* @return array
*/
public function get_files(): array {
$rii = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $this->directory_path ) );
$files = array();

/**
* File log
*
* @var $file SplFileInfo
*/
foreach ( $rii as $file ) {
if ( ! $file->isFile() ) {
continue;
}
$files[] = $this->get_file_details( $file );
}

return $files;
}

/**
* Adds info level message.
*
Expand Down
5 changes: 4 additions & 1 deletion reepay-woocommerce-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use GuzzleHttp\Psr7\HttpFactory;
use Reepay\Checkout\Api;
use Reepay\Checkout\Api\Controller\DebugController;
use Reepay\Checkout\Api\Controller\LogsController;
use Reepay\Checkout\Api\Controller\MetaFieldsController;
use Reepay\Checkout\DIContainer;
use Reepay\Checkout\Gateways;
Expand Down Expand Up @@ -295,7 +296,8 @@ public function sdk( bool $force_live_key = false ): Sdk {
*/
public function log( string $source = 'billwerk' ): JsonLogger {
return ( new JsonLogger(
wp_upload_dir()['basedir'] . '/billwerk-logs',
wp_upload_dir()['basedir'] . '/' . JsonLogger::FOLDER,
wp_upload_dir()['baseurl'] . '/' . JsonLogger::FOLDER,
$source
) )->add_ignored_classes_backtrace(
array(
Expand Down Expand Up @@ -362,6 +364,7 @@ public function include_classes() {
public function init_rest_api(): void {
( new MetaFieldsController() )->register_routes();
( new DebugController() )->register_routes();
( new LogsController() )->register_routes();
}
}

Expand Down
13 changes: 13 additions & 0 deletions templates/admin/logs-page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Custom logs page
*
* @package Reepay\Checkout
*/

defined( 'ABSPATH' ) || exit();
?>

<div class="wrap">
<div class="billwerk-logs"></div>
</div>
Binary file modified vite/bun.lockb
Binary file not shown.
Loading

0 comments on commit 38dc360

Please sign in to comment.