Skip to content

Commit

Permalink
Add global header/footer block architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Aug 26, 2021
1 parent 025c2aa commit a17be8a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

Over time, this is intended to become the canonical source repository all `mu-plugins` on the WordPress.org network. At the moment, it only includes a few.

## Usage

1. Add to a project's `composer.json`, example
1. run `composer update` to download it
1. do in env/0-sandbox.php . on production, it's already defined
```php
define( 'WPORG_GIT_MUPLUGINS_DIR', dirname( ABSPATH ) . '/vendor/wporg/wporg-mu-plugins' );
```
1. `require_once` the files that you want. e.g., have a themes's `functions.php`
```php
require_once WPORG_GIT_MUPLUGINS_DIR . '/mu-plugins/blocks/global-header-footer/blocks.php';
```

## Sync/Deploy

Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"name": "wordpress/wporg-mu-plugins",
"description": "`mu-plugins` for the WordPress.org network",
"license": "GPL-2.0-or-later",
"require": {},
"name": "wporg/wporg-mu-plugins",
"description": "`mu-plugins` for the WordPress.org network",
"license": "GPL-2.0-or-later",
"extra" : {
"sync-svn" : {
"main-branch" : "trunk",
Expand Down
32 changes: 32 additions & 0 deletions mu-plugins/blocks/global-header-footer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Global Header

## Setup




## Register as a block (for Full Site Editing themes)

Add as a composer dependency, install, then add this to a theme's `functions.php`:

```php
require_once WPORG_GIT_MUPLUGINS_DIR . '/mu-plugins/blocks/global-header-footer/blocks.php';
```


## Include directly in PHP (for classic themes)

Add as a composer dependency, install, then

```php
require_once WPORG_GIT_MUPLUGINS_DIR . '/mu-plugins/blocks/global-header-footer/universal-header.php';
```

todo path should be "blocks", or more generic like "components", "template-parts", ?


## Embed as an iframe (for Trac, Codex, etc)

<iframe ...>
src=http...?embed_context=codex
63 changes: 63 additions & 0 deletions mu-plugins/blocks/global-header-footer/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace WordPress_org\MU_Plugins\Global_Header_Footer;

defined( 'WPINC' ) || die();

add_action( 'init', __NAMESPACE__ . '\register_assets', 9 );
// why 9? if can be 10, then callers could be 9


function register_assets() {
// don't want this visible in Inserter. need to create ticket for that?
register_block_type(
'wordpress-org/global-header',
array( 'render_callback' => __NAMESPACE__ . '\render_global_header' )
);

register_block_type(
'wordpress-org/global-footer',
array( 'render_callback' => __NAMESPACE__ . '\render_global_footer' )
);
}

/**
* Render the global header in a block context.
*
* @param array $attributes Block attributes.
*
* @return string
*/
function render_global_header( $attributes ) {
/*
todo
meta tags included called automaticaly in FSE themes
so the header needs to avoid adding them for FSE, or we need to disable FSE automatically adding them
*/

ob_start();
require_once __DIR__ . '/universal-header.php';
// cant include inside namespace b/c that messes things up?
// if so, is there a way to de-scope it?
return ob_get_clean();
}

/**
* Render the global footer in a block context.
*
* @param array $attributes Block attributes.
*
* @return string
*/
function render_global_footer( $attributes ) {
ob_start();
require_once __DIR__ . '/universal-footer.php';
return ob_get_clean();
}


// maybe make an api endpoint to serve this to codex/trac?
// all universal logic should go inside header.php, so that Trac, the Codex, etc can load it
5 changes: 5 additions & 0 deletions mu-plugins/blocks/global-header-footer/universal-footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
echo 'this is the global footer :)';
return;


4 changes: 4 additions & 0 deletions mu-plugins/blocks/global-header-footer/universal-header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
echo 'this is the global header :)';
return;

0 comments on commit a17be8a

Please sign in to comment.