/wp-content/plugins/squirrly-seo/controllers/SQ_Frontend.php

https://gitlab.com/memuller.web/wp_site · PHP · 199 lines · 143 code · 31 blank · 25 comment · 48 complexity · efd8398ab054a3d98afda10dc5265cfe MD5 · raw file

  1. <?php
  2. class SQ_Frontend extends SQ_FrontController {
  3. public static $options;
  4. public function __construct() {
  5. if ($this->_isAjax()) {
  6. return;
  7. }
  8. parent::__construct();
  9. SQ_ObjController::getController('SQ_Tools', false);
  10. if (SQ_Tools::$options['sq_use'] == 1) {
  11. /* Check if sitemap is on */
  12. if (SQ_Tools::$options['sq_auto_sitemap'] == 1) {
  13. /* Load the Sitemap */
  14. add_filter('rewrite_rules_array', array($this, 'rewrite_rules'), 1, 1);
  15. SQ_ObjController::getController('SQ_Sitemaps');
  16. }
  17. if (SQ_Tools::$options['sq_auto_feed'] == 1) {
  18. /* Load the Feed Style */
  19. SQ_ObjController::getController('SQ_Feed');
  20. }
  21. //validate custom arguments for favicon and sitemap
  22. add_filter('query_vars', array($this, 'validateParams'), 1, 1);
  23. if (!$this->_isAjax()) {
  24. add_filter('sq_title', array($this->model, 'clearTitle'));
  25. add_filter('sq_description', array($this->model, 'clearDescription'));
  26. add_action('plugins_loaded', array($this->model, 'startBuffer'));
  27. //flush the header with the title and removing duplicates
  28. add_action('wp_head', array($this->model, 'flushHeader'),99);
  29. add_action('shutdown', array($this->model, 'flushHeader'));
  30. }
  31. if (SQ_Tools::$options['sq_url_fix'] == 1) {
  32. add_action('the_content', array($this, 'fixFeedLinks'), 11);
  33. }
  34. }
  35. }
  36. public function rewrite_rules($wp_rewrite) {
  37. if (SQ_Tools::$options['sq_auto_sitemap'] == 1) {
  38. foreach (SQ_Tools::$options['sq_sitemap'] as $name => $sitemap) {
  39. $rules[preg_quote($sitemap[0]) . '$'] = 'index.php?sq_feed=' . $name;
  40. }
  41. }
  42. return array_merge($rules, $wp_rewrite);
  43. }
  44. private function _isAjax() {
  45. if (isset($_SERVER['PHP_SELF']) && strpos($_SERVER['PHP_SELF'], '/admin-ajax.php') !== false)
  46. return true;
  47. return false;
  48. }
  49. /**
  50. * Hook the Header load
  51. */
  52. public function hookFronthead() {
  53. if (!$this->_isAjax()) {
  54. if (SQ_Tools::$options['sq_use'] == 1) {
  55. echo $this->model->setStartTag();
  56. }
  57. SQ_ObjController::getController('SQ_DisplayController', false)
  58. ->loadMedia(_SQ_THEME_URL_ . 'css/sq_frontend.css');
  59. }
  60. }
  61. /**
  62. * Called after plugins are loaded
  63. */
  64. public function hookPreload() {
  65. //Check for sitemap and robots
  66. if (SQ_Tools::$options['sq_use'] == 1) {
  67. if (isset($_SERVER['REQUEST_URI']) && SQ_Tools::$options['sq_auto_robots'] == 1) {
  68. if (substr(strrchr($_SERVER['REQUEST_URI'], "/"), 1) == "robots.txt" || $_SERVER['REQUEST_URI'] == "/robots.txt") {
  69. $this->model->robots();
  70. }
  71. }
  72. //check the action call
  73. $this->action();
  74. }
  75. }
  76. /**
  77. * Change the image path to absolute when in feed
  78. */
  79. public function fixFeedLinks($content) {
  80. if (is_feed()) {
  81. $find = $replace = $urls = array();
  82. @preg_match_all('/<img[^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/i', $content, $out);
  83. if (is_array($out)) {
  84. if (!is_array($out[1]) || empty($out[1]))
  85. return $content;
  86. foreach ($out[1] as $row) {
  87. if (strpos($row, '//') === false) {
  88. if (!in_array($row, $urls)) {
  89. $urls[] = $row;
  90. }
  91. }
  92. }
  93. }
  94. @preg_match_all('/<a[^>]*href=[\'"]([^\'"]+)[\'"][^>]*>/i', $content, $out);
  95. if (is_array($out)) {
  96. if (!is_array($out[1]) || empty($out[1]))
  97. return $content;
  98. foreach ($out[1] as $row) {
  99. if (strpos($row, '//') === false) {
  100. if (!in_array($row, $urls)) {
  101. $urls[] = $row;
  102. }
  103. }
  104. }
  105. }
  106. if (!is_array($urls) || (is_array($urls) && empty($urls))) {
  107. return $content;
  108. }
  109. $urls = array_unique($urls);
  110. foreach ($urls as $url) {
  111. $find[] = "'" . $url . "'";
  112. $replace[] = "'" . esc_url(get_bloginfo('url') . $url) . "'";
  113. $find[] = '"' . $url . '"';
  114. $replace[] = '"' . esc_url(get_bloginfo('url') . $url) . '"';
  115. }
  116. if (!empty($find) && !empty($replace)) {
  117. $content = str_replace($find, $replace, $content);
  118. }
  119. }
  120. return $content;
  121. }
  122. /**
  123. * Validate the params for getting the basic info from the server
  124. * eg favicon.ico
  125. *
  126. * @param array $vars
  127. * @return $vars
  128. */
  129. public function validateParams($vars) {
  130. array_push($vars, 'sq_feed');
  131. array_push($vars, 'sq_get');
  132. array_push($vars, 'sq_size');
  133. return $vars;
  134. }
  135. public function action() {
  136. global $wp_query;
  137. if (!empty($wp_query->query_vars["sq_get"])) {
  138. $wp_query->is_404 = false;
  139. switch (get_query_var('sq_get')) {
  140. case 'favicon':
  141. if (SQ_Tools::$options['favicon'] <> '') {
  142. //show the favico file
  143. SQ_Tools::setHeader('ico');
  144. readfile(_SQ_CACHE_DIR_ . SQ_Tools::$options['favicon']);
  145. exit();
  146. }
  147. break;
  148. case 'touchicon':
  149. $size = (int) get_query_var('sq_size');
  150. if (SQ_Tools::$options['favicon'] <> '') {
  151. //show the favico file
  152. SQ_Tools::setHeader('png');
  153. if ($size <> '') {
  154. readfile(_SQ_CACHE_DIR_ . SQ_Tools::$options['favicon'] . get_query_var('sq_size'));
  155. } else {
  156. readfile(_SQ_CACHE_DIR_ . SQ_Tools::$options['favicon']);
  157. }
  158. exit();
  159. }
  160. break;
  161. case 'feedcss':
  162. readfile(_SQ_THEME_DIR_ . 'css/' . 'sq_feed.css');
  163. exit();
  164. break;
  165. }
  166. }
  167. }
  168. }