PluginProbe
NHS Blocks / 1.2
NHS Blocks v1.2
1.4.1 1.4.0 trunk 1.0.0 1.0.1 1.0.4 1.1.0 1.1.1 1.1.3 1.1.4 1.1.4.1 1.1.5 1.1.6 1.1.7 1.1.8 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 All 47 releases
nhsblocks / nhsblocks.php

nhsblocks.php in NHS Blocks 1.2, at nhsblocks.php

393 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: NHS Blocks
4 * Plugin URI: https://github.com/NHSLeadership/nhsblocks
5 * Description: Gutenberg native custom blocks companion plugin for the NHS Nightingale theme (can also be standalone). Based on nhsuk frontend framework.
6 * Author: Tony Blacker, NHS Leadership Academy
7 * License: GPL v3
8 * Requires at least: 5.0
9 * Tested up to: 5.4.1
10 *
11 * Version: 1.2
12 * Stable tag: 1.2
13 *
14 * @package nhsblocks
15 */
16
17 defined( 'ABSPATH' ) || exit;
18
19 /**
20 * Load translations (if any) for the plugin from the /languages/ folder.
21 *
22 * @link https://developer.wordpress.org/reference/functions/load_plugin_textdomain/
23 */
24 add_action( 'init', 'nhsblocks_load_textdomain' );
25
26 /**
27 * Set the domain to be used for translations.
28 */
29 function nhsblocks_load_textdomain() {
30 load_plugin_textdomain( 'nhsblocks', false, basename( __DIR__ ) . '/languages' );
31 }
32
33 /**
34 * Add custom "nhsblocks" block category.
35 *
36 * @link https://wordpress.org/gutenberg/handbook/designers-developers/developers/filters/block-filters/#managing-block-categories
37 */
38 add_filter( 'block_categories', 'nhsblocks_block_categories', 10, 2 );
39
40 /**
41 * Create the category.
42 *
43 * @param array $categories the details of added categories (in this case an array of 1 item).
44 * @param integer $post Unused variable, intended for future expansion of function.
45 *
46 * @return array
47 */
48 function nhsblocks_block_categories( $categories, $post ) {
49
50 return array_merge(
51 $categories,
52 array(
53 array(
54 'slug' => 'nhsblocks',
55 'title' => __( 'NHS Frontend Blocks', 'nhsblocks' ),
56 'icon' => 'screen',
57 ),
58 )
59 );
60 }
61
62 /**
63 * Registers all block assets so that they can be enqueued through the Block Editor in
64 * the corresponding context.
65 *
66 * @link https://wordpress.org/gutenberg/handbook/designers-developers/developers/block-api/block-registration/
67 */
68 add_action( 'init', 'nhsblocks_register_blocks' );
69
70 /**
71 * Function to initiate the Gutenberg blocks in this theme.
72 */
73 function nhsblocks_register_blocks() {
74
75 // If Block Editor is not active, bail.
76 if ( ! function_exists( 'register_block_type' ) ) {
77 return;
78 }
79
80 // Register the block editor script.
81 wp_register_script(
82 'nhsblocks-editor-script', // label.
83 plugins_url( '/build/index.js', __FILE__ ), // script file.
84 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor', 'wp-data' ), // dependencies.
85 '20201202', // version
86 'in_footer' // where to load
87 );
88
89 register_block_type(
90 'nhsblocks/panel1',
91 array(
92 'editor_script' => 'nhsblocks-editor-script',
93 // Calls registered script above. Registering one brings all. One block to rule them all.
94 )
95 );
96 register_block_type( 'nhsblocks/dashboardnav' );
97 register_block_type( 'nhsblocks/dodont' );
98 register_block_type( 'nhsblocks/nhsbutton' );
99 register_block_type( 'nhsblocks/reveal1' );
100 register_block_type( 'nhsblocks/promo1' );
101 register_block_type( 'nhsblocks/quote1' );
102 register_block_type( 'nhsblocks/card1' );
103 register_block_type( 'nhsblocks/rowgroup' );
104 register_block_type( 'nhsblocks/heroblock' );
105 register_block_type( 'nhsblocks/contentslist' );
106 register_block_type( 'nhsblocks/contentslistitem' );
107 register_block_type( 'nhsblocks/reviewdate' );
108 register_block_type( 'nhsblocks/stripesblock' );
109
110 register_block_type( 'nhsblocks/pagination' );
111
112 if ( function_exists( 'wp_set_script_translations' ) ) {
113 /**
114 * Adds internationalization support.
115 *
116 * @link https://wordpress.org/gutenberg/handbook/designers-developers/developers/internationalization/
117 * @link https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/
118 */
119 wp_set_script_translations( 'nhsblocks-editor-script', 'nhsblocks', plugins_url( '/languages', __FILE__ ) );
120 }
121
122 }
123
124
125 add_action( 'plugins_loaded', 'nhsblocks_register_dynamic_blocks' );
126
127
128 /**
129 *
130 * Taken / Inspired by https://johnblackbourn.com/gutenberg-block-template-part/
131 *
132 * Generic block rendering callback function to load a block from a theme template part.
133 *
134 * Loads a block from the `blocks` subdirectory according to the name of the block, and places the
135 * block attributes and block content into namespaced query vars. If there's no corresponding block
136 * template part, the block content is returned unaltered.
137 *
138 * A block named `foo/block1` looks for a template part named `blocks/foo/block1.php`.
139 *
140 * The block attributes and content can be accessed inside the template part via query vars:
141 *
142 * - `get_query_var( 'foo/block1/attribute1' )`
143 * - `get_query_var( 'foo/block1/attribute2' )`
144 * - `get_query_var( 'foo/block1/content' )`
145 *
146 * @param string $name The full block name, for example 'foo/block1'.
147 * @param array $attributes Array of attributes saved on the block instance.
148 * @param string $content Optional user-entered block content. Can be null.
149 * @return string The dynamic block content.
150 */
151
152 function nhsblocks_register_dynamic_blocks() {
153
154 // Only load if Gutenberg is available.
155 if ( ! function_exists( 'register_block_type' ) ) {
156 return;
157 }
158
159 $blocks = [
160 'nhsblocks/contentslistpage',
161 ];
162
163 foreach ( $blocks as $block ) {
164
165 register_block_type(
166 $block,
167 [
168 // https://github.com/WordPress/gutenberg/issues/4671
169 'render_callback' => function( array $attributes, string $content = null ) use ( $block ) {
170
171 return nhsblocks_block_renderer( $block, $attributes, $content );
172 },
173 ]
174 );
175
176 }
177
178 }
179
180
181
182
183 function nhsblocks_block_renderer( string $name, array $attributes, string $content = null ) {
184
185 // change template name slash to scores
186 $template_name = str_replace( '/', '-', $name );
187
188 // Set query vars so they are accessible to the template part:
189 foreach ( $attributes as $attribute_name => $attribute_value ) {
190 set_query_var( $name . '/' . $attribute_name, $attribute_value );
191 }
192 set_query_var( $name . '/content', $content );
193 set_query_var( $name . '/class', 'wp-block-' . $template_name );
194
195 // get template file directory
196 $template_file = plugin_dir_path( __FILE__ ) . "/templates/{$template_name}.php";
197
198 // var_dump( $template_file );
199
200
201 // Load the template part in an output buffer:
202 ob_start();
203 load_template( $template_file );
204 $output = ob_get_clean();
205
206 // Fall back to just the block content if there's no template part:
207 if ( '' === $output ) {
208 $output = (string) $content;
209 }
210
211 // Clear the query vars so they don't bleed into subsequent instances of the same block type
212 foreach ( $attributes as $attribute_name => $attribute_value ) {
213 set_query_var( $name . '/' . $attribute_name, null );
214 }
215 set_query_var( $name . '/content', null );
216
217 return $output;
218 }
219
220
221 /**
222 * Build classes based on block attributes.
223 * Returns string of classes.
224 *
225 * @param array $attributes - Block attributes.
226 *
227 * @return array $classes - the finished array of classes to attach to blocks.
228 */
229 function nhsblocks_block_classes( $attributes ) {
230 $classes = null;
231 if ( $attributes['align'] ) {
232 $classes = 'align' . $attributes['align'] . ' ';
233 }
234
235 if ( $attributes['className'] ) {
236 $classes .= $attributes['className'];
237 }
238
239 return $classes;
240 }
241
242 /**
243 * Queues up the gutenberg editor style
244 */
245 function nhsblocks_gutenberg_editor_styles() {
246 $theme = wp_get_theme(); // gets the current theme
247 if ( 'Nightingale' !== $theme->name ) {
248 wp_enqueue_style( 'nhsl-block-editor-styles', plugins_url( 'style-gutenburg.css', __FILE__ ), false, '1.1', 'all' );
249 }
250 }
251
252 add_action( 'enqueue_block_editor_assets', 'nhsblocks_gutenberg_editor_styles' ); // Pulls the enqueued file in to standard wp process.
253
254 /**
255 * Queues up the blocks styling for front end
256 */
257 function nhsblocks_register_style() {
258 $theme = wp_get_theme(); // gets the current theme
259 $parent = wp_get_theme( get_template() );
260 if ( 'Nightingale' !== $theme->name && ( 'Nightingale' !== $parent->name ) ) {
261 wp_register_style( 'nhsblocks', plugins_url( 'style.min.css', __FILE__ ) );
262 }
263 }
264
265 add_action( 'init', 'nhsblocks_register_style' ); // Pulls front end styling to standard wp process.
266
267 function nhsblocks_enqueue_style() {
268 $theme = wp_get_theme(); // gets the current theme
269 if ( 'Nightingale' !== $theme->name ) {
270 wp_enqueue_style( 'nhsblocks' );
271 }
272 }
273
274 add_action( 'wp_enqueue_scripts', 'nhsblocks_enqueue_style' );
275
276
277 function nhsblocks_hero_footer() {
278 $theme = wp_get_theme(); // gets the current theme
279 $scriptout = "<script>
280 const heroBlock = document.querySelector('.wp-block-nhsblocks-heroblock');
281 const removeElements = (elms) => elms.forEach(el => el.remove());
282 const tabbedTabs = document.querySelector( '.nhsuk-bordered-tabs-container' );
283 if ( ( heroBlock ) ) {
284 matches = heroBlock.matches ? heroBlock.matches('.wp-block-nhsblocks-heroblock') : heroBlock.msMatchesSelector('.wp-block-nhsblocks-heroblock');
285 if ( matches === true ) { ";
286 if ( 'Nightingale' === $theme->name || 'Nightingale' === $theme->parent_theme ) {
287 $scriptout .= "
288 const mainContent = document.querySelector( 'main' );
289 const contentInner = document.querySelector( '#contentinner' );
290 const wholeDoc = document.querySelector( 'body' );
291 const breadCrumb = document.querySelector( '.nhsuk-breadcrumb' );
292 const articleTitle = document.querySelector( '.entry-header' );
293 const sectionTitle = wholeDoc.querySelector( '#nhsuk-tabbed-title' );
294 const tabbedTabs = document.querySelector( '.nhsuk-bordered-tabs-container' );";
295 } else {
296 $scriptout .= "
297 const mainContent = document.querySelector( 'main' );
298 const contentInner = document.querySelector( 'article' );
299 const wholeDoc = document.querySelector( 'body' );
300 const articleTitle = document.querySelector( '.entry-header' );";
301 }
302 $scriptout .= "
303 mainContent.insertBefore( heroBlock, contentInner );
304 articleTitle.style.display = 'none';
305 mainContent.style.paddingTop = '0';
306 if ( tabbedTabs ) {
307 mainContent.insertBefore( tabbedTabs, contentInner );
308 heroBlock.style.borderBottom = '70px solid white';
309 heroBlock.style.marginBottom = 'none';
310 } else {
311 heroBlock.style.marginBottom = '70px';
312 }
313 if ( breadCrumb ) {
314 wholeDoc.removeChild( breadCrumb );
315 }
316 if ( sectionTitle ) {
317 removeElements( document.querySelectorAll('#nhsuk-tabbed-title') );
318 }
319 }
320 } else if ( tabbedTabs ) {";
321 if ( 'Nightingale' === $theme->name || 'Nightingale' === $theme->parent_theme ) {
322 $scriptout .= "
323 const mainContent = document.querySelector( 'main' );
324 const contentInner = document.querySelector( '#contentinner' );
325 const wholeDoc = document.querySelector( 'body' );
326 const breadCrumb = document.querySelector( '.nhsuk-breadcrumb' );
327 const articleTitle = document.querySelector( '.entry-header' );
328 const sectionTitle = wholeDoc.querySelector( '#nhsuk-tabbed-title' );";
329
330 } else {
331 $scriptout .= "
332 const mainContent = document.querySelector( 'main' );
333 const contentInner = document.querySelector( 'article' );
334 const wholeDoc = document.querySelector( 'body' );
335 const articleTitle = document.querySelector( '.entry-header' );";
336 }
337
338 $scriptout .= "
339 mainContent.insertBefore( tabbedTabs, contentInner );
340 if ( breadCrumb ) {
341 wholeDoc.removeChild( breadCrumb );
342 }
343 if ( sectionTitle ) {
344 removeElements( document.querySelectorAll( '#nhsuk-tabbed-title' ) );
345 }
346 articleTitle.style.marginTop = '20px';
347 mainContent.style.paddingTop = '0';
348 }
349 // Page Link JS
350 const careCardWarning = document.querySelector('.nhsuk-care-card.is-style-warning-callout');
351 if ( ( careCardWarning ) ) {
352 const visuallyHidden = careCardWarning.querySelector('.nhsuk-u-visually-hidden');
353 jQuery(visuallyHidden).html('Warning advice: ');
354 }
355 const careCardUrgent = document.querySelector('.nhsuk-care-card.is-style-urgent');
356 if ( ( careCardUrgent ) ) {
357 const visuallyHidden = careCardUrgent.querySelector('.nhsuk-u-visually-hidden');
358 jQuery(visuallyHidden).html('Urgent advice: ');
359 }
360 const careCardImmediate = document.querySelector('.nhsuk-care-card.is-style-immediate');
361 if ( ( careCardImmediate ) ) {
362 const visuallyHidden = careCardImmediate.querySelector('.nhsuk-u-visually-hidden');
363 jQuery(visuallyHidden).html('Immediate action required: ');
364 }
365
366 ( function(){
367 let url = window.location.href.split(/[?#]/)[0];
368 let pageList = document.querySelectorAll('.nhsuk-contents-list li.nhsuk-contents-list__item');
369 for (var i = pageList.length - 1; i >= 0; i--) {
370 let nhsList = pageList[i];
371 let link = pageList[i].children[0].href;
372 if( link === url ){
373 let txt = pageList[i].innerText;
374 pageList[i].innerHTML = txt;
375 }
376 }
377 })();
378 // Smooth scroll to link
379 jQuery( document ).ready(function( $ ) {
380 $('.js-scroll-to').on('click', function(e) {
381 e.preventDefault();
382 let link = $(this).attr('href');
383 $('html, body').animate({
384 scrollTop: $( link ).offset().top - 50
385 }, 200);
386 });
387 });
388 </script>";
389 echo $scriptout;
390 }
391
392 add_action( 'wp_footer', 'nhsblocks_hero_footer' );
393