$response_body ); $data = json_decode( $json, true ); if ( empty( $data ) ) { $message = $data === null ? 'Invalid JSON' : 'Empty JSON'; wpforms_log( 'Cached data: ' . $message, [ 'class' => static::class, 'cache_file' => $this->settings['cache_file'], 'remote_source' => $this->settings['remote_source'], 'json_result' => $message, 'code' => $response_code, 'headers' => $response_headers, 'content_length' => $response_body_len, 'body' => $response_body_log, ], [ 'type' => [ 'error' ], ] ); return []; } return $this->prepare_cache_data( $data ); } /** * Schedule updates. * * @since 1.6.8 */ public function schedule_update_cache() { // Just skip if not need to register scheduled action. if ( empty( $this->settings['update_action'] ) ) { return; } $tasks = wpforms()->get( 'tasks' ); if ( ! $tasks instanceof Tasks || $tasks->is_scheduled( $this->settings['update_action'] ) !== false ) { return; } $tasks->create( $this->settings['update_action'] ) ->recurring( time() + $this->settings['cache_ttl'], $this->settings['cache_ttl'] ) ->params() ->register(); } /** * Complete the cache directory. * * @since 1.6.8 */ public function cache_dir_complete() { if ( ! $this->updated ) { return; } wpforms_create_upload_dir_htaccess_file(); wpforms_create_cache_dir_htaccess_file(); wpforms_create_index_html_file( $this->cache_dir ); wpforms_create_index_php_file( $this->cache_dir ); } /** * Invalidate cache. * * @since 1.8.7 */ public function invalidate_cache() { Transient::delete( $this->cache_key ); } /** * Prepare data to store in a local cache. * * @since 1.6.8 * * @param array|mixed $data Raw data received by the remote request. * * @return array Prepared data for caching. */ protected function prepare_cache_data( $data ): array { if ( empty( $data ) || ! is_array( $data ) ) { return []; } return $data; } /** * Maybe update transient duration time. * * Allows updating transient duration time if it's less than expiration time. * To do this, overwrite this method in child classes. * * @since 1.8.7 * * @param array $data Data received by the remote request. * * @return bool|array */ protected function maybe_update_transient( array $data ) { return $data; } }