Completed
Push — master ( 84cd3f...1a4147 )
by Leon
01:15
created

DP_Discontinued_Product::save()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 1
dl 0
loc 21
rs 9.2728
c 0
b 0
f 0
1
<?php
2
/**
3
 * WooCommerce Discontinued Product Class
4
 *
5
 * @package woocommerce
6
 * @since 1.0.0
7
 */
8
9
if ( ! class_exists( 'DP_Discontinued_Product' ) ) {
10
	/**
11
	 * DP_Discontinued_Product Class
12
	 *
13
	 * @since 1.0.0
14
	 */
15
	class DP_Discontinued_Product {
16
17
		/**
18
		 * Get the current page ID before it is set in WP_Query.
19
		 *
20
		 * @var int
21
		 */
22
		private $current_page_id = 0;
23
24
		/**
25
		 * Inititiate the DP_Discontinued_Product.
26
		 *
27
		 * @since 1.0.0
28
		 */
29
		public function __construct() {
30
			if ( ! is_admin() ) {
31
				$page                  = get_page_by_path( $_SERVER['REQUEST_URI'] );
32
				$page_id               = is_object( $page ) && property_exists( $page, 'ID' ) ? $page->ID : false;
33
				$this->current_page_id = $page_id ? $page_id : $this->current_page_id;
34
				add_action( 'init', array( $this, 'woocommerce_flush_rewrite_rules' ) );
35
				add_action( 'woocommerce_product_query', array( $this, 'exclude_discontinued_products' ), 1000 );
36
				add_filter( 'woocommerce_get_shop_page_id', array( $this, 'override_shop_page_id' ), 1, 1 );
37
				add_action( 'template_redirect', array( $this, 'add_dp_alt_products' ) );
38
			}
39
			if ( is_admin() ) {
40
				add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'add_discontinued_product_tab' ) );
41
				add_action( 'woocommerce_product_data_panels', array( $this, 'add_discontinued_product_panel' ) );
42
				add_action( 'woocommerce_process_product_meta', array( $this, 'save' ) );
43
				add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
44
			}
45
		}
46
		/**
47
		 * Conditionally flush rewrite rules.
48
		 *
49
		 * @since 2.0.0
50
		 */
51
		public function woocommerce_flush_rewrite_rules() {
52
53
			$dc_shop_page_id = (int) get_option( 'dp_shop_page_id' );
54
			$shop_page_id    = wc_get_page_id( 'shop' );
55
			if ( $dc_shop_page_id && $this->current_page_id === $shop_page_id || $this->current_page_id === $dc_shop_page_id ) {
56
				do_action( 'woocommerce_flush_rewrite_rules' );
57
			}
58
		}
59
		/**
60
		 * Enqueue CSS and JS.
61
		 *
62
		 * @since 1.0.0
63
		 */
64
		public function enqueue_scripts() {
65
66
			$screen = get_current_screen();
67
			if ( 'product' === $screen->post_type ) {
68
69
				wp_register_style( 'discontinued_product_styles', DP_URI . '/assets/css/discontinued_product.css', array(), DP_VER );
70
				wp_enqueue_style( 'discontinued_product_styles' );
71
			}
72
73
		}
74
75
		/**
76
		 * Add tab to the Product Data meta box.
77
		 *
78
		 * @since 1.0.0
79
		 */
80
		public function add_discontinued_product_tab() {
81
82
			?>
83
			<li class="discontinued_product_tab"><a href="#discontinued_product_tab_data"><span><?php esc_html_e( 'Discontinued Products', 'discontinued-products' ); ?></span></a></li>
84
			<?php
85
		}
86
87
		/**
88
		 * Add tab to the Product Data meta box.
89
		 *
90
		 * @since 1.0.0
91
		 */
92
		public function add_discontinued_product_panel() {
93
94
			global $post;
95
			?>
96
			<div id="discontinued_product_tab_data" class="panel woocommerce_options_panel wc-metaboxes-wrapper">
97
				<div class="options_group">
98
					<?php
99
						woocommerce_wp_checkbox(
100
							array(
101
								'id'            => '_is_discontinued',
102
								'wrapper_class' => '',
103
								'label'         => __( 'Is Discontinued', 'discontinued-products' ),
104
								'description'   => __( 'Check if this product is discontinued', 'discontinued-products' ),
105
								'value'         => has_term( absint( get_option( 'dp_discontinued_term' ) ), 'product_discontinued', $post->ID ) ? 'yes' : '',
106
							)
107
						);
108
						$placeholder = get_option( 'dp_discontinued_text' );
109
						woocommerce_wp_text_input(
110
							array(
111
								'id'          => '_discontinued_product_text',
112
								'label'       => __( 'Display text', 'discontinued-products' ),
113
								'placeholder' => $placeholder,
114
								'desc_tip'    => 'true',
115
								'description' => __( 'Enter text to be shown when this product is discontinued', 'discontinued-products' ),
116
							)
117
						);
118
					?>
119
				</div>
120
121
				<div class="options_group">
122
123
					<p class="form-field">
124
						<label for="alt_products"><?php esc_html_e( 'Alternative Products', 'discontinued-products' ); ?></label>
125
						<select name="alt_products[]" class="wc-product-search" multiple="multiple" style="width: 50%;" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'discontinued-products' ); ?>" data-action="woocommerce_json_search_products_and_variations">
