@@ -15,108 +15,108 @@ |
||
| 15 | 15 | // @@todo: add a hook to clear the cached files now and then. |
| 16 | 16 | class Cacheable_Http_Client extends Simple_Http_Client { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The TTL of cached responses in seconds. |
|
| 20 | - * |
|
| 21 | - * @var int $ttl The TTL in seconds. |
|
| 22 | - * @access private |
|
| 23 | - * @since 1.0.0 |
|
| 24 | - */ |
|
| 25 | - private $ttl; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The cache dir where the cached responses are written. |
|
| 29 | - * |
|
| 30 | - * @since 1.0.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var string $cache_dir The cache dir where the cached responses are written. |
|
| 33 | - */ |
|
| 34 | - private $cache_dir; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * A {@link Wordlift_Log_Service} instance. |
|
| 38 | - * |
|
| 39 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 40 | - * @access private |
|
| 41 | - * @since 1.0.0 |
|
| 42 | - */ |
|
| 43 | - private $log; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Create a {@link Cacheable_Http_Client} with the specified TTL, default 900 secs. |
|
| 47 | - * |
|
| 48 | - * @param int $ttl The cache TTL, default 900 secs. |
|
| 49 | - * |
|
| 50 | - * @since 1.0.0 |
|
| 51 | - */ |
|
| 52 | - public function __construct( $ttl = 900 ) { |
|
| 53 | - |
|
| 54 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 55 | - |
|
| 56 | - $this->ttl = $ttl; |
|
| 57 | - |
|
| 58 | - // Get the temp dir and add the directory separator if missing. |
|
| 59 | - $temp_dir = get_temp_dir(); |
|
| 60 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
| 61 | - $temp_dir .= DIRECTORY_SEPARATOR; |
|
| 62 | - } |
|
| 63 | - $this->cache_dir = $temp_dir . 'wlfb-http-cache'; |
|
| 64 | - |
|
| 65 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
| 66 | - wp_mkdir_p( $this->cache_dir ); |
|
| 67 | - |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @inheritDoc |
|
| 72 | - */ |
|
| 73 | - public function request( $url, $options = array() ) { |
|
| 74 | - |
|
| 75 | - // Create a hash and a path to the cache file. |
|
| 76 | - $hash = md5( $url ) . '-' . md5( serialize( $options ) ); |
|
| 77 | - $filename = $this->get_path( $hash ); |
|
| 78 | - |
|
| 79 | - // If the cache file exists and it's not too old, then return it. |
|
| 80 | - if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
| 81 | - $this->log->trace( "Cache HIT.\n" ); |
|
| 82 | - |
|
| 83 | - return json_decode( file_get_contents( $filename ), true ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $this->log->trace( "Cache MISS for URL $url, hash $hash.\n" ); |
|
| 87 | - |
|
| 88 | - // Get a fresh response and return it. |
|
| 89 | - $response = parent::request( $url, $options ); |
|
| 90 | - |
|
| 91 | - // Return immediately, do not cache. |
|
| 92 | - if ( is_wp_error( $response ) ) { |
|
| 93 | - return $response; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // Do not cache response with invalid status codes or status code different from 2xx. |
|
| 97 | - $code = wp_remote_retrieve_response_code( $response ); |
|
| 98 | - if ( ! is_numeric( $code ) || 2 !== intval( $code ) / 100 ) { |
|
| 99 | - return $response; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Cache. |
|
| 103 | - @unlink( $filename ); |
|
| 104 | - @file_put_contents( $filename, json_encode( $response ) ); |
|
| 105 | - |
|
| 106 | - return $response; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
| 111 | - * |
|
| 112 | - * @param string $hash A file hash. |
|
| 113 | - * |
|
| 114 | - * @return string The full path to the file. |
|
| 115 | - * @since 1.0.0 |
|
| 116 | - */ |
|
| 117 | - private function get_path( $hash ) { |
|
| 118 | - |
|
| 119 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
| 120 | - } |
|
| 18 | + /** |
|
| 19 | + * The TTL of cached responses in seconds. |
|
| 20 | + * |
|
| 21 | + * @var int $ttl The TTL in seconds. |
|
| 22 | + * @access private |
|
| 23 | + * @since 1.0.0 |
|
| 24 | + */ |
|
| 25 | + private $ttl; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The cache dir where the cached responses are written. |
|
| 29 | + * |
|
| 30 | + * @since 1.0.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var string $cache_dir The cache dir where the cached responses are written. |
|
| 33 | + */ |
|
| 34 | + private $cache_dir; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * A {@link Wordlift_Log_Service} instance. |
|
| 38 | + * |
|
| 39 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 40 | + * @access private |
|
| 41 | + * @since 1.0.0 |
|
| 42 | + */ |
|
| 43 | + private $log; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Create a {@link Cacheable_Http_Client} with the specified TTL, default 900 secs. |
|
| 47 | + * |
|
| 48 | + * @param int $ttl The cache TTL, default 900 secs. |
|
| 49 | + * |
|
| 50 | + * @since 1.0.0 |
|
| 51 | + */ |
|
| 52 | + public function __construct( $ttl = 900 ) { |
|
| 53 | + |
|
| 54 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 55 | + |
|
| 56 | + $this->ttl = $ttl; |
|
| 57 | + |
|
| 58 | + // Get the temp dir and add the directory separator if missing. |
|
| 59 | + $temp_dir = get_temp_dir(); |
|
| 60 | + if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
| 61 | + $temp_dir .= DIRECTORY_SEPARATOR; |
|
| 62 | + } |
|
| 63 | + $this->cache_dir = $temp_dir . 'wlfb-http-cache'; |
|
| 64 | + |
|
| 65 | + $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
| 66 | + wp_mkdir_p( $this->cache_dir ); |
|
| 67 | + |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @inheritDoc |
|
| 72 | + */ |
|
| 73 | + public function request( $url, $options = array() ) { |
|
| 74 | + |
|
| 75 | + // Create a hash and a path to the cache file. |
|
| 76 | + $hash = md5( $url ) . '-' . md5( serialize( $options ) ); |
|
| 77 | + $filename = $this->get_path( $hash ); |
|
| 78 | + |
|
| 79 | + // If the cache file exists and it's not too old, then return it. |
|
| 80 | + if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
| 81 | + $this->log->trace( "Cache HIT.\n" ); |
|
| 82 | + |
|
| 83 | + return json_decode( file_get_contents( $filename ), true ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $this->log->trace( "Cache MISS for URL $url, hash $hash.\n" ); |
|
| 87 | + |
|
| 88 | + // Get a fresh response and return it. |
|
| 89 | + $response = parent::request( $url, $options ); |
|
| 90 | + |
|
| 91 | + // Return immediately, do not cache. |
|
| 92 | + if ( is_wp_error( $response ) ) { |
|
| 93 | + return $response; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // Do not cache response with invalid status codes or status code different from 2xx. |
|
| 97 | + $code = wp_remote_retrieve_response_code( $response ); |
|
| 98 | + if ( ! is_numeric( $code ) || 2 !== intval( $code ) / 100 ) { |
|
| 99 | + return $response; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Cache. |
|
| 103 | + @unlink( $filename ); |
|
| 104 | + @file_put_contents( $filename, json_encode( $response ) ); |
|
| 105 | + |
|
| 106 | + return $response; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
| 111 | + * |
|
| 112 | + * @param string $hash A file hash. |
|
| 113 | + * |
|
| 114 | + * @return string The full path to the file. |
|
| 115 | + * @since 1.0.0 |
|
| 116 | + */ |
|
| 117 | + private function get_path( $hash ) { |
|
| 118 | + |
|
| 119 | + return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | 122 | } |
@@ -49,59 +49,59 @@ discard block |
||
| 49 | 49 | * |
| 50 | 50 | * @since 1.0.0 |
| 51 | 51 | */ |
| 52 | - public function __construct( $ttl = 900 ) { |
|
| 52 | + public function __construct($ttl = 900) { |
|
| 53 | 53 | |
| 54 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 54 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 55 | 55 | |
| 56 | 56 | $this->ttl = $ttl; |
| 57 | 57 | |
| 58 | 58 | // Get the temp dir and add the directory separator if missing. |
| 59 | 59 | $temp_dir = get_temp_dir(); |
| 60 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
| 60 | + if (DIRECTORY_SEPARATOR !== substr($temp_dir, - strlen(DIRECTORY_SEPARATOR))) { |
|
| 61 | 61 | $temp_dir .= DIRECTORY_SEPARATOR; |
| 62 | 62 | } |
| 63 | - $this->cache_dir = $temp_dir . 'wlfb-http-cache'; |
|
| 63 | + $this->cache_dir = $temp_dir.'wlfb-http-cache'; |
|
| 64 | 64 | |
| 65 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
| 66 | - wp_mkdir_p( $this->cache_dir ); |
|
| 65 | + $this->log->trace("Creating the cache folder {$this->cache_dir}..."); |
|
| 66 | + wp_mkdir_p($this->cache_dir); |
|
| 67 | 67 | |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * @inheritDoc |
| 72 | 72 | */ |
| 73 | - public function request( $url, $options = array() ) { |
|
| 73 | + public function request($url, $options = array()) { |
|
| 74 | 74 | |
| 75 | 75 | // Create a hash and a path to the cache file. |
| 76 | - $hash = md5( $url ) . '-' . md5( serialize( $options ) ); |
|
| 77 | - $filename = $this->get_path( $hash ); |
|
| 76 | + $hash = md5($url).'-'.md5(serialize($options)); |
|
| 77 | + $filename = $this->get_path($hash); |
|
| 78 | 78 | |
| 79 | 79 | // If the cache file exists and it's not too old, then return it. |
| 80 | - if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
| 81 | - $this->log->trace( "Cache HIT.\n" ); |
|
| 80 | + if (file_exists($filename) && $this->ttl >= time() - filemtime($filename)) { |
|
| 81 | + $this->log->trace("Cache HIT.\n"); |
|
| 82 | 82 | |
| 83 | - return json_decode( file_get_contents( $filename ), true ); |
|
| 83 | + return json_decode(file_get_contents($filename), true); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - $this->log->trace( "Cache MISS for URL $url, hash $hash.\n" ); |
|
| 86 | + $this->log->trace("Cache MISS for URL $url, hash $hash.\n"); |
|
| 87 | 87 | |
| 88 | 88 | // Get a fresh response and return it. |
| 89 | - $response = parent::request( $url, $options ); |
|
| 89 | + $response = parent::request($url, $options); |
|
| 90 | 90 | |
| 91 | 91 | // Return immediately, do not cache. |
| 92 | - if ( is_wp_error( $response ) ) { |
|
| 92 | + if (is_wp_error($response)) { |
|
| 93 | 93 | return $response; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | // Do not cache response with invalid status codes or status code different from 2xx. |
| 97 | - $code = wp_remote_retrieve_response_code( $response ); |
|
| 98 | - if ( ! is_numeric( $code ) || 2 !== intval( $code ) / 100 ) { |
|
| 97 | + $code = wp_remote_retrieve_response_code($response); |
|
| 98 | + if ( ! is_numeric($code) || 2 !== intval($code) / 100) { |
|
| 99 | 99 | return $response; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // Cache. |
| 103 | - @unlink( $filename ); |
|
| 104 | - @file_put_contents( $filename, json_encode( $response ) ); |
|
| 103 | + @unlink($filename); |
|
| 104 | + @file_put_contents($filename, json_encode($response)); |
|
| 105 | 105 | |
| 106 | 106 | return $response; |
| 107 | 107 | } |
@@ -114,9 +114,9 @@ discard block |
||
| 114 | 114 | * @return string The full path to the file. |
| 115 | 115 | * @since 1.0.0 |
| 116 | 116 | */ |
| 117 | - private function get_path( $hash ) { |
|
| 117 | + private function get_path($hash) { |
|
| 118 | 118 | |
| 119 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
| 119 | + return $this->cache_dir.DIRECTORY_SEPARATOR.$hash; |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | } |
@@ -10,22 +10,22 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Simple_Http_Client implements Http_Client { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @inheritDoc |
|
| 15 | - */ |
|
| 16 | - public function get( $url, $options = array() ) { |
|
| 13 | + /** |
|
| 14 | + * @inheritDoc |
|
| 15 | + */ |
|
| 16 | + public function get( $url, $options = array() ) { |
|
| 17 | 17 | |
| 18 | - $options['method'] = 'GET'; |
|
| 18 | + $options['method'] = 'GET'; |
|
| 19 | 19 | |
| 20 | - return $this->request( $url, $options ); |
|
| 21 | - } |
|
| 20 | + return $this->request( $url, $options ); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @inheritDoc |
|
| 25 | - */ |
|
| 26 | - public function request( $url, $options = array() ) { |
|
| 23 | + /** |
|
| 24 | + * @inheritDoc |
|
| 25 | + */ |
|
| 26 | + public function request( $url, $options = array() ) { |
|
| 27 | 27 | |
| 28 | - return wp_remote_request( $url, $options ); |
|
| 29 | - } |
|
| 28 | + return wp_remote_request( $url, $options ); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | 31 | } |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | /** |
| 14 | 14 | * @inheritDoc |
| 15 | 15 | */ |
| 16 | - public function get( $url, $options = array() ) { |
|
| 16 | + public function get($url, $options = array()) { |
|
| 17 | 17 | |
| 18 | 18 | $options['method'] = 'GET'; |
| 19 | 19 | |
| 20 | - return $this->request( $url, $options ); |
|
| 20 | + return $this->request($url, $options); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * @inheritDoc |
| 25 | 25 | */ |
| 26 | - public function request( $url, $options = array() ) { |
|
| 26 | + public function request($url, $options = array()) { |
|
| 27 | 27 | |
| 28 | - return wp_remote_request( $url, $options ); |
|
| 28 | + return wp_remote_request($url, $options); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | } |
@@ -14,32 +14,32 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Install_3_23_4 extends Wordlift_Install { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - protected static $version = '3.23.4'; |
|
| 17 | + /** |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + protected static $version = '3.23.4'; |
|
| 21 | 21 | |
| 22 | - public function install() { |
|
| 22 | + public function install() { |
|
| 23 | 23 | |
| 24 | - $existing_term = get_term_by( 'slug', 'web-page', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 24 | + $existing_term = get_term_by( 'slug', 'web-page', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 25 | 25 | |
| 26 | - // Bail out if term exists. |
|
| 27 | - if ( false !== $existing_term ) { |
|
| 28 | - return; |
|
| 29 | - } |
|
| 26 | + // Bail out if term exists. |
|
| 27 | + if ( false !== $existing_term ) { |
|
| 28 | + return; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - $term = wp_insert_term( |
|
| 32 | - 'WebPage', |
|
| 33 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 34 | - array( |
|
| 35 | - 'slug' => 'web-page', |
|
| 36 | - 'description' => 'A Web Page.', |
|
| 37 | - ) |
|
| 38 | - ); |
|
| 31 | + $term = wp_insert_term( |
|
| 32 | + 'WebPage', |
|
| 33 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 34 | + array( |
|
| 35 | + 'slug' => 'web-page', |
|
| 36 | + 'description' => 'A Web Page.', |
|
| 37 | + ) |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - update_term_meta( $term['term_id'], '_wl_name', 'WebPage' ); |
|
| 41 | - update_term_meta( $term['term_id'], '_wl_uri', "http://schema.org/WebPage" ); |
|
| 40 | + update_term_meta( $term['term_id'], '_wl_name', 'WebPage' ); |
|
| 41 | + update_term_meta( $term['term_id'], '_wl_uri', "http://schema.org/WebPage" ); |
|
| 42 | 42 | |
| 43 | - } |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | } |
@@ -21,10 +21,10 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | public function install() { |
| 23 | 23 | |
| 24 | - $existing_term = get_term_by( 'slug', 'web-page', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 24 | + $existing_term = get_term_by('slug', 'web-page', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 25 | 25 | |
| 26 | 26 | // Bail out if term exists. |
| 27 | - if ( false !== $existing_term ) { |
|
| 27 | + if (false !== $existing_term) { |
|
| 28 | 28 | return; |
| 29 | 29 | } |
| 30 | 30 | |
@@ -37,8 +37,8 @@ discard block |
||
| 37 | 37 | ) |
| 38 | 38 | ); |
| 39 | 39 | |
| 40 | - update_term_meta( $term['term_id'], '_wl_name', 'WebPage' ); |
|
| 41 | - update_term_meta( $term['term_id'], '_wl_uri', "http://schema.org/WebPage" ); |
|
| 40 | + update_term_meta($term['term_id'], '_wl_name', 'WebPage'); |
|
| 41 | + update_term_meta($term['term_id'], '_wl_uri', "http://schema.org/WebPage"); |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | |
@@ -16,231 +16,231 @@ |
||
| 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} registered instances. |
|
| 48 | - * |
|
| 49 | - * Each {@link Wordlift_File_Cache_Service} adds itself to the registered |
|
| 50 | - * instances. |
|
| 51 | - * |
|
| 52 | - * @since 3.16.3 |
|
| 53 | - * @access private |
|
| 54 | - * @var array $instances An array of {@link Wordlift_File_Cache_Service} instances. |
|
| 55 | - */ |
|
| 56 | - private static $instances = array(); |
|
| 57 | - |
|
| 58 | - private static $instance; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Create a {@link Wordlift_File_Cache_Service} instance. |
|
| 62 | - * |
|
| 63 | - * The File Cache Service requires a base cache directory (to which a unique |
|
| 64 | - * id for the current site will be appended) and a file extension for cache |
|
| 65 | - * files (by default `.wlcache`) is used. |
|
| 66 | - * |
|
| 67 | - * @param string $cache_dir The base cache directory. |
|
| 68 | - * @param string $file_extension The file extension, by default `.wlcache`. |
|
| 69 | - * |
|
| 70 | - * @since 3.16.0 |
|
| 71 | - * |
|
| 72 | - */ |
|
| 73 | - public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
| 74 | - |
|
| 75 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 76 | - |
|
| 77 | - // Set the cache directory using the base directory provided by the caller |
|
| 78 | - // and appending a hash for the unique site id. |
|
| 79 | - $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
| 80 | - $this->file_extension = $file_extension; |
|
| 81 | - |
|
| 82 | - // Create the cache dir. |
|
| 83 | - if ( ! file_exists( $this->cache_dir ) ) { |
|
| 84 | - @mkdir( $this->cache_dir, 0755, true ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // Add ourselves to the list of instances. |
|
| 88 | - self::$instances[] = $this; |
|
| 89 | - |
|
| 90 | - // Initialize the singleton and the ajax method. |
|
| 91 | - if ( ! isset( self::$instance ) ) { |
|
| 92 | - self::$instance = $this; |
|
| 93 | - |
|
| 94 | - add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
| 98 | - |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the cached response for the specified `id`. |
|
| 103 | - * |
|
| 104 | - * @param int $id The cache `id`. |
|
| 105 | - * |
|
| 106 | - * @return mixed|false The cached contents or false if the cache isn't found. |
|
| 107 | - * @since 3.16.0 |
|
| 108 | - * |
|
| 109 | - */ |
|
| 110 | - function get_cache( $id ) { |
|
| 111 | - |
|
| 112 | - // Bail out if we don't have the cache. |
|
| 113 | - if ( ! $this->has_cache( $id ) ) { |
|
| 114 | - return false; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - // Get the filename. |
|
| 118 | - $filename = $this->get_filename( $id ); |
|
| 119 | - |
|
| 120 | - $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
| 121 | - |
|
| 122 | - // Try to decode the contents. |
|
| 123 | - $contents = json_decode( file_get_contents( $filename ), true ); |
|
| 124 | - |
|
| 125 | - // Return false if decoding failed, otherwise the decoded contents. |
|
| 126 | - return $contents ?: false; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Set the cache contents for the specified `id`. |
|
| 131 | - * |
|
| 132 | - * @param int $id The cache id. |
|
| 133 | - * |
|
| 134 | - * @return bool True if the `id` has a cache. |
|
| 135 | - * @since 3.16.0 |
|
| 136 | - * |
|
| 137 | - */ |
|
| 138 | - function has_cache( $id ) { |
|
| 139 | - |
|
| 140 | - // Get the filename. |
|
| 141 | - $filename = $this->get_filename( $id ); |
|
| 142 | - |
|
| 143 | - // Bail out if the file doesn't exist. |
|
| 144 | - return file_exists( $filename ); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @inheritdoc |
|
| 149 | - */ |
|
| 150 | - function set_cache( $id, $contents ) { |
|
| 151 | - |
|
| 152 | - $filename = $this->get_filename( $id ); |
|
| 153 | - |
|
| 154 | - $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
| 155 | - |
|
| 156 | - @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
| 157 | - |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Delete the cache for the specified `id`. |
|
| 162 | - * |
|
| 163 | - * @param int $id The cache `id`. |
|
| 164 | - * |
|
| 165 | - * @since 3.16.0 |
|
| 166 | - * |
|
| 167 | - */ |
|
| 168 | - function delete_cache( $id ) { |
|
| 169 | - |
|
| 170 | - $filename = $this->get_filename( $id ); |
|
| 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} registered instances. |
|
| 48 | + * |
|
| 49 | + * Each {@link Wordlift_File_Cache_Service} adds itself to the registered |
|
| 50 | + * instances. |
|
| 51 | + * |
|
| 52 | + * @since 3.16.3 |
|
| 53 | + * @access private |
|
| 54 | + * @var array $instances An array of {@link Wordlift_File_Cache_Service} instances. |
|
| 55 | + */ |
|
| 56 | + private static $instances = array(); |
|
| 57 | + |
|
| 58 | + private static $instance; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Create a {@link Wordlift_File_Cache_Service} instance. |
|
| 62 | + * |
|
| 63 | + * The File Cache Service requires a base cache directory (to which a unique |
|
| 64 | + * id for the current site will be appended) and a file extension for cache |
|
| 65 | + * files (by default `.wlcache`) is used. |
|
| 66 | + * |
|
| 67 | + * @param string $cache_dir The base cache directory. |
|
| 68 | + * @param string $file_extension The file extension, by default `.wlcache`. |
|
| 69 | + * |
|
| 70 | + * @since 3.16.0 |
|
| 71 | + * |
|
| 72 | + */ |
|
| 73 | + public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
| 74 | + |
|
| 75 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 76 | + |
|
| 77 | + // Set the cache directory using the base directory provided by the caller |
|
| 78 | + // and appending a hash for the unique site id. |
|
| 79 | + $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
| 80 | + $this->file_extension = $file_extension; |
|
| 81 | + |
|
| 82 | + // Create the cache dir. |
|
| 83 | + if ( ! file_exists( $this->cache_dir ) ) { |
|
| 84 | + @mkdir( $this->cache_dir, 0755, true ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // Add ourselves to the list of instances. |
|
| 88 | + self::$instances[] = $this; |
|
| 89 | + |
|
| 90 | + // Initialize the singleton and the ajax method. |
|
| 91 | + if ( ! isset( self::$instance ) ) { |
|
| 92 | + self::$instance = $this; |
|
| 93 | + |
|
| 94 | + add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
| 98 | + |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the cached response for the specified `id`. |
|
| 103 | + * |
|
| 104 | + * @param int $id The cache `id`. |
|
| 105 | + * |
|
| 106 | + * @return mixed|false The cached contents or false if the cache isn't found. |
|
| 107 | + * @since 3.16.0 |
|
| 108 | + * |
|
| 109 | + */ |
|
| 110 | + function get_cache( $id ) { |
|
| 111 | + |
|
| 112 | + // Bail out if we don't have the cache. |
|
| 113 | + if ( ! $this->has_cache( $id ) ) { |
|
| 114 | + return false; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + // Get the filename. |
|
| 118 | + $filename = $this->get_filename( $id ); |
|
| 119 | + |
|
| 120 | + $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
| 121 | + |
|
| 122 | + // Try to decode the contents. |
|
| 123 | + $contents = json_decode( file_get_contents( $filename ), true ); |
|
| 124 | + |
|
| 125 | + // Return false if decoding failed, otherwise the decoded contents. |
|
| 126 | + return $contents ?: false; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Set the cache contents for the specified `id`. |
|
| 131 | + * |
|
| 132 | + * @param int $id The cache id. |
|
| 133 | + * |
|
| 134 | + * @return bool True if the `id` has a cache. |
|
| 135 | + * @since 3.16.0 |
|
| 136 | + * |
|
| 137 | + */ |
|
| 138 | + function has_cache( $id ) { |
|
| 139 | + |
|
| 140 | + // Get the filename. |
|
| 141 | + $filename = $this->get_filename( $id ); |
|
| 142 | + |
|
| 143 | + // Bail out if the file doesn't exist. |
|
| 144 | + return file_exists( $filename ); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @inheritdoc |
|
| 149 | + */ |
|
| 150 | + function set_cache( $id, $contents ) { |
|
| 151 | + |
|
| 152 | + $filename = $this->get_filename( $id ); |
|
| 153 | + |
|
| 154 | + $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
| 155 | + |
|
| 156 | + @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
| 157 | + |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Delete the cache for the specified `id`. |
|
| 162 | + * |
|
| 163 | + * @param int $id The cache `id`. |
|
| 164 | + * |
|
| 165 | + * @since 3.16.0 |
|
| 166 | + * |
|
| 167 | + */ |
|
| 168 | + function delete_cache( $id ) { |
|
| 169 | + |
|
| 170 | + $filename = $this->get_filename( $id ); |
|
| 171 | 171 | |
| 172 | - $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
| 172 | + $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
| 173 | 173 | |
| 174 | - if ( file_exists( $filename ) ) { |
|
| 175 | - @unlink( $filename ); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - } |
|
| 174 | + if ( file_exists( $filename ) ) { |
|
| 175 | + @unlink( $filename ); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Flush the whole cache. |
|
| 182 | - * |
|
| 183 | - * @since 3.16.0 |
|
| 184 | - */ |
|
| 185 | - function flush() { |
|
| 180 | + /** |
|
| 181 | + * Flush the whole cache. |
|
| 182 | + * |
|
| 183 | + * @since 3.16.0 |
|
| 184 | + */ |
|
| 185 | + function flush() { |
|
| 186 | 186 | |
| 187 | - // Bail out if the cache dir isn't set. |
|
| 188 | - if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
| 189 | - return; |
|
| 190 | - } |
|
| 187 | + // Bail out if the cache dir isn't set. |
|
| 188 | + if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
| 189 | + return; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
| 192 | + $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
| 193 | 193 | |
| 194 | - $handle = @opendir( $this->cache_dir ); |
|
| 194 | + $handle = @opendir( $this->cache_dir ); |
|
| 195 | 195 | |
| 196 | - // Bail out if the directory can't be opened. |
|
| 197 | - if ( false === $handle ) { |
|
| 198 | - return; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - // Calculate the file extension length for matching file names. |
|
| 202 | - $file_extension_length = strlen( $this->file_extension ); |
|
| 203 | - |
|
| 204 | - // Loop into the directory to delete files. |
|
| 205 | - while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 206 | - if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
| 207 | - && file_exists( $this->cache_dir . $entry ) ) { |
|
| 208 | - $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
| 196 | + // Bail out if the directory can't be opened. |
|
| 197 | + if ( false === $handle ) { |
|
| 198 | + return; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + // Calculate the file extension length for matching file names. |
|
| 202 | + $file_extension_length = strlen( $this->file_extension ); |
|
| 203 | + |
|
| 204 | + // Loop into the directory to delete files. |
|
| 205 | + while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 206 | + if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
| 207 | + && file_exists( $this->cache_dir . $entry ) ) { |
|
| 208 | + $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
| 209 | 209 | |
| 210 | - @unlink( $this->cache_dir . $entry ); |
|
| 211 | - } |
|
| 212 | - } |
|
| 210 | + @unlink( $this->cache_dir . $entry ); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - // Finally closed the directory. |
|
| 215 | - closedir( $handle ); |
|
| 214 | + // Finally closed the directory. |
|
| 215 | + closedir( $handle ); |
|
| 216 | 216 | |
| 217 | - } |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - public static function flush_all() { |
|
| 219 | + public static function flush_all() { |
|
| 220 | 220 | |
| 221 | - foreach ( self::$instances as $instance ) { |
|
| 222 | - $instance->flush(); |
|
| 223 | - } |
|
| 221 | + foreach ( self::$instances as $instance ) { |
|
| 222 | + $instance->flush(); |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | - if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
| 226 | - && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { |
|
| 227 | - wp_send_json_success(); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - } |
|
| 225 | + if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
| 226 | + && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { |
|
| 227 | + wp_send_json_success(); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Get the filename holding the cache contents for the specified `id`. |
|
| 234 | - * |
|
| 235 | - * @param int $id The cache `id`. |
|
| 236 | - * |
|
| 237 | - * @return string The filename. |
|
| 238 | - * @since 3.16.0 |
|
| 239 | - * |
|
| 240 | - */ |
|
| 241 | - private function get_filename( $id ) { |
|
| 232 | + /** |
|
| 233 | + * Get the filename holding the cache contents for the specified `id`. |
|
| 234 | + * |
|
| 235 | + * @param int $id The cache `id`. |
|
| 236 | + * |
|
| 237 | + * @return string The filename. |
|
| 238 | + * @since 3.16.0 |
|
| 239 | + * |
|
| 240 | + */ |
|
| 241 | + private function get_filename( $id ) { |
|
| 242 | 242 | |
| 243 | - return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
| 244 | - } |
|
| 243 | + return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | 246 | } |
@@ -70,31 +70,31 @@ discard block |
||
| 70 | 70 | * @since 3.16.0 |
| 71 | 71 | * |
| 72 | 72 | */ |
| 73 | - public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
| 73 | + public function __construct($cache_dir, $file_extension = '.wlcache') { |
|
| 74 | 74 | |
| 75 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 75 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 76 | 76 | |
| 77 | 77 | // Set the cache directory using the base directory provided by the caller |
| 78 | 78 | // and appending a hash for the unique site id. |
| 79 | - $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
| 79 | + $this->cache_dir = trailingslashit($cache_dir).md5(get_site_url()).'/'; |
|
| 80 | 80 | $this->file_extension = $file_extension; |
| 81 | 81 | |
| 82 | 82 | // Create the cache dir. |
| 83 | - if ( ! file_exists( $this->cache_dir ) ) { |
|
| 84 | - @mkdir( $this->cache_dir, 0755, true ); |
|
| 83 | + if ( ! file_exists($this->cache_dir)) { |
|
| 84 | + @mkdir($this->cache_dir, 0755, true); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // Add ourselves to the list of instances. |
| 88 | 88 | self::$instances[] = $this; |
| 89 | 89 | |
| 90 | 90 | // Initialize the singleton and the ajax method. |
| 91 | - if ( ! isset( self::$instance ) ) { |
|
| 91 | + if ( ! isset(self::$instance)) { |
|
| 92 | 92 | self::$instance = $this; |
| 93 | 93 | |
| 94 | - add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
| 94 | + add_action('wp_ajax_wl_file_cache__flush_all', array('Wordlift_File_Cache_Service', 'flush_all')); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
| 97 | + $this->log->debug("File Cache service initialized on $this->cache_dir."); |
|
| 98 | 98 | |
| 99 | 99 | } |
| 100 | 100 | |
@@ -107,20 +107,20 @@ discard block |
||
| 107 | 107 | * @since 3.16.0 |
| 108 | 108 | * |
| 109 | 109 | */ |
| 110 | - function get_cache( $id ) { |
|
| 110 | + function get_cache($id) { |
|
| 111 | 111 | |
| 112 | 112 | // Bail out if we don't have the cache. |
| 113 | - if ( ! $this->has_cache( $id ) ) { |
|
| 113 | + if ( ! $this->has_cache($id)) { |
|
| 114 | 114 | return false; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // Get the filename. |
| 118 | - $filename = $this->get_filename( $id ); |
|
| 118 | + $filename = $this->get_filename($id); |
|
| 119 | 119 | |
| 120 | - $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
| 120 | + $this->log->trace("Trying to get cache contents for $id from $filename..."); |
|
| 121 | 121 | |
| 122 | 122 | // Try to decode the contents. |
| 123 | - $contents = json_decode( file_get_contents( $filename ), true ); |
|
| 123 | + $contents = json_decode(file_get_contents($filename), true); |
|
| 124 | 124 | |
| 125 | 125 | // Return false if decoding failed, otherwise the decoded contents. |
| 126 | 126 | return $contents ?: false; |
@@ -135,25 +135,25 @@ discard block |
||
| 135 | 135 | * @since 3.16.0 |
| 136 | 136 | * |
| 137 | 137 | */ |
| 138 | - function has_cache( $id ) { |
|
| 138 | + function has_cache($id) { |
|
| 139 | 139 | |
| 140 | 140 | // Get the filename. |
| 141 | - $filename = $this->get_filename( $id ); |
|
| 141 | + $filename = $this->get_filename($id); |
|
| 142 | 142 | |
| 143 | 143 | // Bail out if the file doesn't exist. |
| 144 | - return file_exists( $filename ); |
|
| 144 | + return file_exists($filename); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * @inheritdoc |
| 149 | 149 | */ |
| 150 | - function set_cache( $id, $contents ) { |
|
| 150 | + function set_cache($id, $contents) { |
|
| 151 | 151 | |
| 152 | - $filename = $this->get_filename( $id ); |
|
| 152 | + $filename = $this->get_filename($id); |
|
| 153 | 153 | |
| 154 | - $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
| 154 | + $this->log->trace("Writing cache contents for $id to $filename..."); |
|
| 155 | 155 | |
| 156 | - @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
| 156 | + @file_put_contents($filename, wp_json_encode($contents)); |
|
| 157 | 157 | |
| 158 | 158 | } |
| 159 | 159 | |
@@ -165,14 +165,14 @@ discard block |
||
| 165 | 165 | * @since 3.16.0 |
| 166 | 166 | * |
| 167 | 167 | */ |
| 168 | - function delete_cache( $id ) { |
|
| 168 | + function delete_cache($id) { |
|
| 169 | 169 | |
| 170 | - $filename = $this->get_filename( $id ); |
|
| 170 | + $filename = $this->get_filename($id); |
|
| 171 | 171 | |
| 172 | - $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
| 172 | + $this->log->trace("Deleting cache contents for $id, file $filename..."); |
|
| 173 | 173 | |
| 174 | - if ( file_exists( $filename ) ) { |
|
| 175 | - @unlink( $filename ); |
|
| 174 | + if (file_exists($filename)) { |
|
| 175 | + @unlink($filename); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | } |
@@ -185,45 +185,45 @@ discard block |
||
| 185 | 185 | function flush() { |
| 186 | 186 | |
| 187 | 187 | // Bail out if the cache dir isn't set. |
| 188 | - if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
| 188 | + if (empty($this->cache_dir) || '/' === $this->cache_dir) { |
|
| 189 | 189 | return; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
| 192 | + $this->log->trace("Flushing cache contents from $this->cache_dir..."); |
|
| 193 | 193 | |
| 194 | - $handle = @opendir( $this->cache_dir ); |
|
| 194 | + $handle = @opendir($this->cache_dir); |
|
| 195 | 195 | |
| 196 | 196 | // Bail out if the directory can't be opened. |
| 197 | - if ( false === $handle ) { |
|
| 197 | + if (false === $handle) { |
|
| 198 | 198 | return; |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | // Calculate the file extension length for matching file names. |
| 202 | - $file_extension_length = strlen( $this->file_extension ); |
|
| 202 | + $file_extension_length = strlen($this->file_extension); |
|
| 203 | 203 | |
| 204 | 204 | // Loop into the directory to delete files. |
| 205 | - while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 206 | - if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
| 207 | - && file_exists( $this->cache_dir . $entry ) ) { |
|
| 208 | - $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
| 205 | + while (false !== ($entry = readdir($handle))) { |
|
| 206 | + if (substr($entry, - $file_extension_length) === $this->file_extension |
|
| 207 | + && file_exists($this->cache_dir.$entry)) { |
|
| 208 | + $this->log->trace("Deleting file {$this->cache_dir}{$entry}..."); |
|
| 209 | 209 | |
| 210 | - @unlink( $this->cache_dir . $entry ); |
|
| 210 | + @unlink($this->cache_dir.$entry); |
|
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | // Finally closed the directory. |
| 215 | - closedir( $handle ); |
|
| 215 | + closedir($handle); |
|
| 216 | 216 | |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | public static function flush_all() { |
| 220 | 220 | |
| 221 | - foreach ( self::$instances as $instance ) { |
|
| 221 | + foreach (self::$instances as $instance) { |
|
| 222 | 222 | $instance->flush(); |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
| 226 | - && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { |
|
| 225 | + if (defined('DOING_AJAX') && DOING_AJAX |
|
| 226 | + && isset($_REQUEST['action']) && 'wl_file_cache__flush_all' === $_REQUEST['action']) { |
|
| 227 | 227 | wp_send_json_success(); |
| 228 | 228 | } |
| 229 | 229 | |
@@ -238,9 +238,9 @@ discard block |
||
| 238 | 238 | * @since 3.16.0 |
| 239 | 239 | * |
| 240 | 240 | */ |
| 241 | - private function get_filename( $id ) { |
|
| 241 | + private function get_filename($id) { |
|
| 242 | 242 | |
| 243 | - return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
| 243 | + return $this->cache_dir.md5($id).$this->file_extension; |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | } |
@@ -23,55 +23,55 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Wordlift_Post_Excerpt_Helper { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get the text excerpt for the provided {@link WP_Post}. |
|
| 28 | - * |
|
| 29 | - * Since anyone can hook on the excerpt generation filters, and |
|
| 30 | - * amend it with non textual content, we play it self and generate |
|
| 31 | - * the excerpt ourselves, mimicking the way wordpress core does it. |
|
| 32 | - * |
|
| 33 | - * @since 3.10.0 |
|
| 34 | - * |
|
| 35 | - * @param WP_Post $post The {@link WP_Post}. |
|
| 36 | - * @param int $length The desired excerpt length. |
|
| 37 | - * @param string $more The desired more string. |
|
| 38 | - * |
|
| 39 | - * @return string The excerpt. |
|
| 40 | - */ |
|
| 41 | - public static function get_text_excerpt( $post, $length = 55, $more = '...' ) { |
|
| 26 | + /** |
|
| 27 | + * Get the text excerpt for the provided {@link WP_Post}. |
|
| 28 | + * |
|
| 29 | + * Since anyone can hook on the excerpt generation filters, and |
|
| 30 | + * amend it with non textual content, we play it self and generate |
|
| 31 | + * the excerpt ourselves, mimicking the way wordpress core does it. |
|
| 32 | + * |
|
| 33 | + * @since 3.10.0 |
|
| 34 | + * |
|
| 35 | + * @param WP_Post $post The {@link WP_Post}. |
|
| 36 | + * @param int $length The desired excerpt length. |
|
| 37 | + * @param string $more The desired more string. |
|
| 38 | + * |
|
| 39 | + * @return string The excerpt. |
|
| 40 | + */ |
|
| 41 | + public static function get_text_excerpt( $post, $length = 55, $more = '...' ) { |
|
| 42 | 42 | |
| 43 | - /* |
|
| 43 | + /* |
|
| 44 | 44 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 45 | 45 | * because the content is written elsewhere. |
| 46 | 46 | * |
| 47 | 47 | * @since 3.20.0 |
| 48 | 48 | */ |
| 49 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 49 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 50 | 50 | |
| 51 | - // Filter shortcode content in post_content, before using it for trimming |
|
| 52 | - $post_content = do_shortcode( $post_content ); |
|
| 51 | + // Filter shortcode content in post_content, before using it for trimming |
|
| 52 | + $post_content = do_shortcode( $post_content ); |
|
| 53 | 53 | |
| 54 | - // Get the excerpt and trim it. Use the `post_excerpt` if available. |
|
| 55 | - $excerpt = wp_trim_words( ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post_content, $length, $more ); |
|
| 54 | + // Get the excerpt and trim it. Use the `post_excerpt` if available. |
|
| 55 | + $excerpt = wp_trim_words( ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post_content, $length, $more ); |
|
| 56 | 56 | |
| 57 | - // Remove shortcodes and decode html entities. |
|
| 58 | - return html_entity_decode( self::strip_all_shortcodes( $excerpt ) ); |
|
| 59 | - } |
|
| 57 | + // Remove shortcodes and decode html entities. |
|
| 58 | + return html_entity_decode( self::strip_all_shortcodes( $excerpt ) ); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Remove all the shortcodes from the content. We're using our own function |
|
| 63 | - * because WordPress' own `strip_shortcodes` only takes into consideration |
|
| 64 | - * shortcodes for installed plugins/themes. |
|
| 65 | - * |
|
| 66 | - * @since 3.12.0 |
|
| 67 | - * |
|
| 68 | - * @param string $content The content with shortcodes. |
|
| 69 | - * |
|
| 70 | - * @return string The content without shortcodes. |
|
| 71 | - */ |
|
| 72 | - private static function strip_all_shortcodes( $content ) { |
|
| 61 | + /** |
|
| 62 | + * Remove all the shortcodes from the content. We're using our own function |
|
| 63 | + * because WordPress' own `strip_shortcodes` only takes into consideration |
|
| 64 | + * shortcodes for installed plugins/themes. |
|
| 65 | + * |
|
| 66 | + * @since 3.12.0 |
|
| 67 | + * |
|
| 68 | + * @param string $content The content with shortcodes. |
|
| 69 | + * |
|
| 70 | + * @return string The content without shortcodes. |
|
| 71 | + */ |
|
| 72 | + private static function strip_all_shortcodes( $content ) { |
|
| 73 | 73 | |
| 74 | - return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 75 | - } |
|
| 74 | + return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @return string The excerpt. |
| 40 | 40 | */ |
| 41 | - public static function get_text_excerpt( $post, $length = 55, $more = '...' ) { |
|
| 41 | + public static function get_text_excerpt($post, $length = 55, $more = '...') { |
|
| 42 | 42 | |
| 43 | 43 | /* |
| 44 | 44 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
@@ -46,16 +46,16 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @since 3.20.0 |
| 48 | 48 | */ |
| 49 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 49 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 50 | 50 | |
| 51 | 51 | // Filter shortcode content in post_content, before using it for trimming |
| 52 | - $post_content = do_shortcode( $post_content ); |
|
| 52 | + $post_content = do_shortcode($post_content); |
|
| 53 | 53 | |
| 54 | 54 | // Get the excerpt and trim it. Use the `post_excerpt` if available. |
| 55 | - $excerpt = wp_trim_words( ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post_content, $length, $more ); |
|
| 55 | + $excerpt = wp_trim_words( ! empty($post->post_excerpt) ? $post->post_excerpt : $post_content, $length, $more); |
|
| 56 | 56 | |
| 57 | 57 | // Remove shortcodes and decode html entities. |
| 58 | - return html_entity_decode( self::strip_all_shortcodes( $excerpt ) ); |
|
| 58 | + return html_entity_decode(self::strip_all_shortcodes($excerpt)); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -69,9 +69,9 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @return string The content without shortcodes. |
| 71 | 71 | */ |
| 72 | - private static function strip_all_shortcodes( $content ) { |
|
| 72 | + private static function strip_all_shortcodes($content) { |
|
| 73 | 73 | |
| 74 | - return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 74 | + return preg_replace('/\[[^]]+\]/', '', $content); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | } |
@@ -11,15 +11,15 @@ |
||
| 11 | 11 | |
| 12 | 12 | interface Autocomplete_Service { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Query the service for the specified data. |
|
| 16 | - * |
|
| 17 | - * @param string $query The query. |
|
| 18 | - * @param string $scope The scope. |
|
| 19 | - * @param string|string[] $excludes URLs to exclude. |
|
| 20 | - * |
|
| 21 | - * @return array An array of results. |
|
| 22 | - */ |
|
| 23 | - public function query( $query, $scope, $excludes ); |
|
| 14 | + /** |
|
| 15 | + * Query the service for the specified data. |
|
| 16 | + * |
|
| 17 | + * @param string $query The query. |
|
| 18 | + * @param string $scope The scope. |
|
| 19 | + * @param string|string[] $excludes URLs to exclude. |
|
| 20 | + * |
|
| 21 | + * @return array An array of results. |
|
| 22 | + */ |
|
| 23 | + public function query( $query, $scope, $excludes ); |
|
| 24 | 24 | |
| 25 | 25 | } |
@@ -20,6 +20,6 @@ |
||
| 20 | 20 | * |
| 21 | 21 | * @return array An array of results. |
| 22 | 22 | */ |
| 23 | - public function query( $query, $scope, $excludes ); |
|
| 23 | + public function query($query, $scope, $excludes); |
|
| 24 | 24 | |
| 25 | 25 | } |
@@ -16,37 +16,37 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Admin_Mappings_Page extends Wordlift_Admin_Page { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - function get_page_title() { |
|
| 19 | + /** |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + function get_page_title() { |
|
| 23 | 23 | |
| 24 | - return __( 'Schema.org Types', 'wordlift' ); |
|
| 25 | - } |
|
| 24 | + return __( 'Schema.org Types', 'wordlift' ); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - function get_menu_title() { |
|
| 27 | + /** |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + function get_menu_title() { |
|
| 31 | 31 | |
| 32 | - return __( 'Schema.org Types', 'wordlift' ); |
|
| 33 | - } |
|
| 32 | + return __( 'Schema.org Types', 'wordlift' ); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * {@inheritdoc} |
|
| 37 | - */ |
|
| 38 | - function get_menu_slug() { |
|
| 35 | + /** |
|
| 36 | + * {@inheritdoc} |
|
| 37 | + */ |
|
| 38 | + function get_menu_slug() { |
|
| 39 | 39 | |
| 40 | - return 'wl_mappings'; |
|
| 41 | - } |
|
| 40 | + return 'wl_mappings'; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * {@inheritdoc} |
|
| 45 | - */ |
|
| 43 | + /** |
|
| 44 | + * {@inheritdoc} |
|
| 45 | + */ |
|
| 46 | 46 | |
| 47 | - function get_partial_name() { |
|
| 47 | + function get_partial_name() { |
|
| 48 | 48 | |
| 49 | - return 'wordlift-admin-mappings-page.php'; |
|
| 50 | - } |
|
| 49 | + return 'wordlift-admin-mappings-page.php'; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | 52 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | function get_page_title() { |
| 23 | 23 | |
| 24 | - return __( 'Schema.org Types', 'wordlift' ); |
|
| 24 | + return __('Schema.org Types', 'wordlift'); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | function get_menu_title() { |
| 31 | 31 | |
| 32 | - return __( 'Schema.org Types', 'wordlift' ); |
|
| 32 | + return __('Schema.org Types', 'wordlift'); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
@@ -12,336 +12,336 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | final class Mappings_DBO { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The {@link wpdb} instance. |
|
| 17 | - * |
|
| 18 | - * @since 3.25.0 |
|
| 19 | - * @access private |
|
| 20 | - * @var \wpdb $wpdb The {@link wpdb} instance. |
|
| 21 | - */ |
|
| 22 | - private $wpdb = null; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Construct DBO object. |
|
| 26 | - */ |
|
| 27 | - public function __construct() { |
|
| 28 | - global $wpdb; |
|
| 29 | - $this->wpdb = $wpdb; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Returns an array of mappings. |
|
| 34 | - * |
|
| 35 | - * Each mapping contains one or more rule groups which in turn contain one or more rules. |
|
| 36 | - * |
|
| 37 | - * @return array An array of mappings. |
|
| 38 | - */ |
|
| 39 | - public function get_mappings() { |
|
| 40 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 41 | - |
|
| 42 | - return $this->wpdb->get_results( |
|
| 43 | - "SELECT * FROM $mapping_table_name", |
|
| 44 | - ARRAY_A |
|
| 45 | - ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Returns an array of active mappings. |
|
| 50 | - * |
|
| 51 | - * Each mapping contains one or more rule groups which in turn contain one or more rules. |
|
| 52 | - * |
|
| 53 | - * @return array An array of mappings. |
|
| 54 | - */ |
|
| 55 | - public function get_active_mappings() { |
|
| 56 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 57 | - |
|
| 58 | - return $this->wpdb->get_results( |
|
| 59 | - "SELECT * FROM $mapping_table_name WHERE mapping_status = 'active'", |
|
| 60 | - ARRAY_A |
|
| 61 | - ); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Returns a list of property rows |
|
| 66 | - * |
|
| 67 | - * @param int $mapping_id Primary key of mapping table. |
|
| 68 | - * |
|
| 69 | - * @return array List of property items belong to $mapping_id. |
|
| 70 | - */ |
|
| 71 | - public function get_properties( $mapping_id ) { |
|
| 72 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 73 | - $property_rows = $this->wpdb->get_results( |
|
| 74 | - $this->wpdb->prepare( "SELECT * FROM $property_table_name WHERE mapping_id=%d", $mapping_id ), |
|
| 75 | - ARRAY_A |
|
| 76 | - ); |
|
| 77 | - |
|
| 78 | - return $property_rows; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Check if the row exists in the table |
|
| 83 | - * |
|
| 84 | - * @param string $table_name The table name you want to query, completely escaped value. |
|
| 85 | - * @param string $primary_key_name The primary key you want to query, should be escaped before passing. |
|
| 86 | - * @param int $primary_key_value The primary key value, no need to escape. |
|
| 87 | - * |
|
| 88 | - * @return bool Returns true if the row exists, false if it does not |
|
| 89 | - */ |
|
| 90 | - private function check_if_row_exists( $table_name, $primary_key_name, $primary_key_value ) { |
|
| 91 | - $primary_key_value = (int) $primary_key_value; |
|
| 92 | - $count = (int) $this->wpdb->get_var( |
|
| 93 | - $this->wpdb->prepare( |
|
| 94 | - "SELECT COUNT($primary_key_name) from $table_name where $primary_key_name = %d", |
|
| 95 | - $primary_key_value |
|
| 96 | - ) |
|
| 97 | - ); |
|
| 98 | - |
|
| 99 | - return $count > 0; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Insert new mapping item with title |
|
| 104 | - * |
|
| 105 | - * @param string $title Title of the mapping item. |
|
| 106 | - * |
|
| 107 | - * @return int Id of the inserted mapping item. |
|
| 108 | - */ |
|
| 109 | - public function insert_mapping_item( $title ) { |
|
| 110 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 111 | - $this->wpdb->insert( |
|
| 112 | - $mapping_table_name, |
|
| 113 | - array( 'mapping_title' => $title ) |
|
| 114 | - ); |
|
| 115 | - |
|
| 116 | - return $this->wpdb->insert_id; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Update mapping item with new title |
|
| 121 | - * |
|
| 122 | - * @param array $mapping_data Array of the mapping data. |
|
| 123 | - * |
|
| 124 | - * @return int Id of the inserted mapping item |
|
| 125 | - */ |
|
| 126 | - public function insert_or_update_mapping_item( $mapping_data ) { |
|
| 127 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 128 | - $mapping_id = array_key_exists( 'mapping_id', $mapping_data ) ? (int) $mapping_data['mapping_id'] : null; |
|
| 129 | - if ( $this->check_if_row_exists( $mapping_table_name, 'mapping_id', $mapping_data['mapping_id'] ) ) { |
|
| 130 | - $this->wpdb->update( |
|
| 131 | - $mapping_table_name, |
|
| 132 | - $mapping_data, |
|
| 133 | - array( 'mapping_id' => $mapping_id ) |
|
| 134 | - ); |
|
| 135 | - } else { |
|
| 136 | - $this->wpdb->insert( |
|
| 137 | - $mapping_table_name, |
|
| 138 | - $mapping_data |
|
| 139 | - ); |
|
| 140 | - $mapping_id = (int) $this->wpdb->insert_id; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return $mapping_id; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Updates rule item. |
|
| 148 | - * |
|
| 149 | - * @param array $rule_item_data The rule_item_data, should contain rule_id. |
|
| 150 | - * |
|
| 151 | - * @return int $rule_id The inserted rule id. |
|
| 152 | - */ |
|
| 153 | - public function insert_or_update_rule_item( $rule_item_data ) { |
|
| 154 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 155 | - $rule_id = array_key_exists( 'rule_id', $rule_item_data ) ? $rule_item_data['rule_id'] : null; |
|
| 156 | - if ( $this->check_if_row_exists( $rule_table_name, 'rule_id', $rule_id ) ) { |
|
| 157 | - $this->wpdb->update( $rule_table_name, $rule_item_data, array( 'rule_id' => $rule_id ) ); |
|
| 158 | - } else { |
|
| 159 | - $this->wpdb->insert( $rule_table_name, $rule_item_data ); |
|
| 160 | - $rule_id = $this->wpdb->insert_id; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $rule_id; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * If a rule group exists doesn't do anything, but if rule group |
|
| 168 | - * didn't exist then it inserts a new entry. |
|
| 169 | - * |
|
| 170 | - * @param int $mapping_id Primary key for mapping table. |
|
| 171 | - * |
|
| 172 | - * @return int The inserted rule group id. |
|
| 173 | - */ |
|
| 174 | - public function insert_rule_group( $mapping_id ) { |
|
| 175 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 176 | - $this->wpdb->insert( |
|
| 177 | - $rule_group_table_name, |
|
| 178 | - array( |
|
| 179 | - 'mapping_id' => $mapping_id, |
|
| 180 | - ) |
|
| 181 | - ); |
|
| 182 | - |
|
| 183 | - return $this->wpdb->insert_id; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Deletes a rule item by rule_id from rule and rule group table. |
|
| 188 | - * |
|
| 189 | - * @param int $rule_id Primary key for rule table. |
|
| 190 | - * |
|
| 191 | - * @return void |
|
| 192 | - */ |
|
| 193 | - public function delete_rule_item( $rule_id ) { |
|
| 194 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 195 | - $this->wpdb->delete( $rule_table_name, array( 'rule_id' => $rule_id ) ); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Deletes a rule group item by rule_group_id from rule group table. |
|
| 200 | - * |
|
| 201 | - * @param int $rule_group_id Primary key for rule table. |
|
| 202 | - * |
|
| 203 | - * @return void |
|
| 204 | - */ |
|
| 205 | - public function delete_rule_group_item( $rule_group_id ) { |
|
| 206 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 207 | - $this->wpdb->delete( $rule_group_table_name, array( 'rule_group_id' => $rule_group_id ) ); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Deletes a mapping item by mapping_id |
|
| 212 | - * |
|
| 213 | - * @param int $mapping_id Primary key for mapping table. |
|
| 214 | - * |
|
| 215 | - * @return void |
|
| 216 | - */ |
|
| 217 | - public function delete_mapping_item( $mapping_id ) { |
|
| 218 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 219 | - $this->wpdb->delete( $mapping_table_name, array( 'mapping_id' => $mapping_id ) ); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Gets a list of rule group items. |
|
| 224 | - * |
|
| 225 | - * @param int $mapping_id Primary key for mapping table. |
|
| 226 | - * |
|
| 227 | - * @return array Get list of rule group items. |
|
| 228 | - */ |
|
| 229 | - public function get_rule_group_list( $mapping_id ) { |
|
| 230 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 231 | - |
|
| 232 | - return $this->wpdb->get_results( |
|
| 233 | - $this->wpdb->prepare( |
|
| 234 | - "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
|
| 235 | - $mapping_id |
|
| 236 | - ), |
|
| 237 | - ARRAY_A |
|
| 238 | - ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Gets a list of rule group items. |
|
| 243 | - * |
|
| 244 | - * @param int $mapping_id Primary key for mapping table. |
|
| 245 | - * |
|
| 246 | - * @return array Get list of rule group items. |
|
| 247 | - */ |
|
| 248 | - public function get_rule_groups_by_mapping( $mapping_id ) { |
|
| 249 | - |
|
| 250 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 251 | - $rule_group_rows = $this->wpdb->get_results( |
|
| 252 | - $this->wpdb->prepare( |
|
| 253 | - "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
|
| 254 | - $mapping_id |
|
| 255 | - ), |
|
| 256 | - ARRAY_A |
|
| 257 | - ); |
|
| 258 | - |
|
| 259 | - // List of all rule group items. |
|
| 260 | - $rule_groups = array(); |
|
| 261 | - foreach ( $rule_group_rows as $rule_group_row ) { |
|
| 262 | - $rule_groups[] = array( |
|
| 263 | - 'rule_group_id' => $rule_group_row['rule_group_id'], |
|
| 264 | - 'rules' => $this->get_rules_by_rule_group( $rule_group_row['rule_group_id'] ), |
|
| 265 | - ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - return $rule_groups; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Gets a list of rule items belong to rule_group_id. |
|
| 273 | - * |
|
| 274 | - * @param int $rule_group_id Indicates which group the item belongs to. |
|
| 275 | - * |
|
| 276 | - * @return array Get list of rule items. |
|
| 277 | - */ |
|
| 278 | - public function get_rules_by_rule_group( $rule_group_id ) { |
|
| 279 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 280 | - |
|
| 281 | - return $this->wpdb->get_results( |
|
| 282 | - $this->wpdb->prepare( |
|
| 283 | - "SELECT * FROM $rule_table_name WHERE rule_group_id=%d", |
|
| 284 | - $rule_group_id |
|
| 285 | - ), |
|
| 286 | - ARRAY_A |
|
| 287 | - ); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Insert/Update property item. |
|
| 292 | - * |
|
| 293 | - * @param array $property_data Property row from table/ui. |
|
| 294 | - * |
|
| 295 | - * @return int Inserted Property Id. |
|
| 296 | - */ |
|
| 297 | - public function insert_or_update_property( $property_data ) { |
|
| 298 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 299 | - $property_id = array_key_exists( 'property_id', $property_data ) ? $property_data['property_id'] : null; |
|
| 300 | - |
|
| 301 | - if ( $this->check_if_row_exists( $property_table_name, 'property_id', $property_id ) ) { |
|
| 302 | - $this->wpdb->update( |
|
| 303 | - $property_table_name, |
|
| 304 | - $property_data, |
|
| 305 | - array( 'property_id' => $property_id ) |
|
| 306 | - ); |
|
| 307 | - } else { |
|
| 308 | - $this->wpdb->insert( $property_table_name, $property_data ); |
|
| 309 | - $property_id = $this->wpdb->insert_id; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - return $property_id; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Gets a single mapping item row. |
|
| 317 | - * |
|
| 318 | - * @param int $mapping_id Primary key of mapping table. |
|
| 319 | - * |
|
| 320 | - * @return array Returns single mapping table row.. |
|
| 321 | - */ |
|
| 322 | - public function get_mapping_item_data( $mapping_id ) { |
|
| 323 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 324 | - |
|
| 325 | - return $this->wpdb->get_row( |
|
| 326 | - $this->wpdb->prepare( |
|
| 327 | - "SELECT * FROM $mapping_table_name WHERE mapping_id=%d", |
|
| 328 | - $mapping_id |
|
| 329 | - ), |
|
| 330 | - ARRAY_A |
|
| 331 | - ); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Delete property item. |
|
| 336 | - * |
|
| 337 | - * @param int $property_id Primary key for property table. |
|
| 338 | - * |
|
| 339 | - * @return int|false The number of rows updated, or false on error. |
|
| 340 | - */ |
|
| 341 | - public function delete_property( $property_id ) { |
|
| 342 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 343 | - |
|
| 344 | - return $this->wpdb->delete( $property_table_name, array( 'property_id' => $property_id ) ); |
|
| 345 | - } |
|
| 15 | + /** |
|
| 16 | + * The {@link wpdb} instance. |
|
| 17 | + * |
|
| 18 | + * @since 3.25.0 |
|
| 19 | + * @access private |
|
| 20 | + * @var \wpdb $wpdb The {@link wpdb} instance. |
|
| 21 | + */ |
|
| 22 | + private $wpdb = null; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Construct DBO object. |
|
| 26 | + */ |
|
| 27 | + public function __construct() { |
|
| 28 | + global $wpdb; |
|
| 29 | + $this->wpdb = $wpdb; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Returns an array of mappings. |
|
| 34 | + * |
|
| 35 | + * Each mapping contains one or more rule groups which in turn contain one or more rules. |
|
| 36 | + * |
|
| 37 | + * @return array An array of mappings. |
|
| 38 | + */ |
|
| 39 | + public function get_mappings() { |
|
| 40 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 41 | + |
|
| 42 | + return $this->wpdb->get_results( |
|
| 43 | + "SELECT * FROM $mapping_table_name", |
|
| 44 | + ARRAY_A |
|
| 45 | + ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Returns an array of active mappings. |
|
| 50 | + * |
|
| 51 | + * Each mapping contains one or more rule groups which in turn contain one or more rules. |
|
| 52 | + * |
|
| 53 | + * @return array An array of mappings. |
|
| 54 | + */ |
|
| 55 | + public function get_active_mappings() { |
|
| 56 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 57 | + |
|
| 58 | + return $this->wpdb->get_results( |
|
| 59 | + "SELECT * FROM $mapping_table_name WHERE mapping_status = 'active'", |
|
| 60 | + ARRAY_A |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Returns a list of property rows |
|
| 66 | + * |
|
| 67 | + * @param int $mapping_id Primary key of mapping table. |
|
| 68 | + * |
|
| 69 | + * @return array List of property items belong to $mapping_id. |
|
| 70 | + */ |
|
| 71 | + public function get_properties( $mapping_id ) { |
|
| 72 | + $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 73 | + $property_rows = $this->wpdb->get_results( |
|
| 74 | + $this->wpdb->prepare( "SELECT * FROM $property_table_name WHERE mapping_id=%d", $mapping_id ), |
|
| 75 | + ARRAY_A |
|
| 76 | + ); |
|
| 77 | + |
|
| 78 | + return $property_rows; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Check if the row exists in the table |
|
| 83 | + * |
|
| 84 | + * @param string $table_name The table name you want to query, completely escaped value. |
|
| 85 | + * @param string $primary_key_name The primary key you want to query, should be escaped before passing. |
|
| 86 | + * @param int $primary_key_value The primary key value, no need to escape. |
|
| 87 | + * |
|
| 88 | + * @return bool Returns true if the row exists, false if it does not |
|
| 89 | + */ |
|
| 90 | + private function check_if_row_exists( $table_name, $primary_key_name, $primary_key_value ) { |
|
| 91 | + $primary_key_value = (int) $primary_key_value; |
|
| 92 | + $count = (int) $this->wpdb->get_var( |
|
| 93 | + $this->wpdb->prepare( |
|
| 94 | + "SELECT COUNT($primary_key_name) from $table_name where $primary_key_name = %d", |
|
| 95 | + $primary_key_value |
|
| 96 | + ) |
|
| 97 | + ); |
|
| 98 | + |
|
| 99 | + return $count > 0; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Insert new mapping item with title |
|
| 104 | + * |
|
| 105 | + * @param string $title Title of the mapping item. |
|
| 106 | + * |
|
| 107 | + * @return int Id of the inserted mapping item. |
|
| 108 | + */ |
|
| 109 | + public function insert_mapping_item( $title ) { |
|
| 110 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 111 | + $this->wpdb->insert( |
|
| 112 | + $mapping_table_name, |
|
| 113 | + array( 'mapping_title' => $title ) |
|
| 114 | + ); |
|
| 115 | + |
|
| 116 | + return $this->wpdb->insert_id; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Update mapping item with new title |
|
| 121 | + * |
|
| 122 | + * @param array $mapping_data Array of the mapping data. |
|
| 123 | + * |
|
| 124 | + * @return int Id of the inserted mapping item |
|
| 125 | + */ |
|
| 126 | + public function insert_or_update_mapping_item( $mapping_data ) { |
|
| 127 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 128 | + $mapping_id = array_key_exists( 'mapping_id', $mapping_data ) ? (int) $mapping_data['mapping_id'] : null; |
|
| 129 | + if ( $this->check_if_row_exists( $mapping_table_name, 'mapping_id', $mapping_data['mapping_id'] ) ) { |
|
| 130 | + $this->wpdb->update( |
|
| 131 | + $mapping_table_name, |
|
| 132 | + $mapping_data, |
|
| 133 | + array( 'mapping_id' => $mapping_id ) |
|
| 134 | + ); |
|
| 135 | + } else { |
|
| 136 | + $this->wpdb->insert( |
|
| 137 | + $mapping_table_name, |
|
| 138 | + $mapping_data |
|
| 139 | + ); |
|
| 140 | + $mapping_id = (int) $this->wpdb->insert_id; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return $mapping_id; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Updates rule item. |
|
| 148 | + * |
|
| 149 | + * @param array $rule_item_data The rule_item_data, should contain rule_id. |
|
| 150 | + * |
|
| 151 | + * @return int $rule_id The inserted rule id. |
|
| 152 | + */ |
|
| 153 | + public function insert_or_update_rule_item( $rule_item_data ) { |
|
| 154 | + $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 155 | + $rule_id = array_key_exists( 'rule_id', $rule_item_data ) ? $rule_item_data['rule_id'] : null; |
|
| 156 | + if ( $this->check_if_row_exists( $rule_table_name, 'rule_id', $rule_id ) ) { |
|
| 157 | + $this->wpdb->update( $rule_table_name, $rule_item_data, array( 'rule_id' => $rule_id ) ); |
|
| 158 | + } else { |
|
| 159 | + $this->wpdb->insert( $rule_table_name, $rule_item_data ); |
|
| 160 | + $rule_id = $this->wpdb->insert_id; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $rule_id; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * If a rule group exists doesn't do anything, but if rule group |
|
| 168 | + * didn't exist then it inserts a new entry. |
|
| 169 | + * |
|
| 170 | + * @param int $mapping_id Primary key for mapping table. |
|
| 171 | + * |
|
| 172 | + * @return int The inserted rule group id. |
|
| 173 | + */ |
|
| 174 | + public function insert_rule_group( $mapping_id ) { |
|
| 175 | + $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 176 | + $this->wpdb->insert( |
|
| 177 | + $rule_group_table_name, |
|
| 178 | + array( |
|
| 179 | + 'mapping_id' => $mapping_id, |
|
| 180 | + ) |
|
| 181 | + ); |
|
| 182 | + |
|
| 183 | + return $this->wpdb->insert_id; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Deletes a rule item by rule_id from rule and rule group table. |
|
| 188 | + * |
|
| 189 | + * @param int $rule_id Primary key for rule table. |
|
| 190 | + * |
|
| 191 | + * @return void |
|
| 192 | + */ |
|
| 193 | + public function delete_rule_item( $rule_id ) { |
|
| 194 | + $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 195 | + $this->wpdb->delete( $rule_table_name, array( 'rule_id' => $rule_id ) ); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Deletes a rule group item by rule_group_id from rule group table. |
|
| 200 | + * |
|
| 201 | + * @param int $rule_group_id Primary key for rule table. |
|
| 202 | + * |
|
| 203 | + * @return void |
|
| 204 | + */ |
|
| 205 | + public function delete_rule_group_item( $rule_group_id ) { |
|
| 206 | + $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 207 | + $this->wpdb->delete( $rule_group_table_name, array( 'rule_group_id' => $rule_group_id ) ); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Deletes a mapping item by mapping_id |
|
| 212 | + * |
|
| 213 | + * @param int $mapping_id Primary key for mapping table. |
|
| 214 | + * |
|
| 215 | + * @return void |
|
| 216 | + */ |
|
| 217 | + public function delete_mapping_item( $mapping_id ) { |
|
| 218 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 219 | + $this->wpdb->delete( $mapping_table_name, array( 'mapping_id' => $mapping_id ) ); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Gets a list of rule group items. |
|
| 224 | + * |
|
| 225 | + * @param int $mapping_id Primary key for mapping table. |
|
| 226 | + * |
|
| 227 | + * @return array Get list of rule group items. |
|
| 228 | + */ |
|
| 229 | + public function get_rule_group_list( $mapping_id ) { |
|
| 230 | + $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 231 | + |
|
| 232 | + return $this->wpdb->get_results( |
|
| 233 | + $this->wpdb->prepare( |
|
| 234 | + "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
|
| 235 | + $mapping_id |
|
| 236 | + ), |
|
| 237 | + ARRAY_A |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Gets a list of rule group items. |
|
| 243 | + * |
|
| 244 | + * @param int $mapping_id Primary key for mapping table. |
|
| 245 | + * |
|
| 246 | + * @return array Get list of rule group items. |
|
| 247 | + */ |
|
| 248 | + public function get_rule_groups_by_mapping( $mapping_id ) { |
|
| 249 | + |
|
| 250 | + $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 251 | + $rule_group_rows = $this->wpdb->get_results( |
|
| 252 | + $this->wpdb->prepare( |
|
| 253 | + "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
|
| 254 | + $mapping_id |
|
| 255 | + ), |
|
| 256 | + ARRAY_A |
|
| 257 | + ); |
|
| 258 | + |
|
| 259 | + // List of all rule group items. |
|
| 260 | + $rule_groups = array(); |
|
| 261 | + foreach ( $rule_group_rows as $rule_group_row ) { |
|
| 262 | + $rule_groups[] = array( |
|
| 263 | + 'rule_group_id' => $rule_group_row['rule_group_id'], |
|
| 264 | + 'rules' => $this->get_rules_by_rule_group( $rule_group_row['rule_group_id'] ), |
|
| 265 | + ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + return $rule_groups; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Gets a list of rule items belong to rule_group_id. |
|
| 273 | + * |
|
| 274 | + * @param int $rule_group_id Indicates which group the item belongs to. |
|
| 275 | + * |
|
| 276 | + * @return array Get list of rule items. |
|
| 277 | + */ |
|
| 278 | + public function get_rules_by_rule_group( $rule_group_id ) { |
|
| 279 | + $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 280 | + |
|
| 281 | + return $this->wpdb->get_results( |
|
| 282 | + $this->wpdb->prepare( |
|
| 283 | + "SELECT * FROM $rule_table_name WHERE rule_group_id=%d", |
|
| 284 | + $rule_group_id |
|
| 285 | + ), |
|
| 286 | + ARRAY_A |
|
| 287 | + ); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Insert/Update property item. |
|
| 292 | + * |
|
| 293 | + * @param array $property_data Property row from table/ui. |
|
| 294 | + * |
|
| 295 | + * @return int Inserted Property Id. |
|
| 296 | + */ |
|
| 297 | + public function insert_or_update_property( $property_data ) { |
|
| 298 | + $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 299 | + $property_id = array_key_exists( 'property_id', $property_data ) ? $property_data['property_id'] : null; |
|
| 300 | + |
|
| 301 | + if ( $this->check_if_row_exists( $property_table_name, 'property_id', $property_id ) ) { |
|
| 302 | + $this->wpdb->update( |
|
| 303 | + $property_table_name, |
|
| 304 | + $property_data, |
|
| 305 | + array( 'property_id' => $property_id ) |
|
| 306 | + ); |
|
| 307 | + } else { |
|
| 308 | + $this->wpdb->insert( $property_table_name, $property_data ); |
|
| 309 | + $property_id = $this->wpdb->insert_id; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + return $property_id; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Gets a single mapping item row. |
|
| 317 | + * |
|
| 318 | + * @param int $mapping_id Primary key of mapping table. |
|
| 319 | + * |
|
| 320 | + * @return array Returns single mapping table row.. |
|
| 321 | + */ |
|
| 322 | + public function get_mapping_item_data( $mapping_id ) { |
|
| 323 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 324 | + |
|
| 325 | + return $this->wpdb->get_row( |
|
| 326 | + $this->wpdb->prepare( |
|
| 327 | + "SELECT * FROM $mapping_table_name WHERE mapping_id=%d", |
|
| 328 | + $mapping_id |
|
| 329 | + ), |
|
| 330 | + ARRAY_A |
|
| 331 | + ); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Delete property item. |
|
| 336 | + * |
|
| 337 | + * @param int $property_id Primary key for property table. |
|
| 338 | + * |
|
| 339 | + * @return int|false The number of rows updated, or false on error. |
|
| 340 | + */ |
|
| 341 | + public function delete_property( $property_id ) { |
|
| 342 | + $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 343 | + |
|
| 344 | + return $this->wpdb->delete( $property_table_name, array( 'property_id' => $property_id ) ); |
|
| 345 | + } |
|
| 346 | 346 | |
| 347 | 347 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * @return array An array of mappings. |
| 38 | 38 | */ |
| 39 | 39 | public function get_mappings() { |
| 40 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 40 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 41 | 41 | |
| 42 | 42 | return $this->wpdb->get_results( |
| 43 | 43 | "SELECT * FROM $mapping_table_name", |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @return array An array of mappings. |
| 54 | 54 | */ |
| 55 | 55 | public function get_active_mappings() { |
| 56 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 56 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 57 | 57 | |
| 58 | 58 | return $this->wpdb->get_results( |
| 59 | 59 | "SELECT * FROM $mapping_table_name WHERE mapping_status = 'active'", |
@@ -68,10 +68,10 @@ discard block |
||
| 68 | 68 | * |
| 69 | 69 | * @return array List of property items belong to $mapping_id. |
| 70 | 70 | */ |
| 71 | - public function get_properties( $mapping_id ) { |
|
| 72 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 71 | + public function get_properties($mapping_id) { |
|
| 72 | + $property_table_name = $this->wpdb->prefix.WL_PROPERTY_TABLE_NAME; |
|
| 73 | 73 | $property_rows = $this->wpdb->get_results( |
| 74 | - $this->wpdb->prepare( "SELECT * FROM $property_table_name WHERE mapping_id=%d", $mapping_id ), |
|
| 74 | + $this->wpdb->prepare("SELECT * FROM $property_table_name WHERE mapping_id=%d", $mapping_id), |
|
| 75 | 75 | ARRAY_A |
| 76 | 76 | ); |
| 77 | 77 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * |
| 88 | 88 | * @return bool Returns true if the row exists, false if it does not |
| 89 | 89 | */ |
| 90 | - private function check_if_row_exists( $table_name, $primary_key_name, $primary_key_value ) { |
|
| 90 | + private function check_if_row_exists($table_name, $primary_key_name, $primary_key_value) { |
|
| 91 | 91 | $primary_key_value = (int) $primary_key_value; |
| 92 | 92 | $count = (int) $this->wpdb->get_var( |
| 93 | 93 | $this->wpdb->prepare( |
@@ -106,11 +106,11 @@ discard block |
||
| 106 | 106 | * |
| 107 | 107 | * @return int Id of the inserted mapping item. |
| 108 | 108 | */ |
| 109 | - public function insert_mapping_item( $title ) { |
|
| 110 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 109 | + public function insert_mapping_item($title) { |
|
| 110 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 111 | 111 | $this->wpdb->insert( |
| 112 | 112 | $mapping_table_name, |
| 113 | - array( 'mapping_title' => $title ) |
|
| 113 | + array('mapping_title' => $title) |
|
| 114 | 114 | ); |
| 115 | 115 | |
| 116 | 116 | return $this->wpdb->insert_id; |
@@ -123,14 +123,14 @@ discard block |
||
| 123 | 123 | * |
| 124 | 124 | * @return int Id of the inserted mapping item |
| 125 | 125 | */ |
| 126 | - public function insert_or_update_mapping_item( $mapping_data ) { |
|
| 127 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 128 | - $mapping_id = array_key_exists( 'mapping_id', $mapping_data ) ? (int) $mapping_data['mapping_id'] : null; |
|
| 129 | - if ( $this->check_if_row_exists( $mapping_table_name, 'mapping_id', $mapping_data['mapping_id'] ) ) { |
|
| 126 | + public function insert_or_update_mapping_item($mapping_data) { |
|
| 127 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 128 | + $mapping_id = array_key_exists('mapping_id', $mapping_data) ? (int) $mapping_data['mapping_id'] : null; |
|
| 129 | + if ($this->check_if_row_exists($mapping_table_name, 'mapping_id', $mapping_data['mapping_id'])) { |
|
| 130 | 130 | $this->wpdb->update( |
| 131 | 131 | $mapping_table_name, |
| 132 | 132 | $mapping_data, |
| 133 | - array( 'mapping_id' => $mapping_id ) |
|
| 133 | + array('mapping_id' => $mapping_id) |
|
| 134 | 134 | ); |
| 135 | 135 | } else { |
| 136 | 136 | $this->wpdb->insert( |
@@ -150,13 +150,13 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @return int $rule_id The inserted rule id. |
| 152 | 152 | */ |
| 153 | - public function insert_or_update_rule_item( $rule_item_data ) { |
|
| 154 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 155 | - $rule_id = array_key_exists( 'rule_id', $rule_item_data ) ? $rule_item_data['rule_id'] : null; |
|
| 156 | - if ( $this->check_if_row_exists( $rule_table_name, 'rule_id', $rule_id ) ) { |
|
| 157 | - $this->wpdb->update( $rule_table_name, $rule_item_data, array( 'rule_id' => $rule_id ) ); |
|
| 153 | + public function insert_or_update_rule_item($rule_item_data) { |
|
| 154 | + $rule_table_name = $this->wpdb->prefix.WL_RULE_TABLE_NAME; |
|
| 155 | + $rule_id = array_key_exists('rule_id', $rule_item_data) ? $rule_item_data['rule_id'] : null; |
|
| 156 | + if ($this->check_if_row_exists($rule_table_name, 'rule_id', $rule_id)) { |
|
| 157 | + $this->wpdb->update($rule_table_name, $rule_item_data, array('rule_id' => $rule_id)); |
|
| 158 | 158 | } else { |
| 159 | - $this->wpdb->insert( $rule_table_name, $rule_item_data ); |
|
| 159 | + $this->wpdb->insert($rule_table_name, $rule_item_data); |
|
| 160 | 160 | $rule_id = $this->wpdb->insert_id; |
| 161 | 161 | } |
| 162 | 162 | |
@@ -171,8 +171,8 @@ discard block |
||
| 171 | 171 | * |
| 172 | 172 | * @return int The inserted rule group id. |
| 173 | 173 | */ |
| 174 | - public function insert_rule_group( $mapping_id ) { |
|
| 175 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 174 | + public function insert_rule_group($mapping_id) { |
|
| 175 | + $rule_group_table_name = $this->wpdb->prefix.WL_RULE_GROUP_TABLE_NAME; |
|
| 176 | 176 | $this->wpdb->insert( |
| 177 | 177 | $rule_group_table_name, |
| 178 | 178 | array( |
@@ -190,9 +190,9 @@ discard block |
||
| 190 | 190 | * |
| 191 | 191 | * @return void |
| 192 | 192 | */ |
| 193 | - public function delete_rule_item( $rule_id ) { |
|
| 194 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 195 | - $this->wpdb->delete( $rule_table_name, array( 'rule_id' => $rule_id ) ); |
|
| 193 | + public function delete_rule_item($rule_id) { |
|
| 194 | + $rule_table_name = $this->wpdb->prefix.WL_RULE_TABLE_NAME; |
|
| 195 | + $this->wpdb->delete($rule_table_name, array('rule_id' => $rule_id)); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,9 +202,9 @@ discard block |
||
| 202 | 202 | * |
| 203 | 203 | * @return void |
| 204 | 204 | */ |
| 205 | - public function delete_rule_group_item( $rule_group_id ) { |
|
| 206 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 207 | - $this->wpdb->delete( $rule_group_table_name, array( 'rule_group_id' => $rule_group_id ) ); |
|
| 205 | + public function delete_rule_group_item($rule_group_id) { |
|
| 206 | + $rule_group_table_name = $this->wpdb->prefix.WL_RULE_GROUP_TABLE_NAME; |
|
| 207 | + $this->wpdb->delete($rule_group_table_name, array('rule_group_id' => $rule_group_id)); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
@@ -214,9 +214,9 @@ discard block |
||
| 214 | 214 | * |
| 215 | 215 | * @return void |
| 216 | 216 | */ |
| 217 | - public function delete_mapping_item( $mapping_id ) { |
|
| 218 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 219 | - $this->wpdb->delete( $mapping_table_name, array( 'mapping_id' => $mapping_id ) ); |
|
| 217 | + public function delete_mapping_item($mapping_id) { |
|
| 218 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 219 | + $this->wpdb->delete($mapping_table_name, array('mapping_id' => $mapping_id)); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | /** |
@@ -226,8 +226,8 @@ discard block |
||
| 226 | 226 | * |
| 227 | 227 | * @return array Get list of rule group items. |
| 228 | 228 | */ |
| 229 | - public function get_rule_group_list( $mapping_id ) { |
|
| 230 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 229 | + public function get_rule_group_list($mapping_id) { |
|
| 230 | + $rule_group_table_name = $this->wpdb->prefix.WL_RULE_GROUP_TABLE_NAME; |
|
| 231 | 231 | |
| 232 | 232 | return $this->wpdb->get_results( |
| 233 | 233 | $this->wpdb->prepare( |
@@ -245,9 +245,9 @@ discard block |
||
| 245 | 245 | * |
| 246 | 246 | * @return array Get list of rule group items. |
| 247 | 247 | */ |
| 248 | - public function get_rule_groups_by_mapping( $mapping_id ) { |
|
| 248 | + public function get_rule_groups_by_mapping($mapping_id) { |
|
| 249 | 249 | |
| 250 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 250 | + $rule_group_table_name = $this->wpdb->prefix.WL_RULE_GROUP_TABLE_NAME; |
|
| 251 | 251 | $rule_group_rows = $this->wpdb->get_results( |
| 252 | 252 | $this->wpdb->prepare( |
| 253 | 253 | "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
@@ -258,10 +258,10 @@ discard block |
||
| 258 | 258 | |
| 259 | 259 | // List of all rule group items. |
| 260 | 260 | $rule_groups = array(); |
| 261 | - foreach ( $rule_group_rows as $rule_group_row ) { |
|
| 261 | + foreach ($rule_group_rows as $rule_group_row) { |
|
| 262 | 262 | $rule_groups[] = array( |
| 263 | 263 | 'rule_group_id' => $rule_group_row['rule_group_id'], |
| 264 | - 'rules' => $this->get_rules_by_rule_group( $rule_group_row['rule_group_id'] ), |
|
| 264 | + 'rules' => $this->get_rules_by_rule_group($rule_group_row['rule_group_id']), |
|
| 265 | 265 | ); |
| 266 | 266 | } |
| 267 | 267 | |
@@ -275,8 +275,8 @@ discard block |
||
| 275 | 275 | * |
| 276 | 276 | * @return array Get list of rule items. |
| 277 | 277 | */ |
| 278 | - public function get_rules_by_rule_group( $rule_group_id ) { |
|
| 279 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 278 | + public function get_rules_by_rule_group($rule_group_id) { |
|
| 279 | + $rule_table_name = $this->wpdb->prefix.WL_RULE_TABLE_NAME; |
|
| 280 | 280 | |
| 281 | 281 | return $this->wpdb->get_results( |
| 282 | 282 | $this->wpdb->prepare( |
@@ -294,18 +294,18 @@ discard block |
||
| 294 | 294 | * |
| 295 | 295 | * @return int Inserted Property Id. |
| 296 | 296 | */ |
| 297 | - public function insert_or_update_property( $property_data ) { |
|
| 298 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 299 | - $property_id = array_key_exists( 'property_id', $property_data ) ? $property_data['property_id'] : null; |
|
| 297 | + public function insert_or_update_property($property_data) { |
|
| 298 | + $property_table_name = $this->wpdb->prefix.WL_PROPERTY_TABLE_NAME; |
|
| 299 | + $property_id = array_key_exists('property_id', $property_data) ? $property_data['property_id'] : null; |
|
| 300 | 300 | |
| 301 | - if ( $this->check_if_row_exists( $property_table_name, 'property_id', $property_id ) ) { |
|
| 301 | + if ($this->check_if_row_exists($property_table_name, 'property_id', $property_id)) { |
|
| 302 | 302 | $this->wpdb->update( |
| 303 | 303 | $property_table_name, |
| 304 | 304 | $property_data, |
| 305 | - array( 'property_id' => $property_id ) |
|
| 305 | + array('property_id' => $property_id) |
|
| 306 | 306 | ); |
| 307 | 307 | } else { |
| 308 | - $this->wpdb->insert( $property_table_name, $property_data ); |
|
| 308 | + $this->wpdb->insert($property_table_name, $property_data); |
|
| 309 | 309 | $property_id = $this->wpdb->insert_id; |
| 310 | 310 | } |
| 311 | 311 | |
@@ -319,8 +319,8 @@ discard block |
||
| 319 | 319 | * |
| 320 | 320 | * @return array Returns single mapping table row.. |
| 321 | 321 | */ |
| 322 | - public function get_mapping_item_data( $mapping_id ) { |
|
| 323 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 322 | + public function get_mapping_item_data($mapping_id) { |
|
| 323 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 324 | 324 | |
| 325 | 325 | return $this->wpdb->get_row( |
| 326 | 326 | $this->wpdb->prepare( |
@@ -338,10 +338,10 @@ discard block |
||
| 338 | 338 | * |
| 339 | 339 | * @return int|false The number of rows updated, or false on error. |
| 340 | 340 | */ |
| 341 | - public function delete_property( $property_id ) { |
|
| 342 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 341 | + public function delete_property($property_id) { |
|
| 342 | + $property_table_name = $this->wpdb->prefix.WL_PROPERTY_TABLE_NAME; |
|
| 343 | 343 | |
| 344 | - return $this->wpdb->delete( $property_table_name, array( 'property_id' => $property_id ) ); |
|
| 344 | + return $this->wpdb->delete($property_table_name, array('property_id' => $property_id)); |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | } |
@@ -15,32 +15,32 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Mappings_Transform_Function { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Returns unique name of the transform function. |
|
| 20 | - * |
|
| 21 | - * @return string $name Unique name of the transform function, it should not be repeated |
|
| 22 | - * for any other transform function. |
|
| 23 | - */ |
|
| 24 | - public function get_name(); |
|
| 18 | + /** |
|
| 19 | + * Returns unique name of the transform function. |
|
| 20 | + * |
|
| 21 | + * @return string $name Unique name of the transform function, it should not be repeated |
|
| 22 | + * for any other transform function. |
|
| 23 | + */ |
|
| 24 | + public function get_name(); |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Returns label of the transform function. |
|
| 28 | - * |
|
| 29 | - * @return string $label Label of the transform function to be used in UI, need not |
|
| 30 | - * be unique. |
|
| 31 | - */ |
|
| 32 | - public function get_label(); |
|
| 26 | + /** |
|
| 27 | + * Returns label of the transform function. |
|
| 28 | + * |
|
| 29 | + * @return string $label Label of the transform function to be used in UI, need not |
|
| 30 | + * be unique. |
|
| 31 | + */ |
|
| 32 | + public function get_label(); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Transform data and map to the desired keys. |
|
| 36 | - * |
|
| 37 | - * @param array|string $data An Associative Array containing raw data or string. |
|
| 38 | - * @param array $jsonld The JSON-LD structure. |
|
| 39 | - * @param int[] $references An array of post IDs referenced by the JSON-LD structure. |
|
| 40 | - * @param int $post_id The post ID. |
|
| 41 | - * |
|
| 42 | - * @return array|string Return Mapped data. |
|
| 43 | - */ |
|
| 44 | - public function transform_data( $data, $jsonld, &$references, $post_id ); |
|
| 34 | + /** |
|
| 35 | + * Transform data and map to the desired keys. |
|
| 36 | + * |
|
| 37 | + * @param array|string $data An Associative Array containing raw data or string. |
|
| 38 | + * @param array $jsonld The JSON-LD structure. |
|
| 39 | + * @param int[] $references An array of post IDs referenced by the JSON-LD structure. |
|
| 40 | + * @param int $post_id The post ID. |
|
| 41 | + * |
|
| 42 | + * @return array|string Return Mapped data. |
|
| 43 | + */ |
|
| 44 | + public function transform_data( $data, $jsonld, &$references, $post_id ); |
|
| 45 | 45 | |
| 46 | 46 | } |
@@ -41,6 +41,6 @@ |
||
| 41 | 41 | * |
| 42 | 42 | * @return array|string Return Mapped data. |
| 43 | 43 | */ |
| 44 | - public function transform_data( $data, $jsonld, &$references, $post_id ); |
|
| 44 | + public function transform_data($data, $jsonld, &$references, $post_id); |
|
| 45 | 45 | |
| 46 | 46 | } |