1

For some reason when I activated multi-site, the e-check payment option no longer shows in the WooCommerce > Settings > Payments panel.

<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
/**
 * @package GreenPay™ by Green Payment Processing
 * @version 1.2.13
 */
/**
  * Plugin Name: GreenPay™ by Green Payment Processing
  * Description: GreenPay™ gateway for WooCommerce
  * Author: Green Payment Processing
  * Contributors: greenmoney
  * Version: 1.2.13
  * Author URI: http://www.green.money/
  * Copyright: © 2019 Green Payment Processing
    *
  * Tested up to: 5.2.2
  * WC requires at least: 3.0.0
  * WC tested up to: 3.6.5
    *Copyright © 2019 Green Payment Processing
    *
  * License: Modified MIT license
    *
    *Permission is hereby granted, free of charge, to any person obtaining a copy
    *of this plugin software and associated documentation files (the "Software"),
    *to deal in the Software, including without limitation the rights to use, copy,
    *modify, merge, publish, and distribute the Software, and to permit persons to
    *whom the Software is furnished to do so, subject to the following conditions:
    *
    *The above copyright notice and this permission notice shall be included in all
    *copies or substantial portions of the Software.
    *
    *Distributed copies of the Software either in whole or in part must be provided
    *free of charge to the persons to whom the Software is furnished.
    *
    *It is wholly understood that the term "Software" applies solely to this code and
    *associated documentation files and not to any other property or softwares owned
    *by the copyright holders, including but not limited to any software this code may
    *interface with as a necessary component.
    *
    *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    *SOFTWARE.
  */

if ( ! class_exists( 'Woocommerce_Gateway_Green_Money' ) ) {
    include_once('includes/gmpg_extra_functions.php');
    include_once('includes/gmpg_settings.php');
    /**
     * class:   Woocommerce_Gateway_Green_Money
     * desc:    plugin class to Woocommerce Gateway GreenPay™ by Green Payment Processing
     */
    class Woocommerce_Gateway_Green_Money {

        private static $instance;

        public static function instance() {
            if ( ! self::$instance ) {
                self::$instance = new Woocommerce_Gateway_Green_Money();
                if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
                    self::$instance->setup_constants();
                    self::$instance->hooks();
                    self::$instance->includes();
                    self::$instance->load_textdomain();

                    add_filter( 'woocommerce_payment_gateways', array( self::$instance, 'add_wc_gateway' ) );
                }
            }
            return self::$instance;
        }

        private function setup_constants() {
            // Plugin path
            define( 'WOO_GM_DIR', plugin_dir_path( __FILE__ ) );

            // Plugin URL
            define( 'WOO_GM_URL', plugin_dir_url( __FILE__ ) );
        }

        private function hooks() {
            register_activation_hook( __FILE__, array( 'Woocommerce_Gateway_Green_Money', 'activate' ) );
            register_deactivation_hook( __FILE__, array( 'Woocommerce_Gateway_Green_Money', 'deactivate' ) );
        }

        private function includes() {
            require_once WOO_GM_DIR . 'includes/gateway.php';
        }

        /**
         * Add the gateway to WooCommerce.
         *
         * @access public
         * @param  array  $methods
         * @return array
         */
        function add_wc_gateway( $methods ) {

            $methods[] = 'WC_Gateway_Green_Money';
            return $methods;
        }

        public function load_textdomain() {
            // Set filter for language directory
            $lang_dir    = WOO_GM_DIR . '/languages/';
            $lang_dir    = apply_filters( 'woo_gateway_green_money_lang_dir', $lang_dir );

            // Traditional WordPress plugin locale filter
            $locale  = apply_filters( 'plugin_locale', get_locale(), '' );
            $mofile  = sprintf( '%1$s-%2$s.mo', 'woocommerce-gateway-green-money', $locale );

            // Setup paths to current locale file
            $mofile_local    = $lang_dir . $mofile;
            $mofile_global   = WP_LANG_DIR . '/woocommerce-gateway-green-money/' . $mofile;

            if ( file_exists( $mofile_global ) ) {
                // Look in global /wp-content/languages/woocommerce-gateway-green-money/ folder
                load_textdomain( 'woocommerce-gateway-green-money', $mofile_global );
            } elseif ( file_exists( $mofile_local ) ) {
                // Look in local /wp-content/plugins/woocommerce-gateway-green-money/languages/ folder
                load_textdomain( 'woocommerce-gateway-green-money', $mofile_local );
            } else {
                // Load the default language files
                load_plugin_textdomain( 'woocommerce-gateway-green-money', false, $lang_dir );
            }
        }// END public function __construct()

         public static function activate() {
             flush_rewrite_rules();
         }

         public static function deactivate() {
             flush_rewrite_rules();
         }

    }// END class Woocommerce_Gateway_Green_Money

}// END if(!class_exists("Woocommerce_Gateway_Green_Money"))

