-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunax-future-products.php
189 lines (154 loc) · 5.19 KB
/
unax-future-products.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Plugin Name: Unax Addons - Future Products
* Description: Plugin provides custom functionalities for future products
* Version: 1.0.0
* Author: Unax
* Author URI: https://unax.org/
* Text Domain: unax-addons-future-products
* Domain Path: /languages
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* @package Unax_Addons\Future_Products
* @author Unax
*/
namespace Unax_Addons;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// Load the textdomain.
load_plugin_textdomain( 'unax', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
// Hooks.
// add_filter( 'woocommerce_product_object_query_args', array( '\Unax_Addons\Future_Products', 'product_object_query_args' ) );
add_action( 'pre_get_posts', array( '\Unax_Addons\Future_Products', 'show_future_products' ) );
// add_filter( 'the_permalink', array( '\Unax_Addons\Future_Products', 'fix_permalink' ), 10, 2 );
// add_filter( 'woocommerce_is_purchasable', array( '\Unax_Addons\Future_Products', 'is_purchasable' ), 10, 2 );
// add_filter( 'woocommerce_product_is_visible', array( '\Unax_Addons\Future_Products', 'product_is_visible' ), 10, 2 );
// add_filter( 'woocommerce_variation_is_purchasable', array( '\Unax_Addons\Future_Products', 'is_purchasable' ), 10, 2 );
// add_filter( 'woocommerce_variation_is_visible', array( '\Unax_Addons\Future_Products', 'variation_is_visible' ), 10, 4 );
/*
* Class Future_Products
*/
class Future_Products {
/**
* Post types.
*
* @var array
*/
public static $post_types = array( 'product', 'product_variation' );
/**
* Show future products.
*
* @param array $query object.
*/
public static function product_object_query_args( $query_vars ) {
array_push( $query_vars['status'], 'future' );
return $query_vars;
}
/**
* Show future products.
*
* @param WP_Query $query object.
*/
public static function show_future_products( $query ) {
// Check if the query is for products.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
$query->set( 'post_status', array( 'publish', 'future' ) );
$query->set( 'orderby', 'date' );
$query->set( 'order', 'ASC' );
}
/**
* Fix future product permalink.
*
* @param string $permalink Permalink.
* @param int $post WP_Post.
*
* @return string
*/
public static function fix_permalink( $permalink, $post ) {
/* For filter recursion (infinite loop) */
static $recursing = false;
if ( empty( $post->ID ) ) {
return $permalink;
}
if ( ! $recursing ) {
if ( isset( $post->post_status ) && ( 'future' === $post->post_status ) ) {
// Set the post status to publish to get the 'publish' permalink.
$post->post_status = 'publish';
$recursing = true;
return get_permalink( $post ) ;
}
}
$recursing = false;
return $permalink;
}
/**
* Show future products.
*
* @param bool $purchasable Product is purchasable.
* @param int $product WC Product.
*/
public static function is_purchasable( $purchasable, $product ) {
if ( $product->exists() && ( 'future' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) ) && '' !== $product->get_price() ) {
$purchasable = true;
}
return $purchasable;
}
/**
* Show future products.
*
* @param bool $visible Product is visible.
* @param int $product_id WC Product id.
*/
public static function product_is_visible( $visible, $product_id ) {
$product = wc_get_product( $product_id );
if ( empty( $product ) ) {
return $visible;
}
if ( 'visible' !== $product->get_catalog_visibility() ) {
return $visible;
}
if ( 'future' === $product->get_status() ) {
$visible = true;
}
if ( $product->get_parent_id() ) {
$parent_product = wc_get_product( $product->get_parent_id() );
if ( $parent_product && 'future' === $parent_product->get_status() ) {
$visible = true;
}
}
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $product->is_in_stock() ) {
$visible = false;
}
return $visible;
}
/**
* Show future products.
*
* @param bool $purchasable Product is purchasable.
* @param int $product WC Product.
*/
public static function variation_is_purchasable( $purchasable, $variation ) {
if ( $variation->variation_is_visible() && ( 'future' === $variation->parent_data['status'] || current_user_can( 'edit_post', $variation->get_parent_id() ) ) ) {
$purchasable = true;
}
return $purchasable;
}
/**
* Show future products.
*
* @param bool $visible Variation is visible.
* @param int $variation_id Product variation id.
* @param int $parent_id Product id.
* @param WC_Product_Variation $variation Product variation.
*/
public static function variation_is_visible( $visible, $variation_id, $parent_id, $variation ) {
if ( 'future' === get_post_status( $variation_id ) && '' !== $variation->get_price() ) {
$visible = true;
}
return $visible;
}
}