3
3
namespace schemaTypes ;
4
4
use schemaFunctions \Pressbooks_Metadata_General_Functions as gen_func ;
5
5
use schemaFunctions \Pressbooks_Metadata_Create_Metabox as create_metabox ;
6
+ use Spatie \SchemaOrg \Schema as jsonldGen ;
6
7
7
8
/**
8
9
* 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) {
184
185
}
185
186
186
187
/**
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.
188
203
* @since 0.8.1
189
204
*
190
205
*/
191
- public function pmdt_get_metatags () {
206
+ private function get_microdata () {
192
207
//Creating microtags
193
208
$ html = "<!-- Microtags --> \n" ;
194
209
195
210
$ html .= '<div itemscope itemtype=" ' .$ this ->typeUrl .'"> ' ;
196
211
197
212
foreach ( $ this ->type_fields as $ itemprop => $ details ) {
198
213
$ propName = strtolower ('pb_ ' . $ itemprop . '_ ' .$ this ->typeName .'_ ' . $ this ->type_level );
214
+ $ clearTypeName = str_replace ('http://schema.org/ ' ,'' ,$ this ->typeUrl );
199
215
if ($ this ->pmdt_prop_run ($ itemprop )) {
200
216
$ value = $ this ->pmdt_get_value ($ propName );
201
217
if (!empty ($ value )){$ html .= "<meta itemprop = ' " . $ itemprop . "' content = ' " . $ value . "'> \n" ;}
@@ -204,4 +220,47 @@ public function pmdt_get_metatags() {
204
220
$ html .= '</div> ' ;
205
221
return $ html ;
206
222
}
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
+ }
207
266
}
0 commit comments