PluginProbe
Cooked – Recipe Management / 1.11.4
Cooked – Recipe Management v1.11.4
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.11.4, at includes/class.cooked-post-types.php

524 lines 22.1 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( 'wp_head', [&$this, 'cooked_meta_tags'], 5 );
29 add_action( 'manage_cp_recipe_posts_custom_column', [&$this, 'custom_columns_data'], 10, 2 );
30
31 add_filter( 'enter_title_here', [&$this, 'change_new_recipe_title'] );
32 add_filter( 'query_vars', [&$this, 'add_query_vars_filter'] );
33 add_filter( 'manage_cp_recipe_posts_columns', [&$this, 'custom_columns'] );
34 add_filter( 'nav_menu_css_class', [&$this, 'cooked_nav_classes'], 10, 2 );
35 add_filter( 'redirect_canonical', [&$this, 'disable_canonical_redirect'], 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 static function activation() {
187 self::init();
188 self::init_roles();
189 flush_rewrite_rules();
190 }
191
192 public static function init_roles() {
193 // Clean up for any old caps or caps that were inserted incorrectly.
194 if ( $role_object = get_role( 'subscriber' ) ) {
195 if ( $role_object->has_cap( 'edit_cp_recipes' ) ) {
196 Cooked_Roles::clean_caps();
197 }
198 }
199
200 Cooked_Roles::add_roles();
201 Cooked_Roles::add_caps();
202 }
203
204 public static function init() {
205 $_cooked_settings = Cooked_Settings::get();
206 $_cooked_taxonomies = Cooked_Taxonomies::get();
207
208 $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 );
209
210 // Security check: Only allow settings update from admin area with proper permissions
211 if (!empty($_GET['settings-updated']) && is_admin() && current_user_can('manage_options') && isset($_GET['page']) && $_GET['page'] === 'cooked_settings') {
212 // Recipe Permalink
213 $permalink_parts = explode( '/', $_cooked_settings['recipe_permalink'] );
214 if ( isset( $permalink_parts[1] ) ):
215 foreach ( $permalink_parts as $key => $part ):
216 $part = sanitize_title_with_dashes( $part, null, 'save');
217 $permalink_parts[$key] = sanitize_title_with_dashes( $part, null, 'save');
218 endforeach;
219 $recipe_permalink = implode( '/', $permalink_parts );
220 else:
221 $recipe_permalink = sanitize_title_with_dashes( $_cooked_settings['recipe_permalink'], null, 'save');
222 endif;
223
224 // Recipe Author Permalink
225 $permalink_parts = explode( '/', $_cooked_settings['recipe_author_permalink'] );
226 if ( isset( $permalink_parts[1] ) ):
227 foreach( $permalink_parts as $key => $part ):
228 $part = sanitize_title_with_dashes( $part, null, 'save');
229 $permalink_parts[$key] = sanitize_title_with_dashes( $part, null, 'save');
230 endforeach;
231 $recipe_author_permalink = implode( '/', $permalink_parts );
232 else:
233 $recipe_author_permalink = sanitize_title_with_dashes( $_cooked_settings['recipe_author_permalink'], null, 'save');
234 endif;
235
236 // Recipe Category Permalink
237 $permalink_parts = explode( '/', $_cooked_settings['recipe_category_permalink'] );
238 if ( isset( $permalink_parts[1] ) ):
239 foreach ( $permalink_parts as $key => $part ):
240 $part = sanitize_title_with_dashes( $part, null, 'save');
241 $permalink_parts[$key] = sanitize_title_with_dashes( $part, null, 'save');
242 endforeach;
243 $recipe_category_permalink = implode( '/', $permalink_parts );
244 else:
245 $recipe_category_permalink = sanitize_title_with_dashes( $_cooked_settings['recipe_category_permalink'], null, 'save');
246 endif;
247
248 $taxonomy_settings_update = apply_filters( 'cooked_taxonomy_settings_update', [
249 'recipe_permalink' => (!$_cooked_settings['recipe_permalink'] ? 'recipes' : $recipe_permalink),
250 'recipe_author_permalink' => (!$_cooked_settings['recipe_author_permalink'] || 'author' == $_cooked_settings['recipe_author_permalink'] ? 'recipe-author' : $recipe_author_permalink),
251 'recipe_category_permalink' => (!$_cooked_settings['recipe_category_permalink'] ? 'recipe-category' : $recipe_category_permalink)
252 ]);
253
254 foreach ( $taxonomy_settings_update as $setting_key => $setting_value ) {
255 $_cooked_settings[ $setting_key ] = $setting_value;
256 }
257
258 update_option( 'cooked_settings', $_cooked_settings );
259 update_option( 'cooked_settings_saved', true );
260
261 flush_rewrite_rules();
262 }
263
264 // Get base path - either parent page slug or empty
265 $base_path = $parent_page_slug ? $parent_page_slug . '/' : '';
266
267 global $cooked_taxonomies_for_menu;
268
269 if ( !empty($_cooked_taxonomies) ) {
270 foreach ( $_cooked_taxonomies as $slug => $args ) {
271 register_taxonomy( $slug, ['cp_recipe'], $args );
272 add_rewrite_tag("%{$slug}%", '([^/]+)');
273
274 // Taxonomy search sort pagination
275 add_rewrite_rule(
276 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/search/([^/]*)/sort/([^/]*)/page/([^/]*)/?',
277 '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]',
278 'top'
279 );
280
281 // Taxonomy search sort
282 add_rewrite_rule(
283 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/search/([^/]*)/sort/([^/]*)/?',
284 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_search_s=$matches[2]&cooked_browse_sort_by=$matches[3]',
285 'top'
286 );
287
288 // Taxonomy sort pagination
289 add_rewrite_rule(
290 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/sort/([^/]*)/page/([^/]*)/?',
291 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_browse_sort_by=$matches[2]&paged=$matches[3]',
292 'top'
293 );
294
295 // Taxonomy sort
296 add_rewrite_rule(
297 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/sort/([^/]*)/?',
298 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_browse_sort_by=$matches[2]',
299 'top'
300 );
301
302 // Taxonomy search
303 add_rewrite_rule(
304 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/search/([^/]*)/?',
305 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]&cooked_search_s=$matches[2]',
306 'top'
307 );
308
309 // Taxonomy pagination
310 add_rewrite_rule(
311 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/page/([^/]*)/?',
312 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&paged=$matches[2]&' . $slug . '=$matches[1]',
313 'top'
314 );
315
316 // Taxonomy
317 add_rewrite_rule(
318 '^' . $base_path . $args['rewrite']['slug'] . '/([^/]*)/?',
319 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&' . $slug . '=$matches[1]',
320 'top'
321 );
322
323 $cooked_taxonomies_for_menu[] = [
324 'menu' => 'cooked_recipes_menu',
325 'name' => $args['labels']['menu_name'],
326 'capability' => 'manage_categories',
327 'url' => 'edit-tags.php?taxonomy=' . $slug . '&post_type=cp_recipe'
328 ];
329 }
330 }
331
332 // Search sort pagination
333 add_rewrite_rule(
334 '^' . $base_path . 'search/([^/]*)/sort/([^/]*)/page/([^/]*)/?',
335 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_search_s=$matches[1]&cooked_browse_sort_by=$matches[2]&paged=$matches[3]',
336 'top'
337 );
338
339 // Search sort
340 add_rewrite_rule(
341 '^' . $base_path . 'search/([^/]*)/sort/([^/]*)/?',
342 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_search_s=$matches[1]&cooked_browse_sort_by=$matches[2]',
343 'top'
344 );
345
346 // Sort Pagination
347 add_rewrite_rule(
348 '^' . $base_path . 'sort/([^/]*)/page/([^/]*)/?',
349 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_browse_sort_by=$matches[1]&paged=$matches[2]',
350 'top'
351 );
352
353 // Sort
354 add_rewrite_rule(
355 '^' . $base_path . 'sort/([^/]*)/?',
356 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_browse_sort_by=$matches[1]',
357 'top'
358 );
359
360 // Search
361 add_rewrite_rule(
362 '^' . $base_path . 'search/([^/]*)/?',
363 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&cooked_search_s=$matches[1]',
364 'top'
365 );
366
367 // Pagination
368 add_rewrite_rule(
369 '^' . $base_path . 'page/([^/]*)/?',
370 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&paged=$matches[1]',
371 'top'
372 );
373
374 // Plain
375 add_rewrite_rule(
376 '^' . $parent_page_slug . '/?$',
377 'index.php?page_id=' . $_cooked_settings['browse_page'],
378 'top'
379 );
380
381 add_rewrite_tag('%cooked_search_s%', '([^&]+)');
382 add_rewrite_tag('%cooked_browse_sort_by%', '([^&]+)');
383
384 $post_types = self::get();
385 if ( !empty($post_types) ) {
386 foreach ( $post_types as $slug => $args ) {
387 register_post_type( $slug, $args );
388 }
389 }
390 }
391
392 public static function image_sizes() {
393 add_image_size( 'cooked-profile-photo', 150, 150, true );
394 add_image_size( 'cooked-square', 700, 700, true );
395 add_image_size( 'cooked-medium', 700, 525, true );
396 add_image_size( 'cooked-large', 2000, 2000 );
397 }
398
399 public static function get() {
400 global $_cooked_settings;
401
402 $recipe_permalink = isset($_cooked_settings['recipe_permalink']) && $_cooked_settings['recipe_permalink'] ? $_cooked_settings['recipe_permalink'] : 'recipes';
403 $public_recipes = true;
404 $has_archive_slug = sanitize_title_with_dashes( __('Recipe Archive', 'cooked') );
405 $exclude_from_search = false;
406
407 if ( !isset($_GET['print']) && isset( $_cooked_settings['advanced'] ) && in_array( 'disable_public_recipes', $_cooked_settings['advanced'] ) ) {
408 $public_recipes = false;
409 $has_archive_slug = false;
410 $exclude_from_search = true;
411 }
412
413 if ( isset( $_cooked_settings['advanced'] ) && in_array( 'disable_cp_recipe_archive', $_cooked_settings['advanced'] ) ) {
414 $has_archive_slug = false;
415 }
416
417 $post_types = apply_filters( 'cooked_post_types', [
418 'cp_recipe' => [
419 'labels' => [
420 'name' => _x('Recipes', 'cooked'),
421 'singular_name' => _x('Recipe', 'cooked'),
422 'menu_name' => __('Recipes', 'cooked'),
423 'name_admin_bar' => __('Recipe', 'cooked'),
424 'add_new' => __('Add New', 'cooked'),
425 'add_new_item' => __('Add New Recipe', 'cooked'),
426 'new_item' => __('New Recipe', 'cooked'),
427 'edit_item' => __('Edit Recipe', 'cooked'),
428 'view_item' => __('View Recipe', 'cooked'),
429 'all_items' => __('All Recipes', 'cooked'),
430 'search_items' => __('Search Recipes', 'cooked'),
431 'not_found' => __('No recipes found.', 'cooked'),
432 'not_found_in_trash' => __('No recipes found in trash.', 'cooked')
433 ],
434 'description' => __('Recipes', 'cooked'),
435 'public' => $public_recipes,
436 'show_ui' => true,
437 'show_in_admin_bar' => true,
438 'show_in_menu' => 'cooked_recipes_menu',
439 'show_in_rest' => true,
440 'rest_base' => 'cooked_recipe',
441 'rest_controller_class' => 'WP_REST_Posts_Controller',
442 'exclude_from_search' => $exclude_from_search,
443 'has_archive' => $has_archive_slug,
444 'menu_position' => 25,
445 'supports' => ['title', 'editor', 'thumbnail', 'comments', 'author'],
446 'rewrite' => [
447 'with_front' => false,
448 'slug' => $recipe_permalink
449 ]
450 ]
451 ]
452 );
453
454 return $post_types;
455 }
456
457 public function change_new_recipe_title($title) {
458 $screen = get_current_screen();
459 if ('cp_recipe' == $screen->post_type) {
460 $title = __('Recipe title ...', 'cooked');
461 }
462
463 return $title;
464 }
465
466 /**
467 * Add a post display state for special Cooked pages in the page list table.
468 *
469 * @param array $post_states An array of post display states.
470 * @param WP_Post $post The current post object.
471 */
472 public function add_display_post_states( $post_states, $post ) {
473 global $_cooked_settings;
474
475 $browse_page_id = !empty($_cooked_settings['browse_page']) ? $_cooked_settings['browse_page'] : false;
476
477 if ( $browse_page_id == $post->ID ) {
478 $post_states['cooked_page_for_browse_recipes'] = __( 'Cooked Browse Recipes Page', 'cooked' );
479 }
480
481 return $post_states;
482 }
483
484 /**
485 * Disable canonical redirects for Cooked URLs on the homepage
486 *
487 * @param string $redirect_url The redirect URL
488 * @param string $requested_url The originally requested URL
489 * @return string|bool The redirect URL or false to prevent redirect
490 */
491 public function disable_canonical_redirect($redirect_url, $requested_url) {
492 global $_cooked_settings;
493 $_cooked_taxonomies = Cooked_Taxonomies::get();
494
495 // Only process if this is the homepage
496 if (!is_front_page()) {
497 return $redirect_url;
498 }
499
500 // Check if any Cooked query vars are present
501 $cooked_query_vars = [
502 'cooked_search_s',
503 'cooked_browse_sort_by',
504 'paged'
505 ];
506
507 // Add taxonomy query vars
508 if (!empty($_cooked_taxonomies)) {
509 foreach ( $_cooked_taxonomies as $slug => $args ) {
510 $cooked_query_vars[] = $slug;
511 }
512 }
513
514 foreach ($cooked_query_vars as $var) {
515 if (get_query_var($var)) {
516 return false;
517 }
518 }
519
520 return $redirect_url;
521 }
522
523 }
524