| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Code_Snippets\Core; |
| 4 | 4 | |
| 5 | +use function Code_Snippets\Utils\validate_network_param; | |
| 5 | 6 | use const Code_Snippets\CACHE_GROUP; |
| 6 | 7 | |
| 7 | 8 | /** |
| 8 | 9 | * Functions used to manage the database tables. |
| @@ -61,30 +62,8 @@ | ||
| 61 | 62 | $wpdb->ms_global_tables[] = self::MS_TABLE_NAME; |
| 62 | 63 | } |
| 63 | 64 | |
| 64 | 65 | /** |
| 65 | - * Validate a provided 'network' or 'multisite' param, converting it to a boolean. | |
| 66 | - * | |
| 67 | - * @param bool|null $network Network argument value. | |
| 68 | - * | |
| 69 | - * @return bool Sanitized value. | |
| 70 | - */ | |
| 71 | - public static function validate_network_param( ?bool $network = null ): bool { | |
| 72 | - | |
| 73 | - // If multisite is not active, then assume the value is false. | |
| 74 | - if ( ! is_multisite() ) { | |
| 75 | - return false; | |
| 76 | - } | |
| 77 | - | |
| 78 | - // If $multisite is null, try to base it on the current admin page. | |
| 79 | - if ( is_null( $network ) && function_exists( 'is_network_admin' ) ) { | |
| 80 | - return is_network_admin(); | |
| 81 | - } | |
| 82 | - | |
| 83 | - return (bool) $network; | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | 66 | * Return the appropriate snippet table name |
| 88 | 67 | * |
| 89 | 68 | * @param bool|null $is_network Whether retrieve the multisite table name (true) or the site table name (false). |
| 90 | 69 | * |
| @@ -91,9 +70,9 @@ | ||
| 91 | 70 | * @return string The snippet table name |
| 92 | 71 | * @since 2.0 |
| 93 | 72 | */ |
| 94 | 73 | public function get_table_name( ?bool $is_network = null ): string { |
| 95 | - $is_network = is_bool( $is_network ) ? $is_network : self::validate_network_param( $is_network ); | |
| 74 | + $is_network = is_bool( $is_network ) ? $is_network : validate_network_param( $is_network ); | |
| 96 | 75 | return $is_network ? $this->ms_table : $this->table; |
| 97 | 76 | } |
| 98 | 77 | |
| 99 | 78 | /** |
| @@ -165,11 +144,15 @@ | ||
| 165 | 144 | active TINYINT(1) NOT NULL DEFAULT 0, |
| 166 | 145 | modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 167 | 146 | revision BIGINT(20) NOT NULL DEFAULT 1, |
| 168 | 147 | cloud_id VARCHAR(255) NULL, |
| 148 | + created_by BIGINT(20) UNSIGNED NULL, | |
| 149 | + updated_by BIGINT(20) UNSIGNED NULL, | |
| 169 | 150 | PRIMARY KEY (id), |
| 170 | 151 | KEY scope (scope), |
| 171 | - KEY active (active) | |
| 152 | + KEY active (active), | |
| 153 | + KEY created_by (created_by), | |
| 154 | + KEY updated_by (updated_by) | |
| 172 | 155 | ) $charset_collate;"; |
| 173 | 156 | |
| 174 | 157 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 175 | 158 | dbDelta( $sql ); |
| @@ -182,22 +165,23 @@ | ||
| 182 | 165 | |
| 183 | 166 | return $success; |
| 184 | 167 | } |
| 185 | 168 | |
| 186 | - /** | |
| 187 | - * Generate the SQL for fetching active snippets from the database. | |
| 188 | - * | |
| 189 | - * @param string[] $scopes List of scopes to retrieve in. | |
| 190 | - * | |
| 191 | - * @return array{ | |
| 192 | - * id: int, | |
| 193 | - * code: string, | |
| 194 | - * scope: string, | |
| 195 | - * table: string, | |
| 196 | - * network: bool, | |
| 197 | - * priority: int, | |
| 198 | - * } List of active snippets. | |
| 199 | - */ | |
| 169 | + /** | |
| 170 | + * Generate the SQL for fetching active snippets from the database. | |
| 171 | + * | |
| 172 | + * @param string[] $scopes List of scopes to retrieve in. | |
| 173 | + * | |
| 174 | + * @return array{ | |
| 175 | + * id: int, | |
| 176 | + * code: string, | |
| 177 | + * scope: string, | |
| 178 | + * table: string, | |
| 179 | + * network: bool, | |
| 180 | + * priority: int, | |
| 181 | + * condition_id: int, | |
| 182 | + * }[] List of active snippets. | |
| 183 | + */ | |
| 200 | 184 | public function fetch_active_snippets( array $scopes ): array { |
| 201 | 185 | $active_snippets = []; |
| 202 | 186 | |
| 203 | 187 | // Fetch the active snippets for the current site, if there are any. |
| @@ -317,9 +301,9 @@ | ||
| 317 | 301 | * @param bool $active_only Whether to only fetch active snippets from the table. |
| 318 | 302 | * |
| 319 | 303 | * @return array<string, array<string, mixed>>|false List of active snippets, if any could be retrieved. |
| 320 | 304 | */ |
| 321 | - private static function fetch_snippets_from_table( string $table_name, array $scopes, bool $active_only = true ) { | |
| 305 | + private static function fetch_snippets_from_table( string $table_name, array $scopes, bool $active_only ) { | |
| 322 | 306 | global $wpdb; |
| 323 | 307 | |
| 324 | 308 | $cache_key = sprintf( 'active_snippets_%s_%s', sanitize_key( join( '_', $scopes ) ), $table_name ); |
| 325 | 309 | $cached_snippets = wp_cache_get( $cache_key, CACHE_GROUP ); |
| @@ -332,9 +316,11 @@ | ||
| 332 | 316 | return false; |
| 333 | 317 | } |
| 334 | 318 | |
| 335 | 319 | $scopes_format = implode( ',', array_fill( 0, count( $scopes ), '%s' ) ); |
| 336 | - $extra_where = $active_only ? 'AND active=1' : ''; | |
| 320 | + $extra_where = $active_only | |
| 321 | + ? 'AND active=1' | |
| 322 | + : 'AND active <> -1'; | |
| 337 | 323 | |
| 338 | 324 | $snippets = $wpdb->get_results( |
| 339 | 325 | // phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 340 | 326 | $wpdb->prepare( |