-
Notifications
You must be signed in to change notification settings - Fork 1
/
seo_meta.module
904 lines (806 loc) · 28.9 KB
/
seo_meta.module
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
<?php
/**
* @file seo_meta.module
*/
define('TYPE_DEFAULTS', 0);
define('TYPE_HOMEPAGE', 1);
define('TYPE_NODE', 2);
define('TYPE_VIEW', 3);
define('TYPE_TAXONOMY', 4);
define('TAG_WEIGHT', 100);
/**
* Implements hook_config_info().
*/
function seo_meta_config_info() {
$prefixes['seo_meta.settings'] = array(
'label' => t('SEO Meta Tags'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_permission().
*
*/
function seo_meta_permission() {
return array(
'administer seo meta settings' => array(
'title' => t('Administer SEO Meta Tags settings')
),
);
}
/**
* Implements hook_menu().
*
*/
function seo_meta_menu() {
$sub_path = seo_meta_menu_sub_path();
$items = array();
$items['admin/config/' . $sub_path . '/seo_meta'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => 'SEO Meta Tags',
'page callback' => 'backdrop_get_form',
'page arguments' => array('seo_meta_settings_form'),
'file' => 'seo_meta.admin.inc',
'access callback' => 'user_access',
'access arguments' => array('administer seo meta settings'),
);
$items['admin/config/' . $sub_path . '/seo_meta/default'] = array(
'title' => 'Default meta tags',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
$items['admin/config/' . $sub_path . '/seo_meta/home'] = array(
'title' => 'Front page',
'type' => MENU_LOCAL_TASK,
'page arguments' => array('seo_meta_settings_homepage_form'),
'file' => 'seo_meta.admin.inc',
'access callback' => 'user_access',
'access arguments' => array('administer seo meta settings'),
'weight' => 2,
);
$items['admin/config/' . $sub_path . '/seo_meta/home/%'] = array(
'title' => 'Front page meta tags edit form',
'type' => MENU_LOCAL_TASK,
'page arguments' => array('seo_meta_custom_item_tags_edit'),
'file' => 'seo_meta.admin.inc',
'access arguments' => array('administer seo meta settings'),
);
$items['admin/config/' . $sub_path . '/seo_meta/views'] = array(
'title' => 'Views pages',
'type' => MENU_LOCAL_TASK,
'page arguments' => array('seo_meta_settings_views_form'),
'file' => 'seo_meta.admin.inc',
'access callback' => 'user_access',
'access arguments' => array('administer seo meta settings'),
'weight' => 3,
);
$items['admin/config/' . $sub_path . '/seo_meta/views/%'] = array(
'title' => 'Views page meta tags edit form',
'type' => MENU_LOCAL_TASK,
'page arguments' => array('seo_meta_custom_item_tags_edit'),
'file' => 'seo_meta.admin.inc',
'access arguments' => array('administer seo meta settings'),
);
if (module_exists('taxonomy')) {
$items['admin/config/' . $sub_path . '/seo_meta/taxonomy'] = array(
'title' => 'Taxonomy pages',
'type' => MENU_LOCAL_TASK,
'page arguments' => array('seo_meta_settings_taxonomy_form'),
'file' => 'seo_meta.admin.inc',
'access callback' => 'user_access',
'access arguments' => array('administer seo meta settings'),
'weight' => 4,
);
$items['admin/config/' . $sub_path . '/seo_meta/taxonomy/%'] = array(
'title' => 'Taxonomy page meta tags edit form',
'type' => MENU_LOCAL_TASK,
'page arguments' => array('seo_meta_custom_item_tags_edit'),
'file' => 'seo_meta.admin.inc',
'access arguments' => array('administer seo meta settings'),
);
}
return $items;
}
/**
* Implements hook_form_alter().
*
* Adds fieldset to the node create/edit form as vertical tab.
*
*/
function seo_meta_form_alter(&$form, $form_state, $form_id) {
$sub_path = seo_meta_menu_sub_path();
if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) {
$config = config('seo_meta.settings');
$excluded_types = $config->get('excluded_types');
foreach (array_keys($excluded_types, 0, true) as $key) {
unset($excluded_types[$key]);
}
if (array_key_exists($form['#node']->type, $excluded_types)) {
return;
}
$site_frontpage = config_get('system.core', 'site_frontpage');
$use_keywords = $config->get('use_keywords');
$form['#validate'][] = 'seo_meta_form_validate';
// edit existing node or add new
$nid = isset($form['nid']['#value']) ? $form['nid']['#value'] : NULL;
$saved_tags = isset($nid) ? seo_meta_get_saved_tags(TYPE_NODE, $nid) : NULL;
$form['seo_meta'] = array(
'#type' => 'fieldset',
'#title' => t('SEO meta tags'),
'#access' => user_access('administer seo meta settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
'#weight' => 99,
'#group' => 'additional_settings',
'#attached' => array(
'js' => array(
'vertical-tabs' => backdrop_get_path('module', 'seo_meta') . '/js/seo_meta.admin.js',
),
),
);
$form['seo_meta']['#attached']['css'] = array(
backdrop_get_path('module', 'seo_meta') . '/css/seo_meta.admin.css',
);
// Inform user if currently editing node is set as site front page
if (strpos($site_frontpage, 'node/') === 0 && $form['#node']->nid == substr($site_frontpage, 5)) {
$form['seo_meta']['note'] = array(
'#prefix' => '<div class="messages warning">',
'#suffix' => '</div>',
'#markup' => t('This node is set as site <a href="@front">front page</a>, '
. 'so for this page <a href="@link">front page meta tags</a> will be used.',
array('@link' => url('admin/config/' . $sub_path . '/seo_meta/home'),'@front' => url('admin/config/system/site-information'))),
);
}
else {
$form['seo_meta']['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Define tags for page of this node'),
'#default_value' => !empty($saved_tags),
'#description' => t('Optional. If not enabled, <a href="@url">default values</a> will be used', array('@url' => url('admin/config/' . $sub_path . '/seo_meta'))),
);
$form['seo_meta']['holder'] = array(
'#type' => 'container',
'#parents' => array('seo_meta'),
'#states' => array(
'invisible' => array(
'input[name="seo_meta[enabled]"]' => array('checked' => FALSE),
),
),
);
$form['seo_meta']['holder']['page_title'] = array(
'#type' => 'textfield',
'#title' => t('Page title'),
'#default_value' => isset($saved_tags['page_title']) ? $saved_tags['page_title'] : '',
'#description' => t('Description of this page to appear in the <title> tag for search engines search result listings.'),
);
if ($use_keywords) {
$form['seo_meta']['holder']['meta_keywords'] = array(
'#type' => 'textfield',
'#title' => t('Meta keywords'),
'#default_value' => isset($saved_tags['meta_keywords']) ? $saved_tags['meta_keywords'] : '',
'#size' => 120,
'#maxlength' => 255,
'#description' => t('Comma separated list of keywords (no more than 10 keyword phrases).'),
);
}
$form['seo_meta']['holder']['meta_description'] = array(
'#type' => 'textarea',
'#title' => t('Meta description'),
'#default_value' => isset($saved_tags['meta_description']) ? $saved_tags['meta_description'] : '',
'#rows' => 3,
'#description' => t('One or two sentence described this page (160-240 characters max. without any HTML markup.)'),
);
$form['seo_meta']['holder']['more'] = array(
'#type' => 'fieldset',
'#title' => t('More'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['seo_meta']['holder']['more']['meta_robots'] = array(
'#type' => 'checkboxes',
'#title' => t('Meta robots'),
'#options' => backdrop_map_assoc(array('NOINDEX', 'NOFOLLOW', 'NOARCHIVE', 'NOODP', 'NOYDIR', 'NOSNIPPET')),
'#default_value' => isset($saved_tags['meta_robots']) ? unserialize($saved_tags['meta_robots']) : array(),
'#description' => t('Search engines assume "INDEX" and "FOLLOW" unless these are specifically disabled above.'),
'#attributes' => array('class' => array('seo-meta')),
);
$form['seo_meta']['holder']['more']['rel_canonical'] = array(
'#type' => 'textfield',
'#title' => t('Canonical URL'),
'#default_value' => isset($saved_tags['rel_canonical']) ? $saved_tags['rel_canonical'] : '',
'#description' => t("Use a relative URL without the initial slash for current default domain or full URL."
. " By default (if no value) this will automatically use URL of this page."),
);
$form['seo_meta']['holder']['open_graph'] = array(
'#type' => 'fieldset',
'#title' => t('Open Graph'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['seo_meta']['holder']['open_graph']['meta_og_title'] = array(
'#type' => 'textfield',
'#title' => t('Open Graph title'),
'#default_value' => isset($saved_tags['meta_og_title']) ? $saved_tags['meta_og_title'] : '',
'#description' => t('The title of this page/article as rendered on Facebook (without any branding such as your site name.)'),
);
$form['seo_meta']['holder']['open_graph']['meta_og_description'] = array(
'#type' => 'textarea',
'#title' => t('Open Graph description'),
'#default_value' => isset($saved_tags['meta_og_description']) ? $saved_tags['meta_og_description'] : '',
'#rows' => 3,
'#description' => t('A brief description of the content, usually between 2 and 4 sentences (160-240 characters max. without any HTML markup). This will displayed below the title of the post on Facebook.'),
);
$form['seo_meta']['holder']['open_graph']['meta_og_type'] = array(
'#title' => t('Open Graph type'),
'#type' => 'select',
'#options' => array(
'' => t('Select type'),
'website' => 'website',
'article' => 'article',
'product' => 'product',
'profile' => 'profile',
),
'#default_value' => isset($saved_tags['meta_og_type']) ? $saved_tags['meta_og_type'] : '',
'#description' => t('The type of media of your content.'),
);
$form['seo_meta']['holder']['open_graph']['meta_og_image_fid'] = array(
'#title' => t('Upload image that appears when someone shares the content to Facebook.'),
'#type' => 'managed_file',
'#upload_location' => 'public://' . $config->get('meta_og_images_dir'),
'#default_value' => isset($saved_tags['meta_og_image_fid']) ? $saved_tags['meta_og_image_fid'] : '',
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array(file_upload_max_size()),
),
);
$form['seo_meta']['holder']['open_graph']['meta_og_site_name'] = array(
'#type' => 'textfield',
'#title' => t('Site name'),
'#default_value' => isset($saved_tags['meta_og_site_name']) ? $saved_tags['meta_og_site_name'] : '',
'#description' => t('The name of your website (such as IMDb, not imdb.com).'),
);
$form['seo_meta']['holder']['twitter'] = array(
'#type' => 'fieldset',
'#title' => t('Twitter'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['seo_meta']['holder']['twitter']['meta_tw_card'] = array(
'#title' => t('Twitter Card type'),
'#type' => 'select',
'#options' => array(
'' => t('Select card type'),
'summary' => 'Summary Card',
'summary_large_image' => 'Summary Card with Large Image',
'photo' => 'Photo Card',
'gallery' => 'Gallery Card',
'product' => 'Product Card',
),
'#default_value' => isset($saved_tags['meta_tw_card']) ? $saved_tags['meta_tw_card'] : '',
'#description' => t('The card type.'),
);
$form['seo_meta']['holder']['twitter']['meta_tw_site'] = array(
'#type' => 'textfield',
'#title' => t('Site'),
'#default_value' => isset($saved_tags['meta_tw_site']) ? $saved_tags['meta_tw_site'] : '',
'#description' => t('@username of website.'),
);
$form['seo_meta']['holder']['twitter']['meta_tw_creator'] = array(
'#type' => 'textfield',
'#title' => t('Creator'),
'#default_value' => isset($saved_tags['meta_tw_creator']) ? $saved_tags['meta_tw_creator'] : '',
'#description' => t('@username of content creator.'),
);
$form['seo_meta']['holder']['twitter']['div'] = array(
'#markup' => '<div class="description">' . t('Note: When the Twitter card processor looks for tags on your page,
it first checks for the Twitter property, and if not present,
falls back to the supported Open Graph property (for example site URL, title, description and image).
This allows for both to be defined on the page independently, and minimizes
the amount of duplicate markup required to describe your content and experience.') . '</div>',
);
}
}
}
/**
* Implements hook_validate() for node edit form.
*
* @todo per field validation.
*/
function seo_meta_form_validate($form, &$form_state) {
if (isset($form['#node']) && is_object($form['#node'])) {
if (isset($form_state['values']['seo_meta']['enabled']) && $form_state['values']['seo_meta']['enabled'] == 1) {
$seo_meta_values = $form_state['values']['seo_meta'];
$empty_tags = (
empty($seo_meta_values['page_title']) &&
empty($seo_meta_values['meta_keywords']) &&
empty($seo_meta_values['meta_description'])) ? TRUE : FALSE;
$checked_boxes = FALSE;
foreach ($seo_meta_values['more']['meta_robots'] as $tag => $value) {
if (is_string($value)) {
$checked_boxes = TRUE;
}
}
if ($empty_tags && !$checked_boxes) {
form_set_error('',
t('Please fill in some fields in the form "SEO meta tags" or uncheck checkbox "Define tags for page of this node".'));
}
if (backdrop_strlen($seo_meta_values['meta_description']) > 254) {
form_set_error('seo_meta][meta_description',
t('Text you trying fill in to field "Meta description" (under "SEO meta tags" tab) is too long!'));
}
if (backdrop_strlen($seo_meta_values['open_graph']['meta_og_description']) > 254) {
form_set_error('seo_meta][open_graph][meta_og_description',
t('Text you trying fill in to field "Open Graph description" (under "SEO meta tags" tab > "Open Graph") is too long!'));
}
}
}
}
/**
* Helper for collect data from node
*
* @param Node $node
* @return array $data (key => value when insert/update)
*/
function seo_meta_get_node_data(Node $node) {
$config = config('seo_meta.settings');
$keywords = $config->get('use_keywords') ? trim($node->seo_meta['meta_keywords']) : '';
return array(
'ce_type' => TYPE_NODE,
'ce_id' => $node->nid,
'path_alias' => trim($node->path['alias']),
'langcode' => isset($node->langcode) ? $node->langcode : LANGUAGE_NONE,
'page_title' => trim($node->seo_meta['page_title']),
'meta_keywords' => $keywords,
'meta_description' => trim($node->seo_meta['meta_description']),
'meta_robots' => serialize($node->seo_meta['more']['meta_robots']),
'rel_canonical' => trim($node->seo_meta['more']['rel_canonical']),
'meta_og_title' => trim($node->seo_meta['open_graph']['meta_og_title']),
'meta_og_description' => trim($node->seo_meta['open_graph']['meta_og_description']),
'meta_og_type' => $node->seo_meta['open_graph']['meta_og_type'],
'meta_og_image_fid' => $node->seo_meta['open_graph']['meta_og_image_fid'],
'meta_og_site_name' => trim($node->seo_meta['open_graph']['meta_og_site_name']),
'meta_tw_card' => $node->seo_meta['twitter']['meta_tw_card'],
'meta_tw_site' => trim($node->seo_meta['twitter']['meta_tw_site']),
'meta_tw_creator' => trim($node->seo_meta['twitter']['meta_tw_creator']),
'uid' => $node->uid,
'changed' => time(),
);
}
/**
* Implements hook_node_presave().
*/
function seo_meta_node_presave(Node $node) {
if (isset($node->seo_meta['enabled']) && $node->seo_meta['enabled'] == 1) {
// save OG image as permanent file
if (!empty($node->seo_meta['open_graph']['meta_og_image_fid'])) {
$file = file_load($node->seo_meta['open_graph']['meta_og_image_fid']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
file_usage_add($file, 'file', 'file', $file->fid);
}
}
}
/**
* Implements hook_node_insert().
*/
function seo_meta_node_insert(Node $node) {
if (isset($node->seo_meta['enabled']) && $node->seo_meta['enabled'] == 1) {
$data = seo_meta_get_node_data($node);
seo_meta_record_insert($data);
}
}
/**
* Implements hook_node_update().
*/
function seo_meta_node_update(Node $node) {
$result = db_select('seo_meta', 'sm')
->fields('sm')
->condition('ce_type', TYPE_NODE, '=')
->condition('ce_id', $node->nid, '=')
->execute()
->fetchObject();
$sm_id = is_object($result) ? $sm_id = $result->sm_id : NULL;
if (isset($node->seo_meta['enabled']) && $node->seo_meta['enabled'] == 1) {
$data = seo_meta_get_node_data($node);
if ($sm_id) {
seo_meta_record_update(TYPE_NODE, $node->nid, $data);
}
else {
seo_meta_record_insert($data);
}
}
else {
if ($sm_id) {
seo_meta_record_delete(TYPE_NODE, $node->nid);
}
}
}
/**
* Implements hook_node_predelete().
*/
function seo_meta_node_predelete(Node $node) {
seo_meta_record_delete(TYPE_NODE, $node->nid);
}
/**
* Implements hook_html_head_alter().
*/
function seo_meta_html_head_alter(&$head_elements) {
$head_elements = array_reverse($head_elements);
$config = config('seo_meta.settings');
$fix_favicon = $config->get('fix_favicon');
$tmp_arr = array();
foreach ($head_elements as $key => $element) {
if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
// deduplicate system canonical
$tmp_arr[] = $head_elements[$key];
if (count($tmp_arr)> 1) {
unset($head_elements[$key]);
}
}
if ($fix_favicon) {
if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'shortcut icon') {
$head_elements[$key]['#attributes']['type'] = 'image/x-icon';
$head_elements[$key]['#attributes']['rel'] = 'icon';
}
}
}
}
/**
*
* @global object $language
* @return object $caller or FALSE for admin path
*/
function seo_meta_caller() {
if (path_is_admin($_GET['q'])) {
return FALSE;
}
global $language;
$caller = new stdClass();
$caller->langcode = $language->langcode;
if (arg(0) == 'node' && is_numeric(arg(1))) {
$caller->type = TYPE_NODE;
$caller->id = arg(1);
}
if (arg(0) == 'taxonomy' && arg(1) == 'term' && arg(2)) {
$term_name = taxonomy_term_load(arg(2))->name;
$caller->type = TYPE_TAXONOMY;
$caller->id = $term_name;
}
if (function_exists('views_get_page_view') && views_get_page_view()){
$view_name = views_get_page_view()->name;
$caller->type = TYPE_VIEW;
$caller->id = $view_name;
}
// front(home)page as type should overwrite original content entry (node, view...) if it defined as FP
if (backdrop_is_front_page()) {
$caller->is_front = TRUE;
$caller->type = TYPE_HOMEPAGE;
$caller->id = $caller->langcode != 'und' ? $caller->langcode : '';
}
// fix for pages with undefined id and type, user login for example
$caller->id = isset($caller->id) ? $caller->id : $caller->langcode;
$caller->type = isset($caller->type) ? $caller->type : TYPE_DEFAULTS;
return $caller;
}
/**
* Get tags associated with content entry -
* node or other page.
*
* @params string $ce_type -
* content entry type code
* @params string $ce_id
* content entry type id
* @return array of tags or false
*
*/
function seo_meta_get_saved_tags($ce_type, $ce_id) {
$result = db_select('seo_meta', 'sm')
->fields('sm', array(
'page_title',
'meta_keywords',
'meta_description',
'meta_robots',
'rel_canonical',
'meta_tw_card',
'meta_tw_site',
'meta_tw_creator',
'meta_og_type',
'meta_og_title',
'meta_og_description',
'meta_og_site_name',
'meta_og_image_fid',
)
)
->condition('ce_type', $ce_type, '=')
->condition('ce_id', $ce_id, '=')
->execute()
->fetchAssoc();
return $result;
}
/**
* Get saved default tags
*
* @params string langcode
*
* @return array of tags or false
*
*/
function seo_meta_get_saved_defaults($langcode) {
$result = db_select('seo_meta', 'sm')
->fields('sm', array(
'meta_keywords',
'meta_description',
'meta_robots',
'meta_tw_card',
'meta_tw_site',
'meta_tw_creator',
'meta_og_type',
'meta_og_title',
'meta_og_description',
'meta_og_site_name',
'meta_og_image_fid',
)
)
->condition('ce_type', TYPE_DEFAULTS, '=')
->condition('ce_id', $langcode, '=') // CHECK !!!!
->execute()
->fetchAssoc();
return $result;
}
/**
* Insert to table new content entry tags
*
* @param array $data
*/
function seo_meta_record_insert($data = array()) {
db_insert('seo_meta')
->fields($data)
->execute();
}
/**
* Update content entry tags
*
* @param string $ce_type
* @param string $ce_id
* @param array $data
*/
function seo_meta_record_update($ce_type = '', $ce_id = '', $data = array()) {
db_update('seo_meta')
->fields($data)
->condition('ce_type', $ce_type, '=')
->condition('ce_id', $ce_id, '=')
->execute();
}
/**
* Remove content entry tags
*
* @param string $ce_type
* @param string $ce_id
*/
function seo_meta_record_delete($ce_type = '', $ce_id = '') {
$result = db_select('seo_meta', 'sm')
->fields('sm')
->condition('ce_type', $ce_type, '=')
->condition('ce_id', $ce_id, '=')
->execute()
->fetchObject();
if (is_object($result)) {
$sm_id = $result->sm_id;
$sm_fid = $result->meta_og_image_fid;
if (!empty($sm_fid)) {
file_delete($sm_fid);
}
db_delete('seo_meta')
->condition('sm_id', $sm_id)
->execute();
}
}
/**
* Prepare and add to render saved or default meta tags
*
*/
function seo_meta_build_page_tags() {
$secure = config_get('system.core', 'canonical_secure') == 1 ? TRUE : FALSE;
$caller = seo_meta_caller();
$weight = TAG_WEIGHT;
if (!$caller) {
return;
}
$saved_tags = seo_meta_get_saved_tags($caller->type, $caller->id);
if (!empty($saved_tags)) {
$tags = $saved_tags;
}
else {
$tags = seo_meta_get_saved_defaults($caller->langcode);
}
if (!empty($tags)) {
foreach ($tags as $tag => $value) {
if (!empty($value)) {
$excluded_tags = array('page_title', 'rel_canonical');
if (in_array($tag, $excluded_tags)) {
continue;
}
if ($tag == 'meta_robots') {
$array_meta_robots = unserialize($value);
$meta_robots = '';
foreach ($array_meta_robots as $key => $name) {
if ($name) {
$meta_robots .= strtolower($key) . ', ';
}
}
$value = rtrim($meta_robots, ', ');
if ($value == '') {
continue;
}
}
if ($tag == 'meta_og_image_fid') {
$image = file_load($value);
$value = is_object($image) ? file_create_url($image->uri) : '';
if (!empty($value) && $secure) {
$value = str_replace('http://', 'https://', $value);
}
$attribute = 'property';
$tag = str_replace('_fid', '', $tag);
}
if (strpos($tag, 'meta_og') === FALSE) {
$attribute = 'name';
if (strpos($tag, 'meta_tw') === FALSE) {
$weight = $weight + 10;
$tag = str_replace('meta_', '', $tag);
}
else {
$weight = $weight + 11;
$tag = str_replace('meta_tw_', 'twitter:', $tag);
}
}
else {
$attribute = 'property';
$weight = $weight + 75;
$tag = str_replace('meta_og_', 'og:', $tag);
}
$element = array(
'#tag' => 'meta',
'#attributes' => array(
$attribute => $tag,
'content' => $value,
),
'#weight' => $weight,
);
backdrop_add_html_head($element, 'seo_meta_' . $tag);
}
}
}
}
function seo_meta_preprocess_page(&$variables) {
// not for admin pages
if (path_is_admin($_GET['q'])) {
return FALSE;
}
global $base_url;
$is_node_page = arg(0) == 'node' && is_numeric(arg(1));
$status = backdrop_get_http_header('status');
// awoid non-custom http status errors pages
if (in_array($status, array('404 Not Found', '403 Forbidden')) && !$is_node_page) {
return FALSE;
}
$config = config('seo_meta.settings');
$use_og_url = $config->get('use_og_url');
$caller = seo_meta_caller();
$caller_type = is_object($caller) ? $caller->type : NULL;
$seo_meta_title = is_object($caller) ? seo_meta_get_tag_value('page_title', $caller->type, $caller->id) : '';
$head_site_name = $variables['head_title_array']['name'];
// Add page title
if (!empty($seo_meta_title) && $seo_meta_title != 'und') {
$page_title = $seo_meta_title;
}
else {
$page_title = isset($variables['head_title_array']['title']) ? $variables['head_title_array']['title'] : '';
}
if (!empty($page_title)) {
if ($config->get('append_site_name')) {
$site_name_divider = $config->get('site_name_divider');
$variables['head_title'] = $page_title . ' ' . $site_name_divider . ' ' . $head_site_name;
}
else {
$variables['head_title'] = $page_title;
}
}
// Add canonical URL to views and homepages overriding
if ($caller_type == TYPE_VIEW) {
$view_url = url(current_path(), array('absolute' => TRUE));
$canonical_url = seo_meta_canonical_url($view_url);
}
if ($caller_type == TYPE_HOMEPAGE) {
$canonical_url = seo_meta_canonical_url($base_url);
}
if (!empty($canonical_url)) {
backdrop_add_html_head_link(array('rel' => 'canonical', 'href' => $canonical_url));
}
// add og:url if selected in default settings
if ($use_og_url) {
$current_path = current_path();
$secure = config_get('system.core', 'canonical_secure') == 1 ? TRUE : FALSE;
if ($caller_type == TYPE_HOMEPAGE) {
$current_url = url('/', array('absolute' => TRUE, 'https' => $secure));
}
else {
$current_url = url($current_path, array('absolute' => TRUE, 'https' => $secure));
}
backdrop_add_html_head(
array(
'#tag' => 'meta',
'#attributes' => array(
'property' => 'og:url',
'content' => $current_url,
),
'#weight' => TAG_WEIGHT + 65,
),
'seo_meta_url'
);
}
// Add rest of meta tags
seo_meta_build_page_tags();
}
/**
* Redefine system canonical if needed
*
* @return string $canonical_url
*/
function seo_meta_canonical_url($system_canonical = '') {
global $base_url;
$canonical_secure = config_get('system.core', 'canonical_secure');
$proto = $canonical_secure ? 'https://' : 'http://';
$path = parse_url($system_canonical, PHP_URL_PATH);
$host = parse_url($base_url, PHP_URL_HOST);
$base_canonical_url = rtrim(config_get('seo_meta.settings', 'base_canonical_url'), '/');
$caller = seo_meta_caller();
// If base canonical URL is not defined by default
if ($base_canonical_url == '') {
$base_canonical_url = $host;
}
$canonical_url = $proto . $base_canonical_url . $path;
// Check if canonical URL redefined in content entry
if (is_object($caller)) {
$rel_canonical = seo_meta_get_tag_value('rel_canonical', $caller->type, $caller->id);
if ($rel_canonical != '') {
$canonical_url = strpos($rel_canonical, 'http') !== 0 ? $proto . $base_canonical_url . '/' . $rel_canonical : $rel_canonical;
}
}
return $canonical_url;
}
/**
* Helper for getting value of page title
* or canonical URL for content entry
*
* @param string $tag_name
* @param string $ce_type
* @param string $ce_id
* @return string
*/
function seo_meta_get_tag_value($tag_name = '', $ce_type = '', $ce_id = '') {
$tag_value = '';
$result = db_select('seo_meta', 'sm')
->fields('sm', array($tag_name))
->condition('ce_type', $ce_type, '=')
->condition('ce_id', $ce_id, '=')
->execute()
->fetchField();
if (!empty($result)) {
$tag_value = $result;
}
return $tag_value;
}
/**
* Helper for BD >= 1.6.0 - menu sub-path was changed from 'search' to 'metadata;
* @return string
*/
function seo_meta_menu_sub_path() {
$sub_path = 'search';
$backdrop_version = explode('.', BACKDROP_VERSION);
if ($backdrop_version[0] == 1 && $backdrop_version[1] > 5) {
$sub_path = 'metadata';
}
return $sub_path;
}