codesnippetspro / code-snippets

Code Snippets WordPress Plugin
https://codesnippets.pro
Other
324 stars 106 forks source link

Individual site activation not enabled with multi-site #192

Open AmibeWebsites opened 1 month ago

AmibeWebsites commented 1 month ago

Prerequisites

Type of Bug

A Code Snippet

Description

In a multi-site, the option to 'Allow this snippet to be activated on individual sites on the network' does not save (and automatically disables the snippet on any individual site where it was activated for that site).

What I would expect is that the snippet would be available for activation on individual sites in that network, and that if it had previously already been active prior to saving the snippet, that it would remain active.

What happens is that the snippet is not made available to be activated on individual sites in the network, and if it had been activated for any individual site, it is now deactivated.

My work-around is to directly edit the 'shared_network_snippets' meta_key in the database for the network. And then I go and re-enable for all individual sites that use it.

Steps to reproduce

Steps to reproduce, in a multi-site setup, with multiple networks:

  1. Add new or edit an existing snippet
  2. Tick the option 'Allow this snippet to be activated on individual sites on the network'
  3. Save changes
  4. Refresh the page to see that the option is no longer ticked

Isolating the problem

Comment

My actual environment is a multi-network setup, in other words, many multi-sites.

There is a slight complexity in that while the snippet is the same across all networks (in other words... if it gets edited through one network all networks reflect the same change), the option to 'Allow this snippet to be activated on individual sites on the network' can however be different per network.

I did checked with multi-network functionality turned off and the issue still occurred, so I'm guessing it's a multi-site issue, rather than being unique to a multi-network setup.
llicour commented 2 weeks ago

same issue for me (v3.6.5) I try some patchs that seems to work, waiting for an official bugfix (not sure it cover all cases, but for basic snippet edition), seems ok

patch php/snippet-ops.php

--- a/wp-content/plugins/code-snippets-pro/php/snippet-ops.php
+++ b/wp-content/plugins/code-snippets-pro/php/snippet-ops.php
@@ -230,11 +230,11 @@ function update_shared_network_snippets( array $snippets ): bool {

        foreach ( $snippets as $snippet ) {
                if ( $snippet->shared_network ) {
-                       if ( $snippet->active ) {
+                       //if ( $snippet->active ) {
                                $shared_ids[] = $snippet->id;
                        } else {
                                $unshared_ids[] = $snippet->id;
-                       }
+                       //}
                }
        }

add new filter

add_filter("code_snippets/get_snippet", function($snippet, $id, $network) {
    if ($network) {
        $shared_network_snippets = get_site_option( 'shared_network_snippets', array() );
        $snippet->shared_network = in_array( $snippet->fields['id'], $shared_network_snippets, true );
    }
    return $snippet;
}, 10, 3);