-
Notifications
You must be signed in to change notification settings - Fork 0
/
kks-news-viewer.php
107 lines (95 loc) · 2.95 KB
/
kks-news-viewer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* Plugin Name: KKS News Viewer
* Description: Displays MailerLite newsletters via iframe
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.1.4
* Author: Shrinkray
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: kks-news-viewer
*
* @package kks
* @return void
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Register the block type
*/
function kks_newsletter_block_register(): void
{
register_block_type(
__DIR__ . '/build/block.json',
[
'render_template' => plugin_dir_path(__FILE__) . 'build/news-viewer.php'
]
);
if (function_exists('acf_register_block_type')) {
acf_register_block_type([
'name' => 'kks-news-viewer',
'title' => __('KKS News Viewer'),
'description' => __('Displays MailerLite newsletters via iframe'),
'render_template' => plugin_dir_path(__FILE__) . 'build/news-viewer.php',
'category' => 'acf-blocks',
'icon' => 'email',
'mode' => 'preview',
'supports' => [
'mode' => false,
'align' => true,
'jsx' => true
]
]);
}
}
/**
* Register ACF fields
*/
function kks_newsletter_register_include_fields(): void
{
$path = __DIR__ . '/acf-fields.json';
$field_json = json_decode( file_get_contents( $path ), true );
$field_json['location'] = array(
array(
array(
'param' => 'block',
'operator' => '==',
'value' => 'kks/kks-news-viewer', // block.json name.
),
),
);
$field_json['local'] = 'json';
$field_json['local_file'] = $path;
if (!function_exists('acf_add_local_field_group')) {
return;
}
acf_add_local_field_group( $field_json ); // This simplifies the process of adding fields without duplicating code.
}
add_action( 'kks/include_fields', 'kks_newsletter_register_include_fields' );
/**
* Initialize the plugin
*/
function kks_newsletter_init() {
add_action('init', 'kks_newsletter_block_register', 5);
add_action('acf/init', 'kks_newsletter_register_include_fields', 99);
}
// Initialize the plugin
add_action('plugins_loaded', 'kks_newsletter_init');
// Register block category
function kks_newsletter_block_categories(array $block_categories): array
{
$block_categories = array_merge(
[
[
'slug' => 'acf-blocks',
'title' => __( 'KKS Blocks', 'kks-newsletter-block' ),
'icon' => 'pm',
]
],
$block_categories,
);
return $block_categories;
}
add_filter( 'block_categories_all', 'kks_newsletter_block_categories' );