Guest User

Untitled

a guest
Aug 18th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. diff -Naur /source/wp-rest-cache/admin/partials/sub-cache-details.php /modified/wp-rest-cache/admin/partials/sub-cache-details.php
  2. --- /source/wp-rest-cache/admin/partials/sub-cache-details.php 2019-07-12 13:20:22.000000000 +0300
  3. +++ /modified/wp-rest-cache/admin/partials/sub-cache-details.php 2020-08-18 23:06:58.666943006 +0300
  4. @@ -91,6 +91,16 @@
  5. </div>
  6. </div>
  7. <?php endif; ?>
  8. + <?php if ( isset( $wp_rest_cache['data']['uris'] ) ) : ?>
  9. + <div class="postbox">
  10. + <h3 class="hndle"><?php esc_html_e( 'Request URIs', 'wp-rest-cache' ); ?></h3>
  11. + <div class="inside">
  12. + <?php foreach ( $wp_rest_cache['data']['uris'] as $uri ) : ?>
  13. + <p><?php echo $uri; ?></p>
  14. + <?php endforeach; ?>
  15. + </div>
  16. + </div>
  17. + <?php endif; ?>
  18. <div class="postbox">
  19. <h3 class="hndle"><?php esc_html_e( 'Cache data', 'wp-rest-cache' ); ?></h3>
  20. <div class="inside">
  21. diff -Naur /source/wp-rest-cache/includes/api/class-endpoint-api.php /modified/wp-rest-cache/includes/api/class-endpoint-api.php
  22. --- /source/wp-rest-cache/includes/api/class-endpoint-api.php 2020-07-02 09:25:52.000000000 +0300
  23. +++ /modified/wp-rest-cache/includes/api/class-endpoint-api.php 2020-08-18 22:58:42.192442025 +0300
  24. @@ -167,8 +167,10 @@
  25. private function build_cache_key() {
  26. $this->build_request_uri();
  27. $this->set_cacheable_request_headers();
  28. -
  29. - $this->cache_key = md5( $this->request_uri . wp_json_encode( $this->request_headers ) );
  30. +
  31. + $cache_key_params = get_option( 'wp_rest_cache_cache_key_params', [] );
  32. +
  33. + $this->cache_key = md5( $this->request_uri . wp_json_encode( $this->request_headers ) . wp_json_encode($cache_key_params) );
  34. }
  35.  
  36. /**
  37. @@ -218,10 +220,11 @@
  38. * @param array $result Response data to send to the client.
  39. * @param \WP_REST_Server $server Server instance.
  40. * @param \WP_REST_Request $request Request used to generate the response.
  41. + * @param array $uris
  42. *
  43. * @return array Response data to send to the client.
  44. */
  45. - public function save_cache( $result, \WP_REST_Server $server, \WP_REST_Request $request ) {
  46. + public function save_cache( $result, \WP_REST_Server $server, \WP_REST_Request $request, $uris = [] ) {
  47. // Only Avoid cache if not 200.
  48. if ( ! empty( $result ) && is_array( $result ) && isset( $result['data']['status'] ) && 200 !== (int) $result['data']['status'] ) {
  49. return $result;
  50. @@ -233,9 +236,12 @@
  51. }
  52.  
  53. $data = array(
  54. - 'data' => $result,
  55. - 'headers' => $this->response_headers,
  56. + 'data' => $result,
  57. + 'headers' => $this->response_headers,
  58. + 'uris' => $uris,
  59. );
  60. + $data['uris'][$_SERVER['REQUEST_URI']] = $_SERVER['REQUEST_URI'];
  61. +
  62. \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->set_cache( $this->cache_key, $data, 'endpoint', $this->request_uri, '', $this->request_headers );
  63.  
  64. return $result;
  65. @@ -340,6 +346,12 @@
  66. $this->rest_send_cors_headers( '' );
  67.  
  68. echo $data; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  69. +
  70. + $cache['uris'] = (isset($cache['uris'])) ? $cache['uris'] : [];
  71. + if (!isset($cache['uris'][$_SERVER['REQUEST_URI']])) {
  72. + $this->save_cache($cache['data'], new \WP_REST_Server(), $this->request, $cache['uris']);
  73. + }
  74. +
  75. exit;
  76. }
  77. }
  78. @@ -466,6 +478,20 @@
  79. if ( (int) $original_cache_hit_recording !== (int) $cache_hit_recording ) {
  80. update_option( 'wp_rest_cache_hit_recording', (int) $cache_hit_recording, true );
  81. }
  82. +
  83. + $original_cache_key_params = get_option( 'wp_rest_cache_cache_key_params', [] );
  84. + /**
  85. + * Filter to add custom params in cache key
  86. + *
  87. + * Allows to add custom parameters in cache keys. For example if you wish to add the date to each cache key
  88. + *
  89. + * @param array $original_cache_key_params An array of existing custom cache key params
  90. + */
  91. + $cache_key_params = apply_filters( 'wp_rest_cache/cache_key_params', $original_cache_key_params );
  92. + if ( $original_cache_key_params !== $cache_key_params ) {
  93. + ksort($cache_key_params, SORT_NATURAL);
  94. + update_option( 'wp_rest_cache_cache_key_params', $cache_key_params, true );
  95. + }
  96. }
  97.  
  98. /**
  99. diff -Naur /source/wp-rest-cache/includes/caching/class-caching.php /modified/wp-rest-cache/includes/caching/class-caching.php
  100. --- /source/wp-rest-cache/includes/caching/class-caching.php 2020-07-02 09:25:52.000000000 +0300
  101. +++ /modified/wp-rest-cache/includes/caching/class-caching.php 2020-08-18 23:16:25.378398843 +0300
  102. @@ -462,7 +462,9 @@
  103.  
  104. // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  105. $affected_rows = $wpdb->query( $wpdb->prepare( $sql, date_i18n( 'Y-m-d H:i:s', 0 ) ) );
  106. -
  107. +
  108. + apply_filters('wp_rest_cache/delete_all_caches', $affected_rows);
  109. +
  110. if ( 0 !== $affected_rows && false !== $affected_rows ) {
  111. $this->schedule_cleanup();
  112. }
  113. @@ -496,7 +498,9 @@
  114.  
  115. // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  116. $affected_rows = $wpdb->query( $wpdb->prepare( $sql, date_i18n( 'Y-m-d H:i:s', 0 ), $id, $object_type ) );
  117. -
  118. +
  119. + apply_filters('wp_rest_cache/delete_related_caches', $affected_rows);
  120. +
  121. if ( 0 !== $affected_rows && false !== $affected_rows ) {
  122. $this->schedule_cleanup();
  123. }
  124. @@ -519,7 +523,9 @@
  125.  
  126. // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  127. $affected_rows = $wpdb->query( $wpdb->prepare( $sql, date_i18n( 'Y-m-d H:i:s', 0 ), 'endpoint', $object_type, false ) );
  128. -
  129. +
  130. + apply_filters('wp_rest_cache/delete_object_type_caches', $affected_rows);
  131. +
  132. if ( 0 !== $affected_rows && false !== $affected_rows ) {
  133. $this->schedule_cleanup();
  134. }
  135. diff -Naur /source/wp-rest-cache/includes/class-activator.php /modified/wp-rest-cache/includes/class-activator.php
  136. --- /source/wp-rest-cache/includes/class-activator.php 2020-07-02 09:25:52.000000000 +0300
  137. +++ /modified/wp-rest-cache/includes/class-activator.php 2020-08-18 22:41:12.178802150 +0300
  138. @@ -44,6 +44,9 @@
  139. if ( ! is_null( get_option( 'wp_rest_cache_hit_recording', null ) ) ) {
  140. add_option( 'wp_rest_cache_hit_recording', 1, '', true );
  141. }
  142. + if ( ! get_option( 'wp_rest_cache_cache_key_params' ) ) {
  143. + add_option( 'wp_rest_cache_cache_key_params', [], '', true );
  144. + }
  145.  
  146. self::create_mu_plugin();
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment