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/settings page #59

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
308 changes: 282 additions & 26 deletions admin/class-admin.php

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions filters/ncb-denhaag-meta-accordion-allowed-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
/**
* Return boolean based on post types we want to allow to show the Share buttons.
*
* @param array $blocks The allowed innerBlocks.
* @param string $post_type Post types we want to allow.
* @param int $post_id The post ID.
* @param array $blocks The allowed innerBlocks.
*
* @return bool
* @return array
*/
function ncb_filter_denhaag_meta_accordion_allowed_blocks( array $blocks = [], string $post_type = 'post', int $post_id = 0 ) {
function ncb_filter_denhaag_meta_accordion_allowed_blocks( array $blocks = [] ):array {
$ncb_blocks = [
'ncb-denhaag/authentication',
'ncb-denhaag/button-group',
Expand All @@ -33,10 +31,10 @@ function ncb_filter_denhaag_meta_accordion_allowed_blocks( array $blocks = [], s
'ncb-denhaag/table',

// Core components we allow.
'core/list'
'core/list',
];

return array_unique( array_merge( $blocks, $ncb_blocks ) );
}

add_filter( 'ncb_denhaag_meta_accordion_allowed_blocks', 'ncb_filter_denhaag_meta_accordion_allowed_blocks', 10, 3 );
add_filter( 'ncb_denhaag_meta_accordion_allowed_blocks', 'ncb_filter_denhaag_meta_accordion_allowed_blocks', 10, 1 );
8 changes: 0 additions & 8 deletions filters/ncb-denhaag-meta-buttons.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,3 @@ function ncb_filter_denhaag_meta_buttons( string $post_type = 'post', int $post_
}

add_filter( 'ncb_denhaag_meta_buttons', 'ncb_filter_denhaag_meta_buttons', 10, 2 );








5 changes: 2 additions & 3 deletions filters/ncb-filter-denhaag-meta-show-share.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
*
* @param bool $state The default state.
* @param string $post_type Post types we want to allow.
* @param int $post_id The post ID.
*
* @return bool
*/
function ncb_filter_denhaag_meta_show_share( bool $state, string $post_type = 'post', int $post_id = 0 ) {
function ncb_filter_denhaag_meta_show_share( bool $state, string $post_type = 'post' ): bool {
return in_array( esc_attr( $post_type ), [ 'post' ], true );
}

add_filter( 'ncb_denhaag_meta_show_share', 'ncb_filter_denhaag_meta_show_share', 10, 3 );
add_filter( 'ncb_denhaag_meta_show_share', 'ncb_filter_denhaag_meta_show_share', 10, 2 );
18 changes: 5 additions & 13 deletions filters/ncb-filter-denhaag-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,23 @@
/**
* Returns array of metadata we want to show in the denhaag/meta block.
*
* @param array $array Array of meta data.
* @param array $args Array of meta data.
* @param string $post_type The post type.
* @param int $post_id The post ID.
*
* @return array
*/
function ncb_filter_denhaag_meta( array $array = [], string $post_type = 'post', int $post_id = 0 ) {
function ncb_filter_denhaag_meta( array $args = [], string $post_type = 'post', int $post_id = 0 ): array {

if ( in_array( $post_type, apply_filters( 'ncb_denhaag_meta_allow_post_types_post_type', [ 'post' ] ), true ) ) {
$array['post_type'] = apply_filters( 'ncb_denhaag_meta_post_type_label', $post_type, 'singular_name' );
$args['post_type'] = apply_filters( 'ncb_denhaag_meta_post_type_label', $post_type, 'singular_name' );
}

if ( in_array( $post_type, apply_filters( 'ncb_denhaag_meta_allow_post_types_date', [ 'post' ] ), true ) ) {
$array['date'] = apply_filters( 'ncb_denhaag_meta_date', get_the_date( 'U', $post_id ) );
$args['date'] = apply_filters( 'ncb_denhaag_meta_date', get_the_date( 'U', $post_id ) );
}

return ! empty( $array ) ? array_filter( $array ) : [];
return ! empty( $args ) ? array_filter( $args ) : [];
}

add_filter( 'ncb_denhaag_meta', 'ncb_filter_denhaag_meta', 10, 3 );








100 changes: 58 additions & 42 deletions frontend/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class Frontend {
* @return array Array of block categories.
*/
public function register_custom_block_category( $categories ) {

$ncb_categories = [
[
'slug' => 'nlds-community-blocks',
Expand All @@ -52,7 +51,6 @@ public function register_custom_block_category( $categories ) {
],
];


foreach ( glob( NCB_ABSPATH . NCB_ASSETS_DIR . 'blocks/*', GLOB_ONLYDIR ) as $ncb_community_path ) {
if ( empty( $ncb_community_path ) ) {
continue;
Expand Down Expand Up @@ -98,7 +96,7 @@ public function enqueue_block_assets() {
return false;
}

$ncb_theme = esc_attr( get_option( 'ncb_municipality', 'denhaag' ) );
$ncb_theme = esc_attr( get_option( 'ncb_organisation', '' ) );
if ( ! empty( $ncb_theme ) && Plugin::has_resource( NCB_ABSPATH . NCB_ASSETS_DIR . "client/tokens/ncb-$ncb_theme-tokens.css" ) ) {
wp_enqueue_style(
"ncb-$ncb_theme-tokens",
Expand All @@ -125,9 +123,9 @@ public function enqueue_block_assets() {

// Get block version, priority to package-lock.json version.
$ncb_package_version = self::get_package_version( self::get_package_name_from_block_name( $ncb_block_meta['name'] ) );
if( empty( $ncb_package_version ) && ! empty( $ncb_block_meta['version'] ) ) {
if ( empty( $ncb_package_version ) && ! empty( $ncb_block_meta['version'] ) ) {
$ncb_package_version = esc_attr( $ncb_block_meta['version'] );
} else if ( empty( $ncb_package_version ) ) {
} elseif ( empty( $ncb_package_version ) ) {
$ncb_package_version = filemtime( $ncb_community . '/style.css' );
}

Expand Down Expand Up @@ -318,12 +316,19 @@ public function ncb_denhaag_extend_wp_kses_posts( $allowed_tags ) {
/**
* Set <body> class based on the selected theme.
*
* @param {array} $classes An array of classes.
* @param string|string[] $classes An array of classes.
*
* @return mixed
* @source https://developer.wordpress.org/reference/functions/body_class/
*
* @return array
*/
public function ncb_body_class_by_community_theme( $classes ) {
$ncb_theme = esc_attr( get_option( 'ncb_municipality', 'denhaag' ) );
public function ncb_body_class_by_community_theme( mixed $classes ): array {

if ( is_string( $classes ) ) {
$classes = explode( ' ', $classes );
}

$ncb_theme = esc_attr( get_option( 'ncb_organisation', '' ) );
if ( ! empty( $ncb_theme ) ) {
$classes[] = "$ncb_theme-theme";
}
Expand Down Expand Up @@ -435,10 +440,12 @@ public function ncb_action_community_icons_based_on_block() {
/*
* Each block that has an icon.
*/
if ( self::has_block_in_editor_or_widgets( [
'ncb-denhaag/accordion',
'ncb-denhaag/accordion-item',
] ) ) {
if ( self::has_block_in_editor_or_widgets(
[
'ncb-denhaag/accordion',
'ncb-denhaag/accordion-item',
]
) ) {
$icons['denhaag'][] = [
'@type' => 'path',
'id' => 'ncb-denhaag-chevron-down-icon',
Expand All @@ -459,12 +466,14 @@ public function ncb_action_community_icons_based_on_block() {
];
}

if ( self::has_block_in_editor_or_widgets( [
'ncb-denhaag/button',
'ncb-denhaag/description-list',
'ncb-denhaag/paragraph',
'ncb-denhaag/link-item',
] ) ) {
if ( self::has_block_in_editor_or_widgets(
[
'ncb-denhaag/button',
'ncb-denhaag/description-list',
'ncb-denhaag/paragraph',
'ncb-denhaag/link-item',
]
) ) {
$icons['denhaag'][] = [
'@type' => 'path',
'id' => 'ncb-denhaag-external-icon',
Expand All @@ -473,7 +482,7 @@ public function ncb_action_community_icons_based_on_block() {
];
}

if ( self::has_block_in_editor_or_widgets( ['ncb-denhaag/button'] ) ) {
if ( self::has_block_in_editor_or_widgets( [ 'ncb-denhaag/button' ] ) ) {
$icons['denhaag'][] = [
'@type' => 'path',
'id' => 'ncb-denhaag-arrow-right-icon',
Expand All @@ -489,11 +498,13 @@ public function ncb_action_community_icons_based_on_block() {
];
}

if ( self::has_block_in_editor_or_widgets( [
'ncb-denhaag/highlighted-links',
'ncb-denhaag/link-group',
'ncb-denhaag/link-item',
] ) ) {
if ( self::has_block_in_editor_or_widgets(
[
'ncb-denhaag/highlighted-links',
'ncb-denhaag/link-group',
'ncb-denhaag/link-item',
]
) ) {
$icons['denhaag'][] = [
'@type' => 'path',
'id' => 'ncb-denhaag-arrow-right-icon',
Expand Down Expand Up @@ -550,11 +561,13 @@ public function ncb_action_community_icons_based_on_block() {
];
}

if ( self::has_block_in_editor_or_widgets( [
'ncb-denhaag/social-links',
'ncb-denhaag/social-link',
'ncb-denhaag/meta',
] ) ) {
if ( self::has_block_in_editor_or_widgets(
[
'ncb-denhaag/social-links',
'ncb-denhaag/social-link',
'ncb-denhaag/meta',
]
) ) {
$icons['denhaag'][] = [
'@type' => 'path',
'id' => 'ncb-denhaag-whatsapp-icon',
Expand Down Expand Up @@ -742,10 +755,12 @@ private static function get_package_version( $handle ): string {
return '';
}

$lock = get_object_vars( json_decode(
$lock = get_object_vars(
json_decode(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( $lock )
) );
file_get_contents( $lock )
)
);
}

if ( ! empty( $lock['dependencies']->{$handle} ) ) {
Expand All @@ -758,22 +773,22 @@ private static function get_package_version( $handle ): string {
/**
* Ge the package name from the block name.
*
* @param string $string The name of the block.
* @param string $block_name The name of the block.
*
* @return string|null
*/
private static function get_package_name_from_block_name( $string ) {
private static function get_package_name_from_block_name( string $block_name ): ?string {

if( empty( $string )) {
if ( empty( $block_name ) ) {
return null;
}

if ( str_starts_with( $string, '@gemeente-' ) ) {
if ( str_starts_with( $block_name, '@gemeente-' ) ) {
// Probably already formatted to the correct format.
return $string;
return $block_name;
}

preg_match( '/ncb-([a-zA-Z]+)[\/-]([a-zA-Z-]+)/i', $string, $matches );
preg_match( '/ncb-([a-zA-Z]+)[\/-]([a-zA-Z-]+)/i', $block_name, $matches );

if ( empty( $matches ) || empty( $matches[1] ) || empty( $matches[2] ) ) {
return null;
Expand All @@ -785,23 +800,24 @@ private static function get_package_name_from_block_name( $string ) {

/**
* Returns boolean if one of the blocks is on the page.
*
* @param string[] $block_names Array of blocknames.
*
* @return bool
*/
private static function has_blocks( $block_names ) {
if( empty( $block_names ) ) {
if ( empty( $block_names ) ) {
return false;
}

if ( is_string( $block_names ) ) {
return has_block( $block_names );
}

foreach( $block_names as $block_name) {
foreach ( $block_names as $block_name ) {
$ncb_has_block = has_block( $block_name );

if( $ncb_has_block ) {
if ( $ncb_has_block ) {
return true;
}
}
Expand Down
1 change: 0 additions & 1 deletion functions/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,3 @@ function ncb_file_get_contents( $path_or_url ) {
return false;
}
}

8 changes: 5 additions & 3 deletions functions/ncb-mix.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ function ncb_mix( $path ): string {
return NCB_ASSETS_URL . $path;
}

$manifest = get_object_vars( json_decode(
$manifest = get_object_vars(
json_decode(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( $manifest ),
) );
file_get_contents( $manifest ),
)
);
}

if ( ! str_starts_with( $path, '/' ) ) {
Expand Down
5 changes: 3 additions & 2 deletions includes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function define_constants() {
private function set_locale() {
$plugin_i18n = new I18n();

add_action( 'plugins_loaded', array( $plugin_i18n, 'load_plugin_textdomain' ) );
add_action( 'plugins_loaded', [ $plugin_i18n, 'load_plugin_textdomain' ] );
}

/**
Expand All @@ -100,6 +100,7 @@ private function define_admin_hooks() {

$frontend = new Frontend();
add_action( 'admin_head', [ $frontend, 'ncb_action_community_icons_based_on_block' ] );
add_action( 'admin_enqueue_scripts', [ $admin, 'enqueue_settings_page_assets' ] );
}

/**
Expand All @@ -120,7 +121,7 @@ private function define_frontend_hooks() {
add_filter( 'body_class', [ $frontend, 'ncb_body_class_by_community_theme' ] );
add_action( 'wp_head', [ $frontend, 'ncb_action_community_icons_based_on_block' ] );

$ncb_theme = esc_attr( get_option( 'ncb_municipality', 'denhaag' ) );
$ncb_theme = esc_attr( get_option( 'ncb_organisation', '' ) );
switch ( $ncb_theme ) {
case 'denhaag':
add_filter( 'wp_kses_allowed_html', [ $frontend, 'ncb_denhaag_extend_wp_kses_posts' ] );
Expand Down
2 changes: 1 addition & 1 deletion nlds-community-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}

require_once plugin_dir_path( __FILE__ ) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'class-autoloader.php';
spl_autoload_register( array( '\Nlds_Community_Blocks\Includes\Autoloader', 'autoload' ) );
spl_autoload_register( [ '\Nlds_Community_Blocks\Includes\Autoloader', 'autoload' ] );

// Make sure global functions are loaded.
foreach ( glob( plugin_dir_path( __FILE__ ) . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR . '*.php' ) as $ncb_plugin_functions ) {
Expand Down
Loading
Loading