Skip to content

Commit

Permalink
QTS: fix warnings and cleanup (#671)
Browse files Browse the repository at this point in the history
Several functions had missing or incompatible return values.
For example, WP actions do not expect return values -> void.

Proper PHPDoc tags allow tools to detect inconsistencies.
Fix return tags and complete also param tags.

Delete comments bloating the code, minor cleanup.
  • Loading branch information
herrvigg committed Feb 19, 2022
1 parent 0a9ed2f commit 2369434
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 111 deletions.
2 changes: 1 addition & 1 deletion modules/slugs/includes/class-qtranslate-slug-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function widget( $args, $instance ) {
$title = empty( $instance['title'] ) ? __( 'Language', 'qts' ) : apply_filters( 'widget_title', $instance['title'] );
$hide_title = empty( $instance['hide-title'] ) ? false : 'on';
$type = $instance['type'];
$short_text = ( $instance['short_text'] == 'on' ) ? true : false;
$short_text = $instance['short_text'] == 'on';

if ( $type != 'text' && $type != 'image' && $type != 'both' && $type != 'dropdown' ) {
$type = 'text';
Expand Down
43 changes: 27 additions & 16 deletions modules/slugs/includes/class-qtranslate-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,21 @@ function init() {

/**
* Filters menu link attribute.
*
* @param string $item_title The menu item title attribute.
*
* @return string
*/
function qts_filter_attr_title( $attr_title ) {
return $this->qts_quickuse( $attr_title );
function qts_filter_attr_title( $item_title ) {
return $this->qts_quickuse( $item_title );
}

/**
* Adds proper links to the content with available translations.
* Fixes issue #25
*
* @global $qtranslate_slug used to convert the url
* @global $q_config available languages
* @global QtranslateSlug $qtranslate_slug used to convert the url
* @global array $q_config available languages
*/
public function qtranslate_slug_header_extended() {
if ( is_404() ) {
Expand Down Expand Up @@ -372,14 +376,16 @@ public function get_slug( $id, $lang ) {
* @param string $text the whole text
* @param string $lang (optional) get the text in this language, or if empty, the current
*
* @return array the text in the required language
* @return string the text in the required language
*/
public function qts_quickuse( $text, $lang = '' ) {
$lang = '' == $lang ? $this->current_lang : $lang;
$parsed_text = qtranxf_getSortedLanguages( $text ); // TODO: this call looks very wrong!
if ( ! empty( $parsed_text[ $lang ] ) ) {
return $parsed_text[ $lang ];
}
// TODO: check what to do if lang not found in ML values
return $text;
}

/**
Expand Down Expand Up @@ -592,15 +598,16 @@ public function query_vars( $query_vars ) {
}
}

// TODO check this call, looks bug-prone
return count( array_diff( $query_vars, $wp->public_query_vars ) ) > 0 ? $query_vars : $wp->public_query_vars;
}

/**
* Function called when query parameters are processed by Wordpress.
*
* @param $query query parameters
* @param array $query query parameters
*
* @return array() $query processed
* @return array $query processed
*/
function filter_request( $query ) {
global $wp;
Expand Down Expand Up @@ -817,11 +824,11 @@ public function get_extra_permastruct( $permastruct = false, $name = false ) {
* Filter that translates the slug parts in a page link.
*
* @param string $link the link for the page generated by Wordpress
* @param WP_Post $post
* @param WP_Post|WP_Error $post
* @param bool $leavename
* @param bool $sample
*
* @return string the link translated
* @return string|WP_Error the link translated
*/
public function post_type_link( $link, $post, $leavename, $sample ) {
global $wp_rewrite;
Expand Down Expand Up @@ -1172,7 +1179,7 @@ function get_object_terms( $terms, $obj_id, $taxonomy, $args ) {
*/
public function hide_quick_edit() {
echo "<!-- QTS remove quick edit box -->" . PHP_EOL;
echo "<style type=\"text/css\" media=\"screen\">" . PHP_EOL;
echo "<style media=\"screen\">" . PHP_EOL;
echo " .inline-edit-row fieldset.inline-edit-col-left .inline-edit-col *:first-child + label + label { display: none !important }" . PHP_EOL;
echo "</style>" . PHP_EOL;
}
Expand Down Expand Up @@ -1275,9 +1282,9 @@ public function draw_meta_box( $post ) {
*/
public function validate_post_slug( $slug, $post, $lang ) {

$post_title = trim( qtranxf_use( $lang, $post->post_title) );
$post_title = trim( qtranxf_use( $lang, $post->post_title ) );

$post_name = get_post_meta( $post->ID, $this->get_meta_key( $lang ), true );
$post_name = get_post_meta( $post->ID, $this->get_meta_key( $lang ), true );
if ( ! $post_name ) {
$post_name = $post->post_name;
}
Expand Down Expand Up @@ -1383,6 +1390,8 @@ public function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type,
*
* @param $post_id int the post id
* @param $post object the post object
*
* @return void
*/
public function save_postdata( $post_id, $post ) {
$post_type_object = get_post_type_object( $post->post_type );
Expand All @@ -1391,7 +1400,7 @@ public function save_postdata( $post_id, $post ) {
|| ( ! isset( $_POST['post_ID'] ) || $post_id != $_POST['post_ID'] ) // check revision
|| ( isset( $_POST['qts_nonce'] ) && ! wp_verify_nonce( $_POST['qts_nonce'], 'qts_nonce' ) ) // verify nonce
|| ( ! current_user_can( $post_type_object->cap->edit_post, $post_id ) ) ) { // check permission
return $post_id;
return;
}
foreach ( $this->get_enabled_languages() as $lang ) {

Expand Down Expand Up @@ -1425,7 +1434,7 @@ public function show_term_fields( $term ) {
$value = ( $slug ) ? htmlspecialchars( $slug, ENT_QUOTES ) : '';

echo "<tr class=\"form-field form-required\">" . PHP_EOL;
echo "<th scope=\"row\" valig=\"top\"><label for=\"qts_{$lang}_slug\">" . sprintf( __( 'Slug (%s)', 'qts' ), $q_config['language_name'][ $lang ] ) . "</label></th>" . PHP_EOL;
echo "<th scope=\"row\"><label for=\"qts_{$lang}_slug\">" . sprintf( __( 'Slug (%s)', 'qts' ), $q_config['language_name'][ $lang ] ) . "</label></th>" . PHP_EOL;
echo "<td><input type=\"text\" name=\"qts_{$lang}_slug\" value=\"" . urldecode( $value ) . "\" /></td></tr>" . PHP_EOL;

}
Expand Down Expand Up @@ -1535,14 +1544,16 @@ public function unique_term_slug( $slug, $term, $lang ) {
* @param $term_id int the term id
* @param $tt_id int the term taxonomy id
* @param $taxonomy object the term object
*
* @return void
*/
public function save_term( $term_id, $tt_id, $taxonomy ) {
$cur_screen = get_current_screen();
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // check autosave
|| ( ! current_user_can( 'edit_posts' ) ) // check permission
|| ( isset( $cur_screen ) && $cur_screen->id === "nav-menus" )
) {
return $term_id;
return;
}

$term = get_term( $term_id, $taxonomy );
Expand Down Expand Up @@ -1773,7 +1784,7 @@ public function language_menu( $type = "text", $args = array() ) {

//TODO: add i18n for alt attribute
//43LC: hardcoding height and width
$link_flag = "<img widht=\"18\" height=\"12\" src=\"$link_flag_url\" alt=\"$language_name\" />";
$link_flag = "<img width=\"18\" height=\"12\" src=\"$link_flag_url\" alt=\"$language_name\" />";
}
echo "<li $item_class><a href=\"$url\" lang=\"$lang\" hreflang=\"$lang\"$link_class>$link_flag$link_content</a></li>" . PHP_EOL;
endforeach;
Expand Down
10 changes: 5 additions & 5 deletions modules/slugs/includes/qtranslate-slug-settings-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function get_multi_txt_choices( $name = false ) {
return array();
}
$choices = array();
foreach ( $q_config['enabled_languages'] as $key => $lang ) {
foreach ( $q_config['enabled_languages'] as $lang ) {
$label = sprintf( __( 'Slug (%s)', 'qts' ), $q_config['language_name'][ $lang ] );
$choices[] = "$label|$lang"; // prints: 'Slug (English)|en' ( $name = books )
}
Expand Down Expand Up @@ -77,7 +77,7 @@ function qts_options_page_fields() {
"section" => "post_types",
"id" => QTS_PREFIX . "post_type_" . $post_type->name,
"title" => $post_type->labels->singular_name,
"desc" => sprintf( __( '<code>http://example.org/<u>%s</u>/some-%s/</code>', 'qts' ), $post_type->name, $post_type->name ),
"desc" => sprintf( __( '<code>https://example.org/<u>%s</u>/some-%s/</code>', 'qts' ), $post_type->name, $post_type->name ),
'class' => 'qts-slug',
"type" => "multi-text",
"choices" => get_multi_txt_choices( $post_type->name ),
Expand All @@ -90,7 +90,7 @@ function qts_options_page_fields() {
"section" => "taxonomies",
"id" => QTS_PREFIX . "taxonomy_category",
"title" => __( 'Categories', 'qts' ),
"desc" => __( '<code>http://example.org/<u>category</u>/some-category/</code>', 'qts' ),
"desc" => __( '<code>https://example.org/<u>category</u>/some-category/</code>', 'qts' ),
"type" => "multi-text",
'class' => 'qts-slug',
"choices" => get_multi_txt_choices( 'category' ),
Expand All @@ -101,7 +101,7 @@ function qts_options_page_fields() {
"section" => "taxonomies",
"id" => QTS_PREFIX . "taxonomy_post_tag",
"title" => __( 'Tags', 'qts' ),
"desc" => __( '<code>http://example.org/<u>tag</u>/some-tag/</code>', 'qts' ),
"desc" => __( '<code>https://example.org/<u>tag</u>/some-tag/</code>', 'qts' ),
"type" => "multi-text",
'class' => 'qts-slug',
"choices" => get_multi_txt_choices( 'post_tag' ),
Expand All @@ -116,7 +116,7 @@ function qts_options_page_fields() {
"section" => "taxonomies",
"id" => QTS_PREFIX . "taxonomy_" . $taxonomy->name,
"title" => $taxonomy->labels->singular_name,
"desc" => sprintf( __( '<code>http://example.org/<u>%s</u>/some-%s/</code>', 'qts' ), $taxonomy->name, $taxonomy->name ),
"desc" => sprintf( __( '<code>https://example.org/<u>%s</u>/some-%s/</code>', 'qts' ), $taxonomy->name, $taxonomy->name ),
"type" => "multi-text",
'class' => 'qts-slug',
"choices" => get_multi_txt_choices( $taxonomy->name ),
Expand Down
Loading

0 comments on commit 2369434

Please sign in to comment.