sanitize_notices( $notices ); set_transient( 'affiliatex_campaign_notices', $notices, DAY_IN_SECONDS ); } /** * Sanitize each notice item by mapping each values inside the array * * @param array $notices * @return array */ private function sanitize_notices( array $notices ): array { $notices = array_map( function ( $notice ) { $notice['name'] = isset( $notice['name'] ) ? sanitize_text_field( $notice['name'] ) : ''; $notice['title'] = isset( $notice['title'] ) ? sanitize_text_field( $notice['title'] ) : ''; $notice['description'] = isset( $notice['description'] ) ? wp_kses_post( $notice['description'] ) : ''; $notice['option_buttons'] = isset( $notice['option_buttons'] ) && is_array( $notice['option_buttons'] ) ? $notice['option_buttons'] : array(); $notice['props'] = isset( $notice['props'] ) && is_array( $notice['props'] ) ? $notice['props'] : array(); $notice['props']['start'] = isset( $notice['props']['start'] ) ? sanitize_text_field( $notice['props']['start'] ) : ''; $notice['props']['end'] = isset( $notice['props']['end'] ) ? sanitize_text_field( $notice['props']['end'] ) : ''; $notice['props']['enabled'] = isset( $notice['props']['enabled'] ) ? (bool) sanitize_text_field( $notice['props']['enabled'] ) : false; $audience = isset( $notice['props']['audience'] ) ? sanitize_text_field( $notice['props']['audience'] ) : 'all'; $notice['props']['audience'] = in_array( $audience, array( 'all', 'free', 'pro' ), true ) ? $audience : 'all'; $notice['option_buttons'] = array_map( function ( $button ) { $button['title'] = isset( $button['title'] ) ? sanitize_text_field( $button['title'] ) : ''; if ( isset( $button['tag_name'] ) ) { $button['tag_name'] = sanitize_text_field( $button['tag_name'] ); } if ( ! isset( $button['attributes'] ) ) { return $button; } $attributes = $button['attributes']; $attributes = array_map( function ( $attribute ) { return sanitize_text_field( $attribute ); }, $attributes ); return array_merge( $button, array( 'attributes' => $attributes, ) ); }, $notice['option_buttons'] ); return $notice; }, $notices ); return $notices; } /** * Create @CampaignNotice instance for each notice stored in database * * @return void */ public function initiate_pulled_notices(): void { $notices = get_transient( 'affiliatex_campaign_notices' ) ? get_transient( 'affiliatex_campaign_notices' ) : array(); foreach ( $notices as $notice ) { if ( empty( $notice['name'] ) || ! isset( $notice['props']['enabled'] ) || $notice['props']['enabled'] === false ) { continue; } new CampaignNotice( $notice['name'], $notice['title'], $notice['description'], $notice['option_buttons'], $notice['props'] ); } } }