WP_Icons_Registry::registerprotectedWP 1.0

Registers an icon.

Method of the class: WP_Icons_Registry{}

No Hooks.

Returns

true|false. True if the icon was registered with success and false otherwise.

Usage

// protected - for code of main (parent) or child class
$result = $this->register( $icon_name, $icon_properties );
$icon_name(string) (required)
Icon name including namespace.
$icon_properties(array) (required)

List of properties for the icon.

  • label(string)
    Required. A human-readable label for the icon.

  • content(string)
    Optional. SVG markup for the icon. If not provided, the content will be retrieved from the filePath if set. If both content and filePath are not set, the icon will not be registered.

  • filePath(string)
    Optional. The full path to the file containing the icon content.

WP_Icons_Registry::register() code WP 7.0

protected function register( $icon_name, $icon_properties ) {
	if ( ! isset( $icon_name ) || ! is_string( $icon_name ) ) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Icon name must be a string.' ),
			'7.0.0'
		);
		return false;
	}

	$allowed_keys = array_fill_keys( array( 'label', 'content', 'filePath' ), 1 );
	foreach ( array_keys( $icon_properties ) as $key ) {
		if ( ! array_key_exists( $key, $allowed_keys ) ) {
			_doing_it_wrong(
				__METHOD__,
				sprintf(
					// translators: %s is the name of any user-provided key
					__( 'Invalid icon property: "%s".' ),
					$key
				),
				'7.0.0'
			);
			return false;
		}
	}

	if ( ! isset( $icon_properties['label'] ) || ! is_string( $icon_properties['label'] ) ) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Icon label must be a string.' ),
			'7.0.0'
		);
		return false;
	}

	if (
		( ! isset( $icon_properties['content'] ) && ! isset( $icon_properties['filePath'] ) ) ||
		( isset( $icon_properties['content'] ) && isset( $icon_properties['filePath'] ) )
	) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Icons must provide either `content` or `filePath`.' ),
			'7.0.0'
		);
		return false;
	}

	if ( isset( $icon_properties['content'] ) ) {
		if ( ! is_string( $icon_properties['content'] ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Icon content must be a string.' ),
				'7.0.0'
			);
			return false;
		}

		$sanitized_icon_content = $this->sanitize_icon_content( $icon_properties['content'] );
		if ( empty( $sanitized_icon_content ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Icon content does not contain valid SVG markup.' ),
				'7.0.0'
			);
			return false;
		}
	}

	$icon = array_merge(
		$icon_properties,
		array( 'name' => $icon_name )
	);

	$this->registered_icons[ $icon_name ] = $icon;

	return true;
}