Skip to content

Commit aee63ed

Browse files
authored
Block reorg (#299)
* Block reorg, includes block JS and a block.json * Compilation for block and jQuery selector script added * render part that uses `the_msls()` for the frontend part added * ServerSideRender fallback still used in the editor * Cleanup and some tests added
1 parent f6ea12f commit aee63ed

28 files changed

+183
-142
lines changed

build/exclude

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/bin/
55
/build/
66
/docs/
7+
/js/msls-widget-block/
78
/legacy-tests/
89
/release/
910
/multisite-language-switcher/

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"require-dev": {
1313
"phpunit/phpunit": "^9.6",
1414
"brain/monkey": "^2.6",
15-
"phpstan/phpstan": "^1.10",
15+
"phpstan/phpstan": "^1.11",
1616
"szepeviktor/phpstan-wordpress": "^1.3",
1717
"phpstan/extension-installer": "^1.3",
1818
"antecedent/patchwork": "^2.1",

includes/MslsAdmin.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function get_options_page_link() {
9595
public function __call( $method, $args ) {
9696
$parts = explode( '_', $method, 2 );
9797

98-
if ( 'rewrite' === $parts[0] ) {
98+
if ( is_array( $parts ) && 'rewrite' === $parts[0] ) {
9999
return $this->render_rewrite( $parts[1] );
100100
}
101101

includes/MslsAdminIconTaxonomy.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ class MslsAdminIconTaxonomy extends MslsAdminIcon {
2828
* @uses get_edit_term_link()
2929
*/
3030
public function set_href( int $id ): MslsAdminIcon {
31-
$this->href = get_edit_term_link(
32-
$id,
33-
$this->type,
34-
MslsTaxonomy::instance()->get_post_type()
35-
);
31+
$object_type = MslsTaxonomy::instance()->get_post_type();
32+
33+
$this->href = get_edit_term_link( $id, $this->type, $object_type );
3634

3735
return $this;
3836
}
@@ -41,16 +39,13 @@ public function set_href( int $id ): MslsAdminIcon {
4139
* Set the path by type
4240
*
4341
* @return MslsAdminIconTaxonomy
44-
* @uses add_query_arg()
4542
*
4643
*/
4744
public function set_path(): MslsAdminIcon {
4845
$args = [ 'taxonomy' => $this->type ];
4946
$post_type = MslsTaxonomy::instance()->get_post_type();
5047

51-
if ( ! empty( $post_type ) ) {
52-
$args['post_type'] = $post_type;
53-
}
48+
$post_type !== '' && $args['post_type'] = $post_type;
5449

5550
$this->path = add_query_arg( $args, $this->path );
5651

includes/MslsCustomColumn.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ public function th( $columns ) {
5353
foreach ( $blogs as $blog ) {
5454
$language = $blog->get_language();
5555

56-
$icon = new MslsAdminIcon( null );
56+
$icon_type= $this->options->admin_display === 'label' ? 'label' : 'flag';
57+
58+
$icon = new MslsAdminIcon( null );
5759
$icon->set_language( $language );
58-
if ( $this->options->admin_display === 'label' ) {
59-
$icon->set_icon_type( 'label' );
60-
} else {
61-
$icon->set_icon_type( 'flag' );
62-
}
60+
$icon->set_icon_type( $icon_type );
6361

6462
if ( $post_id = get_the_ID() ) {
6563
$icon->set_id( $post_id );

includes/MslsCustomFilter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public static function init() {
4141
* Echo's select tag with list of blogs
4242
* @uses selected
4343
*/
44-
public function add_filter() {
44+
public function add_filter(): void {
4545
$id = (
46-
filter_has_var( INPUT_GET, 'msls_filter' ) ?
46+
filter_has_var( INPUT_GET, 'msls_filter' ) ?
4747
filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ) :
4848
''
4949
);

includes/MslsPlugin.php

+6-29
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function content_filter( $content ) {
172172
*
173173
* @return string
174174
*/
175-
function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
175+
public function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
176176
$obj = MslsOutput::init();
177177
$links = $obj->get( 1, true, true );
178178
$output = __( 'This post is also available in %s.', 'multisite-language-switcher' );
@@ -217,31 +217,8 @@ function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
217217
*/
218218
public function block_init() {
219219
if ( ! $this->options->is_excluded() ) {
220-
$handle = 'msls-widget-block';
221-
$callback = [ $this, 'block_render' ];
222-
223-
global $pagenow;
224-
225-
$toLoad = [ 'wp-blocks', 'wp-element', 'wp-components' ];
226-
if ( $pagenow === 'widgets.php' ) {
227-
$toLoad[] = 'wp-edit-widgets';
228-
} else {
229-
$toLoad[] = 'wp-editor';
230-
}
231-
232-
wp_register_script(
233-
$handle,
234-
self::plugins_url( 'js/msls-widget-block.js' ),
235-
$toLoad
236-
);
237-
238-
register_block_type( 'lloc/msls-widget-block', [
239-
'attributes' => [ 'title' => [ 'type' => 'string' ] ],
240-
'editor_script' => $handle,
241-
'render_callback' => $callback,
242-
] );
243-
244-
add_shortcode( 'sc_msls_widget', $callback );
220+
register_block_type( self::plugin_dir_path('js/msls-widget-block' ) );
221+
add_shortcode( 'sc_msls_widget', [ $this, 'block_render' ] );
245222

246223
return true;
247224
}
@@ -274,15 +251,15 @@ public function custom_enqueue() {
274251
return false;
275252
}
276253

277-
$ver = defined( 'MSLS_PLUGIN_VERSION' ) ? constant( 'MSLS_PLUGIN_VERSION' ) : false;
278-
$postfix = defined( 'SCRIPT_DEBUG' ) && constant( 'SCRIPT_DEBUG' ) ? '' : '.min';
254+
$ver = defined( 'MSLS_PLUGIN_VERSION' ) ? constant( 'MSLS_PLUGIN_VERSION' ) : false;
255+
$folder = defined( 'SCRIPT_DEBUG' ) && constant( 'SCRIPT_DEBUG' ) ? 'src' : 'js';
279256

280257
wp_enqueue_style( 'msls-styles', self::plugins_url( 'css/msls.css' ), [], $ver );
281258
wp_enqueue_style( 'msls-flags', self::plugins_url( 'css-flags/css/flag-icon.min.css' ), [], $ver );
282259

283260
if ( $this->options->activate_autocomplete ) {
284261
wp_enqueue_script( 'msls-autocomplete',
285-
self::plugins_url( "js/msls{$postfix}.js" ),
262+
self::plugins_url( "$folder/msls.js" ),
286263
[ 'jquery-ui-autocomplete' ],
287264
$ver );
288265

js/msls-widget-block.js

-30
This file was deleted.

js/msls-widget-block/block.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 3,
4+
"title": "Multisite Language Switcher",
5+
"icon": "translation",
6+
"category": "widgets",
7+
"name": "lloc/msls-widget-block",
8+
"version": "2.6.4",
9+
"description": "Review the settings for the Multisite Language Switcher plugin, as the block utilizes the API function `the_msls()` for its output.",
10+
"example": {},
11+
"supports": {
12+
"html": false
13+
},
14+
"textdomain": "multisite-language-switcher",
15+
"editorScript": "file:./index.js",
16+
"render": "file:./render.php"
17+
}

js/msls-widget-block/index.asset.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-server-side-render'), 'version' => 'cc5b60bf835008a66743');

js/msls-widget-block/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/msls-widget-block/render.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
the_msls();

js/msls.js

+1-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/msls.min.js

-1
This file was deleted.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"scripts": {
3-
"uglify": "uglifyjs js/msls.js > js/msls.min.js",
3+
"uglify": "uglifyjs src/msls.js > js/msls.js",
44
"less": "lessc css/msls.less css/msls.css --clean-css=\"--s1 --advanced\"",
5-
"build": "npm run uglify && npm run less"
5+
"build-msls-block": "wp-scripts build src/msls-widget-block/index.js --webpack-src-dir=src/msls-widget-block --output-path=js/msls-widget-block",
6+
"build": "npm run uglify && npm run less && npm run build-msls-block"
67
},
78
"dependencies": {
89
"less": "^4.2.0",
@@ -11,6 +12,7 @@
1112
},
1213
"devDependencies": {
1314
"@playwright/test": "^1.44.0",
15+
"@wordpress/scripts": "^27.9.0",
1416
"@types/node": "^20.12.11"
1517
}
1618
}

patchwork.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"redefinable-internals": ["filter_input_array"]}
1+
{"redefinable-internals": ["filter_input_array", "filter_has_var"]}

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</coverage>
88
<php>
99
<const name="MSLS_PLUGIN_PATH" value="multisite-language-switcher/MultisiteLanguageSwitcher.php"/>
10+
<const name="MSLS_PLUGIN__FILE__" value="/var/www/html/wp-content/plugins/multisite-language-switcher/MultisiteLanguageSwitcher.php"/>
1011
<const name="WP_DEBUG" value="true"/>
1112
</php>
1213
<testsuite name="Internal tests">

readme.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Contributors: realloc, lucatume
44
Donate link: https://www.greenpeace.org/international/
55
Tags: multilingual, multisite, language, switcher, localization
6-
Requires at least: 5.3
6+
Requires at least: 6.1
77
Tested up to: 6.5
88
Requires PHP: 7.4
99
Stable tag: 2.6.4

src/index.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Silence is golden.
3+

src/msls-widget-block/block.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 3,
4+
"title": "Multisite Language Switcher",
5+
"icon": "translation",
6+
"category": "widgets",
7+
"name": "lloc/msls-widget-block",
8+
"version": "2.6.4",
9+
"description": "Review the settings for the Multisite Language Switcher plugin, as the block utilizes the API function `the_msls()` for its output.",
10+
"example": {},
11+
"supports": {
12+
"html": false
13+
},
14+
"textdomain": "multisite-language-switcher",
15+
"editorScript": "file:./index.js",
16+
"render": "file:./render.php"
17+
}

src/msls-widget-block/edit.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import ServerSideRender from '@wordpress/server-side-render';
2+
import { useBlockProps } from '@wordpress/block-editor';
3+
4+
export default function Edit(props) {
5+
const blockProps = useBlockProps();
6+
return (
7+
<div { ...blockProps }>
8+
<ServerSideRender
9+
block="lloc/msls-widget-block"
10+
attributes={ props.attributes }
11+
/>
12+
</div>
13+
);
14+
}

src/msls-widget-block/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { registerBlockType } from '@wordpress/blocks';
2+
3+
import Edit from './edit';
4+
import metadata from './block.json';
5+
6+
registerBlockType(metadata.name, {
7+
edit: Edit
8+
} );

src/msls-widget-block/index.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Silence is golden.
3+

src/msls-widget-block/render.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
the_msls();

0 commit comments

Comments
 (0)