-
Notifications
You must be signed in to change notification settings - Fork 0
/
edd-restrict-categories.php
245 lines (213 loc) · 5.43 KB
/
edd-restrict-categories.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
<?php
/**
* Plugin Name: EDD Restrict Categories
* Plugin URI: https://easydigitaldownloads.com/downloads/restrict-categories/
* Description: Password protect your digital download category archives, and the products inside them.
* Version: 1.0.0
* Author: Frankie Jarrett
* Author URI: http://frankiejarrett.com
* Depends: Easy Digital Downloads
* Text Domain: edd-restrict-categories
* Domain Path: /languages
*
* Copyright: © 2015 Frankie Jarrett.
* License: GNU General Public License v2.0
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main Plugin Class
*
* Password protect your digital download category archives, and the products inside them.
*
* @version 1.0.0
* @package Easy Digital Downloads
* @author Frankie Jarrett
*/
class EDD_Restrict_Categories {
/**
* Hold class instance
*
* @access public
* @static
*
* @var EDD_Restrict_Categories
*/
public static $instance;
/**
* Hold the taxonomy slugs where restriction is allowed
*
* @access public
* @static
*
* @var array
*/
public static $taxonomies;
/**
* Hold the post types where taxonomy restrictions should be honored
*
* @access public
* @static
*
* @var array
*/
public static $post_types;
/**
* Plugin version number
*
* @const string
*/
const VERSION = '1.0.0';
/**
* Unique key prefix
*
* @const string
*/
const PREFIX = 'eddrc_';
/**
* Class constructor
*
* @access private
*/
private function __construct() {
if ( ! $this->edd_exists() ) {
return;
}
define( 'EDD_RESTRICT_CATEGORIES_URL', plugins_url( '/', __FILE__ ) );
define( 'EDD_RESTRICT_CATEGORIES_DIR', plugin_dir_path( __FILE__ ) );
define( 'EDD_RESTRICT_CATEGORIES_INC_DIR', EDD_RESTRICT_CATEGORIES_DIR . 'includes/' );
add_action( 'plugins_loaded', function() {
foreach ( glob( EDD_RESTRICT_CATEGORIES_INC_DIR . '*.php' ) as $include ) {
if ( is_readable( $include ) ) {
require_once $include;
}
}
} );
/**
* Register the taxonomy slugs where restriction will be allowed
*
* @since 1.0.0
*
* @param array $taxonomies
*
* @return array
*/
self::$taxonomies = (array) apply_filters( 'eddrc_taxonomies', array( 'download_category', 'download_tag' ) );
/**
* Register the post types where taxonomy restrictions should be honored
*
* @since 1.0.0
*
* @param array $post_types
*
* @return array
*/
self::$post_types = (array) apply_filters( 'eddrc_post_types', array( 'download' ) );
// Enqueue scripts and styles in the admin
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}
/**
* Return an active instance of this class
*
* @access public
* @since 1.0.0
* @static
*
* @return EDD_Restrict_Categories
*/
public static function get_instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Returns true if EDD exists
*
* Looks at the active list of plugins on the site to
* determine if EDD is installed and activated.
*
* @access private
* @since 1.0.0
*
* @return bool
*/
private function edd_exists() {
return in_array( 'easy-digital-downloads/easy-digital-downloads.php', (array) apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
}
/**
* Enqueue scripts and styles in the admin
*
* @action admin_enqueue_scripts
*
* @access public
* @since 1.0.0
*
* @return void
*/
public function admin_enqueue_scripts() {
$screen = get_current_screen();
if ( ! isset( $screen->id ) ) {
return;
}
$screen_ids = array();
foreach ( self::$taxonomies as $taxonomy ) {
$screen_ids[] = 'edit-' . $taxonomy;
}
$screen_ids = implode( ' ', $screen_ids );
if ( false === strpos( $screen_ids, $screen->id ) ) {
return;
}
// Scripts
wp_register_script( 'eddrc-select2', EDD_RESTRICT_CATEGORIES_URL . 'ui/js/select2.full.min.js', array( 'jquery' ), '4.0.0' );
wp_enqueue_script( 'eddrc-admin', EDD_RESTRICT_CATEGORIES_URL . 'ui/js/admin.min.js', array( 'jquery', 'eddrc-select2' ), self::VERSION );
// Styles
wp_register_style( 'eddrc-select2', EDD_RESTRICT_CATEGORIES_URL . 'ui/css/select2.min.css', array(), '4.0.0' );
wp_enqueue_style( 'eddrc-admin', EDD_RESTRICT_CATEGORIES_URL . 'ui/css/admin.min.css', array( 'eddrc-select2' ), self::VERSION );
}
/**
* Return an array of user role labels
*
* @access public
* @since 1.0.0
* @static
*
* @return array
*/
public static function get_role_labels() {
$roles = array();
$_roles = new WP_Roles();
foreach ( $_roles->get_names() as $role => $label ) {
$roles[ $role ] = translate_user_role( $label );
}
return (array) $roles;
}
/**
* Return a particular taxonomy label with fallback support
*
* @access public
* @since 1.0.0
* @static
*
* @param string $taxonomy
* @param string $label (optional)
*
* @return string
*/
public static function get_tax_label( $taxonomy, $label = 'name' ) {
if ( false === ( $taxonomy = get_taxonomy( $taxonomy ) ) ) {
return (string) $taxonomy;
}
$labels = get_taxonomy_labels( $taxonomy );
$output = ! empty( $labels->$label ) ? $labels->$label : ( ! empty( $labels->name ) ? $labels->name : $taxonomy );
return (string) $output;
}
}
/**
* Instantiate the plugin instance
*
* @global EDD_Restrict_Categories
*/
$GLOBALS['edd_restrict_categories'] = EDD_Restrict_Categories::get_instance();