/** * ActivityPub Moderation Admin JavaScript */ /* global activitypubModerationL10n, jQuery */ /** * @param {Object} $ - jQuery * @param {Object} wp - WordPress global object * @param {Object} wp.i18n - Internationalization functions * @param {Object} wp.a11y - Accessibility functions * @param {Object} wp.ajax - AJAX functions */ (function( $, wp ) { 'use strict'; var __ = wp.i18n.__; var _n = wp.i18n._n; var sprintf = wp.i18n.sprintf; /** * Helper function to show a message using wp.a11y and alert * * @param {string} message - The message to display */ function showMessage( message ) { if ( wp.a11y && wp.a11y.speak ) { wp.a11y.speak( message, 'assertive' ); } alert( message ); } /** * Helper function to validate domain format * * @param {string} domain - The domain to validate * @return {boolean} Whether the domain is valid */ function isValidDomain( domain ) { // Basic domain validation - must contain at least one dot and valid characters var domainRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; return domainRegex.test( domain ) && domain.includes( '.' ) && domain.length > 3; } /** * Helper function to check if a term already exists in the UI * * @param {string} type - The type of block (domain or keyword) * @param {string} value - The value to check * @param {string} context - The context (user or site) * @param {number|null} userId - The user ID (for user context) * @return {boolean} Whether the term is already blocked */ function isTermAlreadyBlocked( type, value, context, userId ) { var selector; if ( context === 'user' ) { selector = '.activitypub-user-block-list[data-user-id="' + userId + '"] .remove-user-block-btn[data-type="' + type + '"][data-value="' + value + '"]'; } else if ( context === 'site' ) { selector = '.remove-site-block-btn[data-type="' + type + '"][data-value="' + value + '"]'; } return $( selector ).length > 0; } /** * Validate a blocked term value * * @param {string} type - The type of block (domain or keyword) * @param {string} value - The value to validate * @param {string} context - The context (user or site) * @param {number|null} userId - The user ID (for user context) * @return {boolean} Whether the value is valid */ function validateBlockedTerm( type, value, context, userId ) { if ( ! value ) { showMessage( __( 'Please enter a value to block.', 'activitypub' ) ); return false; } if ( type === 'domain' && ! isValidDomain( value ) ) { showMessage( __( 'Please enter a valid domain (e.g., example.com).', 'activitypub' ) ); return false; } if ( isTermAlreadyBlocked( type, value, context, userId ) ) { showMessage( __( 'This term is already blocked.', 'activitypub' ) ); return false; } return true; } /** * Create a table row for a blocked term. * * @param {string} type - The type of block (domain or keyword) * @param {string} value - The blocked value * @param {string} context - The context (user or site) * @return {jQuery} The constructed table row */ function createBlockedTermRow( type, value, context ) { var $button = $( '