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
|
<?php /** * Custom Facebook Feed block with live preview. * * @since 2.3 */ namespace CustomFacebookFeed;
class CFF_Blocks {
/** * Indicates if current integration is allowed to load. * * @since 1.8 * * @return bool */ public function allow_load() { return function_exists( 'register_block_type' ); }
/** * Loads an integration. * * @since 2.3 */ public function load() { $this->hooks(); }
/** * Integration hooks. * * @since 2.3 */ protected function hooks() { add_action( 'init', array( $this, 'register_block' ) ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); }
/** * Register Custom Facebook Feed Gutenberg block on the backend. * * @since 2.3 */ public function register_block() {
wp_register_style( 'cff-blocks-styles', trailingslashit( CFF_PLUGIN_URL ) . 'assets/css/cff-blocks.css', array( 'wp-edit-blocks' ), CFFVER );
$attributes = array( 'shortcodeSettings' => array( 'type' => 'string', ), 'noNewChanges' => array( 'type' => 'boolean', ), 'executed' => array( 'type' => 'boolean', ) );
register_block_type( 'cff/cff-feed-block', array( 'attributes' => $attributes, 'render_callback' => array( $this, 'get_feed_html' ), ) ); }
/** * Load Custom Facebook Feed Gutenberg block scripts. * * @since 2.3 */ public function enqueue_block_editor_assets() { $access_token = get_option('cff_access_token'); \cff_main()->enqueue_styles_assets(); \cff_main()->enqueue_scripts_assets();
#cff_add_my_stylesheet(); #cff_scripts_method();
wp_enqueue_style( 'cff-blocks-styles' ); wp_enqueue_script( 'cff-feed-block', trailingslashit( CFF_PLUGIN_URL ) . 'assets/js/cff-blocks.js', array( 'wp-blocks', 'wp-i18n', 'wp-element' ), CFFVER, true );
$shortcodeSettings = '';
$i18n = array( 'addSettings' => esc_html__( 'Add Settings', 'custom-facebook-feed' ), 'shortcodeSettings' => esc_html__( 'Shortcode Settings', 'custom-facebook-feed' ), 'example' => esc_html__( 'Example', 'custom-facebook-feed' ), 'preview' => esc_html__( 'Apply Changes', 'custom-facebook-feed' ),
);
wp_localize_script( 'cff-feed-block', 'cff_block_editor', array( 'wpnonce' => wp_create_nonce( 'facebook-blocks' ), 'canShowFeed' => ! empty( $access_token ), 'configureLink' => get_admin_url() . '?page=cff-top', 'shortcodeSettings' => $shortcodeSettings, 'i18n' => $i18n, ) ); }
/** * Get form HTML to display in a Custom Facebook Feed Gutenberg block. * * @param array $attr Attributes passed by Custom Facebook Feed Gutenberg block. * * @since 2.3 * * @return string */ public function get_feed_html( $attr ) {
$return = '';
$shortcode_settings = isset( $attr['shortcodeSettings'] ) ? $attr['shortcodeSettings'] : '';
$shortcode_settings = str_replace(array( '[custom-facebook-feed', ']' ), '', $shortcode_settings);
$return .= do_shortcode( '[custom-facebook-feed '.$shortcode_settings.']' );
return $return;
}
/** * Checking if is Gutenberg REST API call. * * @since 2.3 * * @return bool True if is Gutenberg REST API call. */ public static function is_gb_editor() {
// TODO: Find a better way to check if is GB editor API call. return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore }
}
|