-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-random-content.php
282 lines (210 loc) · 5.7 KB
/
class-random-content.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
class Endo_Random_Content {
/**
* The ID of this plugin.
*
* @since 0.1.0
* @access private
* @var string $name The ID of this plugin.
*/
private $name;
/**
* The current version of the plugin.
*
* @since 0.1.0
* @access private
* @var string $version The version of the plugin
*/
private $version;
/**
* Initializes the plugin by defining the properties.
*
* @since 0.1.0
*/
public function __construct() {
$this->name = 'random-content';
$this->version = '1.3.1';
}
/**
* Defines the hooks that will register the post type and taxonomy, adds the shortcode, and registers the widget
*
* @since 1.0
*/
public function run() {
add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'init', array( $this, 'register_taxonomy' ) );
add_action( 'init', array( $this, 'plugin_textdomain' ) );
// this shortcode name (random) has been deprecated due to conflicts with other plugins
add_shortcode( 'random', array( &$this, 'shortcode') );
add_shortcode( 'random_content', array( &$this, 'new_shortcode') );
add_action( 'widgets_init' , array( $this, 'register_endo_wrc_widget' ) );
add_filter( 'manage_edit-endo_wrc_group_columns', array( $this, 'add_random_content_group_columns' ) );
add_filter( 'manage_endo_wrc_group_custom_column', array( $this, 'random_content_group_custom_columns' ), 10, 3 );
}
/**
* Loads the localization files for translation
*
* @since 1.2
*/
public function plugin_textdomain() {
load_plugin_textdomain('random-content', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Calls the widget class that extends WP_Widget
*
* @since 1.0
*/
public function register_endo_wrc_widget() {
require_once( plugin_dir_path( __FILE__ ) . 'class-random-content-widget.php' );
register_widget('Endo_WRC_Widget');
}
/**
* Registers the random content post type
*
* @since 0.1.0
*/
public function register_post_type() {
$args = array(
'labels' => array(
'name' => __('Random Content', 'random-content'),
'singular_name' => __('Random Content', 'random-content'),
'add_new' => __('Add New', 'random-content'),
'add_new_item' => __('Add New Random Content', 'random-content'),
'edit_item' => __('Edit Random Content', 'random-content'),
'new_item' => __('Add New Random Content', 'random-content'),
'view_item' => __('View Random Content', 'random-content'),
'search_items' => __('Search', 'random-content'),
'not_found' => __('No Random Content Found', 'random-content'),
'not_found_in_trash' => __('No Random Content Found in Trash', 'random-content')
),
'show_ui' => true,
'supports' => array(
'title',
'editor'
)
);
register_post_type( 'endo_wrc_cpt', $args );
}
/**
* Registers the group taxonomy for the random content post type
*
* @since 0.1.0
*/
public function register_taxonomy() {
register_taxonomy(
'endo_wrc_group',
'endo_wrc_cpt',
array(
'label' => __( 'Group', 'random-content' ),
'hierarchical' => true,
'show_admin_column' => true
)
);
}
/**
* Adds extra column for the ID to the group custom taxonomy
*
* @since 1.0
*/
public function add_random_content_group_columns( $columns ) {
return array_merge( $columns, array( 'group_id' => 'ID' ) );
}
/**
* Adds the group ID to the custom column
*
* @since 1.0
*/
public function random_content_group_custom_columns( $value, $column_name, $id ) {
switch( $column_name ) {
case 'group_id' :
$value = $id;
break;
default:
break;
}
return $value;
}
/**
* Defines random content shortcode
*
* @since 0.3.0
* @deprecated 1.0.0
*/
public function shortcode( $atts ) {
$a = shortcode_atts( array(
'group_id' => '',
'num_posts' => 1,
), $atts );
// if $group_id is set, then filter results by $group_id
if ( !empty( $a['group_id'] ) ) {
$my_query = new WP_Query( array(
'post_type' => 'endo_wrc_cpt',
'posts_per_page' => $a['num_posts'],
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'endo_wrc_group',
'field' => 'id',
'terms' => $a['group_id']
)
)
) );
} else {
// filter through all entries
$my_query = new WP_Query( array(
'post_type' => 'endo_wrc_cpt',
'posts_per_page' => $a['num_posts'],
'orderby' => 'rand'
) );
}
if ( $my_query->have_posts() ) {
$content = "";
while ( $my_query->have_posts() ) : $my_query->the_post();
$content .= apply_filters('the_content', get_the_content() );
endwhile;
} else {
$content = __('No posts found.', 'random-content');
}
wp_reset_postdata();
return $content;
}
/**
* Defines new random content shortcode
*
* @since 1.0
*/
public function new_shortcode( $atts ) {
$a = shortcode_atts( array(
'group_id' => '',
'num_posts' => 1,
), $atts );
$args = array(
'post_type' => 'endo_wrc_cpt',
'posts_per_page' => $a['num_posts'],
'orderby' => 'rand'
);
// if $group_id is set, then filter results by $group_id
if ( !empty( $a['group_id'] ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'endo_wrc_group',
'field' => 'id',
'terms' => $a['group_id']
)
);
}
$random_query = new WP_Query( $args );
ob_start();
if ( $random_query->have_posts() ) {
while ( $random_query->have_posts() ) : $random_query->the_post();
the_content();
endwhile;
} else {
_e('No posts found.', 'random-content');
}
wp_reset_postdata();
$content = ob_get_contents();
ob_end_clean();
return apply_filters( 'rc_content', $content );
}
} // end class