le::get_contents( $this->cache_file ); // Do not decrypt non-encrypted legacy files, they will be encrypted on the scheduled update. if ( static::ENCRYPT && ! wpforms_is_json( $content ) ) { $content = Crypto::decrypt( $content ); } return (array) json_decode( $content, true ); } /** * Update cache. * * @since 1.8.2 * * @param bool $force Force update. * * @return bool */ public function update( bool $force = false ): bool { if ( ! $force && time() < $this->cache_time() + self::REQUEST_LOCK_TIME * MINUTE_IN_SECONDS ) { return false; } Transient::set( $this->cache_key, time(), $this->settings['cache_ttl'] ); if ( ! wp_mkdir_p( $this->cache_dir ) ) { return false; } $data = $this->perform_remote_request(); $content = wp_json_encode( $data ); $this->maybe_update_transient( $data ); if ( static::ENCRYPT ) { $content = Crypto::encrypt( $content ); } if ( ! File::put_contents( $this->cache_file, $content ) ) { return false; } $this->updated = true; return true; } /** * Get cached data. * * @since 1.6.8 * @deprecated 1.8.2 * * @return array Cached data. * @noinspection PhpUnused */ public function get_cached() { _deprecated_function( __METHOD__, '1.8.2 of the WPForms plugin', __CLASS__ . '::get()' ); return $this->get(); } /** * Update cached data with actual data retrieved from the remote source. * * @since 1.6.8 * @deprecated 1.8.2 * * @return array * @noinspection PhpUnused */ public function update_cache() { _deprecated_function( __METHOD__, '1.8.2 of the WPForms plugin' ); $this->update(); return $this->get(); } /** * Get data from API. * * @since 1.8.2 * * @return array */ private function perform_remote_request(): array { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded $wpforms_key = wpforms()->is_pro() ? wpforms_get_license_key() : 'lite'; $query_args = array_merge( [ 'tgm-updater-key' => $wpforms_key ], $this->settings['query_args'] ?? [] ); $request_url = add_query_arg( $query_args, $this->settings['remote_source'] ); $user_agent = wpforms_get_default_user_agent(); $request = wp_remote_get( $request_url, [ 'timeout' => 10, 'user-agent' => $user_agent, ] ); $request_url_log = remove_query_arg( [ 'tgm-updater-key' ], $request_url ); $response = $request['http_response'] ?? null; $response_code = $response ? $response->get_status() : ''; $response_headers = wp_remote_retrieve_headers( $request )->getAll(); $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 ); $is_wp_error = is_wp_error( $request ); // Log the response details in debug mode. if ( wpforms_debug() ) { wpforms_log( 'Cached data: Response details', [ 'class' => static::class, 'request_url' => $request_url_log, 'code' => $response_code, 'headers' => $response_headers, 'content_length' => $response_body_len, 'body' => $response_body_log, ], [ 'type' => [ 'log' ], ] ); } // Log the error if any. if ( $response_code > 399 || $is_wp_error ) { wpforms_log( 'Cached data: HTTP request error', [ 'class' => static::class, 'request_url' => $request_url_log, 'is_wp_error' => $is_wp_error ? 'Yes' : 'No', 'error_message' => $is_wp_error ? $request->get_error_message() : '', 'code' => $response_code, 'headers' => $response_headers, 'content_length' => $response_body_len, 'body' => $response_body_log, 'error_data' => $is_wp_error ? $request->get_all_error_data() : '', ], [ 'type' => [ 'error' ], ] ); return []; } $json = trim( $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; } }