option WordPress uses. Never edits widget_logic keys. */ final class Widget_Store { const KEY = 'gtfg_rule'; /** * Per-request cache of unserialized widget options (get_option() unserializes * the whole option on every call). Cleared on any write to an option and on * switch_blog; not used in the Customizer preview, whose values are filtered. * * @var array */ private static $cache = array(); /** * Clear the cache (all, or one option). * * @param string|null $option Option name. */ public static function flush_cache( $option = null ) { if ( is_string( $option ) ) { unset( self::$cache[ $option ] ); } else { self::$cache = array(); } } /** * Read a widget option through the cache. * * @param string $name Option name. * @return mixed */ private static function option( $name ) { if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) { return get_option( $name ); } if ( ! array_key_exists( $name, self::$cache ) ) { self::$cache[ $name ] = get_option( $name ); } return self::$cache[ $name ]; } /** * Locate a widget's option and instance key. * * @param string $widget_id Widget ID. * @return array{option:string, number:int|null}|null */ public static function locate( $widget_id ) { if ( ! is_string( $widget_id ) || ! preg_match( '/^[A-Za-z0-9_\-]+$/', $widget_id ) ) { return null; } if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $m ) ) { return array( 'option' => 'widget_' . $m[1], 'number' => (int) $m[2], ); } return array( 'option' => 'widget_' . $widget_id, 'number' => null, ); } /** * Read one instance. * * @param string $widget_id Widget ID. * @return array|null */ public static function instance( $widget_id ) { $loc = self::locate( $widget_id ); if ( null === $loc ) { return null; } $opt = self::option( $loc['option'] ); if ( ! is_array( $opt ) ) { return null; } if ( null === $loc['number'] ) { return $opt; } return isset( $opt[ $loc['number'] ] ) && is_array( $opt[ $loc['number'] ] ) ? $opt[ $loc['number'] ] : null; } /** * Read the stored rule of a widget. * * @param string $widget_id Widget ID. * @return mixed Raw rule or null. */ public static function rule( $widget_id ) { $inst = self::instance( $widget_id ); return ( is_array( $inst ) && array_key_exists( self::KEY, $inst ) ) ? $inst[ self::KEY ] : null; } /** * Write the rule of one widget. Re-reads the option immediately before * writing and writes only that option. * * @param string $widget_id Widget ID. * @param array|null $rule Rule array, or null to remove our key. * @return bool */ public static function write_rule( $widget_id, $rule ) { $loc = self::locate( $widget_id ); if ( null === $loc ) { return false; } wp_cache_delete( $loc['option'], 'options' ); $opt = get_option( $loc['option'] ); if ( ! is_array( $opt ) ) { return false; } if ( null === $loc['number'] ) { $inst = $opt; } elseif ( isset( $opt[ $loc['number'] ] ) && is_array( $opt[ $loc['number'] ] ) ) { $inst = $opt[ $loc['number'] ]; } else { return false; } if ( null === $rule ) { unset( $inst[ self::KEY ] ); } else { $inst[ self::KEY ] = $rule; } if ( null === $loc['number'] ) { $opt = $inst; } else { $opt[ $loc['number'] ] = $inst; } update_option( $loc['option'], $opt ); self::flush_cache( $loc['option'] ); if ( is_array( $rule ) && isset( $rule['ext']['gateforge']['saved_hash'] ) ) { Saved_Rules::queue( $rule, 'widget', (string) $widget_id ); } else { Saved_Rules::widgets_changed(); } return true; } /** * Every widget instance that carries a gtfg_rule key, widget ID => raw rule. * Reads option names with one query and values through the option API. * * @return array */ public static function all_rules() { global $wpdb; $names = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s ORDER BY option_name", $wpdb->esc_like( 'widget_' ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- names only; values are read through get_option(). $out = array(); foreach ( is_array( $names ) ? $names : array() as $name ) { if ( 0 === strpos( $name, 'widget_logic' ) ) { continue; } $value = get_option( $name ); if ( ! is_array( $value ) ) { continue; } $id_base = substr( $name, 7 ); if ( array_key_exists( self::KEY, $value ) ) { $out[ $id_base ] = $value[ self::KEY ]; continue; } foreach ( $value as $number => $inst ) { if ( is_int( $number ) && is_array( $inst ) && array_key_exists( self::KEY, $inst ) ) { $out[ $id_base . '-' . $number ] = $inst[ self::KEY ]; } } } return $out; } }