Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: test-logging-plugin
- Plugin URI: http://none
- Description: contract work for visser-labs
- Version: 0.0.1
- Author: Aliaksei Herman
- Author URI: http://a-herrmann.by
- License: none
- */
- class WP_Test_Logging_Plugin {
- //required for the file name
- private $yymmddhhmmss;
- public function __construct() {
- add_action( 'admin_init', array( $this, 'register_my_setting' ) );
- add_action( 'all', array( $this, 'log_to' ) );
- add_action( 'admin_menu', array( $this, 'menu' ) );
- add_action( 'admin_print_styles', array( $this, 'plugin_theme_style' ) );
- add_action( 'template_redirect', array( $this, 'on_admin_form_submit' ) );
- $this->yymmddhhmmss = date( 'YMDHis' );
- }
- public function log_to() {
- $hook = current_filter();
- switch ( get_option( 'where_to_log_to', 1 ) ) {
- case 1:
- file_put_contents( plugin_dir_path( __FILE__ ) . '/log/'. $this->yymmddhhmmss . '.log', $this->udate( "Y-m-d H:i:s:u" ) . ' :: ' . $hook . "\n", FILE_APPEND | LOCK_EX );
- break;
- case 2:
- echo $hook;
- break;
- }
- }
- //gets the time in milliseconds
- private function udate( $format, $utimestamp = null ) {
- if ( is_null( $utimestamp ) )
- $utimestamp = microtime( true );
- $timestamp = floor( $utimestamp );
- $milliseconds = round( ( $utimestamp - $timestamp ) * 1000000 );
- return date( preg_replace( '`(?<!\\\\)u`', $milliseconds, $format ), $timestamp );
- }
- public function register_my_setting() {
- register_setting( 'WP_Test_Logging_Plugin', 'where_to_log_to');
- //add_option('where_to_load_to', 1, $autoload = 'yes');
- }
- public function on_admin_form_submit() {
- if ( isset( $_POST['selection'] ) ) {
- update_option( 'where_to_log_to', $_POST['selection'] );
- }
- }
- public function menu() {
- add_options_page( 'Logging Options', 'Logging Plugin', 'manage_options', 'my-unique-identifier', array( $this, 'plugin_options' ) );
- }
- public function plugin_theme_style() {
- wp_enqueue_style( 'plugin-theme', plugins_url( '/css/style.css', __FILE__ ), array(), '1.0' );
- }
- public function plugin_options() {
- if ( ! current_user_can( 'manage_options' ) ) {
- wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
- } ?>
- <div class="wrap">
- <div id="icon-options-logging" class="icon32"></div>
- <h2>Logging Plugin</h2>
- <form method="post" action="" name="form">
- <h3>Logging Settings</h3>
- <p>
- Select the plugin behavior.
- </p>
- <table class="form-table permalink-structure">
- <tbody>
- <tr>
- <th>
- <label>
- <input type="radio" checked="checked" value="1" name="selection">
- Log To File
- </label>
- </th>
- <td>
- The log is stored inside the plugin's "log" folder.
- </td>
- </tr>
- <tr>
- <th>
- <label>
- <input type="radio" checked="checked" value="2" name="selection">
- Log To Screen
- </label>
- </th>
- <td>
- All the information will be shown on screen.
- </td>
- </tr>
- </tbody>
- </table>
- <p class="submit">
- <input id="submit" class="button button-primary" type="submit" value="Save Changes" name="submit">
- </p>
- </form>
- </div>
- <?php //ar_dump(get_option('where_to_log_to', 1));
- }
- }
- new WP_Test_Logging_Plugin();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement