Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QTS: rationalize get post_types/taxonomies functions and calls #1121

Merged
merged 8 commits into from
Mar 18, 2022
76 changes: 51 additions & 25 deletions modules/slugs/includes/class-qtranslate-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function get_slug( $id, $lang ) {
*/
public function modify_rewrite_rules() {
// post types rules
$post_types = get_post_types( array( '_builtin' => false ), 'objects' );
$post_types = $this->get_public_post_types();
foreach ( $post_types as $post_type ) {
$this->generate_extra_rules( $post_type->name );
}
Expand Down Expand Up @@ -501,9 +501,12 @@ function filter_request( $query ) {
if ( ( isset( $wp->matched_query ) || empty( $query ) ) && ! isset( $query['s'] ) ) {
$query = wp_parse_args( $wp->matched_query );
}
foreach ( get_post_types() as $post_type ) {
if ( array_key_exists( $post_type, $query ) && ! in_array( $post_type, array( 'post', 'page' ) ) ) {
$query['post_type'] = $post_type;
foreach ( $this->get_public_post_types() as $post_type ) {
if ( array_key_exists( $post_type->name, $query ) && ! in_array( $post_type->name, array(
'post',
'page'
) ) ) {
$query['post_type'] = $post_type->name;
}
}
// -> page
Expand Down Expand Up @@ -581,19 +584,18 @@ function filter_request( $query ) {


// -> taxonomy
$taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) );
foreach ( $taxonomies as $term_name ):
if ( isset( $query[ $term_name ] ) ) {
$term_slug = $this->get_last_slash( $query[ $term_name ] );
$term = $this->get_term_by( 'slug', $term_slug, $term_name );
foreach ( $this->get_public_taxonomies() as $item ):
if ( isset( $query[ $item->name ] ) ) {
$term_slug = $this->get_last_slash( $query[ $item->name ] );
$term = $this->get_term_by( 'slug', $term_slug, $item->name );
if ( ! $term ) {
return $query;
}
$cache_array = array( $term );
update_term_cache( $cache_array, $term_name ); // caching query :)
$id = $term;
$query[ $term_name ] = $term->slug;
$function = 'get_term_link';
update_term_cache( $cache_array, $item->name ); // caching query :)
$id = $term;
$query[ $item->name ] = $term->slug;
$function = 'get_term_link';

}
endforeach;
Expand Down Expand Up @@ -1537,6 +1539,42 @@ public function blog_names( $blogs ) {
return $blogs;
}

/**
* Helper: returns public taxonomies.
*
* @return array of public taxonomies objects
*/
public function get_public_taxonomies() {
$all_taxonomies = get_taxonomies( array( 'public' => true, 'show_ui' => true ), 'objects' );
$taxonomies = array();

foreach ( $all_taxonomies as $taxonomy ) {
if ( $taxonomy->rewrite ) {
$taxonomies[] = $taxonomy;
}
}

return $taxonomies;
}

/**
* Helper: returns public post_types with rewritable slugs.
*
* @return array of public post_types objects
*/
public function get_public_post_types() {
$all_post_types = get_post_types( array( 'public' => true ), 'objects' );
$post_types = array();

foreach ( $all_post_types as $post_type ) {
if ( $post_type->rewrite ) {
$post_types[] = $post_type;
}
}

return $post_types;
}

/**
* Return the current / temp language.
*/
Expand Down Expand Up @@ -1596,18 +1634,6 @@ private function generate_extra_rules( $name = false ) {
endforeach;
}

/**
* Helper: returns public built-in and not built-in taxonomies.
*
* @return array of public taxonomies objects
*/
private function get_public_taxonomies() {
$builtin = get_taxonomies( array( 'public' => true, 'show_ui' => true, '_builtin' => true ), 'object' );
$taxonomies = get_taxonomies( array( 'public' => true, 'show_ui' => true, '_builtin' => false ), 'object' );

return array_merge( $builtin, $taxonomies );
}

/**
* Parse a hierarchical name and extract the last one.
*
Expand Down
64 changes: 0 additions & 64 deletions modules/slugs/includes/qtranslate-slug-settings-options.php

This file was deleted.

78 changes: 72 additions & 6 deletions modules/slugs/includes/qtranslate-slug-settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// page settings sections & fields as well as the contextual help text.
include_once( 'qtranslate-slug-settings-options.php' );
add_action( 'qtranslate_update_settings', 'qts_update_settings' );
add_action( 'qtranslate_configuration', 'qts_show_settings_page' );

/**
* Helper function for defining variables for the current page.
Expand Down Expand Up @@ -87,7 +87,6 @@ function qts_show_form_field( $args = array() ) {
}
echo "<label for=" . QTS_OPTIONS_NAME . "[$id|${item[1]}]>" . $item[0] . "</label> " .
"<input class='$field_class' type='text' id='$id|${item[1]}' name='" . QTS_OPTIONS_NAME . "[$id|${item[1]}]' value='" . urldecode( $value ) . "' /><br/>";
//echo "<span>$item[0]:</span> <input class='$field_class' type='text' id='$id|$item[1]' name='" . QTS_OPTIONS_NAME . "[$id|$item[1]]' value='$value' /><br/>";
}
echo ( $desc != '' ) ? "<p class='qtranxs-notes'>$desc</p>" : "";
break;
Expand Down Expand Up @@ -205,8 +204,6 @@ function qts_show_settings_page() {
QTX_Admin_Settings::close_section( 'slugs' );
}

add_action( 'qtranslate_configuration', 'qts_show_settings_page' );

/**
* Validate input.
*
Expand Down Expand Up @@ -356,4 +353,73 @@ function qts_update_settings() {
$qtranslate_slug->save_options( $qts_settings );
}

add_action( 'qtranslate_update_settings', 'qts_update_settings' );
/**
* Define our settings sections.
*
* @return array key=$id, array value=$title in: add_settings_section( $id, $title, $callback, $page );
*/
function qts_options_page_sections() {
$sections = array();
$sections['post_types'] = __( 'Post types', 'qtranslate' );
$sections['taxonomies'] = __( 'Taxonomies', 'qtranslate' );

return $sections;
}

/**
* Helper for create arrays of choices.
*
* @return array
*/
function get_multi_txt_choices() {
global $q_config;

$choices = array();
foreach ( $q_config['enabled_languages'] as $lang ) {
$label = sprintf( __( 'Slug' ) . ' (%s)', $q_config['language_name'][ $lang ] );
$choices[] = "$label|$lang"; // prints: 'Slug (English)|en'
}

return $choices;
}

/**
* Define our form fields (settings).
*
* @return array
*/
function qts_options_page_fields() {
global $qtranslate_slug;
$options = array();

$post_types = $qtranslate_slug->get_public_post_types();
foreach ( $post_types as $post_type ) {
$options[] = qts_options_page_build_slug_fields( $post_type, "post_types", "post_type_" );
}

$taxonomies = $qtranslate_slug->get_public_taxonomies();
foreach ( $taxonomies as $taxonomy ) {
$options[] = qts_options_page_build_slug_fields( $taxonomy, "taxonomies", "taxonomy_" );
}

return array_filter( $options );
}

function qts_options_page_build_slug_fields( $object, $target_section, $id_prefix ) {
if ( is_array( $object->rewrite ) && array_key_exists( 'slug', $object->rewrite ) ) {
$slug = ltrim( $object->rewrite['slug'], "/" );
} else {
$slug = $object->name;
}

return array(
"section" => $target_section,
"id" => QTS_PREFIX . $id_prefix . $object->name,
"title" => qtranxf_use( qtranxf_getLanguage(), $object->label ),
"desc" => sprintf( '<code>https://example.org/<u>%s</u>/some-%s/</code>', $slug, $object->name ),
'class' => 'qts-slug', // used in qts_validate_options. TODO: cleaner way to be considered...
"type" => "multi-text",
"choices" => get_multi_txt_choices(),
"std" => ""
);
}