Skip to content

Commit 922f1b9

Browse files
authored
Merge pull request #206 from my-language-skills/Chris
Chris
2 parents 97c2e43 + 22a809b commit 922f1b9

16 files changed

+181
-798
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
all-in-one-metadata/.idea
33
all-in-one-metadata/.vscode
4+
all-in-one-metadata/vendor

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ https://github.com/pressbooks/pressbooks/issues/950
4040

4141
## Installation
4242

43-
1. Clone (or copy) this repository to the `/wp-content/plugins/` directory
43+
1. Clone (or copy) this repository folder `all-in-one-metadata` to the `/wp-content/plugins/` directory
44+
1. Navigate into `/wp-content/plugins/all-in-one-metadata` and run `composer install` and then `composer dump-autoload -o`
4445
1. Activate the plugin through the 'Plugins' screen in WordPress
4546

4647
## Frequently Asked Questions

all-in-one-metadata/admin/adminFunctions/class-pressbooks-metadata-options.php

+10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function add_metaboxes() {
8686
add_meta_box('metadata-location', 'Location Of Metadata', array($this, 'render_metabox_schema_locations'), $this->pagehook, 'normal', 'core');
8787
add_meta_box('activated-schema-locations', 'Activated Locations For Schema Types', array($this, 'render_metabox_active_schemas'), $this->pagehook, 'normal', 'core');
8888
add_meta_box('specific-metadata', 'Specific Metadata', array($this, 'render_metabox_specific_metadata'), $this->pagehook, 'normal', 'core');
89+
add_meta_box('general-settings', 'General Settings', array($this, 'render_general_settings'), $this->pagehook, 'normal', 'core');
8990
}
9091

9192
/**
@@ -115,6 +116,15 @@ function render_metabox_active_schemas(){
115116
include_once plugin_dir_path( dirname( __FILE__ ) ) . 'partials/pressbooks-metadata-admin-settings-activeSchemas.php';
116117
}
117118

119+
/**
120+
* Render data for the general_settings metabox.
121+
*
122+
* @since 0.x
123+
*/
124+
function render_general_settings(){
125+
include_once plugin_dir_path( dirname( __FILE__ ) ) . 'partials/pressbooks-metadata-admin-settings-general.php';
126+
}
127+
118128
/**
119129
* A function that manipulates the inputs for saving the new cpt data
120130
* @since 0.9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* The file containing the html for the genral settings Metabox in the settings.
4+
*
5+
* @link https://github.com/Books4Languages/pressbooks-metadata
6+
* @since 0.x
7+
*
8+
* @package Pressbooks_Metadata
9+
* @subpackage Pressbooks_Metadata/admin/partials
10+
* @author Christos Amyrotos <[email protected]>
11+
*/
12+
?>
13+
14+
<form method="post" action="options.php">
15+
<?php
16+
settings_fields( 'general_settings_page' );
17+
do_settings_sections( 'general_settings_page' );
18+
submit_button();
19+
echo '<br><br>';
20+
?>
21+
</form>

all-in-one-metadata/admin/schemaFunctions/class-pressbooks-metadata-engine.php

