Passed
Push — master ( 5c13d7...6249b0 )
by Warwick
02:37 queued 12s
created

LSX_WETU_Importer_Post_Columns   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A get_instance() 0 7 2
A display_page() 0 3 1
A register_tour_columns() 0 14 2
A output_tour_ref_column() 0 5 2
A register_sortable_columns() 0 4 1
B columns_posts_orderby() 0 20 6
1
<?php
2
/**
3
 * The Post Columns code for the Importer Plugin
4
 *
5
 * @package   lsx_wetu_importer
6
 * @author    LightSpeed
7
 * @license   GPL-3.0+
8
 * @link
9
 * @copyright 2019 LightSpeed
10
 **/
11
12
/**
13
 * The Post Columns code for the Importer Plugin
14
 */
15
class LSX_WETU_Importer_Post_Columns {
16
17
	/**
18
	 * Holds instance of the class
19
	 *
20
	 * @var object
21
	 */
22
	private static $instance;
23
24
	/**
25
	 * Initialize the plugin by setting localization, filters, and administration functions.
26
	 *
27
	 * @since 1.0.0
28
	 *
29
	 * @access private
30
	 */
31
	public function __construct() {
32
		add_filter( 'manage_tour_posts_columns', array( $this, 'register_tour_columns' ) );
33
		add_action( 'manage_tour_posts_custom_column', array( $this, 'output_tour_ref_column' ), 10, 2 );
34
35
		// Sortables Columns.
36
		add_filter( 'manage_edit-tour_sortable_columns', array( $this, 'register_sortable_columns' ) );
37
		add_action( 'pre_get_posts', array( $this, 'columns_posts_orderby' ) );
38
	}
39
40
	/**
41
	 * Return an instance of this class.
42
	 *
43
	 * @return  object
44
	 */
45
	public static function get_instance() {
46
		// If the single instance hasn't been set, set it now.
47
		if ( ! isset( self::$instance ) ) {
48
			self::$instance = new self();
49
		}
50
		return self::$instance;
51
	}
52
53
	/**
54
	 * Display the importer welcome screen
55
	 */
56
	public function display_page() {
57
58
	}
59
60
	/**
61
	 * Registers the tour ref column
62
	 *
63
	 * @param array $columns
64
	 * @return array
65
	 */
66
	public function register_tour_columns( $columns ) {
67
		$new_columns = array(
68
			'cb'       => $columns['cb'],
69
			'title'    => $columns['title'],
70
			'wetu_ref' => __( 'Ref', 'lsx-wetu-importer' ),
71
		);
72
		unset( $columns['cb'] );
73
		unset( $columns['title'] );
74
		foreach ( $columns as $column_key => $column_label ) {
75
			$new_columns[ $column_key ] = $column_label;
76
		}
77
		$columns = $new_columns;
78
		return $columns;
79
	}
80
81
	/**
82
	 * Outputs the tour reference column
83
	 *
84
	 * @param string $column
85
	 * @param string $post_id
86
	 * @return void
87
	 */
88
	public function output_tour_ref_column( $column, $post_id ) {
89
		if ( 'wetu_ref' === $column ) {
90
			echo esc_attr( get_post_meta( $post_id, 'lsx_wetu_ref', true ) );
91
		}
92
	}
93
94
	/**
95
	 * Register the columns that will be sortable
96
	 *
97
	 * @param array $columns
98
	 * @return array
99
	 */
100
	public function register_sortable_columns( $columns = array() ) {
101
		$columns['wetu_ref'] = 'price_per_month';
102
		return $columns;
103
	}
104
105
	/**
106
	 * Sort the columns
107
	 *
108
	 * @param object $query WP_Query()
109
	 * @return void
110
	 */
111
	public function columns_posts_orderby( $query ) {
112
		if( ! is_admin() || ! $query->is_main_query() ) {
113
			return;
114
		}
115
		if ( 'wetu_ref' === $query->get( 'orderby' ) ) {
116
			$query->set( 'orderby', 'meta_value' );
117
			$query->set( 'meta_key', 'lsx_wetu_reference' );
118
		}
119
		if ( $query->is_search() && 'tour' === $query->get( 'post_type' ) ) {
120
			$meta_query = array(
121
				'relation' => 'OR',
122
				array(
123
					'key' => 'lsx_wetu_ref',
124
					'value' => get_search_query(),
125
					'compare' => 'LIKE',
126
				),
127
			);
128
			$query->set( 'meta_query', $meta_query );
129
		}
130
	}
131
}
132