Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
type: 'POST',
data: {
action: 'trash_log_delete_csv',
nonce: trashLogAdmin.nonce,
nonce: trashLogAdmin.nonceDelete,
},
success: function( response ) {
if ( response.success ) {
Expand Down Expand Up @@ -87,7 +87,7 @@
type: 'POST',
data: {
action: 'trash_log_generate_csv',
nonce: trashLogAdmin.nonce,
nonce: trashLogAdmin.nonceGenerate,
},
success: function( response ) {
if ( response.success ) {
Expand Down Expand Up @@ -123,7 +123,7 @@
type: 'POST',
data: {
action: 'trash_log_generate_csv',
nonce: trashLogAdmin.nonce,
nonce: trashLogAdmin.nonceGenerate,
},
success: function( response ) {
if ( response.success ) {
Expand Down
2 changes: 1 addition & 1 deletion autoload.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Trash_Log\Trash_Log;
namespace BEAPI\Trash_Log;

/**
* An example of a general-purpose implementation that includes the optional
Expand Down
16 changes: 5 additions & 11 deletions classes/Admin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Trash_Log\Trash_Log;
namespace BEAPI\Trash_Log;

/**
* Handles admin page and AJAX actions.
Expand Down Expand Up @@ -52,10 +52,6 @@ protected function init(): void {
* @return bool True if user has permission, false otherwise.
*/
private function current_user_can_access(): bool {
if ( is_multisite() ) {
return is_super_admin();
}

return current_user_can( 'manage_options' );
}

Expand Down Expand Up @@ -189,6 +185,8 @@ public function enqueue_scripts( string $hook_suffix ): void {
[
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'trash_log_admin' ),
'nonceDelete' => wp_create_nonce( 'trash_log_delete_csv' ),
'nonceGenerate' => wp_create_nonce( 'trash_log_generate_csv' ),
'confirmText' => __( 'Are you sure you want to delete the CSV file?', 'trash-log' ),
'confirmPurgeText' => __( 'Are you sure you want to purge all log entries from the database? This action cannot be undone.', 'trash-log' ),
]
Expand All @@ -203,10 +201,6 @@ public function enqueue_scripts( string $hook_suffix ): void {
* @return void
*/
public function render_admin_page(): void {
if ( ! $this->current_user_can_access() ) {
wp_die( esc_html__( 'You do not have permission to access this page.', 'trash-log' ) );
}

$csv_handler = CSV_Handler::get_instance();
$logger = Logger::get_instance();
$csv_exists = $csv_handler->csv_exists();
Expand Down Expand Up @@ -296,7 +290,7 @@ public function ajax_download_csv(): void {
* @return void
*/
public function ajax_delete_csv(): void {
if ( ! $this->verify_ajax_request( 'trash_log_admin' ) ) {
if ( ! $this->verify_ajax_request( 'trash_log_delete_csv' ) ) {
return;
}

Expand All @@ -318,7 +312,7 @@ public function ajax_delete_csv(): void {
* @return void
*/
public function ajax_generate_csv(): void {
if ( ! $this->verify_ajax_request( 'trash_log_admin' ) ) {
if ( ! $this->verify_ajax_request( 'trash_log_generate_csv' ) ) {
return;
}

Expand Down
32 changes: 11 additions & 21 deletions classes/CSV_Handler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Trash_Log\Trash_Log;
namespace BEAPI\Trash_Log;

/**
* Handles CSV file generation, storage, and management.
Expand Down Expand Up @@ -256,15 +256,11 @@ public function download_csv(): void {
* @return bool True if user has permission, false otherwise.
*/
public function current_user_can_access(): bool {
if ( is_multisite() ) {
return is_super_admin();
}

return current_user_can( 'manage_options' );
}

/**
* Protect specific CSV file with .htaccess file.
* Protect specific CSV file with .htaccess file using WordPress markers.
*
* @since 1.0.0
*
Expand All @@ -276,21 +272,15 @@ private function protect_csv_directory( string $csv_dir ): bool {
return false;
}

$htaccess_file = trailingslashit( $csv_dir ) . '.htaccess';
$htaccess_content = "# Protect Trash Log CSV file\n";
$htaccess_content .= "# Deny direct access to trash-log.csv only\n";
$htaccess_content .= '<Files ' . self::CSV_FILENAME . ">\n";
$htaccess_content .= " Order allow,deny\n";
$htaccess_content .= " Deny from all\n";
$htaccess_content .= "</Files>\n";

$written = file_put_contents( $htaccess_file, $htaccess_content );
if ( false === $written ) {
return false;
}

chmod( $htaccess_file, 0644 );
$htaccess_file = trailingslashit( $csv_dir ) . '.htaccess';
$marker = 'Trash Log';
$insertion = [
'<Files ' . self::CSV_FILENAME . '>',
' Order allow,deny',
' Deny from all',
'</Files>',
];

return true;
return insert_with_markers( $htaccess_file, $marker, $insertion );
}
}
21 changes: 3 additions & 18 deletions classes/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Trash_Log\Trash_Log;
namespace BEAPI\Trash_Log;

/**
* The purpose of the API class is to have the basic reusable methods like :
Expand All @@ -14,7 +14,8 @@
* and stick to the non context methods
*
* Class API
* @package Trash_Log\Trash_Log
*
* @package BEAPI\Trash_Log
*/
class Helpers {

Expand Down Expand Up @@ -142,22 +143,6 @@ public static function datetime_i18n( string $format, \DateTime $date ): string
return date_i18n( $format, $date->format( 'U' ) );
}

/**
* Format a timestamp to DD/MM/YYYY format.
*
* @since 1.0.0
*
* @param int $timestamp Unix timestamp.
* @return string Formatted date in DD/MM/YYYY format.
*/
public static function format_date_dd_mm_yyyy( int $timestamp ): string {
if ( $timestamp <= 0 ) {
return '';
}

return date_i18n( 'd/m/Y', $timestamp );
}

/**
* Get estimated database size for an option value.
*
Expand Down
13 changes: 6 additions & 7 deletions classes/Logger.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Trash_Log\Trash_Log;
namespace BEAPI\Trash_Log;

/**
* Handles logging of trashed posts, pages, and media.
Expand Down Expand Up @@ -30,7 +30,7 @@ class Logger {
*/
protected function init(): void {
// log posts, pages, custom post types
add_action( 'wp_trash_post', [ $this, 'log_post_trash' ], 10, 2 );
add_action( 'wp_trash_post', [ $this, 'log_post_trash' ], 10, 1 );

// log attachments
add_action( 'delete_attachment', [ $this, 'log_deleted_attachment' ], 5, 1 );
Expand Down Expand Up @@ -67,11 +67,10 @@ public function log_deleted_attachment( int $post_id ): void {
*
* @since 1.0.0
*
* @param int $post_id Post ID.
* @param string $previous_status Previous post status (unused, required by hook).
* @param int $post_id Post ID.
* @return void
*/
public function log_post_trash( int $post_id, string $_previous_status ): void {
public function log_post_trash( int $post_id ): void {
$post = get_post( $post_id );
if ( ! $post ) {
return;
Expand Down Expand Up @@ -123,9 +122,9 @@ public function log_trashed_post( int $post_id ): void {
// Get deletion date.
$trash_time = get_post_meta( $post_id, '_wp_trash_meta_time', true );
if ( ! empty( $trash_time ) && is_numeric( $trash_time ) ) {
$deletion_date = Helpers::format_date_dd_mm_yyyy( (int) $trash_time );
$deletion_date = Helpers::format_date( (string) $trash_time, 'U', 'd/m/Y' );
} else {
$deletion_date = Helpers::format_date_dd_mm_yyyy( time() );
$deletion_date = Helpers::format_date( (string) time(), 'U', 'd/m/Y' );
}

// Get deleted item name.
Expand Down
10 changes: 3 additions & 7 deletions classes/Main.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Trash_Log\Trash_Log;
namespace BEAPI\Trash_Log;

/**
* The purpose of the main class is to init all the plugin base code like :
Expand All @@ -11,7 +11,8 @@
* - Loading the text domain
*
* Class Main
* @package Trash_Log\Trash_Log
*
* @package BEAPI\Trash_Log
*/
class Main {
/**
Expand All @@ -21,11 +22,6 @@ class Main {

protected function init(): void {
add_action( 'init', [ $this, 'init_translations' ] );

// Initialize plugin components.
Logger::get_instance();
CSV_Handler::get_instance();
Admin::get_instance();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions classes/Singleton.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Trash_Log\Trash_Log;
namespace BEAPI\Trash_Log;

/**
* Singleton base class for having singleton implementation
Expand All @@ -11,7 +11,8 @@
* /!\ The get_instance method have to be implemented !
*
* Class Singleton
* @package Trash_Log\Trash_Log
*
* @package BEAPI\Trash_Log
*/
trait Singleton {

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"autoload": {
"psr-4": {
"Trash_Log\\Trash_Log\\": "classes/"
"BEAPI\\Trash_Log\\": "classes/"
}
},
"scripts": {
Expand Down
Binary file modified languages/trash-log-fr_FR.mo
Binary file not shown.
Loading