tweetcards.php
4.2 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
<?php
/**
* Content Plugin for Joomla! - Tweet Cards
*
* @author JoomlaTemplat.es <htmgarcia@gmail.com>
* @copyright Copyright 2016 JoomlaTemplat.es
* @license GNU Public License version 3 or later
* @link https://joomlatemplat.es/extensions/tweet-cards
*/
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
/**
* Class PlgContentTweetcards
*
* @since 0.0.1
*/
class PlgContentTweetcards extends JPlugin
{
/**
* Event method onContentBeforeDisplay
*
* @param string $context The context of the content being passed to the plugin
* @param mixed &$row An object with a "text" property
* @param mixed &$params Additional parameters
* @param int $page Optional page number
*
* @return null
*
* @since 0.0.1
*/
public function onContentBeforeDisplay($context, &$row, &$params, $page = 0)
{
$twitteraccount = $this->params->get('twitteraccount', '@htmgarcia');
$type = $this->params->get('type', 'summary');
// カテゴリページも追加
if ($context == 'com_content.article' || $context == 'com_content.category') {
$this->setMetadata (
$this->setMetatitle(
$row->metadata,
$row->title == 'home' ? '鹿屋体育大学 - ホーム|鹿屋体育大学' : '鹿屋体育大学 - ' . $row->title . '|鹿屋体育大学'
),
$this->setImage(
$row->images,
$row->text,
$context,
$row->category_alias
),
$this->setMetadesc(
$row->metadesc,
$row->title == 'home' ? '鹿屋体育大学(National Institute of Fitness and Sports in KANOYA)は鹿児島県鹿屋(かのや)市に本部を置く全国で唯一の国立体育大学です' : $row->text
),
$twitteraccount,
$type
);
}
}
/**
* Event method that create the open graph meta tags
*
* @param string $title The article's title
*
* @since 0.0.1
*/
protected function setMetadata ($title, $image, $metadesc, $twitteraccount, $type) {
$doc = JFactory::getDocument();
$doc->setMetaData( 'twitter:card', $type );
$doc->setMetaData( 'twitter:site', $twitteraccount );
$doc->setMetaData( 'twitter:title', $title );
$doc->setMetaData( 'twitter:description', $metadesc );
$doc->setMetaData( 'twitter:image', $image );
}
/**
* Extract article's image
*
* @return string
*
* @since 0.0.1
*/
protected function setImage($images, $text, $context, $category_alias) {
$fullImage = json_decode($images);
preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $text, $matches);
if ( !empty($fullImage->image_fulltext) ) {
$image = JURI::base() . $fullImage->image_fulltext;
// 本文中の画像の自動選択をカテゴリ:インフォメーションのみ有効化
} elseif( $category_alias == 'target-info' && $context == 'com_content.article' && !empty($matches[ 1 ][ 0 ]) ) {
$image = $matches[ 1 ][ 0 ];
$image = str_replace(JURI::base(), '', $image);
$image = JURI::base() . $image;
} else {
$image = '';
}
return $image;
}
/**
* Extract article's meta description
*
* @return string
*
* @since 0.0.1
*/
protected function setMetadesc($metadesc, $text) {
if($metadesc) {
$metadesc = $metadesc;
} else {
$metadesc = substr( strip_tags($text), 0, 157 ) . '...';
}
return $metadesc;
}
/**
* Extract article's meta title
*
* @return string
*
* @since 0.0.1
*/
protected function setMetatitle($metadata, $title) {
$metadata = json_decode($metadata);
if( !empty($metadata->metatitle) ) {
$metatitle = $metadata->metatitle;
} else {
$metatitle = htmlspecialchars($title);
}
return $metatitle;
}
}