Skip to content

Commit

Permalink
Merge pull request #80 from alleyinteractive/feature/issue-31/add-php…
Browse files Browse the repository at this point in the history
…stan-support

Issue-31: Add phpstan support
  • Loading branch information
nikkifurls authored Apr 2, 2024
2 parents 987e43d + 80dfa50 commit 211a09b
Show file tree
Hide file tree
Showing 49 changed files with 557 additions and 290 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Code Quality

on:
push:
branches:
- develop

jobs:
phpstan:
uses: alleyinteractive/.github/.github/workflows/php-code-quality.yml@main
with:
php: '8.2'
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `Newsletter Builder` will be documented in this file.

## 0.3.7 - 2024-04-01

- Adds phpstan package and fixes phpstan issues
- Adds code-quality GitHub workflow

## 0.3.6 - 2024-03-26

- Adds placeholder in the image header block
Expand Down
10 changes: 5 additions & 5 deletions block-filters/separator/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Registers assets so that they can be enqueued through Gutenberg in
* the corresponding context.
*/
function register_separator_scripts() {
function register_separator_scripts(): void {
wp_register_script(
'plugin-newsletter-separator',
get_entry_asset_url( 'wp-newsletter-builder-separator' ),
Expand All @@ -26,7 +26,7 @@ function register_separator_scripts() {
/**
* Enqueue block editor assets for separator.
*/
function action_enqueue_separator_assets() {
function action_enqueue_separator_assets(): void {
$post_type = get_edit_post_type();
if ( 'nb_newsletter' !== $post_type ) {
return;
Expand All @@ -39,11 +39,11 @@ function action_enqueue_separator_assets() {
/**
* Add a block separator if needed.
*
* @param string $block_content The current block content.
* @param \WP_Block $block The parse block object.
* @param string $block_content The current block content.
* @param array $block The parsed block object.
* @return string $block_content The new block content.
*/
function add_separator( $block_content, $block ) {
function add_separator( string $block_content, array $block ): string {
if ( empty( $block['attrs']['hasSeparator'] ) ) {
return $block_content;
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/email-settings/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_email_settings_block_init() {
function wp_newsletter_builder_email_settings_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
2 changes: 1 addition & 1 deletion blocks/footer/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_footer_block_init() {
function wp_newsletter_builder_footer_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
13 changes: 7 additions & 6 deletions blocks/footer/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
*/

$nb_settings = get_option( 'nb_campaign_monitor_settings' );
$nb_facebook_url = $nb_settings['footer_settings']['facebook_url'] ?? '';
$nb_twitter_url = $nb_settings['footer_settings']['twitter_url'] ?? '';
$nb_instagram_url = $nb_settings['footer_settings']['instagram_url'] ?? '';
$nb_youtube_url = $nb_settings['footer_settings']['youtube_url'] ?? '';
$nb_image_id = $nb_settings['footer_settings']['image'] ?? 0;
$nb_address = $nb_settings['footer_settings']['address'] ?? '';
$nb_footer_settings = is_array( $nb_settings ) ? $nb_settings['footer_settings'] : [];
$nb_facebook_url = $nb_footer_settings['facebook_url'] ?? '';
$nb_twitter_url = $nb_footer_settings['twitter_url'] ?? '';
$nb_instagram_url = $nb_footer_settings['instagram_url'] ?? '';
$nb_youtube_url = $nb_footer_settings['youtube_url'] ?? '';
$nb_image_id = $nb_footer_settings['image'] ?? 0;
$nb_address = $nb_footer_settings['address'] ?? '';
$nb_has_social_links = ! empty( $nb_facebook_url ) || ! empty( $nb_twitter_url ) || ! empty( $nb_instagram_url ) || ! empty( $nb_youtube_url );
$nb_narrow_separator = ! empty( $attributes['narrow_separator'] );
$nb_separator_class = $nb_narrow_separator ? '' : 'is-style-wide';
Expand Down
2 changes: 1 addition & 1 deletion blocks/header/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_header_block_init() {
function wp_newsletter_builder_header_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
10 changes: 7 additions & 3 deletions blocks/header/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
*
* @package wp-newsletter-builder
*/
$wp_newsletter_builder_block_post_id = get_the_ID();
if ( empty( $wp_newsletter_builder_block_post_id ) ) {
return;
}

$image_id = get_post_meta( get_the_ID(), 'nb_newsletter_header_img', true );
if ( empty( $image_id ) ) {
$wp_newsletter_builder_block_image_id = get_post_meta( $wp_newsletter_builder_block_post_id, 'nb_newsletter_header_img', true );
if ( empty( $wp_newsletter_builder_block_image_id ) || ! is_int( $wp_newsletter_builder_block_image_id ) ) {
return;
}
// TODO: Add a check to see if the image exists.
// TODO: Get proper alt text.
?>
<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?> role="banner" align="center">
<?php echo wp_kses_post( wp_get_attachment_image( $image_id, 'full', false ) ); ?>
<?php echo wp_kses_post( wp_get_attachment_image( $wp_newsletter_builder_block_image_id, 'full', false ) ); ?>
</div>
2 changes: 1 addition & 1 deletion blocks/post-byline/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_post_byline_block_init() {
function wp_newsletter_builder_post_byline_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
2 changes: 1 addition & 1 deletion blocks/post-byline/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$wp_newsletter_builder_block_post = $block->context['postId'] ?? null;

$wp_newsletter_builder_block_post = get_post( $wp_newsletter_builder_block_post );
if ( empty( $wp_newsletter_builder_block_post ) || ! $wp_newsletter_builder_block_post ) {
if ( empty( $wp_newsletter_builder_block_post ) ) {
return;
}
$wp_newsletter_builder_byline = ! empty( $attributes['overrideByline'] ) ? $attributes['overrideByline'] : get_byline( $wp_newsletter_builder_block_post );
Expand Down
2 changes: 1 addition & 1 deletion blocks/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_post_content_block_init() {
function wp_newsletter_builder_post_content_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
2 changes: 1 addition & 1 deletion blocks/post-content/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$wp_newsletter_builder_block_post = $block->context['postId'] ?? null;

$wp_newsletter_builder_block_post = get_post( $wp_newsletter_builder_block_post );
if ( empty( $wp_newsletter_builder_block_post ) || ! $wp_newsletter_builder_block_post ) {
if ( empty( $wp_newsletter_builder_block_post ) ) {
return;
}
$wp_newsletter_builder_content = $wp_newsletter_builder_block_post->post_content;
Expand Down
2 changes: 1 addition & 1 deletion blocks/post-excerpt/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_post_excerpt_block_init() {
function wp_newsletter_builder_post_excerpt_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
2 changes: 1 addition & 1 deletion blocks/post-excerpt/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$wp_newsletter_builder_block_post = $block->context['postId'] ?? null;

$wp_newsletter_builder_block_post = get_post( $wp_newsletter_builder_block_post );
if ( empty( $wp_newsletter_builder_block_post ) || ! $wp_newsletter_builder_block_post ) {
if ( empty( $wp_newsletter_builder_block_post ) ) {
return;
}
$wp_newsletter_builder_excerpt = ! empty( $attributes['overrideExcerpt'] ) ? $attributes['overrideExcerpt'] : get_the_excerpt( $wp_newsletter_builder_block_post );
Expand Down
2 changes: 1 addition & 1 deletion blocks/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_post_featured_image_block_init() {
function wp_newsletter_builder_post_featured_image_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
5 changes: 3 additions & 2 deletions blocks/post-featured-image/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
$wp_newsletter_builder_block_post = $block->context['postId'] ?? null;

$wp_newsletter_builder_block_post = get_post( $wp_newsletter_builder_block_post );
if ( empty( $wp_newsletter_builder_block_post ) || ! $wp_newsletter_builder_block_post ) {
if ( empty( $wp_newsletter_builder_block_post ) ) {
return;
}
$wp_newsletter_builder_img_sizes = $attributes['imgSizes'] ?? '';
$wp_newsletter_builder_post_image = ! empty( $attributes['overrideImage'] ) ? $attributes['overrideImage'] : get_post_thumbnail_id( $wp_newsletter_builder_block_post->ID );

$wp_newsletter_builder_post_permalink = (string) get_the_permalink();
?>
<a <?php echo wp_kses_data( get_block_wrapper_attributes( [ 'class' => 'post__image-link' ] ) ); ?> href="<?php echo esc_url( get_the_permalink() ); ?>">
<a <?php echo wp_kses_data( get_block_wrapper_attributes( [ 'class' => 'post__image-link' ] ) ); ?> href="<?php echo esc_url( $wp_newsletter_builder_post_permalink ); ?>">
<?php echo wp_get_attachment_image( $wp_newsletter_builder_post_image, 'full', false, [ 'sizes' => $wp_newsletter_builder_img_sizes ] ); ?>
</a>
2 changes: 1 addition & 1 deletion blocks/post-read-more/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_post_read_more_block_init() {
function wp_newsletter_builder_post_read_more_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
10 changes: 6 additions & 4 deletions blocks/post-read-more/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
$wp_newsletter_builder_block_post = $block->context['postId'] ?? null;

$wp_newsletter_builder_block_post = get_post( $wp_newsletter_builder_block_post );
if ( empty( $wp_newsletter_builder_block_post ) || ! $wp_newsletter_builder_block_post ) {
if ( empty( $wp_newsletter_builder_block_post ) ) {
return;
}
$wp_newsletter_builder_read_more_text = $attributes['readMoreText'] ?: __( 'Read More', 'wp-newsletter-builder' );
$wp_newsletter_builder_read_more_text = $attributes['readMoreText'] ?? __( 'Read More', 'wp-newsletter-builder' );

$wp_newsletter_builder_post_permalink = (string) get_the_permalink();
?>
<div <?php echo wp_kses_data( get_block_wrapper_attributes( [ 'class' => 'wp-block-button has-text-align-center' ] ) ); ?>>
<!--[if mso]>
<v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="<?php echo esc_url( $wp_newsletter_builder_post_url ); ?>" style="height:48px;v-text-anchor:middle;width:200px;" arcsize="10%" stroke="f" fillcolor="#D62827">
<v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="<?php echo esc_url( $wp_newsletter_builder_post_permalink ); ?>" style="height:48px;v-text-anchor:middle;width:200px;" arcsize="10%" stroke="f" fillcolor="#D62827">
<w:anchorlock/>
<center>
<![endif]-->
<a class="wp-element-button" href="<?php echo esc_url( get_the_permalink() ); ?>">
<a class="wp-element-button" href="<?php echo esc_url( $wp_newsletter_builder_post_permalink ); ?>">
<?php echo esc_html( $wp_newsletter_builder_read_more_text ); ?>
</a>
<!--[if mso]>
Expand Down
2 changes: 1 addition & 1 deletion blocks/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_post_title_block_init() {
function wp_newsletter_builder_post_title_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
6 changes: 4 additions & 2 deletions blocks/post-title/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
$wp_newsletter_builder_block_post = $block->context['postId'] ?? null;

$wp_newsletter_builder_block_post = get_post( $wp_newsletter_builder_block_post );
if ( empty( $wp_newsletter_builder_block_post ) || ! $wp_newsletter_builder_block_post ) {
if ( empty( $wp_newsletter_builder_block_post ) ) {
return;
}
$wp_newsletter_builder_post_title = ! empty( $attributes['overrideTitle'] ) ? $attributes['overrideTitle'] : $wp_newsletter_builder_block_post->post_title;

$wp_newsletter_builder_smaller_font = $attributes['smallerFont'] ?? false;
$wp_newsletter_builder_title_class = $wp_newsletter_builder_smaller_font ? 'post__title--small' : '';

$wp_newsletter_builder_post_permalink = (string) get_the_permalink();
?>
<a <?php echo wp_kses_data( get_block_wrapper_attributes( [ 'class' => 'post__title-link' ] ) ); ?> href="<?php echo esc_url( get_the_permalink() ); ?>">
<a <?php echo wp_kses_data( get_block_wrapper_attributes( [ 'class' => 'post__title-link' ] ) ); ?> href="<?php echo esc_url( $wp_newsletter_builder_post_permalink ); ?>">
<h2 class="<?php echo esc_attr( $wp_newsletter_builder_title_class ); ?>">
<?php if ( ! empty( $wp_newsletter_builder_number ) ) : ?>
<span class="newsletter-post__number"><?php echo esc_html( $wp_newsletter_builder_number ); ?>.</span>
Expand Down
2 changes: 1 addition & 1 deletion blocks/post/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_post_block_init() {
function wp_newsletter_builder_post_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
9 changes: 7 additions & 2 deletions blocks/post/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
* @package wp-newsletter-builder
*/

if ( empty( $attributes['postId'] ) ) {
return;
}

$wp_newsletter_builder_block_post = get_post( $attributes['postId'] );
if ( empty( $attributes['postId'] ) || ! $wp_newsletter_builder_block_post ) {
if ( empty( $wp_newsletter_builder_block_post ) ) {
return;
}

?>
<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
<?php echo wp_kses_post( $content ); ?>
<?php echo wp_kses_post( $content ?? '' ); ?>
</div>
2 changes: 1 addition & 1 deletion blocks/section/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_section_block_init() {
function wp_newsletter_builder_section_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
7 changes: 4 additions & 3 deletions blocks/section/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
* @package wp-newsletter-builder
*/

$wp_newsletter_builder_heading = $attributes['heading'];
$wp_newsletter_builder_heading = $attributes['heading'] ?? '';
$wp_newsletter_builder_content = $content ?? '';

// Check if the content is empty.
if ( empty( trim( wp_strip_all_tags( $content ) ) ) ) {
if ( empty( trim( wp_strip_all_tags( $wp_newsletter_builder_content ) ) ) ) {
return;
}
if ( ! empty( $wp_newsletter_builder_heading ) ) {
printf( '<h2 class="wp-newsletter-builder-section__heading">%s</h2>', esc_html( $wp_newsletter_builder_heading ) );
}

echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $wp_newsletter_builder_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2 changes: 1 addition & 1 deletion blocks/signup-form-list/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_signup_form_list_block_init() {
function wp_newsletter_builder_signup_form_list_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
4 changes: 2 additions & 2 deletions blocks/signup-form-list/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @package wp-newsletter-builder
*/

$wp_newsletter_builder_title = $attributes['title'];
$wp_newsletter_builder_title = $attributes['title'] ?? '';
$wp_newsletter_builder_frequency = $attributes['frequency'] ?? '';
$wp_newsletter_builder_description = $attributes['description'] ?? '';
$wp_newsletter_builder_list_id = $attributes['listId'];
$wp_newsletter_builder_list_id = $attributes['listId'] ?? '';
$wp_newsletter_builder_logo_id = $attributes['logo'] ?? null;
$wp_newsletter_builder_checked = $attributes['initialChecked'] ?? false;
$wp_newsletter_builder_checked_str = $wp_newsletter_builder_checked ? 'checked="checked"' : '';
Expand Down
10 changes: 5 additions & 5 deletions blocks/signup-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_newsletter_builder_signup_form_block_init() {
function wp_newsletter_builder_signup_form_block_init(): void {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand All @@ -23,10 +23,10 @@ function wp_newsletter_builder_signup_form_block_init() {
/**
* Allows the use of the input tag in the newsletter builder block.
*
* @param array $tags The allowed tags.
* @return array
* @param array<string, array<string, array<string, string>|bool>> $tags The allowed tags.
* @return array<string, array<string, array<string, string>|bool>>
*/
function wp_newsletter_builder_modify_wpkses_post_allowed_tags( $tags ) {
function wp_newsletter_builder_modify_wpkses_post_allowed_tags( array $tags ): array {
$tags['input'] = [
'type' => true,
'name' => true,
Expand All @@ -37,4 +37,4 @@ function wp_newsletter_builder_modify_wpkses_post_allowed_tags( $tags ) {
];
return $tags;
}
add_filter( 'wp_kses_allowed_html', 'wp_newsletter_builder_modify_wpkses_post_allowed_tags', 10, 2 );
add_filter( 'wp_kses_allowed_html', 'wp_newsletter_builder_modify_wpkses_post_allowed_tags' );
5 changes: 3 additions & 2 deletions blocks/signup-form/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
* @package wp-newsletter-builder
*/

$wp_newsletter_builder_header_text = $attributes['headerText'];
$wp_newsletter_builder_header_text = $attributes['headerText'] ?? '';
$wp_newsletter_builder_subheader_text = $attributes['subheaderText'] ?? '';
$wp_newsletter_builder_disclaimer_text = $attributes['disclaimerText'] ?? '';
$wp_newsletter_builder_button_text = $attributes['buttonText'] ?? __( 'Subscribe', 'wp-newsletter-builder' );
$wp_newsletter_builder_list_id = $attributes['listId'] ?? '';
$wp_newsletter_builder_content = $content ?? '';
?>
<form <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?> data-component="wp-newsletter-builder-signup">
<div class="wp-block-wp-newsletter-builder-signup-form__header container container--entry-content">
Expand Down Expand Up @@ -49,7 +50,7 @@
if ( ! empty( $wp_newsletter_builder_list_id ) ) {
printf( '<input type="hidden" name="wp-newsletter-builder-hidden" value="%s" />', esc_attr( $wp_newsletter_builder_list_id ) );
} else {
echo wp_kses_post( $content );
echo wp_kses_post( $wp_newsletter_builder_content );
}
?>
</form>
Loading

0 comments on commit 211a09b

Please sign in to comment.