-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpp-gallery-category.php
251 lines (210 loc) · 6.45 KB
/
mpp-gallery-category.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* Plugin Name: MediaPress Gallery Category
* Plugin URI: http://buddydev.com
* Description: This plugin create a custom taxonomy for MediaPress and allow users to add gallery to these categories and filter them on gallery directory based on category.
* Version: 1.0.0
* Author: BuddyDev Team
* Author URI: https://buddydev.com
*/
/**
* Contributor Name: Ravi Sharma ( raviousprime )
*
* This plugin is an alias for mpp-gallery-categories it enable user to select only one category at the time of creation or edit gallery detail page.
*/
// Exit if file access directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class MPP_Gallery_Categories_Helper
*/
class MPP_Gallery_Categories_Helper {
/**
* Class instance
*
* @var MPP_Gallery_Categories_Helper
*/
private static $instance = null;
/**
* The constructor.
*/
private function __construct() {
$this->setup();
}
/**
* Class instance
*
* @return MPP_Gallery_Categories_Helper
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Setup the requirements
*/
public function setup() {
// register custom taxonomy for mediapress gallery.
add_action( 'mpp_setup', array( $this, 'register_gallery_taxonomy' ), 99 );
// add interface to assign gallery to any category.
add_action( 'mpp_after_edit_gallery_form_fields', array( $this, 'add_interface' ) );
add_action( 'mpp_before_create_gallery_form_submit_field', array( $this, 'add_interface' ) );
// assign term to gallery.
add_action( 'mpp_gallery_created', array( $this, 'save_gallery_category' ) );
add_action( 'mpp_gallery_updated', array( $this, 'save_gallery_category' ), 8 );
// add category filter on gallery page.
add_action( 'mpp_gallery_directory_order_options', array( $this, 'add_category_filter' ) );
// ajax action to filter gallery.
add_action( 'wp_ajax_mpp_filter', array( $this, 'load_filter_list' ), 5 );
add_action( 'wp_ajax_nopriv_mpp_filter', array( $this, 'load_filter_list' ), 5 );
}
/**
* Register a custom taxonomy for MediaPress
*/
public function register_gallery_taxonomy() {
$labels = array(
'name' => _x( 'Gallery Categories', 'taxonomy general name', 'mpp-gallery-category' ),
'singular_name' => _x( 'Gallery Category', 'taxonomy singular name', 'mpp-gallery-category' ),
'search_items' => __( 'Search Gallery Category', 'mpp-gallery-category' ),
'all_items' => __( 'All Gallery Category', 'mpp-gallery-category' ),
'edit_item' => __( 'Edit Gallery Category', 'mpp-gallery-category' ),
'update_item' => __( 'Update Gallery Category', 'mpp-gallery-category' ),
'add_new_item' => __( 'Add New Gallery Category', 'mpp-gallery-category' ),
'new_item_name' => __( 'New Gallery Category Name', 'mpp-gallery-category' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $this->get_taxonomy_slug() ),
);
register_taxonomy( $this->get_taxonomy(), mpp_get_gallery_post_type(), $args );
}
/**
* Get gallery slug
*
* @return string
*/
public function get_taxonomy_slug() {
return apply_filters( 'mpp_gallery_category_taxonomy_slug', 'mpp-gallery-category' );
}
/**
* Get custom taxonomy name
*
* @return string
*/
public function get_taxonomy() {
return 'mpp_gallery_category';
}
/**
* Add interface to apply category to the gallery
*/
public function add_interface() {
$gallery_id = mpp_is_gallery_create() ? false : mpp_get_current_gallery_id();
$args = array(
'taxonomy' => $this->get_taxonomy(),
'name' => 'gallery_category',
'hide_empty' => false,
);
if ( ! empty( $gallery_id ) ) {
$args['selected'] = $this->get_gallery_category( $gallery_id );
}
echo '<div class="mpp-u mpp-gallery-categories"><label>' . __( 'Choose Category', 'mpp_gallery_categories' ) . '</label>';
wp_dropdown_categories( $args );
echo '</div>';
}
/**
* Get gallery category
*
* @param int $gallery_id Gallery id.
*
* @return int
*/
public function get_gallery_category( $gallery_id ) {
return current( wp_get_post_terms( $gallery_id, $this->get_taxonomy(), array( 'fields' => 'ids' ) ) );
}
/**
* Save gallery category
*
* @param int $gallery_id Gallery id.
*/
public function save_gallery_category( $gallery_id ) {
if ( ! isset( $_POST['gallery_category'] ) ) {
return;
}
$term = absint( $_POST['gallery_category'] );
if ( empty( $term ) ) {
return;
}
wp_set_object_terms( $gallery_id, $term, $this->get_taxonomy(), false );
}
/**
* Add category filter to gallery listing directory.
*/
public function add_category_filter() {
$args = array(
'hide_empty' => 0,
'fields' => 'id=>name',
'taxonomy' => $this->get_taxonomy(),
);
$terms = get_terms( $args );
?>
<?php if ( ! empty( $terms ) ) : ?>
<optgroup label="<?php _e( 'By Category', 'mpp-gallery-category' ) ?>">
<?php foreach ( ( array ) $terms as $id => $name ) : ?>
<option value="mpp-gallery-category-<?php echo $id ?>">
<?php echo $name; ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endif; ?>
<?php
}
/**
* Load filter list
*/
public function load_filter_list() {
$type = isset( $_POST['filter'] ) ? $_POST['filter'] : '';
if ( strpos( $type, 'mpp-gallery-category-' ) === false ) {
return;
}
$cat_id = absint( str_replace( 'mpp-gallery-category-', '', $type ) );
$page = absint( $_POST['page'] );
$scope = $_POST['scope'];
$search_terms = $_POST['search_terms'];
// Make the query and setup.
mediapress()->is_directory = true;
// Get all public galleries, should we do type filtering.
mediapress()->the_gallery_query = new MPP_Gallery_Query(
array(
'status' => 'public',
'page' => $page,
'search_terms' => $search_terms,
'tax_query' => array(
array(
'taxonomy' => $this->get_taxonomy(),
'field' => 'term_id',
'terms' => $cat_id,
),
),
)
);
mpp_get_template( 'gallery/loop-gallery.php' );
exit( 0 );
}
}
/**
* Class instance
*
* @return MPP_Gallery_Categories_Helper
*/
function mpp_gallery_categories() {
return MPP_Gallery_Categories_Helper::get_instance();
}
mpp_gallery_categories();