Skip to content

Commit

Permalink
Merge pull request #389 from lloc/raise-coverage
Browse files Browse the repository at this point in the history
Allowed HTML in Component class
  • Loading branch information
lloc authored Sep 24, 2024
2 parents f61e8a6 + 862660a commit 00855cd
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 74 deletions.
50 changes: 50 additions & 0 deletions includes/Component/Component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component;

/**
* Abstract class Input
*
* @package lloc\Msls\Component
*/
abstract class Component {

const INPUT_PREFIX = 'msls_input_';

const ALLOWED_HTML = array(
'label' => array(
'for' => array(),
),
'option' => array(
'value' => array(),
'selected' => array(),
),
'select' => array(
'id' => array(),
'name' => array(),
),
'input' => array(
'type' => array(),
'class' => array(),
'id' => array(),
'name' => array(),
'value' => array(),
'size' => array(),
'readonly' => array(),
),
);

/**
* @return string
*/
abstract public function render(): string;

/**
* Adds our input elements to the allowed HTML elements of a post
*/
public static function get_allowed_html(): array {
$my_allowed = wp_kses_allowed_html( 'post' );

return array_merge( $my_allowed, self::ALLOWED_HTML );
}
}
6 changes: 3 additions & 3 deletions includes/Component/Icon.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component;

/**
* Class Icon
*
* @package lloc\Msls\Component
*/
abstract class Icon {
Expand Down Expand Up @@ -48,5 +49,4 @@ abstract protected function get_include(): string;
* @return string
*/
abstract public function get( string $language ): string;

}
}
4 changes: 2 additions & 2 deletions includes/Component/Icon/IconLabel.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Icon;

