PluginProbe
NHS Blocks / 1.3.9
NHS Blocks v1.3.9
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.3.9, at nhsblocks.php

413 lines 13.8 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: 6.0
10 *
11 * Version: 1.3.9
12 * Stable tag: 1.3.9
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 = array(
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 * Undocumented function
182 *
183 * @param string $name name.
184 * @param array $attributes attributes.
185 * @param string $content content.
186 * @return string
187 */
188 function nhsblocks_block_renderer( string $name, array $attributes, string $content = null ) {
189
190 // change template name slash to scores.
191 $template_name = str_replace( '/', '-', $name );
192
193 // Set query vars so they are accessible to the template part.
194 foreach ( $attributes as $attribute_name => $attribute_value ) {
195 set_query_var( $name . '/' . $attribute_name, $attribute_value );
196 }
197 set_query_var( $name . '/content', $content );
198 set_query_var( $name . '/class', 'wp-block-' . $template_name );
199
200 // get template file directory
201 $template_file = plugin_dir_path( __FILE__ ) . "/templates/{$template_name}.php";
202
203 // Load the template part in an output buffer:.
204 ob_start();
205 load_template( $template_file );
206 $output = ob_get_clean();
207
208 // Fall back to just the block content if there's no template part:.
209 if ( '' === $output ) {
210 $output = (string) $content;
211 }
212
213 // Clear the query vars so they don't bleed into subsequent instances of the same block type.
214 foreach ( $attributes as $attribute_name => $attribute_value ) {
215 set_query_var( $name . '/' . $attribute_name, null );
216 }
217 set_query_var( $name . '/content', null );
218
219 return $output;
220 }
221
222
223 /**
224 * Build classes based on block attributes.
225 * Returns string of classes.
226 *
227 * @param array $attributes - Block attributes.
228 *
229 * @return array $classes - the finished array of classes to attach to blocks.
230 */
231 function nhsblocks_block_classes( $attributes ) {
232 $classes = null;
233 if ( $attributes['align'] ) {
234 $classes = 'align' . $attributes['align'] . ' ';
235 }
236
237 if ( $attributes['className'] ) {
238 $classes .= $attributes['className'];
239 }
240
241 return $classes;
242 }
243
244 /**
245 * Queues up the gutenberg editor style
246 */
247 function nhsblocks_gutenberg_editor_styles()
248 {
249 wp_enqueue_style('nhsl-block-editor-styles', plugins_url('style-gutenburg.css', __FILE__), false, '1.1', 'all');
250 $plugin_version = get_bloginfo('version'); // WP version by default.
251 $plugin_data = get_plugin_data(plugin_dir_path(__FILE__) . 'nhsblocks.php');
252 if (isset($plugin_data['Name']) && !empty($plugin_data['Version']) && 'NHS Blocks' === $plugin_data['Name']) {
253 $plugin_version = $plugin_data['Version'];
254 }
255 wp_enqueue_style('nhsblocks-editor-styles', plugins_url('style.min.css', __FILE__), false, $plugin_version, 'all');
256 }
257
258 add_action( 'enqueue_block_editor_assets', 'nhsblocks_gutenberg_editor_styles' ); // Pulls the enqueued file in to standard wp process.
259
260 /**
261 * Queues up the blocks styling for front end
262 */
263 function nhsblocks_register_style() {
264 $theme = wp_get_theme(); // gets the current theme.
265 $parent = wp_get_theme( get_template() );
266 $plugin_version = get_bloginfo( 'version' ); // WP version by default.
267 if ( 'Nightingale' !== $theme->name && ( 'Nightingale' !== $parent->name ) ) {
268 $plugin_data = get_plugin_data( plugin_dir_path( __FILE__ ) . 'nhsblocks.php' );
269 if ( isset( $plugin_data['Name'] ) && ! empty( $plugin_data['Version'] ) && 'NHS Blocks' === $plugin_data['Name'] ) {
270 $plugin_version = $plugin_data['Version'];
271 }
272 wp_register_style( 'nhsblocks', plugins_url( 'style.min.css', __FILE__ ), array(), $plugin_version, 'all' );
273 }
274 }
275
276 add_action( 'init', 'nhsblocks_register_style' ); // Pulls front end styling to standard wp process.
277
278 /**
279 * Nhsblocks enqueue style.
280 *
281 * @return void
282 */
283 function nhsblocks_enqueue_style() {
284 $theme = wp_get_theme(); // gets the current theme.
285 if ( 'Nightingale' !== $theme->name ) {
286 wp_enqueue_style( 'nhsblocks' );
287 }
288 }
289
290 add_action( 'wp_enqueue_scripts', 'nhsblocks_enqueue_style' );
291
292 /**
293 * Hero footer
294 *
295 * @return void
296 */
297 function nhsblocks_hero_footer() {
298 $theme = wp_get_theme(); // gets the current theme.
299 $scriptout = "<script>
300
301 const heroBlock = document.querySelector('.wp-block-nhsblocks-heroblock');
302 const tabbedTabs = document.querySelector( '.nhsuk-bordered-tabs-container' );
303 if ( ( heroBlock ) ) {
304 matches = heroBlock.matches ? heroBlock.matches('.wp-block-nhsblocks-heroblock') : heroBlock.msMatchesSelector('.wp-block-nhsblocks-heroblock');
305 if ( matches === true ) { ";
306 if ( 'Nightingale' === $theme->name || 'Nightingale' === $theme->parent_theme ) {
307 $scriptout .= "
308 const mainContent = document.querySelector( 'main' );
309 const contentInner = document.querySelector( '#contentinner' );
310 const wholeDoc = document.querySelector( 'body' );
311 const breadCrumb = document.querySelector( '.nhsuk-breadcrumb' );
312 const articleTitle = document.querySelector( '.entry-header' );
313 const sectionTitle = wholeDoc.querySelector( '#nhsuk-tabbed-title' );
314 const tabbedTabs = document.querySelector( '.nhsuk-bordered-tabs-container' );";
315 } else {
316 $scriptout .= "
317 const mainContent = document.querySelector( 'main' );
318 const contentInner = document.querySelector( 'article' );
319 const wholeDoc = document.querySelector( 'body' );
320 const articleTitle = document.querySelector( '.entry-header' );";
321 }
322 $scriptout .= "
323 mainContent.insertBefore( heroBlock, contentInner );
324 articleTitle.style.display = 'none';
325 mainContent.style.paddingTop = '0';
326 if ( tabbedTabs ) {
327 mainContent.insertBefore( tabbedTabs, contentInner );
328 heroBlock.style.borderBottom = '70px solid white';
329 heroBlock.style.marginBottom = 'none';
330 } else {
331 heroBlock.style.marginBottom = '70px';
332 }
333 if ( breadCrumb ) {
334 wholeDoc.removeChild( breadCrumb );
335 }
336 if ( sectionTitle ) {
337 removeElements( document.querySelectorAll('#nhsuk-tabbed-title') );
338 }
339 }
340 } else if ( tabbedTabs ) {";
341 if ( 'Nightingale' === $theme->name || 'Nightingale' === $theme->parent_theme ) {
342 $scriptout .= "
343 const mainContent = document.querySelector( 'main' );
344 const contentInner = document.querySelector( '#contentinner' );
345 const wholeDoc = document.querySelector( 'body' );
346 const breadCrumb = document.querySelector( '.nhsuk-breadcrumb' );
347 const articleTitle = document.querySelector( '.entry-header' );
348 const sectionTitle = wholeDoc.querySelector( '#nhsuk-tabbed-title' );";
349
350 } else {
351 $scriptout .= "
352 const mainContent = document.querySelector( 'main' );
353 const contentInner = document.querySelector( 'article' );
354 const wholeDoc = document.querySelector( 'body' );
355 const articleTitle = document.querySelector( '.entry-header' );";
356 }
357
358 $scriptout .= "
359 mainContent.insertBefore( tabbedTabs, contentInner );
360 if ( breadCrumb ) {
361 wholeDoc.removeChild( breadCrumb );
362 }
363 if ( sectionTitle ) {
364 removeElements( document.querySelectorAll( '#nhsuk-tabbed-title' ) );
365 }
366 articleTitle.style.marginTop = '20px';
367 mainContent.style.paddingTop = '0';
368 }
369 // Page Link JS
370 const careCardWarning = document.querySelector('.nhsuk-care-card.is-style-warning-callout');
371 if ( ( careCardWarning ) ) {
372 const visuallyHidden = careCardWarning.querySelector('.nhsuk-u-visually-hidden');
373 jQuery(visuallyHidden).html('Warning advice: ');
374 }
375 const careCardUrgent = document.querySelector('.nhsuk-care-card.is-style-urgent');
376 if ( ( careCardUrgent ) ) {
377 const visuallyHidden = careCardUrgent.querySelector('.nhsuk-u-visually-hidden');
378 jQuery(visuallyHidden).html('Urgent advice: ');
379 }
380 const careCardImmediate = document.querySelector('.nhsuk-care-card.is-style-immediate');
381 if ( ( careCardImmediate ) ) {
382 const visuallyHidden = careCardImmediate.querySelector('.nhsuk-u-visually-hidden');
383 jQuery(visuallyHidden).html('Immediate action required: ');
384 }
385
386 ( function(){
387 let url = window.location.href.split(/[?#]/)[0];
388 let pageList = document.querySelectorAll('.nhsuk-contents-list li.nhsuk-contents-list__item');
389 for (var i = pageList.length - 1; i >= 0; i--) {
390 let nhsList = pageList[i];
391 let link = pageList[i].children[0].href;
392 if( link === url ){
393 let txt = pageList[i].innerText;
394 pageList[i].innerHTML = txt;
395 }
396 }
397 })();
398 // Smooth scroll to link
399 jQuery( document ).ready(function( $ ) {
400 $('.js-scroll-to').on('click', function(e) {
401 e.preventDefault();
402 let link = $(this).attr('href');
403 $('html, body').animate({
404 scrollTop: $( link ).offset().top - 50
405 }, 200);
406 });
407 });
408 </script>";
409 echo $scriptout;
410 }
411
412 add_action( 'wp_footer', 'nhsblocks_hero_footer' );
413