Skip to content

JS Hooks & Filters

Daniel L. Iser edited this page Oct 4, 2023 · 1 revision

JavaScript Filters

/**
 * Filter the control attributes.
 *
 * @param {Object} attributes Attributes for the block.
 * @param {Object} settings   Settings for the block.
 *
 * @return {Object} Modified attributes.
 */
return applyFilters( 'contentControl.blockControls.controlAttributes', {} );

/**
 * Filter the explicitly allowed block types.
 *
 * @param {string[]} allowedBlocks Array of explicitly allowed block types.
 *
 * @return {string[]} Array of explicitly allowed block types.
 */
applyFilters(
 'contentControl.blockEditor.allowedBlocks',
 getAllowedBlocks()
) as string[];

/**
 * Filter the explicitly excluded block types.
 *
 * @param {string[]} excludedBlocks Array of explicitly excluded block types.
 *
 * @return {string[]} Array of explicitly excluded block types.
 */
applyFilters(
 'contentControl.blockEditor.excludedBlocks',
 getExcludedBlocks()
) as string[];

/**
 * Filter the screen sizes available for device rules.
 *
 * @param {DeviceScreenSizes} screenSizes The screen sizes available for device rules.
 *
 * @return {DeviceScreenSizes} The filtered screen sizes.
 */
const screenSizes = applyFilters( 'contentControl.blockControls.screenSizes', {
 mobile: { label: __( 'Mobile', 'content-control' ), icon: mobile },
 tablet: { label: __( 'Tablet', 'content-control' ), icon: tablet },
 desktop: {
  label: __( 'Desktop', 'content-control' ),
  icon: desktop,
 },
} ) as DeviceScreenSizes;

/**
 * Filter the default block controls.
 *
 * @param {BlockControls} defaults The default block controls.
 *
 * @return {BlockControls} The filtered default block controls.
 */
const defaults = applyFilters(
 'contentControl.blockControls.defaultBlockControls',
 {
  enabled: false,
  rules: {
   device: getDefaultDeviceBlockControls(),
   user: getDefaultUserBlockControls(),
  },
 }
) as B extends true
 ? {
   enabled: boolean;
   rules: NonNullableFields< BlockControls[ 'rules' ] >;
   }
 : BlockControls;

/**
 * Filter the default device block controls.
 *
 * @param {DeviceBlockControlsGroup} defaults The default device block controls.
 *
 * @return {DeviceBlockControlsGroup} The filtered default device block controls.
 */
return applyFilters(
 'contentControl.blockControls.defaultDeviceBlockControls',
 {
  hideOn: {
   desktop: false,
   tablet: false,
   mobile: false,
  },
 }
) as DeviceBlockControlsGroup;

/**
 * Filter the default user block controls.
 *
 * @param {UserBlockControlsGroup} defaults The default user block controls.
 *
 * @return {UserBlockControlsGroup} The filtered default user block controls.
 */
return applyFilters( 'contentControl.blockControls.defaultUserBlockControls', {
 userStatus: 'logged_in',
 roleMatch: 'any',
 userRoles: [],
} ) as UserBlockControlsGroup;

/**
 * Filter the block controls context.
 *
 * @param {BlockControlsContextType} blockControlsContext The block controls context.
 *
 * @return {BlockControlsContextType} The filtered block controls context.
 */
return applyFilters(
 'contentControl.blockControls.context',
 blockControlsContext
) as typeof blockControlsContext;

/**
 * Filter the default settings.
 *
 * @param {Settings} settings Default settings.
 *
 * @return {Settings} Default settings.
 */
applyFilters( 'contentControl.defaultSettings', {} );

/**
 * Filter the list of views.
 *
 * @param {TabComponent[]} views List of views.
 *
 * @return {TabComponent[]} Filtered list of views.
 */
_views = applyFilters( 'contentControl.adminViews', [] );

/**
 * Define the tabs to show in the editor.
 *
 * @param {TabComponent[]} tabs Array of tab components.
 *
 * @return {TabComponent[]} Array of tab components.
 */
const tabs: TabComponent[] = applyFilters(
 'contentControl.restrictionEditor.tabs',
 []
);

/**
 * Allow external overrides via a filter with null default.
 *
 * @param {boolean|undefined}       show     The current value of the field.
 * @param {string}                  field    The field name.
 * @param {Restriction['settings']} settings The current settings.
 * @param {string}                  tab      The current tab name.
 * @return {boolean|undefined} The new value of the field.
 */
const show = applyFilters(
 'contentControl.restrictionEditor.showField',
 undefined,
 field,
 settings
) as boolean | undefined;

/**
 * Filter the tabs that are shown on the settings page.
 *
 * @param {TabComponent[]} tabs The tabs that are shown on the settings page.
 *
 * @return {TabComponent[]} The filtered tabs.
 */
const tabs: TabComponent[] = applyFilters(
 'contentControl.settingsPage.tabs',
 []
);

/**
 * Filter the list of known block types.
 *
 * @param {typeof knownBlockTypes} knownBlockTypes The list of known block types.
 *
 * @return {typeof knownBlockTypes} The filtered list of known block types.
 */
const blockTypes = applyFilters(
 'contentControl.settingsPage.knownBlockTypes',
 knownBlockTypes
) as typeof knownBlockTypes;

/**
 * Filtered & mappable list of TabComponent definitions.
 *
 * @param {RegisteredMediaQuery[]} mediaQueries List of media query definitions.
 *
 * @return {RegisteredMediaQuery[]} Filtered list of media query definitions.
 */
const mediaQueries: RegisteredMediaQuery[] = applyFilters(
 'contentControl.settingsPage.customMediaQueries',
 []
);

/**
 * Filtered & mappable list of TabComponent definitions.
 *
 * @param {SectionList} sections List of sections.
 *
 * @return {SectionList} Filtered list of sections.
 */
const sections: SectionList = applyFilters(
 'contentControl.settingsPage.blockControlSections',
 []
);

/**
 * Filter the list of sections on the "General" tab.
 *
 * @param {SectionList} sections List of sections.
 *
 * @return {SectionList} Filtered list of sections.
 */
const sections: SectionList = applyFilters(
 'contentControl.settingsPage.generalSections',
 []
) as SectionList;

/**
 * Filterable list of plugin permission setting fields.
 *
 * @param {Object[]} pluginPermissions List of plugin permission setting fields.
 *
 * @return {Object[]} Filtered list of plugin permission setting fields.
 */
const pluginPermissions: {
 name: string;
 label: string;
 description?: string;
 default: string;
}[] = applyFilters( 'contentControl.settingsPage.pluginPermissionLabels', [] );

/**
 * Filter the list of sections on the "Upgrade" tab.
 *
 * @param {SectionList} sections List of sections.
 *
 * @return {SectionList} Filtered list of sections.
 */
const sections: SectionList = applyFilters(
 'contentControl.settingsPage.upgradeSections',
 []
) as SectionList;

Clone this wiki locally