Skip to content

Commit

Permalink
Merge pull request #107 from petenelson/code-cleanup
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
petenelson authored Jan 24, 2017
2 parents f7f8349 + 1ea93e6 commit 0597d26
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 97 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
6 changes: 0 additions & 6 deletions includes/class-wp-rest-api-log-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ class WP_REST_API_Log_Common {
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
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?
}


}

}
}
92 changes: 50 additions & 42 deletions includes/settings/class-wp-rest-api-log-settings-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,46 @@ class WP_REST_API_Log_Settings_Routes extends WP_REST_API_Log_Settings_Base {

static $settings_key = 'wp-rest-api-log-settings-routes';


/**
* Hooks up WorPress actions and filters.
*
* @return void
*/
static public function plugins_loaded() {
add_action( 'admin_init', array( __CLASS__, 'register_general_settings' ) );
add_action( 'admin_init', array( __CLASS__, 'register_routes_settings' ) );
add_filter( 'wp-rest-api-log-settings-tabs', array( __CLASS__, 'add_tab') );
}


/**
* Adds a Routes tab.
*
* @param array $tabs List of tabs.
* @return array
*/
static public function add_tab( $tabs ) {
$tabs[ self::$settings_key ] = __( 'Routes', 'wp-rest-api-log' );
return $tabs;
}


/**
* Gets the default Routes settings.
*
* @return array
*/
static public function get_default_settings() {
return array(
'ignore-core-oembed' => '1',
'ignore-core-oembed' => '1',
'route-log-matching-mode' => '',
'route-filters' => '',
);
}


static public function register_general_settings() {
/**
* Registers settings sections and fields for the Routes tab.
*
* @return void
*/
static public function register_routes_settings() {
$key = self::$settings_key;

register_setting( $key, $key, array( __CLASS__, 'sanitize_settings') );
Expand All @@ -46,19 +65,22 @@ static public function register_general_settings() {
array(
'key' => $key,
'name' => 'ignore-core-oembed',
'after' => '<p class="description">Built-in /oembed/1.0/embed route</p>'
'after' => '<p class="description">' . __( 'Built-in /oembed/1.0/embed route', 'wp-rest-api-log' ) . '</p>',
)
);

add_settings_field(
'route-log-matching-mode',
__( 'Route Logging Mode', 'wp-rest-api-log' ),
array( __CLASS__, 'log_matching_mode_dropdown' ),
array( __CLASS__, 'settings_check_radio_list' ),
$key,
$section,
array(
'key' => $key,
'name' => 'route-log-matching-mode',
'type' => 'radio',
'items' => WP_REST_API_Log_Filters::filter_modes(),
'default' => array( '' ),
)
);

Expand All @@ -72,50 +94,36 @@ static public function register_general_settings() {
'key' => $key,
'name' => 'route-filters',
'after' => '
<p class="description">One route per line, examples</p>
<p class="description">' . __( 'One route per line, examples', 'wp-rest-api-log' ) . '</p>
<ul>
<li>Exact Match: /wp/v2/posts</li>
<li>Wildcard Match: /wp/v2/*</li>
<li>Regex: ^/wp/v2/.*$</li>
<li>' . __( 'Exact Match', 'wp-rest-api-log' ) . ': /wp/v2/posts</li>
<li>' . __( 'Wildcard Match', 'wp-rest-api-log' ) . ': /wp/v2/*</li>
<li>' . __( 'Regex', 'wp-rest-api-log' ) . ': ^\/wp\/v2\/.*$</li>
</ul>
<p class="description">Regex matches must start with ^</p>
',
<p class="description">' . __( 'Regex matches must start with ^', 'wp-rest-api-log' ) . '</p>',
)
);
}

/**
* Callback for outputting the log matching mode.
* Sanitzes the route settings.
*
* @param array $args
* @return void
* @param array $settings List of settings.
* @return array
*/
static public function log_matching_mode_dropdown( $args ) {

$args = wp_parse_args( $args, array(
'key' => '',
'name' => '',
)
);

$option = get_option( $args['key'] );
$mode = isset( $option[ $args['name'] ] ) ? $option[ $args['name'] ] : '';

// Get the list of filter modes.
$filter_modes = WP_REST_API_Log_Filters::filter_modes();

?>
<select name="<?php echo esc_attr( "{$args['key']}[{$args['name']}]"); ?>">
<?php foreach( $filter_modes as $value => $name ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $mode ); ?>><?php echo esc_html( $name ); ?></option>
<?php endforeach; ?>
</select>
<?php

}
static public function sanitize_settings( $settings ) {

// Sanitize string fields.
$string_fields = array(
'ignore-core-oembed',
'route-log-matching-mode',
);

static public function sanitize_settings( $settings ) {
foreach( $string_fields as $field ) {
if ( isset( $settings[ $field ] ) ) {
$settings[ $field ] = filter_var( $settings[ $field ], FILTER_SANITIZE_STRING );
}
}

return $settings;
}
Expand Down
Loading

0 comments on commit 0597d26

Please sign in to comment.