Skip to content

Commit

Permalink
New: WooCommerce HPOS compatibility (beta) (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmigf authored Jul 21, 2023
1 parent 3f0e4bf commit 4100f97
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 26 deletions.
71 changes: 71 additions & 0 deletions includes/class-wciep-order-util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

class WPO_WCIEP_Order_Util {

protected static $_instance = null;
public $wc_order_util_class_object;

/**
* Construct.
*/
public function __construct() {
$this->wc_order_util_class_object = $this->get_wc_order_util_class();
}

/**
* Instance.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}

/**
* Function to check woocommerce OrderUtil class is exists or not.
*/
public function get_wc_order_util_class() {
if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) ) {
return \Automattic\WooCommerce\Utilities\OrderUtil::class;
} else {
return false;
}
}

/**
* Function to get order type.
*/
public function get_order_type( $order_id ) {
if ( $this->wc_order_util_class_object && is_callable( [ $this->wc_order_util_class_object, 'get_order_type' ] ) ) {
return $this->wc_order_util_class_object::get_order_type( intval( $order_id ) );
} else {
return get_post_type( intval( $order_id ) );
}
}

/**
* Function to check id HPOS functionality is enabled or not.
*/
public function custom_orders_table_usage_is_enabled() {
if ( $this->wc_order_util_class_object && is_callable( [ $this->wc_order_util_class_object, 'custom_orders_table_usage_is_enabled' ] ) ) {
return $this->wc_order_util_class_object::custom_orders_table_usage_is_enabled();
} else {
return false;
}
}

/**
* Function to check screen.
*/
public function custom_order_table_screen() {
$screen = $this->custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order';

return $screen;
}

} // end class
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
<?php

