Skip to content
Draft
20 changes: 20 additions & 0 deletions src/css/manage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@
#wpbody-content & .column-name {
white-space: nowrap; /* prevents wrapping of snippet title */
}

td.column-date, th.column-date {
white-space: nowrap;
inline-size: 130px; /* fixed column width */
min-inline-size: 130px;
max-inline-size: 130px;
text-align: right;
padding-inline-start: 8px;
padding-inline-end: 8px;
overflow: hidden;
text-overflow: ellipsis;
}

/* Ensure the name column can shrink/ellipsis instead of pushing the date. */
td.column-name, th.column-name {
max-inline-size: calc(100% - 140px);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}

td.column-description {
Expand Down
54 changes: 30 additions & 24 deletions src/php/admin-menus/class-import-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,36 @@ protected function print_messages() {
echo '</p></div>';
}

if ( isset( $_REQUEST['imported'] ) ) {
echo '<div id="message" class="updated fade"><p>';

$imported = intval( $_REQUEST['imported'] );

if ( 0 === $imported ) {
esc_html_e( 'No snippets were imported.', 'code-snippets' );

} else {
/* translators: %d: amount of snippets imported */
printf(
_n(
'Successfully imported %d snippet.',
'Successfully imported %d snippets.',
$imported,
'code-snippets'
),
'<strong>' . number_format_i18n( $imported ) . '</strong>',
);

printf(
' <a href="%s">%s</a>',
esc_url( code_snippets()->get_menu_url( 'manage' ) ),
esc_html__( 'Have fun!', 'code-snippets' )
if ( isset( $_REQUEST['imported'] ) ) {
echo '<div id="message" class="updated fade"><p>';

$imported = intval( $_REQUEST['imported'] );

if ( 0 === $imported ) {
esc_html_e( 'No snippets were imported.', 'code-snippets' );

} else {
$imported_count = sprintf( '<strong>%s</strong>', esc_html( number_format_i18n( $imported ) ) );
printf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printf seems redundant here. What are we intending to achieve with this change?

'%s',
wp_kses_post(
sprintf(
/* translators: %s: number of snippets imported. */
_n(
'Successfully imported %s snippet.',
'Successfully imported %s snippets.',
$imported,
'code-snippets'
),
$imported_count
)
)
);

printf(
' <a href="%s">%s</a>',
esc_url( code_snippets()->get_menu_url( 'manage' ) ),
esc_html__( 'Have fun!', 'code-snippets' )
);
}

Expand Down
76 changes: 40 additions & 36 deletions src/php/class-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,17 +618,21 @@ public function get_views(): array {
continue 2;
}

$shared_label_template = $this->is_network
? _n_noop(
if ( $this->is_network ) {
/* translators: %s: total number of snippets shared with subsites. */
$shared_label_template = _n_noop(
'Shared with Subsites <span class="count">(%s)</span>',
'Shared with Subsites <span class="count">(%s)</span>',
'code-snippets'
)
: _n_noop(
);
} else {
/* translators: %s: total number of network snippets. */
$shared_label_template = _n_noop(
'Network Snippets <span class="count">(%s)</span>',
'Network Snippets <span class="count">(%s)</span>',
'code-snippets'
);
}

$template = translate_nooped_plural( $shared_label_template, $count, 'code-snippets' );
break;
Expand Down Expand Up @@ -663,16 +667,16 @@ public function get_views(): array {
* @since 2.0
*/
public function get_current_tags() {
global $snippets, $status;
global $code_snippets_snippets, $status;

// If we're not viewing a snippets table, get all used tags instead.
if ( ! isset( $snippets, $status ) ) {
if ( ! isset( $code_snippets_snippets, $status ) ) {
$tags = get_all_snippet_tags();
} else {
$tags = array();

// Merge all tags into a single array.
foreach ( $snippets[ $status ] as $snippet ) {
foreach ( $code_snippets_snippets[ $status ] as $snippet ) {
$tags = array_merge( $snippet->tags, $tags );
}

Expand Down Expand Up @@ -1088,12 +1092,12 @@ public function prepare_items() {
/**
* Global variables.
*
* @var string $status Current status view.
* @var array<string, Snippet[]> $snippets List of snippets for views.
* @var array<string, integer> $totals List of total items for views.
* @var string $s Current search term.
* @var string $status Current status view.
* @var array<string, Snippet[]> $code_snippets_snippets List of snippets for views.
* @var array<string, integer> $totals List of total items for views.
* @var string $s Current search term.
*/
global $status, $snippets, $totals, $s;
global $status, $code_snippets_snippets, $totals, $s;

wp_reset_vars( array( 'orderby', 'order', 's' ) );

Expand All @@ -1107,21 +1111,21 @@ public function prepare_items() {
}

$this->process_requested_actions();
$snippets = array_fill_keys( $this->statuses, array() );
$code_snippets_snippets = array_fill_keys( $this->statuses, array() );

$all_snippets = apply_filters( 'code_snippets/list_table/get_snippets', $this->fetch_shared_network_snippets( get_snippets() ) );

// Separate trashed snippets from the main collection
$snippets['trashed'] = array_filter( $all_snippets, function( $snippet ) {
$code_snippets_snippets['trashed'] = array_filter( $all_snippets, function( $snippet ) {
return $snippet->is_trashed();
});

// Filter out trashed snippets from the 'all' collection
$snippets['all'] = array_filter( $all_snippets, function( $snippet ) {
$code_snippets_snippets['all'] = array_filter( $all_snippets, function( $snippet ) {
return ! $snippet->is_trashed();
});

foreach ( $snippets['all'] as $snippet ) {
foreach ( $code_snippets_snippets['all'] as $snippet ) {
if ( $snippet->active ) {
$this->active_by_condition[ $snippet->condition_id ][] = $snippet;
}
Expand All @@ -1131,58 +1135,58 @@ public function prepare_items() {
$type = sanitize_key( wp_unslash( $_GET['type'] ?? '' ) );

if ( $type && 'all' !== $type ) {
$snippets['all'] = array_filter(
$snippets['all'],
$code_snippets_snippets['all'] = array_filter(
$code_snippets_snippets['all'],
function ( Snippet $snippet ) use ( $type ) {
return $type === $snippet->type;
}
);

// Filter trashed snippets by type
$snippets['trashed'] = array_filter(
$snippets['trashed'],
$code_snippets_snippets['trashed'] = array_filter(
$code_snippets_snippets['trashed'],
function ( Snippet $snippet ) use ( $type ) {
return $type === $snippet->type;
}
);
}

// Add scope tags to all snippets (including trashed).
foreach ( $snippets['all'] as $snippet ) {
foreach ( $code_snippets_snippets['all'] as $snippet ) {
if ( 'global' !== $snippet->scope ) {
$snippet->add_tag( $snippet->scope );
}
}

foreach ( $snippets['trashed'] as $snippet ) {
foreach ( $code_snippets_snippets['trashed'] as $snippet ) {
if ( 'global' !== $snippet->scope ) {
$snippet->add_tag( $snippet->scope );
}
}

// Filter snippets by tag.
if ( ! empty( $_GET['tag'] ) ) {
$snippets['all'] = array_filter( $snippets['all'], array( $this, 'tags_filter_callback' ) );
$snippets['trashed'] = array_filter( $snippets['trashed'], array( $this, 'tags_filter_callback' ) );
$code_snippets_snippets['all'] = array_filter( $code_snippets_snippets['all'], array( $this, 'tags_filter_callback' ) );
$code_snippets_snippets['trashed'] = array_filter( $code_snippets_snippets['trashed'], array( $this, 'tags_filter_callback' ) );
}

// Filter snippets based on search query.
if ( $s ) {
$snippets['all'] = array_filter( $snippets['all'], array( $this, 'search_by_line_callback' ) );
$snippets['trashed'] = array_filter( $snippets['trashed'], array( $this, 'search_by_line_callback' ) );
$code_snippets_snippets['all'] = array_filter( $code_snippets_snippets['all'], array( $this, 'search_by_line_callback' ) );
$code_snippets_snippets['trashed'] = array_filter( $code_snippets_snippets['trashed'], array( $this, 'search_by_line_callback' ) );
}

if ( is_multisite() ) {
$snippets['shared_network'] = array_values(
$code_snippets_snippets['shared_network'] = array_values(
array_filter(
$snippets['all'],
$code_snippets_snippets['all'],
static function ( Snippet $snippet ) {
return $snippet->shared_network;
}
)
);
} else {
$snippets['shared_network'] = array();
$code_snippets_snippets['shared_network'] = array();
}

// Clear recently activated snippets older than a week.
Expand All @@ -1205,20 +1209,20 @@ static function ( Snippet $snippet ) {
*
* @var Snippet $snippet
*/
foreach ( $snippets['all'] as $snippet ) {
foreach ( $code_snippets_snippets['all'] as $snippet ) {
// Skip trashed snippets (they're already in their own section)
if ( $snippet->is_trashed() ) {
continue;
}

if ( $snippet->active || $this->is_condition_active( $snippet ) ) {
$snippets['active'][] = $snippet;
$code_snippets_snippets['active'][] = $snippet;
} else {
$snippets['inactive'][] = $snippet;
$code_snippets_snippets['inactive'][] = $snippet;

// Was the snippet recently deactivated?
if ( isset( $recently_activated[ $snippet->id ] ) ) {
$snippets['recently_activated'][] = $snippet;
$code_snippets_snippets['recently_activated'][] = $snippet;
}
}
}
Expand All @@ -1228,16 +1232,16 @@ static function ( Snippet $snippet ) {
function ( $section_snippets ) {
return count( $section_snippets );
},
$snippets
$code_snippets_snippets
);

// If the current status is empty, default to all.
if ( empty( $snippets[ $status ] ) ) {
if ( empty( $code_snippets_snippets[ $status ] ) ) {
$status = 'all';
}

// Get the current data.
$data = $snippets[ $status ];
$data = $code_snippets_snippets[ $status ];

// Decide how many records per page to show by getting the user's setting in the Screen Options panel.
$sort_by = $this->screen->get_option( 'per_page', 'option' );
Expand Down
10 changes: 5 additions & 5 deletions src/php/cloud/class-cloud-search-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ public function display_rows() {

echo '</a>';
?>
</h3>
<ul class="action-buttons">
<?php echo cloud_lts_build_action_links( $item, 'search' ); ?>
</ul>
</div>
</h3>
<ul class="action-buttons">
<?php echo wp_kses_post( cloud_lts_build_action_links( $item, 'search' ) ); ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will break the thickbox script. Escaping isn't necessary here – if we want to improve compliance, the underlying function really should be refactored to do output itself.

</ul>
</div>
<div class="column-description">
<p><?php echo wp_kses_post( $this->process_description( $item->description ) ); ?></p>
<p class="authors">
Expand Down
2 changes: 2 additions & 0 deletions src/php/front-end/mce-strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use _WP_Editors;

/* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */
/**
* Variable types.
*
Expand Down Expand Up @@ -53,3 +54,4 @@
$strings = [ _WP_Editors::$mce_locale => [ 'code_snippets' => $strings ] ];
/** $strings is used by outer file. @noinspection PhpUnusedLocalVariableInspection */
$strings = 'tinyMCE.addI18n(' . wp_json_encode( $strings ) . ');';
/* phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */
14 changes: 7 additions & 7 deletions src/php/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@
// Since Imposter rewrites namespaces to Code_Snippets\Vendor\*, we need to remove the original PSR-4
// mappings that Composer generates so other plugins can load their own copies of these libraries.
if ( $code_snippets_autoloader instanceof \Composer\Autoload\ClassLoader ) {
$prefixes = $code_snippets_autoloader->getPrefixesPsr4();
$our_prefix = 'Code_Snippets\\Vendor\\';
$code_snippets_prefixes = $code_snippets_autoloader->getPrefixesPsr4();
$code_snippets_vendor_prefix = 'Code_Snippets\\Vendor\\';

foreach ( $prefixes as $namespace => $paths ) {
foreach ( $code_snippets_prefixes as $code_snippets_namespace => $code_snippets_paths ) {
// Remove any non-Code_Snippets namespace that has a corresponding prefixed version
if ( strpos( $namespace, $our_prefix ) === false ) {
$prefixed_namespace = $our_prefix . $namespace;
if ( isset( $prefixes[ $prefixed_namespace ] ) ) {
$code_snippets_autoloader->setPsr4( $namespace, [] );
if ( strpos( $code_snippets_namespace, $code_snippets_vendor_prefix ) === false ) {
$code_snippets_prefixed_namespace = $code_snippets_vendor_prefix . $code_snippets_namespace;
if ( isset( $code_snippets_prefixes[ $code_snippets_prefixed_namespace ] ) ) {
$code_snippets_autoloader->setPsr4( $code_snippets_namespace, [] );
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions src/php/settings/class-version-switch.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,18 @@ public static function handle_version_switch( string $target_version ): array {
return self::create_error_response( $install_result->get_error_message() );
}

if ( $install_result ) {
delete_transient( VERSION_CACHE_KEY );
if ( $install_result ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this introduces some incorrect indentation.

delete_transient( VERSION_CACHE_KEY );

return [
'success' => true,
'message' => sprintf( __( 'Successfully switched to version %s. Please refresh the page to see changes.', 'code-snippets' ), $target_version ),
];
}
return [
'success' => true,
/* translators: %s: the version number that was switched to. */
'message' => sprintf( __( 'Successfully switched to version %s. Please refresh the page to see changes.', 'code-snippets' ), $target_version ),
];
}

return self::handle_installation_failure( $target_version, $validation['download_url'], $install_result );
}
return self::handle_installation_failure( $target_version, $validation['download_url'], $install_result );
}

public static function render_version_switch_field( array $args ): void {
$current_version = self::get_current_version();
Expand Down Expand Up @@ -291,7 +292,7 @@ public static function render_version_switch_field( array $args ): void {

public static function ajax_switch_version(): void {
if ( ! wp_verify_nonce( $_POST['nonce'] ?? '', 'code_snippets_version_switch' ) ) {
wp_die( __( 'Security check failed.', 'code-snippets' ) );
wp_die( esc_html__( 'Security check failed.', 'code-snippets' ) );
}

if ( ! current_user_can( 'update_plugins' ) ) {
Expand Down Expand Up @@ -329,7 +330,7 @@ public static function render_refresh_versions_field( array $args ): void {

public static function ajax_refresh_versions(): void {
if ( ! wp_verify_nonce( $_POST['nonce'] ?? '', 'code_snippets_refresh_versions' ) ) {
wp_die( __( 'Security check failed.', 'code-snippets' ) );
wp_die( esc_html__( 'Security check failed.', 'code-snippets' ) );
}

if ( ! current_user_can( 'manage_options' ) ) {
Expand Down
Loading
Loading