Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions includes/Admin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

function commonsbooking_admin() {
/**
* Enqueue scripts and styles for admin pages.
*
* @return void
*/
function commonsbooking_admin(): void {
// jQuery
wp_enqueue_script( 'jquery' );

Expand Down Expand Up @@ -213,14 +218,14 @@ function commonsbooking_sanitizeHTML( $string ): string {
/**
* Create filter hooks for cmb2 fields
*
* @param array $field_args Array of field args.
* @param array<string, mixed> $field_args Array of field args.
*
*
* : https://cmb2.io/docs/field-parameters#-default_cb
*
* @return mixed
*/
function commonsbooking_filter_from_cmb2( $field_args ) {
function commonsbooking_filter_from_cmb2( array $field_args ) {
// Only return default value if we don't have a post ID (in the 'post' query variable)
if ( isset( $_GET['post'] ) ) {
// No default value.
Expand Down Expand Up @@ -260,14 +265,14 @@ function cmb2_set_checkbox_default_for_new_post() {
/**
* Recursive sanitation for text or array
*
* @param array|string $data
* @param array<mixed>|string $data
* @param string $sanitizeFunction name of the sanitziation function, default = sanitize_text_field. You can use any method that accepts a string as parameter
*
* See more wordpress sanitization functions: https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/
*
* @return array|string
* @return array<mixed>|string
*/
function commonsbooking_sanitizeArrayorString( $data, $sanitizeFunction = 'sanitize_text_field' ) {
function commonsbooking_sanitizeArrayorString( array|string $data, string $sanitizeFunction = 'sanitize_text_field' ): array|string {
if ( is_array( $data ) ) {
foreach ( $data as $key => $value ) {
$data[ $key ] = commonsbooking_sanitizeArrayorString( $value, $sanitizeFunction );
Expand Down
16 changes: 13 additions & 3 deletions includes/Public.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
use CommonsBooking\View\View;


function commonsbooking_public() {
/**
* Enqueue scripts and styles for public-facing pages.
*
* @return void
*/
function commonsbooking_public(): void {

wp_enqueue_style(
'cb-styles-public',
Expand Down Expand Up @@ -143,8 +148,13 @@ function commonsbooking_public() {
add_action( 'wp_ajax_cb_map_geo_search', array( MapData::class, 'geo_search' ) );
add_action( 'wp_ajax_nopriv_cb_map_geo_search', array( MapData::class, 'geo_search' ) );

// Query vars
function commonsbooking_query_vars( $qvars ) {
/**
* Register custom query vars for CommonsBooking.
*
* @param string[] $qvars Existing query vars.
* @return string[]
*/
function commonsbooking_query_vars( array $qvars ): array {
$qvars[] = 'cb-location';
$qvars[] = 'cb-item';
$qvars[] = 'cb-type';
Expand Down
10 changes: 8 additions & 2 deletions includes/Shortcodes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php

function commonsbooking_tag( $atts ) {
/**
* Shortcode handler for [cb] shortcode.
*
* @param array<string, string>|string $atts Shortcode attributes.
* @return string
*/
function commonsbooking_tag( array|string $atts ): string {
$atts = shortcode_atts(
array(
'tag' => '',
Expand All @@ -9,7 +15,7 @@ function commonsbooking_tag( $atts ) {
'cb'
);

echo commonsbooking_sanitizeHTML( commonsbooking_parse_shortcode( $atts['tag'] ) );
return commonsbooking_sanitizeHTML( commonsbooking_parse_shortcode( $atts['tag'] ) );
}

add_shortcode( 'cb', 'commonsbooking_tag' );
14 changes: 10 additions & 4 deletions includes/TemplateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* Parses templates and extracts the template tags used in e-mail templates: {{xxx:yyyy}}
*
* @param string $template
* @param array $objects
* @param array<string, mixed> $objects
* @param callable $sanitizeFunction The callable used to remove unwanted tags/characters (use default 'commonsbooking_sanitizeHTML' or 'sanitize_text_field')
*
* @return mixed
*/
function commonsbooking_parse_template( string $template = '', $objects = [], $sanitizeFunction = 'commonsbooking_sanitizeHTML' ) {
function commonsbooking_parse_template( string $template = '', array $objects = [], $sanitizeFunction = 'commonsbooking_sanitizeHTML' ) {
$template = preg_replace_callback(
'/\{{.*?\}}/',
function ( $match ) use ( $objects, $sanitizeFunction ) {
Expand Down Expand Up @@ -40,7 +40,13 @@ function ( $match ) use ( $objects, $sanitizeFunction ) {
}
}

function commonsbooking_parse_shortcode( $tag ) {
/**
* Parses a single shortcode tag.
*
* @param string $tag
* @return mixed
*/
function commonsbooking_parse_shortcode( string $tag ) {
$tag = (array) $tag;
return commonsbooking_parse_template_callback( $tag );
}
Expand All @@ -52,7 +58,7 @@ function commonsbooking_parse_shortcode( $tag ) {
* Example: {{[this comes before: ]item:post_title[this comes after]}}
*
* @param mixed $match
* @param array $objects
* @param array<string, mixed> $objects
* @param callable $sanitizeFunction The callable used to remove unwanted tags/characters
*
* @return false|mixed
Expand Down
56 changes: 38 additions & 18 deletions includes/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
/**
* Checks if current user is allowed to edit custom post.
*
* @param $post
* @param \WP_Post|int $post
*
* @return bool
* @throws Exception
*/
function commonsbooking_isCurrentUserAllowedToEdit( $post ): bool {
function commonsbooking_isCurrentUserAllowedToEdit( \WP_Post|int $post ): bool {
if ( ! is_user_logged_in() ) {
return false; }

Expand All @@ -24,13 +24,13 @@ function commonsbooking_isCurrentUserAllowedToEdit( $post ): bool {
/**
* Checks if user is allowed to edit custom post.
*
* @param $post
* @param $user
* @param \WP_Post|int $post
* @param WP_User $user
*
* @return bool
* @throws Exception
*/
function commonsbooking_isUserAllowedToEdit( $post, WP_User $user ): bool {
function commonsbooking_isUserAllowedToEdit( \WP_Post|int $post, WP_User $user ): bool {

if ( ! Plugin::isPostCustomPostType( $post ) ) {
return false;
Expand All @@ -52,9 +52,10 @@ function commonsbooking_isUserAllowedToEdit( $post, WP_User $user ): bool {
/**
* Validates if current user is allowed to edit current post in admin.
*
* @param $current_screen
* @param \WP_Screen $current_screen
* @return void
*/
function commonsbooking_validate_user_on_edit( $current_screen ) {
function commonsbooking_validate_user_on_edit( \WP_Screen $current_screen ): void {
if ( $current_screen->base == 'post' && in_array( $current_screen->id, Plugin::getCustomPostTypesLabels() ) ) {
if ( array_key_exists( 'action', $_GET ) && $_GET['action'] == 'edit' ) {
$post = get_post( intval( $_GET['post'] ) );
Expand Down Expand Up @@ -127,14 +128,25 @@ function ( $posts, $query ) {
add_filter( 'views_edit-' . $custom_post_type, 'commonsbooking_custom_view_count', 10, 1 );
}

// Filter function for fix of counts in admin lists for custom post types.
function commonsbooking_custom_view_count( $views ) {
/**
* Filter function for fix of counts in admin lists for custom post types.
*
* @param array<string, string> $views
* @return array<string, string>
*/
function commonsbooking_custom_view_count( array $views ): array {
global $current_screen;
return commonsbooking_fix_view_counts( str_replace( 'edit-', '', $current_screen->id ), $views );
}

// fixes counts for custom posts countings in admin list
function commonsbooking_fix_view_counts( $postType, $views ) {
/**
* Fixes counts for custom posts countings in admin list.
*
* @param string $postType
* @param array<string, string> $views
* @return array<string, string>
*/
function commonsbooking_fix_view_counts( string $postType, array $views ): array {
// admin is allowed to see all posts
if ( commonsbooking_isCurrentUserAdmin() ) {
return $views;
Expand Down Expand Up @@ -165,8 +177,12 @@ function commonsbooking_fix_view_counts( $postType, $views ) {
return array_intersect_key( $views, $counts );
}

// Check if current user has admin role
function commonsbooking_isCurrentUserAdmin() {
/**
* Check if current user has admin role.
*
* @return bool
*/
function commonsbooking_isCurrentUserAdmin(): bool {
if ( ! is_user_logged_in() ) {
return false; }
$user = wp_get_current_user();
Expand Down Expand Up @@ -225,8 +241,12 @@ function commonsbooking_isUserCBManager( \WP_User $user ): bool {
return apply_filters( 'commonsbooking_isCurrentUserCBManager', $isManager, $user );
}

// Check if current user has subscriber role
function commonsbooking_isCurrentUserSubscriber() {
/**
* Check if current user has subscriber role.
*
* @return bool
*/
function commonsbooking_isCurrentUserSubscriber(): bool {
$user = wp_get_current_user();

$isSubscriber = in_array( 'subscriber', $user->roles );
Expand Down Expand Up @@ -340,12 +360,12 @@ function commonsbooking_isUserAllowedToSee( $post, WP_User $user ): bool {
*
* Used by Service\iCalendar for authentication.
*
* @param $user_id
* @param $user_hash
* @param int|string $user_id
* @param string $user_hash
*
* @return bool
*/
function commonsbooking_isUIDHashComboCorrect( $user_id, $user_hash ): bool {
function commonsbooking_isUIDHashComboCorrect( int|string $user_id, string $user_hash ): bool {
if ( wp_hash( $user_id ) == $user_hash ) {
return true;
} else {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ parameters:
- commonsbooking.php
- src/
- includes/
level: 5
level: 6
ignoreErrors:
- '#Constant (COMMONSBOOKING.*|WP_DEBUG_LOG) not found.#'
# - '#Instantiated class (CommonsBooking.*CB_Data) not found.#'
Expand Down
12 changes: 6 additions & 6 deletions src/API/AvailabilityRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
/**
* This retrieves bookable timeframes and the different items assigned, with their respective availability.
*
* @param bool $id The id of a {@see \CommonsBooking\Wordpress\CustomPostType\Item::post_type} post to search for
* @param bool|int $id The id of a {@see \CommonsBooking\Wordpress\CustomPostType\Item::post_type} post to search for
*
* @return array
* @return array<int, mixed>
* @throws Exception
*/
public function getItemData( $id = false ): array {
Expand All @@ -57,11 +57,11 @@
/**
* Get one item from the collection
*
* @param $request WP_REST_Request
* @param WP_REST_Request<array<string, mixed>> $request
*
* @return WP_REST_Response|WP_Error
*/
public function get_item( $request ) {
public function get_item( WP_REST_Request $request ) {

Check failure on line 64 in src/API/AvailabilityRoute.php

View workflow job for this annotation

GitHub Actions / WP latest on PHP 8.5

Parameter #1 $request (WP_REST_Request) of method CommonsBooking\API\AvailabilityRoute::get_item() is not contravariant with parameter #1 $request (mixed) of method WP_REST_Controller::get_item().

Check failure on line 64 in src/API/AvailabilityRoute.php

View workflow job for this annotation

GitHub Actions / WP latest on PHP 8.5

Parameter #1 $request (WP_REST_Request) of method CommonsBooking\API\AvailabilityRoute::get_item() is not contravariant with parameter #1 $request (mixed) of method WP_REST_Controller::get_item().
// get parameters from request
$params = $request->get_params();
$data = new stdClass();
Expand All @@ -76,11 +76,11 @@
/**
* Get a collection of items
*
* @param $request WP_REST_Request full data about the request.
* @param WP_REST_Request<array<string, mixed>> $request Full data about the request.
*
* @return WP_REST_Response
*/
public function get_items( $request ) {
public function get_items( WP_REST_Request $request ) {

Check failure on line 83 in src/API/AvailabilityRoute.php

View workflow job for this annotation

GitHub Actions / WP latest on PHP 8.5

Parameter #1 $request (WP_REST_Request) of method CommonsBooking\API\AvailabilityRoute::get_items() is not contravariant with parameter #1 $request (mixed) of method WP_REST_Controller::get_items().

Check failure on line 83 in src/API/AvailabilityRoute.php

View workflow job for this annotation

GitHub Actions / WP latest on PHP 8.5

Parameter #1 $request (WP_REST_Request) of method CommonsBooking\API\AvailabilityRoute::get_items() is not contravariant with parameter #1 $request (mixed) of method WP_REST_Controller::get_items().
$data = new stdClass();
$data->availability = [];

Expand Down
15 changes: 8 additions & 7 deletions src/API/BaseRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ class BaseRoute extends WP_REST_Controller {
// the location of the .schema.json files locally
const SCHEMA_PATH = COMMONSBOOKING_PLUGIN_DIR . 'includes/commons-api-json-schema/';

/** @var string */
protected $schemaUrl;

/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
public function register_routes(): void {
$version = '1';
$namespace = COMMONSBOOKING_PLUGIN_SLUG . '/v' . $version;
register_rest_route(
Expand Down Expand Up @@ -96,7 +97,7 @@ public function register_routes() {
*
* @param object $data instance of stdclass or object to validate.
*/
public function validateData( $data ) {
public function validateData( $data ): void {
$validator = new Validator();

// Opis does not fetch remote $ref targets in getSchemaJson() main schema.
Expand Down Expand Up @@ -158,8 +159,8 @@ private function getSchemaJson(): string {
/**
* Adds schema-fields for output to current route (needed for /.../schema endpoint)
*
* @param array $schema Assoc array of schema json object.
* @return array
* @param array<string, mixed> $schema Assoc array of schema json object.
* @return array<string, mixed>
*/
public function add_additional_fields_schema( $schema ): array {
$schemaArray = json_decode( $this->getSchemaJson(), true );
Expand All @@ -170,11 +171,11 @@ public function add_additional_fields_schema( $schema ): array {
/**
* Escapes JSON String for output.
*
* @param $string
* @param string $string
*
* @return false|string
* @return string
*/
public function escapeJsonString( $string ) {
public function escapeJsonString( string $string ): string {
return substr( wp_json_encode( $string ), 1, - 1 ) ? : '';
}

Expand Down
10 changes: 5 additions & 5 deletions src/API/GBFS/BaseRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
/**
* Returns Rest Response with items.
*
* @param WP_REST_Request $request
* @param WP_REST_Request<array<string, mixed>> $request
*
* @return WP_REST_Response
*/
public function get_items( $request ): WP_REST_Response {
public function get_items( WP_REST_Request $request ): WP_REST_Response {

Check failure on line 28 in src/API/GBFS/BaseRoute.php

View workflow job for this annotation

GitHub Actions / WP latest on PHP 8.5

Parameter #1 $request (WP_REST_Request) of method CommonsBooking\API\GBFS\BaseRoute::get_items() is not contravariant with parameter #1 $request (mixed) of method WP_REST_Controller::get_items().

Check failure on line 28 in src/API/GBFS/BaseRoute.php

View workflow job for this annotation

GitHub Actions / WP latest on PHP 8.5

Parameter #1 $request (WP_REST_Request) of method CommonsBooking\API\GBFS\BaseRoute::get_items() is not contravariant with parameter #1 $request (mixed) of method WP_REST_Controller::get_items().
$response = new stdClass();
$response->data = new stdClass();
$response->data->stations = $this->getItemData( $request );
Expand All @@ -43,11 +43,11 @@
/**
* Returns item data array.
*
* @param $request
* @param WP_REST_Request<array<string, mixed>> $request
*
* @return array
* @return array<int, mixed>
*/
public function getItemData( $request ): array {
public function getItemData( WP_REST_Request $request ): array {
$data = [];
$locations = Location::get();

Expand Down
2 changes: 1 addition & 1 deletion src/API/GBFS/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function get_items( $request ): WP_REST_Response {
return new WP_REST_Response( $response, 200 );
}

private function get_feed( $name ): stdClass {
private function get_feed( string $name ): stdClass {
$feed = new stdClass();
$feed->name = $name;
$feed->url = get_rest_url() . 'commonsbooking/v1/' . $name . '.json';
Expand Down
Loading
Loading