| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Preview class. |
| 4 | 4 | * This class is responsible for preview related functionality. |
| 5 | - * | |
| 5 | + * | |
| 6 | 6 | * @version 4.0.0 |
| 7 | 7 | * @author Expresstech System |
| 8 | - * | |
| 9 | - * @package responsive-menu-pro | |
| 8 | + * | |
| 9 | + * @package responsive-menu | |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace RMP\Features\Inc; |
| 13 | 13 | |
| @@ -15,9 +15,9 @@ | ||
| 15 | 15 | use RMP\Features\Inc\RMP_Menu; |
| 16 | 16 | |
| 17 | 17 | // Disable the direct access to this class. |
| 18 | 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | - exit; | |
| 19 | + exit; | |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * Class Preview |
| @@ -36,39 +36,70 @@ | ||
| 36 | 36 | /** |
| 37 | 37 | * To setup action/filter. |
| 38 | 38 | * |
| 39 | 39 | * @version 4.0.0 |
| 40 | - * | |
| 40 | + * | |
| 41 | 41 | * @return void |
| 42 | 42 | */ |
| 43 | 43 | protected function setup_hooks() { |
| 44 | - add_action('wp_ajax_rmp_enable_menu_item', array( $this, 'enable_menu_item' ) ); | |
| 44 | + add_action( 'wp_ajax_rmp_enable_menu_item', array( $this, 'enable_menu_item' ) ); | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | + * Menu elements the live preview can render, mapped to the RMP_Menu | |
| 49 | + * method that renders each one. | |
| 50 | + * | |
| 51 | + * The keys match the `data-toggle` values on the item-order checkboxes in | |
| 52 | + * templates/menu-elements/. | |
| 53 | + * | |
| 54 | + * @since 4.7.4 | |
| 55 | + * | |
| 56 | + * @var array | |
| 57 | + */ | |
| 58 | + const PREVIEW_ELEMENTS = array( | |
| 59 | + 'menu' => 'menu', | |
| 60 | + 'search' => 'menu_search_box', | |
| 61 | + 'title' => 'menu_title', | |
| 62 | + 'social-icons' => 'menu_social_icons', | |
| 63 | + 'additional-content' => 'menu_additional_content', | |
| 64 | + ); | |
| 65 | + | |
| 66 | + /** | |
| 48 | 67 | * This function get the content of menu item for live preview element. |
| 49 | - * | |
| 68 | + * | |
| 50 | 69 | * @return HTML |
| 51 | 70 | */ |
| 52 | 71 | public function enable_menu_item() { |
| 72 | + check_ajax_referer( 'rmp_nonce', 'ajax_nonce' ); | |
| 53 | 73 | |
| 54 | - check_ajax_referer( 'rmp_nonce', 'ajax_nonce' ); | |
| 74 | + $menu_id = isset( $_POST['menu_id'] ) ? absint( wp_unslash( $_POST['menu_id'] ) ) : 0; | |
| 55 | 75 | |
| 56 | - $menu_id = $_POST['menu_id']; | |
| 57 | - $menu_element = $_POST['menu_element']; | |
| 58 | - $menu = new RMP_Menu($menu_id); | |
| 76 | + if ( empty( $menu_id ) ) { | |
| 77 | + wp_send_json_error( array( 'message' => esc_html__( 'Menu ID missing !', 'responsive-menu' ) ) ); | |
| 78 | + } | |
| 59 | 79 | |
| 60 | - if ( $menu_element === 'menu' ) { | |
| 61 | - $html = $menu->menu(); | |
| 62 | - } elseif ( 'search' === $menu_element ) { | |
| 63 | - $html = $menu->menu_search_box(); | |
| 64 | - } elseif( 'title' === $menu_element ) { | |
| 65 | - $html = $menu->menu_title(); | |
| 66 | - } else { | |
| 67 | - $html = $menu->menu_additional_content(); | |
| 80 | + /** | |
| 81 | + * The element name is a slug, not a number — intval() collapsed every | |
| 82 | + * value except 'menu' to 0, so no branch below ever matched and the | |
| 83 | + * preview always rendered the additional-content element. | |
| 84 | + */ | |
| 85 | + $menu_element = isset( $_POST['menu_element'] ) ? sanitize_key( wp_unslash( $_POST['menu_element'] ) ) : ''; | |
| 86 | + | |
| 87 | + if ( ! isset( self::PREVIEW_ELEMENTS[ $menu_element ] ) ) { | |
| 88 | + wp_send_json_error( array( 'message' => esc_html__( 'Unknown menu element !', 'responsive-menu' ) ) ); | |
| 68 | 89 | } |
| 69 | 90 | |
| 70 | - wp_send_json_success( [ 'markup' => $html ] ); | |
| 91 | + $menu = new RMP_Menu( $menu_id ); | |
| 92 | + $method = self::PREVIEW_ELEMENTS[ $menu_element ]; | |
| 71 | 93 | |
| 94 | + /** | |
| 95 | + * Every render method echoes its markup and returns nothing, so the | |
| 96 | + * output has to be captured — otherwise it is emitted before the JSON | |
| 97 | + * body and the response is not parseable. | |
| 98 | + */ | |
| 99 | + ob_start(); | |
| 100 | + $menu->$method(); | |
| 101 | + $html = ob_get_clean(); | |
| 102 | + | |
| 103 | + wp_send_json_success( array( 'markup' => $html ) ); | |
| 72 | 104 | } |
| 73 | - | |
| 74 | 105 | } |