| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Publisher; |
| 4 |
|
| 5 |
use ParseError; |
| 6 |
use WPCoder\Dashboard\DBManager; |
| 7 |
use WPCoder\Dashboard\FolderManager; |
| 8 |
use WPCoder\WPCoder; |
| 9 |
|
| 10 |
class PHPIncludes { |
| 11 |
public function __construct() { |
| 12 |
add_action( 'init', [ $this, 'include_php' ] ); |
| 13 |
add_action( 'admin_notices', [ $this, 'display_admin_error_notice' ] ); |
| 14 |
} |
| 15 |
|
| 16 |
public function include_php(): void { |
| 17 |
$save_mode = isset( $_GET['wpcoder-safe-mode'] ) ? absint( $_GET['wpcoder-safe-mode'] ) : 0; |
| 18 |
if ( $save_mode === 1 ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$this->include_global(); |
| 23 |
$this->include_file(); |
| 24 |
} |
| 25 |
|
| 26 |
private function include_global(): void { |
| 27 |
$file_path = FolderManager::path_upload_dir() . 'global-php.php'; |
| 28 |
|
| 29 |
if ( file_exists( $file_path ) && get_option( '_wpcoder_enable_php' ) ) { |
| 30 |
$this->try_include_file( $file_path ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
private function include_file(): void { |
| 35 |
$all_data = DBManager::get_all_data(); |
| 36 |
|
| 37 |
if ( empty( $all_data ) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
foreach ( $all_data as $data ) { |
| 42 |
if ( ! empty( $data->php_include ) && empty( $data->status ) ) { |
| 43 |
$path = FolderManager::code_path( $data ); |
| 44 |
|
| 45 |
if ( absint( $data->php_include ) === 1 && is_admin() ) { |
| 46 |
$this->try_include_file( $path ); |
| 47 |
} |
| 48 |
|
| 49 |
if ( absint( $data->php_include ) === 2 && ! is_admin() ) { |
| 50 |
$this->try_include_file( $path ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( absint( $data->php_include ) === 3 ) { |
| 54 |
$this->try_include_file( $path ); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
private function try_include_file( $file_path ) { |
| 62 |
try { |
| 63 |
@include_once $file_path; |
| 64 |
} catch ( ParseError $e ) { |
| 65 |
$parts = explode( '/', $file_path ); |
| 66 |
$file_name = end( $parts ); |
| 67 |
if ( $file_name === 'global-php.php' ) { |
| 68 |
$page_url = admin_url() . 'admin.php?page=' . WPCoder::SLUG . '-global'; |
| 69 |
|
| 70 |
$message = sprintf( |
| 71 |
/* translators: 1. link to the page 2. error name. */ |
| 72 |
__( 'An error occurred while loading the PHP script from <a href="%1$s">Global PHP</a>. Error: %2$s', 'wp-coder' ), |
| 73 |
esc_url( $page_url ), |
| 74 |
esc_attr( $e->getMessage() ) |
| 75 |
); |
| 76 |
} else { |
| 77 |
$name_parts = explode('-', $file_name); |
| 78 |
$num_part = end($name_parts); |
| 79 |
$id = str_replace('.php', '', $num_part); |
| 80 |
$page_url = admin_url() . 'admin.php?page=' . WPCoder::SLUG . '-settings&action=update&id='.absint($id); |
| 81 |
|
| 82 |
$message = sprintf( |
| 83 |
/* translators: 1. link to the code 2. code ID. 3. error message */ |
| 84 |
__( 'An error occurred while loading the PHP script from <a href="%1$s">WP Code ID %2$d</a>. Error: %3$s', 'wp-coder' ), |
| 85 |
esc_url( $page_url ), |
| 86 |
absint($id), |
| 87 |
esc_attr( $e->getMessage() ), |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
error_log( "Parse error in file '$file_path': " . $e->getMessage() ); |
| 92 |
set_transient( 'wp_coder_admin_error_php_notice', $message, 30 ); |
| 93 |
|
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function display_admin_error_notice(): void { |
| 98 |
// Check if the transient exists |
| 99 |
$error_message = get_transient( 'wp_coder_admin_error_php_notice' ); |
| 100 |
if ( $error_message ) { |
| 101 |
?> |
| 102 |
<div class="notice notice-error"> |
| 103 |
<p><?php echo wp_kses_post( $error_message ); ?></p> |
| 104 |
</div> |
| 105 |
<?php |
| 106 |
// Delete the transient to prevent repeated notices |
| 107 |
delete_transient( 'wp_coder_admin_error_php_notice' ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
} |