-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-beyond-legacy-resources.php
More file actions
380 lines (331 loc) · 11.4 KB
/
Copy pathclass-beyond-legacy-resources.php
File metadata and controls
380 lines (331 loc) · 11.4 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Plugin Name: Resource Post Type by Volkan
* Description: This plugin adds a Resource Post Type to your WordPress website.
* Version: 1.0.0
* Author: Samuel Nzaro
* Author URI: https://github.com/mevolkan
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Beyond_Legacy_Resources' ) ) {
class Beyond_Legacy_Resources {
}
}
/**
* Registers a custom post type 'resource'.
*
* @since 1.0.0
*
* @return void
*/
function bl_register_resource(): void {
$labels = array(
'name' => _x( 'Resource', 'Post Type General Name', 'resource' ),
'singular_name' => _x( 'Resource', 'Post Type Singular Name', 'resource' ),
'menu_name' => __( 'Resources', 'resource' ),
'name_admin_bar' => __( 'Resources', 'resource' ),
'archives' => __( 'Resources Archives', 'resource' ),
'attributes' => __( 'Resources Attributes', 'resource' ),
'parent_item_colon' => __( 'Parent Resource:', 'resource' ),
'all_items' => __( 'All Resources', 'resource' ),
'add_new_item' => __( 'Add New Resource', 'resource' ),
'add_new' => __( 'Add New', 'resource' ),
'new_item' => __( 'New Resource', 'resource' ),
'edit_item' => __( 'Edit Resource', 'resource' ),
'update_item' => __( 'Update Resource', 'resource' ),
'view_item' => __( 'View Resource', 'resource' ),
'view_items' => __( 'View Resources', 'resource' ),
'search_items' => __( 'Search Resources', 'resource' ),
'not_found' => __( 'Resource Not Found', 'resource' ),
'not_found_in_trash' => __( 'Resource Not Found in Trash', 'resource' ),
'featured_image' => __( 'Featured Image', 'resource' ),
'set_featured_image' => __( 'Set Featured Image', 'resource' ),
'remove_featured_image' => __( 'Remove Featured Image', 'resource' ),
'use_featured_image' => __( 'Use as Featured Image', 'resource' ),
'insert_into_item' => __( 'Insert into Resource', 'resource' ),
'uploaded_to_this_item' => __( 'Uploaded to this Resource', 'resource' ),
'items_list' => __( 'Resources List', 'resource' ),
'items_list_navigation' => __( 'Resources List Navigation', 'resource' ),
'filter_items_list' => __( 'Filter Resources List', 'resource' ),
);
$labels = apply_filters( 'resource_labels', $labels );
$args = array(
'label' => __( 'Resource', 'resource' ),
'description' => __( 'Resource Description', 'resource' ),
'labels' => $labels,
'supports' => array(
'title',
'editor',
'author',
'excerpt',
'thumbnail',
'revisions',
),
'taxonomies' => array(
'category',
'post_tag',
),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-media-text',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'can_export' => true,
'capability_type' => 'page',
'show_in_rest' => true,
'rest_base' => 'resource',
);
$args = apply_filters( 'resource_args', $args );
register_post_type( 'resource', $args );
}
function add_url_metabox() {
add_meta_box( 'resource_url_metabox', 'Resource URL', 'render_resource_url_metabox', 'resource', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'add_url_metabox' );
function render_resource_url_metabox( $post ) {
$resource_url = get_post_meta( $post->ID, '_resource_url', true );
?>
<label for="resource-url">Enter a Custom URL:</label>
<input type="text" id="resource-url" name="resource_url" value="<?php echo esc_attr( $resource_url ); ?>">
or Pick from Media Library:
<input type="button" id="upload-media-button" class="button" value="Pick from Media Library">
<?php
}
function save_resource_url_metabox( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['resource_url'] ) ) {
update_post_meta( $post_id, '_resource_url', sanitize_text_field( wp_unslash( $_POST['resource_url'] ) ) );
}
}
add_action( 'save_post', 'save_resource_url_metabox' );
add_action( 'admin_enqueue_scripts', 'load_wp_media_files' );
function load_wp_media_files() {
// change to the $page where you want to enqueue the script
// if( $page == 'options-general.php' ) {
wp_enqueue_media();
wp_enqueue_script( 'resources_script', plugins_url( '/js/mediafiles.js', __FILE__ ), array( 'jquery' ), '0.1' );
// }
}
function add_resource_price_meta_box() {
add_meta_box( 'resource_price_meta', 'Resource Price', 'render_resource_price_meta_box', 'resource', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'add_resource_price_meta_box' );
function render_resource_price_meta_box( $post ) {
// Retrieve the saved price, if any
$resource_price = get_post_meta( $post->ID, '_resource_price', true );
?>
<label for="resource-price">Resource Price:</label>
<input type="text" id="resource-price" name="resource_price" value="<?php echo esc_attr( $resource_price ); ?>">
<?php
}
function save_resource_price_meta_box( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['resource_price'] ) ) {
update_post_meta( $post_id, '_resource_price', sanitize_text_field( wp_unslash( $_POST['resource_price'] ) ) );
}
}
add_action( 'save_post', 'save_resource_price_meta_box' );
/* Filter the single_template with our custom function*/
function get_template_path( $template, $type ) {
$theme_file = locate_template( array( $type . '-resource.php' ) );
if ( $theme_file ) {
return $theme_file;
} else {
$plugin_template = plugin_dir_path( __FILE__ ) . '/includes/templates/' . $type . '-resource.php';
if ( file_exists( $plugin_template ) ) {
return $plugin_template;
}
}
return $template;
}
function load_resource_single_template( $template ) {
if ( is_single() && get_post_type() === 'resource' ) {
return get_template_path( $template, 'single' );
}
return $template;
}
add_filter( 'template_include', 'load_resource_single_template' );
function load_resource_archive_template( $template ) {
if ( is_post_type_archive( 'resource' ) ) {
return get_template_path( $template, 'archive' );
}
return $template;
}
add_filter( 'template_include', 'load_resource_archive_template' );
add_action( 'init', 'bl_register_resource', 0 );
function resource_user_scripts() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'resource_style', $plugin_url . '/css/styles.css' );
}
add_action( 'wp_enqueue_scripts', 'resource_user_scripts' );
function resource_loop_shortcode( $atts ) {
global $wp_query;
ob_start();
// Define default values for attributes.
$atts = shortcode_atts(
array(
'count' => 6,
// posts per page
'limit' => 6,
'columns' => 3,
),
$atts
);
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'resource',
'posts_per_page' => $atts['count'],
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
echo '<div class="row resources">';
while ( $loop->have_posts() ) {
$loop->the_post();
if ( '' === locate_template( 'includes/templates/partials/loop.php', true, false ) ) {
include 'includes/templates/partials/loop.php';
}
get_template_part( 'includes/templates/partials/loop' );
}
echo '</div>';
echo '<div class="pagination">';
$big = 999999999;
echo paginate_links(
array(
// 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'total' => $loop->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'prev_text' => 'Previous',
'next_text' => 'Next',
)
);
echo '</div>';
}
wp_reset_postdata();
return ob_get_clean(); // Return the buffered output
wp_reset_query();
}
add_shortcode( 'resource-loop', 'resource_loop_shortcode' );
function add_resources_page_template( $templates ) {
$templates[ plugin_dir_path( __FILE__ ) . 'includes/templates/resources-template.php' ] = __( 'Resource Page Template', 'resource' );
return $templates;
}
add_filter( 'theme_page_templates', 'add_resources_page_template' );
function combined_resources_loop( $atts ) {
global $wp_query;
ob_start(); // Start output buffering
$atts = shortcode_atts(
array(
'count' => 12,
// posts per page
'limit' => 12,
'columns' => 3,
),
$atts
);
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => array( 'resource', 'post' ),
'posts_per_page' => $atts['count'],
'paged' => $paged,
);
$query = new WP_Query( $args );
include 'includes/templates/partials/resources-filter.php';
if ( $query->have_posts() ) {
echo '<div class="row resources filtered-results">';
while ( $query->have_posts() ) {
$query->the_post();
if ( '' === locate_template( 'includes/templates/partials/loop.php', true, false ) ) {
include 'includes/templates/partials/loop.php';
}
get_template_part( 'includes/templates/partials/loop' );
}
echo '</div>';
echo '<div class="pagination">';
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
// 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'prev_text' => 'Previous',
'next_text' => 'Next',
)
);
echo '</div>';
}
wp_reset_postdata();
return ob_get_clean(); // Return the buffered output
wp_reset_query();
}
add_shortcode( 'combined-loop', 'combined_resources_loop' );
// Ajax Filter
function filter_resources() {
$filter = sanitize_text_field( $_POST['filter'] );
if ( in_array( $filter, array( 'all', 'blogs' ) ) ) {
$args = array(
'post_type' => ( $filter === 'all' ) ? array( 'post', 'resource' ) : ( $filter === 'blogs' ? 'post' : $filter ),
'posts_per_page' => -1,
);
}
if ( in_array( $filter, array( 'audio', 'ebook', 'video' ) ) ) {
$tax_query[] = array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $filter,
),
);
$args = array(
'post_type' => array( 'resource', 'post' ),
'posts_per_page' => -1,
'tax_query' => $tax_query,
);
}
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
if ( '' === locate_template( 'includes/templates/partials/loop.php', true, false ) ) {
include 'includes/templates/partials/loop.php';
}
get_template_part( 'includes/templates/partials/loop' );
}
} else {
echo 'No Resource found';
}
wp_die();
}
add_action( 'wp_ajax_filter_resources', 'filter_resources' );
add_action( 'wp_ajax_nopriv_filter_resources', 'filter_resources' );
function localize_ajax_url() {
wp_enqueue_script( 'resources_script', plugins_url( '/js/resources-filter.js', __FILE__ ), array( 'jquery' ), '0.1' );
wp_localize_script(
'resources_script',
'ajaxUrl',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'localize_ajax_url' );