|
1 |
| -<?php |
2 |
| -/** |
3 |
| - * Class RequestProcessor |
4 |
| - * |
5 |
| - * @package WordPress_Custom_Fields_Permalink |
6 |
| - */ |
7 |
| - |
8 |
| -namespace CustomFieldsPermalink; |
9 |
| - |
10 |
| -use WP_Post; |
11 |
| - |
12 |
| -/** |
13 |
| - * Class WP_Permalink generates WordPress permalinks. |
14 |
| - */ |
15 |
| -class WP_Permalink { |
16 |
| - |
17 |
| - /** |
18 |
| - * Post meta provider. |
19 |
| - * |
20 |
| - * @var WP_Post_Meta |
21 |
| - */ |
22 |
| - private $post_meta; |
23 |
| - |
24 |
| - /** |
25 |
| - * WP_Permalink constructor. |
26 |
| - * |
27 |
| - * @param WP_Post_Meta $post_meta Post meta provider. |
28 |
| - */ |
29 |
| - public function __construct( WP_Post_Meta $post_meta ) { |
30 |
| - $this->post_meta = $post_meta; |
31 |
| - } |
32 |
| - |
33 |
| - /** |
34 |
| - * Filters the permalink structure for a post before token replacement occurs.. |
35 |
| - * The pre_post_link filter implementation. |
36 |
| - * |
37 |
| - * @param string $permalink The site's permalink structure. |
38 |
| - * @param WP_Post $post The post in question. |
39 |
| - * @param bool $leavename Whether to keep the post name. |
40 |
| - * |
41 |
| - * @link https://developer.wordpress.org/reference/hooks/pre_post_link/ |
42 |
| - * |
43 |
| - * @return mixed |
44 |
| - */ |
45 |
| - public function link_post( $permalink, $post, $leavename ) { |
46 |
| - return $this->link_rewrite_fields( $permalink, $post ); |
47 |
| - } |
48 |
| - |
49 |
| - /** |
50 |
| - * Filters the permalink for a post of a custom post type. |
51 |
| - * The post_type_link filter implementation. |
52 |
| - * |
53 |
| - * @param string $permalink The post's permalink. |
54 |
| - * @param WP_Post $post The post in question. |
55 |
| - * @param bool $leavename Whether to keep the post name. |
56 |
| - * @param bool $sample Is it a sample permalink. |
57 |
| - * |
58 |
| - * @link https://developer.wordpress.org/reference/hooks/post_type_link/ |
59 |
| - * |
60 |
| - * @return mixed |
61 |
| - */ |
62 |
| - public function link_post_type( $permalink, $post, $leavename, $sample ) { |
63 |
| - return $this->link_rewrite_fields( $permalink, $post ); |
64 |
| - } |
65 |
| - |
66 |
| - /** |
67 |
| - * Rewrites permalink replacing custom fields. |
68 |
| - * |
69 |
| - * @param string $permalink The permalink. |
70 |
| - * @param WP_Post $post The post. |
71 |
| - * |
72 |
| - * @return string |
73 |
| - */ |
74 |
| - private function link_rewrite_fields( $permalink, $post ) { |
75 |
| - $that = $this; |
76 |
| - $replace_callback = function ( $matches ) use ( &$post, &$that ) { |
77 |
| - return $that->link_rewrite_fields_extract( $post, $matches[2] ); |
78 |
| - }; |
79 |
| - return preg_replace_callback( '#(%field_(.*?)%)#', $replace_callback, $permalink ); |
80 |
| - } |
81 |
| - |
82 |
| - /** |
83 |
| - * Extract the metadata value from the post. |
84 |
| - * |
85 |
| - * @param WP_Post $post The post. |
86 |
| - * @param string $field_name The metadata key to extract. |
87 |
| - * |
88 |
| - * @return string |
89 |
| - */ |
90 |
| - public function link_rewrite_fields_extract( $post, $field_name ) { |
91 |
| - $post_meta = $this->post_meta->get_post_meta( $post ); |
92 |
| - if ( ! isset( $post_meta[ $field_name ] ) ) { |
93 |
| - return ''; |
94 |
| - } |
95 |
| - $value = $post_meta[ $field_name ][0]; |
96 |
| - $value = sanitize_title( $value ); |
97 |
| - return $value; |
98 |
| - } |
99 |
| -} |
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class RequestProcessor |
| 4 | + * |
| 5 | + * @package WordPress_Custom_Fields_Permalink |
| 6 | + */ |
| 7 | + |
| 8 | +namespace CustomFieldsPermalink; |
| 9 | + |
| 10 | +use WP_Post; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class WP_Permalink generates WordPress permalinks. |
| 14 | + */ |
| 15 | +class WP_Permalink { |
| 16 | + |
| 17 | + /** |
| 18 | + * Post meta provider. |
| 19 | + * |
| 20 | + * @var WP_Post_Meta |
| 21 | + */ |
| 22 | + private $post_meta; |
| 23 | + |
| 24 | + /** |
| 25 | + * WP_Permalink constructor. |
| 26 | + * |
| 27 | + * @param WP_Post_Meta $post_meta Post meta provider. |
| 28 | + */ |
| 29 | + public function __construct( WP_Post_Meta $post_meta ) { |
| 30 | + $this->post_meta = $post_meta; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Filters the permalink structure for a post before token replacement occurs.. |
| 35 | + * The pre_post_link filter implementation. |
| 36 | + * |
| 37 | + * @param string $permalink The site's permalink structure. |
| 38 | + * @param WP_Post $post The post in question. |
| 39 | + * @param bool $leavename Whether to keep the post name. |
| 40 | + * |
| 41 | + * @link https://developer.wordpress.org/reference/hooks/pre_post_link/ |
| 42 | + * |
| 43 | + * @return mixed |
| 44 | + */ |
| 45 | + public function link_post( $permalink, $post, $leavename ) { |
| 46 | + return $this->link_rewrite_fields( $permalink, $post ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Filters the permalink for a post of a custom post type. |
| 51 | + * The post_type_link filter implementation. |
| 52 | + * |
| 53 | + * @param string $permalink The post's permalink. |
| 54 | + * @param WP_Post $post The post in question. |
| 55 | + * @param bool $leavename Whether to keep the post name. |
| 56 | + * @param bool $sample Is it a sample permalink. |
| 57 | + * |
| 58 | + * @link https://developer.wordpress.org/reference/hooks/post_type_link/ |
| 59 | + * |
| 60 | + * @return mixed |
| 61 | + */ |
| 62 | + public function link_post_type( $permalink, $post, $leavename, $sample ) { |
| 63 | + return $this->link_rewrite_fields( $permalink, $post ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Rewrites permalink replacing custom fields. |
| 68 | + * |
| 69 | + * @param string $permalink The permalink. |
| 70 | + * @param WP_Post $post The post. |
| 71 | + * |
| 72 | + * @return string |
| 73 | + */ |
| 74 | + private function link_rewrite_fields( $permalink, $post ) { |
| 75 | + $that = $this; |
| 76 | + $replace_callback = function ( $matches ) use ( &$post, &$that ) { |
| 77 | + return $that->link_rewrite_fields_extract( $post, $matches[2] ); |
| 78 | + }; |
| 79 | + return preg_replace_callback( '#(%field_(.*?)%)#', $replace_callback, $permalink ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Extract the metadata value from the post. |
| 84 | + * |
| 85 | + * @param WP_Post $post The post. |
| 86 | + * @param string $field_name The metadata key to extract. |
| 87 | + * |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + public function link_rewrite_fields_extract( $post, $field_name ) { |
| 91 | + $post_meta = $this->post_meta->get_post_meta( $post ); |
| 92 | + if ( ! isset( $post_meta[ $field_name ] ) ) { |
| 93 | + return ''; |
| 94 | + } |
| 95 | + $value = $post_meta[ $field_name ][0]; |
| 96 | + $value = sanitize_title( $value ); |
| 97 | + return $value; |
| 98 | + } |
| 99 | +} |
0 commit comments