Skip to content

Commit

Permalink
Issue #69 - move shortcodes from oik and add bwtrace option=logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed May 14, 2018
1 parent 7da109e commit 86b21a6
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 0 deletions.
1 change: 1 addition & 0 deletions includes/class-trace-file-selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function set_file_path( $file_path=null ) {
*
* If the constant is not set it determines it based on this file's location.
*
* @return string fully qualified path with trailing slash
*/
public function get_abspath() {
if ( !defined('ABSPATH') ) {
Expand Down
166 changes: 166 additions & 0 deletions includes/class-trace-logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

/**
* @copyright (C) Copyright Bobbing Wide 2018
* @package oik-bwtrace
*
* Trace logs: Summary and purging
*
*/
class trace_logs {

private $options;
private $trace_files;
private $min_date;
private $max_date;

public function __construct() {
$this->get_options();
}

/**
* Displays trace log summary
*
*
* Type | Path | Files | Size | From | To
* -- | -- | -- | -- | -- | --
* daily | bwtrace.vt | 161 | 3,956,420 | 2017-05-17 | 2018-04-27
* browser | bwtraces.loh | 1095 | 1,908,844,717 | 2018-03-12 | 2018-04-27
* ajax | bwtrace.ajax | 969 | 228,329,472 | 2018-03-13 | 2018-04-16
* rest | bwtraces.rest | 1141 | 1,544,215,267 | 2018-03-27 | 2018-04-16
* cli | bwtraces.cli | 112 | 100,209,273 | 2018-03-11 | 2018-04-03
*/
function display_summary() {
stag( "table", "widefat" );
stag( "thead" );
bw_tablerow( bw_as_array( "Type,Path,Files,Size,From,To" ), "tr", "th" );
etag( "thead" );
stag( "tbody" );
$this->summarise( "daily", "bwtrace.vt" );
$this->summarise( "browser", $this->get_option_value( "file" ) );
$this->summarise( "ajax", $this->get_option_value( "file_ajax" ) );
$this->summarise( "rest", $this->get_option_value( "file_rest" ) );
$this->summarise( "cli", $this->get_option_value( "file_cli" ) );
etag( "tbody" );
etag( "table" );
}

/**
* Load the trace options
*/
private function get_options() {
$this->options = get_option( "bw_trace_options" );
}

/**
* Gets an option value
*
* Doesn't support defaults
*
* @param string $name
* @return string | null
*/
private function get_option_value( $name ) {
$value = bw_array_get( $this->options, $name, null );
return $value;
}

/**
* Summarise log files for a tracing type
*
*
*/
private function summarise( $type, $path ) {
$record = array();
$record[] = $type;
$record[] = $path;

$file_mask = ABSPATH . $path;

$files = $this->query_files( $file_mask);
$record[] = count( $files );
$record[] = number_format( $this->size( $files ) );
$this->date_range( $files );
$record[] = $this->min_date;
$record[] = $this->max_date;
bw_tablerow( $record );
}

/**
* Queries trace files given the file mask
*
* We need them sorted in the natural sort sequence
* where .2 is greater than .11
*
* @param string $file_mask
* @return array of fully qualified file names
*/
public function query_files( $file_mask ) {
$files = glob( $file_mask . ".*", GLOB_NOSORT );
//$files = $this->trim_to_limit( $files, $file_mask, $this->limit );
natsort( $files );
$this->trace_files = $files;
return $this->trace_files;
}

/**
* Determine the total size of the files
*
* @param array $files array of fully qualified file names
* @return integer total file size, in bytes
*/
public function size( $files ) {
$total = 0;
foreach ( $files as $file ) {
$filesize = filesize( $file );
$total += $filesize;
}
return $total;
}

/**
* Determines the date range spanned by the files
*
* Can be used to indicate the level of pruning required
*
* Sets $this->min_date and $this->max_date in format Y-m-d
*
* @param array $file array of fully qualified file names
*
*/
public function date_range( $files ) {
$this->min_date = null;
$this->max_date = null;
if ( count( $files ) ) {
$this->min_date = date( "Y-m-d" );
}
foreach ( $files as $file ) {
$filemtime = filemtime( $file );
$filedate = date( "Y-m-d", $filemtime );
if ( $filedate < $this->min_date ) {
$this->min_date = $filedate;
}
if ( $filedate > $this->max_date ) {
$this->max_date = $filedate;
}

}
}

/**
* Implement a delete method that can be used to tidy trace log files
*
* This would be called by the `wp trace delete <type> --retain=1` subcommand
*
*/

public function delete( $args, $assoc_args ) {


}





}
13 changes: 13 additions & 0 deletions oik-bwtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ function oik_bwtrace_plugins_loaded() {
}

add_action( "oik_admin_menu", "oik_bwtrace_admin_menu" );

add_action( "oik_add_shortcodes", "oik_bwtrace_add_shortcodes", 11 );
}

/**
Expand Down Expand Up @@ -236,6 +238,17 @@ function oik_bwtrace_admin_menu() {
bw_load_plugin_textdomain( 'oik-bwtrace' );
}

/**
* Adds the trace shortcodes
*/
function oik_bwtrace_add_shortcodes() {

bw_add_shortcode( 'bwtron', 'bw_trace_on', oik_path( "shortcodes/oik-trace.php", "oik-bwtrace") , false );
bw_add_shortcode( 'bwtroff', 'bw_trace_off', oik_path( "shortcodes/oik-trace.php", "oik-bwtrace" ) , false );
bw_add_shortcode( 'bwtrace', 'bw_trace_button', oik_path( "shortcodes/oik-trace.php", "oik-bwtrace" ) , false );

}

function oik_bwtrace_wp_cli() {
if ( defined( "WP_CLI" ) && WP_CLI ) {
WP_CLI::debug( "WP-CLI is active. Loading trace command" );
Expand Down
115 changes: 115 additions & 0 deletions shortcodes/oik-trace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
if ( defined( 'OIK_TRACE_SHORTCODES_INCLUDED' ) ) return;
define( 'OIK_TRACE_SHORTCODES_INCLUDED', true );
/*
Copyright 2012-2018 Bobbing Wide (email : herb@bobbingwide.com )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2,
as published by the Free Software Foundation.
You may NOT assume that you can use any other version of the GPL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The license for this software can likely be found here:
http://www.gnu.org/licenses/gpl-2.0.html
*/

/**
* Syntax for [bwtrace] shortcode
*
* Note that the function name is based on the shortcode not the implementing function.
* This enables both the shortcode and the help and examples to be implemented as lazy functions
* **?** Actually it's an oversight! Herb 2012/03/20
*/
function bwtrace__syntax( $shortcode='bwtrace' ) {
$syntax = array( "text" => BW_::bw_skv( " ", __( "text", "oik" ), __( "text for the trace button", "oik" ) )
, "option" => BW_::bw_skv( "", "view|reset|logs", __( "trace control links to display", "oik" ) )
, "type" => BW_::bw_skv( "browser", "ajax|rest|cli", __( "Trace type" ) )
);
return( $syntax );
}

/**
* Displays examples for [bwtrace] shortcode
*/
function bwtrace__example( $shortcode='bwtrace' ) {
bw_invoke_shortcode( $shortcode, null, __( "To display the trace options and trace reset buttons.", "oik" ) );
bw_invoke_shortcode( $shortcode, "option=view", __( "To display a link to the active trace file.", "oik" ) );
bw_invoke_shortcode( $shortcode, "option=reset", __( "To display the trace reset only", "oik" ) );
}

/**
* Implements [bwtrace] shortcode
*
* Shortcode for toggling or setting trace options
* Provide a button for controlling trace
*
* @param array $atts - shortcode options
* option=view, reset, other
* text=text for the trace options button
* @return string - the expanded shortcode. If trace is not enabled it returns null.
*/
function bw_trace_button( $atts=NULL ) {
global $bw_trace_on;
if ( $bw_trace_on ) {
$option = bw_array_get( $atts, 'option', NULL );

switch ( $option ) {
case 'view':
oik_require2( 'admin/oik-bwtrace.php', "oik-bwtrace" );
$bw_trace_url = bw_trace_url();
BW_::alink( "button", $bw_trace_url, __( "View trace log", "oik" ), sprintf( __( 'View trace output in your browser. %1$s', "oik" ), $bw_trace_url ) );
break;

case 'reset':
bw_trace_reset_form();
break;

case 'logs':
bw_trace_logs( $atts );
break;

default:
$url = get_site_url( NULL, 'wp-admin/options-general.php?page=bw_trace_options' );
$text = bw_array_get( $atts, 'text', __( "Trace options", "oik" ) );
//$img = retimage( null, oik_url( 'images/oik-trace_48.png'), $text );
BW_::alink( "button", $url, $text, $text );
bw_trace_reset_form();
break;

}
} else {

}
return( bw_ret());
}

/**
* Creates the Trace reset button for use somewhere in any page
*/
function bw_trace_reset_form() {
oik_require( "bobbforms.inc" );
e( '<form method="post" action="" class="bw_inline">' );
e( isubmit( "_bw_trace_reset", __( "Trace reset", "oik" ), null ));
etag( "form" );
}


/**
* Displays trace log summary
*/
function bw_trace_logs( $atts ) {
oik_require( "includes/class-trace-logs.php", "oik-bwtrace" );
$trace_logs = new trace_logs();
$trace_logs->display_summary();
}



0 comments on commit 86b21a6

Please sign in to comment.