Skip to content

Commit bba22b5

Browse files
committed
Add Schema provider. Add documentation.
1 parent c984286 commit bba22b5

File tree

6 files changed

+893
-0
lines changed

6 files changed

+893
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A collection of extensions for your Roots\Sage 9.x beta themes.
88
- **Blade Directives:** @directives for loop, query, sidebar, FontAwesome, and more.
99
- **Menu Provider:** Register nav menu and markup via configuration file.
1010
- **Sidebar Provider:** Register sidebar and widget markup via configuration file.
11+
- **Schema Provider** Quickly add schema.org markup via @schema directive.
1112

1213
## Requirements
1314
This package is specifically built for `Roots\Sage 9.0.0-beta.4` and above. It goes without saying that your development server needs to have `Php 7.0` or greater as well.
@@ -46,6 +47,9 @@ add_action('after_setup_theme', function (){
4647

4748
});
4849
```
50+
51+
**Important** Since this package is introducing new Blade directives, you should clear your cached/compiled Blade files. Those files are located in `wp-content\uploads\cache`.
52+
4953
## Overview
5054
Outside of one line of code that you need to add to `setup.php` and the config files, there is nothing else you need to do. Config files are automatically registered with the Sage Container, no messing with `functions.php`.
5155

@@ -188,12 +192,32 @@ Alternatively, you can still use WordPress functions to display a sidebar.
188192
```blade
189193
@php( \dynamic_sidebar('sidebar-primary') )
190194
```
195+
196+
### Schema Provider
197+
As with the Blade directives, there are numerous schema.org attributes and has its own documentaion. There are also two filters for each attribute, which can be used to extend them further. See [Schema Provider Documentation](https://github.com/webstractions/sage-xpress/tree/master/docs/schema.md) for complete details.
198+
199+
Using the `@schema` directive makes markup simple.
200+
201+
```blade
202+
{{-- Author --}}
203+
<span @schema( 'entry-author' )>
204+
@php( the_author_posts_link() )
205+
</span>
206+
```
207+
Will produce the following Php.
208+
```php
209+
<span class="entry-author" itemprop="author" itemscope="itemscope" itemtype="http://schema.org/Person">
210+
<?php ( the_author_posts_link() ); ?>
211+
</span>
212+
```
213+
191214
## Documentation
192215
- [Blade Directives](https://github.com/webstractions/sage-xpress/tree/master/docs/blade.md)
193216

194217
## Acknowledgements
195218
- Roots/Sage Discourse thread [Resources for Blade](https://discourse.roots.io/t/best-practice-resources-for-blade/8341) and [Log1x's](https://discourse.roots.io/u/Log1x) contributions for inspiring the Blade component.
196219
- Appstract's [Laravel Blade Directives](https://github.com/appstract/laravel-blade-directives) for configuration and service provider logic.
220+
- Justin Tadlock's `hybrid_attr()` functions from older versions of [Hybrid Core](https://github.com/justintadlock/hybrid-core).
197221

198222

199223
## License

docs/schema.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Schema Usage
2+
3+
Based on Justin Tadlock's `hybrid_attr()` functions from older versions of [Hybrid Core](https://github.com/justintadlock/hybrid-core). Justin has since removed the schema.org attributes from the functions and they are resurected here.
4+
5+
This provider is still a work in progress. It is functional, but has quirks. For instance, I am having problems using the `@schema()` directive using `@include` in an already included partial for some reason.
6+
7+
Another quirk is the original `hybrid_attr()` function allowed for a third `$args` parameter array. You are able to pass stuff like `['class'=>'col-med-4 col-sm-12']` and have that rolled into the attributes. Doing something like this using a single Blade string called `$expression` is tough. At any rate, the same thing can be handled via filters.
8+
9+
### Attributes
10+
For the most part, adding schema.org attributes is pretty straight forward. You just add `@schema('slug')` to your Blade files. The slug names can be found in the filters section below.
11+
12+
Some slugs need a little more explanation.
13+
14+
`@schema('body')` for the `<body>` tag automatically joins the `get_body_class()` into the list of classes. It also detects what type of page/post is being displayed and will act accordingly and set `itemtype` to WebPage, Blog, or SearchResultsPage.
15+
16+
Three of the schemas require a second parameter:
17+
- `@schema('sidebar,context')` Where context is the Id of the sidebar
18+
- `@schema('menu,context')` Where context is the Id of the menu
19+
- `@schema('entry-terms,context')` Where context is optional. It can either be `category` or `post_tag` and will add an `itemprop` of 'articleSection' or 'keywords' respectively. If omitted, no itemprop will be defined.
20+
21+
Three of the schemas reveal the fact that the blog is WordPress. If you have any plugins or custom code to hide this fact, you may wish to avoid using them. They are `@schema('header')`, `@schema('footer')`, and `@schema('sidebar')` which adds an `itemstype` of WPHeader, WPFooter, and WPSidebar respectively. Of course, you can add your own filters to change `itemtype` attributes.
22+
23+
### Filters
24+
All filters have a priority of 5 and one argument, except for `sage_schema_sidebar`, `sage_schema_menu`, and `sage_schema_entry-terms`. Those exceptions require a second `$context` argument. For instance `@schema('sidebar, primary')`.
25+
26+
Each filter follows a pattern `sage_schema_{$slug}`. Slugs are hyphenated.
27+
28+
Additionally, there is a second set of matching filters for customizing `class` attributes. They follow a similar pattern `sage_schema_{$slug}_class`
29+
30+
**Schema for major structural elements.**
31+
- `sage_schema_body`
32+
- `sage_schema_header`
33+
- `sage_schema_footer`
34+
- `sage_schema_content`
35+
- `sage_schema_sidebar` (2)
36+
- `sage_schema_menu` (2)
37+
38+
**Header schema.**
39+
- `sage_schema_head`
40+
- `sage_schema_branding`
41+
- `sage_schema_site-title`
42+
- `sage_schema_site-description`
43+
44+
**Archive page header schema.**
45+
- `sage_schema_archive-header`
46+
- `sage_schema_archive-title`
47+
- `sage_schema_archive-description`
48+
49+
**Post-specific schema.**
50+
- `sage_schema_post`
51+
- `sage_schema_entry` (Alias for "post")
52+
- `sage_schema_entry-title`
53+
- `sage_schema_entry-author`
54+
- `sage_schema_entry-published`
55+
- `sage_schema_entry-content`
56+
- `sage_schema_entry-summary`
57+
- `sage_schema_entry-terms` (2)
58+
59+
**Comment specific schema.**
60+
- `sage_schema_comment`
61+
- `sage_schema_comment-author`
62+
- `sage_schema_comment-published`
63+
- `sage_schema_comment-permalink`
64+
- `sage_schema_comment-content`

src/Blade/directives.php

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@
4848
return "<?php echo do_action($expression); ?>";
4949
},
5050

51+
/*
52+
|---------------------------------------------------------------------
53+
| @schema
54+
|---------------------------------------------------------------------
55+
*/
56+
'schema' => function($expression) {
57+
// $expression should be in form of [$slug, $context='', $attr=[]]
58+
$expression = explode( ",", $expression );
59+
$expression = array_replace( ['','',[] ] , $expression);
60+
$html = \App\sage()->make('HtmlSchema')->attr($expression[0],$expression[1],$expression[2]);
61+
return $html;
62+
},
5163
/*
5264
|---------------------------------------------------------------------
5365
| @istrue / @isfalse

src/SageXpress.php

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function bootstrap() {
2121
$this->registerBladeDirectives();
2222
$this->registerMenuProvider();
2323
$this->registerSidebarProvider();
24+
$this->registerSchemaProvider();
2425
$this->registerShortcodesProvider();
2526
$this->registerLayoutProvider();
2627
}
@@ -43,6 +44,12 @@ protected function registerSidebarProvider() {
4344
new SidebarProvider($this->sage);
4445
}
4546

47+
protected function registerSchemaProvider() {
48+
49+
$this->sage->bind('HtmlSchema', \SageXpress\Schema\SchemaFunctions::class);
50+
new \SageXpress\Schema\SchemaProvider($this->sage);
51+
}
52+
4653
protected function registerShortcodesProvider() {
4754

4855
}

0 commit comments

Comments
 (0)