From 842b59ad53963697f556742d79c384734fffbc42 Mon Sep 17 00:00:00 2001 From: Akshay Urankar <101685739+akshayurankar48@users.noreply.github.com> Date: Fri, 6 Mar 2026 15:54:06 +0530 Subject: [PATCH] refactor: clean up legacy code in index.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Extract duplicate enqueue logic from post_enqueue() and post_new_enqueue() into a shared private enqueue_post_editor_scripts() helper - Remove dead farbtastic fallback in bsf_color_scripts() — plugin requires WP 3.7+ so the WP < 3.5 branch was unreachable - Replace bare die() with wp_die() in submit_request() and submit_color() - Fix esc_html_e() misuse in ternary expressions — esc_html_e() returns null so the ternary was always truthy; restructured as if/else Closes #235 --- index.php | 83 +++++++++++++++++-------------------------------------- 1 file changed, 26 insertions(+), 57 deletions(-) diff --git a/index.php b/index.php index 6450f82..f9c0fe4 100644 --- a/index.php +++ b/index.php @@ -129,51 +129,31 @@ public function bsf_settings_link( $links ) { return $links; } /** - * Print the star rating style on post edit page. + * Enqueue post editor scripts and styles on post.php. * * @param string $hook Hook. */ public function post_enqueue( $hook ) { - if ( 'post.php' != $hook ) { + if ( 'post.php' !== $hook ) { return; } - $current_admin_screen = get_current_screen(); - - // Default exclusions for WooCommerce and other problematic post types. - $default_exclusions = array( 'product', 'shop_order', 'shop_coupon', 'product_variation' ); - $exclude_custom_post_type = apply_filters( 'bsf_exclude_custom_post_type', $default_exclusions ); - - if ( in_array( $current_admin_screen->post_type, $exclude_custom_post_type ) ) { - return; - } - - // Additional check for WooCommerce products to prevent conflicts. - if ( 'product' === $current_admin_screen->post_type ) { - return; - } - wp_enqueue_script( 'jquery' ); - wp_enqueue_script( 'bsf_jquery_star' ); - wp_enqueue_script( 'bsf_toggle' ); - wp_enqueue_style( 'star_style' ); - wp_register_script( 'bsf-scripts', BSF_META_BOX_URL . 'js/cmb.js', '', '0.9.1' ); - wp_enqueue_script( 'bsf-scripts' ); - wp_enqueue_media(); - wp_register_script( 'bsf-scripts-media', BSF_META_BOX_URL . 'js/media.js', array( 'jquery' ), '1.0' ); - wp_enqueue_script( 'bsf-scripts-media' ); - wp_enqueue_script( 'jquery-ui-datepicker' ); - if ( ! function_exists( 'vc_map' ) ) { - wp_enqueue_style( 'jquery-style', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css', null, '1.0' ); - } + $this->enqueue_post_editor_scripts(); } /** - * Post_new_enqueue. + * Enqueue post editor scripts and styles on post-new.php. * * @param string $hook Hook. */ public function post_new_enqueue( $hook ) { - if ( 'post-new.php' != $hook ) { + if ( 'post-new.php' !== $hook ) { return; } + $this->enqueue_post_editor_scripts(); + } + /** + * Shared helper: enqueue scripts and styles for the post editor screens. + */ + private function enqueue_post_editor_scripts() { $current_admin_screen = get_current_screen(); // Default exclusions for WooCommerce and other problematic post types. @@ -199,7 +179,7 @@ public function post_new_enqueue( $hook ) { wp_enqueue_script( 'bsf-scripts-media' ); wp_enqueue_script( 'jquery-ui-datepicker' ); if ( ! function_exists( 'vc_map' ) ) { - wp_enqueue_style( 'jquery-style', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css', null, 1.0 ); + wp_enqueue_style( 'jquery-style', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css', null, '1.0' ); } } /** @@ -361,9 +341,12 @@ public function submit_request() { $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From:' . $name . '<' . $from . '>' . "\r\n"; $result = wp_mail( $to, $subject, wp_kses_post( $html ), $headers ); - echo $result ? esc_html_e( 'Thank you!', 'rich-snippets' ) : esc_html_e( 'Something went wrong!', 'rich-snippets' ); - - die(); + if ( $result ) { + esc_html_e( 'Thank you!', 'rich-snippets' ); + } else { + esc_html_e( 'Something went wrong!', 'rich-snippets' ); + } + wp_die(); } /** * Submit_color. @@ -390,9 +373,12 @@ public function submit_color() { 'snippet_title_color' => $title_color, 'snippet_box_color' => $box_color, ); - echo update_option( 'bsf_custom', $color_opt ) ? esc_html_e( 'Settings saved !', 'rich-snippets' ) : esc_html_e( 'Error occured. Settings were not saved !', 'rich-snippets' ); - - die(); + if ( update_option( 'bsf_custom', $color_opt ) ) { + esc_html_e( 'Settings saved !', 'rich-snippets' ); + } else { + esc_html_e( 'Error occured. Settings were not saved !', 'rich-snippets' ); + } + wp_die(); } } } @@ -409,25 +395,8 @@ public function iris_enqueue_scripts() { * Bsf_color_scripts. */ public function bsf_color_scripts() { - global $wp_version; - $bsf_script_array = array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'media-upload', 'thickbox' ); - - // styles required for cmb. - $bsf_style_array = array( 'thickbox' ); - - // if we're 3.5 or later, user wp-color-picker. - if ( 3.5 <= $wp_version ) { - - $bsf_script_array[] = 'wp-color-picker'; - $bsf_style_array[] = 'wp-color-picker'; - - } else { - - // otherwise use the older 'farbtastic'. - $bsf_script_array[] = 'farbtastic'; - $bsf_style_array[] = 'farbtastic'; - - } + $bsf_script_array = array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'media-upload', 'thickbox', 'wp-color-picker' ); + $bsf_style_array = array( 'thickbox', 'wp-color-picker' ); } /**