PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/community/Fishpig/Wordpress/Addon/WordPressSEO/Helper/Data.php

https://gitlab.com/hennesfinest/wordpress-integration
PHP | 497 lines | 300 code | 87 blank | 110 comment | 48 complexity | 6ba33be2f05abdbca18ed0e61741851d MD5 | raw file
  1. <?php
  2. /**
  3. * @category Fishpig
  4. * @package Fishpig_Wordpress
  5. * @license http://fishpig.co.uk/license.txt
  6. * @author Ben Tideswell <help@fishpig.co.uk>
  7. */
  8. class Fishpig_Wordpress_Addon_WordPressSEO_Helper_Data extends Fishpig_Wordpress_Helper_Plugin_Seo_Abstract
  9. {
  10. /**
  11. * A list of option fields used by the extension
  12. * All fields are prefixed with wpseo_
  13. *
  14. * @var array
  15. */
  16. protected $_optionFields = array('', 'titles', 'xml', 'social', 'rss', 'internallinks');
  17. /**
  18. * The value used to separate token's in the title
  19. *
  20. * @var string
  21. */
  22. protected $_rewriteTitleToken = '%%';
  23. /**
  24. * Automatically load the plugin options
  25. *
  26. */
  27. protected function _construct()
  28. {
  29. parent::_construct();
  30. $data = array();
  31. foreach($this->_optionFields as $key) {
  32. if ($key !== '') {
  33. $key = '_' . $key;
  34. }
  35. $options = Mage::helper('wordpress')->getWpOption('wpseo' . $key);
  36. if ($options) {
  37. $options = unserialize($options);
  38. foreach($options as $key => $value) {
  39. if (strpos($key, '-') !== false) {
  40. unset($options[$key]);
  41. $options[str_replace('-', '_', $key)] = $value;
  42. }
  43. }
  44. $data = array_merge($data, $options);
  45. }
  46. }
  47. $this->setData($data);
  48. }
  49. /**
  50. * Determine whether All In One SEO is enabled
  51. *
  52. * @return bool
  53. */
  54. public function isEnabled()
  55. {
  56. return Mage::helper('wordpress')->isPluginEnabled('Wordpress SEO');
  57. }
  58. /**
  59. * Perform global actions after the user_func has been called
  60. *
  61. * @return $this
  62. */
  63. protected function _afterObserver()
  64. {
  65. $headBlock = $this->_getHeadBlock();
  66. $robots = array();
  67. if ($this->getNoodp()) {
  68. $robots[] = 'noodp';
  69. }
  70. if ($this->getNoydir()) {
  71. $robots[] = 'noydir';
  72. }
  73. if (count($robots) > 0) {
  74. if ($headBlock->getRobots() === '*') {
  75. $headBlock->setRobots('index,follow,' . implode(',', $robots));
  76. }
  77. else {
  78. $robots = array_unique(array_merge(explode(',', $headBlock->getRobots()), $robots));
  79. $headBlock->setRobots(implode(',', $robots));
  80. }
  81. }
  82. $this->_updateBreadcrumb('blog', $this->getBreadcrumbsHome());
  83. return $this;
  84. }
  85. /**
  86. * Process the SEO values for the homepage
  87. *
  88. * @param $action
  89. * @param Varien_Object $object
  90. */
  91. public function processRouteWordPressIndexIndex($object = null)
  92. {
  93. if (($headBlock = $this->_getHeadBlock()) !== false) {
  94. $this->_applyMeta(array(
  95. 'title' => $this->_getTitleFormat('home'),
  96. 'description' => trim($this->getMetadescHome()),
  97. 'keywords' => trim($this->getMetakeyHome()),
  98. ));
  99. if ($this->getPlusAuthor()) {
  100. $this->_addGooglePlusLinkRel($this->getPlusAuthor());
  101. }
  102. }
  103. return $this;
  104. }
  105. /**
  106. * Process the SEO values for the blog view page
  107. *
  108. * @param $action
  109. * @param Varien_Object $post
  110. */
  111. public function processRouteWordPressPostView($post)
  112. {
  113. $this->_applyPostPageLogic($post);
  114. return $this;
  115. }
  116. /**
  117. * Process the SEO values for the blog view page
  118. *
  119. * @param $action
  120. * @param Varien_Object $page
  121. */
  122. public function processRouteWordPressPageView($page)
  123. {
  124. $this->_applyPostPageLogic($page, 'page');
  125. return $this;
  126. }
  127. /**
  128. * Process the SEO values for the blog view page
  129. *
  130. * @param Varien_Object $object
  131. * @param string $type
  132. * @param Varien_Object $page
  133. */
  134. protected function _applyPostPageLogic($object, $type = 'post')
  135. {
  136. $meta = new Varien_Object(array(
  137. 'title' => $this->_getTitleFormat($type),
  138. 'description' => trim($this->getData('metadesc_' . $type)),
  139. 'keywords' => trim($this->getData('metakey_' . $type)),
  140. ));
  141. if (($value = trim($object->getMetaValue('_yoast_wpseo_title'))) !== '') {
  142. $data = $this->getRewriteData();
  143. $data['title'] = $value;
  144. $this->setRewriteData($data);
  145. }
  146. if (($value = trim($object->getMetaValue('_yoast_wpseo_metadesc'))) !== '') {
  147. $meta->setDescription($value);
  148. }
  149. if (($value = trim($object->getMetaValue('_yoast_wpseo_metakeywords'))) !== '') {
  150. $meta->setKeywords($value);
  151. }
  152. $robots = array();
  153. $noIndex = (int)$object->getMetaValue('_yoast_wpseo_meta-robots-noindex');
  154. if ($noIndex === 0) {
  155. $robots['index'] = '';
  156. }
  157. else if ($noIndex === 1) {
  158. $robots['noindex'] = '';
  159. }
  160. else if ($noIndex === 2) {
  161. $robots['index'] = '';
  162. }
  163. else if ($this->getNoindexPost()) {
  164. $robots['noindex'] = '';
  165. }
  166. if ($object->getMetaValue('_yoast_wpseo_meta-robots-nofollow')) {
  167. $robots['nofollow'] = '';
  168. }
  169. else {
  170. $robots['follow'] = '';
  171. }
  172. if (($advancedRobots = trim($object->getMetaValue('_yoast_wpseo_meta-robots-adv'))) !== '') {
  173. if ($advancedRobots !== 'none') {
  174. $robots = explode(',', $advancedRobots);
  175. }
  176. }
  177. $robots = array_keys($robots);
  178. if (count($robots) > 0) {
  179. $meta->setRobots(implode(',', $robots));
  180. }
  181. $this->_applyMeta($meta->getData());
  182. if (($headBlock = $this->_getHeadBlock()) !== false) {
  183. if ($canon = $object->getMetaValue('_yoast_wpseo_canonical')) {
  184. $headBlock->removeItem('link_rel', $object->getUrl());
  185. $headBlock->addItem('link_rel', $canon, 'rel="canonical"');
  186. }
  187. $this->_addGooglePlusLinkRel($object->getAuthor());
  188. }
  189. if ($this->getOpengraph() === 'on') {
  190. $this->_addOpenGraphTags($object, $type);
  191. }
  192. return $this;
  193. }
  194. /**
  195. * Add the open graph tags to the post/page
  196. *
  197. * @object
  198. * @param string $type = 'post'
  199. * @return
  200. */
  201. protected function _addOpenGraphTags($object, $type = 'post')
  202. {
  203. if (($head = Mage::getSingleton('core/layout')->getBlock('head')) !== false) {
  204. $tags = array(
  205. 'site_name' => Mage::helper('wordpress')->getWpOption('blogname'),
  206. 'type' => 'blog',
  207. 'url' => $object->getPermalink(),
  208. 'type' => 'article',
  209. 'title' => $object->getPostTitle(),
  210. 'description' => $head->getDescription(),
  211. );
  212. if ($object->getFeaturedImage()) {
  213. $image = $object->getFeaturedImage();
  214. $tags['image'] = $image->getLargeImage() ? $image->getLargeImage() : $image->getAvailableImage();
  215. }
  216. foreach($tags as $key => $value) {
  217. if (trim($value) !== '') {
  218. $tags[$key] = sprintf('<meta property="og:%s" content="%s" />', $key, addslashes(Mage::helper('core')->escapeHtml($value)));
  219. }
  220. else {
  221. unset($tags[$key]);
  222. }
  223. }
  224. $head->setChild('wp.openGraph',
  225. Mage::getSingleton('core/layout')->createBlock('core/text')->setText(implode("\n", $tags) . "\n")
  226. );
  227. }
  228. }
  229. /**
  230. * Category page
  231. *
  232. * @param $action
  233. * @param Varien_Object $category
  234. */
  235. public function processRouteWordpressPostCategoryView($category)
  236. {
  237. $meta = new Varien_Object(array(
  238. 'title' => $this->_getTitleFormat('category'),
  239. 'description' => $this->getMetadescCategory(),
  240. 'keywords' => $this->getMetakeyCategory(),
  241. 'robots' => $this->getNoindexCategory() ? 'noindex,follow' : '',
  242. ));
  243. $this->_applyMeta($meta->getData());
  244. return $this;
  245. }
  246. /**
  247. * Archive page
  248. *
  249. * @param $action
  250. * @param Varien_Object $archive
  251. */
  252. public function processRouteWordpressArchiveView($archive)
  253. {
  254. if ($this->getDisableDate()) {
  255. $this->_redirect(Mage::helper('wordpress')->getBlogRoute());
  256. }
  257. $meta = new Varien_Object(array(
  258. 'title' => $this->_getTitleFormat('archive'),
  259. 'description' => $this->getMetadescArchive(),
  260. 'keywords' => $this->getMetakeyArchive(),
  261. 'robots' => $this->getNoindexArchive() ? 'noindex,follow' : '',
  262. ));
  263. $this->_applyMeta($meta->getData());
  264. $this->_updateBreadcrumb('archive_label', $this->getBreadcrumbsArchiveprefix());
  265. return $this;
  266. }
  267. /**
  268. * Author page
  269. *
  270. * @param $action
  271. * @param Varien_Object $author
  272. */
  273. public function processRouteWordpressAuthorView($author)
  274. {
  275. if ($this->getDisableAuthor()) {
  276. $this->_redirect(Mage::helper('wordpress')->getBlogRoute());
  277. }
  278. $meta = new Varien_Object(array(
  279. 'title' => $this->_getTitleFormat('author'),
  280. 'description' => $this->getMetadescAuthor(),
  281. 'keywords' => $this->getMetakeyAuthor(),
  282. 'robots' => $this->getNoindexAuthor() ? 'noindex,follow' : '',
  283. ));
  284. $this->_applyMeta($meta->getData());
  285. $this->_addGooglePlusLinkRel($author);
  286. return $this;
  287. }
  288. /**
  289. * Tag page
  290. *
  291. * @param $action
  292. * @param Varien_Object $tag
  293. */
  294. public function processRouteWordpressPostTagView($tag)
  295. {
  296. $meta = new Varien_Object(array(
  297. 'title' => $this->_getTitleFormat('post_tag'),
  298. 'description' => $this->getMetadescPostTag(),
  299. 'keywords' => $this->getMetakeyPostTag(),
  300. 'robots' => $this->getNoindexPostTag() ? 'noindex,follow' : '',
  301. ));
  302. $this->_applyMeta($meta->getData());
  303. return $this;
  304. }
  305. /**
  306. * Process the search results page
  307. *
  308. * @param $action
  309. * @param $object
  310. */
  311. public function processRouteWordpressSearchIndex($object = null)
  312. {
  313. $meta = new Varien_Object(array(
  314. 'title' => $this->_getTitleFormat('search'),
  315. ));
  316. $this->_applyMeta($meta->getData());
  317. $this->_updateBreadcrumb('search_label', $this->getBreadcrumbsSearchprefix());
  318. return $this;
  319. }
  320. /**
  321. * Retrieve the rewrite data
  322. *
  323. * @return array
  324. */
  325. public function getRewriteData()
  326. {
  327. if (!$this->hasRewriteData()) {
  328. $data = array(
  329. 'sitename' => Mage::helper('wordpress')->getWpOption('blogname'),
  330. 'sitedesc' => Mage::helper('wordpress')->getWpOption('blogdescription'),
  331. );
  332. if (($object = Mage::registry('wordpress_post')) !== null || ($object = Mage::registry('wordpress_page')) !== null) {
  333. $data['date'] = $object->getPostDate();
  334. $data['title'] = $object->getPostTitle();
  335. $data['excerpt'] = trim(strip_tags($object->getPostExcerpt()));
  336. $data['excerpt_only'] = $data['excerpt'];
  337. $categories = array();
  338. if ($object instanceof Fishpig_Wordpress_Model_Post) {
  339. foreach($object->getParentCategories()->load() as $category) {
  340. $categories[] = $category->getName();
  341. }
  342. }
  343. $data['category'] = implode(', ', $categories);
  344. $data['modified'] = $object->getPostModified();
  345. $data['id'] = $object->getId();
  346. $data['name'] = $object->getAuthor()->getUserNicename();
  347. $data['userid'] = $object->getAuthor()->getId();
  348. }
  349. if (($category = Mage::registry('wordpress_category')) !== null) {
  350. $data['category_description'] = trim(strip_tags($category->getDescription()));
  351. $data['term_description'] = $data['category_description'];
  352. $data['term_title'] = $category->getName();
  353. }
  354. if (($tag = Mage::registry('wordpress_post_tag')) !== null) {
  355. $data['tag_description'] = trim(strip_tags($tag->getDescription()));
  356. $data['term_description'] = $data['tag_description'];
  357. $data['term_title'] = $tag->getName();
  358. }
  359. if (($term = Mage::registry('wordpress_term')) !== null) {
  360. $data['term_description'] = trim(strip_tags($term->getDescription()));
  361. $data['term_title'] = $term->getName();
  362. }
  363. if (($archive = Mage::registry('wordpress_archive')) !== null) {
  364. $data['date'] = $archive->getName();
  365. }
  366. if (($author = Mage::registry('wordpress_author')) !== null) {
  367. $data['name'] = $author->getDisplayName();
  368. }
  369. $data['currenttime'] = Mage::helper('wordpress')->formatTime(date('Y-m-d H:i:s'));
  370. $data['currentdate'] = Mage::helper('wordpress')->formatDate(date('Y-m-d H:i:s'));
  371. $data['currentmonth'] = date('F');
  372. $data['currentyear'] = date('Y');
  373. $data['sep'] = '|';
  374. if (($value = trim(Mage::helper('wordpress/router')->getSearchTerm(true))) !== '') {
  375. $data['searchphrase'] = $value;
  376. }
  377. $this->setRewriteData($data);
  378. }
  379. return $this->_getData('rewrite_data');
  380. }
  381. /**
  382. * Retrieve the title format for the given key
  383. *
  384. * @param string $key
  385. * @return string
  386. */
  387. protected function _getTitleFormat($key)
  388. {
  389. return trim($this->getData('title_' . $key));
  390. }
  391. /**
  392. * Add the Google Plus rel="author" tag
  393. *
  394. * @param int|Fishpig_Wordpress_Model_User
  395. * @return $this
  396. */
  397. protected function _addGooglePlusLinkRel($user)
  398. {
  399. if (!is_object($user)) {
  400. $user = Mage::getModel('wordpress/user')->load($user);
  401. if (!$user->getId()) {
  402. return $this;
  403. }
  404. }
  405. if ($user->getId() && $user->getMetaValue('googleplus')) {
  406. $this->_getHeadBlock()->addItem('link_rel', $user->getMetaValue('googleplus'), 'rel="author"');
  407. }
  408. return $this;
  409. }
  410. }