126
							<?php
127
							$product_ids = array_filter( array_map( 'absint', (array) get_post_meta( $post->ID, '_alt_products', true ) ) );
128
129
							foreach ( $product_ids as $product_id ) {
130
								$product = wc_get_product( $product_id );
131
								if ( is_object( $product ) ) {
132
									echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
133
								}
134
							}
135
							?>
136
						</select>
137
						<?php
138
						// @codingStandardsIgnoreStart
139
						echo wc_help_tip( __( 'Any product that is added to this field will generate a button for the add to cart area that will link to the corresponding product.', 'discontinued-products' ) );
140
						// @codingStandardsIgnoreEnd
141
						?>
142
					</p>
143
144
145
					<?php
146
						$placeholder = get_option( 'dp_alt_text' );
147
						woocommerce_wp_text_input(
148
							array(
149
								'id'          => '_alt_product_text',
150
								'label'       => __( 'Alternative product text', 'discontinued-products' ),
151
								'placeholder' => $placeholder,
152
								'desc_tip'    => 'true',
153
								'description' => __( 'Enter text to be shown when alternative product are suggested.', 'discontinued-products' ),
154
							)
155
						);
156
					?>
157
				</div>
158
159
				<div class="options_group">
160
					<?php
161
						$hide_from_shop = '';
162
						$hide_from_shop = has_term( absint( get_option( 'dp_hide_shop_term' ) ), 'product_discontinued', $post->ID ) ? 'hide' : $hide_from_shop;
163
						$hide_from_shop = has_term( absint( get_option( 'dp_show_shop_term' ) ), 'product_discontinued', $post->ID ) ? 'show' : $hide_from_shop;
164
						woocommerce_wp_select(
165
							array(
166
								'id'          => '_hide_from_shop',
167
								'label'       => __( 'Hide on shop / archive.', 'discontinued-products' ),
168
								'desc_tip'    => 'true',
169
								'description' => __( 'Hide from shop / archive pages.', 'discontinued-products' ),
170
								'options'     => array(
171
									''     => __( 'Default', 'discontinued-products' ),
172
									'hide' => __( 'Hide', 'discontinued-products' ),
173
									'show' => __( 'Show', 'discontinued-products' ),
174
								),
175
								'value'       => $hide_from_shop,
176
							)
177
						);
178
						$hide_from_search = '';
179
						$hide_from_search = has_term( absint( get_option( 'dp_hide_search_term' ) ), 'product_discontinued', $post->ID ) ? 'hide' : $hide_from_search;
180
						$hide_from_search = has_term( absint( get_option( 'dp_show_search_term' ) ), 'product_discontinued', $post->ID ) ? 'show' : $hide_from_search;
0 ignored issues
show
Unused Code introduced by
$hide_from_search is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
181
						woocommerce_wp_select(
182
							array(
183
								'id'          => '_hide_from_search',
184
								'label'       => __( 'Hide on search.', 'discontinued-products' ),
185
								'desc_tip'    => 'true',
186
								'description' => __( 'Hide from search results.', 'discontinued-products' ),
187
								'options'     => array(
188
									''     => __( 'Default', 'discontinued-products' ),
189
									'hide' => __( 'Hide', 'discontinued-products' ),
190
									'show' => __( 'Show', 'discontinued-products' ),
191
								),
192
							)
193
						);
194
					?>
195
				</div>
196
			</div>
197
			<?php
198
		}
199
		/**
200
		 * Save dicontinued product settings.
201
		 *
202
		 * @since 2.0.0
203
		 * @param int $post_id Optional. ID of the product to update.
204
		 */
