diff --git a/inc/WPUBaseAdminPage/WPUBaseAdminPage.php b/inc/WPUBaseAdminPage/WPUBaseAdminPage.php index 8447c6b..ef0baac 100644 --- a/inc/WPUBaseAdminPage/WPUBaseAdminPage.php +++ b/inc/WPUBaseAdminPage/WPUBaseAdminPage.php @@ -4,7 +4,7 @@ /* Class Name: WPU Base Admin page Description: A class to handle pages in WordPress -Version: 1.6.0 +Version: 1.7.0 Class URI: https://github.com/WordPressUtilities/wpubaseplugin Author: Darklg Author URI: https://darklg.me/ @@ -238,11 +238,13 @@ public function add_settings_link($links) { public function set_admin_page_main() { $page = $this->get_page(); + $form_classname = $this->prefix . $page . '-form'; + echo $this->get_wrapper_start(); // Default Form if ($this->pages[$page]['has_form']): - echo '
pages[$page]['has_file'] ? ' enctype="multipart/form-data"' : '') . '>
'; + echo 'pages[$page]['has_file'] ? ' enctype="multipart/form-data"' : '') . '>
'; echo ''; echo ''; wp_nonce_field('action-main-form-' . $page, 'action-main-form-' . $this->options['id'] . '-' . $page); @@ -283,4 +285,11 @@ private function get_page() { } return $page; } + + public function get_page_url($page_id){ + if(!isset($this->pages[$page_id])){ + return false; + } + return $this->pages[$page_id]['url']; + } } diff --git a/inc/WPUBaseToolbox/README.md b/inc/WPUBaseToolbox/README.md index e5fcb9f..6d1bdd8 100644 --- a/inc/WPUBaseToolbox/README.md +++ b/inc/WPUBaseToolbox/README.md @@ -6,8 +6,10 @@ Cool helpers for WordPress Plugins. ## Insert in the INIT hook ```php -require_once dirname(__FILE__) . '/inc/WPUBaseToolbox/WPUBaseToolbox.php'; -$this->basetoolbox = new \myplugin\WPUBaseToolbox(); +require_once __DIR__ . '/inc/WPUBaseToolbox/WPUBaseToolbox.php'; +$this->basetoolbox = new \myplugin\WPUBaseToolbox(array( + 'need_form_js' => false +)); ``` ## Use functions diff --git a/inc/WPUBaseToolbox/WPUBaseToolbox.php b/inc/WPUBaseToolbox/WPUBaseToolbox.php index 15ac84e..9a12eb4 100644 --- a/inc/WPUBaseToolbox/WPUBaseToolbox.php +++ b/inc/WPUBaseToolbox/WPUBaseToolbox.php @@ -4,7 +4,7 @@ /* Class Name: WPU Base Toolbox Description: Cool helpers for WordPress Plugins -Version: 0.12.1 +Version: 0.14.0 Class URI: https://github.com/WordPressUtilities/wpubaseplugin Author: Darklg Author URI: https://darklg.me/ @@ -15,15 +15,27 @@ defined('ABSPATH') || die; class WPUBaseToolbox { - private $plugin_version = '0.12.1'; - public function __construct() { + private $plugin_version = '0.14.0'; + private $args = array(); + private $default_module_args = array( + 'need_form_js' => true + ); + + public function __construct($args = array()) { + if (!is_array($args)) { + $args = array(); + } + $this->args = array_merge($this->default_module_args, $args); + add_action('wp_enqueue_scripts', array(&$this, 'form_scripts' )); } function form_scripts() { - wp_enqueue_script(__NAMESPACE__ . '-wpubasetoolbox-form-validation', plugins_url('assets/form-validation.js', __FILE__), array(), $this->plugin_version); + if ($this->args['need_form_js']) { + wp_enqueue_script(__NAMESPACE__ . '-wpubasetoolbox-form-validation', plugins_url('assets/form-validation.js', __FILE__), array(), $this->plugin_version); + } } /* ---------------------------------------------------------- @@ -449,4 +461,111 @@ function array_to_html_table($array, $args = array()) { return $html; } + /* ---------------------------------------------------------- + Export + ---------------------------------------------------------- */ + + /* Ensure all lines have the same keys + -------------------------- */ + + function export_array_clean_for_csv($data) { + + /* Extract all available keys */ + $all_keys = array(); + foreach ($data as $item) { + $all_keys = array_merge($all_keys, array_keys($item)); + } + $all_keys = array_unique($all_keys); + + foreach ($data as $item_key => $item) { + /* Ensure all rows have the same keys */ + foreach ($all_keys as $k) { + if (!isset($item[$k])) { + $data[$item_key][$k] = ''; + } + } + /* Ensure same sorting of all keys */ + ksort($data[$item_key]); + } + + return $data; + } + + /* Array to JSON + -------------------------- */ + + public function export_array_to_json($data, $name) { + if (!isset($data[0])) { + return; + } + /* Correct headers */ + header('Content-type: application/json'); + header('Content-Disposition: attachment; filename=' . $name . '.json'); + header('Pragma: no-cache'); + + echo json_encode($data); + } + + /* Array to CSV + -------------------------- */ + + public function export_array_to_csv($data, $name) { + if (!isset($data[0])) { + return; + } + + $data = $this->export_array_clean_for_csv($data); + + /* Correct headers */ + header('Content-Type: application/csv'); + header('Content-Disposition: attachment; filename=' . $name . '.csv'); + header('Pragma: no-cache'); + + $all_keys = array_keys($data[0]); + + /* Build and send CSV */ + $output = fopen("php://output", 'w'); + fputcsv($output, $all_keys); + foreach ($data as $item) { + fputcsv($output, $item); + } + fclose($output); + die; + } + + /* ---------------------------------------------------------- + IPs + ---------------------------------------------------------- */ + + /* Thanks to https://stackoverflow.com/a/13646735/975337 */ + function get_user_ip($anonymized = true) { + if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) { + $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; + $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; + } + $client = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : ''; + $forward = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : ''; + $remote = $_SERVER['REMOTE_ADDR']; + + if (filter_var($client, FILTER_VALIDATE_IP)) { + $ip = $client; + } elseif (filter_var($forward, FILTER_VALIDATE_IP)) { + $ip = $forward; + } else { + $ip = $remote; + } + if (!$anonymized) { + return $ip; + } + return $this->anonymize_ip($ip); + } + + /* Thanks to https://gist.github.com/svrnm/3a124d2af18a6726f66e */ + function anonymize_ip($ip) { + if ($ip = @inet_pton($ip)) { + return inet_ntop(substr($ip, 0, strlen($ip) / 2) . str_repeat(chr(0), strlen($ip) / 2)); + } + return '0.0.0.0'; + } + } diff --git a/inc/index.php b/inc/index.php new file mode 100644 index 0000000..194cdb1 --- /dev/null +++ b/inc/index.php @@ -0,0 +1 @@ + 'wpuerrorlogs', 'name' => 'WPU Error Logs' @@ -75,6 +77,9 @@ public function plugins_loaded() { require_once __DIR__ . '/inc/WPUBaseAdminPage/WPUBaseAdminPage.php'; $this->adminpages = new \wpuerrorlogs\WPUBaseAdminPage(); $this->adminpages->init($pages_options, $admin_pages); + + # HOOKS + $this->number_of_days = apply_filters('wpuerrorlogs__number_of_days', $this->number_of_days); } public function page_content__main() { @@ -85,13 +90,12 @@ public function page_content__main() { return; } - $errors = $this->get_logs(); + $number_of_days = $this->number_of_days; + if (isset($_GET['number_of_days']) && is_numeric($_GET['number_of_days']) && $_GET['number_of_days'] <= $this->number_of_days) { + $number_of_days = intval($_GET['number_of_days']); + } - /* Prepare for display */ - $errors = array_map(function ($item) { - $item['text'] = $this->display_content_with_toggle($item['text']); - return $item; - }, $errors); + $errors = $this->get_logs($number_of_days); /* Keep only first five and extract data */ $colnames = array( @@ -101,6 +105,14 @@ public function page_content__main() { 'text' => __('Text', 'wpuerrorlogs') ); + /* Select number of days */ + echo ' '; + echo ''; + /* Top errors */ $top_errors = $this->sort_errors_by_top($errors, 10); echo '

' . __('Top errors', 'wpuerrorlogs') . '

'; @@ -121,6 +133,21 @@ public function page_content__main() { )); echo $html_errors ? $html_errors : '

' . __('No errors at the moment.', 'wpuerrorlogs') . '

'; + /* Latest by type */ + $fatal_errors = array_filter($errors, function ($item) { + return $item['type'] == 'php-fatal'; + }); + $latest_fatal_errors = $this->sort_errors_by_latest($fatal_errors, 10); + $html_errors = $this->basetoolbox->array_to_html_table($latest_fatal_errors, array( + 'table_classname' => 'widefat striped', + 'htmlspecialchars_td' => false, + 'colnames' => $colnames + )); + if ($html_errors) { + echo '

' . __('Latest fatal errors', 'wpuerrorlogs') . '

'; + echo $html_errors; + } + } /* ---------------------------------------------------------- @@ -146,6 +173,7 @@ function sort_errors_by_top($errors, $max_number = 5) { 'text' => $this->expand_error_text($text) ); } + $top_errors = $this->prepare_errors_for_display($top_errors); return $top_errors; } @@ -154,16 +182,27 @@ function sort_errors_by_latest($errors, $max_number = 5) { foreach ($latest_errors as $i => $error) { $latest_errors[$i]['text'] = $this->expand_error_text($error['text']); } + /* Reset keys */ + $latest_errors = array_values($latest_errors); + $latest_errors = $this->prepare_errors_for_display($latest_errors); return $latest_errors; } + function prepare_errors_for_display($errors) { + /* Prepare for display */ + $errors = array_map(function ($item) { + $item['text'] = $this->display_content_with_toggle($item['text']); + return $item; + }, $errors); + return $errors; + } + /* ---------------------------------------------------------- Extract logs from file ---------------------------------------------------------- */ - function get_logs() { + function get_logs($number_of_days) { - $number_of_days = 5; $previous_files = array(); /* Try to obtain previous files */