@@ -18,17 +18,17 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | interface Wordlift_Post_Converter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Convert the specified post id. |
|
| 23 | - * |
|
| 24 | - * @since 3.16.0 $references argument added. |
|
| 25 | - * @since 3.10.0 |
|
| 26 | - * |
|
| 27 | - * @param int $post_id The post id. |
|
| 28 | - * @param array $references An array of posts referenced by the specified post. |
|
| 29 | - * |
|
| 30 | - * @return mixed The conversion result. |
|
| 31 | - */ |
|
| 32 | - public function convert( $post_id, &$references = array() ); |
|
| 21 | + /** |
|
| 22 | + * Convert the specified post id. |
|
| 23 | + * |
|
| 24 | + * @since 3.16.0 $references argument added. |
|
| 25 | + * @since 3.10.0 |
|
| 26 | + * |
|
| 27 | + * @param int $post_id The post id. |
|
| 28 | + * @param array $references An array of posts referenced by the specified post. |
|
| 29 | + * |
|
| 30 | + * @return mixed The conversion result. |
|
| 31 | + */ |
|
| 32 | + public function convert( $post_id, &$references = array() ); |
|
| 33 | 33 | |
| 34 | 34 | } |
@@ -29,6 +29,6 @@ |
||
| 29 | 29 | * |
| 30 | 30 | * @return mixed The conversion result. |
| 31 | 31 | */ |
| 32 | - public function convert( $post_id, &$references = array() ); |
|
| 32 | + public function convert($post_id, &$references = array()); |
|
| 33 | 33 | |
| 34 | 34 | } |
@@ -17,42 +17,42 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | interface Wordlift_Cache_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Get the cached response for the specified `id`. |
|
| 22 | - * |
|
| 23 | - * @since 3.16.0 |
|
| 24 | - * |
|
| 25 | - * @param int $id The cache `id`. |
|
| 26 | - * |
|
| 27 | - * @return mixed|false The cached contents or false if the cache isn't found. |
|
| 28 | - */ |
|
| 29 | - function get_cache( $id ); |
|
| 20 | + /** |
|
| 21 | + * Get the cached response for the specified `id`. |
|
| 22 | + * |
|
| 23 | + * @since 3.16.0 |
|
| 24 | + * |
|
| 25 | + * @param int $id The cache `id`. |
|
| 26 | + * |
|
| 27 | + * @return mixed|false The cached contents or false if the cache isn't found. |
|
| 28 | + */ |
|
| 29 | + function get_cache( $id ); |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Set the cache contents for the specified `id`. |
|
| 33 | - * |
|
| 34 | - * @since 3.16.0 |
|
| 35 | - * |
|
| 36 | - * @param int $id The cache id. |
|
| 37 | - * @param mixed $contents The cache contents. |
|
| 38 | - */ |
|
| 39 | - function set_cache( $id, $contents ); |
|
| 31 | + /** |
|
| 32 | + * Set the cache contents for the specified `id`. |
|
| 33 | + * |
|
| 34 | + * @since 3.16.0 |
|
| 35 | + * |
|
| 36 | + * @param int $id The cache id. |
|
| 37 | + * @param mixed $contents The cache contents. |
|
| 38 | + */ |
|
| 39 | + function set_cache( $id, $contents ); |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Delete the cache for the specified `id`. |
|
| 44 | - * |
|
| 45 | - * @since 3.16.0 |
|
| 46 | - * |
|
| 47 | - * @param int $id The cache `id`. |
|
| 48 | - */ |
|
| 49 | - function delete_cache( $id ); |
|
| 42 | + /** |
|
| 43 | + * Delete the cache for the specified `id`. |
|
| 44 | + * |
|
| 45 | + * @since 3.16.0 |
|
| 46 | + * |
|
| 47 | + * @param int $id The cache `id`. |
|
| 48 | + */ |
|
| 49 | + function delete_cache( $id ); |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Flush the whole cache. |
|
| 53 | - * |
|
| 54 | - * @since 3.16.0 |
|
| 55 | - */ |
|
| 56 | - function flush(); |
|
| 51 | + /** |
|
| 52 | + * Flush the whole cache. |
|
| 53 | + * |
|
| 54 | + * @since 3.16.0 |
|
| 55 | + */ |
|
| 56 | + function flush(); |
|
| 57 | 57 | |
| 58 | 58 | } |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @return mixed|false The cached contents or false if the cache isn't found. |
| 28 | 28 | */ |
| 29 | - function get_cache( $id ); |
|
| 29 | + function get_cache($id); |
|
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * Set the cache contents for the specified `id`. |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | * @param int $id The cache id. |
| 37 | 37 | * @param mixed $contents The cache contents. |
| 38 | 38 | */ |
| 39 | - function set_cache( $id, $contents ); |
|
| 39 | + function set_cache($id, $contents); |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @param int $id The cache `id`. |
| 48 | 48 | */ |
| 49 | - function delete_cache( $id ); |
|
| 49 | + function delete_cache($id); |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * Flush the whole cache. |
@@ -16,180 +16,180 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_File_Cache_Service implements Wordlift_Cache_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The cache directory. |
|
| 21 | - * |
|
| 22 | - * @since 3.16.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var string $cache_dir The root cache directory (ending with a trailing slash). |
|
| 25 | - */ |
|
| 26 | - private $cache_dir; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The file extension for cache files (e.g. `.wlcache`). |
|
| 30 | - * |
|
| 31 | - * @since 3.16.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var string $file_extension The file extension for cache files (e.g. `.wlcache`). |
|
| 34 | - */ |
|
| 35 | - private $file_extension; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A {@link Wordlift_Log_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.16.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $log; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The {@link Wordlift_File_Cache_Service} singleton instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.16.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var \Wordlift_File_Cache_Service $instance The {@link Wordlift_File_Cache_Service} singleton instance. |
|
| 52 | - */ |
|
| 53 | - private static $instance; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Create a {@link Wordlift_File_Cache_Service} instance. |
|
| 57 | - * |
|
| 58 | - * The File Cache Service requires a base cache directory (to which a unique |
|
| 59 | - * id for the current site will be appended) and a file extension for cache |
|
| 60 | - * files (by default `.wlcache`) is used. |
|
| 61 | - * |
|
| 62 | - * @since 3.16.0 |
|
| 63 | - * |
|
| 64 | - * @param string $cache_dir The base cache directory. |
|
| 65 | - * @param string $file_extension The file extension, by default `.wlcache`. |
|
| 66 | - */ |
|
| 67 | - public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
| 68 | - |
|
| 69 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 70 | - |
|
| 71 | - // Set the cache directory using the base directory provided by the caller |
|
| 72 | - // and appending a hash for the unique site id. |
|
| 73 | - $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
| 74 | - $this->file_extension = $file_extension; |
|
| 75 | - |
|
| 76 | - // Create the cache dir. |
|
| 77 | - if ( ! file_exists( $this->cache_dir ) ) { |
|
| 78 | - mkdir( $this->cache_dir, 0755, true ); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - self::$instance = $this; |
|
| 82 | - |
|
| 83 | - $this->log->info( "File Cache service initialized on $this->cache_dir." ); |
|
| 84 | - |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Get the {@link Wordlift_File_Cache_Service} singleton instance. |
|
| 89 | - * |
|
| 90 | - * @since 3.16.0 |
|
| 91 | - * @return \Wordlift_File_Cache_Service The {@link Wordlift_File_Cache_Service} singleton instance. |
|
| 92 | - */ |
|
| 93 | - public static function get_instance() { |
|
| 94 | - |
|
| 95 | - return self::$instance; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @inheritdoc |
|
| 100 | - */ |
|
| 101 | - function get_cache( $id ) { |
|
| 102 | - |
|
| 103 | - // Get the filename. |
|
| 104 | - $filename = $this->get_filename( $id ); |
|
| 105 | - |
|
| 106 | - // Bail out if the file doesn't exist. |
|
| 107 | - if ( ! file_exists( $filename ) ) { |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
| 112 | - |
|
| 113 | - // Try to decode the contents. |
|
| 114 | - $contents = json_decode( file_get_contents( $filename ), true ); |
|
| 115 | - |
|
| 116 | - // Return false if decoding failed, otherwise the decoded contents. |
|
| 117 | - return $contents ?: false; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @inheritdoc |
|
| 122 | - */ |
|
| 123 | - function set_cache( $id, $contents ) { |
|
| 124 | - |
|
| 125 | - $filename = $this->get_filename( $id ); |
|
| 126 | - |
|
| 127 | - $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
| 128 | - |
|
| 129 | - file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
| 130 | - |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @inheritdoc |
|
| 135 | - */ |
|
| 136 | - function delete_cache( $id ) { |
|
| 137 | - |
|
| 138 | - $filename = $this->get_filename( $id ); |
|
| 139 | - |
|
| 140 | - $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
| 141 | - |
|
| 142 | - wp_delete_file( $filename ); |
|
| 19 | + /** |
|
| 20 | + * The cache directory. |
|
| 21 | + * |
|
| 22 | + * @since 3.16.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var string $cache_dir The root cache directory (ending with a trailing slash). |
|
| 25 | + */ |
|
| 26 | + private $cache_dir; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The file extension for cache files (e.g. `.wlcache`). |
|
| 30 | + * |
|
| 31 | + * @since 3.16.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var string $file_extension The file extension for cache files (e.g. `.wlcache`). |
|
| 34 | + */ |
|
| 35 | + private $file_extension; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A {@link Wordlift_Log_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.16.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $log; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The {@link Wordlift_File_Cache_Service} singleton instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.16.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var \Wordlift_File_Cache_Service $instance The {@link Wordlift_File_Cache_Service} singleton instance. |
|
| 52 | + */ |
|
| 53 | + private static $instance; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Create a {@link Wordlift_File_Cache_Service} instance. |
|
| 57 | + * |
|
| 58 | + * The File Cache Service requires a base cache directory (to which a unique |
|
| 59 | + * id for the current site will be appended) and a file extension for cache |
|
| 60 | + * files (by default `.wlcache`) is used. |
|
| 61 | + * |
|
| 62 | + * @since 3.16.0 |
|
| 63 | + * |
|
| 64 | + * @param string $cache_dir The base cache directory. |
|
| 65 | + * @param string $file_extension The file extension, by default `.wlcache`. |
|
| 66 | + */ |
|
| 67 | + public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
| 68 | + |
|
| 69 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 70 | + |
|
| 71 | + // Set the cache directory using the base directory provided by the caller |
|
| 72 | + // and appending a hash for the unique site id. |
|
| 73 | + $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
| 74 | + $this->file_extension = $file_extension; |
|
| 75 | + |
|
| 76 | + // Create the cache dir. |
|
| 77 | + if ( ! file_exists( $this->cache_dir ) ) { |
|
| 78 | + mkdir( $this->cache_dir, 0755, true ); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + self::$instance = $this; |
|
| 82 | + |
|
| 83 | + $this->log->info( "File Cache service initialized on $this->cache_dir." ); |
|
| 84 | + |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Get the {@link Wordlift_File_Cache_Service} singleton instance. |
|
| 89 | + * |
|
| 90 | + * @since 3.16.0 |
|
| 91 | + * @return \Wordlift_File_Cache_Service The {@link Wordlift_File_Cache_Service} singleton instance. |
|
| 92 | + */ |
|
| 93 | + public static function get_instance() { |
|
| 94 | + |
|
| 95 | + return self::$instance; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @inheritdoc |
|
| 100 | + */ |
|
| 101 | + function get_cache( $id ) { |
|
| 102 | + |
|
| 103 | + // Get the filename. |
|
| 104 | + $filename = $this->get_filename( $id ); |
|
| 105 | + |
|
| 106 | + // Bail out if the file doesn't exist. |
|
| 107 | + if ( ! file_exists( $filename ) ) { |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
| 112 | + |
|
| 113 | + // Try to decode the contents. |
|
| 114 | + $contents = json_decode( file_get_contents( $filename ), true ); |
|
| 115 | + |
|
| 116 | + // Return false if decoding failed, otherwise the decoded contents. |
|
| 117 | + return $contents ?: false; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @inheritdoc |
|
| 122 | + */ |
|
| 123 | + function set_cache( $id, $contents ) { |
|
| 124 | + |
|
| 125 | + $filename = $this->get_filename( $id ); |
|
| 126 | + |
|
| 127 | + $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
| 128 | + |
|
| 129 | + file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
| 130 | + |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @inheritdoc |
|
| 135 | + */ |
|
| 136 | + function delete_cache( $id ) { |
|
| 137 | + |
|
| 138 | + $filename = $this->get_filename( $id ); |
|
| 139 | + |
|
| 140 | + $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
| 141 | + |
|
| 142 | + wp_delete_file( $filename ); |
|
| 143 | 143 | |
| 144 | - } |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * @inheritdoc |
|
| 148 | - */ |
|
| 149 | - function flush() { |
|
| 146 | + /** |
|
| 147 | + * @inheritdoc |
|
| 148 | + */ |
|
| 149 | + function flush() { |
|
| 150 | 150 | |
| 151 | - // Bail out if the cache dir isn't set. |
|
| 152 | - if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
| 153 | - return; |
|
| 154 | - } |
|
| 151 | + // Bail out if the cache dir isn't set. |
|
| 152 | + if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
| 153 | + return; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - $this->log->trace( "Flushing cache contents..." ); |
|
| 156 | + $this->log->trace( "Flushing cache contents..." ); |
|
| 157 | 157 | |
| 158 | - $handle = @opendir( $this->cache_dir ); |
|
| 158 | + $handle = @opendir( $this->cache_dir ); |
|
| 159 | 159 | |
| 160 | - // Bail out if the directory can't be opened. |
|
| 161 | - if ( false === $handle ) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 160 | + // Bail out if the directory can't be opened. |
|
| 161 | + if ( false === $handle ) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - // Calculate the file extension length for matching file names. |
|
| 166 | - $file_extension_length = strlen( $this->file_extension ); |
|
| 165 | + // Calculate the file extension length for matching file names. |
|
| 166 | + $file_extension_length = strlen( $this->file_extension ); |
|
| 167 | 167 | |
| 168 | - // Loop into the directory to delete files. |
|
| 169 | - while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 170 | - if ( $this->file_extension === substr( $entry, - $file_extension_length ) ) { |
|
| 171 | - $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
| 172 | - wp_delete_file( $this->cache_dir . $entry ); |
|
| 173 | - } |
|
| 174 | - } |
|
| 168 | + // Loop into the directory to delete files. |
|
| 169 | + while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 170 | + if ( $this->file_extension === substr( $entry, - $file_extension_length ) ) { |
|
| 171 | + $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
| 172 | + wp_delete_file( $this->cache_dir . $entry ); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - // Finally closed the directory. |
|
| 177 | - closedir( $handle ); |
|
| 176 | + // Finally closed the directory. |
|
| 177 | + closedir( $handle ); |
|
| 178 | 178 | |
| 179 | - } |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Get the filename holding the cache contents for the specified `id`. |
|
| 183 | - * |
|
| 184 | - * @since 3.16.0 |
|
| 185 | - * |
|
| 186 | - * @param int $id The cache `id`. |
|
| 187 | - * |
|
| 188 | - * @return string The filename. |
|
| 189 | - */ |
|
| 190 | - private function get_filename( $id ) { |
|
| 181 | + /** |
|
| 182 | + * Get the filename holding the cache contents for the specified `id`. |
|
| 183 | + * |
|
| 184 | + * @since 3.16.0 |
|
| 185 | + * |
|
| 186 | + * @param int $id The cache `id`. |
|
| 187 | + * |
|
| 188 | + * @return string The filename. |
|
| 189 | + */ |
|
| 190 | + private function get_filename( $id ) { |
|
| 191 | 191 | |
| 192 | - return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
| 193 | - } |
|
| 192 | + return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | 195 | } |
@@ -64,23 +64,23 @@ discard block |
||
| 64 | 64 | * @param string $cache_dir The base cache directory. |
| 65 | 65 | * @param string $file_extension The file extension, by default `.wlcache`. |
| 66 | 66 | */ |
| 67 | - public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
| 67 | + public function __construct($cache_dir, $file_extension = '.wlcache') { |
|
| 68 | 68 | |
| 69 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 69 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 70 | 70 | |
| 71 | 71 | // Set the cache directory using the base directory provided by the caller |
| 72 | 72 | // and appending a hash for the unique site id. |
| 73 | - $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
| 73 | + $this->cache_dir = trailingslashit($cache_dir).md5(get_site_url()).'/'; |
|
| 74 | 74 | $this->file_extension = $file_extension; |
| 75 | 75 | |
| 76 | 76 | // Create the cache dir. |
| 77 | - if ( ! file_exists( $this->cache_dir ) ) { |
|
| 78 | - mkdir( $this->cache_dir, 0755, true ); |
|
| 77 | + if ( ! file_exists($this->cache_dir)) { |
|
| 78 | + mkdir($this->cache_dir, 0755, true); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | self::$instance = $this; |
| 82 | 82 | |
| 83 | - $this->log->info( "File Cache service initialized on $this->cache_dir." ); |
|
| 83 | + $this->log->info("File Cache service initialized on $this->cache_dir."); |
|
| 84 | 84 | |
| 85 | 85 | } |
| 86 | 86 | |
@@ -98,20 +98,20 @@ discard block |
||
| 98 | 98 | /** |
| 99 | 99 | * @inheritdoc |
| 100 | 100 | */ |
| 101 | - function get_cache( $id ) { |
|
| 101 | + function get_cache($id) { |
|
| 102 | 102 | |
| 103 | 103 | // Get the filename. |
| 104 | - $filename = $this->get_filename( $id ); |
|
| 104 | + $filename = $this->get_filename($id); |
|
| 105 | 105 | |
| 106 | 106 | // Bail out if the file doesn't exist. |
| 107 | - if ( ! file_exists( $filename ) ) { |
|
| 107 | + if ( ! file_exists($filename)) { |
|
| 108 | 108 | return false; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
| 111 | + $this->log->trace("Trying to get cache contents for $id from $filename..."); |
|
| 112 | 112 | |
| 113 | 113 | // Try to decode the contents. |
| 114 | - $contents = json_decode( file_get_contents( $filename ), true ); |
|
| 114 | + $contents = json_decode(file_get_contents($filename), true); |
|
| 115 | 115 | |
| 116 | 116 | // Return false if decoding failed, otherwise the decoded contents. |
| 117 | 117 | return $contents ?: false; |
@@ -120,26 +120,26 @@ discard block |
||
| 120 | 120 | /** |
| 121 | 121 | * @inheritdoc |
| 122 | 122 | */ |
| 123 | - function set_cache( $id, $contents ) { |
|
| 123 | + function set_cache($id, $contents) { |
|
| 124 | 124 | |
| 125 | - $filename = $this->get_filename( $id ); |
|
| 125 | + $filename = $this->get_filename($id); |
|
| 126 | 126 | |
| 127 | - $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
| 127 | + $this->log->trace("Writing cache contents for $id to $filename..."); |
|
| 128 | 128 | |
| 129 | - file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
| 129 | + file_put_contents($filename, wp_json_encode($contents)); |
|
| 130 | 130 | |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
| 134 | 134 | * @inheritdoc |
| 135 | 135 | */ |
| 136 | - function delete_cache( $id ) { |
|
| 136 | + function delete_cache($id) { |
|
| 137 | 137 | |
| 138 | - $filename = $this->get_filename( $id ); |
|
| 138 | + $filename = $this->get_filename($id); |
|
| 139 | 139 | |
| 140 | - $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
| 140 | + $this->log->trace("Deleting cache contents for $id, file $filename..."); |
|
| 141 | 141 | |
| 142 | - wp_delete_file( $filename ); |
|
| 142 | + wp_delete_file($filename); |
|
| 143 | 143 | |
| 144 | 144 | } |
| 145 | 145 | |
@@ -149,32 +149,32 @@ discard block |
||
| 149 | 149 | function flush() { |
| 150 | 150 | |
| 151 | 151 | // Bail out if the cache dir isn't set. |
| 152 | - if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
| 152 | + if (empty($this->cache_dir) || '/' === $this->cache_dir) { |
|
| 153 | 153 | return; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - $this->log->trace( "Flushing cache contents..." ); |
|
| 156 | + $this->log->trace("Flushing cache contents..."); |
|
| 157 | 157 | |
| 158 | - $handle = @opendir( $this->cache_dir ); |
|
| 158 | + $handle = @opendir($this->cache_dir); |
|
| 159 | 159 | |
| 160 | 160 | // Bail out if the directory can't be opened. |
| 161 | - if ( false === $handle ) { |
|
| 161 | + if (false === $handle) { |
|
| 162 | 162 | return; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | // Calculate the file extension length for matching file names. |
| 166 | - $file_extension_length = strlen( $this->file_extension ); |
|
| 166 | + $file_extension_length = strlen($this->file_extension); |
|
| 167 | 167 | |
| 168 | 168 | // Loop into the directory to delete files. |
| 169 | - while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 170 | - if ( $this->file_extension === substr( $entry, - $file_extension_length ) ) { |
|
| 171 | - $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
| 172 | - wp_delete_file( $this->cache_dir . $entry ); |
|
| 169 | + while (false !== ($entry = readdir($handle))) { |
|
| 170 | + if ($this->file_extension === substr($entry, - $file_extension_length)) { |
|
| 171 | + $this->log->trace("Deleting file {$this->cache_dir}{$entry}..."); |
|
| 172 | + wp_delete_file($this->cache_dir.$entry); |
|
| 173 | 173 | } |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // Finally closed the directory. |
| 177 | - closedir( $handle ); |
|
| 177 | + closedir($handle); |
|
| 178 | 178 | |
| 179 | 179 | } |
| 180 | 180 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return string The filename. |
| 189 | 189 | */ |
| 190 | - private function get_filename( $id ) { |
|
| 190 | + private function get_filename($id) { |
|
| 191 | 191 | |
| 192 | - return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
| 192 | + return $this->cache_dir.md5($id).$this->file_extension; |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | } |
@@ -8,6 +8,6 @@ |
||
| 8 | 8 | * @package Wordlift |
| 9 | 9 | * @subpackage Wordlift/includes/cache |
| 10 | 10 | */ |
| 11 | -require_once plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . 'includes/cache/intf-wordlift-cache-service.php'; |
|
| 12 | -require_once plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . 'includes/cache/class-wordlift-file-cache-service.php'; |
|
| 13 | -require_once plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . 'includes/cache/class-wordlift-cached-post-converter.php'; |
|
| 11 | +require_once plugin_dir_path(dirname(dirname(__FILE__))).'includes/cache/intf-wordlift-cache-service.php'; |
|
| 12 | +require_once plugin_dir_path(dirname(dirname(__FILE__))).'includes/cache/class-wordlift-file-cache-service.php'; |
|
| 13 | +require_once plugin_dir_path(dirname(dirname(__FILE__))).'includes/cache/class-wordlift-cached-post-converter.php'; |
|
@@ -18,218 +18,218 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Cached_Post_Converter implements Wordlift_Post_Converter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A {@link Wordlift_Post_Converter} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.16.0 |
|
| 25 | - * |
|
| 26 | - * @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance. |
|
| 27 | - */ |
|
| 28 | - private $converter; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A {@link Wordlift_Log_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.16.0 |
|
| 34 | - * |
|
| 35 | - * @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $log; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var Wordlift_Cache_Service |
|
| 41 | - */ |
|
| 42 | - private $cache_service; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Wordlift_Cached_Post_Converter constructor. |
|
| 46 | - * |
|
| 47 | - * @param \Wordlift_Post_Converter $converter |
|
| 48 | - * @param \Wordlift_Cache_Service $cache_service |
|
| 49 | - */ |
|
| 50 | - public function __construct( $converter, $cache_service ) { |
|
| 51 | - |
|
| 52 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 53 | - |
|
| 54 | - $this->converter = $converter; |
|
| 55 | - $this->cache_service = $cache_service; |
|
| 56 | - |
|
| 57 | - $this->init_hooks(); |
|
| 58 | - |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Hooks to catch post/post meta changes in order to invalidate the cache. |
|
| 63 | - * |
|
| 64 | - * @since 3.16.0 |
|
| 65 | - */ |
|
| 66 | - private function init_hooks() { |
|
| 67 | - |
|
| 68 | - // Hook on post save to flush relevant cache. |
|
| 69 | - add_action( 'save_post', array( $this, 'save_post' ) ); |
|
| 70 | - |
|
| 71 | - add_action( 'added_post_meta', array( |
|
| 72 | - $this, |
|
| 73 | - 'changed_post_meta', |
|
| 74 | - ), 10, 2 ); |
|
| 75 | - add_action( 'updated_post_meta', array( |
|
| 76 | - $this, |
|
| 77 | - 'changed_post_meta', |
|
| 78 | - ), 10, 2 ); |
|
| 79 | - add_action( 'deleted_post_meta', array( |
|
| 80 | - $this, |
|
| 81 | - 'changed_post_meta', |
|
| 82 | - ), 10, 2 ); |
|
| 83 | - |
|
| 84 | - // Flush cache when wordlift settings were updated. |
|
| 85 | - add_action( 'update_option_wl_general_settings', array( |
|
| 86 | - $this, |
|
| 87 | - 'update_option_wl_general_settings', |
|
| 88 | - ) ); |
|
| 89 | - |
|
| 90 | - // Invalid cache on relationship change. |
|
| 91 | - add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
| 92 | - add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
| 93 | - |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @inheritdoc |
|
| 98 | - */ |
|
| 99 | - public function convert( $post_id, &$references = array(), &$cache = false ) { |
|
| 100 | - |
|
| 101 | - $this->log->trace( "Converting post $post_id..." ); |
|
| 102 | - |
|
| 103 | - // Try to get a cached result. |
|
| 104 | - $contents = $this->get_cache( $post_id, $references ); |
|
| 105 | - |
|
| 106 | - // Return the cached contents if any. |
|
| 107 | - if ( false !== $contents ) { |
|
| 108 | - $this->log->debug( "Cached contents found for post $post_id." ); |
|
| 109 | - |
|
| 110 | - // Inform the caller that this is cached result. |
|
| 111 | - $cache = true; |
|
| 112 | - |
|
| 113 | - // Return the contents. |
|
| 114 | - return $contents; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $cache = false; |
|
| 118 | - |
|
| 119 | - // Convert the the post. |
|
| 120 | - $jsonld = $this->converter->convert( $post_id, $references ); |
|
| 121 | - |
|
| 122 | - // Cache the results. |
|
| 123 | - $this->set_cache( $post_id, $references, $jsonld ); |
|
| 124 | - |
|
| 125 | - // Finally return the JSON-LD. |
|
| 126 | - return $jsonld; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Try to get the cached contents. |
|
| 131 | - * |
|
| 132 | - * @since 3.16.0 |
|
| 133 | - * |
|
| 134 | - * @param int $post_id The {@link WP_Post} id. |
|
| 135 | - * @param array $references The referenced posts. |
|
| 136 | - * |
|
| 137 | - * @return mixed|bool The cached contents or false if the cached isn't found. |
|
| 138 | - */ |
|
| 139 | - private function get_cache( $post_id, &$references = array() ) { |
|
| 140 | - |
|
| 141 | - $this->log->trace( "Getting cached contents for post $post_id..." ); |
|
| 142 | - |
|
| 143 | - // Get the cache. |
|
| 144 | - $contents = $this->cache_service->get_cache( $post_id ); |
|
| 145 | - |
|
| 146 | - // Bail out if we don't have cached contents or the cached contents are |
|
| 147 | - // invalid. |
|
| 148 | - if ( false === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
| 149 | - $this->log->debug( "Cached contents for post $post_id not found." ); |
|
| 150 | - |
|
| 151 | - return false; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // Remap the cache. |
|
| 155 | - $references = $contents['references']; |
|
| 156 | - |
|
| 157 | - return $contents['jsonld']; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Set the cache with the provided results. |
|
| 162 | - * |
|
| 163 | - * The function will prepare the provided results and will ask the {@link Wordlift_Cache_Service} |
|
| 164 | - * to cache them. |
|
| 165 | - * |
|
| 166 | - * @since 3.16.0 |
|
| 167 | - * |
|
| 168 | - * @param int $post_id The {@link WP_Post} id. |
|
| 169 | - * @param array $references An array of references. |
|
| 170 | - * @param array $jsonld A JSON-LD structure. |
|
| 171 | - */ |
|
| 172 | - private function set_cache( $post_id, $references, $jsonld ) { |
|
| 173 | - |
|
| 174 | - $this->log->trace( "Caching result for post $post_id..." ); |
|
| 175 | - |
|
| 176 | - $this->cache_service->set_cache( $post_id, array( |
|
| 177 | - 'references' => $references, |
|
| 178 | - 'jsonld' => $jsonld, |
|
| 179 | - ) ); |
|
| 180 | - |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Hook to 'save_post', will invalidate the cache for that post. |
|
| 185 | - * |
|
| 186 | - * @since 3.16.0 |
|
| 187 | - * |
|
| 188 | - * @param int $post_id The {@link WP_Post} id. |
|
| 189 | - */ |
|
| 190 | - public function save_post( $post_id ) { |
|
| 191 | - $this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
| 192 | - |
|
| 193 | - $this->cache_service->delete_cache( $post_id ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Hook to meta changed for a {@link WP_Post}, will cause the cause to |
|
| 198 | - * invalidate. |
|
| 199 | - * |
|
| 200 | - * @since 3.16.0 |
|
| 201 | - * |
|
| 202 | - * @param int $id The {@link WP_Post} meta id. |
|
| 203 | - * @param int $post_id The {@link WP_Post} id. |
|
| 204 | - */ |
|
| 205 | - public function changed_post_meta( $id, $post_id ) { |
|
| 206 | - $this->log->trace( "Post $post_id meta changed, invalidating cache..." ); |
|
| 207 | - |
|
| 208 | - $this->cache_service->delete_cache( $post_id ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Hook to WordLift's options changes, will flush the cache. |
|
| 213 | - * |
|
| 214 | - * @since 3.16.0 |
|
| 215 | - */ |
|
| 216 | - public function update_option_wl_general_settings() { |
|
| 217 | - $this->log->trace( "WordLift options changed, flushing cache..." ); |
|
| 218 | - |
|
| 219 | - $this->cache_service->flush(); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Hook to WordLift's post/entity relation changes, will invalidate the cache. |
|
| 224 | - * |
|
| 225 | - * @since 3.16.0 |
|
| 226 | - * |
|
| 227 | - * @param int $post_id The {@link WP_Post} id. |
|
| 228 | - */ |
|
| 229 | - public function relation_changed( $post_id ) { |
|
| 230 | - $this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
| 231 | - |
|
| 232 | - $this->cache_service->delete_cache( $post_id ); |
|
| 233 | - } |
|
| 21 | + /** |
|
| 22 | + * A {@link Wordlift_Post_Converter} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.16.0 |
|
| 25 | + * |
|
| 26 | + * @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance. |
|
| 27 | + */ |
|
| 28 | + private $converter; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A {@link Wordlift_Log_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.16.0 |
|
| 34 | + * |
|
| 35 | + * @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $log; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var Wordlift_Cache_Service |
|
| 41 | + */ |
|
| 42 | + private $cache_service; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Wordlift_Cached_Post_Converter constructor. |
|
| 46 | + * |
|
| 47 | + * @param \Wordlift_Post_Converter $converter |
|
| 48 | + * @param \Wordlift_Cache_Service $cache_service |
|
| 49 | + */ |
|
| 50 | + public function __construct( $converter, $cache_service ) { |
|
| 51 | + |
|
| 52 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 53 | + |
|
| 54 | + $this->converter = $converter; |
|
| 55 | + $this->cache_service = $cache_service; |
|
| 56 | + |
|
| 57 | + $this->init_hooks(); |
|
| 58 | + |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Hooks to catch post/post meta changes in order to invalidate the cache. |
|
| 63 | + * |
|
| 64 | + * @since 3.16.0 |
|
| 65 | + */ |
|
| 66 | + private function init_hooks() { |
|
| 67 | + |
|
| 68 | + // Hook on post save to flush relevant cache. |
|
| 69 | + add_action( 'save_post', array( $this, 'save_post' ) ); |
|
| 70 | + |
|
| 71 | + add_action( 'added_post_meta', array( |
|
| 72 | + $this, |
|
| 73 | + 'changed_post_meta', |
|
| 74 | + ), 10, 2 ); |
|
| 75 | + add_action( 'updated_post_meta', array( |
|
| 76 | + $this, |
|
| 77 | + 'changed_post_meta', |
|
| 78 | + ), 10, 2 ); |
|
| 79 | + add_action( 'deleted_post_meta', array( |
|
| 80 | + $this, |
|
| 81 | + 'changed_post_meta', |
|
| 82 | + ), 10, 2 ); |
|
| 83 | + |
|
| 84 | + // Flush cache when wordlift settings were updated. |
|
| 85 | + add_action( 'update_option_wl_general_settings', array( |
|
| 86 | + $this, |
|
| 87 | + 'update_option_wl_general_settings', |
|
| 88 | + ) ); |
|
| 89 | + |
|
| 90 | + // Invalid cache on relationship change. |
|
| 91 | + add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
| 92 | + add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
| 93 | + |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @inheritdoc |
|
| 98 | + */ |
|
| 99 | + public function convert( $post_id, &$references = array(), &$cache = false ) { |
|
| 100 | + |
|
| 101 | + $this->log->trace( "Converting post $post_id..." ); |
|
| 102 | + |
|
| 103 | + // Try to get a cached result. |
|
| 104 | + $contents = $this->get_cache( $post_id, $references ); |
|
| 105 | + |
|
| 106 | + // Return the cached contents if any. |
|
| 107 | + if ( false !== $contents ) { |
|
| 108 | + $this->log->debug( "Cached contents found for post $post_id." ); |
|
| 109 | + |
|
| 110 | + // Inform the caller that this is cached result. |
|
| 111 | + $cache = true; |
|
| 112 | + |
|
| 113 | + // Return the contents. |
|
| 114 | + return $contents; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $cache = false; |
|
| 118 | + |
|
| 119 | + // Convert the the post. |
|
| 120 | + $jsonld = $this->converter->convert( $post_id, $references ); |
|
| 121 | + |
|
| 122 | + // Cache the results. |
|
| 123 | + $this->set_cache( $post_id, $references, $jsonld ); |
|
| 124 | + |
|
| 125 | + // Finally return the JSON-LD. |
|
| 126 | + return $jsonld; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Try to get the cached contents. |
|
| 131 | + * |
|
| 132 | + * @since 3.16.0 |
|
| 133 | + * |
|
| 134 | + * @param int $post_id The {@link WP_Post} id. |
|
| 135 | + * @param array $references The referenced posts. |
|
| 136 | + * |
|
| 137 | + * @return mixed|bool The cached contents or false if the cached isn't found. |
|
| 138 | + */ |
|
| 139 | + private function get_cache( $post_id, &$references = array() ) { |
|
| 140 | + |
|
| 141 | + $this->log->trace( "Getting cached contents for post $post_id..." ); |
|
| 142 | + |
|
| 143 | + // Get the cache. |
|
| 144 | + $contents = $this->cache_service->get_cache( $post_id ); |
|
| 145 | + |
|
| 146 | + // Bail out if we don't have cached contents or the cached contents are |
|
| 147 | + // invalid. |
|
| 148 | + if ( false === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
| 149 | + $this->log->debug( "Cached contents for post $post_id not found." ); |
|
| 150 | + |
|
| 151 | + return false; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // Remap the cache. |
|
| 155 | + $references = $contents['references']; |
|
| 156 | + |
|
| 157 | + return $contents['jsonld']; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Set the cache with the provided results. |
|
| 162 | + * |
|
| 163 | + * The function will prepare the provided results and will ask the {@link Wordlift_Cache_Service} |
|
| 164 | + * to cache them. |
|
| 165 | + * |
|
| 166 | + * @since 3.16.0 |
|
| 167 | + * |
|
| 168 | + * @param int $post_id The {@link WP_Post} id. |
|
| 169 | + * @param array $references An array of references. |
|
| 170 | + * @param array $jsonld A JSON-LD structure. |
|
| 171 | + */ |
|
| 172 | + private function set_cache( $post_id, $references, $jsonld ) { |
|
| 173 | + |
|
| 174 | + $this->log->trace( "Caching result for post $post_id..." ); |
|
| 175 | + |
|
| 176 | + $this->cache_service->set_cache( $post_id, array( |
|
| 177 | + 'references' => $references, |
|
| 178 | + 'jsonld' => $jsonld, |
|
| 179 | + ) ); |
|
| 180 | + |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Hook to 'save_post', will invalidate the cache for that post. |
|
| 185 | + * |
|
| 186 | + * @since 3.16.0 |
|
| 187 | + * |
|
| 188 | + * @param int $post_id The {@link WP_Post} id. |
|
| 189 | + */ |
|
| 190 | + public function save_post( $post_id ) { |
|
| 191 | + $this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
| 192 | + |
|
| 193 | + $this->cache_service->delete_cache( $post_id ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Hook to meta changed for a {@link WP_Post}, will cause the cause to |
|
| 198 | + * invalidate. |
|
| 199 | + * |
|
| 200 | + * @since 3.16.0 |
|
| 201 | + * |
|
| 202 | + * @param int $id The {@link WP_Post} meta id. |
|
| 203 | + * @param int $post_id The {@link WP_Post} id. |
|
| 204 | + */ |
|
| 205 | + public function changed_post_meta( $id, $post_id ) { |
|
| 206 | + $this->log->trace( "Post $post_id meta changed, invalidating cache..." ); |
|
| 207 | + |
|
| 208 | + $this->cache_service->delete_cache( $post_id ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Hook to WordLift's options changes, will flush the cache. |
|
| 213 | + * |
|
| 214 | + * @since 3.16.0 |
|
| 215 | + */ |
|
| 216 | + public function update_option_wl_general_settings() { |
|
| 217 | + $this->log->trace( "WordLift options changed, flushing cache..." ); |
|
| 218 | + |
|
| 219 | + $this->cache_service->flush(); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Hook to WordLift's post/entity relation changes, will invalidate the cache. |
|
| 224 | + * |
|
| 225 | + * @since 3.16.0 |
|
| 226 | + * |
|
| 227 | + * @param int $post_id The {@link WP_Post} id. |
|
| 228 | + */ |
|
| 229 | + public function relation_changed( $post_id ) { |
|
| 230 | + $this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
| 231 | + |
|
| 232 | + $this->cache_service->delete_cache( $post_id ); |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | 235 | } |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | * @param \Wordlift_Post_Converter $converter |
| 48 | 48 | * @param \Wordlift_Cache_Service $cache_service |
| 49 | 49 | */ |
| 50 | - public function __construct( $converter, $cache_service ) { |
|
| 50 | + public function __construct($converter, $cache_service) { |
|
| 51 | 51 | |
| 52 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 52 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 53 | 53 | |
| 54 | 54 | $this->converter = $converter; |
| 55 | 55 | $this->cache_service = $cache_service; |
@@ -66,46 +66,46 @@ discard block |
||
| 66 | 66 | private function init_hooks() { |
| 67 | 67 | |
| 68 | 68 | // Hook on post save to flush relevant cache. |
| 69 | - add_action( 'save_post', array( $this, 'save_post' ) ); |
|
| 69 | + add_action('save_post', array($this, 'save_post')); |
|
| 70 | 70 | |
| 71 | - add_action( 'added_post_meta', array( |
|
| 71 | + add_action('added_post_meta', array( |
|
| 72 | 72 | $this, |
| 73 | 73 | 'changed_post_meta', |
| 74 | - ), 10, 2 ); |
|
| 75 | - add_action( 'updated_post_meta', array( |
|
| 74 | + ), 10, 2); |
|
| 75 | + add_action('updated_post_meta', array( |
|
| 76 | 76 | $this, |
| 77 | 77 | 'changed_post_meta', |
| 78 | - ), 10, 2 ); |
|
| 79 | - add_action( 'deleted_post_meta', array( |
|
| 78 | + ), 10, 2); |
|
| 79 | + add_action('deleted_post_meta', array( |
|
| 80 | 80 | $this, |
| 81 | 81 | 'changed_post_meta', |
| 82 | - ), 10, 2 ); |
|
| 82 | + ), 10, 2); |
|
| 83 | 83 | |
| 84 | 84 | // Flush cache when wordlift settings were updated. |
| 85 | - add_action( 'update_option_wl_general_settings', array( |
|
| 85 | + add_action('update_option_wl_general_settings', array( |
|
| 86 | 86 | $this, |
| 87 | 87 | 'update_option_wl_general_settings', |
| 88 | - ) ); |
|
| 88 | + )); |
|
| 89 | 89 | |
| 90 | 90 | // Invalid cache on relationship change. |
| 91 | - add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
| 92 | - add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
| 91 | + add_action('wl_relation_added', array($this, 'relation_changed')); |
|
| 92 | + add_action('wl_relation_deleted', array($this, 'relation_changed')); |
|
| 93 | 93 | |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | /** |
| 97 | 97 | * @inheritdoc |
| 98 | 98 | */ |
| 99 | - public function convert( $post_id, &$references = array(), &$cache = false ) { |
|
| 99 | + public function convert($post_id, &$references = array(), &$cache = false) { |
|
| 100 | 100 | |
| 101 | - $this->log->trace( "Converting post $post_id..." ); |
|
| 101 | + $this->log->trace("Converting post $post_id..."); |
|
| 102 | 102 | |
| 103 | 103 | // Try to get a cached result. |
| 104 | - $contents = $this->get_cache( $post_id, $references ); |
|
| 104 | + $contents = $this->get_cache($post_id, $references); |
|
| 105 | 105 | |
| 106 | 106 | // Return the cached contents if any. |
| 107 | - if ( false !== $contents ) { |
|
| 108 | - $this->log->debug( "Cached contents found for post $post_id." ); |
|
| 107 | + if (false !== $contents) { |
|
| 108 | + $this->log->debug("Cached contents found for post $post_id."); |
|
| 109 | 109 | |
| 110 | 110 | // Inform the caller that this is cached result. |
| 111 | 111 | $cache = true; |
@@ -117,10 +117,10 @@ discard block |
||
| 117 | 117 | $cache = false; |
| 118 | 118 | |
| 119 | 119 | // Convert the the post. |
| 120 | - $jsonld = $this->converter->convert( $post_id, $references ); |
|
| 120 | + $jsonld = $this->converter->convert($post_id, $references); |
|
| 121 | 121 | |
| 122 | 122 | // Cache the results. |
| 123 | - $this->set_cache( $post_id, $references, $jsonld ); |
|
| 123 | + $this->set_cache($post_id, $references, $jsonld); |
|
| 124 | 124 | |
| 125 | 125 | // Finally return the JSON-LD. |
| 126 | 126 | return $jsonld; |
@@ -136,17 +136,17 @@ discard block |
||
| 136 | 136 | * |
| 137 | 137 | * @return mixed|bool The cached contents or false if the cached isn't found. |
| 138 | 138 | */ |
| 139 | - private function get_cache( $post_id, &$references = array() ) { |
|
| 139 | + private function get_cache($post_id, &$references = array()) { |
|
| 140 | 140 | |
| 141 | - $this->log->trace( "Getting cached contents for post $post_id..." ); |
|
| 141 | + $this->log->trace("Getting cached contents for post $post_id..."); |
|
| 142 | 142 | |
| 143 | 143 | // Get the cache. |
| 144 | - $contents = $this->cache_service->get_cache( $post_id ); |
|
| 144 | + $contents = $this->cache_service->get_cache($post_id); |
|
| 145 | 145 | |
| 146 | 146 | // Bail out if we don't have cached contents or the cached contents are |
| 147 | 147 | // invalid. |
| 148 | - if ( false === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
| 149 | - $this->log->debug( "Cached contents for post $post_id not found." ); |
|
| 148 | + if (false === $contents || ! isset($contents['jsonld']) || ! isset($contents['references'])) { |
|
| 149 | + $this->log->debug("Cached contents for post $post_id not found."); |
|
| 150 | 150 | |
| 151 | 151 | return false; |
| 152 | 152 | } |
@@ -169,14 +169,14 @@ discard block |
||
| 169 | 169 | * @param array $references An array of references. |
| 170 | 170 | * @param array $jsonld A JSON-LD structure. |
| 171 | 171 | */ |
| 172 | - private function set_cache( $post_id, $references, $jsonld ) { |
|
| 172 | + private function set_cache($post_id, $references, $jsonld) { |
|
| 173 | 173 | |
| 174 | - $this->log->trace( "Caching result for post $post_id..." ); |
|
| 174 | + $this->log->trace("Caching result for post $post_id..."); |
|
| 175 | 175 | |
| 176 | - $this->cache_service->set_cache( $post_id, array( |
|
| 176 | + $this->cache_service->set_cache($post_id, array( |
|
| 177 | 177 | 'references' => $references, |
| 178 | 178 | 'jsonld' => $jsonld, |
| 179 | - ) ); |
|
| 179 | + )); |
|
| 180 | 180 | |
| 181 | 181 | } |
| 182 | 182 | |
@@ -187,10 +187,10 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @param int $post_id The {@link WP_Post} id. |
| 189 | 189 | */ |
| 190 | - public function save_post( $post_id ) { |
|
| 191 | - $this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
| 190 | + public function save_post($post_id) { |
|
| 191 | + $this->log->trace("Post $post_id saved, invalidating cache..."); |
|
| 192 | 192 | |
| 193 | - $this->cache_service->delete_cache( $post_id ); |
|
| 193 | + $this->cache_service->delete_cache($post_id); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | /** |
@@ -202,10 +202,10 @@ discard block |
||
| 202 | 202 | * @param int $id The {@link WP_Post} meta id. |
| 203 | 203 | * @param int $post_id The {@link WP_Post} id. |
| 204 | 204 | */ |
| 205 | - public function changed_post_meta( $id, $post_id ) { |
|
| 206 | - $this->log->trace( "Post $post_id meta changed, invalidating cache..." ); |
|
| 205 | + public function changed_post_meta($id, $post_id) { |
|
| 206 | + $this->log->trace("Post $post_id meta changed, invalidating cache..."); |
|
| 207 | 207 | |
| 208 | - $this->cache_service->delete_cache( $post_id ); |
|
| 208 | + $this->cache_service->delete_cache($post_id); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | /** |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | * @since 3.16.0 |
| 215 | 215 | */ |
| 216 | 216 | public function update_option_wl_general_settings() { |
| 217 | - $this->log->trace( "WordLift options changed, flushing cache..." ); |
|
| 217 | + $this->log->trace("WordLift options changed, flushing cache..."); |
|
| 218 | 218 | |
| 219 | 219 | $this->cache_service->flush(); |
| 220 | 220 | } |
@@ -226,10 +226,10 @@ discard block |
||
| 226 | 226 | * |
| 227 | 227 | * @param int $post_id The {@link WP_Post} id. |
| 228 | 228 | */ |
| 229 | - public function relation_changed( $post_id ) { |
|
| 230 | - $this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
| 229 | + public function relation_changed($post_id) { |
|
| 230 | + $this->log->trace("Post $post_id relations changed, invalidating cache..."); |
|
| 231 | 231 | |
| 232 | - $this->cache_service->delete_cache( $post_id ); |
|
| 232 | + $this->cache_service->delete_cache($post_id); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | } |
@@ -21,18 +21,18 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Deactivator { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Short Description. (use period) |
|
| 26 | - * |
|
| 27 | - * Long Description. |
|
| 28 | - * |
|
| 29 | - * @since 1.0.0 |
|
| 30 | - */ |
|
| 31 | - public static function deactivate() { |
|
| 24 | + /** |
|
| 25 | + * Short Description. (use period) |
|
| 26 | + * |
|
| 27 | + * Long Description. |
|
| 28 | + * |
|
| 29 | + * @since 1.0.0 |
|
| 30 | + */ |
|
| 31 | + public static function deactivate() { |
|
| 32 | 32 | |
| 33 | - // Clear caches. |
|
| 34 | - Wordlift_File_Cache_Service::get_instance()->flush(); |
|
| 33 | + // Clear caches. |
|
| 34 | + Wordlift_File_Cache_Service::get_instance()->flush(); |
|
| 35 | 35 | |
| 36 | - } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | } |
@@ -17,98 +17,98 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Sample_Data_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * An array of sample data. |
|
| 22 | - * |
|
| 23 | - * @since 3.12.0 |
|
| 24 | - * @var array $samples An array of sample data. |
|
| 25 | - */ |
|
| 26 | - private $samples = array( |
|
| 27 | - array( |
|
| 28 | - 'post' => array( |
|
| 29 | - 'post_name' => 'praesent_imperdiet_odio_sed_lectus_vulputate_finibus', |
|
| 30 | - 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
| 31 | - 'post_content' => 'Praesent imperdiet odio sed lectus vulputate finibus. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.', |
|
| 32 | - 'post_type' => 'entity', |
|
| 33 | - 'post_status' => 'publish', |
|
| 34 | - ), |
|
| 35 | - 'entity_type_uri' => 'http://schema.org/Event', |
|
| 36 | - ), |
|
| 37 | - array( |
|
| 38 | - 'post' => array( |
|
| 39 | - 'post_name' => 'nullam_tempor_lectus_sit_amet_tincidunt_euismod', |
|
| 40 | - 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
| 41 | - 'post_content' => 'Nullam tempor lectus sit amet tincidunt euismod. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.', |
|
| 42 | - 'post_type' => 'entity', |
|
| 43 | - 'post_status' => 'publish', |
|
| 44 | - ), |
|
| 45 | - 'entity_type_uri' => 'http://schema.org/Place', |
|
| 46 | - ), |
|
| 47 | - array( |
|
| 48 | - 'post' => array( |
|
| 49 | - 'post_name' => 'praesent_luctus_tincidunt_odio_quis_aliquam', |
|
| 50 | - 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
| 51 | - 'post_content' => 'Praesent luctus tincidunt odio quis aliquam. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.', |
|
| 52 | - 'post_type' => 'entity', |
|
| 53 | - 'post_status' => 'publish', |
|
| 54 | - ), |
|
| 55 | - 'entity_type_uri' => 'http://schema.org/Organization', |
|
| 56 | - ), |
|
| 57 | - array( |
|
| 58 | - 'post' => array( |
|
| 59 | - 'post_name' => 'lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit', |
|
| 60 | - 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
| 61 | - 'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', |
|
| 62 | - 'post_type' => 'entity', |
|
| 63 | - 'post_status' => 'publish', |
|
| 64 | - ), |
|
| 65 | - 'entity_type_uri' => 'http://schema.org/CreativeWork', |
|
| 66 | - ), |
|
| 67 | - array( |
|
| 68 | - 'post' => |
|
| 69 | - array( |
|
| 70 | - 'post_name' => 'post_1', |
|
| 71 | - 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
| 72 | - 'post_content' => '<span><span id="urn:enhancement-da554278-9522-2d83-76ad-8129d2292cb3" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.</span>', |
|
| 73 | - 'post_type' => 'post', |
|
| 74 | - 'post_status' => 'publish', |
|
| 75 | - ), |
|
| 76 | - ), |
|
| 77 | - array( |
|
| 78 | - 'post' => |
|
| 79 | - array( |
|
| 80 | - 'post_name' => 'post_2', |
|
| 81 | - 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
| 82 | - 'post_content' => '<span><span id="urn:local-text-annotation-p8i5o4279ex3rsbwqkrx9z5mh1ox91ae" class="textannotation disambiguated wl-place" itemid="{dataset-uri}/entity/nullam_tempor_lectus_sit_amet_tincidunt_euismod">Nullam tempor lectus sit amet tincidunt euismod</span>. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</span>', |
|
| 83 | - 'post_type' => 'post', |
|
| 84 | - 'post_status' => 'publish', |
|
| 85 | - ), |
|
| 86 | - ), |
|
| 87 | - array( |
|
| 88 | - 'post' => |
|
| 89 | - array( |
|
| 90 | - 'post_name' => 'post_3', |
|
| 91 | - 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
| 92 | - 'post_content' => '<span><span id="urn:enhancement-b3487a20-4696-b6d9-6c55-842445f5c263" class="textannotation disambiguated wl-organization" itemid="{dataset-uri}/entity/praesent_luctus_tincidunt_odio_quis_aliquam">Praesent luctus tincidunt odio quis aliquam</span>. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.</span>', |
|
| 93 | - 'post_type' => 'post', |
|
| 94 | - 'post_status' => 'publish', |
|
| 95 | - ), |
|
| 96 | - ), |
|
| 97 | - array( |
|
| 98 | - 'post' => |
|
| 99 | - array( |
|
| 100 | - 'post_name' => 'post_4', |
|
| 101 | - 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
| 102 | - 'post_content' => '<span><span id="urn:enhancement-4edc3bde-d275-22f9-8d50-0b707596b292" class="textannotation disambiguated wl-thing" itemid="{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit">Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa.</span>', |
|
| 103 | - 'post_type' => 'post', |
|
| 104 | - 'post_status' => 'publish', |
|
| 105 | - ), |
|
| 106 | - ), |
|
| 107 | - array( |
|
| 108 | - 'post' => array( |
|
| 109 | - 'post_name' => 'post_5', |
|
| 110 | - 'post_title' => 'Lorem ipsum', |
|
| 111 | - 'post_content' => " |
|
| 20 | + /** |
|
| 21 | + * An array of sample data. |
|
| 22 | + * |
|
| 23 | + * @since 3.12.0 |
|
| 24 | + * @var array $samples An array of sample data. |
|
| 25 | + */ |
|
| 26 | + private $samples = array( |
|
| 27 | + array( |
|
| 28 | + 'post' => array( |
|
| 29 | + 'post_name' => 'praesent_imperdiet_odio_sed_lectus_vulputate_finibus', |
|
| 30 | + 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
| 31 | + 'post_content' => 'Praesent imperdiet odio sed lectus vulputate finibus. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.', |
|
| 32 | + 'post_type' => 'entity', |
|
| 33 | + 'post_status' => 'publish', |
|
| 34 | + ), |
|
| 35 | + 'entity_type_uri' => 'http://schema.org/Event', |
|
| 36 | + ), |
|
| 37 | + array( |
|
| 38 | + 'post' => array( |
|
| 39 | + 'post_name' => 'nullam_tempor_lectus_sit_amet_tincidunt_euismod', |
|
| 40 | + 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
| 41 | + 'post_content' => 'Nullam tempor lectus sit amet tincidunt euismod. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.', |
|
| 42 | + 'post_type' => 'entity', |
|
| 43 | + 'post_status' => 'publish', |
|
| 44 | + ), |
|
| 45 | + 'entity_type_uri' => 'http://schema.org/Place', |
|
| 46 | + ), |
|
| 47 | + array( |
|
| 48 | + 'post' => array( |
|
| 49 | + 'post_name' => 'praesent_luctus_tincidunt_odio_quis_aliquam', |
|
| 50 | + 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
| 51 | + 'post_content' => 'Praesent luctus tincidunt odio quis aliquam. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.', |
|
| 52 | + 'post_type' => 'entity', |
|
| 53 | + 'post_status' => 'publish', |
|
| 54 | + ), |
|
| 55 | + 'entity_type_uri' => 'http://schema.org/Organization', |
|
| 56 | + ), |
|
| 57 | + array( |
|
| 58 | + 'post' => array( |
|
| 59 | + 'post_name' => 'lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit', |
|
| 60 | + 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
| 61 | + 'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', |
|
| 62 | + 'post_type' => 'entity', |
|
| 63 | + 'post_status' => 'publish', |
|
| 64 | + ), |
|
| 65 | + 'entity_type_uri' => 'http://schema.org/CreativeWork', |
|
| 66 | + ), |
|
| 67 | + array( |
|
| 68 | + 'post' => |
|
| 69 | + array( |
|
| 70 | + 'post_name' => 'post_1', |
|
| 71 | + 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
| 72 | + 'post_content' => '<span><span id="urn:enhancement-da554278-9522-2d83-76ad-8129d2292cb3" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.</span>', |
|
| 73 | + 'post_type' => 'post', |
|
| 74 | + 'post_status' => 'publish', |
|
| 75 | + ), |
|
| 76 | + ), |
|
| 77 | + array( |
|
| 78 | + 'post' => |
|
| 79 | + array( |
|
| 80 | + 'post_name' => 'post_2', |
|
| 81 | + 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
| 82 | + 'post_content' => '<span><span id="urn:local-text-annotation-p8i5o4279ex3rsbwqkrx9z5mh1ox91ae" class="textannotation disambiguated wl-place" itemid="{dataset-uri}/entity/nullam_tempor_lectus_sit_amet_tincidunt_euismod">Nullam tempor lectus sit amet tincidunt euismod</span>. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</span>', |
|
| 83 | + 'post_type' => 'post', |
|
| 84 | + 'post_status' => 'publish', |
|
| 85 | + ), |
|
| 86 | + ), |
|
| 87 | + array( |
|
| 88 | + 'post' => |
|
| 89 | + array( |
|
| 90 | + 'post_name' => 'post_3', |
|
| 91 | + 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
| 92 | + 'post_content' => '<span><span id="urn:enhancement-b3487a20-4696-b6d9-6c55-842445f5c263" class="textannotation disambiguated wl-organization" itemid="{dataset-uri}/entity/praesent_luctus_tincidunt_odio_quis_aliquam">Praesent luctus tincidunt odio quis aliquam</span>. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.</span>', |
|
| 93 | + 'post_type' => 'post', |
|
| 94 | + 'post_status' => 'publish', |
|
| 95 | + ), |
|
| 96 | + ), |
|
| 97 | + array( |
|
| 98 | + 'post' => |
|
| 99 | + array( |
|
| 100 | + 'post_name' => 'post_4', |
|
| 101 | + 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
| 102 | + 'post_content' => '<span><span id="urn:enhancement-4edc3bde-d275-22f9-8d50-0b707596b292" class="textannotation disambiguated wl-thing" itemid="{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit">Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa.</span>', |
|
| 103 | + 'post_type' => 'post', |
|
| 104 | + 'post_status' => 'publish', |
|
| 105 | + ), |
|
| 106 | + ), |
|
| 107 | + array( |
|
| 108 | + 'post' => array( |
|
| 109 | + 'post_name' => 'post_5', |
|
| 110 | + 'post_title' => 'Lorem ipsum', |
|
| 111 | + 'post_content' => " |
|
| 112 | 112 | <span id=\"urn:enhancement-28cb4112-64cf-bd49-ef97-a2ee54727de7\" class=\"textannotation disambiguated wl-thing\" itemid=\"{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit\">Lorem ipsum</span> dolor sit amet, consectetur adipiscing elit. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa. |
| 113 | 113 | |
| 114 | 114 | [wl_navigator] |
@@ -123,190 +123,190 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | <span id=\"urn:local-text-annotation-v0kqdtx685n6cg9jrfvl67amkhm28hxh\" class=\"textannotation disambiguated wl-event\" itemid=\"{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus\">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus. |
| 125 | 125 | ", |
| 126 | - 'post_type' => 'post', |
|
| 127 | - 'post_status' => 'publish', |
|
| 128 | - ), |
|
| 129 | - ), |
|
| 130 | - ); |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 134 | - * |
|
| 135 | - * @since 3.12.0 |
|
| 136 | - * @access private |
|
| 137 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 138 | - */ |
|
| 139 | - private $entity_type_service; |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 143 | - * |
|
| 144 | - * @since 3.12.0 |
|
| 145 | - * @access private |
|
| 146 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 147 | - */ |
|
| 148 | - private $configuration_service; |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Create a {@link Wordlift_Sample_Data_Service} instance. |
|
| 152 | - * |
|
| 153 | - * @since 3.12.0 |
|
| 154 | - * |
|
| 155 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 156 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 157 | - */ |
|
| 158 | - function __construct( $entity_type_service, $configuration_service ) { |
|
| 159 | - |
|
| 160 | - $this->entity_type_service = $entity_type_service; |
|
| 161 | - $this->configuration_service = $configuration_service; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Create sample data in this WordPress instance. |
|
| 166 | - * |
|
| 167 | - * @since 3.12.0 |
|
| 168 | - */ |
|
| 169 | - function create() { |
|
| 170 | - |
|
| 171 | - // Get the source image path. |
|
| 172 | - $source = plugin_dir_path( dirname( __FILE__ ) ) . 'images/rome.png'; |
|
| 173 | - |
|
| 174 | - // Create an attachment with the local file. |
|
| 175 | - $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
| 176 | - |
|
| 177 | - // Add a flag to signal the attachment is sample data and allow easy delete |
|
| 178 | - // afterwards. |
|
| 179 | - add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
| 180 | - |
|
| 181 | - // Get the dataset URI, used for replacements in the `post_content`. |
|
| 182 | - $dataset_uri = $this->configuration_service->get_dataset_uri(); |
|
| 183 | - |
|
| 184 | - // Create 4 entities. |
|
| 185 | - // Create 4 posts referencing each one entity. |
|
| 186 | - // Create 1 post referencing all the entities. |
|
| 187 | - foreach ( $this->samples as $sample ) { |
|
| 188 | - |
|
| 189 | - // Get the post data. |
|
| 190 | - $post = array_replace_recursive( $sample['post'], array( 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ) ) ); |
|
| 191 | - |
|
| 192 | - // Insert the post. |
|
| 193 | - $post_id = wp_insert_post( $post ); |
|
| 194 | - |
|
| 195 | - // Add a flag to signal the post is sample data and allow easy delete |
|
| 196 | - // afterwards. |
|
| 197 | - add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
| 198 | - |
|
| 199 | - // Set the post thumbnail. |
|
| 200 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 201 | - |
|
| 202 | - // If the `entity_type_uri` property is set, set it on the post. |
|
| 203 | - if ( isset( $sample['entity_type_uri'] ) ) { |
|
| 204 | - $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Remove the sample data from this WordPress instance. |
|
| 214 | - * |
|
| 215 | - * @since 3.12.0 |
|
| 216 | - */ |
|
| 217 | - function delete() { |
|
| 218 | - |
|
| 219 | - $this->delete_by_type( 'post' ); |
|
| 220 | - $this->delete_by_type( 'entity' ); |
|
| 221 | - $this->delete_by_type( 'attachment' ); |
|
| 222 | - |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Remove the sample data of the specified type (e.g. `post`, `entity`, `attachment`) |
|
| 227 | - * from the local WordPress instance. |
|
| 228 | - * |
|
| 229 | - * @since 3.12.0 |
|
| 230 | - * |
|
| 231 | - * @param string $type WordPress {@link WP_Post}'s type, e.g. `post`, `entity`, `attachment`. |
|
| 232 | - */ |
|
| 233 | - private function delete_by_type( $type ) { |
|
| 234 | - |
|
| 235 | - $posts = get_posts( array( |
|
| 236 | - 'meta_key' => '_wl_sample_data', |
|
| 237 | - 'meta_value' => 1, |
|
| 238 | - 'post_status' => 'any', |
|
| 239 | - 'post_type' => $type, |
|
| 240 | - ) ); |
|
| 241 | - |
|
| 242 | - foreach ( $posts as $post ) { |
|
| 243 | - wp_delete_post( $post->ID, true ); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Create a WordPress' attachment using the specified file. |
|
| 250 | - * |
|
| 251 | - * @since 3.12.0 |
|
| 252 | - * |
|
| 253 | - * @param string $source The source file path. |
|
| 254 | - * |
|
| 255 | - * @return int WordPress' attachment's id. |
|
| 256 | - */ |
|
| 257 | - private function create_attachment_from_local_file( $source ) { |
|
| 258 | - |
|
| 259 | - // Get the path to the upload directory. |
|
| 260 | - $upload_dir = wp_upload_dir(); |
|
| 261 | - $upload_path = $upload_dir['path']; |
|
| 262 | - |
|
| 263 | - // Get the destination image path. |
|
| 264 | - $destination = $upload_path . '/wl-sample-data.png'; |
|
| 265 | - |
|
| 266 | - // Copy the source file to the destination. |
|
| 267 | - @copy( $source, $destination ); |
|
| 268 | - |
|
| 269 | - return $this->create_attachment( $destination ); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Create a WordPress attachment using the specified file in the upload folder. |
|
| 274 | - * |
|
| 275 | - * @see https://codex.wordpress.org/Function_Reference/wp_insert_attachment |
|
| 276 | - * |
|
| 277 | - * @since 3.12.0 |
|
| 278 | - * |
|
| 279 | - * @param string $filename The image filename. |
|
| 280 | - * |
|
| 281 | - * @return int The attachment id. |
|
| 282 | - */ |
|
| 283 | - private function create_attachment( $filename ) { |
|
| 284 | - |
|
| 285 | - // Check the type of file. We'll use this as the 'post_mime_type'. |
|
| 286 | - $filetype = wp_check_filetype( basename( $filename ), null ); |
|
| 287 | - |
|
| 288 | - // Get the path to the upload directory. |
|
| 289 | - $wp_upload_dir = wp_upload_dir(); |
|
| 290 | - |
|
| 291 | - // Prepare an array of post data for the attachment. |
|
| 292 | - $attachment = array( |
|
| 293 | - 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
| 294 | - 'post_mime_type' => $filetype['type'], |
|
| 295 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
| 296 | - 'post_content' => '', |
|
| 297 | - 'post_status' => 'inherit', |
|
| 298 | - ); |
|
| 299 | - |
|
| 300 | - // Insert the attachment. |
|
| 301 | - $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
| 302 | - |
|
| 303 | - // Generate the metadata for the attachment, and update the database record. |
|
| 304 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 305 | - |
|
| 306 | - // Update the attachment metadata. |
|
| 307 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 308 | - |
|
| 309 | - return $attachment_id; |
|
| 310 | - } |
|
| 126 | + 'post_type' => 'post', |
|
| 127 | + 'post_status' => 'publish', |
|
| 128 | + ), |
|
| 129 | + ), |
|
| 130 | + ); |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 134 | + * |
|
| 135 | + * @since 3.12.0 |
|
| 136 | + * @access private |
|
| 137 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 138 | + */ |
|
| 139 | + private $entity_type_service; |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 143 | + * |
|
| 144 | + * @since 3.12.0 |
|
| 145 | + * @access private |
|
| 146 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 147 | + */ |
|
| 148 | + private $configuration_service; |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Create a {@link Wordlift_Sample_Data_Service} instance. |
|
| 152 | + * |
|
| 153 | + * @since 3.12.0 |
|
| 154 | + * |
|
| 155 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 156 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 157 | + */ |
|
| 158 | + function __construct( $entity_type_service, $configuration_service ) { |
|
| 159 | + |
|
| 160 | + $this->entity_type_service = $entity_type_service; |
|
| 161 | + $this->configuration_service = $configuration_service; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Create sample data in this WordPress instance. |
|
| 166 | + * |
|
| 167 | + * @since 3.12.0 |
|
| 168 | + */ |
|
| 169 | + function create() { |
|
| 170 | + |
|
| 171 | + // Get the source image path. |
|
| 172 | + $source = plugin_dir_path( dirname( __FILE__ ) ) . 'images/rome.png'; |
|
| 173 | + |
|
| 174 | + // Create an attachment with the local file. |
|
| 175 | + $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
| 176 | + |
|
| 177 | + // Add a flag to signal the attachment is sample data and allow easy delete |
|
| 178 | + // afterwards. |
|
| 179 | + add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
| 180 | + |
|
| 181 | + // Get the dataset URI, used for replacements in the `post_content`. |
|
| 182 | + $dataset_uri = $this->configuration_service->get_dataset_uri(); |
|
| 183 | + |
|
| 184 | + // Create 4 entities. |
|
| 185 | + // Create 4 posts referencing each one entity. |
|
| 186 | + // Create 1 post referencing all the entities. |
|
| 187 | + foreach ( $this->samples as $sample ) { |
|
| 188 | + |
|
| 189 | + // Get the post data. |
|
| 190 | + $post = array_replace_recursive( $sample['post'], array( 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ) ) ); |
|
| 191 | + |
|
| 192 | + // Insert the post. |
|
| 193 | + $post_id = wp_insert_post( $post ); |
|
| 194 | + |
|
| 195 | + // Add a flag to signal the post is sample data and allow easy delete |
|
| 196 | + // afterwards. |
|
| 197 | + add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
| 198 | + |
|
| 199 | + // Set the post thumbnail. |
|
| 200 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 201 | + |
|
| 202 | + // If the `entity_type_uri` property is set, set it on the post. |
|
| 203 | + if ( isset( $sample['entity_type_uri'] ) ) { |
|
| 204 | + $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Remove the sample data from this WordPress instance. |
|
| 214 | + * |
|
| 215 | + * @since 3.12.0 |
|
| 216 | + */ |
|
| 217 | + function delete() { |
|
| 218 | + |
|
| 219 | + $this->delete_by_type( 'post' ); |
|
| 220 | + $this->delete_by_type( 'entity' ); |
|
| 221 | + $this->delete_by_type( 'attachment' ); |
|
| 222 | + |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Remove the sample data of the specified type (e.g. `post`, `entity`, `attachment`) |
|
| 227 | + * from the local WordPress instance. |
|
| 228 | + * |
|
| 229 | + * @since 3.12.0 |
|
| 230 | + * |
|
| 231 | + * @param string $type WordPress {@link WP_Post}'s type, e.g. `post`, `entity`, `attachment`. |
|
| 232 | + */ |
|
| 233 | + private function delete_by_type( $type ) { |
|
| 234 | + |
|
| 235 | + $posts = get_posts( array( |
|
| 236 | + 'meta_key' => '_wl_sample_data', |
|
| 237 | + 'meta_value' => 1, |
|
| 238 | + 'post_status' => 'any', |
|
| 239 | + 'post_type' => $type, |
|
| 240 | + ) ); |
|
| 241 | + |
|
| 242 | + foreach ( $posts as $post ) { |
|
| 243 | + wp_delete_post( $post->ID, true ); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Create a WordPress' attachment using the specified file. |
|
| 250 | + * |
|
| 251 | + * @since 3.12.0 |
|
| 252 | + * |
|
| 253 | + * @param string $source The source file path. |
|
| 254 | + * |
|
| 255 | + * @return int WordPress' attachment's id. |
|
| 256 | + */ |
|
| 257 | + private function create_attachment_from_local_file( $source ) { |
|
| 258 | + |
|
| 259 | + // Get the path to the upload directory. |
|
| 260 | + $upload_dir = wp_upload_dir(); |
|
| 261 | + $upload_path = $upload_dir['path']; |
|
| 262 | + |
|
| 263 | + // Get the destination image path. |
|
| 264 | + $destination = $upload_path . '/wl-sample-data.png'; |
|
| 265 | + |
|
| 266 | + // Copy the source file to the destination. |
|
| 267 | + @copy( $source, $destination ); |
|
| 268 | + |
|
| 269 | + return $this->create_attachment( $destination ); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Create a WordPress attachment using the specified file in the upload folder. |
|
| 274 | + * |
|
| 275 | + * @see https://codex.wordpress.org/Function_Reference/wp_insert_attachment |
|
| 276 | + * |
|
| 277 | + * @since 3.12.0 |
|
| 278 | + * |
|
| 279 | + * @param string $filename The image filename. |
|
| 280 | + * |
|
| 281 | + * @return int The attachment id. |
|
| 282 | + */ |
|
| 283 | + private function create_attachment( $filename ) { |
|
| 284 | + |
|
| 285 | + // Check the type of file. We'll use this as the 'post_mime_type'. |
|
| 286 | + $filetype = wp_check_filetype( basename( $filename ), null ); |
|
| 287 | + |
|
| 288 | + // Get the path to the upload directory. |
|
| 289 | + $wp_upload_dir = wp_upload_dir(); |
|
| 290 | + |
|
| 291 | + // Prepare an array of post data for the attachment. |
|
| 292 | + $attachment = array( |
|
| 293 | + 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
| 294 | + 'post_mime_type' => $filetype['type'], |
|
| 295 | + 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
| 296 | + 'post_content' => '', |
|
| 297 | + 'post_status' => 'inherit', |
|
| 298 | + ); |
|
| 299 | + |
|
| 300 | + // Insert the attachment. |
|
| 301 | + $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
| 302 | + |
|
| 303 | + // Generate the metadata for the attachment, and update the database record. |
|
| 304 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 305 | + |
|
| 306 | + // Update the attachment metadata. |
|
| 307 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 308 | + |
|
| 309 | + return $attachment_id; |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | 312 | } |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
| 156 | 156 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 157 | 157 | */ |
| 158 | - function __construct( $entity_type_service, $configuration_service ) { |
|
| 158 | + function __construct($entity_type_service, $configuration_service) { |
|
| 159 | 159 | |
| 160 | 160 | $this->entity_type_service = $entity_type_service; |
| 161 | 161 | $this->configuration_service = $configuration_service; |
@@ -169,14 +169,14 @@ discard block |
||
| 169 | 169 | function create() { |
| 170 | 170 | |
| 171 | 171 | // Get the source image path. |
| 172 | - $source = plugin_dir_path( dirname( __FILE__ ) ) . 'images/rome.png'; |
|
| 172 | + $source = plugin_dir_path(dirname(__FILE__)).'images/rome.png'; |
|
| 173 | 173 | |
| 174 | 174 | // Create an attachment with the local file. |
| 175 | - $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
| 175 | + $attachment_id = $this->create_attachment_from_local_file($source); |
|
| 176 | 176 | |
| 177 | 177 | // Add a flag to signal the attachment is sample data and allow easy delete |
| 178 | 178 | // afterwards. |
| 179 | - add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
| 179 | + add_post_meta($attachment_id, '_wl_sample_data', 1, true); |
|
| 180 | 180 | |
| 181 | 181 | // Get the dataset URI, used for replacements in the `post_content`. |
| 182 | 182 | $dataset_uri = $this->configuration_service->get_dataset_uri(); |
@@ -184,24 +184,24 @@ discard block |
||
| 184 | 184 | // Create 4 entities. |
| 185 | 185 | // Create 4 posts referencing each one entity. |
| 186 | 186 | // Create 1 post referencing all the entities. |
| 187 | - foreach ( $this->samples as $sample ) { |
|
| 187 | + foreach ($this->samples as $sample) { |
|
| 188 | 188 | |
| 189 | 189 | // Get the post data. |
| 190 | - $post = array_replace_recursive( $sample['post'], array( 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ) ) ); |
|
| 190 | + $post = array_replace_recursive($sample['post'], array('post_content' => str_replace('{dataset-uri}', $dataset_uri, $sample['post']['post_content']))); |
|
| 191 | 191 | |
| 192 | 192 | // Insert the post. |
| 193 | - $post_id = wp_insert_post( $post ); |
|
| 193 | + $post_id = wp_insert_post($post); |
|
| 194 | 194 | |
| 195 | 195 | // Add a flag to signal the post is sample data and allow easy delete |
| 196 | 196 | // afterwards. |
| 197 | - add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
| 197 | + add_post_meta($post_id, '_wl_sample_data', 1, true); |
|
| 198 | 198 | |
| 199 | 199 | // Set the post thumbnail. |
| 200 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 200 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 201 | 201 | |
| 202 | 202 | // If the `entity_type_uri` property is set, set it on the post. |
| 203 | - if ( isset( $sample['entity_type_uri'] ) ) { |
|
| 204 | - $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
| 203 | + if (isset($sample['entity_type_uri'])) { |
|
| 204 | + $this->entity_type_service->set($post_id, $sample['entity_type_uri']); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | } |
@@ -216,9 +216,9 @@ discard block |
||
| 216 | 216 | */ |
| 217 | 217 | function delete() { |
| 218 | 218 | |
| 219 | - $this->delete_by_type( 'post' ); |
|
| 220 | - $this->delete_by_type( 'entity' ); |
|
| 221 | - $this->delete_by_type( 'attachment' ); |
|
| 219 | + $this->delete_by_type('post'); |
|
| 220 | + $this->delete_by_type('entity'); |
|
| 221 | + $this->delete_by_type('attachment'); |
|
| 222 | 222 | |
| 223 | 223 | } |
| 224 | 224 | |
@@ -230,17 +230,17 @@ discard block |
||
| 230 | 230 | * |
| 231 | 231 | * @param string $type WordPress {@link WP_Post}'s type, e.g. `post`, `entity`, `attachment`. |
| 232 | 232 | */ |
| 233 | - private function delete_by_type( $type ) { |
|
| 233 | + private function delete_by_type($type) { |
|
| 234 | 234 | |
| 235 | - $posts = get_posts( array( |
|
| 235 | + $posts = get_posts(array( |
|
| 236 | 236 | 'meta_key' => '_wl_sample_data', |
| 237 | 237 | 'meta_value' => 1, |
| 238 | 238 | 'post_status' => 'any', |
| 239 | 239 | 'post_type' => $type, |
| 240 | - ) ); |
|
| 240 | + )); |
|
| 241 | 241 | |
| 242 | - foreach ( $posts as $post ) { |
|
| 243 | - wp_delete_post( $post->ID, true ); |
|
| 242 | + foreach ($posts as $post) { |
|
| 243 | + wp_delete_post($post->ID, true); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | } |
@@ -254,19 +254,19 @@ discard block |
||
| 254 | 254 | * |
| 255 | 255 | * @return int WordPress' attachment's id. |
| 256 | 256 | */ |
| 257 | - private function create_attachment_from_local_file( $source ) { |
|
| 257 | + private function create_attachment_from_local_file($source) { |
|
| 258 | 258 | |
| 259 | 259 | // Get the path to the upload directory. |
| 260 | 260 | $upload_dir = wp_upload_dir(); |
| 261 | 261 | $upload_path = $upload_dir['path']; |
| 262 | 262 | |
| 263 | 263 | // Get the destination image path. |
| 264 | - $destination = $upload_path . '/wl-sample-data.png'; |
|
| 264 | + $destination = $upload_path.'/wl-sample-data.png'; |
|
| 265 | 265 | |
| 266 | 266 | // Copy the source file to the destination. |
| 267 | - @copy( $source, $destination ); |
|
| 267 | + @copy($source, $destination); |
|
| 268 | 268 | |
| 269 | - return $this->create_attachment( $destination ); |
|
| 269 | + return $this->create_attachment($destination); |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | /** |
@@ -280,31 +280,31 @@ discard block |
||
| 280 | 280 | * |
| 281 | 281 | * @return int The attachment id. |
| 282 | 282 | */ |
| 283 | - private function create_attachment( $filename ) { |
|
| 283 | + private function create_attachment($filename) { |
|
| 284 | 284 | |
| 285 | 285 | // Check the type of file. We'll use this as the 'post_mime_type'. |
| 286 | - $filetype = wp_check_filetype( basename( $filename ), null ); |
|
| 286 | + $filetype = wp_check_filetype(basename($filename), null); |
|
| 287 | 287 | |
| 288 | 288 | // Get the path to the upload directory. |
| 289 | 289 | $wp_upload_dir = wp_upload_dir(); |
| 290 | 290 | |
| 291 | 291 | // Prepare an array of post data for the attachment. |
| 292 | 292 | $attachment = array( |
| 293 | - 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
| 293 | + 'guid' => $wp_upload_dir['url'].'/'.basename($filename), |
|
| 294 | 294 | 'post_mime_type' => $filetype['type'], |
| 295 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
| 295 | + 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), |
|
| 296 | 296 | 'post_content' => '', |
| 297 | 297 | 'post_status' => 'inherit', |
| 298 | 298 | ); |
| 299 | 299 | |
| 300 | 300 | // Insert the attachment. |
| 301 | - $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
| 301 | + $attachment_id = wp_insert_attachment($attachment, $filename); |
|
| 302 | 302 | |
| 303 | 303 | // Generate the metadata for the attachment, and update the database record. |
| 304 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 304 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 305 | 305 | |
| 306 | 306 | // Update the attachment metadata. |
| 307 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 307 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 308 | 308 | |
| 309 | 309 | return $attachment_id; |
| 310 | 310 | } |