| 1 |
<?php |
| 2 |
/** |
| 3 |
* Products list table (Products → All Products) integration. |
| 4 |
* |
| 5 |
* @package SpringDevs\Subscription\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SpringDevs\Subscription\Admin; |
| 9 |
|
| 10 |
// HPOS: This file does not access WooCommerce order data directly. |
| 11 |
// All meta access is for product data only, not WooCommerce order data. |
| 12 |
|
| 13 |
/** |
| 14 |
* ProductList class |
| 15 |
* |
| 16 |
* Marks products managed by WPSubscription with the plugin icon beside the |
| 17 |
* product name on `edit.php?post_type=product`. |
| 18 |
* |
| 19 |
* @package SpringDevs\Subscription\Admin |
| 20 |
*/ |
| 21 |
class ProductList { |
| 22 |
|
| 23 |
/** |
| 24 |
* Initialize the class |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
// After WooCommerce's own renderer (priority 10), so the icon lands |
| 28 |
// beside the product name and above the row actions. |
| 29 |
add_action( 'manage_product_posts_custom_column', array( $this, 'render_icon' ), 20, 2 ); |
| 30 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Print the plugin icon in the name column of a subscription product. |
| 35 |
* |
| 36 |
* @param string $column Column key. |
| 37 |
* @param int $post_id Product id. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function render_icon( $column, $post_id ) { |
| 42 |
if ( 'name' !== $column ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
$product = wc_get_product( $post_id ); |
| 47 |
if ( ! $product || ! self::is_subscription_product( $product ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$label = __( 'This product is sold as a subscription by WPSubscription.', 'subscription' ); |
| 52 |
|
| 53 |
printf( |
| 54 |
'<img class="subscrpt-product-list-icon" src="%1$s" width="16" height="16" alt="%2$s" title="%2$s" />', |
| 55 |
esc_url( SUBSCRPT_ASSETS . '/images/icons/subscription-20.png' ), |
| 56 |
esc_attr( $label ) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Whether a product is sold as a subscription. |
| 62 |
* |
| 63 |
* Simple products check the product itself; variable products count when |
| 64 |
* any variation is subscription-enabled (a plan tied to the parent applies |
| 65 |
* to every variation, so it is picked up there). |
| 66 |
* |
| 67 |
* @param \WC_Product $product Product. |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
public static function is_subscription_product( $product ) { |
| 72 |
$product_id = $product->get_id(); |
| 73 |
|
| 74 |
if ( ! $product->is_type( 'variable' ) ) { |
| 75 |
return subscrpt_is_subscription_enabled( $product_id ); |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( $product->get_children() as $variation_id ) { |
| 79 |
if ( subscrpt_is_subscription_enabled( $product_id, $variation_id ) ) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Icon styles, on the products list screen only. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function enqueue_styles() { |
| 93 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; |
| 94 |
if ( ! $screen || 'edit-product' !== $screen->id ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
wp_register_style( 'subscrpt_product_list', false, array(), SUBSCRPT_VERSION ); |
| 99 |
wp_enqueue_style( 'subscrpt_product_list' ); |
| 100 |
wp_add_inline_style( |
| 101 |
'subscrpt_product_list', |
| 102 |
'.subscrpt-product-list-icon{margin-left:6px;vertical-align:middle;}' |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|