Skip to content

Commit

Permalink
Merge pull request #108 from petenelson/rc-1.4.0
Browse files Browse the repository at this point in the history
Rc 1.4.0
  • Loading branch information
petenelson authored Jan 24, 2017
2 parents f8da37c + 6a6e974 commit 76f2e17
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 116 deletions.
2 changes: 2 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ engines:
enabled: false
Controversial/CamelCaseMethodName:
enabled: false
Controversial/CamelCasePropertyName:
enabled: false
CleanCode/ElseExpression:
enabled: false
CleanCode/StaticAccess:
Expand Down
1 change: 0 additions & 1 deletion admin/class-wp-rest-api-log-admin-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function admin_init() {
add_action( 'restrict_manage_posts', array( $this, 'add_method_dropdown' ) );
add_action( 'restrict_manage_posts', array( $this, 'add_status_dropdown' ) );
add_action( 'restrict_manage_posts', array( $this, 'add_source_dropdown' ) );

}


Expand Down
8 changes: 1 addition & 7 deletions includes/class-wp-rest-api-log-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@
class WP_REST_API_Log_Common {

const PLUGIN_NAME = 'wp-rest-api-log';
const VERSION = '2016-12-05-01';
const VERSION = '2017-01-16-01';
const TEXT_DOMAIN = 'wp-rest-api-log';


public function plugins_loaded() {

}


static public function current_milliseconds() {
return self::microtime_to_milliseconds( microtime() );
}
Expand Down
38 changes: 19 additions & 19 deletions includes/class-wp-rest-api-log-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
class WP_REST_API_Log_Controller {


public function plugins_loaded() {
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
static function plugins_loaded() {
add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
}


public function register_rest_routes() {
static public function register_rest_routes() {

register_rest_route( WP_REST_API_Log_Common::PLUGIN_NAME, '/entries', array(
'methods' => array( WP_REST_Server::READABLE ),
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_permissions_check' ),
'callback' => array( __CLASS__, 'get_items' ),
'permission_callback' => array( __CLASS__, 'get_permissions_check' ),
'args' => array(
'from' => array(
'default' => '',
Expand Down Expand Up @@ -66,12 +66,12 @@ public function register_rest_routes() {

register_rest_route( WP_REST_API_Log_Common::PLUGIN_NAME, '/entry/(?P<id>[\d]+)', array(
'methods' => array( WP_REST_Server::READABLE ),
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_permissions_check' ),
'callback' => array( __CLASS__, 'get_item' ),
'permission_callback' => array( __CLASS__, 'get_permissions_check' ),
'args' => array(
'id' => array(
'sanitize_callback' => 'absint',
'validate_callback' => array( $this, 'validate_entry_id' ),
'validate_callback' => array( __CLASS__, 'validate_entry_id' ),
'default' => 0,
),
),
Expand All @@ -80,8 +80,8 @@ public function register_rest_routes() {

register_rest_route( WP_REST_API_Log_Common::PLUGIN_NAME, '/entry', array(
'methods' => array( WP_REST_Server::DELETABLE ),
'callback' => array( $this, 'delete_items' ),
'permission_callback' => array( $this, 'delete_items_permissions_check' ),
'callback' => array( __CLASS__, 'delete_items' ),
'permission_callback' => array( __CLASS__, 'delete_items_permissions_check' ),
'args' => array( // TODO refator delete, this won't work with $_REQUESTs
'older-than-seconds' => array(
'sanitize_callback' => 'absint', // TODO add validate callback
Expand All @@ -93,14 +93,14 @@ public function register_rest_routes() {

register_rest_route( WP_REST_API_Log_Common::PLUGIN_NAME, '/routes', array(
'methods' => array( WP_REST_Server::READABLE ),
'callback' => array( $this, 'get_routes' ),
'permission_callback' => array( $this, 'get_permissions_check' ),
'callback' => array( __CLASS__, 'get_routes' ),
'permission_callback' => array( __CLASS__, 'get_permissions_check' ),
) );

}


public function get_items( WP_REST_Request $request ) {
static public function get_items( WP_REST_Request $request ) {

$args = array(
'id' => $request['id'],
Expand All @@ -125,7 +125,7 @@ public function get_items( WP_REST_Request $request ) {
}


public function get_item( WP_REST_Request $request ) {
static public function get_item( WP_REST_Request $request ) {

$post = get_post( $request['id'] );
$entry = new WP_REST_API_Log_Entry( $args['id'] );
Expand All @@ -139,15 +139,15 @@ public function get_item( WP_REST_Request $request ) {
}


public function validate_entry_id( $id ) {
static public function validate_entry_id( $id ) {
if ( $id < 1 ) {
return new WP_Error( 'invalid_entry_id', sprintf( __( 'Invalid REST API Log ID %d.', 'wp-rest-api-log' ), $args['id'] ), array( 'status' => 404 ) );
} else {
return true;
}
}

public function get_routes( WP_REST_Request $request ) {
static public function get_routes( WP_REST_Request $request ) {

global $wpdb;

Expand All @@ -161,7 +161,7 @@ public function get_routes( WP_REST_Request $request ) {
}


public function delete_items( WP_REST_Request $request ) {
static public function delete_items( WP_REST_Request $request ) {
// TODO refactor
$args = array(
'older_than_seconds' => $request['older-than-seconds'],
Expand All @@ -172,12 +172,12 @@ public function delete_items( WP_REST_Request $request ) {
}


public function get_permissions_check() {
static public function get_permissions_check() {
return apply_filters( WP_REST_API_Log_Common::PLUGIN_NAME . '-can-view-entries', current_user_can( 'read_' . WP_REST_API_Log_DB::POST_TYPE ) );
}


public function delete_items_permissions_check() {
static public function delete_items_permissions_check() {
return apply_filters( WP_REST_API_Log_Common::PLUGIN_NAME . '-can-delete-entries', current_user_can( 'delete_' . WP_REST_API_Log_DB::POST_TYPE ) );
}

Expand Down
108 changes: 108 additions & 0 deletions includes/class-wp-rest-api-log-filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

if ( ! defined( 'ABSPATH' ) ) die( 'restricted access' );

class WP_REST_API_Log_Filters {

/**
* Returns a list of filtering modes.
*
* @return array
*/
static public function filter_modes() {
return array(
'' => __( 'All', 'wp-rest-api-log' ),
'log_matches' => __( 'Only Matching Filters', 'wp-rest-api-log' ),
'exclude_matches' => __( 'Exclude Matching Filters', 'wp-rest-api-log' ),
);
}


/**
* Converts a route filter into regex pattern.
*
* @param string $route_filter Route filter, may include * wildcards.
* @return string
*/
public static function route_to_regex( $route_filter ) {

if ( ! empty( $route_filter ) ) {

// If it starts with a carat, treat it as regex and
// make no changes.
if ( '^' === substr( $route_filter, 0, 1 ) ) {
return $route_filter;
} else {

// Replace wildcard with regex wildcard.
$route_filter = str_replace( '*', '.*', $route_filter );

// Add the start of the match.
$route_filter = '^' . $route_filter;

// Add the end of the match.
$route_filter .= '$';

// Convert backslash to literals.
$route_filter = str_replace( '/', "\/", $route_filter );
}

}

return $route_filter;
}

/**
* Determines if the supplied route can be logged.
*
* @param string $route Route (ex: /wp/v2).
* @return bool
*/
static public function can_log_route( $route ) {

// Get the filter mode.
$route_logging_mode = apply_filters( 'wp-rest-api-log-setting-get', 'routes', 'route-log-matching-mode' );

// If no logging mode is set, we can log the route.
if ( empty( $route_logging_mode ) ) {
return true;
}

// Get the route filters.
$route_filters = apply_filters( 'wp-rest-api-log-setting-get', 'routes', 'route-filters' );
$route_filters = array_values( array_map( 'trim', explode( "\n", $route_filters ) ) );

// If we're set to exclude matching filters, but we have no filters,
// then the route can be logged
if ( 'exclude_matches' === $route_logging_mode && empty( $route_filters ) ) {
return true;
}

// Loop through the filters and apply each one to the route.
foreach( $route_filters as $route_filter ) {
if ( empty( $route_filter ) ) {
continue;
}

$regex = self::route_to_regex( $route_filter );

//preg_match() returns 1 if the pattern matches given subject,
//0 if it does not, or FALSE if an error occurred.
$match = preg_match( '/' . $regex . '/', $route );

// We can log this if the mode is set to log only matches.
if ( 1 === $match && 'log_matches' === $route_logging_mode ) {
return true;
}

// We cannot log this if the mode is set to exclude matches.
if ( 1 === $match && 'exclude_matches' === $route_logging_mode ) {
return false;
}
}

// At this point, we can only log the match if we're set to exclude
// mode and the loop above did not find a match.
return 'exclude_matches' === $route_logging_mode;
}
}
10 changes: 3 additions & 7 deletions includes/class-wp-rest-api-log-i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
class WP_REST_API_Log_i18n {


public function plugins_loaded() {
static public function plugins_loaded() {

load_plugin_textdomain(
WP_REST_API_Log_Common::TEXT_DOMAIN,
'wp-rest-api-log',
false,
dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/'
);

}


} // end class

}
}
31 changes: 12 additions & 19 deletions includes/class-wp-rest-api-log-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

if ( ! defined( 'ABSPATH' ) ) die( 'restricted access' );

if ( ! class_exists( 'WP_REST_API_Log_Post_type' ) ) {
if ( ! class_exists( 'WP_REST_API_Log_Post_Type' ) ) {

class WP_REST_API_Log_Post_type {
class WP_REST_API_Log_Post_Type {

public function plugins_loaded() {
add_action( 'init', array( $this, 'register_custom_post_types' ) );
add_action( 'init', array( $this, 'register_custom_taxonomies' ) );
static public function plugins_loaded() {
add_action( 'init', array( __CLASS__, 'register_custom_post_types' ) );
add_action( 'init', array( __CLASS__, 'register_custom_taxonomies' ) );

}

public function register_custom_post_types() {
static public function register_custom_post_types() {

$args = $this->get_post_type_args();
$args = self::get_post_type_args();

register_post_type( WP_REST_API_Log_DB::POST_TYPE, $args );

}


public function get_post_type_labels() {
static public function get_post_type_labels() {

$labels = array(
'name' => esc_html__( 'REST API Log Entries', 'ms-research' ),
Expand All @@ -41,10 +40,10 @@ public function get_post_type_labels() {
}


public function get_post_type_args() {
static public function get_post_type_args() {

$args = array(
'labels' => $this->get_post_type_labels(),
'labels' => self::get_post_type_labels(),
'show_in_rest' => true,
'rest_base' => WP_REST_API_Log_DB::POST_TYPE, // allows the CPT to show up in the native API
'hierarchical' => false,
Expand Down Expand Up @@ -75,7 +74,7 @@ public function get_post_type_args() {
}


public function register_custom_taxonomies() {
static public function register_custom_taxonomies() {

// HTTP Method

Expand Down Expand Up @@ -112,12 +111,6 @@ public function register_custom_taxonomies() {
$args['labels']['singular_name'] = __( 'Log Sources', 'wp-rest-api-log' );

register_taxonomy( WP_REST_API_Log_DB::TAXONOMY_SOURCE, array( WP_REST_API_Log_DB::POST_TYPE ), $args );

// namespace?
}


}

}
}
Loading

0 comments on commit 76f2e17

Please sign in to comment.