PluginProbe
Cooked – Recipe Management / 1.9.5
Cooked – Recipe Management v1.9.5
1.16.1 1.16.0 1.15.0 trunk 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.12.0 1.13.0 1.14.0 1.7.10 1.7.11 1.7.12 1.7.13 1.7.15.1 1.7.15.3 1.7.15.4 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 All 37 releases
cooked / includes / class.cooked-post-types.php

class.cooked-post-types.php in Cooked – Recipe Management 1.9.5, at includes/class.cooked-post-types.php

516 lines 22.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Types
4 *
5 * @package Cooked
6 * @subpackage Post Types
7 * @since 1.0.0
8 */
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 /**
14 * Cooked_Post_Types Class
15 *
16 * This class handles the post type creation.
17 *
18 * @since 1.0.0
19 */
20 class Cooked_Post_Types {
21
22 function __construct() {
23 register_activation_hook( COOKED_PLUGIN_FILE, [&$this, 'activation'] );
24
25 add_action( 'init', [&$this, 'init'] );
26 add_filter( 'admin_init', [&$this, 'init_roles'] );
27 add_action( 'after_setup_theme', [&$this, 'image_sizes'] );
28 // add_action( 'template_redirect', [&$this, 'redirects'] );
29 add_action( 'wp_head', [&$this, 'cooked_meta_tags'], 5 );
30 add_action( 'manage_cp_recipe_posts_custom_column', [&$this, 'custom_columns_data'], 10, 2 );
31
32 add_filter( 'enter_title_here', [&$this, 'change_new_recipe_title'] );
33 add_filter( 'query_vars', [&$this, 'add_query_vars_filter'] );
34 add_filter( 'manage_cp_recipe_posts_columns', [&$this, 'custom_columns'] );
35 add_filter( 'nav_menu_css_class', [&$this, 'cooked_nav_classes'], 10, 2 );
36
37 // Taxonomy Titles
38 add_action( 'template_redirect', [&$this, 'remove_default_title_tag'] );
39 add_filter( 'the_title', [&$this, 'taxonomy_page_title'], 10, 2 );
40 add_filter( 'pre_wp_nav_menu', [&$this, 'disable_taxonomy_page_title'], 10, 2 );
41 add_filter( 'wp_nav_menu_items', [&$this, 'enable_taxonomy_page_title'], 10, 2 );
42 add_filter( 'wp_title', [&$this, 'taxonomy_meta_title'], 10 );
43
44 // Add a post display state for special pages.
45 add_filter( 'display_post_states', [&$this, 'add_display_post_states' ], 10, 2 );
46 }
47
48 function disable_taxonomy_page_title( $nav_menu, $args ) {
49 remove_filter( 'the_title', [&$this, 'taxonomy_page_title'], 10, 2 );
50 return $nav_menu;
51 }
52
53 function enable_taxonomy_page_title( $items, $args ) {
54 add_filter( 'the_title', [&$this, 'taxonomy_page_title'], 10, 2 );
55 return $items;
56 }
57
58 function taxonomy_page_title( $title = '', $id = 0 ) {
59 if ( is_admin() ) return $title;
60
61 global $wp_query, $post, $_cooked_settings;
62 $browse_page_id = !empty($_cooked_settings['browse_page']) ? $_cooked_settings['browse_page'] : false;
63
64 if ( is_page( $browse_page_id ) && $id == $browse_page_id && isset($wp_query->query['cp_recipe_category']) && taxonomy_exists('cp_recipe_category') && term_exists( $wp_query->query['cp_recipe_category'], 'cp_recipe_category' ) ):
65 $cooked_term = get_term_by( 'slug', $wp_query->query['cp_recipe_category'], 'cp_recipe_category' );
66 return $cooked_term->name;
67 endif;
68
69 return $title;
70 }
71
72 function taxonomy_meta_title( $title = '' ) {
73 global $wp_query, $post, $_cooked_settings;
74 $browse_page_id = !empty($_cooked_settings['browse_page']) ? $_cooked_settings['browse_page'] : false;
75
76 if ( is_page( $browse_page_id ) && $post->ID == $browse_page_id && isset($wp_query->query['cp_recipe_category']) && taxonomy_exists('cp_recipe_category') && term_exists( $wp_query->query['cp_recipe_category'], 'cp_recipe_category' ) ):
77 $cooked_term = get_term_by( 'slug', $wp_query->query['cp_recipe_category'], 'cp_recipe_category' );
78 return $cooked_term->name;
79 endif;
80
81 return $title;
82 }
83
84 function custom_columns( $columns ) {
85 $new_columns = [];
86 foreach( $columns as $key => $val ):
87 $new_columns[$key] = $val;
88 if ( $key == 'cb' ):
89 $new_columns['featured_image'] = __( 'Photo', 'cooked' );
90 endif;
91 endforeach;
92 return $new_columns;
93 }
94
95 function custom_columns_data( $column, $post_id ) {
96 if ( $column == 'featured_image' ):
97 echo '<span class="cooked-admin-recipes-list-image">';
98 echo the_post_thumbnail( 'thumbnail' );
99 echo '</span>';
100 endif;
101 }
102
103 public static function cooked_nav_classes( $classes, $item ) {
104 global $_cooked_settings;
105 $blog_page_id = get_option( 'page_for_posts', false );
106 $browse_page_id = $_cooked_settings['browse_page'];
107
108 if ( ( is_post_type_archive( 'cp_recipe' ) || is_singular( 'cp_recipe' ) )
109 && $item->object_id == $blog_page_id ){
110 $classes = array_diff( $classes, ['current_page_parent'] );
111 }
112
113 if ( ( is_post_type_archive( 'cp_recipe' ) || is_singular( 'cp_recipe' ) )
114 && $item->object_id == $browse_page_id && is_array($classes) && !in_array( 'current_page_parent', $classes ) ){
115 $classes[] = 'current_page_parent';
116 }
117
118 return $classes;
119 }
120
121 public static function cooked_meta_tags() {
122 global $_cooked_settings, $post, $wp_query;
123
124 if ( isset($_cooked_settings['advanced']) && !empty($_cooked_settings['advanced']) && in_array( 'disable_meta_tags', $_cooked_settings['advanced'] ) )
125 return false;
126
127 if ( isset($wp_query->query['cp_recipe_category']) && taxonomy_exists('cp_recipe_category') && term_exists( $wp_query->query['cp_recipe_category'], 'cp_recipe_category' ) ) {
128 $cooked_term = get_term_by( 'slug', $wp_query->query['cp_recipe_category'], 'cp_recipe_category' );
129 }
130
131 // Browse page.
132 if ( isset( $cooked_term ) && $cooked_term->name ) {
133 ?><title><?php echo esc_html($cooked_term->name) . ' / ' . esc_html(get_bloginfo('name')); ?></title>
134 <meta name="description" content="<?php echo esc_attr( $cooked_term->description ); ?>">
135 <meta property="og:title" content="<?php echo esc_attr( $cooked_term->name ); ?>">
136 <meta property="og:description" content="<?php echo esc_attr( $cooked_term->description ); ?>"><?php
137 }
138
139 // Single recipe.
140 if ( isset( $post->post_type ) && $post->post_type == 'cp_recipe' ) {
141 ob_start();
142
143 $recipe = get_post( $post->ID );
144 $recipe_settings = Cooked_Recipes::get( $post->ID, true );
145 $image_url = false;
146
147 if ( has_post_thumbnail($recipe) ) {
148 $image_url = get_the_post_thumbnail_url( $recipe, 'cooked-large' );
149 }
150
151 $description = '';
152 if (!empty($recipe_settings['seo_description'])):
153 $description = wp_strip_all_tags( preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $recipe_settings['seo_description']) ); ;
154 elseif (!empty($recipe_settings['excerpt'])):
155 $description = wp_strip_all_tags( preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $recipe_settings['excerpt']) );
156 elseif (!empty($recipe_settings['title'])):
157 $description = $recipe_settings['title'];
158 endif;
159 ?>
160
161 <meta name="description" content="<?php echo esc_attr( $description ); ?>">
162 <meta property="og:type" content="website">
163 <meta property="og:title" content="<?php echo esc_attr( $post->post_title ); ?>">
164 <meta property="og:description" content="<?php echo esc_attr( $description ); ?>">
165 <meta property="og:image" content="<?php echo esc_attr( $image_url ); ?>">
166 <meta property="og:locale" content="<?php echo esc_attr( get_locale() ); ?>">
167 <meta property="og:url" content="<?php echo get_permalink( $post->ID ); ?>"><?php
168
169 echo ob_get_clean();
170 }
171 }
172
173 public static function add_query_vars_filter( $vars ) {
174 $vars[] = 'servings';
175
176 return $vars;
177 }
178
179 public function remove_default_title_tag() {
180 global $wp_query;
181 if ( isset($wp_query->query['cp_recipe_category']) && taxonomy_exists('cp_recipe_category') && term_exists( $wp_query->query['cp_recipe_category'], 'cp_recipe_category' ) ) {
182 remove_action( 'wp_head', '_wp_render_title_tag', 1 );
183 }
184 }
185
186 public function redirects() {
187 $_cooked_settings = Cooked_Settings::get();
188 $parent_page = isset($_cooked_settings['browse_page']) && $_cooked_settings['browse_page'] ? $_cooked_settings['browse_page'] : false;
189 $front_page = get_option( 'page_on_front' );
190
191 if ( $parent_page ):
192 if ( is_post_type_archive('cp_recipe') && !is_feed() ):
193 if ( wp_redirect( get_permalink( $parent_page ) ) ):
194 exit;
195 endif;
196 elseif ( is_tax('cp_recipe_category') ):
197 global $wp_query;
198 if ( isset($wp_query->query['cp_recipe_category']) && taxonomy_exists('cp_recipe_category') && term_exists( $wp_query->query['cp_recipe_category'], 'cp_recipe_category' )
199 || isset($wp_query->query['taxonomy']) && $wp_query->query['taxonomy'] == 'cp_recipe_category' && taxonomy_exists('cp_recipe_category') && term_exists( $wp_query->query['term'], 'cp_recipe_category' ) ):
200 if ( $parent_page != $front_page && get_option('permalink_structure') ):
201 if ( wp_redirect( esc_url_raw( untrailingslashit( get_permalink( $parent_page ) ) . '/' . $_cooked_settings['recipe_category_permalink'] . '/' . ( isset( $wp_query->query['term'] ) ? $wp_query->query['term'] : $wp_query->query['cp_recipe_category'] ) ) ) ):
202 exit;
203 endif;
204 elseif ( $parent_page == $front_page ):
205 if ( wp_redirect( esc_url_raw( get_home_url() . '?cp_recipe_category=' . ( isset( $wp_query->query['term'] ) ? $wp_query->query['term'] : $wp_query->query['cp_recipe_category'] ) ) ) ):
206 exit;
207 endif;
208 else:
209 if ( wp_redirect( esc_url_raw( get_permalink( $parent_page ) . '&cp_recipe_category=' . ( isset( $wp_query->query['term'] ) ? $wp_query->query['term'] : $wp_query->query['cp_recipe_category'] ) ) ) ):
210 exit;
211 endif;
212 endif;
213 endif;
214 else:
215 do_action( 'cooked_redirects' );
216 endif;
217 endif;
218 }
219
220 public static function activation() {
221 self::init();
222 self::init_roles();
223 flush_rewrite_rules();
224 }
225
226 public static function init_roles() {
227 // Clean up for any old caps or caps that were inserted incorrectly.
228 if ( $role_object = get_role( 'subscriber' ) ) {
229 if ( $role_object->has_cap( 'edit_cp_recipes' ) ) {
230 Cooked_Roles::clean_caps();
231 }
232 }
233
234 Cooked_Roles::add_roles();
235 Cooked_Roles::add_caps();
236 }
237
238 public static function init() {
239 $_cooked_settings = Cooked_Settings::get();
240 $_cooked_taxonomies = Cooked_Taxonomies::get();
241
242 $parent_page_slug = ( isset($_cooked_settings['browse_page']) && $_cooked_settings['browse_page'] ? ltrim( untrailingslashit( str_replace( home_url(), '', get_permalink( $_cooked_settings['browse_page'] ) ) ), '/' ) : false );
243
244 if (!empty($_GET['settings-updated'])) {
245 // Recipe Permalink
246 $permalink_parts = explode( '/', $_cooked_settings['recipe_permalink'] );
247 if ( isset( $permalink_parts[1] ) ):
248 foreach ( $permalink_parts as $key => $part ):
249 $part = sanitize_title_with_dashes( $part, null, 'save');
250 $permalink_parts[$key] = sanitize_title_with_dashes( $part, null, 'save');
251 endforeach;
252 $recipe_permalink = implode( '/', $permalink_parts );
253 else:
254 $recipe_permalink = sanitize_title_with_dashes( $_cooked_settings['recipe_permalink'], null, 'save');
255 endif;
256
257 // Recipe Author Permalink
258 $permalink_parts = explode( '/', $_cooked_settings['recipe_author_permalink'] );
259 if ( isset( $permalink_parts[1] ) ):
260 foreach( $permalink_parts as $key => $part ):
261 $part = sanitize_title_with_dashes( $part, null, 'save');
262 $permalink_parts[$key] = sanitize_title_with_dashes( $part, null, 'save');
263 endforeach;
264 $recipe_author_permalink = implode( '/', $permalink_parts );
265 else:
266 $recipe_author_permalink = sanitize_title_with_dashes( $_cooked_settings['recipe_author_permalink'], null, 'save');
267 endif;
268
269 // Recipe Category Permalink
270 $permalink_parts = explode( '/', $_cooked_settings['recipe_category_permalink'] );
271 if ( isset( $permalink_parts[1] ) ):
272 foreach ( $permalink_parts as $key => $part ):
273 $part = sanitize_title_with_dashes( $part, null, 'save');
274 $permalink_parts[$key] = sanitize_title_with_dashes( $part, null, 'save');
275 endforeach;
276 $recipe_category_permalink = implode( '/', $permalink_parts );
277 else:
278 $recipe_category_permalink = sanitize_title_with_dashes( $_cooked_settings['recipe_category_permalink'], null, 'save');
279 endif;
280
281 $taxonomy_settings_update = apply_filters( 'cooked_taxonomy_settings_update', [
282 'recipe_permalink' => (!$_cooked_settings['recipe_permalink'] ? 'recipes' : $recipe_permalink),
283 'recipe_author_permalink' => (!$_cooked_settings['recipe_author_permalink'] || 'author' == $_cooked_settings['recipe_author_permalink'] ? 'recipe-author' : $recipe_author_permalink),
284 'recipe_category_permalink' => (!$_cooked_settings['recipe_category_permalink'] ? 'recipe-category' : $recipe_category_permalink)
285 ]);
286
287 foreach ( $taxonomy_settings_update as $setting_key => $setting_value ) {
288 $_cooked_settings[ $setting_key ] = $setting_value;
289 }
290
291 update_option( 'cooked_settings', $_cooked_settings );
292 update_option( 'cooked_settings_saved', true );
293
294 flush_rewrite_rules();
295 }
296
297 // Get base path - either parent page slug or empty
298 $base_path = $parent_page_slug ? $parent_page_slug . '/' : '';
299
300 global $cooked_taxonomies_for_menu;
301
302 if ( !empty($_cooked_taxonomies) ) {
303 foreach ( $_cooked_taxonomies as $slug => $args ) {
304 register_taxonomy( $slug, ['cp_recipe'], $args );
305
306 // Taxonomy search sort pagination
307 add_rewrite_rule(
308 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/search/([^/]*)/sort/([^/]*)/page/([^/]*)/?',
309 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_search_s=$matches[2]&cooked_browse_sort_by=$matches[3]&paged=$matches[4]',
310 'top'
311 );
312
313 // Taxonomy search sort
314 add_rewrite_rule(
315 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/search/([^/]*)/sort/([^/]*)/?',
316 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_search_s=$matches[2]&cooked_browse_sort_by=$matches[3]',
317 'top'
318 );
319
320 // Taxonomy sort pagination
321 add_rewrite_rule(
322 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/sort/([^/]*)/page/([^/]*)/?',
323 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_browse_sort_by=$matches[2]&paged=$matches[3]',
324 'top'
325 );
326
327 // Taxonomy sort
328 add_rewrite_rule(
329 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/sort/([^/]*)/?',
330 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_browse_sort_by=$matches[2]',
331 'top'
332 );
333
334 // Taxonomy search
335 add_rewrite_rule(
336 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/search/([^/]*)/?',
337 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_search_s=$matches[2]',
338 'top'
339 );
340
341 // Taxonomy pagination
342 add_rewrite_rule(
343 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/page/([^/]*)/?',
344 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&paged=$matches[2]&' . $slug . '=$matches[1]',
345 'top'
346 );
347
348 // Taxonomy
349 add_rewrite_rule(
350 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/?',
351 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]',
352 'top'
353 );
354
355 $cooked_taxonomies_for_menu[] = [
356 'menu' => 'cooked_recipes_menu',
357 'name' => $args['labels']['menu_name'],
358 'capability' => 'manage_categories',
359 'url' => 'edit-tags.php?taxonomy=' . $slug . '&post_type=cp_recipe'
360 ];
361 }
362 }
363
364 // Search sort pagination
365 add_rewrite_rule(
366 '^' . $base_path . 'search/([^/]*)/sort/([^/]*)/page/([^/]*)/?',
367 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_search_s=$matches[1]&cooked_browse_sort_by=$matches[2]&paged=$matches[3]',
368 'top'
369 );
370
371 // Search sort
372 add_rewrite_rule(
373 '^' . $base_path . 'search/([^/]*)/sort/([^/]*)/?',
374 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_search_s=$matches[1]&cooked_browse_sort_by=$matches[2]',
375 'top'
376 );
377
378 // Sort Pagination
379 add_rewrite_rule(
380 '^' . $base_path . 'sort/([^/]*)/page/([^/]*)/?',
381 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_browse_sort_by=$matches[1]&paged=$matches[2]',
382 'top'
383 );
384
385 // Sort
386 add_rewrite_rule(
387 '^' . $base_path . 'sort/([^/]*)/?',
388 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_browse_sort_by=$matches[1]',
389 'top'
390 );
391
392 // Search
393 add_rewrite_rule(
394 '^' . $base_path . 'search/([^/]*)/?',
395 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_search_s=$matches[1]',
396 'top'
397 );
398
399 // Pagination
400 add_rewrite_rule(
401 '^' . $base_path . 'page/([^/]*)/?',
402 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&paged=$matches[1]',
403 'top'
404 );
405
406 // Plain
407 add_rewrite_rule(
408 '^' . $parent_page_slug . '/?$',
409 'index.php?page_id=' . $_cooked_settings['browse_page'],
410 'top'
411 );
412
413 add_rewrite_tag('%cooked_search_s%', '([^&]+)');
414 add_rewrite_tag('%cooked_browse_sort_by%', '([^&]+)');
415
416 $post_types = self::get();
417 if ( !empty($post_types) ) {
418 foreach ( $post_types as $slug => $args ) {
419 register_post_type( $slug, $args );
420 }
421 }
422 }
423
424 public static function image_sizes() {
425 add_image_size( 'cooked-square', 700, 700, true );
426 add_image_size( 'cooked-medium', 700, 525, true );
427 add_image_size( 'cooked-large', 2000, 2000 );
428 }
429
430 public static function get() {
431 global $_cooked_settings;
432
433 $recipe_permalink = isset($_cooked_settings['recipe_permalink']) && $_cooked_settings['recipe_permalink'] ? $_cooked_settings['recipe_permalink'] : 'recipes';
434 $public_recipes = true;
435 $has_archive_slug = sanitize_title_with_dashes( __('Recipe Archive', 'cooked') );
436 $exclude_from_search = false;
437
438 if ( !isset($_GET['print']) && isset( $_cooked_settings['advanced'] ) && in_array( 'disable_public_recipes', $_cooked_settings['advanced'] ) ) {
439 $public_recipes = false;
440 $has_archive_slug = false;
441 $exclude_from_search = true;
442 }
443
444 if ( isset( $_cooked_settings['advanced'] ) && in_array( 'disable_cp_recipe_archive', $_cooked_settings['advanced'] ) ) {
445 $has_archive_slug = false;
446 }
447
448 $post_types = apply_filters( 'cooked_post_types', [
449 'cp_recipe' => [
450 'labels' => [
451 'name' => _x('Recipes', 'cooked'),
452 'singular_name' => _x('Recipe', 'cooked'),
453 'menu_name' => __('Recipes', 'cooked'),
454 'name_admin_bar' => __('Recipe', 'cooked'),
455 'add_new' => __('Add New', 'cooked'),
456 'add_new_item' => __('Add New Recipe', 'cooked'),
457 'new_item' => __('New Recipe', 'cooked'),
458 'edit_item' => __('Edit Recipe', 'cooked'),
459 'view_item' => __('View Recipe', 'cooked'),
460 'all_items' => __('All Recipes', 'cooked'),
461 'search_items' => __('Search Recipes', 'cooked'),
462 'not_found' => __('No recipes found.', 'cooked'),
463 'not_found_in_trash' => __('No recipes found in trash.', 'cooked')
464 ],
465 'description' => __('Recipes', 'cooked'),
466 'public' => $public_recipes,
467 'show_ui' => true,
468 'show_in_admin_bar' => true,
469 'show_in_menu' => 'cooked_recipes_menu',
470 'show_in_rest' => true,
471 'rest_base' => 'cooked_recipe',
472 'rest_controller_class' => 'WP_REST_Posts_Controller',
473 'exclude_from_search' => $exclude_from_search,
474 'has_archive' => $has_archive_slug,
475 'menu_position' => 25,
476 'supports' => ['title', 'editor', 'thumbnail', 'comments', 'author'],
477 'rewrite' => [
478 'with_front' => false,
479 'slug' => $recipe_permalink
480 ]
481 ]
482 ]
483 );
484
485 return $post_types;
486 }
487
488 public function change_new_recipe_title($title) {
489 $screen = get_current_screen();
490 if ('cp_recipe' == $screen->post_type) {
491 $title = __('Recipe title ...', 'cooked');
492 }
493
494 return $title;
495 }
496
497 /**
498 * Add a post display state for special Cooked pages in the page list table.
499 *
500 * @param array $post_states An array of post display states.
501 * @param WP_Post $post The current post object.
502 */
503 public function add_display_post_states( $post_states, $post ) {
504 global $_cooked_settings;
505
506 $browse_page_id = !empty($_cooked_settings['browse_page']) ? $_cooked_settings['browse_page'] : false;
507
508 if ( $browse_page_id == $post->ID ) {
509 $post_states['cooked_page_for_browse_recipes'] = __( 'Cooked Browse Recipes Page', 'cooked' );
510 }
511
512 return $post_states;
513 }
514
515 }
516