SHARE
TWEET

Untitled

a guest Sep 29th, 2013 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: test-logging-plugin
  4. Plugin URI: http://none
  5. Description: contract work for visser-labs
  6. Version: 0.0.1
  7. Author: Aliaksei Herman
  8. Author URI: http://a-herrmann.by
  9. License: none
  10. */
  11.  
  12. class WP_Test_Logging_Plugin {
  13.    
  14.     //required for the file name
  15.     private $yymmddhhmmss;
  16.    
  17.     public function __construct() {
  18.         add_action( 'admin_init', array( $this, 'register_my_setting' ) );
  19.         add_action( 'all', array( $this, 'log_to' ) );
  20.         add_action( 'admin_menu', array( $this, 'menu' ) );
  21.         add_action( 'admin_print_styles', array( $this, 'plugin_theme_style' ) );
  22.         add_action( 'template_redirect', array( $this, 'on_admin_form_submit' ) );
  23.         $this->yymmddhhmmss = date( 'YMDHis' );
  24.     }
  25.    
  26.     public function log_to() {
  27.         $hook = current_filter();
  28.         switch ( get_option( 'where_to_log_to', 1 ) ) {
  29.             case 1:
  30.                 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 );
  31.                 break;
  32.             case 2:
  33.                 echo $hook;
  34.                 break;
  35.         }
  36.     }
  37.  
  38.     //gets the time in milliseconds
  39.     private function udate( $format, $utimestamp = null ) {
  40.         if ( is_null( $utimestamp ) )
  41.             $utimestamp = microtime( true );
  42.  
  43.         $timestamp      = floor( $utimestamp );
  44.         $milliseconds   = round( ( $utimestamp - $timestamp ) * 1000000 );
  45.  
  46.         return date( preg_replace( '`(?<!\\\\)u`', $milliseconds, $format ), $timestamp );
  47.     }
  48.    
  49.     public function register_my_setting() {
  50.         register_setting( 'WP_Test_Logging_Plugin', 'where_to_log_to');
  51.         //add_option('where_to_load_to', 1, $autoload = 'yes');
  52.     }
  53.    
  54.     public function on_admin_form_submit() {
  55.         if ( isset( $_POST['selection'] ) ) {
  56.             update_option( 'where_to_log_to', $_POST['selection'] );
  57.         }
  58.     }
  59.    
  60.     public function menu() {
  61.         add_options_page( 'Logging Options', 'Logging Plugin', 'manage_options', 'my-unique-identifier', array( $this, 'plugin_options' ) );
  62.     }
  63.    
  64.     public function plugin_theme_style() {
  65.         wp_enqueue_style( 'plugin-theme', plugins_url( '/css/style.css', __FILE__ ), array(), '1.0' );
  66.     }
  67.    
  68.     public function plugin_options() {
  69.         if ( ! current_user_can( 'manage_options' ) ) {
  70.             wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
  71.         } ?>
  72.         <div class="wrap">
  73.             <div id="icon-options-logging" class="icon32"></div>
  74.             <h2>Logging Plugin</h2>
  75.             <form method="post" action="" name="form">
  76.                 <h3>Logging Settings</h3>
  77.                 <p>
  78.                     Select the plugin behavior.
  79.                 </p>
  80.                 <table class="form-table permalink-structure">
  81.                     <tbody>
  82.                         <tr>
  83.                             <th>
  84.                                 <label>
  85.                                     <input type="radio" checked="checked" value="1" name="selection">
  86.                                     Log To File
  87.                                 </label>
  88.                             </th>
  89.                             <td>
  90.                                 The log is stored inside the plugin's "log" folder.
  91.                             </td>
  92.                         </tr>
  93.                         <tr>
  94.                             <th>
  95.                                 <label>
  96.                                     <input type="radio" checked="checked" value="2" name="selection">
  97.                                     Log To Screen
  98.                                 </label>
  99.                             </th>
  100.                             <td>
  101.                                 All the information will be shown on screen.
  102.                             </td>
  103.                         </tr>
  104.                     </tbody>
  105.                 </table>
  106.                 <p class="submit">
  107.                     <input id="submit" class="button button-primary" type="submit" value="Save Changes" name="submit">
  108.                 </p>
  109.             </form>
  110.         </div>
  111.     <?php //ar_dump(get_option('where_to_log_to', 1));
  112.     }
  113. }
  114. new WP_Test_Logging_Plugin();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top