WPBP / WordPress-Plugin-Boilerplate-Powered

Wordpress Plugin Boilerplate but Powered with examples and a generator!
https://wpbp.github.io/
GNU General Public License v3.0
782 stars 116 forks source link

[BUG] ($network_wide) must be of type bool, null given #223

Closed MikeiLL closed 1 year ago

MikeiLL commented 1 year ago

Hey, amigos. Happy holidays.

To Reproduce Steps to reproduce the behavior:

  1. Start env in wp-env using php8.1
  2. Errors out at public static function activate( bool $network_wide ) in

Expected behavior Successful deployment of dev env.

Additional context

Fatal error: Uncaught TypeError: My_Plugin\Backend\ActDeact::activate(): Argument #1 ($network_wide) must be of type bool, null given, called in /var/www/html/wp-includes/class-wp-hook.php on line 308 and defined in /var/www/html/wp-content/plugins/plugin-name/backend/ActDeact.php:62
Stack trace

I removed the bool type declaration from the function signature.

Mte90 commented 1 year ago

If you see there isn't in the boilerplate https://github.com/WPBP/WordPress-Plugin-Boilerplate-Powered/blob/master/plugin-name/backend/ActDeact.php#L62 Probably you have a codesniffer that insert it.

MikeiLL commented 1 year ago

This is what was generated by the generator:

/**
 * Activate and deactive method of the plugin and relates.
 */
class ActDeact extends Base {

    /**
     * Initialize the class.
     *
     * @return void|bool
     */
    public function initialize() {
        if ( !parent::initialize() ) {
            return;
        }

        // Activate plugin when new blog is added
        \add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );

        \add_action( 'admin_init', array( $this, 'upgrade_procedure' ) );
    }

    /**
     * Fired when a new site is activated with a WPMU environment.
     *
     * @param int $blog_id ID of the new blog.
     * @since 1.0.0
     * @return void
     */
    public function activate_new_site( int $blog_id ) {
        if ( 1 !== \did_action( 'wpmu_new_blog' ) ) {
            return;
        }

        \switch_to_blog( $blog_id );
        self::single_activate();
        \restore_current_blog();
    }

    /**
     * Fired when the plugin is activated.
     *
     * @param bool $network_wide True if active in a multiste, false if classic site.
     * @since 1.0.0
     * @return void
     */
    public static function activate( $network_wide ) {
        if ( \function_exists( 'is_multisite' ) && \is_multisite() ) {
            if ( $network_wide ) {
                // Get all blog ids
                /** @var array<\WP_Site> $blogs */
                $blogs = \get_sites();

                foreach ( $blogs as $blog ) {
                    \switch_to_blog( (int) $blog->blog_id );
                    self::single_activate();
                    \restore_current_blog();
                }

                return;
            }
        }

        self::single_activate();
    }

    /**
     * Fired when the plugin is deactivated.
     *
     * @param bool $network_wide True if WPMU superadmin uses
     * "Network Deactivate" action, false if
     * WPMU is disabled or plugin is
     * deactivated on an individual blog.
     * @since 1.0.0
     * @return void
     */
    public static function deactivate( bool $network_wide ) {
        if ( \function_exists( 'is_multisite' ) && \is_multisite() ) {
            if ( $network_wide ) {
                // Get all blog ids
                /** @var array<\WP_Site> $blogs */
                $blogs = \get_sites();

                foreach ( $blogs as $blog ) {
                    \switch_to_blog( (int) $blog->blog_id );
                    self::single_deactivate();
                    \restore_current_blog();
                }

                return;
            }
        }

        self::single_deactivate();
    }

    /**
     * Add admin capabilities
     *
     * @return void
     */
    public static function add_capabilities() {
        // Add the capabilites to all the roles
        $caps  = array(
            'create_plugins',
            'read_demo',
            'read_private_demoes',
            'edit_demo',
            'edit_demoes',
            'edit_private_demoes',
            'edit_published_demoes',
            'edit_others_demoes',
            'publish_demoes',
            'delete_demo',
            'delete_demoes',
            'delete_private_demoes',
            'delete_published_demoes',
            'delete_others_demoes',
            'manage_demoes',
        );
        $roles = array(
            \get_role( 'administrator' ),
            \get_role( 'editor' ),
            \get_role( 'author' ),
            \get_role( 'contributor' ),
            \get_role( 'subscriber' ),
        );

        foreach ( $roles as $role ) {
            foreach ( $caps as $cap ) {
                if ( \is_null( $role ) ) {
                    continue;
                }

                $role->add_cap( $cap );
            }
        }
    }

    /**
     * Remove capabilities to specific roles
     *
     * @return void
     */
    public static function remove_capabilities() {
        // Remove capabilities to specific roles
        $bad_caps = array(
            'create_demoes',
            'read_private_demoes',
            'edit_demo',
            'edit_demoes',
            'edit_private_demoes',
            'edit_published_demoes',
            'edit_others_demoes',
            'publish_demoes',
            'delete_demo',
            'delete_demoes',
            'delete_private_demoes',
            'delete_published_demoes',
            'delete_others_demoes',
            'manage_demoes',
        );
        $roles    = array(
            \get_role( 'author' ),
            \get_role( 'contributor' ),
            \get_role( 'subscriber' ),
        );

        foreach ( $roles as $role ) {
            foreach ( $bad_caps as $cap ) {
                if ( \is_null( $role ) ) {
                    continue;
                }

                $role->remove_cap( $cap );
            }
        }
    }

    /**
     * Upgrade procedure
     *
     * @return void
     */
    public static function upgrade_procedure() {
        if ( !\is_admin() ) {
            return;
        }

        $version = \strval( \get_option( 'blinkwellness-mindbody-version' ) );

        if ( !\version_compare( BWM_VERSION, $version, '>' ) ) {
            return;
        }

        \update_option( 'blinkwellness-mindbody-version', BWM_VERSION );
        \delete_option( BWM_TEXTDOMAIN . '_fake-meta' );
    }

    /**
     * Fired for each blog when the plugin is activated.
     *
     * @since 1.0.0
     * @return void
     */
    private static function single_activate() {
        self::add_capabilities();
        self::upgrade_procedure();
        // Clear the permalinks
        \flush_rewrite_rules();
    }

    /**
     * Fired for each blog when the plugin is deactivated.
     *
     * @since 1.0.0
     * @return void
     */
    private static function single_deactivate() {
        self::remove_capabilities();
        // Clear the permalinks
        \flush_rewrite_rules();
    }

}
MikeiLL commented 1 year ago

May have removed a couple of @TODOs so that the codesniffer would stop complaining.

Mte90 commented 1 year ago

So the $network_wide doesn't have any type hint by the generator but something else generated the issue you faced.

MikeiLL commented 1 year ago

Ah. Thank you. I think one of the linters had asked if I wanted it to fix some errors. Nice.