Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/return types #257

Merged
merged 5 commits into from
Dec 3, 2024
Merged
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
14 changes: 7 additions & 7 deletions mu-plugins/10up-plugin/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions mu-plugins/10up-plugin/includes/classes/ModuleInitialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ private function __construct() {
/**
* The list of initialized classes.
*
* @var array
* @var array<\TenUpPlugin\Module>
*/
protected $classes = [];

/**
* Get all the TenUpPlugin plugin classes.
*
* @return array
* @return array<string>
*/
protected function get_classes() {
$class_finder = new ClassFinder();
Expand All @@ -78,6 +78,7 @@ public function init_classes() {
}

// Create a new reflection of the class.
// @phpstan-ignore-next-line
$reflection_class = new ReflectionClass( $class );

// Using reflection, check if the class can be initialized.
Expand All @@ -94,6 +95,10 @@ public function init_classes() {
// Initialize the class.
$instantiated_class = new $class();

if ( ! $instantiated_class instanceof Module ) {
continue;
}

// Assign the classes into the order they should be initialized.
$load_class_order[ intval( $instantiated_class->load_order ) ][] = [
'slug' => $slug,
Expand Down Expand Up @@ -152,7 +157,7 @@ public function get_class( $class_name ) {
/**
* Get all the initialized classes.
*
* @return array
* @return array<\TenUpPlugin\Module>
*/
public function get_all_classes() {
return $this->classes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function is_hierarchical() {
/**
* Default post type supported feature names.
*
* @return array
* @return array<string>
*/
public function get_editor_supports() {
$supports = [
Expand All @@ -110,7 +110,7 @@ public function get_editor_supports() {
/**
* Get the options for the post type.
*
* @return array
* @return array<mixed>
*/
public function get_options() {
return [
Expand All @@ -131,7 +131,7 @@ public function get_options() {
/**
* Get the labels for the post type.
*
* @return array
* @return array<string>
*/
public function get_labels() {
$plural_label = $this->get_plural_label();
Expand Down Expand Up @@ -225,7 +225,7 @@ public function register_taxonomies() {
* Returns the default supported taxonomies. The subclass should declare the
* Taxonomies that it supports here if required.
*
* @return array
* @return array<string>
*/
public function get_supported_taxonomies() {
return [];
Expand Down
2 changes: 1 addition & 1 deletion mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function can_register() {
* Returns the default supported taxonomies. The subclass should declare the
* Taxonomies that it supports here if required.
*
* @return array
* @return array<string>
*/
public function get_supported_taxonomies() {
return [
Expand Down
2 changes: 1 addition & 1 deletion mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function get_name() {
* Returns the default supported taxonomies. The subclass should declare the
* Taxonomies that it supports here if required.
*
* @return array
* @return array<string>
*/
public function get_supported_taxonomies() {
return [];
Expand Down
2 changes: 1 addition & 1 deletion mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function get_name() {
*
* Note: This will not remove the default taxonomies that are registered by core.
*
* @return array
* @return array<string>
*/
public function get_supported_taxonomies() {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function register() {
/**
* Get the options for the taxonomy.
*
* @return array
* @return array<mixed>
*/
public function get_options() {
return [
Expand All @@ -112,7 +112,7 @@ public function get_options() {
/**
* Get the labels for the taxonomy.
*
* @return array
* @return array<string>
*/
public function get_labels() {
$plural_label = $this->get_plural_label();
Expand Down Expand Up @@ -145,10 +145,10 @@ public function get_labels() {
* Setting the post types to null to ensure no post type is registered with
* this taxonomy. Post Type classes declare their supported taxonomies.
*
* @return array|null
* @return array<string>
*/
public function get_post_types() {
return null;
return [];
}

/**
Expand Down
16 changes: 10 additions & 6 deletions mu-plugins/10up-plugin/includes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function deactivate() {
/**
* The list of knows contexts for enqueuing scripts/styles.
*
* @return array
* @return array<string>
*/
function get_enqueue_contexts() {
return [ 'admin', 'frontend', 'shared' ];
Expand All @@ -108,12 +108,14 @@ function get_enqueue_contexts() {
* @param string $script Script file name (no .js extension)
* @param string $context Context for the script ('admin', 'frontend', or 'shared')
*
* @return string|WP_Error URL
* @throws \RuntimeException If an invalid $context is specified.
*
* @return string URL
*/
function script_url( $script, $context ) {

if ( ! in_array( $context, get_enqueue_contexts(), true ) ) {
return new WP_Error( 'invalid_enqueue_context', 'Invalid $context specified in TenUpPlugin script loader.' );
throw new \RuntimeException( 'Invalid $context specified in TenUpPlugin script loader.' );
fabiankaegy marked this conversation as resolved.
Show resolved Hide resolved
}

return TENUP_PLUGIN_URL . "dist/js/{$script}.js";
Expand All @@ -125,12 +127,14 @@ function script_url( $script, $context ) {
* @param string $stylesheet Stylesheet file name (no .css extension)
* @param string $context Context for the script ('admin', 'frontend', or 'shared')
*
* @throws \RuntimeException If an invalid $context is specified.
*
* @return string URL
*/
function style_url( $stylesheet, $context ) {

if ( ! in_array( $context, get_enqueue_contexts(), true ) ) {
return new WP_Error( 'invalid_enqueue_context', 'Invalid $context specified in TenUpPlugin stylesheet loader.' );
throw new \RuntimeException( 'Invalid $context specified in TenUpPlugin stylesheet loader.' );
}

return TENUP_PLUGIN_URL . "dist/css/{$stylesheet}.css";
Expand Down Expand Up @@ -259,7 +263,7 @@ function mce_css( $stylesheets ) {
* @link https://core.trac.wordpress.org/ticket/12009
* @param string $tag The script tag.
* @param string $handle The script handle.
* @return string
* @return string|null
*/
function script_loader_tag( $tag, $handle ) {
$script_execution = wp_scripts()->get_data( $handle, 'script_execution' );
Expand All @@ -269,7 +273,7 @@ function script_loader_tag( $tag, $handle ) {
}

if ( 'async' !== $script_execution && 'defer' !== $script_execution ) {
return $tag; // _doing_it_wrong()?
return $tag;
}

// Abort adding async/defer for scripts that have this script as a dependency. _doing_it_wrong()?
Expand Down
9 changes: 7 additions & 2 deletions mu-plugins/10up-plugin/includes/utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@
*
* @param string $slug Asset slug as defined in build/webpack configuration
* @param string $attribute Optional attribute to get. Can be version or dependencies
* @return string|array
* @return ($attribute is null ? array{version: string, dependencies: array<string>} : $attribute is 'dependencies' ? array<string> : string)
*/
function get_asset_info( $slug, $attribute = null ) {
if ( file_exists( TENUP_PLUGIN_PATH . 'dist/js/' . $slug . '.asset.php' ) ) {
$asset = require TENUP_PLUGIN_PATH . 'dist/js/' . $slug . '.asset.php';
} elseif ( file_exists( TENUP_PLUGIN_PATH . 'dist/css/' . $slug . '.asset.php' ) ) {
$asset = require TENUP_PLUGIN_PATH . 'dist/css/' . $slug . '.asset.php';
} else {
return null;
$asset = [
'version' => TENUP_PLUGIN_VERSION,
'dependencies' => [],
fabiankaegy marked this conversation as resolved.
Show resolved Hide resolved
];
}

// @var <array{version: string, dependencies: array<string>}> $asset

if ( ! empty( $attribute ) && isset( $asset[ $attribute ] ) ) {
return $asset[ $attribute ];
}
Expand Down
5 changes: 4 additions & 1 deletion mu-plugins/10up-plugin/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@

if ( $is_local && file_exists( __DIR__ . '/dist/fast-refresh.php' ) ) {
require_once __DIR__ . '/dist/fast-refresh.php';
TenUpToolkit\set_dist_url_path( basename( __DIR__ ), TENUP_PLUGIN_DIST_URL, TENUP_PLUGIN_DIST_PATH );

if ( function_exists( 'TenUpToolkit\set_dist_url_path' ) ) {
TenUpToolkit\set_dist_url_path( basename( __DIR__ ), TENUP_PLUGIN_DIST_URL, TENUP_PLUGIN_DIST_PATH );
}
}

// Require Composer autoloader if it exists.
Expand Down
5 changes: 4 additions & 1 deletion themes/10up-theme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

if ( $is_local && file_exists( __DIR__ . '/dist/fast-refresh.php' ) ) {
require_once __DIR__ . '/dist/fast-refresh.php';
TenUpToolkit\set_dist_url_path( basename( __DIR__ ), TENUP_THEME_DIST_URL, TENUP_THEME_DIST_PATH );

if ( function_exists( 'TenUpToolkit\set_dist_url_path' ) ) {
TenUpToolkit\set_dist_url_path( basename( __DIR__ ), TENUP_THEME_DIST_URL, TENUP_THEME_DIST_PATH );
}
}

require_once TENUP_THEME_INC . 'core.php';
Expand Down
8 changes: 8 additions & 0 deletions themes/10up-theme/includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function register_theme_blocks() {
if ( file_exists( TENUP_THEME_BLOCK_DIST_DIR ) ) {
$block_json_files = glob( TENUP_THEME_BLOCK_DIST_DIR . '*/block.json' );

if ( empty( $block_json_files ) ) {
return;
}

// auto register all blocks that were found.
foreach ( $block_json_files as $filename ) {

Expand Down Expand Up @@ -102,6 +106,10 @@ function blocks_editor_styles() {
function enqueue_block_specific_styles() {
$stylesheets = glob( TENUP_THEME_DIST_PATH . 'blocks/autoenqueue/**/*.css' );

if ( empty( $stylesheets ) ) {
return;
}

foreach ( $stylesheets as $stylesheet_path ) {
$block_type = str_replace( TENUP_THEME_DIST_PATH . 'blocks/autoenqueue/', '', $stylesheet_path );
$block_type = str_replace( '.css', '', $block_type );
Expand Down
11 changes: 8 additions & 3 deletions themes/10up-theme/includes/classes/ModuleInitialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ private function __construct() {
/**
* The list of initialized classes.
*
* @var array
* @var array<\TenUpTheme\Module>
*/
protected $classes = [];

/**
* Get all the TenUpTheme plugin classes.
*
* @return array
* @return array<string>
*/
protected function get_classes() {
$class_finder = new ClassFinder();
Expand All @@ -79,6 +79,7 @@ public function init_classes() {
}

// Create a new reflection of the class.
// @phpstan-ignore-next-line
$reflection_class = new ReflectionClass( $class );

// Using reflection, check if the class can be initialized.
Expand All @@ -95,6 +96,10 @@ public function init_classes() {
// Initialize the class.
$instantiated_class = new $class();

if ( ! $instantiated_class instanceof Module ) {
continue;
}

// Assign the classes into the order they should be initialized.
$load_class_order[ intval( $instantiated_class->load_order ) ][] = [
'slug' => $slug,
Expand Down Expand Up @@ -153,7 +158,7 @@ public function get_class( $class_name ) {
/**
* Get all the initialized classes.
*
* @return array
* @return array<\TenUpTheme\Module>
*/
public function get_all_classes() {
return $this->classes;
Expand Down
6 changes: 4 additions & 2 deletions themes/10up-theme/includes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ function i18n() {

/**
* Sets up theme defaults and registers support for various WordPress features.
*
* @return void
*/
function theme_setup() {
add_theme_support( 'automatic-feed-links' );
Expand Down Expand Up @@ -258,7 +260,7 @@ function js_detection() {
* @link https://core.trac.wordpress.org/ticket/12009
* @param string $tag The script tag.
* @param string $handle The script handle.
* @return string
* @return string|null
*/
function script_loader_tag( $tag, $handle ) {
$script_execution = wp_scripts()->get_data( $handle, 'script_execution' );
Expand Down Expand Up @@ -301,7 +303,7 @@ function script_loader_tag( $tag, $handle ) {
*/
function embed_ct_css() {

$debug_performance = rest_sanitize_boolean( filter_input( INPUT_GET, 'debug_perf', FILTER_SANITIZE_NUMBER_INT ) );
$debug_performance = rest_sanitize_boolean( boolval( filter_input( INPUT_GET, 'debug_perf', FILTER_SANITIZE_NUMBER_INT ) ) );

if ( ! $debug_performance ) {
return;
Expand Down
Loading