XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
seo.class.php
Go to the documentation of this file.
1 <?php
2 class seo extends ModuleObject
3 {
4  public $SEO = array(
5  'link' => array(),
6  'meta' => array()
7  );
8 
9  protected $canonical_url;
10 
11  private $triggers = array(
12  array('display', 'seo', 'controller', 'triggerBeforeDisplay', 'before'),
13  array('file.deleteFile', 'seo', 'controller', 'triggerAfterFileDeleteFile', 'after'),
14  array('document.updateDocument', 'seo', 'controller', 'triggerAfterDocumentUpdateDocument', 'after'),
15  array('document.deleteDocument', 'seo', 'controller', 'triggerAfterDocumentDeleteDocument', 'after'),
16  array('module.dispAdditionSetup', 'seo', 'view', 'triggerDispSeoAdditionSetup', 'before')
17  );
18 
19  public function getConfig()
20  {
21  $oModuleModel = getModel('module');
22  $config = $oModuleModel->getModuleConfig('seo');
23 
24  require_once(_XE_PATH_ . 'libs/idna_convert/idna_convert.class.php');
25  $IDN = new idna_convert(array('idn_version' => 2008));
26  $request_uri = $IDN->encode(Context::get('request_uri'));
27 
28  if (!$config) $config = new stdClass;
29  if (!$config->enable) $config->enable = 'Y';
30  if (!$config->use_optimize_title) $config->use_optimize_title = 'N';
31  if (!$config->ga_except_admin) $config->ga_except_admin = 'N';
32  if (!$config->ga_track_subdomain) $config->ga_track_subdomain = 'N';
33  if ($config->site_image)
34  {
35  $config->site_image_url = $request_uri . 'files/attach/site_image/' . $config->site_image;
36 
37  $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
38  if($oCacheHandler->isSupport()) {
39  $site_image = false;
40  $cache_key = 'seo:site_image';
41  $site_image = $oCacheHandler->get($cache_key);
42  if(!$site_image) {
43  $path = _XE_PATH_ . 'files/attach/site_image/';
44  list($width, $height) = @getimagesize($path . $config->site_image);
45  $site_image_dimension = array(
46  'width' => $width,
47  'height' => $height
48  );
49  $cache_key = 'seo:site_image';
50  $oCacheHandler->put($cache_key, $site_image_dimension);
51  }
52  }
53  }
54 
55  return $config;
56  }
57 
58  public function addMeta($property, $content, $attr_name = 'property')
59  {
60  if (!$content) return;
61 
63  $oModuleController->replaceDefinedLangCode($content);
64  if (!in_array($property, array('og:url'))) {
65  $content = htmlspecialchars($content);
66  $content = preg_replace("/(\s+)/", ' ', $content);
67  }
68 
69  $this->SEO['meta'][] = array('property' => $property, 'content' => $content, 'attr_name' => $attr_name);
70  }
71 
72  public function addLink($rel, $href)
73  {
74  if (!$href) return;
75 
76  $this->SEO['link'][] = array('rel' => $rel, 'href' => $href);
77  }
78 
79  protected function applySEO()
80  {
81  $config = $this->getConfig();
82  $logged_info = Context::get('logged_info');
83 
84  foreach ($this->SEO as $type => $list) {
85  if (!$list || !count($list)) continue;
86 
87  foreach ($list as $val) {
88  if ($type == 'meta') {
89  $attr_name = $val['attr_name'];
90  Context::addHtmlHeader('<meta ' . $attr_name . '="' . $val['property'] . '" content="' . $val['content'] . '" />');
91  } elseif ($type == 'link') {
92  Context::addHtmlHeader('<link rel="' . $val['rel'] . '" href="' . $val['href'] . '" />');
93  }
94  }
95  }
96 
97  // Google Analytics
98  if ($config->ga_id && !($config->ga_except_admin == 'Y' && $logged_info->is_admin == 'Y')) {
99  $gaq_push = array();
100  // $gaq_push[] = '_gaq.push([\'_setAccount\', \'' . $config->ga_id . '\']);';
101  $gaq_push[] = "ga('create', '{$config->ga_id}', 'auto');";
102  $canonical_url = str_replace(Context::get('request_uri'), '/', $this->canonical_url);
103  $gaq_push[] = "ga('send', 'pageview', '{$canonical_url}');";
104  $gaq_push = implode(PHP_EOL, $gaq_push);
105 
106  $ga_script = <<< GASCRIPT
107 <!-- Google Analytics -->
108 <script>
109 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
110 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
111 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
112 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
113 
114 {$gaq_push}
115 </script>
116 GASCRIPT;
117 
118  Context::addHtmlHeader($ga_script . PHP_EOL);
119  }
120 
121  // Naver Analytics
122  if ($config->na_id && !($config->na_except_admin == 'Y' && $logged_info->is_admin == 'Y')) {
123  $na_script = <<< NASCRIPT
124 <!-- NAVER Analytics -->
125 <script src="//wcs.naver.net/wcslog.js"></script>
126 <script>if(!wcs_add){var wcs_add={};};wcs_add['wa']='{$config->na_id}';if(typeof wcs_do!="undefined"){wcs_do();}</script>
127 NASCRIPT;
128  Context::addHtmlFooter($na_script . PHP_EOL);
129  }
130  }
131 
132  function moduleInstall()
133  {
134  return $this->makeObject();
135  }
136 
137  function checkUpdate()
138  {
139  $oModuleModel = getModel('module');
140 
141  $seo_config = $this->getConfig();
142 
143  if($seo_config->enable === 'Y') {
144  foreach ($this->triggers as $trigger) {
145  if (!$oModuleModel->getTrigger($trigger[0], $trigger[1], $trigger[2], $trigger[3], $trigger[4])) return TRUE;
146  }
147  }
148 
149  return FALSE;
150  }
151 
152  function moduleUpdate()
153  {
154  $oModuleModel = getModel('module');
155  $oModuleController = getController('module');
156 
157  $seo_config = $this->getConfig();
158 
159  if($seo_config->enable === 'Y') {
160  foreach ($this->triggers as $trigger) {
161  if (!$oModuleModel->getTrigger($trigger[0], $trigger[1], $trigger[2], $trigger[3], $trigger[4])) {
162  $oModuleController->insertTrigger($trigger[0], $trigger[1], $trigger[2], $trigger[3], $trigger[4]);
163  }
164  }
165  }
166 
167  return $this->makeObject(0, 'success_updated');
168  }
169 
170  function moduleUninstall()
171  {
172  $oModuleController = getController('module');
173 
174  foreach ($this->triggers as $trigger) {
175  $oModuleController->deleteTrigger($trigger[0], $trigger[1], $trigger[2], $trigger[3], $trigger[4]);
176  }
177 
178  return $this->makeObject();
179  }
180 
181  public function makeObject($code = 0, $message = 'success')
182  {
183  return class_exists('BaseObject') ? new BaseObject($code, $message) : new Object($code, $message);
184  }
185 }
186 /* !End of file */
$oModuleModel
Definition: ko.install.php:236
getConfig()
Definition: seo.class.php:19
$IDN
Definition: example.php:7
getController($module_name)
Definition: func.inc.php:90
addHtmlHeader($header)
& getInstance($target= 'object', $info=null, $always_use_file=false)
checkUpdate()
Definition: seo.class.php:137
makeObject($code=0, $message= 'success')
Definition: seo.class.php:181
$canonical_url
Definition: seo.class.php:9
addLink($rel, $href)
Definition: seo.class.php:72
moduleUpdate()
Definition: seo.class.php:152
document the module&#39;s high class {
const _XE_PATH_
Definition: config.inc.php:49
getModel($module_name)
Definition: func.inc.php:145
applySEO()
Definition: seo.class.php:79
Definition: seo.class.php:2
moduleInstall()
Definition: seo.class.php:132
$oModuleController
Definition: ko.install.php:287
$SEO
Definition: seo.class.php:4
addHtmlFooter($footer)
moduleUninstall()
Definition: seo.class.php:170
addMeta($property, $content, $attr_name= 'property')
Definition: seo.class.php:58