-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
executable file
·163 lines (135 loc) · 3.67 KB
/
plugin.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Plugin Name: Advanced Tabs Gutenberg Block
* Description: A custom Gutenberg Block to show content in tabs style.
* Requires at least: 5.7
* Requires PHP: 7.0
* Version: 1.1.2
* Author: Zakaria Binsaifullah
* Author URI: https://makegutenblock.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: advanced-tabs-block
*
* @package @wordpress/create-block
*/
/**
* @package Zero Configuration with @wordpress/create-block
* [atbs] && [ATBS] ===> Prefix
*/
// Stop Direct Access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// include admin page
require_once plugin_dir_path( __FILE__ ) . 'admin/admin.php';
/**
* Blocks Final Class
*/
final class ATBS_BLOCKS_CLASS {
public function __construct() {
// define constants
$this->atbs_define_constants();
// block initialization
add_action( 'init', [ $this, 'atbs_blocks_init' ] );
// admin page
add_action( 'activated_plugin', [ $this, 'atbs_user_admin_page' ] );
// blocks category
if( version_compare( $GLOBALS['wp_version'], '5.7', '<' ) ) {
add_filter( 'block_categories', [ $this, 'atbs_register_block_category' ], 10, 2 );
} else {
add_filter( 'block_categories_all', [ $this, 'atbs_register_block_category' ], 10, 2 );
}
// enqueue block assets
add_action( 'enqueue_block_assets', [ $this, 'atbs_external_libraries' ] );
}
/**
* Initialize the plugin
*/
public static function init(){
static $instance = false;
if( ! $instance ) {
$instance = new self();
}
return $instance;
}
/**
* Define the plugin constants
*/
private function atbs_define_constants() {
define( 'ATBS_VERSION', '1.1.0' );
define( 'ATBS_URL', plugin_dir_url( __FILE__ ) );
define( 'ATBS_LIB_URL', ATBS_URL . 'lib/' );
}
/**
* Blocks Registration
*/
public function atbs_register_block( $name, $options = array() ) {
register_block_type( __DIR__ . '/build/blocks/' . $name, $options );
}
// render inline css
public function atbs_render_inline_css( $handle, $css ) {
wp_register_style( $handle, false );
wp_enqueue_style( $handle );
wp_add_inline_style( $handle, $css );
}
/**
* Blocks Initialization
*/
public function atbs_blocks_init() {
// register single block
$this->atbs_register_block( 'tabs', array(
'render_callback' => [ $this, 'atbs_render_block' ],
) );
}
// inline css
public function atbs_inline_css($handle, $css){
// register inline style
wp_register_style( $handle, false );
// enqueue inline style
wp_enqueue_style( $handle );
// add inline style at head
wp_add_inline_style( $handle, $css );
}
// render function
public function atbs_render_block($attributes, $content){
require_once __DIR__ . '/templates/tabs.php';
$handle = 'atbs-'.$attributes['uniqueId'];
$this->atbs_inline_css( $handle, tabs_callback($attributes));
return $content;
}
/**
* Register Block Category
*/
public function atbs_register_block_category( $categories, $post ) {
return array_merge(
array(
array(
'slug' => 'atbs-block',
'title' => __( 'Tabs Block', 'advanced-tabs-block' ),
),
),
$categories,
);
}
// Admin page
public function atbs_user_admin_page( $plugin ) {
if( plugin_basename(__FILE__) == $plugin ){
wp_redirect( admin_url( 'tools.php?page=atbs-block' ) );
die();
}
}
/**
* Enqueue Block Assets
*/
public function atbs_external_libraries() {
// enqueue JS
if( ! is_admin() && has_block('atbs/tabs')){
wp_enqueue_script( 'atbs-tabs', ATBS_LIB_URL . 'js/tabs.js', array(), ATBS_VERSION, true );
}
}
}
/**
* Kickoff
*/
ATBS_BLOCKS_CLASS::init();