rs ) ? $raw_headers->getAll() : (array) $raw_headers; $response_body = wp_remote_retrieve_body( $request ); $response_body_len = strlen( $response_body ); $response_body_log = $response_body_len > 1024 ? "(First 1 kB):\n" . substr( trim( $response_body ), 0, 1024 ) . '...' : trim( $response_body ); $response_body_log = esc_html( $response_body_log ); $log_data = [ 'class' => static::class, 'request_url' => $request_url_log, 'code' => $response_code, 'headers' => $response_headers, 'content_length' => $response_body_len, 'body' => $response_body_log, ]; // Log the response details in debug mode. if ( wpforms_debug() ) { $this->add_log( 'Cached data: Response details', $log_data ); } // Log the error if the response code is not 2xx or 3xx. if ( $response_code > 399 ) { $this->add_log( 'Cached data: HTTP request error', $log_data, 'error' ); return []; } $json = trim( $response_body ); $data = json_decode( $json, true ); if ( empty( $data ) ) { $message = $data === null ? 'Invalid JSON' : 'Empty JSON'; $log_data = array_merge( $log_data, [ 'json_result' => $message, 'cache_file' => $this->settings['cache_file'], 'remote_source' => $this->settings['remote_source'], ] ); $this->add_log( 'Cached data: ' . $message, $log_data, 'error' ); return []; } return $this->prepare_cache_data( $data ); } /** * Add log. * * @since 1.8.9 * * @param string $title Log title. * @param array $data Log data. * @param string $type Log type. */ private function add_log( string $title, array $data, string $type = 'log' ) { wpforms_log( $title, $data, [ 'type' => [ $type ], ] ); } /** * 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()->obj( '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; } }