WooCommerce E-Check支付网关在站点切换到多站点时消失

时间:2019-07-19 作者:Andrew S

由于某种原因,当我激活多站点时,电子支票支付选项不再显示在WooCommerce>Settings>Payments面板中。

<?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 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

这是因为:

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\' ) );
}
如果此条件不成立,则整个类不会执行任何操作:

in_array( \'woocommerce/woocommerce.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ), true )
问题是,如果插件被网络激活,那么它就不会存储在get_option( \'active_plugins\' ).

要检查插件是否被网络激活,还需要检查get_network_option( \'active_sitewide_plugins\' ) (请注意,“站点”范围是旧术语,表示网络):

$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 ) ) {

}
或者,更好的是,甚至不用麻烦。此类仅在以下情况下实例化class_exists( \'WooCommerce\' ) 退货true:

if ( ! class_exists( \'WooCommerce\' ) ) {
    // etc.
} else { //else WooCommerce class exists
    return Woocommerce_Gateway_Green_Money::instance();
}
因此,检查WooCommerce是否在instance() 方法是完全冗余的。

然而,在你做任何事情之前,如果你不是这个插件的开发人员,那么你需要让他们修复这个插件。如果您自己做了更改,而插件收到的更新没有解决问题,那么插件就会再次崩溃。

不过,我过去使用过的权宜之计是利用active_plugins 过滤器强制修复现有条件:

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;
} );
该代码将确保WooCommerce是否被激活,这是通过检查是否存在WC() 函数,然后active_plugins 即使插件被网络激活,check也将返回true。不过,这可以说是一种黑客行为,因此正确修复插件应该是第一选择。

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。