PluginProbe
Responsive Menu – Mobile Menu & Hamburger Menu Builder / 4.7.4
Responsive Menu – Mobile Menu & Hamburger Menu Builder v4.7.4
4.7.4 4.7.3 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.10 4.1.11 4.1.12 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 All 91 releases
responsive-menu / v4.0.0 / inc / classes / class-preview.php

class-preview.php in Responsive Menu – Mobile Menu & Hamburger Menu Builder 4.7.4, at v4.0.0/inc/classes/class-preview.php

106 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Preview class.
4 * This class is responsible for preview related functionality.
5 *
6 * @version 4.0.0
7 * @author Expresstech System
8 *
9 * @package responsive-menu
10 */
11
12 namespace RMP\Features\Inc;
13
14 use RMP\Features\Inc\Traits\Singleton;
15 use RMP\Features\Inc\RMP_Menu;
16
17 // Disable the direct access to this class.
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * Class Preview
24 */
25 class Preview {
26
27 use Singleton;
28
29 /**
30 * Construct method.
31 */
32 protected function __construct() {
33 $this->setup_hooks();
34 }
35
36 /**
37 * To setup action/filter.
38 *
39 * @version 4.0.0
40 *
41 * @return void
42 */
43 protected function setup_hooks() {
44 add_action( 'wp_ajax_rmp_enable_menu_item', array( $this, 'enable_menu_item' ) );
45 }
46
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 /**
67 * This function get the content of menu item for live preview element.
68 *
69 * @return HTML
70 */
71 public function enable_menu_item() {
72 check_ajax_referer( 'rmp_nonce', 'ajax_nonce' );
73
74 $menu_id = isset( $_POST['menu_id'] ) ? absint( wp_unslash( $_POST['menu_id'] ) ) : 0;
75
76 if ( empty( $menu_id ) ) {
77 wp_send_json_error( array( 'message' => esc_html__( 'Menu ID missing !', 'responsive-menu' ) ) );
78 }
79
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' ) ) );
89 }
90
91 $menu = new RMP_Menu( $menu_id );
92 $method = self::PREVIEW_ELEMENTS[ $menu_element ];
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 ) );
104 }
105 }
106