PageRenderTime 89ms CodeModel.GetById 59ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/cloudflare/src/WordPress/Hooks.php

https://bitbucket.org/carloskikea/helpet
PHP | 269 lines | 204 code | 49 blank | 16 comment | 35 complexity | d49a872c55b1ef943d6dbdab5a7c41c1 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. namespace CF\WordPress;
  3. use CF\API\APIInterface;
  4. use CF\Integration;
  5. use Psr\Log\LoggerInterface;
  6. class Hooks
  7. {
  8. protected $api;
  9. protected $config;
  10. protected $dataStore;
  11. protected $integrationContext;
  12. protected $integrationAPI;
  13. protected $logger;
  14. protected $proxy;
  15. const CLOUDFLARE_JSON = 'CLOUDFLARE_JSON';
  16. const WP_AJAX_ACTION = 'cloudflare_proxy';
  17. public function __construct()
  18. {
  19. $this->config = new Integration\DefaultConfig(file_get_contents(CLOUDFLARE_PLUGIN_DIR.'config.js', true));
  20. $this->logger = new Integration\DefaultLogger($this->config->getValue('debug'));
  21. $this->dataStore = new DataStore($this->logger);
  22. $this->integrationAPI = new WordPressAPI($this->dataStore);
  23. $this->integrationContext = new Integration\DefaultIntegration($this->config, $this->integrationAPI, $this->dataStore, $this->logger);
  24. $this->api = new WordPressClientAPI($this->integrationContext);
  25. $this->proxy = new Proxy($this->integrationContext);
  26. }
  27. /**
  28. * @param \CF\API\APIInterface $api
  29. */
  30. public function setAPI(APIInterface $api)
  31. {
  32. $this->api = $api;
  33. }
  34. public function setConfig(Integration\ConfigInterface $config)
  35. {
  36. $this->config = $config;
  37. }
  38. public function setDataStore(Integration\DataStoreInterface $dataStore)
  39. {
  40. $this->dataStore = $dataStore;
  41. }
  42. public function setIntegrationContext(Integration\IntegrationInterface $integrationContext)
  43. {
  44. $this->integrationContext = $integrationContext;
  45. }
  46. public function setIntegrationAPI(Integration\IntegrationAPIInterface $integrationAPI)
  47. {
  48. $this->integrationAPI = $integrationAPI;
  49. }
  50. public function setLogger(LoggerInterface $logger)
  51. {
  52. $this->logger = $logger;
  53. }
  54. public function setProxy(Proxy $proxy)
  55. {
  56. $this->proxy = $proxy;
  57. }
  58. public function cloudflareConfigPage()
  59. {
  60. if (function_exists('add_options_page')) {
  61. add_options_page(__('Cloudflare Configuration'), __('Cloudflare'), 'manage_options', 'cloudflare', array($this, 'cloudflareIndexPage'));
  62. }
  63. }
  64. public function cloudflareIndexPage()
  65. {
  66. include CLOUDFLARE_PLUGIN_DIR.'index.php';
  67. }
  68. public function pluginActionLinks($links)
  69. {
  70. $links[] = '<a href="'.get_admin_url(null, 'options-general.php?page=cloudflare').'">Settings</a>';
  71. return $links;
  72. }
  73. public function initProxy()
  74. {
  75. $this->proxy->run();
  76. }
  77. public function activate()
  78. {
  79. if (version_compare($GLOBALS['wp_version'], CLOUDFLARE_MIN_WP_VERSION, '<')) {
  80. deactivate_plugins(basename(CLOUDFLARE_PLUGIN_DIR));
  81. wp_die('<p><strong>Cloudflare</strong> plugin requires WordPress version '.CLOUDFLARE_MIN_WP_VERSION.' or greater.</p>', 'Plugin Activation Error', array('response' => 200, 'back_link' => true));
  82. }
  83. return true;
  84. }
  85. public function deactivate()
  86. {
  87. $this->dataStore->clearDataStore();
  88. }
  89. public function purgeCacheEverything()
  90. {
  91. if ($this->isPluginSpecificCacheEnabled()) {
  92. $wpDomainList = $this->integrationAPI->getDomainList();
  93. $wpDomain = $wpDomainList[0];
  94. if (count($wpDomain) > 0) {
  95. $zoneTag = $this->api->getZoneTag($wpDomain);
  96. if (isset($zoneTag)) {
  97. $isOK = $this->api->zonePurgeCache($zoneTag);
  98. $isOK = ($isOK) ? 'succeeded' : 'failed';
  99. $this->logger->debug("purgeCacheEverything " . $isOK);
  100. }
  101. }
  102. }
  103. }
  104. public function purgeCacheByRevelantURLs($postId)
  105. {
  106. if ($this->isPluginSpecificCacheEnabled()) {
  107. $wpDomainList = $this->integrationAPI->getDomainList();
  108. $wpDomain = $wpDomainList[0];
  109. if (count($wpDomain) <= 0) {
  110. return;
  111. }
  112. $validPostStatus = array('publish', 'trash');
  113. $thisPostStatus = get_post_status($postId);
  114. if (get_permalink($postId) != true || !in_array($thisPostStatus, $validPostStatus)) {
  115. return;
  116. }
  117. if (is_int(wp_is_post_autosave($postId)) || is_int(wp_is_post_revision($postId))) {
  118. return;
  119. }
  120. $savedPost = get_post($postId);
  121. if (is_a($savedPost, 'WP_Post') == false) {
  122. return;
  123. }
  124. $urls = $this->getPostRelatedLinks($postId);
  125. $urls = apply_filters('cloudflare_purge_by_url', $urls, $postId);
  126. $zoneTag = $this->api->getZoneTag($wpDomain);
  127. if (isset($zoneTag) && !empty($urls)) {
  128. $isOK = $this->api->zonePurgeFiles($zoneTag, $urls);
  129. $isOK = ($isOK) ? 'succeeded' : 'failed';
  130. $this->logger->debug("List of URLs purged are: " . print_r($urls, true));
  131. $this->logger->debug("purgeCacheByRevelantURLs " . $isOK);
  132. }
  133. }
  134. }
  135. public function getPostRelatedLinks($postId)
  136. {
  137. $listofurls = array();
  138. $postType = get_post_type($postId);
  139. //Purge taxonomies terms URLs
  140. $postTypeTaxonomies = get_object_taxonomies($postType);
  141. foreach ($postTypeTaxonomies as $taxonomy) {
  142. $terms = get_the_terms($postId, $taxonomy);
  143. if (empty($terms) || is_wp_error($terms)) {
  144. continue;
  145. }
  146. foreach ($terms as $term) {
  147. $termLink = get_term_link($term);
  148. if (!is_wp_error($termLink)) {
  149. array_push($listofurls, $termLink);
  150. }
  151. }
  152. }
  153. // Author URL
  154. array_push(
  155. $listofurls,
  156. get_author_posts_url(get_post_field('post_author', $postId)),
  157. get_author_feed_link(get_post_field('post_author', $postId))
  158. );
  159. // Archives and their feeds
  160. if (get_post_type_archive_link($postType) == true) {
  161. array_push(
  162. $listofurls,
  163. get_post_type_archive_link($postType),
  164. get_post_type_archive_feed_link($postType)
  165. );
  166. }
  167. // Post URL
  168. array_push($listofurls, get_permalink($postId));
  169. // Also clean URL for trashed post.
  170. if (get_post_status($postId) == 'trash') {
  171. $trashPost = get_permalink($postId);
  172. $trashPost = str_replace('__trashed', '', $trashPost);
  173. array_push($listofurls, $trashPost, $trashPost.'feed/');
  174. }
  175. // Feeds
  176. array_push(
  177. $listofurls,
  178. get_bloginfo_rss('rdf_url'),
  179. get_bloginfo_rss('rss_url'),
  180. get_bloginfo_rss('rss2_url'),
  181. get_bloginfo_rss('atom_url'),
  182. get_bloginfo_rss('comments_rss2_url'),
  183. get_post_comments_feed_link($postId)
  184. );
  185. // Home Page and (if used) posts page
  186. array_push($listofurls, home_url('/'));
  187. $pageLink = get_permalink(get_option('page_for_posts'));
  188. if (is_string($pageLink) && !empty($pageLink) && get_option('show_on_front') == 'page') {
  189. array_push($listofurls, $pageLink);
  190. }
  191. // Purge https and http URLs
  192. if (function_exists('force_ssl_admin') && force_ssl_admin()) {
  193. $listofurls = array_merge($listofurls, str_replace('https://', 'http://', $listofurls));
  194. } elseif (!is_ssl() && function_exists('force_ssl_content') && force_ssl_content()) {
  195. $listofurls = array_merge($listofurls, str_replace('http://', 'https://', $listofurls));
  196. }
  197. return $listofurls;
  198. }
  199. protected function isPluginSpecificCacheEnabled()
  200. {
  201. $cacheSettingObject = $this->dataStore->getPluginSetting(\CF\API\Plugin::SETTING_PLUGIN_SPECIFIC_CACHE);
  202. $cacheSettingValue = $cacheSettingObject[\CF\API\Plugin::SETTING_VALUE_KEY];
  203. return isset($cacheSettingValue) && $cacheSettingValue !== false && $cacheSettingValue !== 'off';
  204. }
  205. public function http2ServerPushInit()
  206. {
  207. HTTP2ServerPush::init();
  208. }
  209. /*
  210. * php://input can only be read once before PHP 5.6, try to grab it ONLY if the request
  211. * is coming from the cloudflare proxy. We store it in a global so \CF\WordPress\Proxy
  212. * can act on the request body later on in the script execution.
  213. */
  214. public function getCloudflareRequestJSON()
  215. {
  216. if (isset($_GET['action']) && $_GET['action'] === self::WP_AJAX_ACTION) {
  217. $GLOBALS[self::CLOUDFLARE_JSON] = file_get_contents('php://input');
  218. }
  219. }
  220. }