ble( string $table_name ): bool { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); /* Create the database table */ $sql = "CREATE TABLE $table_name ( id BIGINT(20) NOT NULL AUTO_INCREMENT, name TINYTEXT NOT NULL, description TEXT NOT NULL, code LONGTEXT NOT NULL, tags LONGTEXT NOT NULL, scope VARCHAR(15) NOT NULL DEFAULT 'global', condition_id BIGINT(20) NOT NULL DEFAULT 0, priority SMALLINT NOT NULL DEFAULT 10, active TINYINT(1) NOT NULL DEFAULT 0, modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, revision BIGINT(20) NOT NULL DEFAULT 1, cloud_id VARCHAR(255) NULL, PRIMARY KEY (id), KEY scope (scope), KEY active (active) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); $success = empty( $wpdb->last_error ); if ( $success ) { do_action( 'code_snippets/create_table', $table_name ); } return $success; } /** * Generate the SQL for fetching active snippets from the database. * * @param string[] $scopes List of scopes to retrieve in. * * @return array{ * id: int, * code: string, * scope: string, * table: string, * network: bool, * priority: int, * } List of active snippets. */ public function fetch_active_snippets( array $scopes ): array { $active_snippets = []; // Fetch the active snippets for the current site, if there are any. $snippets = $this->fetch_snippets_from_table( $this->table, $scopes, true ); if ( $snippets ) { foreach ( $snippets as $snippet ) { $active_snippets[] = [ 'id' => intval( $snippet['id'] ), 'code' => $snippet['code'], 'scope' => $snippet['scope'], 'table' => $this->table, 'network' => false, 'priority' => intval( $snippet['priority'] ), ]; } } // If multisite is enabled, fetch all snippets from the network table, and filter down to only active snippets. if ( is_multisite() ) { $ms_snippets = $this->fetch_snippets_from_table( $this->ms_table, $scopes, false ); if ( $ms_snippets ) { $active_shared_ids = get_option( 'active_shared_network_snippets', [] ); $active_shared_ids = is_array( $active_shared_ids ) ? array_map( 'intval', $active_shared_ids ) : []; foreach ( $ms_snippets as $snippet ) { $id = intval( $snippet['id'] ); $active_value = intval( $snippet['active'] ); if ( ! self::is_network_snippet_enabled( $active_value, $id, $active_shared_ids ) ) { continue; } $active_snippets[] = [ 'id' => $id, 'code' => $snippet['code'], 'scope' => $snippet['scope'], 'table' => $this->ms_table, 'network' => true, 'priority' => intval( $snippet['priority'] ), ]; } $this->sort_active_snippets( $active_snippets ); } } return $active_snippets; } /** * Determine whether a network snippet should execute on the current site. * * Network snippets execute when active=1, or when the snippet is listed as active-shared for the site. * Trashed snippets (active=-1) should never execute. * * @param int $active_value Raw active value: 1=active, 0=inactive, -1=trashed (can be stored as a string in the database). * @param int $snippet_id Snippet ID. * @param int[] $active_shared_ids Active shared network snippet IDs for the current site. * * @return bool */ public static function is_network_snippet_enabled( int $active_value, int $snippet_id, array $active_shared_ids ): bool { if ( -1 === $active_value ) { return false; } if ( 1 === $active_value ) { return true; } return in_array( $snippet_id, $active_shared_ids, true ); } /** * Sort the active snippets by priority, table, and ID. * * @param array $active_snippets List of active snippets to sort. */ private function sort_active_snippets( array &$active_snippets ): void { $comparisons = [ function ( array $a, array $b ) { return $a['priority'] <=> $b['priority']; }, function ( array $a, array $b ) { $a_table = $a['table'] === $this->ms_table ? 0 : 1; $b_table = $b['table'] === $this->ms_table ? 0 : 1; return $a_table <=> $b_table; }, function ( array $a, array $b ) { return $a['id'] <=> $b['id']; }, ]; usort( $active_snippets, static function ( $a, $b ) use ( $comparisons ) { foreach ( $comparisons as $comparison ) { $result = $comparison( $a, $b ); if ( 0 !== $result ) { return $result; } } return 0; } ); } /** * Fetch a list of active snippets from a database table. * * @param string $table_name Name of table to fetch snippets from. * @param array $scopes List of scopes to include in query. * @param boolean $active_only Whether to only fetch active snippets from the table. * * @return array>|false List of active snippets, if any could be retrieved. * * @phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare */ private static function fetch_snippets_from_table( string $table_name, array $scopes, bool $active_only = true ) { global $wpdb; $cache_key = sprintf( 'active_snippets_%s_%s', sanitize_key( join( '_', $scopes ) ), $table_name ); $cached_snippets = wp_cache_get( $cache_key, CACHE_GROUP ); if ( is_array( $cached_snippets ) ) { return $cached_snippets; } if ( ! self::table_exists( $table_name ) ) { return false; } $scopes_format = implode( ',', array_fill( 0, count( $scopes ), '%s' ) ); $extra_where = $active_only ? 'AND active=1' : ''; $snippets = $wpdb->get_results( $wpdb->prepare( " SELECT id, code, scope, active, priority FROM $table_name WHERE scope IN ($scopes_format) $extra_where ORDER BY priority, id", $scopes ), 'ARRAY_A' ); // Cache the full list of snippets. if ( is_array( $snippets ) ) { wp_cache_set( $cache_key, $snippets, CACHE_GROUP ); return $snippets; } return false; } }
Fatal error: Uncaught Error: Class "Code_Snippets\DB" not found in /htdocs/wp-content/plugins/code-snippets/php/class-plugin.php:117 Stack trace: #0 /htdocs/wp-content/plugins/code-snippets/php/load.php(83): Code_Snippets\Plugin->load_plugin() #1 /htdocs/wp-content/plugins/code-snippets/code-snippets.php(61): require_once('/htdocs/wp-cont...') #2 /htdocs/wp-settings.php(545): include_once('/htdocs/wp-cont...') #3 /htdocs/wp-config.php(98): require_once('/htdocs/wp-sett...') #4 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #5 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #6 /htdocs/index.php(17): require('/htdocs/wp-blog...') #7 {main} thrown in /htdocs/wp-content/plugins/code-snippets/php/class-plugin.php on line 117