Expand All @@ -10,7 +10,7 @@
*
* @package lloc\Msls\Component
*/
class IconLabel extends Icon {
final class IconLabel extends Icon {

/**
* @return string
Expand Down
8 changes: 4 additions & 4 deletions includes/Component/Icon/IconPng.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Icon;

Expand All @@ -7,9 +7,10 @@

/**
* Class IconPng
*
* @package lloc\Msls\Component
*/
class IconPng extends Icon {
final class IconPng extends Icon {

const FLAGS_FILE = 'flags/flags.php';

Expand All @@ -28,5 +29,4 @@ protected function get_include(): string {
public function get( string $language ): string {
return $this->map[ $language ] ?? $this->maybe( $language, '', '.png' );
}

}
}
8 changes: 4 additions & 4 deletions includes/Component/Icon/IconSvg.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Icon;

Expand All @@ -7,9 +7,10 @@

/**
* Class IconSvg
*
* @package lloc\Msls\Component
*/
class IconSvg extends Icon {
final class IconSvg extends Icon {

const FLAGS_FILE = 'css-flags/flags.php';

Expand All @@ -28,5 +29,4 @@ protected function get_include(): string {
public function get( string $language ): string {
return $this->map[ $language ] ?? $this->maybe( $language, 'flag-icon-' );
}

}
}
6 changes: 3 additions & 3 deletions includes/Component/Input/Checkbox.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Input;

use lloc\Msls\Component\InputInterface;
use lloc\Msls\Component\Component;

/**
* Class Checkbox
*
* @package lloc\Msls\Component\Input
*/
class Checkbox implements InputInterface {
final class Checkbox extends Component {

/**
* @var string
Expand Down
15 changes: 7 additions & 8 deletions includes/Component/Input/Group.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php

<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Input;

use lloc\Msls\Component\InputInterface;
use lloc\Msls\Component\Component;

/**
* Class Options
*
* @package lloc\Msls\Component\Input
*/
class Group implements InputInterface {
final class Group extends Component {

/**
* @var InputInterface[]
* @var Component[]
*/
protected $arr = array();

Expand All @@ -32,11 +31,11 @@ public function __construct( string $glue = ' ' ) {
}

/**
* @param InputInterface $input
* @param Component $input
*
* @return self
*/
public function add( InputInterface $input ): self {
public function add( Component $input ): self {
$this->arr[] = $input;

return $this;
Expand All @@ -47,7 +46,7 @@ public function add( InputInterface $input ): self {
*/
public function render(): string {
$items = array_map(
function ( InputInterface $input ) {
function ( Component $input ) {
return $input->render(); // phpcs:ignore WordPress.Security.EscapeOutput
},
$this->arr
Expand Down
6 changes: 3 additions & 3 deletions includes/Component/Input/Label.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Input;

use lloc\Msls\Component\InputInterface;
use lloc\Msls\Component\Component;

/**
* Class Label
*
* @package lloc\Msls\Component\Input
*/
class Label implements InputInterface {
final class Label extends Component {

/**
* @var string
Expand Down
6 changes: 3 additions & 3 deletions includes/Component/Input/Option.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Input;

use lloc\Msls\Component\InputInterface;
use lloc\Msls\Component\Component;

/**
* Class Option
*
* @package lloc\Msls\Component\Input
*/
class Option implements InputInterface {
final class Option extends Component {

/**
* @var string
Expand Down
8 changes: 4 additions & 4 deletions includes/Component/Input/Select.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Input;

use lloc\Msls\Component\InputInterface;
use lloc\Msls\Component\Component;

class Select implements InputInterface {
final class Select extends Component {

const RENDER_FILTER = 'msls_input_select_name';

Expand All @@ -28,7 +28,7 @@ public function __construct( string $key, array $arr, ?string $selected = null )

$this->options = new Group( '' );
foreach ( $arr as $key => $value ) {
$this->options->add( new Option( $key, strval( $value ), $selected ) );
$this->options->add( new Option( strval( $key ), strval( $value ), $selected ) );
}
}

Expand Down
7 changes: 3 additions & 4 deletions includes/Component/Input/Text.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

<?php declare( strict_types = 1 );

namespace lloc\Msls\Component\Input;

use lloc\Msls\Component\InputInterface;
use lloc\Msls\Component\Component;

class Text implements InputInterface {
final class Text extends Component {

const DEFAULT_SIZE = 30;

Expand Down
18 changes: 0 additions & 18 deletions includes/Component/InputInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions includes/Component/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace lloc\Msls\Component;

class Wrapper {
final class Wrapper extends Component {

protected string $element;

Expand All @@ -14,6 +14,6 @@ public function __construct( string $element, string $content ) {
}

public function render(): string {
return sprintf( '<%1$s>%2$s</%1$s>', esc_html( $this->element ), wp_kses_post( $this->content ) );
return sprintf( '<%1$s>%2$s</%1$s>', esc_html( $this->element ), wp_kses( $this->content, self::get_allowed_html() ) );
}
}
3 changes: 2 additions & 1 deletion includes/ContentImport/LogWriters/AdminNoticeLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lloc\Msls\ContentImport\LogWriters;

use lloc\Msls\Component\Component;
use lloc\Msls\ContentImport\ImportCoordinates;
use lloc\Msls\MslsRegistryInstance;

Expand Down Expand Up @@ -141,7 +142,7 @@ public function show_last_log( $echo = true ): ?string {
}

if ( $echo ) {
echo wp_kses_post( $html );
echo wp_kses( $html, Component::get_allowed_html() );
}

// we've shown it, no reason to keep it
Expand Down
5 changes: 3 additions & 2 deletions includes/ContentImport/MetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lloc\Msls\ContentImport;

use lloc\Msls\Component\Component;
use lloc\Msls\Component\Wrapper;
use lloc\Msls\ContentImport\Importers\Map;
use lloc\Msls\MslsBlogCollection;
Expand Down Expand Up @@ -86,7 +87,7 @@ function ( $lang ) use ( $mydata ) {
$output = ( new Wrapper( 'p', $warning ) )->render();
}

echo wp_kses_post( $output );
echo wp_kses( $output, Component::get_allowed_html() );
}

protected function inline_thickbox_url( array $data = array() ): string {
Expand Down Expand Up @@ -172,7 +173,7 @@ protected function inline_thickbox_html( $echo = true, array $data = array() ):
$html = ob_get_clean();

if ( $echo ) {
echo wp_kses_post( $html );
echo wp_kses( $html, Component::get_allowed_html() );
}

return $html;
Expand Down
4 changes: 3 additions & 1 deletion includes/MslsCustomColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace lloc\Msls;

use lloc\Msls\Component\Component;

/**
* Handling of existing/not existing translations in the backend listings of
* various post types
Expand Down Expand Up @@ -94,7 +96,7 @@ public function td( $column_name, $item_id ): void {
printf(
'<span class="msls-icon-wrapper %1$s">%2$s</span>',
esc_attr( $this->options->get_icon_type() ),
wp_kses_post( $icon->get_a() )
wp_kses( $icon->get_a(), Component::get_allowed_html() )
);

restore_current_blog();
Expand Down
6 changes: 3 additions & 3 deletions includes/MslsMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace lloc\Msls;

use lloc\Msls\Component\InputInterface;
use lloc\Msls\Component\Component;

/**
* Abstraction for the hook classes
Expand Down Expand Up @@ -75,9 +75,9 @@ public function get_input_array( $object_id ): array {
return $arr;
}

$offset = strlen( InputInterface::INPUT_PREFIX );
$offset = strlen( Component::INPUT_PREFIX );
foreach ( $input_post as $key => $value ) {
if ( false === strpos( $key, InputInterface::INPUT_PREFIX ) || empty( $value ) ) {
if ( false === strpos( $key, Component::INPUT_PREFIX ) || empty( $value ) ) {
continue;
}

Expand Down
Loading

0 comments on commit 00855cd

Please sign in to comment.