placement(); $registered = array(); if ( isset( $wp_widget_factory->widgets ) && is_array( $wp_widget_factory->widgets ) ) { foreach ( $wp_widget_factory->widgets as $w ) { if ( isset( $w->id_base ) ) { $registered[ 'widget_' . $w->id_base ] = $w->id_base; } } } $limit = $this->memory_limit(); $discovery = $this->option_names( $limit ); $names = array_unique( array_merge( array_keys( $registered ), $discovery['names'] ) ); sort( $names ); $strings = array(); $by_widget = array(); $block_attr = array(); $truncated = null !== $discovery['truncated']; $reason = $discovery['truncated']; $read = 0; $skip = array( self::LEGACY_OPTION, self::LEGACY_SETTINGS ); foreach ( $names as $name ) { if ( in_array( $name, $skip, true ) || 0 === strpos( $name, 'widget_logic' ) ) { continue; } if ( $limit > 0 && memory_get_usage() > $limit * self::MEMORY_SHARE ) { $truncated = true; $reason = 'memory'; break; } ++$read; $value = get_option( $name ); if ( ! is_array( $value ) ) { continue; } $id_base = substr( $name, 7 ); if ( isset( $value['widget_logic'] ) && is_string( $value['widget_logic'] ) && ! $this->is_numbered( $value ) ) { // Old-style widget: top-level key in widget_ (Q2). $this->add( $strings, $by_widget, $id_base, 'old_style', 'old:' . $id_base, $value['widget_logic'], $placement, $value, false ); continue; } foreach ( $value as $number => $inst ) { if ( ! is_int( $number ) || ! is_array( $inst ) ) { continue; } $wid = $id_base . '-' . $number; if ( 'block' === $id_base && isset( $inst['content'] ) && is_string( $inst['content'] ) && false !== strpos( $inst['content'], '"widgetLogic"' ) ) { foreach ( $this->block_attr_rules( $inst['content'] ) as $found ) { $block_attr[] = array( 'widget_id' => $wid, 'block' => $found['block'], 'source' => $found['logic'], ); } } if ( ! array_key_exists( 'widget_logic', $inst ) || ! is_string( $inst['widget_logic'] ) ) { continue; } $location = 'block' === $id_base ? 'block_widget' : 'instance'; $orphan = ! isset( $registered[ $name ] ); $this->add( $strings, $by_widget, $wid, $location, 'instance:' . $wid, $inst['widget_logic'], $placement, $inst, $orphan ); } } // The legacy widget_logic option: widget ID => string (instance string wins, conflict listed). $legacy = get_option( self::LEGACY_OPTION ); $conflicts = array(); $globals = array(); if ( is_array( $legacy ) ) { foreach ( $legacy as $key => $text ) { $key = (string) $key; if ( 0 === strpos( $key, 'widget_logic-options-' ) ) { $globals[ $key ] = $text; continue; } if ( 'msg' === $key || ! is_string( $text ) ) { continue; } $text = stripslashes( $text ); // As Widget Logic itself read it. if ( isset( $by_widget[ $key ] ) ) { $conflicts[] = array( 'widget_id' => $key, 'kept' => 'instance', 'dropped' => 'legacy_option', 'source' => $text, ); continue; } $this->add( $strings, $by_widget, $key, 'legacy_option', 'option:' . $key, $text, $placement, null, false ); } } // Optional export file. Entries that match no widget are kept as records // with their original expression (R-1); nothing is skipped silently. $unmatched = array(); $unmatched_entries = array(); $file_skipped = array(); if ( is_array( $export_entries ) ) { foreach ( $export_entries as $key => $text ) { $key = (string) $key; if ( 0 === strpos( $key, 'widget_logic-options-' ) ) { if ( ! isset( $globals[ $key ] ) ) { $globals[ $key ] = $text; } continue; } if ( 'msg' === $key ) { continue; // Widget Logic's own status message, not a widget. } if ( ! is_string( $text ) ) { // The reader rejects these at parse time (R-2); kept as a backstop. $file_skipped[] = array( 'widget_id' => $key, 'reason' => 'value_not_string', ); continue; } if ( ! $this->widget_exists( $key, $placement ) ) { $unmatched[] = $key; $unmatched_entries[] = array( 'widget_id' => $key, 'source' => $text, ); continue; } if ( isset( $by_widget[ $key ] ) ) { $conflicts[] = array( 'widget_id' => $key, 'kept' => 'site', 'dropped' => 'export_file', 'source' => $text, ); continue; } $this->add( $strings, $by_widget, $key, 'export_file', 'file:' . $key, $text, $placement, null, false ); } } $settings6 = get_option( self::LEGACY_SETTINGS, null ); return array( 'strings' => $strings, 'conflicts' => $conflicts, 'globals' => $globals, 'settings_6x' => $settings6, 'six_x_evidence' => null !== $settings6, 'block_attr_rules' => $block_attr, 'unmatched_file_ids' => $unmatched, 'unmatched_entries' => $unmatched_entries, 'file_skipped' => $file_skipped, 'allowed_functions' => $this->allowed_functions_filter(), 'widget_content' => $this->widget_content_hooks(), 'truncated' => $truncated, 'truncated_reason' => $truncated ? (string) $reason : '', 'options_found' => count( $discovery['names'] ), 'options_listed' => count( array_filter( $names, static function ( $n ) { return 0 !== strpos( $n, 'widget_logic' ); } ) ), 'options_read' => $read, 'option_name_cap' => $discovery['cap'], ); } /** * Discover widget_* option names in bounded pages (R-3). Stops at the cap * or when memory runs short, and says so; never returns a partial list as * if it were complete. * * @param int $limit PHP memory limit in bytes (0 = unlimited). * @return array{names:array, truncated:?string, cap:int} truncated is null, 'option_name_limit' or 'memory'. */ public function option_names( $limit ) { global $wpdb; /** * Filters the most widget_* option names one scan will read. * * @param int $max Default Scanner::MAX_OPTION_NAMES. */ $max = max( 1, (int) apply_filters( 'gtfg_scan_max_option_names', self::MAX_OPTION_NAMES ) ); $page = min( self::NAME_PAGE, $max + 1 ); $names = array(); $after = ''; $like = $wpdb->esc_like( 'widget_' ) . '%'; while ( true ) { $batch = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s AND option_name > %s ORDER BY option_name LIMIT %d", $like, $after, $page ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- names only, bounded pages; values are read through get_option(). $batch = is_array( $batch ) ? $batch : array(); foreach ( $batch as $n ) { if ( count( $names ) >= $max ) { return array( 'names' => $names, 'truncated' => 'option_name_limit', 'cap' => $max, ); } $names[] = (string) $n; } if ( count( $batch ) < $page ) { break; } if ( $limit > 0 && memory_get_usage() > $limit * self::MEMORY_SHARE ) { return array( 'names' => $names, 'truncated' => 'memory', 'cap' => $max, ); } $after = (string) end( $batch ); } return array( 'names' => $names, 'truncated' => null, 'cap' => $max, ); } /** * Placement map from the raw sidebars_widgets option (never the filtered one). * * @return array widget ID => sidebar ID. */ public function placement() { $raw = get_option( 'sidebars_widgets', array() ); $out = array(); if ( is_array( $raw ) ) { foreach ( $raw as $sidebar => $ids ) { if ( is_array( $ids ) ) { foreach ( $ids as $id ) { if ( is_string( $id ) ) { $out[ $id ] = (string) $sidebar; } } } } } return $out; } /** * Add one string. * * @param array $strings List (by reference). * @param array $by_widget Index (by reference). * @param string $wid Widget ID. * @param string $location Location kind. * @param string $ref Source ref. * @param string $text String. * @param array $placement Placement map. * @param array|null $inst Instance. * @param bool $orphan Orphan instance. */ private function add( array &$strings, array &$by_widget, $wid, $location, $ref, $text, array $placement, $inst, $orphan ) { $by_widget[ $wid ] = true; if ( '' === trim( $text ) ) { return; // An empty string never restricted the widget. } $sidebar = isset( $placement[ $wid ] ) ? $placement[ $wid ] : ''; $strings[] = array( 'widget_id' => $wid, 'location' => $location, 'ref' => $ref, 'source' => $text, 'sidebar' => $sidebar, 'title' => ( is_array( $inst ) && isset( $inst['title'] ) && is_string( $inst['title'] ) ) ? $inst['title'] : '', 'inactive' => '' === $sidebar || 'wp_inactive_widgets' === $sidebar, 'orphan' => $orphan, ); } /** * Does a value have numbered-instance structure. * * @param array $value Option value. * @return bool */ private function is_numbered( array $value ) { foreach ( $value as $k => $v ) { if ( is_int( $k ) && is_array( $v ) ) { return true; } } return false; } /** * Does a widget ID exist on this site. * * @param string $wid Widget ID. * @param array $placement Placement. * @return bool */ private function widget_exists( $wid, array $placement ) { if ( isset( $placement[ $wid ] ) ) { return true; } return null !== \GateForge\Storage\Widget_Store::instance( $wid ); } /** * Find Widget Logic 6.x per-block widgetLogic attributes in block markup (Q3). * Listed in the report only; block content is never rewritten. * * @param string $content Block markup. * @return array */ private function block_attr_rules( $content ) { $out = array(); $walk = function ( $blocks ) use ( &$walk, &$out ) { foreach ( $blocks as $b ) { if ( isset( $b['attrs']['widgetLogic'] ) && is_string( $b['attrs']['widgetLogic'] ) && '' !== trim( $b['attrs']['widgetLogic'] ) ) { $out[] = array( 'block' => (string) $b['blockName'], 'logic' => $b['attrs']['widgetLogic'], ); } if ( ! empty( $b['innerBlocks'] ) ) { $walk( $b['innerBlocks'] ); } } }; $walk( parse_blocks( $content ) ); return $out; } /** * Extra names from the legacy filter (R4). Calls the theme's filter, which * returns names; it never calls the named functions. * * @return array */ private function allowed_functions_filter() { $names = apply_filters( 'widget_logic_allowed_functions', array() ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- reading the legacy plugin's filter on purpose. $out = array(); if ( is_array( $names ) ) { foreach ( $names as $n ) { if ( is_string( $n ) && preg_match( '/^[A-Za-z_][A-Za-z0-9_]*$/', $n ) ) { $out[] = $n; } } } return array_values( array_unique( $out ) ); } /** * Callbacks hooked on widget_content (D-7): only reported when something is hooked. * * @return array */ private function widget_content_hooks() { global $wp_filter; $out = array(); if ( ! has_filter( 'widget_content' ) || ! isset( $wp_filter['widget_content'] ) ) { return $out; } foreach ( $wp_filter['widget_content']->callbacks as $priority => $cbs ) { foreach ( $cbs as $cb ) { $file = ''; try { if ( is_string( $cb['function'] ) && function_exists( $cb['function'] ) ) { $r = new \ReflectionFunction( $cb['function'] ); $file = (string) $r->getFileName(); } elseif ( $cb['function'] instanceof \Closure ) { $r = new \ReflectionFunction( $cb['function'] ); $file = (string) $r->getFileName(); } elseif ( is_array( $cb['function'] ) && 2 === count( $cb['function'] ) ) { $r = new \ReflectionMethod( $cb['function'][0], (string) $cb['function'][1] ); $file = (string) $r->getFileName(); } } catch ( \ReflectionException $e ) { $file = ''; } $out[] = array( 'priority' => (int) $priority, 'file' => $file ? str_replace( ABSPATH, '', $file ) : '', ); } } return $out; } /** * PHP memory limit in bytes, or 0 when unlimited. * * @return int */ private function memory_limit() { $v = function_exists( 'wp_convert_hr_to_bytes' ) ? wp_convert_hr_to_bytes( (string) ini_get( 'memory_limit' ) ) : 0; return $v > 0 ? $v : 0; } }