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

DP_Taxonomy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register_taxonomy() 0 16 1
A maybe_create_terms() 0 18 6
1
<?php
2
/**
3
 * WooCommerce Discontinued Products Taxonomy Class
4
 *
5
 * @package woocommerce
6
 * @since 1.1.0
7
 */
8
9
if ( ! class_exists( 'DP_Taxonomy' ) ) {
10
	/**
11
	 * DP_Taxonomy Class
12
	 *
13
	 * @since 1.1.0
14
	 */
15
	class DP_Taxonomy {
16
17
		/**
18
		 * Inititiate the DP_Taxonomy.
19
		 *
20
		 * @since 2.0.0
21
		 */
22
		public function __construct() {
23
24
			add_action( 'init', array( $this, 'register_taxonomy' ), 5 );
25
			add_action( 'init', array( $this, 'maybe_create_terms' ), 100 );
26
		}
27
28
		/**
29
		 * Reguster the discontinued taxonomy.
30
		 *
31
		 * @since 2.0.0
32
		 */
33
		public function register_taxonomy() {
34
35
			register_taxonomy(
36
				'product_discontinued',
37
				array( 'product' ),
38
				array(
39
					'hierarchical'      => false,
40
					'show_ui'           => false,
41
					'show_in_nav_menus' => false,
42
					'query_var'         => is_admin(),
43
					'rewrite'           => false,
44
					'public'            => false,
45
					'label'             => _x( 'Product discontinued', 'Taxonomy name', 'discontinued-products' ),
46
				)
47
			);
48
		}
49
50
		/**
51
		 * Create the product_discontinued terms if they don't exist.
52
		 *
53
		 * @since 2.0.0
54
		 */
55
		public function maybe_create_terms() {
56
57
			$discontinued   = term_exists( 'dp-discontinued', 'product_discontinued' );
58
			$hide_on_shop   = term_exists( 'dp-hide-shop', 'product_discontinued' );
59
			$show_on_shop   = term_exists( 'dp-show-shop', 'product_discontinued' );
60
			$hide_on_search = term_exists( 'dp-hide-search', 'product_discontinued' );
61
			$show_on_search = term_exists( 'dp-show-search', 'product_discontinued' );
62
			$discontinued   = $discontinued ? $discontinued : wp_insert_term( 'dp-discontinued', 'product_discontinued', array( 'slug' => 'dp-discontinued' ) );
63
			$hide_on_shop   = $hide_on_shop ? $hide_on_shop : wp_insert_term( 'dp-hide-shop', 'product_discontinued', array( 'slug' => 'dp-hide-shop' ) );
64
			$show_on_shop   = $show_on_shop ? $show_on_shop : wp_insert_term( 'dp-show-shop', 'product_discontinued', array( 'slug' => 'dp-show-shop' ) );
65
			$hide_on_search = $hide_on_search ? $hide_on_search : wp_insert_term( 'dp-hide-search', 'product_discontinued', array( 'slug' => 'dp-hide-search' ) );
66
			$show_on_search = $show_on_search ? $show_on_search : wp_insert_term( 'dp-show-search', 'product_discontinued', array( 'slug' => 'dp-show-search' ) );
67
			update_option( 'dp_discontinued_term', $discontinued['term_id'] );
68
			update_option( 'dp_hide_shop_term', $hide_on_shop['term_id'] );
69
			update_option( 'dp_show_shop_term', $show_on_shop['term_id'] );
70
			update_option( 'dp_hide_search_term', $hide_on_search['term_id'] );
71
			update_option( 'dp_show_search_term', $show_on_search['term_id'] );
72
		}
73
	}
74
75
	$dp_taxonomy = new DP_Taxonomy();
76
}
77