instance field holding the HTML. */ public const WIDGETS = array( 'widget_text' => 'text', 'widget_custom_html' => 'content', 'widget_block' => 'content', ); /** * All links in active widgets. * * @param bool $include_images Include images. * @return array}> */ public static function extract_all( bool $include_images = true ): array { $active = self::active_widget_ids(); $home = home_url( '/' ); $result = array(); foreach ( self::WIDGETS as $option => $field ) { $id_base = substr( $option, strlen( 'widget_' ) ); $instances = get_option( $option, array() ); if ( ! is_array( $instances ) ) { continue; } foreach ( $instances as $number => $instance ) { if ( ! is_int( $number ) || ! is_array( $instance ) || ! isset( $active[ $id_base . '-' . $number ] ) ) { continue; } $html = isset( $instance[ $field ] ) && is_string( $instance[ $field ] ) ? $instance[ $field ] : ''; if ( '' === $html ) { continue; } $links = PostContentExtractor::extract( $html, $home, $include_images ); if ( empty( $links ) ) { continue; } $title = isset( $instance['title'] ) && is_string( $instance['title'] ) && '' !== $instance['title'] ? $instance['title'] : $id_base . '-' . $number; foreach ( $links as &$link ) { $link['source_key'] = $option; /* translators: %s: widget title or ID. */ $link['context'] = '' !== $link['context'] ? $link['context'] : sprintf( __( 'Widget: %s', 'unbreak' ), $title ); } unset( $link ); $result[] = array( 'source_id' => $number, 'source_key' => $option, 'links' => $links, ); } } return $result; } /** * Widget IDs (e.g. "text-2") placed in any sidebar except the inactive area. * * @return array */ private static function active_widget_ids(): array { $sidebars = get_option( 'sidebars_widgets', array() ); $active = array(); if ( ! is_array( $sidebars ) ) { return $active; } foreach ( $sidebars as $sidebar => $widgets ) { if ( 'wp_inactive_widgets' === $sidebar || ! is_array( $widgets ) ) { continue; } foreach ( $widgets as $widget_id ) { if ( is_string( $widget_id ) ) { $active[ $widget_id ] = true; } } } return $active; } }