class ImprovedExternalProducts_Settings {
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

class WPO_WCIEP_Settings {

protected static $_instance = null;

public function __construct() {
add_action( 'admin_init', array( $this, 'init_settings' ) ); // Registers settings
add_action( 'admin_menu', array( $this, 'iepp_add_page' ) );
}

/**
* Instance.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}

/**
* User settings.
Expand Down Expand Up @@ -576,7 +592,7 @@ public function icons_radio_element_callback( $args ) {
echo $html;
}

function multicheckbox_element_callback( $args ) {
public function multicheckbox_element_callback( $args ) {
$options = get_option('woocommerce-improved-external-products');
$pag = 'woocommerce-improved-external-products';
$_cats = get_terms( 'product_cat' );
Expand Down
67 changes: 43 additions & 24 deletions wc-improved-external-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: woocommerce-improved-external-products
* WC requires at least: 2.6.0
* WC requires at least: 3.0
* WC tested up to: 6.9
*/

class ImprovedExternalProducts {

protected $plugin_version = '1.5.16';
public $settings;

/**
* Construct.
Expand All @@ -40,6 +41,9 @@ public function __construct() {
// Display the admin notification
add_action( 'admin_notices', array( $this, 'go_pro_notice' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'backend_scripts_styles' ) );

// HPOS compatibility
add_action( 'before_woocommerce_init', array( $this, 'woocommerce_hpos_compatible' ) );
}

/**
Expand All @@ -57,18 +61,31 @@ private function define( $name, $value ) {
* Redirect: Make It So
*
*/
function iepp_activate() {
public function iepp_activate() {
add_option('iepp_do_activation_redirect', true);
}

/**
* Declares WooCommerce HPOS compatibility.
*
* @return void
*/
public function woocommerce_hpos_compatible() {
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}

/**
* Shows a notice for the Pro version on the order admin pages
*/
function go_pro_notice() {
if ( !isset($GLOBALS['post_type']) || !in_array( $GLOBALS['post_type'], array('shop_order','product') ) ) {
public function go_pro_notice() {
$screen = $this->orderUtil->custom_order_table_screen();

if ( ( isset( $_REQUEST['page'] ) && 'iepp_options_page' != $_REQUEST['page'] ) || ! in_array( $screen, array( 'shop_order', 'edit-shop_order', 'woocommerce_page_wc-orders', 'edit-product', 'product' ) ) ) {
return;
}

if ( get_option( 'wpo_iepp_pro_notice_dismissed' ) !== false || get_option( 'iepp_go_pro_notice' ) == 'gopro' ) {
return;
} else {
Expand Down Expand Up @@ -106,8 +123,10 @@ function go_pro_notice() {
}
}

function backend_scripts_styles() {
if ( isset($GLOBALS['post_type']) && in_array( $GLOBALS['post_type'], array('shop_order','product') ) ) {
public function backend_scripts_styles() {
$screen = $this->orderUtil->custom_order_table_screen();

if ( ( isset( $_REQUEST['page'] ) && 'iepp_options_page' == $_REQUEST['page'] ) || in_array( $screen, array( 'shop_order', 'edit-shop_order', 'woocommerce_page_wc-orders', 'edit-product', 'product' ) ) ) {
wp_enqueue_script(
'wpo-iepp-admin',
untrailingslashit( plugins_url( '/', __FILE__ ) ) . '/assets/js/admin-script.js',
Expand All @@ -117,7 +136,7 @@ function backend_scripts_styles() {
}
}

function iepp_redirect() {
public function iepp_redirect() {
if (get_option('iepp_do_activation_redirect', false)) {
delete_option('iepp_do_activation_redirect');
if(!isset($_GET['activate-multi'])){
Expand All @@ -126,7 +145,7 @@ function iepp_redirect() {
}
}

function modify_external_product_links(){
public function modify_external_product_links(){
/* single product actions */
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_external_add_to_cart', array($this,'iepp_external_add_to_cart'), 30 );
Expand All @@ -137,7 +156,7 @@ function modify_external_product_links(){
*
* @subpackage Product
*/
function iepp_external_add_to_cart() {
public function iepp_external_add_to_cart() {
global $product;

$product = wc_get_product($product);
Expand All @@ -147,7 +166,7 @@ function iepp_external_add_to_cart() {
return;
}

$target = $this->determine_link_target( $this->get_product_id( $product ) );
$target = $this->determine_link_target( $product->get_id() );
$price_html = $product->get_price_html();
if ( $target == true ) {
$target = '_blank';
Expand Down Expand Up @@ -177,15 +196,20 @@ function iepp_external_add_to_cart() {
<?php
}

function includes(){
if(!class_exists('ImprovedExternalProducts_Settings')){
require_once( 'includes/wc-improved-external-products-settings.php' );
public function includes() {
if ( ! class_exists( 'WPO_WCIEP_Order_Util' ) ) {
require_once( 'includes/class-wciep-order-util.php' );
// Get settings
$this->orderUtil = WPO_WCIEP_Order_Util::instance();
}
if ( ! class_exists( 'WPO_WCIEP_Settings' ) ) {
require_once( 'includes/class-wciep-settings.php' );
// Get settings
$this->settings = new ImprovedExternalProducts_Settings();
$this->settings = WPO_WCIEP_Settings::instance();
}
}

function add_js_to_footer(){
public function add_js_to_footer(){
$options = get_option('woocommerce-improved-external-products');
//$extra_selectors = $options['additional_javascript_selectors'];
/* Add code to product page */
Expand All @@ -195,7 +219,7 @@ function add_js_to_footer(){

/* If the product is external */
if($product->is_type( 'external' )){
if($this->determine_link_target( $this->get_product_id( $product ) ) == true){
if($this->determine_link_target( $product->get_id() ) == true){
$target = '_blank';
} else {
$target = '';
Expand All @@ -214,14 +238,9 @@ function add_js_to_footer(){
}
}

function determine_link_target($product_id){
public function determine_link_target($product_id){
return true;
}

// Backwards compatible product ID getter for WC2.6 and older
function get_product_id( $product ) {
return method_exists( $product, 'get_id') ? $product->get_id() : $product->id;
}

}

$ImprovedExternalProducts = new ImprovedExternalProducts();

0 comments on commit 4100f97

Please sign in to comment.