Apache' ) || str_contains( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) ); /** * Whether the server software is Nginx or something else * * @global bool $is_nginx */ $is_nginx = ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) ); /** * Whether the server software is IIS or something else * * @global bool $is_IIS */ $is_IIS = ! $is_apache && ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) || str_contains( $_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer' ) ); /** * Whether the server software is IIS 7.X or greater * * @global bool $is_iis7 */ $is_iis7 = $is_IIS && (int) substr( $_SERVER['SERVER_SOFTWARE'], strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/' ) + 14 ) >= 7; /** * Test if the current browser runs on a mobile device (smart phone, tablet, etc.) * * @since 3.4.0 * * @return bool */ function wp_is_mobile() { if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { $is_mobile = false; } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) // Many mobile devices (all iPhone, iPad, etc.) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' ) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Silk/' ) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Kindle' ) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' ) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' ) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) ) { $is_mobile = true; } else { $is_mobile = false; } /** * Filters whether the request should be treated as coming from a mobile device or not. * * @since 4.9.0 * * @param bool $is_mobile Whether the request is from a mobile device or not. */ return apply_filters( 'wp_is_mobile', $is_mobile ); } lta() to apply the SQL code */ public static function create_table( $table_name ) { 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', priority SMALLINT NOT NULL DEFAULT 10, active TINYINT(1) NOT NULL DEFAULT 0, modified DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', 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; } /** * Build a list of formatting placeholders for an array of data. * * @param int $count Length of data. * @param string $placeholder Placeholder to use. Defaults to string placeholder. * * @return string List of placeholders, ready for inclusion in query. */ private static function build_format_list( $count, $placeholder = '%s' ) { return implode( ',', array_fill( 0, $count, $placeholder ) ); } /** * 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 array $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( $table_name, array $scopes, $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 = self::build_format_list( count( $scopes ) ); $extra_where = $active_only ? 'AND active=1' : ''; $snippets = $wpdb->get_results( $wpdb->prepare( " SELECT id, code, scope, active FROM $table_name WHERE scope IN ($scopes_format) $extra_where ORDER BY priority, id", $scopes ), 'ARRAY_A' ); // db call ok. // Cache the full list of snippets. if ( is_array( $snippets ) ) { wp_cache_set( $cache_key, $snippets, CACHE_GROUP ); return $snippets; } return false; } /** * Generate the SQL for fetching active snippets from the database. * * @param array|string $scopes List of scopes to retrieve in. * * @return array List of active snippets, indexed by table. */ public function fetch_active_snippets( $scopes ) { $active_snippets = array(); // Ensure that the list of scopes is an array. if ( ! is_array( $scopes ) ) { $scopes = array( $scopes ); } // Fetch the active snippets for the current site, if there are any. $snippets = $this->fetch_snippets_from_table( $this->table, $scopes ); if ( $snippets ) { $active_snippets[ $this->table ] = $snippets; } // If multisite is enabled, fetch all snippets from the network table, and filter down to only active snippets. if ( is_multisite() ) { $active_shared_ids = (array) get_option( 'active_shared_network_snippets', array() ); $ms_snippets = $this->fetch_snippets_from_table( $this->ms_table, $scopes, false ); if ( $ms_snippets ) { $active_snippets[ $this->ms_table ] = array_filter( $ms_snippets, function ( $snippet ) use ( $active_shared_ids ) { return $snippet['active'] || in_array( intval( $snippet['id'] ), $active_shared_ids, true ); } ); } } return $active_snippets; } }