205
		public static function save( $post_id ) {
206
207
			$terms = array();
208
			update_post_meta( $post_id, '_discontinued_product_text', filter_input( INPUT_POST, '_discontinued_product_text' ) );
209
			$alt_products = filter_input( INPUT_POST, 'alt_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ? filter_input( INPUT_POST, 'alt_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) : array();
210
			update_post_meta( $post_id, '_alt_products', $alt_products );
211
			update_post_meta( $post_id, '_alt_product_text', filter_input( INPUT_POST, '_alt_product_text' ) );
212
			$is_discontinued = filter_input( INPUT_POST, '_is_discontinued' );
213
			if ( $is_discontinued ) {
214
				$terms[] = (int) get_option( 'dp_discontinued_term' );
215
			}
216
			$hide_from_shop = filter_input( INPUT_POST, '_hide_from_shop' );
217
			if ( $hide_from_shop ) {
218
				$terms[] = (int) get_option( 'dp_' . $hide_from_shop . '_shop_term' );
219
			}
220
			$hide_from_search = filter_input( INPUT_POST, '_hide_from_search' );
221
			if ( $hide_from_search ) {
222
				$terms[] = (int) get_option( 'dp_' . $hide_from_search . '_search_term' );
223
			}
224
			wp_set_object_terms( $post_id, $terms, 'product_discontinued' );
225
		}
226
227
		/**
228
		 * Remove add to cart.
229
		 * Remove add to cart and all related buttons like wishlist, and add the alt products.
230
		 *
231
		 * @since 1.0.0
232
		 */
233
		public static function add_dp_alt_products() {
234
235
			if ( dp_is_discontinued() ) {
236
				add_action( 'woocommerce_single_product_summary', 'dp_alt_products', 60 );
237
			}
238
		}
239
240
		/**
241
		 * Exclude discontinued products.
242
		 * Add a the post__not_in argiment to the main query for products.
243
		 *
244
		 * @since 1.0.0
245
		 * @param object $q Main WP Query.
246
		 */
247
		public function exclude_discontinued_products( $q ) {
248
			if ( $q->is_main_query() ) {
249
				$dc_shop_page_id        = (int) get_option( 'dp_shop_page_id' );
250
				$hide_from_shop         = get_option( 'dp_hide_from_shop' );
251
				$dp_discontinued_term   = (int) get_option( 'dp_discontinued_term' );
252
				$dp_hide_shop_term      = (int) get_option( 'dp_hide_shop_term' );
253
				$hide_shop_terms        = 'yes' === $hide_from_shop ? array( $dp_discontinued_term, $dp_hide_shop_term ) : array( $dp_hide_shop_term );
254
				$hide_from_search       = get_option( 'dp_hide_from_search' );
255
				$dp_hide_search_term    = (int) get_option( 'dp_hide_search_term' );
256
				$hide_search_terms      = 'yes' === $hide_from_search ? array( $dp_discontinued_term, $dp_hide_search_term ) : array( $dp_hide_search_term );
257
				$tax_query              = $q->get( 'tax_query' );
258
				$tax_query              = is_array( $tax_query ) ? $tax_query : array();
259
				if ( $this->current_page_id === $dc_shop_page_id ) {
260
					$tax_query[] = array(
261
						'taxonomy' => 'product_discontinued',
262
						'field'    => 'id',
263
						'terms'    => $dp_discontinued_term,
264
						'operator' => 'IN',
265
					);
266
				}
267
				if (  ( is_shop() || is_product_category() ) && $this->current_page_id !== $dc_shop_page_id && ! $q->is_search() ) {
268
					$tax_query[] = array(
269
						'relation' => 'OR',
270
						array(
271
							'taxonomy' => 'product_discontinued',
272
							'field'    => 'id',
273
							'terms'    => $hide_shop_terms,
274
							'operator' => 'NOT IN',
275
						),
276
						array(
277
							'taxonomy' => 'product_discontinued',
278
							'field'    => 'id',
279
							'terms'    => array( (int) get_option( 'dp_show_shop_term' ) ),
280
							'operator' => 'IN',
281
						),
282
					);
283
				}
284
				if ( $q->is_search() ) {
285
					$tax_query[] = array(
286
						'relation' => 'OR',
287
						array(
288
							'taxonomy' => 'product_discontinued',
289
							'field'    => 'id',
290
							'terms'    => $hide_search_terms,
291
							'operator' => 'NOT IN',
292
						),
293
						array(
294
							'taxonomy' => 'product_discontinued',
295
							'field'    => 'id',
296
							'terms'    => array( (int) get_option( 'dp_show_search_term' ) ),
297
							'operator' => 'IN',
298
						),
299
					);
300
				}
301
				$q->set( 'tax_query', $tax_query );
302
			}
303
		}
304
305
		/**
306
		 * Borrow the WooCommerce shop template for discontinued products.
307
		 *
308
		 * @since 1.4.0
309
		 * @param int $shop_page_id Shop page ID.
310
		 */
311
		public function override_shop_page_id( $shop_page_id ) {
312
313
			$dc_shop_page_id = (int) get_option( 'dp_shop_page_id' );
314
315
			if ( $this->current_page_id === $dc_shop_page_id ) {
316
				$shop_page_id = $dc_shop_page_id;
317
			}
318
			return $shop_page_id;
319
		}
320
	}
321
322
	$dp_discontinued_product = new DP_Discontinued_Product();
323
}
324