+8
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ public static function get_all_post_types(){
103103
*/
104104
public function register_settings() {
105105

106+
//General settings
107+
$generalSettSection = "generalSettingsSection";
108+
$generalSettPage = "general_settings_page";
109+
110+
//Registering the general settings for the general settings metabox
111+
add_settings_section($generalSettSection, "Metadata Output Type", null, $generalSettPage);
112+
new post_type_fields('jsonld_output','Enable Jsonld',$generalSettPage,$generalSettSection);
113+
106114
//Post Level
107115
$postLevelSection = "postLevelSection";
108116
$postLevelPage = "post_level_tab";

all-in-one-metadata/admin/schemaTypes/class-pressbooks-metadata-type.php

+61-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace schemaTypes;
44
use schemaFunctions\Pressbooks_Metadata_General_Functions as gen_func;
55
use schemaFunctions\Pressbooks_Metadata_Create_Metabox as create_metabox;
6+
use Spatie\SchemaOrg\Schema as jsonldGen;
67

78
/**
89
* The class for the Type including operations, this class is used as a base class for all the types
@@ -184,18 +185,33 @@ public function pmdt_add_metabox($meta_position) {
184185
}
185186

186187
/**
187-
* A function that creates the metadata for the book type.
188+
* This is a routing function used to check if the administrator wants the metadata to be in jsonld or microdata format
189+
* @since 0.x
190+
*
191+
*/
192+
public function pmdt_get_metatags(){
193+
//Here we need to check for a wordpress option
194+
if(!get_option('jsonld_output')){
195+
return $this->get_microdata();
196+
}else{
197+
return $this->get_jsonld();
198+
}
199+
}
200+
201+
/**
202+
* A function that creates the metadata for the types using microdata.
188203
* @since 0.8.1
189204
*
190205
*/
191-
public function pmdt_get_metatags() {
206+
private function get_microdata() {
192207
//Creating microtags
193208
$html = "<!-- Microtags --> \n";
194209

195210
$html .= '<div itemscope itemtype="'.$this->typeUrl.'">';
196211

197212
foreach ( $this->type_fields as $itemprop => $details ) {
198213
$propName = strtolower('pb_' . $itemprop . '_'.$this->typeName.'_' . $this->type_level);
214+
$clearTypeName = str_replace('http://schema.org/','',$this->typeUrl);
199215
if ($this->pmdt_prop_run($itemprop)) {
200216
$value = $this->pmdt_get_value($propName);
201217
if(!empty($value)){$html .= "<meta itemprop = '" . $itemprop . "' content = '" . $value . "'>\n";}
@@ -204,4 +220,47 @@ public function pmdt_get_metatags() {
204220
$html .= '</div>';
205221
return $html;
206222
}
223+
224+
/**
225+
* A function that creates the metadata for the types using jsonld.
226+
* @since 0.x
227+
*
228+
*/
229+
private function get_jsonld(){
230+
231+
//Getting the clear name of the type so we can load a class (type object) from the spatie/schema
232+
$clearTypeName = str_replace('http://schema.org/','',$this->typeUrl);
233+
234+
//Changing the first letter of the type into lower case so we can match the function name in the library (starting point Schema.php)
235+
$clearTypeName = lcfirst($clearTypeName);
236+
237+
//Calling the Schema.php class from the library invoking its function that returns the type
238+
//Note that the functions in Schema.php are static and the name of the function ($clearTypeName) returns the type of the name
239+
//For example $schema = jsonldGen::$book(); will return a book object
240+
$schema = new jsonldGen;
241+
242+
//Checking if the type exists in the library, in case we have a naming error comming from our type files we end the opperation
243+
if(!method_exists($schema,$clearTypeName)){
244+
return;
245+
}
246+
247+
//Creating a schema type from the library
248+
$schema = jsonldGen::$clearTypeName();
249+
250+
251+
//Where ever we find a property that has a value we add it into the object created above ($schema)
252+
foreach ( $this->type_fields as $itemprop => $details ) {
253+
$propName = strtolower('pb_' . $itemprop . '_'.$this->typeName.'_' . $this->type_level);
254+
if ($this->pmdt_prop_run($itemprop)) {
255+
$value = $this->pmdt_get_value($propName);
256+
if(!empty($value)){
257+
//Note that schema is the object created above and $itemprop is used to call a function from the type stored in the $schema variable
258+
//Assuming like above that the $schema is holding a book object doing this $schema->illustrator('a_name') sets the illustrator property of the type to 'a_name'
259+
$schema->$itemprop($value);
260+
}
261+
}
262+
}
263+
//This uses the $schema object and all the properties we gave it above (illustrator for example) to return jasonld data
264+
return $schema->toScript();
265+
}
207266
}

all-in-one-metadata/composer.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
"schemaTypes\\": "admin/schemaTypes",
77
"requiredPlugins\\": "admin/requiredPlugins",
88
"schemaTypes\\cw\\": "admin/schemaTypes/creativeWorks",
9-
"schemaTypes\\organization\\": "admin/schemaTypesorganization",
9+
"schemaTypes\\organization\\": "admin/schemaTypes/organization",
1010
"adminFunctions\\": "admin/adminFunctions",
1111
"networkFunctions\\": "admin/networkFunctions",
12-
"vocabularyFunctions\\": "admin/vocabularyFunctions"
12+
"vocabularyFunctions\\": "admin/vocabularyFunctions",
13+
"Spatie\\SchemaOrg\\": "vendor/spatie/schema-org/src"
1314
}
15+
},
16+
"require": {
17+
"spatie/schema-org": "^1.4"
1418
}
15-
}
19+
}
20+

all-in-one-metadata/composer.lock

+70
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

all-in-one-metadata/vendor/autoload.php

-7
This file was deleted.

0 commit comments

Comments
 (0)