function woocommerce_gateway_green_money_load() {
    if ( ! class_exists( 'WooCommerce' ) ) {

        require_once( ABSPATH . 'wp-admin/includes/plugin.php' );

        $plugins = get_plugins();

        foreach ( $plugins as $plugin_path => $plugin ) {
            if ( 'WooCommerce' === $plugin['Name'] ) {
                define( 'HAS_WOO', true );
                break;
            }
        }
        add_action( 'admin_notices', 'woocommerce_gateway_green_money_notice' );
    } else { //else WooCommerce class exists
        return Woocommerce_Gateway_Green_Money::instance();
    }
}
add_action( 'plugins_loaded', 'woocommerce_gateway_green_money_load' );

function woocommerce_gateway_green_money_notice() {
    if ( HAS_WOO ) {
        echo '<div class="error"><p>' . wp_kses( __( 'GreenPay™ by Green Payment Processing add-on requires WooCommerce! Please activate it to continue!', 'woocommerce-gateway-green-money' ), $allowed_html_array ) . '</p></div>';
    } else {
        echo '<div class="error"><p>' . wp_kses( __( 'GreenPay™ by Green Payment Processing add-on requires WooCommerce! Please install it to continue!', 'woocommerce-gateway-green-money' ), $allowed_html_array ) . '</p></div>';
    }
}

1 Answer 1

2

It's because of this:

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
    self::$instance->setup_constants();
    self::$instance->hooks();
    self::$instance->includes();
    self::$instance->load_textdomain();

    add_filter( 'woocommerce_payment_gateways', array( self::$instance, 'add_wc_gateway' ) );
}

The entire class doesn't do anything if this condition isn't true:

in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true )

The problem is that this if a plugin is network activated then it's not stored in get_option( 'active_plugins' ).

To check if a plugin is network activated you need to also check get_network_option( 'active_sitewide_plugins' ) (note that "site" wide is old terminology and means network):

$site_plugins    = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
$network_plugins = get_network_option( 'active_sitewide_plugins' );

if ( in_array( 'woocommerce/woocommerce.php', $site_plugins ) || in_array( 'woocommerce/woocommerce.php', $network_plugins ) ) {

}

Or, better yet, don't even bother. This class is only instantiated if class_exists( 'WooCommerce' ) returns true:

if ( ! class_exists( 'WooCommerce' ) ) {
    // etc.
} else { //else WooCommerce class exists
    return Woocommerce_Gateway_Green_Money::instance();
}

So checking whether WooCommerce is active inside the instance() method is entirely redundant.

Before you do anything however, if you are not the developer of this plugin then you need to ask them to fix this. If you make changes yourself and the plugin receives an update that doesn't fix the issue, it's just going to break again.

A stop-gap solution though, that I've used in the past, is to take advantage of the active_plugins filter to force fix the existing condition:

add_filter( 'active_plugins', function( $active_plugins ) {
    if ( function_exists( 'WC' ) && ! in_array( 'woocommerce/woocommerce.php', $active_plugins ) ) {
        $active_plugins[] = 'woocommerce/woocommerce.php';
    }

    return $active_plugins;
} );

That code will ensure that if WooCommerce is activated, which is determined by checking for the existence of the WC() function, then the active_plugins check will return true even if the plugin is network activated. It's arguably a hack though, so fixing the plugin properly should be the first option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.