namespace WPForms\Emails;
* Fetching and formatting Info Blocks for Email Summaries class.
* Source of info blocks content.
const SOURCE_URL = 'https://wpformsapi.com/feeds/v1/email-summaries/%s';
* Get info blocks info from the cache file or remote.
public function get_all() {
$cache_file = $this->get_cache_file_path();
if ( empty( $cache_file ) || ! is_readable( $cache_file ) ) {
return $this->fetch_all();
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$contents = file_get_contents( $cache_file );
$contents = json_decode( $contents, true );
return $this->verify_fetched( $contents );
* Fetch info blocks info from remote.
public function fetch_all() {
$this->get_remote_source(),
'user-agent' => wpforms_get_default_user_agent(),
if ( is_wp_error( $res ) ) {
$body = wp_remote_retrieve_body( $res );
$body = json_decode( $body, true );
return $this->verify_fetched( $body );
* Verify fetched blocks data.
* @param array $fetched Fetched blocks data.
protected function verify_fetched( $fetched ) {
if ( ! \is_array( $fetched ) ) {
foreach ( $fetched as $item ) {
if ( empty( $item['id'] ) ) {
$id = \absint( $item['id'] );
* Get info blocks relevant to customer's licence.
protected function get_by_license() {
$data = $this->get_all();
if ( empty( $data ) || ! \is_array( $data ) ) {
// When there is no license, we assume it's a Lite version.
// This is needed to show blocks for Lite users, as they don't have a license type.
$license_type = wpforms_setting( 'type', 'lite', 'wpforms_license' );
foreach ( $data as $key => $item ) {
if ( ! isset( $item['type'] ) || ! \is_array( $item['type'] ) ) {
if ( ! \in_array( $license_type, $item['type'], true ) ) {
$filtered[ $key ] = $item;
* Get the first block with a valid id.
* Needed to ignore blocks with invalid/missing ids.
* @param array $data Blocks array.
protected function get_first_with_id( $data ) {
if ( empty( $data ) || ! \is_array( $data ) ) {
foreach ( $data as $item ) {
$item_id = \absint( $item['id'] );
if ( ! empty( $item_id ) ) {
* Get next info block that wasn't sent yet.
public function get_next() {
$data = $this->get_by_license();
if ( empty( $data ) || ! \is_array( $data ) ) {
$blocks_sent = \get_option( 'wpforms_emails_infoblocks_sent' );
if ( empty( $blocks_sent ) || ! \is_array( $blocks_sent ) ) {
$block = $this->get_first_with_id( $data );
$data = \array_diff_key( $data, \array_flip( $blocks_sent ) );
$block = $this->get_first_with_id( $data );
* Register a block as sent.
* @param array $info_block Info block.
public function register_sent( $info_block ) {
$block_id = isset( $info_block['id'] ) ? absint( $info_block['id'] ) : false;
if ( empty( $block_id ) ) {
$option_name = 'wpforms_email_summaries_info_blocks_sent';
$blocks = get_option( $option_name );
if ( empty( $blocks ) || ! is_array( $blocks ) ) {
update_option( $option_name, [ $block_id ] );
if ( in_array( $block_id, $blocks, true ) ) {
update_option( $option_name, $blocks );
* Get a path of the blocks cache file.
public function get_cache_file_path() {
$upload_dir = wpforms_upload_dir();
if ( ! isset( $upload_dir['path'] ) ) {
$cache_dir = trailingslashit( $upload_dir['path'] ) . 'cache';
return wp_normalize_path( trailingslashit( $cache_dir ) . 'email-summaries.json' );
* Fetch and cache blocks in a file.
public function cache_all() {
$file_path = $this->get_cache_file_path();
if ( empty( $file_path ) ) {
$dir = dirname( $file_path );
if ( ! wp_mkdir_p( $dir ) ) {
wpforms_create_index_html_file( $dir );
wpforms_create_upload_dir_htaccess_file();
$info_blocks = $this->fetch_all();
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
file_put_contents( $file_path, wp_json_encode( $info_blocks ) );
* Retrieve the source URL.
protected function get_remote_source(): string {
$license_type = wpforms()->is_pro() ? wpforms_get_license_type() : 'lite';
return sprintf( self::SOURCE_URL, $license_type );