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

Feature/warn or deny #27

Merged
merged 7 commits into from
Jul 24, 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
32 changes: 27 additions & 5 deletions block-accessibility-checks.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
<?php
/**
* Plugin Name: Block Accessibility Checks
* Plugin URI: https://example.com/block-accessibility-checks
* Description: This plugin helps ensure your content meets WCAG (Web Content Accessibility Guidelines) requirements.
* Requires at least: 6.3
* Requires PHP: 7.0
* Version: 0.1.0
* Requires at least: WordPress 6.3
* Requires PHP: 7.0
* Author: Troy Chaplin
* Author URI: https://example.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: block-accessibility-checks
* Domain Path: /languages
*
* @package block-accessibility-checks
*/

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
die;
}

// Setup autoloading
require_once __DIR__ . '/vendor/autoload.php';

// Define constants
if (!defined('BLOCK_ACCESSIBILITY_MODE')) {
define('BLOCK_ACCESSIBILITY_MODE', 'DENY'); // Default value, can be overridden with WARN in wp-config.php
}

// Include dependencies
use BlockAccessibility\AssetEnqueuer;
use BlockAccessibility\ScriptsAndStyles;
use BlockAccessibility\Translations;

// Define plugin file and Text Domain
$pluginFile = __FILE__;
$textDomain = 'block-accessibility-checks';

// Translation setup
$translations = new Translations($pluginFile, $textDomain);
add_action('plugins_loaded', [$translations, 'loadTextDomain']);

// Enqueue block editor assets
$assetEnqueuer = new AssetEnqueuer(__FILE__);
add_action('enqueue_block_editor_assets', [$assetEnqueuer, 'enqueueAssets']);
$scriptsStyles = new ScriptsAndStyles($pluginFile, $translations);
add_action('enqueue_block_editor_assets', [$scriptsStyles, 'enqueueAssets']);
2 changes: 1 addition & 1 deletion build/block-checks.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-block-editor', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins'), 'version' => '8d731575fd93004bcbf2');
<?php return array('dependencies' => array('wp-block-editor', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins'), 'version' => 'b6bbeb5f881b8ca07b5a');
2 changes: 1 addition & 1 deletion build/block-checks.js

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

47 changes: 32 additions & 15 deletions src/AssetEnqueuer.php → src/ScriptsAndStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,58 @@

namespace BlockAccessibility;

class AssetEnqueuer
class ScriptsAndStyles
{
private $pluginFile;
private $translations;

public function __construct($pluginFile) {
// Ensure the plugin file points to the main plugin file, typically plugin.php at the root
public function __construct($pluginFile, Translations $translations) {
$this->pluginFile = $pluginFile;
$this->translations = $translations;
}

public function enqueueAssets() {
public function enqueueAssets()
{
$script_handle = 'block-accessibility-script';
$this->translations->setupScriptTranslations($script_handle);

$this->enqueueScripts();
$this->enqueueStyles();
}

private function enqueueScripts()
{
$script_path = 'build/block-checks.js';
$style_path = 'build/block-checks.css';

$script_handle = 'block-accessibility-script';

wp_enqueue_script(
$script_handle,
plugins_url($script_path, $this->pluginFile),
['wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor'],
filemtime(plugin_dir_path($this->pluginFile) . $script_path),
true
);


$this->localizeScript($script_handle);
}

private function localizeScript($handle)
{
wp_localize_script(
$handle,
'blockAccessibilitySettings',
['mode' => BLOCK_ACCESSIBILITY_MODE]
);
}

private function enqueueStyles()
{
$style_path = 'build/block-checks.css';
wp_enqueue_style(
'block-checks-style',
plugins_url($style_path, $this->pluginFile),
[],
filemtime(plugin_dir_path($this->pluginFile) . $style_path)
);

// Load the text domain for the script
wp_set_script_translations(
$script_handle,
'block-accessibility-checks',
plugin_dir_path($this->pluginFile) . 'languages'
);
}
}
26 changes: 26 additions & 0 deletions src/Translations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace BlockAccessibility;

class Translations
{
private $pluginFile;
private $textDomain;

public function __construct($pluginFile, $textDomain) {
$this->pluginFile = $pluginFile;
$this->textDomain = $textDomain;
}

public function loadTextDomain() {
load_plugin_textdomain($this->textDomain, false, dirname(plugin_basename($this->pluginFile)) . '/languages/');
}

public function setupScriptTranslations($scriptHandle) {
wp_set_script_translations(
$scriptHandle,
$this->textDomain,
plugin_dir_path($this->pluginFile) . 'languages'
);
}
}
6 changes: 5 additions & 1 deletion src/scripts/helpers/blockInvalidation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global blockAccessibilitySettings */
import { useDispatch } from '@wordpress/data';
import { GetInvalidBlocks } from './getInvalidBlocks';
import { useEffect } from '@wordpress/element';
Expand All @@ -15,7 +16,10 @@ export function BlockInvalidation() {
} = useDispatch('core/editor');

useEffect(() => {
if (invalidBlocks.length > 0) {
if (
invalidBlocks.length > 0 &&
blockAccessibilitySettings.mode === 'DENY'
) {
lockPostSaving();
lockPostAutosaving();
disablePublishSidebar();
Expand Down
Loading