-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.php
More file actions
111 lines (103 loc) · 3.74 KB
/
plugin.php
File metadata and controls
111 lines (103 loc) · 3.74 KB
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
<?php
/**
* UBC Wiki Embed Block
*
* @package UBC Blocks
* @author Kelvin Xu
* @copyright 2020 University of British Columbia
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: UBC Wiki Embed Block
* Plugin URI: https://ubc.ca/
* Description: Gutenberg block to embed content from wiki websites
* Version: 1.0.0
* Author: Kelvin Xu
* Text Domain: ubc-wiki-embed-block
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
namespace UBC\Blocks\WikiEmbed;
add_action('init', __NAMESPACE__ . '\\load_block_assets', 10);
/**
* Load assets required by Wiki Embed Block. Javascript and CSS
* Enqueue to editor only
*
* @return void
*/
function load_block_assets(){
wp_register_script(
'ubc-wiki-embed-script',
plugins_url( 'build/block.js', __FILE__ ),
array(
'wp-blocks',
'wp-i18n',
'wp-element',
'wp-editor',
'wp-plugins',
'wp-edit-post',
),
filemtime( plugin_dir_path( __FILE__ ) . 'build/block.js'),
true
);
wp_register_style(
'ubc-wiki-embed-style',
plugins_url( '/build/block.css', __FILE__ ),
array( 'wp-edit-blocks' ),
filemtime( plugin_dir_path( __FILE__ ) . 'build/block.css' )
);
// Get default settings from Wiki Embed plugin
$wiki_embed_defaults = get_option( 'wikiembed_options' ) ? get_option( 'wikiembed_options' )['default'] : null;
/**
* Wiki embed plugin only save global settings when clicked on save button on the settings page, not during plugin activation.
* So all the settings here have hardcoded fallback aligned with Wiki Embed plugin default.
*/
register_block_type(
'ubc/wiki-embed',
array(
'editor_script' => 'ubc-wiki-embed-script',
'editor_style' => 'ubc-wiki-embed-style',
'render_callback' => __NAMESPACE__ . '\\render_block',
'attributes' => array(
'source' => array(
'type' => 'string',
'default' => '',
),
'headingType' => array(
'type' => 'integer',
'default' => $wiki_embed_defaults ? absint( $wiki_embed_defaults['tabs'] ) : 1,
),
'noEditLinks' => array(
'type' => 'boolean',
'default' => $wiki_embed_defaults ? boolval( $wiki_embed_defaults['no-edit'] ) : true,
),
'noTabContens' => array(
'type' => 'boolean',
'default' => $wiki_embed_defaults ? boolval( $wiki_embed_defaults['no-contents'] ) : true,
),
'noInfoBox' => array(
'type' => 'boolean',
'default' => $wiki_embed_defaults ? boolval( $wiki_embed_defaults['no-infobox'] ) : false,
),
)
)
);
}
/**
* Server Side Rendering content of the Wiki Embed block.
*
* @param [array] $attributes Block attributes ( settings about how content should be embeded and displayed )
* @return void
*/
function render_block( $attributes ) {
return $attributes['source'] ? do_shortcode(
sprintf(
'[wiki-embed%1$s%2$s%3$s%4$s%5$s]',
$attributes['source'] ? ' url="' . esc_html( $attributes['source'] ) . '"' : '',
$attributes['headingType'] === 0 ? '' : ' ' . ( $attributes['headingType'] === 1 ? 'tabs' : 'accordion' ),
$attributes['noEditLinks'] ? ' no-edit' : '',
$attributes["noTabContens"] ? ' no-contents' : '',
$attributes["noInfoBox"] ? ' no-infobox' : ''
)
) : '';
}