Skip to content

Commit

Permalink
Hide sidebar widgets if widget block is enabled (#54)
Browse files Browse the repository at this point in the history
* Hide sidebar widgets option if widget block is enabled
* Update wp-tests
* Composer allow plugins
* Require yoast/phpunit-polyfills
  • Loading branch information
grappler authored Mar 17, 2022
1 parent 03e9f3a commit 5ce6f97
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 19 deletions.
49 changes: 39 additions & 10 deletions bin/install-wp-tests.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SKIP_DB_CREATE=${6-false}
TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}

download() {
if [ `which curl` ]; then
Expand All @@ -25,7 +25,11 @@ download() {
fi
}

if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
WP_BRANCH=${WP_VERSION%\-*}
WP_TESTS_TAG="branches/$WP_BRANCH"

elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_TESTS_TAG="branches/$WP_VERSION"
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
Expand All @@ -47,7 +51,6 @@ else
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi

set -ex

install_wp() {
Expand All @@ -59,10 +62,10 @@ install_wp() {
mkdir -p $WP_CORE_DIR

if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p $TMPDIR/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip
unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/
mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR
mkdir -p $TMPDIR/wordpress-trunk
rm -rf $TMPDIR/wordpress-trunk/*
svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress
mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR
else
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
Expand Down Expand Up @@ -104,15 +107,17 @@ install_test_suite() {
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
rm -rf $WP_TESTS_DIR/{includes,data}
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
fi

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
# remove all forward slashes in the end
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
Expand All @@ -121,6 +126,23 @@ install_test_suite() {

}

recreate_db() {
shopt -s nocasematch
if [[ $1 =~ ^(y|yes)$ ]]
then
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
create_db
echo "Recreated the database ($DB_NAME)."
else
echo "Leaving the existing database ($DB_NAME) in place."
fi
shopt -u nocasematch
}

create_db() {
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

install_db() {

if [ ${SKIP_DB_CREATE} = "true" ]; then
Expand All @@ -144,7 +166,14 @@ install_db() {
fi

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ]
then
echo "Reinstalling will delete the existing test database ($DB_NAME)"
read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
recreate_db $DELETE_EXISTING_DB
else
create_db
fi
}

install_wp
Expand Down
32 changes: 31 additions & 1 deletion classes/class-wp-widget-disable.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,15 @@ public function render_sidebar_checkboxes() {
return;
}

$options = (array) get_option( $this->sidebar_widgets_option, [] );
$options = (array) get_option( $this->sidebar_widgets_option, [] );
$widgets_to_hide = $this->get_widgets_to_hide_from_legacy_widget_block();
$use_widgets_block_editor = $this->use_widgets_block_editor();

foreach ( $widgets as $id => $widget_object ) {
// Hide widgets if widgets block is enabled.
if ( $use_widgets_block_editor && in_array( $widget_object->id_base, $widgets_to_hide, true ) ) {
continue;
}
printf(
'<p><input type="checkbox" id="%1$s" name="%2$s" value="disabled" %3$s> <label for="%1$s">%4$s</label></p>',
esc_attr( $id ),
Expand Down Expand Up @@ -791,4 +797,28 @@ public function render_dashboard_checkboxes() {
</p>
<?php
}

/**
* Check if block editor is enabled for widgets.
*
* @return bool
*/
public function use_widgets_block_editor() {
if ( function_exists( 'wp_use_widgets_block_editor' ) ) {
return wp_use_widgets_block_editor();
}
return false;
}

/**
* Get list of widgets to hide from legacy widget block.
*
* @return array
*/
public function get_widgets_to_hide_from_legacy_widget_block() {
if ( function_exists( 'get_legacy_widget_block_editor_settings' ) ) {
return get_legacy_widget_block_editor_settings()['widgetTypesToHideFromLegacyWidgetBlock'];
}
return [];
}
}
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
"widgets"
],
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"require": {
"php": ">=5.6",
"wearerequired/wp-requirements-check": "~1.1"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"phpunit/phpunit": "^5 || ^7",
"wearerequired/coding-standards": "^1.6"
"wearerequired/coding-standards": "^1.6",
"yoast/phpunit-polyfills": "^1.0"
},
"scripts": {
"format": "vendor/bin/phpcbf --report-summary --report-source .",
Expand Down
34 changes: 29 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
<?php
/**
* PHPUnit bootstrap file.
*
* @package Wp_Widget_Disable
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );

if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

require dirname( dirname( __FILE__ ) ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';

// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $_phpunit_polyfills_path ) {
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
}

require_once $_tests_dir . '/includes/functions.php';
if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) {
echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit( 1 );
}

// Give access to tests_add_filter() function.
require_once "{$_tests_dir}/includes/functions.php";

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( __FILE__ ) . '/../wp-widget-disable.php';
require dirname( dirname( __FILE__ ) ) . '/wp-widget-disable.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

// Start up the WP testing environment.
require "{$_tests_dir}/includes/bootstrap.php";
class WP_Widget_Disable_TestCase extends WP_UnitTestCase {
function plugin() {
return WP_Widget_Disable_Plugin::get_instance();
Expand Down

0 comments on commit 5ce6f97

Please sign in to comment.