Skip to content

Instantly share code, notes, and snippets.

@deltafactory
Created May 22, 2015 16:31
Show Gist options
  • Save deltafactory/693e3de1dcf0efe2c19d to your computer and use it in GitHub Desktop.
Save deltafactory/693e3de1dcf0efe2c19d to your computer and use it in GitHub Desktop.
Untested proof of concept for a refactored Nexus WooCommerce Bridge plugin main file.
<?php
/*
Plugin Name: Nexus WooCommerce bridge
Version: 1.1.0
Plugin URI: http://nexusthemes.com
Description: Add widgets/plugins and other functionality from WooCommerce into Nexus Framework
Author: Gert-Jan Bark, Jeff Brand
Author URI: http://nexusthemes.com
*/
NXS_WooCommerce::setup();
class NXS_WooCommerce {
static function setup() {
add_action( 'init', array( __CLASS__, 'init' ) );
add_action( 'admin_notices', array( __CLASS__, 'framework_missing_notice' ) );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) );
// Nexus hooks
add_filter( 'nxs_links_getposttypes', array( __CLASS__, 'nxs_links_getposttypes' ) );
add_action( 'nxs_getwidgets', array( __CLASS__, 'getwidgets' ), 10, 2);
// WooCommerce hooks
add_action( 'woocommerce_before_template_part', array( __CLASS__, 'before_template' ) ); //"nxs_woobridge_beforetemplate");
add_action( 'woocommerce_after_template_part', array( __CLASS__, 'after_template' ) ); //nxs_woobridge_aftertemplate");
add_filter( 'add_to_cart_fragments', array( __CLASS__, 'add_to_cart_fragments' ), 99 );
}
static function is_framework_active() {
return defined( 'NXS_FRAMEWORKLOADED' );
}
static function is_wc_active() {
return function_exists( 'WC' ) || isset( $GLOBALS['woocommerce'] );
}
static function init() {
if ( ! self::is_framework_active() ) {
return;
}
// WOO widgets
nxs_lazyload_plugin_widget(__FILE__, "wooproductdetail");
nxs_lazyload_plugin_widget(__FILE__, "woomessages");
nxs_lazyload_plugin_widget(__FILE__, "woocheckout");
nxs_lazyload_plugin_widget(__FILE__, "woothankyou");
nxs_lazyload_plugin_widget(__FILE__, "woocart");
nxs_lazyload_plugin_widget(__FILE__, "wooprodlist");
nxs_lazyload_plugin_widget(__FILE__, "wooaddtocart");
nxs_lazyload_plugin_widget(__FILE__, "woogotocart");
nxs_lazyload_plugin_widget(__FILE__, "wooproductreference");
nxs_lazyload_plugin_widget(__FILE__, "woominicart");
// WOO business rules
nxs_lazyload_plugin_widget(__FILE__, "woobusrulearchiveprodcatdyncontent");
nxs_lazyload_plugin_widget(__FILE__, "woobusrulewoopage");
nxs_lazyload_plugin_widget(__FILE__, "woobusruleproduct");
nxs_lazyload_plugin_widget(__FILE__, "woobusrulecategory");
nxs_lazyload_plugin_widget(__FILE__, "woobusrulearchiveprodcat");
}
static function framework_missing_notice() {
if ( self::is_framework_active() ) {
// @todo: i18n?
echo '<div class="error"><p>The nxs_woobridge plugin is not initialized; NexusFramework dependency is missing (hint: activate a WordPress theme from NexusThemes.com first)</p></div>';
}
}
static function enqueue_styles() {
wp_enqueue_style('nxs-woobridge-style', plugins_url( 'css/nxswoobridge.css', __FILE__ ), array(), nxs_getthemeversion(), 'all' );
}
static function nxs_links_getposttypes( $result ) {
$result[] = "product";
return $result;
}
static function add_to_cart_fragments( $result ) {
$result["div.widget_shopping_cart_content"] = self::decoratetemplate_minicart( $result['div.widget_shopping_cart_content'] );
return $result;
}
static function before_template() {
ob_start( array( __CLASS__, 'decoratetemplate_pagerendering' ) );
}
static function after_template() {
ob_end_flush();
}
// Convert form into nexusform
static function decoratetemplate_pagerendering( $buffer ) {
$buffer = str_replace("<form ", "<form class='nxs-form' ", $buffer);
$buffer = str_replace("class=\"button\"", "class=\"button nxs-button nxs-button-scale-2-0 nxs-colorzen nxs-colorzen-c22\"", $buffer);
$buffer = str_replace("button alt", "button alt nxs-button nxs-button-scale-2-0 nxs-colorzen nxs-colorzen-c22 ", $buffer);
return $buffer;
}
static function decoratetemplate_minicart( $buffer ) {
$buffer = str_replace( "button", "button nxs-button nxs-button-scale-1-0 nxs-colorzen nxs-colorzen-c22", $buffer );
return $buffer;
}
static function getwidgets( $result, $widgetargs ) {
if ( ! self::is_wc_active() ) {
return $result;
}
switch ( $widgetargs['nxsposttype'] ) {
case 'busrulesset':
$result[] = array("widgetid" => "woobusrulearchiveprodcatdyncontent");
$result[] = array("widgetid" => "woobusrulewoopage");
$result[] = array("widgetid" => "woobusruleproduct");
$result[] = array("widgetid" => "woobusrulecategory");
$result[] = array("widgetid" => "woobusrulearchiveprodcat");
break;
case 'post':
case 'footer':
case 'header':
case 'subheader':
case 'subfooter':
case 'pagelet':
case 'sidebar':
$result[] = array("widgetid" => "wooproductdetail");
$result[] = array("widgetid" => "woomessages");
$result[] = array("widgetid" => "wooprodlist");
$result[] = array("widgetid" => "woocheckout");
$result[] = array("widgetid" => "woothankyou");
$result[] = array("widgetid" => "woocart");
$result[] = array("widgetid" => "wooaddtocart");
$result[] = array("widgetid" => "woogotocart");
$result[] = array("widgetid" => "wooproductreference");
$result[] = array("widgetid" => "woominicart");
break;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment