-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkg.php
723 lines (610 loc) · 22.1 KB
/
kg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
<?php
/*
Plugin Name: Knowledge Graph
Plugin URI: https://github.com/DavidEngland/Knowledge-Graph
Description: Adds knowlege graph json-ld to WP.
Author: <a href="http://about.me/DavidEngland">Dave England</a>
Author URI: http://about.me/DavidEngland
Version: 0.0.1
License: GPL v2
Usage: Visit the plugin\'s settings page to configure your options.
Tags: knowledge, graph, head, wp_head, jsonld, ldjson, schema, microdata
*/
//Define some standard constants.
if ( !defined( 'MYPLUGIN_THEME_DIR' ) )
define( 'MYPLUGIN_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template() );
if ( !defined( 'MYPLUGIN_PLUGIN_NAME' ) )
define( 'MYPLUGIN_PLUGIN_NAME', trim( dirname( plugin_basename( __FILE__ ) ), '/' ) );
if ( !defined( 'MYPLUGIN_PLUGIN_DIR' ) )
define( 'MYPLUGIN_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . MYPLUGIN_PLUGIN_NAME );
if ( !defined( 'MYPLUGIN_PLUGIN_URL' ) )
define( 'MYPLUGIN_PLUGIN_URL', WP_PLUGIN_URL . '/' . MYPLUGIN_PLUGIN_NAME );
if ( !defined( 'MYPLUGIN_VERSION_KEY' ) )
define( 'MYPLUGIN_VERSION_KEY', 'myplugin_version' );
if ( !defined( 'MYPLUGIN_VERSION_NUM' ) )
define( 'MYPLUGIN_VERSION_NUM', '0.0.1' );
add_option( MYPLUGIN_VERSION_KEY, MYPLUGIN_VERSION_NUM );
//Add settings link on plugin page
add_filter( 'plugin_action_links', 'myplugin_plugin_action_links', 10, 2 );
function myplugin_plugin_action_links( $links, $file ) {
static $this_plugin;
if ( !$this_plugin ) {
$this_plugin = plugin_basename( __FILE__ );
} //!$this_plugin
if ( $file == $this_plugin ) {
$settings_link = '<a href="' . get_bloginfo( 'wpurl' ) . '/wp-admin/options-general.php?page=options-general.php_knowledge_graph">Settings</a>';
array_unshift( $links, $settings_link );
} //$file == $this_plugin
return $links;
}
$contact_types = array(
"sales",
"customer support",
"reservations",
"credit card support",
"emergency",
"customer service",
"technical support",
"billing support",
"bill payment",
"baggage tracking",
"roadside assistance",
"package tracking"
);
foreach ( $contact_types as $contact_type ) {
$contact_slug[] = str_replace( ' ', '-', $contact_type );
$type_contact[ $contact_type ] = ucwords( $contact_type );
} //$contact_types as $contact_type
//Read in Country names indexed by two letter codes (ISO-3166), from http://country.io
$country_names = json_decode( file_get_contents( MYPLUGIN_PLUGIN_DIR . "/lib/names.json" ), true );
//Read in Country two letter to three letter mappings
$country_2to3 = json_decode( file_get_contents( MYPLUGIN_PLUGIN_DIR . "/lib/iso3.json" ), true );
//Same for days of the week
$days = json_decode( file_get_contents( MYPLUGIN_PLUGIN_DIR . "/lib/days.json" ), true );
//JSON Pretty Print, because some old version of PHP don't have.
require_once( MYPLUGIN_PLUGIN_DIR . "/lib/json_format.php" );
//include the main class file
require_once( MYPLUGIN_PLUGIN_DIR . "/admin-page-class/admin-page-class.php" );
function my_array_merge( $arry, $key, $value ) {
if ( !empty( $value ) ) {
return array_merge( $arry, array(
$key => $value
) );
} //!empty( $value )
return $arry;
}
/**
* admin page configuration
*/
$config = array(
'menu' => 'settings', //sub page to settings page
'page_title' => __( 'Knowledge Graph', 'apc' ), //The name of this page
'capability' => 'edit_themes', // The capability needed to view the page
'option_group' => 'kg_options', //the name of the option to create in the database
'id' => 'admin_page', // meta box id, unique per page
'fields' => array( ), // list of fields (can be added by field arrays)
'local_images' => false, // Use local or hosted images (meta box images for add/remove)
'use_with_theme' => false //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
);
/**
* instantiate your admin page
*/
$options_panel = new BF_Admin_Page_Class( $config );
$options_panel->OpenTabs_container( '' );
/**
* define your admin page tabs listing
*/
$options_panel->TabsListing( array(
'links' => array(
'options_1' => __( 'Basic Information', 'apc' ),
'options_2' => __( 'Branding', 'apc' ),
'options_3' => __( 'Links', 'apc' ),
'options_4' => __( 'Contact', 'apc' ),
'options_4a' => __( 'Hours', 'apc' ),
'options_5' => __( 'Search', 'apc' ),
'options_6' => __( 'Install', 'apc' ),
'options_7' => __( 'Import Export', 'apc' )
)
) );
/**
* Open admin page first tab
*/
$options_panel->OpenTab( 'options_1' );
/**
* Add fields to admin page first tab
*
*/
//title
$options_panel->Title( __( "Basic Information", "apc" ) );
//An optionl descrption paragraph
$options_panel->addParagraph( __( "Company Name and address.", "apc" ) );
$options_panel->addSelect( '@context', array(
"http://schema.org" => "MicroData"
), array(
'desc' => __( "Only one type supported currently", "apc" )
) );
$options_panel->addSelect( '@type', array(
"Organization" => "Organization",
"LocalBusiness" => "Local Business",
"Airline" => "Airline",
"EducationalOrganization" => "Educational Organization",
"GovernmentOrganization" => "Government Organization",
"NGO" => "Non-government Organization",
"PerformingGroup" => "Performing Group",
"AnimalShelter" => "Animal Shelter",
"AutomotiveBusiness" => "Automotive Business",
"ChildCare" => "Child Care",
"DryCleaningOrLaundry" => "Dry Cleaning or Laundry",
"EmergencyService" => "Emergency Service",
"EmploymentAgency" => "Employment Agency",
"EntertainmentBusiness" => "Entertainment Business",
"FinancialService" => "Financial Service",
"FoodEstablishment" => "Food Establishment",
"GovernmentOffice" => "Government Office",
"HealthAndBeautyBusiness" => "Health and Beauty Business",
"HomeAndConstructionBusiness" => "Home and Construction Business",
"InternetCafe" => "Internet Cafe",
"Library" => "Library",
"LodgingBusiness" => "Lodging Business",
"MedicalOrganization" => "Medical Organization",
"ProfessionalService" => "Professional Service",
"RadioStation" => "Radio Station",
"RealEstateAgent" => "Real Estate Agent",
"RecyclingCenter" => "Recycling Center",
"SelfStorage" => "Self Storage",
"ShoppingCenter" => "Shopping Center",
"SportsActivityLocation" => "Sports Activity Location",
"Store" => "Store",
"TelevisionStation" => "Television Station",
"TouristInformationCenter" => "Tourist Information Center",
"TravelAgency" => "Travel Agency"
), array(
'desc' => __( "Choose the best category @type", "apc" ),
'std' => __( "Organization", "apc" )
) );
$options_panel->addText( 'name', array(
'name' => __( 'Company Name', 'apc' ),
'std' => get_bloginfo( 'name' ),
'desc' => __( 'Legal Company name', 'apc' )
) );
//email with validation
$options_panel->addText( 'email', array(
'name' => __( ' Main Email ', 'apc' ),
'std' => get_bloginfo( 'admin-email' ),
'desc' => __( "Enter a valid e-mail address.", "apc" ),
'validate' => array(
'email' => array(
'param' => '',
'message' => __( "Must be a valid email address!", "apc" )
)
)
) );
$options_panel->addText( 'streetAddress', array(
'name' => __( 'Street', 'apc' ),
'std' => '103 Elm',
'validate' => array(
'street' => array(
'param' => '',
'message' => __( "Enter a valid Street please!", "apc" )
)
)
) );
$options_panel->addText( 'addressLocality', array(
'name' => __( 'City', 'apc' ),
'std' => 'City',
'validate' => array(
'alphanumeric' => array(
'param' => '',
'message' => __( "Must be alpha numberic!", "apc" )
)
)
) );
$options_panel->addText( 'addressRegion', array(
'name' => __( 'State', 'apc' ),
'std' => 'AL',
'validate' => array(
'alphanumeric' => array(
'param' => '',
'message' => __( "Must be alpha numberic!", "apc" )
)
)
) );
$options_panel->addText( 'postalCode', array(
'name' => __( 'Postal Zip Code', 'apc' ),
'std' => '35633',
'validate' => array(
'alphanumeric' => array(
'param' => '',
'message' => __( "Must be alpha numberic!", "apc" )
)
)
) );
$options_panel->addSelect( 'addressCountry', $country_names, array(
'name' => __( 'Country', 'apc' ),
'std' => array(
'UNITED STATES'
),
'desc' => __( 'Choose your Country', 'apc' )
) );
$options_panel->addTextarea( 'description', array(
'name' => __( 'Description', 'apc' ),
'std' => get_bloginfo( 'description' ),
'desc' => __( 'Brief description of what company is.', 'apc' )
) );
/**
* Close first tab
*/
$options_panel->CloseTab();
/**
* Open admin page Second tab
*/
$options_panel->OpenTab( 'options_2' );
//title
$options_panel->Title( __( 'Logo', 'apc' ) );
//Typography field
//Image field
$options_panel->addImage( 'logo', array(
'name' => __( 'Company Logo ', 'apc' ),
'preview_height' => '150px',
'preview_width' => '150px',
'desc' => __( 'Company logo image<br/><small>Image preview may appear distorted, but, the image itself should NOT be.</small>', 'apc' )
) );
/**
* Close second tab
*/
$options_panel->CloseTab();
/**
* Open admin page 3rd tab
*/
$options_panel->OpenTab( 'options_3' );
/**
* Add fields to your admin page 3rd tab
*
*/
//title
$options_panel->Title( __( "URL and Social sites", "apc" ) );
$options_panel->addText( 'url', array(
'name' => __( ' Website ', 'apc' ),
'std' => get_bloginfo( 'wpurl' ),
'desc' => __( "URL of Company's main website", "apc" ),
'validate' => array(
'url' => array(
'param' => '',
'message' => __( "must be a valid URL", "apc" )
)
)
) );
$repeater_fields[] = $options_panel->addText( 'li', array(
'name' => __( 'Enter a URL ', 'apc' ),
'std' => 'http://',
'validate' => array(
'url' => array(
'param' => '',
'message' => __( "must be a valid URL", "apc" )
)
)
), true );
$options_panel->addRepeaterBlock( 'sameAs', array(
'name' => __( 'Other Company profile links', 'apc' ),
'fields' => $repeater_fields,
'desc' => __( 'For Example: http://www.facebook.com/CompanyName', 'apc' )
) );
/**
* Close 3rd tab
*/
$options_panel->CloseTab();
/**
* Open admin page 4th tab
*/
$options_panel->OpenTab( 'options_4' );
//title
$options_panel->Title( __( "Contact", "apc" ) );
$options_panel->addText( 'telephone', array(
'name' => __( 'Main Telephone Number', 'apc' ),
'std' => '+1(800) 555-5555',
'desc' => __( 'Company main phone number', 'apc' ),
'validate' => array(
'phone' => array(
'param' => '',
'message' => __( "Telephone Number does not validate!", "apc" )
)
)
) );
$options_panel->addCheckbox( 'telephone_show', array(
'name' => __( 'Show main phone?', 'apc' ),
'std' => true,
'desc' => __( 'Inculde main phone or not', 'apc' )
) );
$phone_options[] = $options_panel->addText( 'telephone', array(
'name' => __( 'Phone Number', 'apc' ),
'validate' => array(
'phone' => array(
'param' => '',
'message' => __( "Telephone Number does not validate!", "apc" )
)
)
), true );
$phone_options[] = $options_panel->addSelect( 'contactType', $type_contact, array(
'name' => __( "Choose Type", "apc" )
), true );
$phone_options[] = $options_panel->addCheckbox( 'TollFree', array(
'name' => __( 'Toll Free?', 'apc' ),
'std' => true
), true );
$phone_options[] = $options_panel->addCheckbox( 'HearingImpairedSupported', array(
'name' => __( 'Hearing Impaired?', 'apc' ),
'std' => false
), true );
$phone_options[] = $options_panel->addText( 'areaServed', array(
'name' => __( "Area Served", 'apc' ),
'std' => "US, CA, MX"
), true );
$phone_options[] = $options_panel->addText( 'availableLanguage', array(
'name' => __( "Languages, comma seperated", 'apc' ),
'std' => "English, Spanish, German, French"
), true );
$options_panel->addRepeaterBlock( 'ContactPoints', array(
'inline' => true,
'name' => __( "Contact Point", 'apc' ),
'fields' => $phone_options
), true );
/**
* Close 4th tab
*/
$options_panel->CloseTab();
/**
* Open admin page
*/
$options_panel->OpenTab( 'options_4a' );
//title
$options_panel->Title( __( 'Opening Hours', 'apc' ) );
$Conditinal_fields[] = $options_panel->addTime( 'mondayOpen', array(
'name' => __( "Time open Monday", "apc" ),
'std' => '08:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'mondayClose', array(
'name' => __( "Time close Monday", "apc" ),
'std' => '17:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'tuesdayOpen', array(
'name' => __( "Time open Tuesday", "apc" ),
'std' => '08:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'tuesdayClose', array(
'name' => __( "Time close Tuesday", "apc" ),
'std' => '17:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'wednesdayOpen', array(
'name' => __( "Time open Wednesday", "apc" ),
'std' => '08:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'wednesdayClose', array(
'name' => __( "Time close Wednesday", "apc" ),
'std' => '17:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'thursdayOpen', array(
'name' => __( "Time open Thursday", "apc" ),
'std' => '08:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'thursdayClose', array(
'name' => __( "Time close Thursday", "apc" ),
'std' => '17:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'fridayOpen', array(
'name' => __( "Time open Friday", "apc" ),
'std' => '08:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'fridayClose', array(
'name' => __( "Time close Friday", "apc" ),
'std' => '17:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'saturdayOpen', array(
'name' => __( "Time open Saturday", "apc" ),
'std' => '09:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'saturdayClose', array(
'name' => __( "Time close Saturday", "apc" ),
'std' => '17:00'
), true );
$Conditinal_fields[] = $options_panel->addTime( 'sundayOpen', array(
'name' => __( "Time open Sunday", "apc" )
), true );
$Conditinal_fields[] = $options_panel->addTime( 'sundayClose', array(
'name' => __( "Time close Sunday", "apc" )
), true );
/**
* Then just add the fields to the repeater block
*/
//conditinal block
$options_panel->addCondition( 'openingHours', array(
'name' => __( 'Enable Times open? ', 'apc' ),
'desc' => __( '<small>Turn ON if you want to enable the <strong>Time open for each day of the week</strong>.</small>', 'apc' ),
'fields' => $Conditinal_fields,
'std' => false
) );
/**
* Close tab
*/
$options_panel->CloseTab();
/**
* Open admin page 5th tab
*/
$options_panel->OpenTab( 'options_5' );
$options_panel->Title( __( "Sitelinks Search Box", "apc" ) );
$options_panel->addCheckBoxList(
'junk',
array('op1'=>"Op1",'op2'=>"Op2")
);
// $options_panel->addCode('code',array('name'=> __('Code Editor ','apc'),'syntax' => 'php', 'desc' => __('code editor field description','apc')));
$options_panel->addCode( 'target', array(
'name' => 'Company Custom Search',
'syntax' => 'php',
'std' => 'https://example.com/?s={search_term_string}',
'desc' => 'See <a href="https://developers.google.com/structured-data/slsb-overview" target="_blank">https://developers.google.com/structured-data/slsb-overview</a> for additional information.'
) );
/**
* Close 5th tab
*/
$options_panel->CloseTab();
//Search
$options_panel->OpenTab( 'options_6' );
$data = get_option( 'kg_options' );
//assemble the physical postal address
$addr = array(
"@type" => "PostalAddress",
"streetAddress" => $data[ 'streetAddress' ],
"addressLocality" => $data[ 'addressLocality' ],
"addressRegion" => $data[ 'addressRegion' ],
"postalCode" => $data[ 'postalCode' ],
"addressCountry" => $country_2to3[ $data[ 'addressCountry' ] ]
);
/**
Start forming the main data structure. Other
optional parts will be formed and merged later.
*/
$data2 = array(
"@context" => $data[ '@context' ],
"@type" => $data[ '@type' ],
'name' => $data[ 'name' ],
"address" => $addr
);
$data2 = my_array_merge( $data2, "email", $data[ 'email' ] );
$data2 = my_array_merge( $data2, "logo", $data[ 'logo' ][ 'src' ] );
$data2 = my_array_merge( $data2, "description", $data[ 'description' ] );
//add main phone?
if ( $data[ 'telephone_show' ] ) {
$data2 = my_array_merge( $data2, "telephone", $data[ 'telephone' ] );
} //$data[ 'telephone_show' ]
// Opening Hours, hours of operation
if ( isset( $data[ 'openingHours' ][ 'enabled' ] ) ) {
$hours = array( );
foreach ( array(
"Mo",
"Tu",
"We",
"Th",
"Fr",
"Sa",
"Su"
) as $day ) {
$day_long = $days[ $day ];
$id_1 = strtolower( $day_long ) . 'Open';
$id_2 = strtolower( $day_long ) . 'Close';
$opening_time = $data[ 'openingHours' ][ $id_1 ];
$closing_time = $data[ 'openingHours' ][ $id_2 ];
if ( empty( $opening_time ) && empty( $closing_time ) ) {
$get_hours = NULL;
} //empty( $opening_time ) && empty( $closing_time )
else {
$get_hours = "$day $opening_time-$closing_time";
}
if ( !is_null( $get_hours ) ) {
$hours[] = $get_hours;
} //!is_null( $get_hours )
} //foreach short day abbrev
$data2 = my_array_merge( $data2, "openingHours", $hours );
} //if opening hours is set
// There had better be a "url" => $data['url'], or else, what's the point!
$data2 = my_array_merge( $data2, "url", $data[ 'url' ] );
// sameAs links pointing to other organizational profiles
$alinks = $data[ "sameAs" ];
if ( !empty( $alinks ) ) {
$links = array( );
foreach ( $alinks as $alink ) {
$links[] = $alink[ "li" ];
} //$alinks as $alink
$data2 = my_array_merge( $data2, "sameAs", $links );
} //if ( !empty ( $alinks ) )
//Points of Contact, e.g.,
$contact_points = $data[ 'ContactPoints' ];
$contact_point = array( );
for ( $i = 0; $i < count( $contact_points ); $i++ ) {
$contact = array(
"@type" => "ContactPoint"
);
$contact = array_merge( $contact, array(
"contactType" => $contact_points[ $i ][ "contactType" ]
) );
$contact = my_array_merge( $contact, "telephone", $contact_points[ $i ][ "telephone" ] );
$options = array( );
if ( isset( $contact_points[ $i ][ "TollFree" ] ) ) {
$options[] = "TollFree";
} //isset( $contact_points[ $i ][ "TollFree" ] )
if ( isset( $contact_points[ $i ][ "HearingImpairedSupported" ] ) ) {
$options[] = "HearingImpairedSupported";
} //isset( $contact_points[ $i ][ "HearingImpairedSupported" ] )
$contact = my_array_merge( $contact, "contactOption", $options );
$contact = my_array_merge( $contact, "areaServed", str_getcsv( $contact_points[ $i ][ "areaServed" ] ) );
$contact = my_array_merge( $contact, "availableLanguage", str_getcsv( $contact_points[ $i ][ "availableLanguage" ] ) );
$contact_point = array_merge( $contact_point, array(
$contact
) );
} //$i = 0; $i < count( $contact_points ); $i++
$data2 = my_array_merge( $data2, "contactPoint", $contact_point );
$json_view_data[] = $options_panel->addParagraph( '<pre><code>' . json_format( json_encode( $data ) ) . '</code></pre>', true );
$options_panel->addCondition( 'json_data', array(
'name' => __( 'Show current data in JSON format?', 'apc' ),
'desc' => __( '<small>Current entered data in JSON format</small>', 'apc' ),
'fields' => $json_view_data,
'std' => false
) );
$json_view_data2[] = $options_panel->addParagraph( '<pre>' . json_format( json_encode( $data2 ) ) . '</pre>', true );
$options_panel->addCondition( 'json_data2', array(
'name' => __( 'Show generated data in JSON format?', 'apc' ),
'desc' => __( '<small>Select, copy, paste below and edit as needed.<br/>The opening and closing script tags will be added.</small>', 'apc' ),
'fields' => $json_view_data2,
'std' => false
) );
$options_panel->addCode( 'the_script', array(
'name' => __( 'This is what will be included in the HEAD section of this site!', 'apc' ),
'std' => json_format( json_encode( $data2 ) ),
'syntax' => 'javascript',
'desc' => 'Test the above at <a href="https://developers.google.com/structured-data/testing-tool/" target="_blank">https://developers.google.com/structured-data/testing-tool/</a>'
) );
//checkbox field
$options_panel->addCheckbox( 'write_file', array(
'name' => __( 'Use the above code? ', 'apc' ),
'std' => false,
'desc' => __( 'Check to use the above on this website!', 'apc' )
) );
if ( $data[ "write_file" ] ) {
/**
* Write the edited script ('the_script') to a file, that will then be read
* back end and included in the <head/>.
*
*/
$jsonFile = MYPLUGIN_PLUGIN_DIR . "/knowledge-graph.json";
$fh = fopen( $jsonFile, 'w' );
//header('Content-type: application/ld+json');
$str = '<script type="application/ld+json">' . PHP_EOL;
//$str .= json_encode( $data2, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ).PHP_EOL;
$str .= $data[ 'the_script' ];
$str .= '</script>' . PHP_EOL;
fwrite( $fh, $str );
function kg_header( ) {
$jsonFile = MYPLUGIN_PLUGIN_DIR . "/knowledge-graph.json";
$fh = fopen( "knowledge-graph.json", 'r' );
$json = fread( $fh, filesize( $jsonFile ) );
echo $json . PHP_EOL;
}
add_action( 'wp_head', 'kg_header' );
} //$data[ "write_file" ]
/**
* Close 6th tab
*/
$options_panel->CloseTab();
/**
* Open admin page 7th tab
*/
$options_panel->OpenTab( 'options_7' );
//title
$options_panel->Title( __( "Import Export", "apc" ) );
/**
* add import export functionallty
*/
$options_panel->addImportExport();
/**
* Close 7th tab
*/
$options_panel->CloseTab();
?>