-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOpenGraph.php
More file actions
879 lines (763 loc) · 34.6 KB
/
OpenGraph.php
File metadata and controls
879 lines (763 loc) · 34.6 KB
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
<?php
/**
* PHP class for Typesetter CMS plugin 'OpenGraph'
*
* @package OpenGraph
* @author J. Krausz (http://typesetter-addons.grafikrausz.at)
* @version 1.0-b4
*/
defined('is_running') or die('Not an entry point...');
class OpenGraph{
static $ogp_data = array();
static $ogp_settings = array();
static $ogp_tags = array(
array(
'property' => 'og:type',
'hint' => 'optional: The type of your object, e.g., "website". Depending on the type you specify, other properties may also be required.',
'content' => '',
'control' => array(
'type' => 'select',
'values' => array(
array( 'value' => 'website', 'text' => 'website' ),
array( 'value' => 'blog', 'text' => 'blog' ),
array( 'value' => 'article', 'text' => 'article' ),
array( 'value' => 'product', 'text' => 'product' ),
),
'default_value' => array('website'),
'attributes' => array(
'class' => 'gpinput has-helper',
),
'helpers' => array('type'),
),
),
array(
'property' => 'og:title',
'hint' => '<strong>required:</strong> The title of your object as it should appear within the graph, e.g., "Heading Page".',
'content' => '',
'inherit' => 1,
'control' => array(
'type' => 'text',
'values' => array(''),
'default_value' => array(''),
'attributes' => array(
'class' => 'gpinput has-helper',
'placeholder' => 'the title must not be empty!',
'required' => 'required',
),
'helpers' => array('title', 'char_counter'),
),
),
array(
'property' => 'og:url',
'hint' => '<strong>required:</strong> The canonical URL of your object that will be used as its permanent ID in the graph, e.g., "http://www.imdb.com/title/tt0117500/"',
'content' => '',
'inherit' => 1,
'control' => array(
'type' => 'text',
'values' => array(''),
'default_value' => array(''),
'attributes' => array(
'class' => 'gpinput has-helper',
'placeholder' => 'the URL must not be empty!',
'required' => 'required',
),
'helpers' => array('url'),
),
),
array(
'property' => 'og:image',
'hint' => '<strong>required:</strong> An image URL which should represent your object within the graph.',
'content' => '',
'inherit' => 1,
'control' => array(
'type' => 'text',
'values' => array(''),
'default_value' => array(''),
'attributes' => array(
'class' => 'gpinput has-helper',
'placeholder' => 'the image URL must not be empty!',
'required' => 'required',
),
'helpers' => array('show_image','image','select_file'),
),
),
array(
'property' => 'og:site_name',
'hint' => 'optional: If your object is part of a larger web site, the name which should be displayed for the overall site.',
'content' => '',
'inherit' => 1,
'control' => array(
'type' => 'text',
'values' => array(''),
'default_value' => array(''),
'attributes' => array(
'class' => 'gpinput has-helper',
'placeholder' => '',
),
'helpers' => array('site_name', 'char_counter'),
),
),
array(
'property' => 'og:description',
'hint' => 'optional: A one to two sentence description of your object.',
'content' => '',
'inherit' => 1,
'control' => array(
'type' => 'textarea',
'values' => array(''),
'default_value' => array(''),
'attributes' => array(
'class' => 'gptextarea has-helper',
'placeholder' => '',
),
'helpers' => array('description', 'char_counter'),
),
),
array(
'property' => 'og:video',
'hint' => 'optional: A URL to a video file that complements this object.',
'content' => '',
'inherit' => 1,
'control' => array(
'type' => 'text',
'values' => array(''),
'default_value' => array(''),
'attributes' => array(
'class' => 'gpinput has-helper',
'placeholder' => '',
),
'helpers' => array('video', 'select_file'),
),
),
array(
'property' => 'og:audio',
'hint' => 'optional: A URL to an audio file to accompany this object.',
'content' => '',
'inherit' => 1,
'control' => array(
'type' => 'text',
'values' => array(''),
'default_value' => array(''),
'attributes' => array(
'class' => 'gpinput has-helper',
'placeholder' => '',
),
'helpers' => array('audio', 'select_file'),
),
),
array(
'property' => 'og:locale',
'hint' => 'optional: The locale these tags are marked up in. Of the format language_TERRITORY. Default is en_US.',
'content' => '',
'control' => array(
'type' => 'select',
'values' => array(
array( 'value' => '', 'text' => '--- none ---' ),
array( 'value' => 'af_ZA', 'text' => 'af_ZA → Afrikaans' ),
array( 'value' => 'ak_GH', 'text' => 'ak_GH → Akan' ),
array( 'value' => 'am_ET', 'text' => 'am_ET → Amharic' ),
array( 'value' => 'ar_AR', 'text' => 'ar_AR → Arabic' ),
array( 'value' => 'as_IN', 'text' => 'as_IN → Assamese' ),
array( 'value' => 'ay_BO', 'text' => 'ay_BO → Aymara' ),
array( 'value' => 'az_AZ', 'text' => 'az_AZ → Azerbaijani' ),
array( 'value' => 'be_BY', 'text' => 'be_BY → Belarusian' ),
array( 'value' => 'bg_BG', 'text' => 'bg_BG → Bulgarian' ),
array( 'value' => 'bn_IN', 'text' => 'bn_IN → Bengali' ),
array( 'value' => 'br_FR', 'text' => 'br_FR → Breton' ),
array( 'value' => 'bs_BA', 'text' => 'bs_BA → Bosnian' ),
array( 'value' => 'ca_ES', 'text' => 'ca_ES → Catalan' ),
array( 'value' => 'cb_IQ', 'text' => 'cb_IQ → Sorani Kurdish' ),
array( 'value' => 'ck_US', 'text' => 'ck_US → Cherokee' ),
array( 'value' => 'co_FR', 'text' => 'co_FR → Corsican' ),
array( 'value' => 'cs_CZ', 'text' => 'cs_CZ → Czech' ),
array( 'value' => 'cx_PH', 'text' => 'cx_PH → Cebuano' ),
array( 'value' => 'cy_GB', 'text' => 'cy_GB → Welsh' ),
array( 'value' => 'da_DK', 'text' => 'da_DK → Danish' ),
array( 'value' => 'de_DE', 'text' => 'de_DE → German' ),
array( 'value' => 'el_GR', 'text' => 'el_GR → Greek' ),
array( 'value' => 'en_GB', 'text' => 'en_GB → English (UK)' ),
array( 'value' => 'en_IN', 'text' => 'en_IN → English (India)' ),
array( 'value' => 'en_PI', 'text' => 'en_PI → English (Pirate)' ),
array( 'value' => 'en_UD', 'text' => 'en_UD → English (Upside Down)' ),
array( 'value' => 'en_US', 'text' => 'en_US → English (US)' ),
array( 'value' => 'eo_EO', 'text' => 'eo_EO → Esperanto' ),
array( 'value' => 'es_CL', 'text' => 'es_CL → Spanish (Chile)' ),
array( 'value' => 'es_CO', 'text' => 'es_CO → Spanish (Colombia)' ),
array( 'value' => 'es_ES', 'text' => 'es_ES → Spanish (Spain)' ),
array( 'value' => 'es_LA', 'text' => 'es_LA → Spanish' ),
array( 'value' => 'es_MX', 'text' => 'es_MX → Spanish (Mexico)' ),
array( 'value' => 'es_VE', 'text' => 'es_VE → Spanish (Venezuela)' ),
array( 'value' => 'et_EE', 'text' => 'et_EE → Estonian' ),
array( 'value' => 'eu_ES', 'text' => 'eu_ES → Basque' ),
array( 'value' => 'fa_IR', 'text' => 'fa_IR → Persian' ),
array( 'value' => 'fb_LT', 'text' => 'fb_LT → Leet Speak' ),
array( 'value' => 'ff_NG', 'text' => 'ff_NG → Fulah' ),
array( 'value' => 'fi_FI', 'text' => 'fi_FI → Finnish' ),
array( 'value' => 'fo_FO', 'text' => 'fo_FO → Faroese' ),
array( 'value' => 'fr_CA', 'text' => 'fr_CA → French (Canada)' ),
array( 'value' => 'fr_FR', 'text' => 'fr_FR → French (France)' ),
array( 'value' => 'fy_NL', 'text' => 'fy_NL → Frisian' ),
array( 'value' => 'ga_IE', 'text' => 'ga_IE → Irish' ),
array( 'value' => 'gl_ES', 'text' => 'gl_ES → Galician' ),
array( 'value' => 'gn_PY', 'text' => 'gn_PY → Guarani' ),
array( 'value' => 'gu_IN', 'text' => 'gu_IN → Gujarati' ),
array( 'value' => 'gx_GR', 'text' => 'gx_GR → Classical Greek' ),
array( 'value' => 'ha_NG', 'text' => 'ha_NG → Hausa' ),
array( 'value' => 'he_IL', 'text' => 'he_IL → Hebrew' ),
array( 'value' => 'hi_IN', 'text' => 'hi_IN → Hindi' ),
array( 'value' => 'hr_HR', 'text' => 'hr_HR → Croatian' ),
array( 'value' => 'ht_HT', 'text' => 'ht_HT → Haitian Creole' ),
array( 'value' => 'hu_HU', 'text' => 'hu_HU → Hungarian' ),
array( 'value' => 'hy_AM', 'text' => 'hy_AM → Armenian' ),
array( 'value' => 'id_ID', 'text' => 'id_ID → Indonesian' ),
array( 'value' => 'ig_NG', 'text' => 'ig_NG → Igbo' ),
array( 'value' => 'is_IS', 'text' => 'is_IS → Icelandic' ),
array( 'value' => 'it_IT', 'text' => 'it_IT → Italian' ),
array( 'value' => 'ja_JP', 'text' => 'ja_JP → Japanese' ),
array( 'value' => 'ja_KS', 'text' => 'ja_KS → Japanese (Kansai)' ),
array( 'value' => 'jv_ID', 'text' => 'jv_ID → Javanese' ),
array( 'value' => 'ka_GE', 'text' => 'ka_GE → Georgian' ),
array( 'value' => 'kk_KZ', 'text' => 'kk_KZ → Kazakh' ),
array( 'value' => 'km_KH', 'text' => 'km_KH → Khmer' ),
array( 'value' => 'kn_IN', 'text' => 'kn_IN → Kannada' ),
array( 'value' => 'ko_KR', 'text' => 'ko_KR → Korean' ),
array( 'value' => 'ku_TR', 'text' => 'ku_TR → Kurdish (Kurmanji)' ),
array( 'value' => 'ky_KG', 'text' => 'ky_KG → Kyrgyz' ),
array( 'value' => 'la_VA', 'text' => 'la_VA → Latin' ),
array( 'value' => 'lg_UG', 'text' => 'lg_UG → Ganda' ),
array( 'value' => 'li_NL', 'text' => 'li_NL → Limburgish' ),
array( 'value' => 'ln_CD', 'text' => 'ln_CD → Lingala' ),
array( 'value' => 'lo_LA', 'text' => 'lo_LA → Lao' ),
array( 'value' => 'lt_LT', 'text' => 'lt_LT → Lithuanian' ),
array( 'value' => 'lv_LV', 'text' => 'lv_LV → Latvian' ),
array( 'value' => 'mg_MG', 'text' => 'mg_MG → Malagasy' ),
array( 'value' => 'mi_NZ', 'text' => 'mi_NZ → Maori' ),
array( 'value' => 'mk_MK', 'text' => 'mk_MK → Macedonian' ),
array( 'value' => 'ml_IN', 'text' => 'ml_IN → Malayalam' ),
array( 'value' => 'mn_MN', 'text' => 'mn_MN → Mongolian' ),
array( 'value' => 'mr_IN', 'text' => 'mr_IN → Marathi' ),
array( 'value' => 'ms_MY', 'text' => 'ms_MY → Malay' ),
array( 'value' => 'mt_MT', 'text' => 'mt_MT → Maltese' ),
array( 'value' => 'my_MM', 'text' => 'my_MM → Burmese' ),
array( 'value' => 'nb_NO', 'text' => 'nb_NO → Norwegian (bokmal)' ),
array( 'value' => 'nd_ZW', 'text' => 'nd_ZW → Ndebele' ),
array( 'value' => 'ne_NP', 'text' => 'ne_NP → Nepali' ),
array( 'value' => 'nl_BE', 'text' => 'nl_BE → Dutch (België)' ),
array( 'value' => 'nl_NL', 'text' => 'nl_NL → Dutch' ),
array( 'value' => 'nn_NO', 'text' => 'nn_NO → Norwegian (nynorsk)' ),
array( 'value' => 'ny_MW', 'text' => 'ny_MW → Chewa' ),
array( 'value' => 'or_IN', 'text' => 'or_IN → Oriya' ),
array( 'value' => 'pa_IN', 'text' => 'pa_IN → Punjabi' ),
array( 'value' => 'pl_PL', 'text' => 'pl_PL → Polish' ),
array( 'value' => 'ps_AF', 'text' => 'ps_AF → Pashto' ),
array( 'value' => 'pt_BR', 'text' => 'pt_BR → Portuguese (Brazil)' ),
array( 'value' => 'pt_PT', 'text' => 'pt_PT → Portuguese (Portugal)' ),
array( 'value' => 'qc_GT', 'text' => 'qc_GT → Quiché' ),
array( 'value' => 'qu_PE', 'text' => 'qu_PE → Quechua' ),
array( 'value' => 'rm_CH', 'text' => 'rm_CH → Romansh' ),
array( 'value' => 'ro_RO', 'text' => 'ro_RO → Romanian' ),
array( 'value' => 'ru_RU', 'text' => 'ru_RU → Russian' ),
array( 'value' => 'rw_RW', 'text' => 'rw_RW → Kinyarwanda' ),
array( 'value' => 'sa_IN', 'text' => 'sa_IN → Sanskrit' ),
array( 'value' => 'sc_IT', 'text' => 'sc_IT → Sardinian' ),
array( 'value' => 'se_NO', 'text' => 'se_NO → Northern Sámi' ),
array( 'value' => 'si_LK', 'text' => 'si_LK → Sinhala' ),
array( 'value' => 'sk_SK', 'text' => 'sk_SK → Slovak' ),
array( 'value' => 'sl_SI', 'text' => 'sl_SI → Slovenian' ),
array( 'value' => 'sn_ZW', 'text' => 'sn_ZW → Shona' ),
array( 'value' => 'so_SO', 'text' => 'so_SO → Somali' ),
array( 'value' => 'sq_AL', 'text' => 'sq_AL → Albanian' ),
array( 'value' => 'sr_RS', 'text' => 'sr_RS → Serbian' ),
array( 'value' => 'sv_SE', 'text' => 'sv_SE → Swedish' ),
array( 'value' => 'sw_KE', 'text' => 'sw_KE → Swahili' ),
array( 'value' => 'sy_SY', 'text' => 'sy_SY → Syriac' ),
array( 'value' => 'sz_PL', 'text' => 'sz_PL → Silesian' ),
array( 'value' => 'ta_IN', 'text' => 'ta_IN → Tamil' ),
array( 'value' => 'te_IN', 'text' => 'te_IN → Telugu' ),
array( 'value' => 'tg_TJ', 'text' => 'tg_TJ → Tajik' ),
array( 'value' => 'th_TH', 'text' => 'th_TH → Thai' ),
array( 'value' => 'tk_TM', 'text' => 'tk_TM → Turkmen' ),
array( 'value' => 'tl_PH', 'text' => 'tl_PH → Filipino' ),
array( 'value' => 'tl_ST', 'text' => 'tl_ST → Klingon' ),
array( 'value' => 'tr_TR', 'text' => 'tr_TR → Turkish' ),
array( 'value' => 'tt_RU', 'text' => 'tt_RU → Tatar' ),
array( 'value' => 'tz_MA', 'text' => 'tz_MA → Tamazight' ),
array( 'value' => 'uk_UA', 'text' => 'uk_UA → Ukrainian' ),
array( 'value' => 'ur_PK', 'text' => 'ur_PK → Urdu' ),
array( 'value' => 'uz_UZ', 'text' => 'uz_UZ → Uzbek' ),
array( 'value' => 'vi_VN', 'text' => 'vi_VN → Vietnamese' ),
array( 'value' => 'wo_SN', 'text' => 'wo_SN → Wolof' ),
array( 'value' => 'xh_ZA', 'text' => 'xh_ZA → Xhosa' ),
array( 'value' => 'yi_DE', 'text' => 'yi_DE → Yiddish' ),
array( 'value' => 'yo_NG', 'text' => 'yo_NG → Yoruba' ),
array( 'value' => 'zh_CN', 'text' => 'zh_CN → Simplified Chinese (China)' ),
array( 'value' => 'zh_HK', 'text' => 'zh_HK → Traditional Chinese (Hong Kong)' ),
array( 'value' => 'zh_TW', 'text' => 'zh_TW → Traditional Chinese (Taiwan)' ),
array( 'value' => 'zu_ZA', 'text' => 'zu_ZA → Zulu' ),
array( 'value' => 'zz_TR', 'text' => 'zz_TR → Zazaki' )
),
'default_value' => array(''),
'attributes' => array(
'class' => 'gpinput has-helper',
'placeholder' => '',
),
'helpers' => array('locale'),
),
),
);
/*
* Typesetter Action hook
*/
public static function GetHead() {
global $page, $addonRelativeCode;
if( \gp\tool::LoggedIn() ){
$page->css_admin[] = $addonRelativeCode . '/OpenGraph.css';
$page->head_js[] = $addonRelativeCode . '/OpenGraph.js';
$ogp_helper_data = array();
$ogp_helper_data['url_prefix'] = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://" ) . \gp\tool::ServerName(false,true);
$ogp_helper_data['url'] = self::GetDefaultContent('og:url');
$ogp_helper_data['type'] = self::GetDefaultContent('og:type');
$ogp_helper_data['site_name'] = self::GetDefaultContent('og:site_name');
$ogp_helper_data['title'] = self::GetDefaultContent('og:title');
$ogp_helper_data['locale'] = self::GetDefaultContent('og:locale');
$ogp_helper_data['description'] = self::GetDefaultContent('og:description');
$ogp_helper_data['image'] = self::UrlPrefix( self::GetDefaultContent('og:image'), 'add' );
$ogp_helper_data['video'] = self::UrlPrefix( self::GetDefaultContent('og:video'), 'add' );
$ogp_helper_data['audio'] = self::UrlPrefix( self::GetDefaultContent('og:audio'), 'add' );
$page->head_script .= "\n" . 'var OpenGraphHelper_Defaults = ' . json_encode($ogp_helper_data, JSON_FORCE_OBJECT) . ';' . "\n\n";
}
self::GetOgpData();
$page->head .= "\n<!-- OpenGraph start -->";
foreach( self::$ogp_tags as $key => $tag_arr ){
$property = $tag_arr['property'];
if( isset(self::$ogp_data[$property]) ){
$content = self::$ogp_data[$property];
switch( $property ){
case 'og:url':
case 'og:image':
case 'og:video':
case 'og:audio':
//$content = urlencode(self::UrlPrefix($content, 'add'));
$content = self::UrlPrefix($content, 'add');
break;
}
}else{
$content = self::GetDefaultContent($property);
}
if( !empty($content) ){
$page->head .= "\n" . '<meta property="' . $property . '" content="' . $content . '" />';
}
}
$page->head .= "\n<!-- OpenGraph end -->\n";
}
/*
* Typesetter Filter hook
*/
public static function PageRunScript($cmd) {
global $page, $langmessage, $addonRelativeCode;
if( \gp\tool::LoggedIn() ){
$page->admin_links[] = array(
$page->requested,
'<i class="ogp-icon"></i> Open Graph',
'cmd=OpenGraphForm',
'title="set OpenGraph tags" class="ogp-button" data-cmd="gpabox"'
);
switch( $cmd ){
case 'OpenGraphForm':
self::GetOgpData();
ob_start();
echo '<div class="inline_box">';
echo \gp\tool::Link('Admin_OpenGraph', $langmessage['general_settings'], '', 'style="float:right;"');
echo '<h4 style="margin:-1em 0 1em 0;">';
echo '<img src="' . $addonRelativeCode . '/img/OpenGraph-color.svg" class="ogp-logo-color" />';
echo 'Open Graph » ' . $langmessage['page_options'];
echo '</h4>';
self::OgpForm('page');
echo '<script>';
echo 'OpenGraphHelpers.init();';
echo '</script>';
echo '</div>';
$page->contentBuffer = ob_get_clean();
return 'return';
break;
case 'SaveOgpData':
self::SaveOgpData();
return $cmd;
break;
}
}
return $cmd;
}
public static function AdminPage() {
global $page, $addonRelativeCode, $langmessage;
$page->jQueryCode .= "\n/* OpenGraph start */\n" . 'OpenGraphHelpers.init();' . "\n/* OpenGraph end */\n";
$cmd = \gp\tool::GetCommand();
if( $cmd == 'SaveOgpData' ){
self::SaveOgpData();
}
echo '<h3 class="hqmargin">';
echo '<img src="' . $addonRelativeCode . '/img/OpenGraph-color.svg" class="ogp-logo-color" />';
echo 'Open Graph » ' . $langmessage['general_settings'];
echo '</h3><br/>';
self::GetOgpData();
self::OgpForm('admin');
}
public static function GetDefaultContent($property) {
global $page, $config, $addonRelativeCode, $langmessage;
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$content = '';
$blog_page_title = self::GetBlogPageTitle();
switch( $property ){
case 'og:title':
// use page label
$content = $langmessage['unavailable'];
if( $blog_page_title ){
// Blog pages
$content = str_replace('_', ' ', $blog_page_title);
}elseif( isset($page->TitleInfo['label']) ){
// Regular pages
$content = $page->TitleInfo['label'];
}elseif( isset($page->TitleInfo['lang_index']) ){
// Special pages
$content = gpOutput::SelectText($page->TitleInfo['lang_index']);
}
break;
case 'og:type':
// this is always 'website'
$content = 'website';
if( $page->gp_index == 'special_blog' || $page->gp_index == 'special_blog_categories' ){
$content = 'blog';
}
if( $page->gp_index == 'special_blog' && $blog_page_title ){
$content = 'article';
}
break;
case 'og:url':
// use page URL
$content = $protocol . \gp\tool::ServerName(false,true) . \gp\tool::GetUrl($page->requested);
break;
case 'og:description':
if( isset($page->TitleInfo['description']) ){
// use page meta description
$content = $page->TitleInfo['description'];
}else{
// use website meta description
$content = $config['desc'];
}
break;
case 'og:locale':
// $content = self::GetPageLanguage();
if( isset($config['opengraph']['og:locale']) ){
// use default locale, if set
$content = $config['opengraph']['og:locale'];
}else{
$content = '';
}
break;
case 'og:site_name':
// use website title
$content = $config['title'];
break;
case 'og:image':
if( isset($config['opengraph']['og:image']) ){
// use default image, if set
$content = $config['opengraph']['og:image'];
}else{
// use plugin's preset image
$content = $protocol . \gp\tool::ServerName(false,true) . $addonRelativeCode . '/img/default_image.png';
}
break;
case 'og:video':
if( isset($config['opengraph']['og:video']) ){
// use default video, if set
$content = $config['opengraph']['og:video'];
}else{
$content = '';
}
break;
case 'og:audio':
if( isset($config['opengraph']['og:audio']) ){
// use default audio, if set
$content = $config['opengraph']['og:audio'];
}else{
$content = '';
}
break;
}
return $content;
}
public static function GetOgpData() {
global $page, $addonPathData;
$page_ogp_data = array();
if( isset($page->TitleInfo['opengraph']) ){
$page_ogp_data = $page->TitleInfo['opengraph'];
}
$blog_page_title = self::GetBlogPageTitle();
if( $blog_page_title ){
$ogp_data_file = $addonPathData . '/' . $page->gp_index . '.php';
if( file_exists($ogp_data_file) ){
include($ogp_data_file);
if( !empty($ogp_data[$blog_page_title]) ){
$page_ogp_data = $ogp_data[$blog_page_title];
}
}
}
foreach( self::$ogp_tags as $key => $tag_arr ){
$property = $tag_arr['property'];
if( isset($page_ogp_data[$property]) ){
// stored property for page
self::$ogp_data[$property] = $page_ogp_data[$property];
self::$ogp_settings[$property] = 'custom';
}else{
// page property is not defined, get default value
self::$ogp_data[$property] = self::GetDefaultContent($property);
self::$ogp_settings[$property] = 'default';
}
}
if( $page->title ){
self::$ogp_data = \gp\tool\Plugins::Filter('OpenGraph', array(self::$ogp_data));
}
// msg('ogp_settings: ' . pre(self::$ogp_settings));
// msg('ogp_data: ' . pre(self::$ogp_data));
}
public static function SaveOgpData() {
global $page, $config, $gp_index, $gp_titles, $langmessage, $addonPathData;
$opengraph_arr = array();
// overwrite defaults with post values
foreach( self::$ogp_tags as $key => $tag_arr ){
$property = $tag_arr['property'];
if( !empty($_POST[$property]) ){
$content = trim($_POST[$property]);
switch( $property ){
case 'og:url':
case 'og:image':
case 'og:video':
case 'og:audio':
$content = self::UrlPrefix($content,'remove');
break;
}
$opengraph_arr[$property] = $content;
}
}
$blog_page_title = self::GetBlogPageTitle(); // returns false if page is not Simple Blog generated
if( !$page->gp_index ){
$save_to = 'site_config'; // admin page, default values posted
}elseif( $blog_page_title ){
$save_to = 'blog_info'; // page is a Simple Blog subpage (post or category)
}else{
$save_to = 'gp_titles'; // regular page
}
switch( $save_to ){
case 'gp_titles':
$gp_titles[$page->gp_index]['opengraph'] = $opengraph_arr;
\gp\admin\Tools::SavePagesPHP(true, true);
break;
case 'blog_info':
// $page->gp_index will be 'special_blog' or 'special_blog_categories'
$ogp_data_file = $addonPathData . '/' . $page->gp_index . '.php';
$ogp_data = array();
if( file_exists($ogp_data_file) ){
// load existing data
include($ogp_data_file);
}
$ogp_data[$blog_page_title] = $opengraph_arr;
if( \gp\tool\Files::SaveData($ogp_data_file, 'ogp_data', $ogp_data) ){
msg($langmessage['SAVED'] . ' (Simple Blog)');
}else{
msg($langmessage['OOPS'] . ' (Simple Blog)');
}
break;
case 'site_config':
$config['opengraph'] = $opengraph_arr;
\gp\admin\Tools::SaveConfig(true, true);
break;
}
}
/**
* For Simple Blog support
* @return (string)title of single blog post or blog category page
* @return (boolean)false if current page is not such a page
*/
public static function GetBlogPageTitle(){
global $page;
return substr($page->requested, strlen($page->title . '/'));
}
public static function OgpForm($render_mode='admin'){
global $page, $langmessage, $config;
// msg('$page = ' . pre(get_object_vars($page)));
$form_action = \gp\tool::GetUrl($page->requested);
$data_cmd = $page->gp_index ? ' data-cmd="gppost" ' : '';
$admin_box_close = $page->gp_index ? 'admin_box_close ' : '';
echo '<form method="post" action="' . $form_action . '" id="ogp-form">';
echo '<table class="bordered full_width ogp-table">';
echo '<thead>';
echo '<tr>';
echo '<th class="gp_header">property</th>';
echo '<th class="gp_header">content</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach( self::$ogp_tags as $key => $tag_arr ){
$property = $tag_arr['property'];
if( $render_mode == 'admin' && ($property == 'og:title' || $property == 'og:url' || $property == 'og:site_name' || $property == 'og:description') ){
// on Admin Page/Defalt Settings don't render title, url, site_name and description
continue;
}
$content = !empty(self::$ogp_data[$property]) ? self::$ogp_data[$property] : '' ;
switch( $property ){
case 'og:url':
case 'og:image':
case 'og:video':
case 'og:audio':
$content = self::UrlPrefix($content,'add');
break;
}
echo '<tr>';
echo '<td>';
echo '<div class="ogp-text-large">' . $property . '</div>';
echo '<div class="ogp-text-small">' . $tag_arr['hint'] . '</div>';
echo '</td>';
echo '<td>';
$attributes = ' ';
foreach( $tag_arr['control']['attributes'] as $attr => $attr_val ){
$attributes .= $attr . '="' . $attr_val . '" ';
}
$disabled = self::$ogp_settings[$property] == 'default' && $render_mode == 'page' ? ' disabled="disabled" ' : ' ';
switch( $tag_arr['control']['type'] ){
case 'text':
echo '<input' . $attributes . $disabled . 'type="text" name="'. $property . '" value="' . $content . '"/>';
break;
case 'textarea':
echo '<textarea' . $attributes . ' name="'. $property . '">' . $content . '</textarea>';
break;
case 'select':
echo '<select' . $attributes . $disabled . ' name="'. $property . '">';
foreach( $tag_arr['control']['values'] as $key => $value_arr ){
$selected = $content == $value_arr['value'] ? ' selected="selected" ' : ' ';
echo '<option' . $selected . 'value="' . $value_arr['value'] . '">' . $value_arr['text'] . '</option>';
}
echo '</select>';
break;
case 'radio':
foreach( $tag_arr['control']['values'] as $key => $value_arr ){
$checked = $content == 'on' ? ' checked="checked" ' : ' ';
echo '<input type="radio"' . $attributes . $checked . $disabled . ' name="'. $property . '" ';
echo 'value="' . $value_arr['value'] . '"/> ' . htmlspecialchars($value_arr['text']) . ' ';
}
break;
case 'checkbox':
$checked = !empty($content) ? ' checked="checked" ' : ' ';
echo '<input' . $attributes . $checked . $disabled . 'type="checkbox" name="'. $property . '"/> ' . $tag_arr['content'];
break;
}
self::GetHelpers($tag_arr['control']['helpers'], $render_mode);
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '<br/>';
echo '<span style="float:right;">See also ';
echo '<a href="http://ogp.me" target="_blank">ogp.me</a>, ';
echo '<a href="https://developers.facebook.com/docs/sharing/best-practices#tags" target="_blank"><i class="fa fa-facebook-official"></i> Best Practices</a>';
echo '</span>';
echo '<input type="hidden" name="cmd" value="SaveOgpData"/> ';
echo '<input onclick="OpenGraphHelpers.destroy()" type="submit" name="" value="' . $langmessage['save'] . '" class="gpsubmit"' . $data_cmd . '/>';
echo '<input onclick="OpenGraphHelpers.destroy()" type="button" class="' . $admin_box_close . 'gpcancel" name="" value="' . $langmessage['cancel'] . '" />';
echo '</form>';
}
public static function GetHelpers($helpers=array(), $render_mode){
global $langmessage;
if( count($helpers) ){
echo '<div class="ogp-helper cf">';
foreach( $helpers as $key => $helper ){
switch( $helper ){
case 'select_file':
echo '<a href="#" class="ogp-select-file-btn gpsubmit">' . $langmessage['uploaded_files'] . '</a>';
break;
case 'show_image':
echo '<div class="ogp-image-preview">';
echo '<div class="ogp-image-bg"></div>';
echo '</div>';
echo '<div class="ogp-image-preview-btns">';
echo '<a class="gpcancel" href="javascript:;" data-socialmedia="facebook"><i class="fa fa-fw fa-facebook-official"></i> facebook</a>';
// echo '<a class="gpcancel" href="javascript:;" data-socialmedia="googleplus"><i class="fa fa-fw fa-google-plus"></i> Google+</a>';
echo '<a class="gpcancel" href="javascript:;" data-socialmedia="linkedin"><i class="fa fa-fw fa-linkedin"></i> LinkedIn</a>';
echo '<a class="gpcancel" href="javascript:;" data-socialmedia="pinterest"><i class="fa fa-fw fa-pinterest"></i> Pinterest</a>';
echo '<a class="gpcancel" href="javascript:;" data-socialmedia="twitter"><i class="fa fa-fw fa-twitter"></i> Twitter</a>';
echo '</div>';
echo '<br style="clear:both; height:0; margin:0;" />';
break;
case 'char_counter':
echo '<div class="ogp-char-counter"><span class="ogp-char-count"></span> ' . str_replace('%s ', '', $langmessage['_characters']) . '</div>';
break;
case 'type':
case 'site_name':
case 'title':
case 'url':
case 'description':
if( $render_mode == 'page'){
// auto checkbox only in page render mode
$checked = self::$ogp_settings['og:' . $helper ] == 'default' ? ' checked="checked" ' : ' ';
echo '<label class="ogp-checkbox ogp-auto-value" title="generate from existing page data">';
echo '<input' . $checked . 'type="checkbox" data-helper-name="' . $helper . '"/><span></span> auto';
echo '</label>';
}
break;
case 'image':
case 'video':
case 'audio':
case 'locale':
if( $render_mode == 'page'){
$checked = self::$ogp_settings['og:' . $helper ] == 'default' ? ' checked="checked" ' : ' ';
echo '<label class="ogp-checkbox ogp-auto-value" title="use ' . $langmessage['general_settings'] . '">';
echo '<input' . $checked . 'type="checkbox" data-helper-name="' . $helper . '"/><span></span> default';
echo '</label>';
}
break;
}
}
echo '</div>';
}
}
/**
* Multi-Language Manager Support
* @return (string) language code
*/
public static function GetPageLanguage(){
global $page, $config, $ml_object;
if( !$ml_object ){
return $config['language'];
}else{
// only if Multi-Language Manager ist installed
$ml_list = $ml_object->GetList($page->gp_index);
$ml_lang = is_array($ml_list) && ($ml_lang = array_search($page->gp_index, $ml_list)) !== false ? $ml_lang : $config['language'];
return $ml_lang;
}
}
/**
* Strips or adds protocol, server name and $dirPrefix
* from/to internal URLs (starting with '/')
* to store them in a portable format
*/
public static function UrlPrefix($url, $action=false){
global $dirPrefix;
$url_prefix =
((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://" )
. \gp\tool::ServerName(false,true)
. $dirPrefix;
switch( $action ){
case 'add':
if( substr($url, 0, 1) == '/' && substr($url, 0, 2) !== '//' ){
$url = $url_prefix . $url;
}
break;
case 'remove':
if( strpos($url, $url_prefix) !== false ){
$url = substr($url, strlen($url_prefix));
}
break;
}
return $url;
}
}