admin
1 week ago
auto-insert
4 months ago
conditional-logic
6 months ago
execute
5 months ago
generator
6 months ago
lite
9 months ago
capabilities.php
2 years ago
class-wpcode-abilities-api.php
1 week ago
class-wpcode-admin-bar-info.php
2 years ago
class-wpcode-auto-insert.php
4 months ago
class-wpcode-capabilities.php
3 years ago
class-wpcode-conditional-logic.php
4 months ago
class-wpcode-error.php
2 years ago
class-wpcode-file-cache.php
1 year ago
class-wpcode-file-logger.php
1 year ago
class-wpcode-generator.php
4 months ago
class-wpcode-install.php
1 year ago
class-wpcode-library-auth.php
1 year ago
class-wpcode-library.php
1 week ago
class-wpcode-settings.php
6 months ago
class-wpcode-smart-tags.php
11 months ago
class-wpcode-snippet-cache.php
4 months ago
class-wpcode-snippet-execute.php
1 year ago
class-wpcode-snippet.php
1 year ago
compat.php
2 years ago
global-output.php
1 year ago
helpers.php
1 year ago
icons.php
5 months ago
ihaf.php
3 years ago
legacy.php
3 years ago
pluggable.php
2 years ago
post-type.php
1 week ago
safe-mode.php
11 months ago
shortcode.php
2 years ago
class-wpcode-snippet-cache.php
181 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Cache active snippets in a single query. |
| 4 | * |
| 5 | * @package WPCode |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class WPCode_Snippet_Cache. |
| 10 | */ |
| 11 | class WPCode_Snippet_Cache { |
| 12 | |
| 13 | /** |
| 14 | * The option name used for storing data in the db. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $option_name = 'wpcode_snippets'; |
| 19 | |
| 20 | /** |
| 21 | * The snippets stored in the db. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | protected $snippets; |
| 26 | |
| 27 | /** |
| 28 | * Get the snippets data from the cache. |
| 29 | * |
| 30 | * @return array |
| 31 | */ |
| 32 | public function get_cached_snippets() { |
| 33 | if ( ! isset( $this->snippets ) ) { |
| 34 | $this->snippets = $this->get_snippets_from_db_cache(); |
| 35 | } |
| 36 | |
| 37 | return $this->snippets; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get snippets from database cache. |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | protected function get_snippets_from_db_cache() { |
| 46 | $all_snippets = $this->get_option(); |
| 47 | foreach ( $all_snippets as $location => $snippets ) { |
| 48 | if ( empty( $snippets ) ) { |
| 49 | continue; |
| 50 | } |
| 51 | if ( ! is_array( $all_snippets[ $location ] ) ) { |
| 52 | $all_snippets[ $location ] = array(); |
| 53 | } |
| 54 | // Load minimal snippet data from array. |
| 55 | foreach ( $snippets as $key => $snippet ) { |
| 56 | $all_snippets[ $location ][ $key ] = $this->load_snippet( $snippet ); |
| 57 | } |
| 58 | |
| 59 | usort( $all_snippets[ $location ], array( $this, 'priority_order' ) ); |
| 60 | } |
| 61 | |
| 62 | return $all_snippets; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Load a snippet by id, WP_Post or array. |
| 67 | * |
| 68 | * @param array|int|WP_Post $snippet_data Load a snippet by id, WP_Post or array. |
| 69 | * |
| 70 | * @return WPCode_Snippet |
| 71 | */ |
| 72 | public function load_snippet( $snippet_data ) { |
| 73 | return new WPCode_Snippet( $snippet_data ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get cached snippets in an array by their id. |
| 78 | * |
| 79 | * @return WPCode_Snippet[] |
| 80 | */ |
| 81 | public function get_cached_snippets_by_id() { |
| 82 | $snippets_by_id = array(); |
| 83 | $cached_snippets = $this->get_cached_snippets(); |
| 84 | foreach ( $cached_snippets as $snippets ) { |
| 85 | foreach ( $snippets as $snippet ) { |
| 86 | $snippets_by_id[ $snippet->get_id() ] = $snippet; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return $snippets_by_id; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Used for sorting by priority. |
| 95 | * |
| 96 | * @param WPCode_Snippet $snippet_a The first snippet. |
| 97 | * @param WPCode_Snippet $snippet_b The second snippet. |
| 98 | * |
| 99 | * @return int |
| 100 | */ |
| 101 | public function priority_order( $snippet_a, $snippet_b ) { |
| 102 | return $snippet_a->get_priority() - $snippet_b->get_priority(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Delete the cache option completely. |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public function delete_cache() { |
| 111 | update_option( $this->option_name, array() ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Save all the loaded snippets in a single option. |
| 116 | * |
| 117 | * @return void |
| 118 | */ |
| 119 | public function cache_all_loaded_snippets() { |
| 120 | if ( ! apply_filters( 'wpcode_cache_active_snippets', true ) ) { |
| 121 | return; |
| 122 | } |
| 123 | $auto_inserts = wpcode()->auto_insert->get_types(); |
| 124 | $snippets_by_location = array(); |
| 125 | foreach ( $auto_inserts as $auto_insert ) { |
| 126 | // We don't want to use cached data when gathering stuff for cache. |
| 127 | add_filter( 'wpcode_use_auto_insert_cache', '__return_false' ); |
| 128 | // Make sure snippets were not already loaded by earlier hooks. |
| 129 | unset( $auto_insert->snippets ); |
| 130 | $snippets_by_location = array_merge( $auto_insert->get_snippets(), $snippets_by_location ); |
| 131 | } |
| 132 | |
| 133 | $data_for_cache = array(); |
| 134 | foreach ( $snippets_by_location as $location => $snippets ) { |
| 135 | if ( empty( $snippets ) ) { |
| 136 | continue; |
| 137 | } |
| 138 | $data_for_cache[ $location ] = $this->prepare_snippets_for_caching( $snippets ); |
| 139 | } |
| 140 | |
| 141 | $this->update_option( $data_for_cache ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Update the option with the new data. |
| 146 | * |
| 147 | * @param array $data_for_cache The data to store in the option. |
| 148 | * |
| 149 | * @return bool |
| 150 | */ |
| 151 | public function update_option( $data_for_cache ) { |
| 152 | return update_option( $this->option_name, $data_for_cache ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the option from the db. |
| 157 | * |
| 158 | * @return array |
| 159 | */ |
| 160 | public function get_option() { |
| 161 | return (array) get_option( $this->option_name, array() ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Go through an array of snippets and extract just the minimal data |
| 166 | * needed for running the snippets. |
| 167 | * |
| 168 | * @param WPCode_Snippet[] $snippets The snippets array. |
| 169 | * |
| 170 | * @return array |
| 171 | */ |
| 172 | protected function prepare_snippets_for_caching( $snippets ) { |
| 173 | $prepared_snippets = array(); |
| 174 | foreach ( $snippets as $snippet ) { |
| 175 | $prepared_snippets[] = $snippet->get_data_for_caching(); |
| 176 | } |
| 177 | |
| 178 | return $prepared_snippets; |
| 179 | } |
| 180 | } |
| 181 |