Skip to content

Commit

Permalink
Merge pull request #112 from Automattic/feature/fix/cs
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryJones authored Aug 16, 2024
2 parents 0b7e9d3 + 38f7792 commit 5e51557
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 185 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/cs-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ jobs:
xml-schema-file: ./vendor/phpunit/phpunit/phpunit.xsd

# Check the code-style consistency of the PHP files.
# - name: Check PHP code style
# continue-on-error: true
# run: vendor/bin/phpcs --report-full --report-checkstyle=./phpcs-report.xml
- name: Check PHP code style
continue-on-error: true
run: vendor/bin/phpcs --report-full --report-checkstyle=./phpcs-report.xml

# - name: Show PHPCS results in PR
# run: cs2pr ./phpcs-report.xml
- name: Show PHPCS results in PR
run: cs2pr ./phpcs-report.xml
14 changes: 12 additions & 2 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<file>.</file>
<!-- Ignoring Files and Folders:
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
<exclude-pattern>/node_modules/</exclude-pattern>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/tests/</exclude-pattern>

Expand Down Expand Up @@ -36,8 +37,17 @@
<!-- Rules: WordPress Coding Standards - see
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<!-- WordPress-Extra includes WordPress-Core -->
<rule ref="WordPress-Extra"/>
<rule ref="WordPress-Docs"/>
<rule ref="WordPress-Extra">
<exclude name="Universal.Operators.DisallowShortTernary.Found"/>
</rule>
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>widget.zone-posts.php</exclude-pattern>
<exclude-pattern>zoninator.php</exclude-pattern>
</rule>
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
<exclude-pattern>widget.zone-posts.php</exclude-pattern>
</rule>
<!-- <rule ref="WordPress-Docs"/>-->
<!-- For help in understanding these custom sniff properties:
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<config name="minimum_supported_wp_version" value="5.9"/>
Expand Down
1 change: 1 addition & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Yes, a prefix of z is too short, but fixing this is a breaking change.

function z_get_zoninator() {
global $zoninator;
Expand Down
69 changes: 35 additions & 34 deletions includes/class-zoninator-api-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function get_zones( $request ) {
return $this->bad_request(
array(
'message' => $results->get_error_message(),
)
)
);
}

Expand All @@ -142,23 +142,23 @@ public function get_zones( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function create_zone( $request ) {
$name = $this->_get_param( $request, 'name', '' );
$slug = $this->_get_param( $request, 'slug', $name );
$description = $this->_get_param( $request, 'description', '' );
$name = $this->get_param( $request, 'name', '' );
$slug = $this->get_param( $request, 'slug', $name );
$description = $this->get_param( $request, 'description', '' );

$result = $this->instance->insert_zone(
$slug,
$name,
array(
'description' => $description,
)
)
);

if ( is_wp_error( $result ) ) {
return $this->bad_request(
array(
'message' => $result->get_error_message(),
)
)
);
}

Expand All @@ -174,10 +174,10 @@ public function create_zone( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function update_zone( $request ) {
$zone_id = $this->_get_param( $request, 'zone_id', 0, 'absint' );
$name = $this->_get_param( $request, 'name', '' );
$slug = $this->_get_param( $request, 'slug', '' );
$description = $this->_get_param( $request, 'description', '', 'strip_tags' );
$zone_id = $this->get_param( $request, 'zone_id', 0, 'absint' );
$name = $this->get_param( $request, 'name', '' );
$slug = $this->get_param( $request, 'slug', '' );
$description = $this->get_param( $request, 'description', '', 'strip_tags' );

$zone = $this->instance->get_zone( $zone_id );
$update_params = array();
Expand All @@ -204,7 +204,7 @@ public function update_zone( $request ) {
return $this->bad_request(
array(
'message' => $result->get_error_message(),
)
)
);
}

Expand All @@ -218,7 +218,7 @@ public function update_zone( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function delete_zone( $request ) {
$zone_id = $this->_get_param( $request, 'zone_id', 0, 'absint' );
$zone_id = $this->get_param( $request, 'zone_id', 0, 'absint' );

$zone = $this->instance->get_zone( $zone_id );

Expand All @@ -232,7 +232,7 @@ public function delete_zone( $request ) {
return $this->bad_request(
array(
'message' => $result->get_error_message(),
)
)
);
}

Expand All @@ -246,7 +246,7 @@ public function delete_zone( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function get_zone_posts( $request ) {
$zone_id = $this->_get_param( $request, 'zone_id', 0, 'absint' );
$zone_id = $this->get_param( $request, 'zone_id', 0, 'absint' );

if ( empty( $zone_id ) || false === $this->instance->get_zone( $zone_id ) ) {
return $this->not_found( $this->translations[ self::INVALID_ZONE_ID ] );
Expand All @@ -268,8 +268,8 @@ public function get_zone_posts( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function update_zone_posts( $request ) {
$zone_id = $this->_get_param( $request, 'zone_id', 0, 'absint' );
$post_ids = $this->_get_param( $request, 'post_ids', array() );
$zone_id = $this->get_param( $request, 'zone_id', 0, 'absint' );
$post_ids = $this->get_param( $request, 'post_ids', array() );

if ( ! $this->instance->get_zone( $zone_id ) ) {
return $this->not_found( $this->translations[ self::INVALID_ZONE_ID ] );
Expand All @@ -281,7 +281,7 @@ public function update_zone_posts( $request ) {
return $this->bad_request(
array(
'message' => $this->translations[ self::INVALID_POST_ID ],
)
)
);
}

Expand All @@ -301,7 +301,7 @@ public function update_zone_posts( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function zone_update_lock( $request ) {
$zone_id = $this->_get_param( $request, 'zone_id', 0, 'absint' );
$zone_id = $this->get_param( $request, 'zone_id', 0, 'absint' );
if ( ! $zone_id ) {
return $this->_bad_request( self::ZONE_ID_REQUIRED, __( 'zone id required', 'zoninator' ) );
}
Expand Down Expand Up @@ -330,7 +330,7 @@ public function zone_update_lock( $request ) {
'timeout' => $this->instance->zone_lock_period,
'max_lock_period' => $this->instance->zone_max_lock_period,
),
200
200
);
}

Expand Down Expand Up @@ -371,7 +371,7 @@ public function get_zone_posts_permissions_check( $request ) {
* @return WP_Error|bool
*/
public function update_zone_permissions_check( $request ) {
$zone_id = $this->_get_param( $request, 'zone_id', 0, 'absint' );
$zone_id = $this->get_param( $request, 'zone_id', 0, 'absint' );
return $this->_permissions_check( 'update', $zone_id );
}

Expand All @@ -389,15 +389,16 @@ public function sanitize_string( $item ) {
}

/**
* @param WP_REST_Request $object
* @param $var
* @param string $default
* @param WP_REST_Request $rest_request
* @param string $key Parameter name.
* @param string $default_value
* @param string $sanitize_callback
*
* @return array|mixed|null|string
*/
private function _get_param( $object, $var, $default = '', $sanitize_callback = '' ) {
$value = $object->get_param( $var );
$value = empty( $value ) ? $default : $value;
private function get_param( $rest_request, $key, $default_value = '', $sanitize_callback = '' ) {
$value = $rest_request->get_param( $key );
$value = empty( $value ) ? $default_value : $value;

if ( is_callable( $sanitize_callback ) ) {
$value = ( is_array( $value ) ) ? array_map( $sanitize_callback, $value ) : call_user_func( $sanitize_callback, $value );
Expand All @@ -406,7 +407,7 @@ private function _get_param( $object, $var, $default = '', $sanitize_callback =
return $value;
}

public function _params_for_create_zone() {
public function _params_for_create_zone() { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
return array(
'name' => array(
'type' => 'string',
Expand All @@ -429,7 +430,7 @@ public function _params_for_create_zone() {
);
}

public function _params_for_update_zone() {
public function _params_for_update_zone() { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
return array(
'name' => array(
'type' => 'string',
Expand All @@ -449,7 +450,7 @@ public function _params_for_update_zone() {
);
}

public function _get_zone_id_param() {
public function _get_zone_id_param() { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
return array(
'zone_id' => array(
'type' => 'integer',
Expand All @@ -460,7 +461,7 @@ public function _get_zone_id_param() {
);
}

public function _get_zone_post_rest_route_params() {
public function _get_zone_post_rest_route_params() { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
$zone_params = $this->_get_zone_id_param();
return array_merge(
array(
Expand All @@ -471,11 +472,11 @@ public function _get_zone_post_rest_route_params() {
'items' => array( 'type' => 'integer' ),
),
),
$zone_params
$zone_params
);
}

public function _filter_zone_properties( $zone ) {
public function _filter_zone_properties( $zone ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
$data = $zone->to_array();

return array(
Expand All @@ -486,15 +487,15 @@ public function _filter_zone_properties( $zone ) {
);
}

private function _bad_request( $code, $message ) {
private function _bad_request( $code, $message ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
return new WP_Error( $code, $message, array( 'status' => 400 ) );
}

/**
* @param $zone_id
* @return bool|WP_Error
*/
private function _permissions_check( $action, $zone_id = null ) {
private function _permissions_check( $action, $zone_id = null ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
if ( ! $this->instance->check( $action, $zone_id ) ) {
return new WP_Error( self::PERMISSION_DENIED, __( "Sorry, you're not supposed to do that...", 'zoninator' ) );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-zoninator-api-filter-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function strip_slashes( $item ) {
}

public function strip_tags( $item ) {
return strip_tags( $item );
return wp_strip_all_tags( $item );
}

public function date_before_set( $model, $item ) {
Expand Down
3 changes: 2 additions & 1 deletion includes/class-zoninator-api-schema-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public function as_schema( $model_definition ) {
'$schema' => 'http://json-schema.org/schema#',
'title' => $model_definition->get_name(),
'type' => 'object',
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- applying a core filter.
'properties' => (array) apply_filters( 'rest_api_schema_properties', $properties, $model_definition ),
);

if ( $required !== array() ) {
if ( array() !== $required ) {
$schema['required'] = $required;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/zoninator_rest/class-zoninator-rest-classloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct( $prefix, $base_dir ) {
$this->prefix = $prefix;
$this->base_dir = $base_dir;
if ( ! is_dir( $this->base_dir ) ) {
throw new Exception( 'base_dir does not exist: ' . $this->base_dir );
throw new Exception( esc_html( 'base_dir does not exist: ' . $this->base_dir ) );
}
}

Expand Down Expand Up @@ -139,7 +139,7 @@ private function include_class_file( $path_to_the_class ) {
}

if ( ! file_exists( $path_to_the_class ) ) {
throw new Exception( $path_to_the_class . ' not found' );
throw new Exception( esc_html( $path_to_the_class . ' not found' ) );
}

$included = include_once $path_to_the_class;
Expand Down
24 changes: 13 additions & 11 deletions lib/zoninator_rest/class-zoninator-rest-environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class Zoninator_REST_Environment {
* @param Zoninator_REST_Bootstrap $bootstrap The bootstrap.
*/
public function __construct( $bootstrap ) {
$this->bootstrap = $bootstrap;
$this->type_registry = new Zoninator_REST_Type_Registry();
$this->bootstrap = $bootstrap;
$this->type_registry = new Zoninator_REST_Type_Registry();
$this->type_registry->initialize( $this );
// initialize our array vars.
$this->array_var( self::MODELS )
Expand Down Expand Up @@ -106,17 +106,18 @@ public function push_builder( $where, $builder ) {
/**
* Retrieve a previously defined Zoninator_REST_Model
*
* @param string $class the class name.
* @param string $class_name the class name.
*
* @return Zoninator_REST_Model the definition.
* @throws Zoninator_REST_Exception Throws in case the model is not registered.
*/
public function model( $class ) {
if ( ! class_exists( $class ) ) {
throw new Zoninator_REST_Exception( $class . ' does not exist' );
public function model( $class_name ) {
if ( ! class_exists( $class_name ) ) {
throw new Zoninator_REST_Exception( esc_html( $class_name . ' does not exist' ) );
}

Zoninator_REST_Expect::that( isset( $this->model_definitions[ $class ] ), $class . ' definition does not exist' );
return $this->model_definitions[ $class ];
Zoninator_REST_Expect::that( isset( $this->model_definitions[ $class_name ] ), $class_name . ' definition does not exist' );
return $this->model_definitions[ $class_name ];
}

/**
Expand Down Expand Up @@ -161,6 +162,7 @@ public function start() {
}

if ( false === $this->has_started ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
do_action( 'mt_environment_before_start', $this, get_class( $this ) );
$this->load_pending_builders( self::MODELS );
$this->load_pending_builders( self::BUNDLES );
Expand All @@ -180,7 +182,7 @@ public function start() {
* @param array $rest_apis The existing rest apis.
* @param Zoninator_REST_Environment $this The Environment.
*/
$rest_apis = (array) apply_filters( 'mt_environment_get_rest_apis', $this->rest_apis, $this );
$rest_apis = (array) apply_filters( 'mt_environment_get_rest_apis', $this->rest_apis, $this ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

foreach ( $rest_apis as $bundle ) {
/**
Expand All @@ -190,7 +192,7 @@ public function start() {
}

$this->has_started = true;
do_action( 'mt_environment_after_start', $this );
do_action( 'mt_environment_after_start', $this ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
}

return $this;
Expand Down Expand Up @@ -250,7 +252,7 @@ public function get( $name ) {
*
* @return mixed
*/
return apply_filters( 'mt_variable_get', $value, $this, $name );
return apply_filters( 'mt_variable_get', $value, $this, $name ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
}

/**
Expand Down
Loading

0 comments on commit 5e51557

Please sign in to comment.