@@ -21,190 +21,190 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Key_Validation_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * A {@link Wordlift_Log_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.14.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $log; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var Ttl_Cache |
|
| 35 | - */ |
|
| 36 | - private $ttl_cache_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 40 | - * |
|
| 41 | - * @since 3.14.0 |
|
| 42 | - */ |
|
| 43 | - public function __construct() { |
|
| 44 | - |
|
| 45 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 46 | - |
|
| 47 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 48 | - /** |
|
| 49 | - * Filter: wl_feature__enable__notices. |
|
| 50 | - * |
|
| 51 | - * @return bool |
|
| 52 | - * @since 3.27.6 |
|
| 53 | - */ |
|
| 54 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 55 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Validate the provided key. |
|
| 64 | - * |
|
| 65 | - * @param string $key WordLift's key to validate. |
|
| 66 | - * |
|
| 67 | - * @return WP_Error|array The response or WP_Error on failure. |
|
| 68 | - * @since 3.9.0 |
|
| 69 | - */ |
|
| 70 | - public function get_account_info( $key ) { |
|
| 71 | - |
|
| 72 | - $this->log->debug( 'Validating key...' ); |
|
| 73 | - |
|
| 74 | - $response = Default_Api_Service::get_instance()->get( |
|
| 75 | - '/accounts/info', |
|
| 76 | - array( |
|
| 77 | - 'Authorization' => "Key $key", |
|
| 78 | - ) |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param $response \Wordlift\Api\Response |
|
| 83 | - * |
|
| 84 | - * @since 3.38.5 |
|
| 85 | - * This action is fired when the key is validated. |
|
| 86 | - */ |
|
| 87 | - do_action( 'wl_key_validation_response', $response ); |
|
| 88 | - |
|
| 89 | - return $response->get_response(); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - private function key_validation_request( $key ) { |
|
| 93 | - $response = $this->get_account_info( $key ); |
|
| 94 | - |
|
| 95 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 96 | - throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 100 | - |
|
| 101 | - $url = $res_body['url']; |
|
| 102 | - |
|
| 103 | - $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
| 104 | - $plugin_features = array( |
|
| 105 | - Entity_Type_Setter::STARTER_PLAN, |
|
| 106 | - Entity_Type_Setter::PROFESSIONAL_PLAN, |
|
| 107 | - Entity_Type_Setter::BUSINESS_PLAN, |
|
| 108 | - ); |
|
| 109 | - |
|
| 110 | - if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
| 111 | - throw new \Exception( __( 'This key is not valid. Start building your Knowledge Graph by purchasing a WordLift subscription <a href=\'https://wordlift.io/pricing/\'>here</a>.', 'wordlift' ) ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - // Considering that production URL may be filtered. |
|
| 115 | - $home_url = get_option( 'home' ); |
|
| 116 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 117 | - |
|
| 118 | - if ( ! empty( $url ) && $url !== $site_url ) { |
|
| 119 | - throw new \Exception( __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ) ); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return true; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Check if key is valid |
|
| 127 | - * |
|
| 128 | - * @param $key string |
|
| 129 | - * |
|
| 130 | - * @return bool |
|
| 131 | - */ |
|
| 132 | - public function is_key_valid( $key ) { |
|
| 133 | - try { |
|
| 134 | - $this->key_validation_request( $key ); |
|
| 135 | - |
|
| 136 | - return true; |
|
| 137 | - } catch ( \Exception $e ) { |
|
| 138 | - return false; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 144 | - * |
|
| 145 | - * @since 3.9.0 |
|
| 146 | - */ |
|
| 147 | - public function validate_key() { |
|
| 148 | - |
|
| 149 | - // Ensure we don't have garbage before us. |
|
| 150 | - ob_clean(); |
|
| 151 | - |
|
| 152 | - // Check if we have a key. |
|
| 153 | - if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 154 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 158 | - |
|
| 159 | - try { |
|
| 160 | - $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 161 | - wp_send_json_success( |
|
| 162 | - array( |
|
| 163 | - 'valid' => true, |
|
| 164 | - 'message' => '', |
|
| 165 | - ) |
|
| 166 | - ); |
|
| 167 | - |
|
| 168 | - } catch ( \Exception $e ) { |
|
| 169 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 170 | - wp_send_json_success( |
|
| 171 | - array( |
|
| 172 | - 'valid' => false, |
|
| 173 | - 'message' => $e->getMessage(), |
|
| 174 | - 'api_url' => Default_Api_Service::get_instance()->get_base_url(), |
|
| 175 | - ) |
|
| 176 | - ); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 182 | - */ |
|
| 183 | - public function wl_load_plugin() { |
|
| 184 | - |
|
| 185 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 186 | - $home_url = get_option( 'home' ); |
|
| 187 | - |
|
| 188 | - if ( ! $wl_blog_url ) { |
|
| 189 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 190 | - } elseif ( $wl_blog_url !== $home_url ) { |
|
| 191 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 192 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 193 | - set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * This function is hooked to the `admin_notices` to show admin notification. |
|
| 200 | - */ |
|
| 201 | - public function wl_key_update_notice() { |
|
| 202 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 203 | - ?> |
|
| 24 | + /** |
|
| 25 | + * A {@link Wordlift_Log_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.14.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $log; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var Ttl_Cache |
|
| 35 | + */ |
|
| 36 | + private $ttl_cache_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 40 | + * |
|
| 41 | + * @since 3.14.0 |
|
| 42 | + */ |
|
| 43 | + public function __construct() { |
|
| 44 | + |
|
| 45 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 46 | + |
|
| 47 | + add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 48 | + /** |
|
| 49 | + * Filter: wl_feature__enable__notices. |
|
| 50 | + * |
|
| 51 | + * @return bool |
|
| 52 | + * @since 3.27.6 |
|
| 53 | + */ |
|
| 54 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 55 | + add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Validate the provided key. |
|
| 64 | + * |
|
| 65 | + * @param string $key WordLift's key to validate. |
|
| 66 | + * |
|
| 67 | + * @return WP_Error|array The response or WP_Error on failure. |
|
| 68 | + * @since 3.9.0 |
|
| 69 | + */ |
|
| 70 | + public function get_account_info( $key ) { |
|
| 71 | + |
|
| 72 | + $this->log->debug( 'Validating key...' ); |
|
| 73 | + |
|
| 74 | + $response = Default_Api_Service::get_instance()->get( |
|
| 75 | + '/accounts/info', |
|
| 76 | + array( |
|
| 77 | + 'Authorization' => "Key $key", |
|
| 78 | + ) |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param $response \Wordlift\Api\Response |
|
| 83 | + * |
|
| 84 | + * @since 3.38.5 |
|
| 85 | + * This action is fired when the key is validated. |
|
| 86 | + */ |
|
| 87 | + do_action( 'wl_key_validation_response', $response ); |
|
| 88 | + |
|
| 89 | + return $response->get_response(); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + private function key_validation_request( $key ) { |
|
| 93 | + $response = $this->get_account_info( $key ); |
|
| 94 | + |
|
| 95 | + if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 96 | + throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 100 | + |
|
| 101 | + $url = $res_body['url']; |
|
| 102 | + |
|
| 103 | + $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
| 104 | + $plugin_features = array( |
|
| 105 | + Entity_Type_Setter::STARTER_PLAN, |
|
| 106 | + Entity_Type_Setter::PROFESSIONAL_PLAN, |
|
| 107 | + Entity_Type_Setter::BUSINESS_PLAN, |
|
| 108 | + ); |
|
| 109 | + |
|
| 110 | + if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
| 111 | + throw new \Exception( __( 'This key is not valid. Start building your Knowledge Graph by purchasing a WordLift subscription <a href=\'https://wordlift.io/pricing/\'>here</a>.', 'wordlift' ) ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // Considering that production URL may be filtered. |
|
| 115 | + $home_url = get_option( 'home' ); |
|
| 116 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 117 | + |
|
| 118 | + if ( ! empty( $url ) && $url !== $site_url ) { |
|
| 119 | + throw new \Exception( __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ) ); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return true; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Check if key is valid |
|
| 127 | + * |
|
| 128 | + * @param $key string |
|
| 129 | + * |
|
| 130 | + * @return bool |
|
| 131 | + */ |
|
| 132 | + public function is_key_valid( $key ) { |
|
| 133 | + try { |
|
| 134 | + $this->key_validation_request( $key ); |
|
| 135 | + |
|
| 136 | + return true; |
|
| 137 | + } catch ( \Exception $e ) { |
|
| 138 | + return false; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 144 | + * |
|
| 145 | + * @since 3.9.0 |
|
| 146 | + */ |
|
| 147 | + public function validate_key() { |
|
| 148 | + |
|
| 149 | + // Ensure we don't have garbage before us. |
|
| 150 | + ob_clean(); |
|
| 151 | + |
|
| 152 | + // Check if we have a key. |
|
| 153 | + if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 154 | + wp_send_json_error( 'The key parameter is required.' ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 158 | + |
|
| 159 | + try { |
|
| 160 | + $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 161 | + wp_send_json_success( |
|
| 162 | + array( |
|
| 163 | + 'valid' => true, |
|
| 164 | + 'message' => '', |
|
| 165 | + ) |
|
| 166 | + ); |
|
| 167 | + |
|
| 168 | + } catch ( \Exception $e ) { |
|
| 169 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 170 | + wp_send_json_success( |
|
| 171 | + array( |
|
| 172 | + 'valid' => false, |
|
| 173 | + 'message' => $e->getMessage(), |
|
| 174 | + 'api_url' => Default_Api_Service::get_instance()->get_base_url(), |
|
| 175 | + ) |
|
| 176 | + ); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 182 | + */ |
|
| 183 | + public function wl_load_plugin() { |
|
| 184 | + |
|
| 185 | + $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 186 | + $home_url = get_option( 'home' ); |
|
| 187 | + |
|
| 188 | + if ( ! $wl_blog_url ) { |
|
| 189 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 190 | + } elseif ( $wl_blog_url !== $home_url ) { |
|
| 191 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 192 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 193 | + set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * This function is hooked to the `admin_notices` to show admin notification. |
|
| 200 | + */ |
|
| 201 | + public function wl_key_update_notice() { |
|
| 202 | + if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 203 | + ?> |
|
| 204 | 204 | <div class="updated notice is-dismissible error"> |
| 205 | 205 | <p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p> |
| 206 | 206 | </div> |
| 207 | 207 | <?php |
| 208 | - } |
|
| 209 | - } |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -32,11 +32,11 @@ discard block |
||
| 32 | 32 | use Wordlift\Post\Post_Adapter; |
| 33 | 33 | // If this file is called directly, abort. |
| 34 | 34 | if ( ! defined( 'WPINC' ) ) { |
| 35 | - die; |
|
| 35 | + die; |
|
| 36 | 36 | } |
| 37 | 37 | define( |
| 38 | - 'WORDLIFT_PLUGIN_FILE', |
|
| 39 | - __FILE__ |
|
| 38 | + 'WORDLIFT_PLUGIN_FILE', |
|
| 39 | + __FILE__ |
|
| 40 | 40 | ); |
| 41 | 41 | define( 'WORDLIFT_VERSION', '3.40.8' ); |
| 42 | 42 | |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * @since 3.33.6 |
| 51 | 51 | */ |
| 52 | 52 | if ( ! apply_filters( 'wl_is_enabled', true ) ) { |
| 53 | - return; |
|
| 53 | + return; |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php'; |
@@ -79,33 +79,33 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | function activate_wordlift() { |
| 81 | 81 | |
| 82 | - $log = Wordlift_Log_Service::get_logger( 'activate_wordlift' ); |
|
| 82 | + $log = Wordlift_Log_Service::get_logger( 'activate_wordlift' ); |
|
| 83 | 83 | |
| 84 | - $log->info( 'Activating WordLift...' ); |
|
| 84 | + $log->info( 'Activating WordLift...' ); |
|
| 85 | 85 | |
| 86 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 87 | - Wordlift_Activator::activate(); |
|
| 86 | + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 87 | + Wordlift_Activator::activate(); |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks. |
|
| 91 | - * |
|
| 92 | - * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
| 93 | - * @since 3.19.2 |
|
| 94 | - */ |
|
| 95 | - Wordlift_Http_Api::activate(); |
|
| 96 | - |
|
| 97 | - // Ensure the post type is registered before flushing the rewrite rules. |
|
| 98 | - Wordlift_Entity_Post_Type_Service::get_instance()->register(); |
|
| 99 | - flush_rewrite_rules(); |
|
| 100 | - /** |
|
| 101 | - * @since 3.27.7 |
|
| 102 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 103 | - */ |
|
| 104 | - Top_Entities::activate(); |
|
| 89 | + /** |
|
| 90 | + * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks. |
|
| 91 | + * |
|
| 92 | + * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
| 93 | + * @since 3.19.2 |
|
| 94 | + */ |
|
| 95 | + Wordlift_Http_Api::activate(); |
|
| 96 | + |
|
| 97 | + // Ensure the post type is registered before flushing the rewrite rules. |
|
| 98 | + Wordlift_Entity_Post_Type_Service::get_instance()->register(); |
|
| 99 | + flush_rewrite_rules(); |
|
| 100 | + /** |
|
| 101 | + * @since 3.27.7 |
|
| 102 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 103 | + */ |
|
| 104 | + Top_Entities::activate(); |
|
| 105 | 105 | |
| 106 | - if ( ! wp_next_scheduled( 'wl_daily_cron' ) ) { |
|
| 107 | - wp_schedule_event( time(), 'daily', 'wl_daily_cron' ); |
|
| 108 | - } |
|
| 106 | + if ( ! wp_next_scheduled( 'wl_daily_cron' ) ) { |
|
| 107 | + wp_schedule_event( time(), 'daily', 'wl_daily_cron' ); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | } |
| 111 | 111 | |
@@ -115,23 +115,23 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | function deactivate_wordlift() { |
| 117 | 117 | |
| 118 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 119 | - Wordlift_Deactivator::deactivate(); |
|
| 120 | - Wordlift_Http_Api::deactivate(); |
|
| 121 | - Ttl_Cache_Cleaner::deactivate(); |
|
| 122 | - /** |
|
| 123 | - * @since 3.27.7 |
|
| 124 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 125 | - */ |
|
| 126 | - Top_Entities::deactivate(); |
|
| 127 | - /** |
|
| 128 | - * @since 3.27.8 |
|
| 129 | - * Remove notification flag on deactivation. |
|
| 130 | - */ |
|
| 131 | - Key_Validation_Notice::remove_notification_flag(); |
|
| 132 | - flush_rewrite_rules(); |
|
| 133 | - |
|
| 134 | - wp_clear_scheduled_hook( 'wl_daily_cron' ); |
|
| 118 | + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 119 | + Wordlift_Deactivator::deactivate(); |
|
| 120 | + Wordlift_Http_Api::deactivate(); |
|
| 121 | + Ttl_Cache_Cleaner::deactivate(); |
|
| 122 | + /** |
|
| 123 | + * @since 3.27.7 |
|
| 124 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 125 | + */ |
|
| 126 | + Top_Entities::deactivate(); |
|
| 127 | + /** |
|
| 128 | + * @since 3.27.8 |
|
| 129 | + * Remove notification flag on deactivation. |
|
| 130 | + */ |
|
| 131 | + Key_Validation_Notice::remove_notification_flag(); |
|
| 132 | + flush_rewrite_rules(); |
|
| 133 | + |
|
| 134 | + wp_clear_scheduled_hook( 'wl_daily_cron' ); |
|
| 135 | 135 | |
| 136 | 136 | } |
| 137 | 137 | |
@@ -154,84 +154,84 @@ discard block |
||
| 154 | 154 | * @since 1.0.0 |
| 155 | 155 | */ |
| 156 | 156 | function run_wordlift() { |
| 157 | - /** |
|
| 158 | - * Filter: wl_feature__enable__widgets. |
|
| 159 | - * |
|
| 160 | - * @param bool whether the widgets needed to be registered, defaults to true. |
|
| 161 | - * |
|
| 162 | - * @return bool |
|
| 163 | - * @since 3.27.6 |
|
| 164 | - */ |
|
| 165 | - if ( apply_filters( 'wl_feature__enable__widgets', true ) ) { |
|
| 166 | - add_action( 'widgets_init', 'wl_register_chord_widget' ); |
|
| 167 | - add_action( 'widgets_init', 'wl_register_geo_widget' ); |
|
| 168 | - add_action( 'widgets_init', 'wl_register_timeline_widget' ); |
|
| 169 | - } |
|
| 170 | - add_filter( 'widget_text', 'do_shortcode' ); |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Filter: wl_feature__enable__analysis |
|
| 174 | - * |
|
| 175 | - * @param bool Whether to send api request to analysis or not |
|
| 176 | - * |
|
| 177 | - * @return bool |
|
| 178 | - * @since 3.27.6 |
|
| 179 | - */ |
|
| 180 | - if ( apply_filters( 'wl_feature__enable__analysis', true ) ) { |
|
| 181 | - add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' ); |
|
| 182 | - } else { |
|
| 183 | - add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' ); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $plugin = new Wordlift(); |
|
| 187 | - $plugin->run(); |
|
| 188 | - |
|
| 189 | - // Initialize the TTL Cache Cleaner. |
|
| 190 | - new Ttl_Cache_Cleaner(); |
|
| 191 | - |
|
| 192 | - // Load the new Post Adapter. |
|
| 193 | - new Post_Adapter(); |
|
| 194 | - |
|
| 195 | - // Load the API Data Hooks. |
|
| 196 | - new Api_Data_Hooks(); |
|
| 197 | - |
|
| 198 | - add_action( |
|
| 199 | - 'plugins_loaded', |
|
| 200 | - function () { |
|
| 201 | - // All features from registry should be initialized here. |
|
| 202 | - $features_registry = Features_Registry::get_instance(); |
|
| 203 | - $features_registry->initialize_all_features(); |
|
| 204 | - }, |
|
| 205 | - 5 |
|
| 206 | - ); |
|
| 207 | - |
|
| 208 | - add_action( |
|
| 209 | - 'plugins_loaded', |
|
| 210 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 211 | - function () use ( $plugin ) { |
|
| 212 | - |
|
| 213 | - new Wordlift_Products_Navigator_Shortcode_REST(); |
|
| 214 | - |
|
| 215 | - // Register the Dataset module, requires `$api_service`. |
|
| 216 | - require_once plugin_dir_path( __FILE__ ) . 'wordlift/dataset/index.php'; |
|
| 217 | - require_once plugin_dir_path( __FILE__ ) . 'wordlift/shipping-data/index.php'; |
|
| 218 | - |
|
| 219 | - /* |
|
| 157 | + /** |
|
| 158 | + * Filter: wl_feature__enable__widgets. |
|
| 159 | + * |
|
| 160 | + * @param bool whether the widgets needed to be registered, defaults to true. |
|
| 161 | + * |
|
| 162 | + * @return bool |
|
| 163 | + * @since 3.27.6 |
|
| 164 | + */ |
|
| 165 | + if ( apply_filters( 'wl_feature__enable__widgets', true ) ) { |
|
| 166 | + add_action( 'widgets_init', 'wl_register_chord_widget' ); |
|
| 167 | + add_action( 'widgets_init', 'wl_register_geo_widget' ); |
|
| 168 | + add_action( 'widgets_init', 'wl_register_timeline_widget' ); |
|
| 169 | + } |
|
| 170 | + add_filter( 'widget_text', 'do_shortcode' ); |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Filter: wl_feature__enable__analysis |
|
| 174 | + * |
|
| 175 | + * @param bool Whether to send api request to analysis or not |
|
| 176 | + * |
|
| 177 | + * @return bool |
|
| 178 | + * @since 3.27.6 |
|
| 179 | + */ |
|
| 180 | + if ( apply_filters( 'wl_feature__enable__analysis', true ) ) { |
|
| 181 | + add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' ); |
|
| 182 | + } else { |
|
| 183 | + add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' ); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $plugin = new Wordlift(); |
|
| 187 | + $plugin->run(); |
|
| 188 | + |
|
| 189 | + // Initialize the TTL Cache Cleaner. |
|
| 190 | + new Ttl_Cache_Cleaner(); |
|
| 191 | + |
|
| 192 | + // Load the new Post Adapter. |
|
| 193 | + new Post_Adapter(); |
|
| 194 | + |
|
| 195 | + // Load the API Data Hooks. |
|
| 196 | + new Api_Data_Hooks(); |
|
| 197 | + |
|
| 198 | + add_action( |
|
| 199 | + 'plugins_loaded', |
|
| 200 | + function () { |
|
| 201 | + // All features from registry should be initialized here. |
|
| 202 | + $features_registry = Features_Registry::get_instance(); |
|
| 203 | + $features_registry->initialize_all_features(); |
|
| 204 | + }, |
|
| 205 | + 5 |
|
| 206 | + ); |
|
| 207 | + |
|
| 208 | + add_action( |
|
| 209 | + 'plugins_loaded', |
|
| 210 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 211 | + function () use ( $plugin ) { |
|
| 212 | + |
|
| 213 | + new Wordlift_Products_Navigator_Shortcode_REST(); |
|
| 214 | + |
|
| 215 | + // Register the Dataset module, requires `$api_service`. |
|
| 216 | + require_once plugin_dir_path( __FILE__ ) . 'wordlift/dataset/index.php'; |
|
| 217 | + require_once plugin_dir_path( __FILE__ ) . 'wordlift/shipping-data/index.php'; |
|
| 218 | + |
|
| 219 | + /* |
|
| 220 | 220 | * Require the Entity annotation cleanup module. |
| 221 | 221 | * |
| 222 | 222 | * @since 3.34.6 |
| 223 | 223 | */ |
| 224 | - require_once plugin_dir_path( __FILE__ ) . 'wordlift/cleanup/index.php'; |
|
| 224 | + require_once plugin_dir_path( __FILE__ ) . 'wordlift/cleanup/index.php'; |
|
| 225 | 225 | |
| 226 | - /* |
|
| 226 | + /* |
|
| 227 | 227 | * Import LOD entities. |
| 228 | 228 | * |
| 229 | 229 | * @since 3.35.0 |
| 230 | 230 | */ |
| 231 | - require_once plugin_dir_path( __FILE__ ) . 'wordlift/lod-import/index.php'; |
|
| 231 | + require_once plugin_dir_path( __FILE__ ) . 'wordlift/lod-import/index.php'; |
|
| 232 | 232 | |
| 233 | - } |
|
| 234 | - ); |
|
| 233 | + } |
|
| 234 | + ); |
|
| 235 | 235 | |
| 236 | 236 | } |
| 237 | 237 | |
@@ -245,45 +245,45 @@ discard block |
||
| 245 | 245 | */ |
| 246 | 246 | function wordlift_plugin_autoload_register() { |
| 247 | 247 | |
| 248 | - spl_autoload_register( |
|
| 249 | - function ( $class_name ) { |
|
| 248 | + spl_autoload_register( |
|
| 249 | + function ( $class_name ) { |
|
| 250 | 250 | |
| 251 | - // Bail out if these are not our classes. |
|
| 252 | - if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) { |
|
| 253 | - return false; |
|
| 254 | - } |
|
| 251 | + // Bail out if these are not our classes. |
|
| 252 | + if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) { |
|
| 253 | + return false; |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - $class_name_lc = strtolower( str_replace( '_', '-', $class_name ) ); |
|
| 256 | + $class_name_lc = strtolower( str_replace( '_', '-', $class_name ) ); |
|
| 257 | 257 | |
| 258 | - preg_match( '|^(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches ); |
|
| 258 | + preg_match( '|^(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches ); |
|
| 259 | 259 | |
| 260 | - $path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] ); |
|
| 261 | - $file = 'class-' . $matches[2] . '.php'; |
|
| 260 | + $path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] ); |
|
| 261 | + $file = 'class-' . $matches[2] . '.php'; |
|
| 262 | 262 | |
| 263 | - $full_path = plugin_dir_path( __FILE__ ) . $path . DIRECTORY_SEPARATOR . $file; |
|
| 263 | + $full_path = plugin_dir_path( __FILE__ ) . $path . DIRECTORY_SEPARATOR . $file; |
|
| 264 | 264 | |
| 265 | - if ( ! file_exists( $full_path ) ) { |
|
| 266 | - return false; |
|
| 267 | - } |
|
| 265 | + if ( ! file_exists( $full_path ) ) { |
|
| 266 | + return false; |
|
| 267 | + } |
|
| 268 | 268 | |
| 269 | - require_once $full_path; |
|
| 269 | + require_once $full_path; |
|
| 270 | 270 | |
| 271 | - return true; |
|
| 272 | - } |
|
| 273 | - ); |
|
| 271 | + return true; |
|
| 272 | + } |
|
| 273 | + ); |
|
| 274 | 274 | |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | function wl_block_categories( $categories ) { |
| 278 | - return array_merge( |
|
| 279 | - $categories, |
|
| 280 | - array( |
|
| 281 | - array( |
|
| 282 | - 'slug' => 'wordlift', |
|
| 283 | - 'title' => __( 'WordLift', 'wordlift' ), |
|
| 284 | - ), |
|
| 285 | - ) |
|
| 286 | - ); |
|
| 278 | + return array_merge( |
|
| 279 | + $categories, |
|
| 280 | + array( |
|
| 281 | + array( |
|
| 282 | + 'slug' => 'wordlift', |
|
| 283 | + 'title' => __( 'WordLift', 'wordlift' ), |
|
| 284 | + ), |
|
| 285 | + ) |
|
| 286 | + ); |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | /** |
@@ -291,19 +291,19 @@ discard block |
||
| 291 | 291 | * this has to be removed when removing the legacy fields from the ui. |
| 292 | 292 | */ |
| 293 | 293 | function wl_enqueue_leaflet( $in_footer = false ) { |
| 294 | - // Leaflet. |
|
| 295 | - wp_enqueue_style( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.css', array(), '1.6.0' ); |
|
| 296 | - wp_enqueue_script( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.js', array(), '1.6.0', $in_footer ); |
|
| 294 | + // Leaflet. |
|
| 295 | + wp_enqueue_style( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.css', array(), '1.6.0' ); |
|
| 296 | + wp_enqueue_script( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.js', array(), '1.6.0', $in_footer ); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | add_filter( 'block_categories', 'wl_block_categories', 10 ); |
| 300 | 300 | |
| 301 | 301 | // Temporary fix for a typo in WooCommerce Extension. |
| 302 | 302 | add_filter( |
| 303 | - 'wl_feature__enable__dataset', |
|
| 304 | - function ( $value ) { |
|
| 305 | - return apply_filters( 'wl_features__enable__dataset', $value ); |
|
| 306 | - } |
|
| 303 | + 'wl_feature__enable__dataset', |
|
| 304 | + function ( $value ) { |
|
| 305 | + return apply_filters( 'wl_features__enable__dataset', $value ); |
|
| 306 | + } |
|
| 307 | 307 | ); |
| 308 | 308 | |
| 309 | 309 | require_once __DIR__ . '/modules/food-kg/load.php'; |
@@ -312,26 +312,26 @@ discard block |
||
| 312 | 312 | require_once __DIR__ . '/modules/include-exclude-push-config/load.php'; |
| 313 | 313 | |
| 314 | 314 | add_action( |
| 315 | - 'update_plugins_adthrive.wordlift.io', |
|
| 316 | - function ( $update, $plugin_data, $plugin_file ) { |
|
| 317 | - // Bail out if it's not our plugin. |
|
| 318 | - $update_uri = $plugin_data['UpdateURI']; |
|
| 319 | - if ( 'wordlift/wordlift.php' !== $plugin_file || ! isset( $update_uri ) ) { |
|
| 320 | - return $update; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - $response = wp_remote_get( "$update_uri?nocache=" . time() ); |
|
| 324 | - |
|
| 325 | - if ( is_wp_error( $response ) ) { |
|
| 326 | - return $update; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - try { |
|
| 330 | - return json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 331 | - } catch ( Exception $e ) { |
|
| 332 | - return $update; |
|
| 333 | - } |
|
| 334 | - }, |
|
| 335 | - 10, |
|
| 336 | - 3 |
|
| 315 | + 'update_plugins_adthrive.wordlift.io', |
|
| 316 | + function ( $update, $plugin_data, $plugin_file ) { |
|
| 317 | + // Bail out if it's not our plugin. |
|
| 318 | + $update_uri = $plugin_data['UpdateURI']; |
|
| 319 | + if ( 'wordlift/wordlift.php' !== $plugin_file || ! isset( $update_uri ) ) { |
|
| 320 | + return $update; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + $response = wp_remote_get( "$update_uri?nocache=" . time() ); |
|
| 324 | + |
|
| 325 | + if ( is_wp_error( $response ) ) { |
|
| 326 | + return $update; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + try { |
|
| 330 | + return json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 331 | + } catch ( Exception $e ) { |
|
| 332 | + return $update; |
|
| 333 | + } |
|
| 334 | + }, |
|
| 335 | + 10, |
|
| 336 | + 3 |
|
| 337 | 337 | ); |
@@ -23,428 +23,428 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The JSON-LD context. |
|
| 28 | - * |
|
| 29 | - * @since 3.10.0 |
|
| 30 | - */ |
|
| 31 | - const CONTEXT = 'http://schema.org'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.10.0 |
|
| 37 | - * @access protected |
|
| 38 | - * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 39 | - */ |
|
| 40 | - protected $entity_type_service; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * A {@link Wordlift_User_Service} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.10.0 |
|
| 46 | - * @access private |
|
| 47 | - * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 48 | - */ |
|
| 49 | - protected $user_service; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * A {@link Wordlift_Attachment_Service} instance. |
|
| 53 | - * |
|
| 54 | - * @since 3.10.0 |
|
| 55 | - * @access private |
|
| 56 | - * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 57 | - */ |
|
| 58 | - protected $attachment_service; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @var Wordlift_Property_Getter |
|
| 62 | - */ |
|
| 63 | - private $property_getter; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 67 | - * |
|
| 68 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 69 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 70 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 71 | - * @param \Wordlift_Property_Getter $property_getter |
|
| 72 | - * |
|
| 73 | - * @since 3.10.0 |
|
| 74 | - */ |
|
| 75 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
| 76 | - $this->entity_type_service = $entity_type_service; |
|
| 77 | - $this->user_service = $user_service; |
|
| 78 | - $this->attachment_service = $attachment_service; |
|
| 79 | - $this->property_getter = $property_getter; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 84 | - * found while processing the post is set in the $references array. |
|
| 85 | - * |
|
| 86 | - * @param int $post_id The post id. |
|
| 87 | - * @param array $references An array of entity references. |
|
| 88 | - * @param array $references_infos |
|
| 89 | - * |
|
| 90 | - * @return array A JSON-LD array. |
|
| 91 | - * @since 3.10.0 |
|
| 92 | - */ |
|
| 93 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 94 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 95 | - |
|
| 96 | - // Get the post instance. |
|
| 97 | - $post = get_post( $post_id ); |
|
| 98 | - if ( null === $post ) { |
|
| 99 | - // Post not found. |
|
| 100 | - return null; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // Get the post URI @id. |
|
| 104 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
| 105 | - if ( $id === null ) { |
|
| 106 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /* |
|
| 26 | + /** |
|
| 27 | + * The JSON-LD context. |
|
| 28 | + * |
|
| 29 | + * @since 3.10.0 |
|
| 30 | + */ |
|
| 31 | + const CONTEXT = 'http://schema.org'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.10.0 |
|
| 37 | + * @access protected |
|
| 38 | + * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 39 | + */ |
|
| 40 | + protected $entity_type_service; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * A {@link Wordlift_User_Service} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.10.0 |
|
| 46 | + * @access private |
|
| 47 | + * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 48 | + */ |
|
| 49 | + protected $user_service; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * A {@link Wordlift_Attachment_Service} instance. |
|
| 53 | + * |
|
| 54 | + * @since 3.10.0 |
|
| 55 | + * @access private |
|
| 56 | + * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 57 | + */ |
|
| 58 | + protected $attachment_service; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @var Wordlift_Property_Getter |
|
| 62 | + */ |
|
| 63 | + private $property_getter; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 67 | + * |
|
| 68 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 69 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 70 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 71 | + * @param \Wordlift_Property_Getter $property_getter |
|
| 72 | + * |
|
| 73 | + * @since 3.10.0 |
|
| 74 | + */ |
|
| 75 | + public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
| 76 | + $this->entity_type_service = $entity_type_service; |
|
| 77 | + $this->user_service = $user_service; |
|
| 78 | + $this->attachment_service = $attachment_service; |
|
| 79 | + $this->property_getter = $property_getter; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 84 | + * found while processing the post is set in the $references array. |
|
| 85 | + * |
|
| 86 | + * @param int $post_id The post id. |
|
| 87 | + * @param array $references An array of entity references. |
|
| 88 | + * @param array $references_infos |
|
| 89 | + * |
|
| 90 | + * @return array A JSON-LD array. |
|
| 91 | + * @since 3.10.0 |
|
| 92 | + */ |
|
| 93 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 94 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 95 | + |
|
| 96 | + // Get the post instance. |
|
| 97 | + $post = get_post( $post_id ); |
|
| 98 | + if ( null === $post ) { |
|
| 99 | + // Post not found. |
|
| 100 | + return null; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // Get the post URI @id. |
|
| 104 | + $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
| 105 | + if ( $id === null ) { |
|
| 106 | + $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /* |
|
| 110 | 110 | * The `types` variable holds one or more entity types. The `type` variable isn't used anymore. |
| 111 | 111 | * |
| 112 | 112 | * @since 3.20.0 We support more than one entity type. |
| 113 | 113 | * |
| 114 | 114 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 115 | 115 | */ |
| 116 | - // Get the entity @type. We consider `post` BlogPostings. |
|
| 117 | - // $type = $this->entity_type_service->get( $post_id ); |
|
| 118 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 119 | - $type = self::make_one( $types ); |
|
| 120 | - |
|
| 121 | - // Prepare the response. |
|
| 122 | - $jsonld = array( |
|
| 123 | - '@context' => self::CONTEXT, |
|
| 124 | - '@id' => $id, |
|
| 125 | - '@type' => $type, |
|
| 126 | - ); |
|
| 127 | - |
|
| 128 | - if ( post_type_supports( $post->post_type, 'excerpt' ) ) { |
|
| 129 | - $jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 133 | - /* |
|
| 116 | + // Get the entity @type. We consider `post` BlogPostings. |
|
| 117 | + // $type = $this->entity_type_service->get( $post_id ); |
|
| 118 | + $types = $this->entity_type_service->get_names( $post_id ); |
|
| 119 | + $type = self::make_one( $types ); |
|
| 120 | + |
|
| 121 | + // Prepare the response. |
|
| 122 | + $jsonld = array( |
|
| 123 | + '@context' => self::CONTEXT, |
|
| 124 | + '@id' => $id, |
|
| 125 | + '@type' => $type, |
|
| 126 | + ); |
|
| 127 | + |
|
| 128 | + if ( post_type_supports( $post->post_type, 'excerpt' ) ) { |
|
| 129 | + $jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 133 | + /* |
|
| 134 | 134 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 135 | 135 | * because the content is written elsewhere. |
| 136 | 136 | * |
| 137 | 137 | * @since 3.20.0 |
| 138 | 138 | */ |
| 139 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 140 | - if ( ! empty( $post_content ) || in_array( 'Product', (array) $type, true ) ) { |
|
| 141 | - // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 142 | - // main entity for the specified URL. It might be however that the |
|
| 143 | - // post/page is actually about another specific entity. How WL deals |
|
| 144 | - // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 145 | - // |
|
| 146 | - // See http://schema.org/mainEntityOfPage |
|
| 147 | - // |
|
| 148 | - // No need to specify `'@type' => 'WebPage'. |
|
| 149 | - // |
|
| 150 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 151 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
| 155 | - * |
|
| 156 | - * @since 3.27.7 |
|
| 157 | - */ |
|
| 158 | - if ( in_array( |
|
| 159 | - $type, |
|
| 160 | - array( |
|
| 161 | - 'Occupation', |
|
| 162 | - 'OccupationAggregationByEmployer', |
|
| 163 | - ), |
|
| 164 | - true |
|
| 165 | - ) ) { |
|
| 166 | - $jsonld['mainEntityOfPage'] = array( |
|
| 167 | - '@type' => 'WebPage', |
|
| 168 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
| 169 | - ); |
|
| 170 | - } |
|
| 171 | - }; |
|
| 172 | - |
|
| 173 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 174 | - |
|
| 175 | - // Get the entities referenced by this post and set it to the `references` |
|
| 176 | - // array so that the caller can do further processing, such as printing out |
|
| 177 | - // more of those references. |
|
| 178 | - $references_without_locations = Object_Relation_Service::get_instance() |
|
| 179 | - ->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 180 | - |
|
| 181 | - /* |
|
| 139 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 140 | + if ( ! empty( $post_content ) || in_array( 'Product', (array) $type, true ) ) { |
|
| 141 | + // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 142 | + // main entity for the specified URL. It might be however that the |
|
| 143 | + // post/page is actually about another specific entity. How WL deals |
|
| 144 | + // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 145 | + // |
|
| 146 | + // See http://schema.org/mainEntityOfPage |
|
| 147 | + // |
|
| 148 | + // No need to specify `'@type' => 'WebPage'. |
|
| 149 | + // |
|
| 150 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 151 | + $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
| 155 | + * |
|
| 156 | + * @since 3.27.7 |
|
| 157 | + */ |
|
| 158 | + if ( in_array( |
|
| 159 | + $type, |
|
| 160 | + array( |
|
| 161 | + 'Occupation', |
|
| 162 | + 'OccupationAggregationByEmployer', |
|
| 163 | + ), |
|
| 164 | + true |
|
| 165 | + ) ) { |
|
| 166 | + $jsonld['mainEntityOfPage'] = array( |
|
| 167 | + '@type' => 'WebPage', |
|
| 168 | + 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
| 169 | + ); |
|
| 170 | + } |
|
| 171 | + }; |
|
| 172 | + |
|
| 173 | + $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 174 | + |
|
| 175 | + // Get the entities referenced by this post and set it to the `references` |
|
| 176 | + // array so that the caller can do further processing, such as printing out |
|
| 177 | + // more of those references. |
|
| 178 | + $references_without_locations = Object_Relation_Service::get_instance() |
|
| 179 | + ->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 180 | + |
|
| 181 | + /* |
|
| 182 | 182 | * Add the locations to the references. |
| 183 | 183 | * |
| 184 | 184 | * @since 3.19.5 |
| 185 | 185 | * |
| 186 | 186 | * @see https://github.com/insideout10/wordlift-plugin/issues/858. |
| 187 | 187 | */ |
| 188 | - // A reference to use in closure. |
|
| 189 | - $entity_type_service = $this->entity_type_service; |
|
| 190 | - $locations = array_reduce( |
|
| 191 | - $references_without_locations, |
|
| 192 | - function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 193 | - /** |
|
| 194 | - * @var $reference Reference |
|
| 195 | - */ |
|
| 196 | - // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 197 | - if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 198 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 199 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 200 | - |
|
| 201 | - return $carry; |
|
| 202 | - } |
|
| 203 | - $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 204 | - $post_location_references = array_map( |
|
| 205 | - function ( $post_id ) { |
|
| 206 | - return new Post_Reference( $post_id ); |
|
| 207 | - }, |
|
| 208 | - $post_location_ids |
|
| 209 | - ); |
|
| 210 | - |
|
| 211 | - return array_merge( $carry, $post_location_references ); |
|
| 212 | - }, |
|
| 213 | - array() |
|
| 214 | - ); |
|
| 215 | - |
|
| 216 | - // Merge the references with the referenced locations if any. |
|
| 217 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 218 | - $references = array_merge( $references_without_locations, $locations ); |
|
| 219 | - |
|
| 220 | - return $jsonld; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 225 | - * part since it is set with the '@context'. |
|
| 226 | - * |
|
| 227 | - * @param string $value The property value. |
|
| 228 | - * |
|
| 229 | - * @return string The property value without the context. |
|
| 230 | - * @since 3.10.0 |
|
| 231 | - */ |
|
| 232 | - public function relative_to_context( $value ) { |
|
| 233 | - return ! is_array( $value ) && 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Set the images, by looking for embedded images, for images loaded via the |
|
| 238 | - * gallery and for the featured image. |
|
| 239 | - * |
|
| 240 | - * Uses the cache service to store the results of this function for a day. |
|
| 241 | - * |
|
| 242 | - * @param $attachment_service Wordlift_Attachment_Service |
|
| 243 | - * @param WP_Post $post The target {@link WP_Post}. |
|
| 244 | - * @param array $jsonld The JSON-LD array. |
|
| 245 | - * |
|
| 246 | - * @since 3.10.0 |
|
| 247 | - */ |
|
| 248 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 249 | - |
|
| 250 | - // Prepare the attachment ids array. |
|
| 251 | - $ids = array(); |
|
| 252 | - |
|
| 253 | - // Set the thumbnail id as first attachment id, if any. |
|
| 254 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 255 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 256 | - $ids[] = $thumbnail_id; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - // For the time being the following is being removed since the query |
|
| 260 | - // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 261 | - // |
|
| 262 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 263 | - // |
|
| 264 | - // Get the embeds, removing existing ids. |
|
| 265 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 266 | - if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
| 267 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 268 | - } else { |
|
| 269 | - $embeds = array(); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // Get the gallery, removing existing ids. |
|
| 273 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 274 | - |
|
| 275 | - // Map the attachment ids to images' data structured for schema.org use. |
|
| 276 | - $images_with_sizes = array_filter( |
|
| 277 | - array_reduce( |
|
| 278 | - array_merge( $ids, $embeds, $gallery ), |
|
| 279 | - function ( $carry, $item ) { |
|
| 280 | - /* |
|
| 188 | + // A reference to use in closure. |
|
| 189 | + $entity_type_service = $this->entity_type_service; |
|
| 190 | + $locations = array_reduce( |
|
| 191 | + $references_without_locations, |
|
| 192 | + function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 193 | + /** |
|
| 194 | + * @var $reference Reference |
|
| 195 | + */ |
|
| 196 | + // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 197 | + if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 198 | + && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 199 | + && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 200 | + |
|
| 201 | + return $carry; |
|
| 202 | + } |
|
| 203 | + $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 204 | + $post_location_references = array_map( |
|
| 205 | + function ( $post_id ) { |
|
| 206 | + return new Post_Reference( $post_id ); |
|
| 207 | + }, |
|
| 208 | + $post_location_ids |
|
| 209 | + ); |
|
| 210 | + |
|
| 211 | + return array_merge( $carry, $post_location_references ); |
|
| 212 | + }, |
|
| 213 | + array() |
|
| 214 | + ); |
|
| 215 | + |
|
| 216 | + // Merge the references with the referenced locations if any. |
|
| 217 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 218 | + $references = array_merge( $references_without_locations, $locations ); |
|
| 219 | + |
|
| 220 | + return $jsonld; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 225 | + * part since it is set with the '@context'. |
|
| 226 | + * |
|
| 227 | + * @param string $value The property value. |
|
| 228 | + * |
|
| 229 | + * @return string The property value without the context. |
|
| 230 | + * @since 3.10.0 |
|
| 231 | + */ |
|
| 232 | + public function relative_to_context( $value ) { |
|
| 233 | + return ! is_array( $value ) && 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Set the images, by looking for embedded images, for images loaded via the |
|
| 238 | + * gallery and for the featured image. |
|
| 239 | + * |
|
| 240 | + * Uses the cache service to store the results of this function for a day. |
|
| 241 | + * |
|
| 242 | + * @param $attachment_service Wordlift_Attachment_Service |
|
| 243 | + * @param WP_Post $post The target {@link WP_Post}. |
|
| 244 | + * @param array $jsonld The JSON-LD array. |
|
| 245 | + * |
|
| 246 | + * @since 3.10.0 |
|
| 247 | + */ |
|
| 248 | + public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 249 | + |
|
| 250 | + // Prepare the attachment ids array. |
|
| 251 | + $ids = array(); |
|
| 252 | + |
|
| 253 | + // Set the thumbnail id as first attachment id, if any. |
|
| 254 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 255 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 256 | + $ids[] = $thumbnail_id; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + // For the time being the following is being removed since the query |
|
| 260 | + // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 261 | + // |
|
| 262 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 263 | + // |
|
| 264 | + // Get the embeds, removing existing ids. |
|
| 265 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 266 | + if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
| 267 | + $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 268 | + } else { |
|
| 269 | + $embeds = array(); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // Get the gallery, removing existing ids. |
|
| 273 | + $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 274 | + |
|
| 275 | + // Map the attachment ids to images' data structured for schema.org use. |
|
| 276 | + $images_with_sizes = array_filter( |
|
| 277 | + array_reduce( |
|
| 278 | + array_merge( $ids, $embeds, $gallery ), |
|
| 279 | + function ( $carry, $item ) { |
|
| 280 | + /* |
|
| 281 | 281 | * @todo: we're not sure that we're getting attachment data here, we |
| 282 | 282 | * should filter `false`s. |
| 283 | 283 | */ |
| 284 | 284 | |
| 285 | - $sources = array_merge( |
|
| 286 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 287 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 288 | - ); |
|
| 289 | - |
|
| 290 | - $sources_with_image = array_filter( |
|
| 291 | - $sources, |
|
| 292 | - function ( $source ) { |
|
| 293 | - return ! empty( $source[0] ); |
|
| 294 | - } |
|
| 295 | - ); |
|
| 296 | - |
|
| 297 | - // Get the attachment data. |
|
| 298 | - // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 299 | - |
|
| 300 | - // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 301 | - |
|
| 302 | - // Bail if image is not found. |
|
| 303 | - // In some cases, you can delete the image from the database |
|
| 304 | - // or from uploads dir, but the image id still exists as featured image |
|
| 305 | - // or in [gallery] shortcode. |
|
| 306 | - // if ( empty( $attachment[0] ) ) { |
|
| 307 | - if ( empty( $sources_with_image ) ) { |
|
| 308 | - return $carry; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - // Merge the arrays. |
|
| 312 | - return array_merge( |
|
| 313 | - $carry, |
|
| 314 | - $sources_with_image |
|
| 315 | - ); |
|
| 316 | - }, |
|
| 317 | - // Initial array. |
|
| 318 | - |
|
| 319 | - array() |
|
| 320 | - ) |
|
| 321 | - ); |
|
| 322 | - |
|
| 323 | - // Refactor data as per schema.org specifications. |
|
| 324 | - $images = array_map( |
|
| 325 | - function ( $attachment ) { |
|
| 326 | - return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 327 | - array( |
|
| 328 | - '@type' => 'ImageObject', |
|
| 329 | - 'url' => $attachment[0], |
|
| 330 | - ), |
|
| 331 | - $attachment |
|
| 332 | - ); |
|
| 333 | - }, |
|
| 334 | - $images_with_sizes |
|
| 335 | - ); |
|
| 336 | - |
|
| 337 | - // Add images if present. |
|
| 338 | - if ( 0 < count( $images ) ) { |
|
| 339 | - $jsonld['image'] = $images; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * If the provided array of values contains only one value, then one single |
|
| 346 | - * value is returned, otherwise the original array is returned. |
|
| 347 | - * |
|
| 348 | - * @param array $value An array of values. |
|
| 349 | - * |
|
| 350 | - * @return mixed|array A single value or the original array. |
|
| 351 | - * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 352 | - * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 353 | - * @since 3.8.0 |
|
| 354 | - * @access private |
|
| 355 | - */ |
|
| 356 | - protected static function make_one( $value ) { |
|
| 357 | - |
|
| 358 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Process the provided array by adding the width / height if the values |
|
| 363 | - * are available and are greater than 0. |
|
| 364 | - * |
|
| 365 | - * @param array $image The `ImageObject` array. |
|
| 366 | - * @param array $attachment The attachment array. |
|
| 367 | - * |
|
| 368 | - * @return array The enriched `ImageObject` array. |
|
| 369 | - * @since 3.14.0 |
|
| 370 | - */ |
|
| 371 | - public static function set_image_size( $image, $attachment ) { |
|
| 372 | - |
|
| 373 | - // If you specify a "width" or "height" value you should leave out |
|
| 374 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 375 | - // |
|
| 376 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 377 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 378 | - $image['width'] = $attachment[1]; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 382 | - $image['height'] = $attachment[2]; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - return $image; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property |
|
| 390 | - * for the post entity type. |
|
| 391 | - * |
|
| 392 | - * @param array $jsonld The JSON-LD array. |
|
| 393 | - * @param array $fields The entity types field array. |
|
| 394 | - * @param WP_Post $post The target {@link WP_Post} instance. |
|
| 395 | - * @param array $references The references array. |
|
| 396 | - * |
|
| 397 | - * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
|
| 398 | - * the {@link Wordlift_Schema_Service} class. |
|
| 399 | - */ |
|
| 400 | - protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
| 401 | - |
|
| 402 | - // Set a reference to use in closures. |
|
| 403 | - $converter = $this; |
|
| 404 | - |
|
| 405 | - // Try each field on the entity. |
|
| 406 | - foreach ( $fields as $key => $value ) { |
|
| 407 | - |
|
| 408 | - // Get the predicate. |
|
| 409 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
| 410 | - |
|
| 411 | - // Get the value, the property service will get the right extractor |
|
| 412 | - // for that property. |
|
| 413 | - $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
| 414 | - |
|
| 415 | - if ( empty( $value ) ) { |
|
| 416 | - continue; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - // Map the value to the property name. |
|
| 420 | - // If we got an array with just one value, we return that one value. |
|
| 421 | - // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
| 422 | - $jsonld[ $name ] = self::make_one( |
|
| 423 | - array_map( |
|
| 424 | - function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
| 425 | - |
|
| 426 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 427 | - |
|
| 428 | - $url = $item->get_url(); |
|
| 429 | - |
|
| 430 | - // The refactored converters require the entity id. |
|
| 431 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 432 | - $references[] = $item->to_reference(); |
|
| 433 | - |
|
| 434 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 435 | - $references_infos[] = array( 'reference' => $item ); |
|
| 436 | - |
|
| 437 | - return array( '@id' => $url ); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - return $converter->relative_to_context( $item ); |
|
| 441 | - }, |
|
| 442 | - $value |
|
| 443 | - ) |
|
| 444 | - ); |
|
| 445 | - |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - } |
|
| 285 | + $sources = array_merge( |
|
| 286 | + Wordlift_Image_Service::get_sources( $item ), |
|
| 287 | + array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 288 | + ); |
|
| 289 | + |
|
| 290 | + $sources_with_image = array_filter( |
|
| 291 | + $sources, |
|
| 292 | + function ( $source ) { |
|
| 293 | + return ! empty( $source[0] ); |
|
| 294 | + } |
|
| 295 | + ); |
|
| 296 | + |
|
| 297 | + // Get the attachment data. |
|
| 298 | + // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 299 | + |
|
| 300 | + // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 301 | + |
|
| 302 | + // Bail if image is not found. |
|
| 303 | + // In some cases, you can delete the image from the database |
|
| 304 | + // or from uploads dir, but the image id still exists as featured image |
|
| 305 | + // or in [gallery] shortcode. |
|
| 306 | + // if ( empty( $attachment[0] ) ) { |
|
| 307 | + if ( empty( $sources_with_image ) ) { |
|
| 308 | + return $carry; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + // Merge the arrays. |
|
| 312 | + return array_merge( |
|
| 313 | + $carry, |
|
| 314 | + $sources_with_image |
|
| 315 | + ); |
|
| 316 | + }, |
|
| 317 | + // Initial array. |
|
| 318 | + |
|
| 319 | + array() |
|
| 320 | + ) |
|
| 321 | + ); |
|
| 322 | + |
|
| 323 | + // Refactor data as per schema.org specifications. |
|
| 324 | + $images = array_map( |
|
| 325 | + function ( $attachment ) { |
|
| 326 | + return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 327 | + array( |
|
| 328 | + '@type' => 'ImageObject', |
|
| 329 | + 'url' => $attachment[0], |
|
| 330 | + ), |
|
| 331 | + $attachment |
|
| 332 | + ); |
|
| 333 | + }, |
|
| 334 | + $images_with_sizes |
|
| 335 | + ); |
|
| 336 | + |
|
| 337 | + // Add images if present. |
|
| 338 | + if ( 0 < count( $images ) ) { |
|
| 339 | + $jsonld['image'] = $images; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * If the provided array of values contains only one value, then one single |
|
| 346 | + * value is returned, otherwise the original array is returned. |
|
| 347 | + * |
|
| 348 | + * @param array $value An array of values. |
|
| 349 | + * |
|
| 350 | + * @return mixed|array A single value or the original array. |
|
| 351 | + * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 352 | + * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 353 | + * @since 3.8.0 |
|
| 354 | + * @access private |
|
| 355 | + */ |
|
| 356 | + protected static function make_one( $value ) { |
|
| 357 | + |
|
| 358 | + return 1 === count( $value ) ? $value[0] : $value; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Process the provided array by adding the width / height if the values |
|
| 363 | + * are available and are greater than 0. |
|
| 364 | + * |
|
| 365 | + * @param array $image The `ImageObject` array. |
|
| 366 | + * @param array $attachment The attachment array. |
|
| 367 | + * |
|
| 368 | + * @return array The enriched `ImageObject` array. |
|
| 369 | + * @since 3.14.0 |
|
| 370 | + */ |
|
| 371 | + public static function set_image_size( $image, $attachment ) { |
|
| 372 | + |
|
| 373 | + // If you specify a "width" or "height" value you should leave out |
|
| 374 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 375 | + // |
|
| 376 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 377 | + if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 378 | + $image['width'] = $attachment[1]; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 382 | + $image['height'] = $attachment[2]; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + return $image; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property |
|
| 390 | + * for the post entity type. |
|
| 391 | + * |
|
| 392 | + * @param array $jsonld The JSON-LD array. |
|
| 393 | + * @param array $fields The entity types field array. |
|
| 394 | + * @param WP_Post $post The target {@link WP_Post} instance. |
|
| 395 | + * @param array $references The references array. |
|
| 396 | + * |
|
| 397 | + * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
|
| 398 | + * the {@link Wordlift_Schema_Service} class. |
|
| 399 | + */ |
|
| 400 | + protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
| 401 | + |
|
| 402 | + // Set a reference to use in closures. |
|
| 403 | + $converter = $this; |
|
| 404 | + |
|
| 405 | + // Try each field on the entity. |
|
| 406 | + foreach ( $fields as $key => $value ) { |
|
| 407 | + |
|
| 408 | + // Get the predicate. |
|
| 409 | + $name = $this->relative_to_context( $value['predicate'] ); |
|
| 410 | + |
|
| 411 | + // Get the value, the property service will get the right extractor |
|
| 412 | + // for that property. |
|
| 413 | + $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
| 414 | + |
|
| 415 | + if ( empty( $value ) ) { |
|
| 416 | + continue; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + // Map the value to the property name. |
|
| 420 | + // If we got an array with just one value, we return that one value. |
|
| 421 | + // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
| 422 | + $jsonld[ $name ] = self::make_one( |
|
| 423 | + array_map( |
|
| 424 | + function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
| 425 | + |
|
| 426 | + if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 427 | + |
|
| 428 | + $url = $item->get_url(); |
|
| 429 | + |
|
| 430 | + // The refactored converters require the entity id. |
|
| 431 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 432 | + $references[] = $item->to_reference(); |
|
| 433 | + |
|
| 434 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 435 | + $references_infos[] = array( 'reference' => $item ); |
|
| 436 | + |
|
| 437 | + return array( '@id' => $url ); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + return $converter->relative_to_context( $item ); |
|
| 441 | + }, |
|
| 442 | + $value |
|
| 443 | + ) |
|
| 444 | + ); |
|
| 445 | + |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + } |
|
| 449 | 449 | |
| 450 | 450 | } |
@@ -21,355 +21,355 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Cached_Post_Converter implements Wordlift_Post_Converter { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * A {@link Wordlift_Post_Converter} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.16.0 |
|
| 28 | - * |
|
| 29 | - * @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance. |
|
| 30 | - */ |
|
| 31 | - private $converter; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A {@link Wordlift_Log_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.16.0 |
|
| 37 | - * |
|
| 38 | - * @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance. |
|
| 39 | - */ |
|
| 40 | - private $log; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * A list of meta keys that do not cause the cache to update. |
|
| 44 | - * |
|
| 45 | - * @since 3.17.3 |
|
| 46 | - * @var array An array of ignored meta keys. |
|
| 47 | - */ |
|
| 48 | - private static $ignored_meta_keys = array( |
|
| 49 | - '_edit_lock', |
|
| 50 | - '_edit_last', |
|
| 51 | - '_wp_page_template', |
|
| 52 | - '_wp_attachment_is_custom_background', |
|
| 53 | - '_wp_attachment_backup_sizes', |
|
| 54 | - '_wp_attachment_is_custom_header', |
|
| 55 | - ); |
|
| 56 | - /** |
|
| 57 | - * @var Ttl_Cache |
|
| 58 | - */ |
|
| 59 | - private $cache; |
|
| 60 | - /** |
|
| 61 | - * @var Reference_Processor |
|
| 62 | - */ |
|
| 63 | - private $reference_processor; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Wordlift_Cached_Post_Converter constructor. |
|
| 67 | - * |
|
| 68 | - * @param \Wordlift_Post_Converter $converter The {@link Wordlift_Post_Converter} implementation. |
|
| 69 | - * @param Ttl_Cache $cache The {@link Ttl_Cache} cache instance. |
|
| 70 | - */ |
|
| 71 | - public function __construct( $converter, $cache ) { |
|
| 72 | - |
|
| 73 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 74 | - |
|
| 75 | - $this->cache = $cache; |
|
| 76 | - $this->converter = $converter; |
|
| 77 | - $this->reference_processor = Reference_Processor::get_instance(); |
|
| 78 | - $this->init_hooks(); |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Hooks to catch post/post meta changes in order to invalidate the cache. |
|
| 84 | - * |
|
| 85 | - * @since 3.16.0 |
|
| 86 | - */ |
|
| 87 | - private function init_hooks() { |
|
| 88 | - |
|
| 89 | - // Hook on post save to flush relevant cache. |
|
| 90 | - add_action( 'save_post', array( $this, 'save_post' ) ); |
|
| 91 | - |
|
| 92 | - add_action( |
|
| 93 | - 'added_post_meta', |
|
| 94 | - array( |
|
| 95 | - $this, |
|
| 96 | - 'changed_post_meta', |
|
| 97 | - ), |
|
| 98 | - 10, |
|
| 99 | - 3 |
|
| 100 | - ); |
|
| 101 | - add_action( |
|
| 102 | - 'updated_post_meta', |
|
| 103 | - array( |
|
| 104 | - $this, |
|
| 105 | - 'changed_post_meta', |
|
| 106 | - ), |
|
| 107 | - 10, |
|
| 108 | - 3 |
|
| 109 | - ); |
|
| 110 | - add_action( |
|
| 111 | - 'deleted_post_meta', |
|
| 112 | - array( |
|
| 113 | - $this, |
|
| 114 | - 'changed_post_meta', |
|
| 115 | - ), |
|
| 116 | - 10, |
|
| 117 | - 3 |
|
| 118 | - ); |
|
| 119 | - |
|
| 120 | - // Flush cache when wordlift settings were updated. |
|
| 121 | - add_action( |
|
| 122 | - 'update_option_wl_general_settings', |
|
| 123 | - array( |
|
| 124 | - $this, |
|
| 125 | - 'update_option_wl_general_settings', |
|
| 126 | - ) |
|
| 127 | - ); |
|
| 128 | - |
|
| 129 | - // Flushes the cache when permalink structure is changed. |
|
| 130 | - add_action( |
|
| 131 | - 'update_option_permalink_structure', |
|
| 132 | - array( |
|
| 133 | - $this, |
|
| 134 | - 'permalinks_structure_changed', |
|
| 135 | - ) |
|
| 136 | - ); |
|
| 137 | - |
|
| 138 | - // Invalid cache on relationship change. |
|
| 139 | - add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
| 140 | - add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
| 141 | - |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Note that the `&$cache` parameter here is used only to report whether the response comes from the cache. It |
|
| 146 | - * used by `test-issue-626.php` and nowhere else in code. |
|
| 147 | - * |
|
| 148 | - * @inheritdoc |
|
| 149 | - */ |
|
| 150 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 151 | - public function convert( $post_id, &$references = array(), &$references_infos = array(), &$cache = false ) { |
|
| 152 | - |
|
| 153 | - // Ensure post ID is `int`. Otherwise we may have issues with caching, since caching is strict about |
|
| 154 | - // key var types. |
|
| 155 | - $post_id = (int) $post_id; |
|
| 156 | - |
|
| 157 | - $this->log->trace( "Converting post $post_id..." ); |
|
| 158 | - |
|
| 159 | - // Try to get a cached result. |
|
| 160 | - $contents = $this->get_cache( $post_id, $references ); |
|
| 161 | - |
|
| 162 | - // Return the cached contents if any. |
|
| 163 | - if ( false !== $contents ) { |
|
| 164 | - $this->log->debug( "Cached contents found for post $post_id." ); |
|
| 165 | - |
|
| 166 | - // Inform the caller that this is cached result. |
|
| 167 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 168 | - $cache = true; |
|
| 169 | - $this->add_http_header( $post_id, true ); |
|
| 170 | - |
|
| 171 | - // Return the contents. |
|
| 172 | - return $contents; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - // Set cached to false. |
|
| 176 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 177 | - $cache = false; |
|
| 178 | - $this->add_http_header( $post_id, false ); |
|
| 179 | - |
|
| 180 | - // Convert the post. |
|
| 181 | - $jsonld = $this->converter->convert( $post_id, $references, $references_infos ); |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @since 3.32.0 |
|
| 185 | - * We cant apply json_encode on the objects {@link \Wordlift\Jsonld\Reference}, so we decode |
|
| 186 | - * it here before saving it on cache. |
|
| 187 | - */ |
|
| 188 | - // Cache the results. |
|
| 189 | - $this->set_cache( $post_id, $references, $jsonld ); |
|
| 190 | - |
|
| 191 | - // Finally return the JSON-LD. |
|
| 192 | - return $jsonld; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Try to get the cached contents. |
|
| 197 | - * |
|
| 198 | - * @param int $post_id The {@link WP_Post} id. |
|
| 199 | - * @param array $references The referenced posts. |
|
| 200 | - * |
|
| 201 | - * @return mixed|bool The cached contents or false if the cached isn't found. |
|
| 202 | - * @since 3.16.0 |
|
| 203 | - */ |
|
| 204 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 205 | - private function get_cache( $post_id, &$references = array() ) { |
|
| 206 | - |
|
| 207 | - // Ensure post ID is int, because cache is strict about var types. |
|
| 208 | - $post_id = (int) $post_id; |
|
| 209 | - |
|
| 210 | - $this->log->trace( "Getting cached contents for post $post_id..." ); |
|
| 211 | - |
|
| 212 | - // Get the cache. |
|
| 213 | - $modified_date_time = get_post_datetime( $post_id, 'modified', 'gmt' ); |
|
| 214 | - $contents = $this->cache->get( $post_id, $modified_date_time->getTimestamp() ); |
|
| 215 | - |
|
| 216 | - // Bail out if we don't have cached contents or the cached contents are |
|
| 217 | - // invalid. |
|
| 218 | - if ( null === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
| 219 | - $this->log->debug( "Cached contents for post $post_id not found." ); |
|
| 220 | - |
|
| 221 | - return false; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // Remap the cache. |
|
| 225 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 226 | - $references = $this->reference_processor->deserialize_references( $contents['references'] ); |
|
| 227 | - |
|
| 228 | - return $contents['jsonld']; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Set the cache with the provided results. |
|
| 233 | - * |
|
| 234 | - * The function will prepare the provided results and will ask the {@link Ttl_Cache} to cache them. |
|
| 235 | - * |
|
| 236 | - * @param int $post_id The {@link WP_Post} id. |
|
| 237 | - * @param array $references An array of references. |
|
| 238 | - * @param array $jsonld A JSON-LD structure. |
|
| 239 | - * |
|
| 240 | - * @since 3.16.0 |
|
| 241 | - */ |
|
| 242 | - private function set_cache( $post_id, $references, $jsonld ) { |
|
| 243 | - |
|
| 244 | - $this->log->trace( "Caching result for post $post_id..." ); |
|
| 245 | - |
|
| 246 | - $this->cache->put( |
|
| 247 | - $post_id, |
|
| 248 | - array( |
|
| 249 | - 'references' => $this->reference_processor->serialize_references( $references ), |
|
| 250 | - 'jsonld' => $jsonld, |
|
| 251 | - ) |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Hook to 'save_post', will invalidate the cache for that post. |
|
| 258 | - * |
|
| 259 | - * @param int $post_id The {@link WP_Post} id. |
|
| 260 | - * |
|
| 261 | - * @since 3.16.0 |
|
| 262 | - */ |
|
| 263 | - public function save_post( $post_id ) { |
|
| 264 | - |
|
| 265 | - $this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
| 266 | - |
|
| 267 | - $this->cache->delete( $post_id ); |
|
| 268 | - |
|
| 269 | - $this->flush_cache_if_publisher( $post_id ); |
|
| 270 | - |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Hook to meta changed for a {@link WP_Post}, will cause the cause to |
|
| 275 | - * invalidate. |
|
| 276 | - * |
|
| 277 | - * @param int $id The {@link WP_Post} meta id. |
|
| 278 | - * @param int $post_id The {@link WP_Post} id. |
|
| 279 | - * @param string $meta_key The meta key. |
|
| 280 | - * |
|
| 281 | - * @since 3.16.0 |
|
| 282 | - */ |
|
| 283 | - public function changed_post_meta( $id, $post_id, $meta_key ) { |
|
| 284 | - |
|
| 285 | - if ( in_array( $meta_key, self::$ignored_meta_keys, true ) ) { |
|
| 286 | - $this->log->trace( "Post $post_id meta $meta_key ignored." ); |
|
| 287 | - |
|
| 288 | - return; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - $this->log->trace( "Post $post_id meta $meta_key changed, invalidating cache..." ); |
|
| 292 | - |
|
| 293 | - // Delete the single cache file. |
|
| 294 | - $this->cache->delete( $post_id ); |
|
| 295 | - |
|
| 296 | - // Flush the cache if it's the publisher. |
|
| 297 | - $this->flush_cache_if_publisher( $post_id ); |
|
| 298 | - |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Hook to WordLift's options changes, will flush the cache. |
|
| 303 | - * |
|
| 304 | - * @since 3.16.0 |
|
| 305 | - */ |
|
| 306 | - public function update_option_wl_general_settings() { |
|
| 307 | - $this->log->trace( 'WordLift options changed, flushing cache...' ); |
|
| 308 | - |
|
| 309 | - $this->cache->flush(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Hook when permalinks are changed, will flush the cache. |
|
| 314 | - * |
|
| 315 | - * @since 3.17.0 |
|
| 316 | - */ |
|
| 317 | - public function permalinks_structure_changed() { |
|
| 318 | - $this->log->trace( 'Permalinks structure changed, flushing cache...' ); |
|
| 319 | - |
|
| 320 | - $this->cache->flush(); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Hook to WordLift's post/entity relation changes, will invalidate the cache. |
|
| 325 | - * |
|
| 326 | - * @param int $post_id The {@link WP_Post} id. |
|
| 327 | - * |
|
| 328 | - * @since 3.16.0 |
|
| 329 | - */ |
|
| 330 | - public function relation_changed( $post_id ) { |
|
| 331 | - $this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
| 332 | - |
|
| 333 | - $this->cache->delete( $post_id ); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * When in Ajax, prints an http header with the information whether the |
|
| 338 | - * response is cached or not. |
|
| 339 | - * |
|
| 340 | - * @param int $post_id The {@link WP_Post} id. |
|
| 341 | - * @param bool $cache Whether the response fragment is cached. |
|
| 342 | - * |
|
| 343 | - * @since 3.16.0 |
|
| 344 | - */ |
|
| 345 | - private function add_http_header( $post_id, $cache ) { |
|
| 346 | - |
|
| 347 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || headers_sent() ) { |
|
| 348 | - return; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - header( "X-WordLift-JsonLd-Cache-$post_id: " . ( $cache ? 'HIT' : 'MISS' ) ); |
|
| 352 | - |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Call the `flush` operation on the {@link Ttl_Cache} if |
|
| 357 | - * the publisher has changed. |
|
| 358 | - * |
|
| 359 | - * @param int $post_id The changed {@link WP_Post}'s id. |
|
| 360 | - * |
|
| 361 | - * @since 3.16.0 |
|
| 362 | - */ |
|
| 363 | - private function flush_cache_if_publisher( $post_id ) { |
|
| 364 | - |
|
| 365 | - // Bail out if it's not the publisher. |
|
| 366 | - if ( Wordlift_Configuration_Service::get_instance()->get_publisher_id() !== $post_id ) { |
|
| 367 | - return; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - // Flush the cache, since the publisher has changed. |
|
| 371 | - $this->cache->flush(); |
|
| 372 | - |
|
| 373 | - } |
|
| 24 | + /** |
|
| 25 | + * A {@link Wordlift_Post_Converter} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.16.0 |
|
| 28 | + * |
|
| 29 | + * @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance. |
|
| 30 | + */ |
|
| 31 | + private $converter; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A {@link Wordlift_Log_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.16.0 |
|
| 37 | + * |
|
| 38 | + * @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance. |
|
| 39 | + */ |
|
| 40 | + private $log; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * A list of meta keys that do not cause the cache to update. |
|
| 44 | + * |
|
| 45 | + * @since 3.17.3 |
|
| 46 | + * @var array An array of ignored meta keys. |
|
| 47 | + */ |
|
| 48 | + private static $ignored_meta_keys = array( |
|
| 49 | + '_edit_lock', |
|
| 50 | + '_edit_last', |
|
| 51 | + '_wp_page_template', |
|
| 52 | + '_wp_attachment_is_custom_background', |
|
| 53 | + '_wp_attachment_backup_sizes', |
|
| 54 | + '_wp_attachment_is_custom_header', |
|
| 55 | + ); |
|
| 56 | + /** |
|
| 57 | + * @var Ttl_Cache |
|
| 58 | + */ |
|
| 59 | + private $cache; |
|
| 60 | + /** |
|
| 61 | + * @var Reference_Processor |
|
| 62 | + */ |
|
| 63 | + private $reference_processor; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Wordlift_Cached_Post_Converter constructor. |
|
| 67 | + * |
|
| 68 | + * @param \Wordlift_Post_Converter $converter The {@link Wordlift_Post_Converter} implementation. |
|
| 69 | + * @param Ttl_Cache $cache The {@link Ttl_Cache} cache instance. |
|
| 70 | + */ |
|
| 71 | + public function __construct( $converter, $cache ) { |
|
| 72 | + |
|
| 73 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 74 | + |
|
| 75 | + $this->cache = $cache; |
|
| 76 | + $this->converter = $converter; |
|
| 77 | + $this->reference_processor = Reference_Processor::get_instance(); |
|
| 78 | + $this->init_hooks(); |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Hooks to catch post/post meta changes in order to invalidate the cache. |
|
| 84 | + * |
|
| 85 | + * @since 3.16.0 |
|
| 86 | + */ |
|
| 87 | + private function init_hooks() { |
|
| 88 | + |
|
| 89 | + // Hook on post save to flush relevant cache. |
|
| 90 | + add_action( 'save_post', array( $this, 'save_post' ) ); |
|
| 91 | + |
|
| 92 | + add_action( |
|
| 93 | + 'added_post_meta', |
|
| 94 | + array( |
|
| 95 | + $this, |
|
| 96 | + 'changed_post_meta', |
|
| 97 | + ), |
|
| 98 | + 10, |
|
| 99 | + 3 |
|
| 100 | + ); |
|
| 101 | + add_action( |
|
| 102 | + 'updated_post_meta', |
|
| 103 | + array( |
|
| 104 | + $this, |
|
| 105 | + 'changed_post_meta', |
|
| 106 | + ), |
|
| 107 | + 10, |
|
| 108 | + 3 |
|
| 109 | + ); |
|
| 110 | + add_action( |
|
| 111 | + 'deleted_post_meta', |
|
| 112 | + array( |
|
| 113 | + $this, |
|
| 114 | + 'changed_post_meta', |
|
| 115 | + ), |
|
| 116 | + 10, |
|
| 117 | + 3 |
|
| 118 | + ); |
|
| 119 | + |
|
| 120 | + // Flush cache when wordlift settings were updated. |
|
| 121 | + add_action( |
|
| 122 | + 'update_option_wl_general_settings', |
|
| 123 | + array( |
|
| 124 | + $this, |
|
| 125 | + 'update_option_wl_general_settings', |
|
| 126 | + ) |
|
| 127 | + ); |
|
| 128 | + |
|
| 129 | + // Flushes the cache when permalink structure is changed. |
|
| 130 | + add_action( |
|
| 131 | + 'update_option_permalink_structure', |
|
| 132 | + array( |
|
| 133 | + $this, |
|
| 134 | + 'permalinks_structure_changed', |
|
| 135 | + ) |
|
| 136 | + ); |
|
| 137 | + |
|
| 138 | + // Invalid cache on relationship change. |
|
| 139 | + add_action( 'wl_relation_added', array( $this, 'relation_changed' ) ); |
|
| 140 | + add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) ); |
|
| 141 | + |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Note that the `&$cache` parameter here is used only to report whether the response comes from the cache. It |
|
| 146 | + * used by `test-issue-626.php` and nowhere else in code. |
|
| 147 | + * |
|
| 148 | + * @inheritdoc |
|
| 149 | + */ |
|
| 150 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 151 | + public function convert( $post_id, &$references = array(), &$references_infos = array(), &$cache = false ) { |
|
| 152 | + |
|
| 153 | + // Ensure post ID is `int`. Otherwise we may have issues with caching, since caching is strict about |
|
| 154 | + // key var types. |
|
| 155 | + $post_id = (int) $post_id; |
|
| 156 | + |
|
| 157 | + $this->log->trace( "Converting post $post_id..." ); |
|
| 158 | + |
|
| 159 | + // Try to get a cached result. |
|
| 160 | + $contents = $this->get_cache( $post_id, $references ); |
|
| 161 | + |
|
| 162 | + // Return the cached contents if any. |
|
| 163 | + if ( false !== $contents ) { |
|
| 164 | + $this->log->debug( "Cached contents found for post $post_id." ); |
|
| 165 | + |
|
| 166 | + // Inform the caller that this is cached result. |
|
| 167 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 168 | + $cache = true; |
|
| 169 | + $this->add_http_header( $post_id, true ); |
|
| 170 | + |
|
| 171 | + // Return the contents. |
|
| 172 | + return $contents; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + // Set cached to false. |
|
| 176 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 177 | + $cache = false; |
|
| 178 | + $this->add_http_header( $post_id, false ); |
|
| 179 | + |
|
| 180 | + // Convert the post. |
|
| 181 | + $jsonld = $this->converter->convert( $post_id, $references, $references_infos ); |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @since 3.32.0 |
|
| 185 | + * We cant apply json_encode on the objects {@link \Wordlift\Jsonld\Reference}, so we decode |
|
| 186 | + * it here before saving it on cache. |
|
| 187 | + */ |
|
| 188 | + // Cache the results. |
|
| 189 | + $this->set_cache( $post_id, $references, $jsonld ); |
|
| 190 | + |
|
| 191 | + // Finally return the JSON-LD. |
|
| 192 | + return $jsonld; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Try to get the cached contents. |
|
| 197 | + * |
|
| 198 | + * @param int $post_id The {@link WP_Post} id. |
|
| 199 | + * @param array $references The referenced posts. |
|
| 200 | + * |
|
| 201 | + * @return mixed|bool The cached contents or false if the cached isn't found. |
|
| 202 | + * @since 3.16.0 |
|
| 203 | + */ |
|
| 204 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 205 | + private function get_cache( $post_id, &$references = array() ) { |
|
| 206 | + |
|
| 207 | + // Ensure post ID is int, because cache is strict about var types. |
|
| 208 | + $post_id = (int) $post_id; |
|
| 209 | + |
|
| 210 | + $this->log->trace( "Getting cached contents for post $post_id..." ); |
|
| 211 | + |
|
| 212 | + // Get the cache. |
|
| 213 | + $modified_date_time = get_post_datetime( $post_id, 'modified', 'gmt' ); |
|
| 214 | + $contents = $this->cache->get( $post_id, $modified_date_time->getTimestamp() ); |
|
| 215 | + |
|
| 216 | + // Bail out if we don't have cached contents or the cached contents are |
|
| 217 | + // invalid. |
|
| 218 | + if ( null === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) { |
|
| 219 | + $this->log->debug( "Cached contents for post $post_id not found." ); |
|
| 220 | + |
|
| 221 | + return false; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + // Remap the cache. |
|
| 225 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 226 | + $references = $this->reference_processor->deserialize_references( $contents['references'] ); |
|
| 227 | + |
|
| 228 | + return $contents['jsonld']; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Set the cache with the provided results. |
|
| 233 | + * |
|
| 234 | + * The function will prepare the provided results and will ask the {@link Ttl_Cache} to cache them. |
|
| 235 | + * |
|
| 236 | + * @param int $post_id The {@link WP_Post} id. |
|
| 237 | + * @param array $references An array of references. |
|
| 238 | + * @param array $jsonld A JSON-LD structure. |
|
| 239 | + * |
|
| 240 | + * @since 3.16.0 |
|
| 241 | + */ |
|
| 242 | + private function set_cache( $post_id, $references, $jsonld ) { |
|
| 243 | + |
|
| 244 | + $this->log->trace( "Caching result for post $post_id..." ); |
|
| 245 | + |
|
| 246 | + $this->cache->put( |
|
| 247 | + $post_id, |
|
| 248 | + array( |
|
| 249 | + 'references' => $this->reference_processor->serialize_references( $references ), |
|
| 250 | + 'jsonld' => $jsonld, |
|
| 251 | + ) |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Hook to 'save_post', will invalidate the cache for that post. |
|
| 258 | + * |
|
| 259 | + * @param int $post_id The {@link WP_Post} id. |
|
| 260 | + * |
|
| 261 | + * @since 3.16.0 |
|
| 262 | + */ |
|
| 263 | + public function save_post( $post_id ) { |
|
| 264 | + |
|
| 265 | + $this->log->trace( "Post $post_id saved, invalidating cache..." ); |
|
| 266 | + |
|
| 267 | + $this->cache->delete( $post_id ); |
|
| 268 | + |
|
| 269 | + $this->flush_cache_if_publisher( $post_id ); |
|
| 270 | + |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Hook to meta changed for a {@link WP_Post}, will cause the cause to |
|
| 275 | + * invalidate. |
|
| 276 | + * |
|
| 277 | + * @param int $id The {@link WP_Post} meta id. |
|
| 278 | + * @param int $post_id The {@link WP_Post} id. |
|
| 279 | + * @param string $meta_key The meta key. |
|
| 280 | + * |
|
| 281 | + * @since 3.16.0 |
|
| 282 | + */ |
|
| 283 | + public function changed_post_meta( $id, $post_id, $meta_key ) { |
|
| 284 | + |
|
| 285 | + if ( in_array( $meta_key, self::$ignored_meta_keys, true ) ) { |
|
| 286 | + $this->log->trace( "Post $post_id meta $meta_key ignored." ); |
|
| 287 | + |
|
| 288 | + return; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + $this->log->trace( "Post $post_id meta $meta_key changed, invalidating cache..." ); |
|
| 292 | + |
|
| 293 | + // Delete the single cache file. |
|
| 294 | + $this->cache->delete( $post_id ); |
|
| 295 | + |
|
| 296 | + // Flush the cache if it's the publisher. |
|
| 297 | + $this->flush_cache_if_publisher( $post_id ); |
|
| 298 | + |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Hook to WordLift's options changes, will flush the cache. |
|
| 303 | + * |
|
| 304 | + * @since 3.16.0 |
|
| 305 | + */ |
|
| 306 | + public function update_option_wl_general_settings() { |
|
| 307 | + $this->log->trace( 'WordLift options changed, flushing cache...' ); |
|
| 308 | + |
|
| 309 | + $this->cache->flush(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Hook when permalinks are changed, will flush the cache. |
|
| 314 | + * |
|
| 315 | + * @since 3.17.0 |
|
| 316 | + */ |
|
| 317 | + public function permalinks_structure_changed() { |
|
| 318 | + $this->log->trace( 'Permalinks structure changed, flushing cache...' ); |
|
| 319 | + |
|
| 320 | + $this->cache->flush(); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Hook to WordLift's post/entity relation changes, will invalidate the cache. |
|
| 325 | + * |
|
| 326 | + * @param int $post_id The {@link WP_Post} id. |
|
| 327 | + * |
|
| 328 | + * @since 3.16.0 |
|
| 329 | + */ |
|
| 330 | + public function relation_changed( $post_id ) { |
|
| 331 | + $this->log->trace( "Post $post_id relations changed, invalidating cache..." ); |
|
| 332 | + |
|
| 333 | + $this->cache->delete( $post_id ); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * When in Ajax, prints an http header with the information whether the |
|
| 338 | + * response is cached or not. |
|
| 339 | + * |
|
| 340 | + * @param int $post_id The {@link WP_Post} id. |
|
| 341 | + * @param bool $cache Whether the response fragment is cached. |
|
| 342 | + * |
|
| 343 | + * @since 3.16.0 |
|
| 344 | + */ |
|
| 345 | + private function add_http_header( $post_id, $cache ) { |
|
| 346 | + |
|
| 347 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || headers_sent() ) { |
|
| 348 | + return; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + header( "X-WordLift-JsonLd-Cache-$post_id: " . ( $cache ? 'HIT' : 'MISS' ) ); |
|
| 352 | + |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Call the `flush` operation on the {@link Ttl_Cache} if |
|
| 357 | + * the publisher has changed. |
|
| 358 | + * |
|
| 359 | + * @param int $post_id The changed {@link WP_Post}'s id. |
|
| 360 | + * |
|
| 361 | + * @since 3.16.0 |
|
| 362 | + */ |
|
| 363 | + private function flush_cache_if_publisher( $post_id ) { |
|
| 364 | + |
|
| 365 | + // Bail out if it's not the publisher. |
|
| 366 | + if ( Wordlift_Configuration_Service::get_instance()->get_publisher_id() !== $post_id ) { |
|
| 367 | + return; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + // Flush the cache, since the publisher has changed. |
|
| 371 | + $this->cache->flush(); |
|
| 372 | + |
|
| 373 | + } |
|
| 374 | 374 | |
| 375 | 375 | } |
@@ -17,189 +17,189 @@ |
||
| 17 | 17 | // @@todo: add a hook to clear the cached files now and then. |
| 18 | 18 | class Ttl_Cache { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * The cache name. |
|
| 22 | - * |
|
| 23 | - * @var string $name The cache name. |
|
| 24 | - * @access private |
|
| 25 | - * @since 3.21.2 |
|
| 26 | - */ |
|
| 27 | - private $name; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The TTL of cached responses in seconds. |
|
| 31 | - * |
|
| 32 | - * @var int $ttl The TTL in seconds. |
|
| 33 | - * @access private |
|
| 34 | - * @since 3.21.2 |
|
| 35 | - */ |
|
| 36 | - private $ttl; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The cache dir where the cached data is written. |
|
| 40 | - * |
|
| 41 | - * @since 3.21.2 |
|
| 42 | - * @access private |
|
| 43 | - * @var string $cache_dir The cache dir where the cached responses are written. |
|
| 44 | - */ |
|
| 45 | - private $cache_dir; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * A {@link Wordlift_Log_Service} instance. |
|
| 49 | - * |
|
| 50 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 51 | - * @access private |
|
| 52 | - * @since 3.21.2 |
|
| 53 | - */ |
|
| 54 | - private $log; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var array |
|
| 58 | - */ |
|
| 59 | - private static $caches = array(); |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs. |
|
| 63 | - * |
|
| 64 | - * @param string $name The cache name. |
|
| 65 | - * @param int $ttl The cache TTL, default 900 secs. |
|
| 66 | - * |
|
| 67 | - * @since 3.21.2 |
|
| 68 | - */ |
|
| 69 | - public function __construct( $name, $ttl = 900 ) { |
|
| 70 | - |
|
| 71 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 72 | - |
|
| 73 | - $this->name = $name; |
|
| 74 | - $this->ttl = $ttl; |
|
| 75 | - |
|
| 76 | - $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name ); |
|
| 77 | - |
|
| 78 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
| 79 | - wp_mkdir_p( $this->cache_dir ); |
|
| 80 | - |
|
| 81 | - self::$caches[ $name ] = $this; |
|
| 82 | - |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Get the root cache folder. |
|
| 87 | - * |
|
| 88 | - * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files. |
|
| 89 | - * |
|
| 90 | - * @return string The root cache folder. |
|
| 91 | - * @since 3.22.5 |
|
| 92 | - */ |
|
| 93 | - public static function get_cache_folder() { |
|
| 94 | - |
|
| 95 | - // Get the temp dir and add the directory separator if missing. |
|
| 96 | - $temp_dir = get_temp_dir(); |
|
| 97 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
| 98 | - $temp_dir .= DIRECTORY_SEPARATOR; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Get the cached data for the specified key. |
|
| 106 | - * |
|
| 107 | - * @param mixed $key A serializable key. |
|
| 108 | - * |
|
| 109 | - * @return mixed|null |
|
| 110 | - * @since 3.21.2 |
|
| 111 | - */ |
|
| 112 | - public function get( $key, $mintime = 0 ) { |
|
| 113 | - |
|
| 114 | - $filename = $this->get_filename( $key ); |
|
| 115 | - |
|
| 116 | - // No cache. |
|
| 117 | - if ( ! file_exists( $filename ) ) { |
|
| 118 | - return null; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // The cache is not updated or the ttl expired. Delete. |
|
| 122 | - $filemtime = filemtime( $filename ); |
|
| 123 | - if ( $filemtime < $mintime || $this->ttl < ( time() - $filemtime ) ) { |
|
| 124 | - $this->delete( $key ); |
|
| 125 | - |
|
| 126 | - return null; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $this->log->trace( "Cache HIT.\n" ); |
|
| 130 | - |
|
| 131 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
| 132 | - return json_decode( file_get_contents( $filename ), true ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - public function put( $key, $data ) { |
|
| 136 | - |
|
| 137 | - $filename = $this->get_filename( $key ); |
|
| 138 | - |
|
| 139 | - // Cache. |
|
| 140 | - if ( file_exists( $filename ) ) { |
|
| 141 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 142 | - @unlink( $filename ); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
| 146 | - @file_put_contents( $filename, wp_json_encode( $data ) ); |
|
| 147 | - |
|
| 148 | - } |
|
| 20 | + /** |
|
| 21 | + * The cache name. |
|
| 22 | + * |
|
| 23 | + * @var string $name The cache name. |
|
| 24 | + * @access private |
|
| 25 | + * @since 3.21.2 |
|
| 26 | + */ |
|
| 27 | + private $name; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The TTL of cached responses in seconds. |
|
| 31 | + * |
|
| 32 | + * @var int $ttl The TTL in seconds. |
|
| 33 | + * @access private |
|
| 34 | + * @since 3.21.2 |
|
| 35 | + */ |
|
| 36 | + private $ttl; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The cache dir where the cached data is written. |
|
| 40 | + * |
|
| 41 | + * @since 3.21.2 |
|
| 42 | + * @access private |
|
| 43 | + * @var string $cache_dir The cache dir where the cached responses are written. |
|
| 44 | + */ |
|
| 45 | + private $cache_dir; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * A {@link Wordlift_Log_Service} instance. |
|
| 49 | + * |
|
| 50 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 51 | + * @access private |
|
| 52 | + * @since 3.21.2 |
|
| 53 | + */ |
|
| 54 | + private $log; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var array |
|
| 58 | + */ |
|
| 59 | + private static $caches = array(); |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs. |
|
| 63 | + * |
|
| 64 | + * @param string $name The cache name. |
|
| 65 | + * @param int $ttl The cache TTL, default 900 secs. |
|
| 66 | + * |
|
| 67 | + * @since 3.21.2 |
|
| 68 | + */ |
|
| 69 | + public function __construct( $name, $ttl = 900 ) { |
|
| 70 | + |
|
| 71 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 72 | + |
|
| 73 | + $this->name = $name; |
|
| 74 | + $this->ttl = $ttl; |
|
| 75 | + |
|
| 76 | + $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name ); |
|
| 77 | + |
|
| 78 | + $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
| 79 | + wp_mkdir_p( $this->cache_dir ); |
|
| 80 | + |
|
| 81 | + self::$caches[ $name ] = $this; |
|
| 82 | + |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Get the root cache folder. |
|
| 87 | + * |
|
| 88 | + * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files. |
|
| 89 | + * |
|
| 90 | + * @return string The root cache folder. |
|
| 91 | + * @since 3.22.5 |
|
| 92 | + */ |
|
| 93 | + public static function get_cache_folder() { |
|
| 94 | + |
|
| 95 | + // Get the temp dir and add the directory separator if missing. |
|
| 96 | + $temp_dir = get_temp_dir(); |
|
| 97 | + if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
| 98 | + $temp_dir .= DIRECTORY_SEPARATOR; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Get the cached data for the specified key. |
|
| 106 | + * |
|
| 107 | + * @param mixed $key A serializable key. |
|
| 108 | + * |
|
| 109 | + * @return mixed|null |
|
| 110 | + * @since 3.21.2 |
|
| 111 | + */ |
|
| 112 | + public function get( $key, $mintime = 0 ) { |
|
| 113 | + |
|
| 114 | + $filename = $this->get_filename( $key ); |
|
| 115 | + |
|
| 116 | + // No cache. |
|
| 117 | + if ( ! file_exists( $filename ) ) { |
|
| 118 | + return null; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // The cache is not updated or the ttl expired. Delete. |
|
| 122 | + $filemtime = filemtime( $filename ); |
|
| 123 | + if ( $filemtime < $mintime || $this->ttl < ( time() - $filemtime ) ) { |
|
| 124 | + $this->delete( $key ); |
|
| 125 | + |
|
| 126 | + return null; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $this->log->trace( "Cache HIT.\n" ); |
|
| 130 | + |
|
| 131 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
| 132 | + return json_decode( file_get_contents( $filename ), true ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + public function put( $key, $data ) { |
|
| 136 | + |
|
| 137 | + $filename = $this->get_filename( $key ); |
|
| 138 | + |
|
| 139 | + // Cache. |
|
| 140 | + if ( file_exists( $filename ) ) { |
|
| 141 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 142 | + @unlink( $filename ); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
| 146 | + @file_put_contents( $filename, wp_json_encode( $data ) ); |
|
| 147 | + |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - public function delete( $key ) { |
|
| 150 | + public function delete( $key ) { |
|
| 151 | 151 | |
| 152 | - $filename = $this->get_filename( $key ); |
|
| 152 | + $filename = $this->get_filename( $key ); |
|
| 153 | 153 | |
| 154 | - // Delete. |
|
| 155 | - if ( file_exists( $filename ) ) { |
|
| 156 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 157 | - @unlink( $filename ); |
|
| 158 | - } |
|
| 154 | + // Delete. |
|
| 155 | + if ( file_exists( $filename ) ) { |
|
| 156 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 157 | + @unlink( $filename ); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - public function flush() { |
|
| 162 | + public function flush() { |
|
| 163 | 163 | |
| 164 | - $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' ); |
|
| 165 | - foreach ( $files as $file ) { // iterate files |
|
| 166 | - if ( is_file( $file ) ) { |
|
| 167 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 168 | - @unlink( $file ); |
|
| 169 | - } |
|
| 170 | - } |
|
| 164 | + $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' ); |
|
| 165 | + foreach ( $files as $file ) { // iterate files |
|
| 166 | + if ( is_file( $file ) ) { |
|
| 167 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 168 | + @unlink( $file ); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - } |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - public static function flush_all() { |
|
| 174 | + public static function flush_all() { |
|
| 175 | 175 | |
| 176 | - /** @var Ttl_Cache $cache */ |
|
| 177 | - foreach ( self::$caches as $cache ) { |
|
| 178 | - $cache->flush(); |
|
| 179 | - } |
|
| 176 | + /** @var Ttl_Cache $cache */ |
|
| 177 | + foreach ( self::$caches as $cache ) { |
|
| 178 | + $cache->flush(); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - } |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
| 185 | - * |
|
| 186 | - * @param string $hash A file hash. |
|
| 187 | - * |
|
| 188 | - * @return string The full path to the file. |
|
| 189 | - * @since 3.21.2 |
|
| 190 | - */ |
|
| 191 | - private function get_path( $hash ) { |
|
| 183 | + /** |
|
| 184 | + * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
| 185 | + * |
|
| 186 | + * @param string $hash A file hash. |
|
| 187 | + * |
|
| 188 | + * @return string The full path to the file. |
|
| 189 | + * @since 3.21.2 |
|
| 190 | + */ |
|
| 191 | + private function get_path( $hash ) { |
|
| 192 | 192 | |
| 193 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
| 194 | - } |
|
| 193 | + return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - private function get_filename( $key ) { |
|
| 196 | + private function get_filename( $key ) { |
|
| 197 | 197 | |
| 198 | - // Create a hash and a path to the cache file. |
|
| 199 | - $hash = md5( wp_json_encode( $key ) ); |
|
| 200 | - $filename = $this->get_path( $hash ); |
|
| 198 | + // Create a hash and a path to the cache file. |
|
| 199 | + $hash = md5( wp_json_encode( $key ) ); |
|
| 200 | + $filename = $this->get_path( $hash ); |
|
| 201 | 201 | |
| 202 | - return $filename; |
|
| 203 | - } |
|
| 202 | + return $filename; |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | 205 | } |