|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class CustomFieldsPermalink |
| 4 | + * |
| 5 | + * @package WordPress_Custom_Fields_Permalink |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Class CustomFieldsPermalink provides the implementation of custom post fields in permalinks. |
| 10 | + */ |
| 11 | +class CustomFieldsPermalink { |
| 12 | + |
| 13 | + const PARAM_CUSTOMFIELD_KEY = 'custom_field_key'; |
| 14 | + const PARAM_CUSTOMFIELD_VALUE = 'custom_field_value'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Do check against meta value or not. |
| 18 | + * |
| 19 | + * @var bool |
| 20 | + */ |
| 21 | + private static $check_custom_field_value = false; |
| 22 | + |
| 23 | + /** |
| 24 | + * Filters the permalink structure for a post before token replacement occurs.. |
| 25 | + * The pre_post_link filter implementation. |
| 26 | + * |
| 27 | + * @param string $permalink The site's permalink structure. |
| 28 | + * @param WP_Post $post The post in question. |
| 29 | + * @param bool $leavename Whether to keep the post name. |
| 30 | + * |
| 31 | + * @link https://developer.wordpress.org/reference/hooks/pre_post_link/ |
| 32 | + * |
| 33 | + * @return mixed |
| 34 | + */ |
| 35 | + public static function link_post( $permalink, $post, $leavename ) { |
| 36 | + return self::link_rewrite_fields( $permalink, $post ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Filters the permalink for a post of a custom post type. |
| 41 | + * The post_type_link filter implementation. |
| 42 | + * |
| 43 | + * @param string $permalink The post's permalink. |
| 44 | + * @param WP_Post $post The post in question. |
| 45 | + * @param bool $leavename Whether to keep the post name. |
| 46 | + * @param bool $sample Is it a sample permalink. |
| 47 | + * |
| 48 | + * @link https://developer.wordpress.org/reference/hooks/post_type_link/ |
| 49 | + * |
| 50 | + * @return mixed |
| 51 | + */ |
| 52 | + public static function link_post_type( $permalink, $post, $leavename, $sample ) { |
| 53 | + return self::link_rewrite_fields( $permalink, $post ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Rewrites permalink replacing custom fields. |
| 58 | + * |
| 59 | + * @param string $permalink The permalink. |
| 60 | + * @param WP_Post $post The post. |
| 61 | + * |
| 62 | + * @return string |
| 63 | + */ |
| 64 | + private static function link_rewrite_fields( $permalink, $post ) { |
| 65 | + $replace_callback = function ( $matches ) use ( &$post ) { |
| 66 | + return CustomFieldsPermalink::link_rewrite_fields_extract( $post, $matches[2] ); |
| 67 | + }; |
| 68 | + |
| 69 | + return preg_replace_callback( '#(%field_(.*?)%)#', $replace_callback, $permalink ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Extract the metadata value from the post. |
| 74 | + * |
| 75 | + * @param WP_Post $post The post. |
| 76 | + * @param string $field_name The metadata key to extract. |
| 77 | + * |
| 78 | + * @return string |
| 79 | + */ |
| 80 | + private static function link_rewrite_fields_extract( $post, $field_name ) { |
| 81 | + $post_meta = get_post_meta( $post->ID ); |
| 82 | + |
| 83 | + if ( ! isset( $post_meta[ $field_name ] ) ) { |
| 84 | + return ''; |
| 85 | + } |
| 86 | + |
| 87 | + $value = $post_meta[ $field_name ][0]; |
| 88 | + |
| 89 | + $value = sanitize_title( $value ); |
| 90 | + |
| 91 | + return $value; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Filters the query variables whitelist before processing. |
| 96 | + * The query_vars filter implementation. |
| 97 | + * |
| 98 | + * @param array $public_query_vars The array of whitelisted query variables. |
| 99 | + * |
| 100 | + * @link https://developer.wordpress.org/reference/hooks/query_vars/ |
| 101 | + * |
| 102 | + * @return mixed |
| 103 | + */ |
| 104 | + public static function register_extra_query_vars( $public_query_vars ) { |
| 105 | + array_push( $public_query_vars, self::PARAM_CUSTOMFIELD_KEY, self::PARAM_CUSTOMFIELD_VALUE ); |
| 106 | + |
| 107 | + return $public_query_vars; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Filters the array of parsed query variables. |
| 112 | + * The request filter implementation. |
| 113 | + * |
| 114 | + * @param array $query_vars The array of requested query variables. |
| 115 | + * |
| 116 | + * @link https://developer.wordpress.org/reference/hooks/request/ |
| 117 | + * |
| 118 | + * @return mixed |
| 119 | + */ |
| 120 | + public static function process_request( $query_vars ) { |
| 121 | + // Additional parameters added to WordPress. |
| 122 | + // Main Loop query. |
| 123 | + if ( array_key_exists( self::PARAM_CUSTOMFIELD_KEY, $query_vars ) ) { |
| 124 | + $query_vars['meta_key'] = $query_vars[ self::PARAM_CUSTOMFIELD_KEY ]; |
| 125 | + |
| 126 | + // Remove temporary injected parameter. |
| 127 | + unset( $query_vars[ self::PARAM_CUSTOMFIELD_KEY ] ); |
| 128 | + |
| 129 | + // Do not check field's value for this moment. |
| 130 | + if ( true === self::$check_custom_field_value ) { |
| 131 | + if ( array_key_exists( self::PARAM_CUSTOMFIELD_VALUE, $query_vars ) ) { |
| 132 | + $query_vars['meta_value'] = $query_vars[ self::PARAM_CUSTOMFIELD_VALUE ]; |
| 133 | + |
| 134 | + // Remove temporary injected parameter. |
| 135 | + unset( $query_vars[ self::PARAM_CUSTOMFIELD_VALUE ] ); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return $query_vars; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Filters the full set of generated rewrite rules. |
| 145 | + * The rewrite_rules_array filter implementation. |
| 146 | + * |
| 147 | + * @param array $rules The compiled array of rewrite rules. |
| 148 | + * |
| 149 | + * @link https://developer.wordpress.org/reference/hooks/rewrite_rules_array/ |
| 150 | + * |
| 151 | + * @return array |
| 152 | + */ |
| 153 | + public static function rewrite_rules_array_filter( $rules ) { |
| 154 | + $keys = array_keys( $rules ); |
| 155 | + $tmp = $rules; |
| 156 | + $rules = array(); |
| 157 | + |
| 158 | + $j = sizeof( $keys ); |
| 159 | + for ( $i = 0; $i < $j; ++ $i ) { |
| 160 | + $key = $keys[ $i ]; |
| 161 | + |
| 162 | + if ( preg_match( '/%field_([^%]*?)%/', $key ) ) { |
| 163 | + $key_new = preg_replace( |
| 164 | + '/%field_([^%]*?)%/', |
| 165 | + '([^/]+)', |
| 166 | + // You can simply add next group to the url, because WordPress. |
| 167 | + // Detect them automatically and add next $matches indices. |
| 168 | + $key |
| 169 | + ); |
| 170 | + $rules[ $key_new ] = preg_replace( |
| 171 | + '/%field_([^%]*?)%/', |
| 172 | + sprintf( '%s=$1&%s=', self::PARAM_CUSTOMFIELD_KEY, self::PARAM_CUSTOMFIELD_VALUE ), |
| 173 | + // Here on the end will be pasted $matches[$i] from $keyNew, |
| 174 | + // so we can grab it it the future in self::PARAM_CUSTOMFIELD_VALUE parameter. |
| 175 | + $tmp[ $key ] |
| 176 | + ); |
| 177 | + } else { |
| 178 | + $rules[ $key ] = $tmp[ $key ]; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return $rules; |
| 183 | + } |
| 184 | +} |
0 commit comments