From ecb9212855a4c6944794b089ea4bf23ac61ff6af Mon Sep 17 00:00:00 2001 From: dilipbheda Date: Wed, 16 Apr 2025 15:10:54 +0530 Subject: [PATCH] Enhance code quality by optimizing logic and aligning with coding standards --- app/admin/RTMediaAdmin.php | 18 ++++++------ app/admin/RTMediaFormHandler.php | 18 ++++++------ app/admin/RTMediaUploadTermsAdmin.php | 1 - app/helper/RTMediaActivityModel.php | 3 +- app/helper/RTMediaCommentNotification.php | 5 ++-- app/helper/RTMediaInteractionModel.php | 3 +- app/helper/RTMediaLikeNotification.php | 3 +- app/helper/RTMediaModel.php | 28 ++++++++----------- app/helper/rtDimensions.php | 16 +++++------ app/helper/rtProgress.php | 6 ++-- app/importers/BPMediaAlbumimporter.php | 1 + app/importers/BPMediaImporter.php | 9 ++---- app/importers/RTMediaActivityUpgrade.php | 5 +--- app/importers/RTMediaMigration.php | 26 ++++++----------- app/main/RTMedia.php | 9 +++--- .../controllers/template/rtmedia-actions.php | 4 +-- app/main/routers/query/RTMediaQuery.php | 4 +-- 17 files changed, 65 insertions(+), 94 deletions(-) diff --git a/app/admin/RTMediaAdmin.php b/app/admin/RTMediaAdmin.php index 88fe2c492..973c488c4 100755 --- a/app/admin/RTMediaAdmin.php +++ b/app/admin/RTMediaAdmin.php @@ -106,11 +106,9 @@ public function __construct() { if ( isset( $rtmedia_option['general_showAdminMenu'] ) && 1 === intval( $rtmedia_option['general_showAdminMenu'] ) ) { add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100, 1 ); } - } else { - if ( is_array( $rtmedia->options ) ) { - if ( 1 === intval( $rtmedia->options['general_showAdminMenu'] ) ) { - add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100, 1 ); - } + } elseif ( is_array( $rtmedia->options ) ) { + if ( 1 === intval( $rtmedia->options['general_showAdminMenu'] ) ) { + add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100, 1 ); } } @@ -1403,10 +1401,10 @@ public function linkback() { */ public function export_settings() { - // permission check. - if ( ! current_user_can( 'manage_options' ) ) { - wp_send_json_error( array( 'message' => esc_html__( 'You do not have permission to export settings.', 'buddypress-media' ) ) ); - } + // permission check. + if ( ! current_user_can( 'manage_options' ) ) { + wp_send_json_error( array( 'message' => esc_html__( 'You do not have permission to export settings.', 'buddypress-media' ) ) ); + } $rtmedia_option = get_option( 'rtmedia-options' ); @@ -1435,7 +1433,7 @@ public function import_settings( $file_path ) { } $settings_data_json_string = file_get_contents( $file_path ); - $settings_data_json = json_decode( $settings_data_json_string, true ); + $settings_data_json = json_decode( $settings_data_json_string, true ); wp_delete_file( $file_path ); if ( empty( $settings_data_json ) ) { diff --git a/app/admin/RTMediaFormHandler.php b/app/admin/RTMediaFormHandler.php index 32f3d372f..c9d36a98a 100755 --- a/app/admin/RTMediaFormHandler.php +++ b/app/admin/RTMediaFormHandler.php @@ -52,11 +52,11 @@ public static function selectBox( $args ) { // phpcs:ignore WordPress.NamingConv * @access static * * @param array $args arguments to display textarea. - * @param bool $echo Do echo or not. + * @param bool $display Do echo or not. * * @return string $chk_obj->get_textarea( $args ) */ - public static function textarea( $args, $echo = true ) { + public static function textarea( $args, $display = true ) { $defaults = array( 'key' => '', 'desc' => '', @@ -83,7 +83,7 @@ public static function textarea( $args, $echo = true ) { $chk_obj = new rtForm(); - if ( $echo ) { + if ( $display ) { $chk_obj->display_textarea( $args ); } else { return $chk_obj->get_textarea( $args ); @@ -98,11 +98,11 @@ public static function textarea( $args, $echo = true ) { * @access static * * @param array $args arguments to display checkbox. - * @param bool $echo Do echo or not. + * @param bool $display Do echo or not. * * @return string $chk_obj->get_switch( $args ) */ - public static function checkbox( $args, $echo = true ) { + public static function checkbox( $args, $display = true ) { $defaults = array( 'key' => '', 'desc' => '', @@ -129,7 +129,7 @@ public static function checkbox( $args, $echo = true ) { $chk_obj = new rtForm(); - if ( $echo ) { + if ( $display ) { $chk_obj->display_switch( $args ); } else { return $chk_obj->get_switch( $args ); @@ -265,13 +265,13 @@ public static function textbox( $args ) { * @access static * * @param array $args arguments to create link. - * @param bool $echo Echo or not. + * @param bool $display Echo or not. * * @return string $link_obj * * @throws rtFormInvalidArgumentsException Invalid argument exception. */ - public static function link( $args, $echo = true ) { + public static function link( $args, $display = true ) { $defaults = array( 'href' => '', @@ -299,7 +299,7 @@ public static function link( $args, $echo = true ) { $link_obj = new rtForm(); - if ( $echo ) { + if ( $display ) { $link_obj->display_link( $args ); } else { diff --git a/app/admin/RTMediaUploadTermsAdmin.php b/app/admin/RTMediaUploadTermsAdmin.php index 5628bd62c..c8dd3f57e 100755 --- a/app/admin/RTMediaUploadTermsAdmin.php +++ b/app/admin/RTMediaUploadTermsAdmin.php @@ -66,7 +66,6 @@ public function enqueue_scripts_styles() { ); wp_localize_script( 'rtmedia-upload-terms-main', 'rtm_upload_terms_error_msgs', $translation_data ); - } /** diff --git a/app/helper/RTMediaActivityModel.php b/app/helper/RTMediaActivityModel.php index 3898e4ed2..63bd02c59 100644 --- a/app/helper/RTMediaActivityModel.php +++ b/app/helper/RTMediaActivityModel.php @@ -105,9 +105,8 @@ public function check( $activity_id = '' ) { if ( $results ) { return true; - } else { - return false; } + return false; } /** diff --git a/app/helper/RTMediaCommentNotification.php b/app/helper/RTMediaCommentNotification.php index 11a2c4869..4644388fa 100644 --- a/app/helper/RTMediaCommentNotification.php +++ b/app/helper/RTMediaCommentNotification.php @@ -100,10 +100,10 @@ public function format_comment_notifications( $params ) { esc_url( $return['link'] ), esc_html( $return['text'] ) ); - } else { - return $return; } + return $return; } + return ''; } /** @@ -143,7 +143,6 @@ public function remove_comment_notification( $comment_id ) { BP_Notifications_Notification::delete( array( 'id' => $comment_notification_id ) ); delete_comment_meta( $comment_id, 'comment_notification_id' ); } - } } diff --git a/app/helper/RTMediaInteractionModel.php b/app/helper/RTMediaInteractionModel.php index 73d207584..5f9ad440e 100644 --- a/app/helper/RTMediaInteractionModel.php +++ b/app/helper/RTMediaInteractionModel.php @@ -47,9 +47,8 @@ public function check( $user_id = '', $media_id = '', $action = '' ) { if ( $results ) { return true; - } else { - return false; } + return false; } /** diff --git a/app/helper/RTMediaLikeNotification.php b/app/helper/RTMediaLikeNotification.php index 9141da3c5..95e5e3a27 100644 --- a/app/helper/RTMediaLikeNotification.php +++ b/app/helper/RTMediaLikeNotification.php @@ -137,9 +137,8 @@ public function format_like_notifications( $params ) { esc_url( $return['link'] ), esc_html( $return['text'] ) ); - } else { - return $return; } + return $return; } } diff --git a/app/helper/RTMediaModel.php b/app/helper/RTMediaModel.php index 9a43a4dd4..4f32557ca 100755 --- a/app/helper/RTMediaModel.php +++ b/app/helper/RTMediaModel.php @@ -125,22 +125,22 @@ public function get( $columns, $offset = false, $per_page = false, $order_by = ' $allowed_order_columns = array( 'media_id', 'media_title', 'file_size', 'ratings_average', 'likes' ); // Define allowed columns. list( $order_column, $order_direction ) = explode( ' ', $order_by . ' ' ); // Default to space if no direction provided. - if ( ! in_array( strtolower( $order_column ), $allowed_order_columns ) || ! in_array( + if ( ! in_array( strtolower( $order_column ), $allowed_order_columns, true ) || ! in_array( strtolower( $order_direction ), array( 'asc', 'desc', '', - ) + ), + true ) ) { $order_by = 'media_id desc'; // Default order. } + $qorder_by = ''; if ( $order_by ) { $order_by = esc_sql( $order_by ); $qorder_by = " ORDER BY {$this->table_name}.{$order_by}"; - } else { - $qorder_by = ''; } // phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores @@ -459,10 +459,8 @@ public function get_other_album_count( $profile_id, $context = 'profile' ) { if ( 'profile' === $context ) { $sql .= $wpdb->prepare( ' AND media_author=%d ', $profile_id ); - } else { - if ( 'group' === $context ) { - $sql .= $wpdb->prepare( ' AND context_id=%d ', $profile_id ); - } + } elseif ( 'group' === $context ) { + $sql .= $wpdb->prepare( ' AND context_id=%d ', $profile_id ); } $sql .= 'limit 100'; @@ -470,9 +468,8 @@ public function get_other_album_count( $profile_id, $context = 'profile' ) { if ( isset( $result ) ) { return count( $result ); - } else { - return 0; } + return 0; } /** @@ -500,12 +497,9 @@ public function get_media_count() { $counts = $rtmedia_nav_obj->actual_counts( $bp->groups->current_group->id, 'group' ); $other_count = $this->get_other_album_count( $bp->groups->current_group->id, 'group' ); } - } else { - - if ( function_exists( 'bp_displayed_user_id' ) ) { - $counts = $rtmedia_nav_obj->actual_counts( bp_displayed_user_id(), 'profile' ); - $other_count = $this->get_other_album_count( bp_displayed_user_id(), 'profile' ); - } + } elseif ( function_exists( 'bp_displayed_user_id' ) ) { + $counts = $rtmedia_nav_obj->actual_counts( bp_displayed_user_id(), 'profile' ); + $other_count = $this->get_other_album_count( bp_displayed_user_id(), 'profile' ); } $remaining_all_media = ( ! empty( $counts['total']['all'] ) ) ? $counts['total']['all'] : 0; @@ -513,7 +507,7 @@ public function get_media_count() { $remaining_photos = ( ! empty( $counts['total']['photo'] ) ) ? $counts['total']['photo'] : 0; $remaining_videos = ( ! empty( $counts['total']['video'] ) ) ? $counts['total']['video'] : 0; $remaining_music = ( ! empty( $counts['total']['music'] ) ) ? $counts['total']['music'] : 0; - $remaining_docs = ( ! empty( $counts['total']['document'] ) ) ? $counts['total']['document'] : 0; + $remaining_docs = ( ! empty( $counts['total']['document'] ) ) ? $counts['total']['document'] : 0; } $media_counts = array( diff --git a/app/helper/rtDimensions.php b/app/helper/rtDimensions.php index 86ff9e8c2..77ce30382 100755 --- a/app/helper/rtDimensions.php +++ b/app/helper/rtDimensions.php @@ -50,7 +50,7 @@ private function get_default_id() { * @access private */ private function update_default_id() { - self::$id_count ++; + ++self::$id_count; } /** @@ -70,21 +70,22 @@ private function get_default_class() { * * @access private * - * @param string $element element. - * @param null $class class. + * @param string $element element. + * @param array|null $embedd_class class. * * @return string * * @throws rtFormsInvalidArgumentsException Form invalid argument exception. */ - private function embedd_class( $element, $class = null ) { + private function embedd_class( $element, $embedd_class = null ) { $html = 'class= "' . $this->get_default_class(); - if ( isset( $class ) ) { + if ( isset( $embedd_class ) ) { - if ( is_array( $class ) ) { - $html .= ' ' . implode( ' ', $class ); + if ( is_array( $embedd_class ) ) { + $html .= ' ' . implode( ' ', $embedd_class ); } else { + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped throw new rtFormsInvalidArgumentsException( 'class [' . $element . ']' ); } } @@ -192,5 +193,4 @@ public function get_dimensions( $attributes = '' ) { public function display_dimensions( $args = '' ) { echo wp_kses( $this->get_dimensions( $args ), RTMedia::expanded_allowed_tags() ); } - } diff --git a/app/helper/rtProgress.php b/app/helper/rtProgress.php index acf713b45..e9632e935 100755 --- a/app/helper/rtProgress.php +++ b/app/helper/rtProgress.php @@ -27,18 +27,18 @@ public function __construct() { * @access public * * @param float $progress Current progress. - * @param bool $echo Echo. + * @param bool $display Echo. * * @return string $progress_ui */ - public function progress_ui( $progress, $echo = true ) { + public function progress_ui( $progress, $display = true ) { $progress_ui = '
'; - if ( $echo ) { + if ( $display ) { echo wp_kses( $progress_ui, RTMedia::expanded_allowed_tags() ); } else { return $progress_ui; diff --git a/app/importers/BPMediaAlbumimporter.php b/app/importers/BPMediaAlbumimporter.php index 95b06867c..d0fd2a4ed 100755 --- a/app/importers/BPMediaAlbumimporter.php +++ b/app/importers/BPMediaAlbumimporter.php @@ -510,6 +510,7 @@ public static function bpmedia_ajax_import_step_favorites() { 'meta_key' => 'bp_favorite_activities', 'offset' => $offset, 'number' => 1, + 'fields' => 'ID', ) ); diff --git a/app/importers/BPMediaImporter.php b/app/importers/BPMediaImporter.php index e79f88fbb..d84c2ffdb 100755 --- a/app/importers/BPMediaImporter.php +++ b/app/importers/BPMediaImporter.php @@ -67,9 +67,8 @@ public static function _active( $path ) { // phpcs:ignore PSR2.Methods.MethodDec $plugins = get_plugins(); if ( array_key_exists( $path, $plugins ) ) { return 0; - } else { - return - 1; } + return - 1; } /** @@ -221,10 +220,8 @@ public static function delete( $path ) { } return rmdir( $path ); - } else { - if ( true === is_file( $path ) ) { - return unlink( $path ); - } + } elseif ( true === is_file( $path ) ) { + return unlink( $path ); } return false; diff --git a/app/importers/RTMediaActivityUpgrade.php b/app/importers/RTMediaActivityUpgrade.php index f2cc0efa9..719a09824 100644 --- a/app/importers/RTMediaActivityUpgrade.php +++ b/app/importers/RTMediaActivityUpgrade.php @@ -140,7 +140,6 @@ public function rtmedia_activity_upgrade( $lastid = 0, $limit = 1 ) { echo '0'; wp_die(); } - } /** @@ -199,11 +198,9 @@ public function add_rtmedia_media_activity_upgrade_notice() { /** * Get pending count. * - * @param bool|int $activity_id Activity id. - * * @return int */ - public function get_pending_count( $activity_id = false ) { + public function get_pending_count() { global $wpdb; $rtmedia_activity_model = new RTMediaActivityModel(); $rtmedia_model = new RTMediaModel(); diff --git a/app/importers/RTMediaMigration.php b/app/importers/RTMediaMigration.php index b80c1770e..c22ad1fcc 100755 --- a/app/importers/RTMediaMigration.php +++ b/app/importers/RTMediaMigration.php @@ -38,7 +38,7 @@ public function __construct() { wp_safe_redirect( esc_url_raw( $http_referer ) ); } if ( false !== rtmedia_get_site_option( 'rt_migration_hide_notice' ) ) { - return true; + return; } $force = filter_input( INPUT_GET, 'force', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); @@ -237,9 +237,8 @@ public function get_last_imported() { $row = $wpdb->get_row( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( $row ) { return $row->post_ID; - } else { - return false; } + return false; } /** @@ -367,10 +366,9 @@ public function manage_album() { $album_id = $wpdb->get_var( $wpdb->prepare( "select media_id from $this->bmp_table where id = %d", $album_rt_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared + $bp_prefix = ''; if ( function_exists( 'bp_core_get_table_prefix' ) ) { $bp_prefix = bp_core_get_table_prefix(); - } else { - $bp_prefix = ''; } if ( $stage < 1 ) { @@ -752,10 +750,9 @@ public function migrate_single_media( $result, $album = false ) { $blog_id = get_current_blog_id(); $old = $result; + $bp_prefix = ''; if ( function_exists( 'bp_core_get_table_prefix' ) ) { $bp_prefix = bp_core_get_table_prefix(); - } else { - $bp_prefix = ''; } global $wpdb; @@ -865,15 +862,13 @@ public function migrate_single_media( $result, $album = false ) { } } } + $album_id = 0; if ( 0 !== intval( $result->parent ) ) { $album_id = $this->migrate_single_media( $result->parent, true ); - } else { - $album_id = 0; } + $likes = 0; if ( function_exists( 'bp_activity_get_meta' ) ) { $likes = bp_activity_get_meta( $result->activity_id, 'favorite_count' ); - } else { - $likes = 0; } $wpdb->insert( @@ -897,12 +892,10 @@ public function migrate_single_media( $result, $album = false ) { $last_id = $wpdb->insert_id; if ( 'album' !== $media_type && ( function_exists( 'bp_core_get_user_domain' ) || function_exists( 'bp_members_get_user_url' ) ) && $activity_data ) { + $bp_prefix = ''; if ( function_exists( 'bp_core_get_table_prefix' ) ) { $bp_prefix = bp_core_get_table_prefix(); - } else { - $bp_prefix = ''; } - $activity_data->old_primary_link = $activity_data->primary_link; $parent_link = get_rtmedia_user_link( $activity_data->user_id ); $activity_data->primary_link = $parent_link . RTMEDIA_MEDIA_SLUG . '/' . $last_id; @@ -925,7 +918,7 @@ public function migrate_single_media( $result, $album = false ) { ), array( 'id' => $activity_data->id ) ); - } else if ( 'group' === $media_context ) { + } elseif ( 'group' === $media_context ) { $activity_data->old_primary_link = $activity_data->primary_link; $parent_link = get_rtmedia_group_link( abs( intval( $result->context_id ) ) ); $parent_link = trailingslashit( $parent_link ); @@ -1198,10 +1191,9 @@ public function importmedia( $id, $prefix ) { public function search_and_replace( $old, $new ) { global $wpdb; + $bp_prefix = $wpdb->prefix; if ( function_exists( 'bp_core_get_table_prefix' ) ) { $bp_prefix = bp_core_get_table_prefix(); - } else { - $bp_prefix = $wpdb->prefix; } $sql = $wpdb->prepare( "update {$bp_prefix}bp_activity set action=replace(action,%s,%s) ,content=replace(content,%s,%s), primary_link=replace(primary_link,%s,%s) where id > 0;", $old, $new, $old, $new, $old, $new ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared diff --git a/app/main/RTMedia.php b/app/main/RTMedia.php index ccc1809ed..c970a162c 100755 --- a/app/main/RTMedia.php +++ b/app/main/RTMedia.php @@ -2048,7 +2048,7 @@ function get_rtmedia_permalink( $id ) { // check for global album. $parent_link = parentlink_global_album( $id ); if ( '' === $parent_link && isset( $media[0]->media_author ) ) { - $parent_link = get_rtmedia_user_link( $media[0]->media_author ); + $parent_link = get_rtmedia_user_link( $media[0]->media_author ); } } @@ -2083,7 +2083,7 @@ function get_rtmedia_user_link( $id ) { if ( function_exists( 'bp_members_get_user_url' ) ) { return bp_members_get_user_url( $id ); - } else if ( function_exists( 'bp_core_get_user_domain' ) ) { + } elseif ( function_exists( 'bp_core_get_user_domain' ) ) { return bp_core_get_user_domain( $id ); } @@ -2101,9 +2101,8 @@ function get_rtmedia_user_link( $id ) { function rtmedia_update_site_option( $option_name, $option_value ) { if ( is_multisite() ) { return update_option( $option_name, $option_value ); - } else { - return update_site_option( $option_name, $option_value ); } + return update_site_option( $option_name, $option_value ); } /** @@ -2167,7 +2166,7 @@ function rtmedia_get_site_option( $option_name, $default = false ) { * Function to show privacy message provided from rtMedia settings in front end. */ function rtm_privacy_message_on_website() { - global $rtmedia; + global $rtmedia; $options = $rtmedia->options; $rtm_privacy_message_options = array( diff --git a/app/main/controllers/template/rtmedia-actions.php b/app/main/controllers/template/rtmedia-actions.php index 8bdb8833f..80cef24f2 100644 --- a/app/main/controllers/template/rtmedia-actions.php +++ b/app/main/controllers/template/rtmedia-actions.php @@ -525,7 +525,6 @@ function rtmedia_add_media_delete_nonce_shortcode() { if ( isset( $rtmedia_query->is_gallery_shortcode ) && true === $rtmedia_query->is_gallery_shortcode ) { wp_nonce_field( 'rtmedia_' . get_current_user_id(), 'rtmedia_media_delete_nonce' ); } - } add_action( 'rtmedia_pre_template', 'rtmedia_add_media_delete_nonce_shortcode' ); @@ -989,9 +988,8 @@ function rtmedia_set_permalink() { function rtmedia_override_canonical( $redirect_url, $requested_url ) { if ( is_front_page() && get_query_var( 'pg' ) ) { return $requested_url; - } else { - return $redirect_url; } + return $redirect_url; } add_filter( 'redirect_canonical', 'rtmedia_override_canonical', 10, 2 ); diff --git a/app/main/routers/query/RTMediaQuery.php b/app/main/routers/query/RTMediaQuery.php index aa7858652..58b208eb5 100755 --- a/app/main/routers/query/RTMediaQuery.php +++ b/app/main/routers/query/RTMediaQuery.php @@ -1174,7 +1174,7 @@ public function rtmedia() { * @return array */ public function next_media() { - $this->current_media++; + ++$this->current_media; $this->rtmedia = $this->media[ $this->current_media ]; @@ -1192,7 +1192,7 @@ public function permalink() { if ( function_exists( 'bp_members_get_user_url' ) ) { $parent_link = bp_members_get_user_url( $rtmedia_media->media_author ); - } else if ( function_exists( 'bp_core_get_user_domain' ) ) { + } elseif ( function_exists( 'bp_core_get_user_domain' ) ) { $parent_link = bp_core_get_user_domain( $rtmedia_media->media_author ); } else { $parent_link = get_author_posts_url( $rtmedia_media->media_author );