From 33f74fe5cc9a7af0ed77956564f059413cc824c1 Mon Sep 17 00:00:00 2001 From: HerrVigg Date: Mon, 23 May 2022 01:02:47 +0200 Subject: [PATCH] QTS: fix many PHPStorm warnings Add missing `isset` checks on variables. Simplify functions where args are not used (e.g. $output OBJECT). Many minor fixes. --- .../slugs/includes/class-qtranslate-slug.php | 62 +++++++------------ .../slugs/includes/qtranslate-slug-admin.php | 13 ++-- .../includes/qtranslate-slug-settings.php | 20 +++--- 3 files changed, 39 insertions(+), 56 deletions(-) diff --git a/modules/slugs/includes/class-qtranslate-slug.php b/modules/slugs/includes/class-qtranslate-slug.php index f12e07ee..73a0da72 100644 --- a/modules/slugs/includes/class-qtranslate-slug.php +++ b/modules/slugs/includes/class-qtranslate-slug.php @@ -131,8 +131,6 @@ public function get_slug( $id, $lang ) { /** * Adds news rules to translate the URL bases, * this function must be called on flush_rewrite or 'flush_rewrite_rules'. - * - * @param object $wp_rewrite */ public function modify_rewrite_rules() { // post types rules @@ -167,9 +165,6 @@ public function get_base_slug( $name = false, $lang = false ) { } $qts_options = $this->options_buffer; $option_name = QTS_PREFIX . $type . '_' . $name; - if ( ! isset( $qts_options[ $option_name ] ) || empty( $qts_options[ $option_name ] ) ) { - return false; - } if ( isset( $qts_options[ $option_name ][ $lang ] ) ) { return $qts_options[ $option_name ][ $lang ]; } @@ -298,7 +293,7 @@ public function query_vars( $query_vars ) { $request_match = $req_uri . '/' . $request; } if ( preg_match( "#^$match#", $request_match, $matches ) || preg_match( "#^$match#", urldecode( $request_match ), $matches ) ) { - if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) { + if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[(\d+)\]/', $query, $varmatch ) ) { // this is a verbose page match, lets check to be sure about it if ( ! $page_foundid = $this->get_page_by_path( $matches[ $varmatch[1] ] ) ) { continue; @@ -313,7 +308,7 @@ public function query_vars( $query_vars ) { } } - if ( isset( $wp->matched_rule ) ) { + if ( isset( $wp->matched_rule ) && isset( $query ) && isset( $matches ) ) { // Trim the query of everything up to the '?'. $query = preg_replace( "!^.+\?!", '', $query ); // Substitute the substring matches into the query. @@ -448,7 +443,7 @@ function filter_request( $query ) { $id = $query['post_type']; } else { $page_slug = ( isset( $query['name'] ) && ! empty( $query['name'] ) ) ? $query['name'] : $query[ $query['post_type'] ]; - $page = $this->get_page_by_path( $page_slug, OBJECT, $query['post_type'] ); + $page = $this->get_page_by_path( $page_slug, $query['post_type'] ); if ( ! $page ) { return $query; } @@ -483,8 +478,8 @@ function filter_request( $query ) { */ // -> post - if ( ! $function && ( isset( $query['name'] ) || isset( $query['p'] ) ) ) { - $post = isset( $query['p'] ) ? get_post( $query['p'] ) : $this->get_page_by_path( $query['name'], OBJECT, 'post' ); + if ( ! isset( $function ) && ( isset( $query['name'] ) || isset( $query['p'] ) ) ) { + $post = isset( $query['p'] ) ? get_post( $query['p'] ) : $this->get_page_by_path( $query['name'], 'post' ); if ( ! $post ) { return $query; } @@ -496,7 +491,7 @@ function filter_request( $query ) { } endif; - if ( isset( $function ) ) { + if ( isset( $function ) && isset( $id ) ) { // parse all languages links foreach ( $q_config['enabled_languages'] as $lang ) { @@ -763,7 +758,7 @@ public function _get_page_link( $link, $id ) { $current_post = $post; if ( ! $id ) { - $id = (int) $post->ID; + $id = $post->ID; } else { $current_post = get_post( $id ); } @@ -849,7 +844,7 @@ public function term_link( $link, $term, $taxonomy ) { if ( $t->rewrite['hierarchical'] ) { $hierarchical_slugs = array(); $ancestors = get_ancestors( $term->term_id, $taxonomy ); - foreach ( (array) $ancestors as $ancestor ) { + foreach ( $ancestors as $ancestor ) { $ancestor_term = get_term( $ancestor, $taxonomy ); $ancestor_slug = get_metadata( 'term', $ancestor_term->term_id, QTS_META_PREFIX . $this->get_temp_lang(), true ); @@ -925,7 +920,7 @@ public function get_public_post_types() { private function get_temp_lang() { global $q_config; - return ( $this->temp_lang ) ? $this->temp_lang : $q_config['language']; + return ( $this->temp_lang ) ?: $q_config['language']; } /** @@ -974,12 +969,11 @@ private function get_last_slash( $slug ) { * Retrieves a page id given its path. * * @param string $page_path Page path - * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT. * @param string $post_type Optional. Post type. Default page. * * @return mixed Null when complete. */ - private function get_page_id_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { + private function get_page_id_by_path( $page_path, $post_type = 'page' ) { global $wpdb; $page_path = rawurlencode( urldecode( $page_path ) ); @@ -1039,15 +1033,14 @@ private function get_page_id_by_path( $page_path, $output = OBJECT, $post_type = * Retrieves a page given its path. * * @param string $page_path Page path - * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT. * @param string $post_type Optional. Post type. Default page. * - * @return mixed Null when complete. + * @return array|WP_Post|null Null when complete. */ - private function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { - $foundid = $this->get_page_id_by_path( $page_path, $output, $post_type ); + private function get_page_by_path( $page_path, $post_type = 'page' ) { + $foundid = $this->get_page_id_by_path( $page_path, $post_type ); if ( $foundid ) { - return get_post( $foundid, $output ); + return get_post( $foundid ); } return null; @@ -1169,15 +1162,14 @@ private function get_page_uri( $page ) { /** * Get all Term data from database by Term field and data. * - * @param (string) $field Either 'slug', 'name', or 'id' - * @param (string|int) $value Search for this term value - * @param (string) $taxonomy Taxonomy Name - * @param (string) $output Constant OBJECT, ARRAY_A, or ARRAY_N - * @param (string) $filter Optional, default is raw or no WordPress defined filter will applied. + * @param string $field Either 'slug', 'name', or 'id' + * @param string|int $value Search for this term value + * @param string $taxonomy Taxonomy Name * - * @return (mixed) Term Row from database. Will return false if $taxonomy does not exist or $term was not found. + * @return array|false|object|WP_Error|WP_Term|null Term Row from database. Will return false if $taxonomy does not exist or $term was not found. + * TODO: simplify return type and error handling, unexpected results may cause bugs! */ - private function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) { + private function get_term_by( $field, $value, $taxonomy ) { global $wpdb; if ( ! taxonomy_exists( $taxonomy ) ) { @@ -1196,7 +1188,7 @@ private function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filt $value = stripslashes( $value ); $field = 't.name'; } else { - $term = get_term( (int) $value, $taxonomy, $output, $filter ); + $term = get_term( (int) $value, $taxonomy ); if ( is_wp_error( $term ) ) { $term = false; } @@ -1219,16 +1211,8 @@ private function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filt $term = apply_filters( 'get_term', $term, $taxonomy ); $term = apply_filters( "get_$taxonomy", $term, $taxonomy ); - $term = sanitize_term( $term, $taxonomy, $filter ); + $term = sanitize_term( $term, $taxonomy, 'raw' ); - if ( $output == OBJECT ) { - return $term; - } elseif ( $output == ARRAY_A ) { - return get_object_vars( $term ); - } elseif ( $output == ARRAY_N ) { - return array_values( get_object_vars( $term ) ); - } else { - return $term; - } + return $term; } } diff --git a/modules/slugs/includes/qtranslate-slug-admin.php b/modules/slugs/includes/qtranslate-slug-admin.php index 187c23a0..86d8bad2 100644 --- a/modules/slugs/includes/qtranslate-slug-admin.php +++ b/modules/slugs/includes/qtranslate-slug-admin.php @@ -234,7 +234,7 @@ function qts_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $po } else { // TODO: update unique_slug :: missing hieararchical from current wp func ( 4.3.1 ) // Post slugs must be unique across all posts. - $check_sql = "SELECT $wpdb->postmeta.meta_value FROM $wpdb->posts,$wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '%s' AND $wpdb->postmeta.meta_value = '%s' AND $wpdb->posts.post_type = %s AND ID != %d LIMIT 1"; + $check_sql = "SELECT $wpdb->postmeta.meta_value FROM $wpdb->posts,$wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '%s' AND $wpdb->postmeta.meta_value = '%s' AND $wpdb->posts.post_type = %s AND $wpdb->posts.ID != %d LIMIT 1"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, QTS_META_PREFIX . $lang, $slug, $post_type, $post_ID ) ); // TODO: update unique_slug :: missing check for conflict with dates archive from current wp func ( 4.3.1 ) @@ -296,6 +296,8 @@ function qts_save_postdata( $post_id, $post = null ) { * @return string the slug validated */ function qts_validate_term_slug( $slug, $term, $lang ) { + global $q_config; + $term_name = trim( qtranxf_use( $lang, $term->name, false, true ) ); if ( $term_name === '' ) { $term_name = trim( qtranxf_use( $q_config['default_language'], $term->name ) ); @@ -498,6 +500,9 @@ function qts_hide_term_slug_box() { return; endswitch; + if ( ! isset( $id ) ) { + return; + } echo "" . PHP_EOL; echo "