-
Notifications
You must be signed in to change notification settings - Fork 8
/
hello-gutenberg.php
106 lines (95 loc) · 2.8 KB
/
hello-gutenberg.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
<?php
/**
* Plugin Name: Hello Gutenberg
* Plugin URI: https://github.com/HardeepAsrani/hello-gutenberg/
* Description: Gutenberg examples.
* Author: Hardeep Asrani
* Author URI: http://www.hardeepasrani.com
* Version: 1.0.0
* License: GPL2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* @package hello-gutenberg
*/
// Exit if accessed directly.
defined('ABSPATH') || exit;
// Load Sidebar Functions
require_once( plugin_dir_path( __FILE__ ) . 'sidebar.php' );
/**
* Enqueue front end and editor JavaScript and CSS
*/
function hello_gutenberg_scripts() {
$blockPath = '/dist/block.js';
$stylePath = '/dist/block.css';
// Enqueue the bundled block JS file
wp_enqueue_script(
'hello-gutenberg-block-js',
plugins_url( $blockPath, __FILE__ ),
[ 'wp-i18n', 'wp-blocks', 'wp-edit-post', 'wp-element', 'wp-editor', 'wp-components', 'wp-data', 'wp-plugins', 'wp-edit-post', 'wp-api' ],
filemtime( plugin_dir_path(__FILE__) . $blockPath )
);
// Enqueue frontend and editor block styles
wp_enqueue_style(
'hello-gutenberg-block-css',
plugins_url( $stylePath, __FILE__ ),
'',
filemtime( plugin_dir_path(__FILE__) . $stylePath )
);
}
// Hook scripts function into block editor hook
add_action( 'enqueue_block_assets', 'hello_gutenberg_scripts' );
/**
* Shortcode Initializer.
*/
function hello_gutenberg_shortcode_callback( $attr ) {
extract( $attr );
if ( isset( $tweet ) ) {
$output =
'<div class="' . ( ! empty( $theme ) ? $theme : 'click-to-tweet' ) . '">
<div class="ctt-text">' . $tweet . '</div>
<p><a href="https://twitter.com/intent/tweet?text='. ( ! empty( $tweetsent ) ? $tweetsent : $tweet ) .'" class="ctt-btn" target="_blank">' . $button . '</a></p>
</div>';
return $output;
}
}
add_shortcode( 'clicktotweet', 'hello_gutenberg_shortcode_callback' );
/**
* Block Initializer.
*/
add_action( 'plugins_loaded', function () {
if ( function_exists( 'register_block_type' ) ) {
// Hook server side rendering into render callback
register_block_type(
'hello-gutenberg/click-to-tweet', array(
'render_callback' => 'hello_gutenberg_block_callback',
'attributes' => array(
'tweet' => array(
'type' => 'string',
),
'tweetsent' => array(
'type' => 'string',
),
'button' => array(
'type' => 'string',
'default' => 'Tweet',
),
'theme' => array(
'type' => 'boolean',
'default' => false,
),
),
)
);
}
} );
/**
* Block Output.
*/
function hello_gutenberg_block_callback( $attr ) {
extract( $attr );
if ( isset( $tweet ) ) {
$theme = ( $theme === true ? 'click-to-tweet-alt' : 'click-to-tweet' );
$shortcode_string = '[clicktotweet tweet="%s" tweetsent="%s" button="%s" theme="%s"]';
return sprintf( $shortcode_string, $tweet, $tweetsent, $button, $theme );
}
}