Skip to content

Commit

Permalink
Merge pull request #255 from transifex/custom_post_types_redirect_rules
Browse files Browse the repository at this point in the history
Rewrite rules for custom post types
  • Loading branch information
foteinigk authored May 31, 2024
2 parents 33b52f5 + 66663fe commit 967ff46
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions includes/lib/transifex-live-integration-subdirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Transifex_Live_Integration_Subdirectory {
private $source_language;

/**
* List of languages used by rewrite
* List of languages used by rewrite
* @var array
*/
private $language_codes;
Expand Down Expand Up @@ -116,7 +116,7 @@ function parse_query_hook( $query ) {

/*
* WP parse_query filter,additional logic to support localized static frontpages
* @param array $query WP query object.
* @param array $query WP query object.
* @return array Returns the filtered query object
*/

Expand Down Expand Up @@ -169,14 +169,16 @@ function post_rewrite_rules_hook( $rules ) {
$this->post_permastruct = $pp;
$rr = Transifex_Live_Integration_Generate_Rewrite_Rules::generate_rewrite_rules( $pp, EP_PERMALINK, true, false, false, true );
$rewrite = array_merge( $rr, $rules );

// Handle custom post types from custom filter
$custom_rewrite = $this->custom_type_rules_hook();
$rewrite = array_merge( $custom_rewrite, $rewrite);

if ($custom_rewrite) {
$rewrite = array_merge( $custom_rewrite, $rewrite);
}

return $rewrite;
}

/**
* Callback function to the WP page_rewrite_rules
*
Expand All @@ -190,7 +192,10 @@ function custom_type_rules_hook(){
// Get custom rules from 3rd party module, if our own filter is being used
$custom_types_array = array();
$custom_types_array = apply_filters('transifex_generate_rewrite_rules', $custom_types_array);
$rewrite_array = array();

// Get rules for custom post type
$custom_post_types_rewrite_rules = $this->custom_post_type_rewrite_rules_hook();
$custom_types_array = array_merge($custom_post_types_rewrite_rules, $custom_types_array);
foreach($custom_types_array as $custom_type_regex => $custom_type_action){
// Replace the lang placeholder with the language regex for this configuation
$custom_type_regex = str_replace("%lang%", $this->languages_regex, $custom_type_regex);
Expand All @@ -200,6 +205,29 @@ function custom_type_rules_hook(){
return $rewrite_array;
}

/**
* Generates custom rewrite rules for custom post types.
*
* This function retrieves all custom post types that are not built-in,
* and for each custom post type, it generates a set of rewrite rules
* based on the post type's slug and a specified permalink structure.
*
* @return array An array of generated rewrite rules for custom post types.
*/
function custom_post_type_rewrite_rules_hook() {
$rules = array();
$custom_post_types = get_post_types(array('_builtin' => false));
foreach ($custom_post_types as $post_type) {
$post_type_object = get_post_type_object($post_type);
$slug = $post_type_object->rewrite['slug'];
if ($slug) {
$post_type_object = get_post_type_object($post_type);
$rules['%lang%/' . $slug .'/([^/]+)/?$'] = 'index.php?lang=$matches[1]&' . $post_type . '=$matches[2]';
}
}
return $rules;
}

/**
* Function to build page permastructs
*/
Expand Down

0 comments on commit 967ff46

Please sign in to comment.